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