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