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