2 * drivers/media/video/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@maxwell.research.nokia.com>
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.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29 #include <linux/clk.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/gpio.h>
33 #include <linux/module.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/slab.h>
36 #include <linux/v4l2-mediabus.h>
37 #include <media/v4l2-device.h>
41 #define SMIAPP_ALIGN_DIM(dim, flags) \
42 ((flags) & V4L2_SUBDEV_SEL_FLAG_SIZE_GE \
47 * smiapp_module_idents - supported camera modules
49 static const struct smiapp_module_ident smiapp_module_idents[] = {
50 SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
51 SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
52 SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
53 SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
54 SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
55 SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk),
56 SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
57 SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
58 SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk),
59 SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk),
60 SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk),
65 * Dynamic Capability Identification
69 static int smiapp_read_frame_fmt(struct smiapp_sensor *sensor)
71 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
72 u32 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc;
76 int embedded_start = -1, embedded_end = -1;
79 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
84 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
89 ncol_desc = (fmt_model_subtype
90 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK)
91 >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT;
92 nrow_desc = fmt_model_subtype
93 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK;
95 dev_dbg(&client->dev, "format_model_type %s\n",
96 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
98 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
99 ? "4 byte" : "is simply bad");
101 for (i = 0; i < ncol_desc + nrow_desc; i++) {
108 if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) {
111 SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i),
118 & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK)
119 >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT;
120 pixels = desc & SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK;
121 } else if (fmt_model_type
122 == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE) {
125 SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i),
132 & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK)
133 >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT;
134 pixels = desc & SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK;
136 dev_dbg(&client->dev,
137 "invalid frame format model type %d\n",
148 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
151 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY:
154 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK:
157 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK:
160 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
165 dev_dbg(&client->dev, "pixelcode %d\n", pixelcode);
169 dev_dbg(&client->dev, "%s pixels: %d %s\n",
170 what, pixels, which);
175 /* Handle row descriptors */
177 == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED) {
178 embedded_start = line_count;
180 if (pixelcode == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
181 || pixels >= sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES] / 2)
182 image_start = line_count;
183 if (embedded_start != -1 && embedded_end == -1)
184 embedded_end = line_count;
186 line_count += pixels;
189 if (embedded_start == -1 || embedded_end == -1) {
194 dev_dbg(&client->dev, "embedded data from lines %d to %d\n",
195 embedded_start, embedded_end);
196 dev_dbg(&client->dev, "image data starts at line %d\n", image_start);
201 static int smiapp_pll_configure(struct smiapp_sensor *sensor)
203 struct smiapp_pll *pll = &sensor->pll;
207 sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt_pix_clk_div);
212 sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt_sys_clk_div);
217 sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
222 sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
226 /* Lane op clock ratio does not apply here. */
228 sensor, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS,
229 DIV_ROUND_UP(pll->op_sys_clk_freq_hz, 1000000 / 256 / 256));
230 if (rval < 0 || sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
234 sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op_pix_clk_div);
239 sensor, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op_sys_clk_div);
242 static int smiapp_pll_update(struct smiapp_sensor *sensor)
244 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
245 struct smiapp_pll_limits lim = {
246 .min_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV],
247 .max_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV],
248 .min_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ],
249 .max_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ],
250 .min_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER],
251 .max_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER],
252 .min_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ],
253 .max_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ],
255 .min_op_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV],
256 .max_op_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV],
257 .min_op_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV],
258 .max_op_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV],
259 .min_op_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ],
260 .max_op_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ],
261 .min_op_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ],
262 .max_op_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ],
264 .min_vt_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV],
265 .max_vt_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV],
266 .min_vt_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV],
267 .max_vt_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV],
268 .min_vt_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ],
269 .max_vt_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ],
270 .min_vt_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ],
271 .max_vt_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ],
273 .min_line_length_pck_bin = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN],
274 .min_line_length_pck = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK],
276 struct smiapp_pll *pll = &sensor->pll;
279 memset(&sensor->pll, 0, sizeof(sensor->pll));
281 pll->lanes = sensor->platform_data->lanes;
282 pll->ext_clk_freq_hz = sensor->platform_data->ext_clk;
284 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0) {
286 * Fill in operational clock divisors limits from the
287 * video timing ones. On profile 0 sensors the
288 * requirements regarding them are essentially the
289 * same as on VT ones.
291 lim.min_op_sys_clk_div = lim.min_vt_sys_clk_div;
292 lim.max_op_sys_clk_div = lim.max_vt_sys_clk_div;
293 lim.min_op_pix_clk_div = lim.min_vt_pix_clk_div;
294 lim.max_op_pix_clk_div = lim.max_vt_pix_clk_div;
295 lim.min_op_sys_clk_freq_hz = lim.min_vt_sys_clk_freq_hz;
296 lim.max_op_sys_clk_freq_hz = lim.max_vt_sys_clk_freq_hz;
297 lim.min_op_pix_clk_freq_hz = lim.min_vt_pix_clk_freq_hz;
298 lim.max_op_pix_clk_freq_hz = lim.max_vt_pix_clk_freq_hz;
299 /* Profile 0 sensors have no separate OP clock branch. */
300 pll->flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
303 if (smiapp_needs_quirk(sensor,
304 SMIAPP_QUIRK_FLAG_OP_PIX_CLOCK_PER_LANE))
305 pll->flags |= SMIAPP_PLL_FLAG_OP_PIX_CLOCK_PER_LANE;
307 pll->binning_horizontal = sensor->binning_horizontal;
308 pll->binning_vertical = sensor->binning_vertical;
310 sensor->link_freq->qmenu_int[sensor->link_freq->val];
311 pll->scale_m = sensor->scale_m;
312 pll->scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
313 pll->bits_per_pixel = sensor->csi_format->compressed;
315 rval = smiapp_pll_calculate(&client->dev, &lim, pll);
319 sensor->pixel_rate_parray->cur.val64 = pll->vt_pix_clk_freq_hz;
320 sensor->pixel_rate_csi->cur.val64 = pll->pixel_rate_csi;
328 * V4L2 Controls handling
332 static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
334 struct v4l2_ctrl *ctrl = sensor->exposure;
337 max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
338 + sensor->vblank->val
339 - sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN];
342 if (ctrl->default_value > max)
343 ctrl->default_value = max;
346 if (ctrl->cur.val > max)
353 * 1. Bits-per-pixel, descending.
354 * 2. Bits-per-pixel compressed, descending.
355 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
356 * orders must be defined.
358 static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
359 { V4L2_MBUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
360 { V4L2_MBUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
361 { V4L2_MBUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
362 { V4L2_MBUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
363 { V4L2_MBUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
364 { V4L2_MBUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
365 { V4L2_MBUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
366 { V4L2_MBUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
367 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
368 { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
369 { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
370 { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
371 { V4L2_MBUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, },
372 { V4L2_MBUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, },
373 { V4L2_MBUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, },
374 { V4L2_MBUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, },
377 const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
379 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
380 - (unsigned long)smiapp_csi_data_formats) \
381 / sizeof(*smiapp_csi_data_formats))
383 static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
385 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
389 if (sensor->hflip->val)
390 flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
392 if (sensor->vflip->val)
393 flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
396 flip ^= sensor->hvflip_inv_mask;
398 dev_dbg(&client->dev, "flip %d\n", flip);
399 return sensor->default_pixel_order ^ flip;
402 static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
404 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
405 unsigned int csi_format_idx =
406 to_csi_format_idx(sensor->csi_format) & ~3;
407 unsigned int internal_csi_format_idx =
408 to_csi_format_idx(sensor->internal_csi_format) & ~3;
409 unsigned int pixel_order = smiapp_pixel_order(sensor);
411 sensor->mbus_frame_fmts =
412 sensor->default_mbus_frame_fmts << pixel_order;
414 &smiapp_csi_data_formats[csi_format_idx + pixel_order];
415 sensor->internal_csi_format =
416 &smiapp_csi_data_formats[internal_csi_format_idx
419 BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
420 >= ARRAY_SIZE(smiapp_csi_data_formats));
421 BUG_ON(min(internal_csi_format_idx, csi_format_idx) < 0);
423 dev_dbg(&client->dev, "new pixel order %s\n",
424 pixel_order_str[pixel_order]);
427 static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
429 struct smiapp_sensor *sensor =
430 container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
437 case V4L2_CID_ANALOGUE_GAIN:
440 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
442 case V4L2_CID_EXPOSURE:
445 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
449 if (sensor->streaming)
452 if (sensor->hflip->val)
453 orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
455 if (sensor->vflip->val)
456 orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
458 orient ^= sensor->hvflip_inv_mask;
459 rval = smiapp_write(sensor,
460 SMIAPP_REG_U8_IMAGE_ORIENTATION,
465 smiapp_update_mbus_formats(sensor);
469 case V4L2_CID_VBLANK:
470 exposure = sensor->exposure->val;
472 __smiapp_update_exposure_limits(sensor);
474 if (exposure > sensor->exposure->maximum) {
475 sensor->exposure->val =
476 sensor->exposure->maximum;
477 rval = smiapp_set_ctrl(
484 sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
485 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
488 case V4L2_CID_HBLANK:
490 sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
491 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
494 case V4L2_CID_LINK_FREQ:
495 if (sensor->streaming)
498 return smiapp_pll_update(sensor);
505 static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
506 .s_ctrl = smiapp_set_ctrl,
509 static int smiapp_init_controls(struct smiapp_sensor *sensor)
511 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
515 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 7);
518 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
520 sensor->analog_gain = v4l2_ctrl_new_std(
521 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
522 V4L2_CID_ANALOGUE_GAIN,
523 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN],
524 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX],
525 max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U),
526 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]);
528 /* Exposure limits will be updated soon, use just something here. */
529 sensor->exposure = v4l2_ctrl_new_std(
530 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
531 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
533 sensor->hflip = v4l2_ctrl_new_std(
534 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
535 V4L2_CID_HFLIP, 0, 1, 1, 0);
536 sensor->vflip = v4l2_ctrl_new_std(
537 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
538 V4L2_CID_VFLIP, 0, 1, 1, 0);
540 sensor->vblank = v4l2_ctrl_new_std(
541 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
542 V4L2_CID_VBLANK, 0, 1, 1, 0);
545 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
547 sensor->hblank = v4l2_ctrl_new_std(
548 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
549 V4L2_CID_HBLANK, 0, 1, 1, 0);
552 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
554 sensor->pixel_rate_parray = v4l2_ctrl_new_std(
555 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
556 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
558 if (sensor->pixel_array->ctrl_handler.error) {
559 dev_err(&client->dev,
560 "pixel array controls initialization failed (%d)\n",
561 sensor->pixel_array->ctrl_handler.error);
562 rval = sensor->pixel_array->ctrl_handler.error;
566 sensor->pixel_array->sd.ctrl_handler =
567 &sensor->pixel_array->ctrl_handler;
569 v4l2_ctrl_cluster(2, &sensor->hflip);
571 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
574 sensor->src->ctrl_handler.lock = &sensor->mutex;
576 for (max = 0; sensor->platform_data->op_sys_clock[max + 1]; max++);
578 sensor->link_freq = v4l2_ctrl_new_int_menu(
579 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
580 V4L2_CID_LINK_FREQ, max, 0,
581 sensor->platform_data->op_sys_clock);
583 sensor->pixel_rate_csi = v4l2_ctrl_new_std(
584 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
585 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
587 if (sensor->src->ctrl_handler.error) {
588 dev_err(&client->dev,
589 "src controls initialization failed (%d)\n",
590 sensor->src->ctrl_handler.error);
591 rval = sensor->src->ctrl_handler.error;
595 sensor->src->sd.ctrl_handler =
596 &sensor->src->ctrl_handler;
601 v4l2_ctrl_handler_free(&sensor->pixel_array->ctrl_handler);
602 v4l2_ctrl_handler_free(&sensor->src->ctrl_handler);
607 static void smiapp_free_controls(struct smiapp_sensor *sensor)
611 for (i = 0; i < sensor->ssds_used; i++)
612 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
615 static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
618 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
623 for (i = 0; i < n; i++) {
625 sensor, smiapp_reg_limits[limit[i]].addr, &val);
628 sensor->limits[limit[i]] = val;
629 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %d, 0x%x\n",
630 smiapp_reg_limits[limit[i]].addr,
631 smiapp_reg_limits[limit[i]].what, val, val);
637 static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
642 for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
643 rval = smiapp_get_limits(sensor, &i, 1);
648 if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
649 smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
654 static int smiapp_get_limits_binning(struct smiapp_sensor *sensor)
656 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
657 static u32 const limits[] = {
658 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN,
659 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN,
660 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN,
661 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN,
662 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN,
663 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN,
664 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN,
666 static u32 const limits_replace[] = {
667 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES,
668 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES,
669 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK,
670 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK,
671 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK,
672 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN,
673 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN,
678 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
679 SMIAPP_BINNING_CAPABILITY_NO) {
680 for (i = 0; i < ARRAY_SIZE(limits); i++)
681 sensor->limits[limits[i]] =
682 sensor->limits[limits_replace[i]];
687 rval = smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits));
692 * Sanity check whether the binning limits are valid. If not,
693 * use the non-binning ones.
695 if (sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN]
696 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN]
697 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN])
700 for (i = 0; i < ARRAY_SIZE(limits); i++) {
701 dev_dbg(&client->dev,
702 "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
703 smiapp_reg_limits[limits[i]].addr,
704 smiapp_reg_limits[limits[i]].what,
705 sensor->limits[limits_replace[i]],
706 sensor->limits[limits_replace[i]]);
707 sensor->limits[limits[i]] =
708 sensor->limits[limits_replace[i]];
714 static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
716 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
717 unsigned int type, n;
718 unsigned int i, pixel_order;
722 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
726 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
728 rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
733 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
734 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
738 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
739 pixel_order_str[pixel_order]);
742 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
743 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
745 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
746 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
752 sensor->default_pixel_order = pixel_order;
753 sensor->mbus_frame_fmts = 0;
755 for (i = 0; i < n; i++) {
760 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
764 dev_dbg(&client->dev, "bpp %d, compressed %d\n",
767 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
768 const struct smiapp_csi_data_format *f =
769 &smiapp_csi_data_formats[j];
771 if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
774 if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
777 dev_dbg(&client->dev, "jolly good! %d\n", j);
779 sensor->default_mbus_frame_fmts |= 1 << j;
780 if (!sensor->csi_format) {
781 sensor->csi_format = f;
782 sensor->internal_csi_format = f;
787 if (!sensor->csi_format) {
788 dev_err(&client->dev, "no supported mbus code found\n");
792 smiapp_update_mbus_formats(sensor);
797 static void smiapp_update_blanking(struct smiapp_sensor *sensor)
799 struct v4l2_ctrl *vblank = sensor->vblank;
800 struct v4l2_ctrl *hblank = sensor->hblank;
804 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
805 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
806 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
808 sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
809 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
811 vblank->val = clamp_t(int, vblank->val,
812 vblank->minimum, vblank->maximum);
813 vblank->default_value = vblank->minimum;
814 vblank->val = vblank->val;
815 vblank->cur.val = vblank->val;
819 sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
820 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
821 sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
823 sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
824 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
826 hblank->val = clamp_t(int, hblank->val,
827 hblank->minimum, hblank->maximum);
828 hblank->default_value = hblank->minimum;
829 hblank->val = hblank->val;
830 hblank->cur.val = hblank->val;
832 __smiapp_update_exposure_limits(sensor);
835 static int smiapp_update_mode(struct smiapp_sensor *sensor)
837 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
838 unsigned int binning_mode;
841 dev_dbg(&client->dev, "frame size: %dx%d\n",
842 sensor->src->crop[SMIAPP_PAD_SRC].width,
843 sensor->src->crop[SMIAPP_PAD_SRC].height);
844 dev_dbg(&client->dev, "csi format width: %d\n",
845 sensor->csi_format->width);
847 /* Binning has to be set up here; it affects limits */
848 if (sensor->binning_horizontal == 1 &&
849 sensor->binning_vertical == 1) {
853 (sensor->binning_horizontal << 4)
854 | sensor->binning_vertical;
857 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
863 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
867 /* Get updated limits due to binning */
868 rval = smiapp_get_limits_binning(sensor);
872 rval = smiapp_pll_update(sensor);
876 /* Output from pixel array, including blanking */
877 smiapp_update_blanking(sensor);
879 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
880 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
882 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
883 sensor->pll.vt_pix_clk_freq_hz /
884 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
885 + sensor->hblank->val) *
886 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
887 + sensor->vblank->val) / 100));
894 * SMIA++ NVM handling
897 static int smiapp_read_nvm(struct smiapp_sensor *sensor,
903 np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
904 for (p = 0; p < np; p++) {
907 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
911 rval = smiapp_write(sensor,
912 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
913 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
914 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
918 for (i = 0; i < 1000; i++) {
921 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);
926 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
936 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
939 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
949 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
958 * SMIA++ CCI address control
961 static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
963 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
967 client->addr = sensor->platform_data->i2c_addr_dfl;
969 rval = smiapp_write(sensor,
970 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
971 sensor->platform_data->i2c_addr_alt << 1);
975 client->addr = sensor->platform_data->i2c_addr_alt;
977 /* verify addr change went ok */
978 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
982 if (val != sensor->platform_data->i2c_addr_alt << 1)
990 * SMIA++ Mode Control
993 static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
995 struct smiapp_flash_strobe_parms *strobe_setup;
996 unsigned int ext_freq = sensor->platform_data->ext_clk;
998 u32 strobe_adjustment;
999 u32 strobe_width_high_rs;
1002 strobe_setup = sensor->platform_data->strobe_setup;
1005 * How to calculate registers related to strobe length. Please
1006 * do not change, or if you do at least know what you're
1009 * Sakari Ailus <sakari.ailus@maxwell.research.nokia.com> 2010-10-25
1011 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1012 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1014 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1015 * flash_strobe_adjustment E N, [1 - 0xff]
1017 * The formula above is written as below to keep it on one
1020 * l / 10^6 = w / e * a
1022 * Let's mark w * a by x:
1030 * The strobe width must be at least as long as requested,
1031 * thus rounding upwards is needed.
1033 * x = (l * e + 10^6 - 1) / 10^6
1034 * -----------------------------
1036 * Maximum possible accuracy is wanted at all times. Thus keep
1037 * a as small as possible.
1039 * Calculate a, assuming maximum w, with rounding upwards:
1041 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1042 * -------------------------------------
1044 * Thus, we also get w, with that a, with rounding upwards:
1046 * w = (x + a - 1) / a
1047 * -------------------
1051 * x E [1, (2^16 - 1) * (2^8 - 1)]
1053 * Substituting maximum x to the original formula (with rounding),
1054 * the maximum l is thus
1056 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1058 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1059 * --------------------------------------------------
1061 * flash_strobe_length must be clamped between 1 and
1062 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1066 * flash_strobe_adjustment = ((flash_strobe_length *
1067 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1069 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1070 * EXTCLK freq + 10^6 - 1) / 10^6 +
1071 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1073 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1074 1000000 + 1, ext_freq);
1075 strobe_setup->strobe_width_high_us =
1076 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1078 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1079 1000000 - 1), 1000000ULL);
1080 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1081 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1084 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
1085 strobe_setup->mode);
1089 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1094 rval = smiapp_write(
1095 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1096 strobe_width_high_rs);
1100 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1101 strobe_setup->strobe_delay);
1105 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1106 strobe_setup->stobe_start_point);
1110 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1111 strobe_setup->trigger);
1114 sensor->platform_data->strobe_setup->trigger = 0;
1119 /* -----------------------------------------------------------------------------
1123 static int smiapp_power_on(struct smiapp_sensor *sensor)
1125 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1129 rval = regulator_enable(sensor->vana);
1131 dev_err(&client->dev, "failed to enable vana regulator\n");
1134 usleep_range(1000, 1000);
1136 if (sensor->platform_data->set_xclk)
1137 rval = sensor->platform_data->set_xclk(
1138 &sensor->src->sd, sensor->platform_data->ext_clk);
1140 rval = clk_enable(sensor->ext_clk);
1142 dev_dbg(&client->dev, "failed to set xclk\n");
1145 usleep_range(1000, 1000);
1147 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1148 gpio_set_value(sensor->platform_data->xshutdown, 1);
1150 sleep = SMIAPP_RESET_DELAY(sensor->platform_data->ext_clk);
1151 usleep_range(sleep, sleep);
1154 * Failures to respond to the address change command have been noticed.
1155 * Those failures seem to be caused by the sensor requiring a longer
1156 * boot time than advertised. An additional 10ms delay seems to work
1157 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1158 * unnecessary. The failures need to be investigated to find a proper
1159 * fix, and a delay will likely need to be added here if the I2C write
1160 * retry hack is reverted before the root cause of the boot time issue
1164 if (sensor->platform_data->i2c_addr_alt) {
1165 rval = smiapp_change_cci_addr(sensor);
1167 dev_err(&client->dev, "cci address change error\n");
1168 goto out_cci_addr_fail;
1172 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
1173 SMIAPP_SOFTWARE_RESET);
1175 dev_err(&client->dev, "software reset failed\n");
1176 goto out_cci_addr_fail;
1179 if (sensor->platform_data->i2c_addr_alt) {
1180 rval = smiapp_change_cci_addr(sensor);
1182 dev_err(&client->dev, "cci address change error\n");
1183 goto out_cci_addr_fail;
1187 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
1188 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1190 dev_err(&client->dev, "compression mode set failed\n");
1191 goto out_cci_addr_fail;
1194 rval = smiapp_write(
1195 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1196 sensor->platform_data->ext_clk / (1000000 / (1 << 8)));
1198 dev_err(&client->dev, "extclk frequency set failed\n");
1199 goto out_cci_addr_fail;
1202 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1203 sensor->platform_data->lanes - 1);
1205 dev_err(&client->dev, "csi lane mode set failed\n");
1206 goto out_cci_addr_fail;
1209 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1210 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1212 dev_err(&client->dev, "fast standby set failed\n");
1213 goto out_cci_addr_fail;
1216 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1217 sensor->platform_data->csi_signalling_mode);
1219 dev_err(&client->dev, "csi signalling mode set failed\n");
1220 goto out_cci_addr_fail;
1223 /* DPHY control done by sensor based on requested link rate */
1224 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
1225 SMIAPP_DPHY_CTRL_UI);
1229 rval = smiapp_call_quirk(sensor, post_poweron);
1231 dev_err(&client->dev, "post_poweron quirks failed\n");
1232 goto out_cci_addr_fail;
1235 /* Are we still initialising...? If yes, return here. */
1236 if (!sensor->pixel_array)
1239 rval = v4l2_ctrl_handler_setup(
1240 &sensor->pixel_array->ctrl_handler);
1242 goto out_cci_addr_fail;
1244 rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1246 goto out_cci_addr_fail;
1248 mutex_lock(&sensor->mutex);
1249 rval = smiapp_update_mode(sensor);
1250 mutex_unlock(&sensor->mutex);
1252 goto out_cci_addr_fail;
1257 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1258 gpio_set_value(sensor->platform_data->xshutdown, 0);
1259 if (sensor->platform_data->set_xclk)
1260 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1262 clk_disable(sensor->ext_clk);
1265 regulator_disable(sensor->vana);
1269 static void smiapp_power_off(struct smiapp_sensor *sensor)
1272 * Currently power/clock to lens are enable/disabled separately
1273 * but they are essentially the same signals. So if the sensor is
1274 * powered off while the lens is powered on the sensor does not
1275 * really see a power off and next time the cci address change
1276 * will fail. So do a soft reset explicitly here.
1278 if (sensor->platform_data->i2c_addr_alt)
1279 smiapp_write(sensor,
1280 SMIAPP_REG_U8_SOFTWARE_RESET,
1281 SMIAPP_SOFTWARE_RESET);
1283 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1284 gpio_set_value(sensor->platform_data->xshutdown, 0);
1285 if (sensor->platform_data->set_xclk)
1286 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1288 clk_disable(sensor->ext_clk);
1289 usleep_range(5000, 5000);
1290 regulator_disable(sensor->vana);
1291 sensor->streaming = 0;
1294 static int smiapp_set_power(struct v4l2_subdev *subdev, int on)
1296 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1299 mutex_lock(&sensor->power_mutex);
1302 * If the power count is modified from 0 to != 0 or from != 0
1303 * to 0, update the power state.
1305 if (!sensor->power_count == !on)
1309 /* Power on and perform initialisation. */
1310 ret = smiapp_power_on(sensor);
1314 smiapp_power_off(sensor);
1317 /* Update the power count. */
1318 sensor->power_count += on ? 1 : -1;
1319 WARN_ON(sensor->power_count < 0);
1322 mutex_unlock(&sensor->power_mutex);
1326 /* -----------------------------------------------------------------------------
1327 * Video stream management
1330 static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1332 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1335 mutex_lock(&sensor->mutex);
1337 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1338 (sensor->csi_format->width << 8) |
1339 sensor->csi_format->compressed);
1343 rval = smiapp_pll_configure(sensor);
1347 /* Analog crop start coordinates */
1348 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
1349 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1353 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
1354 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1358 /* Analog crop end coordinates */
1359 rval = smiapp_write(
1360 sensor, SMIAPP_REG_U16_X_ADDR_END,
1361 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1362 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1366 rval = smiapp_write(
1367 sensor, SMIAPP_REG_U16_Y_ADDR_END,
1368 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1369 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1374 * Output from pixel array, including blanking, is set using
1375 * controls below. No need to set here.
1379 if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1380 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1381 rval = smiapp_write(
1382 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1383 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1387 rval = smiapp_write(
1388 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1389 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1393 rval = smiapp_write(
1394 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1395 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1399 rval = smiapp_write(
1400 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1401 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1407 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1408 != SMIAPP_SCALING_CAPABILITY_NONE) {
1409 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
1410 sensor->scaling_mode);
1414 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
1420 /* Output size from sensor */
1421 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1422 sensor->src->crop[SMIAPP_PAD_SRC].width);
1425 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1426 sensor->src->crop[SMIAPP_PAD_SRC].height);
1430 if ((sensor->flash_capability &
1431 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1432 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1433 sensor->platform_data->strobe_setup != NULL &&
1434 sensor->platform_data->strobe_setup->trigger != 0) {
1435 rval = smiapp_setup_flash_strobe(sensor);
1440 rval = smiapp_call_quirk(sensor, pre_streamon);
1442 dev_err(&client->dev, "pre_streamon quirks failed\n");
1446 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1447 SMIAPP_MODE_SELECT_STREAMING);
1450 mutex_unlock(&sensor->mutex);
1455 static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1457 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1460 mutex_lock(&sensor->mutex);
1461 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1462 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1466 rval = smiapp_call_quirk(sensor, post_streamoff);
1468 dev_err(&client->dev, "post_streamoff quirks failed\n");
1471 mutex_unlock(&sensor->mutex);
1475 /* -----------------------------------------------------------------------------
1476 * V4L2 subdev video operations
1479 static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1481 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1484 if (sensor->streaming == enable)
1488 sensor->streaming = 1;
1489 rval = smiapp_start_streaming(sensor);
1491 sensor->streaming = 0;
1493 rval = smiapp_stop_streaming(sensor);
1494 sensor->streaming = 0;
1500 static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1501 struct v4l2_subdev_fh *fh,
1502 struct v4l2_subdev_mbus_code_enum *code)
1504 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1505 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1510 mutex_lock(&sensor->mutex);
1512 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1513 subdev->name, code->pad, code->index);
1515 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1519 code->code = sensor->internal_csi_format->code;
1524 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1525 if (sensor->mbus_frame_fmts & (1 << i))
1528 if (idx == code->index) {
1529 code->code = smiapp_csi_data_formats[i].code;
1530 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1531 code->index, i, code->code);
1538 mutex_unlock(&sensor->mutex);
1543 static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1546 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1548 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1549 return sensor->csi_format->code;
1551 return sensor->internal_csi_format->code;
1554 static int __smiapp_get_format(struct v4l2_subdev *subdev,
1555 struct v4l2_subdev_fh *fh,
1556 struct v4l2_subdev_format *fmt)
1558 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1560 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1561 fmt->format = *v4l2_subdev_get_try_format(fh, fmt->pad);
1563 struct v4l2_rect *r;
1565 if (fmt->pad == ssd->source_pad)
1566 r = &ssd->crop[ssd->source_pad];
1570 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1571 fmt->format.width = r->width;
1572 fmt->format.height = r->height;
1578 static int smiapp_get_format(struct v4l2_subdev *subdev,
1579 struct v4l2_subdev_fh *fh,
1580 struct v4l2_subdev_format *fmt)
1582 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1585 mutex_lock(&sensor->mutex);
1586 rval = __smiapp_get_format(subdev, fh, fmt);
1587 mutex_unlock(&sensor->mutex);
1592 static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1593 struct v4l2_subdev_fh *fh,
1594 struct v4l2_rect **crops,
1595 struct v4l2_rect **comps, int which)
1597 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1600 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1602 for (i = 0; i < subdev->entity.num_pads; i++)
1603 crops[i] = &ssd->crop[i];
1605 *comps = &ssd->compose;
1608 for (i = 0; i < subdev->entity.num_pads; i++) {
1609 crops[i] = v4l2_subdev_get_try_crop(fh, i);
1614 *comps = v4l2_subdev_get_try_compose(fh,
1621 /* Changes require propagation only on sink pad. */
1622 static void smiapp_propagate(struct v4l2_subdev *subdev,
1623 struct v4l2_subdev_fh *fh, int which,
1626 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1627 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1628 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1630 smiapp_get_crop_compose(subdev, fh, crops, &comp, which);
1633 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
1634 comp->width = crops[SMIAPP_PAD_SINK]->width;
1635 comp->height = crops[SMIAPP_PAD_SINK]->height;
1636 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1637 if (ssd == sensor->scaler) {
1640 SMIAPP_LIMIT_SCALER_N_MIN];
1641 sensor->scaling_mode =
1642 SMIAPP_SCALING_MODE_NONE;
1643 } else if (ssd == sensor->binner) {
1644 sensor->binning_horizontal = 1;
1645 sensor->binning_vertical = 1;
1649 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
1650 *crops[SMIAPP_PAD_SRC] = *comp;
1657 static const struct smiapp_csi_data_format
1658 *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1660 const struct smiapp_csi_data_format *csi_format = sensor->csi_format;
1663 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1664 if (sensor->mbus_frame_fmts & (1 << i)
1665 && smiapp_csi_data_formats[i].code == code)
1666 return &smiapp_csi_data_formats[i];
1672 static int smiapp_set_format(struct v4l2_subdev *subdev,
1673 struct v4l2_subdev_fh *fh,
1674 struct v4l2_subdev_format *fmt)
1676 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1677 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1678 struct v4l2_rect *crops[SMIAPP_PADS];
1680 mutex_lock(&sensor->mutex);
1683 * Media bus code is changeable on src subdev's source pad. On
1684 * other source pads we just get format here.
1686 if (fmt->pad == ssd->source_pad) {
1687 u32 code = fmt->format.code;
1688 int rval = __smiapp_get_format(subdev, fh, fmt);
1690 if (!rval && subdev == &sensor->src->sd) {
1691 const struct smiapp_csi_data_format *csi_format =
1692 smiapp_validate_csi_data_format(sensor, code);
1693 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1694 sensor->csi_format = csi_format;
1695 fmt->format.code = csi_format->code;
1698 mutex_unlock(&sensor->mutex);
1702 /* Sink pad. Width and height are changeable here. */
1703 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1704 fmt->format.width &= ~1;
1705 fmt->format.height &= ~1;
1708 clamp(fmt->format.width,
1709 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
1710 sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
1711 fmt->format.height =
1712 clamp(fmt->format.height,
1713 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
1714 sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
1716 smiapp_get_crop_compose(subdev, fh, crops, NULL, fmt->which);
1718 crops[ssd->sink_pad]->left = 0;
1719 crops[ssd->sink_pad]->top = 0;
1720 crops[ssd->sink_pad]->width = fmt->format.width;
1721 crops[ssd->sink_pad]->height = fmt->format.height;
1722 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1723 ssd->sink_fmt = *crops[ssd->sink_pad];
1724 smiapp_propagate(subdev, fh, fmt->which,
1725 V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL);
1727 mutex_unlock(&sensor->mutex);
1733 * Calculate goodness of scaled image size compared to expected image
1734 * size and flags provided.
1736 #define SCALING_GOODNESS 100000
1737 #define SCALING_GOODNESS_EXTREME 100000000
1738 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1739 int h, int ask_h, u32 flags)
1741 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1742 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1750 if (flags & V4L2_SUBDEV_SEL_FLAG_SIZE_GE) {
1752 val -= SCALING_GOODNESS;
1754 val -= SCALING_GOODNESS;
1757 if (flags & V4L2_SUBDEV_SEL_FLAG_SIZE_LE) {
1759 val -= SCALING_GOODNESS;
1761 val -= SCALING_GOODNESS;
1764 val -= abs(w - ask_w);
1765 val -= abs(h - ask_h);
1767 if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
1768 val -= SCALING_GOODNESS_EXTREME;
1770 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1771 w, ask_h, h, ask_h, val);
1776 static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1777 struct v4l2_subdev_fh *fh,
1778 struct v4l2_subdev_selection *sel,
1779 struct v4l2_rect **crops,
1780 struct v4l2_rect *comp)
1782 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1784 unsigned int binh = 1, binv = 1;
1785 unsigned int best = scaling_goodness(
1787 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1788 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1790 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1791 int this = scaling_goodness(
1793 crops[SMIAPP_PAD_SINK]->width
1794 / sensor->binning_subtypes[i].horizontal,
1796 crops[SMIAPP_PAD_SINK]->height
1797 / sensor->binning_subtypes[i].vertical,
1798 sel->r.height, sel->flags);
1801 binh = sensor->binning_subtypes[i].horizontal;
1802 binv = sensor->binning_subtypes[i].vertical;
1806 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1807 sensor->binning_vertical = binv;
1808 sensor->binning_horizontal = binh;
1811 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1812 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1816 * Calculate best scaling ratio and mode for given output resolution.
1818 * Try all of these: horizontal ratio, vertical ratio and smallest
1819 * size possible (horizontally).
1821 * Also try whether horizontal scaler or full scaler gives a better
1824 static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1825 struct v4l2_subdev_fh *fh,
1826 struct v4l2_subdev_selection *sel,
1827 struct v4l2_rect **crops,
1828 struct v4l2_rect *comp)
1830 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1831 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1832 u32 min, max, a, b, max_m;
1833 u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
1834 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1840 sel->r.width = min_t(unsigned int, sel->r.width,
1841 crops[SMIAPP_PAD_SINK]->width);
1842 sel->r.height = min_t(unsigned int, sel->r.height,
1843 crops[SMIAPP_PAD_SINK]->height);
1845 a = crops[SMIAPP_PAD_SINK]->width
1846 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
1847 b = crops[SMIAPP_PAD_SINK]->height
1848 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
1849 max_m = crops[SMIAPP_PAD_SINK]->width
1850 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
1851 / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
1853 a = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1854 max(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1855 b = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1856 max(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1857 max_m = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1858 max(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1860 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1862 min = min(max_m, min(a, b));
1863 max = min(max_m, max(a, b));
1872 try[ntry] = min + 1;
1875 try[ntry] = max + 1;
1880 for (i = 0; i < ntry; i++) {
1881 int this = scaling_goodness(
1883 crops[SMIAPP_PAD_SINK]->width
1885 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1887 crops[SMIAPP_PAD_SINK]->height,
1891 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
1895 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1899 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1900 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
1903 this = scaling_goodness(
1904 subdev, crops[SMIAPP_PAD_SINK]->width
1906 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1908 crops[SMIAPP_PAD_SINK]->height
1910 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1916 mode = SMIAPP_SCALING_MODE_BOTH;
1922 (crops[SMIAPP_PAD_SINK]->width
1924 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
1925 if (mode == SMIAPP_SCALING_MODE_BOTH)
1927 (crops[SMIAPP_PAD_SINK]->height
1929 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
1932 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
1934 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1935 sensor->scale_m = scale_m;
1936 sensor->scaling_mode = mode;
1939 /* We're only called on source pads. This function sets scaling. */
1940 static int smiapp_set_compose(struct v4l2_subdev *subdev,
1941 struct v4l2_subdev_fh *fh,
1942 struct v4l2_subdev_selection *sel)
1944 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1945 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1946 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1948 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
1953 if (ssd == sensor->binner)
1954 smiapp_set_compose_binner(subdev, fh, sel, crops, comp);
1956 smiapp_set_compose_scaler(subdev, fh, sel, crops, comp);
1959 smiapp_propagate(subdev, fh, sel->which,
1960 V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL);
1962 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1963 return smiapp_update_mode(sensor);
1968 static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
1969 struct v4l2_subdev_selection *sel)
1971 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1972 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1974 /* We only implement crop in three places. */
1975 switch (sel->target) {
1976 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
1977 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
1978 if (ssd == sensor->pixel_array
1979 && sel->pad == SMIAPP_PA_PAD_SRC)
1981 if (ssd == sensor->src
1982 && sel->pad == SMIAPP_PAD_SRC)
1984 if (ssd == sensor->scaler
1985 && sel->pad == SMIAPP_PAD_SINK
1986 && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1987 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
1990 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
1991 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
1992 if (sel->pad == ssd->source_pad)
1994 if (ssd == sensor->binner)
1996 if (ssd == sensor->scaler
1997 && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1998 != SMIAPP_SCALING_CAPABILITY_NONE)
2006 static int smiapp_set_crop(struct v4l2_subdev *subdev,
2007 struct v4l2_subdev_fh *fh,
2008 struct v4l2_subdev_selection *sel)
2010 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2011 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2012 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
2013 struct v4l2_rect _r;
2015 smiapp_get_crop_compose(subdev, fh, crops, NULL, sel->which);
2017 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2018 if (sel->pad == ssd->sink_pad)
2019 src_size = &ssd->sink_fmt;
2021 src_size = &ssd->compose;
2023 if (sel->pad == ssd->sink_pad) {
2026 _r.width = v4l2_subdev_get_try_format(fh, sel->pad)
2028 _r.height = v4l2_subdev_get_try_format(fh, sel->pad)
2033 v4l2_subdev_get_try_compose(
2038 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2043 sel->r.width = min(sel->r.width, src_size->width);
2044 sel->r.height = min(sel->r.height, src_size->height);
2046 sel->r.left = min(sel->r.left, src_size->width - sel->r.width);
2047 sel->r.top = min(sel->r.top, src_size->height - sel->r.height);
2049 *crops[sel->pad] = sel->r;
2051 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2052 smiapp_propagate(subdev, fh, sel->which,
2053 V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL);
2058 static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2059 struct v4l2_subdev_fh *fh,
2060 struct v4l2_subdev_selection *sel)
2062 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2063 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2064 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2065 struct v4l2_rect sink_fmt;
2068 ret = __smiapp_sel_supported(subdev, sel);
2072 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
2074 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2075 sink_fmt = ssd->sink_fmt;
2077 struct v4l2_mbus_framefmt *fmt =
2078 v4l2_subdev_get_try_format(fh, ssd->sink_pad);
2082 sink_fmt.width = fmt->width;
2083 sink_fmt.height = fmt->height;
2086 switch (sel->target) {
2087 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
2088 if (ssd == sensor->pixel_array) {
2090 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2092 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2093 } else if (sel->pad == ssd->sink_pad) {
2099 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
2100 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
2101 sel->r = *crops[sel->pad];
2103 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
2111 static int smiapp_get_selection(struct v4l2_subdev *subdev,
2112 struct v4l2_subdev_fh *fh,
2113 struct v4l2_subdev_selection *sel)
2115 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2118 mutex_lock(&sensor->mutex);
2119 rval = __smiapp_get_selection(subdev, fh, sel);
2120 mutex_unlock(&sensor->mutex);
2124 static int smiapp_set_selection(struct v4l2_subdev *subdev,
2125 struct v4l2_subdev_fh *fh,
2126 struct v4l2_subdev_selection *sel)
2128 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2131 ret = __smiapp_sel_supported(subdev, sel);
2135 mutex_lock(&sensor->mutex);
2137 sel->r.left = max(0, sel->r.left & ~1);
2138 sel->r.top = max(0, sel->r.top & ~1);
2139 sel->r.width = max(0, SMIAPP_ALIGN_DIM(sel->r.width, sel->flags));
2140 sel->r.height = max(0, SMIAPP_ALIGN_DIM(sel->r.height, sel->flags));
2142 sel->r.width = max_t(unsigned int,
2143 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
2145 sel->r.height = max_t(unsigned int,
2146 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
2149 switch (sel->target) {
2150 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
2151 ret = smiapp_set_crop(subdev, fh, sel);
2153 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
2154 ret = smiapp_set_compose(subdev, fh, sel);
2160 mutex_unlock(&sensor->mutex);
2164 static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2166 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2168 *frames = sensor->frame_skip;
2172 /* -----------------------------------------------------------------------------
2177 smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2180 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2181 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2182 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2183 unsigned int nbytes;
2185 if (!sensor->dev_init_done)
2188 if (!sensor->nvm_size) {
2189 /* NVM not read yet - read it now */
2190 sensor->nvm_size = sensor->platform_data->nvm_size;
2191 if (smiapp_set_power(subdev, 1) < 0)
2193 if (smiapp_read_nvm(sensor, sensor->nvm)) {
2194 dev_err(&client->dev, "nvm read failed\n");
2197 smiapp_set_power(subdev, 0);
2200 * NVM is still way below a PAGE_SIZE, so we can safely
2201 * assume this for now.
2203 nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
2204 memcpy(buf, sensor->nvm, nbytes);
2208 static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2210 /* -----------------------------------------------------------------------------
2211 * V4L2 subdev core operations
2214 static int smiapp_identify_module(struct v4l2_subdev *subdev)
2216 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2217 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2218 struct smiapp_module_info *minfo = &sensor->minfo;
2222 minfo->name = SMIAPP_NAME;
2225 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2226 &minfo->manufacturer_id);
2228 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2231 rval = smiapp_read_8only(sensor,
2232 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2233 &minfo->revision_number_major);
2235 rval = smiapp_read_8only(sensor,
2236 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2237 &minfo->revision_number_minor);
2239 rval = smiapp_read_8only(sensor,
2240 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2241 &minfo->module_year);
2243 rval = smiapp_read_8only(sensor,
2244 SMIAPP_REG_U8_MODULE_DATE_MONTH,
2245 &minfo->module_month);
2247 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2248 &minfo->module_day);
2252 rval = smiapp_read_8only(sensor,
2253 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2254 &minfo->sensor_manufacturer_id);
2256 rval = smiapp_read_8only(sensor,
2257 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2258 &minfo->sensor_model_id);
2260 rval = smiapp_read_8only(sensor,
2261 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2262 &minfo->sensor_revision_number);
2264 rval = smiapp_read_8only(sensor,
2265 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2266 &minfo->sensor_firmware_version);
2270 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2271 &minfo->smia_version);
2273 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2274 &minfo->smiapp_version);
2277 dev_err(&client->dev, "sensor detection failed\n");
2281 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2282 minfo->manufacturer_id, minfo->model_id);
2284 dev_dbg(&client->dev,
2285 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2286 minfo->revision_number_major, minfo->revision_number_minor,
2287 minfo->module_year, minfo->module_month, minfo->module_day);
2289 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2290 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2292 dev_dbg(&client->dev,
2293 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2294 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2296 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2297 minfo->smia_version, minfo->smiapp_version);
2300 * Some modules have bad data in the lvalues below. Hope the
2301 * rvalues have better stuff. The lvalues are module
2302 * parameters whereas the rvalues are sensor parameters.
2304 if (!minfo->manufacturer_id && !minfo->model_id) {
2305 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2306 minfo->model_id = minfo->sensor_model_id;
2307 minfo->revision_number_major = minfo->sensor_revision_number;
2310 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2311 if (smiapp_module_idents[i].manufacturer_id
2312 != minfo->manufacturer_id)
2314 if (smiapp_module_idents[i].model_id != minfo->model_id)
2316 if (smiapp_module_idents[i].flags
2317 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2318 if (smiapp_module_idents[i].revision_number_major
2319 < minfo->revision_number_major)
2322 if (smiapp_module_idents[i].revision_number_major
2323 != minfo->revision_number_major)
2327 minfo->name = smiapp_module_idents[i].name;
2328 minfo->quirk = smiapp_module_idents[i].quirk;
2332 if (i >= ARRAY_SIZE(smiapp_module_idents))
2333 dev_warn(&client->dev,
2334 "no quirks for this module; let's hope it's fully compliant\n");
2336 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2337 minfo->name, minfo->manufacturer_id, minfo->model_id,
2338 minfo->revision_number_major);
2340 strlcpy(subdev->name, sensor->minfo.name, sizeof(subdev->name));
2345 static const struct v4l2_subdev_ops smiapp_ops;
2346 static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2347 static const struct media_entity_operations smiapp_entity_ops;
2349 static int smiapp_registered(struct v4l2_subdev *subdev)
2351 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2352 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2353 struct smiapp_subdev *last = NULL;
2358 sensor->vana = regulator_get(&client->dev, "VANA");
2359 if (IS_ERR(sensor->vana)) {
2360 dev_err(&client->dev, "could not get regulator for vana\n");
2364 if (!sensor->platform_data->set_xclk) {
2365 sensor->ext_clk = clk_get(&client->dev,
2366 sensor->platform_data->ext_clk_name);
2367 if (IS_ERR(sensor->ext_clk)) {
2368 dev_err(&client->dev, "could not get clock %s\n",
2369 sensor->platform_data->ext_clk_name);
2374 rval = clk_set_rate(sensor->ext_clk,
2375 sensor->platform_data->ext_clk);
2377 dev_err(&client->dev,
2378 "unable to set clock %s freq to %u\n",
2379 sensor->platform_data->ext_clk_name,
2380 sensor->platform_data->ext_clk);
2382 goto out_clk_set_rate;
2386 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) {
2387 if (gpio_request_one(sensor->platform_data->xshutdown, 0,
2388 "SMIA++ xshutdown") != 0) {
2389 dev_err(&client->dev,
2390 "unable to acquire reset gpio %d\n",
2391 sensor->platform_data->xshutdown);
2393 goto out_clk_set_rate;
2397 rval = smiapp_power_on(sensor);
2400 goto out_smiapp_power_on;
2403 rval = smiapp_identify_module(subdev);
2409 rval = smiapp_get_all_limits(sensor);
2416 * Handle Sensor Module orientation on the board.
2418 * The application of H-FLIP and V-FLIP on the sensor is modified by
2419 * the sensor orientation on the board.
2421 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2422 * both H-FLIP and V-FLIP for normal operation which also implies
2423 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2424 * controls will need to be internally inverted.
2426 * Rotation also changes the bayer pattern.
2428 if (sensor->platform_data->module_board_orient ==
2429 SMIAPP_MODULE_BOARD_ORIENT_180)
2430 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2431 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2433 rval = smiapp_get_mbus_formats(sensor);
2439 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2442 rval = smiapp_read(sensor,
2443 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2448 sensor->nbinning_subtypes = min_t(u8, val,
2449 SMIAPP_BINNING_SUBTYPES);
2451 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2453 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
2458 sensor->binning_subtypes[i] =
2459 *(struct smiapp_binning_subtype *)&val;
2461 dev_dbg(&client->dev, "binning %xx%x\n",
2462 sensor->binning_subtypes[i].horizontal,
2463 sensor->binning_subtypes[i].vertical);
2466 sensor->binning_horizontal = 1;
2467 sensor->binning_vertical = 1;
2469 /* SMIA++ NVM initialization - it will be read from the sensor
2470 * when it is first requested by userspace.
2472 if (sensor->minfo.smiapp_version && sensor->platform_data->nvm_size) {
2473 sensor->nvm = kzalloc(sensor->platform_data->nvm_size,
2475 if (sensor->nvm == NULL) {
2476 dev_err(&client->dev, "nvm buf allocation failed\n");
2481 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
2482 dev_err(&client->dev, "sysfs nvm entry failed\n");
2488 rval = smiapp_call_quirk(sensor, limits);
2490 dev_err(&client->dev, "limits quirks failed\n");
2491 goto out_nvm_release;
2494 /* We consider this as profile 0 sensor if any of these are zero. */
2495 if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
2496 !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
2497 !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
2498 !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
2499 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
2500 } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2501 != SMIAPP_SCALING_CAPABILITY_NONE) {
2502 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2503 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2504 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
2506 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
2507 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2508 sensor->ssds_used++;
2509 } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2510 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
2511 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2512 sensor->ssds_used++;
2514 sensor->binner = &sensor->ssds[sensor->ssds_used];
2515 sensor->ssds_used++;
2516 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
2517 sensor->ssds_used++;
2519 sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
2521 for (i = 0; i < SMIAPP_SUBDEVS; i++) {
2523 struct smiapp_subdev *ssd;
2525 } const __this[] = {
2526 { sensor->scaler, "scaler", },
2527 { sensor->binner, "binner", },
2528 { sensor->pixel_array, "pixel array", },
2529 }, *_this = &__this[i];
2530 struct smiapp_subdev *this = _this->ssd;
2535 if (this != sensor->src)
2536 v4l2_subdev_init(&this->sd, &smiapp_ops);
2538 this->sensor = sensor;
2540 if (this == sensor->pixel_array) {
2544 this->source_pad = 1;
2547 snprintf(this->sd.name,
2548 sizeof(this->sd.name), "%s %s",
2549 sensor->minfo.name, _this->name);
2551 this->sink_fmt.width =
2552 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2553 this->sink_fmt.height =
2554 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2555 this->compose.width = this->sink_fmt.width;
2556 this->compose.height = this->sink_fmt.height;
2557 this->crop[this->source_pad] = this->compose;
2558 this->pads[this->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2559 if (this != sensor->pixel_array) {
2560 this->crop[this->sink_pad] = this->compose;
2561 this->pads[this->sink_pad].flags = MEDIA_PAD_FL_SINK;
2564 this->sd.entity.ops = &smiapp_entity_ops;
2571 this->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2572 this->sd.internal_ops = &smiapp_internal_ops;
2573 this->sd.owner = NULL;
2574 v4l2_set_subdevdata(&this->sd, client);
2576 rval = media_entity_init(&this->sd.entity,
2577 this->npads, this->pads, 0);
2579 dev_err(&client->dev,
2580 "media_entity_init failed\n");
2581 goto out_nvm_release;
2584 rval = media_entity_create_link(&this->sd.entity,
2588 MEDIA_LNK_FL_ENABLED |
2589 MEDIA_LNK_FL_IMMUTABLE);
2591 dev_err(&client->dev,
2592 "media_entity_create_link failed\n");
2593 goto out_nvm_release;
2596 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2599 dev_err(&client->dev,
2600 "v4l2_device_register_subdev failed\n");
2601 goto out_nvm_release;
2607 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
2609 sensor->pixel_array->sd.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
2612 smiapp_read_frame_fmt(sensor);
2613 rval = smiapp_init_controls(sensor);
2615 goto out_nvm_release;
2617 rval = smiapp_update_mode(sensor);
2619 dev_err(&client->dev, "update mode failed\n");
2620 goto out_nvm_release;
2623 sensor->streaming = false;
2624 sensor->dev_init_done = true;
2626 /* check flash capability */
2627 rval = smiapp_read(sensor, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY, &tmp);
2628 sensor->flash_capability = tmp;
2630 goto out_nvm_release;
2632 smiapp_power_off(sensor);
2637 device_remove_file(&client->dev, &dev_attr_nvm);
2642 smiapp_power_off(sensor);
2644 out_smiapp_power_on:
2645 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2646 gpio_free(sensor->platform_data->xshutdown);
2649 clk_put(sensor->ext_clk);
2650 sensor->ext_clk = NULL;
2653 regulator_put(sensor->vana);
2654 sensor->vana = NULL;
2658 static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2660 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2661 struct smiapp_sensor *sensor = ssd->sensor;
2663 smiapp_csi_data_formats[smiapp_pixel_order(sensor)].code;
2666 mutex_lock(&sensor->mutex);
2668 for (i = 0; i < ssd->npads; i++) {
2669 struct v4l2_mbus_framefmt *try_fmt =
2670 v4l2_subdev_get_try_format(fh, i);
2671 struct v4l2_rect *try_crop = v4l2_subdev_get_try_crop(fh, i);
2672 struct v4l2_rect *try_comp;
2674 try_fmt->width = sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2675 try_fmt->height = sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2676 try_fmt->code = mbus_code;
2680 try_crop->width = try_fmt->width;
2681 try_crop->height = try_fmt->height;
2683 if (ssd != sensor->pixel_array)
2686 try_comp = v4l2_subdev_get_try_compose(fh, i);
2687 *try_comp = *try_crop;
2690 mutex_unlock(&sensor->mutex);
2692 return smiapp_set_power(sd, 1);
2695 static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2697 return smiapp_set_power(sd, 0);
2700 static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2701 .s_stream = smiapp_set_stream,
2704 static const struct v4l2_subdev_core_ops smiapp_core_ops = {
2705 .s_power = smiapp_set_power,
2708 static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2709 .enum_mbus_code = smiapp_enum_mbus_code,
2710 .get_fmt = smiapp_get_format,
2711 .set_fmt = smiapp_set_format,
2712 .get_selection = smiapp_get_selection,
2713 .set_selection = smiapp_set_selection,
2716 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2717 .g_skip_frames = smiapp_get_skip_frames,
2720 static const struct v4l2_subdev_ops smiapp_ops = {
2721 .core = &smiapp_core_ops,
2722 .video = &smiapp_video_ops,
2723 .pad = &smiapp_pad_ops,
2724 .sensor = &smiapp_sensor_ops,
2727 static const struct media_entity_operations smiapp_entity_ops = {
2728 .link_validate = v4l2_subdev_link_validate,
2731 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2732 .registered = smiapp_registered,
2733 .open = smiapp_open,
2734 .close = smiapp_close,
2737 static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2738 .open = smiapp_open,
2739 .close = smiapp_close,
2742 /* -----------------------------------------------------------------------------
2748 static int smiapp_suspend(struct device *dev)
2750 struct i2c_client *client = to_i2c_client(dev);
2751 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2752 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2755 BUG_ON(mutex_is_locked(&sensor->mutex));
2757 if (sensor->power_count == 0)
2760 if (sensor->streaming)
2761 smiapp_stop_streaming(sensor);
2763 streaming = sensor->streaming;
2765 smiapp_power_off(sensor);
2767 /* save state for resume */
2768 sensor->streaming = streaming;
2773 static int smiapp_resume(struct device *dev)
2775 struct i2c_client *client = to_i2c_client(dev);
2776 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2777 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2780 if (sensor->power_count == 0)
2783 rval = smiapp_power_on(sensor);
2787 if (sensor->streaming)
2788 rval = smiapp_start_streaming(sensor);
2795 #define smiapp_suspend NULL
2796 #define smiapp_resume NULL
2798 #endif /* CONFIG_PM */
2800 static int smiapp_probe(struct i2c_client *client,
2801 const struct i2c_device_id *devid)
2803 struct smiapp_sensor *sensor;
2806 if (client->dev.platform_data == NULL)
2809 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
2813 sensor->platform_data = client->dev.platform_data;
2814 mutex_init(&sensor->mutex);
2815 mutex_init(&sensor->power_mutex);
2816 sensor->src = &sensor->ssds[sensor->ssds_used];
2818 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2819 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2820 sensor->src->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2821 sensor->src->sensor = sensor;
2823 sensor->src->pads[0].flags = MEDIA_PAD_FL_SOURCE;
2824 rval = media_entity_init(&sensor->src->sd.entity, 2,
2825 sensor->src->pads, 0);
2832 static int __exit smiapp_remove(struct i2c_client *client)
2834 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2835 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2838 if (sensor->power_count) {
2839 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2840 gpio_set_value(sensor->platform_data->xshutdown, 0);
2841 if (sensor->platform_data->set_xclk)
2842 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
2844 clk_disable(sensor->ext_clk);
2845 sensor->power_count = 0;
2849 device_remove_file(&client->dev, &dev_attr_nvm);
2853 for (i = 0; i < sensor->ssds_used; i++) {
2854 media_entity_cleanup(&sensor->ssds[i].sd.entity);
2855 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2857 smiapp_free_controls(sensor);
2858 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2859 gpio_free(sensor->platform_data->xshutdown);
2860 if (sensor->ext_clk)
2861 clk_put(sensor->ext_clk);
2863 regulator_put(sensor->vana);
2870 static const struct i2c_device_id smiapp_id_table[] = {
2874 MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
2876 static const struct dev_pm_ops smiapp_pm_ops = {
2877 .suspend = smiapp_suspend,
2878 .resume = smiapp_resume,
2881 static struct i2c_driver smiapp_i2c_driver = {
2883 .name = SMIAPP_NAME,
2884 .pm = &smiapp_pm_ops,
2886 .probe = smiapp_probe,
2887 .remove = __exit_p(smiapp_remove),
2888 .id_table = smiapp_id_table,
2891 module_i2c_driver(smiapp_i2c_driver);
2893 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>");
2894 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
2895 MODULE_LICENSE("GPL");