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