]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/video/mt9v011.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / mt9v011.c
1 /*
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>
9 #include <linux/slab.h>
10 #include <linux/videodev2.h>
11 #include <linux/delay.h>
12 #include <asm/div64.h>
13 #include <media/v4l2-device.h>
14 #include "mt9v011.h"
15 #include <media/v4l2-i2c-drv.h>
16 #include <media/v4l2-chip-ident.h>
17
18 MODULE_DESCRIPTION("Micron mt9v011 sensor driver");
19 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
20 MODULE_LICENSE("GPL");
21
22
23 static int debug;
24 module_param(debug, int, 0);
25 MODULE_PARM_DESC(debug, "Debug level (0-2)");
26
27 /* supported controls */
28 static struct v4l2_queryctrl mt9v011_qctrl[] = {
29 {
30 .id = V4L2_CID_GAIN,
31 .type = V4L2_CTRL_TYPE_INTEGER,
32 .name = "Gain",
33 .minimum = 0,
34 .maximum = (1 << 10) - 1,
35 .step = 1,
36 .default_value = 0x0020,
37 .flags = 0,
38 }, {
39 .id = V4L2_CID_RED_BALANCE,
40 .type = V4L2_CTRL_TYPE_INTEGER,
41 .name = "Red Balance",
42 .minimum = -1 << 9,
43 .maximum = (1 << 9) - 1,
44 .step = 1,
45 .default_value = 0,
46 .flags = 0,
47 }, {
48 .id = V4L2_CID_BLUE_BALANCE,
49 .type = V4L2_CTRL_TYPE_INTEGER,
50 .name = "Blue Balance",
51 .minimum = -1 << 9,
52 .maximum = (1 << 9) - 1,
53 .step = 1,
54 .default_value = 0,
55 .flags = 0,
56 }, {
57 .id = V4L2_CID_HFLIP,
58 .type = V4L2_CTRL_TYPE_BOOLEAN,
59 .name = "Mirror",
60 .minimum = 0,
61 .maximum = 1,
62 .step = 1,
63 .default_value = 0,
64 .flags = 0,
65 }, {
66 .id = V4L2_CID_VFLIP,
67 .type = V4L2_CTRL_TYPE_BOOLEAN,
68 .name = "Vflip",
69 .minimum = 0,
70 .maximum = 1,
71 .step = 1,
72 .default_value = 0,
73 .flags = 0,
74 }, {
75 }
76 };
77
78 struct mt9v011 {
79 struct v4l2_subdev sd;
80 unsigned width, height;
81 unsigned xtal;
82 unsigned hflip:1;
83 unsigned vflip:1;
84
85 u16 global_gain, red_bal, blue_bal;
86 };
87
88 static inline struct mt9v011 *to_mt9v011(struct v4l2_subdev *sd)
89 {
90 return container_of(sd, struct mt9v011, sd);
91 }
92
93 static int mt9v011_read(struct v4l2_subdev *sd, unsigned char addr)
94 {
95 struct i2c_client *c = v4l2_get_subdevdata(sd);
96 __be16 buffer;
97 int rc, val;
98
99 rc = i2c_master_send(c, &addr, 1);
100 if (rc != 1)
101 v4l2_dbg(0, debug, sd,
102 "i2c i/o error: rc == %d (should be 1)\n", rc);
103
104 msleep(10);
105
106 rc = i2c_master_recv(c, (char *)&buffer, 2);
107 if (rc != 2)
108 v4l2_dbg(0, debug, sd,
109 "i2c i/o error: rc == %d (should be 2)\n", rc);
110
111 val = be16_to_cpu(buffer);
112
113 v4l2_dbg(2, debug, sd, "mt9v011: read 0x%02x = 0x%04x\n", addr, val);
114
115 return val;
116 }
117
118 static void mt9v011_write(struct v4l2_subdev *sd, unsigned char addr,
119 u16 value)
120 {
121 struct i2c_client *c = v4l2_get_subdevdata(sd);
122 unsigned char buffer[3];
123 int rc;
124
125 buffer[0] = addr;
126 buffer[1] = value >> 8;
127 buffer[2] = value & 0xff;
128
129 v4l2_dbg(2, debug, sd,
130 "mt9v011: writing 0x%02x 0x%04x\n", buffer[0], value);
131 rc = i2c_master_send(c, buffer, 3);
132 if (rc != 3)
133 v4l2_dbg(0, debug, sd,
134 "i2c i/o error: rc == %d (should be 3)\n", rc);
135 }
136
137
138 struct i2c_reg_value {
139 unsigned char reg;
140 u16 value;
141 };
142
143 /*
144 * Values used at the original driver
145 * Some values are marked as Reserved at the datasheet
146 */
147 static const struct i2c_reg_value mt9v011_init_default[] = {
148 { R0D_MT9V011_RESET, 0x0001 },
149 { R0D_MT9V011_RESET, 0x0000 },
150
151 { R0C_MT9V011_SHUTTER_DELAY, 0x0000 },
152 { R09_MT9V011_SHUTTER_WIDTH, 0x1fc },
153
154 { R0A_MT9V011_CLK_SPEED, 0x0000 },
155 { R1E_MT9V011_DIGITAL_ZOOM, 0x0000 },
156
157 { R07_MT9V011_OUT_CTRL, 0x0002 }, /* chip enable */
158 };
159
160 static void set_balance(struct v4l2_subdev *sd)
161 {
162 struct mt9v011 *core = to_mt9v011(sd);
163 u16 green1_gain, green2_gain, blue_gain, red_gain;
164
165 green1_gain = core->global_gain;
166 green2_gain = core->global_gain;
167
168 blue_gain = core->global_gain +
169 core->global_gain * core->blue_bal / (1 << 9);
170
171 red_gain = core->global_gain +
172 core->global_gain * core->blue_bal / (1 << 9);
173
174 mt9v011_write(sd, R2B_MT9V011_GREEN_1_GAIN, green1_gain);
175 mt9v011_write(sd, R2E_MT9V011_GREEN_2_GAIN, green1_gain);
176 mt9v011_write(sd, R2C_MT9V011_BLUE_GAIN, blue_gain);
177 mt9v011_write(sd, R2D_MT9V011_RED_GAIN, red_gain);
178 }
179
180 static void calc_fps(struct v4l2_subdev *sd, u32 *numerator, u32 *denominator)
181 {
182 struct mt9v011 *core = to_mt9v011(sd);
183 unsigned height, width, hblank, vblank, speed;
184 unsigned row_time, t_time;
185 u64 frames_per_ms;
186 unsigned tmp;
187
188 height = mt9v011_read(sd, R03_MT9V011_HEIGHT);
189 width = mt9v011_read(sd, R04_MT9V011_WIDTH);
190 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK);
191 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK);
192 speed = mt9v011_read(sd, R0A_MT9V011_CLK_SPEED);
193
194 row_time = (width + 113 + hblank) * (speed + 2);
195 t_time = row_time * (height + vblank + 1);
196
197 frames_per_ms = core->xtal * 1000l;
198 do_div(frames_per_ms, t_time);
199 tmp = frames_per_ms;
200
201 v4l2_dbg(1, debug, sd, "Programmed to %u.%03u fps (%d pixel clcks)\n",
202 tmp / 1000, tmp % 1000, t_time);
203
204 if (numerator && denominator) {
205 *numerator = 1000;
206 *denominator = (u32)frames_per_ms;
207 }
208 }
209
210 static u16 calc_speed(struct v4l2_subdev *sd, u32 numerator, u32 denominator)
211 {
212 struct mt9v011 *core = to_mt9v011(sd);
213 unsigned height, width, hblank, vblank;
214 unsigned row_time, line_time;
215 u64 t_time, speed;
216
217 /* Avoid bogus calculus */
218 if (!numerator || !denominator)
219 return 0;
220
221 height = mt9v011_read(sd, R03_MT9V011_HEIGHT);
222 width = mt9v011_read(sd, R04_MT9V011_WIDTH);
223 hblank = mt9v011_read(sd, R05_MT9V011_HBLANK);
224 vblank = mt9v011_read(sd, R06_MT9V011_VBLANK);
225
226 row_time = width + 113 + hblank;
227 line_time = height + vblank + 1;
228
229 t_time = core->xtal * ((u64)numerator);
230 /* round to the closest value */
231 t_time += denominator / 2;
232 do_div(t_time, denominator);
233
234 speed = t_time;
235 do_div(speed, row_time * line_time);
236
237 /* Avoid having a negative value for speed */
238 if (speed < 2)
239 speed = 0;
240 else
241 speed -= 2;
242
243 /* Avoid speed overflow */
244 if (speed > 15)
245 return 15;
246
247 return (u16)speed;
248 }
249
250 static void set_res(struct v4l2_subdev *sd)
251 {
252 struct mt9v011 *core = to_mt9v011(sd);
253 unsigned vstart, hstart;
254
255 /*
256 * The mt9v011 doesn't have scaling. So, in order to select the desired
257 * resolution, we're cropping at the middle of the sensor.
258 * hblank and vblank should be adjusted, in order to warrant that
259 * we'll preserve the line timings for 30 fps, no matter what resolution
260 * is selected.
261 * NOTE: datasheet says that width (and height) should be filled with
262 * width-1. However, this doesn't work, since one pixel per line will
263 * be missing.
264 */
265
266 hstart = 14 + (640 - core->width) / 2;
267 mt9v011_write(sd, R02_MT9V011_COLSTART, hstart);
268 mt9v011_write(sd, R04_MT9V011_WIDTH, core->width);
269 mt9v011_write(sd, R05_MT9V011_HBLANK, 771 - core->width);
270
271 vstart = 8 + (480 - core->height) / 2;
272 mt9v011_write(sd, R01_MT9V011_ROWSTART, vstart);
273 mt9v011_write(sd, R03_MT9V011_HEIGHT, core->height);
274 mt9v011_write(sd, R06_MT9V011_VBLANK, 508 - core->height);
275
276 calc_fps(sd, NULL, NULL);
277 };
278
279 static void set_read_mode(struct v4l2_subdev *sd)
280 {
281 struct mt9v011 *core = to_mt9v011(sd);
282 unsigned mode = 0x1000;
283
284 if (core->hflip)
285 mode |= 0x4000;
286
287 if (core->vflip)
288 mode |= 0x8000;
289
290 mt9v011_write(sd, R20_MT9V011_READ_MODE, mode);
291 }
292
293 static int mt9v011_reset(struct v4l2_subdev *sd, u32 val)
294 {
295 int i;
296
297 for (i = 0; i < ARRAY_SIZE(mt9v011_init_default); i++)
298 mt9v011_write(sd, mt9v011_init_default[i].reg,
299 mt9v011_init_default[i].value);
300
301 set_balance(sd);
302 set_res(sd);
303 set_read_mode(sd);
304
305 return 0;
306 };
307
308 static int mt9v011_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
309 {
310 struct mt9v011 *core = to_mt9v011(sd);
311
312 v4l2_dbg(1, debug, sd, "g_ctrl called\n");
313
314 switch (ctrl->id) {
315 case V4L2_CID_GAIN:
316 ctrl->value = core->global_gain;
317 return 0;
318 case V4L2_CID_RED_BALANCE:
319 ctrl->value = core->red_bal;
320 return 0;
321 case V4L2_CID_BLUE_BALANCE:
322 ctrl->value = core->blue_bal;
323 return 0;
324 case V4L2_CID_HFLIP:
325 ctrl->value = core->hflip ? 1 : 0;
326 return 0;
327 case V4L2_CID_VFLIP:
328 ctrl->value = core->vflip ? 1 : 0;
329 return 0;
330 }
331 return -EINVAL;
332 }
333
334 static int mt9v011_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
335 {
336 int i;
337
338 v4l2_dbg(1, debug, sd, "queryctrl called\n");
339
340 for (i = 0; i < ARRAY_SIZE(mt9v011_qctrl); i++)
341 if (qc->id && qc->id == mt9v011_qctrl[i].id) {
342 memcpy(qc, &(mt9v011_qctrl[i]),
343 sizeof(*qc));
344 return 0;
345 }
346
347 return -EINVAL;
348 }
349
350
351 static int mt9v011_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
352 {
353 struct mt9v011 *core = to_mt9v011(sd);
354 u8 i, n;
355 n = ARRAY_SIZE(mt9v011_qctrl);
356
357 for (i = 0; i < n; i++) {
358 if (ctrl->id != mt9v011_qctrl[i].id)
359 continue;
360 if (ctrl->value < mt9v011_qctrl[i].minimum ||
361 ctrl->value > mt9v011_qctrl[i].maximum)
362 return -ERANGE;
363 v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n",
364 ctrl->id, ctrl->value);
365 break;
366 }
367
368 switch (ctrl->id) {
369 case V4L2_CID_GAIN:
370 core->global_gain = ctrl->value;
371 break;
372 case V4L2_CID_RED_BALANCE:
373 core->red_bal = ctrl->value;
374 break;
375 case V4L2_CID_BLUE_BALANCE:
376 core->blue_bal = ctrl->value;
377 break;
378 case V4L2_CID_HFLIP:
379 core->hflip = ctrl->value;
380 set_read_mode(sd);
381 return 0;
382 case V4L2_CID_VFLIP:
383 core->vflip = ctrl->value;
384 set_read_mode(sd);
385 return 0;
386 default:
387 return -EINVAL;
388 }
389
390 set_balance(sd);
391
392 return 0;
393 }
394
395 static int mt9v011_enum_fmt(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
396 {
397 if (fmt->index > 0)
398 return -EINVAL;
399
400 fmt->flags = 0;
401 strcpy(fmt->description, "8 bpp Bayer GRGR..BGBG");
402 fmt->pixelformat = V4L2_PIX_FMT_SGRBG8;
403
404 return 0;
405 }
406
407 static int mt9v011_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
408 {
409 struct v4l2_pix_format *pix = &fmt->fmt.pix;
410
411 if (pix->pixelformat != V4L2_PIX_FMT_SGRBG8)
412 return -EINVAL;
413
414 v4l_bound_align_image(&pix->width, 48, 639, 1,
415 &pix->height, 32, 480, 1, 0);
416
417 return 0;
418 }
419
420 static int mt9v011_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
421 {
422 struct v4l2_captureparm *cp = &parms->parm.capture;
423
424 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
425 return -EINVAL;
426
427 memset(cp, 0, sizeof(struct v4l2_captureparm));
428 cp->capability = V4L2_CAP_TIMEPERFRAME;
429 calc_fps(sd,
430 &cp->timeperframe.numerator,
431 &cp->timeperframe.denominator);
432
433 return 0;
434 }
435
436 static int mt9v011_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
437 {
438 struct v4l2_captureparm *cp = &parms->parm.capture;
439 struct v4l2_fract *tpf = &cp->timeperframe;
440 u16 speed;
441
442 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
443 return -EINVAL;
444 if (cp->extendedmode != 0)
445 return -EINVAL;
446
447 speed = calc_speed(sd, tpf->numerator, tpf->denominator);
448
449 mt9v011_write(sd, R0A_MT9V011_CLK_SPEED, speed);
450 v4l2_dbg(1, debug, sd, "Setting speed to %d\n", speed);
451
452 /* Recalculate and update fps info */
453 calc_fps(sd, &tpf->numerator, &tpf->denominator);
454
455 return 0;
456 }
457
458 static int mt9v011_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
459 {
460 struct v4l2_pix_format *pix = &fmt->fmt.pix;
461 struct mt9v011 *core = to_mt9v011(sd);
462 int rc;
463
464 rc = mt9v011_try_fmt(sd, fmt);
465 if (rc < 0)
466 return -EINVAL;
467
468 core->width = pix->width;
469 core->height = pix->height;
470
471 set_res(sd);
472
473 return 0;
474 }
475
476 static int mt9v011_s_config(struct v4l2_subdev *sd, int dumb, void *data)
477 {
478 struct mt9v011 *core = to_mt9v011(sd);
479 unsigned *xtal = data;
480
481 v4l2_dbg(1, debug, sd, "s_config called\n");
482
483 if (xtal) {
484 core->xtal = *xtal;
485 v4l2_dbg(1, debug, sd, "xtal set to %d.%03d MHz\n",
486 *xtal / 1000000, (*xtal / 1000) % 1000);
487 }
488
489 return 0;
490 }
491
492
493 #ifdef CONFIG_VIDEO_ADV_DEBUG
494 static int mt9v011_g_register(struct v4l2_subdev *sd,
495 struct v4l2_dbg_register *reg)
496 {
497 struct i2c_client *client = v4l2_get_subdevdata(sd);
498
499 if (!v4l2_chip_match_i2c_client(client, &reg->match))
500 return -EINVAL;
501 if (!capable(CAP_SYS_ADMIN))
502 return -EPERM;
503
504 reg->val = mt9v011_read(sd, reg->reg & 0xff);
505 reg->size = 2;
506
507 return 0;
508 }
509
510 static int mt9v011_s_register(struct v4l2_subdev *sd,
511 struct v4l2_dbg_register *reg)
512 {
513 struct i2c_client *client = v4l2_get_subdevdata(sd);
514
515 if (!v4l2_chip_match_i2c_client(client, &reg->match))
516 return -EINVAL;
517 if (!capable(CAP_SYS_ADMIN))
518 return -EPERM;
519
520 mt9v011_write(sd, reg->reg & 0xff, reg->val & 0xffff);
521
522 return 0;
523 }
524 #endif
525
526 static int mt9v011_g_chip_ident(struct v4l2_subdev *sd,
527 struct v4l2_dbg_chip_ident *chip)
528 {
529 u16 version;
530 struct i2c_client *client = v4l2_get_subdevdata(sd);
531
532 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
533
534 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_MT9V011,
535 version);
536 }
537
538 static const struct v4l2_subdev_core_ops mt9v011_core_ops = {
539 .queryctrl = mt9v011_queryctrl,
540 .g_ctrl = mt9v011_g_ctrl,
541 .s_ctrl = mt9v011_s_ctrl,
542 .reset = mt9v011_reset,
543 .s_config = mt9v011_s_config,
544 .g_chip_ident = mt9v011_g_chip_ident,
545 #ifdef CONFIG_VIDEO_ADV_DEBUG
546 .g_register = mt9v011_g_register,
547 .s_register = mt9v011_s_register,
548 #endif
549 };
550
551 static const struct v4l2_subdev_video_ops mt9v011_video_ops = {
552 .enum_fmt = mt9v011_enum_fmt,
553 .try_fmt = mt9v011_try_fmt,
554 .s_fmt = mt9v011_s_fmt,
555 .g_parm = mt9v011_g_parm,
556 .s_parm = mt9v011_s_parm,
557 };
558
559 static const struct v4l2_subdev_ops mt9v011_ops = {
560 .core = &mt9v011_core_ops,
561 .video = &mt9v011_video_ops,
562 };
563
564
565 /****************************************************************************
566 I2C Client & Driver
567 ****************************************************************************/
568
569 static int mt9v011_probe(struct i2c_client *c,
570 const struct i2c_device_id *id)
571 {
572 u16 version;
573 struct mt9v011 *core;
574 struct v4l2_subdev *sd;
575
576 /* Check if the adapter supports the needed features */
577 if (!i2c_check_functionality(c->adapter,
578 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
579 return -EIO;
580
581 core = kzalloc(sizeof(struct mt9v011), GFP_KERNEL);
582 if (!core)
583 return -ENOMEM;
584
585 sd = &core->sd;
586 v4l2_i2c_subdev_init(sd, c, &mt9v011_ops);
587
588 /* Check if the sensor is really a MT9V011 */
589 version = mt9v011_read(sd, R00_MT9V011_CHIP_VERSION);
590 if ((version != MT9V011_VERSION) &&
591 (version != MT9V011_REV_B_VERSION)) {
592 v4l2_info(sd, "*** unknown micron chip detected (0x%04x).\n",
593 version);
594 kfree(core);
595 return -EINVAL;
596 }
597
598 core->global_gain = 0x0024;
599 core->width = 640;
600 core->height = 480;
601 core->xtal = 27000000; /* Hz */
602
603 v4l_info(c, "chip found @ 0x%02x (%s - chip version 0x%04x)\n",
604 c->addr << 1, c->adapter->name, version);
605
606 return 0;
607 }
608
609 static int mt9v011_remove(struct i2c_client *c)
610 {
611 struct v4l2_subdev *sd = i2c_get_clientdata(c);
612
613 v4l2_dbg(1, debug, sd,
614 "mt9v011.c: removing mt9v011 adapter on address 0x%x\n",
615 c->addr << 1);
616
617 v4l2_device_unregister_subdev(sd);
618 kfree(to_mt9v011(sd));
619 return 0;
620 }
621
622 /* ----------------------------------------------------------------------- */
623
624 static const struct i2c_device_id mt9v011_id[] = {
625 { "mt9v011", 0 },
626 { }
627 };
628 MODULE_DEVICE_TABLE(i2c, mt9v011_id);
629
630 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
631 .name = "mt9v011",
632 .probe = mt9v011_probe,
633 .remove = mt9v011_remove,
634 .id_table = mt9v011_id,
635 };