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