Merge branch 'work.regset' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / drivers / media / i2c / smiapp / smiapp-core.c
blob5e4f6a2ef78e5b3e5bac2670d3fdf01529bc4076
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/media/i2c/smiapp/smiapp-core.c
5 * Generic driver for SMIA/SMIA++ compliant camera modules
7 * Copyright (C) 2010--2012 Nokia Corporation
8 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
10 * Based on smiapp driver by Vimarsh Zutshi
11 * Based on jt8ev1.c by Vimarsh Zutshi
12 * Based on smia-sensor.c by Tuukka Toivonen <tuukkat76@gmail.com>
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/gpio.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/property.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/smiapp.h>
26 #include <linux/v4l2-mediabus.h>
27 #include <media/v4l2-fwnode.h>
28 #include <media/v4l2-device.h>
30 #include "smiapp.h"
32 #define SMIAPP_ALIGN_DIM(dim, flags) \
33 ((flags) & V4L2_SEL_FLAG_GE \
34 ? ALIGN((dim), 2) \
35 : (dim) & ~1)
38 * smiapp_module_idents - supported camera modules
40 static const struct smiapp_module_ident smiapp_module_idents[] = {
41 SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
42 SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
43 SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
44 SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
45 SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
46 SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk),
47 SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
48 SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
49 SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk),
50 SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk),
51 SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk),
56 * Dynamic Capability Identification
60 static u32 smiapp_get_limit(struct smiapp_sensor *sensor,
61 unsigned int limit)
63 if (WARN_ON(limit >= SMIAPP_LIMIT_LAST))
64 return 1;
66 return sensor->limits[limit];
69 #define SMIA_LIM(sensor, limit) \
70 smiapp_get_limit(sensor, SMIAPP_LIMIT_##limit)
72 static int smiapp_read_all_smia_limits(struct smiapp_sensor *sensor)
74 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
75 unsigned int i;
76 int rval;
78 for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
79 u32 val;
81 rval = smiapp_read(
82 sensor, smiapp_reg_limits[i].addr, &val);
83 if (rval)
84 return rval;
86 sensor->limits[i] = val;
88 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n",
89 smiapp_reg_limits[i].addr,
90 smiapp_reg_limits[i].what, val, val);
93 if (SMIA_LIM(sensor, SCALER_N_MIN) == 0)
94 smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
96 return 0;
99 static int smiapp_read_frame_fmt(struct smiapp_sensor *sensor)
101 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
102 u32 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc;
103 unsigned int i;
104 int pixel_count = 0;
105 int line_count = 0;
106 int rval;
108 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
109 &fmt_model_type);
110 if (rval)
111 return rval;
113 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
114 &fmt_model_subtype);
115 if (rval)
116 return rval;
118 ncol_desc = (fmt_model_subtype
119 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK)
120 >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT;
121 nrow_desc = fmt_model_subtype
122 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK;
124 dev_dbg(&client->dev, "format_model_type %s\n",
125 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
126 ? "2 byte" :
127 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
128 ? "4 byte" : "is simply bad");
130 for (i = 0; i < ncol_desc + nrow_desc; i++) {
131 u32 desc;
132 u32 pixelcode;
133 u32 pixels;
134 char *which;
135 char *what;
136 u32 reg;
138 if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) {
139 reg = SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i);
140 rval = smiapp_read(sensor, reg, &desc);
141 if (rval)
142 return rval;
144 pixelcode =
145 (desc
146 & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK)
147 >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT;
148 pixels = desc & SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK;
149 } else if (fmt_model_type
150 == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE) {
151 reg = SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i);
152 rval = smiapp_read(sensor, reg, &desc);
153 if (rval)
154 return rval;
156 pixelcode =
157 (desc
158 & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK)
159 >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT;
160 pixels = desc & SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK;
161 } else {
162 dev_dbg(&client->dev,
163 "invalid frame format model type %d\n",
164 fmt_model_type);
165 return -EINVAL;
168 if (i < ncol_desc)
169 which = "columns";
170 else
171 which = "rows";
173 switch (pixelcode) {
174 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
175 what = "embedded";
176 break;
177 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY:
178 what = "dummy";
179 break;
180 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK:
181 what = "black";
182 break;
183 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK:
184 what = "dark";
185 break;
186 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
187 what = "visible";
188 break;
189 default:
190 what = "invalid";
191 break;
194 dev_dbg(&client->dev,
195 "0x%8.8x %s pixels: %d %s (pixelcode %u)\n", reg,
196 what, pixels, which, pixelcode);
198 if (i < ncol_desc) {
199 if (pixelcode ==
200 SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE)
201 sensor->visible_pixel_start = pixel_count;
202 pixel_count += pixels;
203 continue;
206 /* Handle row descriptors */
207 switch (pixelcode) {
208 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
209 if (sensor->embedded_end)
210 break;
211 sensor->embedded_start = line_count;
212 sensor->embedded_end = line_count + pixels;
213 break;
214 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
215 sensor->image_start = line_count;
216 break;
218 line_count += pixels;
221 if (sensor->embedded_end > sensor->image_start) {
222 dev_dbg(&client->dev,
223 "adjusting image start line to %u (was %u)\n",
224 sensor->embedded_end, sensor->image_start);
225 sensor->image_start = sensor->embedded_end;
228 dev_dbg(&client->dev, "embedded data from lines %d to %d\n",
229 sensor->embedded_start, sensor->embedded_end);
230 dev_dbg(&client->dev, "image data starts at line %d\n",
231 sensor->image_start);
233 return 0;
236 static int smiapp_pll_configure(struct smiapp_sensor *sensor)
238 struct smiapp_pll *pll = &sensor->pll;
239 int rval;
241 rval = smiapp_write(
242 sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt.pix_clk_div);
243 if (rval < 0)
244 return rval;
246 rval = smiapp_write(
247 sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt.sys_clk_div);
248 if (rval < 0)
249 return rval;
251 rval = smiapp_write(
252 sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
253 if (rval < 0)
254 return rval;
256 rval = smiapp_write(
257 sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
258 if (rval < 0)
259 return rval;
261 /* Lane op clock ratio does not apply here. */
262 rval = smiapp_write(
263 sensor, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS,
264 DIV_ROUND_UP(pll->op.sys_clk_freq_hz, 1000000 / 256 / 256));
265 if (rval < 0 || sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
266 return rval;
268 rval = smiapp_write(
269 sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op.pix_clk_div);
270 if (rval < 0)
271 return rval;
273 return smiapp_write(
274 sensor, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op.sys_clk_div);
277 static int smiapp_pll_try(struct smiapp_sensor *sensor,
278 struct smiapp_pll *pll)
280 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
281 struct smiapp_pll_limits lim = {
282 .min_pre_pll_clk_div = SMIA_LIM(sensor, MIN_PRE_PLL_CLK_DIV),
283 .max_pre_pll_clk_div = SMIA_LIM(sensor, MAX_PRE_PLL_CLK_DIV),
284 .min_pll_ip_freq_hz = SMIA_LIM(sensor, MIN_PLL_IP_FREQ_HZ),
285 .max_pll_ip_freq_hz = SMIA_LIM(sensor, MAX_PLL_IP_FREQ_HZ),
286 .min_pll_multiplier = SMIA_LIM(sensor, MIN_PLL_MULTIPLIER),
287 .max_pll_multiplier = SMIA_LIM(sensor, MAX_PLL_MULTIPLIER),
288 .min_pll_op_freq_hz = SMIA_LIM(sensor, MIN_PLL_OP_FREQ_HZ),
289 .max_pll_op_freq_hz = SMIA_LIM(sensor, MAX_PLL_OP_FREQ_HZ),
291 .op.min_sys_clk_div = SMIA_LIM(sensor, MIN_OP_SYS_CLK_DIV),
292 .op.max_sys_clk_div = SMIA_LIM(sensor, MAX_OP_SYS_CLK_DIV),
293 .op.min_pix_clk_div = SMIA_LIM(sensor, MIN_OP_PIX_CLK_DIV),
294 .op.max_pix_clk_div = SMIA_LIM(sensor, MAX_OP_PIX_CLK_DIV),
295 .op.min_sys_clk_freq_hz = SMIA_LIM(sensor, MIN_OP_SYS_CLK_FREQ_HZ),
296 .op.max_sys_clk_freq_hz = SMIA_LIM(sensor, MAX_OP_SYS_CLK_FREQ_HZ),
297 .op.min_pix_clk_freq_hz = SMIA_LIM(sensor, MIN_OP_PIX_CLK_FREQ_HZ),
298 .op.max_pix_clk_freq_hz = SMIA_LIM(sensor, MAX_OP_PIX_CLK_FREQ_HZ),
300 .vt.min_sys_clk_div = SMIA_LIM(sensor, MIN_VT_SYS_CLK_DIV),
301 .vt.max_sys_clk_div = SMIA_LIM(sensor, MAX_VT_SYS_CLK_DIV),
302 .vt.min_pix_clk_div = SMIA_LIM(sensor, MIN_VT_PIX_CLK_DIV),
303 .vt.max_pix_clk_div = SMIA_LIM(sensor, MAX_VT_PIX_CLK_DIV),
304 .vt.min_sys_clk_freq_hz = SMIA_LIM(sensor, MIN_VT_SYS_CLK_FREQ_HZ),
305 .vt.max_sys_clk_freq_hz = SMIA_LIM(sensor, MAX_VT_SYS_CLK_FREQ_HZ),
306 .vt.min_pix_clk_freq_hz = SMIA_LIM(sensor, MIN_VT_PIX_CLK_FREQ_HZ),
307 .vt.max_pix_clk_freq_hz = SMIA_LIM(sensor, MAX_VT_PIX_CLK_FREQ_HZ),
309 .min_line_length_pck_bin = SMIA_LIM(sensor, MIN_LINE_LENGTH_PCK_BIN),
310 .min_line_length_pck = SMIA_LIM(sensor, MIN_LINE_LENGTH_PCK),
313 return smiapp_pll_calculate(&client->dev, &lim, pll);
316 static int smiapp_pll_update(struct smiapp_sensor *sensor)
318 struct smiapp_pll *pll = &sensor->pll;
319 int rval;
321 pll->binning_horizontal = sensor->binning_horizontal;
322 pll->binning_vertical = sensor->binning_vertical;
323 pll->link_freq =
324 sensor->link_freq->qmenu_int[sensor->link_freq->val];
325 pll->scale_m = sensor->scale_m;
326 pll->bits_per_pixel = sensor->csi_format->compressed;
328 rval = smiapp_pll_try(sensor, pll);
329 if (rval < 0)
330 return rval;
332 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_parray,
333 pll->pixel_rate_pixel_array);
334 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_csi, pll->pixel_rate_csi);
336 return 0;
342 * V4L2 Controls handling
346 static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
348 struct v4l2_ctrl *ctrl = sensor->exposure;
349 int max;
351 max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
352 + sensor->vblank->val
353 - SMIA_LIM(sensor, COARSE_INTEGRATION_TIME_MAX_MARGIN);
355 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, ctrl->step, max);
359 * Order matters.
361 * 1. Bits-per-pixel, descending.
362 * 2. Bits-per-pixel compressed, descending.
363 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
364 * orders must be defined.
366 static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
367 { MEDIA_BUS_FMT_SGRBG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GRBG, },
368 { MEDIA_BUS_FMT_SRGGB16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_RGGB, },
369 { MEDIA_BUS_FMT_SBGGR16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_BGGR, },
370 { MEDIA_BUS_FMT_SGBRG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GBRG, },
371 { MEDIA_BUS_FMT_SGRBG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GRBG, },
372 { MEDIA_BUS_FMT_SRGGB14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_RGGB, },
373 { MEDIA_BUS_FMT_SBGGR14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_BGGR, },
374 { MEDIA_BUS_FMT_SGBRG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GBRG, },
375 { MEDIA_BUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
376 { MEDIA_BUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
377 { MEDIA_BUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
378 { MEDIA_BUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
379 { MEDIA_BUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
380 { MEDIA_BUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
381 { MEDIA_BUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
382 { MEDIA_BUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
383 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
384 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
385 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
386 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
387 { MEDIA_BUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, },
388 { MEDIA_BUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, },
389 { MEDIA_BUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, },
390 { MEDIA_BUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, },
393 static const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
395 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
396 - (unsigned long)smiapp_csi_data_formats) \
397 / sizeof(*smiapp_csi_data_formats))
399 static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
401 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
402 int flip = 0;
404 if (sensor->hflip) {
405 if (sensor->hflip->val)
406 flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
408 if (sensor->vflip->val)
409 flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
412 flip ^= sensor->hvflip_inv_mask;
414 dev_dbg(&client->dev, "flip %d\n", flip);
415 return sensor->default_pixel_order ^ flip;
418 static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
420 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
421 unsigned int csi_format_idx =
422 to_csi_format_idx(sensor->csi_format) & ~3;
423 unsigned int internal_csi_format_idx =
424 to_csi_format_idx(sensor->internal_csi_format) & ~3;
425 unsigned int pixel_order = smiapp_pixel_order(sensor);
427 sensor->mbus_frame_fmts =
428 sensor->default_mbus_frame_fmts << pixel_order;
429 sensor->csi_format =
430 &smiapp_csi_data_formats[csi_format_idx + pixel_order];
431 sensor->internal_csi_format =
432 &smiapp_csi_data_formats[internal_csi_format_idx
433 + pixel_order];
435 BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
436 >= ARRAY_SIZE(smiapp_csi_data_formats));
438 dev_dbg(&client->dev, "new pixel order %s\n",
439 pixel_order_str[pixel_order]);
442 static const char * const smiapp_test_patterns[] = {
443 "Disabled",
444 "Solid Colour",
445 "Eight Vertical Colour Bars",
446 "Colour Bars With Fade to Grey",
447 "Pseudorandom Sequence (PN9)",
450 static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
452 struct smiapp_sensor *sensor =
453 container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
454 ->sensor;
455 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
456 int pm_status;
457 u32 orient = 0;
458 unsigned int i;
459 int exposure;
460 int rval;
462 switch (ctrl->id) {
463 case V4L2_CID_HFLIP:
464 case V4L2_CID_VFLIP:
465 if (sensor->streaming)
466 return -EBUSY;
468 if (sensor->hflip->val)
469 orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
471 if (sensor->vflip->val)
472 orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
474 orient ^= sensor->hvflip_inv_mask;
476 smiapp_update_mbus_formats(sensor);
478 break;
479 case V4L2_CID_VBLANK:
480 exposure = sensor->exposure->val;
482 __smiapp_update_exposure_limits(sensor);
484 if (exposure > sensor->exposure->maximum) {
485 sensor->exposure->val = sensor->exposure->maximum;
486 rval = smiapp_set_ctrl(sensor->exposure);
487 if (rval < 0)
488 return rval;
491 break;
492 case V4L2_CID_LINK_FREQ:
493 if (sensor->streaming)
494 return -EBUSY;
496 rval = smiapp_pll_update(sensor);
497 if (rval)
498 return rval;
500 return 0;
501 case V4L2_CID_TEST_PATTERN:
502 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
503 v4l2_ctrl_activate(
504 sensor->test_data[i],
505 ctrl->val ==
506 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR);
508 break;
511 pm_runtime_get_noresume(&client->dev);
512 pm_status = pm_runtime_get_if_in_use(&client->dev);
513 pm_runtime_put_noidle(&client->dev);
514 if (!pm_status)
515 return 0;
517 switch (ctrl->id) {
518 case V4L2_CID_ANALOGUE_GAIN:
519 rval = smiapp_write(
520 sensor,
521 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
523 break;
524 case V4L2_CID_EXPOSURE:
525 rval = smiapp_write(
526 sensor,
527 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
529 break;
530 case V4L2_CID_HFLIP:
531 case V4L2_CID_VFLIP:
532 rval = smiapp_write(sensor, SMIAPP_REG_U8_IMAGE_ORIENTATION,
533 orient);
535 break;
536 case V4L2_CID_VBLANK:
537 rval = smiapp_write(
538 sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
539 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
540 + ctrl->val);
542 break;
543 case V4L2_CID_HBLANK:
544 rval = smiapp_write(
545 sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
546 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
547 + ctrl->val);
549 break;
550 case V4L2_CID_TEST_PATTERN:
551 rval = smiapp_write(
552 sensor, SMIAPP_REG_U16_TEST_PATTERN_MODE, ctrl->val);
554 break;
555 case V4L2_CID_TEST_PATTERN_RED:
556 rval = smiapp_write(
557 sensor, SMIAPP_REG_U16_TEST_DATA_RED, ctrl->val);
559 break;
560 case V4L2_CID_TEST_PATTERN_GREENR:
561 rval = smiapp_write(
562 sensor, SMIAPP_REG_U16_TEST_DATA_GREENR, ctrl->val);
564 break;
565 case V4L2_CID_TEST_PATTERN_BLUE:
566 rval = smiapp_write(
567 sensor, SMIAPP_REG_U16_TEST_DATA_BLUE, ctrl->val);
569 break;
570 case V4L2_CID_TEST_PATTERN_GREENB:
571 rval = smiapp_write(
572 sensor, SMIAPP_REG_U16_TEST_DATA_GREENB, ctrl->val);
574 break;
575 case V4L2_CID_PIXEL_RATE:
576 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
577 rval = 0;
579 break;
580 default:
581 rval = -EINVAL;
584 if (pm_status > 0) {
585 pm_runtime_mark_last_busy(&client->dev);
586 pm_runtime_put_autosuspend(&client->dev);
589 return rval;
592 static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
593 .s_ctrl = smiapp_set_ctrl,
596 static int smiapp_init_controls(struct smiapp_sensor *sensor)
598 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
599 int rval;
601 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 12);
602 if (rval)
603 return rval;
605 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
607 sensor->analog_gain = v4l2_ctrl_new_std(
608 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
609 V4L2_CID_ANALOGUE_GAIN,
610 SMIA_LIM(sensor, ANALOGUE_GAIN_CODE_MIN),
611 SMIA_LIM(sensor, ANALOGUE_GAIN_CODE_MAX),
612 max(SMIA_LIM(sensor, ANALOGUE_GAIN_CODE_STEP), 1U),
613 SMIA_LIM(sensor, ANALOGUE_GAIN_CODE_MIN));
615 /* Exposure limits will be updated soon, use just something here. */
616 sensor->exposure = v4l2_ctrl_new_std(
617 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
618 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
620 sensor->hflip = v4l2_ctrl_new_std(
621 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
622 V4L2_CID_HFLIP, 0, 1, 1, 0);
623 sensor->vflip = v4l2_ctrl_new_std(
624 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
625 V4L2_CID_VFLIP, 0, 1, 1, 0);
627 sensor->vblank = v4l2_ctrl_new_std(
628 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
629 V4L2_CID_VBLANK, 0, 1, 1, 0);
631 if (sensor->vblank)
632 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
634 sensor->hblank = v4l2_ctrl_new_std(
635 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
636 V4L2_CID_HBLANK, 0, 1, 1, 0);
638 if (sensor->hblank)
639 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
641 sensor->pixel_rate_parray = v4l2_ctrl_new_std(
642 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
643 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
645 v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler,
646 &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN,
647 ARRAY_SIZE(smiapp_test_patterns) - 1,
648 0, 0, smiapp_test_patterns);
650 if (sensor->pixel_array->ctrl_handler.error) {
651 dev_err(&client->dev,
652 "pixel array controls initialization failed (%d)\n",
653 sensor->pixel_array->ctrl_handler.error);
654 return sensor->pixel_array->ctrl_handler.error;
657 sensor->pixel_array->sd.ctrl_handler =
658 &sensor->pixel_array->ctrl_handler;
660 v4l2_ctrl_cluster(2, &sensor->hflip);
662 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
663 if (rval)
664 return rval;
666 sensor->src->ctrl_handler.lock = &sensor->mutex;
668 sensor->pixel_rate_csi = v4l2_ctrl_new_std(
669 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
670 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
672 if (sensor->src->ctrl_handler.error) {
673 dev_err(&client->dev,
674 "src controls initialization failed (%d)\n",
675 sensor->src->ctrl_handler.error);
676 return sensor->src->ctrl_handler.error;
679 sensor->src->sd.ctrl_handler = &sensor->src->ctrl_handler;
681 return 0;
685 * For controls that require information on available media bus codes
686 * and linke frequencies.
688 static int smiapp_init_late_controls(struct smiapp_sensor *sensor)
690 unsigned long *valid_link_freqs = &sensor->valid_link_freqs[
691 sensor->csi_format->compressed - sensor->compressed_min_bpp];
692 unsigned int i;
694 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) {
695 int max_value = (1 << sensor->csi_format->width) - 1;
697 sensor->test_data[i] = v4l2_ctrl_new_std(
698 &sensor->pixel_array->ctrl_handler,
699 &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i,
700 0, max_value, 1, max_value);
703 sensor->link_freq = v4l2_ctrl_new_int_menu(
704 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
705 V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs),
706 __ffs(*valid_link_freqs), sensor->hwcfg->op_sys_clock);
708 return sensor->src->ctrl_handler.error;
711 static void smiapp_free_controls(struct smiapp_sensor *sensor)
713 unsigned int i;
715 for (i = 0; i < sensor->ssds_used; i++)
716 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
719 static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
721 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
722 struct smiapp_pll *pll = &sensor->pll;
723 u8 compressed_max_bpp = 0;
724 unsigned int type, n;
725 unsigned int i, pixel_order;
726 int rval;
728 rval = smiapp_read(
729 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
730 if (rval)
731 return rval;
733 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
735 rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
736 &pixel_order);
737 if (rval)
738 return rval;
740 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
741 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
742 return -EINVAL;
745 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
746 pixel_order_str[pixel_order]);
748 switch (type) {
749 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
750 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
751 break;
752 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
753 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
754 break;
755 default:
756 return -EINVAL;
759 sensor->default_pixel_order = pixel_order;
760 sensor->mbus_frame_fmts = 0;
762 for (i = 0; i < n; i++) {
763 unsigned int fmt, j;
765 rval = smiapp_read(
766 sensor,
767 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
768 if (rval)
769 return rval;
771 dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
772 i, fmt >> 8, (u8)fmt);
774 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
775 const struct smiapp_csi_data_format *f =
776 &smiapp_csi_data_formats[j];
778 if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
779 continue;
781 if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
782 continue;
784 dev_dbg(&client->dev, "jolly good! %d\n", j);
786 sensor->default_mbus_frame_fmts |= 1 << j;
790 /* Figure out which BPP values can be used with which formats. */
791 pll->binning_horizontal = 1;
792 pll->binning_vertical = 1;
793 pll->scale_m = sensor->scale_m;
795 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
796 sensor->compressed_min_bpp =
797 min(smiapp_csi_data_formats[i].compressed,
798 sensor->compressed_min_bpp);
799 compressed_max_bpp =
800 max(smiapp_csi_data_formats[i].compressed,
801 compressed_max_bpp);
804 sensor->valid_link_freqs = devm_kcalloc(
805 &client->dev,
806 compressed_max_bpp - sensor->compressed_min_bpp + 1,
807 sizeof(*sensor->valid_link_freqs), GFP_KERNEL);
808 if (!sensor->valid_link_freqs)
809 return -ENOMEM;
811 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
812 const struct smiapp_csi_data_format *f =
813 &smiapp_csi_data_formats[i];
814 unsigned long *valid_link_freqs =
815 &sensor->valid_link_freqs[
816 f->compressed - sensor->compressed_min_bpp];
817 unsigned int j;
819 if (!(sensor->default_mbus_frame_fmts & 1 << i))
820 continue;
822 pll->bits_per_pixel = f->compressed;
824 for (j = 0; sensor->hwcfg->op_sys_clock[j]; j++) {
825 pll->link_freq = sensor->hwcfg->op_sys_clock[j];
827 rval = smiapp_pll_try(sensor, pll);
828 dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n",
829 pll->link_freq, pll->bits_per_pixel,
830 rval ? "not ok" : "ok");
831 if (rval)
832 continue;
834 set_bit(j, valid_link_freqs);
837 if (!*valid_link_freqs) {
838 dev_info(&client->dev,
839 "no valid link frequencies for %u bpp\n",
840 f->compressed);
841 sensor->default_mbus_frame_fmts &= ~BIT(i);
842 continue;
845 if (!sensor->csi_format
846 || f->width > sensor->csi_format->width
847 || (f->width == sensor->csi_format->width
848 && f->compressed > sensor->csi_format->compressed)) {
849 sensor->csi_format = f;
850 sensor->internal_csi_format = f;
854 if (!sensor->csi_format) {
855 dev_err(&client->dev, "no supported mbus code found\n");
856 return -EINVAL;
859 smiapp_update_mbus_formats(sensor);
861 return 0;
864 static void smiapp_update_blanking(struct smiapp_sensor *sensor)
866 struct v4l2_ctrl *vblank = sensor->vblank;
867 struct v4l2_ctrl *hblank = sensor->hblank;
868 uint16_t min_fll, max_fll, min_llp, max_llp, min_lbp;
869 int min, max;
871 if (sensor->binning_vertical > 1 || sensor->binning_horizontal > 1) {
872 min_fll = SMIA_LIM(sensor, MIN_FRAME_LENGTH_LINES_BIN);
873 max_fll = SMIA_LIM(sensor, MAX_FRAME_LENGTH_LINES_BIN);
874 min_llp = SMIA_LIM(sensor, MIN_LINE_LENGTH_PCK_BIN);
875 max_llp = SMIA_LIM(sensor, MAX_LINE_LENGTH_PCK_BIN);
876 min_lbp = SMIA_LIM(sensor, MIN_LINE_BLANKING_PCK_BIN);
877 } else {
878 min_fll = SMIA_LIM(sensor, MIN_FRAME_LENGTH_LINES);
879 max_fll = SMIA_LIM(sensor, MAX_FRAME_LENGTH_LINES);
880 min_llp = SMIA_LIM(sensor, MIN_LINE_LENGTH_PCK);
881 max_llp = SMIA_LIM(sensor, MAX_LINE_LENGTH_PCK);
882 min_lbp = SMIA_LIM(sensor, MIN_LINE_BLANKING_PCK);
885 min = max_t(int,
886 SMIA_LIM(sensor, MIN_FRAME_BLANKING_LINES),
887 min_fll -
888 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
889 max = max_fll - sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
891 __v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);
893 min = max_t(int,
894 min_llp -
895 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
896 min_lbp);
897 max = max_llp - sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
899 __v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
901 __smiapp_update_exposure_limits(sensor);
904 static int smiapp_pll_blanking_update(struct smiapp_sensor *sensor)
906 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
907 int rval;
909 rval = smiapp_pll_update(sensor);
910 if (rval < 0)
911 return rval;
913 /* Output from pixel array, including blanking */
914 smiapp_update_blanking(sensor);
916 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
917 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
919 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
920 sensor->pll.pixel_rate_pixel_array /
921 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
922 + sensor->hblank->val) *
923 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
924 + sensor->vblank->val) / 100));
926 return 0;
931 * SMIA++ NVM handling
935 static int smiapp_read_nvm_page(struct smiapp_sensor *sensor, u32 p, u8 *nvm,
936 u8 *status)
938 unsigned int i;
939 int rval;
940 u32 s;
942 *status = 0;
944 rval = smiapp_write(sensor,
945 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
946 if (rval)
947 return rval;
949 rval = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
950 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN);
951 if (rval)
952 return rval;
954 rval = smiapp_read(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS,
955 &s);
956 if (rval)
957 return rval;
959 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_EUSAGE) {
960 *status = s;
961 return -ENODATA;
964 if (SMIA_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) &
965 SMIAPP_DATA_TRANSFER_IF_CAPABILITY_POLL) {
966 for (i = 1000; i > 0; i--) {
967 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
968 break;
970 rval = smiapp_read(
971 sensor,
972 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS,
973 &s);
975 if (rval)
976 return rval;
979 if (!i)
980 return -ETIMEDOUT;
983 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
984 u32 v;
986 rval = smiapp_read(sensor,
987 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
988 &v);
989 if (rval)
990 return rval;
992 *nvm++ = v;
995 return 0;
998 static int smiapp_read_nvm(struct smiapp_sensor *sensor, unsigned char *nvm,
999 size_t nvm_size)
1001 u8 status = 0;
1002 u32 p;
1003 int rval = 0, rval2;
1005 for (p = 0; p < nvm_size / SMIAPP_NVM_PAGE_SIZE && !rval; p++) {
1006 rval = smiapp_read_nvm_page(sensor, p, nvm, &status);
1007 nvm += SMIAPP_NVM_PAGE_SIZE;
1010 if (rval == -ENODATA &&
1011 status & SMIAPP_DATA_TRANSFER_IF_1_STATUS_EUSAGE)
1012 rval = 0;
1014 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
1015 if (rval < 0)
1016 return rval;
1017 else
1018 return rval2 ?: p * SMIAPP_NVM_PAGE_SIZE;
1023 * SMIA++ CCI address control
1026 static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
1028 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1029 int rval;
1030 u32 val;
1032 client->addr = sensor->hwcfg->i2c_addr_dfl;
1034 rval = smiapp_write(sensor,
1035 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
1036 sensor->hwcfg->i2c_addr_alt << 1);
1037 if (rval)
1038 return rval;
1040 client->addr = sensor->hwcfg->i2c_addr_alt;
1042 /* verify addr change went ok */
1043 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
1044 if (rval)
1045 return rval;
1047 if (val != sensor->hwcfg->i2c_addr_alt << 1)
1048 return -ENODEV;
1050 return 0;
1055 * SMIA++ Mode Control
1058 static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
1060 struct smiapp_flash_strobe_parms *strobe_setup;
1061 unsigned int ext_freq = sensor->hwcfg->ext_clk;
1062 u32 tmp;
1063 u32 strobe_adjustment;
1064 u32 strobe_width_high_rs;
1065 int rval;
1067 strobe_setup = sensor->hwcfg->strobe_setup;
1070 * How to calculate registers related to strobe length. Please
1071 * do not change, or if you do at least know what you're
1072 * doing. :-)
1074 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1076 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1077 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1079 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1080 * flash_strobe_adjustment E N, [1 - 0xff]
1082 * The formula above is written as below to keep it on one
1083 * line:
1085 * l / 10^6 = w / e * a
1087 * Let's mark w * a by x:
1089 * x = w * a
1091 * Thus, we get:
1093 * x = l * e / 10^6
1095 * The strobe width must be at least as long as requested,
1096 * thus rounding upwards is needed.
1098 * x = (l * e + 10^6 - 1) / 10^6
1099 * -----------------------------
1101 * Maximum possible accuracy is wanted at all times. Thus keep
1102 * a as small as possible.
1104 * Calculate a, assuming maximum w, with rounding upwards:
1106 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1107 * -------------------------------------
1109 * Thus, we also get w, with that a, with rounding upwards:
1111 * w = (x + a - 1) / a
1112 * -------------------
1114 * To get limits:
1116 * x E [1, (2^16 - 1) * (2^8 - 1)]
1118 * Substituting maximum x to the original formula (with rounding),
1119 * the maximum l is thus
1121 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1123 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1124 * --------------------------------------------------
1126 * flash_strobe_length must be clamped between 1 and
1127 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1129 * Then,
1131 * flash_strobe_adjustment = ((flash_strobe_length *
1132 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1134 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1135 * EXTCLK freq + 10^6 - 1) / 10^6 +
1136 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1138 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1139 1000000 + 1, ext_freq);
1140 strobe_setup->strobe_width_high_us =
1141 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1143 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1144 1000000 - 1), 1000000ULL);
1145 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1146 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1147 strobe_adjustment;
1149 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
1150 strobe_setup->mode);
1151 if (rval < 0)
1152 goto out;
1154 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1155 strobe_adjustment);
1156 if (rval < 0)
1157 goto out;
1159 rval = smiapp_write(
1160 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1161 strobe_width_high_rs);
1162 if (rval < 0)
1163 goto out;
1165 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1166 strobe_setup->strobe_delay);
1167 if (rval < 0)
1168 goto out;
1170 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1171 strobe_setup->stobe_start_point);
1172 if (rval < 0)
1173 goto out;
1175 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1176 strobe_setup->trigger);
1178 out:
1179 sensor->hwcfg->strobe_setup->trigger = 0;
1181 return rval;
1184 /* -----------------------------------------------------------------------------
1185 * Power management
1188 static int smiapp_power_on(struct device *dev)
1190 struct i2c_client *client = to_i2c_client(dev);
1191 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1192 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1194 * The sub-device related to the I2C device is always the
1195 * source one, i.e. ssds[0].
1197 struct smiapp_sensor *sensor =
1198 container_of(ssd, struct smiapp_sensor, ssds[0]);
1199 unsigned int sleep;
1200 int rval;
1202 rval = regulator_enable(sensor->vana);
1203 if (rval) {
1204 dev_err(&client->dev, "failed to enable vana regulator\n");
1205 return rval;
1207 usleep_range(1000, 1000);
1209 rval = clk_prepare_enable(sensor->ext_clk);
1210 if (rval < 0) {
1211 dev_dbg(&client->dev, "failed to enable xclk\n");
1212 goto out_xclk_fail;
1214 usleep_range(1000, 1000);
1216 gpiod_set_value(sensor->xshutdown, 1);
1218 sleep = SMIAPP_RESET_DELAY(sensor->hwcfg->ext_clk);
1219 usleep_range(sleep, sleep);
1222 * Failures to respond to the address change command have been noticed.
1223 * Those failures seem to be caused by the sensor requiring a longer
1224 * boot time than advertised. An additional 10ms delay seems to work
1225 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1226 * unnecessary. The failures need to be investigated to find a proper
1227 * fix, and a delay will likely need to be added here if the I2C write
1228 * retry hack is reverted before the root cause of the boot time issue
1229 * is found.
1232 if (sensor->hwcfg->i2c_addr_alt) {
1233 rval = smiapp_change_cci_addr(sensor);
1234 if (rval) {
1235 dev_err(&client->dev, "cci address change error\n");
1236 goto out_cci_addr_fail;
1240 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
1241 SMIAPP_SOFTWARE_RESET);
1242 if (rval < 0) {
1243 dev_err(&client->dev, "software reset failed\n");
1244 goto out_cci_addr_fail;
1247 if (sensor->hwcfg->i2c_addr_alt) {
1248 rval = smiapp_change_cci_addr(sensor);
1249 if (rval) {
1250 dev_err(&client->dev, "cci address change error\n");
1251 goto out_cci_addr_fail;
1255 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
1256 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1257 if (rval) {
1258 dev_err(&client->dev, "compression mode set failed\n");
1259 goto out_cci_addr_fail;
1262 rval = smiapp_write(
1263 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1264 sensor->hwcfg->ext_clk / (1000000 / (1 << 8)));
1265 if (rval) {
1266 dev_err(&client->dev, "extclk frequency set failed\n");
1267 goto out_cci_addr_fail;
1270 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1271 sensor->hwcfg->lanes - 1);
1272 if (rval) {
1273 dev_err(&client->dev, "csi lane mode set failed\n");
1274 goto out_cci_addr_fail;
1277 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1278 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1279 if (rval) {
1280 dev_err(&client->dev, "fast standby set failed\n");
1281 goto out_cci_addr_fail;
1284 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1285 sensor->hwcfg->csi_signalling_mode);
1286 if (rval) {
1287 dev_err(&client->dev, "csi signalling mode set failed\n");
1288 goto out_cci_addr_fail;
1291 /* DPHY control done by sensor based on requested link rate */
1292 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
1293 SMIAPP_DPHY_CTRL_UI);
1294 if (rval < 0)
1295 goto out_cci_addr_fail;
1297 rval = smiapp_call_quirk(sensor, post_poweron);
1298 if (rval) {
1299 dev_err(&client->dev, "post_poweron quirks failed\n");
1300 goto out_cci_addr_fail;
1303 return 0;
1305 out_cci_addr_fail:
1306 gpiod_set_value(sensor->xshutdown, 0);
1307 clk_disable_unprepare(sensor->ext_clk);
1309 out_xclk_fail:
1310 regulator_disable(sensor->vana);
1312 return rval;
1315 static int smiapp_power_off(struct device *dev)
1317 struct i2c_client *client = to_i2c_client(dev);
1318 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1319 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1320 struct smiapp_sensor *sensor =
1321 container_of(ssd, struct smiapp_sensor, ssds[0]);
1324 * Currently power/clock to lens are enable/disabled separately
1325 * but they are essentially the same signals. So if the sensor is
1326 * powered off while the lens is powered on the sensor does not
1327 * really see a power off and next time the cci address change
1328 * will fail. So do a soft reset explicitly here.
1330 if (sensor->hwcfg->i2c_addr_alt)
1331 smiapp_write(sensor,
1332 SMIAPP_REG_U8_SOFTWARE_RESET,
1333 SMIAPP_SOFTWARE_RESET);
1335 gpiod_set_value(sensor->xshutdown, 0);
1336 clk_disable_unprepare(sensor->ext_clk);
1337 usleep_range(5000, 5000);
1338 regulator_disable(sensor->vana);
1339 sensor->streaming = false;
1341 return 0;
1344 /* -----------------------------------------------------------------------------
1345 * Video stream management
1348 static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1350 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1351 unsigned int binning_mode;
1352 int rval;
1354 mutex_lock(&sensor->mutex);
1356 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1357 (sensor->csi_format->width << 8) |
1358 sensor->csi_format->compressed);
1359 if (rval)
1360 goto out;
1362 /* Binning configuration */
1363 if (sensor->binning_horizontal == 1 &&
1364 sensor->binning_vertical == 1) {
1365 binning_mode = 0;
1366 } else {
1367 u8 binning_type =
1368 (sensor->binning_horizontal << 4)
1369 | sensor->binning_vertical;
1371 rval = smiapp_write(
1372 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
1373 if (rval < 0)
1374 goto out;
1376 binning_mode = 1;
1378 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
1379 if (rval < 0)
1380 goto out;
1382 /* Set up PLL */
1383 rval = smiapp_pll_configure(sensor);
1384 if (rval)
1385 goto out;
1387 /* Analog crop start coordinates */
1388 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
1389 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1390 if (rval < 0)
1391 goto out;
1393 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
1394 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1395 if (rval < 0)
1396 goto out;
1398 /* Analog crop end coordinates */
1399 rval = smiapp_write(
1400 sensor, SMIAPP_REG_U16_X_ADDR_END,
1401 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1402 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1403 if (rval < 0)
1404 goto out;
1406 rval = smiapp_write(
1407 sensor, SMIAPP_REG_U16_Y_ADDR_END,
1408 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1409 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1410 if (rval < 0)
1411 goto out;
1414 * Output from pixel array, including blanking, is set using
1415 * controls below. No need to set here.
1418 /* Digital crop */
1419 if (SMIA_LIM(sensor, DIGITAL_CROP_CAPABILITY)
1420 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1421 rval = smiapp_write(
1422 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1423 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1424 if (rval < 0)
1425 goto out;
1427 rval = smiapp_write(
1428 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1429 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1430 if (rval < 0)
1431 goto out;
1433 rval = smiapp_write(
1434 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1435 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1436 if (rval < 0)
1437 goto out;
1439 rval = smiapp_write(
1440 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1441 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1442 if (rval < 0)
1443 goto out;
1446 /* Scaling */
1447 if (SMIA_LIM(sensor, SCALING_CAPABILITY)
1448 != SMIAPP_SCALING_CAPABILITY_NONE) {
1449 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
1450 sensor->scaling_mode);
1451 if (rval < 0)
1452 goto out;
1454 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
1455 sensor->scale_m);
1456 if (rval < 0)
1457 goto out;
1460 /* Output size from sensor */
1461 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1462 sensor->src->crop[SMIAPP_PAD_SRC].width);
1463 if (rval < 0)
1464 goto out;
1465 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1466 sensor->src->crop[SMIAPP_PAD_SRC].height);
1467 if (rval < 0)
1468 goto out;
1470 if ((SMIA_LIM(sensor, FLASH_MODE_CAPABILITY) &
1471 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1472 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1473 sensor->hwcfg->strobe_setup != NULL &&
1474 sensor->hwcfg->strobe_setup->trigger != 0) {
1475 rval = smiapp_setup_flash_strobe(sensor);
1476 if (rval)
1477 goto out;
1480 rval = smiapp_call_quirk(sensor, pre_streamon);
1481 if (rval) {
1482 dev_err(&client->dev, "pre_streamon quirks failed\n");
1483 goto out;
1486 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1487 SMIAPP_MODE_SELECT_STREAMING);
1489 out:
1490 mutex_unlock(&sensor->mutex);
1492 return rval;
1495 static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1497 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1498 int rval;
1500 mutex_lock(&sensor->mutex);
1501 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1502 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1503 if (rval)
1504 goto out;
1506 rval = smiapp_call_quirk(sensor, post_streamoff);
1507 if (rval)
1508 dev_err(&client->dev, "post_streamoff quirks failed\n");
1510 out:
1511 mutex_unlock(&sensor->mutex);
1512 return rval;
1515 /* -----------------------------------------------------------------------------
1516 * V4L2 subdev video operations
1519 static int smiapp_pm_get_init(struct smiapp_sensor *sensor)
1521 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1522 int rval;
1524 rval = pm_runtime_get_sync(&client->dev);
1525 if (rval < 0) {
1526 if (rval != -EBUSY && rval != -EAGAIN)
1527 pm_runtime_set_active(&client->dev);
1528 pm_runtime_put_noidle(&client->dev);
1530 return rval;
1531 } else if (!rval) {
1532 rval = v4l2_ctrl_handler_setup(&sensor->pixel_array->
1533 ctrl_handler);
1534 if (rval)
1535 return rval;
1537 return v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1540 return 0;
1543 static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1545 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1546 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1547 int rval;
1549 if (sensor->streaming == enable)
1550 return 0;
1552 if (!enable) {
1553 smiapp_stop_streaming(sensor);
1554 sensor->streaming = false;
1555 pm_runtime_mark_last_busy(&client->dev);
1556 pm_runtime_put_autosuspend(&client->dev);
1558 return 0;
1561 rval = smiapp_pm_get_init(sensor);
1562 if (rval)
1563 return rval;
1565 sensor->streaming = true;
1567 rval = smiapp_start_streaming(sensor);
1568 if (rval < 0) {
1569 sensor->streaming = false;
1570 pm_runtime_mark_last_busy(&client->dev);
1571 pm_runtime_put_autosuspend(&client->dev);
1574 return rval;
1577 static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1578 struct v4l2_subdev_pad_config *cfg,
1579 struct v4l2_subdev_mbus_code_enum *code)
1581 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1582 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1583 unsigned int i;
1584 int idx = -1;
1585 int rval = -EINVAL;
1587 mutex_lock(&sensor->mutex);
1589 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1590 subdev->name, code->pad, code->index);
1592 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1593 if (code->index)
1594 goto out;
1596 code->code = sensor->internal_csi_format->code;
1597 rval = 0;
1598 goto out;
1601 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1602 if (sensor->mbus_frame_fmts & (1 << i))
1603 idx++;
1605 if (idx == code->index) {
1606 code->code = smiapp_csi_data_formats[i].code;
1607 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1608 code->index, i, code->code);
1609 rval = 0;
1610 break;
1614 out:
1615 mutex_unlock(&sensor->mutex);
1617 return rval;
1620 static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1621 unsigned int pad)
1623 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1625 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1626 return sensor->csi_format->code;
1627 else
1628 return sensor->internal_csi_format->code;
1631 static int __smiapp_get_format(struct v4l2_subdev *subdev,
1632 struct v4l2_subdev_pad_config *cfg,
1633 struct v4l2_subdev_format *fmt)
1635 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1637 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1638 fmt->format = *v4l2_subdev_get_try_format(subdev, cfg,
1639 fmt->pad);
1640 } else {
1641 struct v4l2_rect *r;
1643 if (fmt->pad == ssd->source_pad)
1644 r = &ssd->crop[ssd->source_pad];
1645 else
1646 r = &ssd->sink_fmt;
1648 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1649 fmt->format.width = r->width;
1650 fmt->format.height = r->height;
1651 fmt->format.field = V4L2_FIELD_NONE;
1654 return 0;
1657 static int smiapp_get_format(struct v4l2_subdev *subdev,
1658 struct v4l2_subdev_pad_config *cfg,
1659 struct v4l2_subdev_format *fmt)
1661 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1662 int rval;
1664 mutex_lock(&sensor->mutex);
1665 rval = __smiapp_get_format(subdev, cfg, fmt);
1666 mutex_unlock(&sensor->mutex);
1668 return rval;
1671 static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1672 struct v4l2_subdev_pad_config *cfg,
1673 struct v4l2_rect **crops,
1674 struct v4l2_rect **comps, int which)
1676 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1677 unsigned int i;
1679 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1680 if (crops)
1681 for (i = 0; i < subdev->entity.num_pads; i++)
1682 crops[i] = &ssd->crop[i];
1683 if (comps)
1684 *comps = &ssd->compose;
1685 } else {
1686 if (crops) {
1687 for (i = 0; i < subdev->entity.num_pads; i++) {
1688 crops[i] = v4l2_subdev_get_try_crop(subdev, cfg, i);
1689 BUG_ON(!crops[i]);
1692 if (comps) {
1693 *comps = v4l2_subdev_get_try_compose(subdev, cfg,
1694 SMIAPP_PAD_SINK);
1695 BUG_ON(!*comps);
1700 /* Changes require propagation only on sink pad. */
1701 static void smiapp_propagate(struct v4l2_subdev *subdev,
1702 struct v4l2_subdev_pad_config *cfg, int which,
1703 int target)
1705 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1706 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1707 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1709 smiapp_get_crop_compose(subdev, cfg, crops, &comp, which);
1711 switch (target) {
1712 case V4L2_SEL_TGT_CROP:
1713 comp->width = crops[SMIAPP_PAD_SINK]->width;
1714 comp->height = crops[SMIAPP_PAD_SINK]->height;
1715 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1716 if (ssd == sensor->scaler) {
1717 sensor->scale_m =
1718 SMIA_LIM(sensor, SCALER_N_MIN);
1719 sensor->scaling_mode =
1720 SMIAPP_SCALING_MODE_NONE;
1721 } else if (ssd == sensor->binner) {
1722 sensor->binning_horizontal = 1;
1723 sensor->binning_vertical = 1;
1726 /* Fall through */
1727 case V4L2_SEL_TGT_COMPOSE:
1728 *crops[SMIAPP_PAD_SRC] = *comp;
1729 break;
1730 default:
1731 BUG();
1735 static const struct smiapp_csi_data_format
1736 *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1738 unsigned int i;
1740 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1741 if (sensor->mbus_frame_fmts & (1 << i)
1742 && smiapp_csi_data_formats[i].code == code)
1743 return &smiapp_csi_data_formats[i];
1746 return sensor->csi_format;
1749 static int smiapp_set_format_source(struct v4l2_subdev *subdev,
1750 struct v4l2_subdev_pad_config *cfg,
1751 struct v4l2_subdev_format *fmt)
1753 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1754 const struct smiapp_csi_data_format *csi_format,
1755 *old_csi_format = sensor->csi_format;
1756 unsigned long *valid_link_freqs;
1757 u32 code = fmt->format.code;
1758 unsigned int i;
1759 int rval;
1761 rval = __smiapp_get_format(subdev, cfg, fmt);
1762 if (rval)
1763 return rval;
1766 * Media bus code is changeable on src subdev's source pad. On
1767 * other source pads we just get format here.
1769 if (subdev != &sensor->src->sd)
1770 return 0;
1772 csi_format = smiapp_validate_csi_data_format(sensor, code);
1774 fmt->format.code = csi_format->code;
1776 if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1777 return 0;
1779 sensor->csi_format = csi_format;
1781 if (csi_format->width != old_csi_format->width)
1782 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
1783 __v4l2_ctrl_modify_range(
1784 sensor->test_data[i], 0,
1785 (1 << csi_format->width) - 1, 1, 0);
1787 if (csi_format->compressed == old_csi_format->compressed)
1788 return 0;
1790 valid_link_freqs =
1791 &sensor->valid_link_freqs[sensor->csi_format->compressed
1792 - sensor->compressed_min_bpp];
1794 __v4l2_ctrl_modify_range(
1795 sensor->link_freq, 0,
1796 __fls(*valid_link_freqs), ~*valid_link_freqs,
1797 __ffs(*valid_link_freqs));
1799 return smiapp_pll_update(sensor);
1802 static int smiapp_set_format(struct v4l2_subdev *subdev,
1803 struct v4l2_subdev_pad_config *cfg,
1804 struct v4l2_subdev_format *fmt)
1806 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1807 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1808 struct v4l2_rect *crops[SMIAPP_PADS];
1810 mutex_lock(&sensor->mutex);
1812 if (fmt->pad == ssd->source_pad) {
1813 int rval;
1815 rval = smiapp_set_format_source(subdev, cfg, fmt);
1817 mutex_unlock(&sensor->mutex);
1819 return rval;
1822 /* Sink pad. Width and height are changeable here. */
1823 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1824 fmt->format.width &= ~1;
1825 fmt->format.height &= ~1;
1826 fmt->format.field = V4L2_FIELD_NONE;
1828 fmt->format.width =
1829 clamp(fmt->format.width,
1830 SMIA_LIM(sensor, MIN_X_OUTPUT_SIZE),
1831 SMIA_LIM(sensor, MAX_X_OUTPUT_SIZE));
1832 fmt->format.height =
1833 clamp(fmt->format.height,
1834 SMIA_LIM(sensor, MIN_Y_OUTPUT_SIZE),
1835 SMIA_LIM(sensor, MAX_Y_OUTPUT_SIZE));
1837 smiapp_get_crop_compose(subdev, cfg, crops, NULL, fmt->which);
1839 crops[ssd->sink_pad]->left = 0;
1840 crops[ssd->sink_pad]->top = 0;
1841 crops[ssd->sink_pad]->width = fmt->format.width;
1842 crops[ssd->sink_pad]->height = fmt->format.height;
1843 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1844 ssd->sink_fmt = *crops[ssd->sink_pad];
1845 smiapp_propagate(subdev, cfg, fmt->which,
1846 V4L2_SEL_TGT_CROP);
1848 mutex_unlock(&sensor->mutex);
1850 return 0;
1854 * Calculate goodness of scaled image size compared to expected image
1855 * size and flags provided.
1857 #define SCALING_GOODNESS 100000
1858 #define SCALING_GOODNESS_EXTREME 100000000
1859 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1860 int h, int ask_h, u32 flags)
1862 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1863 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1864 int val = 0;
1866 w &= ~1;
1867 ask_w &= ~1;
1868 h &= ~1;
1869 ask_h &= ~1;
1871 if (flags & V4L2_SEL_FLAG_GE) {
1872 if (w < ask_w)
1873 val -= SCALING_GOODNESS;
1874 if (h < ask_h)
1875 val -= SCALING_GOODNESS;
1878 if (flags & V4L2_SEL_FLAG_LE) {
1879 if (w > ask_w)
1880 val -= SCALING_GOODNESS;
1881 if (h > ask_h)
1882 val -= SCALING_GOODNESS;
1885 val -= abs(w - ask_w);
1886 val -= abs(h - ask_h);
1888 if (w < SMIA_LIM(sensor, MIN_X_OUTPUT_SIZE))
1889 val -= SCALING_GOODNESS_EXTREME;
1891 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1892 w, ask_w, h, ask_h, val);
1894 return val;
1897 static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1898 struct v4l2_subdev_pad_config *cfg,
1899 struct v4l2_subdev_selection *sel,
1900 struct v4l2_rect **crops,
1901 struct v4l2_rect *comp)
1903 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1904 unsigned int i;
1905 unsigned int binh = 1, binv = 1;
1906 int best = scaling_goodness(
1907 subdev,
1908 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1909 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1911 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1912 int this = scaling_goodness(
1913 subdev,
1914 crops[SMIAPP_PAD_SINK]->width
1915 / sensor->binning_subtypes[i].horizontal,
1916 sel->r.width,
1917 crops[SMIAPP_PAD_SINK]->height
1918 / sensor->binning_subtypes[i].vertical,
1919 sel->r.height, sel->flags);
1921 if (this > best) {
1922 binh = sensor->binning_subtypes[i].horizontal;
1923 binv = sensor->binning_subtypes[i].vertical;
1924 best = this;
1927 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1928 sensor->binning_vertical = binv;
1929 sensor->binning_horizontal = binh;
1932 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1933 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1937 * Calculate best scaling ratio and mode for given output resolution.
1939 * Try all of these: horizontal ratio, vertical ratio and smallest
1940 * size possible (horizontally).
1942 * Also try whether horizontal scaler or full scaler gives a better
1943 * result.
1945 static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1946 struct v4l2_subdev_pad_config *cfg,
1947 struct v4l2_subdev_selection *sel,
1948 struct v4l2_rect **crops,
1949 struct v4l2_rect *comp)
1951 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1952 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1953 u32 min, max, a, b, max_m;
1954 u32 scale_m = SMIA_LIM(sensor, SCALER_N_MIN);
1955 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1956 u32 try[4];
1957 u32 ntry = 0;
1958 unsigned int i;
1959 int best = INT_MIN;
1961 sel->r.width = min_t(unsigned int, sel->r.width,
1962 crops[SMIAPP_PAD_SINK]->width);
1963 sel->r.height = min_t(unsigned int, sel->r.height,
1964 crops[SMIAPP_PAD_SINK]->height);
1966 a = crops[SMIAPP_PAD_SINK]->width
1967 * SMIA_LIM(sensor, SCALER_N_MIN) / sel->r.width;
1968 b = crops[SMIAPP_PAD_SINK]->height
1969 * SMIA_LIM(sensor, SCALER_N_MIN) / sel->r.height;
1970 max_m = crops[SMIAPP_PAD_SINK]->width
1971 * SMIA_LIM(sensor, SCALER_N_MIN)
1972 / SMIA_LIM(sensor, MIN_X_OUTPUT_SIZE);
1974 a = clamp(a, SMIA_LIM(sensor, SCALER_M_MIN),
1975 SMIA_LIM(sensor, SCALER_M_MAX));
1976 b = clamp(b, SMIA_LIM(sensor, SCALER_M_MIN),
1977 SMIA_LIM(sensor, SCALER_M_MAX));
1978 max_m = clamp(max_m, SMIA_LIM(sensor, SCALER_M_MIN),
1979 SMIA_LIM(sensor, SCALER_M_MAX));
1981 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1983 min = min(max_m, min(a, b));
1984 max = min(max_m, max(a, b));
1986 try[ntry] = min;
1987 ntry++;
1988 if (min != max) {
1989 try[ntry] = max;
1990 ntry++;
1992 if (max != max_m) {
1993 try[ntry] = min + 1;
1994 ntry++;
1995 if (min != max) {
1996 try[ntry] = max + 1;
1997 ntry++;
2001 for (i = 0; i < ntry; i++) {
2002 int this = scaling_goodness(
2003 subdev,
2004 crops[SMIAPP_PAD_SINK]->width
2005 / try[i]
2006 * SMIA_LIM(sensor, SCALER_N_MIN),
2007 sel->r.width,
2008 crops[SMIAPP_PAD_SINK]->height,
2009 sel->r.height,
2010 sel->flags);
2012 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
2014 if (this > best) {
2015 scale_m = try[i];
2016 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
2017 best = this;
2020 if (SMIA_LIM(sensor, SCALING_CAPABILITY)
2021 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2022 continue;
2024 this = scaling_goodness(
2025 subdev, crops[SMIAPP_PAD_SINK]->width
2026 / try[i]
2027 * SMIA_LIM(sensor, SCALER_N_MIN),
2028 sel->r.width,
2029 crops[SMIAPP_PAD_SINK]->height
2030 / try[i]
2031 * SMIA_LIM(sensor, SCALER_N_MIN),
2032 sel->r.height,
2033 sel->flags);
2035 if (this > best) {
2036 scale_m = try[i];
2037 mode = SMIAPP_SCALING_MODE_BOTH;
2038 best = this;
2042 sel->r.width =
2043 (crops[SMIAPP_PAD_SINK]->width
2044 / scale_m
2045 * SMIA_LIM(sensor, SCALER_N_MIN)) & ~1;
2046 if (mode == SMIAPP_SCALING_MODE_BOTH)
2047 sel->r.height =
2048 (crops[SMIAPP_PAD_SINK]->height
2049 / scale_m
2050 * SMIA_LIM(sensor, SCALER_N_MIN))
2051 & ~1;
2052 else
2053 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
2055 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2056 sensor->scale_m = scale_m;
2057 sensor->scaling_mode = mode;
2060 /* We're only called on source pads. This function sets scaling. */
2061 static int smiapp_set_compose(struct v4l2_subdev *subdev,
2062 struct v4l2_subdev_pad_config *cfg,
2063 struct v4l2_subdev_selection *sel)
2065 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2066 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2067 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2069 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2071 sel->r.top = 0;
2072 sel->r.left = 0;
2074 if (ssd == sensor->binner)
2075 smiapp_set_compose_binner(subdev, cfg, sel, crops, comp);
2076 else
2077 smiapp_set_compose_scaler(subdev, cfg, sel, crops, comp);
2079 *comp = sel->r;
2080 smiapp_propagate(subdev, cfg, sel->which, V4L2_SEL_TGT_COMPOSE);
2082 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2083 return smiapp_pll_blanking_update(sensor);
2085 return 0;
2088 static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
2089 struct v4l2_subdev_selection *sel)
2091 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2092 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2094 /* We only implement crop in three places. */
2095 switch (sel->target) {
2096 case V4L2_SEL_TGT_CROP:
2097 case V4L2_SEL_TGT_CROP_BOUNDS:
2098 if (ssd == sensor->pixel_array
2099 && sel->pad == SMIAPP_PA_PAD_SRC)
2100 return 0;
2101 if (ssd == sensor->src
2102 && sel->pad == SMIAPP_PAD_SRC)
2103 return 0;
2104 if (ssd == sensor->scaler
2105 && sel->pad == SMIAPP_PAD_SINK
2106 && SMIA_LIM(sensor, DIGITAL_CROP_CAPABILITY)
2107 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
2108 return 0;
2109 return -EINVAL;
2110 case V4L2_SEL_TGT_NATIVE_SIZE:
2111 if (ssd == sensor->pixel_array
2112 && sel->pad == SMIAPP_PA_PAD_SRC)
2113 return 0;
2114 return -EINVAL;
2115 case V4L2_SEL_TGT_COMPOSE:
2116 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2117 if (sel->pad == ssd->source_pad)
2118 return -EINVAL;
2119 if (ssd == sensor->binner)
2120 return 0;
2121 if (ssd == sensor->scaler
2122 && SMIA_LIM(sensor, SCALING_CAPABILITY)
2123 != SMIAPP_SCALING_CAPABILITY_NONE)
2124 return 0;
2125 /* Fall through */
2126 default:
2127 return -EINVAL;
2131 static int smiapp_set_crop(struct v4l2_subdev *subdev,
2132 struct v4l2_subdev_pad_config *cfg,
2133 struct v4l2_subdev_selection *sel)
2135 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2136 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2137 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
2138 struct v4l2_rect _r;
2140 smiapp_get_crop_compose(subdev, cfg, crops, NULL, sel->which);
2142 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2143 if (sel->pad == ssd->sink_pad)
2144 src_size = &ssd->sink_fmt;
2145 else
2146 src_size = &ssd->compose;
2147 } else {
2148 if (sel->pad == ssd->sink_pad) {
2149 _r.left = 0;
2150 _r.top = 0;
2151 _r.width = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2152 ->width;
2153 _r.height = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2154 ->height;
2155 src_size = &_r;
2156 } else {
2157 src_size = v4l2_subdev_get_try_compose(
2158 subdev, cfg, ssd->sink_pad);
2162 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2163 sel->r.left = 0;
2164 sel->r.top = 0;
2167 sel->r.width = min(sel->r.width, src_size->width);
2168 sel->r.height = min(sel->r.height, src_size->height);
2170 sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
2171 sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
2173 *crops[sel->pad] = sel->r;
2175 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2176 smiapp_propagate(subdev, cfg, sel->which,
2177 V4L2_SEL_TGT_CROP);
2179 return 0;
2182 static void smiapp_get_native_size(struct smiapp_subdev *ssd,
2183 struct v4l2_rect *r)
2185 r->top = 0;
2186 r->left = 0;
2187 r->width = SMIA_LIM(ssd->sensor, X_ADDR_MAX) + 1;
2188 r->height = SMIA_LIM(ssd->sensor, Y_ADDR_MAX) + 1;
2191 static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2192 struct v4l2_subdev_pad_config *cfg,
2193 struct v4l2_subdev_selection *sel)
2195 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2196 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2197 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2198 struct v4l2_rect sink_fmt;
2199 int ret;
2201 ret = __smiapp_sel_supported(subdev, sel);
2202 if (ret)
2203 return ret;
2205 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2207 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2208 sink_fmt = ssd->sink_fmt;
2209 } else {
2210 struct v4l2_mbus_framefmt *fmt =
2211 v4l2_subdev_get_try_format(subdev, cfg, ssd->sink_pad);
2213 sink_fmt.left = 0;
2214 sink_fmt.top = 0;
2215 sink_fmt.width = fmt->width;
2216 sink_fmt.height = fmt->height;
2219 switch (sel->target) {
2220 case V4L2_SEL_TGT_CROP_BOUNDS:
2221 case V4L2_SEL_TGT_NATIVE_SIZE:
2222 if (ssd == sensor->pixel_array)
2223 smiapp_get_native_size(ssd, &sel->r);
2224 else if (sel->pad == ssd->sink_pad)
2225 sel->r = sink_fmt;
2226 else
2227 sel->r = *comp;
2228 break;
2229 case V4L2_SEL_TGT_CROP:
2230 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2231 sel->r = *crops[sel->pad];
2232 break;
2233 case V4L2_SEL_TGT_COMPOSE:
2234 sel->r = *comp;
2235 break;
2238 return 0;
2241 static int smiapp_get_selection(struct v4l2_subdev *subdev,
2242 struct v4l2_subdev_pad_config *cfg,
2243 struct v4l2_subdev_selection *sel)
2245 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2246 int rval;
2248 mutex_lock(&sensor->mutex);
2249 rval = __smiapp_get_selection(subdev, cfg, sel);
2250 mutex_unlock(&sensor->mutex);
2252 return rval;
2254 static int smiapp_set_selection(struct v4l2_subdev *subdev,
2255 struct v4l2_subdev_pad_config *cfg,
2256 struct v4l2_subdev_selection *sel)
2258 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2259 int ret;
2261 ret = __smiapp_sel_supported(subdev, sel);
2262 if (ret)
2263 return ret;
2265 mutex_lock(&sensor->mutex);
2267 sel->r.left = max(0, sel->r.left & ~1);
2268 sel->r.top = max(0, sel->r.top & ~1);
2269 sel->r.width = SMIAPP_ALIGN_DIM(sel->r.width, sel->flags);
2270 sel->r.height = SMIAPP_ALIGN_DIM(sel->r.height, sel->flags);
2272 sel->r.width = max_t(unsigned int,
2273 SMIA_LIM(sensor, MIN_X_OUTPUT_SIZE),
2274 sel->r.width);
2275 sel->r.height = max_t(unsigned int,
2276 SMIA_LIM(sensor, MIN_Y_OUTPUT_SIZE),
2277 sel->r.height);
2279 switch (sel->target) {
2280 case V4L2_SEL_TGT_CROP:
2281 ret = smiapp_set_crop(subdev, cfg, sel);
2282 break;
2283 case V4L2_SEL_TGT_COMPOSE:
2284 ret = smiapp_set_compose(subdev, cfg, sel);
2285 break;
2286 default:
2287 ret = -EINVAL;
2290 mutex_unlock(&sensor->mutex);
2291 return ret;
2294 static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2296 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2298 *frames = sensor->frame_skip;
2299 return 0;
2302 static int smiapp_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines)
2304 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2306 *lines = sensor->image_start;
2308 return 0;
2311 /* -----------------------------------------------------------------------------
2312 * sysfs attributes
2315 static ssize_t
2316 smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2317 char *buf)
2319 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2320 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2321 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2322 int rval;
2324 if (!sensor->dev_init_done)
2325 return -EBUSY;
2327 rval = smiapp_pm_get_init(sensor);
2328 if (rval < 0)
2329 return -ENODEV;
2331 rval = smiapp_read_nvm(sensor, buf, PAGE_SIZE);
2332 if (rval < 0) {
2333 pm_runtime_put(&client->dev);
2334 dev_err(&client->dev, "nvm read failed\n");
2335 return -ENODEV;
2338 pm_runtime_mark_last_busy(&client->dev);
2339 pm_runtime_put_autosuspend(&client->dev);
2342 * NVM is still way below a PAGE_SIZE, so we can safely
2343 * assume this for now.
2345 return rval;
2347 static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2349 static ssize_t
2350 smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2351 char *buf)
2353 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2354 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2355 struct smiapp_module_info *minfo = &sensor->minfo;
2357 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2358 minfo->manufacturer_id, minfo->model_id,
2359 minfo->revision_number_major) + 1;
2362 static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
2364 /* -----------------------------------------------------------------------------
2365 * V4L2 subdev core operations
2368 static int smiapp_identify_module(struct smiapp_sensor *sensor)
2370 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2371 struct smiapp_module_info *minfo = &sensor->minfo;
2372 unsigned int i;
2373 int rval = 0;
2375 minfo->name = SMIAPP_NAME;
2377 /* Module info */
2378 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2379 &minfo->manufacturer_id);
2380 if (!rval)
2381 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2382 &minfo->model_id);
2383 if (!rval)
2384 rval = smiapp_read_8only(sensor,
2385 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2386 &minfo->revision_number_major);
2387 if (!rval)
2388 rval = smiapp_read_8only(sensor,
2389 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2390 &minfo->revision_number_minor);
2391 if (!rval)
2392 rval = smiapp_read_8only(sensor,
2393 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2394 &minfo->module_year);
2395 if (!rval)
2396 rval = smiapp_read_8only(sensor,
2397 SMIAPP_REG_U8_MODULE_DATE_MONTH,
2398 &minfo->module_month);
2399 if (!rval)
2400 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2401 &minfo->module_day);
2403 /* Sensor info */
2404 if (!rval)
2405 rval = smiapp_read_8only(sensor,
2406 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2407 &minfo->sensor_manufacturer_id);
2408 if (!rval)
2409 rval = smiapp_read_8only(sensor,
2410 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2411 &minfo->sensor_model_id);
2412 if (!rval)
2413 rval = smiapp_read_8only(sensor,
2414 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2415 &minfo->sensor_revision_number);
2416 if (!rval)
2417 rval = smiapp_read_8only(sensor,
2418 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2419 &minfo->sensor_firmware_version);
2421 /* SMIA */
2422 if (!rval)
2423 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2424 &minfo->smia_version);
2425 if (!rval)
2426 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2427 &minfo->smiapp_version);
2429 if (rval) {
2430 dev_err(&client->dev, "sensor detection failed\n");
2431 return -ENODEV;
2434 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2435 minfo->manufacturer_id, minfo->model_id);
2437 dev_dbg(&client->dev,
2438 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2439 minfo->revision_number_major, minfo->revision_number_minor,
2440 minfo->module_year, minfo->module_month, minfo->module_day);
2442 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2443 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2445 dev_dbg(&client->dev,
2446 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2447 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2449 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2450 minfo->smia_version, minfo->smiapp_version);
2453 * Some modules have bad data in the lvalues below. Hope the
2454 * rvalues have better stuff. The lvalues are module
2455 * parameters whereas the rvalues are sensor parameters.
2457 if (!minfo->manufacturer_id && !minfo->model_id) {
2458 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2459 minfo->model_id = minfo->sensor_model_id;
2460 minfo->revision_number_major = minfo->sensor_revision_number;
2463 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2464 if (smiapp_module_idents[i].manufacturer_id
2465 != minfo->manufacturer_id)
2466 continue;
2467 if (smiapp_module_idents[i].model_id != minfo->model_id)
2468 continue;
2469 if (smiapp_module_idents[i].flags
2470 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2471 if (smiapp_module_idents[i].revision_number_major
2472 < minfo->revision_number_major)
2473 continue;
2474 } else {
2475 if (smiapp_module_idents[i].revision_number_major
2476 != minfo->revision_number_major)
2477 continue;
2480 minfo->name = smiapp_module_idents[i].name;
2481 minfo->quirk = smiapp_module_idents[i].quirk;
2482 break;
2485 if (i >= ARRAY_SIZE(smiapp_module_idents))
2486 dev_warn(&client->dev,
2487 "no quirks for this module; let's hope it's fully compliant\n");
2489 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2490 minfo->name, minfo->manufacturer_id, minfo->model_id,
2491 minfo->revision_number_major);
2493 return 0;
2496 static const struct v4l2_subdev_ops smiapp_ops;
2497 static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2498 static const struct media_entity_operations smiapp_entity_ops;
2500 static int smiapp_register_subdev(struct smiapp_sensor *sensor,
2501 struct smiapp_subdev *ssd,
2502 struct smiapp_subdev *sink_ssd,
2503 u16 source_pad, u16 sink_pad, u32 link_flags)
2505 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2506 int rval;
2508 if (!sink_ssd)
2509 return 0;
2511 rval = media_entity_pads_init(&ssd->sd.entity,
2512 ssd->npads, ssd->pads);
2513 if (rval) {
2514 dev_err(&client->dev,
2515 "media_entity_pads_init failed\n");
2516 return rval;
2519 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2520 &ssd->sd);
2521 if (rval) {
2522 dev_err(&client->dev,
2523 "v4l2_device_register_subdev failed\n");
2524 return rval;
2527 rval = media_create_pad_link(&ssd->sd.entity, source_pad,
2528 &sink_ssd->sd.entity, sink_pad,
2529 link_flags);
2530 if (rval) {
2531 dev_err(&client->dev,
2532 "media_create_pad_link failed\n");
2533 v4l2_device_unregister_subdev(&ssd->sd);
2534 return rval;
2537 return 0;
2540 static void smiapp_unregistered(struct v4l2_subdev *subdev)
2542 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2543 unsigned int i;
2545 for (i = 1; i < sensor->ssds_used; i++)
2546 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2549 static int smiapp_registered(struct v4l2_subdev *subdev)
2551 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2552 int rval;
2554 if (sensor->scaler) {
2555 rval = smiapp_register_subdev(
2556 sensor, sensor->binner, sensor->scaler,
2557 SMIAPP_PAD_SRC, SMIAPP_PAD_SINK,
2558 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2559 if (rval < 0)
2560 return rval;
2563 rval = smiapp_register_subdev(
2564 sensor, sensor->pixel_array, sensor->binner,
2565 SMIAPP_PA_PAD_SRC, SMIAPP_PAD_SINK,
2566 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2567 if (rval)
2568 goto out_err;
2570 return 0;
2572 out_err:
2573 smiapp_unregistered(subdev);
2575 return rval;
2578 static void smiapp_cleanup(struct smiapp_sensor *sensor)
2580 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2582 device_remove_file(&client->dev, &dev_attr_nvm);
2583 device_remove_file(&client->dev, &dev_attr_ident);
2585 smiapp_free_controls(sensor);
2588 static void smiapp_create_subdev(struct smiapp_sensor *sensor,
2589 struct smiapp_subdev *ssd, const char *name,
2590 unsigned short num_pads)
2592 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2594 if (!ssd)
2595 return;
2597 if (ssd != sensor->src)
2598 v4l2_subdev_init(&ssd->sd, &smiapp_ops);
2600 ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2601 ssd->sensor = sensor;
2603 ssd->npads = num_pads;
2604 ssd->source_pad = num_pads - 1;
2606 v4l2_i2c_subdev_set_name(&ssd->sd, client, sensor->minfo.name, name);
2608 smiapp_get_native_size(ssd, &ssd->sink_fmt);
2610 ssd->compose.width = ssd->sink_fmt.width;
2611 ssd->compose.height = ssd->sink_fmt.height;
2612 ssd->crop[ssd->source_pad] = ssd->compose;
2613 ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2614 if (ssd != sensor->pixel_array) {
2615 ssd->crop[ssd->sink_pad] = ssd->compose;
2616 ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK;
2619 ssd->sd.entity.ops = &smiapp_entity_ops;
2621 if (ssd == sensor->src)
2622 return;
2624 ssd->sd.internal_ops = &smiapp_internal_ops;
2625 ssd->sd.owner = THIS_MODULE;
2626 ssd->sd.dev = &client->dev;
2627 v4l2_set_subdevdata(&ssd->sd, client);
2630 static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2632 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2633 struct smiapp_sensor *sensor = ssd->sensor;
2634 unsigned int i;
2636 mutex_lock(&sensor->mutex);
2638 for (i = 0; i < ssd->npads; i++) {
2639 struct v4l2_mbus_framefmt *try_fmt =
2640 v4l2_subdev_get_try_format(sd, fh->pad, i);
2641 struct v4l2_rect *try_crop =
2642 v4l2_subdev_get_try_crop(sd, fh->pad, i);
2643 struct v4l2_rect *try_comp;
2645 smiapp_get_native_size(ssd, try_crop);
2647 try_fmt->width = try_crop->width;
2648 try_fmt->height = try_crop->height;
2649 try_fmt->code = sensor->internal_csi_format->code;
2650 try_fmt->field = V4L2_FIELD_NONE;
2652 if (ssd != sensor->pixel_array)
2653 continue;
2655 try_comp = v4l2_subdev_get_try_compose(sd, fh->pad, i);
2656 *try_comp = *try_crop;
2659 mutex_unlock(&sensor->mutex);
2661 return 0;
2664 static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2665 .s_stream = smiapp_set_stream,
2668 static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2669 .enum_mbus_code = smiapp_enum_mbus_code,
2670 .get_fmt = smiapp_get_format,
2671 .set_fmt = smiapp_set_format,
2672 .get_selection = smiapp_get_selection,
2673 .set_selection = smiapp_set_selection,
2676 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2677 .g_skip_frames = smiapp_get_skip_frames,
2678 .g_skip_top_lines = smiapp_get_skip_top_lines,
2681 static const struct v4l2_subdev_ops smiapp_ops = {
2682 .video = &smiapp_video_ops,
2683 .pad = &smiapp_pad_ops,
2684 .sensor = &smiapp_sensor_ops,
2687 static const struct media_entity_operations smiapp_entity_ops = {
2688 .link_validate = v4l2_subdev_link_validate,
2691 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2692 .registered = smiapp_registered,
2693 .unregistered = smiapp_unregistered,
2694 .open = smiapp_open,
2697 static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2698 .open = smiapp_open,
2701 /* -----------------------------------------------------------------------------
2702 * I2C Driver
2705 static int __maybe_unused smiapp_suspend(struct device *dev)
2707 struct i2c_client *client = to_i2c_client(dev);
2708 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2709 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2710 bool streaming = sensor->streaming;
2711 int rval;
2713 rval = pm_runtime_get_sync(dev);
2714 if (rval < 0) {
2715 if (rval != -EBUSY && rval != -EAGAIN)
2716 pm_runtime_set_active(&client->dev);
2717 pm_runtime_put(dev);
2718 return -EAGAIN;
2721 if (sensor->streaming)
2722 smiapp_stop_streaming(sensor);
2724 /* save state for resume */
2725 sensor->streaming = streaming;
2727 return 0;
2730 static int __maybe_unused smiapp_resume(struct device *dev)
2732 struct i2c_client *client = to_i2c_client(dev);
2733 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2734 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2735 int rval = 0;
2737 pm_runtime_put(dev);
2739 if (sensor->streaming)
2740 rval = smiapp_start_streaming(sensor);
2742 return rval;
2745 static struct smiapp_hwconfig *smiapp_get_hwconfig(struct device *dev)
2747 struct smiapp_hwconfig *hwcfg;
2748 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
2749 struct fwnode_handle *ep;
2750 struct fwnode_handle *fwnode = dev_fwnode(dev);
2751 u32 rotation;
2752 int i;
2753 int rval;
2755 if (!fwnode)
2756 return dev->platform_data;
2758 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
2759 if (!ep)
2760 return NULL;
2762 bus_cfg.bus_type = V4L2_MBUS_CSI2_DPHY;
2763 rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
2764 if (rval == -ENXIO) {
2765 bus_cfg = (struct v4l2_fwnode_endpoint)
2766 { .bus_type = V4L2_MBUS_CCP2 };
2767 rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
2769 if (rval)
2770 goto out_err;
2772 hwcfg = devm_kzalloc(dev, sizeof(*hwcfg), GFP_KERNEL);
2773 if (!hwcfg)
2774 goto out_err;
2776 switch (bus_cfg.bus_type) {
2777 case V4L2_MBUS_CSI2_DPHY:
2778 hwcfg->csi_signalling_mode = SMIAPP_CSI_SIGNALLING_MODE_CSI2;
2779 hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
2780 break;
2781 case V4L2_MBUS_CCP2:
2782 hwcfg->csi_signalling_mode = (bus_cfg.bus.mipi_csi1.strobe) ?
2783 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE :
2784 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK;
2785 hwcfg->lanes = 1;
2786 break;
2787 default:
2788 dev_err(dev, "unsupported bus %u\n", bus_cfg.bus_type);
2789 goto out_err;
2792 dev_dbg(dev, "lanes %u\n", hwcfg->lanes);
2794 rval = fwnode_property_read_u32(fwnode, "rotation", &rotation);
2795 if (!rval) {
2796 switch (rotation) {
2797 case 180:
2798 hwcfg->module_board_orient =
2799 SMIAPP_MODULE_BOARD_ORIENT_180;
2800 /* Fall through */
2801 case 0:
2802 break;
2803 default:
2804 dev_err(dev, "invalid rotation %u\n", rotation);
2805 goto out_err;
2809 rval = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
2810 &hwcfg->ext_clk);
2811 if (rval)
2812 dev_info(dev, "can't get clock-frequency\n");
2814 dev_dbg(dev, "clk %d, mode %d\n", hwcfg->ext_clk,
2815 hwcfg->csi_signalling_mode);
2817 if (!bus_cfg.nr_of_link_frequencies) {
2818 dev_warn(dev, "no link frequencies defined\n");
2819 goto out_err;
2822 hwcfg->op_sys_clock = devm_kcalloc(
2823 dev, bus_cfg.nr_of_link_frequencies + 1 /* guardian */,
2824 sizeof(*hwcfg->op_sys_clock), GFP_KERNEL);
2825 if (!hwcfg->op_sys_clock)
2826 goto out_err;
2828 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
2829 hwcfg->op_sys_clock[i] = bus_cfg.link_frequencies[i];
2830 dev_dbg(dev, "freq %d: %lld\n", i, hwcfg->op_sys_clock[i]);
2833 v4l2_fwnode_endpoint_free(&bus_cfg);
2834 fwnode_handle_put(ep);
2835 return hwcfg;
2837 out_err:
2838 v4l2_fwnode_endpoint_free(&bus_cfg);
2839 fwnode_handle_put(ep);
2840 return NULL;
2843 static int smiapp_probe(struct i2c_client *client)
2845 struct smiapp_sensor *sensor;
2846 struct smiapp_hwconfig *hwcfg = smiapp_get_hwconfig(&client->dev);
2847 unsigned int i;
2848 int rval;
2850 if (hwcfg == NULL)
2851 return -ENODEV;
2853 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
2854 if (sensor == NULL)
2855 return -ENOMEM;
2857 sensor->hwcfg = hwcfg;
2858 sensor->src = &sensor->ssds[sensor->ssds_used];
2860 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2861 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2863 sensor->vana = devm_regulator_get(&client->dev, "vana");
2864 if (IS_ERR(sensor->vana)) {
2865 dev_err(&client->dev, "could not get regulator for vana\n");
2866 return PTR_ERR(sensor->vana);
2869 sensor->ext_clk = devm_clk_get(&client->dev, NULL);
2870 if (PTR_ERR(sensor->ext_clk) == -ENOENT) {
2871 dev_info(&client->dev, "no clock defined, continuing...\n");
2872 sensor->ext_clk = NULL;
2873 } else if (IS_ERR(sensor->ext_clk)) {
2874 dev_err(&client->dev, "could not get clock (%ld)\n",
2875 PTR_ERR(sensor->ext_clk));
2876 return -EPROBE_DEFER;
2879 if (sensor->ext_clk) {
2880 if (sensor->hwcfg->ext_clk) {
2881 unsigned long rate;
2883 rval = clk_set_rate(sensor->ext_clk,
2884 sensor->hwcfg->ext_clk);
2885 if (rval < 0) {
2886 dev_err(&client->dev,
2887 "unable to set clock freq to %u\n",
2888 sensor->hwcfg->ext_clk);
2889 return rval;
2892 rate = clk_get_rate(sensor->ext_clk);
2893 if (rate != sensor->hwcfg->ext_clk) {
2894 dev_err(&client->dev,
2895 "can't set clock freq, asked for %u but got %lu\n",
2896 sensor->hwcfg->ext_clk, rate);
2897 return rval;
2899 } else {
2900 sensor->hwcfg->ext_clk = clk_get_rate(sensor->ext_clk);
2901 dev_dbg(&client->dev, "obtained clock freq %u\n",
2902 sensor->hwcfg->ext_clk);
2904 } else if (sensor->hwcfg->ext_clk) {
2905 dev_dbg(&client->dev, "assuming clock freq %u\n",
2906 sensor->hwcfg->ext_clk);
2907 } else {
2908 dev_err(&client->dev, "unable to obtain clock freq\n");
2909 return -EINVAL;
2912 sensor->xshutdown = devm_gpiod_get_optional(&client->dev, "xshutdown",
2913 GPIOD_OUT_LOW);
2914 if (IS_ERR(sensor->xshutdown))
2915 return PTR_ERR(sensor->xshutdown);
2917 rval = smiapp_power_on(&client->dev);
2918 if (rval < 0)
2919 return rval;
2921 mutex_init(&sensor->mutex);
2923 rval = smiapp_identify_module(sensor);
2924 if (rval) {
2925 rval = -ENODEV;
2926 goto out_power_off;
2929 rval = smiapp_read_all_smia_limits(sensor);
2930 if (rval) {
2931 rval = -ENODEV;
2932 goto out_power_off;
2935 rval = smiapp_read_frame_fmt(sensor);
2936 if (rval) {
2937 rval = -ENODEV;
2938 goto out_power_off;
2942 * Handle Sensor Module orientation on the board.
2944 * The application of H-FLIP and V-FLIP on the sensor is modified by
2945 * the sensor orientation on the board.
2947 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2948 * both H-FLIP and V-FLIP for normal operation which also implies
2949 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2950 * controls will need to be internally inverted.
2952 * Rotation also changes the bayer pattern.
2954 if (sensor->hwcfg->module_board_orient ==
2955 SMIAPP_MODULE_BOARD_ORIENT_180)
2956 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2957 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2959 rval = smiapp_call_quirk(sensor, limits);
2960 if (rval) {
2961 dev_err(&client->dev, "limits quirks failed\n");
2962 goto out_power_off;
2965 if (SMIA_LIM(sensor, BINNING_CAPABILITY)) {
2966 u32 val;
2968 rval = smiapp_read(sensor,
2969 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2970 if (rval < 0) {
2971 rval = -ENODEV;
2972 goto out_power_off;
2974 sensor->nbinning_subtypes = min_t(u8, val,
2975 SMIAPP_BINNING_SUBTYPES);
2977 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2978 rval = smiapp_read(
2979 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
2980 if (rval < 0) {
2981 rval = -ENODEV;
2982 goto out_power_off;
2984 sensor->binning_subtypes[i] =
2985 *(struct smiapp_binning_subtype *)&val;
2987 dev_dbg(&client->dev, "binning %xx%x\n",
2988 sensor->binning_subtypes[i].horizontal,
2989 sensor->binning_subtypes[i].vertical);
2992 sensor->binning_horizontal = 1;
2993 sensor->binning_vertical = 1;
2995 if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
2996 dev_err(&client->dev, "sysfs ident entry creation failed\n");
2997 rval = -ENOENT;
2998 goto out_power_off;
3001 if (sensor->minfo.smiapp_version &&
3002 SMIA_LIM(sensor, DATA_TRANSFER_IF_CAPABILITY) &
3003 SMIAPP_DATA_TRANSFER_IF_CAPABILITY_SUPPORTED) {
3004 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
3005 dev_err(&client->dev, "sysfs nvm entry failed\n");
3006 rval = -EBUSY;
3007 goto out_cleanup;
3011 /* We consider this as profile 0 sensor if any of these are zero. */
3012 if (!SMIA_LIM(sensor, MIN_OP_SYS_CLK_DIV) ||
3013 !SMIA_LIM(sensor, MAX_OP_SYS_CLK_DIV) ||
3014 !SMIA_LIM(sensor, MIN_OP_PIX_CLK_DIV) ||
3015 !SMIA_LIM(sensor, MAX_OP_PIX_CLK_DIV)) {
3016 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
3017 } else if (SMIA_LIM(sensor, SCALING_CAPABILITY)
3018 != SMIAPP_SCALING_CAPABILITY_NONE) {
3019 if (SMIA_LIM(sensor, SCALING_CAPABILITY)
3020 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
3021 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
3022 else
3023 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
3024 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3025 sensor->ssds_used++;
3026 } else if (SMIA_LIM(sensor, DIGITAL_CROP_CAPABILITY)
3027 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
3028 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3029 sensor->ssds_used++;
3031 sensor->binner = &sensor->ssds[sensor->ssds_used];
3032 sensor->ssds_used++;
3033 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
3034 sensor->ssds_used++;
3036 sensor->scale_m = SMIA_LIM(sensor, SCALER_N_MIN);
3038 /* prepare PLL configuration input values */
3039 sensor->pll.bus_type = SMIAPP_PLL_BUS_TYPE_CSI2;
3040 sensor->pll.csi2.lanes = sensor->hwcfg->lanes;
3041 sensor->pll.ext_clk_freq_hz = sensor->hwcfg->ext_clk;
3042 sensor->pll.scale_n = SMIA_LIM(sensor, SCALER_N_MIN);
3043 /* Profile 0 sensors have no separate OP clock branch. */
3044 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
3045 sensor->pll.flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
3047 smiapp_create_subdev(sensor, sensor->scaler, " scaler", 2);
3048 smiapp_create_subdev(sensor, sensor->binner, " binner", 2);
3049 smiapp_create_subdev(sensor, sensor->pixel_array, " pixel_array", 1);
3051 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
3053 sensor->pixel_array->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3055 rval = smiapp_init_controls(sensor);
3056 if (rval < 0)
3057 goto out_cleanup;
3059 rval = smiapp_call_quirk(sensor, init);
3060 if (rval)
3061 goto out_cleanup;
3063 rval = smiapp_get_mbus_formats(sensor);
3064 if (rval) {
3065 rval = -ENODEV;
3066 goto out_cleanup;
3069 rval = smiapp_init_late_controls(sensor);
3070 if (rval) {
3071 rval = -ENODEV;
3072 goto out_cleanup;
3075 mutex_lock(&sensor->mutex);
3076 rval = smiapp_pll_blanking_update(sensor);
3077 mutex_unlock(&sensor->mutex);
3078 if (rval) {
3079 dev_err(&client->dev, "update mode failed\n");
3080 goto out_cleanup;
3083 sensor->streaming = false;
3084 sensor->dev_init_done = true;
3086 rval = media_entity_pads_init(&sensor->src->sd.entity, 2,
3087 sensor->src->pads);
3088 if (rval < 0)
3089 goto out_media_entity_cleanup;
3091 pm_runtime_set_active(&client->dev);
3092 pm_runtime_get_noresume(&client->dev);
3093 pm_runtime_enable(&client->dev);
3095 rval = v4l2_async_register_subdev_sensor_common(&sensor->src->sd);
3096 if (rval < 0)
3097 goto out_disable_runtime_pm;
3099 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
3100 pm_runtime_use_autosuspend(&client->dev);
3101 pm_runtime_put_autosuspend(&client->dev);
3103 return 0;
3105 out_disable_runtime_pm:
3106 pm_runtime_disable(&client->dev);
3108 out_media_entity_cleanup:
3109 media_entity_cleanup(&sensor->src->sd.entity);
3111 out_cleanup:
3112 smiapp_cleanup(sensor);
3114 out_power_off:
3115 smiapp_power_off(&client->dev);
3116 mutex_destroy(&sensor->mutex);
3118 return rval;
3121 static int smiapp_remove(struct i2c_client *client)
3123 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3124 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
3125 unsigned int i;
3127 v4l2_async_unregister_subdev(subdev);
3129 pm_runtime_disable(&client->dev);
3130 if (!pm_runtime_status_suspended(&client->dev))
3131 smiapp_power_off(&client->dev);
3132 pm_runtime_set_suspended(&client->dev);
3134 for (i = 0; i < sensor->ssds_used; i++) {
3135 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
3136 media_entity_cleanup(&sensor->ssds[i].sd.entity);
3138 smiapp_cleanup(sensor);
3139 mutex_destroy(&sensor->mutex);
3141 return 0;
3144 static const struct of_device_id smiapp_of_table[] = {
3145 { .compatible = "nokia,smia" },
3146 { },
3148 MODULE_DEVICE_TABLE(of, smiapp_of_table);
3150 static const struct i2c_device_id smiapp_id_table[] = {
3151 { SMIAPP_NAME, 0 },
3152 { },
3154 MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
3156 static const struct dev_pm_ops smiapp_pm_ops = {
3157 SET_SYSTEM_SLEEP_PM_OPS(smiapp_suspend, smiapp_resume)
3158 SET_RUNTIME_PM_OPS(smiapp_power_off, smiapp_power_on, NULL)
3161 static struct i2c_driver smiapp_i2c_driver = {
3162 .driver = {
3163 .of_match_table = smiapp_of_table,
3164 .name = SMIAPP_NAME,
3165 .pm = &smiapp_pm_ops,
3167 .probe_new = smiapp_probe,
3168 .remove = smiapp_remove,
3169 .id_table = smiapp_id_table,
3172 module_i2c_driver(smiapp_i2c_driver);
3174 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3175 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3176 MODULE_LICENSE("GPL v2");