blob: e08e588ad24b5f90808279629bf9c80a2c22aecc [file] [log] [blame]
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001/*
Mauro Carvalho Chehabcb7a01a2012-08-14 16:23:43 -03002 * drivers/media/i2c/smiapp/smiapp-core.c
Sakari Ailusccfc97b2012-03-03 17:19:52 -03003 *
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
Sakari Ailus25474282012-04-22 08:24:33 -030029#include <linux/clk.h>
Sakari Ailusccfc97b2012-03-03 17:19:52 -030030#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/gpio.h>
33#include <linux/module.h>
Mauro Carvalho Chehabce7d16a2012-06-21 10:05:56 -030034#include <linux/slab.h>
Sakari Ailusccfc97b2012-03-03 17:19:52 -030035#include <linux/regulator/consumer.h>
36#include <linux/v4l2-mediabus.h>
37#include <media/v4l2-device.h>
38
39#include "smiapp.h"
40
Sakari Ailus563df3d2012-06-13 16:01:10 -030041#define SMIAPP_ALIGN_DIM(dim, flags) \
42 ((flags) & V4L2_SEL_FLAG_GE \
43 ? ALIGN((dim), 2) \
Sakari Ailusccfc97b2012-03-03 17:19:52 -030044 : (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
Sakari Ailus1e73eea2012-04-22 08:55:10 -030079 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -030080 &fmt_model_type);
81 if (rval)
82 return rval;
83
Sakari Ailus1e73eea2012-04-22 08:55:10 -030084 rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -030085 &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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300110 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300111 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300124 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300125 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{
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300203 struct smiapp_pll *pll = &sensor->pll;
204 int rval;
205
206 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300207 sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt_pix_clk_div);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300208 if (rval < 0)
209 return rval;
210
211 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300212 sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt_sys_clk_div);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300213 if (rval < 0)
214 return rval;
215
216 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300217 sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300218 if (rval < 0)
219 return rval;
220
221 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300222 sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300223 if (rval < 0)
224 return rval;
225
226 /* Lane op clock ratio does not apply here. */
227 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300228 sensor, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300229 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300234 sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op_pix_clk_div);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300235 if (rval < 0)
236 return rval;
237
238 return smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300239 sensor, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op_sys_clk_div);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300240}
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, },
Sakari Ailusb8cc8d72012-04-27 10:01:46 -0300371 { 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, },
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300375};
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;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300432 u32 orient = 0;
433 int exposure;
434 int rval;
435
436 switch (ctrl->id) {
437 case V4L2_CID_ANALOGUE_GAIN:
438 return smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300439 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300440 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
441
442 case V4L2_CID_EXPOSURE:
443 return smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300444 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300445 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;
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300459 rval = smiapp_write(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300460 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300484 sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300485 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
486 + ctrl->val);
487
488 case V4L2_CID_HBLANK:
489 return smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300490 sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300491 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);
Sakari Ailus06b491f2012-05-06 07:03:46 -0300512 unsigned int max;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300513 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
Sakari Ailus06b491f2012-05-06 07:03:46 -0300576 for (max = 0; sensor->platform_data->op_sys_clock[max + 1]; max++);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300577
Sakari Ailus06b491f2012-05-06 07:03:46 -0300578 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);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300582
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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300625 sensor, smiapp_reg_limits[limit[i]].addr, &val);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300626 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{
Sakari Ailus3de886e2012-04-26 11:55:13 -0300656 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300657 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 };
Sakari Ailus3de886e2012-04-26 11:55:13 -0300675 unsigned int i;
676 int rval;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300677
678 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
679 SMIAPP_BINNING_CAPABILITY_NO) {
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300680 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
Sakari Ailus3de886e2012-04-26 11:55:13 -0300687 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;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300712}
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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300722 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300723 if (rval)
724 return rval;
725
726 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
727
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300728 rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300729 &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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300759 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300760 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;
Sakari Ailusf67e1572012-09-15 17:19:49 -0300780 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)) {
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300785 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300861 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300862 if (rval < 0)
863 return rval;
864
865 binning_mode = 1;
866 }
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300867 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300868 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{
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300904 u32 i, s, p, np, v;
Sakari Ailus04582942012-04-26 12:05:50 -0300905 int rval = 0, rval2;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300906
907 np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
908 for (p = 0; p < np; p++) {
909 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300910 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300911 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
912 if (rval)
913 goto out;
914
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300915 rval = smiapp_write(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300916 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300924 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300925 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300942 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300943 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:
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300953 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300954 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
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300973 rval = smiapp_write(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300974 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 */
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300982 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300983 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{
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300999 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
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001088 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001089 strobe_setup->mode);
1090 if (rval < 0)
1091 goto out;
1092
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001093 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001094 strobe_adjustment);
1095 if (rval < 0)
1096 goto out;
1097
1098 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001099 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001100 strobe_width_high_rs);
1101 if (rval < 0)
1102 goto out;
1103
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001104 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001105 strobe_setup->strobe_delay);
1106 if (rval < 0)
1107 goto out;
1108
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001109 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001110 strobe_setup->stobe_start_point);
1111 if (rval < 0)
1112 goto out;
1113
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001114 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001115 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
Sakari Ailus25474282012-04-22 08:24:33 -03001140 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);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001145 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
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001176 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001177 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
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001191 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001192 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001199 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001200 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
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001206 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001207 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
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001213 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001214 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
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001220 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001221 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 */
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001228 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001229 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);
Sakari Ailus25474282012-04-22 08:24:33 -03001263 if (sensor->platform_data->set_xclk)
1264 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1265 else
1266 clk_disable(sensor->ext_clk);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001267
1268out_xclk_fail:
1269 regulator_disable(sensor->vana);
1270 return rval;
1271}
1272
1273static void smiapp_power_off(struct smiapp_sensor *sensor)
1274{
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001275 /*
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)
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001283 smiapp_write(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001284 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);
Sakari Ailus25474282012-04-22 08:24:33 -03001289 if (sensor->platform_data->set_xclk)
1290 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1291 else
1292 clk_disable(sensor->ext_clk);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001293 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
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001341 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001342 (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 */
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001352 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001353 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1354 if (rval < 0)
1355 goto out;
1356
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001357 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001358 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001364 sensor, SMIAPP_REG_U16_X_ADDR_END,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001365 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001371 sensor, SMIAPP_REG_U16_Y_ADDR_END,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001372 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(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001386 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001387 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1388 if (rval < 0)
1389 goto out;
1390
1391 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001392 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001393 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1394 if (rval < 0)
1395 goto out;
1396
1397 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001398 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001399 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1400 if (rval < 0)
1401 goto out;
1402
1403 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001404 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001405 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) {
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001413 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001414 sensor->scaling_mode);
1415 if (rval < 0)
1416 goto out;
1417
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001418 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001419 sensor->scale_m);
1420 if (rval < 0)
1421 goto out;
1422 }
1423
1424 /* Output size from sensor */
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001425 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001426 sensor->src->crop[SMIAPP_PAD_SRC].width);
1427 if (rval < 0)
1428 goto out;
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001429 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001430 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
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001450 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001451 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);
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001465 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001466 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) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001637 case V4L2_SEL_TGT_CROP:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001638 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 */
Sakari Ailus5689b282012-05-18 09:31:18 -03001653 case V4L2_SEL_TGT_COMPOSE:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001654 *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,
Sakari Ailus5689b282012-05-18 09:31:18 -03001729 V4L2_SEL_TGT_CROP);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001730
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
Sakari Ailus563df3d2012-06-13 16:01:10 -03001754 if (flags & V4L2_SEL_FLAG_GE) {
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001755 if (w < ask_w)
1756 val -= SCALING_GOODNESS;
1757 if (h < ask_h)
1758 val -= SCALING_GOODNESS;
1759 }
1760
Sakari Ailus563df3d2012-06-13 16:01:10 -03001761 if (flags & V4L2_SEL_FLAG_LE) {
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001762 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,
Sakari Ailus5689b282012-05-18 09:31:18 -03001964 V4L2_SEL_TGT_COMPOSE);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001965
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) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001980 case V4L2_SEL_TGT_CROP:
1981 case V4L2_SEL_TGT_CROP_BOUNDS:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001982 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;
Sakari Ailus5689b282012-05-18 09:31:18 -03001994 case V4L2_SEL_TGT_COMPOSE:
1995 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001996 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,
Sakari Ailus5689b282012-05-18 09:31:18 -03002057 V4L2_SEL_TGT_CROP);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002058
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) {
Sakari Ailus5689b282012-05-18 09:31:18 -03002091 case V4L2_SEL_TGT_CROP_BOUNDS:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002092 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;
Sakari Ailus5689b282012-05-18 09:31:18 -03002103 case V4L2_SEL_TGT_CROP:
2104 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002105 sel->r = *crops[sel->pad];
2106 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03002107 case V4L2_SEL_TGT_COMPOSE:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002108 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) {
Sakari Ailus5689b282012-05-18 09:31:18 -03002154 case V4L2_SEL_TGT_CROP:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002155 ret = smiapp_set_crop(subdev, fh, sel);
2156 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03002157 case V4L2_SEL_TGT_COMPOSE:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002158 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
Sakari Ailuseba66b32012-09-15 18:26:54 -03002214static ssize_t
2215smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2216 char *buf)
2217{
2218 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2219 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2220 struct smiapp_module_info *minfo = &sensor->minfo;
2221
2222 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2223 minfo->manufacturer_id, minfo->model_id,
2224 minfo->revision_number_major) + 1;
2225}
2226
2227static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
2228
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002229/* -----------------------------------------------------------------------------
2230 * V4L2 subdev core operations
2231 */
2232
2233static int smiapp_identify_module(struct v4l2_subdev *subdev)
2234{
2235 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2236 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2237 struct smiapp_module_info *minfo = &sensor->minfo;
2238 unsigned int i;
2239 int rval = 0;
2240
2241 minfo->name = SMIAPP_NAME;
2242
2243 /* Module info */
Sakari Ailus98add8e2012-04-22 09:30:19 -03002244 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2245 &minfo->manufacturer_id);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002246 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002247 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2248 &minfo->model_id);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002249 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002250 rval = smiapp_read_8only(sensor,
2251 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2252 &minfo->revision_number_major);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002253 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002254 rval = smiapp_read_8only(sensor,
2255 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2256 &minfo->revision_number_minor);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002257 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002258 rval = smiapp_read_8only(sensor,
2259 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2260 &minfo->module_year);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002261 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002262 rval = smiapp_read_8only(sensor,
2263 SMIAPP_REG_U8_MODULE_DATE_MONTH,
2264 &minfo->module_month);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002265 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002266 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2267 &minfo->module_day);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002268
2269 /* Sensor info */
2270 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002271 rval = smiapp_read_8only(sensor,
2272 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2273 &minfo->sensor_manufacturer_id);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002274 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002275 rval = smiapp_read_8only(sensor,
2276 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2277 &minfo->sensor_model_id);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002278 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002279 rval = smiapp_read_8only(sensor,
2280 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2281 &minfo->sensor_revision_number);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002282 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002283 rval = smiapp_read_8only(sensor,
2284 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2285 &minfo->sensor_firmware_version);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002286
2287 /* SMIA */
2288 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002289 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2290 &minfo->smia_version);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002291 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002292 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2293 &minfo->smiapp_version);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002294
2295 if (rval) {
2296 dev_err(&client->dev, "sensor detection failed\n");
2297 return -ENODEV;
2298 }
2299
2300 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2301 minfo->manufacturer_id, minfo->model_id);
2302
2303 dev_dbg(&client->dev,
2304 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2305 minfo->revision_number_major, minfo->revision_number_minor,
2306 minfo->module_year, minfo->module_month, minfo->module_day);
2307
2308 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2309 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2310
2311 dev_dbg(&client->dev,
2312 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2313 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2314
2315 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2316 minfo->smia_version, minfo->smiapp_version);
2317
2318 /*
2319 * Some modules have bad data in the lvalues below. Hope the
2320 * rvalues have better stuff. The lvalues are module
2321 * parameters whereas the rvalues are sensor parameters.
2322 */
2323 if (!minfo->manufacturer_id && !minfo->model_id) {
2324 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2325 minfo->model_id = minfo->sensor_model_id;
2326 minfo->revision_number_major = minfo->sensor_revision_number;
2327 }
2328
2329 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2330 if (smiapp_module_idents[i].manufacturer_id
2331 != minfo->manufacturer_id)
2332 continue;
2333 if (smiapp_module_idents[i].model_id != minfo->model_id)
2334 continue;
2335 if (smiapp_module_idents[i].flags
2336 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2337 if (smiapp_module_idents[i].revision_number_major
2338 < minfo->revision_number_major)
2339 continue;
2340 } else {
2341 if (smiapp_module_idents[i].revision_number_major
2342 != minfo->revision_number_major)
2343 continue;
2344 }
2345
2346 minfo->name = smiapp_module_idents[i].name;
2347 minfo->quirk = smiapp_module_idents[i].quirk;
2348 break;
2349 }
2350
2351 if (i >= ARRAY_SIZE(smiapp_module_idents))
2352 dev_warn(&client->dev,
2353 "no quirks for this module; let's hope it's fully compliant\n");
2354
2355 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2356 minfo->name, minfo->manufacturer_id, minfo->model_id,
2357 minfo->revision_number_major);
2358
2359 strlcpy(subdev->name, sensor->minfo.name, sizeof(subdev->name));
2360
2361 return 0;
2362}
2363
2364static const struct v4l2_subdev_ops smiapp_ops;
2365static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2366static const struct media_entity_operations smiapp_entity_ops;
2367
2368static int smiapp_registered(struct v4l2_subdev *subdev)
2369{
2370 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2371 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2372 struct smiapp_subdev *last = NULL;
2373 u32 tmp;
2374 unsigned int i;
2375 int rval;
2376
Sachin Kamat31c1d172012-08-17 01:49:02 -03002377 sensor->vana = devm_regulator_get(&client->dev, "VANA");
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002378 if (IS_ERR(sensor->vana)) {
2379 dev_err(&client->dev, "could not get regulator for vana\n");
2380 return -ENODEV;
2381 }
2382
Sakari Ailus25474282012-04-22 08:24:33 -03002383 if (!sensor->platform_data->set_xclk) {
Sachin Kamat31c1d172012-08-17 01:49:02 -03002384 sensor->ext_clk = devm_clk_get(&client->dev,
2385 sensor->platform_data->ext_clk_name);
Sakari Ailus25474282012-04-22 08:24:33 -03002386 if (IS_ERR(sensor->ext_clk)) {
2387 dev_err(&client->dev, "could not get clock %s\n",
2388 sensor->platform_data->ext_clk_name);
Sachin Kamat31c1d172012-08-17 01:49:02 -03002389 return -ENODEV;
Sakari Ailus25474282012-04-22 08:24:33 -03002390 }
2391
2392 rval = clk_set_rate(sensor->ext_clk,
2393 sensor->platform_data->ext_clk);
2394 if (rval < 0) {
2395 dev_err(&client->dev,
2396 "unable to set clock %s freq to %u\n",
2397 sensor->platform_data->ext_clk_name,
2398 sensor->platform_data->ext_clk);
Sachin Kamat31c1d172012-08-17 01:49:02 -03002399 return -ENODEV;
Sakari Ailus25474282012-04-22 08:24:33 -03002400 }
2401 }
2402
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002403 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) {
2404 if (gpio_request_one(sensor->platform_data->xshutdown, 0,
2405 "SMIA++ xshutdown") != 0) {
2406 dev_err(&client->dev,
2407 "unable to acquire reset gpio %d\n",
2408 sensor->platform_data->xshutdown);
Sachin Kamat31c1d172012-08-17 01:49:02 -03002409 return -ENODEV;
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002410 }
2411 }
2412
2413 rval = smiapp_power_on(sensor);
2414 if (rval) {
2415 rval = -ENODEV;
2416 goto out_smiapp_power_on;
2417 }
2418
2419 rval = smiapp_identify_module(subdev);
2420 if (rval) {
2421 rval = -ENODEV;
2422 goto out_power_off;
2423 }
2424
2425 rval = smiapp_get_all_limits(sensor);
2426 if (rval) {
2427 rval = -ENODEV;
2428 goto out_power_off;
2429 }
2430
2431 /*
2432 * Handle Sensor Module orientation on the board.
2433 *
2434 * The application of H-FLIP and V-FLIP on the sensor is modified by
2435 * the sensor orientation on the board.
2436 *
2437 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2438 * both H-FLIP and V-FLIP for normal operation which also implies
2439 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2440 * controls will need to be internally inverted.
2441 *
2442 * Rotation also changes the bayer pattern.
2443 */
2444 if (sensor->platform_data->module_board_orient ==
2445 SMIAPP_MODULE_BOARD_ORIENT_180)
2446 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2447 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2448
2449 rval = smiapp_get_mbus_formats(sensor);
2450 if (rval) {
2451 rval = -ENODEV;
2452 goto out_power_off;
2453 }
2454
2455 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2456 u32 val;
2457
Sakari Ailus1e73eea2012-04-22 08:55:10 -03002458 rval = smiapp_read(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002459 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2460 if (rval < 0) {
2461 rval = -ENODEV;
2462 goto out_power_off;
2463 }
2464 sensor->nbinning_subtypes = min_t(u8, val,
2465 SMIAPP_BINNING_SUBTYPES);
2466
2467 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2468 rval = smiapp_read(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03002469 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002470 if (rval < 0) {
2471 rval = -ENODEV;
2472 goto out_power_off;
2473 }
2474 sensor->binning_subtypes[i] =
2475 *(struct smiapp_binning_subtype *)&val;
2476
2477 dev_dbg(&client->dev, "binning %xx%x\n",
2478 sensor->binning_subtypes[i].horizontal,
2479 sensor->binning_subtypes[i].vertical);
2480 }
2481 }
2482 sensor->binning_horizontal = 1;
2483 sensor->binning_vertical = 1;
2484
Sakari Ailuseba66b32012-09-15 18:26:54 -03002485 if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
2486 dev_err(&client->dev, "sysfs ident entry creation failed\n");
2487 rval = -ENOENT;
2488 goto out_power_off;
2489 }
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002490 /* SMIA++ NVM initialization - it will be read from the sensor
2491 * when it is first requested by userspace.
2492 */
2493 if (sensor->minfo.smiapp_version && sensor->platform_data->nvm_size) {
Sachin Kamat31c1d172012-08-17 01:49:02 -03002494 sensor->nvm = devm_kzalloc(&client->dev,
2495 sensor->platform_data->nvm_size, GFP_KERNEL);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002496 if (sensor->nvm == NULL) {
2497 dev_err(&client->dev, "nvm buf allocation failed\n");
2498 rval = -ENOMEM;
Sakari Ailuseba66b32012-09-15 18:26:54 -03002499 goto out_ident_release;
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002500 }
2501
2502 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
2503 dev_err(&client->dev, "sysfs nvm entry failed\n");
2504 rval = -EBUSY;
Sakari Ailuseba66b32012-09-15 18:26:54 -03002505 goto out_ident_release;
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002506 }
2507 }
2508
2509 rval = smiapp_call_quirk(sensor, limits);
2510 if (rval) {
2511 dev_err(&client->dev, "limits quirks failed\n");
2512 goto out_nvm_release;
2513 }
2514
2515 /* We consider this as profile 0 sensor if any of these are zero. */
2516 if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
2517 !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
2518 !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
2519 !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
2520 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
2521 } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2522 != SMIAPP_SCALING_CAPABILITY_NONE) {
2523 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2524 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2525 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
2526 else
2527 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
2528 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2529 sensor->ssds_used++;
2530 } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2531 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
2532 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2533 sensor->ssds_used++;
2534 }
2535 sensor->binner = &sensor->ssds[sensor->ssds_used];
2536 sensor->ssds_used++;
2537 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
2538 sensor->ssds_used++;
2539
2540 sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
2541
2542 for (i = 0; i < SMIAPP_SUBDEVS; i++) {
2543 struct {
2544 struct smiapp_subdev *ssd;
2545 char *name;
2546 } const __this[] = {
2547 { sensor->scaler, "scaler", },
2548 { sensor->binner, "binner", },
2549 { sensor->pixel_array, "pixel array", },
2550 }, *_this = &__this[i];
2551 struct smiapp_subdev *this = _this->ssd;
2552
2553 if (!this)
2554 continue;
2555
2556 if (this != sensor->src)
2557 v4l2_subdev_init(&this->sd, &smiapp_ops);
2558
2559 this->sensor = sensor;
2560
2561 if (this == sensor->pixel_array) {
2562 this->npads = 1;
2563 } else {
2564 this->npads = 2;
2565 this->source_pad = 1;
2566 }
2567
2568 snprintf(this->sd.name,
2569 sizeof(this->sd.name), "%s %s",
2570 sensor->minfo.name, _this->name);
2571
2572 this->sink_fmt.width =
2573 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2574 this->sink_fmt.height =
2575 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2576 this->compose.width = this->sink_fmt.width;
2577 this->compose.height = this->sink_fmt.height;
2578 this->crop[this->source_pad] = this->compose;
2579 this->pads[this->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2580 if (this != sensor->pixel_array) {
2581 this->crop[this->sink_pad] = this->compose;
2582 this->pads[this->sink_pad].flags = MEDIA_PAD_FL_SINK;
2583 }
2584
2585 this->sd.entity.ops = &smiapp_entity_ops;
2586
2587 if (last == NULL) {
2588 last = this;
2589 continue;
2590 }
2591
2592 this->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2593 this->sd.internal_ops = &smiapp_internal_ops;
2594 this->sd.owner = NULL;
2595 v4l2_set_subdevdata(&this->sd, client);
2596
2597 rval = media_entity_init(&this->sd.entity,
2598 this->npads, this->pads, 0);
2599 if (rval) {
2600 dev_err(&client->dev,
2601 "media_entity_init failed\n");
2602 goto out_nvm_release;
2603 }
2604
2605 rval = media_entity_create_link(&this->sd.entity,
2606 this->source_pad,
2607 &last->sd.entity,
2608 last->sink_pad,
2609 MEDIA_LNK_FL_ENABLED |
2610 MEDIA_LNK_FL_IMMUTABLE);
2611 if (rval) {
2612 dev_err(&client->dev,
2613 "media_entity_create_link failed\n");
2614 goto out_nvm_release;
2615 }
2616
2617 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2618 &this->sd);
2619 if (rval) {
2620 dev_err(&client->dev,
2621 "v4l2_device_register_subdev failed\n");
2622 goto out_nvm_release;
2623 }
2624
2625 last = this;
2626 }
2627
2628 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
2629
2630 sensor->pixel_array->sd.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
2631
2632 /* final steps */
2633 smiapp_read_frame_fmt(sensor);
2634 rval = smiapp_init_controls(sensor);
2635 if (rval < 0)
2636 goto out_nvm_release;
2637
2638 rval = smiapp_update_mode(sensor);
2639 if (rval) {
2640 dev_err(&client->dev, "update mode failed\n");
2641 goto out_nvm_release;
2642 }
2643
2644 sensor->streaming = false;
2645 sensor->dev_init_done = true;
2646
2647 /* check flash capability */
Sakari Ailus1e73eea2012-04-22 08:55:10 -03002648 rval = smiapp_read(sensor, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY, &tmp);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002649 sensor->flash_capability = tmp;
2650 if (rval)
2651 goto out_nvm_release;
2652
2653 smiapp_power_off(sensor);
2654
2655 return 0;
2656
2657out_nvm_release:
2658 device_remove_file(&client->dev, &dev_attr_nvm);
2659
Sakari Ailuseba66b32012-09-15 18:26:54 -03002660out_ident_release:
2661 device_remove_file(&client->dev, &dev_attr_ident);
2662
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002663out_power_off:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002664 smiapp_power_off(sensor);
2665
2666out_smiapp_power_on:
2667 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2668 gpio_free(sensor->platform_data->xshutdown);
2669
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002670 return rval;
2671}
2672
2673static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2674{
2675 struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2676 struct smiapp_sensor *sensor = ssd->sensor;
2677 u32 mbus_code =
2678 smiapp_csi_data_formats[smiapp_pixel_order(sensor)].code;
2679 unsigned int i;
2680
2681 mutex_lock(&sensor->mutex);
2682
2683 for (i = 0; i < ssd->npads; i++) {
2684 struct v4l2_mbus_framefmt *try_fmt =
2685 v4l2_subdev_get_try_format(fh, i);
2686 struct v4l2_rect *try_crop = v4l2_subdev_get_try_crop(fh, i);
2687 struct v4l2_rect *try_comp;
2688
2689 try_fmt->width = sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2690 try_fmt->height = sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2691 try_fmt->code = mbus_code;
2692
2693 try_crop->top = 0;
2694 try_crop->left = 0;
2695 try_crop->width = try_fmt->width;
2696 try_crop->height = try_fmt->height;
2697
2698 if (ssd != sensor->pixel_array)
2699 continue;
2700
2701 try_comp = v4l2_subdev_get_try_compose(fh, i);
2702 *try_comp = *try_crop;
2703 }
2704
2705 mutex_unlock(&sensor->mutex);
2706
2707 return smiapp_set_power(sd, 1);
2708}
2709
2710static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2711{
2712 return smiapp_set_power(sd, 0);
2713}
2714
2715static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2716 .s_stream = smiapp_set_stream,
2717};
2718
2719static const struct v4l2_subdev_core_ops smiapp_core_ops = {
2720 .s_power = smiapp_set_power,
2721};
2722
2723static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2724 .enum_mbus_code = smiapp_enum_mbus_code,
2725 .get_fmt = smiapp_get_format,
2726 .set_fmt = smiapp_set_format,
2727 .get_selection = smiapp_get_selection,
2728 .set_selection = smiapp_set_selection,
2729};
2730
2731static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2732 .g_skip_frames = smiapp_get_skip_frames,
2733};
2734
2735static const struct v4l2_subdev_ops smiapp_ops = {
2736 .core = &smiapp_core_ops,
2737 .video = &smiapp_video_ops,
2738 .pad = &smiapp_pad_ops,
2739 .sensor = &smiapp_sensor_ops,
2740};
2741
2742static const struct media_entity_operations smiapp_entity_ops = {
2743 .link_validate = v4l2_subdev_link_validate,
2744};
2745
2746static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2747 .registered = smiapp_registered,
2748 .open = smiapp_open,
2749 .close = smiapp_close,
2750};
2751
2752static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2753 .open = smiapp_open,
2754 .close = smiapp_close,
2755};
2756
2757/* -----------------------------------------------------------------------------
2758 * I2C Driver
2759 */
2760
2761#ifdef CONFIG_PM
2762
2763static int smiapp_suspend(struct device *dev)
2764{
2765 struct i2c_client *client = to_i2c_client(dev);
2766 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2767 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2768 bool streaming;
2769
2770 BUG_ON(mutex_is_locked(&sensor->mutex));
2771
2772 if (sensor->power_count == 0)
2773 return 0;
2774
2775 if (sensor->streaming)
2776 smiapp_stop_streaming(sensor);
2777
2778 streaming = sensor->streaming;
2779
2780 smiapp_power_off(sensor);
2781
2782 /* save state for resume */
2783 sensor->streaming = streaming;
2784
2785 return 0;
2786}
2787
2788static int smiapp_resume(struct device *dev)
2789{
2790 struct i2c_client *client = to_i2c_client(dev);
2791 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2792 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2793 int rval;
2794
2795 if (sensor->power_count == 0)
2796 return 0;
2797
2798 rval = smiapp_power_on(sensor);
2799 if (rval)
2800 return rval;
2801
2802 if (sensor->streaming)
2803 rval = smiapp_start_streaming(sensor);
2804
2805 return rval;
2806}
2807
2808#else
2809
2810#define smiapp_suspend NULL
2811#define smiapp_resume NULL
2812
2813#endif /* CONFIG_PM */
2814
2815static int smiapp_probe(struct i2c_client *client,
2816 const struct i2c_device_id *devid)
2817{
2818 struct smiapp_sensor *sensor;
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002819
2820 if (client->dev.platform_data == NULL)
2821 return -ENODEV;
2822
Sachin Kamat31c1d172012-08-17 01:49:02 -03002823 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002824 if (sensor == NULL)
2825 return -ENOMEM;
2826
2827 sensor->platform_data = client->dev.platform_data;
2828 mutex_init(&sensor->mutex);
2829 mutex_init(&sensor->power_mutex);
2830 sensor->src = &sensor->ssds[sensor->ssds_used];
2831
2832 v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2833 sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2834 sensor->src->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2835 sensor->src->sensor = sensor;
2836
2837 sensor->src->pads[0].flags = MEDIA_PAD_FL_SOURCE;
Sachin Kamat31c1d172012-08-17 01:49:02 -03002838 return media_entity_init(&sensor->src->sd.entity, 2,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002839 sensor->src->pads, 0);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002840}
2841
2842static int __exit smiapp_remove(struct i2c_client *client)
2843{
2844 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2845 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2846 unsigned int i;
2847
2848 if (sensor->power_count) {
2849 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2850 gpio_set_value(sensor->platform_data->xshutdown, 0);
Sakari Ailus25474282012-04-22 08:24:33 -03002851 if (sensor->platform_data->set_xclk)
2852 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
2853 else
2854 clk_disable(sensor->ext_clk);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002855 sensor->power_count = 0;
2856 }
2857
Sakari Ailuseba66b32012-09-15 18:26:54 -03002858 device_remove_file(&client->dev, &dev_attr_ident);
Sachin Kamat31c1d172012-08-17 01:49:02 -03002859 if (sensor->nvm)
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002860 device_remove_file(&client->dev, &dev_attr_nvm);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002861
2862 for (i = 0; i < sensor->ssds_used; i++) {
2863 media_entity_cleanup(&sensor->ssds[i].sd.entity);
2864 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2865 }
2866 smiapp_free_controls(sensor);
2867 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
2868 gpio_free(sensor->platform_data->xshutdown);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002869
2870 return 0;
2871}
2872
2873static const struct i2c_device_id smiapp_id_table[] = {
2874 { SMIAPP_NAME, 0 },
2875 { },
2876};
2877MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
2878
2879static const struct dev_pm_ops smiapp_pm_ops = {
2880 .suspend = smiapp_suspend,
2881 .resume = smiapp_resume,
2882};
2883
2884static struct i2c_driver smiapp_i2c_driver = {
2885 .driver = {
2886 .name = SMIAPP_NAME,
2887 .pm = &smiapp_pm_ops,
2888 },
2889 .probe = smiapp_probe,
2890 .remove = __exit_p(smiapp_remove),
2891 .id_table = smiapp_id_table,
2892};
2893
2894module_i2c_driver(smiapp_i2c_driver);
2895
2896MODULE_AUTHOR("Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>");
2897MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
2898MODULE_LICENSE("GPL");