blob: 73b7688cbebd32de12403178f72ff71cd645fc9b [file] [log] [blame]
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -03001/*
2 * mt9v011 -Micron 1/4-Inch VGA Digital Image Sensor
3 *
4 * Copyright (c) 2009 Mauro Carvalho Chehab (mchehab@redhat.com)
5 * This code is placed under the terms of the GNU General Public License v2
6 */
7
8#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030010#include <linux/videodev2.h>
11#include <linux/delay.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040012#include <linux/module.h>
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -030013#include <asm/div64.h>
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030014#include <media/v4l2-device.h>
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030015#include <media/v4l2-chip-ident.h>
Hans Verkuilea01a832012-09-07 05:42:15 -030016#include <media/v4l2-ctrls.h>
Hans Verkuil3c7c9372011-01-08 07:08:02 -030017#include <media/mt9v011.h>
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030018
19MODULE_DESCRIPTION("Micron mt9v011 sensor driver");
20MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
21MODULE_LICENSE("GPL");
22
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030023static int debug;
24module_param(debug, int, 0);
25MODULE_PARM_DESC(debug, "Debug level (0-2)");
26
Hans Verkuil3c7c9372011-01-08 07:08:02 -030027#define R00_MT9V011_CHIP_VERSION 0x00
28#define R01_MT9V011_ROWSTART 0x01
29#define R02_MT9V011_COLSTART 0x02
30#define R03_MT9V011_HEIGHT 0x03
31#define R04_MT9V011_WIDTH 0x04
32#define R05_MT9V011_HBLANK 0x05
33#define R06_MT9V011_VBLANK 0x06
34#define R07_MT9V011_OUT_CTRL 0x07
35#define R09_MT9V011_SHUTTER_WIDTH 0x09
36#define R0A_MT9V011_CLK_SPEED 0x0a
37#define R0B_MT9V011_RESTART 0x0b
38#define R0C_MT9V011_SHUTTER_DELAY 0x0c
39#define R0D_MT9V011_RESET 0x0d
40#define R1E_MT9V011_DIGITAL_ZOOM 0x1e
41#define R20_MT9V011_READ_MODE 0x20
42#define R2B_MT9V011_GREEN_1_GAIN 0x2b
43#define R2C_MT9V011_BLUE_GAIN 0x2c
44#define R2D_MT9V011_RED_GAIN 0x2d
45#define R2E_MT9V011_GREEN_2_GAIN 0x2e
46#define R35_MT9V011_GLOBAL_GAIN 0x35
47#define RF1_MT9V011_CHIP_ENABLE 0xf1
48
49#define MT9V011_VERSION 0x8232
50#define MT9V011_REV_B_VERSION 0x8243
51
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030052struct mt9v011 {
53 struct v4l2_subdev sd;
Hans Verkuilea01a832012-09-07 05:42:15 -030054 struct v4l2_ctrl_handler ctrls;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -030055 unsigned width, height;
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -030056 unsigned xtal;
Mauro Carvalho Chehab2526ea62009-08-07 01:09:54 -030057 unsigned hflip:1;
58 unsigned vflip:1;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030059
Johannes Obermaier32127362011-06-02 13:03:41 -030060 u16 global_gain, exposure;
61 s16 red_bal, blue_bal;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030062};
63
64static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd)
65{
66 return container_of(sd, struct mt9v011, sd);
67}
68
69static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr)
70{
71 struct i2c_client *c = v4l2_get_subdevdata(sd);
72 __be16 buffer;
73 int rc, val;
74
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -030075 rc = i2c_master_send(c, &addr, 1);
76 if (rc != 1)
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030077 v4l2_dbg(0, debug, sd,
78 "i2c i/o error: rc == %d (should be 1)\n", rc);
79
80 msleep(10);
81
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -030082 rc = i2c_master_recv(c, (char *)&buffer, 2);
83 if (rc != 2)
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030084 v4l2_dbg(0, debug, sd,
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -030085 "i2c i/o error: rc == %d (should be 2)\n", rc);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -030086
87 val = be16_to_cpu(buffer);
88
89 v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val);
90
91 return val;
92}
93
94static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr,
95 u16 value)
96{
97 struct i2c_client *c = v4l2_get_subdevdata(sd);
98 unsigned char buffer[3];
99 int rc;
100
101 buffer[0] = addr;
102 buffer[1] = value >> 8;
103 buffer[2] = value & 0xff;
104
105 v4l2_dbg(2, debug, sd,
106 "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300107 rc = i2c_master_send(c, buffer, 3);
Mauro Carvalho Chehabfbe28002009-06-29 11:12:05 -0300108 if (rc != 3)
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300109 v4l2_dbg(0, debug, sd,
110 "i2c i/o error: rc == %d (should be 3)\n", rc);
111}
112
113
114struct i2c_reg_value {
115 unsigned char reg;
116 u16 value;
117};
118
119/*
120 * Values used at the original driver
121 * Some values are marked as Reserved at the datasheet
122 */
123static const struct i2c_reg_value mt9v011_init_default[] = {
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300124 { R0D_MT9V011_RESET, 0x0001 },
125 { R0D_MT9V011_RESET, 0x0000 },
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300126
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300127 { R0C_MT9V011_SHUTTER_DELAY, 0x0000 },
Mauro Carvalho Chehab6934e6f2009-07-04 08:15:11 -0300128 { R09_MT9V011_SHUTTER_WIDTH, 0x1fc },
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300129
Mauro Carvalho Chehab6934e6f2009-07-04 08:15:11 -0300130 { R0A_MT9V011_CLK_SPEED, 0x0000 },
131 { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 },
Mauro Carvalho Chehabafe09f82009-06-29 11:03:22 -0300132
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300133 { R07_MT9V011_OUT_CTRL, 0x0002 }, /* chip enable */
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300134};
135
Johannes Obermaier32127362011-06-02 13:03:41 -0300136
137static u16 calc_mt9v011_gain(s16 lineargain)
138{
139
140 u16 digitalgain = 0;
141 u16 analogmult = 0;
142 u16 analoginit = 0;
143
144 if (lineargain < 0)
145 lineargain = 0;
146
147 /* recommended minimum */
148 lineargain += 0x0020;
149
150 if (lineargain > 2047)
151 lineargain = 2047;
152
153 if (lineargain > 1023) {
154 digitalgain = 3;
155 analogmult = 3;
156 analoginit = lineargain / 16;
157 } else if (lineargain > 511) {
158 digitalgain = 1;
159 analogmult = 3;
160 analoginit = lineargain / 8;
161 } else if (lineargain > 255) {
162 analogmult = 3;
163 analoginit = lineargain / 4;
164 } else if (lineargain > 127) {
165 analogmult = 1;
166 analoginit = lineargain / 2;
167 } else
168 analoginit = lineargain;
169
170 return analoginit + (analogmult << 7) + (digitalgain << 9);
171
172}
173
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300174static void set_balance(struct v4l2_subdev *sd)
175{
176 struct mt9v011 *core = to_mt9v011(sd);
Johannes Obermaier32127362011-06-02 13:03:41 -0300177 u16 green_gain, blue_gain, red_gain;
Johannes Obermaier590929f2011-06-02 12:43:14 -0300178 u16 exposure;
Johannes Obermaier32127362011-06-02 13:03:41 -0300179 s16 bal;
Johannes Obermaier590929f2011-06-02 12:43:14 -0300180
181 exposure = core->exposure;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300182
Johannes Obermaier32127362011-06-02 13:03:41 -0300183 green_gain = calc_mt9v011_gain(core->global_gain);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300184
Johannes Obermaier32127362011-06-02 13:03:41 -0300185 bal = core->global_gain;
186 bal += (core->blue_bal * core->global_gain / (1 << 7));
187 blue_gain = calc_mt9v011_gain(bal);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300188
Johannes Obermaier32127362011-06-02 13:03:41 -0300189 bal = core->global_gain;
190 bal += (core->red_bal * core->global_gain / (1 << 7));
191 red_gain = calc_mt9v011_gain(bal);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300192
Johannes Obermaier32127362011-06-02 13:03:41 -0300193 mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green_gain);
194 mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green_gain);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300195 mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain);
196 mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain);
Johannes Obermaier590929f2011-06-02 12:43:14 -0300197 mt9v011_write(sd, R09_MT9V011_SHUTTER_WIDTH, exposure);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300198}
199
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300200static void calc_fps(struct v4l2_subdev *sd, u32 *numerator, u32 *denominator)
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300201{
202 struct mt9v011 *core = to_mt9v011(sd);
203 unsigned height, width, hblank, vblank, speed;
204 unsigned row_time, t_time;
205 u64 frames_per_ms;
206 unsigned tmp;
207
208 height = mt9v011_read(sd, R03_MT9V011_HEIGHT);
209 width = mt9v011_read(sd, R04_MT9V011_WIDTH);
210 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK);
211 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK);
212 speed = mt9v011_read(sd, R0A_MT9V011_CLK_SPEED);
213
214 row_time = (width + 113 + hblank) * (speed + 2);
215 t_time = row_time * (height + vblank + 1);
216
217 frames_per_ms = core->xtal * 1000l;
218 do_div(frames_per_ms, t_time);
219 tmp = frames_per_ms;
220
221 v4l2_dbg(1, debug, sd, "Programmed to %u.%03u fps (%d pixel clcks)\n",
222 tmp / 1000, tmp % 1000, t_time);
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300223
224 if (numerator && denominator) {
225 *numerator = 1000;
226 *denominator = (u32)frames_per_ms;
227 }
228}
229
230static u16 calc_speed(struct v4l2_subdev *sd, u32 numerator, u32 denominator)
231{
232 struct mt9v011 *core = to_mt9v011(sd);
233 unsigned height, width, hblank, vblank;
234 unsigned row_time, line_time;
235 u64 t_time, speed;
236
237 /* Avoid bogus calculus */
238 if (!numerator || !denominator)
239 return 0;
240
241 height = mt9v011_read(sd, R03_MT9V011_HEIGHT);
242 width = mt9v011_read(sd, R04_MT9V011_WIDTH);
243 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK);
244 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK);
245
246 row_time = width + 113 + hblank;
247 line_time = height + vblank + 1;
248
249 t_time = core->xtal * ((u64)numerator);
250 /* round to the closest value */
251 t_time += denominator / 2;
252 do_div(t_time, denominator);
253
254 speed = t_time;
255 do_div(speed, row_time * line_time);
256
257 /* Avoid having a negative value for speed */
258 if (speed < 2)
259 speed = 0;
260 else
261 speed -= 2;
262
263 /* Avoid speed overflow */
264 if (speed > 15)
265 return 15;
266
267 return (u16)speed;
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300268}
269
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300270static void set_res(struct v4l2_subdev *sd)
271{
272 struct mt9v011 *core = to_mt9v011(sd);
273 unsigned vstart, hstart;
274
275 /*
276 * The mt9v011 doesn't have scaling. So, in order to select the desired
277 * resolution, we're cropping at the middle of the sensor.
278 * hblank and vblank should be adjusted, in order to warrant that
279 * we'll preserve the line timings for 30 fps, no matter what resolution
280 * is selected.
Mauro Carvalho Chehab6934e6f2009-07-04 08:15:11 -0300281 * NOTE: datasheet says that width (and height) should be filled with
282 * width-1. However, this doesn't work, since one pixel per line will
283 * be missing.
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300284 */
285
Johannes Obermaier1048af22011-06-02 12:17:51 -0300286 hstart = 20 + (640 - core->width) / 2;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300287 mt9v011_write(sd, R02_MT9V011_COLSTART, hstart);
288 mt9v011_write(sd, R04_MT9V011_WIDTH, core->width);
289 mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width);
290
Mauro Carvalho Chehabc1806042009-07-14 02:39:19 -0300291 vstart = 8 + (480 - core->height) / 2;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300292 mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart);
293 mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height);
294 mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height);
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300295
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300296 calc_fps(sd, NULL, NULL);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300297};
298
Mauro Carvalho Chehab2526ea62009-08-07 01:09:54 -0300299static void set_read_mode(struct v4l2_subdev *sd)
300{
301 struct mt9v011 *core = to_mt9v011(sd);
302 unsigned mode = 0x1000;
303
304 if (core->hflip)
305 mode |= 0x4000;
306
307 if (core->vflip)
308 mode |= 0x8000;
309
310 mt9v011_write(sd, R20_MT9V011_READ_MODE, mode);
311}
312
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300313static int mt9v011_reset(struct v4l2_subdev *sd, u32 val)
314{
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300315 int i;
316
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300317 for (i = 0; i < ARRAY_SIZE(mt9v011_init_default); i++)
318 mt9v011_write(sd, mt9v011_init_default[i].reg,
319 mt9v011_init_default[i].value);
320
321 set_balance(sd);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300322 set_res(sd);
Mauro Carvalho Chehab2526ea62009-08-07 01:09:54 -0300323 set_read_mode(sd);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300324
325 return 0;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300326}
327
Hans Verkuilea01b112010-05-09 10:19:25 -0300328static int mt9v011_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
329 enum v4l2_mbus_pixelcode *code)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300330{
Hans Verkuilea01b112010-05-09 10:19:25 -0300331 if (index > 0)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300332 return -EINVAL;
333
Hans Verkuilea01b112010-05-09 10:19:25 -0300334 *code = V4L2_MBUS_FMT_SGRBG8_1X8;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300335 return 0;
336}
337
Hans Verkuilea01b112010-05-09 10:19:25 -0300338static int mt9v011_try_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300339{
Hans Verkuilea01b112010-05-09 10:19:25 -0300340 if (fmt->code != V4L2_MBUS_FMT_SGRBG8_1X8)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300341 return -EINVAL;
342
Hans Verkuilea01b112010-05-09 10:19:25 -0300343 v4l_bound_align_image(&fmt->width, 48, 639, 1,
344 &fmt->height, 32, 480, 1, 0);
345 fmt->field = V4L2_FIELD_NONE;
346 fmt->colorspace = V4L2_COLORSPACE_SRGB;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300347
348 return 0;
349}
350
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300351static int mt9v011_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
352{
353 struct v4l2_captureparm *cp = &parms->parm.capture;
354
355 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
356 return -EINVAL;
357
358 memset(cp, 0, sizeof(struct v4l2_captureparm));
359 cp->capability = V4L2_CAP_TIMEPERFRAME;
360 calc_fps(sd,
361 &cp->timeperframe.numerator,
362 &cp->timeperframe.denominator);
363
364 return 0;
365}
366
367static int mt9v011_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
368{
369 struct v4l2_captureparm *cp = &parms->parm.capture;
370 struct v4l2_fract *tpf = &cp->timeperframe;
371 u16 speed;
372
373 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
374 return -EINVAL;
375 if (cp->extendedmode != 0)
376 return -EINVAL;
377
378 speed = calc_speed(sd, tpf->numerator, tpf->denominator);
379
380 mt9v011_write(sd, R0A_MT9V011_CLK_SPEED, speed);
381 v4l2_dbg(1, debug, sd, "Setting speed to %d\n", speed);
382
383 /* Recalculate and update fps info */
384 calc_fps(sd, &tpf->numerator, &tpf->denominator);
385
386 return 0;
387}
388
Hans Verkuilea01b112010-05-09 10:19:25 -0300389static int mt9v011_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300390{
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300391 struct mt9v011 *core = to_mt9v011(sd);
392 int rc;
393
Hans Verkuilea01b112010-05-09 10:19:25 -0300394 rc = mt9v011_try_mbus_fmt(sd, fmt);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300395 if (rc < 0)
396 return -EINVAL;
397
Hans Verkuilea01b112010-05-09 10:19:25 -0300398 core->width = fmt->width;
399 core->height = fmt->height;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300400
401 set_res(sd);
402
403 return 0;
404}
405
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300406#ifdef CONFIG_VIDEO_ADV_DEBUG
407static int mt9v011_g_register(struct v4l2_subdev *sd,
408 struct v4l2_dbg_register *reg)
409{
410 struct i2c_client *client = v4l2_get_subdevdata(sd);
411
412 if (!v4l2_chip_match_i2c_client(client, &reg->match))
413 return -EINVAL;
414 if (!capable(CAP_SYS_ADMIN))
415 return -EPERM;
416
417 reg->val = mt9v011_read(sd, reg->reg & 0xff);
418 reg->size = 2;
419
420 return 0;
421}
422
423static int mt9v011_s_register(struct v4l2_subdev *sd,
424 struct v4l2_dbg_register *reg)
425{
426 struct i2c_client *client = v4l2_get_subdevdata(sd);
427
428 if (!v4l2_chip_match_i2c_client(client, &reg->match))
429 return -EINVAL;
430 if (!capable(CAP_SYS_ADMIN))
431 return -EPERM;
432
433 mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff);
434
435 return 0;
436}
437#endif
438
439static int mt9v011_g_chip_ident(struct v4l2_subdev *sd,
440 struct v4l2_dbg_chip_ident *chip)
441{
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300442 u16 version;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300443 struct i2c_client *client = v4l2_get_subdevdata(sd);
444
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300445 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
446
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300447 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011,
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300448 version);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300449}
450
Hans Verkuilea01a832012-09-07 05:42:15 -0300451static int mt9v011_s_ctrl(struct v4l2_ctrl *ctrl)
452{
453 struct mt9v011 *core =
454 container_of(ctrl->handler, struct mt9v011, ctrls);
455 struct v4l2_subdev *sd = &core->sd;
456
457 switch (ctrl->id) {
458 case V4L2_CID_GAIN:
459 core->global_gain = ctrl->val;
460 break;
461 case V4L2_CID_EXPOSURE:
462 core->exposure = ctrl->val;
463 break;
464 case V4L2_CID_RED_BALANCE:
465 core->red_bal = ctrl->val;
466 break;
467 case V4L2_CID_BLUE_BALANCE:
468 core->blue_bal = ctrl->val;
469 break;
470 case V4L2_CID_HFLIP:
471 core->hflip = ctrl->val;
472 set_read_mode(sd);
473 return 0;
474 case V4L2_CID_VFLIP:
475 core->vflip = ctrl->val;
476 set_read_mode(sd);
477 return 0;
478 default:
479 return -EINVAL;
480 }
481
482 set_balance(sd);
483 return 0;
484}
485
486static struct v4l2_ctrl_ops mt9v011_ctrl_ops = {
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300487 .s_ctrl = mt9v011_s_ctrl,
Hans Verkuilea01a832012-09-07 05:42:15 -0300488};
489
490static const struct v4l2_subdev_core_ops mt9v011_core_ops = {
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300491 .reset = mt9v011_reset,
492 .g_chip_ident = mt9v011_g_chip_ident,
493#ifdef CONFIG_VIDEO_ADV_DEBUG
494 .g_register = mt9v011_g_register,
495 .s_register = mt9v011_s_register,
496#endif
497};
498
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300499static const struct v4l2_subdev_video_ops mt9v011_video_ops = {
Hans Verkuilea01b112010-05-09 10:19:25 -0300500 .enum_mbus_fmt = mt9v011_enum_mbus_fmt,
501 .try_mbus_fmt = mt9v011_try_mbus_fmt,
502 .s_mbus_fmt = mt9v011_s_mbus_fmt,
Mauro Carvalho Chehab83053f72009-08-06 21:03:35 -0300503 .g_parm = mt9v011_g_parm,
504 .s_parm = mt9v011_s_parm,
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300505};
506
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300507static const struct v4l2_subdev_ops mt9v011_ops = {
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300508 .core = &mt9v011_core_ops,
509 .video = &mt9v011_video_ops,
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300510};
511
512
513/****************************************************************************
514 I2C Client & Driver
515 ****************************************************************************/
516
517static int mt9v011_probe(struct i2c_client *c,
518 const struct i2c_device_id *id)
519{
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300520 u16 version;
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300521 struct mt9v011 *core;
522 struct v4l2_subdev *sd;
523
524 /* Check if the adapter supports the needed features */
525 if (!i2c_check_functionality(c->adapter,
526 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
527 return -EIO;
528
529 core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL);
530 if (!core)
531 return -ENOMEM;
532
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300533 sd = &core->sd;
534 v4l2_i2c_subdev_init(sd, c, &mt9v011_ops);
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300535
536 /* Check if the sensor is really a MT9V011 */
537 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300538 if ((version != MT9V011_VERSION) &&
539 (version != MT9V011_REV_B_VERSION)) {
540 v4l2_info(sd, "*** unknown micron chip detected (0x%04x).\n",
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300541 version);
542 kfree(core);
543 return -EINVAL;
544 }
545
Hans Verkuilea01a832012-09-07 05:42:15 -0300546 v4l2_ctrl_handler_init(&core->ctrls, 5);
547 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops,
548 V4L2_CID_GAIN, 0, (1 << 12) - 1 - 0x20, 1, 0x20);
549 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops,
550 V4L2_CID_EXPOSURE, 0, 2047, 1, 0x01fc);
551 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops,
552 V4L2_CID_RED_BALANCE, -(1 << 9), (1 << 9) - 1, 1, 0);
553 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops,
554 V4L2_CID_BLUE_BALANCE, -(1 << 9), (1 << 9) - 1, 1, 0);
555 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops,
556 V4L2_CID_HFLIP, 0, 1, 1, 0);
557 v4l2_ctrl_new_std(&core->ctrls, &mt9v011_ctrl_ops,
558 V4L2_CID_VFLIP, 0, 1, 1, 0);
559
560 if (core->ctrls.error) {
561 int ret = core->ctrls.error;
562
563 v4l2_err(sd, "control initialization error %d\n", ret);
564 v4l2_ctrl_handler_free(&core->ctrls);
565 kfree(core);
566 return ret;
567 }
568 core->sd.ctrl_handler = &core->ctrls;
569
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300570 core->global_gain = 0x0024;
Johannes Obermaier590929f2011-06-02 12:43:14 -0300571 core->exposure = 0x01fc;
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300572 core->width = 640;
573 core->height = 480;
Mauro Carvalho Chehabe11206e2009-07-14 02:38:18 -0300574 core->xtal = 27000000; /* Hz */
Mauro Carvalho Chehab27fe4a32009-07-04 08:03:48 -0300575
Hans Verkuil3c7c9372011-01-08 07:08:02 -0300576 if (c->dev.platform_data) {
577 struct mt9v011_platform_data *pdata = c->dev.platform_data;
578
579 core->xtal = pdata->xtal;
580 v4l2_dbg(1, debug, sd, "xtal set to %d.%03d MHz\n",
581 core->xtal / 1000000, (core->xtal / 1000) % 1000);
582 }
583
Mauro Carvalho Chehab296544e2009-07-27 08:24:29 -0300584 v4l_info(c, "chip found @ 0x%02x (%s - chip version 0x%04x)\n",
585 c->addr << 1, c->adapter->name, version);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300586
587 return 0;
588}
589
590static int mt9v011_remove(struct i2c_client *c)
591{
592 struct v4l2_subdev *sd = i2c_get_clientdata(c);
Hans Verkuilea01a832012-09-07 05:42:15 -0300593 struct mt9v011 *core = to_mt9v011(sd);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300594
595 v4l2_dbg(1, debug, sd,
596 "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
597 c->addr << 1);
598
599 v4l2_device_unregister_subdev(sd);
Hans Verkuilea01a832012-09-07 05:42:15 -0300600 v4l2_ctrl_handler_free(&core->ctrls);
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300601 kfree(to_mt9v011(sd));
602 return 0;
603}
604
605/* ----------------------------------------------------------------------- */
606
607static const struct i2c_device_id mt9v011_id[] = {
608 { "mt9v011", 0 },
609 { }
610};
611MODULE_DEVICE_TABLE(i2c, mt9v011_id);
612
Hans Verkuil6ce58be2010-09-15 15:09:38 -0300613static struct i2c_driver mt9v011_driver = {
614 .driver = {
615 .owner = THIS_MODULE,
616 .name = "mt9v011",
617 },
618 .probe = mt9v011_probe,
619 .remove = mt9v011_remove,
620 .id_table = mt9v011_id,
Mauro Carvalho Chehab7dfba002009-06-29 05:41:26 -0300621};
Hans Verkuil6ce58be2010-09-15 15:09:38 -0300622
Axel Linc6e8d862012-02-12 06:56:32 -0300623module_i2c_driver(mt9v011_driver);