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