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