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