Linux 3.11-rc3
[cris-mirror.git] / drivers / media / i2c / smiapp / smiapp-core.c
blob7ac7580f85c9ab6335e90c1a5c379d33571c0395
1 /*
2 * drivers/media/i2c/smiapp/smiapp-core.c
4 * Generic driver for SMIA/SMIA++ compliant camera modules
6 * Copyright (C) 2010--2012 Nokia Corporation
7 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
9 * Based on smiapp driver by Vimarsh Zutshi
10 * Based on jt8ev1.c by Vimarsh Zutshi
11 * Based on smia-sensor.c by Tuukka Toivonen <tuukkat76@gmail.com>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * version 2 as published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 * 02110-1301 USA
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>
39 #include "smiapp.h"
41 #define SMIAPP_ALIGN_DIM(dim, flags) \
42 ((flags) & V4L2_SEL_FLAG_GE \
43 ? ALIGN((dim), 2) \
44 : (dim) & ~1)
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;
73 unsigned int i;
74 int rval;
75 int line_count = 0;
76 int embedded_start = -1, embedded_end = -1;
77 int image_start = 0;
79 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
80 &fmt_model_type);
81 if (rval)
82 return rval;
84 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
85 &fmt_model_subtype);
86 if (rval)
87 return rval;
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
97 ? "2 byte" :
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++) {
102 u32 desc;
103 u32 pixelcode;
104 u32 pixels;
105 char *which;
106 char *what;
108 if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) {
109 rval = smiapp_read(
110 sensor,
111 SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i),
112 &desc);
113 if (rval)
114 return rval;
116 pixelcode =
117 (desc
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) {
123 rval = smiapp_read(
124 sensor,
125 SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i),
126 &desc);
127 if (rval)
128 return rval;
130 pixelcode =
131 (desc
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;
135 } else {
136 dev_dbg(&client->dev,
137 "invalid frame format model type %d\n",
138 fmt_model_type);
139 return -EINVAL;
142 if (i < ncol_desc)
143 which = "columns";
144 else
145 which = "rows";
147 switch (pixelcode) {
148 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
149 what = "embedded";
150 break;
151 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY:
152 what = "dummy";
153 break;
154 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK:
155 what = "black";
156 break;
157 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK:
158 what = "dark";
159 break;
160 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
161 what = "visible";
162 break;
163 default:
164 what = "invalid";
165 dev_dbg(&client->dev, "pixelcode %d\n", pixelcode);
166 break;
169 dev_dbg(&client->dev, "%s pixels: %d %s\n",
170 what, pixels, which);
172 if (i < ncol_desc)
173 continue;
175 /* Handle row descriptors */
176 if (pixelcode
177 == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED) {
178 embedded_start = line_count;
179 } else {
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) {
190 embedded_start = 0;
191 embedded_end = 0;
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);
198 return 0;
201 static int smiapp_pll_configure(struct smiapp_sensor *sensor)
203 struct smiapp_pll *pll = &sensor->pll;
204 int rval;
206 rval = smiapp_write(
207 sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt_pix_clk_div);
208 if (rval < 0)
209 return rval;
211 rval = smiapp_write(
212 sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt_sys_clk_div);
213 if (rval < 0)
214 return rval;
216 rval = smiapp_write(
217 sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
218 if (rval < 0)
219 return rval;
221 rval = smiapp_write(
222 sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
223 if (rval < 0)
224 return rval;
226 /* Lane op clock ratio does not apply here. */
227 rval = smiapp_write(
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)
231 return rval;
233 rval = smiapp_write(
234 sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op_pix_clk_div);
235 if (rval < 0)
236 return rval;
238 return smiapp_write(
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 .op.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV],
256 .op.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV],
257 .op.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV],
258 .op.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV],
259 .op.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ],
260 .op.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ],
261 .op.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ],
262 .op.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ],
264 .vt.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV],
265 .vt.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV],
266 .vt.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV],
267 .vt.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV],
268 .vt.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ],
269 .vt.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ],
270 .vt.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ],
271 .vt.max_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;
277 int rval;
279 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0) {
281 * Fill in operational clock divisors limits from the
282 * video timing ones. On profile 0 sensors the
283 * requirements regarding them are essentially the
284 * same as on VT ones.
286 lim.op = lim.vt;
289 pll->binning_horizontal = sensor->binning_horizontal;
290 pll->binning_vertical = sensor->binning_vertical;
291 pll->link_freq =
292 sensor->link_freq->qmenu_int[sensor->link_freq->val];
293 pll->scale_m = sensor->scale_m;
294 pll->bits_per_pixel = sensor->csi_format->compressed;
296 rval = smiapp_pll_calculate(&client->dev, &lim, pll);
297 if (rval < 0)
298 return rval;
300 sensor->pixel_rate_parray->cur.val64 = pll->vt_pix_clk_freq_hz;
301 sensor->pixel_rate_csi->cur.val64 = pll->pixel_rate_csi;
303 return 0;
309 * V4L2 Controls handling
313 static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
315 struct v4l2_ctrl *ctrl = sensor->exposure;
316 int max;
318 max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
319 + sensor->vblank->val
320 - sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN];
322 ctrl->maximum = max;
323 if (ctrl->default_value > max)
324 ctrl->default_value = max;
325 if (ctrl->val > max)
326 ctrl->val = max;
327 if (ctrl->cur.val > max)
328 ctrl->cur.val = max;
332 * Order matters.
334 * 1. Bits-per-pixel, descending.
335 * 2. Bits-per-pixel compressed, descending.
336 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
337 * orders must be defined.
339 static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
340 { V4L2_MBUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
341 { V4L2_MBUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
342 { V4L2_MBUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
343 { V4L2_MBUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
344 { V4L2_MBUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
345 { V4L2_MBUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
346 { V4L2_MBUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
347 { V4L2_MBUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
348 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
349 { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
350 { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
351 { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
352 { V4L2_MBUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, },
353 { V4L2_MBUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, },
354 { V4L2_MBUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, },
355 { V4L2_MBUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, },
358 const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
360 #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
361 - (unsigned long)smiapp_csi_data_formats) \
362 / sizeof(*smiapp_csi_data_formats))
364 static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
366 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
367 int flip = 0;
369 if (sensor->hflip) {
370 if (sensor->hflip->val)
371 flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
373 if (sensor->vflip->val)
374 flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
377 flip ^= sensor->hvflip_inv_mask;
379 dev_dbg(&client->dev, "flip %d\n", flip);
380 return sensor->default_pixel_order ^ flip;
383 static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
385 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
386 unsigned int csi_format_idx =
387 to_csi_format_idx(sensor->csi_format) & ~3;
388 unsigned int internal_csi_format_idx =
389 to_csi_format_idx(sensor->internal_csi_format) & ~3;
390 unsigned int pixel_order = smiapp_pixel_order(sensor);
392 sensor->mbus_frame_fmts =
393 sensor->default_mbus_frame_fmts << pixel_order;
394 sensor->csi_format =
395 &smiapp_csi_data_formats[csi_format_idx + pixel_order];
396 sensor->internal_csi_format =
397 &smiapp_csi_data_formats[internal_csi_format_idx
398 + pixel_order];
400 BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
401 >= ARRAY_SIZE(smiapp_csi_data_formats));
402 BUG_ON(min(internal_csi_format_idx, csi_format_idx) < 0);
404 dev_dbg(&client->dev, "new pixel order %s\n",
405 pixel_order_str[pixel_order]);
408 static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
410 struct smiapp_sensor *sensor =
411 container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
412 ->sensor;
413 u32 orient = 0;
414 int exposure;
415 int rval;
417 switch (ctrl->id) {
418 case V4L2_CID_ANALOGUE_GAIN:
419 return smiapp_write(
420 sensor,
421 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
423 case V4L2_CID_EXPOSURE:
424 return smiapp_write(
425 sensor,
426 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
428 case V4L2_CID_HFLIP:
429 case V4L2_CID_VFLIP:
430 if (sensor->streaming)
431 return -EBUSY;
433 if (sensor->hflip->val)
434 orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
436 if (sensor->vflip->val)
437 orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
439 orient ^= sensor->hvflip_inv_mask;
440 rval = smiapp_write(sensor,
441 SMIAPP_REG_U8_IMAGE_ORIENTATION,
442 orient);
443 if (rval < 0)
444 return rval;
446 smiapp_update_mbus_formats(sensor);
448 return 0;
450 case V4L2_CID_VBLANK:
451 exposure = sensor->exposure->val;
453 __smiapp_update_exposure_limits(sensor);
455 if (exposure > sensor->exposure->maximum) {
456 sensor->exposure->val =
457 sensor->exposure->maximum;
458 rval = smiapp_set_ctrl(
459 sensor->exposure);
460 if (rval < 0)
461 return rval;
464 return smiapp_write(
465 sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
466 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
467 + ctrl->val);
469 case V4L2_CID_HBLANK:
470 return smiapp_write(
471 sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
472 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
473 + ctrl->val);
475 case V4L2_CID_LINK_FREQ:
476 if (sensor->streaming)
477 return -EBUSY;
479 return smiapp_pll_update(sensor);
481 default:
482 return -EINVAL;
486 static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
487 .s_ctrl = smiapp_set_ctrl,
490 static int smiapp_init_controls(struct smiapp_sensor *sensor)
492 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
493 unsigned int max;
494 int rval;
496 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 7);
497 if (rval)
498 return rval;
499 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
501 sensor->analog_gain = v4l2_ctrl_new_std(
502 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
503 V4L2_CID_ANALOGUE_GAIN,
504 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN],
505 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX],
506 max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U),
507 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]);
509 /* Exposure limits will be updated soon, use just something here. */
510 sensor->exposure = v4l2_ctrl_new_std(
511 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
512 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
514 sensor->hflip = v4l2_ctrl_new_std(
515 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
516 V4L2_CID_HFLIP, 0, 1, 1, 0);
517 sensor->vflip = v4l2_ctrl_new_std(
518 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
519 V4L2_CID_VFLIP, 0, 1, 1, 0);
521 sensor->vblank = v4l2_ctrl_new_std(
522 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
523 V4L2_CID_VBLANK, 0, 1, 1, 0);
525 if (sensor->vblank)
526 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
528 sensor->hblank = v4l2_ctrl_new_std(
529 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
530 V4L2_CID_HBLANK, 0, 1, 1, 0);
532 if (sensor->hblank)
533 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
535 sensor->pixel_rate_parray = v4l2_ctrl_new_std(
536 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
537 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
539 if (sensor->pixel_array->ctrl_handler.error) {
540 dev_err(&client->dev,
541 "pixel array controls initialization failed (%d)\n",
542 sensor->pixel_array->ctrl_handler.error);
543 rval = sensor->pixel_array->ctrl_handler.error;
544 goto error;
547 sensor->pixel_array->sd.ctrl_handler =
548 &sensor->pixel_array->ctrl_handler;
550 v4l2_ctrl_cluster(2, &sensor->hflip);
552 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
553 if (rval)
554 goto error;
555 sensor->src->ctrl_handler.lock = &sensor->mutex;
557 for (max = 0; sensor->platform_data->op_sys_clock[max + 1]; max++);
559 sensor->link_freq = v4l2_ctrl_new_int_menu(
560 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
561 V4L2_CID_LINK_FREQ, max, 0,
562 sensor->platform_data->op_sys_clock);
564 sensor->pixel_rate_csi = v4l2_ctrl_new_std(
565 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
566 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
568 if (sensor->src->ctrl_handler.error) {
569 dev_err(&client->dev,
570 "src controls initialization failed (%d)\n",
571 sensor->src->ctrl_handler.error);
572 rval = sensor->src->ctrl_handler.error;
573 goto error;
576 sensor->src->sd.ctrl_handler =
577 &sensor->src->ctrl_handler;
579 return 0;
581 error:
582 v4l2_ctrl_handler_free(&sensor->pixel_array->ctrl_handler);
583 v4l2_ctrl_handler_free(&sensor->src->ctrl_handler);
585 return rval;
588 static void smiapp_free_controls(struct smiapp_sensor *sensor)
590 unsigned int i;
592 for (i = 0; i < sensor->ssds_used; i++)
593 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
596 static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
597 unsigned int n)
599 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
600 unsigned int i;
601 u32 val;
602 int rval;
604 for (i = 0; i < n; i++) {
605 rval = smiapp_read(
606 sensor, smiapp_reg_limits[limit[i]].addr, &val);
607 if (rval)
608 return rval;
609 sensor->limits[limit[i]] = val;
610 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %d, 0x%x\n",
611 smiapp_reg_limits[limit[i]].addr,
612 smiapp_reg_limits[limit[i]].what, val, val);
615 return 0;
618 static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
620 unsigned int i;
621 int rval;
623 for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
624 rval = smiapp_get_limits(sensor, &i, 1);
625 if (rval < 0)
626 return rval;
629 if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
630 smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
632 return 0;
635 static int smiapp_get_limits_binning(struct smiapp_sensor *sensor)
637 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
638 static u32 const limits[] = {
639 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN,
640 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN,
641 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN,
642 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN,
643 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN,
644 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN,
645 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN,
647 static u32 const limits_replace[] = {
648 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES,
649 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES,
650 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK,
651 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK,
652 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK,
653 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN,
654 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN,
656 unsigned int i;
657 int rval;
659 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
660 SMIAPP_BINNING_CAPABILITY_NO) {
661 for (i = 0; i < ARRAY_SIZE(limits); i++)
662 sensor->limits[limits[i]] =
663 sensor->limits[limits_replace[i]];
665 return 0;
668 rval = smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits));
669 if (rval < 0)
670 return rval;
673 * Sanity check whether the binning limits are valid. If not,
674 * use the non-binning ones.
676 if (sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN]
677 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN]
678 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN])
679 return 0;
681 for (i = 0; i < ARRAY_SIZE(limits); i++) {
682 dev_dbg(&client->dev,
683 "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
684 smiapp_reg_limits[limits[i]].addr,
685 smiapp_reg_limits[limits[i]].what,
686 sensor->limits[limits_replace[i]],
687 sensor->limits[limits_replace[i]]);
688 sensor->limits[limits[i]] =
689 sensor->limits[limits_replace[i]];
692 return 0;
695 static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
697 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
698 unsigned int type, n;
699 unsigned int i, pixel_order;
700 int rval;
702 rval = smiapp_read(
703 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
704 if (rval)
705 return rval;
707 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
709 rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
710 &pixel_order);
711 if (rval)
712 return rval;
714 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
715 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
716 return -EINVAL;
719 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
720 pixel_order_str[pixel_order]);
722 switch (type) {
723 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
724 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
725 break;
726 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
727 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
728 break;
729 default:
730 return -EINVAL;
733 sensor->default_pixel_order = pixel_order;
734 sensor->mbus_frame_fmts = 0;
736 for (i = 0; i < n; i++) {
737 unsigned int fmt, j;
739 rval = smiapp_read(
740 sensor,
741 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
742 if (rval)
743 return rval;
745 dev_dbg(&client->dev, "bpp %d, compressed %d\n",
746 fmt >> 8, (u8)fmt);
748 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
749 const struct smiapp_csi_data_format *f =
750 &smiapp_csi_data_formats[j];
752 if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
753 continue;
755 if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
756 continue;
758 dev_dbg(&client->dev, "jolly good! %d\n", j);
760 sensor->default_mbus_frame_fmts |= 1 << j;
761 if (!sensor->csi_format
762 || f->width > sensor->csi_format->width
763 || (f->width == sensor->csi_format->width
764 && f->compressed
765 > sensor->csi_format->compressed)) {
766 sensor->csi_format = f;
767 sensor->internal_csi_format = f;
772 if (!sensor->csi_format) {
773 dev_err(&client->dev, "no supported mbus code found\n");
774 return -EINVAL;
777 smiapp_update_mbus_formats(sensor);
779 return 0;
782 static void smiapp_update_blanking(struct smiapp_sensor *sensor)
784 struct v4l2_ctrl *vblank = sensor->vblank;
785 struct v4l2_ctrl *hblank = sensor->hblank;
787 vblank->minimum =
788 max_t(int,
789 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
790 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
791 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
792 vblank->maximum =
793 sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
794 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
796 vblank->val = clamp_t(int, vblank->val,
797 vblank->minimum, vblank->maximum);
798 vblank->default_value = vblank->minimum;
799 vblank->val = vblank->val;
800 vblank->cur.val = vblank->val;
802 hblank->minimum =
803 max_t(int,
804 sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
805 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
806 sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
807 hblank->maximum =
808 sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
809 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
811 hblank->val = clamp_t(int, hblank->val,
812 hblank->minimum, hblank->maximum);
813 hblank->default_value = hblank->minimum;
814 hblank->val = hblank->val;
815 hblank->cur.val = hblank->val;
817 __smiapp_update_exposure_limits(sensor);
820 static int smiapp_update_mode(struct smiapp_sensor *sensor)
822 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
823 unsigned int binning_mode;
824 int rval;
826 dev_dbg(&client->dev, "frame size: %dx%d\n",
827 sensor->src->crop[SMIAPP_PAD_SRC].width,
828 sensor->src->crop[SMIAPP_PAD_SRC].height);
829 dev_dbg(&client->dev, "csi format width: %d\n",
830 sensor->csi_format->width);
832 /* Binning has to be set up here; it affects limits */
833 if (sensor->binning_horizontal == 1 &&
834 sensor->binning_vertical == 1) {
835 binning_mode = 0;
836 } else {
837 u8 binning_type =
838 (sensor->binning_horizontal << 4)
839 | sensor->binning_vertical;
841 rval = smiapp_write(
842 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
843 if (rval < 0)
844 return rval;
846 binning_mode = 1;
848 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
849 if (rval < 0)
850 return rval;
852 /* Get updated limits due to binning */
853 rval = smiapp_get_limits_binning(sensor);
854 if (rval < 0)
855 return rval;
857 rval = smiapp_pll_update(sensor);
858 if (rval < 0)
859 return rval;
861 /* Output from pixel array, including blanking */
862 smiapp_update_blanking(sensor);
864 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
865 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
867 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
868 sensor->pll.vt_pix_clk_freq_hz /
869 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
870 + sensor->hblank->val) *
871 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
872 + sensor->vblank->val) / 100));
874 return 0;
879 * SMIA++ NVM handling
882 static int smiapp_read_nvm(struct smiapp_sensor *sensor,
883 unsigned char *nvm)
885 u32 i, s, p, np, v;
886 int rval = 0, rval2;
888 np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
889 for (p = 0; p < np; p++) {
890 rval = smiapp_write(
891 sensor,
892 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
893 if (rval)
894 goto out;
896 rval = smiapp_write(sensor,
897 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
898 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
899 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
900 if (rval)
901 goto out;
903 for (i = 0; i < 1000; i++) {
904 rval = smiapp_read(
905 sensor,
906 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);
908 if (rval)
909 goto out;
911 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
912 break;
914 if (--i == 0) {
915 rval = -ETIMEDOUT;
916 goto out;
921 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
922 rval = smiapp_read(
923 sensor,
924 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
925 &v);
926 if (rval)
927 goto out;
929 *nvm++ = v;
933 out:
934 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
935 if (rval < 0)
936 return rval;
937 else
938 return rval2;
943 * SMIA++ CCI address control
946 static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
948 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
949 int rval;
950 u32 val;
952 client->addr = sensor->platform_data->i2c_addr_dfl;
954 rval = smiapp_write(sensor,
955 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
956 sensor->platform_data->i2c_addr_alt << 1);
957 if (rval)
958 return rval;
960 client->addr = sensor->platform_data->i2c_addr_alt;
962 /* verify addr change went ok */
963 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
964 if (rval)
965 return rval;
967 if (val != sensor->platform_data->i2c_addr_alt << 1)
968 return -ENODEV;
970 return 0;
975 * SMIA++ Mode Control
978 static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
980 struct smiapp_flash_strobe_parms *strobe_setup;
981 unsigned int ext_freq = sensor->platform_data->ext_clk;
982 u32 tmp;
983 u32 strobe_adjustment;
984 u32 strobe_width_high_rs;
985 int rval;
987 strobe_setup = sensor->platform_data->strobe_setup;
990 * How to calculate registers related to strobe length. Please
991 * do not change, or if you do at least know what you're
992 * doing. :-)
994 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
996 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
997 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
999 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1000 * flash_strobe_adjustment E N, [1 - 0xff]
1002 * The formula above is written as below to keep it on one
1003 * line:
1005 * l / 10^6 = w / e * a
1007 * Let's mark w * a by x:
1009 * x = w * a
1011 * Thus, we get:
1013 * x = l * e / 10^6
1015 * The strobe width must be at least as long as requested,
1016 * thus rounding upwards is needed.
1018 * x = (l * e + 10^6 - 1) / 10^6
1019 * -----------------------------
1021 * Maximum possible accuracy is wanted at all times. Thus keep
1022 * a as small as possible.
1024 * Calculate a, assuming maximum w, with rounding upwards:
1026 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1027 * -------------------------------------
1029 * Thus, we also get w, with that a, with rounding upwards:
1031 * w = (x + a - 1) / a
1032 * -------------------
1034 * To get limits:
1036 * x E [1, (2^16 - 1) * (2^8 - 1)]
1038 * Substituting maximum x to the original formula (with rounding),
1039 * the maximum l is thus
1041 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1043 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1044 * --------------------------------------------------
1046 * flash_strobe_length must be clamped between 1 and
1047 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1049 * Then,
1051 * flash_strobe_adjustment = ((flash_strobe_length *
1052 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1054 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1055 * EXTCLK freq + 10^6 - 1) / 10^6 +
1056 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1058 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1059 1000000 + 1, ext_freq);
1060 strobe_setup->strobe_width_high_us =
1061 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1063 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1064 1000000 - 1), 1000000ULL);
1065 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1066 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1067 strobe_adjustment;
1069 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
1070 strobe_setup->mode);
1071 if (rval < 0)
1072 goto out;
1074 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1075 strobe_adjustment);
1076 if (rval < 0)
1077 goto out;
1079 rval = smiapp_write(
1080 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1081 strobe_width_high_rs);
1082 if (rval < 0)
1083 goto out;
1085 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1086 strobe_setup->strobe_delay);
1087 if (rval < 0)
1088 goto out;
1090 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1091 strobe_setup->stobe_start_point);
1092 if (rval < 0)
1093 goto out;
1095 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1096 strobe_setup->trigger);
1098 out:
1099 sensor->platform_data->strobe_setup->trigger = 0;
1101 return rval;
1104 /* -----------------------------------------------------------------------------
1105 * Power management
1108 static int smiapp_power_on(struct smiapp_sensor *sensor)
1110 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1111 unsigned int sleep;
1112 int rval;
1114 rval = regulator_enable(sensor->vana);
1115 if (rval) {
1116 dev_err(&client->dev, "failed to enable vana regulator\n");
1117 return rval;
1119 usleep_range(1000, 1000);
1121 if (sensor->platform_data->set_xclk)
1122 rval = sensor->platform_data->set_xclk(
1123 &sensor->src->sd, sensor->platform_data->ext_clk);
1124 else
1125 rval = clk_enable(sensor->ext_clk);
1126 if (rval < 0) {
1127 dev_dbg(&client->dev, "failed to set xclk\n");
1128 goto out_xclk_fail;
1130 usleep_range(1000, 1000);
1132 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1133 gpio_set_value(sensor->platform_data->xshutdown, 1);
1135 sleep = SMIAPP_RESET_DELAY(sensor->platform_data->ext_clk);
1136 usleep_range(sleep, sleep);
1139 * Failures to respond to the address change command have been noticed.
1140 * Those failures seem to be caused by the sensor requiring a longer
1141 * boot time than advertised. An additional 10ms delay seems to work
1142 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1143 * unnecessary. The failures need to be investigated to find a proper
1144 * fix, and a delay will likely need to be added here if the I2C write
1145 * retry hack is reverted before the root cause of the boot time issue
1146 * is found.
1149 if (sensor->platform_data->i2c_addr_alt) {
1150 rval = smiapp_change_cci_addr(sensor);
1151 if (rval) {
1152 dev_err(&client->dev, "cci address change error\n");
1153 goto out_cci_addr_fail;
1157 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
1158 SMIAPP_SOFTWARE_RESET);
1159 if (rval < 0) {
1160 dev_err(&client->dev, "software reset failed\n");
1161 goto out_cci_addr_fail;
1164 if (sensor->platform_data->i2c_addr_alt) {
1165 rval = smiapp_change_cci_addr(sensor);
1166 if (rval) {
1167 dev_err(&client->dev, "cci address change error\n");
1168 goto out_cci_addr_fail;
1172 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
1173 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1174 if (rval) {
1175 dev_err(&client->dev, "compression mode set failed\n");
1176 goto out_cci_addr_fail;
1179 rval = smiapp_write(
1180 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1181 sensor->platform_data->ext_clk / (1000000 / (1 << 8)));
1182 if (rval) {
1183 dev_err(&client->dev, "extclk frequency set failed\n");
1184 goto out_cci_addr_fail;
1187 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1188 sensor->platform_data->lanes - 1);
1189 if (rval) {
1190 dev_err(&client->dev, "csi lane mode set failed\n");
1191 goto out_cci_addr_fail;
1194 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1195 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1196 if (rval) {
1197 dev_err(&client->dev, "fast standby set failed\n");
1198 goto out_cci_addr_fail;
1201 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1202 sensor->platform_data->csi_signalling_mode);
1203 if (rval) {
1204 dev_err(&client->dev, "csi signalling mode set failed\n");
1205 goto out_cci_addr_fail;
1208 /* DPHY control done by sensor based on requested link rate */
1209 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
1210 SMIAPP_DPHY_CTRL_UI);
1211 if (rval < 0)
1212 return rval;
1214 rval = smiapp_call_quirk(sensor, post_poweron);
1215 if (rval) {
1216 dev_err(&client->dev, "post_poweron quirks failed\n");
1217 goto out_cci_addr_fail;
1220 /* Are we still initialising...? If yes, return here. */
1221 if (!sensor->pixel_array)
1222 return 0;
1224 rval = v4l2_ctrl_handler_setup(
1225 &sensor->pixel_array->ctrl_handler);
1226 if (rval)
1227 goto out_cci_addr_fail;
1229 rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1230 if (rval)
1231 goto out_cci_addr_fail;
1233 mutex_lock(&sensor->mutex);
1234 rval = smiapp_update_mode(sensor);
1235 mutex_unlock(&sensor->mutex);
1236 if (rval < 0)
1237 goto out_cci_addr_fail;
1239 return 0;
1241 out_cci_addr_fail:
1242 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1243 gpio_set_value(sensor->platform_data->xshutdown, 0);
1244 if (sensor->platform_data->set_xclk)
1245 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1246 else
1247 clk_disable(sensor->ext_clk);
1249 out_xclk_fail:
1250 regulator_disable(sensor->vana);
1251 return rval;
1254 static void smiapp_power_off(struct smiapp_sensor *sensor)
1257 * Currently power/clock to lens are enable/disabled separately
1258 * but they are essentially the same signals. So if the sensor is
1259 * powered off while the lens is powered on the sensor does not
1260 * really see a power off and next time the cci address change
1261 * will fail. So do a soft reset explicitly here.
1263 if (sensor->platform_data->i2c_addr_alt)
1264 smiapp_write(sensor,
1265 SMIAPP_REG_U8_SOFTWARE_RESET,
1266 SMIAPP_SOFTWARE_RESET);
1268 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1269 gpio_set_value(sensor->platform_data->xshutdown, 0);
1270 if (sensor->platform_data->set_xclk)
1271 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1272 else
1273 clk_disable(sensor->ext_clk);
1274 usleep_range(5000, 5000);
1275 regulator_disable(sensor->vana);
1276 sensor->streaming = 0;
1279 static int smiapp_set_power(struct v4l2_subdev *subdev, int on)
1281 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1282 int ret = 0;
1284 mutex_lock(&sensor->power_mutex);
1287 * If the power count is modified from 0 to != 0 or from != 0
1288 * to 0, update the power state.
1290 if (!sensor->power_count == !on)
1291 goto out;
1293 if (on) {
1294 /* Power on and perform initialisation. */
1295 ret = smiapp_power_on(sensor);
1296 if (ret < 0)
1297 goto out;
1298 } else {
1299 smiapp_power_off(sensor);
1302 /* Update the power count. */
1303 sensor->power_count += on ? 1 : -1;
1304 WARN_ON(sensor->power_count < 0);
1306 out:
1307 mutex_unlock(&sensor->power_mutex);
1308 return ret;
1311 /* -----------------------------------------------------------------------------
1312 * Video stream management
1315 static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1317 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1318 int rval;
1320 mutex_lock(&sensor->mutex);
1322 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1323 (sensor->csi_format->width << 8) |
1324 sensor->csi_format->compressed);
1325 if (rval)
1326 goto out;
1328 rval = smiapp_pll_configure(sensor);
1329 if (rval)
1330 goto out;
1332 /* Analog crop start coordinates */
1333 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
1334 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1335 if (rval < 0)
1336 goto out;
1338 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
1339 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1340 if (rval < 0)
1341 goto out;
1343 /* Analog crop end coordinates */
1344 rval = smiapp_write(
1345 sensor, SMIAPP_REG_U16_X_ADDR_END,
1346 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1347 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1348 if (rval < 0)
1349 goto out;
1351 rval = smiapp_write(
1352 sensor, SMIAPP_REG_U16_Y_ADDR_END,
1353 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1354 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1355 if (rval < 0)
1356 goto out;
1359 * Output from pixel array, including blanking, is set using
1360 * controls below. No need to set here.
1363 /* Digital crop */
1364 if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1365 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1366 rval = smiapp_write(
1367 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1368 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1369 if (rval < 0)
1370 goto out;
1372 rval = smiapp_write(
1373 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1374 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1375 if (rval < 0)
1376 goto out;
1378 rval = smiapp_write(
1379 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1380 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1381 if (rval < 0)
1382 goto out;
1384 rval = smiapp_write(
1385 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1386 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1387 if (rval < 0)
1388 goto out;
1391 /* Scaling */
1392 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1393 != SMIAPP_SCALING_CAPABILITY_NONE) {
1394 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
1395 sensor->scaling_mode);
1396 if (rval < 0)
1397 goto out;
1399 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
1400 sensor->scale_m);
1401 if (rval < 0)
1402 goto out;
1405 /* Output size from sensor */
1406 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1407 sensor->src->crop[SMIAPP_PAD_SRC].width);
1408 if (rval < 0)
1409 goto out;
1410 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1411 sensor->src->crop[SMIAPP_PAD_SRC].height);
1412 if (rval < 0)
1413 goto out;
1415 if ((sensor->flash_capability &
1416 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1417 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1418 sensor->platform_data->strobe_setup != NULL &&
1419 sensor->platform_data->strobe_setup->trigger != 0) {
1420 rval = smiapp_setup_flash_strobe(sensor);
1421 if (rval)
1422 goto out;
1425 rval = smiapp_call_quirk(sensor, pre_streamon);
1426 if (rval) {
1427 dev_err(&client->dev, "pre_streamon quirks failed\n");
1428 goto out;
1431 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1432 SMIAPP_MODE_SELECT_STREAMING);
1434 out:
1435 mutex_unlock(&sensor->mutex);
1437 return rval;
1440 static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1442 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1443 int rval;
1445 mutex_lock(&sensor->mutex);
1446 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1447 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1448 if (rval)
1449 goto out;
1451 rval = smiapp_call_quirk(sensor, post_streamoff);
1452 if (rval)
1453 dev_err(&client->dev, "post_streamoff quirks failed\n");
1455 out:
1456 mutex_unlock(&sensor->mutex);
1457 return rval;
1460 /* -----------------------------------------------------------------------------
1461 * V4L2 subdev video operations
1464 static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1466 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1467 int rval;
1469 if (sensor->streaming == enable)
1470 return 0;
1472 if (enable) {
1473 sensor->streaming = 1;
1474 rval = smiapp_start_streaming(sensor);
1475 if (rval < 0)
1476 sensor->streaming = 0;
1477 } else {
1478 rval = smiapp_stop_streaming(sensor);
1479 sensor->streaming = 0;
1482 return rval;
1485 static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1486 struct v4l2_subdev_fh *fh,
1487 struct v4l2_subdev_mbus_code_enum *code)
1489 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1490 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1491 unsigned int i;
1492 int idx = -1;
1493 int rval = -EINVAL;
1495 mutex_lock(&sensor->mutex);
1497 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1498 subdev->name, code->pad, code->index);
1500 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1501 if (code->index)
1502 goto out;
1504 code->code = sensor->internal_csi_format->code;
1505 rval = 0;
1506 goto out;
1509 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1510 if (sensor->mbus_frame_fmts & (1 << i))
1511 idx++;
1513 if (idx == code->index) {
1514 code->code = smiapp_csi_data_formats[i].code;
1515 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1516 code->index, i, code->code);
1517 rval = 0;
1518 break;
1522 out:
1523 mutex_unlock(&sensor->mutex);
1525 return rval;
1528 static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1529 unsigned int pad)
1531 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1533 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1534 return sensor->csi_format->code;
1535 else
1536 return sensor->internal_csi_format->code;
1539 static int __smiapp_get_format(struct v4l2_subdev *subdev,
1540 struct v4l2_subdev_fh *fh,
1541 struct v4l2_subdev_format *fmt)
1543 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1545 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1546 fmt->format = *v4l2_subdev_get_try_format(fh, fmt->pad);
1547 } else {
1548 struct v4l2_rect *r;
1550 if (fmt->pad == ssd->source_pad)
1551 r = &ssd->crop[ssd->source_pad];
1552 else
1553 r = &ssd->sink_fmt;
1555 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1556 fmt->format.width = r->width;
1557 fmt->format.height = r->height;
1560 return 0;
1563 static int smiapp_get_format(struct v4l2_subdev *subdev,
1564 struct v4l2_subdev_fh *fh,
1565 struct v4l2_subdev_format *fmt)
1567 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1568 int rval;
1570 mutex_lock(&sensor->mutex);
1571 rval = __smiapp_get_format(subdev, fh, fmt);
1572 mutex_unlock(&sensor->mutex);
1574 return rval;
1577 static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1578 struct v4l2_subdev_fh *fh,
1579 struct v4l2_rect **crops,
1580 struct v4l2_rect **comps, int which)
1582 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1583 unsigned int i;
1585 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1586 if (crops)
1587 for (i = 0; i < subdev->entity.num_pads; i++)
1588 crops[i] = &ssd->crop[i];
1589 if (comps)
1590 *comps = &ssd->compose;
1591 } else {
1592 if (crops) {
1593 for (i = 0; i < subdev->entity.num_pads; i++) {
1594 crops[i] = v4l2_subdev_get_try_crop(fh, i);
1595 BUG_ON(!crops[i]);
1598 if (comps) {
1599 *comps = v4l2_subdev_get_try_compose(fh,
1600 SMIAPP_PAD_SINK);
1601 BUG_ON(!*comps);
1606 /* Changes require propagation only on sink pad. */
1607 static void smiapp_propagate(struct v4l2_subdev *subdev,
1608 struct v4l2_subdev_fh *fh, int which,
1609 int target)
1611 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1612 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1613 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1615 smiapp_get_crop_compose(subdev, fh, crops, &comp, which);
1617 switch (target) {
1618 case V4L2_SEL_TGT_CROP:
1619 comp->width = crops[SMIAPP_PAD_SINK]->width;
1620 comp->height = crops[SMIAPP_PAD_SINK]->height;
1621 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1622 if (ssd == sensor->scaler) {
1623 sensor->scale_m =
1624 sensor->limits[
1625 SMIAPP_LIMIT_SCALER_N_MIN];
1626 sensor->scaling_mode =
1627 SMIAPP_SCALING_MODE_NONE;
1628 } else if (ssd == sensor->binner) {
1629 sensor->binning_horizontal = 1;
1630 sensor->binning_vertical = 1;
1633 /* Fall through */
1634 case V4L2_SEL_TGT_COMPOSE:
1635 *crops[SMIAPP_PAD_SRC] = *comp;
1636 break;
1637 default:
1638 BUG();
1642 static const struct smiapp_csi_data_format
1643 *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1645 const struct smiapp_csi_data_format *csi_format = sensor->csi_format;
1646 unsigned int i;
1648 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1649 if (sensor->mbus_frame_fmts & (1 << i)
1650 && smiapp_csi_data_formats[i].code == code)
1651 return &smiapp_csi_data_formats[i];
1654 return csi_format;
1657 static int smiapp_set_format(struct v4l2_subdev *subdev,
1658 struct v4l2_subdev_fh *fh,
1659 struct v4l2_subdev_format *fmt)
1661 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1662 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1663 struct v4l2_rect *crops[SMIAPP_PADS];
1665 mutex_lock(&sensor->mutex);
1668 * Media bus code is changeable on src subdev's source pad. On
1669 * other source pads we just get format here.
1671 if (fmt->pad == ssd->source_pad) {
1672 u32 code = fmt->format.code;
1673 int rval = __smiapp_get_format(subdev, fh, fmt);
1675 if (!rval && subdev == &sensor->src->sd) {
1676 const struct smiapp_csi_data_format *csi_format =
1677 smiapp_validate_csi_data_format(sensor, code);
1678 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1679 sensor->csi_format = csi_format;
1680 fmt->format.code = csi_format->code;
1683 mutex_unlock(&sensor->mutex);
1684 return rval;
1687 /* Sink pad. Width and height are changeable here. */
1688 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1689 fmt->format.width &= ~1;
1690 fmt->format.height &= ~1;
1692 fmt->format.width =
1693 clamp(fmt->format.width,
1694 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
1695 sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
1696 fmt->format.height =
1697 clamp(fmt->format.height,
1698 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
1699 sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
1701 smiapp_get_crop_compose(subdev, fh, crops, NULL, fmt->which);
1703 crops[ssd->sink_pad]->left = 0;
1704 crops[ssd->sink_pad]->top = 0;
1705 crops[ssd->sink_pad]->width = fmt->format.width;
1706 crops[ssd->sink_pad]->height = fmt->format.height;
1707 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1708 ssd->sink_fmt = *crops[ssd->sink_pad];
1709 smiapp_propagate(subdev, fh, fmt->which,
1710 V4L2_SEL_TGT_CROP);
1712 mutex_unlock(&sensor->mutex);
1714 return 0;
1718 * Calculate goodness of scaled image size compared to expected image
1719 * size and flags provided.
1721 #define SCALING_GOODNESS 100000
1722 #define SCALING_GOODNESS_EXTREME 100000000
1723 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1724 int h, int ask_h, u32 flags)
1726 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1727 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1728 int val = 0;
1730 w &= ~1;
1731 ask_w &= ~1;
1732 h &= ~1;
1733 ask_h &= ~1;
1735 if (flags & V4L2_SEL_FLAG_GE) {
1736 if (w < ask_w)
1737 val -= SCALING_GOODNESS;
1738 if (h < ask_h)
1739 val -= SCALING_GOODNESS;
1742 if (flags & V4L2_SEL_FLAG_LE) {
1743 if (w > ask_w)
1744 val -= SCALING_GOODNESS;
1745 if (h > ask_h)
1746 val -= SCALING_GOODNESS;
1749 val -= abs(w - ask_w);
1750 val -= abs(h - ask_h);
1752 if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
1753 val -= SCALING_GOODNESS_EXTREME;
1755 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1756 w, ask_h, h, ask_h, val);
1758 return val;
1761 static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1762 struct v4l2_subdev_fh *fh,
1763 struct v4l2_subdev_selection *sel,
1764 struct v4l2_rect **crops,
1765 struct v4l2_rect *comp)
1767 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1768 unsigned int i;
1769 unsigned int binh = 1, binv = 1;
1770 unsigned int best = scaling_goodness(
1771 subdev,
1772 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1773 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1775 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1776 int this = scaling_goodness(
1777 subdev,
1778 crops[SMIAPP_PAD_SINK]->width
1779 / sensor->binning_subtypes[i].horizontal,
1780 sel->r.width,
1781 crops[SMIAPP_PAD_SINK]->height
1782 / sensor->binning_subtypes[i].vertical,
1783 sel->r.height, sel->flags);
1785 if (this > best) {
1786 binh = sensor->binning_subtypes[i].horizontal;
1787 binv = sensor->binning_subtypes[i].vertical;
1788 best = this;
1791 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1792 sensor->binning_vertical = binv;
1793 sensor->binning_horizontal = binh;
1796 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1797 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1801 * Calculate best scaling ratio and mode for given output resolution.
1803 * Try all of these: horizontal ratio, vertical ratio and smallest
1804 * size possible (horizontally).
1806 * Also try whether horizontal scaler or full scaler gives a better
1807 * result.
1809 static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1810 struct v4l2_subdev_fh *fh,
1811 struct v4l2_subdev_selection *sel,
1812 struct v4l2_rect **crops,
1813 struct v4l2_rect *comp)
1815 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1816 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1817 u32 min, max, a, b, max_m;
1818 u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
1819 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1820 u32 try[4];
1821 u32 ntry = 0;
1822 unsigned int i;
1823 int best = INT_MIN;
1825 sel->r.width = min_t(unsigned int, sel->r.width,
1826 crops[SMIAPP_PAD_SINK]->width);
1827 sel->r.height = min_t(unsigned int, sel->r.height,
1828 crops[SMIAPP_PAD_SINK]->height);
1830 a = crops[SMIAPP_PAD_SINK]->width
1831 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
1832 b = crops[SMIAPP_PAD_SINK]->height
1833 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
1834 max_m = crops[SMIAPP_PAD_SINK]->width
1835 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
1836 / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
1838 a = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1839 max(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1840 b = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1841 max(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1842 max_m = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1843 max(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1845 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1847 min = min(max_m, min(a, b));
1848 max = min(max_m, max(a, b));
1850 try[ntry] = min;
1851 ntry++;
1852 if (min != max) {
1853 try[ntry] = max;
1854 ntry++;
1856 if (max != max_m) {
1857 try[ntry] = min + 1;
1858 ntry++;
1859 if (min != max) {
1860 try[ntry] = max + 1;
1861 ntry++;
1865 for (i = 0; i < ntry; i++) {
1866 int this = scaling_goodness(
1867 subdev,
1868 crops[SMIAPP_PAD_SINK]->width
1869 / try[i]
1870 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1871 sel->r.width,
1872 crops[SMIAPP_PAD_SINK]->height,
1873 sel->r.height,
1874 sel->flags);
1876 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
1878 if (this > best) {
1879 scale_m = try[i];
1880 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1881 best = this;
1884 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1885 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
1886 continue;
1888 this = scaling_goodness(
1889 subdev, crops[SMIAPP_PAD_SINK]->width
1890 / try[i]
1891 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1892 sel->r.width,
1893 crops[SMIAPP_PAD_SINK]->height
1894 / try[i]
1895 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1896 sel->r.height,
1897 sel->flags);
1899 if (this > best) {
1900 scale_m = try[i];
1901 mode = SMIAPP_SCALING_MODE_BOTH;
1902 best = this;
1906 sel->r.width =
1907 (crops[SMIAPP_PAD_SINK]->width
1908 / scale_m
1909 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
1910 if (mode == SMIAPP_SCALING_MODE_BOTH)
1911 sel->r.height =
1912 (crops[SMIAPP_PAD_SINK]->height
1913 / scale_m
1914 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
1915 & ~1;
1916 else
1917 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
1919 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1920 sensor->scale_m = scale_m;
1921 sensor->scaling_mode = mode;
1924 /* We're only called on source pads. This function sets scaling. */
1925 static int smiapp_set_compose(struct v4l2_subdev *subdev,
1926 struct v4l2_subdev_fh *fh,
1927 struct v4l2_subdev_selection *sel)
1929 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1930 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1931 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1933 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
1935 sel->r.top = 0;
1936 sel->r.left = 0;
1938 if (ssd == sensor->binner)
1939 smiapp_set_compose_binner(subdev, fh, sel, crops, comp);
1940 else
1941 smiapp_set_compose_scaler(subdev, fh, sel, crops, comp);
1943 *comp = sel->r;
1944 smiapp_propagate(subdev, fh, sel->which,
1945 V4L2_SEL_TGT_COMPOSE);
1947 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1948 return smiapp_update_mode(sensor);
1950 return 0;
1953 static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
1954 struct v4l2_subdev_selection *sel)
1956 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1957 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1959 /* We only implement crop in three places. */
1960 switch (sel->target) {
1961 case V4L2_SEL_TGT_CROP:
1962 case V4L2_SEL_TGT_CROP_BOUNDS:
1963 if (ssd == sensor->pixel_array
1964 && sel->pad == SMIAPP_PA_PAD_SRC)
1965 return 0;
1966 if (ssd == sensor->src
1967 && sel->pad == SMIAPP_PAD_SRC)
1968 return 0;
1969 if (ssd == sensor->scaler
1970 && sel->pad == SMIAPP_PAD_SINK
1971 && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1972 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
1973 return 0;
1974 return -EINVAL;
1975 case V4L2_SEL_TGT_COMPOSE:
1976 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1977 if (sel->pad == ssd->source_pad)
1978 return -EINVAL;
1979 if (ssd == sensor->binner)
1980 return 0;
1981 if (ssd == sensor->scaler
1982 && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1983 != SMIAPP_SCALING_CAPABILITY_NONE)
1984 return 0;
1985 /* Fall through */
1986 default:
1987 return -EINVAL;
1991 static int smiapp_set_crop(struct v4l2_subdev *subdev,
1992 struct v4l2_subdev_fh *fh,
1993 struct v4l2_subdev_selection *sel)
1995 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1996 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1997 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
1998 struct v4l2_rect _r;
2000 smiapp_get_crop_compose(subdev, fh, crops, NULL, sel->which);
2002 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2003 if (sel->pad == ssd->sink_pad)
2004 src_size = &ssd->sink_fmt;
2005 else
2006 src_size = &ssd->compose;
2007 } else {
2008 if (sel->pad == ssd->sink_pad) {
2009 _r.left = 0;
2010 _r.top = 0;
2011 _r.width = v4l2_subdev_get_try_format(fh, sel->pad)
2012 ->width;
2013 _r.height = v4l2_subdev_get_try_format(fh, sel->pad)
2014 ->height;
2015 src_size = &_r;
2016 } else {
2017 src_size =
2018 v4l2_subdev_get_try_compose(
2019 fh, ssd->sink_pad);
2023 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2024 sel->r.left = 0;
2025 sel->r.top = 0;
2028 sel->r.width = min(sel->r.width, src_size->width);
2029 sel->r.height = min(sel->r.height, src_size->height);
2031 sel->r.left = min(sel->r.left, src_size->width - sel->r.width);
2032 sel->r.top = min(sel->r.top, src_size->height - sel->r.height);
2034 *crops[sel->pad] = sel->r;
2036 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2037 smiapp_propagate(subdev, fh, sel->which,
2038 V4L2_SEL_TGT_CROP);
2040 return 0;
2043 static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2044 struct v4l2_subdev_fh *fh,
2045 struct v4l2_subdev_selection *sel)
2047 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2048 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2049 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2050 struct v4l2_rect sink_fmt;
2051 int ret;
2053 ret = __smiapp_sel_supported(subdev, sel);
2054 if (ret)
2055 return ret;
2057 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
2059 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2060 sink_fmt = ssd->sink_fmt;
2061 } else {
2062 struct v4l2_mbus_framefmt *fmt =
2063 v4l2_subdev_get_try_format(fh, ssd->sink_pad);
2065 sink_fmt.left = 0;
2066 sink_fmt.top = 0;
2067 sink_fmt.width = fmt->width;
2068 sink_fmt.height = fmt->height;
2071 switch (sel->target) {
2072 case V4L2_SEL_TGT_CROP_BOUNDS:
2073 if (ssd == sensor->pixel_array) {
2074 sel->r.width =
2075 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2076 sel->r.height =
2077 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2078 } else if (sel->pad == ssd->sink_pad) {
2079 sel->r = sink_fmt;
2080 } else {
2081 sel->r = *comp;
2083 break;
2084 case V4L2_SEL_TGT_CROP:
2085 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2086 sel->r = *crops[sel->pad];
2087 break;
2088 case V4L2_SEL_TGT_COMPOSE:
2089 sel->r = *comp;
2090 break;
2093 return 0;
2096 static int smiapp_get_selection(struct v4l2_subdev *subdev,
2097 struct v4l2_subdev_fh *fh,
2098 struct v4l2_subdev_selection *sel)
2100 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2101 int rval;
2103 mutex_lock(&sensor->mutex);
2104 rval = __smiapp_get_selection(subdev, fh, sel);
2105 mutex_unlock(&sensor->mutex);
2107 return rval;
2109 static int smiapp_set_selection(struct v4l2_subdev *subdev,
2110 struct v4l2_subdev_fh *fh,
2111 struct v4l2_subdev_selection *sel)
2113 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2114 int ret;
2116 ret = __smiapp_sel_supported(subdev, sel);
2117 if (ret)
2118 return ret;
2120 mutex_lock(&sensor->mutex);
2122 sel->r.left = max(0, sel->r.left & ~1);
2123 sel->r.top = max(0, sel->r.top & ~1);
2124 sel->r.width = max(0, SMIAPP_ALIGN_DIM(sel->r.width, sel->flags));
2125 sel->r.height = max(0, SMIAPP_ALIGN_DIM(sel->r.height, sel->flags));
2127 sel->r.width = max_t(unsigned int,
2128 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
2129 sel->r.width);
2130 sel->r.height = max_t(unsigned int,
2131 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
2132 sel->r.height);
2134 switch (sel->target) {
2135 case V4L2_SEL_TGT_CROP:
2136 ret = smiapp_set_crop(subdev, fh, sel);
2137 break;
2138 case V4L2_SEL_TGT_COMPOSE:
2139 ret = smiapp_set_compose(subdev, fh, sel);
2140 break;
2141 default:
2142 BUG();
2145 mutex_unlock(&sensor->mutex);
2146 return ret;
2149 static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2151 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2153 *frames = sensor->frame_skip;
2154 return 0;
2157 /* -----------------------------------------------------------------------------
2158 * sysfs attributes
2161 static ssize_t
2162 smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2163 char *buf)
2165 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2166 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2167 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2168 unsigned int nbytes;
2170 if (!sensor->dev_init_done)
2171 return -EBUSY;
2173 if (!sensor->nvm_size) {
2174 /* NVM not read yet - read it now */
2175 sensor->nvm_size = sensor->platform_data->nvm_size;
2176 if (smiapp_set_power(subdev, 1) < 0)
2177 return -ENODEV;
2178 if (smiapp_read_nvm(sensor, sensor->nvm)) {
2179 dev_err(&client->dev, "nvm read failed\n");
2180 return -ENODEV;
2182 smiapp_set_power(subdev, 0);
2185 * NVM is still way below a PAGE_SIZE, so we can safely
2186 * assume this for now.
2188 nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
2189 memcpy(buf, sensor->nvm, nbytes);
2191 return nbytes;
2193 static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2195 static ssize_t
2196 smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2197 char *buf)
2199 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2200 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2201 struct smiapp_module_info *minfo = &sensor->minfo;
2203 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2204 minfo->manufacturer_id, minfo->model_id,
2205 minfo->revision_number_major) + 1;
2208 static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_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;
2219 unsigned int i;
2220 int rval = 0;
2222 minfo->name = SMIAPP_NAME;
2224 /* Module info */
2225 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2226 &minfo->manufacturer_id);
2227 if (!rval)
2228 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2229 &minfo->model_id);
2230 if (!rval)
2231 rval = smiapp_read_8only(sensor,
2232 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2233 &minfo->revision_number_major);
2234 if (!rval)
2235 rval = smiapp_read_8only(sensor,
2236 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2237 &minfo->revision_number_minor);
2238 if (!rval)
2239 rval = smiapp_read_8only(sensor,
2240 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2241 &minfo->module_year);
2242 if (!rval)
2243 rval = smiapp_read_8only(sensor,
2244 SMIAPP_REG_U8_MODULE_DATE_MONTH,
2245 &minfo->module_month);
2246 if (!rval)
2247 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2248 &minfo->module_day);
2250 /* Sensor info */
2251 if (!rval)
2252 rval = smiapp_read_8only(sensor,
2253 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2254 &minfo->sensor_manufacturer_id);
2255 if (!rval)
2256 rval = smiapp_read_8only(sensor,
2257 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2258 &minfo->sensor_model_id);
2259 if (!rval)
2260 rval = smiapp_read_8only(sensor,
2261 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2262 &minfo->sensor_revision_number);
2263 if (!rval)
2264 rval = smiapp_read_8only(sensor,
2265 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2266 &minfo->sensor_firmware_version);
2268 /* SMIA */
2269 if (!rval)
2270 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2271 &minfo->smia_version);
2272 if (!rval)
2273 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2274 &minfo->smiapp_version);
2276 if (rval) {
2277 dev_err(&client->dev, "sensor detection failed\n");
2278 return -ENODEV;
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)
2313 continue;
2314 if (smiapp_module_idents[i].model_id != minfo->model_id)
2315 continue;
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)
2320 continue;
2321 } else {
2322 if (smiapp_module_idents[i].revision_number_major
2323 != minfo->revision_number_major)
2324 continue;
2327 minfo->name = smiapp_module_idents[i].name;
2328 minfo->quirk = smiapp_module_idents[i].quirk;
2329 break;
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));
2342 return 0;
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_pll *pll = &sensor->pll;
2354 struct smiapp_subdev *last = NULL;
2355 u32 tmp;
2356 unsigned int i;
2357 int rval;
2359 sensor->vana = devm_regulator_get(&client->dev, "VANA");
2360 if (IS_ERR(sensor->vana)) {
2361 dev_err(&client->dev, "could not get regulator for vana\n");
2362 return -ENODEV;
2365 if (!sensor->platform_data->set_xclk) {
2366 sensor->ext_clk = devm_clk_get(&client->dev,
2367 sensor->platform_data->ext_clk_name);
2368 if (IS_ERR(sensor->ext_clk)) {
2369 dev_err(&client->dev, "could not get clock %s\n",
2370 sensor->platform_data->ext_clk_name);
2371 return -ENODEV;
2374 rval = clk_set_rate(sensor->ext_clk,
2375 sensor->platform_data->ext_clk);
2376 if (rval < 0) {
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);
2381 return -ENODEV;
2385 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) {
2386 if (devm_gpio_request_one(&client->dev,
2387 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);
2392 return -ENODEV;
2396 rval = smiapp_power_on(sensor);
2397 if (rval)
2398 return -ENODEV;
2400 rval = smiapp_identify_module(subdev);
2401 if (rval) {
2402 rval = -ENODEV;
2403 goto out_power_off;
2406 rval = smiapp_get_all_limits(sensor);
2407 if (rval) {
2408 rval = -ENODEV;
2409 goto out_power_off;
2413 * Handle Sensor Module orientation on the board.
2415 * The application of H-FLIP and V-FLIP on the sensor is modified by
2416 * the sensor orientation on the board.
2418 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2419 * both H-FLIP and V-FLIP for normal operation which also implies
2420 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2421 * controls will need to be internally inverted.
2423 * Rotation also changes the bayer pattern.
2425 if (sensor->platform_data->module_board_orient ==
2426 SMIAPP_MODULE_BOARD_ORIENT_180)
2427 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2428 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2430 rval = smiapp_get_mbus_formats(sensor);
2431 if (rval) {
2432 rval = -ENODEV;
2433 goto out_power_off;
2436 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2437 u32 val;
2439 rval = smiapp_read(sensor,
2440 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2441 if (rval < 0) {
2442 rval = -ENODEV;
2443 goto out_power_off;
2445 sensor->nbinning_subtypes = min_t(u8, val,
2446 SMIAPP_BINNING_SUBTYPES);
2448 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2449 rval = smiapp_read(
2450 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
2451 if (rval < 0) {
2452 rval = -ENODEV;
2453 goto out_power_off;
2455 sensor->binning_subtypes[i] =
2456 *(struct smiapp_binning_subtype *)&val;
2458 dev_dbg(&client->dev, "binning %xx%x\n",
2459 sensor->binning_subtypes[i].horizontal,
2460 sensor->binning_subtypes[i].vertical);
2463 sensor->binning_horizontal = 1;
2464 sensor->binning_vertical = 1;
2466 if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
2467 dev_err(&client->dev, "sysfs ident entry creation failed\n");
2468 rval = -ENOENT;
2469 goto out_power_off;
2471 /* SMIA++ NVM initialization - it will be read from the sensor
2472 * when it is first requested by userspace.
2474 if (sensor->minfo.smiapp_version && sensor->platform_data->nvm_size) {
2475 sensor->nvm = devm_kzalloc(&client->dev,
2476 sensor->platform_data->nvm_size, GFP_KERNEL);
2477 if (sensor->nvm == NULL) {
2478 dev_err(&client->dev, "nvm buf allocation failed\n");
2479 rval = -ENOMEM;
2480 goto out_ident_release;
2483 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
2484 dev_err(&client->dev, "sysfs nvm entry failed\n");
2485 rval = -EBUSY;
2486 goto out_ident_release;
2490 rval = smiapp_call_quirk(sensor, limits);
2491 if (rval) {
2492 dev_err(&client->dev, "limits quirks failed\n");
2493 goto out_nvm_release;
2496 /* We consider this as profile 0 sensor if any of these are zero. */
2497 if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
2498 !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
2499 !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
2500 !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
2501 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
2502 } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2503 != SMIAPP_SCALING_CAPABILITY_NONE) {
2504 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2505 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2506 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
2507 else
2508 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
2509 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2510 sensor->ssds_used++;
2511 } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2512 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
2513 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2514 sensor->ssds_used++;
2516 sensor->binner = &sensor->ssds[sensor->ssds_used];
2517 sensor->ssds_used++;
2518 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
2519 sensor->ssds_used++;
2521 sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
2523 for (i = 0; i < SMIAPP_SUBDEVS; i++) {
2524 struct {
2525 struct smiapp_subdev *ssd;
2526 char *name;
2527 } const __this[] = {
2528 { sensor->scaler, "scaler", },
2529 { sensor->binner, "binner", },
2530 { sensor->pixel_array, "pixel array", },
2531 }, *_this = &__this[i];
2532 struct smiapp_subdev *this = _this->ssd;
2534 if (!this)
2535 continue;
2537 if (this != sensor->src)
2538 v4l2_subdev_init(&this->sd, &smiapp_ops);
2540 this->sensor = sensor;
2542 if (this == sensor->pixel_array) {
2543 this->npads = 1;
2544 } else {
2545 this->npads = 2;
2546 this->source_pad = 1;
2549 snprintf(this->sd.name,
2550 sizeof(this->sd.name), "%s %s",
2551 sensor->minfo.name, _this->name);
2553 this->sink_fmt.width =
2554 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2555 this->sink_fmt.height =
2556 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2557 this->compose.width = this->sink_fmt.width;
2558 this->compose.height = this->sink_fmt.height;
2559 this->crop[this->source_pad] = this->compose;
2560 this->pads[this->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2561 if (this != sensor->pixel_array) {
2562 this->crop[this->sink_pad] = this->compose;
2563 this->pads[this->sink_pad].flags = MEDIA_PAD_FL_SINK;
2566 this->sd.entity.ops = &smiapp_entity_ops;
2568 if (last == NULL) {
2569 last = this;
2570 continue;
2573 this->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2574 this->sd.internal_ops = &smiapp_internal_ops;
2575 this->sd.owner = NULL;
2576 v4l2_set_subdevdata(&this->sd, client);
2578 rval = media_entity_init(&this->sd.entity,
2579 this->npads, this->pads, 0);
2580 if (rval) {
2581 dev_err(&client->dev,
2582 "media_entity_init failed\n");
2583 goto out_nvm_release;
2586 rval = media_entity_create_link(&this->sd.entity,
2587 this->source_pad,
2588 &last->sd.entity,
2589 last->sink_pad,
2590 MEDIA_LNK_FL_ENABLED |
2591 MEDIA_LNK_FL_IMMUTABLE);
2592 if (rval) {
2593 dev_err(&client->dev,
2594 "media_entity_create_link failed\n");
2595 goto out_nvm_release;
2598 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2599 &this->sd);
2600 if (rval) {
2601 dev_err(&client->dev,
2602 "v4l2_device_register_subdev failed\n");
2603 goto out_nvm_release;
2606 last = this;
2609 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
2611 sensor->pixel_array->sd.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
2613 /* final steps */
2614 smiapp_read_frame_fmt(sensor);
2615 rval = smiapp_init_controls(sensor);
2616 if (rval < 0)
2617 goto out_nvm_release;
2619 /* prepare PLL configuration input values */
2620 pll->bus_type = SMIAPP_PLL_BUS_TYPE_CSI2;
2621 pll->csi2.lanes = sensor->platform_data->lanes;
2622 pll->ext_clk_freq_hz = sensor->platform_data->ext_clk;
2623 /* Profile 0 sensors have no separate OP clock branch. */
2624 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
2625 pll->flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
2626 if (smiapp_needs_quirk(sensor,
2627 SMIAPP_QUIRK_FLAG_OP_PIX_CLOCK_PER_LANE))
2628 pll->flags |= SMIAPP_PLL_FLAG_OP_PIX_CLOCK_PER_LANE;
2629 pll->scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
2631 rval = smiapp_update_mode(sensor);
2632 if (rval) {
2633 dev_err(&client->dev, "update mode failed\n");
2634 goto out_nvm_release;
2637 sensor->streaming = false;
2638 sensor->dev_init_done = true;
2640 /* check flash capability */
2641 rval = smiapp_read(sensor, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY, &tmp);
2642 sensor->flash_capability = tmp;
2643 if (rval)
2644 goto out_nvm_release;
2646 smiapp_power_off(sensor);
2648 return 0;
2650 out_nvm_release:
2651 device_remove_file(&client->dev, &dev_attr_nvm);
2653 out_ident_release:
2654 device_remove_file(&client->dev, &dev_attr_ident);
2656 out_power_off:
2657 smiapp_power_off(sensor);
2658 return rval;
2661 static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2663 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2664 struct smiapp_sensor *sensor = ssd->sensor;
2665 u32 mbus_code =
2666 smiapp_csi_data_formats[smiapp_pixel_order(sensor)].code;
2667 unsigned int i;
2669 mutex_lock(&sensor->mutex);
2671 for (i = 0; i < ssd->npads; i++) {
2672 struct v4l2_mbus_framefmt *try_fmt =
2673 v4l2_subdev_get_try_format(fh, i);
2674 struct v4l2_rect *try_crop = v4l2_subdev_get_try_crop(fh, i);
2675 struct v4l2_rect *try_comp;
2677 try_fmt->width = sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2678 try_fmt->height = sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2679 try_fmt->code = mbus_code;
2681 try_crop->top = 0;
2682 try_crop->left = 0;
2683 try_crop->width = try_fmt->width;
2684 try_crop->height = try_fmt->height;
2686 if (ssd != sensor->pixel_array)
2687 continue;
2689 try_comp = v4l2_subdev_get_try_compose(fh, i);
2690 *try_comp = *try_crop;
2693 mutex_unlock(&sensor->mutex);
2695 return smiapp_set_power(sd, 1);
2698 static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2700 return smiapp_set_power(sd, 0);
2703 static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2704 .s_stream = smiapp_set_stream,
2707 static const struct v4l2_subdev_core_ops smiapp_core_ops = {
2708 .s_power = smiapp_set_power,
2711 static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2712 .enum_mbus_code = smiapp_enum_mbus_code,
2713 .get_fmt = smiapp_get_format,
2714 .set_fmt = smiapp_set_format,
2715 .get_selection = smiapp_get_selection,
2716 .set_selection = smiapp_set_selection,
2719 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2720 .g_skip_frames = smiapp_get_skip_frames,
2723 static const struct v4l2_subdev_ops smiapp_ops = {
2724 .core = &smiapp_core_ops,
2725 .video = &smiapp_video_ops,
2726 .pad = &smiapp_pad_ops,
2727 .sensor = &smiapp_sensor_ops,
2730 static const struct media_entity_operations smiapp_entity_ops = {
2731 .link_validate = v4l2_subdev_link_validate,
2734 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2735 .registered = smiapp_registered,
2736 .open = smiapp_open,
2737 .close = smiapp_close,
2740 static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2741 .open = smiapp_open,
2742 .close = smiapp_close,
2745 /* -----------------------------------------------------------------------------
2746 * I2C Driver
2749 #ifdef CONFIG_PM
2751 static int smiapp_suspend(struct device *dev)
2753 struct i2c_client *client = to_i2c_client(dev);
2754 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2755 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2756 bool streaming;
2758 BUG_ON(mutex_is_locked(&sensor->mutex));
2760 if (sensor->power_count == 0)
2761 return 0;
2763 if (sensor->streaming)
2764 smiapp_stop_streaming(sensor);
2766 streaming = sensor->streaming;
2768 smiapp_power_off(sensor);
2770 /* save state for resume */
2771 sensor->streaming = streaming;
2773 return 0;
2776 static int smiapp_resume(struct device *dev)
2778 struct i2c_client *client = to_i2c_client(dev);
2779 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2780 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2781 int rval;
2783 if (sensor->power_count == 0)
2784 return 0;
2786 rval = smiapp_power_on(sensor);
2787 if (rval)
2788 return rval;
2790 if (sensor->streaming)
2791 rval = smiapp_start_streaming(sensor);
2793 return rval;
2796 #else
2798 #define smiapp_suspend NULL
2799 #define smiapp_resume NULL
2801 #endif /* CONFIG_PM */
2803 static int smiapp_probe(struct i2c_client *client,
2804 const struct i2c_device_id *devid)
2806 struct smiapp_sensor *sensor;
2808 if (client->dev.platform_data == NULL)
2809 return -ENODEV;
2811 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
2812 if (sensor == NULL)
2813 return -ENOMEM;
2815 sensor->platform_data = client->dev.platform_data;
2816 mutex_init(&sensor->mutex);
2817 mutex_init(&sensor->power_mutex);
2818 sensor->src = &sensor->ssds[sensor->ssds_used];
2820 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2821 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2822 sensor->src->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2823 sensor->src->sensor = sensor;
2825 sensor->src->pads[0].flags = MEDIA_PAD_FL_SOURCE;
2826 return media_entity_init(&sensor->src->sd.entity, 2,
2827 sensor->src->pads, 0);
2830 static int smiapp_remove(struct i2c_client *client)
2832 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2833 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2834 unsigned int i;
2836 if (sensor->power_count) {
2837 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2838 gpio_set_value(sensor->platform_data->xshutdown, 0);
2839 if (sensor->platform_data->set_xclk)
2840 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
2841 else
2842 clk_disable(sensor->ext_clk);
2843 sensor->power_count = 0;
2846 device_remove_file(&client->dev, &dev_attr_ident);
2847 if (sensor->nvm)
2848 device_remove_file(&client->dev, &dev_attr_nvm);
2850 for (i = 0; i < sensor->ssds_used; i++) {
2851 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2852 media_entity_cleanup(&sensor->ssds[i].sd.entity);
2854 smiapp_free_controls(sensor);
2856 return 0;
2859 static const struct i2c_device_id smiapp_id_table[] = {
2860 { SMIAPP_NAME, 0 },
2861 { },
2863 MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
2865 static const struct dev_pm_ops smiapp_pm_ops = {
2866 .suspend = smiapp_suspend,
2867 .resume = smiapp_resume,
2870 static struct i2c_driver smiapp_i2c_driver = {
2871 .driver = {
2872 .name = SMIAPP_NAME,
2873 .pm = &smiapp_pm_ops,
2875 .probe = smiapp_probe,
2876 .remove = smiapp_remove,
2877 .id_table = smiapp_id_table,
2880 module_i2c_driver(smiapp_i2c_driver);
2882 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
2883 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
2884 MODULE_LICENSE("GPL");