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