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