V4L/DVB (12506): soc-camera: convert to platform device
[deliverable/linux.git] / drivers / media / video / mt9v022.c
1 /*
2 * Driver for MT9V022 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/log2.h>
16
17 #include <media/v4l2-common.h>
18 #include <media/v4l2-chip-ident.h>
19 #include <media/soc_camera.h>
20
21 /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
22 * The platform has to define i2c_board_info
23 * and call i2c_register_board_info() */
24
25 static char *sensor_type;
26 module_param(sensor_type, charp, S_IRUGO);
27 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
28
29 /* mt9v022 selected register addresses */
30 #define MT9V022_CHIP_VERSION 0x00
31 #define MT9V022_COLUMN_START 0x01
32 #define MT9V022_ROW_START 0x02
33 #define MT9V022_WINDOW_HEIGHT 0x03
34 #define MT9V022_WINDOW_WIDTH 0x04
35 #define MT9V022_HORIZONTAL_BLANKING 0x05
36 #define MT9V022_VERTICAL_BLANKING 0x06
37 #define MT9V022_CHIP_CONTROL 0x07
38 #define MT9V022_SHUTTER_WIDTH1 0x08
39 #define MT9V022_SHUTTER_WIDTH2 0x09
40 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
41 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
42 #define MT9V022_RESET 0x0c
43 #define MT9V022_READ_MODE 0x0d
44 #define MT9V022_MONITOR_MODE 0x0e
45 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
46 #define MT9V022_LED_OUT_CONTROL 0x1b
47 #define MT9V022_ADC_MODE_CONTROL 0x1c
48 #define MT9V022_ANALOG_GAIN 0x34
49 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
50 #define MT9V022_PIXCLK_FV_LV 0x74
51 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
52 #define MT9V022_AEC_AGC_ENABLE 0xAF
53 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
54
55 /* Progressive scan, master, defaults */
56 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
57
58 static const struct soc_camera_data_format mt9v022_colour_formats[] = {
59 /* Order important: first natively supported,
60 * second supported with a GPIO extender */
61 {
62 .name = "Bayer (sRGB) 10 bit",
63 .depth = 10,
64 .fourcc = V4L2_PIX_FMT_SBGGR16,
65 .colorspace = V4L2_COLORSPACE_SRGB,
66 }, {
67 .name = "Bayer (sRGB) 8 bit",
68 .depth = 8,
69 .fourcc = V4L2_PIX_FMT_SBGGR8,
70 .colorspace = V4L2_COLORSPACE_SRGB,
71 }
72 };
73
74 static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
75 /* Order important - see above */
76 {
77 .name = "Monochrome 10 bit",
78 .depth = 10,
79 .fourcc = V4L2_PIX_FMT_Y16,
80 }, {
81 .name = "Monochrome 8 bit",
82 .depth = 8,
83 .fourcc = V4L2_PIX_FMT_GREY,
84 },
85 };
86
87 struct mt9v022 {
88 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
89 u16 chip_control;
90 };
91
92 static int reg_read(struct i2c_client *client, const u8 reg)
93 {
94 s32 data = i2c_smbus_read_word_data(client, reg);
95 return data < 0 ? data : swab16(data);
96 }
97
98 static int reg_write(struct i2c_client *client, const u8 reg,
99 const u16 data)
100 {
101 return i2c_smbus_write_word_data(client, reg, swab16(data));
102 }
103
104 static int reg_set(struct i2c_client *client, const u8 reg,
105 const u16 data)
106 {
107 int ret;
108
109 ret = reg_read(client, reg);
110 if (ret < 0)
111 return ret;
112 return reg_write(client, reg, ret | data);
113 }
114
115 static int reg_clear(struct i2c_client *client, const u8 reg,
116 const u16 data)
117 {
118 int ret;
119
120 ret = reg_read(client, reg);
121 if (ret < 0)
122 return ret;
123 return reg_write(client, reg, ret & ~data);
124 }
125
126 static int mt9v022_init(struct soc_camera_device *icd)
127 {
128 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
129 struct soc_camera_link *icl = to_soc_camera_link(icd);
130 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
131 int ret;
132
133 if (icl->power) {
134 ret = icl->power(&client->dev, 1);
135 if (ret < 0) {
136 dev_err(icd->vdev->parent,
137 "Platform failed to power-on the camera.\n");
138 return ret;
139 }
140 }
141
142 /*
143 * The camera could have been already on, we hard-reset it additionally,
144 * if available. Soft reset is done in video_probe().
145 */
146 if (icl->reset)
147 icl->reset(&client->dev);
148
149 /* Almost the default mode: master, parallel, simultaneous, and an
150 * undocumented bit 0x200, which is present in table 7, but not in 8,
151 * plus snapshot mode to disable scan for now */
152 mt9v022->chip_control |= 0x10;
153 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
154 if (!ret)
155 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
156
157 /* All defaults */
158 if (!ret)
159 /* AEC, AGC on */
160 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
161 if (!ret)
162 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
163 if (!ret)
164 /* default - auto */
165 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
166 if (!ret)
167 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
168
169 return ret;
170 }
171
172 static int mt9v022_release(struct soc_camera_device *icd)
173 {
174 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
175 struct soc_camera_link *icl = to_soc_camera_link(icd);
176
177 if (icl->power)
178 icl->power(&client->dev, 0);
179
180 return 0;
181 }
182
183 static int mt9v022_start_capture(struct soc_camera_device *icd)
184 {
185 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
186 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
187 /* Switch to master "normal" mode */
188 mt9v022->chip_control &= ~0x10;
189 if (reg_write(client, MT9V022_CHIP_CONTROL,
190 mt9v022->chip_control) < 0)
191 return -EIO;
192 return 0;
193 }
194
195 static int mt9v022_stop_capture(struct soc_camera_device *icd)
196 {
197 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
198 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
199 /* Switch to snapshot mode */
200 mt9v022->chip_control |= 0x10;
201 if (reg_write(client, MT9V022_CHIP_CONTROL,
202 mt9v022->chip_control) < 0)
203 return -EIO;
204 return 0;
205 }
206
207 static int mt9v022_set_bus_param(struct soc_camera_device *icd,
208 unsigned long flags)
209 {
210 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
211 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
212 struct soc_camera_link *icl = to_soc_camera_link(icd);
213 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
214 int ret;
215 u16 pixclk = 0;
216
217 /* Only one width bit may be set */
218 if (!is_power_of_2(width_flag))
219 return -EINVAL;
220
221 if (icl->set_bus_param) {
222 ret = icl->set_bus_param(icl, width_flag);
223 if (ret)
224 return ret;
225 } else {
226 /*
227 * Without board specific bus width settings we only support the
228 * sensors native bus width
229 */
230 if (width_flag != SOCAM_DATAWIDTH_10)
231 return -EINVAL;
232 }
233
234 flags = soc_camera_apply_sensor_flags(icl, flags);
235
236 if (flags & SOCAM_PCLK_SAMPLE_RISING)
237 pixclk |= 0x10;
238
239 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
240 pixclk |= 0x1;
241
242 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
243 pixclk |= 0x2;
244
245 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
246 if (ret < 0)
247 return ret;
248
249 if (!(flags & SOCAM_MASTER))
250 mt9v022->chip_control &= ~0x8;
251
252 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
253 if (ret < 0)
254 return ret;
255
256 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
257 pixclk, mt9v022->chip_control);
258
259 return 0;
260 }
261
262 static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
263 {
264 struct soc_camera_link *icl = to_soc_camera_link(icd);
265 unsigned int width_flag;
266
267 if (icl->query_bus_param)
268 width_flag = icl->query_bus_param(icl) &
269 SOCAM_DATAWIDTH_MASK;
270 else
271 width_flag = SOCAM_DATAWIDTH_10;
272
273 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
274 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
275 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
276 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
277 width_flag;
278 }
279
280 static int mt9v022_set_crop(struct soc_camera_device *icd,
281 struct v4l2_rect *rect)
282 {
283 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
284 int ret;
285
286 /* Like in example app. Contradicts the datasheet though */
287 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
288 if (ret >= 0) {
289 if (ret & 1) /* Autoexposure */
290 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
291 rect->height + icd->y_skip_top + 43);
292 else
293 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
294 rect->height + icd->y_skip_top + 43);
295 }
296 /* Setup frame format: defaults apart from width and height */
297 if (!ret)
298 ret = reg_write(client, MT9V022_COLUMN_START, rect->left);
299 if (!ret)
300 ret = reg_write(client, MT9V022_ROW_START, rect->top);
301 if (!ret)
302 /* Default 94, Phytec driver says:
303 * "width + horizontal blank >= 660" */
304 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
305 rect->width > 660 - 43 ? 43 :
306 660 - rect->width);
307 if (!ret)
308 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
309 if (!ret)
310 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect->width);
311 if (!ret)
312 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
313 rect->height + icd->y_skip_top);
314
315 if (ret < 0)
316 return ret;
317
318 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
319
320 return 0;
321 }
322
323 static int mt9v022_set_fmt(struct soc_camera_device *icd,
324 struct v4l2_format *f)
325 {
326 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
327 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
328 struct v4l2_pix_format *pix = &f->fmt.pix;
329 struct v4l2_rect rect = {
330 .left = icd->x_current,
331 .top = icd->y_current,
332 .width = pix->width,
333 .height = pix->height,
334 };
335
336 /* The caller provides a supported format, as verified per call to
337 * icd->try_fmt(), datawidth is from our supported format list */
338 switch (pix->pixelformat) {
339 case V4L2_PIX_FMT_GREY:
340 case V4L2_PIX_FMT_Y16:
341 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
342 return -EINVAL;
343 break;
344 case V4L2_PIX_FMT_SBGGR8:
345 case V4L2_PIX_FMT_SBGGR16:
346 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
347 return -EINVAL;
348 break;
349 case 0:
350 /* No format change, only geometry */
351 break;
352 default:
353 return -EINVAL;
354 }
355
356 /* No support for scaling on this camera, just crop. */
357 return mt9v022_set_crop(icd, &rect);
358 }
359
360 static int mt9v022_try_fmt(struct soc_camera_device *icd,
361 struct v4l2_format *f)
362 {
363 struct v4l2_pix_format *pix = &f->fmt.pix;
364
365 v4l_bound_align_image(&pix->width, 48, 752, 2 /* ? */,
366 &pix->height, 32 + icd->y_skip_top,
367 480 + icd->y_skip_top, 0, 0);
368
369 return 0;
370 }
371
372 static int mt9v022_get_chip_id(struct soc_camera_device *icd,
373 struct v4l2_dbg_chip_ident *id)
374 {
375 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
376 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
377
378 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
379 return -EINVAL;
380
381 if (id->match.addr != client->addr)
382 return -ENODEV;
383
384 id->ident = mt9v022->model;
385 id->revision = 0;
386
387 return 0;
388 }
389
390 #ifdef CONFIG_VIDEO_ADV_DEBUG
391 static int mt9v022_get_register(struct soc_camera_device *icd,
392 struct v4l2_dbg_register *reg)
393 {
394 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
395
396 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
397 return -EINVAL;
398
399 if (reg->match.addr != client->addr)
400 return -ENODEV;
401
402 reg->size = 2;
403 reg->val = reg_read(client, reg->reg);
404
405 if (reg->val > 0xffff)
406 return -EIO;
407
408 return 0;
409 }
410
411 static int mt9v022_set_register(struct soc_camera_device *icd,
412 struct v4l2_dbg_register *reg)
413 {
414 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
415
416 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
417 return -EINVAL;
418
419 if (reg->match.addr != client->addr)
420 return -ENODEV;
421
422 if (reg_write(client, reg->reg, reg->val) < 0)
423 return -EIO;
424
425 return 0;
426 }
427 #endif
428
429 static const struct v4l2_queryctrl mt9v022_controls[] = {
430 {
431 .id = V4L2_CID_VFLIP,
432 .type = V4L2_CTRL_TYPE_BOOLEAN,
433 .name = "Flip Vertically",
434 .minimum = 0,
435 .maximum = 1,
436 .step = 1,
437 .default_value = 0,
438 }, {
439 .id = V4L2_CID_HFLIP,
440 .type = V4L2_CTRL_TYPE_BOOLEAN,
441 .name = "Flip Horizontally",
442 .minimum = 0,
443 .maximum = 1,
444 .step = 1,
445 .default_value = 0,
446 }, {
447 .id = V4L2_CID_GAIN,
448 .type = V4L2_CTRL_TYPE_INTEGER,
449 .name = "Analog Gain",
450 .minimum = 64,
451 .maximum = 127,
452 .step = 1,
453 .default_value = 64,
454 .flags = V4L2_CTRL_FLAG_SLIDER,
455 }, {
456 .id = V4L2_CID_EXPOSURE,
457 .type = V4L2_CTRL_TYPE_INTEGER,
458 .name = "Exposure",
459 .minimum = 1,
460 .maximum = 255,
461 .step = 1,
462 .default_value = 255,
463 .flags = V4L2_CTRL_FLAG_SLIDER,
464 }, {
465 .id = V4L2_CID_AUTOGAIN,
466 .type = V4L2_CTRL_TYPE_BOOLEAN,
467 .name = "Automatic Gain",
468 .minimum = 0,
469 .maximum = 1,
470 .step = 1,
471 .default_value = 1,
472 }, {
473 .id = V4L2_CID_EXPOSURE_AUTO,
474 .type = V4L2_CTRL_TYPE_BOOLEAN,
475 .name = "Automatic Exposure",
476 .minimum = 0,
477 .maximum = 1,
478 .step = 1,
479 .default_value = 1,
480 }
481 };
482
483 static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
484 static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
485
486 static struct soc_camera_ops mt9v022_ops = {
487 .owner = THIS_MODULE,
488 .init = mt9v022_init,
489 .release = mt9v022_release,
490 .start_capture = mt9v022_start_capture,
491 .stop_capture = mt9v022_stop_capture,
492 .set_crop = mt9v022_set_crop,
493 .set_fmt = mt9v022_set_fmt,
494 .try_fmt = mt9v022_try_fmt,
495 .set_bus_param = mt9v022_set_bus_param,
496 .query_bus_param = mt9v022_query_bus_param,
497 .controls = mt9v022_controls,
498 .num_controls = ARRAY_SIZE(mt9v022_controls),
499 .get_control = mt9v022_get_control,
500 .set_control = mt9v022_set_control,
501 .get_chip_id = mt9v022_get_chip_id,
502 #ifdef CONFIG_VIDEO_ADV_DEBUG
503 .get_register = mt9v022_get_register,
504 .set_register = mt9v022_set_register,
505 #endif
506 };
507
508 static int mt9v022_get_control(struct soc_camera_device *icd,
509 struct v4l2_control *ctrl)
510 {
511 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
512 int data;
513
514 switch (ctrl->id) {
515 case V4L2_CID_VFLIP:
516 data = reg_read(client, MT9V022_READ_MODE);
517 if (data < 0)
518 return -EIO;
519 ctrl->value = !!(data & 0x10);
520 break;
521 case V4L2_CID_HFLIP:
522 data = reg_read(client, MT9V022_READ_MODE);
523 if (data < 0)
524 return -EIO;
525 ctrl->value = !!(data & 0x20);
526 break;
527 case V4L2_CID_EXPOSURE_AUTO:
528 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
529 if (data < 0)
530 return -EIO;
531 ctrl->value = !!(data & 0x1);
532 break;
533 case V4L2_CID_AUTOGAIN:
534 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
535 if (data < 0)
536 return -EIO;
537 ctrl->value = !!(data & 0x2);
538 break;
539 }
540 return 0;
541 }
542
543 static int mt9v022_set_control(struct soc_camera_device *icd,
544 struct v4l2_control *ctrl)
545 {
546 int data;
547 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
548 const struct v4l2_queryctrl *qctrl;
549
550 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
551
552 if (!qctrl)
553 return -EINVAL;
554
555 switch (ctrl->id) {
556 case V4L2_CID_VFLIP:
557 if (ctrl->value)
558 data = reg_set(client, MT9V022_READ_MODE, 0x10);
559 else
560 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
561 if (data < 0)
562 return -EIO;
563 break;
564 case V4L2_CID_HFLIP:
565 if (ctrl->value)
566 data = reg_set(client, MT9V022_READ_MODE, 0x20);
567 else
568 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
569 if (data < 0)
570 return -EIO;
571 break;
572 case V4L2_CID_GAIN:
573 /* mt9v022 has minimum == default */
574 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
575 return -EINVAL;
576 else {
577 unsigned long range = qctrl->maximum - qctrl->minimum;
578 /* Datasheet says 16 to 64. autogain only works properly
579 * after setting gain to maximum 14. Larger values
580 * produce "white fly" noise effect. On the whole,
581 * manually setting analog gain does no good. */
582 unsigned long gain = ((ctrl->value - qctrl->minimum) *
583 10 + range / 2) / range + 4;
584 if (gain >= 32)
585 gain &= ~1;
586 /* The user wants to set gain manually, hope, she
587 * knows, what she's doing... Switch AGC off. */
588
589 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
590 return -EIO;
591
592 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
593 reg_read(client, MT9V022_ANALOG_GAIN), gain);
594 if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
595 return -EIO;
596 icd->gain = ctrl->value;
597 }
598 break;
599 case V4L2_CID_EXPOSURE:
600 /* mt9v022 has maximum == default */
601 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
602 return -EINVAL;
603 else {
604 unsigned long range = qctrl->maximum - qctrl->minimum;
605 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
606 479 + range / 2) / range + 1;
607 /* The user wants to set shutter width manually, hope,
608 * she knows, what she's doing... Switch AEC off. */
609
610 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
611 return -EIO;
612
613 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
614 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
615 shutter);
616 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
617 shutter) < 0)
618 return -EIO;
619 icd->exposure = ctrl->value;
620 }
621 break;
622 case V4L2_CID_AUTOGAIN:
623 if (ctrl->value)
624 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
625 else
626 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
627 if (data < 0)
628 return -EIO;
629 break;
630 case V4L2_CID_EXPOSURE_AUTO:
631 if (ctrl->value)
632 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
633 else
634 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
635 if (data < 0)
636 return -EIO;
637 break;
638 }
639 return 0;
640 }
641
642 /* Interface active, can use i2c. If it fails, it can indeed mean, that
643 * this wasn't our capture interface, so, we wait for the right one */
644 static int mt9v022_video_probe(struct soc_camera_device *icd,
645 struct i2c_client *client)
646 {
647 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
648 struct soc_camera_link *icl = to_soc_camera_link(icd);
649 s32 data;
650 int ret;
651 unsigned long flags;
652
653 if (!icd->dev.parent ||
654 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
655 return -ENODEV;
656
657 /* Switch master clock on */
658 ret = soc_camera_video_start(icd, &client->dev);
659 if (ret)
660 return ret;
661
662 /* Read out the chip version register */
663 data = reg_read(client, MT9V022_CHIP_VERSION);
664
665 /* must be 0x1311 or 0x1313 */
666 if (data != 0x1311 && data != 0x1313) {
667 ret = -ENODEV;
668 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
669 data);
670 goto ei2c;
671 }
672
673 /* Soft reset */
674 ret = reg_write(client, MT9V022_RESET, 1);
675 if (ret < 0)
676 goto ei2c;
677 /* 15 clock cycles */
678 udelay(200);
679 if (reg_read(client, MT9V022_RESET)) {
680 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
681 if (ret > 0)
682 ret = -EIO;
683 goto ei2c;
684 }
685
686 /* Set monochrome or colour sensor type */
687 if (sensor_type && (!strcmp("colour", sensor_type) ||
688 !strcmp("color", sensor_type))) {
689 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
690 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
691 icd->formats = mt9v022_colour_formats;
692 } else {
693 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
694 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
695 icd->formats = mt9v022_monochrome_formats;
696 }
697
698 if (ret < 0)
699 goto ei2c;
700
701 icd->num_formats = 0;
702
703 /*
704 * This is a 10bit sensor, so by default we only allow 10bit.
705 * The platform may support different bus widths due to
706 * different routing of the data lines.
707 */
708 if (icl->query_bus_param)
709 flags = icl->query_bus_param(icl);
710 else
711 flags = SOCAM_DATAWIDTH_10;
712
713 if (flags & SOCAM_DATAWIDTH_10)
714 icd->num_formats++;
715 else
716 icd->formats++;
717
718 if (flags & SOCAM_DATAWIDTH_8)
719 icd->num_formats++;
720
721 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
722 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
723 "monochrome" : "colour");
724
725 ei2c:
726 soc_camera_video_stop(icd);
727
728 return ret;
729 }
730
731 static void mt9v022_video_remove(struct soc_camera_device *icd)
732 {
733 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
734 struct soc_camera_link *icl = to_soc_camera_link(icd);
735
736 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", client->addr,
737 icd->dev.parent, icd->vdev);
738 if (icl->free_bus)
739 icl->free_bus(icl);
740 }
741
742 static int mt9v022_probe(struct i2c_client *client,
743 const struct i2c_device_id *did)
744 {
745 struct mt9v022 *mt9v022;
746 struct soc_camera_device *icd = client->dev.platform_data;
747 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
748 struct soc_camera_link *icl;
749 int ret;
750
751 if (!icd) {
752 dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
753 return -EINVAL;
754 }
755
756 icl = to_soc_camera_link(icd);
757 if (!icl) {
758 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
759 return -EINVAL;
760 }
761
762 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
763 dev_warn(&adapter->dev,
764 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
765 return -EIO;
766 }
767
768 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
769 if (!mt9v022)
770 return -ENOMEM;
771
772 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
773 i2c_set_clientdata(client, mt9v022);
774
775 icd->ops = &mt9v022_ops;
776 icd->x_min = 1;
777 icd->y_min = 4;
778 icd->x_current = 1;
779 icd->y_current = 4;
780 icd->width_min = 48;
781 icd->width_max = 752;
782 icd->height_min = 32;
783 icd->height_max = 480;
784 icd->y_skip_top = 1;
785
786 ret = mt9v022_video_probe(icd, client);
787 if (ret) {
788 icd->ops = NULL;
789 i2c_set_clientdata(client, NULL);
790 kfree(mt9v022);
791 }
792
793 return ret;
794 }
795
796 static int mt9v022_remove(struct i2c_client *client)
797 {
798 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
799 struct soc_camera_device *icd = client->dev.platform_data;
800
801 icd->ops = NULL;
802 mt9v022_video_remove(icd);
803 i2c_set_clientdata(client, NULL);
804 client->driver = NULL;
805 kfree(mt9v022);
806
807 return 0;
808 }
809 static const struct i2c_device_id mt9v022_id[] = {
810 { "mt9v022", 0 },
811 { }
812 };
813 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
814
815 static struct i2c_driver mt9v022_i2c_driver = {
816 .driver = {
817 .name = "mt9v022",
818 },
819 .probe = mt9v022_probe,
820 .remove = mt9v022_remove,
821 .id_table = mt9v022_id,
822 };
823
824 static int __init mt9v022_mod_init(void)
825 {
826 return i2c_add_driver(&mt9v022_i2c_driver);
827 }
828
829 static void __exit mt9v022_mod_exit(void)
830 {
831 i2c_del_driver(&mt9v022_i2c_driver);
832 }
833
834 module_init(mt9v022_mod_init);
835 module_exit(mt9v022_mod_exit);
836
837 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
838 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
839 MODULE_LICENSE("GPL");
This page took 0.068417 seconds and 5 git commands to generate.