1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * TI Camera Access Layer (CAL)
5 * Copyright (c) 2015-2020 Texas Instruments Inc.
8 * Benoit Parrot <bparrot@ti.com>
9 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
14 #include <linux/bitfield.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/spinlock.h>
19 #include <linux/videodev2.h>
21 #include <media/media-device.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-dev.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/videobuf2-v4l2.h>
29 #define CAL_MODULE_NAME "cal"
30 #define CAL_NUM_CONTEXT 2
31 #define CAL_NUM_CSI2_PORTS 2
33 #define MAX_WIDTH_BYTES (8192 * 8)
34 #define MAX_HEIGHT_LINES 16383
43 /* CTRL_CORE_CAMERRX_CONTROL register field id */
44 enum cal_camerarx_field {
59 /* buffer for one video frame */
61 /* common v4l buffer stuff -- must be first */
62 struct vb2_v4l2_buffer vb;
63 struct list_head list;
67 struct list_head active;
70 struct cal_camerarx_data {
74 } fields[F_MAX_FIELDS];
75 unsigned int num_lanes;
79 const struct cal_camerarx_data *camerarx;
80 unsigned int num_csi2_phy;
85 * The Camera Adaptation Layer (CAL) module is paired with one or more complex
86 * I/O PHYs (CAMERARX). It contains multiple instances of CSI-2, processing and
89 * The cal_dev structure represents the whole subsystem, including the CAL and
90 * the CAMERARX instances. Instances of struct cal_dev are named cal through the
93 * The cal_camerarx structure represents one CAMERARX instance. Instances of
94 * cal_camerarx are named phy through the driver.
96 * The cal_ctx structure represents the combination of one CSI-2 context, one
97 * processing context and one DMA context. Instance of struct cal_ctx are named
98 * ctx through the driver.
101 struct cal_camerarx {
103 struct resource *res;
105 struct regmap_field *fields[F_MAX_FIELDS];
108 unsigned int instance;
110 struct v4l2_fwnode_endpoint endpoint;
111 struct device_node *sensor_node;
112 struct v4l2_subdev *sensor;
119 struct resource *res;
122 const struct cal_data *data;
125 /* Control Module handle */
126 struct regmap *syscon_camerrx;
127 u32 syscon_camerrx_offset;
129 /* Camera Core Module handle */
130 struct cal_camerarx *phy[CAL_NUM_CSI2_PORTS];
132 struct cal_ctx *ctx[CAL_NUM_CONTEXT];
134 struct media_device mdev;
135 struct v4l2_device v4l2_dev;
136 struct v4l2_async_notifier notifier;
140 * There is one cal_ctx structure for each camera core context.
143 struct v4l2_ctrl_handler ctrl_handler;
144 struct video_device vdev;
145 struct media_pad pad;
148 struct cal_camerarx *phy;
150 /* v4l2_ioctl mutex */
152 /* v4l2 buffers lock */
155 struct cal_dmaqueue vidq;
158 const struct cal_fmt *fmt;
159 /* Used to store current pixel format */
160 struct v4l2_format v_fmt;
161 /* Used to store current mbus frame format */
162 struct v4l2_mbus_framefmt m_fmt;
164 /* Current subdev enumerated format */
165 const struct cal_fmt **active_fmt;
166 unsigned int num_active_fmt;
168 unsigned int sequence;
169 struct vb2_queue vb_vidq;
173 /* Pointer pointing to current v4l2_buffer */
174 struct cal_buffer *cur_frm;
175 /* Pointer pointing to next v4l2_buffer */
176 struct cal_buffer *next_frm;
181 extern unsigned int cal_debug;
182 extern int cal_video_nr;
184 #define cal_dbg(level, cal, fmt, arg...) \
186 if (cal_debug >= (level)) \
187 dev_printk(KERN_DEBUG, (cal)->dev, fmt, ##arg); \
189 #define cal_info(cal, fmt, arg...) \
190 dev_info((cal)->dev, fmt, ##arg)
191 #define cal_err(cal, fmt, arg...) \
192 dev_err((cal)->dev, fmt, ##arg)
194 #define ctx_dbg(level, ctx, fmt, arg...) \
195 cal_dbg(level, (ctx)->cal, "ctx%u: " fmt, (ctx)->index, ##arg)
196 #define ctx_info(ctx, fmt, arg...) \
197 cal_info((ctx)->cal, "ctx%u: " fmt, (ctx)->index, ##arg)
198 #define ctx_err(ctx, fmt, arg...) \
199 cal_err((ctx)->cal, "ctx%u: " fmt, (ctx)->index, ##arg)
201 #define phy_dbg(level, phy, fmt, arg...) \
202 cal_dbg(level, (phy)->cal, "phy%u: " fmt, (phy)->instance, ##arg)
203 #define phy_info(phy, fmt, arg...) \
204 cal_info((phy)->cal, "phy%u: " fmt, (phy)->instance, ##arg)
205 #define phy_err(phy, fmt, arg...) \
206 cal_err((phy)->cal, "phy%u: " fmt, (phy)->instance, ##arg)
208 static inline u32 cal_read(struct cal_dev *cal, u32 offset)
210 return ioread32(cal->base + offset);
213 static inline void cal_write(struct cal_dev *cal, u32 offset, u32 val)
215 iowrite32(val, cal->base + offset);
218 static inline u32 cal_read_field(struct cal_dev *cal, u32 offset, u32 mask)
220 return FIELD_GET(mask, cal_read(cal, offset));
223 static inline void cal_write_field(struct cal_dev *cal, u32 offset, u32 value,
226 u32 val = cal_read(cal, offset);
229 val |= FIELD_PREP(mask, value);
230 cal_write(cal, offset, val);
233 static inline void cal_set_field(u32 *valp, u32 field, u32 mask)
238 val |= (field << __ffs(mask)) & mask;
242 void cal_quickdump_regs(struct cal_dev *cal);
244 void cal_camerarx_disable(struct cal_camerarx *phy);
245 int cal_camerarx_start(struct cal_camerarx *phy, const struct cal_fmt *fmt);
246 void cal_camerarx_stop(struct cal_camerarx *phy);
247 void cal_camerarx_enable_irqs(struct cal_camerarx *phy);
248 void cal_camerarx_disable_irqs(struct cal_camerarx *phy);
249 void cal_camerarx_ppi_enable(struct cal_camerarx *phy);
250 void cal_camerarx_ppi_disable(struct cal_camerarx *phy);
251 void cal_camerarx_i913_errata(struct cal_camerarx *phy);
252 struct cal_camerarx *cal_camerarx_create(struct cal_dev *cal,
253 unsigned int instance);
254 void cal_camerarx_destroy(struct cal_camerarx *phy);
256 void cal_ctx_csi2_config(struct cal_ctx *ctx);
257 void cal_ctx_pix_proc_config(struct cal_ctx *ctx);
258 void cal_ctx_wr_dma_config(struct cal_ctx *ctx, unsigned int width,
259 unsigned int height);
260 void cal_ctx_wr_dma_addr(struct cal_ctx *ctx, unsigned int dmaaddr);
262 int cal_ctx_v4l2_register(struct cal_ctx *ctx);
263 void cal_ctx_v4l2_unregister(struct cal_ctx *ctx);
264 int cal_ctx_v4l2_init(struct cal_ctx *ctx);
265 void cal_ctx_v4l2_cleanup(struct cal_ctx *ctx);
267 #endif /* __TI_CAL_H__ */