blob: 3991c452acb2c38af3628d155b69ba4cc47ead4a [file] [log] [blame]
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001/*
2 * drivers/media/video/smiapp/smiapp-core.c
3 *
4 * Generic driver for SMIA/SMIA++ compliant camera modules
5 *
6 * Copyright (C) 2010--2012 Nokia Corporation
7 * Contact: Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>
8 *
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>
12 *
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.
16 *
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.
21 *
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
26 *
27 */
28
29#include "smiapp-debug.h"
30
31#include <linux/delay.h>
32#include <linux/device.h>
33#include <linux/gpio.h>
34#include <linux/module.h>
35#include <linux/regulator/consumer.h>
36#include <linux/v4l2-mediabus.h>
37#include <media/v4l2-device.h>
38
39#include "smiapp.h"
40
41#define SMIAPP_ALIGN_DIM(dim, flags) \
42 ((flags) & V4L2_SUBDEV_SEL_FLAG_SIZE_GE \
43 ? ALIGN((dim), 2) \
44 : (dim) & ~1)
45
46/*
47 * smiapp_module_idents - supported camera modules
48 */
49static 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),
61};
62
63/*
64 *
65 * Dynamic Capability Identification
66 *
67 */
68
69static int smiapp_read_frame_fmt(struct smiapp_sensor *sensor)
70{
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;
78
79 rval = smiapp_read(client, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
80 &fmt_model_type);
81 if (rval)
82 return rval;
83
84 rval = smiapp_read(client, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
85 &fmt_model_subtype);
86 if (rval)
87 return rval;
88
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;
94
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");
100
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;
107
108 if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) {
109 rval = smiapp_read(
110 client,
111 SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i),
112 &desc);
113 if (rval)
114 return rval;
115
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 client,
125 SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i),
126 &desc);
127 if (rval)
128 return rval;
129
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;
140 }
141
142 if (i < ncol_desc)
143 which = "columns";
144 else
145 which = "rows";
146
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;
167 }
168
169 dev_dbg(&client->dev, "%s pixels: %d %s\n",
170 what, pixels, which);
171
172 if (i < ncol_desc)
173 continue;
174
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;
185 }
186 line_count += pixels;
187 }
188
189 if (embedded_start == -1 || embedded_end == -1) {
190 embedded_start = 0;
191 embedded_end = 0;
192 }
193
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);
197
198 return 0;
199}
200
201static int smiapp_pll_configure(struct smiapp_sensor *sensor)
202{
203 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
204 struct smiapp_pll *pll = &sensor->pll;
205 int rval;
206
207 rval = smiapp_write(
208 client, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt_pix_clk_div);
209 if (rval < 0)
210 return rval;
211
212 rval = smiapp_write(
213 client, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt_sys_clk_div);
214 if (rval < 0)
215 return rval;
216
217 rval = smiapp_write(
218 client, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
219 if (rval < 0)
220 return rval;
221
222 rval = smiapp_write(
223 client, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
224 if (rval < 0)
225 return rval;
226
227 /* Lane op clock ratio does not apply here. */
228 rval = smiapp_write(
229 client, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS,
230 DIV_ROUND_UP(pll->op_sys_clk_freq_hz, 1000000 / 256 / 256));
231 if (rval < 0 || sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
232 return rval;
233
234 rval = smiapp_write(
235 client, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op_pix_clk_div);
236 if (rval < 0)
237 return rval;
238
239 return smiapp_write(
240 client, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op_sys_clk_div);
241}
242
243static int smiapp_pll_update(struct smiapp_sensor *sensor)
244{
245 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
246 struct smiapp_pll_limits lim = {
247 .min_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV],
248 .max_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV],
249 .min_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ],
250 .max_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ],
251 .min_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER],
252 .max_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER],
253 .min_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ],
254 .max_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ],
255
256 .min_op_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV],
257 .max_op_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV],
258 .min_op_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV],
259 .max_op_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV],
260 .min_op_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ],
261 .max_op_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ],
262 .min_op_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ],
263 .max_op_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ],
264
265 .min_vt_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV],
266 .max_vt_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV],
267 .min_vt_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV],
268 .max_vt_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV],
269 .min_vt_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ],
270 .max_vt_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ],
271 .min_vt_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ],
272 .max_vt_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ],
273
274 .min_line_length_pck_bin = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN],
275 .min_line_length_pck = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK],
276 };
277 struct smiapp_pll *pll = &sensor->pll;
278 int rval;
279
280 memset(&sensor->pll, 0, sizeof(sensor->pll));
281
282 pll->lanes = sensor->platform_data->lanes;
283 pll->ext_clk_freq_hz = sensor->platform_data->ext_clk;
284
285 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0) {
286 /*
287 * Fill in operational clock divisors limits from the
288 * video timing ones. On profile 0 sensors the
289 * requirements regarding them are essentially the
290 * same as on VT ones.
291 */
292 lim.min_op_sys_clk_div = lim.min_vt_sys_clk_div;
293 lim.max_op_sys_clk_div = lim.max_vt_sys_clk_div;
294 lim.min_op_pix_clk_div = lim.min_vt_pix_clk_div;
295 lim.max_op_pix_clk_div = lim.max_vt_pix_clk_div;
296 lim.min_op_sys_clk_freq_hz = lim.min_vt_sys_clk_freq_hz;
297 lim.max_op_sys_clk_freq_hz = lim.max_vt_sys_clk_freq_hz;
298 lim.min_op_pix_clk_freq_hz = lim.min_vt_pix_clk_freq_hz;
299 lim.max_op_pix_clk_freq_hz = lim.max_vt_pix_clk_freq_hz;
300 /* Profile 0 sensors have no separate OP clock branch. */
301 pll->flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
302 }
303
304 if (smiapp_needs_quirk(sensor,
305 SMIAPP_QUIRK_FLAG_OP_PIX_CLOCK_PER_LANE))
306 pll->flags |= SMIAPP_PLL_FLAG_OP_PIX_CLOCK_PER_LANE;
307
308 pll->binning_horizontal = sensor->binning_horizontal;
309 pll->binning_vertical = sensor->binning_vertical;
310 pll->link_freq =
311 sensor->link_freq->qmenu_int[sensor->link_freq->val];
312 pll->scale_m = sensor->scale_m;
313 pll->scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
314 pll->bits_per_pixel = sensor->csi_format->compressed;
315
316 rval = smiapp_pll_calculate(&client->dev, &lim, pll);
317 if (rval < 0)
318 return rval;
319
320 sensor->pixel_rate_parray->cur.val64 = pll->vt_pix_clk_freq_hz;
321 sensor->pixel_rate_csi->cur.val64 = pll->pixel_rate_csi;
322
323 return 0;
324}
325
326
327/*
328 *
329 * V4L2 Controls handling
330 *
331 */
332
333static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
334{
335 struct v4l2_ctrl *ctrl = sensor->exposure;
336 int max;
337
338 max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
339 + sensor->vblank->val
340 - sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN];
341
342 ctrl->maximum = max;
343 if (ctrl->default_value > max)
344 ctrl->default_value = max;
345 if (ctrl->val > max)
346 ctrl->val = max;
347 if (ctrl->cur.val > max)
348 ctrl->cur.val = max;
349}
350
351/*
352 * Order matters.
353 *
354 * 1. Bits-per-pixel, descending.
355 * 2. Bits-per-pixel compressed, descending.
356 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
357 * orders must be defined.
358 */
359static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
360 { V4L2_MBUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
361 { V4L2_MBUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
362 { V4L2_MBUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
363 { V4L2_MBUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
364 { V4L2_MBUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
365 { V4L2_MBUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
366 { V4L2_MBUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
367 { V4L2_MBUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
368 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
369 { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
370 { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
371 { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
372};
373
374const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
375
376#define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
377 - (unsigned long)smiapp_csi_data_formats) \
378 / sizeof(*smiapp_csi_data_formats))
379
380static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
381{
382 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
383 int flip = 0;
384
385 if (sensor->hflip) {
386 if (sensor->hflip->val)
387 flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
388
389 if (sensor->vflip->val)
390 flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
391 }
392
393 flip ^= sensor->hvflip_inv_mask;
394
395 dev_dbg(&client->dev, "flip %d\n", flip);
396 return sensor->default_pixel_order ^ flip;
397}
398
399static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
400{
401 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
402 unsigned int csi_format_idx =
403 to_csi_format_idx(sensor->csi_format) & ~3;
404 unsigned int internal_csi_format_idx =
405 to_csi_format_idx(sensor->internal_csi_format) & ~3;
406 unsigned int pixel_order = smiapp_pixel_order(sensor);
407
408 sensor->mbus_frame_fmts =
409 sensor->default_mbus_frame_fmts << pixel_order;
410 sensor->csi_format =
411 &smiapp_csi_data_formats[csi_format_idx + pixel_order];
412 sensor->internal_csi_format =
413 &smiapp_csi_data_formats[internal_csi_format_idx
414 + pixel_order];
415
416 BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
417 >= ARRAY_SIZE(smiapp_csi_data_formats));
418 BUG_ON(min(internal_csi_format_idx, csi_format_idx) < 0);
419
420 dev_dbg(&client->dev, "new pixel order %s\n",
421 pixel_order_str[pixel_order]);
422}
423
424static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
425{
426 struct smiapp_sensor *sensor =
427 container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
428 ->sensor;
429 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
430 u32 orient = 0;
431 int exposure;
432 int rval;
433
434 switch (ctrl->id) {
435 case V4L2_CID_ANALOGUE_GAIN:
436 return smiapp_write(
437 client,
438 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
439
440 case V4L2_CID_EXPOSURE:
441 return smiapp_write(
442 client,
443 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
444
445 case V4L2_CID_HFLIP:
446 case V4L2_CID_VFLIP:
447 if (sensor->streaming)
448 return -EBUSY;
449
450 if (sensor->hflip->val)
451 orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
452
453 if (sensor->vflip->val)
454 orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
455
456 orient ^= sensor->hvflip_inv_mask;
457 rval = smiapp_write(client,
458 SMIAPP_REG_U8_IMAGE_ORIENTATION,
459 orient);
460 if (rval < 0)
461 return rval;
462
463 smiapp_update_mbus_formats(sensor);
464
465 return 0;
466
467 case V4L2_CID_VBLANK:
468 exposure = sensor->exposure->val;
469
470 __smiapp_update_exposure_limits(sensor);
471
472 if (exposure > sensor->exposure->maximum) {
473 sensor->exposure->val =
474 sensor->exposure->maximum;
475 rval = smiapp_set_ctrl(
476 sensor->exposure);
477 if (rval < 0)
478 return rval;
479 }
480
481 return smiapp_write(
482 client, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
483 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
484 + ctrl->val);
485
486 case V4L2_CID_HBLANK:
487 return smiapp_write(
488 client, SMIAPP_REG_U16_LINE_LENGTH_PCK,
489 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
490 + ctrl->val);
491
492 case V4L2_CID_LINK_FREQ:
493 if (sensor->streaming)
494 return -EBUSY;
495
496 return smiapp_pll_update(sensor);
497
498 default:
499 return -EINVAL;
500 }
501}
502
503static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
504 .s_ctrl = smiapp_set_ctrl,
505};
506
507static int smiapp_init_controls(struct smiapp_sensor *sensor)
508{
509 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
510 struct v4l2_ctrl_config cfg;
511 int rval;
512
513 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 7);
514 if (rval)
515 return rval;
516 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
517
518 sensor->analog_gain = v4l2_ctrl_new_std(
519 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
520 V4L2_CID_ANALOGUE_GAIN,
521 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN],
522 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX],
523 max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U),
524 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]);
525
526 /* Exposure limits will be updated soon, use just something here. */
527 sensor->exposure = v4l2_ctrl_new_std(
528 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
529 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
530
531 sensor->hflip = v4l2_ctrl_new_std(
532 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
533 V4L2_CID_HFLIP, 0, 1, 1, 0);
534 sensor->vflip = v4l2_ctrl_new_std(
535 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
536 V4L2_CID_VFLIP, 0, 1, 1, 0);
537
538 sensor->vblank = v4l2_ctrl_new_std(
539 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
540 V4L2_CID_VBLANK, 0, 1, 1, 0);
541
542 if (sensor->vblank)
543 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
544
545 sensor->hblank = v4l2_ctrl_new_std(
546 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
547 V4L2_CID_HBLANK, 0, 1, 1, 0);
548
549 if (sensor->hblank)
550 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
551
552 sensor->pixel_rate_parray = v4l2_ctrl_new_std(
553 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
554 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
555
556 if (sensor->pixel_array->ctrl_handler.error) {
557 dev_err(&client->dev,
558 "pixel array controls initialization failed (%d)\n",
559 sensor->pixel_array->ctrl_handler.error);
560 rval = sensor->pixel_array->ctrl_handler.error;
561 goto error;
562 }
563
564 sensor->pixel_array->sd.ctrl_handler =
565 &sensor->pixel_array->ctrl_handler;
566
567 v4l2_ctrl_cluster(2, &sensor->hflip);
568
569 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
570 if (rval)
571 goto error;
572 sensor->src->ctrl_handler.lock = &sensor->mutex;
573
574 memset(&cfg, 0, sizeof(cfg));
575
576 cfg.ops = &smiapp_ctrl_ops;
577 cfg.id = V4L2_CID_LINK_FREQ;
578 cfg.type = V4L2_CTRL_TYPE_INTEGER_MENU;
579 while (sensor->platform_data->op_sys_clock[cfg.max + 1])
580 cfg.max++;
581 cfg.qmenu_int = sensor->platform_data->op_sys_clock;
582
583 sensor->link_freq = v4l2_ctrl_new_custom(
584 &sensor->src->ctrl_handler, &cfg, NULL);
585
586 sensor->pixel_rate_csi = v4l2_ctrl_new_std(
587 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
588 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
589
590 if (sensor->src->ctrl_handler.error) {
591 dev_err(&client->dev,
592 "src controls initialization failed (%d)\n",
593 sensor->src->ctrl_handler.error);
594 rval = sensor->src->ctrl_handler.error;
595 goto error;
596 }
597
598 sensor->src->sd.ctrl_handler =
599 &sensor->src->ctrl_handler;
600
601 return 0;
602
603error:
604 v4l2_ctrl_handler_free(&sensor->pixel_array->ctrl_handler);
605 v4l2_ctrl_handler_free(&sensor->src->ctrl_handler);
606
607 return rval;
608}
609
610static void smiapp_free_controls(struct smiapp_sensor *sensor)
611{
612 unsigned int i;
613
614 for (i = 0; i < sensor->ssds_used; i++)
615 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
616}
617
618static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
619 unsigned int n)
620{
621 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
622 unsigned int i;
623 u32 val;
624 int rval;
625
626 for (i = 0; i < n; i++) {
627 rval = smiapp_read(
628 client, smiapp_reg_limits[limit[i]].addr, &val);
629 if (rval)
630 return rval;
631 sensor->limits[limit[i]] = val;
632 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %d, 0x%x\n",
633 smiapp_reg_limits[limit[i]].addr,
634 smiapp_reg_limits[limit[i]].what, val, val);
635 }
636
637 return 0;
638}
639
640static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
641{
642 unsigned int i;
643 int rval;
644
645 for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
646 rval = smiapp_get_limits(sensor, &i, 1);
647 if (rval < 0)
648 return rval;
649 }
650
651 if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
652 smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
653
654 return 0;
655}
656
657static int smiapp_get_limits_binning(struct smiapp_sensor *sensor)
658{
659 static u32 const limits[] = {
660 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN,
661 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN,
662 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN,
663 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN,
664 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN,
665 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN,
666 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN,
667 };
668 static u32 const limits_replace[] = {
669 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES,
670 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES,
671 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK,
672 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK,
673 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK,
674 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN,
675 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN,
676 };
677
678 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
679 SMIAPP_BINNING_CAPABILITY_NO) {
680 unsigned int i;
681
682 for (i = 0; i < ARRAY_SIZE(limits); i++)
683 sensor->limits[limits[i]] =
684 sensor->limits[limits_replace[i]];
685
686 return 0;
687 }
688
689 return smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits));
690}
691
692static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
693{
694 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
695 unsigned int type, n;
696 unsigned int i, pixel_order;
697 int rval;
698
699 rval = smiapp_read(
700 client, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
701 if (rval)
702 return rval;
703
704 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
705
706 rval = smiapp_read(client, SMIAPP_REG_U8_PIXEL_ORDER,
707 &pixel_order);
708 if (rval)
709 return rval;
710
711 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
712 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
713 return -EINVAL;
714 }
715
716 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
717 pixel_order_str[pixel_order]);
718
719 switch (type) {
720 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
721 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
722 break;
723 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
724 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
725 break;
726 default:
727 return -EINVAL;
728 }
729
730 sensor->default_pixel_order = pixel_order;
731 sensor->mbus_frame_fmts = 0;
732
733 for (i = 0; i < n; i++) {
734 unsigned int fmt, j;
735
736 rval = smiapp_read(
737 client,
738 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
739 if (rval)
740 return rval;
741
742 dev_dbg(&client->dev, "bpp %d, compressed %d\n",
743 fmt >> 8, (u8)fmt);
744
745 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
746 const struct smiapp_csi_data_format *f =
747 &smiapp_csi_data_formats[j];
748
749 if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
750 continue;
751
752 if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
753 continue;
754
755 dev_dbg(&client->dev, "jolly good! %d\n", j);
756
757 sensor->default_mbus_frame_fmts |= 1 << j;
758 if (!sensor->csi_format) {
759 sensor->csi_format = f;
760 sensor->internal_csi_format = f;
761 }
762 }
763 }
764
765 if (!sensor->csi_format) {
766 dev_err(&client->dev, "no supported mbus code found\n");
767 return -EINVAL;
768 }
769
770 smiapp_update_mbus_formats(sensor);
771
772 return 0;
773}
774
775static void smiapp_update_blanking(struct smiapp_sensor *sensor)
776{
777 struct v4l2_ctrl *vblank = sensor->vblank;
778 struct v4l2_ctrl *hblank = sensor->hblank;
779
780 vblank->minimum =
781 max_t(int,
782 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
783 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
784 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
785 vblank->maximum =
786 sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
787 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
788
789 vblank->val = clamp_t(int, vblank->val,
790 vblank->minimum, vblank->maximum);
791 vblank->default_value = vblank->minimum;
792 vblank->val = vblank->val;
793 vblank->cur.val = vblank->val;
794
795 hblank->minimum =
796 max_t(int,
797 sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
798 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
799 sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
800 hblank->maximum =
801 sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
802 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
803
804 hblank->val = clamp_t(int, hblank->val,
805 hblank->minimum, hblank->maximum);
806 hblank->default_value = hblank->minimum;
807 hblank->val = hblank->val;
808 hblank->cur.val = hblank->val;
809
810 __smiapp_update_exposure_limits(sensor);
811}
812
813static int smiapp_update_mode(struct smiapp_sensor *sensor)
814{
815 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
816 unsigned int binning_mode;
817 int rval;
818
819 dev_dbg(&client->dev, "frame size: %dx%d\n",
820 sensor->src->crop[SMIAPP_PAD_SRC].width,
821 sensor->src->crop[SMIAPP_PAD_SRC].height);
822 dev_dbg(&client->dev, "csi format width: %d\n",
823 sensor->csi_format->width);
824
825 /* Binning has to be set up here; it affects limits */
826 if (sensor->binning_horizontal == 1 &&
827 sensor->binning_vertical == 1) {
828 binning_mode = 0;
829 } else {
830 u8 binning_type =
831 (sensor->binning_horizontal << 4)
832 | sensor->binning_vertical;
833
834 rval = smiapp_write(
835 client, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
836 if (rval < 0)
837 return rval;
838
839 binning_mode = 1;
840 }
841 rval = smiapp_write(client, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
842 if (rval < 0)
843 return rval;
844
845 /* Get updated limits due to binning */
846 rval = smiapp_get_limits_binning(sensor);
847 if (rval < 0)
848 return rval;
849
850 rval = smiapp_pll_update(sensor);
851 if (rval < 0)
852 return rval;
853
854 /* Output from pixel array, including blanking */
855 smiapp_update_blanking(sensor);
856
857 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
858 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
859
860 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
861 sensor->pll.vt_pix_clk_freq_hz /
862 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
863 + sensor->hblank->val) *
864 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
865 + sensor->vblank->val) / 100));
866
867 return 0;
868}
869
870/*
871 *
872 * SMIA++ NVM handling
873 *
874 */
875static int smiapp_read_nvm(struct smiapp_sensor *sensor,
876 unsigned char *nvm)
877{
878 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
879 u32 i, s, p, np, v;
880 int rval, rval2;
881
882 np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
883 for (p = 0; p < np; p++) {
884 rval = smiapp_write(
885 client,
886 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
887 if (rval)
888 goto out;
889
890 rval = smiapp_write(client,
891 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
892 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
893 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
894 if (rval)
895 goto out;
896
897 for (i = 0; i < 1000; i++) {
898 rval = smiapp_read(
899 client,
900 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);
901
902 if (rval)
903 goto out;
904
905 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
906 break;
907
908 if (--i == 0) {
909 rval = -ETIMEDOUT;
910 goto out;
911 }
912
913 }
914
915 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
916 rval = smiapp_read(
917 client,
918 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
919 &v);
920 if (rval)
921 goto out;
922
923 *nvm++ = v;
924 }
925 }
926
927out:
928 rval2 = smiapp_write(client, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
929 if (rval < 0)
930 return rval;
931 else
932 return rval2;
933}
934
935/*
936 *
937 * SMIA++ CCI address control
938 *
939 */
940static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
941{
942 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
943 int rval;
944 u32 val;
945
946 client->addr = sensor->platform_data->i2c_addr_dfl;
947
948 rval = smiapp_write(client,
949 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
950 sensor->platform_data->i2c_addr_alt << 1);
951 if (rval)
952 return rval;
953
954 client->addr = sensor->platform_data->i2c_addr_alt;
955
956 /* verify addr change went ok */
957 rval = smiapp_read(client, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
958 if (rval)
959 return rval;
960
961 if (val != sensor->platform_data->i2c_addr_alt << 1)
962 return -ENODEV;
963
964 return 0;
965}
966
967/*
968 *
969 * SMIA++ Mode Control
970 *
971 */
972static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
973{
974 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
975 struct smiapp_flash_strobe_parms *strobe_setup;
976 unsigned int ext_freq = sensor->platform_data->ext_clk;
977 u32 tmp;
978 u32 strobe_adjustment;
979 u32 strobe_width_high_rs;
980 int rval;
981
982 strobe_setup = sensor->platform_data->strobe_setup;
983
984 /*
985 * How to calculate registers related to strobe length. Please
986 * do not change, or if you do at least know what you're
987 * doing. :-)
988 *
989 * Sakari Ailus <sakari.ailus@maxwell.research.nokia.com> 2010-10-25
990 *
991 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
992 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
993 *
994 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
995 * flash_strobe_adjustment E N, [1 - 0xff]
996 *
997 * The formula above is written as below to keep it on one
998 * line:
999 *
1000 * l / 10^6 = w / e * a
1001 *
1002 * Let's mark w * a by x:
1003 *
1004 * x = w * a
1005 *
1006 * Thus, we get:
1007 *
1008 * x = l * e / 10^6
1009 *
1010 * The strobe width must be at least as long as requested,
1011 * thus rounding upwards is needed.
1012 *
1013 * x = (l * e + 10^6 - 1) / 10^6
1014 * -----------------------------
1015 *
1016 * Maximum possible accuracy is wanted at all times. Thus keep
1017 * a as small as possible.
1018 *
1019 * Calculate a, assuming maximum w, with rounding upwards:
1020 *
1021 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1022 * -------------------------------------
1023 *
1024 * Thus, we also get w, with that a, with rounding upwards:
1025 *
1026 * w = (x + a - 1) / a
1027 * -------------------
1028 *
1029 * To get limits:
1030 *
1031 * x E [1, (2^16 - 1) * (2^8 - 1)]
1032 *
1033 * Substituting maximum x to the original formula (with rounding),
1034 * the maximum l is thus
1035 *
1036 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1037 *
1038 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1039 * --------------------------------------------------
1040 *
1041 * flash_strobe_length must be clamped between 1 and
1042 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1043 *
1044 * Then,
1045 *
1046 * flash_strobe_adjustment = ((flash_strobe_length *
1047 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1048 *
1049 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1050 * EXTCLK freq + 10^6 - 1) / 10^6 +
1051 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1052 */
1053 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1054 1000000 + 1, ext_freq);
1055 strobe_setup->strobe_width_high_us =
1056 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1057
1058 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1059 1000000 - 1), 1000000ULL);
1060 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1061 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1062 strobe_adjustment;
1063
1064 rval = smiapp_write(client, SMIAPP_REG_U8_FLASH_MODE_RS,
1065 strobe_setup->mode);
1066 if (rval < 0)
1067 goto out;
1068
1069 rval = smiapp_write(client, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1070 strobe_adjustment);
1071 if (rval < 0)
1072 goto out;
1073
1074 rval = smiapp_write(
1075 client, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1076 strobe_width_high_rs);
1077 if (rval < 0)
1078 goto out;
1079
1080 rval = smiapp_write(client, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1081 strobe_setup->strobe_delay);
1082 if (rval < 0)
1083 goto out;
1084
1085 rval = smiapp_write(client, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1086 strobe_setup->stobe_start_point);
1087 if (rval < 0)
1088 goto out;
1089
1090 rval = smiapp_write(client, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1091 strobe_setup->trigger);
1092
1093out:
1094 sensor->platform_data->strobe_setup->trigger = 0;
1095
1096 return rval;
1097}
1098
1099/* -----------------------------------------------------------------------------
1100 * Power management
1101 */
1102
1103static int smiapp_power_on(struct smiapp_sensor *sensor)
1104{
1105 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1106 unsigned int sleep;
1107 int rval;
1108
1109 rval = regulator_enable(sensor->vana);
1110 if (rval) {
1111 dev_err(&client->dev, "failed to enable vana regulator\n");
1112 return rval;
1113 }
1114 usleep_range(1000, 1000);
1115
1116 rval = sensor->platform_data->set_xclk(&sensor->src->sd,
1117 sensor->platform_data->ext_clk);
1118 if (rval < 0) {
1119 dev_dbg(&client->dev, "failed to set xclk\n");
1120 goto out_xclk_fail;
1121 }
1122 usleep_range(1000, 1000);
1123
1124 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1125 gpio_set_value(sensor->platform_data->xshutdown, 1);
1126
1127 sleep = SMIAPP_RESET_DELAY(sensor->platform_data->ext_clk);
1128 usleep_range(sleep, sleep);
1129
1130 /*
1131 * Failures to respond to the address change command have been noticed.
1132 * Those failures seem to be caused by the sensor requiring a longer
1133 * boot time than advertised. An additional 10ms delay seems to work
1134 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1135 * unnecessary. The failures need to be investigated to find a proper
1136 * fix, and a delay will likely need to be added here if the I2C write
1137 * retry hack is reverted before the root cause of the boot time issue
1138 * is found.
1139 */
1140
1141 if (sensor->platform_data->i2c_addr_alt) {
1142 rval = smiapp_change_cci_addr(sensor);
1143 if (rval) {
1144 dev_err(&client->dev, "cci address change error\n");
1145 goto out_cci_addr_fail;
1146 }
1147 }
1148
1149 rval = smiapp_write(client, SMIAPP_REG_U8_SOFTWARE_RESET,
1150 SMIAPP_SOFTWARE_RESET);
1151 if (rval < 0) {
1152 dev_err(&client->dev, "software reset failed\n");
1153 goto out_cci_addr_fail;
1154 }
1155
1156 if (sensor->platform_data->i2c_addr_alt) {
1157 rval = smiapp_change_cci_addr(sensor);
1158 if (rval) {
1159 dev_err(&client->dev, "cci address change error\n");
1160 goto out_cci_addr_fail;
1161 }
1162 }
1163
1164 rval = smiapp_write(client, SMIAPP_REG_U16_COMPRESSION_MODE,
1165 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1166 if (rval) {
1167 dev_err(&client->dev, "compression mode set failed\n");
1168 goto out_cci_addr_fail;
1169 }
1170
1171 rval = smiapp_write(
1172 client, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1173 sensor->platform_data->ext_clk / (1000000 / (1 << 8)));
1174 if (rval) {
1175 dev_err(&client->dev, "extclk frequency set failed\n");
1176 goto out_cci_addr_fail;
1177 }
1178
1179 rval = smiapp_write(client, SMIAPP_REG_U8_CSI_LANE_MODE,
1180 sensor->platform_data->lanes - 1);
1181 if (rval) {
1182 dev_err(&client->dev, "csi lane mode set failed\n");
1183 goto out_cci_addr_fail;
1184 }
1185
1186 rval = smiapp_write(client, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1187 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1188 if (rval) {
1189 dev_err(&client->dev, "fast standby set failed\n");
1190 goto out_cci_addr_fail;
1191 }
1192
1193 rval = smiapp_write(client, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1194 sensor->platform_data->csi_signalling_mode);
1195 if (rval) {
1196 dev_err(&client->dev, "csi signalling mode set failed\n");
1197 goto out_cci_addr_fail;
1198 }
1199
1200 /* DPHY control done by sensor based on requested link rate */
1201 rval = smiapp_write(client, SMIAPP_REG_U8_DPHY_CTRL,
1202 SMIAPP_DPHY_CTRL_UI);
1203 if (rval < 0)
1204 return rval;
1205
1206 rval = smiapp_call_quirk(sensor, post_poweron);
1207 if (rval) {
1208 dev_err(&client->dev, "post_poweron quirks failed\n");
1209 goto out_cci_addr_fail;
1210 }
1211
1212 /* Are we still initialising...? If yes, return here. */
1213 if (!sensor->pixel_array)
1214 return 0;
1215
1216 rval = v4l2_ctrl_handler_setup(
1217 &sensor->pixel_array->ctrl_handler);
1218 if (rval)
1219 goto out_cci_addr_fail;
1220
1221 rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1222 if (rval)
1223 goto out_cci_addr_fail;
1224
1225 mutex_lock(&sensor->mutex);
1226 rval = smiapp_update_mode(sensor);
1227 mutex_unlock(&sensor->mutex);
1228 if (rval < 0)
1229 goto out_cci_addr_fail;
1230
1231 return 0;
1232
1233out_cci_addr_fail:
1234 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1235 gpio_set_value(sensor->platform_data->xshutdown, 0);
1236 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1237
1238out_xclk_fail:
1239 regulator_disable(sensor->vana);
1240 return rval;
1241}
1242
1243static void smiapp_power_off(struct smiapp_sensor *sensor)
1244{
1245 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1246
1247 /*
1248 * Currently power/clock to lens are enable/disabled separately
1249 * but they are essentially the same signals. So if the sensor is
1250 * powered off while the lens is powered on the sensor does not
1251 * really see a power off and next time the cci address change
1252 * will fail. So do a soft reset explicitly here.
1253 */
1254 if (sensor->platform_data->i2c_addr_alt)
1255 smiapp_write(client,
1256 SMIAPP_REG_U8_SOFTWARE_RESET,
1257 SMIAPP_SOFTWARE_RESET);
1258
1259 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1260 gpio_set_value(sensor->platform_data->xshutdown, 0);
1261 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1262 usleep_range(5000, 5000);
1263 regulator_disable(sensor->vana);
1264 sensor->streaming = 0;
1265}
1266
1267static int smiapp_set_power(struct v4l2_subdev *subdev, int on)
1268{
1269 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1270 int ret = 0;
1271
1272 mutex_lock(&sensor->power_mutex);
1273
1274 /*
1275 * If the power count is modified from 0 to != 0 or from != 0
1276 * to 0, update the power state.
1277 */
1278 if (!sensor->power_count == !on)
1279 goto out;
1280
1281 if (on) {
1282 /* Power on and perform initialisation. */
1283 ret = smiapp_power_on(sensor);
1284 if (ret < 0)
1285 goto out;
1286 } else {
1287 smiapp_power_off(sensor);
1288 }
1289
1290 /* Update the power count. */
1291 sensor->power_count += on ? 1 : -1;
1292 WARN_ON(sensor->power_count < 0);
1293
1294out:
1295 mutex_unlock(&sensor->power_mutex);
1296 return ret;
1297}
1298
1299/* -----------------------------------------------------------------------------
1300 * Video stream management
1301 */
1302
1303static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1304{
1305 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1306 int rval;
1307
1308 mutex_lock(&sensor->mutex);
1309
1310 rval = smiapp_write(client, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1311 (sensor->csi_format->width << 8) |
1312 sensor->csi_format->compressed);
1313 if (rval)
1314 goto out;
1315
1316 rval = smiapp_pll_configure(sensor);
1317 if (rval)
1318 goto out;
1319
1320 /* Analog crop start coordinates */
1321 rval = smiapp_write(client, SMIAPP_REG_U16_X_ADDR_START,
1322 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1323 if (rval < 0)
1324 goto out;
1325
1326 rval = smiapp_write(client, SMIAPP_REG_U16_Y_ADDR_START,
1327 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1328 if (rval < 0)
1329 goto out;
1330
1331 /* Analog crop end coordinates */
1332 rval = smiapp_write(
1333 client, SMIAPP_REG_U16_X_ADDR_END,
1334 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1335 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1336 if (rval < 0)
1337 goto out;
1338
1339 rval = smiapp_write(
1340 client, SMIAPP_REG_U16_Y_ADDR_END,
1341 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1342 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1343 if (rval < 0)
1344 goto out;
1345
1346 /*
1347 * Output from pixel array, including blanking, is set using
1348 * controls below. No need to set here.
1349 */
1350
1351 /* Digital crop */
1352 if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1353 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1354 rval = smiapp_write(
1355 client, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1356 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1357 if (rval < 0)
1358 goto out;
1359
1360 rval = smiapp_write(
1361 client, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1362 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1363 if (rval < 0)
1364 goto out;
1365
1366 rval = smiapp_write(
1367 client, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1368 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1369 if (rval < 0)
1370 goto out;
1371
1372 rval = smiapp_write(
1373 client, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1374 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1375 if (rval < 0)
1376 goto out;
1377 }
1378
1379 /* Scaling */
1380 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1381 != SMIAPP_SCALING_CAPABILITY_NONE) {
1382 rval = smiapp_write(client, SMIAPP_REG_U16_SCALING_MODE,
1383 sensor->scaling_mode);
1384 if (rval < 0)
1385 goto out;
1386
1387 rval = smiapp_write(client, SMIAPP_REG_U16_SCALE_M,
1388 sensor->scale_m);
1389 if (rval < 0)
1390 goto out;
1391 }
1392
1393 /* Output size from sensor */
1394 rval = smiapp_write(client, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1395 sensor->src->crop[SMIAPP_PAD_SRC].width);
1396 if (rval < 0)
1397 goto out;
1398 rval = smiapp_write(client, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1399 sensor->src->crop[SMIAPP_PAD_SRC].height);
1400 if (rval < 0)
1401 goto out;
1402
1403 if ((sensor->flash_capability &
1404 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1405 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1406 sensor->platform_data->strobe_setup != NULL &&
1407 sensor->platform_data->strobe_setup->trigger != 0) {
1408 rval = smiapp_setup_flash_strobe(sensor);
1409 if (rval)
1410 goto out;
1411 }
1412
1413 rval = smiapp_call_quirk(sensor, pre_streamon);
1414 if (rval) {
1415 dev_err(&client->dev, "pre_streamon quirks failed\n");
1416 goto out;
1417 }
1418
1419 rval = smiapp_write(client, SMIAPP_REG_U8_MODE_SELECT,
1420 SMIAPP_MODE_SELECT_STREAMING);
1421
1422out:
1423 mutex_unlock(&sensor->mutex);
1424
1425 return rval;
1426}
1427
1428static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1429{
1430 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1431 int rval;
1432
1433 mutex_lock(&sensor->mutex);
1434 rval = smiapp_write(client, SMIAPP_REG_U8_MODE_SELECT,
1435 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1436 if (rval)
1437 goto out;
1438
1439 rval = smiapp_call_quirk(sensor, post_streamoff);
1440 if (rval)
1441 dev_err(&client->dev, "post_streamoff quirks failed\n");
1442
1443out:
1444 mutex_unlock(&sensor->mutex);
1445 return rval;
1446}
1447
1448/* -----------------------------------------------------------------------------
1449 * V4L2 subdev video operations
1450 */
1451
1452static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1453{
1454 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1455 int rval;
1456
1457 if (sensor->streaming == enable)
1458 return 0;
1459
1460 if (enable) {
1461 sensor->streaming = 1;
1462 rval = smiapp_start_streaming(sensor);
1463 if (rval < 0)
1464 sensor->streaming = 0;
1465 } else {
1466 rval = smiapp_stop_streaming(sensor);
1467 sensor->streaming = 0;
1468 }
1469
1470 return rval;
1471}
1472
1473static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1474 struct v4l2_subdev_fh *fh,
1475 struct v4l2_subdev_mbus_code_enum *code)
1476{
1477 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1478 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1479 unsigned int i;
1480 int idx = -1;
1481 int rval = -EINVAL;
1482
1483 mutex_lock(&sensor->mutex);
1484
1485 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1486 subdev->name, code->pad, code->index);
1487
1488 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1489 if (code->index)
1490 goto out;
1491
1492 code->code = sensor->internal_csi_format->code;
1493 rval = 0;
1494 goto out;
1495 }
1496
1497 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1498 if (sensor->mbus_frame_fmts & (1 << i))
1499 idx++;
1500
1501 if (idx == code->index) {
1502 code->code = smiapp_csi_data_formats[i].code;
1503 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1504 code->index, i, code->code);
1505 rval = 0;
1506 break;
1507 }
1508 }
1509
1510out:
1511 mutex_unlock(&sensor->mutex);
1512
1513 return rval;
1514}
1515
1516static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1517 unsigned int pad)
1518{
1519 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1520
1521 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1522 return sensor->csi_format->code;
1523 else
1524 return sensor->internal_csi_format->code;
1525}
1526
1527static int __smiapp_get_format(struct v4l2_subdev *subdev,
1528 struct v4l2_subdev_fh *fh,
1529 struct v4l2_subdev_format *fmt)
1530{
1531 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1532
1533 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1534 fmt->format = *v4l2_subdev_get_try_format(fh, fmt->pad);
1535 } else {
1536 struct v4l2_rect *r;
1537
1538 if (fmt->pad == ssd->source_pad)
1539 r = &ssd->crop[ssd->source_pad];
1540 else
1541 r = &ssd->sink_fmt;
1542
1543 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1544 fmt->format.width = r->width;
1545 fmt->format.height = r->height;
1546 }
1547
1548 return 0;
1549}
1550
1551static int smiapp_get_format(struct v4l2_subdev *subdev,
1552 struct v4l2_subdev_fh *fh,
1553 struct v4l2_subdev_format *fmt)
1554{
1555 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1556 int rval;
1557
1558 mutex_lock(&sensor->mutex);
1559 rval = __smiapp_get_format(subdev, fh, fmt);
1560 mutex_unlock(&sensor->mutex);
1561
1562 return rval;
1563}
1564
1565static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1566 struct v4l2_subdev_fh *fh,
1567 struct v4l2_rect **crops,
1568 struct v4l2_rect **comps, int which)
1569{
1570 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1571 unsigned int i;
1572
1573 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1574 if (crops)
1575 for (i = 0; i < subdev->entity.num_pads; i++)
1576 crops[i] = &ssd->crop[i];
1577 if (comps)
1578 *comps = &ssd->compose;
1579 } else {
1580 if (crops) {
1581 for (i = 0; i < subdev->entity.num_pads; i++) {
1582 crops[i] = v4l2_subdev_get_try_crop(fh, i);
1583 BUG_ON(!crops[i]);
1584 }
1585 }
1586 if (comps) {
1587 *comps = v4l2_subdev_get_try_compose(fh,
1588 SMIAPP_PAD_SINK);
1589 BUG_ON(!*comps);
1590 }
1591 }
1592}
1593
1594/* Changes require propagation only on sink pad. */
1595static void smiapp_propagate(struct v4l2_subdev *subdev,
1596 struct v4l2_subdev_fh *fh, int which,
1597 int target)
1598{
1599 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1600 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1601 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1602
1603 smiapp_get_crop_compose(subdev, fh, crops, &comp, which);
1604
1605 switch (target) {
1606 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
1607 comp->width = crops[SMIAPP_PAD_SINK]->width;
1608 comp->height = crops[SMIAPP_PAD_SINK]->height;
1609 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1610 if (ssd == sensor->scaler) {
1611 sensor->scale_m =
1612 sensor->limits[
1613 SMIAPP_LIMIT_SCALER_N_MIN];
1614 sensor->scaling_mode =
1615 SMIAPP_SCALING_MODE_NONE;
1616 } else if (ssd == sensor->binner) {
1617 sensor->binning_horizontal = 1;
1618 sensor->binning_vertical = 1;
1619 }
1620 }
1621 /* Fall through */
1622 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
1623 *crops[SMIAPP_PAD_SRC] = *comp;
1624 break;
1625 default:
1626 BUG();
1627 }
1628}
1629
1630static const struct smiapp_csi_data_format
1631*smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1632{
1633 const struct smiapp_csi_data_format *csi_format = sensor->csi_format;
1634 unsigned int i;
1635
1636 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1637 if (sensor->mbus_frame_fmts & (1 << i)
1638 && smiapp_csi_data_formats[i].code == code)
1639 return &smiapp_csi_data_formats[i];
1640 }
1641
1642 return csi_format;
1643}
1644
1645static int smiapp_set_format(struct v4l2_subdev *subdev,
1646 struct v4l2_subdev_fh *fh,
1647 struct v4l2_subdev_format *fmt)
1648{
1649 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1650 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1651 struct v4l2_rect *crops[SMIAPP_PADS];
1652
1653 mutex_lock(&sensor->mutex);
1654
1655 /*
1656 * Media bus code is changeable on src subdev's source pad. On
1657 * other source pads we just get format here.
1658 */
1659 if (fmt->pad == ssd->source_pad) {
1660 u32 code = fmt->format.code;
1661 int rval = __smiapp_get_format(subdev, fh, fmt);
1662
1663 if (!rval && subdev == &sensor->src->sd) {
1664 const struct smiapp_csi_data_format *csi_format =
1665 smiapp_validate_csi_data_format(sensor, code);
1666 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1667 sensor->csi_format = csi_format;
1668 fmt->format.code = csi_format->code;
1669 }
1670
1671 mutex_unlock(&sensor->mutex);
1672 return rval;
1673 }
1674
1675 /* Sink pad. Width and height are changeable here. */
1676 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1677 fmt->format.width &= ~1;
1678 fmt->format.height &= ~1;
1679
1680 fmt->format.width =
1681 clamp(fmt->format.width,
1682 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
1683 sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
1684 fmt->format.height =
1685 clamp(fmt->format.height,
1686 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
1687 sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
1688
1689 smiapp_get_crop_compose(subdev, fh, crops, NULL, fmt->which);
1690
1691 crops[ssd->sink_pad]->left = 0;
1692 crops[ssd->sink_pad]->top = 0;
1693 crops[ssd->sink_pad]->width = fmt->format.width;
1694 crops[ssd->sink_pad]->height = fmt->format.height;
1695 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1696 ssd->sink_fmt = *crops[ssd->sink_pad];
1697 smiapp_propagate(subdev, fh, fmt->which,
1698 V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL);
1699
1700 mutex_unlock(&sensor->mutex);
1701
1702 return 0;
1703}
1704
1705/*
1706 * Calculate goodness of scaled image size compared to expected image
1707 * size and flags provided.
1708 */
1709#define SCALING_GOODNESS 100000
1710#define SCALING_GOODNESS_EXTREME 100000000
1711static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1712 int h, int ask_h, u32 flags)
1713{
1714 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1715 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1716 int val = 0;
1717
1718 w &= ~1;
1719 ask_w &= ~1;
1720 h &= ~1;
1721 ask_h &= ~1;
1722
1723 if (flags & V4L2_SUBDEV_SEL_FLAG_SIZE_GE) {
1724 if (w < ask_w)
1725 val -= SCALING_GOODNESS;
1726 if (h < ask_h)
1727 val -= SCALING_GOODNESS;
1728 }
1729
1730 if (flags & V4L2_SUBDEV_SEL_FLAG_SIZE_LE) {
1731 if (w > ask_w)
1732 val -= SCALING_GOODNESS;
1733 if (h > ask_h)
1734 val -= SCALING_GOODNESS;
1735 }
1736
1737 val -= abs(w - ask_w);
1738 val -= abs(h - ask_h);
1739
1740 if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
1741 val -= SCALING_GOODNESS_EXTREME;
1742
1743 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1744 w, ask_h, h, ask_h, val);
1745
1746 return val;
1747}
1748
1749static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1750 struct v4l2_subdev_fh *fh,
1751 struct v4l2_subdev_selection *sel,
1752 struct v4l2_rect **crops,
1753 struct v4l2_rect *comp)
1754{
1755 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1756 unsigned int i;
1757 unsigned int binh = 1, binv = 1;
1758 unsigned int best = scaling_goodness(
1759 subdev,
1760 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1761 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1762
1763 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1764 int this = scaling_goodness(
1765 subdev,
1766 crops[SMIAPP_PAD_SINK]->width
1767 / sensor->binning_subtypes[i].horizontal,
1768 sel->r.width,
1769 crops[SMIAPP_PAD_SINK]->height
1770 / sensor->binning_subtypes[i].vertical,
1771 sel->r.height, sel->flags);
1772
1773 if (this > best) {
1774 binh = sensor->binning_subtypes[i].horizontal;
1775 binv = sensor->binning_subtypes[i].vertical;
1776 best = this;
1777 }
1778 }
1779 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1780 sensor->binning_vertical = binv;
1781 sensor->binning_horizontal = binh;
1782 }
1783
1784 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1785 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1786}
1787
1788/*
1789 * Calculate best scaling ratio and mode for given output resolution.
1790 *
1791 * Try all of these: horizontal ratio, vertical ratio and smallest
1792 * size possible (horizontally).
1793 *
1794 * Also try whether horizontal scaler or full scaler gives a better
1795 * result.
1796 */
1797static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1798 struct v4l2_subdev_fh *fh,
1799 struct v4l2_subdev_selection *sel,
1800 struct v4l2_rect **crops,
1801 struct v4l2_rect *comp)
1802{
1803 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1804 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1805 u32 min, max, a, b, max_m;
1806 u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
1807 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1808 u32 try[4];
1809 u32 ntry = 0;
1810 unsigned int i;
1811 int best = INT_MIN;
1812
1813 sel->r.width = min_t(unsigned int, sel->r.width,
1814 crops[SMIAPP_PAD_SINK]->width);
1815 sel->r.height = min_t(unsigned int, sel->r.height,
1816 crops[SMIAPP_PAD_SINK]->height);
1817
1818 a = crops[SMIAPP_PAD_SINK]->width
1819 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
1820 b = crops[SMIAPP_PAD_SINK]->height
1821 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
1822 max_m = crops[SMIAPP_PAD_SINK]->width
1823 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
1824 / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
1825
1826 a = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1827 max(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1828 b = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1829 max(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1830 max_m = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1831 max(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1832
1833 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1834
1835 min = min(max_m, min(a, b));
1836 max = min(max_m, max(a, b));
1837
1838 try[ntry] = min;
1839 ntry++;
1840 if (min != max) {
1841 try[ntry] = max;
1842 ntry++;
1843 }
1844 if (max != max_m) {
1845 try[ntry] = min + 1;
1846 ntry++;
1847 if (min != max) {
1848 try[ntry] = max + 1;
1849 ntry++;
1850 }
1851 }
1852
1853 for (i = 0; i < ntry; i++) {
1854 int this = scaling_goodness(
1855 subdev,
1856 crops[SMIAPP_PAD_SINK]->width
1857 / try[i]
1858 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1859 sel->r.width,
1860 crops[SMIAPP_PAD_SINK]->height,
1861 sel->r.height,
1862 sel->flags);
1863
1864 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
1865
1866 if (this > best) {
1867 scale_m = try[i];
1868 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1869 best = this;
1870 }
1871
1872 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1873 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
1874 continue;
1875
1876 this = scaling_goodness(
1877 subdev, crops[SMIAPP_PAD_SINK]->width
1878 / try[i]
1879 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1880 sel->r.width,
1881 crops[SMIAPP_PAD_SINK]->height
1882 / try[i]
1883 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1884 sel->r.height,
1885 sel->flags);
1886
1887 if (this > best) {
1888 scale_m = try[i];
1889 mode = SMIAPP_SCALING_MODE_BOTH;
1890 best = this;
1891 }
1892 }
1893
1894 sel->r.width =
1895 (crops[SMIAPP_PAD_SINK]->width
1896 / scale_m
1897 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
1898 if (mode == SMIAPP_SCALING_MODE_BOTH)
1899 sel->r.height =
1900 (crops[SMIAPP_PAD_SINK]->height
1901 / scale_m
1902 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
1903 & ~1;
1904 else
1905 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
1906
1907 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1908 sensor->scale_m = scale_m;
1909 sensor->scaling_mode = mode;
1910 }
1911}
1912/* We're only called on source pads. This function sets scaling. */
1913static int smiapp_set_compose(struct v4l2_subdev *subdev,
1914 struct v4l2_subdev_fh *fh,
1915 struct v4l2_subdev_selection *sel)
1916{
1917 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1918 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1919 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1920
1921 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
1922
1923 sel->r.top = 0;
1924 sel->r.left = 0;
1925
1926 if (ssd == sensor->binner)
1927 smiapp_set_compose_binner(subdev, fh, sel, crops, comp);
1928 else
1929 smiapp_set_compose_scaler(subdev, fh, sel, crops, comp);
1930
1931 *comp = sel->r;
1932 smiapp_propagate(subdev, fh, sel->which,
1933 V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL);
1934
1935 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1936 return smiapp_update_mode(sensor);
1937
1938 return 0;
1939}
1940
1941static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
1942 struct v4l2_subdev_selection *sel)
1943{
1944 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1945 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1946
1947 /* We only implement crop in three places. */
1948 switch (sel->target) {
1949 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
1950 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
1951 if (ssd == sensor->pixel_array
1952 && sel->pad == SMIAPP_PA_PAD_SRC)
1953 return 0;
1954 if (ssd == sensor->src
1955 && sel->pad == SMIAPP_PAD_SRC)
1956 return 0;
1957 if (ssd == sensor->scaler
1958 && sel->pad == SMIAPP_PAD_SINK
1959 && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1960 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
1961 return 0;
1962 return -EINVAL;
1963 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
1964 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
1965 if (sel->pad == ssd->source_pad)
1966 return -EINVAL;
1967 if (ssd == sensor->binner)
1968 return 0;
1969 if (ssd == sensor->scaler
1970 && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1971 != SMIAPP_SCALING_CAPABILITY_NONE)
1972 return 0;
1973 /* Fall through */
1974 default:
1975 return -EINVAL;
1976 }
1977}
1978
1979static int smiapp_set_crop(struct v4l2_subdev *subdev,
1980 struct v4l2_subdev_fh *fh,
1981 struct v4l2_subdev_selection *sel)
1982{
1983 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1984 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1985 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
1986 struct v4l2_rect _r;
1987
1988 smiapp_get_crop_compose(subdev, fh, crops, NULL, sel->which);
1989
1990 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1991 if (sel->pad == ssd->sink_pad)
1992 src_size = &ssd->sink_fmt;
1993 else
1994 src_size = &ssd->compose;
1995 } else {
1996 if (sel->pad == ssd->sink_pad) {
1997 _r.left = 0;
1998 _r.top = 0;
1999 _r.width = v4l2_subdev_get_try_format(fh, sel->pad)
2000 ->width;
2001 _r.height = v4l2_subdev_get_try_format(fh, sel->pad)
2002 ->height;
2003 src_size = &_r;
2004 } else {
2005 src_size =
2006 v4l2_subdev_get_try_compose(
2007 fh, ssd->sink_pad);
2008 }
2009 }
2010
2011 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2012 sel->r.left = 0;
2013 sel->r.top = 0;
2014 }
2015
2016 sel->r.width = min(sel->r.width, src_size->width);
2017 sel->r.height = min(sel->r.height, src_size->height);
2018
2019 sel->r.left = min(sel->r.left, src_size->width - sel->r.width);
2020 sel->r.top = min(sel->r.top, src_size->height - sel->r.height);
2021
2022 *crops[sel->pad] = sel->r;
2023
2024 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2025 smiapp_propagate(subdev, fh, sel->which,
2026 V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL);
2027
2028 return 0;
2029}
2030
2031static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2032 struct v4l2_subdev_fh *fh,
2033 struct v4l2_subdev_selection *sel)
2034{
2035 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2036 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2037 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2038 struct v4l2_rect sink_fmt;
2039 int ret;
2040
2041 ret = __smiapp_sel_supported(subdev, sel);
2042 if (ret)
2043 return ret;
2044
2045 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
2046
2047 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2048 sink_fmt = ssd->sink_fmt;
2049 } else {
2050 struct v4l2_mbus_framefmt *fmt =
2051 v4l2_subdev_get_try_format(fh, ssd->sink_pad);
2052
2053 sink_fmt.left = 0;
2054 sink_fmt.top = 0;
2055 sink_fmt.width = fmt->width;
2056 sink_fmt.height = fmt->height;
2057 }
2058
2059 switch (sel->target) {
2060 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
2061 if (ssd == sensor->pixel_array) {
2062 sel->r.width =
2063 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2064 sel->r.height =
2065 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2066 } else if (sel->pad == ssd->sink_pad) {
2067 sel->r = sink_fmt;
2068 } else {
2069 sel->r = *comp;
2070 }
2071 break;
2072 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
2073 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
2074 sel->r = *crops[sel->pad];
2075 break;
2076 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
2077 sel->r = *comp;
2078 break;
2079 }
2080
2081 return 0;
2082}
2083
2084static int smiapp_get_selection(struct v4l2_subdev *subdev,
2085 struct v4l2_subdev_fh *fh,
2086 struct v4l2_subdev_selection *sel)
2087{
2088 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2089 int rval;
2090
2091 mutex_lock(&sensor->mutex);
2092 rval = __smiapp_get_selection(subdev, fh, sel);
2093 mutex_unlock(&sensor->mutex);
2094
2095 return rval;
2096}
2097static int smiapp_set_selection(struct v4l2_subdev *subdev,
2098 struct v4l2_subdev_fh *fh,
2099 struct v4l2_subdev_selection *sel)
2100{
2101 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2102 int ret;
2103
2104 ret = __smiapp_sel_supported(subdev, sel);
2105 if (ret)
2106 return ret;
2107
2108 mutex_lock(&sensor->mutex);
2109
2110 sel->r.left = max(0, sel->r.left & ~1);
2111 sel->r.top = max(0, sel->r.top & ~1);
2112 sel->r.width = max(0, SMIAPP_ALIGN_DIM(sel->r.width, sel->flags));
2113 sel->r.height = max(0, SMIAPP_ALIGN_DIM(sel->r.height, sel->flags));
2114
2115 sel->r.width = max_t(unsigned int,
2116 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
2117 sel->r.width);
2118 sel->r.height = max_t(unsigned int,
2119 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
2120 sel->r.height);
2121
2122 switch (sel->target) {
2123 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
2124 ret = smiapp_set_crop(subdev, fh, sel);
2125 break;
2126 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
2127 ret = smiapp_set_compose(subdev, fh, sel);
2128 break;
2129 default:
2130 BUG();
2131 }
2132
2133 mutex_unlock(&sensor->mutex);
2134 return ret;
2135}
2136
2137static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2138{
2139 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2140
2141 *frames = sensor->frame_skip;
2142 return 0;
2143}
2144
2145/* -----------------------------------------------------------------------------
2146 * sysfs attributes
2147 */
2148
2149static ssize_t
2150smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2151 char *buf)
2152{
2153 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2154 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2155 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2156 unsigned int nbytes;
2157
2158 if (!sensor->dev_init_done)
2159 return -EBUSY;
2160
2161 if (!sensor->nvm_size) {
2162 /* NVM not read yet - read it now */
2163 sensor->nvm_size = sensor->platform_data->nvm_size;
2164 if (smiapp_set_power(subdev, 1) < 0)
2165 return -ENODEV;
2166 if (smiapp_read_nvm(sensor, sensor->nvm)) {
2167 dev_err(&client->dev, "nvm read failed\n");
2168 return -ENODEV;
2169 }
2170 smiapp_set_power(subdev, 0);
2171 }
2172 /*
2173 * NVM is still way below a PAGE_SIZE, so we can safely
2174 * assume this for now.
2175 */
2176 nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
2177 memcpy(buf, sensor->nvm, nbytes);
2178
2179 return nbytes;
2180}
2181static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2182
2183/* -----------------------------------------------------------------------------
2184 * V4L2 subdev core operations
2185 */
2186
2187static int smiapp_identify_module(struct v4l2_subdev *subdev)
2188{
2189 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2190 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2191 struct smiapp_module_info *minfo = &sensor->minfo;
2192 unsigned int i;
2193 int rval = 0;
2194
2195 minfo->name = SMIAPP_NAME;
2196
2197 /* Module info */
2198 rval = smiapp_read(client, SMIAPP_REG_U8_MANUFACTURER_ID,
2199 &minfo->manufacturer_id);
2200 if (!rval)
2201 rval = smiapp_read(client, SMIAPP_REG_U16_MODEL_ID,
2202 &minfo->model_id);
2203 if (!rval)
2204 rval = smiapp_read(client, SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2205 &minfo->revision_number_major);
2206 if (!rval)
2207 rval = smiapp_read(client, SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2208 &minfo->revision_number_minor);
2209 if (!rval)
2210 rval = smiapp_read(client, SMIAPP_REG_U8_MODULE_DATE_YEAR,
2211 &minfo->module_year);
2212 if (!rval)
2213 rval = smiapp_read(client, SMIAPP_REG_U8_MODULE_DATE_MONTH,
2214 &minfo->module_month);
2215 if (!rval)
2216 rval = smiapp_read(client, SMIAPP_REG_U8_MODULE_DATE_DAY,
2217 &minfo->module_day);
2218
2219 /* Sensor info */
2220 if (!rval)
2221 rval = smiapp_read(client,
2222 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2223 &minfo->sensor_manufacturer_id);
2224 if (!rval)
2225 rval = smiapp_read(client, SMIAPP_REG_U16_SENSOR_MODEL_ID,
2226 &minfo->sensor_model_id);
2227 if (!rval)
2228 rval = smiapp_read(client,
2229 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2230 &minfo->sensor_revision_number);
2231 if (!rval)
2232 rval = smiapp_read(client,
2233 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2234 &minfo->sensor_firmware_version);
2235
2236 /* SMIA */
2237 if (!rval)
2238 rval = smiapp_read(client, SMIAPP_REG_U8_SMIA_VERSION,
2239 &minfo->smia_version);
2240 if (!rval)
2241 rval = smiapp_read(client, SMIAPP_REG_U8_SMIAPP_VERSION,
2242 &minfo->smiapp_version);
2243
2244 if (rval) {
2245 dev_err(&client->dev, "sensor detection failed\n");
2246 return -ENODEV;
2247 }
2248
2249 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2250 minfo->manufacturer_id, minfo->model_id);
2251
2252 dev_dbg(&client->dev,
2253 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2254 minfo->revision_number_major, minfo->revision_number_minor,
2255 minfo->module_year, minfo->module_month, minfo->module_day);
2256
2257 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2258 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2259
2260 dev_dbg(&client->dev,
2261 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2262 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2263
2264 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2265 minfo->smia_version, minfo->smiapp_version);
2266
2267 /*
2268 * Some modules have bad data in the lvalues below. Hope the
2269 * rvalues have better stuff. The lvalues are module
2270 * parameters whereas the rvalues are sensor parameters.
2271 */
2272 if (!minfo->manufacturer_id && !minfo->model_id) {
2273 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2274 minfo->model_id = minfo->sensor_model_id;
2275 minfo->revision_number_major = minfo->sensor_revision_number;
2276 }
2277
2278 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2279 if (smiapp_module_idents[i].manufacturer_id
2280 != minfo->manufacturer_id)
2281 continue;
2282 if (smiapp_module_idents[i].model_id != minfo->model_id)
2283 continue;
2284 if (smiapp_module_idents[i].flags
2285 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2286 if (smiapp_module_idents[i].revision_number_major
2287 < minfo->revision_number_major)
2288 continue;
2289 } else {
2290 if (smiapp_module_idents[i].revision_number_major
2291 != minfo->revision_number_major)
2292 continue;
2293 }
2294
2295 minfo->name = smiapp_module_idents[i].name;
2296 minfo->quirk = smiapp_module_idents[i].quirk;
2297 break;
2298 }
2299
2300 if (i >= ARRAY_SIZE(smiapp_module_idents))
2301 dev_warn(&client->dev,
2302 "no quirks for this module; let's hope it's fully compliant\n");
2303
2304 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2305 minfo->name, minfo->manufacturer_id, minfo->model_id,
2306 minfo->revision_number_major);
2307
2308 strlcpy(subdev->name, sensor->minfo.name, sizeof(subdev->name));
2309
2310 return 0;
2311}
2312
2313static const struct v4l2_subdev_ops smiapp_ops;
2314static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2315static const struct media_entity_operations smiapp_entity_ops;
2316
2317static int smiapp_registered(struct v4l2_subdev *subdev)
2318{
2319 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2320 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2321 struct smiapp_subdev *last = NULL;
2322 u32 tmp;
2323 unsigned int i;
2324 int rval;
2325
2326 sensor->vana = regulator_get(&client->dev, "VANA");
2327 if (IS_ERR(sensor->vana)) {
2328 dev_err(&client->dev, "could not get regulator for vana\n");
2329 return -ENODEV;
2330 }
2331
2332 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) {
2333 if (gpio_request_one(sensor->platform_data->xshutdown, 0,
2334 "SMIA++ xshutdown") != 0) {
2335 dev_err(&client->dev,
2336 "unable to acquire reset gpio %d\n",
2337 sensor->platform_data->xshutdown);
2338 rval = -ENODEV;
2339 goto out_gpio_request;
2340 }
2341 }
2342
2343 rval = smiapp_power_on(sensor);
2344 if (rval) {
2345 rval = -ENODEV;
2346 goto out_smiapp_power_on;
2347 }
2348
2349 rval = smiapp_identify_module(subdev);
2350 if (rval) {
2351 rval = -ENODEV;
2352 goto out_power_off;
2353 }
2354
2355 rval = smiapp_get_all_limits(sensor);
2356 if (rval) {
2357 rval = -ENODEV;
2358 goto out_power_off;
2359 }
2360
2361 /*
2362 * Handle Sensor Module orientation on the board.
2363 *
2364 * The application of H-FLIP and V-FLIP on the sensor is modified by
2365 * the sensor orientation on the board.
2366 *
2367 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2368 * both H-FLIP and V-FLIP for normal operation which also implies
2369 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2370 * controls will need to be internally inverted.
2371 *
2372 * Rotation also changes the bayer pattern.
2373 */
2374 if (sensor->platform_data->module_board_orient ==
2375 SMIAPP_MODULE_BOARD_ORIENT_180)
2376 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2377 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2378
2379 rval = smiapp_get_mbus_formats(sensor);
2380 if (rval) {
2381 rval = -ENODEV;
2382 goto out_power_off;
2383 }
2384
2385 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2386 u32 val;
2387
2388 rval = smiapp_read(client,
2389 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2390 if (rval < 0) {
2391 rval = -ENODEV;
2392 goto out_power_off;
2393 }
2394 sensor->nbinning_subtypes = min_t(u8, val,
2395 SMIAPP_BINNING_SUBTYPES);
2396
2397 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2398 rval = smiapp_read(
2399 client, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
2400 if (rval < 0) {
2401 rval = -ENODEV;
2402 goto out_power_off;
2403 }
2404 sensor->binning_subtypes[i] =
2405 *(struct smiapp_binning_subtype *)&val;
2406
2407 dev_dbg(&client->dev, "binning %xx%x\n",
2408 sensor->binning_subtypes[i].horizontal,
2409 sensor->binning_subtypes[i].vertical);
2410 }
2411 }
2412 sensor->binning_horizontal = 1;
2413 sensor->binning_vertical = 1;
2414
2415 /* SMIA++ NVM initialization - it will be read from the sensor
2416 * when it is first requested by userspace.
2417 */
2418 if (sensor->minfo.smiapp_version && sensor->platform_data->nvm_size) {
2419 sensor->nvm = kzalloc(sensor->platform_data->nvm_size,
2420 GFP_KERNEL);
2421 if (sensor->nvm == NULL) {
2422 dev_err(&client->dev, "nvm buf allocation failed\n");
2423 rval = -ENOMEM;
2424 goto out_power_off;
2425 }
2426
2427 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
2428 dev_err(&client->dev, "sysfs nvm entry failed\n");
2429 rval = -EBUSY;
2430 goto out_power_off;
2431 }
2432 }
2433
2434 rval = smiapp_call_quirk(sensor, limits);
2435 if (rval) {
2436 dev_err(&client->dev, "limits quirks failed\n");
2437 goto out_nvm_release;
2438 }
2439
2440 /* We consider this as profile 0 sensor if any of these are zero. */
2441 if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
2442 !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
2443 !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
2444 !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
2445 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
2446 } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2447 != SMIAPP_SCALING_CAPABILITY_NONE) {
2448 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2449 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2450 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
2451 else
2452 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
2453 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2454 sensor->ssds_used++;
2455 } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2456 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
2457 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2458 sensor->ssds_used++;
2459 }
2460 sensor->binner = &sensor->ssds[sensor->ssds_used];
2461 sensor->ssds_used++;
2462 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
2463 sensor->ssds_used++;
2464
2465 sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
2466
2467 for (i = 0; i < SMIAPP_SUBDEVS; i++) {
2468 struct {
2469 struct smiapp_subdev *ssd;
2470 char *name;
2471 } const __this[] = {
2472 { sensor->scaler, "scaler", },
2473 { sensor->binner, "binner", },
2474 { sensor->pixel_array, "pixel array", },
2475 }, *_this = &__this[i];
2476 struct smiapp_subdev *this = _this->ssd;
2477
2478 if (!this)
2479 continue;
2480
2481 if (this != sensor->src)
2482 v4l2_subdev_init(&this->sd, &smiapp_ops);
2483
2484 this->sensor = sensor;
2485
2486 if (this == sensor->pixel_array) {
2487 this->npads = 1;
2488 } else {
2489 this->npads = 2;
2490 this->source_pad = 1;
2491 }
2492
2493 snprintf(this->sd.name,
2494 sizeof(this->sd.name), "%s %s",
2495 sensor->minfo.name, _this->name);
2496
2497 this->sink_fmt.width =
2498 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2499 this->sink_fmt.height =
2500 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2501 this->compose.width = this->sink_fmt.width;
2502 this->compose.height = this->sink_fmt.height;
2503 this->crop[this->source_pad] = this->compose;
2504 this->pads[this->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2505 if (this != sensor->pixel_array) {
2506 this->crop[this->sink_pad] = this->compose;
2507 this->pads[this->sink_pad].flags = MEDIA_PAD_FL_SINK;
2508 }
2509
2510 this->sd.entity.ops = &smiapp_entity_ops;
2511
2512 if (last == NULL) {
2513 last = this;
2514 continue;
2515 }
2516
2517 this->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2518 this->sd.internal_ops = &smiapp_internal_ops;
2519 this->sd.owner = NULL;
2520 v4l2_set_subdevdata(&this->sd, client);
2521
2522 rval = media_entity_init(&this->sd.entity,
2523 this->npads, this->pads, 0);
2524 if (rval) {
2525 dev_err(&client->dev,
2526 "media_entity_init failed\n");
2527 goto out_nvm_release;
2528 }
2529
2530 rval = media_entity_create_link(&this->sd.entity,
2531 this->source_pad,
2532 &last->sd.entity,
2533 last->sink_pad,
2534 MEDIA_LNK_FL_ENABLED |
2535 MEDIA_LNK_FL_IMMUTABLE);
2536 if (rval) {
2537 dev_err(&client->dev,
2538 "media_entity_create_link failed\n");
2539 goto out_nvm_release;
2540 }
2541
2542 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2543 &this->sd);
2544 if (rval) {
2545 dev_err(&client->dev,
2546 "v4l2_device_register_subdev failed\n");
2547 goto out_nvm_release;
2548 }
2549
2550 last = this;
2551 }
2552
2553 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
2554
2555 sensor->pixel_array->sd.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
2556
2557 /* final steps */
2558 smiapp_read_frame_fmt(sensor);
2559 rval = smiapp_init_controls(sensor);
2560 if (rval < 0)
2561 goto out_nvm_release;
2562
2563 rval = smiapp_update_mode(sensor);
2564 if (rval) {
2565 dev_err(&client->dev, "update mode failed\n");
2566 goto out_nvm_release;
2567 }
2568
2569 sensor->streaming = false;
2570 sensor->dev_init_done = true;
2571
2572 /* check flash capability */
2573 rval = smiapp_read(client, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY, &tmp);
2574 sensor->flash_capability = tmp;
2575 if (rval)
2576 goto out_nvm_release;
2577
2578 smiapp_power_off(sensor);
2579
2580 return 0;
2581
2582out_nvm_release:
2583 device_remove_file(&client->dev, &dev_attr_nvm);
2584
2585out_power_off:
2586 kfree(sensor->nvm);
2587 sensor->nvm = NULL;
2588 smiapp_power_off(sensor);
2589
2590out_smiapp_power_on:
2591 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2592 gpio_free(sensor->platform_data->xshutdown);
2593
2594out_gpio_request:
2595 regulator_put(sensor->vana);
2596 sensor->vana = NULL;
2597 return rval;
2598}
2599
2600static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2601{
2602 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2603 struct smiapp_sensor *sensor = ssd->sensor;
2604 u32 mbus_code =
2605 smiapp_csi_data_formats[smiapp_pixel_order(sensor)].code;
2606 unsigned int i;
2607
2608 mutex_lock(&sensor->mutex);
2609
2610 for (i = 0; i < ssd->npads; i++) {
2611 struct v4l2_mbus_framefmt *try_fmt =
2612 v4l2_subdev_get_try_format(fh, i);
2613 struct v4l2_rect *try_crop = v4l2_subdev_get_try_crop(fh, i);
2614 struct v4l2_rect *try_comp;
2615
2616 try_fmt->width = sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2617 try_fmt->height = sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2618 try_fmt->code = mbus_code;
2619
2620 try_crop->top = 0;
2621 try_crop->left = 0;
2622 try_crop->width = try_fmt->width;
2623 try_crop->height = try_fmt->height;
2624
2625 if (ssd != sensor->pixel_array)
2626 continue;
2627
2628 try_comp = v4l2_subdev_get_try_compose(fh, i);
2629 *try_comp = *try_crop;
2630 }
2631
2632 mutex_unlock(&sensor->mutex);
2633
2634 return smiapp_set_power(sd, 1);
2635}
2636
2637static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2638{
2639 return smiapp_set_power(sd, 0);
2640}
2641
2642static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2643 .s_stream = smiapp_set_stream,
2644};
2645
2646static const struct v4l2_subdev_core_ops smiapp_core_ops = {
2647 .s_power = smiapp_set_power,
2648};
2649
2650static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2651 .enum_mbus_code = smiapp_enum_mbus_code,
2652 .get_fmt = smiapp_get_format,
2653 .set_fmt = smiapp_set_format,
2654 .get_selection = smiapp_get_selection,
2655 .set_selection = smiapp_set_selection,
2656};
2657
2658static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2659 .g_skip_frames = smiapp_get_skip_frames,
2660};
2661
2662static const struct v4l2_subdev_ops smiapp_ops = {
2663 .core = &smiapp_core_ops,
2664 .video = &smiapp_video_ops,
2665 .pad = &smiapp_pad_ops,
2666 .sensor = &smiapp_sensor_ops,
2667};
2668
2669static const struct media_entity_operations smiapp_entity_ops = {
2670 .link_validate = v4l2_subdev_link_validate,
2671};
2672
2673static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2674 .registered = smiapp_registered,
2675 .open = smiapp_open,
2676 .close = smiapp_close,
2677};
2678
2679static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2680 .open = smiapp_open,
2681 .close = smiapp_close,
2682};
2683
2684/* -----------------------------------------------------------------------------
2685 * I2C Driver
2686 */
2687
2688#ifdef CONFIG_PM
2689
2690static int smiapp_suspend(struct device *dev)
2691{
2692 struct i2c_client *client = to_i2c_client(dev);
2693 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2694 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2695 bool streaming;
2696
2697 BUG_ON(mutex_is_locked(&sensor->mutex));
2698
2699 if (sensor->power_count == 0)
2700 return 0;
2701
2702 if (sensor->streaming)
2703 smiapp_stop_streaming(sensor);
2704
2705 streaming = sensor->streaming;
2706
2707 smiapp_power_off(sensor);
2708
2709 /* save state for resume */
2710 sensor->streaming = streaming;
2711
2712 return 0;
2713}
2714
2715static int smiapp_resume(struct device *dev)
2716{
2717 struct i2c_client *client = to_i2c_client(dev);
2718 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2719 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2720 int rval;
2721
2722 if (sensor->power_count == 0)
2723 return 0;
2724
2725 rval = smiapp_power_on(sensor);
2726 if (rval)
2727 return rval;
2728
2729 if (sensor->streaming)
2730 rval = smiapp_start_streaming(sensor);
2731
2732 return rval;
2733}
2734
2735#else
2736
2737#define smiapp_suspend NULL
2738#define smiapp_resume NULL
2739
2740#endif /* CONFIG_PM */
2741
2742static int smiapp_probe(struct i2c_client *client,
2743 const struct i2c_device_id *devid)
2744{
2745 struct smiapp_sensor *sensor;
2746 int rval;
2747
2748 if (client->dev.platform_data == NULL)
2749 return -ENODEV;
2750
2751 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
2752 if (sensor == NULL)
2753 return -ENOMEM;
2754
2755 sensor->platform_data = client->dev.platform_data;
2756 mutex_init(&sensor->mutex);
2757 mutex_init(&sensor->power_mutex);
2758 sensor->src = &sensor->ssds[sensor->ssds_used];
2759
2760 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2761 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2762 sensor->src->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2763 sensor->src->sensor = sensor;
2764
2765 sensor->src->pads[0].flags = MEDIA_PAD_FL_SOURCE;
2766 rval = media_entity_init(&sensor->src->sd.entity, 2,
2767 sensor->src->pads, 0);
2768 if (rval < 0)
2769 kfree(sensor);
2770
2771 return rval;
2772}
2773
2774static int __exit smiapp_remove(struct i2c_client *client)
2775{
2776 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2777 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2778 unsigned int i;
2779
2780 if (sensor->power_count) {
2781 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2782 gpio_set_value(sensor->platform_data->xshutdown, 0);
2783 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
2784 sensor->power_count = 0;
2785 }
2786
2787 if (sensor->nvm) {
2788 device_remove_file(&client->dev, &dev_attr_nvm);
2789 kfree(sensor->nvm);
2790 }
2791
2792 for (i = 0; i < sensor->ssds_used; i++) {
2793 media_entity_cleanup(&sensor->ssds[i].sd.entity);
2794 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2795 }
2796 smiapp_free_controls(sensor);
2797 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2798 gpio_free(sensor->platform_data->xshutdown);
2799 if (sensor->vana)
2800 regulator_put(sensor->vana);
2801
2802 kfree(sensor);
2803
2804 return 0;
2805}
2806
2807static const struct i2c_device_id smiapp_id_table[] = {
2808 { SMIAPP_NAME, 0 },
2809 { },
2810};
2811MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
2812
2813static const struct dev_pm_ops smiapp_pm_ops = {
2814 .suspend = smiapp_suspend,
2815 .resume = smiapp_resume,
2816};
2817
2818static struct i2c_driver smiapp_i2c_driver = {
2819 .driver = {
2820 .name = SMIAPP_NAME,
2821 .pm = &smiapp_pm_ops,
2822 },
2823 .probe = smiapp_probe,
2824 .remove = __exit_p(smiapp_remove),
2825 .id_table = smiapp_id_table,
2826};
2827
2828module_i2c_driver(smiapp_i2c_driver);
2829
2830MODULE_AUTHOR("Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>");
2831MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
2832MODULE_LICENSE("GPL");