blob: 62db26b0b4f02a21c3f2d73a8dcc55921ac6db7d [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
Laurent Pinchart3300a8f2013-07-05 07:16:02 -030015#include <linux/clk.h>
Detlev Casanova0f2ce162011-04-05 09:06:21 -030016#include <linux/delay.h>
17#include <linux/i2c.h>
18#include <linux/log2.h>
19#include <linux/mutex.h>
20#include <linux/slab.h>
21#include <linux/videodev2.h>
22#include <linux/v4l2-mediabus.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040023#include <linux/module.h>
Detlev Casanova0f2ce162011-04-05 09:06:21 -030024
25#include <media/mt9v032.h>
26#include <media/v4l2-ctrls.h>
27#include <media/v4l2-device.h>
28#include <media/v4l2-subdev.h>
29
Laurent Pinchart2b9e9f72013-12-02 11:52:44 -030030/* The first four rows are black rows. The active area spans 753x481 pixels. */
31#define MT9V032_PIXEL_ARRAY_HEIGHT 485
32#define MT9V032_PIXEL_ARRAY_WIDTH 753
Detlev Casanova0f2ce162011-04-05 09:06:21 -030033
Laurent Pincharte9a50e42012-07-26 08:02:50 -030034#define MT9V032_SYSCLK_FREQ_DEF 26600000
35
Detlev Casanova0f2ce162011-04-05 09:06:21 -030036#define MT9V032_CHIP_VERSION 0x00
37#define MT9V032_CHIP_ID_REV1 0x1311
38#define MT9V032_CHIP_ID_REV3 0x1313
Laurent Pinchart86cf7862010-11-28 15:07:20 -030039#define MT9V032_COLUMN_START 0x01
Detlev Casanova0f2ce162011-04-05 09:06:21 -030040#define MT9V032_COLUMN_START_MIN 1
Laurent Pinchart86cf7862010-11-28 15:07:20 -030041#define MT9V032_COLUMN_START_DEF 1
Detlev Casanova0f2ce162011-04-05 09:06:21 -030042#define MT9V032_COLUMN_START_MAX 752
Laurent Pinchart86cf7862010-11-28 15:07:20 -030043#define MT9V032_ROW_START 0x02
44#define MT9V032_ROW_START_MIN 4
45#define MT9V032_ROW_START_DEF 5
46#define MT9V032_ROW_START_MAX 482
Detlev Casanova0f2ce162011-04-05 09:06:21 -030047#define MT9V032_WINDOW_HEIGHT 0x03
48#define MT9V032_WINDOW_HEIGHT_MIN 1
49#define MT9V032_WINDOW_HEIGHT_DEF 480
50#define MT9V032_WINDOW_HEIGHT_MAX 480
51#define MT9V032_WINDOW_WIDTH 0x04
52#define MT9V032_WINDOW_WIDTH_MIN 1
53#define MT9V032_WINDOW_WIDTH_DEF 752
54#define MT9V032_WINDOW_WIDTH_MAX 752
55#define MT9V032_HORIZONTAL_BLANKING 0x05
56#define MT9V032_HORIZONTAL_BLANKING_MIN 43
Laurent Pinchart9ec670e2012-07-23 15:03:34 -030057#define MT9V032_HORIZONTAL_BLANKING_DEF 94
Detlev Casanova0f2ce162011-04-05 09:06:21 -030058#define MT9V032_HORIZONTAL_BLANKING_MAX 1023
59#define MT9V032_VERTICAL_BLANKING 0x06
60#define MT9V032_VERTICAL_BLANKING_MIN 4
Laurent Pinchart9ec670e2012-07-23 15:03:34 -030061#define MT9V032_VERTICAL_BLANKING_DEF 45
Detlev Casanova0f2ce162011-04-05 09:06:21 -030062#define MT9V032_VERTICAL_BLANKING_MAX 3000
63#define MT9V032_CHIP_CONTROL 0x07
64#define MT9V032_CHIP_CONTROL_MASTER_MODE (1 << 3)
65#define MT9V032_CHIP_CONTROL_DOUT_ENABLE (1 << 7)
66#define MT9V032_CHIP_CONTROL_SEQUENTIAL (1 << 8)
67#define MT9V032_SHUTTER_WIDTH1 0x08
68#define MT9V032_SHUTTER_WIDTH2 0x09
69#define MT9V032_SHUTTER_WIDTH_CONTROL 0x0a
70#define MT9V032_TOTAL_SHUTTER_WIDTH 0x0b
71#define MT9V032_TOTAL_SHUTTER_WIDTH_MIN 1
72#define MT9V032_TOTAL_SHUTTER_WIDTH_DEF 480
73#define MT9V032_TOTAL_SHUTTER_WIDTH_MAX 32767
74#define MT9V032_RESET 0x0c
75#define MT9V032_READ_MODE 0x0d
76#define MT9V032_READ_MODE_ROW_BIN_MASK (3 << 0)
77#define MT9V032_READ_MODE_ROW_BIN_SHIFT 0
78#define MT9V032_READ_MODE_COLUMN_BIN_MASK (3 << 2)
79#define MT9V032_READ_MODE_COLUMN_BIN_SHIFT 2
80#define MT9V032_READ_MODE_ROW_FLIP (1 << 4)
81#define MT9V032_READ_MODE_COLUMN_FLIP (1 << 5)
82#define MT9V032_READ_MODE_DARK_COLUMNS (1 << 6)
83#define MT9V032_READ_MODE_DARK_ROWS (1 << 7)
84#define MT9V032_PIXEL_OPERATION_MODE 0x0f
85#define MT9V032_PIXEL_OPERATION_MODE_COLOR (1 << 2)
86#define MT9V032_PIXEL_OPERATION_MODE_HDR (1 << 6)
87#define MT9V032_ANALOG_GAIN 0x35
88#define MT9V032_ANALOG_GAIN_MIN 16
89#define MT9V032_ANALOG_GAIN_DEF 16
90#define MT9V032_ANALOG_GAIN_MAX 64
91#define MT9V032_MAX_ANALOG_GAIN 0x36
92#define MT9V032_MAX_ANALOG_GAIN_MAX 127
93#define MT9V032_FRAME_DARK_AVERAGE 0x42
94#define MT9V032_DARK_AVG_THRESH 0x46
95#define MT9V032_DARK_AVG_LOW_THRESH_MASK (255 << 0)
96#define MT9V032_DARK_AVG_LOW_THRESH_SHIFT 0
97#define MT9V032_DARK_AVG_HIGH_THRESH_MASK (255 << 8)
98#define MT9V032_DARK_AVG_HIGH_THRESH_SHIFT 8
99#define MT9V032_ROW_NOISE_CORR_CONTROL 0x70
100#define MT9V032_ROW_NOISE_CORR_ENABLE (1 << 5)
101#define MT9V032_ROW_NOISE_CORR_USE_BLK_AVG (1 << 7)
102#define MT9V032_PIXEL_CLOCK 0x74
103#define MT9V032_PIXEL_CLOCK_INV_LINE (1 << 0)
104#define MT9V032_PIXEL_CLOCK_INV_FRAME (1 << 1)
105#define MT9V032_PIXEL_CLOCK_XOR_LINE (1 << 2)
106#define MT9V032_PIXEL_CLOCK_CONT_LINE (1 << 3)
107#define MT9V032_PIXEL_CLOCK_INV_PXL_CLK (1 << 4)
108#define MT9V032_TEST_PATTERN 0x7f
109#define MT9V032_TEST_PATTERN_DATA_MASK (1023 << 0)
110#define MT9V032_TEST_PATTERN_DATA_SHIFT 0
111#define MT9V032_TEST_PATTERN_USE_DATA (1 << 10)
112#define MT9V032_TEST_PATTERN_GRAY_MASK (3 << 11)
113#define MT9V032_TEST_PATTERN_GRAY_NONE (0 << 11)
114#define MT9V032_TEST_PATTERN_GRAY_VERTICAL (1 << 11)
115#define MT9V032_TEST_PATTERN_GRAY_HORIZONTAL (2 << 11)
116#define MT9V032_TEST_PATTERN_GRAY_DIAGONAL (3 << 11)
117#define MT9V032_TEST_PATTERN_ENABLE (1 << 13)
118#define MT9V032_TEST_PATTERN_FLIP (1 << 14)
119#define MT9V032_AEC_AGC_ENABLE 0xaf
120#define MT9V032_AEC_ENABLE (1 << 0)
121#define MT9V032_AGC_ENABLE (1 << 1)
122#define MT9V032_THERMAL_INFO 0xc1
123
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300124enum mt9v032_model {
125 MT9V032_MODEL_COLOR,
126 MT9V032_MODEL_MONO,
127};
128
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300129struct mt9v032_model_version {
130 unsigned int version;
131 const char *name;
132};
133
134struct mt9v032_model_data {
135 unsigned int min_row_time;
136 unsigned int min_hblank;
137 unsigned int min_vblank;
138 unsigned int max_vblank;
139 unsigned int min_shutter;
140 unsigned int max_shutter;
141 unsigned int pclk_reg;
142};
143
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300144struct mt9v032_model_info {
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300145 const struct mt9v032_model_data *data;
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300146 bool color;
147};
148
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300149static const struct mt9v032_model_version mt9v032_versions[] = {
150 { MT9V032_CHIP_ID_REV1, "MT9V032 rev1/2" },
151 { MT9V032_CHIP_ID_REV3, "MT9V032 rev3" },
152};
153
154static const struct mt9v032_model_data mt9v032_model_data[] = {
155 {
156 /* MT9V032 revisions 1/2/3 */
157 .min_row_time = 660,
158 .min_hblank = MT9V032_HORIZONTAL_BLANKING_MIN,
159 .min_vblank = MT9V032_VERTICAL_BLANKING_MIN,
160 .max_vblank = MT9V032_VERTICAL_BLANKING_MAX,
161 .min_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
162 .max_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MAX,
163 .pclk_reg = MT9V032_PIXEL_CLOCK,
164 },
165};
166
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300167static const struct mt9v032_model_info mt9v032_models[] = {
168 [MT9V032_MODEL_COLOR] = {
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300169 .data = &mt9v032_model_data[0],
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300170 .color = true,
171 },
172 [MT9V032_MODEL_MONO] = {
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300173 .data = &mt9v032_model_data[0],
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300174 .color = false,
175 },
176};
177
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300178struct mt9v032 {
179 struct v4l2_subdev subdev;
180 struct media_pad pad;
181
182 struct v4l2_mbus_framefmt format;
183 struct v4l2_rect crop;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300184 unsigned int hratio;
185 unsigned int vratio;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300186
187 struct v4l2_ctrl_handler ctrls;
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300188 struct {
189 struct v4l2_ctrl *link_freq;
190 struct v4l2_ctrl *pixel_rate;
191 };
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300192
193 struct mutex power_lock;
194 int power_count;
195
Laurent Pinchart3300a8f2013-07-05 07:16:02 -0300196 struct clk *clk;
197
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300198 struct mt9v032_platform_data *pdata;
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300199 const struct mt9v032_model_info *model;
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300200 const struct mt9v032_model_version *version;
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300201
202 u32 sysclk;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300203 u16 chip_control;
204 u16 aec_agc;
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300205 u16 hblank;
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300206 struct {
207 struct v4l2_ctrl *test_pattern;
208 struct v4l2_ctrl *test_pattern_color;
209 };
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300210};
211
212static struct mt9v032 *to_mt9v032(struct v4l2_subdev *sd)
213{
214 return container_of(sd, struct mt9v032, subdev);
215}
216
217static int mt9v032_read(struct i2c_client *client, const u8 reg)
218{
Jonathan Cameron3f877042011-10-21 09:30:25 -0300219 s32 data = i2c_smbus_read_word_swapped(client, reg);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300220 dev_dbg(&client->dev, "%s: read 0x%04x from 0x%02x\n", __func__,
Jonathan Cameron3f877042011-10-21 09:30:25 -0300221 data, reg);
222 return data;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300223}
224
225static int mt9v032_write(struct i2c_client *client, const u8 reg,
226 const u16 data)
227{
228 dev_dbg(&client->dev, "%s: writing 0x%04x to 0x%02x\n", __func__,
229 data, reg);
Jonathan Cameron3f877042011-10-21 09:30:25 -0300230 return i2c_smbus_write_word_swapped(client, reg, data);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300231}
232
233static int mt9v032_set_chip_control(struct mt9v032 *mt9v032, u16 clear, u16 set)
234{
235 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
236 u16 value = (mt9v032->chip_control & ~clear) | set;
237 int ret;
238
239 ret = mt9v032_write(client, MT9V032_CHIP_CONTROL, value);
240 if (ret < 0)
241 return ret;
242
243 mt9v032->chip_control = value;
244 return 0;
245}
246
247static int
248mt9v032_update_aec_agc(struct mt9v032 *mt9v032, u16 which, int enable)
249{
250 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
251 u16 value = mt9v032->aec_agc;
252 int ret;
253
254 if (enable)
255 value |= which;
256 else
257 value &= ~which;
258
259 ret = mt9v032_write(client, MT9V032_AEC_AGC_ENABLE, value);
260 if (ret < 0)
261 return ret;
262
263 mt9v032->aec_agc = value;
264 return 0;
265}
266
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300267static int
268mt9v032_update_hblank(struct mt9v032 *mt9v032)
269{
270 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
271 struct v4l2_rect *crop = &mt9v032->crop;
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300272 unsigned int hblank;
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300273
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300274 hblank = max_t(s32, mt9v032->hblank,
275 mt9v032->model->data->min_row_time - crop->width);
276 return mt9v032_write(client, MT9V032_HORIZONTAL_BLANKING, hblank);
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300277}
278
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300279static int mt9v032_power_on(struct mt9v032 *mt9v032)
280{
281 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
282 int ret;
283
Laurent Pinchart3300a8f2013-07-05 07:16:02 -0300284 clk_set_rate(mt9v032->clk, mt9v032->sysclk);
285 clk_prepare_enable(mt9v032->clk);
286 udelay(1);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300287
288 /* Reset the chip and stop data read out */
289 ret = mt9v032_write(client, MT9V032_RESET, 1);
290 if (ret < 0)
291 return ret;
292
293 ret = mt9v032_write(client, MT9V032_RESET, 0);
294 if (ret < 0)
295 return ret;
296
297 return mt9v032_write(client, MT9V032_CHIP_CONTROL, 0);
298}
299
300static void mt9v032_power_off(struct mt9v032 *mt9v032)
301{
Laurent Pinchart3300a8f2013-07-05 07:16:02 -0300302 clk_disable_unprepare(mt9v032->clk);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300303}
304
305static int __mt9v032_set_power(struct mt9v032 *mt9v032, bool on)
306{
307 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
308 int ret;
309
310 if (!on) {
311 mt9v032_power_off(mt9v032);
312 return 0;
313 }
314
315 ret = mt9v032_power_on(mt9v032);
316 if (ret < 0)
317 return ret;
318
319 /* Configure the pixel clock polarity */
320 if (mt9v032->pdata && mt9v032->pdata->clk_pol) {
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300321 ret = mt9v032_write(client, mt9v032->model->data->pclk_reg,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300322 MT9V032_PIXEL_CLOCK_INV_PXL_CLK);
323 if (ret < 0)
324 return ret;
325 }
326
327 /* Disable the noise correction algorithm and restore the controls. */
328 ret = mt9v032_write(client, MT9V032_ROW_NOISE_CORR_CONTROL, 0);
329 if (ret < 0)
330 return ret;
331
332 return v4l2_ctrl_handler_setup(&mt9v032->ctrls);
333}
334
335/* -----------------------------------------------------------------------------
336 * V4L2 subdev video operations
337 */
338
339static struct v4l2_mbus_framefmt *
340__mt9v032_get_pad_format(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
341 unsigned int pad, enum v4l2_subdev_format_whence which)
342{
343 switch (which) {
344 case V4L2_SUBDEV_FORMAT_TRY:
345 return v4l2_subdev_get_try_format(fh, pad);
346 case V4L2_SUBDEV_FORMAT_ACTIVE:
347 return &mt9v032->format;
348 default:
349 return NULL;
350 }
351}
352
353static struct v4l2_rect *
354__mt9v032_get_pad_crop(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
355 unsigned int pad, enum v4l2_subdev_format_whence which)
356{
357 switch (which) {
358 case V4L2_SUBDEV_FORMAT_TRY:
359 return v4l2_subdev_get_try_crop(fh, pad);
360 case V4L2_SUBDEV_FORMAT_ACTIVE:
361 return &mt9v032->crop;
362 default:
363 return NULL;
364 }
365}
366
367static int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable)
368{
369 const u16 mode = MT9V032_CHIP_CONTROL_MASTER_MODE
370 | MT9V032_CHIP_CONTROL_DOUT_ENABLE
371 | MT9V032_CHIP_CONTROL_SEQUENTIAL;
372 struct i2c_client *client = v4l2_get_subdevdata(subdev);
373 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300374 struct v4l2_rect *crop = &mt9v032->crop;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300375 unsigned int hbin;
376 unsigned int vbin;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300377 int ret;
378
379 if (!enable)
380 return mt9v032_set_chip_control(mt9v032, mode, 0);
381
382 /* Configure the window size and row/column bin */
Laurent Pinchart637f0052013-12-02 16:39:18 -0300383 hbin = fls(mt9v032->hratio) - 1;
384 vbin = fls(mt9v032->vratio) - 1;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300385 ret = mt9v032_write(client, MT9V032_READ_MODE,
Laurent Pinchart637f0052013-12-02 16:39:18 -0300386 hbin << MT9V032_READ_MODE_COLUMN_BIN_SHIFT |
387 vbin << MT9V032_READ_MODE_ROW_BIN_SHIFT);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300388 if (ret < 0)
389 return ret;
390
391 ret = mt9v032_write(client, MT9V032_COLUMN_START, crop->left);
392 if (ret < 0)
393 return ret;
394
395 ret = mt9v032_write(client, MT9V032_ROW_START, crop->top);
396 if (ret < 0)
397 return ret;
398
399 ret = mt9v032_write(client, MT9V032_WINDOW_WIDTH, crop->width);
400 if (ret < 0)
401 return ret;
402
403 ret = mt9v032_write(client, MT9V032_WINDOW_HEIGHT, crop->height);
404 if (ret < 0)
405 return ret;
406
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300407 ret = mt9v032_update_hblank(mt9v032);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300408 if (ret < 0)
409 return ret;
410
411 /* Switch to master "normal" mode */
412 return mt9v032_set_chip_control(mt9v032, 0, mode);
413}
414
415static int mt9v032_enum_mbus_code(struct v4l2_subdev *subdev,
416 struct v4l2_subdev_fh *fh,
417 struct v4l2_subdev_mbus_code_enum *code)
418{
419 if (code->index > 0)
420 return -EINVAL;
421
422 code->code = V4L2_MBUS_FMT_SGRBG10_1X10;
423 return 0;
424}
425
426static int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
427 struct v4l2_subdev_fh *fh,
428 struct v4l2_subdev_frame_size_enum *fse)
429{
Laurent Pinchart637f0052013-12-02 16:39:18 -0300430 if (fse->index >= 3 || fse->code != V4L2_MBUS_FMT_SGRBG10_1X10)
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300431 return -EINVAL;
432
Laurent Pinchart637f0052013-12-02 16:39:18 -0300433 fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300434 fse->max_width = fse->min_width;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300435 fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / (1 << fse->index);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300436 fse->max_height = fse->min_height;
437
438 return 0;
439}
440
441static int mt9v032_get_format(struct v4l2_subdev *subdev,
442 struct v4l2_subdev_fh *fh,
443 struct v4l2_subdev_format *format)
444{
445 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
446
447 format->format = *__mt9v032_get_pad_format(mt9v032, fh, format->pad,
448 format->which);
449 return 0;
450}
451
Laurent Pinchart637f0052013-12-02 16:39:18 -0300452static void mt9v032_configure_pixel_rate(struct mt9v032 *mt9v032)
Sakari Ailus41a33a02012-03-15 18:01:39 -0300453{
454 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
455 int ret;
456
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300457 ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate,
Laurent Pinchart637f0052013-12-02 16:39:18 -0300458 mt9v032->sysclk / mt9v032->hratio);
Sakari Ailus41a33a02012-03-15 18:01:39 -0300459 if (ret < 0)
460 dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret);
461}
462
Laurent Pinchart637f0052013-12-02 16:39:18 -0300463static unsigned int mt9v032_calc_ratio(unsigned int input, unsigned int output)
464{
465 /* Compute the power-of-two binning factor closest to the input size to
466 * output size ratio. Given that the output size is bounded by input/4
467 * and input, a generic implementation would be an ineffective luxury.
468 */
469 if (output * 3 > input * 2)
470 return 1;
471 if (output * 3 > input)
472 return 2;
473 return 4;
474}
475
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300476static int mt9v032_set_format(struct v4l2_subdev *subdev,
477 struct v4l2_subdev_fh *fh,
478 struct v4l2_subdev_format *format)
479{
480 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
481 struct v4l2_mbus_framefmt *__format;
482 struct v4l2_rect *__crop;
483 unsigned int width;
484 unsigned int height;
485 unsigned int hratio;
486 unsigned int vratio;
487
488 __crop = __mt9v032_get_pad_crop(mt9v032, fh, format->pad,
489 format->which);
490
491 /* Clamp the width and height to avoid dividing by zero. */
492 width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
Laurent Pinchart637f0052013-12-02 16:39:18 -0300493 max(__crop->width / 4, MT9V032_WINDOW_WIDTH_MIN),
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300494 __crop->width);
495 height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
Laurent Pinchart637f0052013-12-02 16:39:18 -0300496 max(__crop->height / 4, MT9V032_WINDOW_HEIGHT_MIN),
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300497 __crop->height);
498
Laurent Pinchart637f0052013-12-02 16:39:18 -0300499 hratio = mt9v032_calc_ratio(__crop->width, width);
500 vratio = mt9v032_calc_ratio(__crop->height, height);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300501
502 __format = __mt9v032_get_pad_format(mt9v032, fh, format->pad,
503 format->which);
504 __format->width = __crop->width / hratio;
505 __format->height = __crop->height / vratio;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300506
507 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
508 mt9v032->hratio = hratio;
509 mt9v032->vratio = vratio;
510 mt9v032_configure_pixel_rate(mt9v032);
511 }
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300512
513 format->format = *__format;
514
515 return 0;
516}
517
518static int mt9v032_get_crop(struct v4l2_subdev *subdev,
519 struct v4l2_subdev_fh *fh,
520 struct v4l2_subdev_crop *crop)
521{
522 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
523
524 crop->rect = *__mt9v032_get_pad_crop(mt9v032, fh, crop->pad,
525 crop->which);
526 return 0;
527}
528
529static int mt9v032_set_crop(struct v4l2_subdev *subdev,
530 struct v4l2_subdev_fh *fh,
531 struct v4l2_subdev_crop *crop)
532{
533 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
534 struct v4l2_mbus_framefmt *__format;
535 struct v4l2_rect *__crop;
536 struct v4l2_rect rect;
537
Laurent Pinchart86cf7862010-11-28 15:07:20 -0300538 /* Clamp the crop rectangle boundaries and align them to a non multiple
539 * of 2 pixels to ensure a GRBG Bayer pattern.
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300540 */
Laurent Pinchart86cf7862010-11-28 15:07:20 -0300541 rect.left = clamp(ALIGN(crop->rect.left + 1, 2) - 1,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300542 MT9V032_COLUMN_START_MIN,
543 MT9V032_COLUMN_START_MAX);
Laurent Pinchart86cf7862010-11-28 15:07:20 -0300544 rect.top = clamp(ALIGN(crop->rect.top + 1, 2) - 1,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300545 MT9V032_ROW_START_MIN,
546 MT9V032_ROW_START_MAX);
547 rect.width = clamp(ALIGN(crop->rect.width, 2),
548 MT9V032_WINDOW_WIDTH_MIN,
549 MT9V032_WINDOW_WIDTH_MAX);
550 rect.height = clamp(ALIGN(crop->rect.height, 2),
551 MT9V032_WINDOW_HEIGHT_MIN,
552 MT9V032_WINDOW_HEIGHT_MAX);
553
554 rect.width = min(rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left);
555 rect.height = min(rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top);
556
557 __crop = __mt9v032_get_pad_crop(mt9v032, fh, crop->pad, crop->which);
558
559 if (rect.width != __crop->width || rect.height != __crop->height) {
560 /* Reset the output image size if the crop rectangle size has
561 * been modified.
562 */
563 __format = __mt9v032_get_pad_format(mt9v032, fh, crop->pad,
564 crop->which);
565 __format->width = rect.width;
566 __format->height = rect.height;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300567 if (crop->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
568 mt9v032->hratio = 1;
569 mt9v032->vratio = 1;
570 mt9v032_configure_pixel_rate(mt9v032);
571 }
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300572 }
573
574 *__crop = rect;
575 crop->rect = rect;
576
577 return 0;
578}
579
580/* -----------------------------------------------------------------------------
581 * V4L2 subdev control operations
582 */
583
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300584#define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300585
586static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
587{
588 struct mt9v032 *mt9v032 =
589 container_of(ctrl->handler, struct mt9v032, ctrls);
590 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300591 u32 freq;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300592 u16 data;
593
594 switch (ctrl->id) {
595 case V4L2_CID_AUTOGAIN:
596 return mt9v032_update_aec_agc(mt9v032, MT9V032_AGC_ENABLE,
597 ctrl->val);
598
599 case V4L2_CID_GAIN:
600 return mt9v032_write(client, MT9V032_ANALOG_GAIN, ctrl->val);
601
602 case V4L2_CID_EXPOSURE_AUTO:
603 return mt9v032_update_aec_agc(mt9v032, MT9V032_AEC_ENABLE,
Kartik Mohta5c375982012-05-02 19:19:08 -0300604 !ctrl->val);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300605
606 case V4L2_CID_EXPOSURE:
607 return mt9v032_write(client, MT9V032_TOTAL_SHUTTER_WIDTH,
608 ctrl->val);
609
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300610 case V4L2_CID_HBLANK:
611 mt9v032->hblank = ctrl->val;
612 return mt9v032_update_hblank(mt9v032);
613
614 case V4L2_CID_VBLANK:
615 return mt9v032_write(client, MT9V032_VERTICAL_BLANKING,
616 ctrl->val);
617
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300618 case V4L2_CID_PIXEL_RATE:
619 case V4L2_CID_LINK_FREQ:
620 if (mt9v032->link_freq == NULL)
621 break;
622
623 freq = mt9v032->pdata->link_freqs[mt9v032->link_freq->val];
624 mt9v032->pixel_rate->val64 = freq;
625 mt9v032->sysclk = freq;
626 break;
627
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300628 case V4L2_CID_TEST_PATTERN:
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300629 switch (mt9v032->test_pattern->val) {
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300630 case 0:
631 data = 0;
632 break;
633 case 1:
634 data = MT9V032_TEST_PATTERN_GRAY_VERTICAL
635 | MT9V032_TEST_PATTERN_ENABLE;
636 break;
637 case 2:
638 data = MT9V032_TEST_PATTERN_GRAY_HORIZONTAL
639 | MT9V032_TEST_PATTERN_ENABLE;
640 break;
641 case 3:
642 data = MT9V032_TEST_PATTERN_GRAY_DIAGONAL
643 | MT9V032_TEST_PATTERN_ENABLE;
644 break;
645 default:
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300646 data = (mt9v032->test_pattern_color->val <<
647 MT9V032_TEST_PATTERN_DATA_SHIFT)
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300648 | MT9V032_TEST_PATTERN_USE_DATA
649 | MT9V032_TEST_PATTERN_ENABLE
650 | MT9V032_TEST_PATTERN_FLIP;
651 break;
652 }
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300653 return mt9v032_write(client, MT9V032_TEST_PATTERN, data);
654 }
655
656 return 0;
657}
658
659static struct v4l2_ctrl_ops mt9v032_ctrl_ops = {
660 .s_ctrl = mt9v032_s_ctrl,
661};
662
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300663static const char * const mt9v032_test_pattern_menu[] = {
664 "Disabled",
665 "Gray Vertical Shade",
666 "Gray Horizontal Shade",
667 "Gray Diagonal Shade",
668 "Plain",
669};
670
671static const struct v4l2_ctrl_config mt9v032_test_pattern_color = {
672 .ops = &mt9v032_ctrl_ops,
673 .id = V4L2_CID_TEST_PATTERN_COLOR,
674 .type = V4L2_CTRL_TYPE_INTEGER,
675 .name = "Test Pattern Color",
676 .min = 0,
677 .max = 1023,
678 .step = 1,
679 .def = 0,
680 .flags = 0,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300681};
682
683/* -----------------------------------------------------------------------------
684 * V4L2 subdev core operations
685 */
686
687static int mt9v032_set_power(struct v4l2_subdev *subdev, int on)
688{
689 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
690 int ret = 0;
691
692 mutex_lock(&mt9v032->power_lock);
693
694 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
695 * update the power state.
696 */
697 if (mt9v032->power_count == !on) {
698 ret = __mt9v032_set_power(mt9v032, !!on);
699 if (ret < 0)
700 goto done;
701 }
702
703 /* Update the power count. */
704 mt9v032->power_count += on ? 1 : -1;
705 WARN_ON(mt9v032->power_count < 0);
706
707done:
708 mutex_unlock(&mt9v032->power_lock);
709 return ret;
710}
711
712/* -----------------------------------------------------------------------------
713 * V4L2 subdev internal operations
714 */
715
716static int mt9v032_registered(struct v4l2_subdev *subdev)
717{
718 struct i2c_client *client = v4l2_get_subdevdata(subdev);
719 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300720 unsigned int i;
721 s32 version;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300722 int ret;
723
724 dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n",
725 client->addr);
726
727 ret = mt9v032_power_on(mt9v032);
728 if (ret < 0) {
729 dev_err(&client->dev, "MT9V032 power up failed\n");
730 return ret;
731 }
732
733 /* Read and check the sensor version */
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300734 version = mt9v032_read(client, MT9V032_CHIP_VERSION);
735 if (version < 0) {
736 dev_err(&client->dev, "Failed reading chip version\n");
737 return version;
738 }
739
740 for (i = 0; i < ARRAY_SIZE(mt9v032_versions); ++i) {
741 if (mt9v032_versions[i].version == version) {
742 mt9v032->version = &mt9v032_versions[i];
743 break;
744 }
745 }
746
747 if (mt9v032->version == NULL) {
748 dev_err(&client->dev, "Unsupported chip version 0x%04x\n",
749 version);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300750 return -ENODEV;
751 }
752
753 mt9v032_power_off(mt9v032);
754
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300755 dev_info(&client->dev, "%s detected at address 0x%02x\n",
756 mt9v032->version->name, client->addr);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300757
Laurent Pinchart637f0052013-12-02 16:39:18 -0300758 mt9v032_configure_pixel_rate(mt9v032);
Sakari Ailus41a33a02012-03-15 18:01:39 -0300759
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300760 return ret;
761}
762
763static int mt9v032_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
764{
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300765 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300766 struct v4l2_mbus_framefmt *format;
767 struct v4l2_rect *crop;
768
769 crop = v4l2_subdev_get_try_crop(fh, 0);
770 crop->left = MT9V032_COLUMN_START_DEF;
771 crop->top = MT9V032_ROW_START_DEF;
772 crop->width = MT9V032_WINDOW_WIDTH_DEF;
773 crop->height = MT9V032_WINDOW_HEIGHT_DEF;
774
775 format = v4l2_subdev_get_try_format(fh, 0);
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300776
777 if (mt9v032->model->color)
778 format->code = V4L2_MBUS_FMT_SGRBG10_1X10;
779 else
780 format->code = V4L2_MBUS_FMT_Y10_1X10;
781
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300782 format->width = MT9V032_WINDOW_WIDTH_DEF;
783 format->height = MT9V032_WINDOW_HEIGHT_DEF;
784 format->field = V4L2_FIELD_NONE;
785 format->colorspace = V4L2_COLORSPACE_SRGB;
786
787 return mt9v032_set_power(subdev, 1);
788}
789
790static int mt9v032_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
791{
792 return mt9v032_set_power(subdev, 0);
793}
794
795static struct v4l2_subdev_core_ops mt9v032_subdev_core_ops = {
796 .s_power = mt9v032_set_power,
797};
798
799static struct v4l2_subdev_video_ops mt9v032_subdev_video_ops = {
800 .s_stream = mt9v032_s_stream,
801};
802
803static struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops = {
804 .enum_mbus_code = mt9v032_enum_mbus_code,
805 .enum_frame_size = mt9v032_enum_frame_size,
806 .get_fmt = mt9v032_get_format,
807 .set_fmt = mt9v032_set_format,
808 .get_crop = mt9v032_get_crop,
809 .set_crop = mt9v032_set_crop,
810};
811
812static struct v4l2_subdev_ops mt9v032_subdev_ops = {
813 .core = &mt9v032_subdev_core_ops,
814 .video = &mt9v032_subdev_video_ops,
815 .pad = &mt9v032_subdev_pad_ops,
816};
817
818static const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops = {
819 .registered = mt9v032_registered,
820 .open = mt9v032_open,
821 .close = mt9v032_close,
822};
823
824/* -----------------------------------------------------------------------------
825 * Driver initialization and probing
826 */
827
828static int mt9v032_probe(struct i2c_client *client,
829 const struct i2c_device_id *did)
830{
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300831 struct mt9v032_platform_data *pdata = client->dev.platform_data;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300832 struct mt9v032 *mt9v032;
833 unsigned int i;
834 int ret;
835
836 if (!i2c_check_functionality(client->adapter,
837 I2C_FUNC_SMBUS_WORD_DATA)) {
838 dev_warn(&client->adapter->dev,
839 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
840 return -EIO;
841 }
842
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300843 mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300844 if (!mt9v032)
845 return -ENOMEM;
846
Laurent Pinchart3300a8f2013-07-05 07:16:02 -0300847 mt9v032->clk = devm_clk_get(&client->dev, NULL);
848 if (IS_ERR(mt9v032->clk))
849 return PTR_ERR(mt9v032->clk);
850
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300851 mutex_init(&mt9v032->power_lock);
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300852 mt9v032->pdata = pdata;
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300853 mt9v032->model = (const void *)did->driver_data;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300854
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300855 v4l2_ctrl_handler_init(&mt9v032->ctrls, 10);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300856
857 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
858 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
859 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
860 V4L2_CID_GAIN, MT9V032_ANALOG_GAIN_MIN,
861 MT9V032_ANALOG_GAIN_MAX, 1, MT9V032_ANALOG_GAIN_DEF);
862 v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops,
863 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
864 V4L2_EXPOSURE_AUTO);
865 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300866 V4L2_CID_EXPOSURE, mt9v032->model->data->min_shutter,
867 mt9v032->model->data->max_shutter, 1,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300868 MT9V032_TOTAL_SHUTTER_WIDTH_DEF);
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300869 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300870 V4L2_CID_HBLANK, mt9v032->model->data->min_hblank,
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300871 MT9V032_HORIZONTAL_BLANKING_MAX, 1,
872 MT9V032_HORIZONTAL_BLANKING_DEF);
873 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
Laurent Pinchart0a466b62013-12-02 11:59:52 -0300874 V4L2_CID_VBLANK, mt9v032->model->data->min_vblank,
875 mt9v032->model->data->max_vblank, 1,
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300876 MT9V032_VERTICAL_BLANKING_DEF);
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300877 mt9v032->test_pattern = v4l2_ctrl_new_std_menu_items(&mt9v032->ctrls,
878 &mt9v032_ctrl_ops, V4L2_CID_TEST_PATTERN,
879 ARRAY_SIZE(mt9v032_test_pattern_menu) - 1, 0, 0,
880 mt9v032_test_pattern_menu);
881 mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls,
882 &mt9v032_test_pattern_color, NULL);
883
884 v4l2_ctrl_cluster(2, &mt9v032->test_pattern);
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300885
Sakari Ailus41a33a02012-03-15 18:01:39 -0300886 mt9v032->pixel_rate =
887 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
888 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300889
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300890 if (pdata && pdata->link_freqs) {
891 unsigned int def = 0;
892
893 for (i = 0; pdata->link_freqs[i]; ++i) {
894 if (pdata->link_freqs[i] == pdata->link_def_freq)
895 def = i;
896 }
897
898 mt9v032->link_freq =
899 v4l2_ctrl_new_int_menu(&mt9v032->ctrls,
900 &mt9v032_ctrl_ops,
901 V4L2_CID_LINK_FREQ, i - 1, def,
902 pdata->link_freqs);
903 v4l2_ctrl_cluster(2, &mt9v032->link_freq);
904 }
905
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300906
907 mt9v032->subdev.ctrl_handler = &mt9v032->ctrls;
908
909 if (mt9v032->ctrls.error)
910 printk(KERN_INFO "%s: control initialization error %d\n",
911 __func__, mt9v032->ctrls.error);
912
913 mt9v032->crop.left = MT9V032_COLUMN_START_DEF;
914 mt9v032->crop.top = MT9V032_ROW_START_DEF;
915 mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF;
916 mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF;
917
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300918 if (mt9v032->model->color)
919 mt9v032->format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
920 else
921 mt9v032->format.code = V4L2_MBUS_FMT_Y10_1X10;
922
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300923 mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF;
924 mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF;
925 mt9v032->format.field = V4L2_FIELD_NONE;
926 mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB;
927
Laurent Pinchart637f0052013-12-02 16:39:18 -0300928 mt9v032->hratio = 1;
929 mt9v032->vratio = 1;
930
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300931 mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE;
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300932 mt9v032->hblank = MT9V032_HORIZONTAL_BLANKING_DEF;
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300933 mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300934
935 v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops);
936 mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops;
937 mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
938
939 mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE;
940 ret = media_entity_init(&mt9v032->subdev.entity, 1, &mt9v032->pad, 0);
Laurent Pinchart94625502013-05-02 08:34:30 -0300941
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300942 if (ret < 0)
Laurent Pinchart94625502013-05-02 08:34:30 -0300943 v4l2_ctrl_handler_free(&mt9v032->ctrls);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300944
945 return ret;
946}
947
948static int mt9v032_remove(struct i2c_client *client)
949{
950 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
951 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
952
Laurent Pinchart94625502013-05-02 08:34:30 -0300953 v4l2_ctrl_handler_free(&mt9v032->ctrls);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300954 v4l2_device_unregister_subdev(subdev);
955 media_entity_cleanup(&subdev->entity);
Laurent Pinchart94625502013-05-02 08:34:30 -0300956
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300957 return 0;
958}
959
960static const struct i2c_device_id mt9v032_id[] = {
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300961 { "mt9v032", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_COLOR] },
962 { "mt9v032m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_MONO] },
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300963 { }
964};
965MODULE_DEVICE_TABLE(i2c, mt9v032_id);
966
967static struct i2c_driver mt9v032_driver = {
968 .driver = {
969 .name = "mt9v032",
970 },
971 .probe = mt9v032_probe,
972 .remove = mt9v032_remove,
973 .id_table = mt9v032_id,
974};
975
Axel Linc6e8d862012-02-12 06:56:32 -0300976module_i2c_driver(mt9v032_driver);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300977
978MODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
979MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
980MODULE_LICENSE("GPL");