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