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