blob: 2203a6f52e48808d8565afea207829d8c9299b09 [file] [log] [blame]
Detlev Casanova0f2ce162011-04-05 09:06:21 -03001/*
2 * Driver for MT9V032 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2010, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
5 *
6 * Based on the MT9M001 driver,
7 *
8 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/delay.h>
16#include <linux/i2c.h>
17#include <linux/log2.h>
18#include <linux/mutex.h>
19#include <linux/slab.h>
20#include <linux/videodev2.h>
21#include <linux/v4l2-mediabus.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040022#include <linux/module.h>
Detlev Casanova0f2ce162011-04-05 09:06:21 -030023
24#include <media/mt9v032.h>
25#include <media/v4l2-ctrls.h>
26#include <media/v4l2-device.h>
27#include <media/v4l2-subdev.h>
28
29#define MT9V032_PIXEL_ARRAY_HEIGHT 492
30#define MT9V032_PIXEL_ARRAY_WIDTH 782
31
32#define MT9V032_CHIP_VERSION 0x00
33#define MT9V032_CHIP_ID_REV1 0x1311
34#define MT9V032_CHIP_ID_REV3 0x1313
Laurent Pinchart86cf7862010-11-28 15:07:20 -030035#define MT9V032_COLUMN_START 0x01
Detlev Casanova0f2ce162011-04-05 09:06:21 -030036#define MT9V032_COLUMN_START_MIN 1
Laurent Pinchart86cf7862010-11-28 15:07:20 -030037#define MT9V032_COLUMN_START_DEF 1
Detlev Casanova0f2ce162011-04-05 09:06:21 -030038#define MT9V032_COLUMN_START_MAX 752
Laurent Pinchart86cf7862010-11-28 15:07:20 -030039#define MT9V032_ROW_START 0x02
40#define MT9V032_ROW_START_MIN 4
41#define MT9V032_ROW_START_DEF 5
42#define MT9V032_ROW_START_MAX 482
Detlev Casanova0f2ce162011-04-05 09:06:21 -030043#define MT9V032_WINDOW_HEIGHT 0x03
44#define MT9V032_WINDOW_HEIGHT_MIN 1
45#define MT9V032_WINDOW_HEIGHT_DEF 480
46#define MT9V032_WINDOW_HEIGHT_MAX 480
47#define MT9V032_WINDOW_WIDTH 0x04
48#define MT9V032_WINDOW_WIDTH_MIN 1
49#define MT9V032_WINDOW_WIDTH_DEF 752
50#define MT9V032_WINDOW_WIDTH_MAX 752
51#define MT9V032_HORIZONTAL_BLANKING 0x05
52#define MT9V032_HORIZONTAL_BLANKING_MIN 43
53#define MT9V032_HORIZONTAL_BLANKING_MAX 1023
54#define MT9V032_VERTICAL_BLANKING 0x06
55#define MT9V032_VERTICAL_BLANKING_MIN 4
56#define MT9V032_VERTICAL_BLANKING_MAX 3000
57#define MT9V032_CHIP_CONTROL 0x07
58#define MT9V032_CHIP_CONTROL_MASTER_MODE (1 << 3)
59#define MT9V032_CHIP_CONTROL_DOUT_ENABLE (1 << 7)
60#define MT9V032_CHIP_CONTROL_SEQUENTIAL (1 << 8)
61#define MT9V032_SHUTTER_WIDTH1 0x08
62#define MT9V032_SHUTTER_WIDTH2 0x09
63#define MT9V032_SHUTTER_WIDTH_CONTROL 0x0a
64#define MT9V032_TOTAL_SHUTTER_WIDTH 0x0b
65#define MT9V032_TOTAL_SHUTTER_WIDTH_MIN 1
66#define MT9V032_TOTAL_SHUTTER_WIDTH_DEF 480
67#define MT9V032_TOTAL_SHUTTER_WIDTH_MAX 32767
68#define MT9V032_RESET 0x0c
69#define MT9V032_READ_MODE 0x0d
70#define MT9V032_READ_MODE_ROW_BIN_MASK (3 << 0)
71#define MT9V032_READ_MODE_ROW_BIN_SHIFT 0
72#define MT9V032_READ_MODE_COLUMN_BIN_MASK (3 << 2)
73#define MT9V032_READ_MODE_COLUMN_BIN_SHIFT 2
74#define MT9V032_READ_MODE_ROW_FLIP (1 << 4)
75#define MT9V032_READ_MODE_COLUMN_FLIP (1 << 5)
76#define MT9V032_READ_MODE_DARK_COLUMNS (1 << 6)
77#define MT9V032_READ_MODE_DARK_ROWS (1 << 7)
78#define MT9V032_PIXEL_OPERATION_MODE 0x0f
79#define MT9V032_PIXEL_OPERATION_MODE_COLOR (1 << 2)
80#define MT9V032_PIXEL_OPERATION_MODE_HDR (1 << 6)
81#define MT9V032_ANALOG_GAIN 0x35
82#define MT9V032_ANALOG_GAIN_MIN 16
83#define MT9V032_ANALOG_GAIN_DEF 16
84#define MT9V032_ANALOG_GAIN_MAX 64
85#define MT9V032_MAX_ANALOG_GAIN 0x36
86#define MT9V032_MAX_ANALOG_GAIN_MAX 127
87#define MT9V032_FRAME_DARK_AVERAGE 0x42
88#define MT9V032_DARK_AVG_THRESH 0x46
89#define MT9V032_DARK_AVG_LOW_THRESH_MASK (255 << 0)
90#define MT9V032_DARK_AVG_LOW_THRESH_SHIFT 0
91#define MT9V032_DARK_AVG_HIGH_THRESH_MASK (255 << 8)
92#define MT9V032_DARK_AVG_HIGH_THRESH_SHIFT 8
93#define MT9V032_ROW_NOISE_CORR_CONTROL 0x70
94#define MT9V032_ROW_NOISE_CORR_ENABLE (1 << 5)
95#define MT9V032_ROW_NOISE_CORR_USE_BLK_AVG (1 << 7)
96#define MT9V032_PIXEL_CLOCK 0x74
97#define MT9V032_PIXEL_CLOCK_INV_LINE (1 << 0)
98#define MT9V032_PIXEL_CLOCK_INV_FRAME (1 << 1)
99#define MT9V032_PIXEL_CLOCK_XOR_LINE (1 << 2)
100#define MT9V032_PIXEL_CLOCK_CONT_LINE (1 << 3)
101#define MT9V032_PIXEL_CLOCK_INV_PXL_CLK (1 << 4)
102#define MT9V032_TEST_PATTERN 0x7f
103#define MT9V032_TEST_PATTERN_DATA_MASK (1023 << 0)
104#define MT9V032_TEST_PATTERN_DATA_SHIFT 0
105#define MT9V032_TEST_PATTERN_USE_DATA (1 << 10)
106#define MT9V032_TEST_PATTERN_GRAY_MASK (3 << 11)
107#define MT9V032_TEST_PATTERN_GRAY_NONE (0 << 11)
108#define MT9V032_TEST_PATTERN_GRAY_VERTICAL (1 << 11)
109#define MT9V032_TEST_PATTERN_GRAY_HORIZONTAL (2 << 11)
110#define MT9V032_TEST_PATTERN_GRAY_DIAGONAL (3 << 11)
111#define MT9V032_TEST_PATTERN_ENABLE (1 << 13)
112#define MT9V032_TEST_PATTERN_FLIP (1 << 14)
113#define MT9V032_AEC_AGC_ENABLE 0xaf
114#define MT9V032_AEC_ENABLE (1 << 0)
115#define MT9V032_AGC_ENABLE (1 << 1)
116#define MT9V032_THERMAL_INFO 0xc1
117
118struct mt9v032 {
119 struct v4l2_subdev subdev;
120 struct media_pad pad;
121
122 struct v4l2_mbus_framefmt format;
123 struct v4l2_rect crop;
124
Sakari Ailus41a33a02012-03-15 18:01:39 -0300125 struct v4l2_ctrl *pixel_rate;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300126 struct v4l2_ctrl_handler ctrls;
127
128 struct mutex power_lock;
129 int power_count;
130
131 struct mt9v032_platform_data *pdata;
132 u16 chip_control;
133 u16 aec_agc;
134};
135
136static struct mt9v032 *to_mt9v032(struct v4l2_subdev *sd)
137{
138 return container_of(sd, struct mt9v032, subdev);
139}
140
141static int mt9v032_read(struct i2c_client *client, const u8 reg)
142{
Jonathan Cameron3f877042011-10-21 09:30:25 -0300143 s32 data = i2c_smbus_read_word_swapped(client, reg);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300144 dev_dbg(&client->dev, "%s: read 0x%04x from 0x%02x\n", __func__,
Jonathan Cameron3f877042011-10-21 09:30:25 -0300145 data, reg);
146 return data;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300147}
148
149static int mt9v032_write(struct i2c_client *client, const u8 reg,
150 const u16 data)
151{
152 dev_dbg(&client->dev, "%s: writing 0x%04x to 0x%02x\n", __func__,
153 data, reg);
Jonathan Cameron3f877042011-10-21 09:30:25 -0300154 return i2c_smbus_write_word_swapped(client, reg, data);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300155}
156
157static int mt9v032_set_chip_control(struct mt9v032 *mt9v032, u16 clear, u16 set)
158{
159 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
160 u16 value = (mt9v032->chip_control & ~clear) | set;
161 int ret;
162
163 ret = mt9v032_write(client, MT9V032_CHIP_CONTROL, value);
164 if (ret < 0)
165 return ret;
166
167 mt9v032->chip_control = value;
168 return 0;
169}
170
171static int
172mt9v032_update_aec_agc(struct mt9v032 *mt9v032, u16 which, int enable)
173{
174 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
175 u16 value = mt9v032->aec_agc;
176 int ret;
177
178 if (enable)
179 value |= which;
180 else
181 value &= ~which;
182
183 ret = mt9v032_write(client, MT9V032_AEC_AGC_ENABLE, value);
184 if (ret < 0)
185 return ret;
186
187 mt9v032->aec_agc = value;
188 return 0;
189}
190
Sakari Ailus41a33a02012-03-15 18:01:39 -0300191#define EXT_CLK 25000000
192
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300193static int mt9v032_power_on(struct mt9v032 *mt9v032)
194{
195 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
196 int ret;
197
198 if (mt9v032->pdata->set_clock) {
Sakari Ailus41a33a02012-03-15 18:01:39 -0300199 mt9v032->pdata->set_clock(&mt9v032->subdev, EXT_CLK);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300200 udelay(1);
201 }
202
203 /* Reset the chip and stop data read out */
204 ret = mt9v032_write(client, MT9V032_RESET, 1);
205 if (ret < 0)
206 return ret;
207
208 ret = mt9v032_write(client, MT9V032_RESET, 0);
209 if (ret < 0)
210 return ret;
211
212 return mt9v032_write(client, MT9V032_CHIP_CONTROL, 0);
213}
214
215static void mt9v032_power_off(struct mt9v032 *mt9v032)
216{
217 if (mt9v032->pdata->set_clock)
218 mt9v032->pdata->set_clock(&mt9v032->subdev, 0);
219}
220
221static int __mt9v032_set_power(struct mt9v032 *mt9v032, bool on)
222{
223 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
224 int ret;
225
226 if (!on) {
227 mt9v032_power_off(mt9v032);
228 return 0;
229 }
230
231 ret = mt9v032_power_on(mt9v032);
232 if (ret < 0)
233 return ret;
234
235 /* Configure the pixel clock polarity */
236 if (mt9v032->pdata && mt9v032->pdata->clk_pol) {
237 ret = mt9v032_write(client, MT9V032_PIXEL_CLOCK,
238 MT9V032_PIXEL_CLOCK_INV_PXL_CLK);
239 if (ret < 0)
240 return ret;
241 }
242
243 /* Disable the noise correction algorithm and restore the controls. */
244 ret = mt9v032_write(client, MT9V032_ROW_NOISE_CORR_CONTROL, 0);
245 if (ret < 0)
246 return ret;
247
248 return v4l2_ctrl_handler_setup(&mt9v032->ctrls);
249}
250
251/* -----------------------------------------------------------------------------
252 * V4L2 subdev video operations
253 */
254
255static struct v4l2_mbus_framefmt *
256__mt9v032_get_pad_format(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
257 unsigned int pad, enum v4l2_subdev_format_whence which)
258{
259 switch (which) {
260 case V4L2_SUBDEV_FORMAT_TRY:
261 return v4l2_subdev_get_try_format(fh, pad);
262 case V4L2_SUBDEV_FORMAT_ACTIVE:
263 return &mt9v032->format;
264 default:
265 return NULL;
266 }
267}
268
269static struct v4l2_rect *
270__mt9v032_get_pad_crop(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
271 unsigned int pad, enum v4l2_subdev_format_whence which)
272{
273 switch (which) {
274 case V4L2_SUBDEV_FORMAT_TRY:
275 return v4l2_subdev_get_try_crop(fh, pad);
276 case V4L2_SUBDEV_FORMAT_ACTIVE:
277 return &mt9v032->crop;
278 default:
279 return NULL;
280 }
281}
282
283static int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable)
284{
285 const u16 mode = MT9V032_CHIP_CONTROL_MASTER_MODE
286 | MT9V032_CHIP_CONTROL_DOUT_ENABLE
287 | MT9V032_CHIP_CONTROL_SEQUENTIAL;
288 struct i2c_client *client = v4l2_get_subdevdata(subdev);
289 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
290 struct v4l2_mbus_framefmt *format = &mt9v032->format;
291 struct v4l2_rect *crop = &mt9v032->crop;
292 unsigned int hratio;
293 unsigned int vratio;
294 int ret;
295
296 if (!enable)
297 return mt9v032_set_chip_control(mt9v032, mode, 0);
298
299 /* Configure the window size and row/column bin */
300 hratio = DIV_ROUND_CLOSEST(crop->width, format->width);
301 vratio = DIV_ROUND_CLOSEST(crop->height, format->height);
302
303 ret = mt9v032_write(client, MT9V032_READ_MODE,
304 (hratio - 1) << MT9V032_READ_MODE_ROW_BIN_SHIFT |
305 (vratio - 1) << MT9V032_READ_MODE_COLUMN_BIN_SHIFT);
306 if (ret < 0)
307 return ret;
308
309 ret = mt9v032_write(client, MT9V032_COLUMN_START, crop->left);
310 if (ret < 0)
311 return ret;
312
313 ret = mt9v032_write(client, MT9V032_ROW_START, crop->top);
314 if (ret < 0)
315 return ret;
316
317 ret = mt9v032_write(client, MT9V032_WINDOW_WIDTH, crop->width);
318 if (ret < 0)
319 return ret;
320
321 ret = mt9v032_write(client, MT9V032_WINDOW_HEIGHT, crop->height);
322 if (ret < 0)
323 return ret;
324
325 ret = mt9v032_write(client, MT9V032_HORIZONTAL_BLANKING,
326 max(43, 660 - crop->width));
327 if (ret < 0)
328 return ret;
329
330 /* Switch to master "normal" mode */
331 return mt9v032_set_chip_control(mt9v032, 0, mode);
332}
333
334static int mt9v032_enum_mbus_code(struct v4l2_subdev *subdev,
335 struct v4l2_subdev_fh *fh,
336 struct v4l2_subdev_mbus_code_enum *code)
337{
338 if (code->index > 0)
339 return -EINVAL;
340
341 code->code = V4L2_MBUS_FMT_SGRBG10_1X10;
342 return 0;
343}
344
345static int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
346 struct v4l2_subdev_fh *fh,
347 struct v4l2_subdev_frame_size_enum *fse)
348{
349 if (fse->index >= 8 || fse->code != V4L2_MBUS_FMT_SGRBG10_1X10)
350 return -EINVAL;
351
352 fse->min_width = MT9V032_WINDOW_WIDTH_DEF / fse->index;
353 fse->max_width = fse->min_width;
354 fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / fse->index;
355 fse->max_height = fse->min_height;
356
357 return 0;
358}
359
360static int mt9v032_get_format(struct v4l2_subdev *subdev,
361 struct v4l2_subdev_fh *fh,
362 struct v4l2_subdev_format *format)
363{
364 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
365
366 format->format = *__mt9v032_get_pad_format(mt9v032, fh, format->pad,
367 format->which);
368 return 0;
369}
370
Sakari Ailus41a33a02012-03-15 18:01:39 -0300371static void mt9v032_configure_pixel_rate(struct mt9v032 *mt9v032,
372 unsigned int hratio)
373{
374 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
375 int ret;
376
377 ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate, EXT_CLK / hratio);
378 if (ret < 0)
379 dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret);
380}
381
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300382static int mt9v032_set_format(struct v4l2_subdev *subdev,
383 struct v4l2_subdev_fh *fh,
384 struct v4l2_subdev_format *format)
385{
386 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
387 struct v4l2_mbus_framefmt *__format;
388 struct v4l2_rect *__crop;
389 unsigned int width;
390 unsigned int height;
391 unsigned int hratio;
392 unsigned int vratio;
393
394 __crop = __mt9v032_get_pad_crop(mt9v032, fh, format->pad,
395 format->which);
396
397 /* Clamp the width and height to avoid dividing by zero. */
398 width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
399 max(__crop->width / 8, MT9V032_WINDOW_WIDTH_MIN),
400 __crop->width);
401 height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
402 max(__crop->height / 8, MT9V032_WINDOW_HEIGHT_MIN),
403 __crop->height);
404
405 hratio = DIV_ROUND_CLOSEST(__crop->width, width);
406 vratio = DIV_ROUND_CLOSEST(__crop->height, height);
407
408 __format = __mt9v032_get_pad_format(mt9v032, fh, format->pad,
409 format->which);
410 __format->width = __crop->width / hratio;
411 __format->height = __crop->height / vratio;
Sakari Ailus41a33a02012-03-15 18:01:39 -0300412 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
413 mt9v032_configure_pixel_rate(mt9v032, hratio);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300414
415 format->format = *__format;
416
417 return 0;
418}
419
420static int mt9v032_get_crop(struct v4l2_subdev *subdev,
421 struct v4l2_subdev_fh *fh,
422 struct v4l2_subdev_crop *crop)
423{
424 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
425
426 crop->rect = *__mt9v032_get_pad_crop(mt9v032, fh, crop->pad,
427 crop->which);
428 return 0;
429}
430
431static int mt9v032_set_crop(struct v4l2_subdev *subdev,
432 struct v4l2_subdev_fh *fh,
433 struct v4l2_subdev_crop *crop)
434{
435 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
436 struct v4l2_mbus_framefmt *__format;
437 struct v4l2_rect *__crop;
438 struct v4l2_rect rect;
439
Laurent Pinchart86cf7862010-11-28 15:07:20 -0300440 /* Clamp the crop rectangle boundaries and align them to a non multiple
441 * of 2 pixels to ensure a GRBG Bayer pattern.
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300442 */
Laurent Pinchart86cf7862010-11-28 15:07:20 -0300443 rect.left = clamp(ALIGN(crop->rect.left + 1, 2) - 1,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300444 MT9V032_COLUMN_START_MIN,
445 MT9V032_COLUMN_START_MAX);
Laurent Pinchart86cf7862010-11-28 15:07:20 -0300446 rect.top = clamp(ALIGN(crop->rect.top + 1, 2) - 1,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300447 MT9V032_ROW_START_MIN,
448 MT9V032_ROW_START_MAX);
449 rect.width = clamp(ALIGN(crop->rect.width, 2),
450 MT9V032_WINDOW_WIDTH_MIN,
451 MT9V032_WINDOW_WIDTH_MAX);
452 rect.height = clamp(ALIGN(crop->rect.height, 2),
453 MT9V032_WINDOW_HEIGHT_MIN,
454 MT9V032_WINDOW_HEIGHT_MAX);
455
456 rect.width = min(rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left);
457 rect.height = min(rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top);
458
459 __crop = __mt9v032_get_pad_crop(mt9v032, fh, crop->pad, crop->which);
460
461 if (rect.width != __crop->width || rect.height != __crop->height) {
462 /* Reset the output image size if the crop rectangle size has
463 * been modified.
464 */
465 __format = __mt9v032_get_pad_format(mt9v032, fh, crop->pad,
466 crop->which);
467 __format->width = rect.width;
468 __format->height = rect.height;
Sakari Ailus41a33a02012-03-15 18:01:39 -0300469 if (crop->which == V4L2_SUBDEV_FORMAT_ACTIVE)
470 mt9v032_configure_pixel_rate(mt9v032, 1);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300471 }
472
473 *__crop = rect;
474 crop->rect = rect;
475
476 return 0;
477}
478
479/* -----------------------------------------------------------------------------
480 * V4L2 subdev control operations
481 */
482
483#define V4L2_CID_TEST_PATTERN (V4L2_CID_USER_BASE | 0x1001)
484
485static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
486{
487 struct mt9v032 *mt9v032 =
488 container_of(ctrl->handler, struct mt9v032, ctrls);
489 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
490 u16 data;
491
492 switch (ctrl->id) {
493 case V4L2_CID_AUTOGAIN:
494 return mt9v032_update_aec_agc(mt9v032, MT9V032_AGC_ENABLE,
495 ctrl->val);
496
497 case V4L2_CID_GAIN:
498 return mt9v032_write(client, MT9V032_ANALOG_GAIN, ctrl->val);
499
500 case V4L2_CID_EXPOSURE_AUTO:
501 return mt9v032_update_aec_agc(mt9v032, MT9V032_AEC_ENABLE,
Kartik Mohta5c375982012-05-02 19:19:08 -0300502 !ctrl->val);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300503
504 case V4L2_CID_EXPOSURE:
505 return mt9v032_write(client, MT9V032_TOTAL_SHUTTER_WIDTH,
506 ctrl->val);
507
508 case V4L2_CID_TEST_PATTERN:
509 switch (ctrl->val) {
510 case 0:
511 data = 0;
512 break;
513 case 1:
514 data = MT9V032_TEST_PATTERN_GRAY_VERTICAL
515 | MT9V032_TEST_PATTERN_ENABLE;
516 break;
517 case 2:
518 data = MT9V032_TEST_PATTERN_GRAY_HORIZONTAL
519 | MT9V032_TEST_PATTERN_ENABLE;
520 break;
521 case 3:
522 data = MT9V032_TEST_PATTERN_GRAY_DIAGONAL
523 | MT9V032_TEST_PATTERN_ENABLE;
524 break;
525 default:
526 data = (ctrl->val << MT9V032_TEST_PATTERN_DATA_SHIFT)
527 | MT9V032_TEST_PATTERN_USE_DATA
528 | MT9V032_TEST_PATTERN_ENABLE
529 | MT9V032_TEST_PATTERN_FLIP;
530 break;
531 }
532
533 return mt9v032_write(client, MT9V032_TEST_PATTERN, data);
534 }
535
536 return 0;
537}
538
539static struct v4l2_ctrl_ops mt9v032_ctrl_ops = {
540 .s_ctrl = mt9v032_s_ctrl,
541};
542
543static const struct v4l2_ctrl_config mt9v032_ctrls[] = {
544 {
545 .ops = &mt9v032_ctrl_ops,
546 .id = V4L2_CID_TEST_PATTERN,
547 .type = V4L2_CTRL_TYPE_INTEGER,
548 .name = "Test pattern",
549 .min = 0,
550 .max = 1023,
551 .step = 1,
552 .def = 0,
553 .flags = 0,
554 }
555};
556
557/* -----------------------------------------------------------------------------
558 * V4L2 subdev core operations
559 */
560
561static int mt9v032_set_power(struct v4l2_subdev *subdev, int on)
562{
563 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
564 int ret = 0;
565
566 mutex_lock(&mt9v032->power_lock);
567
568 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
569 * update the power state.
570 */
571 if (mt9v032->power_count == !on) {
572 ret = __mt9v032_set_power(mt9v032, !!on);
573 if (ret < 0)
574 goto done;
575 }
576
577 /* Update the power count. */
578 mt9v032->power_count += on ? 1 : -1;
579 WARN_ON(mt9v032->power_count < 0);
580
581done:
582 mutex_unlock(&mt9v032->power_lock);
583 return ret;
584}
585
586/* -----------------------------------------------------------------------------
587 * V4L2 subdev internal operations
588 */
589
590static int mt9v032_registered(struct v4l2_subdev *subdev)
591{
592 struct i2c_client *client = v4l2_get_subdevdata(subdev);
593 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
594 s32 data;
595 int ret;
596
597 dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n",
598 client->addr);
599
600 ret = mt9v032_power_on(mt9v032);
601 if (ret < 0) {
602 dev_err(&client->dev, "MT9V032 power up failed\n");
603 return ret;
604 }
605
606 /* Read and check the sensor version */
607 data = mt9v032_read(client, MT9V032_CHIP_VERSION);
608 if (data != MT9V032_CHIP_ID_REV1 && data != MT9V032_CHIP_ID_REV3) {
609 dev_err(&client->dev, "MT9V032 not detected, wrong version "
610 "0x%04x\n", data);
611 return -ENODEV;
612 }
613
614 mt9v032_power_off(mt9v032);
615
616 dev_info(&client->dev, "MT9V032 detected at address 0x%02x\n",
617 client->addr);
618
Sakari Ailus41a33a02012-03-15 18:01:39 -0300619 mt9v032_configure_pixel_rate(mt9v032, 1);
620
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300621 return ret;
622}
623
624static int mt9v032_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
625{
626 struct v4l2_mbus_framefmt *format;
627 struct v4l2_rect *crop;
628
629 crop = v4l2_subdev_get_try_crop(fh, 0);
630 crop->left = MT9V032_COLUMN_START_DEF;
631 crop->top = MT9V032_ROW_START_DEF;
632 crop->width = MT9V032_WINDOW_WIDTH_DEF;
633 crop->height = MT9V032_WINDOW_HEIGHT_DEF;
634
635 format = v4l2_subdev_get_try_format(fh, 0);
636 format->code = V4L2_MBUS_FMT_SGRBG10_1X10;
637 format->width = MT9V032_WINDOW_WIDTH_DEF;
638 format->height = MT9V032_WINDOW_HEIGHT_DEF;
639 format->field = V4L2_FIELD_NONE;
640 format->colorspace = V4L2_COLORSPACE_SRGB;
641
642 return mt9v032_set_power(subdev, 1);
643}
644
645static int mt9v032_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
646{
647 return mt9v032_set_power(subdev, 0);
648}
649
650static struct v4l2_subdev_core_ops mt9v032_subdev_core_ops = {
651 .s_power = mt9v032_set_power,
652};
653
654static struct v4l2_subdev_video_ops mt9v032_subdev_video_ops = {
655 .s_stream = mt9v032_s_stream,
656};
657
658static struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops = {
659 .enum_mbus_code = mt9v032_enum_mbus_code,
660 .enum_frame_size = mt9v032_enum_frame_size,
661 .get_fmt = mt9v032_get_format,
662 .set_fmt = mt9v032_set_format,
663 .get_crop = mt9v032_get_crop,
664 .set_crop = mt9v032_set_crop,
665};
666
667static struct v4l2_subdev_ops mt9v032_subdev_ops = {
668 .core = &mt9v032_subdev_core_ops,
669 .video = &mt9v032_subdev_video_ops,
670 .pad = &mt9v032_subdev_pad_ops,
671};
672
673static const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops = {
674 .registered = mt9v032_registered,
675 .open = mt9v032_open,
676 .close = mt9v032_close,
677};
678
679/* -----------------------------------------------------------------------------
680 * Driver initialization and probing
681 */
682
683static int mt9v032_probe(struct i2c_client *client,
684 const struct i2c_device_id *did)
685{
686 struct mt9v032 *mt9v032;
687 unsigned int i;
688 int ret;
689
690 if (!i2c_check_functionality(client->adapter,
691 I2C_FUNC_SMBUS_WORD_DATA)) {
692 dev_warn(&client->adapter->dev,
693 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
694 return -EIO;
695 }
696
697 mt9v032 = kzalloc(sizeof(*mt9v032), GFP_KERNEL);
698 if (!mt9v032)
699 return -ENOMEM;
700
701 mutex_init(&mt9v032->power_lock);
702 mt9v032->pdata = client->dev.platform_data;
703
Sakari Ailus41a33a02012-03-15 18:01:39 -0300704 v4l2_ctrl_handler_init(&mt9v032->ctrls, ARRAY_SIZE(mt9v032_ctrls) + 5);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300705
706 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
707 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
708 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
709 V4L2_CID_GAIN, MT9V032_ANALOG_GAIN_MIN,
710 MT9V032_ANALOG_GAIN_MAX, 1, MT9V032_ANALOG_GAIN_DEF);
711 v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops,
712 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
713 V4L2_EXPOSURE_AUTO);
714 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
715 V4L2_CID_EXPOSURE, MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
716 MT9V032_TOTAL_SHUTTER_WIDTH_MAX, 1,
717 MT9V032_TOTAL_SHUTTER_WIDTH_DEF);
Sakari Ailus41a33a02012-03-15 18:01:39 -0300718 mt9v032->pixel_rate =
719 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
720 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300721
722 for (i = 0; i < ARRAY_SIZE(mt9v032_ctrls); ++i)
723 v4l2_ctrl_new_custom(&mt9v032->ctrls, &mt9v032_ctrls[i], NULL);
724
725 mt9v032->subdev.ctrl_handler = &mt9v032->ctrls;
726
727 if (mt9v032->ctrls.error)
728 printk(KERN_INFO "%s: control initialization error %d\n",
729 __func__, mt9v032->ctrls.error);
730
731 mt9v032->crop.left = MT9V032_COLUMN_START_DEF;
732 mt9v032->crop.top = MT9V032_ROW_START_DEF;
733 mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF;
734 mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF;
735
736 mt9v032->format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
737 mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF;
738 mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF;
739 mt9v032->format.field = V4L2_FIELD_NONE;
740 mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB;
741
742 mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE;
743
744 v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops);
745 mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops;
746 mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
747
748 mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE;
749 ret = media_entity_init(&mt9v032->subdev.entity, 1, &mt9v032->pad, 0);
750 if (ret < 0)
751 kfree(mt9v032);
752
753 return ret;
754}
755
756static int mt9v032_remove(struct i2c_client *client)
757{
758 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
759 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
760
761 v4l2_device_unregister_subdev(subdev);
762 media_entity_cleanup(&subdev->entity);
763 kfree(mt9v032);
764 return 0;
765}
766
767static const struct i2c_device_id mt9v032_id[] = {
768 { "mt9v032", 0 },
769 { }
770};
771MODULE_DEVICE_TABLE(i2c, mt9v032_id);
772
773static struct i2c_driver mt9v032_driver = {
774 .driver = {
775 .name = "mt9v032",
776 },
777 .probe = mt9v032_probe,
778 .remove = mt9v032_remove,
779 .id_table = mt9v032_id,
780};
781
Axel Linc6e8d862012-02-12 06:56:32 -0300782module_i2c_driver(mt9v032_driver);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300783
784MODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
785MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
786MODULE_LICENSE("GPL");