blob: a31d6d18d1e5839144b89d2964aae47a7bf095de [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
129struct mt9v032_model_info {
130 bool color;
131};
132
133static const struct mt9v032_model_info mt9v032_models[] = {
134 [MT9V032_MODEL_COLOR] = {
135 .color = true,
136 },
137 [MT9V032_MODEL_MONO] = {
138 .color = false,
139 },
140};
141
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300142struct mt9v032 {
143 struct v4l2_subdev subdev;
144 struct media_pad pad;
145
146 struct v4l2_mbus_framefmt format;
147 struct v4l2_rect crop;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300148 unsigned int hratio;
149 unsigned int vratio;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300150
151 struct v4l2_ctrl_handler ctrls;
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300152 struct {
153 struct v4l2_ctrl *link_freq;
154 struct v4l2_ctrl *pixel_rate;
155 };
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300156
157 struct mutex power_lock;
158 int power_count;
159
Laurent Pinchart3300a8f2013-07-05 07:16:02 -0300160 struct clk *clk;
161
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300162 struct mt9v032_platform_data *pdata;
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300163 const struct mt9v032_model_info *model;
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300164
165 u32 sysclk;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300166 u16 chip_control;
167 u16 aec_agc;
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300168 u16 hblank;
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300169 struct {
170 struct v4l2_ctrl *test_pattern;
171 struct v4l2_ctrl *test_pattern_color;
172 };
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300173};
174
175static struct mt9v032 *to_mt9v032(struct v4l2_subdev *sd)
176{
177 return container_of(sd, struct mt9v032, subdev);
178}
179
180static int mt9v032_read(struct i2c_client *client, const u8 reg)
181{
Jonathan Cameron3f877042011-10-21 09:30:25 -0300182 s32 data = i2c_smbus_read_word_swapped(client, reg);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300183 dev_dbg(&client->dev, "%s: read 0x%04x from 0x%02x\n", __func__,
Jonathan Cameron3f877042011-10-21 09:30:25 -0300184 data, reg);
185 return data;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300186}
187
188static int mt9v032_write(struct i2c_client *client, const u8 reg,
189 const u16 data)
190{
191 dev_dbg(&client->dev, "%s: writing 0x%04x to 0x%02x\n", __func__,
192 data, reg);
Jonathan Cameron3f877042011-10-21 09:30:25 -0300193 return i2c_smbus_write_word_swapped(client, reg, data);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300194}
195
196static int mt9v032_set_chip_control(struct mt9v032 *mt9v032, u16 clear, u16 set)
197{
198 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
199 u16 value = (mt9v032->chip_control & ~clear) | set;
200 int ret;
201
202 ret = mt9v032_write(client, MT9V032_CHIP_CONTROL, value);
203 if (ret < 0)
204 return ret;
205
206 mt9v032->chip_control = value;
207 return 0;
208}
209
210static int
211mt9v032_update_aec_agc(struct mt9v032 *mt9v032, u16 which, int enable)
212{
213 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
214 u16 value = mt9v032->aec_agc;
215 int ret;
216
217 if (enable)
218 value |= which;
219 else
220 value &= ~which;
221
222 ret = mt9v032_write(client, MT9V032_AEC_AGC_ENABLE, value);
223 if (ret < 0)
224 return ret;
225
226 mt9v032->aec_agc = value;
227 return 0;
228}
229
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300230static int
231mt9v032_update_hblank(struct mt9v032 *mt9v032)
232{
233 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
234 struct v4l2_rect *crop = &mt9v032->crop;
235
236 return mt9v032_write(client, MT9V032_HORIZONTAL_BLANKING,
237 max_t(s32, mt9v032->hblank, 660 - crop->width));
238}
239
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300240static int mt9v032_power_on(struct mt9v032 *mt9v032)
241{
242 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
243 int ret;
244
Laurent Pinchart3300a8f2013-07-05 07:16:02 -0300245 clk_set_rate(mt9v032->clk, mt9v032->sysclk);
246 clk_prepare_enable(mt9v032->clk);
247 udelay(1);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300248
249 /* Reset the chip and stop data read out */
250 ret = mt9v032_write(client, MT9V032_RESET, 1);
251 if (ret < 0)
252 return ret;
253
254 ret = mt9v032_write(client, MT9V032_RESET, 0);
255 if (ret < 0)
256 return ret;
257
258 return mt9v032_write(client, MT9V032_CHIP_CONTROL, 0);
259}
260
261static void mt9v032_power_off(struct mt9v032 *mt9v032)
262{
Laurent Pinchart3300a8f2013-07-05 07:16:02 -0300263 clk_disable_unprepare(mt9v032->clk);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300264}
265
266static int __mt9v032_set_power(struct mt9v032 *mt9v032, bool on)
267{
268 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
269 int ret;
270
271 if (!on) {
272 mt9v032_power_off(mt9v032);
273 return 0;
274 }
275
276 ret = mt9v032_power_on(mt9v032);
277 if (ret < 0)
278 return ret;
279
280 /* Configure the pixel clock polarity */
281 if (mt9v032->pdata && mt9v032->pdata->clk_pol) {
282 ret = mt9v032_write(client, MT9V032_PIXEL_CLOCK,
283 MT9V032_PIXEL_CLOCK_INV_PXL_CLK);
284 if (ret < 0)
285 return ret;
286 }
287
288 /* Disable the noise correction algorithm and restore the controls. */
289 ret = mt9v032_write(client, MT9V032_ROW_NOISE_CORR_CONTROL, 0);
290 if (ret < 0)
291 return ret;
292
293 return v4l2_ctrl_handler_setup(&mt9v032->ctrls);
294}
295
296/* -----------------------------------------------------------------------------
297 * V4L2 subdev video operations
298 */
299
300static struct v4l2_mbus_framefmt *
301__mt9v032_get_pad_format(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
302 unsigned int pad, enum v4l2_subdev_format_whence which)
303{
304 switch (which) {
305 case V4L2_SUBDEV_FORMAT_TRY:
306 return v4l2_subdev_get_try_format(fh, pad);
307 case V4L2_SUBDEV_FORMAT_ACTIVE:
308 return &mt9v032->format;
309 default:
310 return NULL;
311 }
312}
313
314static struct v4l2_rect *
315__mt9v032_get_pad_crop(struct mt9v032 *mt9v032, struct v4l2_subdev_fh *fh,
316 unsigned int pad, enum v4l2_subdev_format_whence which)
317{
318 switch (which) {
319 case V4L2_SUBDEV_FORMAT_TRY:
320 return v4l2_subdev_get_try_crop(fh, pad);
321 case V4L2_SUBDEV_FORMAT_ACTIVE:
322 return &mt9v032->crop;
323 default:
324 return NULL;
325 }
326}
327
328static int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable)
329{
330 const u16 mode = MT9V032_CHIP_CONTROL_MASTER_MODE
331 | MT9V032_CHIP_CONTROL_DOUT_ENABLE
332 | MT9V032_CHIP_CONTROL_SEQUENTIAL;
333 struct i2c_client *client = v4l2_get_subdevdata(subdev);
334 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300335 struct v4l2_rect *crop = &mt9v032->crop;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300336 unsigned int hbin;
337 unsigned int vbin;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300338 int ret;
339
340 if (!enable)
341 return mt9v032_set_chip_control(mt9v032, mode, 0);
342
343 /* Configure the window size and row/column bin */
Laurent Pinchart637f0052013-12-02 16:39:18 -0300344 hbin = fls(mt9v032->hratio) - 1;
345 vbin = fls(mt9v032->vratio) - 1;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300346 ret = mt9v032_write(client, MT9V032_READ_MODE,
Laurent Pinchart637f0052013-12-02 16:39:18 -0300347 hbin << MT9V032_READ_MODE_COLUMN_BIN_SHIFT |
348 vbin << MT9V032_READ_MODE_ROW_BIN_SHIFT);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300349 if (ret < 0)
350 return ret;
351
352 ret = mt9v032_write(client, MT9V032_COLUMN_START, crop->left);
353 if (ret < 0)
354 return ret;
355
356 ret = mt9v032_write(client, MT9V032_ROW_START, crop->top);
357 if (ret < 0)
358 return ret;
359
360 ret = mt9v032_write(client, MT9V032_WINDOW_WIDTH, crop->width);
361 if (ret < 0)
362 return ret;
363
364 ret = mt9v032_write(client, MT9V032_WINDOW_HEIGHT, crop->height);
365 if (ret < 0)
366 return ret;
367
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300368 ret = mt9v032_update_hblank(mt9v032);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300369 if (ret < 0)
370 return ret;
371
372 /* Switch to master "normal" mode */
373 return mt9v032_set_chip_control(mt9v032, 0, mode);
374}
375
376static int mt9v032_enum_mbus_code(struct v4l2_subdev *subdev,
377 struct v4l2_subdev_fh *fh,
378 struct v4l2_subdev_mbus_code_enum *code)
379{
380 if (code->index > 0)
381 return -EINVAL;
382
383 code->code = V4L2_MBUS_FMT_SGRBG10_1X10;
384 return 0;
385}
386
387static int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
388 struct v4l2_subdev_fh *fh,
389 struct v4l2_subdev_frame_size_enum *fse)
390{
Laurent Pinchart637f0052013-12-02 16:39:18 -0300391 if (fse->index >= 3 || fse->code != V4L2_MBUS_FMT_SGRBG10_1X10)
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300392 return -EINVAL;
393
Laurent Pinchart637f0052013-12-02 16:39:18 -0300394 fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300395 fse->max_width = fse->min_width;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300396 fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / (1 << fse->index);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300397 fse->max_height = fse->min_height;
398
399 return 0;
400}
401
402static int mt9v032_get_format(struct v4l2_subdev *subdev,
403 struct v4l2_subdev_fh *fh,
404 struct v4l2_subdev_format *format)
405{
406 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
407
408 format->format = *__mt9v032_get_pad_format(mt9v032, fh, format->pad,
409 format->which);
410 return 0;
411}
412
Laurent Pinchart637f0052013-12-02 16:39:18 -0300413static void mt9v032_configure_pixel_rate(struct mt9v032 *mt9v032)
Sakari Ailus41a33a02012-03-15 18:01:39 -0300414{
415 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
416 int ret;
417
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300418 ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate,
Laurent Pinchart637f0052013-12-02 16:39:18 -0300419 mt9v032->sysclk / mt9v032->hratio);
Sakari Ailus41a33a02012-03-15 18:01:39 -0300420 if (ret < 0)
421 dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret);
422}
423
Laurent Pinchart637f0052013-12-02 16:39:18 -0300424static unsigned int mt9v032_calc_ratio(unsigned int input, unsigned int output)
425{
426 /* Compute the power-of-two binning factor closest to the input size to
427 * output size ratio. Given that the output size is bounded by input/4
428 * and input, a generic implementation would be an ineffective luxury.
429 */
430 if (output * 3 > input * 2)
431 return 1;
432 if (output * 3 > input)
433 return 2;
434 return 4;
435}
436
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300437static int mt9v032_set_format(struct v4l2_subdev *subdev,
438 struct v4l2_subdev_fh *fh,
439 struct v4l2_subdev_format *format)
440{
441 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
442 struct v4l2_mbus_framefmt *__format;
443 struct v4l2_rect *__crop;
444 unsigned int width;
445 unsigned int height;
446 unsigned int hratio;
447 unsigned int vratio;
448
449 __crop = __mt9v032_get_pad_crop(mt9v032, fh, format->pad,
450 format->which);
451
452 /* Clamp the width and height to avoid dividing by zero. */
453 width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
Laurent Pinchart637f0052013-12-02 16:39:18 -0300454 max(__crop->width / 4, MT9V032_WINDOW_WIDTH_MIN),
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300455 __crop->width);
456 height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
Laurent Pinchart637f0052013-12-02 16:39:18 -0300457 max(__crop->height / 4, MT9V032_WINDOW_HEIGHT_MIN),
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300458 __crop->height);
459
Laurent Pinchart637f0052013-12-02 16:39:18 -0300460 hratio = mt9v032_calc_ratio(__crop->width, width);
461 vratio = mt9v032_calc_ratio(__crop->height, height);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300462
463 __format = __mt9v032_get_pad_format(mt9v032, fh, format->pad,
464 format->which);
465 __format->width = __crop->width / hratio;
466 __format->height = __crop->height / vratio;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300467
468 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
469 mt9v032->hratio = hratio;
470 mt9v032->vratio = vratio;
471 mt9v032_configure_pixel_rate(mt9v032);
472 }
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300473
474 format->format = *__format;
475
476 return 0;
477}
478
479static int mt9v032_get_crop(struct v4l2_subdev *subdev,
480 struct v4l2_subdev_fh *fh,
481 struct v4l2_subdev_crop *crop)
482{
483 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
484
485 crop->rect = *__mt9v032_get_pad_crop(mt9v032, fh, crop->pad,
486 crop->which);
487 return 0;
488}
489
490static int mt9v032_set_crop(struct v4l2_subdev *subdev,
491 struct v4l2_subdev_fh *fh,
492 struct v4l2_subdev_crop *crop)
493{
494 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
495 struct v4l2_mbus_framefmt *__format;
496 struct v4l2_rect *__crop;
497 struct v4l2_rect rect;
498
Laurent Pinchart86cf7862010-11-28 15:07:20 -0300499 /* Clamp the crop rectangle boundaries and align them to a non multiple
500 * of 2 pixels to ensure a GRBG Bayer pattern.
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300501 */
Laurent Pinchart86cf7862010-11-28 15:07:20 -0300502 rect.left = clamp(ALIGN(crop->rect.left + 1, 2) - 1,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300503 MT9V032_COLUMN_START_MIN,
504 MT9V032_COLUMN_START_MAX);
Laurent Pinchart86cf7862010-11-28 15:07:20 -0300505 rect.top = clamp(ALIGN(crop->rect.top + 1, 2) - 1,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300506 MT9V032_ROW_START_MIN,
507 MT9V032_ROW_START_MAX);
508 rect.width = clamp(ALIGN(crop->rect.width, 2),
509 MT9V032_WINDOW_WIDTH_MIN,
510 MT9V032_WINDOW_WIDTH_MAX);
511 rect.height = clamp(ALIGN(crop->rect.height, 2),
512 MT9V032_WINDOW_HEIGHT_MIN,
513 MT9V032_WINDOW_HEIGHT_MAX);
514
515 rect.width = min(rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left);
516 rect.height = min(rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top);
517
518 __crop = __mt9v032_get_pad_crop(mt9v032, fh, crop->pad, crop->which);
519
520 if (rect.width != __crop->width || rect.height != __crop->height) {
521 /* Reset the output image size if the crop rectangle size has
522 * been modified.
523 */
524 __format = __mt9v032_get_pad_format(mt9v032, fh, crop->pad,
525 crop->which);
526 __format->width = rect.width;
527 __format->height = rect.height;
Laurent Pinchart637f0052013-12-02 16:39:18 -0300528 if (crop->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
529 mt9v032->hratio = 1;
530 mt9v032->vratio = 1;
531 mt9v032_configure_pixel_rate(mt9v032);
532 }
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300533 }
534
535 *__crop = rect;
536 crop->rect = rect;
537
538 return 0;
539}
540
541/* -----------------------------------------------------------------------------
542 * V4L2 subdev control operations
543 */
544
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300545#define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300546
547static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
548{
549 struct mt9v032 *mt9v032 =
550 container_of(ctrl->handler, struct mt9v032, ctrls);
551 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300552 u32 freq;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300553 u16 data;
554
555 switch (ctrl->id) {
556 case V4L2_CID_AUTOGAIN:
557 return mt9v032_update_aec_agc(mt9v032, MT9V032_AGC_ENABLE,
558 ctrl->val);
559
560 case V4L2_CID_GAIN:
561 return mt9v032_write(client, MT9V032_ANALOG_GAIN, ctrl->val);
562
563 case V4L2_CID_EXPOSURE_AUTO:
564 return mt9v032_update_aec_agc(mt9v032, MT9V032_AEC_ENABLE,
Kartik Mohta5c375982012-05-02 19:19:08 -0300565 !ctrl->val);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300566
567 case V4L2_CID_EXPOSURE:
568 return mt9v032_write(client, MT9V032_TOTAL_SHUTTER_WIDTH,
569 ctrl->val);
570
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300571 case V4L2_CID_HBLANK:
572 mt9v032->hblank = ctrl->val;
573 return mt9v032_update_hblank(mt9v032);
574
575 case V4L2_CID_VBLANK:
576 return mt9v032_write(client, MT9V032_VERTICAL_BLANKING,
577 ctrl->val);
578
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300579 case V4L2_CID_PIXEL_RATE:
580 case V4L2_CID_LINK_FREQ:
581 if (mt9v032->link_freq == NULL)
582 break;
583
584 freq = mt9v032->pdata->link_freqs[mt9v032->link_freq->val];
585 mt9v032->pixel_rate->val64 = freq;
586 mt9v032->sysclk = freq;
587 break;
588
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300589 case V4L2_CID_TEST_PATTERN:
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300590 switch (mt9v032->test_pattern->val) {
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300591 case 0:
592 data = 0;
593 break;
594 case 1:
595 data = MT9V032_TEST_PATTERN_GRAY_VERTICAL
596 | MT9V032_TEST_PATTERN_ENABLE;
597 break;
598 case 2:
599 data = MT9V032_TEST_PATTERN_GRAY_HORIZONTAL
600 | MT9V032_TEST_PATTERN_ENABLE;
601 break;
602 case 3:
603 data = MT9V032_TEST_PATTERN_GRAY_DIAGONAL
604 | MT9V032_TEST_PATTERN_ENABLE;
605 break;
606 default:
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300607 data = (mt9v032->test_pattern_color->val <<
608 MT9V032_TEST_PATTERN_DATA_SHIFT)
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300609 | MT9V032_TEST_PATTERN_USE_DATA
610 | MT9V032_TEST_PATTERN_ENABLE
611 | MT9V032_TEST_PATTERN_FLIP;
612 break;
613 }
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300614 return mt9v032_write(client, MT9V032_TEST_PATTERN, data);
615 }
616
617 return 0;
618}
619
620static struct v4l2_ctrl_ops mt9v032_ctrl_ops = {
621 .s_ctrl = mt9v032_s_ctrl,
622};
623
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300624static const char * const mt9v032_test_pattern_menu[] = {
625 "Disabled",
626 "Gray Vertical Shade",
627 "Gray Horizontal Shade",
628 "Gray Diagonal Shade",
629 "Plain",
630};
631
632static const struct v4l2_ctrl_config mt9v032_test_pattern_color = {
633 .ops = &mt9v032_ctrl_ops,
634 .id = V4L2_CID_TEST_PATTERN_COLOR,
635 .type = V4L2_CTRL_TYPE_INTEGER,
636 .name = "Test Pattern Color",
637 .min = 0,
638 .max = 1023,
639 .step = 1,
640 .def = 0,
641 .flags = 0,
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300642};
643
644/* -----------------------------------------------------------------------------
645 * V4L2 subdev core operations
646 */
647
648static int mt9v032_set_power(struct v4l2_subdev *subdev, int on)
649{
650 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
651 int ret = 0;
652
653 mutex_lock(&mt9v032->power_lock);
654
655 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
656 * update the power state.
657 */
658 if (mt9v032->power_count == !on) {
659 ret = __mt9v032_set_power(mt9v032, !!on);
660 if (ret < 0)
661 goto done;
662 }
663
664 /* Update the power count. */
665 mt9v032->power_count += on ? 1 : -1;
666 WARN_ON(mt9v032->power_count < 0);
667
668done:
669 mutex_unlock(&mt9v032->power_lock);
670 return ret;
671}
672
673/* -----------------------------------------------------------------------------
674 * V4L2 subdev internal operations
675 */
676
677static int mt9v032_registered(struct v4l2_subdev *subdev)
678{
679 struct i2c_client *client = v4l2_get_subdevdata(subdev);
680 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
681 s32 data;
682 int ret;
683
684 dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n",
685 client->addr);
686
687 ret = mt9v032_power_on(mt9v032);
688 if (ret < 0) {
689 dev_err(&client->dev, "MT9V032 power up failed\n");
690 return ret;
691 }
692
693 /* Read and check the sensor version */
694 data = mt9v032_read(client, MT9V032_CHIP_VERSION);
695 if (data != MT9V032_CHIP_ID_REV1 && data != MT9V032_CHIP_ID_REV3) {
696 dev_err(&client->dev, "MT9V032 not detected, wrong version "
697 "0x%04x\n", data);
698 return -ENODEV;
699 }
700
701 mt9v032_power_off(mt9v032);
702
703 dev_info(&client->dev, "MT9V032 detected at address 0x%02x\n",
704 client->addr);
705
Laurent Pinchart637f0052013-12-02 16:39:18 -0300706 mt9v032_configure_pixel_rate(mt9v032);
Sakari Ailus41a33a02012-03-15 18:01:39 -0300707
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300708 return ret;
709}
710
711static int mt9v032_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
712{
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300713 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300714 struct v4l2_mbus_framefmt *format;
715 struct v4l2_rect *crop;
716
717 crop = v4l2_subdev_get_try_crop(fh, 0);
718 crop->left = MT9V032_COLUMN_START_DEF;
719 crop->top = MT9V032_ROW_START_DEF;
720 crop->width = MT9V032_WINDOW_WIDTH_DEF;
721 crop->height = MT9V032_WINDOW_HEIGHT_DEF;
722
723 format = v4l2_subdev_get_try_format(fh, 0);
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300724
725 if (mt9v032->model->color)
726 format->code = V4L2_MBUS_FMT_SGRBG10_1X10;
727 else
728 format->code = V4L2_MBUS_FMT_Y10_1X10;
729
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300730 format->width = MT9V032_WINDOW_WIDTH_DEF;
731 format->height = MT9V032_WINDOW_HEIGHT_DEF;
732 format->field = V4L2_FIELD_NONE;
733 format->colorspace = V4L2_COLORSPACE_SRGB;
734
735 return mt9v032_set_power(subdev, 1);
736}
737
738static int mt9v032_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
739{
740 return mt9v032_set_power(subdev, 0);
741}
742
743static struct v4l2_subdev_core_ops mt9v032_subdev_core_ops = {
744 .s_power = mt9v032_set_power,
745};
746
747static struct v4l2_subdev_video_ops mt9v032_subdev_video_ops = {
748 .s_stream = mt9v032_s_stream,
749};
750
751static struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops = {
752 .enum_mbus_code = mt9v032_enum_mbus_code,
753 .enum_frame_size = mt9v032_enum_frame_size,
754 .get_fmt = mt9v032_get_format,
755 .set_fmt = mt9v032_set_format,
756 .get_crop = mt9v032_get_crop,
757 .set_crop = mt9v032_set_crop,
758};
759
760static struct v4l2_subdev_ops mt9v032_subdev_ops = {
761 .core = &mt9v032_subdev_core_ops,
762 .video = &mt9v032_subdev_video_ops,
763 .pad = &mt9v032_subdev_pad_ops,
764};
765
766static const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops = {
767 .registered = mt9v032_registered,
768 .open = mt9v032_open,
769 .close = mt9v032_close,
770};
771
772/* -----------------------------------------------------------------------------
773 * Driver initialization and probing
774 */
775
776static int mt9v032_probe(struct i2c_client *client,
777 const struct i2c_device_id *did)
778{
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300779 struct mt9v032_platform_data *pdata = client->dev.platform_data;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300780 struct mt9v032 *mt9v032;
781 unsigned int i;
782 int ret;
783
784 if (!i2c_check_functionality(client->adapter,
785 I2C_FUNC_SMBUS_WORD_DATA)) {
786 dev_warn(&client->adapter->dev,
787 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
788 return -EIO;
789 }
790
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300791 mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300792 if (!mt9v032)
793 return -ENOMEM;
794
Laurent Pinchart3300a8f2013-07-05 07:16:02 -0300795 mt9v032->clk = devm_clk_get(&client->dev, NULL);
796 if (IS_ERR(mt9v032->clk))
797 return PTR_ERR(mt9v032->clk);
798
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300799 mutex_init(&mt9v032->power_lock);
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300800 mt9v032->pdata = pdata;
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300801 mt9v032->model = (const void *)did->driver_data;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300802
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300803 v4l2_ctrl_handler_init(&mt9v032->ctrls, 10);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300804
805 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
806 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
807 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
808 V4L2_CID_GAIN, MT9V032_ANALOG_GAIN_MIN,
809 MT9V032_ANALOG_GAIN_MAX, 1, MT9V032_ANALOG_GAIN_DEF);
810 v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops,
811 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
812 V4L2_EXPOSURE_AUTO);
813 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
814 V4L2_CID_EXPOSURE, MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
815 MT9V032_TOTAL_SHUTTER_WIDTH_MAX, 1,
816 MT9V032_TOTAL_SHUTTER_WIDTH_DEF);
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300817 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
818 V4L2_CID_HBLANK, MT9V032_HORIZONTAL_BLANKING_MIN,
819 MT9V032_HORIZONTAL_BLANKING_MAX, 1,
820 MT9V032_HORIZONTAL_BLANKING_DEF);
821 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
822 V4L2_CID_VBLANK, MT9V032_VERTICAL_BLANKING_MIN,
823 MT9V032_VERTICAL_BLANKING_MAX, 1,
824 MT9V032_VERTICAL_BLANKING_DEF);
Lad, Prabhakarb28d7012012-09-25 09:35:43 -0300825 mt9v032->test_pattern = v4l2_ctrl_new_std_menu_items(&mt9v032->ctrls,
826 &mt9v032_ctrl_ops, V4L2_CID_TEST_PATTERN,
827 ARRAY_SIZE(mt9v032_test_pattern_menu) - 1, 0, 0,
828 mt9v032_test_pattern_menu);
829 mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls,
830 &mt9v032_test_pattern_color, NULL);
831
832 v4l2_ctrl_cluster(2, &mt9v032->test_pattern);
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300833
Sakari Ailus41a33a02012-03-15 18:01:39 -0300834 mt9v032->pixel_rate =
835 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
836 V4L2_CID_PIXEL_RATE, 0, 0, 1, 0);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300837
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300838 if (pdata && pdata->link_freqs) {
839 unsigned int def = 0;
840
841 for (i = 0; pdata->link_freqs[i]; ++i) {
842 if (pdata->link_freqs[i] == pdata->link_def_freq)
843 def = i;
844 }
845
846 mt9v032->link_freq =
847 v4l2_ctrl_new_int_menu(&mt9v032->ctrls,
848 &mt9v032_ctrl_ops,
849 V4L2_CID_LINK_FREQ, i - 1, def,
850 pdata->link_freqs);
851 v4l2_ctrl_cluster(2, &mt9v032->link_freq);
852 }
853
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300854
855 mt9v032->subdev.ctrl_handler = &mt9v032->ctrls;
856
857 if (mt9v032->ctrls.error)
858 printk(KERN_INFO "%s: control initialization error %d\n",
859 __func__, mt9v032->ctrls.error);
860
861 mt9v032->crop.left = MT9V032_COLUMN_START_DEF;
862 mt9v032->crop.top = MT9V032_ROW_START_DEF;
863 mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF;
864 mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF;
865
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300866 if (mt9v032->model->color)
867 mt9v032->format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
868 else
869 mt9v032->format.code = V4L2_MBUS_FMT_Y10_1X10;
870
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300871 mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF;
872 mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF;
873 mt9v032->format.field = V4L2_FIELD_NONE;
874 mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB;
875
Laurent Pinchart637f0052013-12-02 16:39:18 -0300876 mt9v032->hratio = 1;
877 mt9v032->vratio = 1;
878
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300879 mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE;
Laurent Pinchart9ec670e2012-07-23 15:03:34 -0300880 mt9v032->hblank = MT9V032_HORIZONTAL_BLANKING_DEF;
Laurent Pincharte9a50e42012-07-26 08:02:50 -0300881 mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF;
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300882
883 v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops);
884 mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops;
885 mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
886
887 mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE;
888 ret = media_entity_init(&mt9v032->subdev.entity, 1, &mt9v032->pad, 0);
Laurent Pinchart94625502013-05-02 08:34:30 -0300889
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300890 if (ret < 0)
Laurent Pinchart94625502013-05-02 08:34:30 -0300891 v4l2_ctrl_handler_free(&mt9v032->ctrls);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300892
893 return ret;
894}
895
896static int mt9v032_remove(struct i2c_client *client)
897{
898 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
899 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
900
Laurent Pinchart94625502013-05-02 08:34:30 -0300901 v4l2_ctrl_handler_free(&mt9v032->ctrls);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300902 v4l2_device_unregister_subdev(subdev);
903 media_entity_cleanup(&subdev->entity);
Laurent Pinchart94625502013-05-02 08:34:30 -0300904
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300905 return 0;
906}
907
908static const struct i2c_device_id mt9v032_id[] = {
Laurent Pinchart220ddc72013-12-04 15:31:13 -0300909 { "mt9v032", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_COLOR] },
910 { "mt9v032m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_MONO] },
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300911 { }
912};
913MODULE_DEVICE_TABLE(i2c, mt9v032_id);
914
915static struct i2c_driver mt9v032_driver = {
916 .driver = {
917 .name = "mt9v032",
918 },
919 .probe = mt9v032_probe,
920 .remove = mt9v032_remove,
921 .id_table = mt9v032_id,
922};
923
Axel Linc6e8d862012-02-12 06:56:32 -0300924module_i2c_driver(mt9v032_driver);
Detlev Casanova0f2ce162011-04-05 09:06:21 -0300925
926MODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
927MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
928MODULE_LICENSE("GPL");