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