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