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