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