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