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