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