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 max, 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 for (max = 0; sensor->hwcfg->op_sys_clock[max + 1]; max++);
639
640 sensor->link_freq = v4l2_ctrl_new_int_menu(
641 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
642 V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs),
643 __ffs(*valid_link_freqs), sensor->hwcfg->op_sys_clock);
644
645 return sensor->src->ctrl_handler.error;
646}
647
648static void smiapp_free_controls(struct smiapp_sensor *sensor)
649{
650 unsigned int i;
651
652 for (i = 0; i < sensor->ssds_used; i++)
653 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
654}
655
656static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
657 unsigned int n)
658{
659 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
660 unsigned int i;
661 u32 val;
662 int rval;
663
664 for (i = 0; i < n; i++) {
665 rval = smiapp_read(
666 sensor, smiapp_reg_limits[limit[i]].addr, &val);
667 if (rval)
668 return rval;
669 sensor->limits[limit[i]] = val;
670 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n",
671 smiapp_reg_limits[limit[i]].addr,
672 smiapp_reg_limits[limit[i]].what, val, val);
673 }
674
675 return 0;
676}
677
678static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
679{
680 unsigned int i;
681 int rval;
682
683 for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
684 rval = smiapp_get_limits(sensor, &i, 1);
685 if (rval < 0)
686 return rval;
687 }
688
689 if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
690 smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
691
692 return 0;
693}
694
695static int smiapp_get_limits_binning(struct smiapp_sensor *sensor)
696{
697 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
698 static u32 const limits[] = {
699 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN,
700 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN,
701 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN,
702 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN,
703 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN,
704 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN,
705 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN,
706 };
707 static u32 const limits_replace[] = {
708 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES,
709 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES,
710 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK,
711 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK,
712 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK,
713 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN,
714 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN,
715 };
716 unsigned int i;
717 int rval;
718
719 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
720 SMIAPP_BINNING_CAPABILITY_NO) {
721 for (i = 0; i < ARRAY_SIZE(limits); i++)
722 sensor->limits[limits[i]] =
723 sensor->limits[limits_replace[i]];
724
725 return 0;
726 }
727
728 rval = smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits));
729 if (rval < 0)
730 return rval;
731
732
733
734
735
736 if (sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN]
737 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN]
738 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN])
739 return 0;
740
741 for (i = 0; i < ARRAY_SIZE(limits); i++) {
742 dev_dbg(&client->dev,
743 "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
744 smiapp_reg_limits[limits[i]].addr,
745 smiapp_reg_limits[limits[i]].what,
746 sensor->limits[limits_replace[i]],
747 sensor->limits[limits_replace[i]]);
748 sensor->limits[limits[i]] =
749 sensor->limits[limits_replace[i]];
750 }
751
752 return 0;
753}
754
755static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
756{
757 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
758 struct smiapp_pll *pll = &sensor->pll;
759 u8 compressed_max_bpp = 0;
760 unsigned int type, n;
761 unsigned int i, pixel_order;
762 int rval;
763
764 rval = smiapp_read(
765 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
766 if (rval)
767 return rval;
768
769 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
770
771 rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
772 &pixel_order);
773 if (rval)
774 return rval;
775
776 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
777 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
778 return -EINVAL;
779 }
780
781 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
782 pixel_order_str[pixel_order]);
783
784 switch (type) {
785 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
786 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
787 break;
788 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
789 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
790 break;
791 default:
792 return -EINVAL;
793 }
794
795 sensor->default_pixel_order = pixel_order;
796 sensor->mbus_frame_fmts = 0;
797
798 for (i = 0; i < n; i++) {
799 unsigned int fmt, j;
800
801 rval = smiapp_read(
802 sensor,
803 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
804 if (rval)
805 return rval;
806
807 dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
808 i, fmt >> 8, (u8)fmt);
809
810 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
811 const struct smiapp_csi_data_format *f =
812 &smiapp_csi_data_formats[j];
813
814 if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
815 continue;
816
817 if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
818 continue;
819
820 dev_dbg(&client->dev, "jolly good! %d\n", j);
821
822 sensor->default_mbus_frame_fmts |= 1 << j;
823 }
824 }
825
826
827 pll->binning_horizontal = 1;
828 pll->binning_vertical = 1;
829 pll->scale_m = sensor->scale_m;
830
831 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
832 sensor->compressed_min_bpp =
833 min(smiapp_csi_data_formats[i].compressed,
834 sensor->compressed_min_bpp);
835 compressed_max_bpp =
836 max(smiapp_csi_data_formats[i].compressed,
837 compressed_max_bpp);
838 }
839
840 sensor->valid_link_freqs = devm_kcalloc(
841 &client->dev,
842 compressed_max_bpp - sensor->compressed_min_bpp + 1,
843 sizeof(*sensor->valid_link_freqs), GFP_KERNEL);
844 if (!sensor->valid_link_freqs)
845 return -ENOMEM;
846
847 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
848 const struct smiapp_csi_data_format *f =
849 &smiapp_csi_data_formats[i];
850 unsigned long *valid_link_freqs =
851 &sensor->valid_link_freqs[
852 f->compressed - sensor->compressed_min_bpp];
853 unsigned int j;
854
855 if (!(sensor->default_mbus_frame_fmts & 1 << i))
856 continue;
857
858 pll->bits_per_pixel = f->compressed;
859
860 for (j = 0; sensor->hwcfg->op_sys_clock[j]; j++) {
861 pll->link_freq = sensor->hwcfg->op_sys_clock[j];
862
863 rval = smiapp_pll_try(sensor, pll);
864 dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n",
865 pll->link_freq, pll->bits_per_pixel,
866 rval ? "not ok" : "ok");
867 if (rval)
868 continue;
869
870 set_bit(j, valid_link_freqs);
871 }
872
873 if (!*valid_link_freqs) {
874 dev_info(&client->dev,
875 "no valid link frequencies for %u bpp\n",
876 f->compressed);
877 sensor->default_mbus_frame_fmts &= ~BIT(i);
878 continue;
879 }
880
881 if (!sensor->csi_format
882 || f->width > sensor->csi_format->width
883 || (f->width == sensor->csi_format->width
884 && f->compressed > sensor->csi_format->compressed)) {
885 sensor->csi_format = f;
886 sensor->internal_csi_format = f;
887 }
888 }
889
890 if (!sensor->csi_format) {
891 dev_err(&client->dev, "no supported mbus code found\n");
892 return -EINVAL;
893 }
894
895 smiapp_update_mbus_formats(sensor);
896
897 return 0;
898}
899
900static void smiapp_update_blanking(struct smiapp_sensor *sensor)
901{
902 struct v4l2_ctrl *vblank = sensor->vblank;
903 struct v4l2_ctrl *hblank = sensor->hblank;
904 int min, max;
905
906 min = max_t(int,
907 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
908 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
909 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
910 max = sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
911 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
912
913 __v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);
914
915 min = max_t(int,
916 sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
917 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
918 sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
919 max = sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
920 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
921
922 __v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
923
924 __smiapp_update_exposure_limits(sensor);
925}
926
927static int smiapp_update_mode(struct smiapp_sensor *sensor)
928{
929 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
930 unsigned int binning_mode;
931 int rval;
932
933
934 if (sensor->binning_horizontal == 1 &&
935 sensor->binning_vertical == 1) {
936 binning_mode = 0;
937 } else {
938 u8 binning_type =
939 (sensor->binning_horizontal << 4)
940 | sensor->binning_vertical;
941
942 rval = smiapp_write(
943 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
944 if (rval < 0)
945 return rval;
946
947 binning_mode = 1;
948 }
949 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
950 if (rval < 0)
951 return rval;
952
953
954 rval = smiapp_get_limits_binning(sensor);
955 if (rval < 0)
956 return rval;
957
958 rval = smiapp_pll_update(sensor);
959 if (rval < 0)
960 return rval;
961
962
963 smiapp_update_blanking(sensor);
964
965 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
966 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
967
968 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
969 sensor->pll.pixel_rate_pixel_array /
970 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
971 + sensor->hblank->val) *
972 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
973 + sensor->vblank->val) / 100));
974
975 return 0;
976}
977
978
979
980
981
982
983static int smiapp_read_nvm(struct smiapp_sensor *sensor,
984 unsigned char *nvm)
985{
986 u32 i, s, p, np, v;
987 int rval = 0, rval2;
988
989 np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
990 for (p = 0; p < np; p++) {
991 rval = smiapp_write(
992 sensor,
993 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
994 if (rval)
995 goto out;
996
997 rval = smiapp_write(sensor,
998 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
999 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
1000 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
1001 if (rval)
1002 goto out;
1003
1004 for (i = 0; i < 1000; i++) {
1005 rval = smiapp_read(
1006 sensor,
1007 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);
1008
1009 if (rval)
1010 goto out;
1011
1012 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
1013 break;
1014
1015 if (--i == 0) {
1016 rval = -ETIMEDOUT;
1017 goto out;
1018 }
1019
1020 }
1021
1022 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
1023 rval = smiapp_read(
1024 sensor,
1025 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
1026 &v);
1027 if (rval)
1028 goto out;
1029
1030 *nvm++ = v;
1031 }
1032 }
1033
1034out:
1035 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
1036 if (rval < 0)
1037 return rval;
1038 else
1039 return rval2;
1040}
1041
1042
1043
1044
1045
1046
1047static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
1048{
1049 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1050 int rval;
1051 u32 val;
1052
1053 client->addr = sensor->hwcfg->i2c_addr_dfl;
1054
1055 rval = smiapp_write(sensor,
1056 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
1057 sensor->hwcfg->i2c_addr_alt << 1);
1058 if (rval)
1059 return rval;
1060
1061 client->addr = sensor->hwcfg->i2c_addr_alt;
1062
1063
1064 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
1065 if (rval)
1066 return rval;
1067
1068 if (val != sensor->hwcfg->i2c_addr_alt << 1)
1069 return -ENODEV;
1070
1071 return 0;
1072}
1073
1074
1075
1076
1077
1078
1079static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
1080{
1081 struct smiapp_flash_strobe_parms *strobe_setup;
1082 unsigned int ext_freq = sensor->hwcfg->ext_clk;
1083 u32 tmp;
1084 u32 strobe_adjustment;
1085 u32 strobe_width_high_rs;
1086 int rval;
1087
1088 strobe_setup = sensor->hwcfg->strobe_setup;
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
1157
1158
1159 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1160 1000000 + 1, ext_freq);
1161 strobe_setup->strobe_width_high_us =
1162 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1163
1164 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1165 1000000 - 1), 1000000ULL);
1166 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1167 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1168 strobe_adjustment;
1169
1170 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
1171 strobe_setup->mode);
1172 if (rval < 0)
1173 goto out;
1174
1175 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1176 strobe_adjustment);
1177 if (rval < 0)
1178 goto out;
1179
1180 rval = smiapp_write(
1181 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1182 strobe_width_high_rs);
1183 if (rval < 0)
1184 goto out;
1185
1186 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1187 strobe_setup->strobe_delay);
1188 if (rval < 0)
1189 goto out;
1190
1191 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1192 strobe_setup->stobe_start_point);
1193 if (rval < 0)
1194 goto out;
1195
1196 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1197 strobe_setup->trigger);
1198
1199out:
1200 sensor->hwcfg->strobe_setup->trigger = 0;
1201
1202 return rval;
1203}
1204
1205
1206
1207
1208
1209static int smiapp_power_on(struct device *dev)
1210{
1211 struct i2c_client *client = to_i2c_client(dev);
1212 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1213 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1214
1215
1216
1217
1218 struct smiapp_sensor *sensor =
1219 container_of(ssd, struct smiapp_sensor, ssds[0]);
1220 unsigned int sleep;
1221 int rval;
1222
1223 rval = regulator_enable(sensor->vana);
1224 if (rval) {
1225 dev_err(&client->dev, "failed to enable vana regulator\n");
1226 return rval;
1227 }
1228 usleep_range(1000, 1000);
1229
1230 rval = clk_prepare_enable(sensor->ext_clk);
1231 if (rval < 0) {
1232 dev_dbg(&client->dev, "failed to enable xclk\n");
1233 goto out_xclk_fail;
1234 }
1235 usleep_range(1000, 1000);
1236
1237 gpiod_set_value(sensor->xshutdown, 1);
1238
1239 sleep = SMIAPP_RESET_DELAY(sensor->hwcfg->ext_clk);
1240 usleep_range(sleep, sleep);
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253 if (sensor->hwcfg->i2c_addr_alt) {
1254 rval = smiapp_change_cci_addr(sensor);
1255 if (rval) {
1256 dev_err(&client->dev, "cci address change error\n");
1257 goto out_cci_addr_fail;
1258 }
1259 }
1260
1261 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
1262 SMIAPP_SOFTWARE_RESET);
1263 if (rval < 0) {
1264 dev_err(&client->dev, "software reset failed\n");
1265 goto out_cci_addr_fail;
1266 }
1267
1268 if (sensor->hwcfg->i2c_addr_alt) {
1269 rval = smiapp_change_cci_addr(sensor);
1270 if (rval) {
1271 dev_err(&client->dev, "cci address change error\n");
1272 goto out_cci_addr_fail;
1273 }
1274 }
1275
1276 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
1277 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1278 if (rval) {
1279 dev_err(&client->dev, "compression mode set failed\n");
1280 goto out_cci_addr_fail;
1281 }
1282
1283 rval = smiapp_write(
1284 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1285 sensor->hwcfg->ext_clk / (1000000 / (1 << 8)));
1286 if (rval) {
1287 dev_err(&client->dev, "extclk frequency set failed\n");
1288 goto out_cci_addr_fail;
1289 }
1290
1291 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1292 sensor->hwcfg->lanes - 1);
1293 if (rval) {
1294 dev_err(&client->dev, "csi lane mode set failed\n");
1295 goto out_cci_addr_fail;
1296 }
1297
1298 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1299 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1300 if (rval) {
1301 dev_err(&client->dev, "fast standby set failed\n");
1302 goto out_cci_addr_fail;
1303 }
1304
1305 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1306 sensor->hwcfg->csi_signalling_mode);
1307 if (rval) {
1308 dev_err(&client->dev, "csi signalling mode set failed\n");
1309 goto out_cci_addr_fail;
1310 }
1311
1312
1313 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
1314 SMIAPP_DPHY_CTRL_UI);
1315 if (rval < 0)
1316 return rval;
1317
1318 rval = smiapp_call_quirk(sensor, post_poweron);
1319 if (rval) {
1320 dev_err(&client->dev, "post_poweron quirks failed\n");
1321 goto out_cci_addr_fail;
1322 }
1323
1324
1325 if (!sensor->pixel_array)
1326 return 0;
1327
1328 rval = v4l2_ctrl_handler_setup(&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 mutex_lock(&sensor->mutex);
1337 rval = smiapp_update_mode(sensor);
1338 mutex_unlock(&sensor->mutex);
1339 if (rval < 0)
1340 goto out_cci_addr_fail;
1341
1342 return 0;
1343
1344out_cci_addr_fail:
1345
1346 gpiod_set_value(sensor->xshutdown, 0);
1347 clk_disable_unprepare(sensor->ext_clk);
1348
1349out_xclk_fail:
1350 regulator_disable(sensor->vana);
1351
1352 return rval;
1353}
1354
1355static int smiapp_power_off(struct device *dev)
1356{
1357 struct i2c_client *client = to_i2c_client(dev);
1358 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1359 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1360 struct smiapp_sensor *sensor =
1361 container_of(ssd, struct smiapp_sensor, ssds[0]);
1362
1363
1364
1365
1366
1367
1368
1369
1370 if (sensor->hwcfg->i2c_addr_alt)
1371 smiapp_write(sensor,
1372 SMIAPP_REG_U8_SOFTWARE_RESET,
1373 SMIAPP_SOFTWARE_RESET);
1374
1375 gpiod_set_value(sensor->xshutdown, 0);
1376 clk_disable_unprepare(sensor->ext_clk);
1377 usleep_range(5000, 5000);
1378 regulator_disable(sensor->vana);
1379 sensor->streaming = false;
1380
1381 return 0;
1382}
1383
1384static int smiapp_set_power(struct v4l2_subdev *subdev, int on)
1385{
1386 int rval;
1387
1388 if (!on) {
1389 pm_runtime_mark_last_busy(subdev->dev);
1390 pm_runtime_put_autosuspend(subdev->dev);
1391
1392 return 0;
1393 }
1394
1395 rval = pm_runtime_get_sync(subdev->dev);
1396 if (rval >= 0)
1397 return 0;
1398
1399 if (rval != -EBUSY && rval != -EAGAIN)
1400 pm_runtime_set_active(subdev->dev);
1401
1402 pm_runtime_put(subdev->dev);
1403
1404 return rval;
1405}
1406
1407
1408
1409
1410
1411static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1412{
1413 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1414 int rval;
1415
1416 mutex_lock(&sensor->mutex);
1417
1418 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1419 (sensor->csi_format->width << 8) |
1420 sensor->csi_format->compressed);
1421 if (rval)
1422 goto out;
1423
1424 rval = smiapp_pll_configure(sensor);
1425 if (rval)
1426 goto out;
1427
1428
1429 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
1430 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1431 if (rval < 0)
1432 goto out;
1433
1434 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
1435 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1436 if (rval < 0)
1437 goto out;
1438
1439
1440 rval = smiapp_write(
1441 sensor, SMIAPP_REG_U16_X_ADDR_END,
1442 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1443 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1444 if (rval < 0)
1445 goto out;
1446
1447 rval = smiapp_write(
1448 sensor, SMIAPP_REG_U16_Y_ADDR_END,
1449 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1450 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1451 if (rval < 0)
1452 goto out;
1453
1454
1455
1456
1457
1458
1459
1460 if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1461 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1462 rval = smiapp_write(
1463 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1464 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1465 if (rval < 0)
1466 goto out;
1467
1468 rval = smiapp_write(
1469 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1470 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1471 if (rval < 0)
1472 goto out;
1473
1474 rval = smiapp_write(
1475 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1476 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1477 if (rval < 0)
1478 goto out;
1479
1480 rval = smiapp_write(
1481 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1482 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1483 if (rval < 0)
1484 goto out;
1485 }
1486
1487
1488 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1489 != SMIAPP_SCALING_CAPABILITY_NONE) {
1490 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
1491 sensor->scaling_mode);
1492 if (rval < 0)
1493 goto out;
1494
1495 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
1496 sensor->scale_m);
1497 if (rval < 0)
1498 goto out;
1499 }
1500
1501
1502 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1503 sensor->src->crop[SMIAPP_PAD_SRC].width);
1504 if (rval < 0)
1505 goto out;
1506 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1507 sensor->src->crop[SMIAPP_PAD_SRC].height);
1508 if (rval < 0)
1509 goto out;
1510
1511 if ((sensor->limits[SMIAPP_LIMIT_FLASH_MODE_CAPABILITY] &
1512 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1513 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1514 sensor->hwcfg->strobe_setup != NULL &&
1515 sensor->hwcfg->strobe_setup->trigger != 0) {
1516 rval = smiapp_setup_flash_strobe(sensor);
1517 if (rval)
1518 goto out;
1519 }
1520
1521 rval = smiapp_call_quirk(sensor, pre_streamon);
1522 if (rval) {
1523 dev_err(&client->dev, "pre_streamon quirks failed\n");
1524 goto out;
1525 }
1526
1527 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1528 SMIAPP_MODE_SELECT_STREAMING);
1529
1530out:
1531 mutex_unlock(&sensor->mutex);
1532
1533 return rval;
1534}
1535
1536static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1537{
1538 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1539 int rval;
1540
1541 mutex_lock(&sensor->mutex);
1542 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1543 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1544 if (rval)
1545 goto out;
1546
1547 rval = smiapp_call_quirk(sensor, post_streamoff);
1548 if (rval)
1549 dev_err(&client->dev, "post_streamoff quirks failed\n");
1550
1551out:
1552 mutex_unlock(&sensor->mutex);
1553 return rval;
1554}
1555
1556
1557
1558
1559
1560static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1561{
1562 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1563 int rval;
1564
1565 if (sensor->streaming == enable)
1566 return 0;
1567
1568 if (enable) {
1569 sensor->streaming = true;
1570 rval = smiapp_start_streaming(sensor);
1571 if (rval < 0)
1572 sensor->streaming = false;
1573 } else {
1574 rval = smiapp_stop_streaming(sensor);
1575 sensor->streaming = false;
1576 }
1577
1578 return rval;
1579}
1580
1581static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1582 struct v4l2_subdev_pad_config *cfg,
1583 struct v4l2_subdev_mbus_code_enum *code)
1584{
1585 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1586 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1587 unsigned int i;
1588 int idx = -1;
1589 int rval = -EINVAL;
1590
1591 mutex_lock(&sensor->mutex);
1592
1593 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1594 subdev->name, code->pad, code->index);
1595
1596 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1597 if (code->index)
1598 goto out;
1599
1600 code->code = sensor->internal_csi_format->code;
1601 rval = 0;
1602 goto out;
1603 }
1604
1605 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1606 if (sensor->mbus_frame_fmts & (1 << i))
1607 idx++;
1608
1609 if (idx == code->index) {
1610 code->code = smiapp_csi_data_formats[i].code;
1611 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1612 code->index, i, code->code);
1613 rval = 0;
1614 break;
1615 }
1616 }
1617
1618out:
1619 mutex_unlock(&sensor->mutex);
1620
1621 return rval;
1622}
1623
1624static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1625 unsigned int pad)
1626{
1627 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1628
1629 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1630 return sensor->csi_format->code;
1631 else
1632 return sensor->internal_csi_format->code;
1633}
1634
1635static 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_subdev *ssd = to_smiapp_subdev(subdev);
1640
1641 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1642 fmt->format = *v4l2_subdev_get_try_format(subdev, cfg,
1643 fmt->pad);
1644 } else {
1645 struct v4l2_rect *r;
1646
1647 if (fmt->pad == ssd->source_pad)
1648 r = &ssd->crop[ssd->source_pad];
1649 else
1650 r = &ssd->sink_fmt;
1651
1652 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1653 fmt->format.width = r->width;
1654 fmt->format.height = r->height;
1655 fmt->format.field = V4L2_FIELD_NONE;
1656 }
1657
1658 return 0;
1659}
1660
1661static int smiapp_get_format(struct v4l2_subdev *subdev,
1662 struct v4l2_subdev_pad_config *cfg,
1663 struct v4l2_subdev_format *fmt)
1664{
1665 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1666 int rval;
1667
1668 mutex_lock(&sensor->mutex);
1669 rval = __smiapp_get_format(subdev, cfg, fmt);
1670 mutex_unlock(&sensor->mutex);
1671
1672 return rval;
1673}
1674
1675static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1676 struct v4l2_subdev_pad_config *cfg,
1677 struct v4l2_rect **crops,
1678 struct v4l2_rect **comps, int which)
1679{
1680 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1681 unsigned int i;
1682
1683 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1684 if (crops)
1685 for (i = 0; i < subdev->entity.num_pads; i++)
1686 crops[i] = &ssd->crop[i];
1687 if (comps)
1688 *comps = &ssd->compose;
1689 } else {
1690 if (crops) {
1691 for (i = 0; i < subdev->entity.num_pads; i++) {
1692 crops[i] = v4l2_subdev_get_try_crop(subdev, cfg, i);
1693 BUG_ON(!crops[i]);
1694 }
1695 }
1696 if (comps) {
1697 *comps = v4l2_subdev_get_try_compose(subdev, cfg,
1698 SMIAPP_PAD_SINK);
1699 BUG_ON(!*comps);
1700 }
1701 }
1702}
1703
1704
1705static void smiapp_propagate(struct v4l2_subdev *subdev,
1706 struct v4l2_subdev_pad_config *cfg, int which,
1707 int target)
1708{
1709 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1710 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1711 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1712
1713 smiapp_get_crop_compose(subdev, cfg, crops, &comp, which);
1714
1715 switch (target) {
1716 case V4L2_SEL_TGT_CROP:
1717 comp->width = crops[SMIAPP_PAD_SINK]->width;
1718 comp->height = crops[SMIAPP_PAD_SINK]->height;
1719 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1720 if (ssd == sensor->scaler) {
1721 sensor->scale_m =
1722 sensor->limits[
1723 SMIAPP_LIMIT_SCALER_N_MIN];
1724 sensor->scaling_mode =
1725 SMIAPP_SCALING_MODE_NONE;
1726 } else if (ssd == sensor->binner) {
1727 sensor->binning_horizontal = 1;
1728 sensor->binning_vertical = 1;
1729 }
1730 }
1731
1732 case V4L2_SEL_TGT_COMPOSE:
1733 *crops[SMIAPP_PAD_SRC] = *comp;
1734 break;
1735 default:
1736 BUG();
1737 }
1738}
1739
1740static const struct smiapp_csi_data_format
1741*smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1742{
1743 unsigned int i;
1744
1745 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1746 if (sensor->mbus_frame_fmts & (1 << i)
1747 && smiapp_csi_data_formats[i].code == code)
1748 return &smiapp_csi_data_formats[i];
1749 }
1750
1751 return sensor->csi_format;
1752}
1753
1754static int smiapp_set_format_source(struct v4l2_subdev *subdev,
1755 struct v4l2_subdev_pad_config *cfg,
1756 struct v4l2_subdev_format *fmt)
1757{
1758 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1759 const struct smiapp_csi_data_format *csi_format,
1760 *old_csi_format = sensor->csi_format;
1761 unsigned long *valid_link_freqs;
1762 u32 code = fmt->format.code;
1763 unsigned int i;
1764 int rval;
1765
1766 rval = __smiapp_get_format(subdev, cfg, fmt);
1767 if (rval)
1768 return rval;
1769
1770
1771
1772
1773
1774 if (subdev != &sensor->src->sd)
1775 return 0;
1776
1777 csi_format = smiapp_validate_csi_data_format(sensor, code);
1778
1779 fmt->format.code = csi_format->code;
1780
1781 if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1782 return 0;
1783
1784 sensor->csi_format = csi_format;
1785
1786 if (csi_format->width != old_csi_format->width)
1787 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
1788 __v4l2_ctrl_modify_range(
1789 sensor->test_data[i], 0,
1790 (1 << csi_format->width) - 1, 1, 0);
1791
1792 if (csi_format->compressed == old_csi_format->compressed)
1793 return 0;
1794
1795 valid_link_freqs =
1796 &sensor->valid_link_freqs[sensor->csi_format->compressed
1797 - sensor->compressed_min_bpp];
1798
1799 __v4l2_ctrl_modify_range(
1800 sensor->link_freq, 0,
1801 __fls(*valid_link_freqs), ~*valid_link_freqs,
1802 __ffs(*valid_link_freqs));
1803
1804 return smiapp_pll_update(sensor);
1805}
1806
1807static int smiapp_set_format(struct v4l2_subdev *subdev,
1808 struct v4l2_subdev_pad_config *cfg,
1809 struct v4l2_subdev_format *fmt)
1810{
1811 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1812 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1813 struct v4l2_rect *crops[SMIAPP_PADS];
1814
1815 mutex_lock(&sensor->mutex);
1816
1817 if (fmt->pad == ssd->source_pad) {
1818 int rval;
1819
1820 rval = smiapp_set_format_source(subdev, cfg, fmt);
1821
1822 mutex_unlock(&sensor->mutex);
1823
1824 return rval;
1825 }
1826
1827
1828 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1829 fmt->format.width &= ~1;
1830 fmt->format.height &= ~1;
1831 fmt->format.field = V4L2_FIELD_NONE;
1832
1833 fmt->format.width =
1834 clamp(fmt->format.width,
1835 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
1836 sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
1837 fmt->format.height =
1838 clamp(fmt->format.height,
1839 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
1840 sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
1841
1842 smiapp_get_crop_compose(subdev, cfg, crops, NULL, fmt->which);
1843
1844 crops[ssd->sink_pad]->left = 0;
1845 crops[ssd->sink_pad]->top = 0;
1846 crops[ssd->sink_pad]->width = fmt->format.width;
1847 crops[ssd->sink_pad]->height = fmt->format.height;
1848 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1849 ssd->sink_fmt = *crops[ssd->sink_pad];
1850 smiapp_propagate(subdev, cfg, fmt->which,
1851 V4L2_SEL_TGT_CROP);
1852
1853 mutex_unlock(&sensor->mutex);
1854
1855 return 0;
1856}
1857
1858
1859
1860
1861
1862#define SCALING_GOODNESS 100000
1863#define SCALING_GOODNESS_EXTREME 100000000
1864static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1865 int h, int ask_h, u32 flags)
1866{
1867 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1868 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1869 int val = 0;
1870
1871 w &= ~1;
1872 ask_w &= ~1;
1873 h &= ~1;
1874 ask_h &= ~1;
1875
1876 if (flags & V4L2_SEL_FLAG_GE) {
1877 if (w < ask_w)
1878 val -= SCALING_GOODNESS;
1879 if (h < ask_h)
1880 val -= SCALING_GOODNESS;
1881 }
1882
1883 if (flags & V4L2_SEL_FLAG_LE) {
1884 if (w > ask_w)
1885 val -= SCALING_GOODNESS;
1886 if (h > ask_h)
1887 val -= SCALING_GOODNESS;
1888 }
1889
1890 val -= abs(w - ask_w);
1891 val -= abs(h - ask_h);
1892
1893 if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
1894 val -= SCALING_GOODNESS_EXTREME;
1895
1896 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1897 w, ask_h, h, ask_h, val);
1898
1899 return val;
1900}
1901
1902static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1903 struct v4l2_subdev_pad_config *cfg,
1904 struct v4l2_subdev_selection *sel,
1905 struct v4l2_rect **crops,
1906 struct v4l2_rect *comp)
1907{
1908 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1909 unsigned int i;
1910 unsigned int binh = 1, binv = 1;
1911 int best = scaling_goodness(
1912 subdev,
1913 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1914 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1915
1916 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1917 int this = scaling_goodness(
1918 subdev,
1919 crops[SMIAPP_PAD_SINK]->width
1920 / sensor->binning_subtypes[i].horizontal,
1921 sel->r.width,
1922 crops[SMIAPP_PAD_SINK]->height
1923 / sensor->binning_subtypes[i].vertical,
1924 sel->r.height, sel->flags);
1925
1926 if (this > best) {
1927 binh = sensor->binning_subtypes[i].horizontal;
1928 binv = sensor->binning_subtypes[i].vertical;
1929 best = this;
1930 }
1931 }
1932 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1933 sensor->binning_vertical = binv;
1934 sensor->binning_horizontal = binh;
1935 }
1936
1937 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1938 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1939}
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1951 struct v4l2_subdev_pad_config *cfg,
1952 struct v4l2_subdev_selection *sel,
1953 struct v4l2_rect **crops,
1954 struct v4l2_rect *comp)
1955{
1956 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1957 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1958 u32 min, max, a, b, max_m;
1959 u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
1960 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1961 u32 try[4];
1962 u32 ntry = 0;
1963 unsigned int i;
1964 int best = INT_MIN;
1965
1966 sel->r.width = min_t(unsigned int, sel->r.width,
1967 crops[SMIAPP_PAD_SINK]->width);
1968 sel->r.height = min_t(unsigned int, sel->r.height,
1969 crops[SMIAPP_PAD_SINK]->height);
1970
1971 a = crops[SMIAPP_PAD_SINK]->width
1972 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
1973 b = crops[SMIAPP_PAD_SINK]->height
1974 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
1975 max_m = crops[SMIAPP_PAD_SINK]->width
1976 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
1977 / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
1978
1979 a = clamp(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1980 sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1981 b = clamp(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1982 sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1983 max_m = clamp(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1984 sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1985
1986 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1987
1988 min = min(max_m, min(a, b));
1989 max = min(max_m, max(a, b));
1990
1991 try[ntry] = min;
1992 ntry++;
1993 if (min != max) {
1994 try[ntry] = max;
1995 ntry++;
1996 }
1997 if (max != max_m) {
1998 try[ntry] = min + 1;
1999 ntry++;
2000 if (min != max) {
2001 try[ntry] = max + 1;
2002 ntry++;
2003 }
2004 }
2005
2006 for (i = 0; i < ntry; i++) {
2007 int this = scaling_goodness(
2008 subdev,
2009 crops[SMIAPP_PAD_SINK]->width
2010 / try[i]
2011 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2012 sel->r.width,
2013 crops[SMIAPP_PAD_SINK]->height,
2014 sel->r.height,
2015 sel->flags);
2016
2017 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
2018
2019 if (this > best) {
2020 scale_m = try[i];
2021 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
2022 best = this;
2023 }
2024
2025 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2026 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2027 continue;
2028
2029 this = scaling_goodness(
2030 subdev, crops[SMIAPP_PAD_SINK]->width
2031 / try[i]
2032 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2033 sel->r.width,
2034 crops[SMIAPP_PAD_SINK]->height
2035 / try[i]
2036 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2037 sel->r.height,
2038 sel->flags);
2039
2040 if (this > best) {
2041 scale_m = try[i];
2042 mode = SMIAPP_SCALING_MODE_BOTH;
2043 best = this;
2044 }
2045 }
2046
2047 sel->r.width =
2048 (crops[SMIAPP_PAD_SINK]->width
2049 / scale_m
2050 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
2051 if (mode == SMIAPP_SCALING_MODE_BOTH)
2052 sel->r.height =
2053 (crops[SMIAPP_PAD_SINK]->height
2054 / scale_m
2055 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
2056 & ~1;
2057 else
2058 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
2059
2060 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2061 sensor->scale_m = scale_m;
2062 sensor->scaling_mode = mode;
2063 }
2064}
2065
2066static int smiapp_set_compose(struct v4l2_subdev *subdev,
2067 struct v4l2_subdev_pad_config *cfg,
2068 struct v4l2_subdev_selection *sel)
2069{
2070 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2071 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2072 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2073
2074 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2075
2076 sel->r.top = 0;
2077 sel->r.left = 0;
2078
2079 if (ssd == sensor->binner)
2080 smiapp_set_compose_binner(subdev, cfg, sel, crops, comp);
2081 else
2082 smiapp_set_compose_scaler(subdev, cfg, sel, crops, comp);
2083
2084 *comp = sel->r;
2085 smiapp_propagate(subdev, cfg, sel->which, V4L2_SEL_TGT_COMPOSE);
2086
2087 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2088 return smiapp_update_mode(sensor);
2089
2090 return 0;
2091}
2092
2093static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
2094 struct v4l2_subdev_selection *sel)
2095{
2096 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2097 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2098
2099
2100 switch (sel->target) {
2101 case V4L2_SEL_TGT_CROP:
2102 case V4L2_SEL_TGT_CROP_BOUNDS:
2103 if (ssd == sensor->pixel_array
2104 && sel->pad == SMIAPP_PA_PAD_SRC)
2105 return 0;
2106 if (ssd == sensor->src
2107 && sel->pad == SMIAPP_PAD_SRC)
2108 return 0;
2109 if (ssd == sensor->scaler
2110 && sel->pad == SMIAPP_PAD_SINK
2111 && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2112 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
2113 return 0;
2114 return -EINVAL;
2115 case V4L2_SEL_TGT_NATIVE_SIZE:
2116 if (ssd == sensor->pixel_array
2117 && sel->pad == SMIAPP_PA_PAD_SRC)
2118 return 0;
2119 return -EINVAL;
2120 case V4L2_SEL_TGT_COMPOSE:
2121 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2122 if (sel->pad == ssd->source_pad)
2123 return -EINVAL;
2124 if (ssd == sensor->binner)
2125 return 0;
2126 if (ssd == sensor->scaler
2127 && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2128 != SMIAPP_SCALING_CAPABILITY_NONE)
2129 return 0;
2130
2131 default:
2132 return -EINVAL;
2133 }
2134}
2135
2136static int smiapp_set_crop(struct v4l2_subdev *subdev,
2137 struct v4l2_subdev_pad_config *cfg,
2138 struct v4l2_subdev_selection *sel)
2139{
2140 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2141 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2142 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
2143 struct v4l2_rect _r;
2144
2145 smiapp_get_crop_compose(subdev, cfg, crops, NULL, sel->which);
2146
2147 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2148 if (sel->pad == ssd->sink_pad)
2149 src_size = &ssd->sink_fmt;
2150 else
2151 src_size = &ssd->compose;
2152 } else {
2153 if (sel->pad == ssd->sink_pad) {
2154 _r.left = 0;
2155 _r.top = 0;
2156 _r.width = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2157 ->width;
2158 _r.height = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2159 ->height;
2160 src_size = &_r;
2161 } else {
2162 src_size = v4l2_subdev_get_try_compose(
2163 subdev, cfg, ssd->sink_pad);
2164 }
2165 }
2166
2167 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2168 sel->r.left = 0;
2169 sel->r.top = 0;
2170 }
2171
2172 sel->r.width = min(sel->r.width, src_size->width);
2173 sel->r.height = min(sel->r.height, src_size->height);
2174
2175 sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
2176 sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
2177
2178 *crops[sel->pad] = sel->r;
2179
2180 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2181 smiapp_propagate(subdev, cfg, sel->which,
2182 V4L2_SEL_TGT_CROP);
2183
2184 return 0;
2185}
2186
2187static void smiapp_get_native_size(struct smiapp_subdev *ssd,
2188 struct v4l2_rect *r)
2189{
2190 r->top = 0;
2191 r->left = 0;
2192 r->width = ssd->sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2193 r->height = ssd->sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2194}
2195
2196static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2197 struct v4l2_subdev_pad_config *cfg,
2198 struct v4l2_subdev_selection *sel)
2199{
2200 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2201 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2202 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2203 struct v4l2_rect sink_fmt;
2204 int ret;
2205
2206 ret = __smiapp_sel_supported(subdev, sel);
2207 if (ret)
2208 return ret;
2209
2210 smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2211
2212 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2213 sink_fmt = ssd->sink_fmt;
2214 } else {
2215 struct v4l2_mbus_framefmt *fmt =
2216 v4l2_subdev_get_try_format(subdev, cfg, ssd->sink_pad);
2217
2218 sink_fmt.left = 0;
2219 sink_fmt.top = 0;
2220 sink_fmt.width = fmt->width;
2221 sink_fmt.height = fmt->height;
2222 }
2223
2224 switch (sel->target) {
2225 case V4L2_SEL_TGT_CROP_BOUNDS:
2226 case V4L2_SEL_TGT_NATIVE_SIZE:
2227 if (ssd == sensor->pixel_array)
2228 smiapp_get_native_size(ssd, &sel->r);
2229 else if (sel->pad == ssd->sink_pad)
2230 sel->r = sink_fmt;
2231 else
2232 sel->r = *comp;
2233 break;
2234 case V4L2_SEL_TGT_CROP:
2235 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2236 sel->r = *crops[sel->pad];
2237 break;
2238 case V4L2_SEL_TGT_COMPOSE:
2239 sel->r = *comp;
2240 break;
2241 }
2242
2243 return 0;
2244}
2245
2246static int smiapp_get_selection(struct v4l2_subdev *subdev,
2247 struct v4l2_subdev_pad_config *cfg,
2248 struct v4l2_subdev_selection *sel)
2249{
2250 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2251 int rval;
2252
2253 mutex_lock(&sensor->mutex);
2254 rval = __smiapp_get_selection(subdev, cfg, sel);
2255 mutex_unlock(&sensor->mutex);
2256
2257 return rval;
2258}
2259static int smiapp_set_selection(struct v4l2_subdev *subdev,
2260 struct v4l2_subdev_pad_config *cfg,
2261 struct v4l2_subdev_selection *sel)
2262{
2263 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2264 int ret;
2265
2266 ret = __smiapp_sel_supported(subdev, sel);
2267 if (ret)
2268 return ret;
2269
2270 mutex_lock(&sensor->mutex);
2271
2272 sel->r.left = max(0, sel->r.left & ~1);
2273 sel->r.top = max(0, sel->r.top & ~1);
2274 sel->r.width = SMIAPP_ALIGN_DIM(sel->r.width, sel->flags);
2275 sel->r.height = SMIAPP_ALIGN_DIM(sel->r.height, sel->flags);
2276
2277 sel->r.width = max_t(unsigned int,
2278 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
2279 sel->r.width);
2280 sel->r.height = max_t(unsigned int,
2281 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
2282 sel->r.height);
2283
2284 switch (sel->target) {
2285 case V4L2_SEL_TGT_CROP:
2286 ret = smiapp_set_crop(subdev, cfg, sel);
2287 break;
2288 case V4L2_SEL_TGT_COMPOSE:
2289 ret = smiapp_set_compose(subdev, cfg, sel);
2290 break;
2291 default:
2292 ret = -EINVAL;
2293 }
2294
2295 mutex_unlock(&sensor->mutex);
2296 return ret;
2297}
2298
2299static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2300{
2301 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2302
2303 *frames = sensor->frame_skip;
2304 return 0;
2305}
2306
2307static int smiapp_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines)
2308{
2309 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2310
2311 *lines = sensor->image_start;
2312
2313 return 0;
2314}
2315
2316
2317
2318
2319
2320static ssize_t
2321smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2322 char *buf)
2323{
2324 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2325 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2326 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2327 unsigned int nbytes;
2328
2329 if (!sensor->dev_init_done)
2330 return -EBUSY;
2331
2332 if (!sensor->nvm_size) {
2333 int rval;
2334
2335
2336 sensor->nvm_size = sensor->hwcfg->nvm_size;
2337
2338 rval = pm_runtime_get_sync(&client->dev);
2339 if (rval < 0) {
2340 if (rval != -EBUSY && rval != -EAGAIN)
2341 pm_runtime_set_active(&client->dev);
2342 pm_runtime_put(&client->dev);
2343 return -ENODEV;
2344 }
2345
2346 if (smiapp_read_nvm(sensor, sensor->nvm)) {
2347 dev_err(&client->dev, "nvm read failed\n");
2348 return -ENODEV;
2349 }
2350
2351 pm_runtime_mark_last_busy(&client->dev);
2352 pm_runtime_put_autosuspend(&client->dev);
2353 }
2354
2355
2356
2357
2358 nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
2359 memcpy(buf, sensor->nvm, nbytes);
2360
2361 return nbytes;
2362}
2363static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2364
2365static ssize_t
2366smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2367 char *buf)
2368{
2369 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2370 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2371 struct smiapp_module_info *minfo = &sensor->minfo;
2372
2373 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2374 minfo->manufacturer_id, minfo->model_id,
2375 minfo->revision_number_major) + 1;
2376}
2377
2378static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
2379
2380
2381
2382
2383
2384static int smiapp_identify_module(struct smiapp_sensor *sensor)
2385{
2386 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2387 struct smiapp_module_info *minfo = &sensor->minfo;
2388 unsigned int i;
2389 int rval = 0;
2390
2391 minfo->name = SMIAPP_NAME;
2392
2393
2394 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2395 &minfo->manufacturer_id);
2396 if (!rval)
2397 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2398 &minfo->model_id);
2399 if (!rval)
2400 rval = smiapp_read_8only(sensor,
2401 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2402 &minfo->revision_number_major);
2403 if (!rval)
2404 rval = smiapp_read_8only(sensor,
2405 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2406 &minfo->revision_number_minor);
2407 if (!rval)
2408 rval = smiapp_read_8only(sensor,
2409 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2410 &minfo->module_year);
2411 if (!rval)
2412 rval = smiapp_read_8only(sensor,
2413 SMIAPP_REG_U8_MODULE_DATE_MONTH,
2414 &minfo->module_month);
2415 if (!rval)
2416 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2417 &minfo->module_day);
2418
2419
2420 if (!rval)
2421 rval = smiapp_read_8only(sensor,
2422 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2423 &minfo->sensor_manufacturer_id);
2424 if (!rval)
2425 rval = smiapp_read_8only(sensor,
2426 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2427 &minfo->sensor_model_id);
2428 if (!rval)
2429 rval = smiapp_read_8only(sensor,
2430 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2431 &minfo->sensor_revision_number);
2432 if (!rval)
2433 rval = smiapp_read_8only(sensor,
2434 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2435 &minfo->sensor_firmware_version);
2436
2437
2438 if (!rval)
2439 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2440 &minfo->smia_version);
2441 if (!rval)
2442 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2443 &minfo->smiapp_version);
2444
2445 if (rval) {
2446 dev_err(&client->dev, "sensor detection failed\n");
2447 return -ENODEV;
2448 }
2449
2450 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2451 minfo->manufacturer_id, minfo->model_id);
2452
2453 dev_dbg(&client->dev,
2454 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2455 minfo->revision_number_major, minfo->revision_number_minor,
2456 minfo->module_year, minfo->module_month, minfo->module_day);
2457
2458 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2459 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2460
2461 dev_dbg(&client->dev,
2462 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2463 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2464
2465 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2466 minfo->smia_version, minfo->smiapp_version);
2467
2468
2469
2470
2471
2472
2473 if (!minfo->manufacturer_id && !minfo->model_id) {
2474 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2475 minfo->model_id = minfo->sensor_model_id;
2476 minfo->revision_number_major = minfo->sensor_revision_number;
2477 }
2478
2479 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2480 if (smiapp_module_idents[i].manufacturer_id
2481 != minfo->manufacturer_id)
2482 continue;
2483 if (smiapp_module_idents[i].model_id != minfo->model_id)
2484 continue;
2485 if (smiapp_module_idents[i].flags
2486 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2487 if (smiapp_module_idents[i].revision_number_major
2488 < minfo->revision_number_major)
2489 continue;
2490 } else {
2491 if (smiapp_module_idents[i].revision_number_major
2492 != minfo->revision_number_major)
2493 continue;
2494 }
2495
2496 minfo->name = smiapp_module_idents[i].name;
2497 minfo->quirk = smiapp_module_idents[i].quirk;
2498 break;
2499 }
2500
2501 if (i >= ARRAY_SIZE(smiapp_module_idents))
2502 dev_warn(&client->dev,
2503 "no quirks for this module; let's hope it's fully compliant\n");
2504
2505 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2506 minfo->name, minfo->manufacturer_id, minfo->model_id,
2507 minfo->revision_number_major);
2508
2509 return 0;
2510}
2511
2512static const struct v4l2_subdev_ops smiapp_ops;
2513static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2514static const struct media_entity_operations smiapp_entity_ops;
2515
2516static int smiapp_register_subdev(struct smiapp_sensor *sensor,
2517 struct smiapp_subdev *ssd,
2518 struct smiapp_subdev *sink_ssd,
2519 u16 source_pad, u16 sink_pad, u32 link_flags)
2520{
2521 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2522 int rval;
2523
2524 if (!sink_ssd)
2525 return 0;
2526
2527 rval = media_entity_pads_init(&ssd->sd.entity,
2528 ssd->npads, ssd->pads);
2529 if (rval) {
2530 dev_err(&client->dev,
2531 "media_entity_pads_init failed\n");
2532 return rval;
2533 }
2534
2535 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2536 &ssd->sd);
2537 if (rval) {
2538 dev_err(&client->dev,
2539 "v4l2_device_register_subdev failed\n");
2540 return rval;
2541 }
2542
2543 rval = media_create_pad_link(&ssd->sd.entity, source_pad,
2544 &sink_ssd->sd.entity, sink_pad,
2545 link_flags);
2546 if (rval) {
2547 dev_err(&client->dev,
2548 "media_create_pad_link failed\n");
2549 v4l2_device_unregister_subdev(&ssd->sd);
2550 return rval;
2551 }
2552
2553 return 0;
2554}
2555
2556static void smiapp_unregistered(struct v4l2_subdev *subdev)
2557{
2558 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2559 unsigned int i;
2560
2561 for (i = 1; i < sensor->ssds_used; i++)
2562 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2563}
2564
2565static int smiapp_registered(struct v4l2_subdev *subdev)
2566{
2567 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2568 int rval;
2569
2570 if (sensor->scaler) {
2571 rval = smiapp_register_subdev(
2572 sensor, sensor->binner, sensor->scaler,
2573 SMIAPP_PAD_SRC, SMIAPP_PAD_SINK,
2574 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2575 if (rval < 0)
2576 return rval;
2577 }
2578
2579 rval = smiapp_register_subdev(
2580 sensor, sensor->pixel_array, sensor->binner,
2581 SMIAPP_PA_PAD_SRC, SMIAPP_PAD_SINK,
2582 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2583 if (rval)
2584 goto out_err;
2585
2586 return 0;
2587
2588out_err:
2589 smiapp_unregistered(subdev);
2590
2591 return rval;
2592}
2593
2594static void smiapp_cleanup(struct smiapp_sensor *sensor)
2595{
2596 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2597
2598 device_remove_file(&client->dev, &dev_attr_nvm);
2599 device_remove_file(&client->dev, &dev_attr_ident);
2600
2601 smiapp_free_controls(sensor);
2602}
2603
2604static void smiapp_create_subdev(struct smiapp_sensor *sensor,
2605 struct smiapp_subdev *ssd, const char *name,
2606 unsigned short num_pads)
2607{
2608 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2609
2610 if (!ssd)
2611 return;
2612
2613 if (ssd != sensor->src)
2614 v4l2_subdev_init(&ssd->sd, &smiapp_ops);
2615
2616 ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2617 ssd->sensor = sensor;
2618
2619 ssd->npads = num_pads;
2620 ssd->source_pad = num_pads - 1;
2621
2622 snprintf(ssd->sd.name,
2623 sizeof(ssd->sd.name), "%s %s %d-%4.4x", sensor->minfo.name,
2624 name, i2c_adapter_id(client->adapter), client->addr);
2625
2626 smiapp_get_native_size(ssd, &ssd->sink_fmt);
2627
2628 ssd->compose.width = ssd->sink_fmt.width;
2629 ssd->compose.height = ssd->sink_fmt.height;
2630 ssd->crop[ssd->source_pad] = ssd->compose;
2631 ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2632 if (ssd != sensor->pixel_array) {
2633 ssd->crop[ssd->sink_pad] = ssd->compose;
2634 ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK;
2635 }
2636
2637 ssd->sd.entity.ops = &smiapp_entity_ops;
2638
2639 if (ssd == sensor->src)
2640 return;
2641
2642 ssd->sd.internal_ops = &smiapp_internal_ops;
2643 ssd->sd.owner = THIS_MODULE;
2644 ssd->sd.dev = &client->dev;
2645 v4l2_set_subdevdata(&ssd->sd, client);
2646}
2647
2648static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2649{
2650 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2651 struct smiapp_sensor *sensor = ssd->sensor;
2652 unsigned int i;
2653 int rval;
2654
2655 mutex_lock(&sensor->mutex);
2656
2657 for (i = 0; i < ssd->npads; i++) {
2658 struct v4l2_mbus_framefmt *try_fmt =
2659 v4l2_subdev_get_try_format(sd, fh->pad, i);
2660 struct v4l2_rect *try_crop =
2661 v4l2_subdev_get_try_crop(sd, fh->pad, i);
2662 struct v4l2_rect *try_comp;
2663
2664 smiapp_get_native_size(ssd, try_crop);
2665
2666 try_fmt->width = try_crop->width;
2667 try_fmt->height = try_crop->height;
2668 try_fmt->code = sensor->internal_csi_format->code;
2669 try_fmt->field = V4L2_FIELD_NONE;
2670
2671 if (ssd != sensor->pixel_array)
2672 continue;
2673
2674 try_comp = v4l2_subdev_get_try_compose(sd, fh->pad, i);
2675 *try_comp = *try_crop;
2676 }
2677
2678 mutex_unlock(&sensor->mutex);
2679
2680 rval = pm_runtime_get_sync(sd->dev);
2681 if (rval >= 0)
2682 return 0;
2683
2684 if (rval != -EBUSY && rval != -EAGAIN)
2685 pm_runtime_set_active(sd->dev);
2686 pm_runtime_put(sd->dev);
2687
2688 return rval;
2689}
2690
2691static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2692{
2693 pm_runtime_mark_last_busy(sd->dev);
2694 pm_runtime_put_autosuspend(sd->dev);
2695
2696 return 0;
2697}
2698
2699static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2700 .s_stream = smiapp_set_stream,
2701};
2702
2703static const struct v4l2_subdev_core_ops smiapp_core_ops = {
2704 .s_power = smiapp_set_power,
2705};
2706
2707static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2708 .enum_mbus_code = smiapp_enum_mbus_code,
2709 .get_fmt = smiapp_get_format,
2710 .set_fmt = smiapp_set_format,
2711 .get_selection = smiapp_get_selection,
2712 .set_selection = smiapp_set_selection,
2713};
2714
2715static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2716 .g_skip_frames = smiapp_get_skip_frames,
2717 .g_skip_top_lines = smiapp_get_skip_top_lines,
2718};
2719
2720static const struct v4l2_subdev_ops smiapp_ops = {
2721 .core = &smiapp_core_ops,
2722 .video = &smiapp_video_ops,
2723 .pad = &smiapp_pad_ops,
2724 .sensor = &smiapp_sensor_ops,
2725};
2726
2727static const struct media_entity_operations smiapp_entity_ops = {
2728 .link_validate = v4l2_subdev_link_validate,
2729};
2730
2731static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2732 .registered = smiapp_registered,
2733 .unregistered = smiapp_unregistered,
2734 .open = smiapp_open,
2735 .close = smiapp_close,
2736};
2737
2738static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2739 .open = smiapp_open,
2740 .close = smiapp_close,
2741};
2742
2743
2744
2745
2746
2747static int __maybe_unused smiapp_suspend(struct device *dev)
2748{
2749 struct i2c_client *client = to_i2c_client(dev);
2750 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2751 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2752 bool streaming = sensor->streaming;
2753 int rval;
2754
2755 rval = pm_runtime_get_sync(dev);
2756 if (rval < 0) {
2757 if (rval != -EBUSY && rval != -EAGAIN)
2758 pm_runtime_set_active(&client->dev);
2759 pm_runtime_put(dev);
2760 return -EAGAIN;
2761 }
2762
2763 if (sensor->streaming)
2764 smiapp_stop_streaming(sensor);
2765
2766
2767 sensor->streaming = streaming;
2768
2769 return 0;
2770}
2771
2772static int __maybe_unused smiapp_resume(struct device *dev)
2773{
2774 struct i2c_client *client = to_i2c_client(dev);
2775 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2776 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2777 int rval = 0;
2778
2779 pm_runtime_put(dev);
2780
2781 if (sensor->streaming)
2782 rval = smiapp_start_streaming(sensor);
2783
2784 return rval;
2785}
2786
2787static struct smiapp_hwconfig *smiapp_get_hwconfig(struct device *dev)
2788{
2789 struct smiapp_hwconfig *hwcfg;
2790 struct v4l2_fwnode_endpoint *bus_cfg;
2791 struct fwnode_handle *ep;
2792 struct fwnode_handle *fwnode = dev_fwnode(dev);
2793 int i;
2794 int rval;
2795
2796 if (!fwnode)
2797 return dev->platform_data;
2798
2799 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
2800 if (!ep)
2801 return NULL;
2802
2803 bus_cfg = v4l2_fwnode_endpoint_alloc_parse(ep);
2804 if (IS_ERR(bus_cfg))
2805 goto out_err;
2806
2807 hwcfg = devm_kzalloc(dev, sizeof(*hwcfg), GFP_KERNEL);
2808 if (!hwcfg)
2809 goto out_err;
2810
2811 switch (bus_cfg->bus_type) {
2812 case V4L2_MBUS_CSI2:
2813 hwcfg->csi_signalling_mode = SMIAPP_CSI_SIGNALLING_MODE_CSI2;
2814 hwcfg->lanes = bus_cfg->bus.mipi_csi2.num_data_lanes;
2815 break;
2816 case V4L2_MBUS_CCP2:
2817 hwcfg->csi_signalling_mode = (bus_cfg->bus.mipi_csi1.strobe) ?
2818 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE :
2819 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK;
2820 hwcfg->lanes = 1;
2821 break;
2822 default:
2823 dev_err(dev, "unsupported bus %u\n", bus_cfg->bus_type);
2824 goto out_err;
2825 }
2826
2827 dev_dbg(dev, "lanes %u\n", hwcfg->lanes);
2828
2829
2830 fwnode_property_read_u32(fwnode, "nokia,nvm-size", &hwcfg->nvm_size);
2831
2832 rval = fwnode_property_read_u32(fwnode, "clock-frequency",
2833 &hwcfg->ext_clk);
2834 if (rval) {
2835 dev_warn(dev, "can't get clock-frequency\n");
2836 goto out_err;
2837 }
2838
2839 dev_dbg(dev, "nvm %d, clk %d, mode %d\n",
2840 hwcfg->nvm_size, hwcfg->ext_clk, hwcfg->csi_signalling_mode);
2841
2842 if (!bus_cfg->nr_of_link_frequencies) {
2843 dev_warn(dev, "no link frequencies defined\n");
2844 goto out_err;
2845 }
2846
2847 hwcfg->op_sys_clock = devm_kcalloc(
2848 dev, bus_cfg->nr_of_link_frequencies + 1 ,
2849 sizeof(*hwcfg->op_sys_clock), GFP_KERNEL);
2850 if (!hwcfg->op_sys_clock)
2851 goto out_err;
2852
2853 for (i = 0; i < bus_cfg->nr_of_link_frequencies; i++) {
2854 hwcfg->op_sys_clock[i] = bus_cfg->link_frequencies[i];
2855 dev_dbg(dev, "freq %d: %lld\n", i, hwcfg->op_sys_clock[i]);
2856 }
2857
2858 v4l2_fwnode_endpoint_free(bus_cfg);
2859 fwnode_handle_put(ep);
2860 return hwcfg;
2861
2862out_err:
2863 v4l2_fwnode_endpoint_free(bus_cfg);
2864 fwnode_handle_put(ep);
2865 return NULL;
2866}
2867
2868static int smiapp_probe(struct i2c_client *client,
2869 const struct i2c_device_id *devid)
2870{
2871 struct smiapp_sensor *sensor;
2872 struct smiapp_hwconfig *hwcfg = smiapp_get_hwconfig(&client->dev);
2873 unsigned int i;
2874 int rval;
2875
2876 if (hwcfg == NULL)
2877 return -ENODEV;
2878
2879 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
2880 if (sensor == NULL)
2881 return -ENOMEM;
2882
2883 sensor->hwcfg = hwcfg;
2884 mutex_init(&sensor->mutex);
2885 sensor->src = &sensor->ssds[sensor->ssds_used];
2886
2887 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2888 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2889
2890 sensor->vana = devm_regulator_get(&client->dev, "vana");
2891 if (IS_ERR(sensor->vana)) {
2892 dev_err(&client->dev, "could not get regulator for vana\n");
2893 return PTR_ERR(sensor->vana);
2894 }
2895
2896 sensor->ext_clk = devm_clk_get(&client->dev, NULL);
2897 if (IS_ERR(sensor->ext_clk)) {
2898 dev_err(&client->dev, "could not get clock (%ld)\n",
2899 PTR_ERR(sensor->ext_clk));
2900 return -EPROBE_DEFER;
2901 }
2902
2903 rval = clk_set_rate(sensor->ext_clk, sensor->hwcfg->ext_clk);
2904 if (rval < 0) {
2905 dev_err(&client->dev,
2906 "unable to set clock freq to %u\n",
2907 sensor->hwcfg->ext_clk);
2908 return rval;
2909 }
2910
2911 sensor->xshutdown = devm_gpiod_get_optional(&client->dev, "xshutdown",
2912 GPIOD_OUT_LOW);
2913 if (IS_ERR(sensor->xshutdown))
2914 return PTR_ERR(sensor->xshutdown);
2915
2916 rval = smiapp_power_on(&client->dev);
2917 if (rval < 0)
2918 return rval;
2919
2920 rval = smiapp_identify_module(sensor);
2921 if (rval) {
2922 rval = -ENODEV;
2923 goto out_power_off;
2924 }
2925
2926 rval = smiapp_get_all_limits(sensor);
2927 if (rval) {
2928 rval = -ENODEV;
2929 goto out_power_off;
2930 }
2931
2932 rval = smiapp_read_frame_fmt(sensor);
2933 if (rval) {
2934 rval = -ENODEV;
2935 goto out_power_off;
2936 }
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951 if (sensor->hwcfg->module_board_orient ==
2952 SMIAPP_MODULE_BOARD_ORIENT_180)
2953 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2954 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2955
2956 rval = smiapp_call_quirk(sensor, limits);
2957 if (rval) {
2958 dev_err(&client->dev, "limits quirks failed\n");
2959 goto out_power_off;
2960 }
2961
2962 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2963 u32 val;
2964
2965 rval = smiapp_read(sensor,
2966 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2967 if (rval < 0) {
2968 rval = -ENODEV;
2969 goto out_power_off;
2970 }
2971 sensor->nbinning_subtypes = min_t(u8, val,
2972 SMIAPP_BINNING_SUBTYPES);
2973
2974 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2975 rval = smiapp_read(
2976 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
2977 if (rval < 0) {
2978 rval = -ENODEV;
2979 goto out_power_off;
2980 }
2981 sensor->binning_subtypes[i] =
2982 *(struct smiapp_binning_subtype *)&val;
2983
2984 dev_dbg(&client->dev, "binning %xx%x\n",
2985 sensor->binning_subtypes[i].horizontal,
2986 sensor->binning_subtypes[i].vertical);
2987 }
2988 }
2989 sensor->binning_horizontal = 1;
2990 sensor->binning_vertical = 1;
2991
2992 if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
2993 dev_err(&client->dev, "sysfs ident entry creation failed\n");
2994 rval = -ENOENT;
2995 goto out_power_off;
2996 }
2997
2998
2999
3000 if (sensor->minfo.smiapp_version && sensor->hwcfg->nvm_size) {
3001 sensor->nvm = devm_kzalloc(&client->dev,
3002 sensor->hwcfg->nvm_size, GFP_KERNEL);
3003 if (sensor->nvm == NULL) {
3004 rval = -ENOMEM;
3005 goto out_cleanup;
3006 }
3007
3008 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
3009 dev_err(&client->dev, "sysfs nvm entry failed\n");
3010 rval = -EBUSY;
3011 goto out_cleanup;
3012 }
3013 }
3014
3015
3016 if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
3017 !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
3018 !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
3019 !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
3020 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
3021 } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
3022 != SMIAPP_SCALING_CAPABILITY_NONE) {
3023 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
3024 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
3025 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
3026 else
3027 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
3028 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3029 sensor->ssds_used++;
3030 } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
3031 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
3032 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3033 sensor->ssds_used++;
3034 }
3035 sensor->binner = &sensor->ssds[sensor->ssds_used];
3036 sensor->ssds_used++;
3037 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
3038 sensor->ssds_used++;
3039
3040 sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
3041
3042
3043 sensor->pll.bus_type = SMIAPP_PLL_BUS_TYPE_CSI2;
3044 sensor->pll.csi2.lanes = sensor->hwcfg->lanes;
3045 sensor->pll.ext_clk_freq_hz = sensor->hwcfg->ext_clk;
3046 sensor->pll.scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
3047
3048 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
3049 sensor->pll.flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
3050
3051 smiapp_create_subdev(sensor, sensor->scaler, "scaler", 2);
3052 smiapp_create_subdev(sensor, sensor->binner, "binner", 2);
3053 smiapp_create_subdev(sensor, sensor->pixel_array, "pixel_array", 1);
3054
3055 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
3056
3057 sensor->pixel_array->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3058
3059 rval = smiapp_init_controls(sensor);
3060 if (rval < 0)
3061 goto out_cleanup;
3062
3063 rval = smiapp_call_quirk(sensor, init);
3064 if (rval)
3065 goto out_cleanup;
3066
3067 rval = smiapp_get_mbus_formats(sensor);
3068 if (rval) {
3069 rval = -ENODEV;
3070 goto out_cleanup;
3071 }
3072
3073 rval = smiapp_init_late_controls(sensor);
3074 if (rval) {
3075 rval = -ENODEV;
3076 goto out_cleanup;
3077 }
3078
3079 mutex_lock(&sensor->mutex);
3080 rval = smiapp_update_mode(sensor);
3081 mutex_unlock(&sensor->mutex);
3082 if (rval) {
3083 dev_err(&client->dev, "update mode failed\n");
3084 goto out_cleanup;
3085 }
3086
3087 sensor->streaming = false;
3088 sensor->dev_init_done = true;
3089
3090 rval = media_entity_pads_init(&sensor->src->sd.entity, 2,
3091 sensor->src->pads);
3092 if (rval < 0)
3093 goto out_media_entity_cleanup;
3094
3095 rval = v4l2_async_register_subdev(&sensor->src->sd);
3096 if (rval < 0)
3097 goto out_media_entity_cleanup;
3098
3099 pm_runtime_set_active(&client->dev);
3100 pm_runtime_get_noresume(&client->dev);
3101 pm_runtime_enable(&client->dev);
3102 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
3103 pm_runtime_use_autosuspend(&client->dev);
3104 pm_runtime_put_autosuspend(&client->dev);
3105
3106 return 0;
3107
3108out_media_entity_cleanup:
3109 media_entity_cleanup(&sensor->src->sd.entity);
3110
3111out_cleanup:
3112 smiapp_cleanup(sensor);
3113
3114out_power_off:
3115 smiapp_power_off(&client->dev);
3116
3117 return rval;
3118}
3119
3120static int smiapp_remove(struct i2c_client *client)
3121{
3122 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3123 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
3124 unsigned int i;
3125
3126 v4l2_async_unregister_subdev(subdev);
3127
3128 pm_runtime_disable(&client->dev);
3129 if (!pm_runtime_status_suspended(&client->dev))
3130 smiapp_power_off(&client->dev);
3131 pm_runtime_set_suspended(&client->dev);
3132
3133 for (i = 0; i < sensor->ssds_used; i++) {
3134 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
3135 media_entity_cleanup(&sensor->ssds[i].sd.entity);
3136 }
3137 smiapp_cleanup(sensor);
3138
3139 return 0;
3140}
3141
3142static const struct of_device_id smiapp_of_table[] = {
3143 { .compatible = "nokia,smia" },
3144 { },
3145};
3146MODULE_DEVICE_TABLE(of, smiapp_of_table);
3147
3148static const struct i2c_device_id smiapp_id_table[] = {
3149 { SMIAPP_NAME, 0 },
3150 { },
3151};
3152MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
3153
3154static const struct dev_pm_ops smiapp_pm_ops = {
3155 SET_SYSTEM_SLEEP_PM_OPS(smiapp_suspend, smiapp_resume)
3156 SET_RUNTIME_PM_OPS(smiapp_power_off, smiapp_power_on, NULL)
3157};
3158
3159static struct i2c_driver smiapp_i2c_driver = {
3160 .driver = {
3161 .of_match_table = smiapp_of_table,
3162 .name = SMIAPP_NAME,
3163 .pm = &smiapp_pm_ops,
3164 },
3165 .probe = smiapp_probe,
3166 .remove = smiapp_remove,
3167 .id_table = smiapp_id_table,
3168};
3169
3170module_i2c_driver(smiapp_i2c_driver);
3171
3172MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3173MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3174MODULE_LICENSE("GPL");
3175