1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Support for Medifield PNW Camera Imaging ISP subsystem.
4 *
5 * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
6 *
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.
10 *
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.
15 *
16 *
17 */
18
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"
24
__csi2_get_format(struct atomisp_mipi_csi2_device * csi2,struct v4l2_subdev_pad_config * cfg,enum v4l2_subdev_format_whence which,unsigned int pad)25 static struct v4l2_mbus_framefmt *__csi2_get_format(struct
26 atomisp_mipi_csi2_device
27 * csi2,
28 struct
29 v4l2_subdev_pad_config *cfg,
30 enum
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);
35 else
36 return &csi2->formats[pad];
37 }
38
39 /*
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
45 */
csi2_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)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)
49 {
50 const struct atomisp_in_fmt_conv *ic = atomisp_in_fmt_conv;
51 unsigned int i = 0;
52
53 while (ic->code) {
54 if (i == code->index) {
55 code->code = ic->code;
56 return 0;
57 }
58 i++, ic++;
59 }
60
61 return -EINVAL;
62 }
63
64 /*
65 * csi2_get_format - Handle get format by pads subdev method
66 * @sd : pointer to v4l2 subdev structure
67 * @fh : V4L2 subdev file handle
68 * @pad: pad num
69 * @fmt: pointer to v4l2 format structure
70 * return -EINVAL or zero on success
71 */
csi2_get_format(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)72 static int csi2_get_format(struct v4l2_subdev *sd,
73 struct v4l2_subdev_pad_config *cfg,
74 struct v4l2_subdev_format *fmt)
75 {
76 struct atomisp_mipi_csi2_device *csi2 = v4l2_get_subdevdata(sd);
77 struct v4l2_mbus_framefmt *format;
78
79 format = __csi2_get_format(csi2, cfg, fmt->which, fmt->pad);
80
81 fmt->format = *format;
82
83 return 0;
84 }
85
atomisp_csi2_set_ffmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,unsigned int which,uint16_t pad,struct v4l2_mbus_framefmt * ffmt)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)
90 {
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);
93
94 if (pad == CSI2_PAD_SINK) {
95 const struct atomisp_in_fmt_conv *ic;
96 struct v4l2_mbus_framefmt tmp_ffmt;
97
98 ic = atomisp_find_in_fmt_conv(ffmt->code);
99 if (ic)
100 actual_ffmt->code = ic->code;
101 else
102 actual_ffmt->code = atomisp_in_fmt_conv[0].code;
103
104 actual_ffmt->width = clamp_t(
105 u32, ffmt->width, ATOM_ISP_MIN_WIDTH,
106 ATOM_ISP_MAX_WIDTH);
107 actual_ffmt->height = clamp_t(
108 u32, ffmt->height, ATOM_ISP_MIN_HEIGHT,
109 ATOM_ISP_MAX_HEIGHT);
110
111 tmp_ffmt = *ffmt = *actual_ffmt;
112
113 return atomisp_csi2_set_ffmt(sd, cfg, which, CSI2_PAD_SOURCE,
114 &tmp_ffmt);
115 }
116
117 /* FIXME: DPCM decompression */
118 *actual_ffmt = *ffmt = *__csi2_get_format(csi2, cfg, which, CSI2_PAD_SINK);
119
120 return 0;
121 }
122
123 /*
124 * csi2_set_format - Handle set format by pads subdev method
125 * @sd : pointer to v4l2 subdev structure
126 * @fh : V4L2 subdev file handle
127 * @pad: pad num
128 * @fmt: pointer to v4l2 format structure
129 * return -EINVAL or zero on success
130 */
csi2_set_format(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)131 static int csi2_set_format(struct v4l2_subdev *sd,
132 struct v4l2_subdev_pad_config *cfg,
133 struct v4l2_subdev_format *fmt)
134 {
135 return atomisp_csi2_set_ffmt(sd, cfg, fmt->which, fmt->pad,
136 &fmt->format);
137 }
138
139 /*
140 * csi2_set_stream - Enable/Disable streaming on the CSI2 module
141 * @sd: ISP CSI2 V4L2 subdevice
142 * @enable: Enable/disable stream (1/0)
143 *
144 * Return 0 on success or a negative error code otherwise.
145 */
csi2_set_stream(struct v4l2_subdev * sd,int enable)146 static int csi2_set_stream(struct v4l2_subdev *sd, int enable)
147 {
148 return 0;
149 }
150
151 /* subdev core operations */
152 static const struct v4l2_subdev_core_ops csi2_core_ops = {
153 };
154
155 /* subdev video operations */
156 static const struct v4l2_subdev_video_ops csi2_video_ops = {
157 .s_stream = csi2_set_stream,
158 };
159
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,
166 };
167
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,
173 };
174
175 /*
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
182 */
csi2_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)183 static int csi2_link_setup(struct media_entity *entity,
184 const struct media_pad *local,
185 const struct media_pad *remote, u32 flags)
186 {
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);
190
191 switch (result) {
192 case CSI2_PAD_SOURCE | MEDIA_ENT_F_OLD_BASE:
193 /* not supported yet */
194 return -EINVAL;
195
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)
199 return -EBUSY;
200 csi2->output |= CSI2_OUTPUT_ISP_SUBDEV;
201 } else {
202 csi2->output &= ~CSI2_OUTPUT_ISP_SUBDEV;
203 }
204 break;
205
206 default:
207 /* Link from camera to CSI2 is fixed... */
208 return -EINVAL;
209 }
210 return 0;
211 }
212
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,
217 };
218
219 /*
220 * ispcsi2_init_entities - Initialize subdev and media entity.
221 * @csi2: Pointer to ispcsi2 structure.
222 * return -ENOMEM or zero on success
223 */
mipi_csi2_init_entities(struct atomisp_mipi_csi2_device * csi2,int port)224 static int mipi_csi2_init_entities(struct atomisp_mipi_csi2_device *csi2,
225 int port)
226 {
227 struct v4l2_subdev *sd = &csi2->subdev;
228 struct media_pad *pads = csi2->pads;
229 struct media_entity *me = &sd->entity;
230 int ret;
231
232 v4l2_subdev_init(sd, &csi2_ops);
233 snprintf(sd->name, sizeof(sd->name), "ATOM ISP CSI2-port%d", port);
234
235 v4l2_set_subdevdata(sd, csi2);
236 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
237
238 pads[CSI2_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
239 pads[CSI2_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
240
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);
244 if (ret < 0)
245 return ret;
246
247 csi2->formats[CSI2_PAD_SINK].code =
248 csi2->formats[CSI2_PAD_SOURCE].code =
249 atomisp_in_fmt_conv[0].code;
250
251 return 0;
252 }
253
254 void
atomisp_mipi_csi2_unregister_entities(struct atomisp_mipi_csi2_device * csi2)255 atomisp_mipi_csi2_unregister_entities(struct atomisp_mipi_csi2_device *csi2)
256 {
257 media_entity_cleanup(&csi2->subdev.entity);
258 v4l2_device_unregister_subdev(&csi2->subdev);
259 }
260
atomisp_mipi_csi2_register_entities(struct atomisp_mipi_csi2_device * csi2,struct v4l2_device * vdev)261 int atomisp_mipi_csi2_register_entities(struct atomisp_mipi_csi2_device *csi2,
262 struct v4l2_device *vdev)
263 {
264 int ret;
265
266 /* Register the subdev and video nodes. */
267 ret = v4l2_device_register_subdev(vdev, &csi2->subdev);
268 if (ret < 0)
269 goto error;
270
271 return 0;
272
273 error:
274 atomisp_mipi_csi2_unregister_entities(csi2);
275 return ret;
276 }
277
278 static const int LIMIT_SHIFT = 6; /* Limit numeric range into 31 bits */
279
280 static int
atomisp_csi2_configure_calc(const short int coeffs[2],int mipi_freq,int def)281 atomisp_csi2_configure_calc(const short int coeffs[2], int mipi_freq, int def)
282 {
283 /* Delay counter accuracy, 1/0.0625 for ANN/CHT, 1/0.125 for BXT */
284 static const int accinv = 16; /* 1 / COUNT_ACC */
285 int r;
286
287 if (mipi_freq >> LIMIT_SHIFT <= 0)
288 return def;
289
290 r = accinv * coeffs[1] * (500000000 >> LIMIT_SHIFT);
291 r /= mipi_freq >> LIMIT_SHIFT;
292 r += accinv * coeffs[0];
293
294 return r;
295 }
296
atomisp_csi2_configure_isp2401(struct atomisp_sub_device * asd)297 static void atomisp_csi2_configure_isp2401(struct atomisp_sub_device *asd)
298 {
299 /*
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)
303 * as follows:
304 * register value = (A/1e9 + B * UI) / COUNT_ACC
305 * where
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
312 * calculated.
313 * Minimum Maximum
314 * Clock lane A B A B
315 * reg_rx_csi_dly_cnt_termen_clane 0 0 38 0
316 * reg_rx_csi_dly_cnt_settle_clane 95 -8 300 -16
317 * Data lanes
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
326 *
327 * We use the minimum values in the calculations below.
328 */
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;
335
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,
340 };
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,
346 };
347 static const hrt_address csi2_lane_base[] = {
348 CSI2_LANE_CL_BASE,
349 CSI2_LANE_D0_BASE,
350 CSI2_LANE_D1_BASE,
351 CSI2_LANE_D2_BASE,
352 CSI2_LANE_D3_BASE,
353 };
354
355 int clk_termen;
356 int clk_settle;
357 int dat_termen;
358 int dat_settle;
359
360 struct v4l2_control ctrl;
361 struct atomisp_device *isp = asd->isp;
362 struct camera_mipi_info *mipi_info;
363 int mipi_freq = 0;
364 enum atomisp_camera_port port;
365
366 int n;
367
368 mipi_info = atomisp_to_sensor_mipi_info(
369 isp->inputs[asd->input_curr].camera);
370 port = mipi_info->port;
371
372 ctrl.id = V4L2_CID_LINK_FREQ;
373 if (v4l2_g_ctrl
374 (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
375 mipi_freq = ctrl.value;
376
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];
387
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);
392 }
393 }
394
atomisp_csi2_configure(struct atomisp_sub_device * asd)395 void atomisp_csi2_configure(struct atomisp_sub_device *asd)
396 {
397 if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401))
398 atomisp_csi2_configure_isp2401(asd);
399 }
400
401 /*
402 * atomisp_mipi_csi2_cleanup - Routine for module driver cleanup
403 */
atomisp_mipi_csi2_cleanup(struct atomisp_device * isp)404 void atomisp_mipi_csi2_cleanup(struct atomisp_device *isp)
405 {
406 }
407
atomisp_mipi_csi2_init(struct atomisp_device * isp)408 int atomisp_mipi_csi2_init(struct atomisp_device *isp)
409 {
410 struct atomisp_mipi_csi2_device *csi2_port;
411 unsigned int i;
412 int ret;
413
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);
418 if (ret < 0)
419 goto fail;
420 }
421
422 return 0;
423
424 fail:
425 atomisp_mipi_csi2_cleanup(isp);
426 return ret;
427 }
428