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