2 * drivers/media/i2c/smiapp/smiapp-core.c
4 * Generic driver for SMIA/SMIA++ compliant camera modules
6 * Copyright (C) 2010--2012 Nokia Corporation
7 * Contact: Sakari Ailus <sakari.ailus@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/slab.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/v4l2-mediabus.h>
37 #include <media/v4l2-device.h>
41 #define SMIAPP_ALIGN_DIM(dim, flags) \
42 ((flags) & V4L2_SEL_FLAG_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 || f->width > sensor->csi_format->width
782 || (f->width == sensor->csi_format->width
784 > sensor->csi_format->compressed)) {
785 sensor->csi_format = f;
786 sensor->internal_csi_format = f;
791 if (!sensor->csi_format) {
792 dev_err(&client->dev, "no supported mbus code found\n");
796 smiapp_update_mbus_formats(sensor);
801 static void smiapp_update_blanking(struct smiapp_sensor *sensor)
803 struct v4l2_ctrl *vblank = sensor->vblank;
804 struct v4l2_ctrl *hblank = sensor->hblank;
808 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
809 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
810 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
812 sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
813 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
815 vblank->val = clamp_t(int, vblank->val,
816 vblank->minimum, vblank->maximum);
817 vblank->default_value = vblank->minimum;
818 vblank->val = vblank->val;
819 vblank->cur.val = vblank->val;
823 sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
824 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
825 sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
827 sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
828 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
830 hblank->val = clamp_t(int, hblank->val,
831 hblank->minimum, hblank->maximum);
832 hblank->default_value = hblank->minimum;
833 hblank->val = hblank->val;
834 hblank->cur.val = hblank->val;
836 __smiapp_update_exposure_limits(sensor);
839 static int smiapp_update_mode(struct smiapp_sensor *sensor)
841 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
842 unsigned int binning_mode;
845 dev_dbg(&client->dev, "frame size: %dx%d\n",
846 sensor->src->crop[SMIAPP_PAD_SRC].width,
847 sensor->src->crop[SMIAPP_PAD_SRC].height);
848 dev_dbg(&client->dev, "csi format width: %d\n",
849 sensor->csi_format->width);
851 /* Binning has to be set up here; it affects limits */
852 if (sensor->binning_horizontal == 1 &&
853 sensor->binning_vertical == 1) {
857 (sensor->binning_horizontal << 4)
858 | sensor->binning_vertical;
861 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
867 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
871 /* Get updated limits due to binning */
872 rval = smiapp_get_limits_binning(sensor);
876 rval = smiapp_pll_update(sensor);
880 /* Output from pixel array, including blanking */
881 smiapp_update_blanking(sensor);
883 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
884 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
886 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
887 sensor->pll.vt_pix_clk_freq_hz /
888 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
889 + sensor->hblank->val) *
890 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
891 + sensor->vblank->val) / 100));
898 * SMIA++ NVM handling
901 static int smiapp_read_nvm(struct smiapp_sensor *sensor,
907 np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
908 for (p = 0; p < np; p++) {
911 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
915 rval = smiapp_write(sensor,
916 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
917 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
918 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
922 for (i = 0; i < 1000; i++) {
925 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);
930 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
940 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
943 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
953 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
962 * SMIA++ CCI address control
965 static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
967 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
971 client->addr = sensor->platform_data->i2c_addr_dfl;
973 rval = smiapp_write(sensor,
974 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
975 sensor->platform_data->i2c_addr_alt << 1);
979 client->addr = sensor->platform_data->i2c_addr_alt;
981 /* verify addr change went ok */
982 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
986 if (val != sensor->platform_data->i2c_addr_alt << 1)
994 * SMIA++ Mode Control
997 static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
999 struct smiapp_flash_strobe_parms *strobe_setup;
1000 unsigned int ext_freq = sensor->platform_data->ext_clk;
1002 u32 strobe_adjustment;
1003 u32 strobe_width_high_rs;
1006 strobe_setup = sensor->platform_data->strobe_setup;
1009 * How to calculate registers related to strobe length. Please
1010 * do not change, or if you do at least know what you're
1013 * Sakari Ailus <sakari.ailus@maxwell.research.nokia.com> 2010-10-25
1015 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1016 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1018 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1019 * flash_strobe_adjustment E N, [1 - 0xff]
1021 * The formula above is written as below to keep it on one
1024 * l / 10^6 = w / e * a
1026 * Let's mark w * a by x:
1034 * The strobe width must be at least as long as requested,
1035 * thus rounding upwards is needed.
1037 * x = (l * e + 10^6 - 1) / 10^6
1038 * -----------------------------
1040 * Maximum possible accuracy is wanted at all times. Thus keep
1041 * a as small as possible.
1043 * Calculate a, assuming maximum w, with rounding upwards:
1045 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1046 * -------------------------------------
1048 * Thus, we also get w, with that a, with rounding upwards:
1050 * w = (x + a - 1) / a
1051 * -------------------
1055 * x E [1, (2^16 - 1) * (2^8 - 1)]
1057 * Substituting maximum x to the original formula (with rounding),
1058 * the maximum l is thus
1060 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1062 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1063 * --------------------------------------------------
1065 * flash_strobe_length must be clamped between 1 and
1066 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1070 * flash_strobe_adjustment = ((flash_strobe_length *
1071 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1073 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1074 * EXTCLK freq + 10^6 - 1) / 10^6 +
1075 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1077 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1078 1000000 + 1, ext_freq);
1079 strobe_setup->strobe_width_high_us =
1080 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1082 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1083 1000000 - 1), 1000000ULL);
1084 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1085 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1088 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
1089 strobe_setup->mode);
1093 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1098 rval = smiapp_write(
1099 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1100 strobe_width_high_rs);
1104 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1105 strobe_setup->strobe_delay);
1109 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1110 strobe_setup->stobe_start_point);
1114 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1115 strobe_setup->trigger);
1118 sensor->platform_data->strobe_setup->trigger = 0;
1123 /* -----------------------------------------------------------------------------
1127 static int smiapp_power_on(struct smiapp_sensor *sensor)
1129 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1133 rval = regulator_enable(sensor->vana);
1135 dev_err(&client->dev, "failed to enable vana regulator\n");
1138 usleep_range(1000, 1000);
1140 if (sensor->platform_data->set_xclk)
1141 rval = sensor->platform_data->set_xclk(
1142 &sensor->src->sd, sensor->platform_data->ext_clk);
1144 rval = clk_enable(sensor->ext_clk);
1146 dev_dbg(&client->dev, "failed to set xclk\n");
1149 usleep_range(1000, 1000);
1151 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1152 gpio_set_value(sensor->platform_data->xshutdown, 1);
1154 sleep = SMIAPP_RESET_DELAY(sensor->platform_data->ext_clk);
1155 usleep_range(sleep, sleep);
1158 * Failures to respond to the address change command have been noticed.
1159 * Those failures seem to be caused by the sensor requiring a longer
1160 * boot time than advertised. An additional 10ms delay seems to work
1161 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1162 * unnecessary. The failures need to be investigated to find a proper
1163 * fix, and a delay will likely need to be added here if the I2C write
1164 * retry hack is reverted before the root cause of the boot time issue
1168 if (sensor->platform_data->i2c_addr_alt) {
1169 rval = smiapp_change_cci_addr(sensor);
1171 dev_err(&client->dev, "cci address change error\n");
1172 goto out_cci_addr_fail;
1176 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
1177 SMIAPP_SOFTWARE_RESET);
1179 dev_err(&client->dev, "software reset failed\n");
1180 goto out_cci_addr_fail;
1183 if (sensor->platform_data->i2c_addr_alt) {
1184 rval = smiapp_change_cci_addr(sensor);
1186 dev_err(&client->dev, "cci address change error\n");
1187 goto out_cci_addr_fail;
1191 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
1192 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1194 dev_err(&client->dev, "compression mode set failed\n");
1195 goto out_cci_addr_fail;
1198 rval = smiapp_write(
1199 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1200 sensor->platform_data->ext_clk / (1000000 / (1 << 8)));
1202 dev_err(&client->dev, "extclk frequency set failed\n");
1203 goto out_cci_addr_fail;
1206 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1207 sensor->platform_data->lanes - 1);
1209 dev_err(&client->dev, "csi lane mode set failed\n");
1210 goto out_cci_addr_fail;
1213 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1214 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1216 dev_err(&client->dev, "fast standby set failed\n");
1217 goto out_cci_addr_fail;
1220 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1221 sensor->platform_data->csi_signalling_mode);
1223 dev_err(&client->dev, "csi signalling mode set failed\n");
1224 goto out_cci_addr_fail;
1227 /* DPHY control done by sensor based on requested link rate */
1228 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
1229 SMIAPP_DPHY_CTRL_UI);
1233 rval = smiapp_call_quirk(sensor, post_poweron);
1235 dev_err(&client->dev, "post_poweron quirks failed\n");
1236 goto out_cci_addr_fail;
1239 /* Are we still initialising...? If yes, return here. */
1240 if (!sensor->pixel_array)
1243 rval = v4l2_ctrl_handler_setup(
1244 &sensor->pixel_array->ctrl_handler);
1246 goto out_cci_addr_fail;
1248 rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1250 goto out_cci_addr_fail;
1252 mutex_lock(&sensor->mutex);
1253 rval = smiapp_update_mode(sensor);
1254 mutex_unlock(&sensor->mutex);
1256 goto out_cci_addr_fail;
1261 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1262 gpio_set_value(sensor->platform_data->xshutdown, 0);
1263 if (sensor->platform_data->set_xclk)
1264 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1266 clk_disable(sensor->ext_clk);
1269 regulator_disable(sensor->vana);
1273 static void smiapp_power_off(struct smiapp_sensor *sensor)
1276 * Currently power/clock to lens are enable/disabled separately
1277 * but they are essentially the same signals. So if the sensor is
1278 * powered off while the lens is powered on the sensor does not
1279 * really see a power off and next time the cci address change
1280 * will fail. So do a soft reset explicitly here.
1282 if (sensor->platform_data->i2c_addr_alt)
1283 smiapp_write(sensor,
1284 SMIAPP_REG_U8_SOFTWARE_RESET,
1285 SMIAPP_SOFTWARE_RESET);
1287 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1288 gpio_set_value(sensor->platform_data->xshutdown, 0);
1289 if (sensor->platform_data->set_xclk)
1290 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1292 clk_disable(sensor->ext_clk);
1293 usleep_range(5000, 5000);
1294 regulator_disable(sensor->vana);
1295 sensor->streaming = 0;
1298 static int smiapp_set_power(struct v4l2_subdev *subdev, int on)
1300 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1303 mutex_lock(&sensor->power_mutex);
1306 * If the power count is modified from 0 to != 0 or from != 0
1307 * to 0, update the power state.
1309 if (!sensor->power_count == !on)
1313 /* Power on and perform initialisation. */
1314 ret = smiapp_power_on(sensor);
1318 smiapp_power_off(sensor);
1321 /* Update the power count. */
1322 sensor->power_count += on ? 1 : -1;
1323 WARN_ON(sensor->power_count < 0);
1326 mutex_unlock(&sensor->power_mutex);
1330 /* -----------------------------------------------------------------------------
1331 * Video stream management
1334 static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1336 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1339 mutex_lock(&sensor->mutex);
1341 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1342 (sensor->csi_format->width << 8) |
1343 sensor->csi_format->compressed);
1347 rval = smiapp_pll_configure(sensor);
1351 /* Analog crop start coordinates */
1352 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
1353 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1357 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
1358 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1362 /* Analog crop end coordinates */
1363 rval = smiapp_write(
1364 sensor, SMIAPP_REG_U16_X_ADDR_END,
1365 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1366 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1370 rval = smiapp_write(
1371 sensor, SMIAPP_REG_U16_Y_ADDR_END,
1372 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1373 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1378 * Output from pixel array, including blanking, is set using
1379 * controls below. No need to set here.
1383 if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1384 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1385 rval = smiapp_write(
1386 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1387 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1391 rval = smiapp_write(
1392 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1393 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1397 rval = smiapp_write(
1398 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1399 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1403 rval = smiapp_write(
1404 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1405 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1411 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1412 != SMIAPP_SCALING_CAPABILITY_NONE) {
1413 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
1414 sensor->scaling_mode);
1418 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
1424 /* Output size from sensor */
1425 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1426 sensor->src->crop[SMIAPP_PAD_SRC].width);
1429 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1430 sensor->src->crop[SMIAPP_PAD_SRC].height);
1434 if ((sensor->flash_capability &
1435 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1436 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1437 sensor->platform_data->strobe_setup != NULL &&
1438 sensor->platform_data->strobe_setup->trigger != 0) {
1439 rval = smiapp_setup_flash_strobe(sensor);
1444 rval = smiapp_call_quirk(sensor, pre_streamon);
1446 dev_err(&client->dev, "pre_streamon quirks failed\n");
1450 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1451 SMIAPP_MODE_SELECT_STREAMING);
1454 mutex_unlock(&sensor->mutex);
1459 static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1461 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1464 mutex_lock(&sensor->mutex);
1465 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1466 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1470 rval = smiapp_call_quirk(sensor, post_streamoff);
1472 dev_err(&client->dev, "post_streamoff quirks failed\n");
1475 mutex_unlock(&sensor->mutex);
1479 /* -----------------------------------------------------------------------------
1480 * V4L2 subdev video operations
1483 static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1485 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1488 if (sensor->streaming == enable)
1492 sensor->streaming = 1;
1493 rval = smiapp_start_streaming(sensor);
1495 sensor->streaming = 0;
1497 rval = smiapp_stop_streaming(sensor);
1498 sensor->streaming = 0;
1504 static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1505 struct v4l2_subdev_fh *fh,
1506 struct v4l2_subdev_mbus_code_enum *code)
1508 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1509 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1514 mutex_lock(&sensor->mutex);
1516 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1517 subdev->name, code->pad, code->index);
1519 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1523 code->code = sensor->internal_csi_format->code;
1528 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1529 if (sensor->mbus_frame_fmts & (1 << i))
1532 if (idx == code->index) {
1533 code->code = smiapp_csi_data_formats[i].code;
1534 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1535 code->index, i, code->code);
1542 mutex_unlock(&sensor->mutex);
1547 static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1550 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1552 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1553 return sensor->csi_format->code;
1555 return sensor->internal_csi_format->code;
1558 static int __smiapp_get_format(struct v4l2_subdev *subdev,
1559 struct v4l2_subdev_fh *fh,
1560 struct v4l2_subdev_format *fmt)
1562 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1564 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1565 fmt->format = *v4l2_subdev_get_try_format(fh, fmt->pad);
1567 struct v4l2_rect *r;
1569 if (fmt->pad == ssd->source_pad)
1570 r = &ssd->crop[ssd->source_pad];
1574 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1575 fmt->format.width = r->width;
1576 fmt->format.height = r->height;
1582 static int smiapp_get_format(struct v4l2_subdev *subdev,
1583 struct v4l2_subdev_fh *fh,
1584 struct v4l2_subdev_format *fmt)
1586 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1589 mutex_lock(&sensor->mutex);
1590 rval = __smiapp_get_format(subdev, fh, fmt);
1591 mutex_unlock(&sensor->mutex);
1596 static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1597 struct v4l2_subdev_fh *fh,
1598 struct v4l2_rect **crops,
1599 struct v4l2_rect **comps, int which)
1601 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1604 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1606 for (i = 0; i < subdev->entity.num_pads; i++)
1607 crops[i] = &ssd->crop[i];
1609 *comps = &ssd->compose;
1612 for (i = 0; i < subdev->entity.num_pads; i++) {
1613 crops[i] = v4l2_subdev_get_try_crop(fh, i);
1618 *comps = v4l2_subdev_get_try_compose(fh,
1625 /* Changes require propagation only on sink pad. */
1626 static void smiapp_propagate(struct v4l2_subdev *subdev,
1627 struct v4l2_subdev_fh *fh, int which,
1630 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1631 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1632 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1634 smiapp_get_crop_compose(subdev, fh, crops, &comp, which);
1637 case V4L2_SEL_TGT_CROP:
1638 comp->width = crops[SMIAPP_PAD_SINK]->width;
1639 comp->height = crops[SMIAPP_PAD_SINK]->height;
1640 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1641 if (ssd == sensor->scaler) {
1644 SMIAPP_LIMIT_SCALER_N_MIN];
1645 sensor->scaling_mode =
1646 SMIAPP_SCALING_MODE_NONE;
1647 } else if (ssd == sensor->binner) {
1648 sensor->binning_horizontal = 1;
1649 sensor->binning_vertical = 1;
1653 case V4L2_SEL_TGT_COMPOSE:
1654 *crops[SMIAPP_PAD_SRC] = *comp;
1661 static const struct smiapp_csi_data_format
1662 *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1664 const struct smiapp_csi_data_format *csi_format = sensor->csi_format;
1667 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1668 if (sensor->mbus_frame_fmts & (1 << i)
1669 && smiapp_csi_data_formats[i].code == code)
1670 return &smiapp_csi_data_formats[i];
1676 static int smiapp_set_format(struct v4l2_subdev *subdev,
1677 struct v4l2_subdev_fh *fh,
1678 struct v4l2_subdev_format *fmt)
1680 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1681 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1682 struct v4l2_rect *crops[SMIAPP_PADS];
1684 mutex_lock(&sensor->mutex);
1687 * Media bus code is changeable on src subdev's source pad. On
1688 * other source pads we just get format here.
1690 if (fmt->pad == ssd->source_pad) {
1691 u32 code = fmt->format.code;
1692 int rval = __smiapp_get_format(subdev, fh, fmt);
1694 if (!rval && subdev == &sensor->src->sd) {
1695 const struct smiapp_csi_data_format *csi_format =
1696 smiapp_validate_csi_data_format(sensor, code);
1697 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1698 sensor->csi_format = csi_format;
1699 fmt->format.code = csi_format->code;
1702 mutex_unlock(&sensor->mutex);
1706 /* Sink pad. Width and height are changeable here. */
1707 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1708 fmt->format.width &= ~1;
1709 fmt->format.height &= ~1;
1712 clamp(fmt->format.width,
1713 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
1714 sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
1715 fmt->format.height =
1716 clamp(fmt->format.height,
1717 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
1718 sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
1720 smiapp_get_crop_compose(subdev, fh, crops, NULL, fmt->which);
1722 crops[ssd->sink_pad]->left = 0;
1723 crops[ssd->sink_pad]->top = 0;
1724 crops[ssd->sink_pad]->width = fmt->format.width;
1725 crops[ssd->sink_pad]->height = fmt->format.height;
1726 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1727 ssd->sink_fmt = *crops[ssd->sink_pad];
1728 smiapp_propagate(subdev, fh, fmt->which,
1731 mutex_unlock(&sensor->mutex);
1737 * Calculate goodness of scaled image size compared to expected image
1738 * size and flags provided.
1740 #define SCALING_GOODNESS 100000
1741 #define SCALING_GOODNESS_EXTREME 100000000
1742 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1743 int h, int ask_h, u32 flags)
1745 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1746 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1754 if (flags & V4L2_SEL_FLAG_GE) {
1756 val -= SCALING_GOODNESS;
1758 val -= SCALING_GOODNESS;
1761 if (flags & V4L2_SEL_FLAG_LE) {
1763 val -= SCALING_GOODNESS;
1765 val -= SCALING_GOODNESS;
1768 val -= abs(w - ask_w);
1769 val -= abs(h - ask_h);
1771 if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
1772 val -= SCALING_GOODNESS_EXTREME;
1774 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1775 w, ask_h, h, ask_h, val);
1780 static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1781 struct v4l2_subdev_fh *fh,
1782 struct v4l2_subdev_selection *sel,
1783 struct v4l2_rect **crops,
1784 struct v4l2_rect *comp)
1786 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1788 unsigned int binh = 1, binv = 1;
1789 unsigned int best = scaling_goodness(
1791 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1792 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1794 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1795 int this = scaling_goodness(
1797 crops[SMIAPP_PAD_SINK]->width
1798 / sensor->binning_subtypes[i].horizontal,
1800 crops[SMIAPP_PAD_SINK]->height
1801 / sensor->binning_subtypes[i].vertical,
1802 sel->r.height, sel->flags);
1805 binh = sensor->binning_subtypes[i].horizontal;
1806 binv = sensor->binning_subtypes[i].vertical;
1810 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1811 sensor->binning_vertical = binv;
1812 sensor->binning_horizontal = binh;
1815 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1816 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1820 * Calculate best scaling ratio and mode for given output resolution.
1822 * Try all of these: horizontal ratio, vertical ratio and smallest
1823 * size possible (horizontally).
1825 * Also try whether horizontal scaler or full scaler gives a better
1828 static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1829 struct v4l2_subdev_fh *fh,
1830 struct v4l2_subdev_selection *sel,
1831 struct v4l2_rect **crops,
1832 struct v4l2_rect *comp)
1834 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1835 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1836 u32 min, max, a, b, max_m;
1837 u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
1838 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1844 sel->r.width = min_t(unsigned int, sel->r.width,
1845 crops[SMIAPP_PAD_SINK]->width);
1846 sel->r.height = min_t(unsigned int, sel->r.height,
1847 crops[SMIAPP_PAD_SINK]->height);
1849 a = crops[SMIAPP_PAD_SINK]->width
1850 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
1851 b = crops[SMIAPP_PAD_SINK]->height
1852 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
1853 max_m = crops[SMIAPP_PAD_SINK]->width
1854 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
1855 / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
1857 a = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1858 max(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1859 b = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1860 max(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1861 max_m = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1862 max(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1864 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1866 min = min(max_m, min(a, b));
1867 max = min(max_m, max(a, b));
1876 try[ntry] = min + 1;
1879 try[ntry] = max + 1;
1884 for (i = 0; i < ntry; i++) {
1885 int this = scaling_goodness(
1887 crops[SMIAPP_PAD_SINK]->width
1889 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1891 crops[SMIAPP_PAD_SINK]->height,
1895 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
1899 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1903 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1904 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
1907 this = scaling_goodness(
1908 subdev, crops[SMIAPP_PAD_SINK]->width
1910 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1912 crops[SMIAPP_PAD_SINK]->height
1914 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1920 mode = SMIAPP_SCALING_MODE_BOTH;
1926 (crops[SMIAPP_PAD_SINK]->width
1928 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
1929 if (mode == SMIAPP_SCALING_MODE_BOTH)
1931 (crops[SMIAPP_PAD_SINK]->height
1933 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
1936 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
1938 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1939 sensor->scale_m = scale_m;
1940 sensor->scaling_mode = mode;
1943 /* We're only called on source pads. This function sets scaling. */
1944 static int smiapp_set_compose(struct v4l2_subdev *subdev,
1945 struct v4l2_subdev_fh *fh,
1946 struct v4l2_subdev_selection *sel)
1948 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1949 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1950 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1952 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
1957 if (ssd == sensor->binner)
1958 smiapp_set_compose_binner(subdev, fh, sel, crops, comp);
1960 smiapp_set_compose_scaler(subdev, fh, sel, crops, comp);
1963 smiapp_propagate(subdev, fh, sel->which,
1964 V4L2_SEL_TGT_COMPOSE);
1966 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1967 return smiapp_update_mode(sensor);
1972 static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
1973 struct v4l2_subdev_selection *sel)
1975 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1976 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1978 /* We only implement crop in three places. */
1979 switch (sel->target) {
1980 case V4L2_SEL_TGT_CROP:
1981 case V4L2_SEL_TGT_CROP_BOUNDS:
1982 if (ssd == sensor->pixel_array
1983 && sel->pad == SMIAPP_PA_PAD_SRC)
1985 if (ssd == sensor->src
1986 && sel->pad == SMIAPP_PAD_SRC)
1988 if (ssd == sensor->scaler
1989 && sel->pad == SMIAPP_PAD_SINK
1990 && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1991 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
1994 case V4L2_SEL_TGT_COMPOSE:
1995 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1996 if (sel->pad == ssd->source_pad)
1998 if (ssd == sensor->binner)
2000 if (ssd == sensor->scaler
2001 && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2002 != SMIAPP_SCALING_CAPABILITY_NONE)
2010 static int smiapp_set_crop(struct v4l2_subdev *subdev,
2011 struct v4l2_subdev_fh *fh,
2012 struct v4l2_subdev_selection *sel)
2014 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2015 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2016 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
2017 struct v4l2_rect _r;
2019 smiapp_get_crop_compose(subdev, fh, crops, NULL, sel->which);
2021 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2022 if (sel->pad == ssd->sink_pad)
2023 src_size = &ssd->sink_fmt;
2025 src_size = &ssd->compose;
2027 if (sel->pad == ssd->sink_pad) {
2030 _r.width = v4l2_subdev_get_try_format(fh, sel->pad)
2032 _r.height = v4l2_subdev_get_try_format(fh, sel->pad)
2037 v4l2_subdev_get_try_compose(
2042 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2047 sel->r.width = min(sel->r.width, src_size->width);
2048 sel->r.height = min(sel->r.height, src_size->height);
2050 sel->r.left = min(sel->r.left, src_size->width - sel->r.width);
2051 sel->r.top = min(sel->r.top, src_size->height - sel->r.height);
2053 *crops[sel->pad] = sel->r;
2055 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2056 smiapp_propagate(subdev, fh, sel->which,
2062 static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2063 struct v4l2_subdev_fh *fh,
2064 struct v4l2_subdev_selection *sel)
2066 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2067 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2068 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2069 struct v4l2_rect sink_fmt;
2072 ret = __smiapp_sel_supported(subdev, sel);
2076 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
2078 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2079 sink_fmt = ssd->sink_fmt;
2081 struct v4l2_mbus_framefmt *fmt =
2082 v4l2_subdev_get_try_format(fh, ssd->sink_pad);
2086 sink_fmt.width = fmt->width;
2087 sink_fmt.height = fmt->height;
2090 switch (sel->target) {
2091 case V4L2_SEL_TGT_CROP_BOUNDS:
2092 if (ssd == sensor->pixel_array) {
2094 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2096 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2097 } else if (sel->pad == ssd->sink_pad) {
2103 case V4L2_SEL_TGT_CROP:
2104 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2105 sel->r = *crops[sel->pad];
2107 case V4L2_SEL_TGT_COMPOSE:
2115 static int smiapp_get_selection(struct v4l2_subdev *subdev,
2116 struct v4l2_subdev_fh *fh,
2117 struct v4l2_subdev_selection *sel)
2119 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2122 mutex_lock(&sensor->mutex);
2123 rval = __smiapp_get_selection(subdev, fh, sel);
2124 mutex_unlock(&sensor->mutex);
2128 static int smiapp_set_selection(struct v4l2_subdev *subdev,
2129 struct v4l2_subdev_fh *fh,
2130 struct v4l2_subdev_selection *sel)
2132 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2135 ret = __smiapp_sel_supported(subdev, sel);
2139 mutex_lock(&sensor->mutex);
2141 sel->r.left = max(0, sel->r.left & ~1);
2142 sel->r.top = max(0, sel->r.top & ~1);
2143 sel->r.width = max(0, SMIAPP_ALIGN_DIM(sel->r.width, sel->flags));
2144 sel->r.height = max(0, SMIAPP_ALIGN_DIM(sel->r.height, sel->flags));
2146 sel->r.width = max_t(unsigned int,
2147 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
2149 sel->r.height = max_t(unsigned int,
2150 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
2153 switch (sel->target) {
2154 case V4L2_SEL_TGT_CROP:
2155 ret = smiapp_set_crop(subdev, fh, sel);
2157 case V4L2_SEL_TGT_COMPOSE:
2158 ret = smiapp_set_compose(subdev, fh, sel);
2164 mutex_unlock(&sensor->mutex);
2168 static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2170 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2172 *frames = sensor->frame_skip;
2176 /* -----------------------------------------------------------------------------
2181 smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2184 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2185 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2186 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2187 unsigned int nbytes;
2189 if (!sensor->dev_init_done)
2192 if (!sensor->nvm_size) {
2193 /* NVM not read yet - read it now */
2194 sensor->nvm_size = sensor->platform_data->nvm_size;
2195 if (smiapp_set_power(subdev, 1) < 0)
2197 if (smiapp_read_nvm(sensor, sensor->nvm)) {
2198 dev_err(&client->dev, "nvm read failed\n");
2201 smiapp_set_power(subdev, 0);
2204 * NVM is still way below a PAGE_SIZE, so we can safely
2205 * assume this for now.
2207 nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
2208 memcpy(buf, sensor->nvm, nbytes);
2212 static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2215 smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2218 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2219 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2220 struct smiapp_module_info *minfo = &sensor->minfo;
2222 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2223 minfo->manufacturer_id, minfo->model_id,
2224 minfo->revision_number_major) + 1;
2227 static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
2229 /* -----------------------------------------------------------------------------
2230 * V4L2 subdev core operations
2233 static int smiapp_identify_module(struct v4l2_subdev *subdev)
2235 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2236 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2237 struct smiapp_module_info *minfo = &sensor->minfo;
2241 minfo->name = SMIAPP_NAME;
2244 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2245 &minfo->manufacturer_id);
2247 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2250 rval = smiapp_read_8only(sensor,
2251 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2252 &minfo->revision_number_major);
2254 rval = smiapp_read_8only(sensor,
2255 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2256 &minfo->revision_number_minor);
2258 rval = smiapp_read_8only(sensor,
2259 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2260 &minfo->module_year);
2262 rval = smiapp_read_8only(sensor,
2263 SMIAPP_REG_U8_MODULE_DATE_MONTH,
2264 &minfo->module_month);
2266 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2267 &minfo->module_day);
2271 rval = smiapp_read_8only(sensor,
2272 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2273 &minfo->sensor_manufacturer_id);
2275 rval = smiapp_read_8only(sensor,
2276 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2277 &minfo->sensor_model_id);
2279 rval = smiapp_read_8only(sensor,
2280 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2281 &minfo->sensor_revision_number);
2283 rval = smiapp_read_8only(sensor,
2284 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2285 &minfo->sensor_firmware_version);
2289 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2290 &minfo->smia_version);
2292 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2293 &minfo->smiapp_version);
2296 dev_err(&client->dev, "sensor detection failed\n");
2300 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2301 minfo->manufacturer_id, minfo->model_id);
2303 dev_dbg(&client->dev,
2304 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2305 minfo->revision_number_major, minfo->revision_number_minor,
2306 minfo->module_year, minfo->module_month, minfo->module_day);
2308 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2309 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2311 dev_dbg(&client->dev,
2312 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2313 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2315 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2316 minfo->smia_version, minfo->smiapp_version);
2319 * Some modules have bad data in the lvalues below. Hope the
2320 * rvalues have better stuff. The lvalues are module
2321 * parameters whereas the rvalues are sensor parameters.
2323 if (!minfo->manufacturer_id && !minfo->model_id) {
2324 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2325 minfo->model_id = minfo->sensor_model_id;
2326 minfo->revision_number_major = minfo->sensor_revision_number;
2329 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2330 if (smiapp_module_idents[i].manufacturer_id
2331 != minfo->manufacturer_id)
2333 if (smiapp_module_idents[i].model_id != minfo->model_id)
2335 if (smiapp_module_idents[i].flags
2336 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2337 if (smiapp_module_idents[i].revision_number_major
2338 < minfo->revision_number_major)
2341 if (smiapp_module_idents[i].revision_number_major
2342 != minfo->revision_number_major)
2346 minfo->name = smiapp_module_idents[i].name;
2347 minfo->quirk = smiapp_module_idents[i].quirk;
2351 if (i >= ARRAY_SIZE(smiapp_module_idents))
2352 dev_warn(&client->dev,
2353 "no quirks for this module; let's hope it's fully compliant\n");
2355 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2356 minfo->name, minfo->manufacturer_id, minfo->model_id,
2357 minfo->revision_number_major);
2359 strlcpy(subdev->name, sensor->minfo.name, sizeof(subdev->name));
2364 static const struct v4l2_subdev_ops smiapp_ops;
2365 static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2366 static const struct media_entity_operations smiapp_entity_ops;
2368 static int smiapp_registered(struct v4l2_subdev *subdev)
2370 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2371 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2372 struct smiapp_subdev *last = NULL;
2377 sensor->vana = devm_regulator_get(&client->dev, "VANA");
2378 if (IS_ERR(sensor->vana)) {
2379 dev_err(&client->dev, "could not get regulator for vana\n");
2383 if (!sensor->platform_data->set_xclk) {
2384 sensor->ext_clk = devm_clk_get(&client->dev,
2385 sensor->platform_data->ext_clk_name);
2386 if (IS_ERR(sensor->ext_clk)) {
2387 dev_err(&client->dev, "could not get clock %s\n",
2388 sensor->platform_data->ext_clk_name);
2392 rval = clk_set_rate(sensor->ext_clk,
2393 sensor->platform_data->ext_clk);
2395 dev_err(&client->dev,
2396 "unable to set clock %s freq to %u\n",
2397 sensor->platform_data->ext_clk_name,
2398 sensor->platform_data->ext_clk);
2403 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) {
2404 if (gpio_request_one(sensor->platform_data->xshutdown, 0,
2405 "SMIA++ xshutdown") != 0) {
2406 dev_err(&client->dev,
2407 "unable to acquire reset gpio %d\n",
2408 sensor->platform_data->xshutdown);
2413 rval = smiapp_power_on(sensor);
2416 goto out_smiapp_power_on;
2419 rval = smiapp_identify_module(subdev);
2425 rval = smiapp_get_all_limits(sensor);
2432 * Handle Sensor Module orientation on the board.
2434 * The application of H-FLIP and V-FLIP on the sensor is modified by
2435 * the sensor orientation on the board.
2437 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2438 * both H-FLIP and V-FLIP for normal operation which also implies
2439 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2440 * controls will need to be internally inverted.
2442 * Rotation also changes the bayer pattern.
2444 if (sensor->platform_data->module_board_orient ==
2445 SMIAPP_MODULE_BOARD_ORIENT_180)
2446 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2447 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2449 rval = smiapp_get_mbus_formats(sensor);
2455 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2458 rval = smiapp_read(sensor,
2459 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2464 sensor->nbinning_subtypes = min_t(u8, val,
2465 SMIAPP_BINNING_SUBTYPES);
2467 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2469 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
2474 sensor->binning_subtypes[i] =
2475 *(struct smiapp_binning_subtype *)&val;
2477 dev_dbg(&client->dev, "binning %xx%x\n",
2478 sensor->binning_subtypes[i].horizontal,
2479 sensor->binning_subtypes[i].vertical);
2482 sensor->binning_horizontal = 1;
2483 sensor->binning_vertical = 1;
2485 if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
2486 dev_err(&client->dev, "sysfs ident entry creation failed\n");
2490 /* SMIA++ NVM initialization - it will be read from the sensor
2491 * when it is first requested by userspace.
2493 if (sensor->minfo.smiapp_version && sensor->platform_data->nvm_size) {
2494 sensor->nvm = devm_kzalloc(&client->dev,
2495 sensor->platform_data->nvm_size, GFP_KERNEL);
2496 if (sensor->nvm == NULL) {
2497 dev_err(&client->dev, "nvm buf allocation failed\n");
2499 goto out_ident_release;
2502 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
2503 dev_err(&client->dev, "sysfs nvm entry failed\n");
2505 goto out_ident_release;
2509 rval = smiapp_call_quirk(sensor, limits);
2511 dev_err(&client->dev, "limits quirks failed\n");
2512 goto out_nvm_release;
2515 /* We consider this as profile 0 sensor if any of these are zero. */
2516 if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
2517 !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
2518 !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
2519 !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
2520 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
2521 } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2522 != SMIAPP_SCALING_CAPABILITY_NONE) {
2523 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2524 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2525 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
2527 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
2528 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2529 sensor->ssds_used++;
2530 } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2531 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
2532 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2533 sensor->ssds_used++;
2535 sensor->binner = &sensor->ssds[sensor->ssds_used];
2536 sensor->ssds_used++;
2537 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
2538 sensor->ssds_used++;
2540 sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
2542 for (i = 0; i < SMIAPP_SUBDEVS; i++) {
2544 struct smiapp_subdev *ssd;
2546 } const __this[] = {
2547 { sensor->scaler, "scaler", },
2548 { sensor->binner, "binner", },
2549 { sensor->pixel_array, "pixel array", },
2550 }, *_this = &__this[i];
2551 struct smiapp_subdev *this = _this->ssd;
2556 if (this != sensor->src)
2557 v4l2_subdev_init(&this->sd, &smiapp_ops);
2559 this->sensor = sensor;
2561 if (this == sensor->pixel_array) {
2565 this->source_pad = 1;
2568 snprintf(this->sd.name,
2569 sizeof(this->sd.name), "%s %s",
2570 sensor->minfo.name, _this->name);
2572 this->sink_fmt.width =
2573 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2574 this->sink_fmt.height =
2575 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2576 this->compose.width = this->sink_fmt.width;
2577 this->compose.height = this->sink_fmt.height;
2578 this->crop[this->source_pad] = this->compose;
2579 this->pads[this->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2580 if (this != sensor->pixel_array) {
2581 this->crop[this->sink_pad] = this->compose;
2582 this->pads[this->sink_pad].flags = MEDIA_PAD_FL_SINK;
2585 this->sd.entity.ops = &smiapp_entity_ops;
2592 this->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2593 this->sd.internal_ops = &smiapp_internal_ops;
2594 this->sd.owner = NULL;
2595 v4l2_set_subdevdata(&this->sd, client);
2597 rval = media_entity_init(&this->sd.entity,
2598 this->npads, this->pads, 0);
2600 dev_err(&client->dev,
2601 "media_entity_init failed\n");
2602 goto out_nvm_release;
2605 rval = media_entity_create_link(&this->sd.entity,
2609 MEDIA_LNK_FL_ENABLED |
2610 MEDIA_LNK_FL_IMMUTABLE);
2612 dev_err(&client->dev,
2613 "media_entity_create_link failed\n");
2614 goto out_nvm_release;
2617 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2620 dev_err(&client->dev,
2621 "v4l2_device_register_subdev failed\n");
2622 goto out_nvm_release;
2628 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
2630 sensor->pixel_array->sd.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
2633 smiapp_read_frame_fmt(sensor);
2634 rval = smiapp_init_controls(sensor);
2636 goto out_nvm_release;
2638 rval = smiapp_update_mode(sensor);
2640 dev_err(&client->dev, "update mode failed\n");
2641 goto out_nvm_release;
2644 sensor->streaming = false;
2645 sensor->dev_init_done = true;
2647 /* check flash capability */
2648 rval = smiapp_read(sensor, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY, &tmp);
2649 sensor->flash_capability = tmp;
2651 goto out_nvm_release;
2653 smiapp_power_off(sensor);
2658 device_remove_file(&client->dev, &dev_attr_nvm);
2661 device_remove_file(&client->dev, &dev_attr_ident);
2664 smiapp_power_off(sensor);
2666 out_smiapp_power_on:
2667 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2668 gpio_free(sensor->platform_data->xshutdown);
2673 static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2675 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2676 struct smiapp_sensor *sensor = ssd->sensor;
2678 smiapp_csi_data_formats[smiapp_pixel_order(sensor)].code;
2681 mutex_lock(&sensor->mutex);
2683 for (i = 0; i < ssd->npads; i++) {
2684 struct v4l2_mbus_framefmt *try_fmt =
2685 v4l2_subdev_get_try_format(fh, i);
2686 struct v4l2_rect *try_crop = v4l2_subdev_get_try_crop(fh, i);
2687 struct v4l2_rect *try_comp;
2689 try_fmt->width = sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2690 try_fmt->height = sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2691 try_fmt->code = mbus_code;
2695 try_crop->width = try_fmt->width;
2696 try_crop->height = try_fmt->height;
2698 if (ssd != sensor->pixel_array)
2701 try_comp = v4l2_subdev_get_try_compose(fh, i);
2702 *try_comp = *try_crop;
2705 mutex_unlock(&sensor->mutex);
2707 return smiapp_set_power(sd, 1);
2710 static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2712 return smiapp_set_power(sd, 0);
2715 static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2716 .s_stream = smiapp_set_stream,
2719 static const struct v4l2_subdev_core_ops smiapp_core_ops = {
2720 .s_power = smiapp_set_power,
2723 static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2724 .enum_mbus_code = smiapp_enum_mbus_code,
2725 .get_fmt = smiapp_get_format,
2726 .set_fmt = smiapp_set_format,
2727 .get_selection = smiapp_get_selection,
2728 .set_selection = smiapp_set_selection,
2731 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2732 .g_skip_frames = smiapp_get_skip_frames,
2735 static const struct v4l2_subdev_ops smiapp_ops = {
2736 .core = &smiapp_core_ops,
2737 .video = &smiapp_video_ops,
2738 .pad = &smiapp_pad_ops,
2739 .sensor = &smiapp_sensor_ops,
2742 static const struct media_entity_operations smiapp_entity_ops = {
2743 .link_validate = v4l2_subdev_link_validate,
2746 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2747 .registered = smiapp_registered,
2748 .open = smiapp_open,
2749 .close = smiapp_close,
2752 static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2753 .open = smiapp_open,
2754 .close = smiapp_close,
2757 /* -----------------------------------------------------------------------------
2763 static int smiapp_suspend(struct device *dev)
2765 struct i2c_client *client = to_i2c_client(dev);
2766 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2767 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2770 BUG_ON(mutex_is_locked(&sensor->mutex));
2772 if (sensor->power_count == 0)
2775 if (sensor->streaming)
2776 smiapp_stop_streaming(sensor);
2778 streaming = sensor->streaming;
2780 smiapp_power_off(sensor);
2782 /* save state for resume */
2783 sensor->streaming = streaming;
2788 static int smiapp_resume(struct device *dev)
2790 struct i2c_client *client = to_i2c_client(dev);
2791 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2792 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2795 if (sensor->power_count == 0)
2798 rval = smiapp_power_on(sensor);
2802 if (sensor->streaming)
2803 rval = smiapp_start_streaming(sensor);
2810 #define smiapp_suspend NULL
2811 #define smiapp_resume NULL
2813 #endif /* CONFIG_PM */
2815 static int smiapp_probe(struct i2c_client *client,
2816 const struct i2c_device_id *devid)
2818 struct smiapp_sensor *sensor;
2820 if (client->dev.platform_data == NULL)
2823 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
2827 sensor->platform_data = client->dev.platform_data;
2828 mutex_init(&sensor->mutex);
2829 mutex_init(&sensor->power_mutex);
2830 sensor->src = &sensor->ssds[sensor->ssds_used];
2832 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2833 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2834 sensor->src->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2835 sensor->src->sensor = sensor;
2837 sensor->src->pads[0].flags = MEDIA_PAD_FL_SOURCE;
2838 return media_entity_init(&sensor->src->sd.entity, 2,
2839 sensor->src->pads, 0);
2842 static int __exit smiapp_remove(struct i2c_client *client)
2844 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2845 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2848 if (sensor->power_count) {
2849 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2850 gpio_set_value(sensor->platform_data->xshutdown, 0);
2851 if (sensor->platform_data->set_xclk)
2852 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
2854 clk_disable(sensor->ext_clk);
2855 sensor->power_count = 0;
2858 device_remove_file(&client->dev, &dev_attr_ident);
2860 device_remove_file(&client->dev, &dev_attr_nvm);
2862 for (i = 0; i < sensor->ssds_used; i++) {
2863 media_entity_cleanup(&sensor->ssds[i].sd.entity);
2864 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2866 smiapp_free_controls(sensor);
2867 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2868 gpio_free(sensor->platform_data->xshutdown);
2873 static const struct i2c_device_id smiapp_id_table[] = {
2877 MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
2879 static const struct dev_pm_ops smiapp_pm_ops = {
2880 .suspend = smiapp_suspend,
2881 .resume = smiapp_resume,
2884 static struct i2c_driver smiapp_i2c_driver = {
2886 .name = SMIAPP_NAME,
2887 .pm = &smiapp_pm_ops,
2889 .probe = smiapp_probe,
2890 .remove = __exit_p(smiapp_remove),
2891 .id_table = smiapp_id_table,
2894 module_i2c_driver(smiapp_i2c_driver);
2896 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>");
2897 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
2898 MODULE_LICENSE("GPL");