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