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