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 
25 static struct
26 v4l2_mbus_framefmt *__csi2_get_format(struct atomisp_mipi_csi2_device *csi2,
27 				      struct v4l2_subdev_state *sd_state,
28 				      enum v4l2_subdev_format_whence which,
29 				      unsigned int pad)
30 {
31 	if (which == V4L2_SUBDEV_FORMAT_TRY)
32 		return v4l2_subdev_get_try_format(&csi2->subdev, sd_state,
33 						  pad);
34 	else
35 		return &csi2->formats[pad];
36 }
37 
38 /*
39  * csi2_enum_mbus_code - Handle pixel format enumeration
40  * @sd     : pointer to v4l2 subdev structure
41  * @fh     : V4L2 subdev file handle
42  * @code   : pointer to v4l2_subdev_pad_mbus_code_enum structure
43  * return -EINVAL or zero on success
44  */
45 static int csi2_enum_mbus_code(struct v4l2_subdev *sd,
46 			       struct v4l2_subdev_state *sd_state,
47 			       struct v4l2_subdev_mbus_code_enum *code)
48 {
49 	const struct atomisp_in_fmt_conv *ic = atomisp_in_fmt_conv;
50 	unsigned int i = 0;
51 
52 	while (ic->code) {
53 		if (i == code->index) {
54 			code->code = ic->code;
55 			return 0;
56 		}
57 		i++, ic++;
58 	}
59 
60 	return -EINVAL;
61 }
62 
63 /*
64  * csi2_get_format - Handle get format by pads subdev method
65  * @sd : pointer to v4l2 subdev structure
66  * @fh : V4L2 subdev file handle
67  * @pad: pad num
68  * @fmt: pointer to v4l2 format structure
69  * return -EINVAL or zero on success
70  */
71 static int csi2_get_format(struct v4l2_subdev *sd,
72 			   struct v4l2_subdev_state *sd_state,
73 			   struct v4l2_subdev_format *fmt)
74 {
75 	struct atomisp_mipi_csi2_device *csi2 = v4l2_get_subdevdata(sd);
76 	struct v4l2_mbus_framefmt *format;
77 
78 	format = __csi2_get_format(csi2, sd_state, fmt->which, fmt->pad);
79 
80 	fmt->format = *format;
81 
82 	return 0;
83 }
84 
85 int atomisp_csi2_set_ffmt(struct v4l2_subdev *sd,
86 			  struct v4l2_subdev_state *sd_state,
87 			  unsigned int which, uint16_t pad,
88 			  struct v4l2_mbus_framefmt *ffmt)
89 {
90 	struct atomisp_mipi_csi2_device *csi2 = v4l2_get_subdevdata(sd);
91 	struct v4l2_mbus_framefmt *actual_ffmt = __csi2_get_format(csi2,
92 								   sd_state,
93 								   which, pad);
94 
95 	if (pad == CSI2_PAD_SINK) {
96 		const struct atomisp_in_fmt_conv *ic;
97 		struct v4l2_mbus_framefmt tmp_ffmt;
98 
99 		ic = atomisp_find_in_fmt_conv(ffmt->code);
100 		if (ic)
101 			actual_ffmt->code = ic->code;
102 		else
103 			actual_ffmt->code = atomisp_in_fmt_conv[0].code;
104 
105 		actual_ffmt->width = clamp_t(u32, ffmt->width,
106 					     ATOM_ISP_MIN_WIDTH,
107 					     ATOM_ISP_MAX_WIDTH);
108 		actual_ffmt->height = clamp_t(u32, ffmt->height,
109 					      ATOM_ISP_MIN_HEIGHT,
110 					      ATOM_ISP_MAX_HEIGHT);
111 
112 		tmp_ffmt = *ffmt = *actual_ffmt;
113 
114 		return atomisp_csi2_set_ffmt(sd, sd_state, which,
115 					     CSI2_PAD_SOURCE,
116 					     &tmp_ffmt);
117 	}
118 
119 	/* FIXME: DPCM decompression */
120 	*actual_ffmt = *ffmt = *__csi2_get_format(csi2, sd_state, which,
121 						  CSI2_PAD_SINK);
122 
123 	return 0;
124 }
125 
126 /*
127  * csi2_set_format - Handle set format by pads subdev method
128  * @sd : pointer to v4l2 subdev structure
129  * @fh : V4L2 subdev file handle
130  * @pad: pad num
131  * @fmt: pointer to v4l2 format structure
132  * return -EINVAL or zero on success
133  */
134 static int csi2_set_format(struct v4l2_subdev *sd,
135 			   struct v4l2_subdev_state *sd_state,
136 			   struct v4l2_subdev_format *fmt)
137 {
138 	return atomisp_csi2_set_ffmt(sd, sd_state, fmt->which, fmt->pad,
139 				     &fmt->format);
140 }
141 
142 /*
143  * csi2_set_stream - Enable/Disable streaming on the CSI2 module
144  * @sd: ISP CSI2 V4L2 subdevice
145  * @enable: Enable/disable stream (1/0)
146  *
147  * Return 0 on success or a negative error code otherwise.
148  */
149 static int csi2_set_stream(struct v4l2_subdev *sd, int enable)
150 {
151 	return 0;
152 }
153 
154 /* subdev core operations */
155 static const struct v4l2_subdev_core_ops csi2_core_ops = {
156 };
157 
158 /* subdev video operations */
159 static const struct v4l2_subdev_video_ops csi2_video_ops = {
160 	.s_stream = csi2_set_stream,
161 };
162 
163 /* subdev pad operations */
164 static const struct v4l2_subdev_pad_ops csi2_pad_ops = {
165 	.enum_mbus_code = csi2_enum_mbus_code,
166 	.get_fmt = csi2_get_format,
167 	.set_fmt = csi2_set_format,
168 	.link_validate = v4l2_subdev_link_validate_default,
169 };
170 
171 /* subdev operations */
172 static const struct v4l2_subdev_ops csi2_ops = {
173 	.core = &csi2_core_ops,
174 	.video = &csi2_video_ops,
175 	.pad = &csi2_pad_ops,
176 };
177 
178 /*
179  * csi2_link_setup - Setup CSI2 connections.
180  * @entity : Pointer to media entity structure
181  * @local  : Pointer to local pad array
182  * @remote : Pointer to remote pad array
183  * @flags  : Link flags
184  * return -EINVAL or zero on success
185  */
186 static int csi2_link_setup(struct media_entity *entity,
187 			   const struct media_pad *local,
188 			   const struct media_pad *remote, u32 flags)
189 {
190 	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
191 	struct atomisp_mipi_csi2_device *csi2 = v4l2_get_subdevdata(sd);
192 	u32 result = local->index | is_media_entity_v4l2_subdev(remote->entity);
193 
194 	switch (result) {
195 	case CSI2_PAD_SOURCE | MEDIA_ENT_F_OLD_BASE:
196 		/* not supported yet */
197 		return -EINVAL;
198 
199 	case CSI2_PAD_SOURCE | MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
200 		if (flags & MEDIA_LNK_FL_ENABLED) {
201 			if (csi2->output & ~CSI2_OUTPUT_ISP_SUBDEV)
202 				return -EBUSY;
203 			csi2->output |= CSI2_OUTPUT_ISP_SUBDEV;
204 		} else {
205 			csi2->output &= ~CSI2_OUTPUT_ISP_SUBDEV;
206 		}
207 		break;
208 
209 	default:
210 		/* Link from camera to CSI2 is fixed... */
211 		return -EINVAL;
212 	}
213 	return 0;
214 }
215 
216 /* media operations */
217 static const struct media_entity_operations csi2_media_ops = {
218 	.link_setup = csi2_link_setup,
219 	.link_validate = v4l2_subdev_link_validate,
220 };
221 
222 /*
223  * ispcsi2_init_entities - Initialize subdev and media entity.
224  * @csi2: Pointer to ispcsi2 structure.
225  * return -ENOMEM or zero on success
226  */
227 static int mipi_csi2_init_entities(struct atomisp_mipi_csi2_device *csi2,
228 				   int port)
229 {
230 	struct v4l2_subdev *sd = &csi2->subdev;
231 	struct media_pad *pads = csi2->pads;
232 	struct media_entity *me = &sd->entity;
233 	int ret;
234 
235 	v4l2_subdev_init(sd, &csi2_ops);
236 	snprintf(sd->name, sizeof(sd->name), "ATOM ISP CSI2-port%d", port);
237 
238 	v4l2_set_subdevdata(sd, csi2);
239 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
240 
241 	pads[CSI2_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
242 	pads[CSI2_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
243 
244 	me->ops = &csi2_media_ops;
245 	me->function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
246 	ret = media_entity_pads_init(me, CSI2_PADS_NUM, pads);
247 	if (ret < 0)
248 		return ret;
249 
250 	csi2->formats[CSI2_PAD_SINK].code = atomisp_in_fmt_conv[0].code;
251 	csi2->formats[CSI2_PAD_SOURCE].code = atomisp_in_fmt_conv[0].code;
252 
253 	return 0;
254 }
255 
256 void
257 atomisp_mipi_csi2_unregister_entities(struct atomisp_mipi_csi2_device *csi2)
258 {
259 	media_entity_cleanup(&csi2->subdev.entity);
260 	v4l2_device_unregister_subdev(&csi2->subdev);
261 }
262 
263 int atomisp_mipi_csi2_register_entities(struct atomisp_mipi_csi2_device *csi2,
264 					struct v4l2_device *vdev)
265 {
266 	int ret;
267 
268 	/* Register the subdev and video nodes. */
269 	ret = v4l2_device_register_subdev(vdev, &csi2->subdev);
270 	if (ret < 0)
271 		goto error;
272 
273 	return 0;
274 
275 error:
276 	atomisp_mipi_csi2_unregister_entities(csi2);
277 	return ret;
278 }
279 
280 static const int LIMIT_SHIFT = 6;	/* Limit numeric range into 31 bits */
281 
282 static int
283 atomisp_csi2_configure_calc(const short int coeffs[2], int mipi_freq, int def)
284 {
285 	/* Delay counter accuracy, 1/0.0625 for ANN/CHT, 1/0.125 for BXT */
286 	static const int accinv = 16;		/* 1 / COUNT_ACC */
287 	int r;
288 
289 	if (mipi_freq >> LIMIT_SHIFT <= 0)
290 		return def;
291 
292 	r = accinv * coeffs[1] * (500000000 >> LIMIT_SHIFT);
293 	r /= mipi_freq >> LIMIT_SHIFT;
294 	r += accinv * coeffs[0];
295 
296 	return r;
297 }
298 
299 static void atomisp_csi2_configure_isp2401(struct atomisp_sub_device *asd)
300 {
301 	/*
302 	 * The ISP2401 new input system CSI2+ receiver has several
303 	 * parameters affecting the receiver timings. These depend
304 	 * on the MIPI bus frequency F in Hz (sensor transmitter rate)
305 	 * as follows:
306 	 *	register value = (A/1e9 + B * UI) / COUNT_ACC
307 	 * where
308 	 *	UI = 1 / (2 * F) in seconds
309 	 *	COUNT_ACC = counter accuracy in seconds
310 	 *	For ANN and CHV, COUNT_ACC = 0.0625 ns
311 	 *	For BXT,  COUNT_ACC = 0.125 ns
312 	 * A and B are coefficients from the table below,
313 	 * depending whether the register minimum or maximum value is
314 	 * calculated.
315 	 *				       Minimum     Maximum
316 	 * Clock lane			       A     B     A     B
317 	 * reg_rx_csi_dly_cnt_termen_clane     0     0    38     0
318 	 * reg_rx_csi_dly_cnt_settle_clane    95    -8   300   -16
319 	 * Data lanes
320 	 * reg_rx_csi_dly_cnt_termen_dlane0    0     0    35     4
321 	 * reg_rx_csi_dly_cnt_settle_dlane0   85    -2   145    -6
322 	 * reg_rx_csi_dly_cnt_termen_dlane1    0     0    35     4
323 	 * reg_rx_csi_dly_cnt_settle_dlane1   85    -2   145    -6
324 	 * reg_rx_csi_dly_cnt_termen_dlane2    0     0    35     4
325 	 * reg_rx_csi_dly_cnt_settle_dlane2   85    -2   145    -6
326 	 * reg_rx_csi_dly_cnt_termen_dlane3    0     0    35     4
327 	 * reg_rx_csi_dly_cnt_settle_dlane3   85    -2   145    -6
328 	 *
329 	 * We use the minimum values in the calculations below.
330 	 */
331 	static const short int coeff_clk_termen[] = { 0, 0 };
332 	static const short int coeff_clk_settle[] = { 95, -8 };
333 	static const short int coeff_dat_termen[] = { 0, 0 };
334 	static const short int coeff_dat_settle[] = { 85, -2 };
335 	static const int TERMEN_DEFAULT		  = 0 * 0;
336 	static const int SETTLE_DEFAULT		  = 0x480;
337 
338 	static const hrt_address csi2_port_base[] = {
339 		[ATOMISP_CAMERA_PORT_PRIMARY]     = CSI2_PORT_A_BASE,
340 		[ATOMISP_CAMERA_PORT_SECONDARY]   = CSI2_PORT_B_BASE,
341 		[ATOMISP_CAMERA_PORT_TERTIARY]    = CSI2_PORT_C_BASE,
342 	};
343 	/* Number of lanes on each port, excluding clock lane */
344 	static const unsigned char csi2_port_lanes[] = {
345 		[ATOMISP_CAMERA_PORT_PRIMARY]     = 4,
346 		[ATOMISP_CAMERA_PORT_SECONDARY]   = 2,
347 		[ATOMISP_CAMERA_PORT_TERTIARY]    = 2,
348 	};
349 	static const hrt_address csi2_lane_base[] = {
350 		CSI2_LANE_CL_BASE,
351 		CSI2_LANE_D0_BASE,
352 		CSI2_LANE_D1_BASE,
353 		CSI2_LANE_D2_BASE,
354 		CSI2_LANE_D3_BASE,
355 	};
356 
357 	int clk_termen;
358 	int clk_settle;
359 	int dat_termen;
360 	int dat_settle;
361 
362 	struct v4l2_control ctrl;
363 	struct atomisp_device *isp = asd->isp;
364 	struct camera_mipi_info *mipi_info;
365 	int mipi_freq = 0;
366 	enum atomisp_camera_port port;
367 
368 	int n;
369 
370 	mipi_info = atomisp_to_sensor_mipi_info(
371 			isp->inputs[asd->input_curr].camera);
372 	port = mipi_info->port;
373 
374 	ctrl.id = V4L2_CID_LINK_FREQ;
375 	if (v4l2_g_ctrl
376 	    (isp->inputs[asd->input_curr].camera->ctrl_handler, &ctrl) == 0)
377 		mipi_freq = ctrl.value;
378 
379 	clk_termen = atomisp_csi2_configure_calc(coeff_clk_termen, mipi_freq,
380 						 TERMEN_DEFAULT);
381 	clk_settle = atomisp_csi2_configure_calc(coeff_clk_settle, mipi_freq,
382 						 SETTLE_DEFAULT);
383 	dat_termen = atomisp_csi2_configure_calc(coeff_dat_termen, mipi_freq,
384 						 TERMEN_DEFAULT);
385 	dat_settle = atomisp_csi2_configure_calc(coeff_dat_settle, mipi_freq,
386 						 SETTLE_DEFAULT);
387 
388 	for (n = 0; n < csi2_port_lanes[port] + 1; n++) {
389 		hrt_address base = csi2_port_base[port] + csi2_lane_base[n];
390 
391 		atomisp_css2_hw_store_32(base + CSI2_REG_RX_CSI_DLY_CNT_TERMEN,
392 					 n == 0 ? clk_termen : dat_termen);
393 		atomisp_css2_hw_store_32(base + CSI2_REG_RX_CSI_DLY_CNT_SETTLE,
394 					 n == 0 ? clk_settle : dat_settle);
395 	}
396 }
397 
398 void atomisp_csi2_configure(struct atomisp_sub_device *asd)
399 {
400 	if (IS_HWREVISION(asd->isp, ATOMISP_HW_REVISION_ISP2401))
401 		atomisp_csi2_configure_isp2401(asd);
402 }
403 
404 /*
405  * atomisp_mipi_csi2_cleanup - Routine for module driver cleanup
406  */
407 void atomisp_mipi_csi2_cleanup(struct atomisp_device *isp)
408 {
409 }
410 
411 int atomisp_mipi_csi2_init(struct atomisp_device *isp)
412 {
413 	struct atomisp_mipi_csi2_device *csi2_port;
414 	unsigned int i;
415 	int ret;
416 
417 	for (i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++) {
418 		csi2_port = &isp->csi2_port[i];
419 		csi2_port->isp = isp;
420 		ret = mipi_csi2_init_entities(csi2_port, i);
421 		if (ret < 0)
422 			goto fail;
423 	}
424 
425 	return 0;
426 
427 fail:
428 	atomisp_mipi_csi2_cleanup(isp);
429 	return ret;
430 }
431