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