sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / i2c / smiapp / smiapp-core.c
blob59872b31f832cb7983337e1ce1290a5eea1aa36a
1 /*
2 * drivers/media/i2c/smiapp/smiapp-core.c
4 * Generic driver for SMIA/SMIA++ compliant camera modules
6 * Copyright (C) 2010--2012 Nokia Corporation
7 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
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>
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.
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.
23 #include <linux/clk.h>
24 #include <linux/delay.h>
25 #include <linux/device.h>
26 #include <linux/gpio.h>
27 #include <linux/gpio/consumer.h>
28 #include <linux/module.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/slab.h>
32 #include <linux/smiapp.h>
33 #include <linux/v4l2-mediabus.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-of.h>
37 #include "smiapp.h"
39 #define SMIAPP_ALIGN_DIM(dim, flags) \
40 ((flags) & V4L2_SEL_FLAG_GE \
41 ? ALIGN((dim), 2) \
42 : (dim) & ~1)
45 * smiapp_module_idents - supported camera modules
47 static const struct smiapp_module_ident smiapp_module_idents[] = {
48 SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
49 SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
50 SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
51 SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
52 SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
53 SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk),
54 SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
55 SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
56 SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk),
57 SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk),
58 SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk),
63 * Dynamic Capability Identification
67 static int smiapp_read_frame_fmt(struct smiapp_sensor *sensor)
69 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
70 u32 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc;
71 unsigned int i;
72 int pixel_count = 0;
73 int line_count = 0;
74 int rval;
76 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
77 &fmt_model_type);
78 if (rval)
79 return rval;
81 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
82 &fmt_model_subtype);
83 if (rval)
84 return rval;
86 ncol_desc = (fmt_model_subtype
87 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK)
88 >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT;
89 nrow_desc = fmt_model_subtype
90 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK;
92 dev_dbg(&client->dev, "format_model_type %s\n",
93 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
94 ? "2 byte" :
95 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
96 ? "4 byte" : "is simply bad");
98 for (i = 0; i < ncol_desc + nrow_desc; i++) {
99 u32 desc;
100 u32 pixelcode;
101 u32 pixels;
102 char *which;
103 char *what;
104 u32 reg;
106 if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) {
107 reg = SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i);
108 rval = smiapp_read(sensor, reg, &desc);
109 if (rval)
110 return rval;
112 pixelcode =
113 (desc
114 & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK)
115 >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT;
116 pixels = desc & SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK;
117 } else if (fmt_model_type
118 == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE) {
119 reg = SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i);
120 rval = smiapp_read(sensor, reg, &desc);
121 if (rval)
122 return rval;
124 pixelcode =
125 (desc
126 & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK)
127 >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT;
128 pixels = desc & SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK;
129 } else {
130 dev_dbg(&client->dev,
131 "invalid frame format model type %d\n",
132 fmt_model_type);
133 return -EINVAL;
136 if (i < ncol_desc)
137 which = "columns";
138 else
139 which = "rows";
141 switch (pixelcode) {
142 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
143 what = "embedded";
144 break;
145 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY:
146 what = "dummy";
147 break;
148 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK:
149 what = "black";
150 break;
151 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK:
152 what = "dark";
153 break;
154 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
155 what = "visible";
156 break;
157 default:
158 what = "invalid";
159 break;
162 dev_dbg(&client->dev,
163 "0x%8.8x %s pixels: %d %s (pixelcode %u)\n", reg,
164 what, pixels, which, pixelcode);
166 if (i < ncol_desc) {
167 if (pixelcode ==
168 SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE)
169 sensor->visible_pixel_start = pixel_count;
170 pixel_count += pixels;
171 continue;
174 /* Handle row descriptors */
175 switch (pixelcode) {
176 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
177 if (sensor->embedded_end)
178 break;
179 sensor->embedded_start = line_count;
180 sensor->embedded_end = line_count + pixels;
181 break;
182 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
183 sensor->image_start = line_count;
184 break;
186 line_count += pixels;
189 if (sensor->embedded_end > sensor->image_start) {
190 dev_dbg(&client->dev,
191 "adjusting image start line to %u (was %u)\n",
192 sensor->embedded_end, sensor->image_start);
193 sensor->image_start = sensor->embedded_end;
196 dev_dbg(&client->dev, "embedded data from lines %d to %d\n",
197 sensor->embedded_start, sensor->embedded_end);
198 dev_dbg(&client->dev, "image data starts at line %d\n",
199 sensor->image_start);
201 return 0;
204 static int smiapp_pll_configure(struct smiapp_sensor *sensor)
206 struct smiapp_pll *pll = &sensor->pll;
207 int rval;
209 rval = smiapp_write(
210 sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt.pix_clk_div);
211 if (rval < 0)
212 return rval;
214 rval = smiapp_write(
215 sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt.sys_clk_div);
216 if (rval < 0)
217 return rval;
219 rval = smiapp_write(
220 sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
221 if (rval < 0)
222 return rval;
224 rval = smiapp_write(
225 sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
226 if (rval < 0)
227 return rval;
229 /* Lane op clock ratio does not apply here. */
230 rval = smiapp_write(
231 sensor, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS,
232 DIV_ROUND_UP(pll->op.sys_clk_freq_hz, 1000000 / 256 / 256));
233 if (rval < 0 || sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
234 return rval;
236 rval = smiapp_write(
237 sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op.pix_clk_div);
238 if (rval < 0)
239 return rval;
241 return smiapp_write(
242 sensor, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op.sys_clk_div);
245 static int smiapp_pll_try(struct smiapp_sensor *sensor,
246 struct smiapp_pll *pll)
248 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
249 struct smiapp_pll_limits lim = {
250 .min_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV],
251 .max_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV],
252 .min_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ],
253 .max_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ],
254 .min_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER],
255 .max_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER],
256 .min_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ],
257 .max_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ],
259 .op.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV],
260 .op.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV],
261 .op.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV],
262 .op.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV],
263 .op.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ],
264 .op.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ],
265 .op.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ],
266 .op.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ],
268 .vt.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV],
269 .vt.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV],
270 .vt.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV],
271 .vt.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV],
272 .vt.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ],
273 .vt.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ],
274 .vt.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ],
275 .vt.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ],
277 .min_line_length_pck_bin = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN],
278 .min_line_length_pck = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK],
281 return smiapp_pll_calculate(&client->dev, &lim, pll);
284 static int smiapp_pll_update(struct smiapp_sensor *sensor)
286 struct smiapp_pll *pll = &sensor->pll;
287 int rval;
289 pll->binning_horizontal = sensor->binning_horizontal;
290 pll->binning_vertical = sensor->binning_vertical;
291 pll->link_freq =
292 sensor->link_freq->qmenu_int[sensor->link_freq->val];
293 pll->scale_m = sensor->scale_m;
294 pll->bits_per_pixel = sensor->csi_format->compressed;
296 rval = smiapp_pll_try(sensor, pll);
297 if (rval < 0)
298 return rval;
300 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_parray,
301 pll->pixel_rate_pixel_array);
302 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_csi, pll->pixel_rate_csi);
304 return 0;
310 * V4L2 Controls handling
314 static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
316 struct v4l2_ctrl *ctrl = sensor->exposure;
317 int max;
319 max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
320 + sensor->vblank->val
321 - sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN];
323 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, ctrl->step, max);
327 * Order matters.
329 * 1. Bits-per-pixel, descending.
330 * 2. Bits-per-pixel compressed, descending.
331 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
332 * orders must be defined.
334 static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
335 { MEDIA_BUS_FMT_SGRBG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GRBG, },
336 { MEDIA_BUS_FMT_SRGGB16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_RGGB, },
337 { MEDIA_BUS_FMT_SBGGR16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_BGGR, },
338 { MEDIA_BUS_FMT_SGBRG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GBRG, },
339 { MEDIA_BUS_FMT_SGRBG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GRBG, },
340 { MEDIA_BUS_FMT_SRGGB14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_RGGB, },
341 { MEDIA_BUS_FMT_SBGGR14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_BGGR, },
342 { MEDIA_BUS_FMT_SGBRG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GBRG, },
343 { MEDIA_BUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
344 { MEDIA_BUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
345 { MEDIA_BUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
346 { MEDIA_BUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
347 { MEDIA_BUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
348 { MEDIA_BUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
349 { MEDIA_BUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
350 { MEDIA_BUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
351 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
352 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
353 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
354 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
355 { MEDIA_BUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, },
356 { MEDIA_BUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, },
357 { MEDIA_BUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, },
358 { MEDIA_BUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, },
361 static const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
363 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
364 - (unsigned long)smiapp_csi_data_formats) \
365 / sizeof(*smiapp_csi_data_formats))
367 static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
369 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
370 int flip = 0;
372 if (sensor->hflip) {
373 if (sensor->hflip->val)
374 flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
376 if (sensor->vflip->val)
377 flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
380 flip ^= sensor->hvflip_inv_mask;
382 dev_dbg(&client->dev, "flip %d\n", flip);
383 return sensor->default_pixel_order ^ flip;
386 static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
388 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
389 unsigned int csi_format_idx =
390 to_csi_format_idx(sensor->csi_format) & ~3;
391 unsigned int internal_csi_format_idx =
392 to_csi_format_idx(sensor->internal_csi_format) & ~3;
393 unsigned int pixel_order = smiapp_pixel_order(sensor);
395 sensor->mbus_frame_fmts =
396 sensor->default_mbus_frame_fmts << pixel_order;
397 sensor->csi_format =
398 &smiapp_csi_data_formats[csi_format_idx + pixel_order];
399 sensor->internal_csi_format =
400 &smiapp_csi_data_formats[internal_csi_format_idx
401 + pixel_order];
403 BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
404 >= ARRAY_SIZE(smiapp_csi_data_formats));
406 dev_dbg(&client->dev, "new pixel order %s\n",
407 pixel_order_str[pixel_order]);
410 static const char * const smiapp_test_patterns[] = {
411 "Disabled",
412 "Solid Colour",
413 "Eight Vertical Colour Bars",
414 "Colour Bars With Fade to Grey",
415 "Pseudorandom Sequence (PN9)",
418 static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
420 struct smiapp_sensor *sensor =
421 container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
422 ->sensor;
423 u32 orient = 0;
424 int exposure;
425 int rval;
427 switch (ctrl->id) {
428 case V4L2_CID_ANALOGUE_GAIN:
429 return smiapp_write(
430 sensor,
431 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
433 case V4L2_CID_EXPOSURE:
434 return smiapp_write(
435 sensor,
436 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
438 case V4L2_CID_HFLIP:
439 case V4L2_CID_VFLIP:
440 if (sensor->streaming)
441 return -EBUSY;
443 if (sensor->hflip->val)
444 orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
446 if (sensor->vflip->val)
447 orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
449 orient ^= sensor->hvflip_inv_mask;
450 rval = smiapp_write(sensor, SMIAPP_REG_U8_IMAGE_ORIENTATION,
451 orient);
452 if (rval < 0)
453 return rval;
455 smiapp_update_mbus_formats(sensor);
457 return 0;
459 case V4L2_CID_VBLANK:
460 exposure = sensor->exposure->val;
462 __smiapp_update_exposure_limits(sensor);
464 if (exposure > sensor->exposure->maximum) {
465 sensor->exposure->val = sensor->exposure->maximum;
466 rval = smiapp_set_ctrl(sensor->exposure);
467 if (rval < 0)
468 return rval;
471 return smiapp_write(
472 sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
473 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
474 + ctrl->val);
476 case V4L2_CID_HBLANK:
477 return smiapp_write(
478 sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
479 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
480 + ctrl->val);
482 case V4L2_CID_LINK_FREQ:
483 if (sensor->streaming)
484 return -EBUSY;
486 return smiapp_pll_update(sensor);
488 case V4L2_CID_TEST_PATTERN: {
489 unsigned int i;
491 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
492 v4l2_ctrl_activate(
493 sensor->test_data[i],
494 ctrl->val ==
495 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR);
497 return smiapp_write(
498 sensor, SMIAPP_REG_U16_TEST_PATTERN_MODE, ctrl->val);
501 case V4L2_CID_TEST_PATTERN_RED:
502 return smiapp_write(
503 sensor, SMIAPP_REG_U16_TEST_DATA_RED, ctrl->val);
505 case V4L2_CID_TEST_PATTERN_GREENR:
506 return smiapp_write(
507 sensor, SMIAPP_REG_U16_TEST_DATA_GREENR, ctrl->val);
509 case V4L2_CID_TEST_PATTERN_BLUE:
510 return smiapp_write(
511 sensor, SMIAPP_REG_U16_TEST_DATA_BLUE, ctrl->val);
513 case V4L2_CID_TEST_PATTERN_GREENB:
514 return smiapp_write(
515 sensor, SMIAPP_REG_U16_TEST_DATA_GREENB, ctrl->val);
517 case V4L2_CID_PIXEL_RATE:
518 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
519 return 0;
521 default:
522 return -EINVAL;
526 static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
527 .s_ctrl = smiapp_set_ctrl,
530 static int smiapp_init_controls(struct smiapp_sensor *sensor)
532 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
533 int rval;
535 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 12);
536 if (rval)
537 return rval;
539 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
541 sensor->analog_gain = v4l2_ctrl_new_std(
542 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
543 V4L2_CID_ANALOGUE_GAIN,
544 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN],
545 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX],
546 max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U),
547 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]);
549 /* Exposure limits will be updated soon, use just something here. */
550 sensor->exposure = v4l2_ctrl_new_std(
551 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
552 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
554 sensor->hflip = v4l2_ctrl_new_std(
555 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
556 V4L2_CID_HFLIP, 0, 1, 1, 0);
557 sensor->vflip = v4l2_ctrl_new_std(
558 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
559 V4L2_CID_VFLIP, 0, 1, 1, 0);
561 sensor->vblank = v4l2_ctrl_new_std(
562 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
563 V4L2_CID_VBLANK, 0, 1, 1, 0);
565 if (sensor->vblank)
566 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
568 sensor->hblank = v4l2_ctrl_new_std(
569 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
570 V4L2_CID_HBLANK, 0, 1, 1, 0);
572 if (sensor->hblank)
573 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
575 sensor->pixel_rate_parray = v4l2_ctrl_new_std(
576 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
577 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
579 v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler,
580 &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN,
581 ARRAY_SIZE(smiapp_test_patterns) - 1,
582 0, 0, smiapp_test_patterns);
584 if (sensor->pixel_array->ctrl_handler.error) {
585 dev_err(&client->dev,
586 "pixel array controls initialization failed (%d)\n",
587 sensor->pixel_array->ctrl_handler.error);
588 return sensor->pixel_array->ctrl_handler.error;
591 sensor->pixel_array->sd.ctrl_handler =
592 &sensor->pixel_array->ctrl_handler;
594 v4l2_ctrl_cluster(2, &sensor->hflip);
596 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
597 if (rval)
598 return rval;
600 sensor->src->ctrl_handler.lock = &sensor->mutex;
602 sensor->pixel_rate_csi = v4l2_ctrl_new_std(
603 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
604 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
606 if (sensor->src->ctrl_handler.error) {
607 dev_err(&client->dev,
608 "src controls initialization failed (%d)\n",
609 sensor->src->ctrl_handler.error);
610 return sensor->src->ctrl_handler.error;
613 sensor->src->sd.ctrl_handler = &sensor->src->ctrl_handler;
615 return 0;
619 * For controls that require information on available media bus codes
620 * and linke frequencies.
622 static int smiapp_init_late_controls(struct smiapp_sensor *sensor)
624 unsigned long *valid_link_freqs = &sensor->valid_link_freqs[
625 sensor->csi_format->compressed - sensor->compressed_min_bpp];
626 unsigned int max, i;
628 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) {
629 int max_value = (1 << sensor->csi_format->width) - 1;
631 sensor->test_data[i] = v4l2_ctrl_new_std(
632 &sensor->pixel_array->ctrl_handler,
633 &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i,
634 0, max_value, 1, max_value);
637 for (max = 0; sensor->hwcfg->op_sys_clock[max + 1]; max++);
639 sensor->link_freq = v4l2_ctrl_new_int_menu(
640 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
641 V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs),
642 __ffs(*valid_link_freqs), sensor->hwcfg->op_sys_clock);
644 return sensor->src->ctrl_handler.error;
647 static void smiapp_free_controls(struct smiapp_sensor *sensor)
649 unsigned int i;
651 for (i = 0; i < sensor->ssds_used; i++)
652 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
655 static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
656 unsigned int n)
658 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
659 unsigned int i;
660 u32 val;
661 int rval;
663 for (i = 0; i < n; i++) {
664 rval = smiapp_read(
665 sensor, smiapp_reg_limits[limit[i]].addr, &val);
666 if (rval)
667 return rval;
668 sensor->limits[limit[i]] = val;
669 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n",
670 smiapp_reg_limits[limit[i]].addr,
671 smiapp_reg_limits[limit[i]].what, val, val);
674 return 0;
677 static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
679 unsigned int i;
680 int rval;
682 for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
683 rval = smiapp_get_limits(sensor, &i, 1);
684 if (rval < 0)
685 return rval;
688 if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
689 smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
691 return 0;
694 static int smiapp_get_limits_binning(struct smiapp_sensor *sensor)
696 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
697 static u32 const limits[] = {
698 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN,
699 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN,
700 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN,
701 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN,
702 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN,
703 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN,
704 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN,
706 static u32 const limits_replace[] = {
707 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES,
708 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES,
709 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK,
710 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK,
711 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK,
712 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN,
713 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN,
715 unsigned int i;
716 int rval;
718 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
719 SMIAPP_BINNING_CAPABILITY_NO) {
720 for (i = 0; i < ARRAY_SIZE(limits); i++)
721 sensor->limits[limits[i]] =
722 sensor->limits[limits_replace[i]];
724 return 0;
727 rval = smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits));
728 if (rval < 0)
729 return rval;
732 * Sanity check whether the binning limits are valid. If not,
733 * use the non-binning ones.
735 if (sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN]
736 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN]
737 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN])
738 return 0;
740 for (i = 0; i < ARRAY_SIZE(limits); i++) {
741 dev_dbg(&client->dev,
742 "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
743 smiapp_reg_limits[limits[i]].addr,
744 smiapp_reg_limits[limits[i]].what,
745 sensor->limits[limits_replace[i]],
746 sensor->limits[limits_replace[i]]);
747 sensor->limits[limits[i]] =
748 sensor->limits[limits_replace[i]];
751 return 0;
754 static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
756 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
757 struct smiapp_pll *pll = &sensor->pll;
758 u8 compressed_max_bpp = 0;
759 unsigned int type, n;
760 unsigned int i, pixel_order;
761 int rval;
763 rval = smiapp_read(
764 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
765 if (rval)
766 return rval;
768 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
770 rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
771 &pixel_order);
772 if (rval)
773 return rval;
775 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
776 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
777 return -EINVAL;
780 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
781 pixel_order_str[pixel_order]);
783 switch (type) {
784 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
785 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
786 break;
787 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
788 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
789 break;
790 default:
791 return -EINVAL;
794 sensor->default_pixel_order = pixel_order;
795 sensor->mbus_frame_fmts = 0;
797 for (i = 0; i < n; i++) {
798 unsigned int fmt, j;
800 rval = smiapp_read(
801 sensor,
802 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
803 if (rval)
804 return rval;
806 dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
807 i, fmt >> 8, (u8)fmt);
809 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
810 const struct smiapp_csi_data_format *f =
811 &smiapp_csi_data_formats[j];
813 if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
814 continue;
816 if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
817 continue;
819 dev_dbg(&client->dev, "jolly good! %d\n", j);
821 sensor->default_mbus_frame_fmts |= 1 << j;
825 /* Figure out which BPP values can be used with which formats. */
826 pll->binning_horizontal = 1;
827 pll->binning_vertical = 1;
828 pll->scale_m = sensor->scale_m;
830 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
831 sensor->compressed_min_bpp =
832 min(smiapp_csi_data_formats[i].compressed,
833 sensor->compressed_min_bpp);
834 compressed_max_bpp =
835 max(smiapp_csi_data_formats[i].compressed,
836 compressed_max_bpp);
839 sensor->valid_link_freqs = devm_kcalloc(
840 &client->dev,
841 compressed_max_bpp - sensor->compressed_min_bpp + 1,
842 sizeof(*sensor->valid_link_freqs), GFP_KERNEL);
844 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
845 const struct smiapp_csi_data_format *f =
846 &smiapp_csi_data_formats[i];
847 unsigned long *valid_link_freqs =
848 &sensor->valid_link_freqs[
849 f->compressed - sensor->compressed_min_bpp];
850 unsigned int j;
852 if (!(sensor->default_mbus_frame_fmts & 1 << i))
853 continue;
855 pll->bits_per_pixel = f->compressed;
857 for (j = 0; sensor->hwcfg->op_sys_clock[j]; j++) {
858 pll->link_freq = sensor->hwcfg->op_sys_clock[j];
860 rval = smiapp_pll_try(sensor, pll);
861 dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n",
862 pll->link_freq, pll->bits_per_pixel,
863 rval ? "not ok" : "ok");
864 if (rval)
865 continue;
867 set_bit(j, valid_link_freqs);
870 if (!*valid_link_freqs) {
871 dev_info(&client->dev,
872 "no valid link frequencies for %u bpp\n",
873 f->compressed);
874 sensor->default_mbus_frame_fmts &= ~BIT(i);
875 continue;
878 if (!sensor->csi_format
879 || f->width > sensor->csi_format->width
880 || (f->width == sensor->csi_format->width
881 && f->compressed > sensor->csi_format->compressed)) {
882 sensor->csi_format = f;
883 sensor->internal_csi_format = f;
887 if (!sensor->csi_format) {
888 dev_err(&client->dev, "no supported mbus code found\n");
889 return -EINVAL;
892 smiapp_update_mbus_formats(sensor);
894 return 0;
897 static void smiapp_update_blanking(struct smiapp_sensor *sensor)
899 struct v4l2_ctrl *vblank = sensor->vblank;
900 struct v4l2_ctrl *hblank = sensor->hblank;
901 int min, max;
903 min = max_t(int,
904 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
905 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
906 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
907 max = sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
908 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
910 __v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);
912 min = max_t(int,
913 sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
914 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
915 sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
916 max = sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
917 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
919 __v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
921 __smiapp_update_exposure_limits(sensor);
924 static int smiapp_update_mode(struct smiapp_sensor *sensor)
926 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
927 unsigned int binning_mode;
928 int rval;
930 /* Binning has to be set up here; it affects limits */
931 if (sensor->binning_horizontal == 1 &&
932 sensor->binning_vertical == 1) {
933 binning_mode = 0;
934 } else {
935 u8 binning_type =
936 (sensor->binning_horizontal << 4)
937 | sensor->binning_vertical;
939 rval = smiapp_write(
940 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
941 if (rval < 0)
942 return rval;
944 binning_mode = 1;
946 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
947 if (rval < 0)
948 return rval;
950 /* Get updated limits due to binning */
951 rval = smiapp_get_limits_binning(sensor);
952 if (rval < 0)
953 return rval;
955 rval = smiapp_pll_update(sensor);
956 if (rval < 0)
957 return rval;
959 /* Output from pixel array, including blanking */
960 smiapp_update_blanking(sensor);
962 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
963 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
965 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
966 sensor->pll.pixel_rate_pixel_array /
967 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
968 + sensor->hblank->val) *
969 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
970 + sensor->vblank->val) / 100));
972 return 0;
977 * SMIA++ NVM handling
980 static int smiapp_read_nvm(struct smiapp_sensor *sensor,
981 unsigned char *nvm)
983 u32 i, s, p, np, v;
984 int rval = 0, rval2;
986 np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
987 for (p = 0; p < np; p++) {
988 rval = smiapp_write(
989 sensor,
990 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
991 if (rval)
992 goto out;
994 rval = smiapp_write(sensor,
995 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
996 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
997 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
998 if (rval)
999 goto out;
1001 for (i = 0; i < 1000; i++) {
1002 rval = smiapp_read(
1003 sensor,
1004 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);
1006 if (rval)
1007 goto out;
1009 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
1010 break;
1012 if (--i == 0) {
1013 rval = -ETIMEDOUT;
1014 goto out;
1019 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
1020 rval = smiapp_read(
1021 sensor,
1022 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
1023 &v);
1024 if (rval)
1025 goto out;
1027 *nvm++ = v;
1031 out:
1032 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
1033 if (rval < 0)
1034 return rval;
1035 else
1036 return rval2;
1041 * SMIA++ CCI address control
1044 static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
1046 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1047 int rval;
1048 u32 val;
1050 client->addr = sensor->hwcfg->i2c_addr_dfl;
1052 rval = smiapp_write(sensor,
1053 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
1054 sensor->hwcfg->i2c_addr_alt << 1);
1055 if (rval)
1056 return rval;
1058 client->addr = sensor->hwcfg->i2c_addr_alt;
1060 /* verify addr change went ok */
1061 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
1062 if (rval)
1063 return rval;
1065 if (val != sensor->hwcfg->i2c_addr_alt << 1)
1066 return -ENODEV;
1068 return 0;
1073 * SMIA++ Mode Control
1076 static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
1078 struct smiapp_flash_strobe_parms *strobe_setup;
1079 unsigned int ext_freq = sensor->hwcfg->ext_clk;
1080 u32 tmp;
1081 u32 strobe_adjustment;
1082 u32 strobe_width_high_rs;
1083 int rval;
1085 strobe_setup = sensor->hwcfg->strobe_setup;
1088 * How to calculate registers related to strobe length. Please
1089 * do not change, or if you do at least know what you're
1090 * doing. :-)
1092 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1094 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1095 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1097 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1098 * flash_strobe_adjustment E N, [1 - 0xff]
1100 * The formula above is written as below to keep it on one
1101 * line:
1103 * l / 10^6 = w / e * a
1105 * Let's mark w * a by x:
1107 * x = w * a
1109 * Thus, we get:
1111 * x = l * e / 10^6
1113 * The strobe width must be at least as long as requested,
1114 * thus rounding upwards is needed.
1116 * x = (l * e + 10^6 - 1) / 10^6
1117 * -----------------------------
1119 * Maximum possible accuracy is wanted at all times. Thus keep
1120 * a as small as possible.
1122 * Calculate a, assuming maximum w, with rounding upwards:
1124 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1125 * -------------------------------------
1127 * Thus, we also get w, with that a, with rounding upwards:
1129 * w = (x + a - 1) / a
1130 * -------------------
1132 * To get limits:
1134 * x E [1, (2^16 - 1) * (2^8 - 1)]
1136 * Substituting maximum x to the original formula (with rounding),
1137 * the maximum l is thus
1139 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1141 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1142 * --------------------------------------------------
1144 * flash_strobe_length must be clamped between 1 and
1145 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1147 * Then,
1149 * flash_strobe_adjustment = ((flash_strobe_length *
1150 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1152 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1153 * EXTCLK freq + 10^6 - 1) / 10^6 +
1154 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1156 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1157 1000000 + 1, ext_freq);
1158 strobe_setup->strobe_width_high_us =
1159 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1161 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1162 1000000 - 1), 1000000ULL);
1163 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1164 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1165 strobe_adjustment;
1167 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
1168 strobe_setup->mode);
1169 if (rval < 0)
1170 goto out;
1172 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1173 strobe_adjustment);
1174 if (rval < 0)
1175 goto out;
1177 rval = smiapp_write(
1178 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1179 strobe_width_high_rs);
1180 if (rval < 0)
1181 goto out;
1183 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1184 strobe_setup->strobe_delay);
1185 if (rval < 0)
1186 goto out;
1188 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1189 strobe_setup->stobe_start_point);
1190 if (rval < 0)
1191 goto out;
1193 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1194 strobe_setup->trigger);
1196 out:
1197 sensor->hwcfg->strobe_setup->trigger = 0;
1199 return rval;
1202 /* -----------------------------------------------------------------------------
1203 * Power management
1206 static int smiapp_power_on(struct device *dev)
1208 struct i2c_client *client = to_i2c_client(dev);
1209 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1210 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1212 * The sub-device related to the I2C device is always the
1213 * source one, i.e. ssds[0].
1215 struct smiapp_sensor *sensor =
1216 container_of(ssd, struct smiapp_sensor, ssds[0]);
1217 unsigned int sleep;
1218 int rval;
1220 rval = regulator_enable(sensor->vana);
1221 if (rval) {
1222 dev_err(&client->dev, "failed to enable vana regulator\n");
1223 return rval;
1225 usleep_range(1000, 1000);
1227 rval = clk_prepare_enable(sensor->ext_clk);
1228 if (rval < 0) {
1229 dev_dbg(&client->dev, "failed to enable xclk\n");
1230 goto out_xclk_fail;
1232 usleep_range(1000, 1000);
1234 gpiod_set_value(sensor->xshutdown, 1);
1236 sleep = SMIAPP_RESET_DELAY(sensor->hwcfg->ext_clk);
1237 usleep_range(sleep, sleep);
1240 * Failures to respond to the address change command have been noticed.
1241 * Those failures seem to be caused by the sensor requiring a longer
1242 * boot time than advertised. An additional 10ms delay seems to work
1243 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1244 * unnecessary. The failures need to be investigated to find a proper
1245 * fix, and a delay will likely need to be added here if the I2C write
1246 * retry hack is reverted before the root cause of the boot time issue
1247 * is found.
1250 if (sensor->hwcfg->i2c_addr_alt) {
1251 rval = smiapp_change_cci_addr(sensor);
1252 if (rval) {
1253 dev_err(&client->dev, "cci address change error\n");
1254 goto out_cci_addr_fail;
1258 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
1259 SMIAPP_SOFTWARE_RESET);
1260 if (rval < 0) {
1261 dev_err(&client->dev, "software reset failed\n");
1262 goto out_cci_addr_fail;
1265 if (sensor->hwcfg->i2c_addr_alt) {
1266 rval = smiapp_change_cci_addr(sensor);
1267 if (rval) {
1268 dev_err(&client->dev, "cci address change error\n");
1269 goto out_cci_addr_fail;
1273 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
1274 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1275 if (rval) {
1276 dev_err(&client->dev, "compression mode set failed\n");
1277 goto out_cci_addr_fail;
1280 rval = smiapp_write(
1281 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1282 sensor->hwcfg->ext_clk / (1000000 / (1 << 8)));
1283 if (rval) {
1284 dev_err(&client->dev, "extclk frequency set failed\n");
1285 goto out_cci_addr_fail;
1288 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1289 sensor->hwcfg->lanes - 1);
1290 if (rval) {
1291 dev_err(&client->dev, "csi lane mode set failed\n");
1292 goto out_cci_addr_fail;
1295 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1296 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1297 if (rval) {
1298 dev_err(&client->dev, "fast standby set failed\n");
1299 goto out_cci_addr_fail;
1302 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1303 sensor->hwcfg->csi_signalling_mode);
1304 if (rval) {
1305 dev_err(&client->dev, "csi signalling mode set failed\n");
1306 goto out_cci_addr_fail;
1309 /* DPHY control done by sensor based on requested link rate */
1310 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
1311 SMIAPP_DPHY_CTRL_UI);
1312 if (rval < 0)
1313 return rval;
1315 rval = smiapp_call_quirk(sensor, post_poweron);
1316 if (rval) {
1317 dev_err(&client->dev, "post_poweron quirks failed\n");
1318 goto out_cci_addr_fail;
1321 /* Are we still initialising...? If yes, return here. */
1322 if (!sensor->pixel_array)
1323 return 0;
1325 rval = v4l2_ctrl_handler_setup(&sensor->pixel_array->ctrl_handler);
1326 if (rval)
1327 goto out_cci_addr_fail;
1329 rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1330 if (rval)
1331 goto out_cci_addr_fail;
1333 mutex_lock(&sensor->mutex);
1334 rval = smiapp_update_mode(sensor);
1335 mutex_unlock(&sensor->mutex);
1336 if (rval < 0)
1337 goto out_cci_addr_fail;
1339 return 0;
1341 out_cci_addr_fail:
1343 gpiod_set_value(sensor->xshutdown, 0);
1344 clk_disable_unprepare(sensor->ext_clk);
1346 out_xclk_fail:
1347 regulator_disable(sensor->vana);
1349 return rval;
1352 static int smiapp_power_off(struct device *dev)
1354 struct i2c_client *client = to_i2c_client(dev);
1355 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1356 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1357 struct smiapp_sensor *sensor =
1358 container_of(ssd, struct smiapp_sensor, ssds[0]);
1361 * Currently power/clock to lens are enable/disabled separately
1362 * but they are essentially the same signals. So if the sensor is
1363 * powered off while the lens is powered on the sensor does not
1364 * really see a power off and next time the cci address change
1365 * will fail. So do a soft reset explicitly here.
1367 if (sensor->hwcfg->i2c_addr_alt)
1368 smiapp_write(sensor,
1369 SMIAPP_REG_U8_SOFTWARE_RESET,
1370 SMIAPP_SOFTWARE_RESET);
1372 gpiod_set_value(sensor->xshutdown, 0);
1373 clk_disable_unprepare(sensor->ext_clk);
1374 usleep_range(5000, 5000);
1375 regulator_disable(sensor->vana);
1376 sensor->streaming = false;
1378 return 0;
1381 static int smiapp_set_power(struct v4l2_subdev *subdev, int on)
1383 int rval;
1385 if (!on) {
1386 pm_runtime_mark_last_busy(subdev->dev);
1387 pm_runtime_put_autosuspend(subdev->dev);
1389 return 0;
1392 rval = pm_runtime_get_sync(subdev->dev);
1393 if (rval >= 0)
1394 return 0;
1396 if (rval != -EBUSY && rval != -EAGAIN)
1397 pm_runtime_set_active(subdev->dev);
1399 pm_runtime_put(subdev->dev);
1401 return rval;
1404 /* -----------------------------------------------------------------------------
1405 * Video stream management
1408 static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1410 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1411 int rval;
1413 mutex_lock(&sensor->mutex);
1415 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1416 (sensor->csi_format->width << 8) |
1417 sensor->csi_format->compressed);
1418 if (rval)
1419 goto out;
1421 rval = smiapp_pll_configure(sensor);
1422 if (rval)
1423 goto out;
1425 /* Analog crop start coordinates */
1426 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
1427 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1428 if (rval < 0)
1429 goto out;
1431 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
1432 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1433 if (rval < 0)
1434 goto out;
1436 /* Analog crop end coordinates */
1437 rval = smiapp_write(
1438 sensor, SMIAPP_REG_U16_X_ADDR_END,
1439 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1440 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1441 if (rval < 0)
1442 goto out;
1444 rval = smiapp_write(
1445 sensor, SMIAPP_REG_U16_Y_ADDR_END,
1446 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1447 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1448 if (rval < 0)
1449 goto out;
1452 * Output from pixel array, including blanking, is set using
1453 * controls below. No need to set here.
1456 /* Digital crop */
1457 if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1458 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1459 rval = smiapp_write(
1460 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1461 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1462 if (rval < 0)
1463 goto out;
1465 rval = smiapp_write(
1466 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1467 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1468 if (rval < 0)
1469 goto out;
1471 rval = smiapp_write(
1472 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1473 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1474 if (rval < 0)
1475 goto out;
1477 rval = smiapp_write(
1478 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1479 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1480 if (rval < 0)
1481 goto out;
1484 /* Scaling */
1485 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1486 != SMIAPP_SCALING_CAPABILITY_NONE) {
1487 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
1488 sensor->scaling_mode);
1489 if (rval < 0)
1490 goto out;
1492 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
1493 sensor->scale_m);
1494 if (rval < 0)
1495 goto out;
1498 /* Output size from sensor */
1499 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1500 sensor->src->crop[SMIAPP_PAD_SRC].width);
1501 if (rval < 0)
1502 goto out;
1503 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1504 sensor->src->crop[SMIAPP_PAD_SRC].height);
1505 if (rval < 0)
1506 goto out;
1508 if ((sensor->limits[SMIAPP_LIMIT_FLASH_MODE_CAPABILITY] &
1509 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1510 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1511 sensor->hwcfg->strobe_setup != NULL &&
1512 sensor->hwcfg->strobe_setup->trigger != 0) {
1513 rval = smiapp_setup_flash_strobe(sensor);
1514 if (rval)
1515 goto out;
1518 rval = smiapp_call_quirk(sensor, pre_streamon);
1519 if (rval) {
1520 dev_err(&client->dev, "pre_streamon quirks failed\n");
1521 goto out;
1524 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1525 SMIAPP_MODE_SELECT_STREAMING);
1527 out:
1528 mutex_unlock(&sensor->mutex);
1530 return rval;
1533 static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1535 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1536 int rval;
1538 mutex_lock(&sensor->mutex);
1539 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1540 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1541 if (rval)
1542 goto out;
1544 rval = smiapp_call_quirk(sensor, post_streamoff);
1545 if (rval)
1546 dev_err(&client->dev, "post_streamoff quirks failed\n");
1548 out:
1549 mutex_unlock(&sensor->mutex);
1550 return rval;
1553 /* -----------------------------------------------------------------------------
1554 * V4L2 subdev video operations
1557 static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1559 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1560 int rval;
1562 if (sensor->streaming == enable)
1563 return 0;
1565 if (enable) {
1566 sensor->streaming = true;
1567 rval = smiapp_start_streaming(sensor);
1568 if (rval < 0)
1569 sensor->streaming = false;
1570 } else {
1571 rval = smiapp_stop_streaming(sensor);
1572 sensor->streaming = false;
1575 return rval;
1578 static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1579 struct v4l2_subdev_pad_config *cfg,
1580 struct v4l2_subdev_mbus_code_enum *code)
1582 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1583 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1584 unsigned int i;
1585 int idx = -1;
1586 int rval = -EINVAL;
1588 mutex_lock(&sensor->mutex);
1590 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1591 subdev->name, code->pad, code->index);
1593 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1594 if (code->index)
1595 goto out;
1597 code->code = sensor->internal_csi_format->code;
1598 rval = 0;
1599 goto out;
1602 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1603 if (sensor->mbus_frame_fmts & (1 << i))
1604 idx++;
1606 if (idx == code->index) {
1607 code->code = smiapp_csi_data_formats[i].code;
1608 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1609 code->index, i, code->code);
1610 rval = 0;
1611 break;
1615 out:
1616 mutex_unlock(&sensor->mutex);
1618 return rval;
1621 static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1622 unsigned int pad)
1624 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1626 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1627 return sensor->csi_format->code;
1628 else
1629 return sensor->internal_csi_format->code;
1632 static int __smiapp_get_format(struct v4l2_subdev *subdev,
1633 struct v4l2_subdev_pad_config *cfg,
1634 struct v4l2_subdev_format *fmt)
1636 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1638 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1639 fmt->format = *v4l2_subdev_get_try_format(subdev, cfg,
1640 fmt->pad);
1641 } else {
1642 struct v4l2_rect *r;
1644 if (fmt->pad == ssd->source_pad)
1645 r = &ssd->crop[ssd->source_pad];
1646 else
1647 r = &ssd->sink_fmt;
1649 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1650 fmt->format.width = r->width;
1651 fmt->format.height = r->height;
1652 fmt->format.field = V4L2_FIELD_NONE;
1655 return 0;
1658 static int smiapp_get_format(struct v4l2_subdev *subdev,
1659 struct v4l2_subdev_pad_config *cfg,
1660 struct v4l2_subdev_format *fmt)
1662 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1663 int rval;
1665 mutex_lock(&sensor->mutex);
1666 rval = __smiapp_get_format(subdev, cfg, fmt);
1667 mutex_unlock(&sensor->mutex);
1669 return rval;
1672 static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1673 struct v4l2_subdev_pad_config *cfg,
1674 struct v4l2_rect **crops,
1675 struct v4l2_rect **comps, int which)
1677 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1678 unsigned int i;
1680 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1681 if (crops)
1682 for (i = 0; i < subdev->entity.num_pads; i++)
1683 crops[i] = &ssd->crop[i];
1684 if (comps)
1685 *comps = &ssd->compose;
1686 } else {
1687 if (crops) {
1688 for (i = 0; i < subdev->entity.num_pads; i++) {
1689 crops[i] = v4l2_subdev_get_try_crop(subdev, cfg, i);
1690 BUG_ON(!crops[i]);
1693 if (comps) {
1694 *comps = v4l2_subdev_get_try_compose(subdev, cfg,
1695 SMIAPP_PAD_SINK);
1696 BUG_ON(!*comps);
1701 /* Changes require propagation only on sink pad. */
1702 static void smiapp_propagate(struct v4l2_subdev *subdev,
1703 struct v4l2_subdev_pad_config *cfg, int which,
1704 int target)
1706 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1707 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1708 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1710 smiapp_get_crop_compose(subdev, cfg, crops, &comp, which);
1712 switch (target) {
1713 case V4L2_SEL_TGT_CROP:
1714 comp->width = crops[SMIAPP_PAD_SINK]->width;
1715 comp->height = crops[SMIAPP_PAD_SINK]->height;
1716 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1717 if (ssd == sensor->scaler) {
1718 sensor->scale_m =
1719 sensor->limits[
1720 SMIAPP_LIMIT_SCALER_N_MIN];
1721 sensor->scaling_mode =
1722 SMIAPP_SCALING_MODE_NONE;
1723 } else if (ssd == sensor->binner) {
1724 sensor->binning_horizontal = 1;
1725 sensor->binning_vertical = 1;
1728 /* Fall through */
1729 case V4L2_SEL_TGT_COMPOSE:
1730 *crops[SMIAPP_PAD_SRC] = *comp;
1731 break;
1732 default:
1733 BUG();
1737 static const struct smiapp_csi_data_format
1738 *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1740 unsigned int i;
1742 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1743 if (sensor->mbus_frame_fmts & (1 << i)
1744 && smiapp_csi_data_formats[i].code == code)
1745 return &smiapp_csi_data_formats[i];
1748 return sensor->csi_format;
1751 static int smiapp_set_format_source(struct v4l2_subdev *subdev,
1752 struct v4l2_subdev_pad_config *cfg,
1753 struct v4l2_subdev_format *fmt)
1755 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1756 const struct smiapp_csi_data_format *csi_format,
1757 *old_csi_format = sensor->csi_format;
1758 unsigned long *valid_link_freqs;
1759 u32 code = fmt->format.code;
1760 unsigned int i;
1761 int rval;
1763 rval = __smiapp_get_format(subdev, cfg, fmt);
1764 if (rval)
1765 return rval;
1768 * Media bus code is changeable on src subdev's source pad. On
1769 * other source pads we just get format here.
1771 if (subdev != &sensor->src->sd)
1772 return 0;
1774 csi_format = smiapp_validate_csi_data_format(sensor, code);
1776 fmt->format.code = csi_format->code;
1778 if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1779 return 0;
1781 sensor->csi_format = csi_format;
1783 if (csi_format->width != old_csi_format->width)
1784 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
1785 __v4l2_ctrl_modify_range(
1786 sensor->test_data[i], 0,
1787 (1 << csi_format->width) - 1, 1, 0);
1789 if (csi_format->compressed == old_csi_format->compressed)
1790 return 0;
1792 valid_link_freqs =
1793 &sensor->valid_link_freqs[sensor->csi_format->compressed
1794 - sensor->compressed_min_bpp];
1796 __v4l2_ctrl_modify_range(
1797 sensor->link_freq, 0,
1798 __fls(*valid_link_freqs), ~*valid_link_freqs,
1799 __ffs(*valid_link_freqs));
1801 return smiapp_pll_update(sensor);
1804 static int smiapp_set_format(struct v4l2_subdev *subdev,
1805 struct v4l2_subdev_pad_config *cfg,
1806 struct v4l2_subdev_format *fmt)
1808 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1809 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1810 struct v4l2_rect *crops[SMIAPP_PADS];
1812 mutex_lock(&sensor->mutex);
1814 if (fmt->pad == ssd->source_pad) {
1815 int rval;
1817 rval = smiapp_set_format_source(subdev, cfg, fmt);
1819 mutex_unlock(&sensor->mutex);
1821 return rval;
1824 /* Sink pad. Width and height are changeable here. */
1825 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1826 fmt->format.width &= ~1;
1827 fmt->format.height &= ~1;
1828 fmt->format.field = V4L2_FIELD_NONE;
1830 fmt->format.width =
1831 clamp(fmt->format.width,
1832 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
1833 sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
1834 fmt->format.height =
1835 clamp(fmt->format.height,
1836 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
1837 sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
1839 smiapp_get_crop_compose(subdev, cfg, crops, NULL, fmt->which);
1841 crops[ssd->sink_pad]->left = 0;
1842 crops[ssd->sink_pad]->top = 0;
1843 crops[ssd->sink_pad]->width = fmt->format.width;
1844 crops[ssd->sink_pad]->height = fmt->format.height;
1845 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1846 ssd->sink_fmt = *crops[ssd->sink_pad];
1847 smiapp_propagate(subdev, cfg, fmt->which,
1848 V4L2_SEL_TGT_CROP);
1850 mutex_unlock(&sensor->mutex);
1852 return 0;
1856 * Calculate goodness of scaled image size compared to expected image
1857 * size and flags provided.
1859 #define SCALING_GOODNESS 100000
1860 #define SCALING_GOODNESS_EXTREME 100000000
1861 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1862 int h, int ask_h, u32 flags)
1864 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1865 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1866 int val = 0;
1868 w &= ~1;
1869 ask_w &= ~1;
1870 h &= ~1;
1871 ask_h &= ~1;
1873 if (flags & V4L2_SEL_FLAG_GE) {
1874 if (w < ask_w)
1875 val -= SCALING_GOODNESS;
1876 if (h < ask_h)
1877 val -= SCALING_GOODNESS;
1880 if (flags & V4L2_SEL_FLAG_LE) {
1881 if (w > ask_w)
1882 val -= SCALING_GOODNESS;
1883 if (h > ask_h)
1884 val -= SCALING_GOODNESS;
1887 val -= abs(w - ask_w);
1888 val -= abs(h - ask_h);
1890 if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
1891 val -= SCALING_GOODNESS_EXTREME;
1893 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1894 w, ask_h, h, ask_h, val);
1896 return val;
1899 static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1900 struct v4l2_subdev_pad_config *cfg,
1901 struct v4l2_subdev_selection *sel,
1902 struct v4l2_rect **crops,
1903 struct v4l2_rect *comp)
1905 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1906 unsigned int i;
1907 unsigned int binh = 1, binv = 1;
1908 int best = scaling_goodness(
1909 subdev,
1910 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1911 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1913 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1914 int this = scaling_goodness(
1915 subdev,
1916 crops[SMIAPP_PAD_SINK]->width
1917 / sensor->binning_subtypes[i].horizontal,
1918 sel->r.width,
1919 crops[SMIAPP_PAD_SINK]->height
1920 / sensor->binning_subtypes[i].vertical,
1921 sel->r.height, sel->flags);
1923 if (this > best) {
1924 binh = sensor->binning_subtypes[i].horizontal;
1925 binv = sensor->binning_subtypes[i].vertical;
1926 best = this;
1929 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1930 sensor->binning_vertical = binv;
1931 sensor->binning_horizontal = binh;
1934 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1935 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1939 * Calculate best scaling ratio and mode for given output resolution.
1941 * Try all of these: horizontal ratio, vertical ratio and smallest
1942 * size possible (horizontally).
1944 * Also try whether horizontal scaler or full scaler gives a better
1945 * result.
1947 static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1948 struct v4l2_subdev_pad_config *cfg,
1949 struct v4l2_subdev_selection *sel,
1950 struct v4l2_rect **crops,
1951 struct v4l2_rect *comp)
1953 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1954 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1955 u32 min, max, a, b, max_m;
1956 u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
1957 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1958 u32 try[4];
1959 u32 ntry = 0;
1960 unsigned int i;
1961 int best = INT_MIN;
1963 sel->r.width = min_t(unsigned int, sel->r.width,
1964 crops[SMIAPP_PAD_SINK]->width);
1965 sel->r.height = min_t(unsigned int, sel->r.height,
1966 crops[SMIAPP_PAD_SINK]->height);
1968 a = crops[SMIAPP_PAD_SINK]->width
1969 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
1970 b = crops[SMIAPP_PAD_SINK]->height
1971 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
1972 max_m = crops[SMIAPP_PAD_SINK]->width
1973 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
1974 / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
1976 a = clamp(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1977 sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1978 b = clamp(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1979 sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1980 max_m = clamp(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1981 sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1983 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1985 min = min(max_m, min(a, b));
1986 max = min(max_m, max(a, b));
1988 try[ntry] = min;
1989 ntry++;
1990 if (min != max) {
1991 try[ntry] = max;
1992 ntry++;
1994 if (max != max_m) {
1995 try[ntry] = min + 1;
1996 ntry++;
1997 if (min != max) {
1998 try[ntry] = max + 1;
1999 ntry++;
2003 for (i = 0; i < ntry; i++) {
2004 int this = scaling_goodness(
2005 subdev,
2006 crops[SMIAPP_PAD_SINK]->width
2007 / try[i]
2008 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2009 sel->r.width,
2010 crops[SMIAPP_PAD_SINK]->height,
2011 sel->r.height,
2012 sel->flags);
2014 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
2016 if (this > best) {
2017 scale_m = try[i];
2018 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
2019 best = this;
2022 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2023 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2024 continue;
2026 this = scaling_goodness(
2027 subdev, crops[SMIAPP_PAD_SINK]->width
2028 / try[i]
2029 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2030 sel->r.width,
2031 crops[SMIAPP_PAD_SINK]->height
2032 / try[i]
2033 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2034 sel->r.height,
2035 sel->flags);
2037 if (this > best) {
2038 scale_m = try[i];
2039 mode = SMIAPP_SCALING_MODE_BOTH;
2040 best = this;
2044 sel->r.width =
2045 (crops[SMIAPP_PAD_SINK]->width
2046 / scale_m
2047 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
2048 if (mode == SMIAPP_SCALING_MODE_BOTH)
2049 sel->r.height =
2050 (crops[SMIAPP_PAD_SINK]->height
2051 / scale_m
2052 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
2053 & ~1;
2054 else
2055 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
2057 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2058 sensor->scale_m = scale_m;
2059 sensor->scaling_mode = mode;
2062 /* We're only called on source pads. This function sets scaling. */
2063 static int smiapp_set_compose(struct v4l2_subdev *subdev,
2064 struct v4l2_subdev_pad_config *cfg,
2065 struct v4l2_subdev_selection *sel)
2067 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2068 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2069 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2071 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2073 sel->r.top = 0;
2074 sel->r.left = 0;
2076 if (ssd == sensor->binner)
2077 smiapp_set_compose_binner(subdev, cfg, sel, crops, comp);
2078 else
2079 smiapp_set_compose_scaler(subdev, cfg, sel, crops, comp);
2081 *comp = sel->r;
2082 smiapp_propagate(subdev, cfg, sel->which, V4L2_SEL_TGT_COMPOSE);
2084 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2085 return smiapp_update_mode(sensor);
2087 return 0;
2090 static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
2091 struct v4l2_subdev_selection *sel)
2093 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2094 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2096 /* We only implement crop in three places. */
2097 switch (sel->target) {
2098 case V4L2_SEL_TGT_CROP:
2099 case V4L2_SEL_TGT_CROP_BOUNDS:
2100 if (ssd == sensor->pixel_array
2101 && sel->pad == SMIAPP_PA_PAD_SRC)
2102 return 0;
2103 if (ssd == sensor->src
2104 && sel->pad == SMIAPP_PAD_SRC)
2105 return 0;
2106 if (ssd == sensor->scaler
2107 && sel->pad == SMIAPP_PAD_SINK
2108 && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2109 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
2110 return 0;
2111 return -EINVAL;
2112 case V4L2_SEL_TGT_NATIVE_SIZE:
2113 if (ssd == sensor->pixel_array
2114 && sel->pad == SMIAPP_PA_PAD_SRC)
2115 return 0;
2116 return -EINVAL;
2117 case V4L2_SEL_TGT_COMPOSE:
2118 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2119 if (sel->pad == ssd->source_pad)
2120 return -EINVAL;
2121 if (ssd == sensor->binner)
2122 return 0;
2123 if (ssd == sensor->scaler
2124 && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2125 != SMIAPP_SCALING_CAPABILITY_NONE)
2126 return 0;
2127 /* Fall through */
2128 default:
2129 return -EINVAL;
2133 static int smiapp_set_crop(struct v4l2_subdev *subdev,
2134 struct v4l2_subdev_pad_config *cfg,
2135 struct v4l2_subdev_selection *sel)
2137 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2138 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2139 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
2140 struct v4l2_rect _r;
2142 smiapp_get_crop_compose(subdev, cfg, crops, NULL, sel->which);
2144 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2145 if (sel->pad == ssd->sink_pad)
2146 src_size = &ssd->sink_fmt;
2147 else
2148 src_size = &ssd->compose;
2149 } else {
2150 if (sel->pad == ssd->sink_pad) {
2151 _r.left = 0;
2152 _r.top = 0;
2153 _r.width = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2154 ->width;
2155 _r.height = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2156 ->height;
2157 src_size = &_r;
2158 } else {
2159 src_size = v4l2_subdev_get_try_compose(
2160 subdev, cfg, ssd->sink_pad);
2164 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2165 sel->r.left = 0;
2166 sel->r.top = 0;
2169 sel->r.width = min(sel->r.width, src_size->width);
2170 sel->r.height = min(sel->r.height, src_size->height);
2172 sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
2173 sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
2175 *crops[sel->pad] = sel->r;
2177 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2178 smiapp_propagate(subdev, cfg, sel->which,
2179 V4L2_SEL_TGT_CROP);
2181 return 0;
2184 static void smiapp_get_native_size(struct smiapp_subdev *ssd,
2185 struct v4l2_rect *r)
2187 r->top = 0;
2188 r->left = 0;
2189 r->width = ssd->sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2190 r->height = ssd->sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2193 static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2194 struct v4l2_subdev_pad_config *cfg,
2195 struct v4l2_subdev_selection *sel)
2197 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2198 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2199 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2200 struct v4l2_rect sink_fmt;
2201 int ret;
2203 ret = __smiapp_sel_supported(subdev, sel);
2204 if (ret)
2205 return ret;
2207 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2209 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2210 sink_fmt = ssd->sink_fmt;
2211 } else {
2212 struct v4l2_mbus_framefmt *fmt =
2213 v4l2_subdev_get_try_format(subdev, cfg, ssd->sink_pad);
2215 sink_fmt.left = 0;
2216 sink_fmt.top = 0;
2217 sink_fmt.width = fmt->width;
2218 sink_fmt.height = fmt->height;
2221 switch (sel->target) {
2222 case V4L2_SEL_TGT_CROP_BOUNDS:
2223 case V4L2_SEL_TGT_NATIVE_SIZE:
2224 if (ssd == sensor->pixel_array)
2225 smiapp_get_native_size(ssd, &sel->r);
2226 else if (sel->pad == ssd->sink_pad)
2227 sel->r = sink_fmt;
2228 else
2229 sel->r = *comp;
2230 break;
2231 case V4L2_SEL_TGT_CROP:
2232 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2233 sel->r = *crops[sel->pad];
2234 break;
2235 case V4L2_SEL_TGT_COMPOSE:
2236 sel->r = *comp;
2237 break;
2240 return 0;
2243 static int smiapp_get_selection(struct v4l2_subdev *subdev,
2244 struct v4l2_subdev_pad_config *cfg,
2245 struct v4l2_subdev_selection *sel)
2247 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2248 int rval;
2250 mutex_lock(&sensor->mutex);
2251 rval = __smiapp_get_selection(subdev, cfg, sel);
2252 mutex_unlock(&sensor->mutex);
2254 return rval;
2256 static int smiapp_set_selection(struct v4l2_subdev *subdev,
2257 struct v4l2_subdev_pad_config *cfg,
2258 struct v4l2_subdev_selection *sel)
2260 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2261 int ret;
2263 ret = __smiapp_sel_supported(subdev, sel);
2264 if (ret)
2265 return ret;
2267 mutex_lock(&sensor->mutex);
2269 sel->r.left = max(0, sel->r.left & ~1);
2270 sel->r.top = max(0, sel->r.top & ~1);
2271 sel->r.width = SMIAPP_ALIGN_DIM(sel->r.width, sel->flags);
2272 sel->r.height = SMIAPP_ALIGN_DIM(sel->r.height, sel->flags);
2274 sel->r.width = max_t(unsigned int,
2275 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
2276 sel->r.width);
2277 sel->r.height = max_t(unsigned int,
2278 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
2279 sel->r.height);
2281 switch (sel->target) {
2282 case V4L2_SEL_TGT_CROP:
2283 ret = smiapp_set_crop(subdev, cfg, sel);
2284 break;
2285 case V4L2_SEL_TGT_COMPOSE:
2286 ret = smiapp_set_compose(subdev, cfg, sel);
2287 break;
2288 default:
2289 ret = -EINVAL;
2292 mutex_unlock(&sensor->mutex);
2293 return ret;
2296 static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2298 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2300 *frames = sensor->frame_skip;
2301 return 0;
2304 static int smiapp_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines)
2306 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2308 *lines = sensor->image_start;
2310 return 0;
2313 /* -----------------------------------------------------------------------------
2314 * sysfs attributes
2317 static ssize_t
2318 smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2319 char *buf)
2321 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2322 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2323 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2324 unsigned int nbytes;
2326 if (!sensor->dev_init_done)
2327 return -EBUSY;
2329 if (!sensor->nvm_size) {
2330 int rval;
2332 /* NVM not read yet - read it now */
2333 sensor->nvm_size = sensor->hwcfg->nvm_size;
2335 rval = pm_runtime_get_sync(&client->dev);
2336 if (rval < 0) {
2337 if (rval != -EBUSY && rval != -EAGAIN)
2338 pm_runtime_set_active(&client->dev);
2339 pm_runtime_put(&client->dev);
2340 return -ENODEV;
2343 if (smiapp_read_nvm(sensor, sensor->nvm)) {
2344 dev_err(&client->dev, "nvm read failed\n");
2345 return -ENODEV;
2348 pm_runtime_mark_last_busy(&client->dev);
2349 pm_runtime_put_autosuspend(&client->dev);
2352 * NVM is still way below a PAGE_SIZE, so we can safely
2353 * assume this for now.
2355 nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
2356 memcpy(buf, sensor->nvm, nbytes);
2358 return nbytes;
2360 static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2362 static ssize_t
2363 smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2364 char *buf)
2366 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2367 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2368 struct smiapp_module_info *minfo = &sensor->minfo;
2370 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2371 minfo->manufacturer_id, minfo->model_id,
2372 minfo->revision_number_major) + 1;
2375 static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
2377 /* -----------------------------------------------------------------------------
2378 * V4L2 subdev core operations
2381 static int smiapp_identify_module(struct smiapp_sensor *sensor)
2383 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2384 struct smiapp_module_info *minfo = &sensor->minfo;
2385 unsigned int i;
2386 int rval = 0;
2388 minfo->name = SMIAPP_NAME;
2390 /* Module info */
2391 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2392 &minfo->manufacturer_id);
2393 if (!rval)
2394 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2395 &minfo->model_id);
2396 if (!rval)
2397 rval = smiapp_read_8only(sensor,
2398 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2399 &minfo->revision_number_major);
2400 if (!rval)
2401 rval = smiapp_read_8only(sensor,
2402 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2403 &minfo->revision_number_minor);
2404 if (!rval)
2405 rval = smiapp_read_8only(sensor,
2406 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2407 &minfo->module_year);
2408 if (!rval)
2409 rval = smiapp_read_8only(sensor,
2410 SMIAPP_REG_U8_MODULE_DATE_MONTH,
2411 &minfo->module_month);
2412 if (!rval)
2413 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2414 &minfo->module_day);
2416 /* Sensor info */
2417 if (!rval)
2418 rval = smiapp_read_8only(sensor,
2419 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2420 &minfo->sensor_manufacturer_id);
2421 if (!rval)
2422 rval = smiapp_read_8only(sensor,
2423 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2424 &minfo->sensor_model_id);
2425 if (!rval)
2426 rval = smiapp_read_8only(sensor,
2427 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2428 &minfo->sensor_revision_number);
2429 if (!rval)
2430 rval = smiapp_read_8only(sensor,
2431 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2432 &minfo->sensor_firmware_version);
2434 /* SMIA */
2435 if (!rval)
2436 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2437 &minfo->smia_version);
2438 if (!rval)
2439 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2440 &minfo->smiapp_version);
2442 if (rval) {
2443 dev_err(&client->dev, "sensor detection failed\n");
2444 return -ENODEV;
2447 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2448 minfo->manufacturer_id, minfo->model_id);
2450 dev_dbg(&client->dev,
2451 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2452 minfo->revision_number_major, minfo->revision_number_minor,
2453 minfo->module_year, minfo->module_month, minfo->module_day);
2455 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2456 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2458 dev_dbg(&client->dev,
2459 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2460 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2462 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2463 minfo->smia_version, minfo->smiapp_version);
2466 * Some modules have bad data in the lvalues below. Hope the
2467 * rvalues have better stuff. The lvalues are module
2468 * parameters whereas the rvalues are sensor parameters.
2470 if (!minfo->manufacturer_id && !minfo->model_id) {
2471 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2472 minfo->model_id = minfo->sensor_model_id;
2473 minfo->revision_number_major = minfo->sensor_revision_number;
2476 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2477 if (smiapp_module_idents[i].manufacturer_id
2478 != minfo->manufacturer_id)
2479 continue;
2480 if (smiapp_module_idents[i].model_id != minfo->model_id)
2481 continue;
2482 if (smiapp_module_idents[i].flags
2483 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2484 if (smiapp_module_idents[i].revision_number_major
2485 < minfo->revision_number_major)
2486 continue;
2487 } else {
2488 if (smiapp_module_idents[i].revision_number_major
2489 != minfo->revision_number_major)
2490 continue;
2493 minfo->name = smiapp_module_idents[i].name;
2494 minfo->quirk = smiapp_module_idents[i].quirk;
2495 break;
2498 if (i >= ARRAY_SIZE(smiapp_module_idents))
2499 dev_warn(&client->dev,
2500 "no quirks for this module; let's hope it's fully compliant\n");
2502 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2503 minfo->name, minfo->manufacturer_id, minfo->model_id,
2504 minfo->revision_number_major);
2506 return 0;
2509 static const struct v4l2_subdev_ops smiapp_ops;
2510 static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2511 static const struct media_entity_operations smiapp_entity_ops;
2513 static int smiapp_register_subdev(struct smiapp_sensor *sensor,
2514 struct smiapp_subdev *ssd,
2515 struct smiapp_subdev *sink_ssd,
2516 u16 source_pad, u16 sink_pad, u32 link_flags)
2518 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2519 int rval;
2521 if (!sink_ssd)
2522 return 0;
2524 rval = media_entity_pads_init(&ssd->sd.entity,
2525 ssd->npads, ssd->pads);
2526 if (rval) {
2527 dev_err(&client->dev,
2528 "media_entity_pads_init failed\n");
2529 return rval;
2532 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2533 &ssd->sd);
2534 if (rval) {
2535 dev_err(&client->dev,
2536 "v4l2_device_register_subdev failed\n");
2537 return rval;
2540 rval = media_create_pad_link(&ssd->sd.entity, source_pad,
2541 &sink_ssd->sd.entity, sink_pad,
2542 link_flags);
2543 if (rval) {
2544 dev_err(&client->dev,
2545 "media_create_pad_link failed\n");
2546 v4l2_device_unregister_subdev(&ssd->sd);
2547 return rval;
2550 return 0;
2553 static void smiapp_unregistered(struct v4l2_subdev *subdev)
2555 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2556 unsigned int i;
2558 for (i = 1; i < sensor->ssds_used; i++)
2559 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2562 static int smiapp_registered(struct v4l2_subdev *subdev)
2564 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2565 int rval;
2567 if (sensor->scaler) {
2568 rval = smiapp_register_subdev(
2569 sensor, sensor->binner, sensor->scaler,
2570 SMIAPP_PAD_SRC, SMIAPP_PAD_SINK,
2571 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2572 if (rval < 0)
2573 return rval;
2576 rval = smiapp_register_subdev(
2577 sensor, sensor->pixel_array, sensor->binner,
2578 SMIAPP_PA_PAD_SRC, SMIAPP_PAD_SINK,
2579 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2580 if (rval)
2581 goto out_err;
2583 return 0;
2585 out_err:
2586 smiapp_unregistered(subdev);
2588 return rval;
2591 static void smiapp_cleanup(struct smiapp_sensor *sensor)
2593 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2595 device_remove_file(&client->dev, &dev_attr_nvm);
2596 device_remove_file(&client->dev, &dev_attr_ident);
2598 smiapp_free_controls(sensor);
2601 static void smiapp_create_subdev(struct smiapp_sensor *sensor,
2602 struct smiapp_subdev *ssd, const char *name,
2603 unsigned short num_pads)
2605 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2607 if (!ssd)
2608 return;
2610 if (ssd != sensor->src)
2611 v4l2_subdev_init(&ssd->sd, &smiapp_ops);
2613 ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2614 ssd->sensor = sensor;
2616 ssd->npads = num_pads;
2617 ssd->source_pad = num_pads - 1;
2619 snprintf(ssd->sd.name,
2620 sizeof(ssd->sd.name), "%s %s %d-%4.4x", sensor->minfo.name,
2621 name, i2c_adapter_id(client->adapter), client->addr);
2623 smiapp_get_native_size(ssd, &ssd->sink_fmt);
2625 ssd->compose.width = ssd->sink_fmt.width;
2626 ssd->compose.height = ssd->sink_fmt.height;
2627 ssd->crop[ssd->source_pad] = ssd->compose;
2628 ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2629 if (ssd != sensor->pixel_array) {
2630 ssd->crop[ssd->sink_pad] = ssd->compose;
2631 ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK;
2634 ssd->sd.entity.ops = &smiapp_entity_ops;
2636 if (ssd == sensor->src)
2637 return;
2639 ssd->sd.internal_ops = &smiapp_internal_ops;
2640 ssd->sd.owner = THIS_MODULE;
2641 ssd->sd.dev = &client->dev;
2642 v4l2_set_subdevdata(&ssd->sd, client);
2645 static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2647 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2648 struct smiapp_sensor *sensor = ssd->sensor;
2649 unsigned int i;
2650 int rval;
2652 mutex_lock(&sensor->mutex);
2654 for (i = 0; i < ssd->npads; i++) {
2655 struct v4l2_mbus_framefmt *try_fmt =
2656 v4l2_subdev_get_try_format(sd, fh->pad, i);
2657 struct v4l2_rect *try_crop =
2658 v4l2_subdev_get_try_crop(sd, fh->pad, i);
2659 struct v4l2_rect *try_comp;
2661 smiapp_get_native_size(ssd, try_crop);
2663 try_fmt->width = try_crop->width;
2664 try_fmt->height = try_crop->height;
2665 try_fmt->code = sensor->internal_csi_format->code;
2666 try_fmt->field = V4L2_FIELD_NONE;
2668 if (ssd != sensor->pixel_array)
2669 continue;
2671 try_comp = v4l2_subdev_get_try_compose(sd, fh->pad, i);
2672 *try_comp = *try_crop;
2675 mutex_unlock(&sensor->mutex);
2677 rval = pm_runtime_get_sync(sd->dev);
2678 if (rval >= 0)
2679 return 0;
2681 if (rval != -EBUSY && rval != -EAGAIN)
2682 pm_runtime_set_active(sd->dev);
2683 pm_runtime_put(sd->dev);
2685 return rval;
2688 static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2690 pm_runtime_mark_last_busy(sd->dev);
2691 pm_runtime_put_autosuspend(sd->dev);
2693 return 0;
2696 static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2697 .s_stream = smiapp_set_stream,
2700 static const struct v4l2_subdev_core_ops smiapp_core_ops = {
2701 .s_power = smiapp_set_power,
2704 static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2705 .enum_mbus_code = smiapp_enum_mbus_code,
2706 .get_fmt = smiapp_get_format,
2707 .set_fmt = smiapp_set_format,
2708 .get_selection = smiapp_get_selection,
2709 .set_selection = smiapp_set_selection,
2712 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2713 .g_skip_frames = smiapp_get_skip_frames,
2714 .g_skip_top_lines = smiapp_get_skip_top_lines,
2717 static const struct v4l2_subdev_ops smiapp_ops = {
2718 .core = &smiapp_core_ops,
2719 .video = &smiapp_video_ops,
2720 .pad = &smiapp_pad_ops,
2721 .sensor = &smiapp_sensor_ops,
2724 static const struct media_entity_operations smiapp_entity_ops = {
2725 .link_validate = v4l2_subdev_link_validate,
2728 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2729 .registered = smiapp_registered,
2730 .unregistered = smiapp_unregistered,
2731 .open = smiapp_open,
2732 .close = smiapp_close,
2735 static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2736 .open = smiapp_open,
2737 .close = smiapp_close,
2740 /* -----------------------------------------------------------------------------
2741 * I2C Driver
2744 #ifdef CONFIG_PM
2746 static int smiapp_suspend(struct device *dev)
2748 struct i2c_client *client = to_i2c_client(dev);
2749 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2750 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2751 bool streaming = sensor->streaming;
2752 int rval;
2754 rval = pm_runtime_get_sync(dev);
2755 if (rval < 0) {
2756 if (rval != -EBUSY && rval != -EAGAIN)
2757 pm_runtime_set_active(&client->dev);
2758 pm_runtime_put(dev);
2759 return -EAGAIN;
2762 if (sensor->streaming)
2763 smiapp_stop_streaming(sensor);
2765 /* save state for resume */
2766 sensor->streaming = streaming;
2768 return 0;
2771 static int smiapp_resume(struct device *dev)
2773 struct i2c_client *client = to_i2c_client(dev);
2774 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2775 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2776 int rval = 0;
2778 pm_runtime_put(dev);
2780 if (sensor->streaming)
2781 rval = smiapp_start_streaming(sensor);
2783 return rval;
2786 #else
2788 #define smiapp_suspend NULL
2789 #define smiapp_resume NULL
2791 #endif /* CONFIG_PM */
2793 static struct smiapp_hwconfig *smiapp_get_hwconfig(struct device *dev)
2795 struct smiapp_hwconfig *hwcfg;
2796 struct v4l2_of_endpoint *bus_cfg;
2797 struct device_node *ep;
2798 int i;
2799 int rval;
2801 if (!dev->of_node)
2802 return dev->platform_data;
2804 ep = of_graph_get_next_endpoint(dev->of_node, NULL);
2805 if (!ep)
2806 return NULL;
2808 bus_cfg = v4l2_of_alloc_parse_endpoint(ep);
2809 if (IS_ERR(bus_cfg))
2810 goto out_err;
2812 hwcfg = devm_kzalloc(dev, sizeof(*hwcfg), GFP_KERNEL);
2813 if (!hwcfg)
2814 goto out_err;
2816 switch (bus_cfg->bus_type) {
2817 case V4L2_MBUS_CSI2:
2818 hwcfg->csi_signalling_mode = SMIAPP_CSI_SIGNALLING_MODE_CSI2;
2819 break;
2820 /* FIXME: add CCP2 support. */
2821 default:
2822 goto out_err;
2825 hwcfg->lanes = bus_cfg->bus.mipi_csi2.num_data_lanes;
2826 dev_dbg(dev, "lanes %u\n", hwcfg->lanes);
2828 /* NVM size is not mandatory */
2829 of_property_read_u32(dev->of_node, "nokia,nvm-size",
2830 &hwcfg->nvm_size);
2832 rval = of_property_read_u32(dev->of_node, "clock-frequency",
2833 &hwcfg->ext_clk);
2834 if (rval) {
2835 dev_warn(dev, "can't get clock-frequency\n");
2836 goto out_err;
2839 dev_dbg(dev, "nvm %d, clk %d, csi %d\n", hwcfg->nvm_size,
2840 hwcfg->ext_clk, hwcfg->csi_signalling_mode);
2842 if (!bus_cfg->nr_of_link_frequencies) {
2843 dev_warn(dev, "no link frequencies defined\n");
2844 goto out_err;
2847 hwcfg->op_sys_clock = devm_kcalloc(
2848 dev, bus_cfg->nr_of_link_frequencies + 1 /* guardian */,
2849 sizeof(*hwcfg->op_sys_clock), GFP_KERNEL);
2850 if (!hwcfg->op_sys_clock)
2851 goto out_err;
2853 for (i = 0; i < bus_cfg->nr_of_link_frequencies; i++) {
2854 hwcfg->op_sys_clock[i] = bus_cfg->link_frequencies[i];
2855 dev_dbg(dev, "freq %d: %lld\n", i, hwcfg->op_sys_clock[i]);
2858 v4l2_of_free_endpoint(bus_cfg);
2859 of_node_put(ep);
2860 return hwcfg;
2862 out_err:
2863 v4l2_of_free_endpoint(bus_cfg);
2864 of_node_put(ep);
2865 return NULL;
2868 static int smiapp_probe(struct i2c_client *client,
2869 const struct i2c_device_id *devid)
2871 struct smiapp_sensor *sensor;
2872 struct smiapp_hwconfig *hwcfg = smiapp_get_hwconfig(&client->dev);
2873 unsigned int i;
2874 int rval;
2876 if (hwcfg == NULL)
2877 return -ENODEV;
2879 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
2880 if (sensor == NULL)
2881 return -ENOMEM;
2883 sensor->hwcfg = hwcfg;
2884 mutex_init(&sensor->mutex);
2885 sensor->src = &sensor->ssds[sensor->ssds_used];
2887 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2888 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2890 sensor->vana = devm_regulator_get(&client->dev, "vana");
2891 if (IS_ERR(sensor->vana)) {
2892 dev_err(&client->dev, "could not get regulator for vana\n");
2893 return PTR_ERR(sensor->vana);
2896 sensor->ext_clk = devm_clk_get(&client->dev, NULL);
2897 if (IS_ERR(sensor->ext_clk)) {
2898 dev_err(&client->dev, "could not get clock (%ld)\n",
2899 PTR_ERR(sensor->ext_clk));
2900 return -EPROBE_DEFER;
2903 rval = clk_set_rate(sensor->ext_clk, sensor->hwcfg->ext_clk);
2904 if (rval < 0) {
2905 dev_err(&client->dev,
2906 "unable to set clock freq to %u\n",
2907 sensor->hwcfg->ext_clk);
2908 return rval;
2911 sensor->xshutdown = devm_gpiod_get_optional(&client->dev, "xshutdown",
2912 GPIOD_OUT_LOW);
2913 if (IS_ERR(sensor->xshutdown))
2914 return PTR_ERR(sensor->xshutdown);
2916 pm_runtime_enable(&client->dev);
2918 rval = pm_runtime_get_sync(&client->dev);
2919 if (rval < 0) {
2920 rval = -ENODEV;
2921 goto out_power_off;
2924 rval = smiapp_identify_module(sensor);
2925 if (rval) {
2926 rval = -ENODEV;
2927 goto out_power_off;
2930 rval = smiapp_get_all_limits(sensor);
2931 if (rval) {
2932 rval = -ENODEV;
2933 goto out_power_off;
2936 rval = smiapp_read_frame_fmt(sensor);
2937 if (rval) {
2938 rval = -ENODEV;
2939 goto out_power_off;
2943 * Handle Sensor Module orientation on the board.
2945 * The application of H-FLIP and V-FLIP on the sensor is modified by
2946 * the sensor orientation on the board.
2948 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2949 * both H-FLIP and V-FLIP for normal operation which also implies
2950 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2951 * controls will need to be internally inverted.
2953 * Rotation also changes the bayer pattern.
2955 if (sensor->hwcfg->module_board_orient ==
2956 SMIAPP_MODULE_BOARD_ORIENT_180)
2957 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2958 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2960 rval = smiapp_call_quirk(sensor, limits);
2961 if (rval) {
2962 dev_err(&client->dev, "limits quirks failed\n");
2963 goto out_power_off;
2966 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2967 u32 val;
2969 rval = smiapp_read(sensor,
2970 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2971 if (rval < 0) {
2972 rval = -ENODEV;
2973 goto out_power_off;
2975 sensor->nbinning_subtypes = min_t(u8, val,
2976 SMIAPP_BINNING_SUBTYPES);
2978 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2979 rval = smiapp_read(
2980 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
2981 if (rval < 0) {
2982 rval = -ENODEV;
2983 goto out_power_off;
2985 sensor->binning_subtypes[i] =
2986 *(struct smiapp_binning_subtype *)&val;
2988 dev_dbg(&client->dev, "binning %xx%x\n",
2989 sensor->binning_subtypes[i].horizontal,
2990 sensor->binning_subtypes[i].vertical);
2993 sensor->binning_horizontal = 1;
2994 sensor->binning_vertical = 1;
2996 if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
2997 dev_err(&client->dev, "sysfs ident entry creation failed\n");
2998 rval = -ENOENT;
2999 goto out_power_off;
3001 /* SMIA++ NVM initialization - it will be read from the sensor
3002 * when it is first requested by userspace.
3004 if (sensor->minfo.smiapp_version && sensor->hwcfg->nvm_size) {
3005 sensor->nvm = devm_kzalloc(&client->dev,
3006 sensor->hwcfg->nvm_size, GFP_KERNEL);
3007 if (sensor->nvm == NULL) {
3008 rval = -ENOMEM;
3009 goto out_cleanup;
3012 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
3013 dev_err(&client->dev, "sysfs nvm entry failed\n");
3014 rval = -EBUSY;
3015 goto out_cleanup;
3019 /* We consider this as profile 0 sensor if any of these are zero. */
3020 if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
3021 !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
3022 !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
3023 !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
3024 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
3025 } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
3026 != SMIAPP_SCALING_CAPABILITY_NONE) {
3027 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
3028 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
3029 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
3030 else
3031 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
3032 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3033 sensor->ssds_used++;
3034 } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
3035 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
3036 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3037 sensor->ssds_used++;
3039 sensor->binner = &sensor->ssds[sensor->ssds_used];
3040 sensor->ssds_used++;
3041 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
3042 sensor->ssds_used++;
3044 sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
3046 /* prepare PLL configuration input values */
3047 sensor->pll.bus_type = SMIAPP_PLL_BUS_TYPE_CSI2;
3048 sensor->pll.csi2.lanes = sensor->hwcfg->lanes;
3049 sensor->pll.ext_clk_freq_hz = sensor->hwcfg->ext_clk;
3050 sensor->pll.scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
3051 /* Profile 0 sensors have no separate OP clock branch. */
3052 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
3053 sensor->pll.flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
3055 smiapp_create_subdev(sensor, sensor->scaler, "scaler", 2);
3056 smiapp_create_subdev(sensor, sensor->binner, "binner", 2);
3057 smiapp_create_subdev(sensor, sensor->pixel_array, "pixel_array", 1);
3059 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
3061 sensor->pixel_array->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3063 rval = smiapp_init_controls(sensor);
3064 if (rval < 0)
3065 goto out_cleanup;
3067 rval = smiapp_call_quirk(sensor, init);
3068 if (rval)
3069 goto out_cleanup;
3071 rval = smiapp_get_mbus_formats(sensor);
3072 if (rval) {
3073 rval = -ENODEV;
3074 goto out_cleanup;
3077 rval = smiapp_init_late_controls(sensor);
3078 if (rval) {
3079 rval = -ENODEV;
3080 goto out_cleanup;
3083 mutex_lock(&sensor->mutex);
3084 rval = smiapp_update_mode(sensor);
3085 mutex_unlock(&sensor->mutex);
3086 if (rval) {
3087 dev_err(&client->dev, "update mode failed\n");
3088 goto out_cleanup;
3091 sensor->streaming = false;
3092 sensor->dev_init_done = true;
3094 rval = media_entity_pads_init(&sensor->src->sd.entity, 2,
3095 sensor->src->pads);
3096 if (rval < 0)
3097 goto out_media_entity_cleanup;
3099 rval = v4l2_async_register_subdev(&sensor->src->sd);
3100 if (rval < 0)
3101 goto out_media_entity_cleanup;
3103 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
3104 pm_runtime_use_autosuspend(&client->dev);
3105 pm_runtime_put_autosuspend(&client->dev);
3107 return 0;
3109 out_media_entity_cleanup:
3110 media_entity_cleanup(&sensor->src->sd.entity);
3112 out_cleanup:
3113 smiapp_cleanup(sensor);
3115 out_power_off:
3116 pm_runtime_put(&client->dev);
3117 pm_runtime_disable(&client->dev);
3119 return rval;
3122 static int smiapp_remove(struct i2c_client *client)
3124 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3125 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
3126 unsigned int i;
3128 v4l2_async_unregister_subdev(subdev);
3130 pm_runtime_suspend(&client->dev);
3131 pm_runtime_disable(&client->dev);
3133 for (i = 0; i < sensor->ssds_used; i++) {
3134 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
3135 media_entity_cleanup(&sensor->ssds[i].sd.entity);
3137 smiapp_cleanup(sensor);
3139 return 0;
3142 static const struct of_device_id smiapp_of_table[] = {
3143 { .compatible = "nokia,smia" },
3144 { },
3146 MODULE_DEVICE_TABLE(of, smiapp_of_table);
3148 static const struct i2c_device_id smiapp_id_table[] = {
3149 { SMIAPP_NAME, 0 },
3150 { },
3152 MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
3154 static const struct dev_pm_ops smiapp_pm_ops = {
3155 SET_SYSTEM_SLEEP_PM_OPS(smiapp_suspend, smiapp_resume)
3156 SET_RUNTIME_PM_OPS(smiapp_power_off, smiapp_power_on, NULL)
3159 static struct i2c_driver smiapp_i2c_driver = {
3160 .driver = {
3161 .of_match_table = smiapp_of_table,
3162 .name = SMIAPP_NAME,
3163 .pm = &smiapp_pm_ops,
3165 .probe = smiapp_probe,
3166 .remove = smiapp_remove,
3167 .id_table = smiapp_id_table,
3170 module_i2c_driver(smiapp_i2c_driver);
3172 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3173 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3174 MODULE_LICENSE("GPL");