• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/media/i2c/ccs/ccs-core.c
4  *
5  * Generic driver for MIPI CCS/SMIA/SMIA++ compliant camera sensors
6  *
7  * Copyright (C) 2020 Intel Corporation
8  * Copyright (C) 2010--2012 Nokia Corporation
9  * Contact: Sakari Ailus <sakari.ailus@linux.intel.com>
10  *
11  * Based on smiapp driver by Vimarsh Zutshi
12  * Based on jt8ev1.c by Vimarsh Zutshi
13  * Based on smia-sensor.c by Tuukka Toivonen <tuukkat76@gmail.com>
14  */
15 
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/firmware.h>
20 #include <linux/gpio.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/module.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/property.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27 #include <linux/smiapp.h>
28 #include <linux/v4l2-mediabus.h>
29 #include <media/v4l2-fwnode.h>
30 #include <media/v4l2-device.h>
31 #include <uapi/linux/ccs.h>
32 
33 #include "ccs.h"
34 
35 #define CCS_ALIGN_DIM(dim, flags)	\
36 	((flags) & V4L2_SEL_FLAG_GE	\
37 	 ? ALIGN((dim), 2)		\
38 	 : (dim) & ~1)
39 
40 static struct ccs_limit_offset {
41 	u16	lim;
42 	u16	info;
43 } ccs_limit_offsets[CCS_L_LAST + 1];
44 
45 /*
46  * ccs_module_idents - supported camera modules
47  */
48 static const struct ccs_module_ident ccs_module_idents[] = {
49 	CCS_IDENT_L(0x01, 0x022b, -1, "vs6555"),
50 	CCS_IDENT_L(0x01, 0x022e, -1, "vw6558"),
51 	CCS_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
52 	CCS_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
53 	CCS_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
54 	CCS_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk),
55 	CCS_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
56 	CCS_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
57 	CCS_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk),
58 	CCS_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk),
59 	CCS_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk),
60 };
61 
62 #define CCS_DEVICE_FLAG_IS_SMIA		BIT(0)
63 
64 struct ccs_device {
65 	unsigned char flags;
66 };
67 
68 static const char * const ccs_regulators[] = { "vcore", "vio", "vana" };
69 
70 /*
71  *
72  * Dynamic Capability Identification
73  *
74  */
75 
ccs_assign_limit(void * ptr,unsigned int width,u32 val)76 static void ccs_assign_limit(void *ptr, unsigned int width, u32 val)
77 {
78 	switch (width) {
79 	case sizeof(u8):
80 		*(u8 *)ptr = val;
81 		break;
82 	case sizeof(u16):
83 		*(u16 *)ptr = val;
84 		break;
85 	case sizeof(u32):
86 		*(u32 *)ptr = val;
87 		break;
88 	}
89 }
90 
ccs_limit_ptr(struct ccs_sensor * sensor,unsigned int limit,unsigned int offset,void ** __ptr)91 static int ccs_limit_ptr(struct ccs_sensor *sensor, unsigned int limit,
92 			 unsigned int offset, void **__ptr)
93 {
94 	const struct ccs_limit *linfo;
95 
96 	if (WARN_ON(limit >= CCS_L_LAST))
97 		return -EINVAL;
98 
99 	linfo = &ccs_limits[ccs_limit_offsets[limit].info];
100 
101 	if (WARN_ON(!sensor->ccs_limits) ||
102 	    WARN_ON(offset + ccs_reg_width(linfo->reg) >
103 		    ccs_limit_offsets[limit + 1].lim))
104 		return -EINVAL;
105 
106 	*__ptr = sensor->ccs_limits + ccs_limit_offsets[limit].lim + offset;
107 
108 	return 0;
109 }
110 
ccs_replace_limit(struct ccs_sensor * sensor,unsigned int limit,unsigned int offset,u32 val)111 void ccs_replace_limit(struct ccs_sensor *sensor,
112 		       unsigned int limit, unsigned int offset, u32 val)
113 {
114 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
115 	const struct ccs_limit *linfo;
116 	void *ptr;
117 	int ret;
118 
119 	ret = ccs_limit_ptr(sensor, limit, offset, &ptr);
120 	if (ret)
121 		return;
122 
123 	linfo = &ccs_limits[ccs_limit_offsets[limit].info];
124 
125 	dev_dbg(&client->dev, "quirk: 0x%8.8x \"%s\" %u = %d, 0x%x\n",
126 		linfo->reg, linfo->name, offset, val, val);
127 
128 	ccs_assign_limit(ptr, ccs_reg_width(linfo->reg), val);
129 }
130 
ccs_get_limit(struct ccs_sensor * sensor,unsigned int limit,unsigned int offset)131 u32 ccs_get_limit(struct ccs_sensor *sensor, unsigned int limit,
132 		  unsigned int offset)
133 {
134 	void *ptr;
135 	u32 val;
136 	int ret;
137 
138 	ret = ccs_limit_ptr(sensor, limit, offset, &ptr);
139 	if (ret)
140 		return 0;
141 
142 	switch (ccs_reg_width(ccs_limits[ccs_limit_offsets[limit].info].reg)) {
143 	case sizeof(u8):
144 		val = *(u8 *)ptr;
145 		break;
146 	case sizeof(u16):
147 		val = *(u16 *)ptr;
148 		break;
149 	case sizeof(u32):
150 		val = *(u32 *)ptr;
151 		break;
152 	default:
153 		WARN_ON(1);
154 		return 0;
155 	}
156 
157 	return ccs_reg_conv(sensor, ccs_limits[limit].reg, val);
158 }
159 
ccs_read_all_limits(struct ccs_sensor * sensor)160 static int ccs_read_all_limits(struct ccs_sensor *sensor)
161 {
162 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
163 	void *ptr, *alloc, *end;
164 	unsigned int i, l;
165 	int ret;
166 
167 	kfree(sensor->ccs_limits);
168 	sensor->ccs_limits = NULL;
169 
170 	alloc = kzalloc(ccs_limit_offsets[CCS_L_LAST].lim, GFP_KERNEL);
171 	if (!alloc)
172 		return -ENOMEM;
173 
174 	end = alloc + ccs_limit_offsets[CCS_L_LAST].lim;
175 
176 	for (i = 0, l = 0, ptr = alloc; ccs_limits[i].size; i++) {
177 		u32 reg = ccs_limits[i].reg;
178 		unsigned int width = ccs_reg_width(reg);
179 		unsigned int j;
180 
181 		if (l == CCS_L_LAST) {
182 			dev_err(&client->dev,
183 				"internal error --- end of limit array\n");
184 			ret = -EINVAL;
185 			goto out_err;
186 		}
187 
188 		for (j = 0; j < ccs_limits[i].size / width;
189 		     j++, reg += width, ptr += width) {
190 			u32 val;
191 
192 			ret = ccs_read_addr_noconv(sensor, reg, &val);
193 			if (ret)
194 				goto out_err;
195 
196 			if (ptr + width > end) {
197 				dev_err(&client->dev,
198 					"internal error --- no room for regs\n");
199 				ret = -EINVAL;
200 				goto out_err;
201 			}
202 
203 			if (!val && j)
204 				break;
205 
206 			ccs_assign_limit(ptr, width, val);
207 
208 			dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n",
209 				reg, ccs_limits[i].name, val, val);
210 		}
211 
212 		if (ccs_limits[i].flags & CCS_L_FL_SAME_REG)
213 			continue;
214 
215 		l++;
216 		ptr = alloc + ccs_limit_offsets[l].lim;
217 	}
218 
219 	if (l != CCS_L_LAST) {
220 		dev_err(&client->dev,
221 			"internal error --- insufficient limits\n");
222 		ret = -EINVAL;
223 		goto out_err;
224 	}
225 
226 	sensor->ccs_limits = alloc;
227 
228 	if (CCS_LIM(sensor, SCALER_N_MIN) < 16)
229 		ccs_replace_limit(sensor, CCS_L_SCALER_N_MIN, 0, 16);
230 
231 	return 0;
232 
233 out_err:
234 	kfree(alloc);
235 
236 	return ret;
237 }
238 
ccs_read_frame_fmt(struct ccs_sensor * sensor)239 static int ccs_read_frame_fmt(struct ccs_sensor *sensor)
240 {
241 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
242 	u8 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc;
243 	unsigned int i;
244 	int pixel_count = 0;
245 	int line_count = 0;
246 
247 	fmt_model_type = CCS_LIM(sensor, FRAME_FORMAT_MODEL_TYPE);
248 	fmt_model_subtype = CCS_LIM(sensor, FRAME_FORMAT_MODEL_SUBTYPE);
249 
250 	ncol_desc = (fmt_model_subtype
251 		     & CCS_FRAME_FORMAT_MODEL_SUBTYPE_COLUMNS_MASK)
252 		>> CCS_FRAME_FORMAT_MODEL_SUBTYPE_COLUMNS_SHIFT;
253 	nrow_desc = fmt_model_subtype
254 		& CCS_FRAME_FORMAT_MODEL_SUBTYPE_ROWS_MASK;
255 
256 	dev_dbg(&client->dev, "format_model_type %s\n",
257 		fmt_model_type == CCS_FRAME_FORMAT_MODEL_TYPE_2_BYTE
258 		? "2 byte" :
259 		fmt_model_type == CCS_FRAME_FORMAT_MODEL_TYPE_4_BYTE
260 		? "4 byte" : "is simply bad");
261 
262 	dev_dbg(&client->dev, "%u column and %u row descriptors\n",
263 		ncol_desc, nrow_desc);
264 
265 	for (i = 0; i < ncol_desc + nrow_desc; i++) {
266 		u32 desc;
267 		u32 pixelcode;
268 		u32 pixels;
269 		char *which;
270 		char *what;
271 
272 		if (fmt_model_type == CCS_FRAME_FORMAT_MODEL_TYPE_2_BYTE) {
273 			desc = CCS_LIM_AT(sensor, FRAME_FORMAT_DESCRIPTOR, i);
274 
275 			pixelcode =
276 				(desc
277 				 & CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_MASK)
278 				>> CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_SHIFT;
279 			pixels = desc & CCS_FRAME_FORMAT_DESCRIPTOR_PIXELS_MASK;
280 		} else if (fmt_model_type
281 			   == CCS_FRAME_FORMAT_MODEL_TYPE_4_BYTE) {
282 			desc = CCS_LIM_AT(sensor, FRAME_FORMAT_DESCRIPTOR_4, i);
283 
284 			pixelcode =
285 				(desc
286 				 & CCS_FRAME_FORMAT_DESCRIPTOR_4_PCODE_MASK)
287 				>> CCS_FRAME_FORMAT_DESCRIPTOR_4_PCODE_SHIFT;
288 			pixels = desc &
289 				CCS_FRAME_FORMAT_DESCRIPTOR_4_PIXELS_MASK;
290 		} else {
291 			dev_dbg(&client->dev,
292 				"invalid frame format model type %d\n",
293 				fmt_model_type);
294 			return -EINVAL;
295 		}
296 
297 		if (i < ncol_desc)
298 			which = "columns";
299 		else
300 			which = "rows";
301 
302 		switch (pixelcode) {
303 		case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_EMBEDDED:
304 			what = "embedded";
305 			break;
306 		case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_DUMMY_PIXEL:
307 			what = "dummy";
308 			break;
309 		case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_BLACK_PIXEL:
310 			what = "black";
311 			break;
312 		case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_DARK_PIXEL:
313 			what = "dark";
314 			break;
315 		case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL:
316 			what = "visible";
317 			break;
318 		default:
319 			what = "invalid";
320 			break;
321 		}
322 
323 		dev_dbg(&client->dev,
324 			"%s pixels: %d %s (pixelcode %u)\n",
325 			what, pixels, which, pixelcode);
326 
327 		if (i < ncol_desc) {
328 			if (pixelcode ==
329 			    CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL)
330 				sensor->visible_pixel_start = pixel_count;
331 			pixel_count += pixels;
332 			continue;
333 		}
334 
335 		/* Handle row descriptors */
336 		switch (pixelcode) {
337 		case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_EMBEDDED:
338 			if (sensor->embedded_end)
339 				break;
340 			sensor->embedded_start = line_count;
341 			sensor->embedded_end = line_count + pixels;
342 			break;
343 		case CCS_FRAME_FORMAT_DESCRIPTOR_PCODE_VISIBLE_PIXEL:
344 			sensor->image_start = line_count;
345 			break;
346 		}
347 		line_count += pixels;
348 	}
349 
350 	if (sensor->embedded_end > sensor->image_start) {
351 		dev_dbg(&client->dev,
352 			"adjusting image start line to %u (was %u)\n",
353 			sensor->embedded_end, sensor->image_start);
354 		sensor->image_start = sensor->embedded_end;
355 	}
356 
357 	dev_dbg(&client->dev, "embedded data from lines %d to %d\n",
358 		sensor->embedded_start, sensor->embedded_end);
359 	dev_dbg(&client->dev, "image data starts at line %d\n",
360 		sensor->image_start);
361 
362 	return 0;
363 }
364 
ccs_pll_configure(struct ccs_sensor * sensor)365 static int ccs_pll_configure(struct ccs_sensor *sensor)
366 {
367 	struct ccs_pll *pll = &sensor->pll;
368 	int rval;
369 
370 	rval = ccs_write(sensor, VT_PIX_CLK_DIV, pll->vt_bk.pix_clk_div);
371 	if (rval < 0)
372 		return rval;
373 
374 	rval = ccs_write(sensor, VT_SYS_CLK_DIV, pll->vt_bk.sys_clk_div);
375 	if (rval < 0)
376 		return rval;
377 
378 	rval = ccs_write(sensor, PRE_PLL_CLK_DIV, pll->vt_fr.pre_pll_clk_div);
379 	if (rval < 0)
380 		return rval;
381 
382 	rval = ccs_write(sensor, PLL_MULTIPLIER, pll->vt_fr.pll_multiplier);
383 	if (rval < 0)
384 		return rval;
385 
386 	if (!(CCS_LIM(sensor, PHY_CTRL_CAPABILITY) &
387 	      CCS_PHY_CTRL_CAPABILITY_AUTO_PHY_CTL)) {
388 		/* Lane op clock ratio does not apply here. */
389 		rval = ccs_write(sensor, REQUESTED_LINK_RATE,
390 				 DIV_ROUND_UP(pll->op_bk.sys_clk_freq_hz,
391 					      1000000 / 256 / 256) *
392 				 (pll->flags & CCS_PLL_FLAG_LANE_SPEED_MODEL ?
393 				  sensor->pll.csi2.lanes : 1) <<
394 				 (pll->flags & CCS_PLL_FLAG_OP_SYS_DDR ?
395 				  1 : 0));
396 		if (rval < 0)
397 			return rval;
398 	}
399 
400 	if (sensor->pll.flags & CCS_PLL_FLAG_NO_OP_CLOCKS)
401 		return 0;
402 
403 	rval = ccs_write(sensor, OP_PIX_CLK_DIV, pll->op_bk.pix_clk_div);
404 	if (rval < 0)
405 		return rval;
406 
407 	rval = ccs_write(sensor, OP_SYS_CLK_DIV, pll->op_bk.sys_clk_div);
408 	if (rval < 0)
409 		return rval;
410 
411 	if (!(pll->flags & CCS_PLL_FLAG_DUAL_PLL))
412 		return 0;
413 
414 	rval = ccs_write(sensor, PLL_MODE, CCS_PLL_MODE_DUAL);
415 	if (rval < 0)
416 		return rval;
417 
418 	rval = ccs_write(sensor, OP_PRE_PLL_CLK_DIV,
419 			 pll->op_fr.pre_pll_clk_div);
420 	if (rval < 0)
421 		return rval;
422 
423 	return ccs_write(sensor, OP_PLL_MULTIPLIER, pll->op_fr.pll_multiplier);
424 }
425 
ccs_pll_try(struct ccs_sensor * sensor,struct ccs_pll * pll)426 static int ccs_pll_try(struct ccs_sensor *sensor, struct ccs_pll *pll)
427 {
428 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
429 	struct ccs_pll_limits lim = {
430 		.vt_fr = {
431 			.min_pre_pll_clk_div = CCS_LIM(sensor, MIN_PRE_PLL_CLK_DIV),
432 			.max_pre_pll_clk_div = CCS_LIM(sensor, MAX_PRE_PLL_CLK_DIV),
433 			.min_pll_ip_clk_freq_hz = CCS_LIM(sensor, MIN_PLL_IP_CLK_FREQ_MHZ),
434 			.max_pll_ip_clk_freq_hz = CCS_LIM(sensor, MAX_PLL_IP_CLK_FREQ_MHZ),
435 			.min_pll_multiplier = CCS_LIM(sensor, MIN_PLL_MULTIPLIER),
436 			.max_pll_multiplier = CCS_LIM(sensor, MAX_PLL_MULTIPLIER),
437 			.min_pll_op_clk_freq_hz = CCS_LIM(sensor, MIN_PLL_OP_CLK_FREQ_MHZ),
438 			.max_pll_op_clk_freq_hz = CCS_LIM(sensor, MAX_PLL_OP_CLK_FREQ_MHZ),
439 		},
440 		.op_fr = {
441 			.min_pre_pll_clk_div = CCS_LIM(sensor, MIN_OP_PRE_PLL_CLK_DIV),
442 			.max_pre_pll_clk_div = CCS_LIM(sensor, MAX_OP_PRE_PLL_CLK_DIV),
443 			.min_pll_ip_clk_freq_hz = CCS_LIM(sensor, MIN_OP_PLL_IP_CLK_FREQ_MHZ),
444 			.max_pll_ip_clk_freq_hz = CCS_LIM(sensor, MAX_OP_PLL_IP_CLK_FREQ_MHZ),
445 			.min_pll_multiplier = CCS_LIM(sensor, MIN_OP_PLL_MULTIPLIER),
446 			.max_pll_multiplier = CCS_LIM(sensor, MAX_OP_PLL_MULTIPLIER),
447 			.min_pll_op_clk_freq_hz = CCS_LIM(sensor, MIN_OP_PLL_OP_CLK_FREQ_MHZ),
448 			.max_pll_op_clk_freq_hz = CCS_LIM(sensor, MAX_OP_PLL_OP_CLK_FREQ_MHZ),
449 		},
450 		.op_bk = {
451 			 .min_sys_clk_div = CCS_LIM(sensor, MIN_OP_SYS_CLK_DIV),
452 			 .max_sys_clk_div = CCS_LIM(sensor, MAX_OP_SYS_CLK_DIV),
453 			 .min_pix_clk_div = CCS_LIM(sensor, MIN_OP_PIX_CLK_DIV),
454 			 .max_pix_clk_div = CCS_LIM(sensor, MAX_OP_PIX_CLK_DIV),
455 			 .min_sys_clk_freq_hz = CCS_LIM(sensor, MIN_OP_SYS_CLK_FREQ_MHZ),
456 			 .max_sys_clk_freq_hz = CCS_LIM(sensor, MAX_OP_SYS_CLK_FREQ_MHZ),
457 			 .min_pix_clk_freq_hz = CCS_LIM(sensor, MIN_OP_PIX_CLK_FREQ_MHZ),
458 			 .max_pix_clk_freq_hz = CCS_LIM(sensor, MAX_OP_PIX_CLK_FREQ_MHZ),
459 		 },
460 		.vt_bk = {
461 			 .min_sys_clk_div = CCS_LIM(sensor, MIN_VT_SYS_CLK_DIV),
462 			 .max_sys_clk_div = CCS_LIM(sensor, MAX_VT_SYS_CLK_DIV),
463 			 .min_pix_clk_div = CCS_LIM(sensor, MIN_VT_PIX_CLK_DIV),
464 			 .max_pix_clk_div = CCS_LIM(sensor, MAX_VT_PIX_CLK_DIV),
465 			 .min_sys_clk_freq_hz = CCS_LIM(sensor, MIN_VT_SYS_CLK_FREQ_MHZ),
466 			 .max_sys_clk_freq_hz = CCS_LIM(sensor, MAX_VT_SYS_CLK_FREQ_MHZ),
467 			 .min_pix_clk_freq_hz = CCS_LIM(sensor, MIN_VT_PIX_CLK_FREQ_MHZ),
468 			 .max_pix_clk_freq_hz = CCS_LIM(sensor, MAX_VT_PIX_CLK_FREQ_MHZ),
469 		 },
470 		.min_line_length_pck_bin = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK_BIN),
471 		.min_line_length_pck = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK),
472 	};
473 
474 	return ccs_pll_calculate(&client->dev, &lim, pll);
475 }
476 
ccs_pll_update(struct ccs_sensor * sensor)477 static int ccs_pll_update(struct ccs_sensor *sensor)
478 {
479 	struct ccs_pll *pll = &sensor->pll;
480 	int rval;
481 
482 	pll->binning_horizontal = sensor->binning_horizontal;
483 	pll->binning_vertical = sensor->binning_vertical;
484 	pll->link_freq =
485 		sensor->link_freq->qmenu_int[sensor->link_freq->val];
486 	pll->scale_m = sensor->scale_m;
487 	pll->bits_per_pixel = sensor->csi_format->compressed;
488 
489 	rval = ccs_pll_try(sensor, pll);
490 	if (rval < 0)
491 		return rval;
492 
493 	__v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_parray,
494 				 pll->pixel_rate_pixel_array);
495 	__v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_csi, pll->pixel_rate_csi);
496 
497 	return 0;
498 }
499 
500 
501 /*
502  *
503  * V4L2 Controls handling
504  *
505  */
506 
__ccs_update_exposure_limits(struct ccs_sensor * sensor)507 static void __ccs_update_exposure_limits(struct ccs_sensor *sensor)
508 {
509 	struct v4l2_ctrl *ctrl = sensor->exposure;
510 	int max;
511 
512 	max = sensor->pixel_array->crop[CCS_PA_PAD_SRC].height
513 		+ sensor->vblank->val
514 		- CCS_LIM(sensor, COARSE_INTEGRATION_TIME_MAX_MARGIN);
515 
516 	__v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, ctrl->step, max);
517 }
518 
519 /*
520  * Order matters.
521  *
522  * 1. Bits-per-pixel, descending.
523  * 2. Bits-per-pixel compressed, descending.
524  * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
525  *    orders must be defined.
526  */
527 static const struct ccs_csi_data_format ccs_csi_data_formats[] = {
528 	{ MEDIA_BUS_FMT_SGRBG16_1X16, 16, 16, CCS_PIXEL_ORDER_GRBG, },
529 	{ MEDIA_BUS_FMT_SRGGB16_1X16, 16, 16, CCS_PIXEL_ORDER_RGGB, },
530 	{ MEDIA_BUS_FMT_SBGGR16_1X16, 16, 16, CCS_PIXEL_ORDER_BGGR, },
531 	{ MEDIA_BUS_FMT_SGBRG16_1X16, 16, 16, CCS_PIXEL_ORDER_GBRG, },
532 	{ MEDIA_BUS_FMT_SGRBG14_1X14, 14, 14, CCS_PIXEL_ORDER_GRBG, },
533 	{ MEDIA_BUS_FMT_SRGGB14_1X14, 14, 14, CCS_PIXEL_ORDER_RGGB, },
534 	{ MEDIA_BUS_FMT_SBGGR14_1X14, 14, 14, CCS_PIXEL_ORDER_BGGR, },
535 	{ MEDIA_BUS_FMT_SGBRG14_1X14, 14, 14, CCS_PIXEL_ORDER_GBRG, },
536 	{ MEDIA_BUS_FMT_SGRBG12_1X12, 12, 12, CCS_PIXEL_ORDER_GRBG, },
537 	{ MEDIA_BUS_FMT_SRGGB12_1X12, 12, 12, CCS_PIXEL_ORDER_RGGB, },
538 	{ MEDIA_BUS_FMT_SBGGR12_1X12, 12, 12, CCS_PIXEL_ORDER_BGGR, },
539 	{ MEDIA_BUS_FMT_SGBRG12_1X12, 12, 12, CCS_PIXEL_ORDER_GBRG, },
540 	{ MEDIA_BUS_FMT_SGRBG10_1X10, 10, 10, CCS_PIXEL_ORDER_GRBG, },
541 	{ MEDIA_BUS_FMT_SRGGB10_1X10, 10, 10, CCS_PIXEL_ORDER_RGGB, },
542 	{ MEDIA_BUS_FMT_SBGGR10_1X10, 10, 10, CCS_PIXEL_ORDER_BGGR, },
543 	{ MEDIA_BUS_FMT_SGBRG10_1X10, 10, 10, CCS_PIXEL_ORDER_GBRG, },
544 	{ MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_GRBG, },
545 	{ MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_RGGB, },
546 	{ MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_BGGR, },
547 	{ MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, CCS_PIXEL_ORDER_GBRG, },
548 	{ MEDIA_BUS_FMT_SGRBG8_1X8, 8, 8, CCS_PIXEL_ORDER_GRBG, },
549 	{ MEDIA_BUS_FMT_SRGGB8_1X8, 8, 8, CCS_PIXEL_ORDER_RGGB, },
550 	{ MEDIA_BUS_FMT_SBGGR8_1X8, 8, 8, CCS_PIXEL_ORDER_BGGR, },
551 	{ MEDIA_BUS_FMT_SGBRG8_1X8, 8, 8, CCS_PIXEL_ORDER_GBRG, },
552 };
553 
554 static const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
555 
556 #define to_csi_format_idx(fmt) (((unsigned long)(fmt)			\
557 				 - (unsigned long)ccs_csi_data_formats) \
558 				/ sizeof(*ccs_csi_data_formats))
559 
ccs_pixel_order(struct ccs_sensor * sensor)560 static u32 ccs_pixel_order(struct ccs_sensor *sensor)
561 {
562 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
563 	int flip = 0;
564 
565 	if (sensor->hflip) {
566 		if (sensor->hflip->val)
567 			flip |= CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR;
568 
569 		if (sensor->vflip->val)
570 			flip |= CCS_IMAGE_ORIENTATION_VERTICAL_FLIP;
571 	}
572 
573 	flip ^= sensor->hvflip_inv_mask;
574 
575 	dev_dbg(&client->dev, "flip %d\n", flip);
576 	return sensor->default_pixel_order ^ flip;
577 }
578 
ccs_update_mbus_formats(struct ccs_sensor * sensor)579 static void ccs_update_mbus_formats(struct ccs_sensor *sensor)
580 {
581 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
582 	unsigned int csi_format_idx =
583 		to_csi_format_idx(sensor->csi_format) & ~3;
584 	unsigned int internal_csi_format_idx =
585 		to_csi_format_idx(sensor->internal_csi_format) & ~3;
586 	unsigned int pixel_order = ccs_pixel_order(sensor);
587 
588 	if (WARN_ON_ONCE(max(internal_csi_format_idx, csi_format_idx) +
589 			 pixel_order >= ARRAY_SIZE(ccs_csi_data_formats)))
590 		return;
591 
592 	sensor->mbus_frame_fmts =
593 		sensor->default_mbus_frame_fmts << pixel_order;
594 	sensor->csi_format =
595 		&ccs_csi_data_formats[csi_format_idx + pixel_order];
596 	sensor->internal_csi_format =
597 		&ccs_csi_data_formats[internal_csi_format_idx
598 					 + pixel_order];
599 
600 	dev_dbg(&client->dev, "new pixel order %s\n",
601 		pixel_order_str[pixel_order]);
602 }
603 
604 static const char * const ccs_test_patterns[] = {
605 	"Disabled",
606 	"Solid Colour",
607 	"Eight Vertical Colour Bars",
608 	"Colour Bars With Fade to Grey",
609 	"Pseudorandom Sequence (PN9)",
610 };
611 
ccs_set_ctrl(struct v4l2_ctrl * ctrl)612 static int ccs_set_ctrl(struct v4l2_ctrl *ctrl)
613 {
614 	struct ccs_sensor *sensor =
615 		container_of(ctrl->handler, struct ccs_subdev, ctrl_handler)
616 			->sensor;
617 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
618 	int pm_status;
619 	u32 orient = 0;
620 	unsigned int i;
621 	int exposure;
622 	int rval;
623 
624 	switch (ctrl->id) {
625 	case V4L2_CID_HFLIP:
626 	case V4L2_CID_VFLIP:
627 		if (sensor->streaming)
628 			return -EBUSY;
629 
630 		if (sensor->hflip->val)
631 			orient |= CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR;
632 
633 		if (sensor->vflip->val)
634 			orient |= CCS_IMAGE_ORIENTATION_VERTICAL_FLIP;
635 
636 		orient ^= sensor->hvflip_inv_mask;
637 
638 		ccs_update_mbus_formats(sensor);
639 
640 		break;
641 	case V4L2_CID_VBLANK:
642 		exposure = sensor->exposure->val;
643 
644 		__ccs_update_exposure_limits(sensor);
645 
646 		if (exposure > sensor->exposure->maximum) {
647 			sensor->exposure->val =	sensor->exposure->maximum;
648 			rval = ccs_set_ctrl(sensor->exposure);
649 			if (rval < 0)
650 				return rval;
651 		}
652 
653 		break;
654 	case V4L2_CID_LINK_FREQ:
655 		if (sensor->streaming)
656 			return -EBUSY;
657 
658 		rval = ccs_pll_update(sensor);
659 		if (rval)
660 			return rval;
661 
662 		return 0;
663 	case V4L2_CID_TEST_PATTERN:
664 		for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
665 			v4l2_ctrl_activate(
666 				sensor->test_data[i],
667 				ctrl->val ==
668 				V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR);
669 
670 		break;
671 	}
672 
673 	pm_status = pm_runtime_get_if_active(&client->dev, true);
674 	if (!pm_status)
675 		return 0;
676 
677 	switch (ctrl->id) {
678 	case V4L2_CID_ANALOGUE_GAIN:
679 		rval = ccs_write(sensor, ANALOG_GAIN_CODE_GLOBAL, ctrl->val);
680 
681 		break;
682 
683 	case V4L2_CID_CCS_ANALOGUE_LINEAR_GAIN:
684 		rval = ccs_write(sensor, ANALOG_LINEAR_GAIN_GLOBAL, ctrl->val);
685 
686 		break;
687 
688 	case V4L2_CID_CCS_ANALOGUE_EXPONENTIAL_GAIN:
689 		rval = ccs_write(sensor, ANALOG_EXPONENTIAL_GAIN_GLOBAL,
690 				 ctrl->val);
691 
692 		break;
693 
694 	case V4L2_CID_DIGITAL_GAIN:
695 		if (CCS_LIM(sensor, DIGITAL_GAIN_CAPABILITY) ==
696 		    CCS_DIGITAL_GAIN_CAPABILITY_GLOBAL) {
697 			rval = ccs_write(sensor, DIGITAL_GAIN_GLOBAL,
698 					 ctrl->val);
699 			break;
700 		}
701 
702 		rval = ccs_write_addr(sensor,
703 				      SMIAPP_REG_U16_DIGITAL_GAIN_GREENR,
704 				      ctrl->val);
705 		if (rval)
706 			break;
707 
708 		rval = ccs_write_addr(sensor,
709 				      SMIAPP_REG_U16_DIGITAL_GAIN_RED,
710 				      ctrl->val);
711 		if (rval)
712 			break;
713 
714 		rval = ccs_write_addr(sensor,
715 				      SMIAPP_REG_U16_DIGITAL_GAIN_BLUE,
716 				      ctrl->val);
717 		if (rval)
718 			break;
719 
720 		rval = ccs_write_addr(sensor,
721 				      SMIAPP_REG_U16_DIGITAL_GAIN_GREENB,
722 				      ctrl->val);
723 
724 		break;
725 	case V4L2_CID_EXPOSURE:
726 		rval = ccs_write(sensor, COARSE_INTEGRATION_TIME, ctrl->val);
727 
728 		break;
729 	case V4L2_CID_HFLIP:
730 	case V4L2_CID_VFLIP:
731 		rval = ccs_write(sensor, IMAGE_ORIENTATION, orient);
732 
733 		break;
734 	case V4L2_CID_VBLANK:
735 		rval = ccs_write(sensor, FRAME_LENGTH_LINES,
736 				 sensor->pixel_array->crop[
737 					 CCS_PA_PAD_SRC].height
738 				 + ctrl->val);
739 
740 		break;
741 	case V4L2_CID_HBLANK:
742 		rval = ccs_write(sensor, LINE_LENGTH_PCK,
743 				 sensor->pixel_array->crop[CCS_PA_PAD_SRC].width
744 				 + ctrl->val);
745 
746 		break;
747 	case V4L2_CID_TEST_PATTERN:
748 		rval = ccs_write(sensor, TEST_PATTERN_MODE, ctrl->val);
749 
750 		break;
751 	case V4L2_CID_TEST_PATTERN_RED:
752 		rval = ccs_write(sensor, TEST_DATA_RED, ctrl->val);
753 
754 		break;
755 	case V4L2_CID_TEST_PATTERN_GREENR:
756 		rval = ccs_write(sensor, TEST_DATA_GREENR, ctrl->val);
757 
758 		break;
759 	case V4L2_CID_TEST_PATTERN_BLUE:
760 		rval = ccs_write(sensor, TEST_DATA_BLUE, ctrl->val);
761 
762 		break;
763 	case V4L2_CID_TEST_PATTERN_GREENB:
764 		rval = ccs_write(sensor, TEST_DATA_GREENB, ctrl->val);
765 
766 		break;
767 	case V4L2_CID_CCS_SHADING_CORRECTION:
768 		rval = ccs_write(sensor, SHADING_CORRECTION_EN,
769 				 ctrl->val ? CCS_SHADING_CORRECTION_EN_ENABLE :
770 				 0);
771 
772 		if (!rval && sensor->luminance_level)
773 			v4l2_ctrl_activate(sensor->luminance_level, ctrl->val);
774 
775 		break;
776 	case V4L2_CID_CCS_LUMINANCE_CORRECTION_LEVEL:
777 		rval = ccs_write(sensor, LUMINANCE_CORRECTION_LEVEL, ctrl->val);
778 
779 		break;
780 	case V4L2_CID_PIXEL_RATE:
781 		/* For v4l2_ctrl_s_ctrl_int64() used internally. */
782 		rval = 0;
783 
784 		break;
785 	default:
786 		rval = -EINVAL;
787 	}
788 
789 	if (pm_status > 0) {
790 		pm_runtime_mark_last_busy(&client->dev);
791 		pm_runtime_put_autosuspend(&client->dev);
792 	}
793 
794 	return rval;
795 }
796 
797 static const struct v4l2_ctrl_ops ccs_ctrl_ops = {
798 	.s_ctrl = ccs_set_ctrl,
799 };
800 
ccs_init_controls(struct ccs_sensor * sensor)801 static int ccs_init_controls(struct ccs_sensor *sensor)
802 {
803 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
804 	int rval;
805 
806 	rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 17);
807 	if (rval)
808 		return rval;
809 
810 	sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
811 
812 	switch (CCS_LIM(sensor, ANALOG_GAIN_CAPABILITY)) {
813 	case CCS_ANALOG_GAIN_CAPABILITY_GLOBAL: {
814 		struct {
815 			const char *name;
816 			u32 id;
817 			s32 value;
818 		} const gain_ctrls[] = {
819 			{ "Analogue Gain m0", V4L2_CID_CCS_ANALOGUE_GAIN_M0,
820 			  CCS_LIM(sensor, ANALOG_GAIN_M0), },
821 			{ "Analogue Gain c0", V4L2_CID_CCS_ANALOGUE_GAIN_C0,
822 			  CCS_LIM(sensor, ANALOG_GAIN_C0), },
823 			{ "Analogue Gain m1", V4L2_CID_CCS_ANALOGUE_GAIN_M1,
824 			  CCS_LIM(sensor, ANALOG_GAIN_M1), },
825 			{ "Analogue Gain c1", V4L2_CID_CCS_ANALOGUE_GAIN_C1,
826 			  CCS_LIM(sensor, ANALOG_GAIN_C1), },
827 		};
828 		struct v4l2_ctrl_config ctrl_cfg = {
829 			.type = V4L2_CTRL_TYPE_INTEGER,
830 			.ops = &ccs_ctrl_ops,
831 			.flags = V4L2_CTRL_FLAG_READ_ONLY,
832 			.step = 1,
833 		};
834 		unsigned int i;
835 
836 		for (i = 0; i < ARRAY_SIZE(gain_ctrls); i++) {
837 			ctrl_cfg.name = gain_ctrls[i].name;
838 			ctrl_cfg.id = gain_ctrls[i].id;
839 			ctrl_cfg.min = ctrl_cfg.max = ctrl_cfg.def =
840 				gain_ctrls[i].value;
841 
842 			v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler,
843 					     &ctrl_cfg, NULL);
844 		}
845 
846 		v4l2_ctrl_new_std(&sensor->pixel_array->ctrl_handler,
847 				  &ccs_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
848 				  CCS_LIM(sensor, ANALOG_GAIN_CODE_MIN),
849 				  CCS_LIM(sensor, ANALOG_GAIN_CODE_MAX),
850 				  max(CCS_LIM(sensor, ANALOG_GAIN_CODE_STEP),
851 				      1U),
852 				  CCS_LIM(sensor, ANALOG_GAIN_CODE_MIN));
853 	}
854 		break;
855 
856 	case CCS_ANALOG_GAIN_CAPABILITY_ALTERNATE_GLOBAL: {
857 		struct {
858 			const char *name;
859 			u32 id;
860 			u16 min, max, step;
861 		} const gain_ctrls[] = {
862 			{
863 				"Analogue Linear Gain",
864 				V4L2_CID_CCS_ANALOGUE_LINEAR_GAIN,
865 				CCS_LIM(sensor, ANALOG_LINEAR_GAIN_MIN),
866 				CCS_LIM(sensor, ANALOG_LINEAR_GAIN_MAX),
867 				max(CCS_LIM(sensor,
868 					    ANALOG_LINEAR_GAIN_STEP_SIZE),
869 				    1U),
870 			},
871 			{
872 				"Analogue Exponential Gain",
873 				V4L2_CID_CCS_ANALOGUE_EXPONENTIAL_GAIN,
874 				CCS_LIM(sensor, ANALOG_EXPONENTIAL_GAIN_MIN),
875 				CCS_LIM(sensor, ANALOG_EXPONENTIAL_GAIN_MAX),
876 				max(CCS_LIM(sensor,
877 					    ANALOG_EXPONENTIAL_GAIN_STEP_SIZE),
878 				    1U),
879 			},
880 		};
881 		struct v4l2_ctrl_config ctrl_cfg = {
882 			.type = V4L2_CTRL_TYPE_INTEGER,
883 			.ops = &ccs_ctrl_ops,
884 		};
885 		unsigned int i;
886 
887 		for (i = 0; i < ARRAY_SIZE(gain_ctrls); i++) {
888 			ctrl_cfg.name = gain_ctrls[i].name;
889 			ctrl_cfg.min = ctrl_cfg.def = gain_ctrls[i].min;
890 			ctrl_cfg.max = gain_ctrls[i].max;
891 			ctrl_cfg.step = gain_ctrls[i].step;
892 			ctrl_cfg.id = gain_ctrls[i].id;
893 
894 			v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler,
895 					     &ctrl_cfg, NULL);
896 		}
897 	}
898 	}
899 
900 	if (CCS_LIM(sensor, SHADING_CORRECTION_CAPABILITY) &
901 	    (CCS_SHADING_CORRECTION_CAPABILITY_COLOR_SHADING |
902 	     CCS_SHADING_CORRECTION_CAPABILITY_LUMINANCE_CORRECTION)) {
903 		const struct v4l2_ctrl_config ctrl_cfg = {
904 			.name = "Shading Correction",
905 			.type = V4L2_CTRL_TYPE_BOOLEAN,
906 			.id = V4L2_CID_CCS_SHADING_CORRECTION,
907 			.ops = &ccs_ctrl_ops,
908 			.max = 1,
909 			.step = 1,
910 		};
911 
912 		v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler,
913 				     &ctrl_cfg, NULL);
914 	}
915 
916 	if (CCS_LIM(sensor, SHADING_CORRECTION_CAPABILITY) &
917 	    CCS_SHADING_CORRECTION_CAPABILITY_LUMINANCE_CORRECTION) {
918 		const struct v4l2_ctrl_config ctrl_cfg = {
919 			.name = "Luminance Correction Level",
920 			.type = V4L2_CTRL_TYPE_BOOLEAN,
921 			.id = V4L2_CID_CCS_LUMINANCE_CORRECTION_LEVEL,
922 			.ops = &ccs_ctrl_ops,
923 			.max = 255,
924 			.step = 1,
925 			.def = 128,
926 		};
927 
928 		sensor->luminance_level =
929 			v4l2_ctrl_new_custom(&sensor->pixel_array->ctrl_handler,
930 					     &ctrl_cfg, NULL);
931 	}
932 
933 	if (CCS_LIM(sensor, DIGITAL_GAIN_CAPABILITY) ==
934 	    CCS_DIGITAL_GAIN_CAPABILITY_GLOBAL ||
935 	    CCS_LIM(sensor, DIGITAL_GAIN_CAPABILITY) ==
936 	    SMIAPP_DIGITAL_GAIN_CAPABILITY_PER_CHANNEL)
937 		v4l2_ctrl_new_std(&sensor->pixel_array->ctrl_handler,
938 				  &ccs_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
939 				  CCS_LIM(sensor, DIGITAL_GAIN_MIN),
940 				  CCS_LIM(sensor, DIGITAL_GAIN_MAX),
941 				  max(CCS_LIM(sensor, DIGITAL_GAIN_STEP_SIZE),
942 				      1U),
943 				  0x100);
944 
945 	/* Exposure limits will be updated soon, use just something here. */
946 	sensor->exposure = v4l2_ctrl_new_std(
947 		&sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
948 		V4L2_CID_EXPOSURE, 0, 0, 1, 0);
949 
950 	sensor->hflip = v4l2_ctrl_new_std(
951 		&sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
952 		V4L2_CID_HFLIP, 0, 1, 1, 0);
953 	sensor->vflip = v4l2_ctrl_new_std(
954 		&sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
955 		V4L2_CID_VFLIP, 0, 1, 1, 0);
956 
957 	sensor->vblank = v4l2_ctrl_new_std(
958 		&sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
959 		V4L2_CID_VBLANK, 0, 1, 1, 0);
960 
961 	if (sensor->vblank)
962 		sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
963 
964 	sensor->hblank = v4l2_ctrl_new_std(
965 		&sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
966 		V4L2_CID_HBLANK, 0, 1, 1, 0);
967 
968 	if (sensor->hblank)
969 		sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
970 
971 	sensor->pixel_rate_parray = v4l2_ctrl_new_std(
972 		&sensor->pixel_array->ctrl_handler, &ccs_ctrl_ops,
973 		V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
974 
975 	v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler,
976 				     &ccs_ctrl_ops, V4L2_CID_TEST_PATTERN,
977 				     ARRAY_SIZE(ccs_test_patterns) - 1,
978 				     0, 0, ccs_test_patterns);
979 
980 	if (sensor->pixel_array->ctrl_handler.error) {
981 		dev_err(&client->dev,
982 			"pixel array controls initialization failed (%d)\n",
983 			sensor->pixel_array->ctrl_handler.error);
984 		return sensor->pixel_array->ctrl_handler.error;
985 	}
986 
987 	sensor->pixel_array->sd.ctrl_handler =
988 		&sensor->pixel_array->ctrl_handler;
989 
990 	v4l2_ctrl_cluster(2, &sensor->hflip);
991 
992 	rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
993 	if (rval)
994 		return rval;
995 
996 	sensor->src->ctrl_handler.lock = &sensor->mutex;
997 
998 	sensor->pixel_rate_csi = v4l2_ctrl_new_std(
999 		&sensor->src->ctrl_handler, &ccs_ctrl_ops,
1000 		V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
1001 
1002 	if (sensor->src->ctrl_handler.error) {
1003 		dev_err(&client->dev,
1004 			"src controls initialization failed (%d)\n",
1005 			sensor->src->ctrl_handler.error);
1006 		return sensor->src->ctrl_handler.error;
1007 	}
1008 
1009 	sensor->src->sd.ctrl_handler = &sensor->src->ctrl_handler;
1010 
1011 	return 0;
1012 }
1013 
1014 /*
1015  * For controls that require information on available media bus codes
1016  * and linke frequencies.
1017  */
ccs_init_late_controls(struct ccs_sensor * sensor)1018 static int ccs_init_late_controls(struct ccs_sensor *sensor)
1019 {
1020 	unsigned long *valid_link_freqs = &sensor->valid_link_freqs[
1021 		sensor->csi_format->compressed - sensor->compressed_min_bpp];
1022 	unsigned int i;
1023 
1024 	for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) {
1025 		int max_value = (1 << sensor->csi_format->width) - 1;
1026 
1027 		sensor->test_data[i] = v4l2_ctrl_new_std(
1028 				&sensor->pixel_array->ctrl_handler,
1029 				&ccs_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i,
1030 				0, max_value, 1, max_value);
1031 	}
1032 
1033 	sensor->link_freq = v4l2_ctrl_new_int_menu(
1034 		&sensor->src->ctrl_handler, &ccs_ctrl_ops,
1035 		V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs),
1036 		__ffs(*valid_link_freqs), sensor->hwcfg.op_sys_clock);
1037 
1038 	return sensor->src->ctrl_handler.error;
1039 }
1040 
ccs_free_controls(struct ccs_sensor * sensor)1041 static void ccs_free_controls(struct ccs_sensor *sensor)
1042 {
1043 	unsigned int i;
1044 
1045 	for (i = 0; i < sensor->ssds_used; i++)
1046 		v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
1047 }
1048 
ccs_get_mbus_formats(struct ccs_sensor * sensor)1049 static int ccs_get_mbus_formats(struct ccs_sensor *sensor)
1050 {
1051 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1052 	struct ccs_pll *pll = &sensor->pll;
1053 	u8 compressed_max_bpp = 0;
1054 	unsigned int type, n;
1055 	unsigned int i, pixel_order;
1056 	int rval;
1057 
1058 	type = CCS_LIM(sensor, DATA_FORMAT_MODEL_TYPE);
1059 
1060 	dev_dbg(&client->dev, "data_format_model_type %d\n", type);
1061 
1062 	rval = ccs_read(sensor, PIXEL_ORDER, &pixel_order);
1063 	if (rval)
1064 		return rval;
1065 
1066 	if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
1067 		dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
1068 		return -EINVAL;
1069 	}
1070 
1071 	dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
1072 		pixel_order_str[pixel_order]);
1073 
1074 	switch (type) {
1075 	case CCS_DATA_FORMAT_MODEL_TYPE_NORMAL:
1076 		n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
1077 		break;
1078 	case CCS_DATA_FORMAT_MODEL_TYPE_EXTENDED:
1079 		n = CCS_LIM_DATA_FORMAT_DESCRIPTOR_MAX_N + 1;
1080 		break;
1081 	default:
1082 		return -EINVAL;
1083 	}
1084 
1085 	sensor->default_pixel_order = pixel_order;
1086 	sensor->mbus_frame_fmts = 0;
1087 
1088 	for (i = 0; i < n; i++) {
1089 		unsigned int fmt, j;
1090 
1091 		fmt = CCS_LIM_AT(sensor, DATA_FORMAT_DESCRIPTOR, i);
1092 
1093 		dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
1094 			i, fmt >> 8, (u8)fmt);
1095 
1096 		for (j = 0; j < ARRAY_SIZE(ccs_csi_data_formats); j++) {
1097 			const struct ccs_csi_data_format *f =
1098 				&ccs_csi_data_formats[j];
1099 
1100 			if (f->pixel_order != CCS_PIXEL_ORDER_GRBG)
1101 				continue;
1102 
1103 			if (f->width != fmt >>
1104 			    CCS_DATA_FORMAT_DESCRIPTOR_UNCOMPRESSED_SHIFT ||
1105 			    f->compressed !=
1106 			    (fmt & CCS_DATA_FORMAT_DESCRIPTOR_COMPRESSED_MASK))
1107 				continue;
1108 
1109 			dev_dbg(&client->dev, "jolly good! %d\n", j);
1110 
1111 			sensor->default_mbus_frame_fmts |= 1 << j;
1112 		}
1113 	}
1114 
1115 	/* Figure out which BPP values can be used with which formats. */
1116 	pll->binning_horizontal = 1;
1117 	pll->binning_vertical = 1;
1118 	pll->scale_m = sensor->scale_m;
1119 
1120 	for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) {
1121 		sensor->compressed_min_bpp =
1122 			min(ccs_csi_data_formats[i].compressed,
1123 			    sensor->compressed_min_bpp);
1124 		compressed_max_bpp =
1125 			max(ccs_csi_data_formats[i].compressed,
1126 			    compressed_max_bpp);
1127 	}
1128 
1129 	sensor->valid_link_freqs = devm_kcalloc(
1130 		&client->dev,
1131 		compressed_max_bpp - sensor->compressed_min_bpp + 1,
1132 		sizeof(*sensor->valid_link_freqs), GFP_KERNEL);
1133 	if (!sensor->valid_link_freqs)
1134 		return -ENOMEM;
1135 
1136 	for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) {
1137 		const struct ccs_csi_data_format *f =
1138 			&ccs_csi_data_formats[i];
1139 		unsigned long *valid_link_freqs =
1140 			&sensor->valid_link_freqs[
1141 				f->compressed - sensor->compressed_min_bpp];
1142 		unsigned int j;
1143 
1144 		if (!(sensor->default_mbus_frame_fmts & 1 << i))
1145 			continue;
1146 
1147 		pll->bits_per_pixel = f->compressed;
1148 
1149 		for (j = 0; sensor->hwcfg.op_sys_clock[j]; j++) {
1150 			pll->link_freq = sensor->hwcfg.op_sys_clock[j];
1151 
1152 			rval = ccs_pll_try(sensor, pll);
1153 			dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n",
1154 				pll->link_freq, pll->bits_per_pixel,
1155 				rval ? "not ok" : "ok");
1156 			if (rval)
1157 				continue;
1158 
1159 			set_bit(j, valid_link_freqs);
1160 		}
1161 
1162 		if (!*valid_link_freqs) {
1163 			dev_info(&client->dev,
1164 				 "no valid link frequencies for %u bpp\n",
1165 				 f->compressed);
1166 			sensor->default_mbus_frame_fmts &= ~BIT(i);
1167 			continue;
1168 		}
1169 
1170 		if (!sensor->csi_format
1171 		    || f->width > sensor->csi_format->width
1172 		    || (f->width == sensor->csi_format->width
1173 			&& f->compressed > sensor->csi_format->compressed)) {
1174 			sensor->csi_format = f;
1175 			sensor->internal_csi_format = f;
1176 		}
1177 	}
1178 
1179 	if (!sensor->csi_format) {
1180 		dev_err(&client->dev, "no supported mbus code found\n");
1181 		return -EINVAL;
1182 	}
1183 
1184 	ccs_update_mbus_formats(sensor);
1185 
1186 	return 0;
1187 }
1188 
ccs_update_blanking(struct ccs_sensor * sensor)1189 static void ccs_update_blanking(struct ccs_sensor *sensor)
1190 {
1191 	struct v4l2_ctrl *vblank = sensor->vblank;
1192 	struct v4l2_ctrl *hblank = sensor->hblank;
1193 	u16 min_fll, max_fll, min_llp, max_llp, min_lbp;
1194 	int min, max;
1195 
1196 	if (sensor->binning_vertical > 1 || sensor->binning_horizontal > 1) {
1197 		min_fll = CCS_LIM(sensor, MIN_FRAME_LENGTH_LINES_BIN);
1198 		max_fll = CCS_LIM(sensor, MAX_FRAME_LENGTH_LINES_BIN);
1199 		min_llp = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK_BIN);
1200 		max_llp = CCS_LIM(sensor, MAX_LINE_LENGTH_PCK_BIN);
1201 		min_lbp = CCS_LIM(sensor, MIN_LINE_BLANKING_PCK_BIN);
1202 	} else {
1203 		min_fll = CCS_LIM(sensor, MIN_FRAME_LENGTH_LINES);
1204 		max_fll = CCS_LIM(sensor, MAX_FRAME_LENGTH_LINES);
1205 		min_llp = CCS_LIM(sensor, MIN_LINE_LENGTH_PCK);
1206 		max_llp = CCS_LIM(sensor, MAX_LINE_LENGTH_PCK);
1207 		min_lbp = CCS_LIM(sensor, MIN_LINE_BLANKING_PCK);
1208 	}
1209 
1210 	min = max_t(int,
1211 		    CCS_LIM(sensor, MIN_FRAME_BLANKING_LINES),
1212 		    min_fll - sensor->pixel_array->crop[CCS_PA_PAD_SRC].height);
1213 	max = max_fll -	sensor->pixel_array->crop[CCS_PA_PAD_SRC].height;
1214 
1215 	__v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);
1216 
1217 	min = max_t(int,
1218 		    min_llp - sensor->pixel_array->crop[CCS_PA_PAD_SRC].width,
1219 		    min_lbp);
1220 	max = max_llp - sensor->pixel_array->crop[CCS_PA_PAD_SRC].width;
1221 
1222 	__v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
1223 
1224 	__ccs_update_exposure_limits(sensor);
1225 }
1226 
ccs_pll_blanking_update(struct ccs_sensor * sensor)1227 static int ccs_pll_blanking_update(struct ccs_sensor *sensor)
1228 {
1229 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1230 	int rval;
1231 
1232 	rval = ccs_pll_update(sensor);
1233 	if (rval < 0)
1234 		return rval;
1235 
1236 	/* Output from pixel array, including blanking */
1237 	ccs_update_blanking(sensor);
1238 
1239 	dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
1240 	dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
1241 
1242 	dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
1243 		sensor->pll.pixel_rate_pixel_array /
1244 		((sensor->pixel_array->crop[CCS_PA_PAD_SRC].width
1245 		  + sensor->hblank->val) *
1246 		 (sensor->pixel_array->crop[CCS_PA_PAD_SRC].height
1247 		  + sensor->vblank->val) / 100));
1248 
1249 	return 0;
1250 }
1251 
1252 /*
1253  *
1254  * SMIA++ NVM handling
1255  *
1256  */
1257 
ccs_read_nvm_page(struct ccs_sensor * sensor,u32 p,u8 * nvm,u8 * status)1258 static int ccs_read_nvm_page(struct ccs_sensor *sensor, u32 p, u8 *nvm,
1259 			     u8 *status)
1260 {
1261 	unsigned int i;
1262 	int rval;
1263 	u32 s;
1264 
1265 	*status = 0;
1266 
1267 	rval = ccs_write(sensor, DATA_TRANSFER_IF_1_PAGE_SELECT, p);
1268 	if (rval)
1269 		return rval;
1270 
1271 	rval = ccs_write(sensor, DATA_TRANSFER_IF_1_CTRL,
1272 			 CCS_DATA_TRANSFER_IF_1_CTRL_ENABLE);
1273 	if (rval)
1274 		return rval;
1275 
1276 	rval = ccs_read(sensor, DATA_TRANSFER_IF_1_STATUS, &s);
1277 	if (rval)
1278 		return rval;
1279 
1280 	if (s & CCS_DATA_TRANSFER_IF_1_STATUS_IMPROPER_IF_USAGE) {
1281 		*status = s;
1282 		return -ENODATA;
1283 	}
1284 
1285 	if (CCS_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) &
1286 	    CCS_DATA_TRANSFER_IF_CAPABILITY_POLLING) {
1287 		for (i = 1000; i > 0; i--) {
1288 			if (s & CCS_DATA_TRANSFER_IF_1_STATUS_READ_IF_READY)
1289 				break;
1290 
1291 			rval = ccs_read(sensor, DATA_TRANSFER_IF_1_STATUS, &s);
1292 			if (rval)
1293 				return rval;
1294 		}
1295 
1296 		if (!i)
1297 			return -ETIMEDOUT;
1298 	}
1299 
1300 	for (i = 0; i <= CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P; i++) {
1301 		u32 v;
1302 
1303 		rval = ccs_read(sensor, DATA_TRANSFER_IF_1_DATA(i), &v);
1304 		if (rval)
1305 			return rval;
1306 
1307 		*nvm++ = v;
1308 	}
1309 
1310 	return 0;
1311 }
1312 
ccs_read_nvm(struct ccs_sensor * sensor,unsigned char * nvm,size_t nvm_size)1313 static int ccs_read_nvm(struct ccs_sensor *sensor, unsigned char *nvm,
1314 			size_t nvm_size)
1315 {
1316 	u8 status = 0;
1317 	u32 p;
1318 	int rval = 0, rval2;
1319 
1320 	for (p = 0; p < nvm_size / (CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P + 1)
1321 		     && !rval; p++) {
1322 		rval = ccs_read_nvm_page(sensor, p, nvm, &status);
1323 		nvm += CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P + 1;
1324 	}
1325 
1326 	if (rval == -ENODATA &&
1327 	    status & CCS_DATA_TRANSFER_IF_1_STATUS_IMPROPER_IF_USAGE)
1328 		rval = 0;
1329 
1330 	rval2 = ccs_write(sensor, DATA_TRANSFER_IF_1_CTRL, 0);
1331 	if (rval < 0)
1332 		return rval;
1333 	else
1334 		return rval2 ?: p * (CCS_LIM_DATA_TRANSFER_IF_1_DATA_MAX_P + 1);
1335 }
1336 
1337 /*
1338  *
1339  * SMIA++ CCI address control
1340  *
1341  */
ccs_change_cci_addr(struct ccs_sensor * sensor)1342 static int ccs_change_cci_addr(struct ccs_sensor *sensor)
1343 {
1344 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1345 	int rval;
1346 	u32 val;
1347 
1348 	client->addr = sensor->hwcfg.i2c_addr_dfl;
1349 
1350 	rval = ccs_write(sensor, CCI_ADDRESS_CTRL,
1351 			 sensor->hwcfg.i2c_addr_alt << 1);
1352 	if (rval)
1353 		return rval;
1354 
1355 	client->addr = sensor->hwcfg.i2c_addr_alt;
1356 
1357 	/* verify addr change went ok */
1358 	rval = ccs_read(sensor, CCI_ADDRESS_CTRL, &val);
1359 	if (rval)
1360 		return rval;
1361 
1362 	if (val != sensor->hwcfg.i2c_addr_alt << 1)
1363 		return -ENODEV;
1364 
1365 	return 0;
1366 }
1367 
1368 /*
1369  *
1370  * SMIA++ Mode Control
1371  *
1372  */
ccs_setup_flash_strobe(struct ccs_sensor * sensor)1373 static int ccs_setup_flash_strobe(struct ccs_sensor *sensor)
1374 {
1375 	struct ccs_flash_strobe_parms *strobe_setup;
1376 	unsigned int ext_freq = sensor->hwcfg.ext_clk;
1377 	u32 tmp;
1378 	u32 strobe_adjustment;
1379 	u32 strobe_width_high_rs;
1380 	int rval;
1381 
1382 	strobe_setup = sensor->hwcfg.strobe_setup;
1383 
1384 	/*
1385 	 * How to calculate registers related to strobe length. Please
1386 	 * do not change, or if you do at least know what you're
1387 	 * doing. :-)
1388 	 *
1389 	 * Sakari Ailus <sakari.ailus@linux.intel.com> 2010-10-25
1390 	 *
1391 	 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1392 	 *	/ EXTCLK freq [Hz]) * flash_strobe_adjustment
1393 	 *
1394 	 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1395 	 * flash_strobe_adjustment E N, [1 - 0xff]
1396 	 *
1397 	 * The formula above is written as below to keep it on one
1398 	 * line:
1399 	 *
1400 	 * l / 10^6 = w / e * a
1401 	 *
1402 	 * Let's mark w * a by x:
1403 	 *
1404 	 * x = w * a
1405 	 *
1406 	 * Thus, we get:
1407 	 *
1408 	 * x = l * e / 10^6
1409 	 *
1410 	 * The strobe width must be at least as long as requested,
1411 	 * thus rounding upwards is needed.
1412 	 *
1413 	 * x = (l * e + 10^6 - 1) / 10^6
1414 	 * -----------------------------
1415 	 *
1416 	 * Maximum possible accuracy is wanted at all times. Thus keep
1417 	 * a as small as possible.
1418 	 *
1419 	 * Calculate a, assuming maximum w, with rounding upwards:
1420 	 *
1421 	 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1422 	 * -------------------------------------
1423 	 *
1424 	 * Thus, we also get w, with that a, with rounding upwards:
1425 	 *
1426 	 * w = (x + a - 1) / a
1427 	 * -------------------
1428 	 *
1429 	 * To get limits:
1430 	 *
1431 	 * x E [1, (2^16 - 1) * (2^8 - 1)]
1432 	 *
1433 	 * Substituting maximum x to the original formula (with rounding),
1434 	 * the maximum l is thus
1435 	 *
1436 	 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1437 	 *
1438 	 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1439 	 * --------------------------------------------------
1440 	 *
1441 	 * flash_strobe_length must be clamped between 1 and
1442 	 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1443 	 *
1444 	 * Then,
1445 	 *
1446 	 * flash_strobe_adjustment = ((flash_strobe_length *
1447 	 *	EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1448 	 *
1449 	 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1450 	 *	EXTCLK freq + 10^6 - 1) / 10^6 +
1451 	 *	flash_strobe_adjustment - 1) / flash_strobe_adjustment
1452 	 */
1453 	tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1454 		      1000000 + 1, ext_freq);
1455 	strobe_setup->strobe_width_high_us =
1456 		clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1457 
1458 	tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1459 			1000000 - 1), 1000000ULL);
1460 	strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1461 	strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1462 				strobe_adjustment;
1463 
1464 	rval = ccs_write(sensor, FLASH_MODE_RS, strobe_setup->mode);
1465 	if (rval < 0)
1466 		goto out;
1467 
1468 	rval = ccs_write(sensor, FLASH_STROBE_ADJUSTMENT, strobe_adjustment);
1469 	if (rval < 0)
1470 		goto out;
1471 
1472 	rval = ccs_write(sensor, TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1473 			 strobe_width_high_rs);
1474 	if (rval < 0)
1475 		goto out;
1476 
1477 	rval = ccs_write(sensor, TFLASH_STROBE_DELAY_RS_CTRL,
1478 			 strobe_setup->strobe_delay);
1479 	if (rval < 0)
1480 		goto out;
1481 
1482 	rval = ccs_write(sensor, FLASH_STROBE_START_POINT,
1483 			 strobe_setup->stobe_start_point);
1484 	if (rval < 0)
1485 		goto out;
1486 
1487 	rval = ccs_write(sensor, FLASH_TRIGGER_RS, strobe_setup->trigger);
1488 
1489 out:
1490 	sensor->hwcfg.strobe_setup->trigger = 0;
1491 
1492 	return rval;
1493 }
1494 
1495 /* -----------------------------------------------------------------------------
1496  * Power management
1497  */
1498 
ccs_write_msr_regs(struct ccs_sensor * sensor)1499 static int ccs_write_msr_regs(struct ccs_sensor *sensor)
1500 {
1501 	int rval;
1502 
1503 	rval = ccs_write_data_regs(sensor,
1504 				   sensor->sdata.sensor_manufacturer_regs,
1505 				   sensor->sdata.num_sensor_manufacturer_regs);
1506 	if (rval)
1507 		return rval;
1508 
1509 	return ccs_write_data_regs(sensor,
1510 				   sensor->mdata.module_manufacturer_regs,
1511 				   sensor->mdata.num_module_manufacturer_regs);
1512 }
1513 
ccs_update_phy_ctrl(struct ccs_sensor * sensor)1514 static int ccs_update_phy_ctrl(struct ccs_sensor *sensor)
1515 {
1516 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1517 	u8 val;
1518 
1519 	if (!sensor->ccs_limits)
1520 		return 0;
1521 
1522 	if (CCS_LIM(sensor, PHY_CTRL_CAPABILITY) &
1523 	    CCS_PHY_CTRL_CAPABILITY_AUTO_PHY_CTL) {
1524 		val = CCS_PHY_CTRL_AUTO;
1525 	} else if (CCS_LIM(sensor, PHY_CTRL_CAPABILITY) &
1526 		   CCS_PHY_CTRL_CAPABILITY_UI_PHY_CTL) {
1527 		val = CCS_PHY_CTRL_UI;
1528 	} else {
1529 		dev_err(&client->dev, "manual PHY control not supported\n");
1530 		return -EINVAL;
1531 	}
1532 
1533 	return ccs_write(sensor, PHY_CTRL, val);
1534 }
1535 
ccs_power_on(struct device * dev)1536 static int ccs_power_on(struct device *dev)
1537 {
1538 	struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1539 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
1540 	/*
1541 	 * The sub-device related to the I2C device is always the
1542 	 * source one, i.e. ssds[0].
1543 	 */
1544 	struct ccs_sensor *sensor =
1545 		container_of(ssd, struct ccs_sensor, ssds[0]);
1546 	const struct ccs_device *ccsdev = device_get_match_data(dev);
1547 	int rval;
1548 
1549 	rval = regulator_bulk_enable(ARRAY_SIZE(ccs_regulators),
1550 				     sensor->regulators);
1551 	if (rval) {
1552 		dev_err(dev, "failed to enable vana regulator\n");
1553 		return rval;
1554 	}
1555 
1556 	if (sensor->reset || sensor->xshutdown || sensor->ext_clk) {
1557 		unsigned int sleep;
1558 
1559 		rval = clk_prepare_enable(sensor->ext_clk);
1560 		if (rval < 0) {
1561 			dev_dbg(dev, "failed to enable xclk\n");
1562 			goto out_xclk_fail;
1563 		}
1564 
1565 		gpiod_set_value(sensor->reset, 0);
1566 		gpiod_set_value(sensor->xshutdown, 1);
1567 
1568 		if (ccsdev->flags & CCS_DEVICE_FLAG_IS_SMIA)
1569 			sleep = SMIAPP_RESET_DELAY(sensor->hwcfg.ext_clk);
1570 		else
1571 			sleep = 5000;
1572 
1573 		usleep_range(sleep, sleep);
1574 	}
1575 
1576 	/*
1577 	 * Failures to respond to the address change command have been noticed.
1578 	 * Those failures seem to be caused by the sensor requiring a longer
1579 	 * boot time than advertised. An additional 10ms delay seems to work
1580 	 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1581 	 * unnecessary. The failures need to be investigated to find a proper
1582 	 * fix, and a delay will likely need to be added here if the I2C write
1583 	 * retry hack is reverted before the root cause of the boot time issue
1584 	 * is found.
1585 	 */
1586 
1587 	if (!sensor->reset && !sensor->xshutdown) {
1588 		u8 retry = 100;
1589 		u32 reset;
1590 
1591 		rval = ccs_write(sensor, SOFTWARE_RESET, CCS_SOFTWARE_RESET_ON);
1592 		if (rval < 0) {
1593 			dev_err(dev, "software reset failed\n");
1594 			goto out_cci_addr_fail;
1595 		}
1596 
1597 		do {
1598 			rval = ccs_read(sensor, SOFTWARE_RESET, &reset);
1599 			reset = !rval && reset == CCS_SOFTWARE_RESET_OFF;
1600 			if (reset)
1601 				break;
1602 
1603 			usleep_range(1000, 2000);
1604 		} while (--retry);
1605 
1606 		if (!reset) {
1607 			dev_err(dev, "software reset failed\n");
1608 			rval = -EIO;
1609 			goto out_cci_addr_fail;
1610 		}
1611 	}
1612 
1613 	if (sensor->hwcfg.i2c_addr_alt) {
1614 		rval = ccs_change_cci_addr(sensor);
1615 		if (rval) {
1616 			dev_err(dev, "cci address change error\n");
1617 			goto out_cci_addr_fail;
1618 		}
1619 	}
1620 
1621 	rval = ccs_write(sensor, COMPRESSION_MODE,
1622 			 CCS_COMPRESSION_MODE_DPCM_PCM_SIMPLE);
1623 	if (rval) {
1624 		dev_err(dev, "compression mode set failed\n");
1625 		goto out_cci_addr_fail;
1626 	}
1627 
1628 	rval = ccs_write(sensor, EXTCLK_FREQUENCY_MHZ,
1629 			 sensor->hwcfg.ext_clk / (1000000 / (1 << 8)));
1630 	if (rval) {
1631 		dev_err(dev, "extclk frequency set failed\n");
1632 		goto out_cci_addr_fail;
1633 	}
1634 
1635 	rval = ccs_write(sensor, CSI_LANE_MODE, sensor->hwcfg.lanes - 1);
1636 	if (rval) {
1637 		dev_err(dev, "csi lane mode set failed\n");
1638 		goto out_cci_addr_fail;
1639 	}
1640 
1641 	rval = ccs_write(sensor, FAST_STANDBY_CTRL,
1642 			 CCS_FAST_STANDBY_CTRL_FRAME_TRUNCATION);
1643 	if (rval) {
1644 		dev_err(dev, "fast standby set failed\n");
1645 		goto out_cci_addr_fail;
1646 	}
1647 
1648 	rval = ccs_write(sensor, CSI_SIGNALING_MODE,
1649 			 sensor->hwcfg.csi_signalling_mode);
1650 	if (rval) {
1651 		dev_err(dev, "csi signalling mode set failed\n");
1652 		goto out_cci_addr_fail;
1653 	}
1654 
1655 	rval = ccs_update_phy_ctrl(sensor);
1656 	if (rval < 0)
1657 		goto out_cci_addr_fail;
1658 
1659 	rval = ccs_write_msr_regs(sensor);
1660 	if (rval)
1661 		goto out_cci_addr_fail;
1662 
1663 	rval = ccs_call_quirk(sensor, post_poweron);
1664 	if (rval) {
1665 		dev_err(dev, "post_poweron quirks failed\n");
1666 		goto out_cci_addr_fail;
1667 	}
1668 
1669 	return 0;
1670 
1671 out_cci_addr_fail:
1672 	gpiod_set_value(sensor->reset, 1);
1673 	gpiod_set_value(sensor->xshutdown, 0);
1674 	clk_disable_unprepare(sensor->ext_clk);
1675 
1676 out_xclk_fail:
1677 	regulator_bulk_disable(ARRAY_SIZE(ccs_regulators),
1678 			       sensor->regulators);
1679 
1680 	return rval;
1681 }
1682 
ccs_power_off(struct device * dev)1683 static int ccs_power_off(struct device *dev)
1684 {
1685 	struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1686 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
1687 	struct ccs_sensor *sensor =
1688 		container_of(ssd, struct ccs_sensor, ssds[0]);
1689 
1690 	/*
1691 	 * Currently power/clock to lens are enable/disabled separately
1692 	 * but they are essentially the same signals. So if the sensor is
1693 	 * powered off while the lens is powered on the sensor does not
1694 	 * really see a power off and next time the cci address change
1695 	 * will fail. So do a soft reset explicitly here.
1696 	 */
1697 	if (sensor->hwcfg.i2c_addr_alt)
1698 		ccs_write(sensor, SOFTWARE_RESET, CCS_SOFTWARE_RESET_ON);
1699 
1700 	gpiod_set_value(sensor->reset, 1);
1701 	gpiod_set_value(sensor->xshutdown, 0);
1702 	clk_disable_unprepare(sensor->ext_clk);
1703 	usleep_range(5000, 5000);
1704 	regulator_bulk_disable(ARRAY_SIZE(ccs_regulators),
1705 			       sensor->regulators);
1706 	sensor->streaming = false;
1707 
1708 	return 0;
1709 }
1710 
1711 /* -----------------------------------------------------------------------------
1712  * Video stream management
1713  */
1714 
ccs_start_streaming(struct ccs_sensor * sensor)1715 static int ccs_start_streaming(struct ccs_sensor *sensor)
1716 {
1717 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1718 	unsigned int binning_mode;
1719 	int rval;
1720 
1721 	mutex_lock(&sensor->mutex);
1722 
1723 	rval = ccs_write(sensor, CSI_DATA_FORMAT,
1724 			 (sensor->csi_format->width << 8) |
1725 			 sensor->csi_format->compressed);
1726 	if (rval)
1727 		goto out;
1728 
1729 	/* Binning configuration */
1730 	if (sensor->binning_horizontal == 1 &&
1731 	    sensor->binning_vertical == 1) {
1732 		binning_mode = 0;
1733 	} else {
1734 		u8 binning_type =
1735 			(sensor->binning_horizontal << 4)
1736 			| sensor->binning_vertical;
1737 
1738 		rval = ccs_write(sensor, BINNING_TYPE, binning_type);
1739 		if (rval < 0)
1740 			goto out;
1741 
1742 		binning_mode = 1;
1743 	}
1744 	rval = ccs_write(sensor, BINNING_MODE, binning_mode);
1745 	if (rval < 0)
1746 		goto out;
1747 
1748 	/* Set up PLL */
1749 	rval = ccs_pll_configure(sensor);
1750 	if (rval)
1751 		goto out;
1752 
1753 	/* Analog crop start coordinates */
1754 	rval = ccs_write(sensor, X_ADDR_START,
1755 			 sensor->pixel_array->crop[CCS_PA_PAD_SRC].left);
1756 	if (rval < 0)
1757 		goto out;
1758 
1759 	rval = ccs_write(sensor, Y_ADDR_START,
1760 			 sensor->pixel_array->crop[CCS_PA_PAD_SRC].top);
1761 	if (rval < 0)
1762 		goto out;
1763 
1764 	/* Analog crop end coordinates */
1765 	rval = ccs_write(
1766 		sensor, X_ADDR_END,
1767 		sensor->pixel_array->crop[CCS_PA_PAD_SRC].left
1768 		+ sensor->pixel_array->crop[CCS_PA_PAD_SRC].width - 1);
1769 	if (rval < 0)
1770 		goto out;
1771 
1772 	rval = ccs_write(
1773 		sensor, Y_ADDR_END,
1774 		sensor->pixel_array->crop[CCS_PA_PAD_SRC].top
1775 		+ sensor->pixel_array->crop[CCS_PA_PAD_SRC].height - 1);
1776 	if (rval < 0)
1777 		goto out;
1778 
1779 	/*
1780 	 * Output from pixel array, including blanking, is set using
1781 	 * controls below. No need to set here.
1782 	 */
1783 
1784 	/* Digital crop */
1785 	if (CCS_LIM(sensor, DIGITAL_CROP_CAPABILITY)
1786 	    == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1787 		rval = ccs_write(
1788 			sensor, DIGITAL_CROP_X_OFFSET,
1789 			sensor->scaler->crop[CCS_PAD_SINK].left);
1790 		if (rval < 0)
1791 			goto out;
1792 
1793 		rval = ccs_write(
1794 			sensor, DIGITAL_CROP_Y_OFFSET,
1795 			sensor->scaler->crop[CCS_PAD_SINK].top);
1796 		if (rval < 0)
1797 			goto out;
1798 
1799 		rval = ccs_write(
1800 			sensor, DIGITAL_CROP_IMAGE_WIDTH,
1801 			sensor->scaler->crop[CCS_PAD_SINK].width);
1802 		if (rval < 0)
1803 			goto out;
1804 
1805 		rval = ccs_write(
1806 			sensor, DIGITAL_CROP_IMAGE_HEIGHT,
1807 			sensor->scaler->crop[CCS_PAD_SINK].height);
1808 		if (rval < 0)
1809 			goto out;
1810 	}
1811 
1812 	/* Scaling */
1813 	if (CCS_LIM(sensor, SCALING_CAPABILITY)
1814 	    != CCS_SCALING_CAPABILITY_NONE) {
1815 		rval = ccs_write(sensor, SCALING_MODE, sensor->scaling_mode);
1816 		if (rval < 0)
1817 			goto out;
1818 
1819 		rval = ccs_write(sensor, SCALE_M, sensor->scale_m);
1820 		if (rval < 0)
1821 			goto out;
1822 	}
1823 
1824 	/* Output size from sensor */
1825 	rval = ccs_write(sensor, X_OUTPUT_SIZE,
1826 			 sensor->src->crop[CCS_PAD_SRC].width);
1827 	if (rval < 0)
1828 		goto out;
1829 	rval = ccs_write(sensor, Y_OUTPUT_SIZE,
1830 			 sensor->src->crop[CCS_PAD_SRC].height);
1831 	if (rval < 0)
1832 		goto out;
1833 
1834 	if (CCS_LIM(sensor, FLASH_MODE_CAPABILITY) &
1835 	    (CCS_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1836 	     SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE) &&
1837 	    sensor->hwcfg.strobe_setup != NULL &&
1838 	    sensor->hwcfg.strobe_setup->trigger != 0) {
1839 		rval = ccs_setup_flash_strobe(sensor);
1840 		if (rval)
1841 			goto out;
1842 	}
1843 
1844 	rval = ccs_call_quirk(sensor, pre_streamon);
1845 	if (rval) {
1846 		dev_err(&client->dev, "pre_streamon quirks failed\n");
1847 		goto out;
1848 	}
1849 
1850 	rval = ccs_write(sensor, MODE_SELECT, CCS_MODE_SELECT_STREAMING);
1851 
1852 out:
1853 	mutex_unlock(&sensor->mutex);
1854 
1855 	return rval;
1856 }
1857 
ccs_stop_streaming(struct ccs_sensor * sensor)1858 static int ccs_stop_streaming(struct ccs_sensor *sensor)
1859 {
1860 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1861 	int rval;
1862 
1863 	mutex_lock(&sensor->mutex);
1864 	rval = ccs_write(sensor, MODE_SELECT, CCS_MODE_SELECT_SOFTWARE_STANDBY);
1865 	if (rval)
1866 		goto out;
1867 
1868 	rval = ccs_call_quirk(sensor, post_streamoff);
1869 	if (rval)
1870 		dev_err(&client->dev, "post_streamoff quirks failed\n");
1871 
1872 out:
1873 	mutex_unlock(&sensor->mutex);
1874 	return rval;
1875 }
1876 
1877 /* -----------------------------------------------------------------------------
1878  * V4L2 subdev video operations
1879  */
1880 
ccs_pm_get_init(struct ccs_sensor * sensor)1881 static int ccs_pm_get_init(struct ccs_sensor *sensor)
1882 {
1883 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1884 	int rval;
1885 
1886 	/*
1887 	 * It can't use pm_runtime_resume_and_get() here, as the driver
1888 	 * relies at the returned value to detect if the device was already
1889 	 * active or not.
1890 	 */
1891 	rval = pm_runtime_get_sync(&client->dev);
1892 	if (rval < 0)
1893 		goto error;
1894 
1895 	/* Device was already active, so don't set controls */
1896 	if (rval == 1)
1897 		return 0;
1898 
1899 	/* Restore V4L2 controls to the previously suspended device */
1900 	rval = v4l2_ctrl_handler_setup(&sensor->pixel_array->ctrl_handler);
1901 	if (rval)
1902 		goto error;
1903 
1904 	rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1905 	if (rval)
1906 		goto error;
1907 
1908 	/* Keep PM runtime usage_count incremented on success */
1909 	return 0;
1910 error:
1911 	pm_runtime_put(&client->dev);
1912 	return rval;
1913 }
1914 
ccs_set_stream(struct v4l2_subdev * subdev,int enable)1915 static int ccs_set_stream(struct v4l2_subdev *subdev, int enable)
1916 {
1917 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
1918 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1919 	int rval;
1920 
1921 	if (sensor->streaming == enable)
1922 		return 0;
1923 
1924 	if (!enable) {
1925 		ccs_stop_streaming(sensor);
1926 		sensor->streaming = false;
1927 		pm_runtime_mark_last_busy(&client->dev);
1928 		pm_runtime_put_autosuspend(&client->dev);
1929 
1930 		return 0;
1931 	}
1932 
1933 	rval = ccs_pm_get_init(sensor);
1934 	if (rval)
1935 		return rval;
1936 
1937 	sensor->streaming = true;
1938 
1939 	rval = ccs_start_streaming(sensor);
1940 	if (rval < 0) {
1941 		sensor->streaming = false;
1942 		pm_runtime_mark_last_busy(&client->dev);
1943 		pm_runtime_put_autosuspend(&client->dev);
1944 	}
1945 
1946 	return rval;
1947 }
1948 
ccs_pre_streamon(struct v4l2_subdev * subdev,u32 flags)1949 static int ccs_pre_streamon(struct v4l2_subdev *subdev, u32 flags)
1950 {
1951 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
1952 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1953 	int rval;
1954 
1955 	if (flags & V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP) {
1956 		switch (sensor->hwcfg.csi_signalling_mode) {
1957 		case CCS_CSI_SIGNALING_MODE_CSI_2_DPHY:
1958 			if (!(CCS_LIM(sensor, PHY_CTRL_CAPABILITY_2) &
1959 			      CCS_PHY_CTRL_CAPABILITY_2_MANUAL_LP_DPHY))
1960 				return -EACCES;
1961 			break;
1962 		case CCS_CSI_SIGNALING_MODE_CSI_2_CPHY:
1963 			if (!(CCS_LIM(sensor, PHY_CTRL_CAPABILITY_2) &
1964 			      CCS_PHY_CTRL_CAPABILITY_2_MANUAL_LP_CPHY))
1965 				return -EACCES;
1966 			break;
1967 		default:
1968 			return -EACCES;
1969 		}
1970 	}
1971 
1972 	rval = ccs_pm_get_init(sensor);
1973 	if (rval)
1974 		return rval;
1975 
1976 	if (flags & V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP) {
1977 		rval = ccs_write(sensor, MANUAL_LP_CTRL,
1978 				 CCS_MANUAL_LP_CTRL_ENABLE);
1979 		if (rval)
1980 			pm_runtime_put(&client->dev);
1981 	}
1982 
1983 	return rval;
1984 }
1985 
ccs_post_streamoff(struct v4l2_subdev * subdev)1986 static int ccs_post_streamoff(struct v4l2_subdev *subdev)
1987 {
1988 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
1989 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1990 
1991 	return pm_runtime_put(&client->dev);
1992 }
1993 
ccs_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)1994 static int ccs_enum_mbus_code(struct v4l2_subdev *subdev,
1995 			      struct v4l2_subdev_state *sd_state,
1996 			      struct v4l2_subdev_mbus_code_enum *code)
1997 {
1998 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
1999 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2000 	unsigned int i;
2001 	int idx = -1;
2002 	int rval = -EINVAL;
2003 
2004 	mutex_lock(&sensor->mutex);
2005 
2006 	dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
2007 		subdev->name, code->pad, code->index);
2008 
2009 	if (subdev != &sensor->src->sd || code->pad != CCS_PAD_SRC) {
2010 		if (code->index)
2011 			goto out;
2012 
2013 		code->code = sensor->internal_csi_format->code;
2014 		rval = 0;
2015 		goto out;
2016 	}
2017 
2018 	for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) {
2019 		if (sensor->mbus_frame_fmts & (1 << i))
2020 			idx++;
2021 
2022 		if (idx == code->index) {
2023 			code->code = ccs_csi_data_formats[i].code;
2024 			dev_err(&client->dev, "found index %d, i %d, code %x\n",
2025 				code->index, i, code->code);
2026 			rval = 0;
2027 			break;
2028 		}
2029 	}
2030 
2031 out:
2032 	mutex_unlock(&sensor->mutex);
2033 
2034 	return rval;
2035 }
2036 
__ccs_get_mbus_code(struct v4l2_subdev * subdev,unsigned int pad)2037 static u32 __ccs_get_mbus_code(struct v4l2_subdev *subdev, unsigned int pad)
2038 {
2039 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2040 
2041 	if (subdev == &sensor->src->sd && pad == CCS_PAD_SRC)
2042 		return sensor->csi_format->code;
2043 	else
2044 		return sensor->internal_csi_format->code;
2045 }
2046 
__ccs_get_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)2047 static int __ccs_get_format(struct v4l2_subdev *subdev,
2048 			    struct v4l2_subdev_state *sd_state,
2049 			    struct v4l2_subdev_format *fmt)
2050 {
2051 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2052 
2053 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2054 		fmt->format = *v4l2_subdev_get_try_format(subdev, sd_state,
2055 							  fmt->pad);
2056 	} else {
2057 		struct v4l2_rect *r;
2058 
2059 		if (fmt->pad == ssd->source_pad)
2060 			r = &ssd->crop[ssd->source_pad];
2061 		else
2062 			r = &ssd->sink_fmt;
2063 
2064 		fmt->format.code = __ccs_get_mbus_code(subdev, fmt->pad);
2065 		fmt->format.width = r->width;
2066 		fmt->format.height = r->height;
2067 		fmt->format.field = V4L2_FIELD_NONE;
2068 	}
2069 
2070 	return 0;
2071 }
2072 
ccs_get_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)2073 static int ccs_get_format(struct v4l2_subdev *subdev,
2074 			  struct v4l2_subdev_state *sd_state,
2075 			  struct v4l2_subdev_format *fmt)
2076 {
2077 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2078 	int rval;
2079 
2080 	mutex_lock(&sensor->mutex);
2081 	rval = __ccs_get_format(subdev, sd_state, fmt);
2082 	mutex_unlock(&sensor->mutex);
2083 
2084 	return rval;
2085 }
2086 
ccs_get_crop_compose(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_rect ** crops,struct v4l2_rect ** comps,int which)2087 static void ccs_get_crop_compose(struct v4l2_subdev *subdev,
2088 				 struct v4l2_subdev_state *sd_state,
2089 				 struct v4l2_rect **crops,
2090 				 struct v4l2_rect **comps, int which)
2091 {
2092 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2093 	unsigned int i;
2094 
2095 	if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2096 		if (crops)
2097 			for (i = 0; i < subdev->entity.num_pads; i++)
2098 				crops[i] = &ssd->crop[i];
2099 		if (comps)
2100 			*comps = &ssd->compose;
2101 	} else {
2102 		if (crops) {
2103 			for (i = 0; i < subdev->entity.num_pads; i++)
2104 				crops[i] = v4l2_subdev_get_try_crop(subdev,
2105 								    sd_state,
2106 								    i);
2107 		}
2108 		if (comps)
2109 			*comps = v4l2_subdev_get_try_compose(subdev, sd_state,
2110 							     CCS_PAD_SINK);
2111 	}
2112 }
2113 
2114 /* Changes require propagation only on sink pad. */
ccs_propagate(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,int which,int target)2115 static void ccs_propagate(struct v4l2_subdev *subdev,
2116 			  struct v4l2_subdev_state *sd_state, int which,
2117 			  int target)
2118 {
2119 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2120 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2121 	struct v4l2_rect *comp, *crops[CCS_PADS];
2122 
2123 	ccs_get_crop_compose(subdev, sd_state, crops, &comp, which);
2124 
2125 	switch (target) {
2126 	case V4L2_SEL_TGT_CROP:
2127 		comp->width = crops[CCS_PAD_SINK]->width;
2128 		comp->height = crops[CCS_PAD_SINK]->height;
2129 		if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2130 			if (ssd == sensor->scaler) {
2131 				sensor->scale_m = CCS_LIM(sensor, SCALER_N_MIN);
2132 				sensor->scaling_mode =
2133 					CCS_SCALING_MODE_NO_SCALING;
2134 			} else if (ssd == sensor->binner) {
2135 				sensor->binning_horizontal = 1;
2136 				sensor->binning_vertical = 1;
2137 			}
2138 		}
2139 		fallthrough;
2140 	case V4L2_SEL_TGT_COMPOSE:
2141 		*crops[CCS_PAD_SRC] = *comp;
2142 		break;
2143 	default:
2144 		WARN_ON_ONCE(1);
2145 	}
2146 }
2147 
2148 static const struct ccs_csi_data_format
ccs_validate_csi_data_format(struct ccs_sensor * sensor,u32 code)2149 *ccs_validate_csi_data_format(struct ccs_sensor *sensor, u32 code)
2150 {
2151 	unsigned int i;
2152 
2153 	for (i = 0; i < ARRAY_SIZE(ccs_csi_data_formats); i++) {
2154 		if (sensor->mbus_frame_fmts & (1 << i) &&
2155 		    ccs_csi_data_formats[i].code == code)
2156 			return &ccs_csi_data_formats[i];
2157 	}
2158 
2159 	return sensor->csi_format;
2160 }
2161 
ccs_set_format_source(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)2162 static int ccs_set_format_source(struct v4l2_subdev *subdev,
2163 				 struct v4l2_subdev_state *sd_state,
2164 				 struct v4l2_subdev_format *fmt)
2165 {
2166 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2167 	const struct ccs_csi_data_format *csi_format,
2168 		*old_csi_format = sensor->csi_format;
2169 	unsigned long *valid_link_freqs;
2170 	u32 code = fmt->format.code;
2171 	unsigned int i;
2172 	int rval;
2173 
2174 	rval = __ccs_get_format(subdev, sd_state, fmt);
2175 	if (rval)
2176 		return rval;
2177 
2178 	/*
2179 	 * Media bus code is changeable on src subdev's source pad. On
2180 	 * other source pads we just get format here.
2181 	 */
2182 	if (subdev != &sensor->src->sd)
2183 		return 0;
2184 
2185 	csi_format = ccs_validate_csi_data_format(sensor, code);
2186 
2187 	fmt->format.code = csi_format->code;
2188 
2189 	if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE)
2190 		return 0;
2191 
2192 	sensor->csi_format = csi_format;
2193 
2194 	if (csi_format->width != old_csi_format->width)
2195 		for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
2196 			__v4l2_ctrl_modify_range(
2197 				sensor->test_data[i], 0,
2198 				(1 << csi_format->width) - 1, 1, 0);
2199 
2200 	if (csi_format->compressed == old_csi_format->compressed)
2201 		return 0;
2202 
2203 	valid_link_freqs =
2204 		&sensor->valid_link_freqs[sensor->csi_format->compressed
2205 					  - sensor->compressed_min_bpp];
2206 
2207 	__v4l2_ctrl_modify_range(
2208 		sensor->link_freq, 0,
2209 		__fls(*valid_link_freqs), ~*valid_link_freqs,
2210 		__ffs(*valid_link_freqs));
2211 
2212 	return ccs_pll_update(sensor);
2213 }
2214 
ccs_set_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)2215 static int ccs_set_format(struct v4l2_subdev *subdev,
2216 			  struct v4l2_subdev_state *sd_state,
2217 			  struct v4l2_subdev_format *fmt)
2218 {
2219 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2220 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2221 	struct v4l2_rect *crops[CCS_PADS];
2222 
2223 	mutex_lock(&sensor->mutex);
2224 
2225 	if (fmt->pad == ssd->source_pad) {
2226 		int rval;
2227 
2228 		rval = ccs_set_format_source(subdev, sd_state, fmt);
2229 
2230 		mutex_unlock(&sensor->mutex);
2231 
2232 		return rval;
2233 	}
2234 
2235 	/* Sink pad. Width and height are changeable here. */
2236 	fmt->format.code = __ccs_get_mbus_code(subdev, fmt->pad);
2237 	fmt->format.width &= ~1;
2238 	fmt->format.height &= ~1;
2239 	fmt->format.field = V4L2_FIELD_NONE;
2240 
2241 	fmt->format.width =
2242 		clamp(fmt->format.width,
2243 		      CCS_LIM(sensor, MIN_X_OUTPUT_SIZE),
2244 		      CCS_LIM(sensor, MAX_X_OUTPUT_SIZE));
2245 	fmt->format.height =
2246 		clamp(fmt->format.height,
2247 		      CCS_LIM(sensor, MIN_Y_OUTPUT_SIZE),
2248 		      CCS_LIM(sensor, MAX_Y_OUTPUT_SIZE));
2249 
2250 	ccs_get_crop_compose(subdev, sd_state, crops, NULL, fmt->which);
2251 
2252 	crops[ssd->sink_pad]->left = 0;
2253 	crops[ssd->sink_pad]->top = 0;
2254 	crops[ssd->sink_pad]->width = fmt->format.width;
2255 	crops[ssd->sink_pad]->height = fmt->format.height;
2256 	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2257 		ssd->sink_fmt = *crops[ssd->sink_pad];
2258 	ccs_propagate(subdev, sd_state, fmt->which, V4L2_SEL_TGT_CROP);
2259 
2260 	mutex_unlock(&sensor->mutex);
2261 
2262 	return 0;
2263 }
2264 
2265 /*
2266  * Calculate goodness of scaled image size compared to expected image
2267  * size and flags provided.
2268  */
2269 #define SCALING_GOODNESS		100000
2270 #define SCALING_GOODNESS_EXTREME	100000000
scaling_goodness(struct v4l2_subdev * subdev,int w,int ask_w,int h,int ask_h,u32 flags)2271 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
2272 			    int h, int ask_h, u32 flags)
2273 {
2274 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2275 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
2276 	int val = 0;
2277 
2278 	w &= ~1;
2279 	ask_w &= ~1;
2280 	h &= ~1;
2281 	ask_h &= ~1;
2282 
2283 	if (flags & V4L2_SEL_FLAG_GE) {
2284 		if (w < ask_w)
2285 			val -= SCALING_GOODNESS;
2286 		if (h < ask_h)
2287 			val -= SCALING_GOODNESS;
2288 	}
2289 
2290 	if (flags & V4L2_SEL_FLAG_LE) {
2291 		if (w > ask_w)
2292 			val -= SCALING_GOODNESS;
2293 		if (h > ask_h)
2294 			val -= SCALING_GOODNESS;
2295 	}
2296 
2297 	val -= abs(w - ask_w);
2298 	val -= abs(h - ask_h);
2299 
2300 	if (w < CCS_LIM(sensor, MIN_X_OUTPUT_SIZE))
2301 		val -= SCALING_GOODNESS_EXTREME;
2302 
2303 	dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
2304 		w, ask_w, h, ask_h, val);
2305 
2306 	return val;
2307 }
2308 
ccs_set_compose_binner(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel,struct v4l2_rect ** crops,struct v4l2_rect * comp)2309 static void ccs_set_compose_binner(struct v4l2_subdev *subdev,
2310 				   struct v4l2_subdev_state *sd_state,
2311 				   struct v4l2_subdev_selection *sel,
2312 				   struct v4l2_rect **crops,
2313 				   struct v4l2_rect *comp)
2314 {
2315 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2316 	unsigned int i;
2317 	unsigned int binh = 1, binv = 1;
2318 	int best = scaling_goodness(
2319 		subdev,
2320 		crops[CCS_PAD_SINK]->width, sel->r.width,
2321 		crops[CCS_PAD_SINK]->height, sel->r.height, sel->flags);
2322 
2323 	for (i = 0; i < sensor->nbinning_subtypes; i++) {
2324 		int this = scaling_goodness(
2325 			subdev,
2326 			crops[CCS_PAD_SINK]->width
2327 			/ sensor->binning_subtypes[i].horizontal,
2328 			sel->r.width,
2329 			crops[CCS_PAD_SINK]->height
2330 			/ sensor->binning_subtypes[i].vertical,
2331 			sel->r.height, sel->flags);
2332 
2333 		if (this > best) {
2334 			binh = sensor->binning_subtypes[i].horizontal;
2335 			binv = sensor->binning_subtypes[i].vertical;
2336 			best = this;
2337 		}
2338 	}
2339 	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2340 		sensor->binning_vertical = binv;
2341 		sensor->binning_horizontal = binh;
2342 	}
2343 
2344 	sel->r.width = (crops[CCS_PAD_SINK]->width / binh) & ~1;
2345 	sel->r.height = (crops[CCS_PAD_SINK]->height / binv) & ~1;
2346 }
2347 
2348 /*
2349  * Calculate best scaling ratio and mode for given output resolution.
2350  *
2351  * Try all of these: horizontal ratio, vertical ratio and smallest
2352  * size possible (horizontally).
2353  *
2354  * Also try whether horizontal scaler or full scaler gives a better
2355  * result.
2356  */
ccs_set_compose_scaler(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel,struct v4l2_rect ** crops,struct v4l2_rect * comp)2357 static void ccs_set_compose_scaler(struct v4l2_subdev *subdev,
2358 				   struct v4l2_subdev_state *sd_state,
2359 				   struct v4l2_subdev_selection *sel,
2360 				   struct v4l2_rect **crops,
2361 				   struct v4l2_rect *comp)
2362 {
2363 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
2364 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2365 	u32 min, max, a, b, max_m;
2366 	u32 scale_m = CCS_LIM(sensor, SCALER_N_MIN);
2367 	int mode = CCS_SCALING_MODE_HORIZONTAL;
2368 	u32 try[4];
2369 	u32 ntry = 0;
2370 	unsigned int i;
2371 	int best = INT_MIN;
2372 
2373 	sel->r.width = min_t(unsigned int, sel->r.width,
2374 			     crops[CCS_PAD_SINK]->width);
2375 	sel->r.height = min_t(unsigned int, sel->r.height,
2376 			      crops[CCS_PAD_SINK]->height);
2377 
2378 	a = crops[CCS_PAD_SINK]->width
2379 		* CCS_LIM(sensor, SCALER_N_MIN) / sel->r.width;
2380 	b = crops[CCS_PAD_SINK]->height
2381 		* CCS_LIM(sensor, SCALER_N_MIN) / sel->r.height;
2382 	max_m = crops[CCS_PAD_SINK]->width
2383 		* CCS_LIM(sensor, SCALER_N_MIN)
2384 		/ CCS_LIM(sensor, MIN_X_OUTPUT_SIZE);
2385 
2386 	a = clamp(a, CCS_LIM(sensor, SCALER_M_MIN),
2387 		  CCS_LIM(sensor, SCALER_M_MAX));
2388 	b = clamp(b, CCS_LIM(sensor, SCALER_M_MIN),
2389 		  CCS_LIM(sensor, SCALER_M_MAX));
2390 	max_m = clamp(max_m, CCS_LIM(sensor, SCALER_M_MIN),
2391 		      CCS_LIM(sensor, SCALER_M_MAX));
2392 
2393 	dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
2394 
2395 	min = min(max_m, min(a, b));
2396 	max = min(max_m, max(a, b));
2397 
2398 	try[ntry] = min;
2399 	ntry++;
2400 	if (min != max) {
2401 		try[ntry] = max;
2402 		ntry++;
2403 	}
2404 	if (max != max_m) {
2405 		try[ntry] = min + 1;
2406 		ntry++;
2407 		if (min != max) {
2408 			try[ntry] = max + 1;
2409 			ntry++;
2410 		}
2411 	}
2412 
2413 	for (i = 0; i < ntry; i++) {
2414 		int this = scaling_goodness(
2415 			subdev,
2416 			crops[CCS_PAD_SINK]->width
2417 			/ try[i] * CCS_LIM(sensor, SCALER_N_MIN),
2418 			sel->r.width,
2419 			crops[CCS_PAD_SINK]->height,
2420 			sel->r.height,
2421 			sel->flags);
2422 
2423 		dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
2424 
2425 		if (this > best) {
2426 			scale_m = try[i];
2427 			mode = CCS_SCALING_MODE_HORIZONTAL;
2428 			best = this;
2429 		}
2430 
2431 		if (CCS_LIM(sensor, SCALING_CAPABILITY)
2432 		    == CCS_SCALING_CAPABILITY_HORIZONTAL)
2433 			continue;
2434 
2435 		this = scaling_goodness(
2436 			subdev, crops[CCS_PAD_SINK]->width
2437 			/ try[i]
2438 			* CCS_LIM(sensor, SCALER_N_MIN),
2439 			sel->r.width,
2440 			crops[CCS_PAD_SINK]->height
2441 			/ try[i]
2442 			* CCS_LIM(sensor, SCALER_N_MIN),
2443 			sel->r.height,
2444 			sel->flags);
2445 
2446 		if (this > best) {
2447 			scale_m = try[i];
2448 			mode = SMIAPP_SCALING_MODE_BOTH;
2449 			best = this;
2450 		}
2451 	}
2452 
2453 	sel->r.width =
2454 		(crops[CCS_PAD_SINK]->width
2455 		 / scale_m
2456 		 * CCS_LIM(sensor, SCALER_N_MIN)) & ~1;
2457 	if (mode == SMIAPP_SCALING_MODE_BOTH)
2458 		sel->r.height =
2459 			(crops[CCS_PAD_SINK]->height
2460 			 / scale_m
2461 			 * CCS_LIM(sensor, SCALER_N_MIN))
2462 			& ~1;
2463 	else
2464 		sel->r.height = crops[CCS_PAD_SINK]->height;
2465 
2466 	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2467 		sensor->scale_m = scale_m;
2468 		sensor->scaling_mode = mode;
2469 	}
2470 }
2471 /* We're only called on source pads. This function sets scaling. */
ccs_set_compose(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)2472 static int ccs_set_compose(struct v4l2_subdev *subdev,
2473 			   struct v4l2_subdev_state *sd_state,
2474 			   struct v4l2_subdev_selection *sel)
2475 {
2476 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2477 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2478 	struct v4l2_rect *comp, *crops[CCS_PADS];
2479 
2480 	ccs_get_crop_compose(subdev, sd_state, crops, &comp, sel->which);
2481 
2482 	sel->r.top = 0;
2483 	sel->r.left = 0;
2484 
2485 	if (ssd == sensor->binner)
2486 		ccs_set_compose_binner(subdev, sd_state, sel, crops, comp);
2487 	else
2488 		ccs_set_compose_scaler(subdev, sd_state, sel, crops, comp);
2489 
2490 	*comp = sel->r;
2491 	ccs_propagate(subdev, sd_state, sel->which, V4L2_SEL_TGT_COMPOSE);
2492 
2493 	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2494 		return ccs_pll_blanking_update(sensor);
2495 
2496 	return 0;
2497 }
2498 
__ccs_sel_supported(struct v4l2_subdev * subdev,struct v4l2_subdev_selection * sel)2499 static int __ccs_sel_supported(struct v4l2_subdev *subdev,
2500 			       struct v4l2_subdev_selection *sel)
2501 {
2502 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2503 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2504 
2505 	/* We only implement crop in three places. */
2506 	switch (sel->target) {
2507 	case V4L2_SEL_TGT_CROP:
2508 	case V4L2_SEL_TGT_CROP_BOUNDS:
2509 		if (ssd == sensor->pixel_array && sel->pad == CCS_PA_PAD_SRC)
2510 			return 0;
2511 		if (ssd == sensor->src && sel->pad == CCS_PAD_SRC)
2512 			return 0;
2513 		if (ssd == sensor->scaler && sel->pad == CCS_PAD_SINK &&
2514 		    CCS_LIM(sensor, DIGITAL_CROP_CAPABILITY)
2515 		    == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
2516 			return 0;
2517 		return -EINVAL;
2518 	case V4L2_SEL_TGT_NATIVE_SIZE:
2519 		if (ssd == sensor->pixel_array && sel->pad == CCS_PA_PAD_SRC)
2520 			return 0;
2521 		return -EINVAL;
2522 	case V4L2_SEL_TGT_COMPOSE:
2523 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2524 		if (sel->pad == ssd->source_pad)
2525 			return -EINVAL;
2526 		if (ssd == sensor->binner)
2527 			return 0;
2528 		if (ssd == sensor->scaler && CCS_LIM(sensor, SCALING_CAPABILITY)
2529 		    != CCS_SCALING_CAPABILITY_NONE)
2530 			return 0;
2531 		fallthrough;
2532 	default:
2533 		return -EINVAL;
2534 	}
2535 }
2536 
ccs_set_crop(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)2537 static int ccs_set_crop(struct v4l2_subdev *subdev,
2538 			struct v4l2_subdev_state *sd_state,
2539 			struct v4l2_subdev_selection *sel)
2540 {
2541 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2542 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2543 	struct v4l2_rect *src_size, *crops[CCS_PADS];
2544 	struct v4l2_rect _r;
2545 
2546 	ccs_get_crop_compose(subdev, sd_state, crops, NULL, sel->which);
2547 
2548 	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2549 		if (sel->pad == ssd->sink_pad)
2550 			src_size = &ssd->sink_fmt;
2551 		else
2552 			src_size = &ssd->compose;
2553 	} else {
2554 		if (sel->pad == ssd->sink_pad) {
2555 			_r.left = 0;
2556 			_r.top = 0;
2557 			_r.width = v4l2_subdev_get_try_format(subdev,
2558 							      sd_state,
2559 							      sel->pad)
2560 				->width;
2561 			_r.height = v4l2_subdev_get_try_format(subdev,
2562 							       sd_state,
2563 							       sel->pad)
2564 				->height;
2565 			src_size = &_r;
2566 		} else {
2567 			src_size = v4l2_subdev_get_try_compose(
2568 				subdev, sd_state, ssd->sink_pad);
2569 		}
2570 	}
2571 
2572 	if (ssd == sensor->src && sel->pad == CCS_PAD_SRC) {
2573 		sel->r.left = 0;
2574 		sel->r.top = 0;
2575 	}
2576 
2577 	sel->r.width = min(sel->r.width, src_size->width);
2578 	sel->r.height = min(sel->r.height, src_size->height);
2579 
2580 	sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
2581 	sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
2582 
2583 	*crops[sel->pad] = sel->r;
2584 
2585 	if (ssd != sensor->pixel_array && sel->pad == CCS_PAD_SINK)
2586 		ccs_propagate(subdev, sd_state, sel->which, V4L2_SEL_TGT_CROP);
2587 
2588 	return 0;
2589 }
2590 
ccs_get_native_size(struct ccs_subdev * ssd,struct v4l2_rect * r)2591 static void ccs_get_native_size(struct ccs_subdev *ssd, struct v4l2_rect *r)
2592 {
2593 	r->top = 0;
2594 	r->left = 0;
2595 	r->width = CCS_LIM(ssd->sensor, X_ADDR_MAX) + 1;
2596 	r->height = CCS_LIM(ssd->sensor, Y_ADDR_MAX) + 1;
2597 }
2598 
__ccs_get_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)2599 static int __ccs_get_selection(struct v4l2_subdev *subdev,
2600 			       struct v4l2_subdev_state *sd_state,
2601 			       struct v4l2_subdev_selection *sel)
2602 {
2603 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2604 	struct ccs_subdev *ssd = to_ccs_subdev(subdev);
2605 	struct v4l2_rect *comp, *crops[CCS_PADS];
2606 	struct v4l2_rect sink_fmt;
2607 	int ret;
2608 
2609 	ret = __ccs_sel_supported(subdev, sel);
2610 	if (ret)
2611 		return ret;
2612 
2613 	ccs_get_crop_compose(subdev, sd_state, crops, &comp, sel->which);
2614 
2615 	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2616 		sink_fmt = ssd->sink_fmt;
2617 	} else {
2618 		struct v4l2_mbus_framefmt *fmt =
2619 			v4l2_subdev_get_try_format(subdev, sd_state,
2620 						   ssd->sink_pad);
2621 
2622 		sink_fmt.left = 0;
2623 		sink_fmt.top = 0;
2624 		sink_fmt.width = fmt->width;
2625 		sink_fmt.height = fmt->height;
2626 	}
2627 
2628 	switch (sel->target) {
2629 	case V4L2_SEL_TGT_CROP_BOUNDS:
2630 	case V4L2_SEL_TGT_NATIVE_SIZE:
2631 		if (ssd == sensor->pixel_array)
2632 			ccs_get_native_size(ssd, &sel->r);
2633 		else if (sel->pad == ssd->sink_pad)
2634 			sel->r = sink_fmt;
2635 		else
2636 			sel->r = *comp;
2637 		break;
2638 	case V4L2_SEL_TGT_CROP:
2639 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2640 		sel->r = *crops[sel->pad];
2641 		break;
2642 	case V4L2_SEL_TGT_COMPOSE:
2643 		sel->r = *comp;
2644 		break;
2645 	}
2646 
2647 	return 0;
2648 }
2649 
ccs_get_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)2650 static int ccs_get_selection(struct v4l2_subdev *subdev,
2651 			     struct v4l2_subdev_state *sd_state,
2652 			     struct v4l2_subdev_selection *sel)
2653 {
2654 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2655 	int rval;
2656 
2657 	mutex_lock(&sensor->mutex);
2658 	rval = __ccs_get_selection(subdev, sd_state, sel);
2659 	mutex_unlock(&sensor->mutex);
2660 
2661 	return rval;
2662 }
2663 
ccs_set_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)2664 static int ccs_set_selection(struct v4l2_subdev *subdev,
2665 			     struct v4l2_subdev_state *sd_state,
2666 			     struct v4l2_subdev_selection *sel)
2667 {
2668 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2669 	int ret;
2670 
2671 	ret = __ccs_sel_supported(subdev, sel);
2672 	if (ret)
2673 		return ret;
2674 
2675 	mutex_lock(&sensor->mutex);
2676 
2677 	sel->r.left = max(0, sel->r.left & ~1);
2678 	sel->r.top = max(0, sel->r.top & ~1);
2679 	sel->r.width = CCS_ALIGN_DIM(sel->r.width, sel->flags);
2680 	sel->r.height =	CCS_ALIGN_DIM(sel->r.height, sel->flags);
2681 
2682 	sel->r.width = max_t(unsigned int, CCS_LIM(sensor, MIN_X_OUTPUT_SIZE),
2683 			     sel->r.width);
2684 	sel->r.height = max_t(unsigned int, CCS_LIM(sensor, MIN_Y_OUTPUT_SIZE),
2685 			      sel->r.height);
2686 
2687 	switch (sel->target) {
2688 	case V4L2_SEL_TGT_CROP:
2689 		ret = ccs_set_crop(subdev, sd_state, sel);
2690 		break;
2691 	case V4L2_SEL_TGT_COMPOSE:
2692 		ret = ccs_set_compose(subdev, sd_state, sel);
2693 		break;
2694 	default:
2695 		ret = -EINVAL;
2696 	}
2697 
2698 	mutex_unlock(&sensor->mutex);
2699 	return ret;
2700 }
2701 
ccs_get_skip_frames(struct v4l2_subdev * subdev,u32 * frames)2702 static int ccs_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2703 {
2704 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2705 
2706 	*frames = sensor->frame_skip;
2707 	return 0;
2708 }
2709 
ccs_get_skip_top_lines(struct v4l2_subdev * subdev,u32 * lines)2710 static int ccs_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines)
2711 {
2712 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2713 
2714 	*lines = sensor->image_start;
2715 
2716 	return 0;
2717 }
2718 
2719 /* -----------------------------------------------------------------------------
2720  * sysfs attributes
2721  */
2722 
2723 static ssize_t
nvm_show(struct device * dev,struct device_attribute * attr,char * buf)2724 nvm_show(struct device *dev, struct device_attribute *attr, char *buf)
2725 {
2726 	struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2727 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
2728 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2729 	int rval;
2730 
2731 	if (!sensor->dev_init_done)
2732 		return -EBUSY;
2733 
2734 	rval = ccs_pm_get_init(sensor);
2735 	if (rval < 0)
2736 		return -ENODEV;
2737 
2738 	rval = ccs_read_nvm(sensor, buf, PAGE_SIZE);
2739 	if (rval < 0) {
2740 		pm_runtime_put(&client->dev);
2741 		dev_err(&client->dev, "nvm read failed\n");
2742 		return -ENODEV;
2743 	}
2744 
2745 	pm_runtime_mark_last_busy(&client->dev);
2746 	pm_runtime_put_autosuspend(&client->dev);
2747 
2748 	/*
2749 	 * NVM is still way below a PAGE_SIZE, so we can safely
2750 	 * assume this for now.
2751 	 */
2752 	return rval;
2753 }
2754 static DEVICE_ATTR_RO(nvm);
2755 
2756 static ssize_t
ident_show(struct device * dev,struct device_attribute * attr,char * buf)2757 ident_show(struct device *dev, struct device_attribute *attr, char *buf)
2758 {
2759 	struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2760 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2761 	struct ccs_module_info *minfo = &sensor->minfo;
2762 
2763 	if (minfo->mipi_manufacturer_id)
2764 		return snprintf(buf, PAGE_SIZE, "%4.4x%4.4x%2.2x\n",
2765 				minfo->mipi_manufacturer_id, minfo->model_id,
2766 				minfo->revision_number) + 1;
2767 	else
2768 		return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2769 				minfo->smia_manufacturer_id, minfo->model_id,
2770 				minfo->revision_number) + 1;
2771 }
2772 static DEVICE_ATTR_RO(ident);
2773 
2774 /* -----------------------------------------------------------------------------
2775  * V4L2 subdev core operations
2776  */
2777 
ccs_identify_module(struct ccs_sensor * sensor)2778 static int ccs_identify_module(struct ccs_sensor *sensor)
2779 {
2780 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2781 	struct ccs_module_info *minfo = &sensor->minfo;
2782 	unsigned int i;
2783 	u32 rev;
2784 	int rval = 0;
2785 
2786 	/* Module info */
2787 	rval = ccs_read(sensor, MODULE_MANUFACTURER_ID,
2788 			&minfo->mipi_manufacturer_id);
2789 	if (!rval && !minfo->mipi_manufacturer_id)
2790 		rval = ccs_read_addr_8only(sensor,
2791 					   SMIAPP_REG_U8_MANUFACTURER_ID,
2792 					   &minfo->smia_manufacturer_id);
2793 	if (!rval)
2794 		rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_MODEL_ID,
2795 					   &minfo->model_id);
2796 	if (!rval)
2797 		rval = ccs_read_addr_8only(sensor,
2798 					   CCS_R_MODULE_REVISION_NUMBER_MAJOR,
2799 					   &rev);
2800 	if (!rval) {
2801 		rval = ccs_read_addr_8only(sensor,
2802 					   CCS_R_MODULE_REVISION_NUMBER_MINOR,
2803 					   &minfo->revision_number);
2804 		minfo->revision_number |= rev << 8;
2805 	}
2806 	if (!rval)
2807 		rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_DATE_YEAR,
2808 					   &minfo->module_year);
2809 	if (!rval)
2810 		rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_DATE_MONTH,
2811 					   &minfo->module_month);
2812 	if (!rval)
2813 		rval = ccs_read_addr_8only(sensor, CCS_R_MODULE_DATE_DAY,
2814 					   &minfo->module_day);
2815 
2816 	/* Sensor info */
2817 	if (!rval)
2818 		rval = ccs_read(sensor, SENSOR_MANUFACTURER_ID,
2819 				&minfo->sensor_mipi_manufacturer_id);
2820 	if (!rval && !minfo->sensor_mipi_manufacturer_id)
2821 		rval = ccs_read_addr_8only(sensor,
2822 					   CCS_R_SENSOR_MANUFACTURER_ID,
2823 					   &minfo->sensor_smia_manufacturer_id);
2824 	if (!rval)
2825 		rval = ccs_read_addr_8only(sensor,
2826 					   CCS_R_SENSOR_MODEL_ID,
2827 					   &minfo->sensor_model_id);
2828 	if (!rval)
2829 		rval = ccs_read_addr_8only(sensor,
2830 					   CCS_R_SENSOR_REVISION_NUMBER,
2831 					   &minfo->sensor_revision_number);
2832 	if (!rval)
2833 		rval = ccs_read_addr_8only(sensor,
2834 					   CCS_R_SENSOR_FIRMWARE_VERSION,
2835 					   &minfo->sensor_firmware_version);
2836 
2837 	/* SMIA */
2838 	if (!rval)
2839 		rval = ccs_read(sensor, MIPI_CCS_VERSION, &minfo->ccs_version);
2840 	if (!rval && !minfo->ccs_version)
2841 		rval = ccs_read_addr_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2842 					   &minfo->smia_version);
2843 	if (!rval && !minfo->ccs_version)
2844 		rval = ccs_read_addr_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2845 					   &minfo->smiapp_version);
2846 
2847 	if (rval) {
2848 		dev_err(&client->dev, "sensor detection failed\n");
2849 		return -ENODEV;
2850 	}
2851 
2852 	if (minfo->mipi_manufacturer_id)
2853 		dev_dbg(&client->dev, "MIPI CCS module 0x%4.4x-0x%4.4x\n",
2854 			minfo->mipi_manufacturer_id, minfo->model_id);
2855 	else
2856 		dev_dbg(&client->dev, "SMIA module 0x%2.2x-0x%4.4x\n",
2857 			minfo->smia_manufacturer_id, minfo->model_id);
2858 
2859 	dev_dbg(&client->dev,
2860 		"module revision 0x%4.4x date %2.2d-%2.2d-%2.2d\n",
2861 		minfo->revision_number, minfo->module_year, minfo->module_month,
2862 		minfo->module_day);
2863 
2864 	if (minfo->sensor_mipi_manufacturer_id)
2865 		dev_dbg(&client->dev, "MIPI CCS sensor 0x%4.4x-0x%4.4x\n",
2866 			minfo->sensor_mipi_manufacturer_id,
2867 			minfo->sensor_model_id);
2868 	else
2869 		dev_dbg(&client->dev, "SMIA sensor 0x%2.2x-0x%4.4x\n",
2870 			minfo->sensor_smia_manufacturer_id,
2871 			minfo->sensor_model_id);
2872 
2873 	dev_dbg(&client->dev,
2874 		"sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2875 		minfo->sensor_revision_number, minfo->sensor_firmware_version);
2876 
2877 	if (minfo->ccs_version) {
2878 		dev_dbg(&client->dev, "MIPI CCS version %u.%u",
2879 			(minfo->ccs_version & CCS_MIPI_CCS_VERSION_MAJOR_MASK)
2880 			>> CCS_MIPI_CCS_VERSION_MAJOR_SHIFT,
2881 			(minfo->ccs_version & CCS_MIPI_CCS_VERSION_MINOR_MASK));
2882 		minfo->name = CCS_NAME;
2883 	} else {
2884 		dev_dbg(&client->dev,
2885 			"smia version %2.2d smiapp version %2.2d\n",
2886 			minfo->smia_version, minfo->smiapp_version);
2887 		minfo->name = SMIAPP_NAME;
2888 	}
2889 
2890 	/*
2891 	 * Some modules have bad data in the lvalues below. Hope the
2892 	 * rvalues have better stuff. The lvalues are module
2893 	 * parameters whereas the rvalues are sensor parameters.
2894 	 */
2895 	if (minfo->sensor_smia_manufacturer_id &&
2896 	    !minfo->smia_manufacturer_id && !minfo->model_id) {
2897 		minfo->smia_manufacturer_id =
2898 			minfo->sensor_smia_manufacturer_id;
2899 		minfo->model_id = minfo->sensor_model_id;
2900 		minfo->revision_number = minfo->sensor_revision_number;
2901 	}
2902 
2903 	for (i = 0; i < ARRAY_SIZE(ccs_module_idents); i++) {
2904 		if (ccs_module_idents[i].mipi_manufacturer_id &&
2905 		    ccs_module_idents[i].mipi_manufacturer_id
2906 		    != minfo->mipi_manufacturer_id)
2907 			continue;
2908 		if (ccs_module_idents[i].smia_manufacturer_id &&
2909 		    ccs_module_idents[i].smia_manufacturer_id
2910 		    != minfo->smia_manufacturer_id)
2911 			continue;
2912 		if (ccs_module_idents[i].model_id != minfo->model_id)
2913 			continue;
2914 		if (ccs_module_idents[i].flags
2915 		    & CCS_MODULE_IDENT_FLAG_REV_LE) {
2916 			if (ccs_module_idents[i].revision_number_major
2917 			    < (minfo->revision_number >> 8))
2918 				continue;
2919 		} else {
2920 			if (ccs_module_idents[i].revision_number_major
2921 			    != (minfo->revision_number >> 8))
2922 				continue;
2923 		}
2924 
2925 		minfo->name = ccs_module_idents[i].name;
2926 		minfo->quirk = ccs_module_idents[i].quirk;
2927 		break;
2928 	}
2929 
2930 	if (i >= ARRAY_SIZE(ccs_module_idents))
2931 		dev_warn(&client->dev,
2932 			 "no quirks for this module; let's hope it's fully compliant\n");
2933 
2934 	dev_dbg(&client->dev, "the sensor is called %s\n", minfo->name);
2935 
2936 	return 0;
2937 }
2938 
2939 static const struct v4l2_subdev_ops ccs_ops;
2940 static const struct v4l2_subdev_internal_ops ccs_internal_ops;
2941 static const struct media_entity_operations ccs_entity_ops;
2942 
ccs_register_subdev(struct ccs_sensor * sensor,struct ccs_subdev * ssd,struct ccs_subdev * sink_ssd,u16 source_pad,u16 sink_pad,u32 link_flags)2943 static int ccs_register_subdev(struct ccs_sensor *sensor,
2944 			       struct ccs_subdev *ssd,
2945 			       struct ccs_subdev *sink_ssd,
2946 			       u16 source_pad, u16 sink_pad, u32 link_flags)
2947 {
2948 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2949 	int rval;
2950 
2951 	if (!sink_ssd)
2952 		return 0;
2953 
2954 	rval = media_entity_pads_init(&ssd->sd.entity, ssd->npads, ssd->pads);
2955 	if (rval) {
2956 		dev_err(&client->dev, "media_entity_pads_init failed\n");
2957 		return rval;
2958 	}
2959 
2960 	rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev, &ssd->sd);
2961 	if (rval) {
2962 		dev_err(&client->dev, "v4l2_device_register_subdev failed\n");
2963 		return rval;
2964 	}
2965 
2966 	rval = media_create_pad_link(&ssd->sd.entity, source_pad,
2967 				     &sink_ssd->sd.entity, sink_pad,
2968 				     link_flags);
2969 	if (rval) {
2970 		dev_err(&client->dev, "media_create_pad_link failed\n");
2971 		v4l2_device_unregister_subdev(&ssd->sd);
2972 		return rval;
2973 	}
2974 
2975 	return 0;
2976 }
2977 
ccs_unregistered(struct v4l2_subdev * subdev)2978 static void ccs_unregistered(struct v4l2_subdev *subdev)
2979 {
2980 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2981 	unsigned int i;
2982 
2983 	for (i = 1; i < sensor->ssds_used; i++)
2984 		v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2985 }
2986 
ccs_registered(struct v4l2_subdev * subdev)2987 static int ccs_registered(struct v4l2_subdev *subdev)
2988 {
2989 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
2990 	int rval;
2991 
2992 	if (sensor->scaler) {
2993 		rval = ccs_register_subdev(sensor, sensor->binner,
2994 					   sensor->scaler,
2995 					   CCS_PAD_SRC, CCS_PAD_SINK,
2996 					   MEDIA_LNK_FL_ENABLED |
2997 					   MEDIA_LNK_FL_IMMUTABLE);
2998 		if (rval < 0)
2999 			return rval;
3000 	}
3001 
3002 	rval = ccs_register_subdev(sensor, sensor->pixel_array, sensor->binner,
3003 				   CCS_PA_PAD_SRC, CCS_PAD_SINK,
3004 				   MEDIA_LNK_FL_ENABLED |
3005 				   MEDIA_LNK_FL_IMMUTABLE);
3006 	if (rval)
3007 		goto out_err;
3008 
3009 	return 0;
3010 
3011 out_err:
3012 	ccs_unregistered(subdev);
3013 
3014 	return rval;
3015 }
3016 
ccs_cleanup(struct ccs_sensor * sensor)3017 static void ccs_cleanup(struct ccs_sensor *sensor)
3018 {
3019 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
3020 
3021 	device_remove_file(&client->dev, &dev_attr_nvm);
3022 	device_remove_file(&client->dev, &dev_attr_ident);
3023 
3024 	ccs_free_controls(sensor);
3025 }
3026 
ccs_create_subdev(struct ccs_sensor * sensor,struct ccs_subdev * ssd,const char * name,unsigned short num_pads,u32 function)3027 static void ccs_create_subdev(struct ccs_sensor *sensor,
3028 			      struct ccs_subdev *ssd, const char *name,
3029 			      unsigned short num_pads, u32 function)
3030 {
3031 	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
3032 
3033 	if (!ssd)
3034 		return;
3035 
3036 	if (ssd != sensor->src)
3037 		v4l2_subdev_init(&ssd->sd, &ccs_ops);
3038 
3039 	ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
3040 	ssd->sd.entity.function = function;
3041 	ssd->sensor = sensor;
3042 
3043 	ssd->npads = num_pads;
3044 	ssd->source_pad = num_pads - 1;
3045 
3046 	v4l2_i2c_subdev_set_name(&ssd->sd, client, sensor->minfo.name, name);
3047 
3048 	ccs_get_native_size(ssd, &ssd->sink_fmt);
3049 
3050 	ssd->compose.width = ssd->sink_fmt.width;
3051 	ssd->compose.height = ssd->sink_fmt.height;
3052 	ssd->crop[ssd->source_pad] = ssd->compose;
3053 	ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE;
3054 	if (ssd != sensor->pixel_array) {
3055 		ssd->crop[ssd->sink_pad] = ssd->compose;
3056 		ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK;
3057 	}
3058 
3059 	ssd->sd.entity.ops = &ccs_entity_ops;
3060 
3061 	if (ssd == sensor->src)
3062 		return;
3063 
3064 	ssd->sd.internal_ops = &ccs_internal_ops;
3065 	ssd->sd.owner = THIS_MODULE;
3066 	ssd->sd.dev = &client->dev;
3067 	v4l2_set_subdevdata(&ssd->sd, client);
3068 }
3069 
ccs_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)3070 static int ccs_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
3071 {
3072 	struct ccs_subdev *ssd = to_ccs_subdev(sd);
3073 	struct ccs_sensor *sensor = ssd->sensor;
3074 	unsigned int i;
3075 
3076 	mutex_lock(&sensor->mutex);
3077 
3078 	for (i = 0; i < ssd->npads; i++) {
3079 		struct v4l2_mbus_framefmt *try_fmt =
3080 			v4l2_subdev_get_try_format(sd, fh->state, i);
3081 		struct v4l2_rect *try_crop =
3082 			v4l2_subdev_get_try_crop(sd, fh->state, i);
3083 		struct v4l2_rect *try_comp;
3084 
3085 		ccs_get_native_size(ssd, try_crop);
3086 
3087 		try_fmt->width = try_crop->width;
3088 		try_fmt->height = try_crop->height;
3089 		try_fmt->code = sensor->internal_csi_format->code;
3090 		try_fmt->field = V4L2_FIELD_NONE;
3091 
3092 		if (ssd == sensor->pixel_array)
3093 			continue;
3094 
3095 		try_comp = v4l2_subdev_get_try_compose(sd, fh->state, i);
3096 		*try_comp = *try_crop;
3097 	}
3098 
3099 	mutex_unlock(&sensor->mutex);
3100 
3101 	return 0;
3102 }
3103 
3104 static const struct v4l2_subdev_video_ops ccs_video_ops = {
3105 	.s_stream = ccs_set_stream,
3106 	.pre_streamon = ccs_pre_streamon,
3107 	.post_streamoff = ccs_post_streamoff,
3108 };
3109 
3110 static const struct v4l2_subdev_pad_ops ccs_pad_ops = {
3111 	.enum_mbus_code = ccs_enum_mbus_code,
3112 	.get_fmt = ccs_get_format,
3113 	.set_fmt = ccs_set_format,
3114 	.get_selection = ccs_get_selection,
3115 	.set_selection = ccs_set_selection,
3116 };
3117 
3118 static const struct v4l2_subdev_sensor_ops ccs_sensor_ops = {
3119 	.g_skip_frames = ccs_get_skip_frames,
3120 	.g_skip_top_lines = ccs_get_skip_top_lines,
3121 };
3122 
3123 static const struct v4l2_subdev_ops ccs_ops = {
3124 	.video = &ccs_video_ops,
3125 	.pad = &ccs_pad_ops,
3126 	.sensor = &ccs_sensor_ops,
3127 };
3128 
3129 static const struct media_entity_operations ccs_entity_ops = {
3130 	.link_validate = v4l2_subdev_link_validate,
3131 };
3132 
3133 static const struct v4l2_subdev_internal_ops ccs_internal_src_ops = {
3134 	.registered = ccs_registered,
3135 	.unregistered = ccs_unregistered,
3136 	.open = ccs_open,
3137 };
3138 
3139 static const struct v4l2_subdev_internal_ops ccs_internal_ops = {
3140 	.open = ccs_open,
3141 };
3142 
3143 /* -----------------------------------------------------------------------------
3144  * I2C Driver
3145  */
3146 
ccs_suspend(struct device * dev)3147 static int __maybe_unused ccs_suspend(struct device *dev)
3148 {
3149 	struct i2c_client *client = to_i2c_client(dev);
3150 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3151 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
3152 	bool streaming = sensor->streaming;
3153 	int rval;
3154 
3155 	rval = pm_runtime_resume_and_get(dev);
3156 	if (rval < 0)
3157 		return rval;
3158 
3159 	if (sensor->streaming)
3160 		ccs_stop_streaming(sensor);
3161 
3162 	/* save state for resume */
3163 	sensor->streaming = streaming;
3164 
3165 	return 0;
3166 }
3167 
ccs_resume(struct device * dev)3168 static int __maybe_unused ccs_resume(struct device *dev)
3169 {
3170 	struct i2c_client *client = to_i2c_client(dev);
3171 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3172 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
3173 	int rval = 0;
3174 
3175 	pm_runtime_put(dev);
3176 
3177 	if (sensor->streaming)
3178 		rval = ccs_start_streaming(sensor);
3179 
3180 	return rval;
3181 }
3182 
ccs_get_hwconfig(struct ccs_sensor * sensor,struct device * dev)3183 static int ccs_get_hwconfig(struct ccs_sensor *sensor, struct device *dev)
3184 {
3185 	struct ccs_hwconfig *hwcfg = &sensor->hwcfg;
3186 	struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = V4L2_MBUS_UNKNOWN };
3187 	struct fwnode_handle *ep;
3188 	struct fwnode_handle *fwnode = dev_fwnode(dev);
3189 	u32 rotation;
3190 	int i;
3191 	int rval;
3192 
3193 	ep = fwnode_graph_get_endpoint_by_id(fwnode, 0, 0,
3194 					     FWNODE_GRAPH_ENDPOINT_NEXT);
3195 	if (!ep)
3196 		return -ENODEV;
3197 
3198 	/*
3199 	 * Note that we do need to rely on detecting the bus type between CSI-2
3200 	 * D-PHY and CCP2 as the old bindings did not require it.
3201 	 */
3202 	rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
3203 	if (rval)
3204 		goto out_err;
3205 
3206 	switch (bus_cfg.bus_type) {
3207 	case V4L2_MBUS_CSI2_DPHY:
3208 		hwcfg->csi_signalling_mode = CCS_CSI_SIGNALING_MODE_CSI_2_DPHY;
3209 		hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
3210 		break;
3211 	case V4L2_MBUS_CSI2_CPHY:
3212 		hwcfg->csi_signalling_mode = CCS_CSI_SIGNALING_MODE_CSI_2_CPHY;
3213 		hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
3214 		break;
3215 	case V4L2_MBUS_CSI1:
3216 	case V4L2_MBUS_CCP2:
3217 		hwcfg->csi_signalling_mode = (bus_cfg.bus.mipi_csi1.strobe) ?
3218 		SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE :
3219 		SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK;
3220 		hwcfg->lanes = 1;
3221 		break;
3222 	default:
3223 		dev_err(dev, "unsupported bus %u\n", bus_cfg.bus_type);
3224 		rval = -EINVAL;
3225 		goto out_err;
3226 	}
3227 
3228 	dev_dbg(dev, "lanes %u\n", hwcfg->lanes);
3229 
3230 	rval = fwnode_property_read_u32(fwnode, "rotation", &rotation);
3231 	if (!rval) {
3232 		switch (rotation) {
3233 		case 180:
3234 			hwcfg->module_board_orient =
3235 				CCS_MODULE_BOARD_ORIENT_180;
3236 			fallthrough;
3237 		case 0:
3238 			break;
3239 		default:
3240 			dev_err(dev, "invalid rotation %u\n", rotation);
3241 			rval = -EINVAL;
3242 			goto out_err;
3243 		}
3244 	}
3245 
3246 	rval = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
3247 					&hwcfg->ext_clk);
3248 	if (rval)
3249 		dev_info(dev, "can't get clock-frequency\n");
3250 
3251 	dev_dbg(dev, "clk %d, mode %d\n", hwcfg->ext_clk,
3252 		hwcfg->csi_signalling_mode);
3253 
3254 	if (!bus_cfg.nr_of_link_frequencies) {
3255 		dev_warn(dev, "no link frequencies defined\n");
3256 		rval = -EINVAL;
3257 		goto out_err;
3258 	}
3259 
3260 	hwcfg->op_sys_clock = devm_kcalloc(
3261 		dev, bus_cfg.nr_of_link_frequencies + 1 /* guardian */,
3262 		sizeof(*hwcfg->op_sys_clock), GFP_KERNEL);
3263 	if (!hwcfg->op_sys_clock) {
3264 		rval = -ENOMEM;
3265 		goto out_err;
3266 	}
3267 
3268 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
3269 		hwcfg->op_sys_clock[i] = bus_cfg.link_frequencies[i];
3270 		dev_dbg(dev, "freq %d: %lld\n", i, hwcfg->op_sys_clock[i]);
3271 	}
3272 
3273 	v4l2_fwnode_endpoint_free(&bus_cfg);
3274 	fwnode_handle_put(ep);
3275 
3276 	return 0;
3277 
3278 out_err:
3279 	v4l2_fwnode_endpoint_free(&bus_cfg);
3280 	fwnode_handle_put(ep);
3281 
3282 	return rval;
3283 }
3284 
ccs_probe(struct i2c_client * client)3285 static int ccs_probe(struct i2c_client *client)
3286 {
3287 	struct ccs_sensor *sensor;
3288 	const struct firmware *fw;
3289 	char filename[40];
3290 	unsigned int i;
3291 	int rval;
3292 
3293 	sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
3294 	if (sensor == NULL)
3295 		return -ENOMEM;
3296 
3297 	rval = ccs_get_hwconfig(sensor, &client->dev);
3298 	if (rval)
3299 		return rval;
3300 
3301 	sensor->src = &sensor->ssds[sensor->ssds_used];
3302 
3303 	v4l2_i2c_subdev_init(&sensor->src->sd, client, &ccs_ops);
3304 	sensor->src->sd.internal_ops = &ccs_internal_src_ops;
3305 
3306 	sensor->regulators = devm_kcalloc(&client->dev,
3307 					  ARRAY_SIZE(ccs_regulators),
3308 					  sizeof(*sensor->regulators),
3309 					  GFP_KERNEL);
3310 	if (!sensor->regulators)
3311 		return -ENOMEM;
3312 
3313 	for (i = 0; i < ARRAY_SIZE(ccs_regulators); i++)
3314 		sensor->regulators[i].supply = ccs_regulators[i];
3315 
3316 	rval = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(ccs_regulators),
3317 				       sensor->regulators);
3318 	if (rval) {
3319 		dev_err(&client->dev, "could not get regulators\n");
3320 		return rval;
3321 	}
3322 
3323 	sensor->ext_clk = devm_clk_get(&client->dev, NULL);
3324 	if (PTR_ERR(sensor->ext_clk) == -ENOENT) {
3325 		dev_info(&client->dev, "no clock defined, continuing...\n");
3326 		sensor->ext_clk = NULL;
3327 	} else if (IS_ERR(sensor->ext_clk)) {
3328 		dev_err(&client->dev, "could not get clock (%ld)\n",
3329 			PTR_ERR(sensor->ext_clk));
3330 		return -EPROBE_DEFER;
3331 	}
3332 
3333 	if (sensor->ext_clk) {
3334 		if (sensor->hwcfg.ext_clk) {
3335 			unsigned long rate;
3336 
3337 			rval = clk_set_rate(sensor->ext_clk,
3338 					    sensor->hwcfg.ext_clk);
3339 			if (rval < 0) {
3340 				dev_err(&client->dev,
3341 					"unable to set clock freq to %u\n",
3342 					sensor->hwcfg.ext_clk);
3343 				return rval;
3344 			}
3345 
3346 			rate = clk_get_rate(sensor->ext_clk);
3347 			if (rate != sensor->hwcfg.ext_clk) {
3348 				dev_err(&client->dev,
3349 					"can't set clock freq, asked for %u but got %lu\n",
3350 					sensor->hwcfg.ext_clk, rate);
3351 				return -EINVAL;
3352 			}
3353 		} else {
3354 			sensor->hwcfg.ext_clk = clk_get_rate(sensor->ext_clk);
3355 			dev_dbg(&client->dev, "obtained clock freq %u\n",
3356 				sensor->hwcfg.ext_clk);
3357 		}
3358 	} else if (sensor->hwcfg.ext_clk) {
3359 		dev_dbg(&client->dev, "assuming clock freq %u\n",
3360 			sensor->hwcfg.ext_clk);
3361 	} else {
3362 		dev_err(&client->dev, "unable to obtain clock freq\n");
3363 		return -EINVAL;
3364 	}
3365 
3366 	if (!sensor->hwcfg.ext_clk) {
3367 		dev_err(&client->dev, "cannot work with xclk frequency 0\n");
3368 		return -EINVAL;
3369 	}
3370 
3371 	sensor->reset = devm_gpiod_get_optional(&client->dev, "reset",
3372 						GPIOD_OUT_HIGH);
3373 	if (IS_ERR(sensor->reset))
3374 		return PTR_ERR(sensor->reset);
3375 	/* Support old users that may have used "xshutdown" property. */
3376 	if (!sensor->reset)
3377 		sensor->xshutdown = devm_gpiod_get_optional(&client->dev,
3378 							    "xshutdown",
3379 							    GPIOD_OUT_LOW);
3380 	if (IS_ERR(sensor->xshutdown))
3381 		return PTR_ERR(sensor->xshutdown);
3382 
3383 	rval = ccs_power_on(&client->dev);
3384 	if (rval < 0)
3385 		return rval;
3386 
3387 	mutex_init(&sensor->mutex);
3388 
3389 	rval = ccs_identify_module(sensor);
3390 	if (rval) {
3391 		rval = -ENODEV;
3392 		goto out_power_off;
3393 	}
3394 
3395 	rval = snprintf(filename, sizeof(filename),
3396 			"ccs/ccs-sensor-%4.4x-%4.4x-%4.4x.fw",
3397 			sensor->minfo.sensor_mipi_manufacturer_id,
3398 			sensor->minfo.sensor_model_id,
3399 			sensor->minfo.sensor_revision_number);
3400 	if (rval >= sizeof(filename)) {
3401 		rval = -ENOMEM;
3402 		goto out_power_off;
3403 	}
3404 
3405 	rval = request_firmware(&fw, filename, &client->dev);
3406 	if (!rval) {
3407 		ccs_data_parse(&sensor->sdata, fw->data, fw->size, &client->dev,
3408 			       true);
3409 		release_firmware(fw);
3410 	}
3411 
3412 	rval = snprintf(filename, sizeof(filename),
3413 			"ccs/ccs-module-%4.4x-%4.4x-%4.4x.fw",
3414 			sensor->minfo.mipi_manufacturer_id,
3415 			sensor->minfo.model_id,
3416 			sensor->minfo.revision_number);
3417 	if (rval >= sizeof(filename)) {
3418 		rval = -ENOMEM;
3419 		goto out_release_sdata;
3420 	}
3421 
3422 	rval = request_firmware(&fw, filename, &client->dev);
3423 	if (!rval) {
3424 		ccs_data_parse(&sensor->mdata, fw->data, fw->size, &client->dev,
3425 			       true);
3426 		release_firmware(fw);
3427 	}
3428 
3429 	rval = ccs_read_all_limits(sensor);
3430 	if (rval)
3431 		goto out_release_mdata;
3432 
3433 	rval = ccs_read_frame_fmt(sensor);
3434 	if (rval) {
3435 		rval = -ENODEV;
3436 		goto out_free_ccs_limits;
3437 	}
3438 
3439 	rval = ccs_update_phy_ctrl(sensor);
3440 	if (rval < 0)
3441 		goto out_free_ccs_limits;
3442 
3443 	/*
3444 	 * Handle Sensor Module orientation on the board.
3445 	 *
3446 	 * The application of H-FLIP and V-FLIP on the sensor is modified by
3447 	 * the sensor orientation on the board.
3448 	 *
3449 	 * For CCS_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
3450 	 * both H-FLIP and V-FLIP for normal operation which also implies
3451 	 * that a set/unset operation for user space HFLIP and VFLIP v4l2
3452 	 * controls will need to be internally inverted.
3453 	 *
3454 	 * Rotation also changes the bayer pattern.
3455 	 */
3456 	if (sensor->hwcfg.module_board_orient ==
3457 	    CCS_MODULE_BOARD_ORIENT_180)
3458 		sensor->hvflip_inv_mask =
3459 			CCS_IMAGE_ORIENTATION_HORIZONTAL_MIRROR |
3460 			CCS_IMAGE_ORIENTATION_VERTICAL_FLIP;
3461 
3462 	rval = ccs_call_quirk(sensor, limits);
3463 	if (rval) {
3464 		dev_err(&client->dev, "limits quirks failed\n");
3465 		goto out_free_ccs_limits;
3466 	}
3467 
3468 	if (CCS_LIM(sensor, BINNING_CAPABILITY)) {
3469 		sensor->nbinning_subtypes =
3470 			min_t(u8, CCS_LIM(sensor, BINNING_SUB_TYPES),
3471 			      CCS_LIM_BINNING_SUB_TYPE_MAX_N);
3472 
3473 		for (i = 0; i < sensor->nbinning_subtypes; i++) {
3474 			sensor->binning_subtypes[i].horizontal =
3475 				CCS_LIM_AT(sensor, BINNING_SUB_TYPE, i) >>
3476 				CCS_BINNING_SUB_TYPE_COLUMN_SHIFT;
3477 			sensor->binning_subtypes[i].vertical =
3478 				CCS_LIM_AT(sensor, BINNING_SUB_TYPE, i) &
3479 				CCS_BINNING_SUB_TYPE_ROW_MASK;
3480 
3481 			dev_dbg(&client->dev, "binning %xx%x\n",
3482 				sensor->binning_subtypes[i].horizontal,
3483 				sensor->binning_subtypes[i].vertical);
3484 		}
3485 	}
3486 	sensor->binning_horizontal = 1;
3487 	sensor->binning_vertical = 1;
3488 
3489 	if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
3490 		dev_err(&client->dev, "sysfs ident entry creation failed\n");
3491 		rval = -ENOENT;
3492 		goto out_free_ccs_limits;
3493 	}
3494 
3495 	if (sensor->minfo.smiapp_version &&
3496 	    CCS_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) &
3497 	    CCS_DATA_TRANSFER_IF_CAPABILITY_SUPPORTED) {
3498 		if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
3499 			dev_err(&client->dev, "sysfs nvm entry failed\n");
3500 			rval = -EBUSY;
3501 			goto out_cleanup;
3502 		}
3503 	}
3504 
3505 	if (!CCS_LIM(sensor, MIN_OP_SYS_CLK_DIV) ||
3506 	    !CCS_LIM(sensor, MAX_OP_SYS_CLK_DIV) ||
3507 	    !CCS_LIM(sensor, MIN_OP_PIX_CLK_DIV) ||
3508 	    !CCS_LIM(sensor, MAX_OP_PIX_CLK_DIV)) {
3509 		/* No OP clock branch */
3510 		sensor->pll.flags |= CCS_PLL_FLAG_NO_OP_CLOCKS;
3511 	} else if (CCS_LIM(sensor, SCALING_CAPABILITY)
3512 		   != CCS_SCALING_CAPABILITY_NONE ||
3513 		   CCS_LIM(sensor, DIGITAL_CROP_CAPABILITY)
3514 		   == CCS_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
3515 		/* We have a scaler or digital crop. */
3516 		sensor->scaler = &sensor->ssds[sensor->ssds_used];
3517 		sensor->ssds_used++;
3518 	}
3519 	sensor->binner = &sensor->ssds[sensor->ssds_used];
3520 	sensor->ssds_used++;
3521 	sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
3522 	sensor->ssds_used++;
3523 
3524 	sensor->scale_m = CCS_LIM(sensor, SCALER_N_MIN);
3525 
3526 	/* prepare PLL configuration input values */
3527 	sensor->pll.bus_type = CCS_PLL_BUS_TYPE_CSI2_DPHY;
3528 	sensor->pll.csi2.lanes = sensor->hwcfg.lanes;
3529 	if (CCS_LIM(sensor, CLOCK_CALCULATION) &
3530 	    CCS_CLOCK_CALCULATION_LANE_SPEED) {
3531 		sensor->pll.flags |= CCS_PLL_FLAG_LANE_SPEED_MODEL;
3532 		if (CCS_LIM(sensor, CLOCK_CALCULATION) &
3533 		    CCS_CLOCK_CALCULATION_LINK_DECOUPLED) {
3534 			sensor->pll.vt_lanes =
3535 				CCS_LIM(sensor, NUM_OF_VT_LANES) + 1;
3536 			sensor->pll.op_lanes =
3537 				CCS_LIM(sensor, NUM_OF_OP_LANES) + 1;
3538 			sensor->pll.flags |= CCS_PLL_FLAG_LINK_DECOUPLED;
3539 		} else {
3540 			sensor->pll.vt_lanes = sensor->pll.csi2.lanes;
3541 			sensor->pll.op_lanes = sensor->pll.csi2.lanes;
3542 		}
3543 	}
3544 	if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) &
3545 	    CCS_CLOCK_TREE_PLL_CAPABILITY_EXT_DIVIDER)
3546 		sensor->pll.flags |= CCS_PLL_FLAG_EXT_IP_PLL_DIVIDER;
3547 	if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) &
3548 	    CCS_CLOCK_TREE_PLL_CAPABILITY_FLEXIBLE_OP_PIX_CLK_DIV)
3549 		sensor->pll.flags |= CCS_PLL_FLAG_FLEXIBLE_OP_PIX_CLK_DIV;
3550 	if (CCS_LIM(sensor, FIFO_SUPPORT_CAPABILITY) &
3551 	    CCS_FIFO_SUPPORT_CAPABILITY_DERATING)
3552 		sensor->pll.flags |= CCS_PLL_FLAG_FIFO_DERATING;
3553 	if (CCS_LIM(sensor, FIFO_SUPPORT_CAPABILITY) &
3554 	    CCS_FIFO_SUPPORT_CAPABILITY_DERATING_OVERRATING)
3555 		sensor->pll.flags |= CCS_PLL_FLAG_FIFO_DERATING |
3556 				     CCS_PLL_FLAG_FIFO_OVERRATING;
3557 	if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) &
3558 	    CCS_CLOCK_TREE_PLL_CAPABILITY_DUAL_PLL) {
3559 		if (CCS_LIM(sensor, CLOCK_TREE_PLL_CAPABILITY) &
3560 		    CCS_CLOCK_TREE_PLL_CAPABILITY_SINGLE_PLL) {
3561 			u32 v;
3562 
3563 			/* Use sensor default in PLL mode selection */
3564 			rval = ccs_read(sensor, PLL_MODE, &v);
3565 			if (rval)
3566 				goto out_cleanup;
3567 
3568 			if (v == CCS_PLL_MODE_DUAL)
3569 				sensor->pll.flags |= CCS_PLL_FLAG_DUAL_PLL;
3570 		} else {
3571 			sensor->pll.flags |= CCS_PLL_FLAG_DUAL_PLL;
3572 		}
3573 		if (CCS_LIM(sensor, CLOCK_CALCULATION) &
3574 		    CCS_CLOCK_CALCULATION_DUAL_PLL_OP_SYS_DDR)
3575 			sensor->pll.flags |= CCS_PLL_FLAG_OP_SYS_DDR;
3576 		if (CCS_LIM(sensor, CLOCK_CALCULATION) &
3577 		    CCS_CLOCK_CALCULATION_DUAL_PLL_OP_PIX_DDR)
3578 			sensor->pll.flags |= CCS_PLL_FLAG_OP_PIX_DDR;
3579 	}
3580 	sensor->pll.op_bits_per_lane = CCS_LIM(sensor, OP_BITS_PER_LANE);
3581 	sensor->pll.ext_clk_freq_hz = sensor->hwcfg.ext_clk;
3582 	sensor->pll.scale_n = CCS_LIM(sensor, SCALER_N_MIN);
3583 
3584 	ccs_create_subdev(sensor, sensor->scaler, " scaler", 2,
3585 			  MEDIA_ENT_F_PROC_VIDEO_SCALER);
3586 	ccs_create_subdev(sensor, sensor->binner, " binner", 2,
3587 			  MEDIA_ENT_F_PROC_VIDEO_SCALER);
3588 	ccs_create_subdev(sensor, sensor->pixel_array, " pixel_array", 1,
3589 			  MEDIA_ENT_F_CAM_SENSOR);
3590 
3591 	rval = ccs_init_controls(sensor);
3592 	if (rval < 0)
3593 		goto out_cleanup;
3594 
3595 	rval = ccs_call_quirk(sensor, init);
3596 	if (rval)
3597 		goto out_cleanup;
3598 
3599 	rval = ccs_get_mbus_formats(sensor);
3600 	if (rval) {
3601 		rval = -ENODEV;
3602 		goto out_cleanup;
3603 	}
3604 
3605 	rval = ccs_init_late_controls(sensor);
3606 	if (rval) {
3607 		rval = -ENODEV;
3608 		goto out_cleanup;
3609 	}
3610 
3611 	mutex_lock(&sensor->mutex);
3612 	rval = ccs_pll_blanking_update(sensor);
3613 	mutex_unlock(&sensor->mutex);
3614 	if (rval) {
3615 		dev_err(&client->dev, "update mode failed\n");
3616 		goto out_cleanup;
3617 	}
3618 
3619 	sensor->streaming = false;
3620 	sensor->dev_init_done = true;
3621 
3622 	rval = media_entity_pads_init(&sensor->src->sd.entity, 2,
3623 				 sensor->src->pads);
3624 	if (rval < 0)
3625 		goto out_media_entity_cleanup;
3626 
3627 	rval = ccs_write_msr_regs(sensor);
3628 	if (rval)
3629 		goto out_media_entity_cleanup;
3630 
3631 	pm_runtime_set_active(&client->dev);
3632 	pm_runtime_get_noresume(&client->dev);
3633 	pm_runtime_enable(&client->dev);
3634 
3635 	rval = v4l2_async_register_subdev_sensor(&sensor->src->sd);
3636 	if (rval < 0)
3637 		goto out_disable_runtime_pm;
3638 
3639 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
3640 	pm_runtime_use_autosuspend(&client->dev);
3641 	pm_runtime_put_autosuspend(&client->dev);
3642 
3643 	return 0;
3644 
3645 out_disable_runtime_pm:
3646 	pm_runtime_put_noidle(&client->dev);
3647 	pm_runtime_disable(&client->dev);
3648 
3649 out_media_entity_cleanup:
3650 	media_entity_cleanup(&sensor->src->sd.entity);
3651 
3652 out_cleanup:
3653 	ccs_cleanup(sensor);
3654 
3655 out_release_mdata:
3656 	kvfree(sensor->mdata.backing);
3657 
3658 out_release_sdata:
3659 	kvfree(sensor->sdata.backing);
3660 
3661 out_free_ccs_limits:
3662 	kfree(sensor->ccs_limits);
3663 
3664 out_power_off:
3665 	ccs_power_off(&client->dev);
3666 	mutex_destroy(&sensor->mutex);
3667 
3668 	return rval;
3669 }
3670 
ccs_remove(struct i2c_client * client)3671 static int ccs_remove(struct i2c_client *client)
3672 {
3673 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3674 	struct ccs_sensor *sensor = to_ccs_sensor(subdev);
3675 	unsigned int i;
3676 
3677 	v4l2_async_unregister_subdev(subdev);
3678 
3679 	pm_runtime_disable(&client->dev);
3680 	if (!pm_runtime_status_suspended(&client->dev))
3681 		ccs_power_off(&client->dev);
3682 	pm_runtime_set_suspended(&client->dev);
3683 
3684 	for (i = 0; i < sensor->ssds_used; i++) {
3685 		v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
3686 		media_entity_cleanup(&sensor->ssds[i].sd.entity);
3687 	}
3688 	ccs_cleanup(sensor);
3689 	mutex_destroy(&sensor->mutex);
3690 	kfree(sensor->ccs_limits);
3691 	kvfree(sensor->sdata.backing);
3692 	kvfree(sensor->mdata.backing);
3693 
3694 	return 0;
3695 }
3696 
3697 static const struct ccs_device smia_device = {
3698 	.flags = CCS_DEVICE_FLAG_IS_SMIA,
3699 };
3700 
3701 static const struct ccs_device ccs_device = {};
3702 
3703 static const struct acpi_device_id ccs_acpi_table[] = {
3704 	{ .id = "MIPI0200", .driver_data = (unsigned long)&ccs_device },
3705 	{ },
3706 };
3707 MODULE_DEVICE_TABLE(acpi, ccs_acpi_table);
3708 
3709 static const struct of_device_id ccs_of_table[] = {
3710 	{ .compatible = "mipi-ccs-1.1", .data = &ccs_device },
3711 	{ .compatible = "mipi-ccs-1.0", .data = &ccs_device },
3712 	{ .compatible = "mipi-ccs", .data = &ccs_device },
3713 	{ .compatible = "nokia,smia", .data = &smia_device },
3714 	{ },
3715 };
3716 MODULE_DEVICE_TABLE(of, ccs_of_table);
3717 
3718 static const struct dev_pm_ops ccs_pm_ops = {
3719 	SET_SYSTEM_SLEEP_PM_OPS(ccs_suspend, ccs_resume)
3720 	SET_RUNTIME_PM_OPS(ccs_power_off, ccs_power_on, NULL)
3721 };
3722 
3723 static struct i2c_driver ccs_i2c_driver = {
3724 	.driver	= {
3725 		.acpi_match_table = ccs_acpi_table,
3726 		.of_match_table = ccs_of_table,
3727 		.name = CCS_NAME,
3728 		.pm = &ccs_pm_ops,
3729 	},
3730 	.probe_new = ccs_probe,
3731 	.remove	= ccs_remove,
3732 };
3733 
ccs_module_init(void)3734 static int ccs_module_init(void)
3735 {
3736 	unsigned int i, l;
3737 
3738 	for (i = 0, l = 0; ccs_limits[i].size && l < CCS_L_LAST; i++) {
3739 		if (!(ccs_limits[i].flags & CCS_L_FL_SAME_REG)) {
3740 			ccs_limit_offsets[l + 1].lim =
3741 				ALIGN(ccs_limit_offsets[l].lim +
3742 				      ccs_limits[i].size,
3743 				      ccs_reg_width(ccs_limits[i + 1].reg));
3744 			ccs_limit_offsets[l].info = i;
3745 			l++;
3746 		} else {
3747 			ccs_limit_offsets[l].lim += ccs_limits[i].size;
3748 		}
3749 	}
3750 
3751 	if (WARN_ON(ccs_limits[i].size))
3752 		return -EINVAL;
3753 
3754 	if (WARN_ON(l != CCS_L_LAST))
3755 		return -EINVAL;
3756 
3757 	return i2c_register_driver(THIS_MODULE, &ccs_i2c_driver);
3758 }
3759 
ccs_module_cleanup(void)3760 static void ccs_module_cleanup(void)
3761 {
3762 	i2c_del_driver(&ccs_i2c_driver);
3763 }
3764 
3765 module_init(ccs_module_init);
3766 module_exit(ccs_module_cleanup);
3767 
3768 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
3769 MODULE_DESCRIPTION("Generic MIPI CCS/SMIA/SMIA++ camera sensor driver");
3770 MODULE_LICENSE("GPL v2");
3771 MODULE_ALIAS("smiapp");
3772