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