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