blob: 868ad0ba59b6f1402f6c8c1a3807f6e9be8cbf8a [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
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300279 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0) {
280 /*
281 * Fill in operational clock divisors limits from the
282 * video timing ones. On profile 0 sensors the
283 * requirements regarding them are essentially the
284 * same as on VT ones.
285 */
286 lim.min_op_sys_clk_div = lim.min_vt_sys_clk_div;
287 lim.max_op_sys_clk_div = lim.max_vt_sys_clk_div;
288 lim.min_op_pix_clk_div = lim.min_vt_pix_clk_div;
289 lim.max_op_pix_clk_div = lim.max_vt_pix_clk_div;
290 lim.min_op_sys_clk_freq_hz = lim.min_vt_sys_clk_freq_hz;
291 lim.max_op_sys_clk_freq_hz = lim.max_vt_sys_clk_freq_hz;
292 lim.min_op_pix_clk_freq_hz = lim.min_vt_pix_clk_freq_hz;
293 lim.max_op_pix_clk_freq_hz = lim.max_vt_pix_clk_freq_hz;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300294 }
295
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300296 pll->binning_horizontal = sensor->binning_horizontal;
297 pll->binning_vertical = sensor->binning_vertical;
298 pll->link_freq =
299 sensor->link_freq->qmenu_int[sensor->link_freq->val];
300 pll->scale_m = sensor->scale_m;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300301 pll->bits_per_pixel = sensor->csi_format->compressed;
302
303 rval = smiapp_pll_calculate(&client->dev, &lim, pll);
304 if (rval < 0)
305 return rval;
306
307 sensor->pixel_rate_parray->cur.val64 = pll->vt_pix_clk_freq_hz;
308 sensor->pixel_rate_csi->cur.val64 = pll->pixel_rate_csi;
309
310 return 0;
311}
312
313
314/*
315 *
316 * V4L2 Controls handling
317 *
318 */
319
320static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
321{
322 struct v4l2_ctrl *ctrl = sensor->exposure;
323 int max;
324
325 max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
326 + sensor->vblank->val
327 - sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN];
328
329 ctrl->maximum = max;
330 if (ctrl->default_value > max)
331 ctrl->default_value = max;
332 if (ctrl->val > max)
333 ctrl->val = max;
334 if (ctrl->cur.val > max)
335 ctrl->cur.val = max;
336}
337
338/*
339 * Order matters.
340 *
341 * 1. Bits-per-pixel, descending.
342 * 2. Bits-per-pixel compressed, descending.
343 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
344 * orders must be defined.
345 */
346static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
347 { V4L2_MBUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
348 { V4L2_MBUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
349 { V4L2_MBUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
350 { V4L2_MBUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
351 { V4L2_MBUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
352 { V4L2_MBUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
353 { V4L2_MBUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
354 { V4L2_MBUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
355 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
356 { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
357 { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
358 { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
Sakari Ailusb8cc8d72012-04-27 10:01:46 -0300359 { V4L2_MBUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, },
360 { V4L2_MBUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, },
361 { V4L2_MBUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, },
362 { V4L2_MBUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, },
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300363};
364
365const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
366
367#define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
368 - (unsigned long)smiapp_csi_data_formats) \
369 / sizeof(*smiapp_csi_data_formats))
370
371static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
372{
373 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
374 int flip = 0;
375
376 if (sensor->hflip) {
377 if (sensor->hflip->val)
378 flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
379
380 if (sensor->vflip->val)
381 flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
382 }
383
384 flip ^= sensor->hvflip_inv_mask;
385
386 dev_dbg(&client->dev, "flip %d\n", flip);
387 return sensor->default_pixel_order ^ flip;
388}
389
390static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
391{
392 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
393 unsigned int csi_format_idx =
394 to_csi_format_idx(sensor->csi_format) & ~3;
395 unsigned int internal_csi_format_idx =
396 to_csi_format_idx(sensor->internal_csi_format) & ~3;
397 unsigned int pixel_order = smiapp_pixel_order(sensor);
398
399 sensor->mbus_frame_fmts =
400 sensor->default_mbus_frame_fmts << pixel_order;
401 sensor->csi_format =
402 &smiapp_csi_data_formats[csi_format_idx + pixel_order];
403 sensor->internal_csi_format =
404 &smiapp_csi_data_formats[internal_csi_format_idx
405 + pixel_order];
406
407 BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
408 >= ARRAY_SIZE(smiapp_csi_data_formats));
409 BUG_ON(min(internal_csi_format_idx, csi_format_idx) < 0);
410
411 dev_dbg(&client->dev, "new pixel order %s\n",
412 pixel_order_str[pixel_order]);
413}
414
415static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
416{
417 struct smiapp_sensor *sensor =
418 container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
419 ->sensor;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300420 u32 orient = 0;
421 int exposure;
422 int rval;
423
424 switch (ctrl->id) {
425 case V4L2_CID_ANALOGUE_GAIN:
426 return smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300427 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300428 SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
429
430 case V4L2_CID_EXPOSURE:
431 return smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300432 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300433 SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
434
435 case V4L2_CID_HFLIP:
436 case V4L2_CID_VFLIP:
437 if (sensor->streaming)
438 return -EBUSY;
439
440 if (sensor->hflip->val)
441 orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
442
443 if (sensor->vflip->val)
444 orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
445
446 orient ^= sensor->hvflip_inv_mask;
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300447 rval = smiapp_write(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300448 SMIAPP_REG_U8_IMAGE_ORIENTATION,
449 orient);
450 if (rval < 0)
451 return rval;
452
453 smiapp_update_mbus_formats(sensor);
454
455 return 0;
456
457 case V4L2_CID_VBLANK:
458 exposure = sensor->exposure->val;
459
460 __smiapp_update_exposure_limits(sensor);
461
462 if (exposure > sensor->exposure->maximum) {
463 sensor->exposure->val =
464 sensor->exposure->maximum;
465 rval = smiapp_set_ctrl(
466 sensor->exposure);
467 if (rval < 0)
468 return rval;
469 }
470
471 return smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300472 sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300473 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
474 + ctrl->val);
475
476 case V4L2_CID_HBLANK:
477 return smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300478 sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300479 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
480 + ctrl->val);
481
482 case V4L2_CID_LINK_FREQ:
483 if (sensor->streaming)
484 return -EBUSY;
485
486 return smiapp_pll_update(sensor);
487
488 default:
489 return -EINVAL;
490 }
491}
492
493static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
494 .s_ctrl = smiapp_set_ctrl,
495};
496
497static int smiapp_init_controls(struct smiapp_sensor *sensor)
498{
499 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
Sakari Ailus06b491f2012-05-06 07:03:46 -0300500 unsigned int max;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300501 int rval;
502
503 rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 7);
504 if (rval)
505 return rval;
506 sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
507
508 sensor->analog_gain = v4l2_ctrl_new_std(
509 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
510 V4L2_CID_ANALOGUE_GAIN,
511 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN],
512 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX],
513 max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U),
514 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]);
515
516 /* Exposure limits will be updated soon, use just something here. */
517 sensor->exposure = v4l2_ctrl_new_std(
518 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
519 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
520
521 sensor->hflip = v4l2_ctrl_new_std(
522 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
523 V4L2_CID_HFLIP, 0, 1, 1, 0);
524 sensor->vflip = v4l2_ctrl_new_std(
525 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
526 V4L2_CID_VFLIP, 0, 1, 1, 0);
527
528 sensor->vblank = v4l2_ctrl_new_std(
529 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
530 V4L2_CID_VBLANK, 0, 1, 1, 0);
531
532 if (sensor->vblank)
533 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
534
535 sensor->hblank = v4l2_ctrl_new_std(
536 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
537 V4L2_CID_HBLANK, 0, 1, 1, 0);
538
539 if (sensor->hblank)
540 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
541
542 sensor->pixel_rate_parray = v4l2_ctrl_new_std(
543 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
544 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
545
546 if (sensor->pixel_array->ctrl_handler.error) {
547 dev_err(&client->dev,
548 "pixel array controls initialization failed (%d)\n",
549 sensor->pixel_array->ctrl_handler.error);
550 rval = sensor->pixel_array->ctrl_handler.error;
551 goto error;
552 }
553
554 sensor->pixel_array->sd.ctrl_handler =
555 &sensor->pixel_array->ctrl_handler;
556
557 v4l2_ctrl_cluster(2, &sensor->hflip);
558
559 rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
560 if (rval)
561 goto error;
562 sensor->src->ctrl_handler.lock = &sensor->mutex;
563
Sakari Ailus06b491f2012-05-06 07:03:46 -0300564 for (max = 0; sensor->platform_data->op_sys_clock[max + 1]; max++);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300565
Sakari Ailus06b491f2012-05-06 07:03:46 -0300566 sensor->link_freq = v4l2_ctrl_new_int_menu(
567 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
568 V4L2_CID_LINK_FREQ, max, 0,
569 sensor->platform_data->op_sys_clock);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300570
571 sensor->pixel_rate_csi = v4l2_ctrl_new_std(
572 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
573 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
574
575 if (sensor->src->ctrl_handler.error) {
576 dev_err(&client->dev,
577 "src controls initialization failed (%d)\n",
578 sensor->src->ctrl_handler.error);
579 rval = sensor->src->ctrl_handler.error;
580 goto error;
581 }
582
583 sensor->src->sd.ctrl_handler =
584 &sensor->src->ctrl_handler;
585
586 return 0;
587
588error:
589 v4l2_ctrl_handler_free(&sensor->pixel_array->ctrl_handler);
590 v4l2_ctrl_handler_free(&sensor->src->ctrl_handler);
591
592 return rval;
593}
594
595static void smiapp_free_controls(struct smiapp_sensor *sensor)
596{
597 unsigned int i;
598
599 for (i = 0; i < sensor->ssds_used; i++)
600 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
601}
602
603static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
604 unsigned int n)
605{
606 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
607 unsigned int i;
608 u32 val;
609 int rval;
610
611 for (i = 0; i < n; i++) {
612 rval = smiapp_read(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300613 sensor, smiapp_reg_limits[limit[i]].addr, &val);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300614 if (rval)
615 return rval;
616 sensor->limits[limit[i]] = val;
617 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %d, 0x%x\n",
618 smiapp_reg_limits[limit[i]].addr,
619 smiapp_reg_limits[limit[i]].what, val, val);
620 }
621
622 return 0;
623}
624
625static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
626{
627 unsigned int i;
628 int rval;
629
630 for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
631 rval = smiapp_get_limits(sensor, &i, 1);
632 if (rval < 0)
633 return rval;
634 }
635
636 if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
637 smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
638
639 return 0;
640}
641
642static int smiapp_get_limits_binning(struct smiapp_sensor *sensor)
643{
Sakari Ailus3de886e2012-04-26 11:55:13 -0300644 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300645 static u32 const limits[] = {
646 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN,
647 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN,
648 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN,
649 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN,
650 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN,
651 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN,
652 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN,
653 };
654 static u32 const limits_replace[] = {
655 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES,
656 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES,
657 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK,
658 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK,
659 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK,
660 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN,
661 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN,
662 };
Sakari Ailus3de886e2012-04-26 11:55:13 -0300663 unsigned int i;
664 int rval;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300665
666 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
667 SMIAPP_BINNING_CAPABILITY_NO) {
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300668 for (i = 0; i < ARRAY_SIZE(limits); i++)
669 sensor->limits[limits[i]] =
670 sensor->limits[limits_replace[i]];
671
672 return 0;
673 }
674
Sakari Ailus3de886e2012-04-26 11:55:13 -0300675 rval = smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits));
676 if (rval < 0)
677 return rval;
678
679 /*
680 * Sanity check whether the binning limits are valid. If not,
681 * use the non-binning ones.
682 */
683 if (sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN]
684 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN]
685 && sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN])
686 return 0;
687
688 for (i = 0; i < ARRAY_SIZE(limits); i++) {
689 dev_dbg(&client->dev,
690 "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
691 smiapp_reg_limits[limits[i]].addr,
692 smiapp_reg_limits[limits[i]].what,
693 sensor->limits[limits_replace[i]],
694 sensor->limits[limits_replace[i]]);
695 sensor->limits[limits[i]] =
696 sensor->limits[limits_replace[i]];
697 }
698
699 return 0;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300700}
701
702static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
703{
704 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
705 unsigned int type, n;
706 unsigned int i, pixel_order;
707 int rval;
708
709 rval = smiapp_read(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300710 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300711 if (rval)
712 return rval;
713
714 dev_dbg(&client->dev, "data_format_model_type %d\n", type);
715
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300716 rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300717 &pixel_order);
718 if (rval)
719 return rval;
720
721 if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
722 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
723 return -EINVAL;
724 }
725
726 dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
727 pixel_order_str[pixel_order]);
728
729 switch (type) {
730 case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
731 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
732 break;
733 case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
734 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
735 break;
736 default:
737 return -EINVAL;
738 }
739
740 sensor->default_pixel_order = pixel_order;
741 sensor->mbus_frame_fmts = 0;
742
743 for (i = 0; i < n; i++) {
744 unsigned int fmt, j;
745
746 rval = smiapp_read(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300747 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300748 SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
749 if (rval)
750 return rval;
751
752 dev_dbg(&client->dev, "bpp %d, compressed %d\n",
753 fmt >> 8, (u8)fmt);
754
755 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
756 const struct smiapp_csi_data_format *f =
757 &smiapp_csi_data_formats[j];
758
759 if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
760 continue;
761
762 if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
763 continue;
764
765 dev_dbg(&client->dev, "jolly good! %d\n", j);
766
767 sensor->default_mbus_frame_fmts |= 1 << j;
Sakari Ailusf67e1572012-09-15 17:19:49 -0300768 if (!sensor->csi_format
769 || f->width > sensor->csi_format->width
770 || (f->width == sensor->csi_format->width
771 && f->compressed
772 > sensor->csi_format->compressed)) {
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300773 sensor->csi_format = f;
774 sensor->internal_csi_format = f;
775 }
776 }
777 }
778
779 if (!sensor->csi_format) {
780 dev_err(&client->dev, "no supported mbus code found\n");
781 return -EINVAL;
782 }
783
784 smiapp_update_mbus_formats(sensor);
785
786 return 0;
787}
788
789static void smiapp_update_blanking(struct smiapp_sensor *sensor)
790{
791 struct v4l2_ctrl *vblank = sensor->vblank;
792 struct v4l2_ctrl *hblank = sensor->hblank;
793
794 vblank->minimum =
795 max_t(int,
796 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
797 sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
798 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
799 vblank->maximum =
800 sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
801 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
802
803 vblank->val = clamp_t(int, vblank->val,
804 vblank->minimum, vblank->maximum);
805 vblank->default_value = vblank->minimum;
806 vblank->val = vblank->val;
807 vblank->cur.val = vblank->val;
808
809 hblank->minimum =
810 max_t(int,
811 sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
812 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
813 sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
814 hblank->maximum =
815 sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
816 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
817
818 hblank->val = clamp_t(int, hblank->val,
819 hblank->minimum, hblank->maximum);
820 hblank->default_value = hblank->minimum;
821 hblank->val = hblank->val;
822 hblank->cur.val = hblank->val;
823
824 __smiapp_update_exposure_limits(sensor);
825}
826
827static int smiapp_update_mode(struct smiapp_sensor *sensor)
828{
829 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
830 unsigned int binning_mode;
831 int rval;
832
833 dev_dbg(&client->dev, "frame size: %dx%d\n",
834 sensor->src->crop[SMIAPP_PAD_SRC].width,
835 sensor->src->crop[SMIAPP_PAD_SRC].height);
836 dev_dbg(&client->dev, "csi format width: %d\n",
837 sensor->csi_format->width);
838
839 /* Binning has to be set up here; it affects limits */
840 if (sensor->binning_horizontal == 1 &&
841 sensor->binning_vertical == 1) {
842 binning_mode = 0;
843 } else {
844 u8 binning_type =
845 (sensor->binning_horizontal << 4)
846 | sensor->binning_vertical;
847
848 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300849 sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300850 if (rval < 0)
851 return rval;
852
853 binning_mode = 1;
854 }
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300855 rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300856 if (rval < 0)
857 return rval;
858
859 /* Get updated limits due to binning */
860 rval = smiapp_get_limits_binning(sensor);
861 if (rval < 0)
862 return rval;
863
864 rval = smiapp_pll_update(sensor);
865 if (rval < 0)
866 return rval;
867
868 /* Output from pixel array, including blanking */
869 smiapp_update_blanking(sensor);
870
871 dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
872 dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
873
874 dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
875 sensor->pll.vt_pix_clk_freq_hz /
876 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
877 + sensor->hblank->val) *
878 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
879 + sensor->vblank->val) / 100));
880
881 return 0;
882}
883
884/*
885 *
886 * SMIA++ NVM handling
887 *
888 */
889static int smiapp_read_nvm(struct smiapp_sensor *sensor,
890 unsigned char *nvm)
891{
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300892 u32 i, s, p, np, v;
Sakari Ailus04582942012-04-26 12:05:50 -0300893 int rval = 0, rval2;
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300894
895 np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
896 for (p = 0; p < np; p++) {
897 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300898 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300899 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
900 if (rval)
901 goto out;
902
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300903 rval = smiapp_write(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300904 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
905 SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
906 SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
907 if (rval)
908 goto out;
909
910 for (i = 0; i < 1000; i++) {
911 rval = smiapp_read(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300912 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300913 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);
914
915 if (rval)
916 goto out;
917
918 if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
919 break;
920
921 if (--i == 0) {
922 rval = -ETIMEDOUT;
923 goto out;
924 }
925
926 }
927
928 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
929 rval = smiapp_read(
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300930 sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300931 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
932 &v);
933 if (rval)
934 goto out;
935
936 *nvm++ = v;
937 }
938 }
939
940out:
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300941 rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300942 if (rval < 0)
943 return rval;
944 else
945 return rval2;
946}
947
948/*
949 *
950 * SMIA++ CCI address control
951 *
952 */
953static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
954{
955 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
956 int rval;
957 u32 val;
958
959 client->addr = sensor->platform_data->i2c_addr_dfl;
960
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300961 rval = smiapp_write(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300962 SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
963 sensor->platform_data->i2c_addr_alt << 1);
964 if (rval)
965 return rval;
966
967 client->addr = sensor->platform_data->i2c_addr_alt;
968
969 /* verify addr change went ok */
Sakari Ailus1e73eea2012-04-22 08:55:10 -0300970 rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300971 if (rval)
972 return rval;
973
974 if (val != sensor->platform_data->i2c_addr_alt << 1)
975 return -ENODEV;
976
977 return 0;
978}
979
980/*
981 *
982 * SMIA++ Mode Control
983 *
984 */
985static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
986{
Sakari Ailusccfc97b2012-03-03 17:19:52 -0300987 struct smiapp_flash_strobe_parms *strobe_setup;
988 unsigned int ext_freq = sensor->platform_data->ext_clk;
989 u32 tmp;
990 u32 strobe_adjustment;
991 u32 strobe_width_high_rs;
992 int rval;
993
994 strobe_setup = sensor->platform_data->strobe_setup;
995
996 /*
997 * How to calculate registers related to strobe length. Please
998 * do not change, or if you do at least know what you're
999 * doing. :-)
1000 *
1001 * Sakari Ailus <sakari.ailus@maxwell.research.nokia.com> 2010-10-25
1002 *
1003 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1004 * / EXTCLK freq [Hz]) * flash_strobe_adjustment
1005 *
1006 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1007 * flash_strobe_adjustment E N, [1 - 0xff]
1008 *
1009 * The formula above is written as below to keep it on one
1010 * line:
1011 *
1012 * l / 10^6 = w / e * a
1013 *
1014 * Let's mark w * a by x:
1015 *
1016 * x = w * a
1017 *
1018 * Thus, we get:
1019 *
1020 * x = l * e / 10^6
1021 *
1022 * The strobe width must be at least as long as requested,
1023 * thus rounding upwards is needed.
1024 *
1025 * x = (l * e + 10^6 - 1) / 10^6
1026 * -----------------------------
1027 *
1028 * Maximum possible accuracy is wanted at all times. Thus keep
1029 * a as small as possible.
1030 *
1031 * Calculate a, assuming maximum w, with rounding upwards:
1032 *
1033 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1034 * -------------------------------------
1035 *
1036 * Thus, we also get w, with that a, with rounding upwards:
1037 *
1038 * w = (x + a - 1) / a
1039 * -------------------
1040 *
1041 * To get limits:
1042 *
1043 * x E [1, (2^16 - 1) * (2^8 - 1)]
1044 *
1045 * Substituting maximum x to the original formula (with rounding),
1046 * the maximum l is thus
1047 *
1048 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1049 *
1050 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1051 * --------------------------------------------------
1052 *
1053 * flash_strobe_length must be clamped between 1 and
1054 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1055 *
1056 * Then,
1057 *
1058 * flash_strobe_adjustment = ((flash_strobe_length *
1059 * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1060 *
1061 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1062 * EXTCLK freq + 10^6 - 1) / 10^6 +
1063 * flash_strobe_adjustment - 1) / flash_strobe_adjustment
1064 */
1065 tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1066 1000000 + 1, ext_freq);
1067 strobe_setup->strobe_width_high_us =
1068 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1069
1070 tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1071 1000000 - 1), 1000000ULL);
1072 strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1073 strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1074 strobe_adjustment;
1075
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001076 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001077 strobe_setup->mode);
1078 if (rval < 0)
1079 goto out;
1080
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001081 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001082 strobe_adjustment);
1083 if (rval < 0)
1084 goto out;
1085
1086 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001087 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001088 strobe_width_high_rs);
1089 if (rval < 0)
1090 goto out;
1091
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001092 rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001093 strobe_setup->strobe_delay);
1094 if (rval < 0)
1095 goto out;
1096
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001097 rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001098 strobe_setup->stobe_start_point);
1099 if (rval < 0)
1100 goto out;
1101
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001102 rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001103 strobe_setup->trigger);
1104
1105out:
1106 sensor->platform_data->strobe_setup->trigger = 0;
1107
1108 return rval;
1109}
1110
1111/* -----------------------------------------------------------------------------
1112 * Power management
1113 */
1114
1115static int smiapp_power_on(struct smiapp_sensor *sensor)
1116{
1117 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1118 unsigned int sleep;
1119 int rval;
1120
1121 rval = regulator_enable(sensor->vana);
1122 if (rval) {
1123 dev_err(&client->dev, "failed to enable vana regulator\n");
1124 return rval;
1125 }
1126 usleep_range(1000, 1000);
1127
Sakari Ailus25474282012-04-22 08:24:33 -03001128 if (sensor->platform_data->set_xclk)
1129 rval = sensor->platform_data->set_xclk(
1130 &sensor->src->sd, sensor->platform_data->ext_clk);
1131 else
1132 rval = clk_enable(sensor->ext_clk);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001133 if (rval < 0) {
1134 dev_dbg(&client->dev, "failed to set xclk\n");
1135 goto out_xclk_fail;
1136 }
1137 usleep_range(1000, 1000);
1138
1139 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1140 gpio_set_value(sensor->platform_data->xshutdown, 1);
1141
1142 sleep = SMIAPP_RESET_DELAY(sensor->platform_data->ext_clk);
1143 usleep_range(sleep, sleep);
1144
1145 /*
1146 * Failures to respond to the address change command have been noticed.
1147 * Those failures seem to be caused by the sensor requiring a longer
1148 * boot time than advertised. An additional 10ms delay seems to work
1149 * around the issue, but the SMIA++ I2C write retry hack makes the delay
1150 * unnecessary. The failures need to be investigated to find a proper
1151 * fix, and a delay will likely need to be added here if the I2C write
1152 * retry hack is reverted before the root cause of the boot time issue
1153 * is found.
1154 */
1155
1156 if (sensor->platform_data->i2c_addr_alt) {
1157 rval = smiapp_change_cci_addr(sensor);
1158 if (rval) {
1159 dev_err(&client->dev, "cci address change error\n");
1160 goto out_cci_addr_fail;
1161 }
1162 }
1163
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001164 rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001165 SMIAPP_SOFTWARE_RESET);
1166 if (rval < 0) {
1167 dev_err(&client->dev, "software reset failed\n");
1168 goto out_cci_addr_fail;
1169 }
1170
1171 if (sensor->platform_data->i2c_addr_alt) {
1172 rval = smiapp_change_cci_addr(sensor);
1173 if (rval) {
1174 dev_err(&client->dev, "cci address change error\n");
1175 goto out_cci_addr_fail;
1176 }
1177 }
1178
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001179 rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001180 SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1181 if (rval) {
1182 dev_err(&client->dev, "compression mode set failed\n");
1183 goto out_cci_addr_fail;
1184 }
1185
1186 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001187 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001188 sensor->platform_data->ext_clk / (1000000 / (1 << 8)));
1189 if (rval) {
1190 dev_err(&client->dev, "extclk frequency set failed\n");
1191 goto out_cci_addr_fail;
1192 }
1193
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001194 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001195 sensor->platform_data->lanes - 1);
1196 if (rval) {
1197 dev_err(&client->dev, "csi lane mode set failed\n");
1198 goto out_cci_addr_fail;
1199 }
1200
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001201 rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001202 SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1203 if (rval) {
1204 dev_err(&client->dev, "fast standby set failed\n");
1205 goto out_cci_addr_fail;
1206 }
1207
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001208 rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001209 sensor->platform_data->csi_signalling_mode);
1210 if (rval) {
1211 dev_err(&client->dev, "csi signalling mode set failed\n");
1212 goto out_cci_addr_fail;
1213 }
1214
1215 /* DPHY control done by sensor based on requested link rate */
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001216 rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001217 SMIAPP_DPHY_CTRL_UI);
1218 if (rval < 0)
1219 return rval;
1220
1221 rval = smiapp_call_quirk(sensor, post_poweron);
1222 if (rval) {
1223 dev_err(&client->dev, "post_poweron quirks failed\n");
1224 goto out_cci_addr_fail;
1225 }
1226
1227 /* Are we still initialising...? If yes, return here. */
1228 if (!sensor->pixel_array)
1229 return 0;
1230
1231 rval = v4l2_ctrl_handler_setup(
1232 &sensor->pixel_array->ctrl_handler);
1233 if (rval)
1234 goto out_cci_addr_fail;
1235
1236 rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1237 if (rval)
1238 goto out_cci_addr_fail;
1239
1240 mutex_lock(&sensor->mutex);
1241 rval = smiapp_update_mode(sensor);
1242 mutex_unlock(&sensor->mutex);
1243 if (rval < 0)
1244 goto out_cci_addr_fail;
1245
1246 return 0;
1247
1248out_cci_addr_fail:
1249 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1250 gpio_set_value(sensor->platform_data->xshutdown, 0);
Sakari Ailus25474282012-04-22 08:24:33 -03001251 if (sensor->platform_data->set_xclk)
1252 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1253 else
1254 clk_disable(sensor->ext_clk);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001255
1256out_xclk_fail:
1257 regulator_disable(sensor->vana);
1258 return rval;
1259}
1260
1261static void smiapp_power_off(struct smiapp_sensor *sensor)
1262{
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001263 /*
1264 * Currently power/clock to lens are enable/disabled separately
1265 * but they are essentially the same signals. So if the sensor is
1266 * powered off while the lens is powered on the sensor does not
1267 * really see a power off and next time the cci address change
1268 * will fail. So do a soft reset explicitly here.
1269 */
1270 if (sensor->platform_data->i2c_addr_alt)
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001271 smiapp_write(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001272 SMIAPP_REG_U8_SOFTWARE_RESET,
1273 SMIAPP_SOFTWARE_RESET);
1274
1275 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN)
1276 gpio_set_value(sensor->platform_data->xshutdown, 0);
Sakari Ailus25474282012-04-22 08:24:33 -03001277 if (sensor->platform_data->set_xclk)
1278 sensor->platform_data->set_xclk(&sensor->src->sd, 0);
1279 else
1280 clk_disable(sensor->ext_clk);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001281 usleep_range(5000, 5000);
1282 regulator_disable(sensor->vana);
1283 sensor->streaming = 0;
1284}
1285
1286static int smiapp_set_power(struct v4l2_subdev *subdev, int on)
1287{
1288 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1289 int ret = 0;
1290
1291 mutex_lock(&sensor->power_mutex);
1292
1293 /*
1294 * If the power count is modified from 0 to != 0 or from != 0
1295 * to 0, update the power state.
1296 */
1297 if (!sensor->power_count == !on)
1298 goto out;
1299
1300 if (on) {
1301 /* Power on and perform initialisation. */
1302 ret = smiapp_power_on(sensor);
1303 if (ret < 0)
1304 goto out;
1305 } else {
1306 smiapp_power_off(sensor);
1307 }
1308
1309 /* Update the power count. */
1310 sensor->power_count += on ? 1 : -1;
1311 WARN_ON(sensor->power_count < 0);
1312
1313out:
1314 mutex_unlock(&sensor->power_mutex);
1315 return ret;
1316}
1317
1318/* -----------------------------------------------------------------------------
1319 * Video stream management
1320 */
1321
1322static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1323{
1324 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1325 int rval;
1326
1327 mutex_lock(&sensor->mutex);
1328
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001329 rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001330 (sensor->csi_format->width << 8) |
1331 sensor->csi_format->compressed);
1332 if (rval)
1333 goto out;
1334
1335 rval = smiapp_pll_configure(sensor);
1336 if (rval)
1337 goto out;
1338
1339 /* Analog crop start coordinates */
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001340 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001341 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1342 if (rval < 0)
1343 goto out;
1344
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001345 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001346 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1347 if (rval < 0)
1348 goto out;
1349
1350 /* Analog crop end coordinates */
1351 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001352 sensor, SMIAPP_REG_U16_X_ADDR_END,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001353 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1354 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1355 if (rval < 0)
1356 goto out;
1357
1358 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001359 sensor, SMIAPP_REG_U16_Y_ADDR_END,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001360 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1361 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1362 if (rval < 0)
1363 goto out;
1364
1365 /*
1366 * Output from pixel array, including blanking, is set using
1367 * controls below. No need to set here.
1368 */
1369
1370 /* Digital crop */
1371 if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1372 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1373 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001374 sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001375 sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1376 if (rval < 0)
1377 goto out;
1378
1379 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001380 sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001381 sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1382 if (rval < 0)
1383 goto out;
1384
1385 rval = smiapp_write(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001386 sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001387 sensor->scaler->crop[SMIAPP_PAD_SINK].width);
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_IMAGE_HEIGHT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001393 sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1394 if (rval < 0)
1395 goto out;
1396 }
1397
1398 /* Scaling */
1399 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1400 != SMIAPP_SCALING_CAPABILITY_NONE) {
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001401 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001402 sensor->scaling_mode);
1403 if (rval < 0)
1404 goto out;
1405
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001406 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001407 sensor->scale_m);
1408 if (rval < 0)
1409 goto out;
1410 }
1411
1412 /* Output size from sensor */
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001413 rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001414 sensor->src->crop[SMIAPP_PAD_SRC].width);
1415 if (rval < 0)
1416 goto out;
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001417 rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001418 sensor->src->crop[SMIAPP_PAD_SRC].height);
1419 if (rval < 0)
1420 goto out;
1421
1422 if ((sensor->flash_capability &
1423 (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1424 SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1425 sensor->platform_data->strobe_setup != NULL &&
1426 sensor->platform_data->strobe_setup->trigger != 0) {
1427 rval = smiapp_setup_flash_strobe(sensor);
1428 if (rval)
1429 goto out;
1430 }
1431
1432 rval = smiapp_call_quirk(sensor, pre_streamon);
1433 if (rval) {
1434 dev_err(&client->dev, "pre_streamon quirks failed\n");
1435 goto out;
1436 }
1437
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001438 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001439 SMIAPP_MODE_SELECT_STREAMING);
1440
1441out:
1442 mutex_unlock(&sensor->mutex);
1443
1444 return rval;
1445}
1446
1447static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1448{
1449 struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1450 int rval;
1451
1452 mutex_lock(&sensor->mutex);
Sakari Ailus1e73eea2012-04-22 08:55:10 -03001453 rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001454 SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1455 if (rval)
1456 goto out;
1457
1458 rval = smiapp_call_quirk(sensor, post_streamoff);
1459 if (rval)
1460 dev_err(&client->dev, "post_streamoff quirks failed\n");
1461
1462out:
1463 mutex_unlock(&sensor->mutex);
1464 return rval;
1465}
1466
1467/* -----------------------------------------------------------------------------
1468 * V4L2 subdev video operations
1469 */
1470
1471static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1472{
1473 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1474 int rval;
1475
1476 if (sensor->streaming == enable)
1477 return 0;
1478
1479 if (enable) {
1480 sensor->streaming = 1;
1481 rval = smiapp_start_streaming(sensor);
1482 if (rval < 0)
1483 sensor->streaming = 0;
1484 } else {
1485 rval = smiapp_stop_streaming(sensor);
1486 sensor->streaming = 0;
1487 }
1488
1489 return rval;
1490}
1491
1492static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1493 struct v4l2_subdev_fh *fh,
1494 struct v4l2_subdev_mbus_code_enum *code)
1495{
1496 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1497 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1498 unsigned int i;
1499 int idx = -1;
1500 int rval = -EINVAL;
1501
1502 mutex_lock(&sensor->mutex);
1503
1504 dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1505 subdev->name, code->pad, code->index);
1506
1507 if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1508 if (code->index)
1509 goto out;
1510
1511 code->code = sensor->internal_csi_format->code;
1512 rval = 0;
1513 goto out;
1514 }
1515
1516 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1517 if (sensor->mbus_frame_fmts & (1 << i))
1518 idx++;
1519
1520 if (idx == code->index) {
1521 code->code = smiapp_csi_data_formats[i].code;
1522 dev_err(&client->dev, "found index %d, i %d, code %x\n",
1523 code->index, i, code->code);
1524 rval = 0;
1525 break;
1526 }
1527 }
1528
1529out:
1530 mutex_unlock(&sensor->mutex);
1531
1532 return rval;
1533}
1534
1535static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1536 unsigned int pad)
1537{
1538 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1539
1540 if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1541 return sensor->csi_format->code;
1542 else
1543 return sensor->internal_csi_format->code;
1544}
1545
1546static int __smiapp_get_format(struct v4l2_subdev *subdev,
1547 struct v4l2_subdev_fh *fh,
1548 struct v4l2_subdev_format *fmt)
1549{
1550 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1551
1552 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1553 fmt->format = *v4l2_subdev_get_try_format(fh, fmt->pad);
1554 } else {
1555 struct v4l2_rect *r;
1556
1557 if (fmt->pad == ssd->source_pad)
1558 r = &ssd->crop[ssd->source_pad];
1559 else
1560 r = &ssd->sink_fmt;
1561
1562 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1563 fmt->format.width = r->width;
1564 fmt->format.height = r->height;
1565 }
1566
1567 return 0;
1568}
1569
1570static int smiapp_get_format(struct v4l2_subdev *subdev,
1571 struct v4l2_subdev_fh *fh,
1572 struct v4l2_subdev_format *fmt)
1573{
1574 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1575 int rval;
1576
1577 mutex_lock(&sensor->mutex);
1578 rval = __smiapp_get_format(subdev, fh, fmt);
1579 mutex_unlock(&sensor->mutex);
1580
1581 return rval;
1582}
1583
1584static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1585 struct v4l2_subdev_fh *fh,
1586 struct v4l2_rect **crops,
1587 struct v4l2_rect **comps, int which)
1588{
1589 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1590 unsigned int i;
1591
1592 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1593 if (crops)
1594 for (i = 0; i < subdev->entity.num_pads; i++)
1595 crops[i] = &ssd->crop[i];
1596 if (comps)
1597 *comps = &ssd->compose;
1598 } else {
1599 if (crops) {
1600 for (i = 0; i < subdev->entity.num_pads; i++) {
1601 crops[i] = v4l2_subdev_get_try_crop(fh, i);
1602 BUG_ON(!crops[i]);
1603 }
1604 }
1605 if (comps) {
1606 *comps = v4l2_subdev_get_try_compose(fh,
1607 SMIAPP_PAD_SINK);
1608 BUG_ON(!*comps);
1609 }
1610 }
1611}
1612
1613/* Changes require propagation only on sink pad. */
1614static void smiapp_propagate(struct v4l2_subdev *subdev,
1615 struct v4l2_subdev_fh *fh, int which,
1616 int target)
1617{
1618 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1619 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1620 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1621
1622 smiapp_get_crop_compose(subdev, fh, crops, &comp, which);
1623
1624 switch (target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001625 case V4L2_SEL_TGT_CROP:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001626 comp->width = crops[SMIAPP_PAD_SINK]->width;
1627 comp->height = crops[SMIAPP_PAD_SINK]->height;
1628 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1629 if (ssd == sensor->scaler) {
1630 sensor->scale_m =
1631 sensor->limits[
1632 SMIAPP_LIMIT_SCALER_N_MIN];
1633 sensor->scaling_mode =
1634 SMIAPP_SCALING_MODE_NONE;
1635 } else if (ssd == sensor->binner) {
1636 sensor->binning_horizontal = 1;
1637 sensor->binning_vertical = 1;
1638 }
1639 }
1640 /* Fall through */
Sakari Ailus5689b282012-05-18 09:31:18 -03001641 case V4L2_SEL_TGT_COMPOSE:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001642 *crops[SMIAPP_PAD_SRC] = *comp;
1643 break;
1644 default:
1645 BUG();
1646 }
1647}
1648
1649static const struct smiapp_csi_data_format
1650*smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1651{
1652 const struct smiapp_csi_data_format *csi_format = sensor->csi_format;
1653 unsigned int i;
1654
1655 for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1656 if (sensor->mbus_frame_fmts & (1 << i)
1657 && smiapp_csi_data_formats[i].code == code)
1658 return &smiapp_csi_data_formats[i];
1659 }
1660
1661 return csi_format;
1662}
1663
1664static int smiapp_set_format(struct v4l2_subdev *subdev,
1665 struct v4l2_subdev_fh *fh,
1666 struct v4l2_subdev_format *fmt)
1667{
1668 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1669 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1670 struct v4l2_rect *crops[SMIAPP_PADS];
1671
1672 mutex_lock(&sensor->mutex);
1673
1674 /*
1675 * Media bus code is changeable on src subdev's source pad. On
1676 * other source pads we just get format here.
1677 */
1678 if (fmt->pad == ssd->source_pad) {
1679 u32 code = fmt->format.code;
1680 int rval = __smiapp_get_format(subdev, fh, fmt);
1681
1682 if (!rval && subdev == &sensor->src->sd) {
1683 const struct smiapp_csi_data_format *csi_format =
1684 smiapp_validate_csi_data_format(sensor, code);
1685 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1686 sensor->csi_format = csi_format;
1687 fmt->format.code = csi_format->code;
1688 }
1689
1690 mutex_unlock(&sensor->mutex);
1691 return rval;
1692 }
1693
1694 /* Sink pad. Width and height are changeable here. */
1695 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1696 fmt->format.width &= ~1;
1697 fmt->format.height &= ~1;
1698
1699 fmt->format.width =
1700 clamp(fmt->format.width,
1701 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
1702 sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
1703 fmt->format.height =
1704 clamp(fmt->format.height,
1705 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
1706 sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
1707
1708 smiapp_get_crop_compose(subdev, fh, crops, NULL, fmt->which);
1709
1710 crops[ssd->sink_pad]->left = 0;
1711 crops[ssd->sink_pad]->top = 0;
1712 crops[ssd->sink_pad]->width = fmt->format.width;
1713 crops[ssd->sink_pad]->height = fmt->format.height;
1714 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1715 ssd->sink_fmt = *crops[ssd->sink_pad];
1716 smiapp_propagate(subdev, fh, fmt->which,
Sakari Ailus5689b282012-05-18 09:31:18 -03001717 V4L2_SEL_TGT_CROP);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001718
1719 mutex_unlock(&sensor->mutex);
1720
1721 return 0;
1722}
1723
1724/*
1725 * Calculate goodness of scaled image size compared to expected image
1726 * size and flags provided.
1727 */
1728#define SCALING_GOODNESS 100000
1729#define SCALING_GOODNESS_EXTREME 100000000
1730static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1731 int h, int ask_h, u32 flags)
1732{
1733 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1734 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1735 int val = 0;
1736
1737 w &= ~1;
1738 ask_w &= ~1;
1739 h &= ~1;
1740 ask_h &= ~1;
1741
Sakari Ailus563df3d2012-06-13 16:01:10 -03001742 if (flags & V4L2_SEL_FLAG_GE) {
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001743 if (w < ask_w)
1744 val -= SCALING_GOODNESS;
1745 if (h < ask_h)
1746 val -= SCALING_GOODNESS;
1747 }
1748
Sakari Ailus563df3d2012-06-13 16:01:10 -03001749 if (flags & V4L2_SEL_FLAG_LE) {
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001750 if (w > ask_w)
1751 val -= SCALING_GOODNESS;
1752 if (h > ask_h)
1753 val -= SCALING_GOODNESS;
1754 }
1755
1756 val -= abs(w - ask_w);
1757 val -= abs(h - ask_h);
1758
1759 if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
1760 val -= SCALING_GOODNESS_EXTREME;
1761
1762 dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1763 w, ask_h, h, ask_h, val);
1764
1765 return val;
1766}
1767
1768static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1769 struct v4l2_subdev_fh *fh,
1770 struct v4l2_subdev_selection *sel,
1771 struct v4l2_rect **crops,
1772 struct v4l2_rect *comp)
1773{
1774 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1775 unsigned int i;
1776 unsigned int binh = 1, binv = 1;
1777 unsigned int best = scaling_goodness(
1778 subdev,
1779 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1780 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1781
1782 for (i = 0; i < sensor->nbinning_subtypes; i++) {
1783 int this = scaling_goodness(
1784 subdev,
1785 crops[SMIAPP_PAD_SINK]->width
1786 / sensor->binning_subtypes[i].horizontal,
1787 sel->r.width,
1788 crops[SMIAPP_PAD_SINK]->height
1789 / sensor->binning_subtypes[i].vertical,
1790 sel->r.height, sel->flags);
1791
1792 if (this > best) {
1793 binh = sensor->binning_subtypes[i].horizontal;
1794 binv = sensor->binning_subtypes[i].vertical;
1795 best = this;
1796 }
1797 }
1798 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1799 sensor->binning_vertical = binv;
1800 sensor->binning_horizontal = binh;
1801 }
1802
1803 sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1804 sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1805}
1806
1807/*
1808 * Calculate best scaling ratio and mode for given output resolution.
1809 *
1810 * Try all of these: horizontal ratio, vertical ratio and smallest
1811 * size possible (horizontally).
1812 *
1813 * Also try whether horizontal scaler or full scaler gives a better
1814 * result.
1815 */
1816static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1817 struct v4l2_subdev_fh *fh,
1818 struct v4l2_subdev_selection *sel,
1819 struct v4l2_rect **crops,
1820 struct v4l2_rect *comp)
1821{
1822 struct i2c_client *client = v4l2_get_subdevdata(subdev);
1823 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1824 u32 min, max, a, b, max_m;
1825 u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
1826 int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1827 u32 try[4];
1828 u32 ntry = 0;
1829 unsigned int i;
1830 int best = INT_MIN;
1831
1832 sel->r.width = min_t(unsigned int, sel->r.width,
1833 crops[SMIAPP_PAD_SINK]->width);
1834 sel->r.height = min_t(unsigned int, sel->r.height,
1835 crops[SMIAPP_PAD_SINK]->height);
1836
1837 a = crops[SMIAPP_PAD_SINK]->width
1838 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
1839 b = crops[SMIAPP_PAD_SINK]->height
1840 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
1841 max_m = crops[SMIAPP_PAD_SINK]->width
1842 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
1843 / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
1844
1845 a = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1846 max(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1847 b = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1848 max(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1849 max_m = min(sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX],
1850 max(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN]));
1851
1852 dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1853
1854 min = min(max_m, min(a, b));
1855 max = min(max_m, max(a, b));
1856
1857 try[ntry] = min;
1858 ntry++;
1859 if (min != max) {
1860 try[ntry] = max;
1861 ntry++;
1862 }
1863 if (max != max_m) {
1864 try[ntry] = min + 1;
1865 ntry++;
1866 if (min != max) {
1867 try[ntry] = max + 1;
1868 ntry++;
1869 }
1870 }
1871
1872 for (i = 0; i < ntry; i++) {
1873 int this = scaling_goodness(
1874 subdev,
1875 crops[SMIAPP_PAD_SINK]->width
1876 / try[i]
1877 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1878 sel->r.width,
1879 crops[SMIAPP_PAD_SINK]->height,
1880 sel->r.height,
1881 sel->flags);
1882
1883 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
1884
1885 if (this > best) {
1886 scale_m = try[i];
1887 mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1888 best = this;
1889 }
1890
1891 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1892 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
1893 continue;
1894
1895 this = scaling_goodness(
1896 subdev, crops[SMIAPP_PAD_SINK]->width
1897 / try[i]
1898 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1899 sel->r.width,
1900 crops[SMIAPP_PAD_SINK]->height
1901 / try[i]
1902 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
1903 sel->r.height,
1904 sel->flags);
1905
1906 if (this > best) {
1907 scale_m = try[i];
1908 mode = SMIAPP_SCALING_MODE_BOTH;
1909 best = this;
1910 }
1911 }
1912
1913 sel->r.width =
1914 (crops[SMIAPP_PAD_SINK]->width
1915 / scale_m
1916 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
1917 if (mode == SMIAPP_SCALING_MODE_BOTH)
1918 sel->r.height =
1919 (crops[SMIAPP_PAD_SINK]->height
1920 / scale_m
1921 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
1922 & ~1;
1923 else
1924 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
1925
1926 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1927 sensor->scale_m = scale_m;
1928 sensor->scaling_mode = mode;
1929 }
1930}
1931/* We're only called on source pads. This function sets scaling. */
1932static int smiapp_set_compose(struct v4l2_subdev *subdev,
1933 struct v4l2_subdev_fh *fh,
1934 struct v4l2_subdev_selection *sel)
1935{
1936 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1937 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1938 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1939
1940 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
1941
1942 sel->r.top = 0;
1943 sel->r.left = 0;
1944
1945 if (ssd == sensor->binner)
1946 smiapp_set_compose_binner(subdev, fh, sel, crops, comp);
1947 else
1948 smiapp_set_compose_scaler(subdev, fh, sel, crops, comp);
1949
1950 *comp = sel->r;
1951 smiapp_propagate(subdev, fh, sel->which,
Sakari Ailus5689b282012-05-18 09:31:18 -03001952 V4L2_SEL_TGT_COMPOSE);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001953
1954 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1955 return smiapp_update_mode(sensor);
1956
1957 return 0;
1958}
1959
1960static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
1961 struct v4l2_subdev_selection *sel)
1962{
1963 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1964 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1965
1966 /* We only implement crop in three places. */
1967 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001968 case V4L2_SEL_TGT_CROP:
1969 case V4L2_SEL_TGT_CROP_BOUNDS:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001970 if (ssd == sensor->pixel_array
1971 && sel->pad == SMIAPP_PA_PAD_SRC)
1972 return 0;
1973 if (ssd == sensor->src
1974 && sel->pad == SMIAPP_PAD_SRC)
1975 return 0;
1976 if (ssd == sensor->scaler
1977 && sel->pad == SMIAPP_PAD_SINK
1978 && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1979 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
1980 return 0;
1981 return -EINVAL;
Sakari Ailus5689b282012-05-18 09:31:18 -03001982 case V4L2_SEL_TGT_COMPOSE:
1983 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03001984 if (sel->pad == ssd->source_pad)
1985 return -EINVAL;
1986 if (ssd == sensor->binner)
1987 return 0;
1988 if (ssd == sensor->scaler
1989 && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1990 != SMIAPP_SCALING_CAPABILITY_NONE)
1991 return 0;
1992 /* Fall through */
1993 default:
1994 return -EINVAL;
1995 }
1996}
1997
1998static int smiapp_set_crop(struct v4l2_subdev *subdev,
1999 struct v4l2_subdev_fh *fh,
2000 struct v4l2_subdev_selection *sel)
2001{
2002 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2003 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2004 struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
2005 struct v4l2_rect _r;
2006
2007 smiapp_get_crop_compose(subdev, fh, crops, NULL, sel->which);
2008
2009 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2010 if (sel->pad == ssd->sink_pad)
2011 src_size = &ssd->sink_fmt;
2012 else
2013 src_size = &ssd->compose;
2014 } else {
2015 if (sel->pad == ssd->sink_pad) {
2016 _r.left = 0;
2017 _r.top = 0;
2018 _r.width = v4l2_subdev_get_try_format(fh, sel->pad)
2019 ->width;
2020 _r.height = v4l2_subdev_get_try_format(fh, sel->pad)
2021 ->height;
2022 src_size = &_r;
2023 } else {
2024 src_size =
2025 v4l2_subdev_get_try_compose(
2026 fh, ssd->sink_pad);
2027 }
2028 }
2029
2030 if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2031 sel->r.left = 0;
2032 sel->r.top = 0;
2033 }
2034
2035 sel->r.width = min(sel->r.width, src_size->width);
2036 sel->r.height = min(sel->r.height, src_size->height);
2037
2038 sel->r.left = min(sel->r.left, src_size->width - sel->r.width);
2039 sel->r.top = min(sel->r.top, src_size->height - sel->r.height);
2040
2041 *crops[sel->pad] = sel->r;
2042
2043 if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2044 smiapp_propagate(subdev, fh, sel->which,
Sakari Ailus5689b282012-05-18 09:31:18 -03002045 V4L2_SEL_TGT_CROP);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002046
2047 return 0;
2048}
2049
2050static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2051 struct v4l2_subdev_fh *fh,
2052 struct v4l2_subdev_selection *sel)
2053{
2054 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2055 struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2056 struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2057 struct v4l2_rect sink_fmt;
2058 int ret;
2059
2060 ret = __smiapp_sel_supported(subdev, sel);
2061 if (ret)
2062 return ret;
2063
2064 smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
2065
2066 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2067 sink_fmt = ssd->sink_fmt;
2068 } else {
2069 struct v4l2_mbus_framefmt *fmt =
2070 v4l2_subdev_get_try_format(fh, ssd->sink_pad);
2071
2072 sink_fmt.left = 0;
2073 sink_fmt.top = 0;
2074 sink_fmt.width = fmt->width;
2075 sink_fmt.height = fmt->height;
2076 }
2077
2078 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03002079 case V4L2_SEL_TGT_CROP_BOUNDS:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002080 if (ssd == sensor->pixel_array) {
2081 sel->r.width =
2082 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2083 sel->r.height =
2084 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2085 } else if (sel->pad == ssd->sink_pad) {
2086 sel->r = sink_fmt;
2087 } else {
2088 sel->r = *comp;
2089 }
2090 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03002091 case V4L2_SEL_TGT_CROP:
2092 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002093 sel->r = *crops[sel->pad];
2094 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03002095 case V4L2_SEL_TGT_COMPOSE:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002096 sel->r = *comp;
2097 break;
2098 }
2099
2100 return 0;
2101}
2102
2103static int smiapp_get_selection(struct v4l2_subdev *subdev,
2104 struct v4l2_subdev_fh *fh,
2105 struct v4l2_subdev_selection *sel)
2106{
2107 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2108 int rval;
2109
2110 mutex_lock(&sensor->mutex);
2111 rval = __smiapp_get_selection(subdev, fh, sel);
2112 mutex_unlock(&sensor->mutex);
2113
2114 return rval;
2115}
2116static int smiapp_set_selection(struct v4l2_subdev *subdev,
2117 struct v4l2_subdev_fh *fh,
2118 struct v4l2_subdev_selection *sel)
2119{
2120 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2121 int ret;
2122
2123 ret = __smiapp_sel_supported(subdev, sel);
2124 if (ret)
2125 return ret;
2126
2127 mutex_lock(&sensor->mutex);
2128
2129 sel->r.left = max(0, sel->r.left & ~1);
2130 sel->r.top = max(0, sel->r.top & ~1);
2131 sel->r.width = max(0, SMIAPP_ALIGN_DIM(sel->r.width, sel->flags));
2132 sel->r.height = max(0, SMIAPP_ALIGN_DIM(sel->r.height, sel->flags));
2133
2134 sel->r.width = max_t(unsigned int,
2135 sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
2136 sel->r.width);
2137 sel->r.height = max_t(unsigned int,
2138 sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
2139 sel->r.height);
2140
2141 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03002142 case V4L2_SEL_TGT_CROP:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002143 ret = smiapp_set_crop(subdev, fh, sel);
2144 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03002145 case V4L2_SEL_TGT_COMPOSE:
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002146 ret = smiapp_set_compose(subdev, fh, sel);
2147 break;
2148 default:
2149 BUG();
2150 }
2151
2152 mutex_unlock(&sensor->mutex);
2153 return ret;
2154}
2155
2156static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2157{
2158 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2159
2160 *frames = sensor->frame_skip;
2161 return 0;
2162}
2163
2164/* -----------------------------------------------------------------------------
2165 * sysfs attributes
2166 */
2167
2168static ssize_t
2169smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2170 char *buf)
2171{
2172 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2173 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2174 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2175 unsigned int nbytes;
2176
2177 if (!sensor->dev_init_done)
2178 return -EBUSY;
2179
2180 if (!sensor->nvm_size) {
2181 /* NVM not read yet - read it now */
2182 sensor->nvm_size = sensor->platform_data->nvm_size;
2183 if (smiapp_set_power(subdev, 1) < 0)
2184 return -ENODEV;
2185 if (smiapp_read_nvm(sensor, sensor->nvm)) {
2186 dev_err(&client->dev, "nvm read failed\n");
2187 return -ENODEV;
2188 }
2189 smiapp_set_power(subdev, 0);
2190 }
2191 /*
2192 * NVM is still way below a PAGE_SIZE, so we can safely
2193 * assume this for now.
2194 */
2195 nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
2196 memcpy(buf, sensor->nvm, nbytes);
2197
2198 return nbytes;
2199}
2200static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2201
Sakari Ailuseba66b32012-09-15 18:26:54 -03002202static ssize_t
2203smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2204 char *buf)
2205{
2206 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2207 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2208 struct smiapp_module_info *minfo = &sensor->minfo;
2209
2210 return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2211 minfo->manufacturer_id, minfo->model_id,
2212 minfo->revision_number_major) + 1;
2213}
2214
2215static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
2216
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002217/* -----------------------------------------------------------------------------
2218 * V4L2 subdev core operations
2219 */
2220
2221static int smiapp_identify_module(struct v4l2_subdev *subdev)
2222{
2223 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2224 struct i2c_client *client = v4l2_get_subdevdata(subdev);
2225 struct smiapp_module_info *minfo = &sensor->minfo;
2226 unsigned int i;
2227 int rval = 0;
2228
2229 minfo->name = SMIAPP_NAME;
2230
2231 /* Module info */
Sakari Ailus98add8e2012-04-22 09:30:19 -03002232 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2233 &minfo->manufacturer_id);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002234 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002235 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2236 &minfo->model_id);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002237 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002238 rval = smiapp_read_8only(sensor,
2239 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2240 &minfo->revision_number_major);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002241 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002242 rval = smiapp_read_8only(sensor,
2243 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2244 &minfo->revision_number_minor);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002245 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002246 rval = smiapp_read_8only(sensor,
2247 SMIAPP_REG_U8_MODULE_DATE_YEAR,
2248 &minfo->module_year);
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_MODULE_DATE_MONTH,
2252 &minfo->module_month);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002253 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002254 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2255 &minfo->module_day);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002256
2257 /* Sensor info */
2258 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002259 rval = smiapp_read_8only(sensor,
2260 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2261 &minfo->sensor_manufacturer_id);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002262 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002263 rval = smiapp_read_8only(sensor,
2264 SMIAPP_REG_U16_SENSOR_MODEL_ID,
2265 &minfo->sensor_model_id);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002266 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002267 rval = smiapp_read_8only(sensor,
2268 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2269 &minfo->sensor_revision_number);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002270 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002271 rval = smiapp_read_8only(sensor,
2272 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2273 &minfo->sensor_firmware_version);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002274
2275 /* SMIA */
2276 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002277 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2278 &minfo->smia_version);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002279 if (!rval)
Sakari Ailus98add8e2012-04-22 09:30:19 -03002280 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2281 &minfo->smiapp_version);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002282
2283 if (rval) {
2284 dev_err(&client->dev, "sensor detection failed\n");
2285 return -ENODEV;
2286 }
2287
2288 dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2289 minfo->manufacturer_id, minfo->model_id);
2290
2291 dev_dbg(&client->dev,
2292 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2293 minfo->revision_number_major, minfo->revision_number_minor,
2294 minfo->module_year, minfo->module_month, minfo->module_day);
2295
2296 dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2297 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2298
2299 dev_dbg(&client->dev,
2300 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2301 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2302
2303 dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2304 minfo->smia_version, minfo->smiapp_version);
2305
2306 /*
2307 * Some modules have bad data in the lvalues below. Hope the
2308 * rvalues have better stuff. The lvalues are module
2309 * parameters whereas the rvalues are sensor parameters.
2310 */
2311 if (!minfo->manufacturer_id && !minfo->model_id) {
2312 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2313 minfo->model_id = minfo->sensor_model_id;
2314 minfo->revision_number_major = minfo->sensor_revision_number;
2315 }
2316
2317 for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2318 if (smiapp_module_idents[i].manufacturer_id
2319 != minfo->manufacturer_id)
2320 continue;
2321 if (smiapp_module_idents[i].model_id != minfo->model_id)
2322 continue;
2323 if (smiapp_module_idents[i].flags
2324 & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2325 if (smiapp_module_idents[i].revision_number_major
2326 < minfo->revision_number_major)
2327 continue;
2328 } else {
2329 if (smiapp_module_idents[i].revision_number_major
2330 != minfo->revision_number_major)
2331 continue;
2332 }
2333
2334 minfo->name = smiapp_module_idents[i].name;
2335 minfo->quirk = smiapp_module_idents[i].quirk;
2336 break;
2337 }
2338
2339 if (i >= ARRAY_SIZE(smiapp_module_idents))
2340 dev_warn(&client->dev,
2341 "no quirks for this module; let's hope it's fully compliant\n");
2342
2343 dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2344 minfo->name, minfo->manufacturer_id, minfo->model_id,
2345 minfo->revision_number_major);
2346
2347 strlcpy(subdev->name, sensor->minfo.name, sizeof(subdev->name));
2348
2349 return 0;
2350}
2351
2352static const struct v4l2_subdev_ops smiapp_ops;
2353static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2354static const struct media_entity_operations smiapp_entity_ops;
2355
2356static int smiapp_registered(struct v4l2_subdev *subdev)
2357{
2358 struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2359 struct i2c_client *client = v4l2_get_subdevdata(subdev);
Sakari Ailus1e9240b2012-10-20 10:17:02 -03002360 struct smiapp_pll *pll = &sensor->pll;
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002361 struct smiapp_subdev *last = NULL;
2362 u32 tmp;
2363 unsigned int i;
2364 int rval;
2365
Sachin Kamat31c1d172012-08-17 01:49:02 -03002366 sensor->vana = devm_regulator_get(&client->dev, "VANA");
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002367 if (IS_ERR(sensor->vana)) {
2368 dev_err(&client->dev, "could not get regulator for vana\n");
2369 return -ENODEV;
2370 }
2371
Sakari Ailus25474282012-04-22 08:24:33 -03002372 if (!sensor->platform_data->set_xclk) {
Sachin Kamat31c1d172012-08-17 01:49:02 -03002373 sensor->ext_clk = devm_clk_get(&client->dev,
2374 sensor->platform_data->ext_clk_name);
Sakari Ailus25474282012-04-22 08:24:33 -03002375 if (IS_ERR(sensor->ext_clk)) {
2376 dev_err(&client->dev, "could not get clock %s\n",
2377 sensor->platform_data->ext_clk_name);
Sachin Kamat31c1d172012-08-17 01:49:02 -03002378 return -ENODEV;
Sakari Ailus25474282012-04-22 08:24:33 -03002379 }
2380
2381 rval = clk_set_rate(sensor->ext_clk,
2382 sensor->platform_data->ext_clk);
2383 if (rval < 0) {
2384 dev_err(&client->dev,
2385 "unable to set clock %s freq to %u\n",
2386 sensor->platform_data->ext_clk_name,
2387 sensor->platform_data->ext_clk);
Sachin Kamat31c1d172012-08-17 01:49:02 -03002388 return -ENODEV;
Sakari Ailus25474282012-04-22 08:24:33 -03002389 }
2390 }
2391
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002392 if (sensor->platform_data->xshutdown != SMIAPP_NO_XSHUTDOWN) {
2393 if (gpio_request_one(sensor->platform_data->xshutdown, 0,
2394 "SMIA++ xshutdown") != 0) {
2395 dev_err(&client->dev,
2396 "unable to acquire reset gpio %d\n",
2397 sensor->platform_data->xshutdown);
Sachin Kamat31c1d172012-08-17 01:49:02 -03002398 return -ENODEV;
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002399 }
2400 }
2401
2402 rval = smiapp_power_on(sensor);
2403 if (rval) {
2404 rval = -ENODEV;
2405 goto out_smiapp_power_on;
2406 }
2407
2408 rval = smiapp_identify_module(subdev);
2409 if (rval) {
2410 rval = -ENODEV;
2411 goto out_power_off;
2412 }
2413
2414 rval = smiapp_get_all_limits(sensor);
2415 if (rval) {
2416 rval = -ENODEV;
2417 goto out_power_off;
2418 }
2419
2420 /*
2421 * Handle Sensor Module orientation on the board.
2422 *
2423 * The application of H-FLIP and V-FLIP on the sensor is modified by
2424 * the sensor orientation on the board.
2425 *
2426 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2427 * both H-FLIP and V-FLIP for normal operation which also implies
2428 * that a set/unset operation for user space HFLIP and VFLIP v4l2
2429 * controls will need to be internally inverted.
2430 *
2431 * Rotation also changes the bayer pattern.
2432 */
2433 if (sensor->platform_data->module_board_orient ==
2434 SMIAPP_MODULE_BOARD_ORIENT_180)
2435 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2436 SMIAPP_IMAGE_ORIENTATION_VFLIP;
2437
2438 rval = smiapp_get_mbus_formats(sensor);
2439 if (rval) {
2440 rval = -ENODEV;
2441 goto out_power_off;
2442 }
2443
2444 if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2445 u32 val;
2446
Sakari Ailus1e73eea2012-04-22 08:55:10 -03002447 rval = smiapp_read(sensor,
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002448 SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2449 if (rval < 0) {
2450 rval = -ENODEV;
2451 goto out_power_off;
2452 }
2453 sensor->nbinning_subtypes = min_t(u8, val,
2454 SMIAPP_BINNING_SUBTYPES);
2455
2456 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2457 rval = smiapp_read(
Sakari Ailus1e73eea2012-04-22 08:55:10 -03002458 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002459 if (rval < 0) {
2460 rval = -ENODEV;
2461 goto out_power_off;
2462 }
2463 sensor->binning_subtypes[i] =
2464 *(struct smiapp_binning_subtype *)&val;
2465
2466 dev_dbg(&client->dev, "binning %xx%x\n",
2467 sensor->binning_subtypes[i].horizontal,
2468 sensor->binning_subtypes[i].vertical);
2469 }
2470 }
2471 sensor->binning_horizontal = 1;
2472 sensor->binning_vertical = 1;
2473
Sakari Ailuseba66b32012-09-15 18:26:54 -03002474 if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
2475 dev_err(&client->dev, "sysfs ident entry creation failed\n");
2476 rval = -ENOENT;
2477 goto out_power_off;
2478 }
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002479 /* SMIA++ NVM initialization - it will be read from the sensor
2480 * when it is first requested by userspace.
2481 */
2482 if (sensor->minfo.smiapp_version && sensor->platform_data->nvm_size) {
Sachin Kamat31c1d172012-08-17 01:49:02 -03002483 sensor->nvm = devm_kzalloc(&client->dev,
2484 sensor->platform_data->nvm_size, GFP_KERNEL);
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002485 if (sensor->nvm == NULL) {
2486 dev_err(&client->dev, "nvm buf allocation failed\n");
2487 rval = -ENOMEM;
Sakari Ailuseba66b32012-09-15 18:26:54 -03002488 goto out_ident_release;
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002489 }
2490
2491 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
2492 dev_err(&client->dev, "sysfs nvm entry failed\n");
2493 rval = -EBUSY;
Sakari Ailuseba66b32012-09-15 18:26:54 -03002494 goto out_ident_release;
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002495 }
2496 }
2497
2498 rval = smiapp_call_quirk(sensor, limits);
2499 if (rval) {
2500 dev_err(&client->dev, "limits quirks failed\n");
2501 goto out_nvm_release;
2502 }
2503
2504 /* We consider this as profile 0 sensor if any of these are zero. */
2505 if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
2506 !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
2507 !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
2508 !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
2509 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
2510 } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2511 != SMIAPP_SCALING_CAPABILITY_NONE) {
2512 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2513 == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2514 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
2515 else
2516 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
2517 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2518 sensor->ssds_used++;
2519 } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2520 == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
2521 sensor->scaler = &sensor->ssds[sensor->ssds_used];
2522 sensor->ssds_used++;
2523 }
2524 sensor->binner = &sensor->ssds[sensor->ssds_used];
2525 sensor->ssds_used++;
2526 sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
2527 sensor->ssds_used++;
2528
2529 sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
2530
2531 for (i = 0; i < SMIAPP_SUBDEVS; i++) {
2532 struct {
2533 struct smiapp_subdev *ssd;
2534 char *name;
2535 } const __this[] = {
2536 { sensor->scaler, "scaler", },
2537 { sensor->binner, "binner", },
2538 { sensor->pixel_array, "pixel array", },
2539 }, *_this = &__this[i];
2540 struct smiapp_subdev *this = _this->ssd;
2541
2542 if (!this)
2543 continue;
2544
2545 if (this != sensor->src)
2546 v4l2_subdev_init(&this->sd, &smiapp_ops);
2547
2548 this->sensor = sensor;
2549
2550 if (this == sensor->pixel_array) {
2551 this->npads = 1;
2552 } else {
2553 this->npads = 2;
2554 this->source_pad = 1;
2555 }
2556
2557 snprintf(this->sd.name,
2558 sizeof(this->sd.name), "%s %s",
2559 sensor->minfo.name, _this->name);
2560
2561 this->sink_fmt.width =
2562 sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2563 this->sink_fmt.height =
2564 sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2565 this->compose.width = this->sink_fmt.width;
2566 this->compose.height = this->sink_fmt.height;
2567 this->crop[this->source_pad] = this->compose;
2568 this->pads[this->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2569 if (this != sensor->pixel_array) {
2570 this->crop[this->sink_pad] = this->compose;
2571 this->pads[this->sink_pad].flags = MEDIA_PAD_FL_SINK;
2572 }
2573
2574 this->sd.entity.ops = &smiapp_entity_ops;
2575
2576 if (last == NULL) {
2577 last = this;
2578 continue;
2579 }
2580
2581 this->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2582 this->sd.internal_ops = &smiapp_internal_ops;
2583 this->sd.owner = NULL;
2584 v4l2_set_subdevdata(&this->sd, client);
2585
2586 rval = media_entity_init(&this->sd.entity,
2587 this->npads, this->pads, 0);
2588 if (rval) {
2589 dev_err(&client->dev,
2590 "media_entity_init failed\n");
2591 goto out_nvm_release;
2592 }
2593
2594 rval = media_entity_create_link(&this->sd.entity,
2595 this->source_pad,
2596 &last->sd.entity,
2597 last->sink_pad,
2598 MEDIA_LNK_FL_ENABLED |
2599 MEDIA_LNK_FL_IMMUTABLE);
2600 if (rval) {
2601 dev_err(&client->dev,
2602 "media_entity_create_link failed\n");
2603 goto out_nvm_release;
2604 }
2605
2606 rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2607 &this->sd);
2608 if (rval) {
2609 dev_err(&client->dev,
2610 "v4l2_device_register_subdev failed\n");
2611 goto out_nvm_release;
2612 }
2613
2614 last = this;
2615 }
2616
2617 dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
2618
2619 sensor->pixel_array->sd.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
2620
2621 /* final steps */
2622 smiapp_read_frame_fmt(sensor);
2623 rval = smiapp_init_controls(sensor);
2624 if (rval < 0)
2625 goto out_nvm_release;
2626
Sakari Ailus1e9240b2012-10-20 10:17:02 -03002627 /* prepare PLL configuration input values */
2628 pll->lanes = sensor->platform_data->lanes;
2629 pll->ext_clk_freq_hz = sensor->platform_data->ext_clk;
2630 /* Profile 0 sensors have no separate OP clock branch. */
2631 if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
2632 pll->flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
2633 if (smiapp_needs_quirk(sensor,
2634 SMIAPP_QUIRK_FLAG_OP_PIX_CLOCK_PER_LANE))
2635 pll->flags |= SMIAPP_PLL_FLAG_OP_PIX_CLOCK_PER_LANE;
2636 pll->scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
2637
Sakari Ailusccfc97b2012-03-03 17:19:52 -03002638 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");