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