1 // SPDX-License-Identifier: GPL-2.0
3 * Support for Medifield PNW Camera Imaging ISP subsystem.
5 * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-mediabus.h>
21 #include "atomisp_cmd.h"
22 #include "atomisp_internal.h"
23 #include "atomisp-regs.h"
25 static struct v4l2_mbus_framefmt *__csi2_get_format(struct
26 atomisp_mipi_csi2_device
29 v4l2_subdev_pad_config *cfg,
31 v4l2_subdev_format_whence
32 which, unsigned int pad) {
33 if (which == V4L2_SUBDEV_FORMAT_TRY)
34 return v4l2_subdev_get_try_format(&csi2->subdev, cfg, pad);
36 return &csi2->formats[pad];
40 * csi2_enum_mbus_code - Handle pixel format enumeration
41 * @sd : pointer to v4l2 subdev structure
42 * @fh : V4L2 subdev file handle
43 * @code : pointer to v4l2_subdev_pad_mbus_code_enum structure
44 * return -EINVAL or zero on success
46 static int csi2_enum_mbus_code(struct v4l2_subdev *sd,
47 struct v4l2_subdev_pad_config *cfg,
48 struct v4l2_subdev_mbus_code_enum *code)
50 const struct atomisp_in_fmt_conv *ic = atomisp_in_fmt_conv;
54 if (i == code->index) {
55 code->code = ic->code;
65 * csi2_get_format - Handle get format by pads subdev method
66 * @sd : pointer to v4l2 subdev structure
67 * @fh : V4L2 subdev file handle
69 * @fmt: pointer to v4l2 format structure
70 * return -EINVAL or zero on success
72 static int csi2_get_format(struct v4l2_subdev *sd,
73 struct v4l2_subdev_pad_config *cfg,
74 struct v4l2_subdev_format *fmt)
76 struct atomisp_mipi_csi2_device *csi2 = v4l2_get_subdevdata(sd);
77 struct v4l2_mbus_framefmt *format;
79 format = __csi2_get_format(csi2, cfg, fmt->which, fmt->pad);
81 fmt->format = *format;
86 int atomisp_csi2_set_ffmt(struct v4l2_subdev *sd,
87 struct v4l2_subdev_pad_config *cfg,
88 unsigned int which, uint16_t pad,
89 struct v4l2_mbus_framefmt *ffmt)
91 struct atomisp_mipi_csi2_device *csi2 = v4l2_get_subdevdata(sd);
92 struct v4l2_mbus_framefmt *actual_ffmt = __csi2_get_format(csi2, cfg, which, pad);
94 if (pad == CSI2_PAD_SINK) {
95 const struct atomisp_in_fmt_conv *ic;
96 struct v4l2_mbus_framefmt tmp_ffmt;
98 ic = atomisp_find_in_fmt_conv(ffmt->code);
100 actual_ffmt->code = ic->code;
102 actual_ffmt->code = atomisp_in_fmt_conv[0].code;
104 actual_ffmt->width = clamp_t(
105 u32, ffmt->width, ATOM_ISP_MIN_WIDTH,
107 actual_ffmt->height = clamp_t(
108 u32, ffmt->height, ATOM_ISP_MIN_HEIGHT,
109 ATOM_ISP_MAX_HEIGHT);
111 tmp_ffmt = *ffmt = *actual_ffmt;
113 return atomisp_csi2_set_ffmt(sd, cfg, which, CSI2_PAD_SOURCE,
117 /* FIXME: DPCM decompression */
118 *actual_ffmt = *ffmt = *__csi2_get_format(csi2, cfg, which, CSI2_PAD_SINK);
124 * csi2_set_format - Handle set format by pads subdev method
125 * @sd : pointer to v4l2 subdev structure
126 * @fh : V4L2 subdev file handle
128 * @fmt: pointer to v4l2 format structure
129 * return -EINVAL or zero on success
131 static int csi2_set_format(struct v4l2_subdev *sd,
132 struct v4l2_subdev_pad_config *cfg,
133 struct v4l2_subdev_format *fmt)
135 return atomisp_csi2_set_ffmt(sd, cfg, fmt->which, fmt->pad,
140 * csi2_set_stream - Enable/Disable streaming on the CSI2 module
141 * @sd: ISP CSI2 V4L2 subdevice
142 * @enable: Enable/disable stream (1/0)
144 * Return 0 on success or a negative error code otherwise.
146 static int csi2_set_stream(struct v4l2_subdev *sd, int enable)
151 /* subdev core operations */
152 static const struct v4l2_subdev_core_ops csi2_core_ops = {
155 /* subdev video operations */
156 static const struct v4l2_subdev_video_ops csi2_video_ops = {
157 .s_stream = csi2_set_stream,
160 /* subdev pad operations */
161 static const struct v4l2_subdev_pad_ops csi2_pad_ops = {
162 .enum_mbus_code = csi2_enum_mbus_code,
163 .get_fmt = csi2_get_format,
164 .set_fmt = csi2_set_format,
165 .link_validate = v4l2_subdev_link_validate_default,
168 /* subdev operations */
169 static const struct v4l2_subdev_ops csi2_ops = {
170 .core = &csi2_core_ops,
171 .video = &csi2_video_ops,
172 .pad = &csi2_pad_ops,
176 * csi2_link_setup - Setup CSI2 connections.
177 * @entity : Pointer to media entity structure
178 * @local : Pointer to local pad array
179 * @remote : Pointer to remote pad array
180 * @flags : Link flags
181 * return -EINVAL or zero on success
183 static int csi2_link_setup(struct media_entity *entity,
184 const struct media_pad *local,
185 const struct media_pad *remote, u32 flags)
187 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
188 struct atomisp_mipi_csi2_device *csi2 = v4l2_get_subdevdata(sd);
189 u32 result = local->index | is_media_entity_v4l2_subdev(remote->entity);
192 case CSI2_PAD_SOURCE | MEDIA_ENT_F_OLD_BASE:
193 /* not supported yet */
196 case CSI2_PAD_SOURCE | MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
197 if (flags & MEDIA_LNK_FL_ENABLED) {
198 if (csi2->output & ~CSI2_OUTPUT_ISP_SUBDEV)
200 csi2->output |= CSI2_OUTPUT_ISP_SUBDEV;
202 csi2->output &= ~CSI2_OUTPUT_ISP_SUBDEV;
207 /* Link from camera to CSI2 is fixed... */
213 /* media operations */
214 static const struct media_entity_operations csi2_media_ops = {
215 .link_setup = csi2_link_setup,
216 .link_validate = v4l2_subdev_link_validate,
220 * ispcsi2_init_entities - Initialize subdev and media entity.
221 * @csi2: Pointer to ispcsi2 structure.
222 * return -ENOMEM or zero on success
224 static int mipi_csi2_init_entities(struct atomisp_mipi_csi2_device *csi2,
227 struct v4l2_subdev *sd = &csi2->subdev;
228 struct media_pad *pads = csi2->pads;
229 struct media_entity *me = &sd->entity;
232 v4l2_subdev_init(sd, &csi2_ops);
233 snprintf(sd->name, sizeof(sd->name), "ATOM ISP CSI2-port%d", port);
235 v4l2_set_subdevdata(sd, csi2);
236 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
238 pads[CSI2_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
239 pads[CSI2_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
241 me->ops = &csi2_media_ops;
242 me->function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
243 ret = media_entity_pads_init(me, CSI2_PADS_NUM, pads);
247 csi2->formats[CSI2_PAD_SINK].code =
248 csi2->formats[CSI2_PAD_SOURCE].code =
249 atomisp_in_fmt_conv[0].code;
255 atomisp_mipi_csi2_unregister_entities(struct atomisp_mipi_csi2_device *csi2)
257 media_entity_cleanup(&csi2->subdev.entity);
258 v4l2_device_unregister_subdev(&csi2->subdev);
261 int atomisp_mipi_csi2_register_entities(struct atomisp_mipi_csi2_device *csi2,
262 struct v4l2_device *vdev)
266 /* Register the subdev and video nodes. */
267 ret = v4l2_device_register_subdev(vdev, &csi2->subdev);
274 atomisp_mipi_csi2_unregister_entities(csi2);
278 static const int LIMIT_SHIFT = 6; /* Limit numeric range into 31 bits */
281 atomisp_csi2_configure_calc(const short int coeffs[2], int mipi_freq, int def)
283 /* Delay counter accuracy, 1/0.0625 for ANN/CHT, 1/0.125 for BXT */
284 static const int accinv = 16; /* 1 / COUNT_ACC */
287 if (mipi_freq >> LIMIT_SHIFT <= 0)
290 r = accinv * coeffs[1] * (500000000 >> LIMIT_SHIFT);
291 r /= mipi_freq >> LIMIT_SHIFT;
292 r += accinv * coeffs[0];
297 static void atomisp_csi2_configure_isp2401(struct atomisp_sub_device *asd)
300 * The ISP2401 new input system CSI2+ receiver has several
301 * parameters affecting the receiver timings. These depend
302 * on the MIPI bus frequency F in Hz (sensor transmitter rate)
304 * register value = (A/1e9 + B * UI) / COUNT_ACC
306 * UI = 1 / (2 * F) in seconds
307 * COUNT_ACC = counter accuracy in seconds
308 * For ANN and CHV, COUNT_ACC = 0.0625 ns
309 * For BXT, COUNT_ACC = 0.125 ns
310 * A and B are coefficients from the table below,
311 * depending whether the register minimum or maximum value is
315 * reg_rx_csi_dly_cnt_termen_clane 0 0 38 0
316 * reg_rx_csi_dly_cnt_settle_clane 95 -8 300 -16
318 * reg_rx_csi_dly_cnt_termen_dlane0 0 0 35 4
319 * reg_rx_csi_dly_cnt_settle_dlane0 85 -2 145 -6
320 * reg_rx_csi_dly_cnt_termen_dlane1 0 0 35 4
321 * reg_rx_csi_dly_cnt_settle_dlane1 85 -2 145 -6
322 * reg_rx_csi_dly_cnt_termen_dlane2 0 0 35 4
323 * reg_rx_csi_dly_cnt_settle_dlane2 85 -2 145 -6
324 * reg_rx_csi_dly_cnt_termen_dlane3 0 0 35 4
325 * reg_rx_csi_dly_cnt_settle_dlane3 85 -2 145 -6
327 * We use the minimum values in the calculations below.
329 static const short int coeff_clk_termen[] = { 0, 0 };
330 static const short int coeff_clk_settle[] = { 95, -8 };
331 static const short int coeff_dat_termen[] = { 0, 0 };
332 static const short int coeff_dat_settle[] = { 85, -2 };
333 static const int TERMEN_DEFAULT = 0 * 0;
334 static const int SETTLE_DEFAULT = 0x480;
336 static const hrt_address csi2_port_base[] = {
337 [ATOMISP_CAMERA_PORT_PRIMARY] = CSI2_PORT_A_BASE,
338 [ATOMISP_CAMERA_PORT_SECONDARY] = CSI2_PORT_B_BASE,
339 [ATOMISP_CAMERA_PORT_TERTIARY] = CSI2_PORT_C_BASE,
341 /* Number of lanes on each port, excluding clock lane */
342 static const unsigned char csi2_port_lanes[] = {
343 [ATOMISP_CAMERA_PORT_PRIMARY] = 4,
344 [ATOMISP_CAMERA_PORT_SECONDARY] = 2,
345 [ATOMISP_CAMERA_PORT_TERTIARY] = 2,
347 static const hrt_address csi2_lane_base[] = {
360 struct v4l2_control ctrl;
361 struct atomisp_device *isp = asd->isp;
362 struct camera_mipi_info *mipi_info;
364 enum atomisp_camera_port port;
368 mipi_info = atomisp_to_sensor_mipi_info(
369 isp->inputs[asd->input_curr].camera);
370 port = mipi_info->port;
372 ctrl.id = V4L2_CID_LINK_FREQ;
374 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
375 mipi_freq = ctrl.value;
377 clk_termen = atomisp_csi2_configure_calc(coeff_clk_termen,
378 mipi_freq, TERMEN_DEFAULT);
379 clk_settle = atomisp_csi2_configure_calc(coeff_clk_settle,
380 mipi_freq, SETTLE_DEFAULT);
381 dat_termen = atomisp_csi2_configure_calc(coeff_dat_termen,
382 mipi_freq, TERMEN_DEFAULT);
383 dat_settle = atomisp_csi2_configure_calc(coeff_dat_settle,
384 mipi_freq, SETTLE_DEFAULT);
385 for (n = 0; n < csi2_port_lanes[port] + 1; n++) {
386 hrt_address base = csi2_port_base[port] + csi2_lane_base[n];
388 atomisp_css2_hw_store_32(base + CSI2_REG_RX_CSI_DLY_CNT_TERMEN,
389 n == 0 ? clk_termen : dat_termen);
390 atomisp_css2_hw_store_32(base + CSI2_REG_RX_CSI_DLY_CNT_SETTLE,
391 n == 0 ? clk_settle : dat_settle);
395 void atomisp_csi2_configure(struct atomisp_sub_device *asd)
397 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401))
398 atomisp_csi2_configure_isp2401(asd);
402 * atomisp_mipi_csi2_cleanup - Routine for module driver cleanup
404 void atomisp_mipi_csi2_cleanup(struct atomisp_device *isp)
408 int atomisp_mipi_csi2_init(struct atomisp_device *isp)
410 struct atomisp_mipi_csi2_device *csi2_port;
414 for (i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++) {
415 csi2_port = &isp->csi2_port[i];
416 csi2_port->isp = isp;
417 ret = mipi_csi2_init_entities(csi2_port, i);
425 atomisp_mipi_csi2_cleanup(isp);