V4L/DVB (13645): soc-camera: fix multi-line comment coding style
[deliverable/linux.git] / drivers / media / video / mt9m001.c
1 /*
2 * Driver for MT9M001 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/log2.h>
15
16 #include <media/v4l2-subdev.h>
17 #include <media/v4l2-chip-ident.h>
18 #include <media/soc_camera.h>
19
20 /*
21 * mt9m001 i2c address 0x5d
22 * The platform has to define ctruct i2c_board_info objects and link to them
23 * from struct soc_camera_link
24 */
25
26 /* mt9m001 selected register addresses */
27 #define MT9M001_CHIP_VERSION 0x00
28 #define MT9M001_ROW_START 0x01
29 #define MT9M001_COLUMN_START 0x02
30 #define MT9M001_WINDOW_HEIGHT 0x03
31 #define MT9M001_WINDOW_WIDTH 0x04
32 #define MT9M001_HORIZONTAL_BLANKING 0x05
33 #define MT9M001_VERTICAL_BLANKING 0x06
34 #define MT9M001_OUTPUT_CONTROL 0x07
35 #define MT9M001_SHUTTER_WIDTH 0x09
36 #define MT9M001_FRAME_RESTART 0x0b
37 #define MT9M001_SHUTTER_DELAY 0x0c
38 #define MT9M001_RESET 0x0d
39 #define MT9M001_READ_OPTIONS1 0x1e
40 #define MT9M001_READ_OPTIONS2 0x20
41 #define MT9M001_GLOBAL_GAIN 0x35
42 #define MT9M001_CHIP_ENABLE 0xF1
43
44 #define MT9M001_MAX_WIDTH 1280
45 #define MT9M001_MAX_HEIGHT 1024
46 #define MT9M001_MIN_WIDTH 48
47 #define MT9M001_MIN_HEIGHT 32
48 #define MT9M001_COLUMN_SKIP 20
49 #define MT9M001_ROW_SKIP 12
50
51 static const struct soc_camera_data_format mt9m001_colour_formats[] = {
52 /*
53 * Order important: first natively supported,
54 * second supported with a GPIO extender
55 */
56 {
57 .name = "Bayer (sRGB) 10 bit",
58 .depth = 10,
59 .fourcc = V4L2_PIX_FMT_SBGGR16,
60 .colorspace = V4L2_COLORSPACE_SRGB,
61 }, {
62 .name = "Bayer (sRGB) 8 bit",
63 .depth = 8,
64 .fourcc = V4L2_PIX_FMT_SBGGR8,
65 .colorspace = V4L2_COLORSPACE_SRGB,
66 }
67 };
68
69 static const struct soc_camera_data_format mt9m001_monochrome_formats[] = {
70 /* Order important - see above */
71 {
72 .name = "Monochrome 10 bit",
73 .depth = 10,
74 .fourcc = V4L2_PIX_FMT_Y16,
75 }, {
76 .name = "Monochrome 8 bit",
77 .depth = 8,
78 .fourcc = V4L2_PIX_FMT_GREY,
79 },
80 };
81
82 struct mt9m001 {
83 struct v4l2_subdev subdev;
84 struct v4l2_rect rect; /* Sensor window */
85 __u32 fourcc;
86 int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
87 unsigned int gain;
88 unsigned int exposure;
89 unsigned short y_skip_top; /* Lines to skip at the top */
90 unsigned char autoexposure;
91 };
92
93 static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
94 {
95 return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
96 }
97
98 static int reg_read(struct i2c_client *client, const u8 reg)
99 {
100 s32 data = i2c_smbus_read_word_data(client, reg);
101 return data < 0 ? data : swab16(data);
102 }
103
104 static int reg_write(struct i2c_client *client, const u8 reg,
105 const u16 data)
106 {
107 return i2c_smbus_write_word_data(client, reg, swab16(data));
108 }
109
110 static int reg_set(struct i2c_client *client, const u8 reg,
111 const u16 data)
112 {
113 int ret;
114
115 ret = reg_read(client, reg);
116 if (ret < 0)
117 return ret;
118 return reg_write(client, reg, ret | data);
119 }
120
121 static int reg_clear(struct i2c_client *client, const u8 reg,
122 const u16 data)
123 {
124 int ret;
125
126 ret = reg_read(client, reg);
127 if (ret < 0)
128 return ret;
129 return reg_write(client, reg, ret & ~data);
130 }
131
132 static int mt9m001_init(struct i2c_client *client)
133 {
134 int ret;
135
136 dev_dbg(&client->dev, "%s\n", __func__);
137
138 /*
139 * We don't know, whether platform provides reset, issue a soft reset
140 * too. This returns all registers to their default values.
141 */
142 ret = reg_write(client, MT9M001_RESET, 1);
143 if (!ret)
144 ret = reg_write(client, MT9M001_RESET, 0);
145
146 /* Disable chip, synchronous option update */
147 if (!ret)
148 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
149
150 return ret;
151 }
152
153 static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
154 {
155 struct i2c_client *client = sd->priv;
156
157 /* Switch to master "normal" mode or stop sensor readout */
158 if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0)
159 return -EIO;
160 return 0;
161 }
162
163 static int mt9m001_set_bus_param(struct soc_camera_device *icd,
164 unsigned long flags)
165 {
166 struct soc_camera_link *icl = to_soc_camera_link(icd);
167 unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
168
169 /* Only one width bit may be set */
170 if (!is_power_of_2(width_flag))
171 return -EINVAL;
172
173 if (icl->set_bus_param)
174 return icl->set_bus_param(icl, width_flag);
175
176 /*
177 * Without board specific bus width settings we only support the
178 * sensors native bus width
179 */
180 if (width_flag == SOCAM_DATAWIDTH_10)
181 return 0;
182
183 return -EINVAL;
184 }
185
186 static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
187 {
188 struct soc_camera_link *icl = to_soc_camera_link(icd);
189 /* MT9M001 has all capture_format parameters fixed */
190 unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
191 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
192 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
193
194 if (icl->query_bus_param)
195 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
196 else
197 flags |= SOCAM_DATAWIDTH_10;
198
199 return soc_camera_apply_sensor_flags(icl, flags);
200 }
201
202 static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
203 {
204 struct i2c_client *client = sd->priv;
205 struct mt9m001 *mt9m001 = to_mt9m001(client);
206 struct v4l2_rect rect = a->c;
207 struct soc_camera_device *icd = client->dev.platform_data;
208 int ret;
209 const u16 hblank = 9, vblank = 25;
210 unsigned int total_h;
211
212 if (mt9m001->fourcc == V4L2_PIX_FMT_SBGGR8 ||
213 mt9m001->fourcc == V4L2_PIX_FMT_SBGGR16)
214 /*
215 * Bayer format - even number of rows for simplicity,
216 * but let the user play with the top row.
217 */
218 rect.height = ALIGN(rect.height, 2);
219
220 /* Datasheet requirement: see register description */
221 rect.width = ALIGN(rect.width, 2);
222 rect.left = ALIGN(rect.left, 2);
223
224 soc_camera_limit_side(&rect.left, &rect.width,
225 MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH);
226
227 soc_camera_limit_side(&rect.top, &rect.height,
228 MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
229
230 total_h = rect.height + mt9m001->y_skip_top + vblank;
231
232 /* Blanking and start values - default... */
233 ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
234 if (!ret)
235 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
236
237 /*
238 * The caller provides a supported format, as verified per
239 * call to icd->try_fmt()
240 */
241 if (!ret)
242 ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
243 if (!ret)
244 ret = reg_write(client, MT9M001_ROW_START, rect.top);
245 if (!ret)
246 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1);
247 if (!ret)
248 ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
249 rect.height + mt9m001->y_skip_top - 1);
250 if (!ret && mt9m001->autoexposure) {
251 ret = reg_write(client, MT9M001_SHUTTER_WIDTH, total_h);
252 if (!ret) {
253 const struct v4l2_queryctrl *qctrl =
254 soc_camera_find_qctrl(icd->ops,
255 V4L2_CID_EXPOSURE);
256 mt9m001->exposure = (524 + (total_h - 1) *
257 (qctrl->maximum - qctrl->minimum)) /
258 1048 + qctrl->minimum;
259 }
260 }
261
262 if (!ret)
263 mt9m001->rect = rect;
264
265 return ret;
266 }
267
268 static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
269 {
270 struct i2c_client *client = sd->priv;
271 struct mt9m001 *mt9m001 = to_mt9m001(client);
272
273 a->c = mt9m001->rect;
274 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
275
276 return 0;
277 }
278
279 static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
280 {
281 a->bounds.left = MT9M001_COLUMN_SKIP;
282 a->bounds.top = MT9M001_ROW_SKIP;
283 a->bounds.width = MT9M001_MAX_WIDTH;
284 a->bounds.height = MT9M001_MAX_HEIGHT;
285 a->defrect = a->bounds;
286 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
287 a->pixelaspect.numerator = 1;
288 a->pixelaspect.denominator = 1;
289
290 return 0;
291 }
292
293 static int mt9m001_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
294 {
295 struct i2c_client *client = sd->priv;
296 struct mt9m001 *mt9m001 = to_mt9m001(client);
297 struct v4l2_pix_format *pix = &f->fmt.pix;
298
299 pix->width = mt9m001->rect.width;
300 pix->height = mt9m001->rect.height;
301 pix->pixelformat = mt9m001->fourcc;
302 pix->field = V4L2_FIELD_NONE;
303 pix->colorspace = V4L2_COLORSPACE_SRGB;
304
305 return 0;
306 }
307
308 static int mt9m001_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
309 {
310 struct i2c_client *client = sd->priv;
311 struct mt9m001 *mt9m001 = to_mt9m001(client);
312 struct v4l2_pix_format *pix = &f->fmt.pix;
313 struct v4l2_crop a = {
314 .c = {
315 .left = mt9m001->rect.left,
316 .top = mt9m001->rect.top,
317 .width = pix->width,
318 .height = pix->height,
319 },
320 };
321 int ret;
322
323 /* No support for scaling so far, just crop. TODO: use skipping */
324 ret = mt9m001_s_crop(sd, &a);
325 if (!ret) {
326 pix->width = mt9m001->rect.width;
327 pix->height = mt9m001->rect.height;
328 mt9m001->fourcc = pix->pixelformat;
329 }
330
331 return ret;
332 }
333
334 static int mt9m001_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
335 {
336 struct i2c_client *client = sd->priv;
337 struct mt9m001 *mt9m001 = to_mt9m001(client);
338 struct v4l2_pix_format *pix = &f->fmt.pix;
339
340 v4l_bound_align_image(&pix->width, MT9M001_MIN_WIDTH,
341 MT9M001_MAX_WIDTH, 1,
342 &pix->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
343 MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
344
345 if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8 ||
346 pix->pixelformat == V4L2_PIX_FMT_SBGGR16)
347 pix->height = ALIGN(pix->height - 1, 2);
348
349 return 0;
350 }
351
352 static int mt9m001_g_chip_ident(struct v4l2_subdev *sd,
353 struct v4l2_dbg_chip_ident *id)
354 {
355 struct i2c_client *client = sd->priv;
356 struct mt9m001 *mt9m001 = to_mt9m001(client);
357
358 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
359 return -EINVAL;
360
361 if (id->match.addr != client->addr)
362 return -ENODEV;
363
364 id->ident = mt9m001->model;
365 id->revision = 0;
366
367 return 0;
368 }
369
370 #ifdef CONFIG_VIDEO_ADV_DEBUG
371 static int mt9m001_g_register(struct v4l2_subdev *sd,
372 struct v4l2_dbg_register *reg)
373 {
374 struct i2c_client *client = sd->priv;
375
376 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
377 return -EINVAL;
378
379 if (reg->match.addr != client->addr)
380 return -ENODEV;
381
382 reg->size = 2;
383 reg->val = reg_read(client, reg->reg);
384
385 if (reg->val > 0xffff)
386 return -EIO;
387
388 return 0;
389 }
390
391 static int mt9m001_s_register(struct v4l2_subdev *sd,
392 struct v4l2_dbg_register *reg)
393 {
394 struct i2c_client *client = sd->priv;
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 if (reg_write(client, reg->reg, reg->val) < 0)
403 return -EIO;
404
405 return 0;
406 }
407 #endif
408
409 static const struct v4l2_queryctrl mt9m001_controls[] = {
410 {
411 .id = V4L2_CID_VFLIP,
412 .type = V4L2_CTRL_TYPE_BOOLEAN,
413 .name = "Flip Vertically",
414 .minimum = 0,
415 .maximum = 1,
416 .step = 1,
417 .default_value = 0,
418 }, {
419 .id = V4L2_CID_GAIN,
420 .type = V4L2_CTRL_TYPE_INTEGER,
421 .name = "Gain",
422 .minimum = 0,
423 .maximum = 127,
424 .step = 1,
425 .default_value = 64,
426 .flags = V4L2_CTRL_FLAG_SLIDER,
427 }, {
428 .id = V4L2_CID_EXPOSURE,
429 .type = V4L2_CTRL_TYPE_INTEGER,
430 .name = "Exposure",
431 .minimum = 1,
432 .maximum = 255,
433 .step = 1,
434 .default_value = 255,
435 .flags = V4L2_CTRL_FLAG_SLIDER,
436 }, {
437 .id = V4L2_CID_EXPOSURE_AUTO,
438 .type = V4L2_CTRL_TYPE_BOOLEAN,
439 .name = "Automatic Exposure",
440 .minimum = 0,
441 .maximum = 1,
442 .step = 1,
443 .default_value = 1,
444 }
445 };
446
447 static struct soc_camera_ops mt9m001_ops = {
448 .set_bus_param = mt9m001_set_bus_param,
449 .query_bus_param = mt9m001_query_bus_param,
450 .controls = mt9m001_controls,
451 .num_controls = ARRAY_SIZE(mt9m001_controls),
452 };
453
454 static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
455 {
456 struct i2c_client *client = sd->priv;
457 struct mt9m001 *mt9m001 = to_mt9m001(client);
458 int data;
459
460 switch (ctrl->id) {
461 case V4L2_CID_VFLIP:
462 data = reg_read(client, MT9M001_READ_OPTIONS2);
463 if (data < 0)
464 return -EIO;
465 ctrl->value = !!(data & 0x8000);
466 break;
467 case V4L2_CID_EXPOSURE_AUTO:
468 ctrl->value = mt9m001->autoexposure;
469 break;
470 case V4L2_CID_GAIN:
471 ctrl->value = mt9m001->gain;
472 break;
473 case V4L2_CID_EXPOSURE:
474 ctrl->value = mt9m001->exposure;
475 break;
476 }
477 return 0;
478 }
479
480 static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
481 {
482 struct i2c_client *client = sd->priv;
483 struct mt9m001 *mt9m001 = to_mt9m001(client);
484 struct soc_camera_device *icd = client->dev.platform_data;
485 const struct v4l2_queryctrl *qctrl;
486 int data;
487
488 qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
489
490 if (!qctrl)
491 return -EINVAL;
492
493 switch (ctrl->id) {
494 case V4L2_CID_VFLIP:
495 if (ctrl->value)
496 data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
497 else
498 data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
499 if (data < 0)
500 return -EIO;
501 break;
502 case V4L2_CID_GAIN:
503 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
504 return -EINVAL;
505 /* See Datasheet Table 7, Gain settings. */
506 if (ctrl->value <= qctrl->default_value) {
507 /* Pack it into 0..1 step 0.125, register values 0..8 */
508 unsigned long range = qctrl->default_value - qctrl->minimum;
509 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
510
511 dev_dbg(&client->dev, "Setting gain %d\n", data);
512 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
513 if (data < 0)
514 return -EIO;
515 } else {
516 /* Pack it into 1.125..15 variable step, register values 9..67 */
517 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
518 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
519 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
520 111 + range / 2) / range + 9;
521
522 if (gain <= 32)
523 data = gain;
524 else if (gain <= 64)
525 data = ((gain - 32) * 16 + 16) / 32 + 80;
526 else
527 data = ((gain - 64) * 7 + 28) / 56 + 96;
528
529 dev_dbg(&client->dev, "Setting gain from %d to %d\n",
530 reg_read(client, MT9M001_GLOBAL_GAIN), data);
531 data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
532 if (data < 0)
533 return -EIO;
534 }
535
536 /* Success */
537 mt9m001->gain = ctrl->value;
538 break;
539 case V4L2_CID_EXPOSURE:
540 /* mt9m001 has maximum == default */
541 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
542 return -EINVAL;
543 else {
544 unsigned long range = qctrl->maximum - qctrl->minimum;
545 unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
546 range / 2) / range + 1;
547
548 dev_dbg(&client->dev,
549 "Setting shutter width from %d to %lu\n",
550 reg_read(client, MT9M001_SHUTTER_WIDTH),
551 shutter);
552 if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
553 return -EIO;
554 mt9m001->exposure = ctrl->value;
555 mt9m001->autoexposure = 0;
556 }
557 break;
558 case V4L2_CID_EXPOSURE_AUTO:
559 if (ctrl->value) {
560 const u16 vblank = 25;
561 unsigned int total_h = mt9m001->rect.height +
562 mt9m001->y_skip_top + vblank;
563 if (reg_write(client, MT9M001_SHUTTER_WIDTH,
564 total_h) < 0)
565 return -EIO;
566 qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
567 mt9m001->exposure = (524 + (total_h - 1) *
568 (qctrl->maximum - qctrl->minimum)) /
569 1048 + qctrl->minimum;
570 mt9m001->autoexposure = 1;
571 } else
572 mt9m001->autoexposure = 0;
573 break;
574 }
575 return 0;
576 }
577
578 /*
579 * Interface active, can use i2c. If it fails, it can indeed mean, that
580 * this wasn't our capture interface, so, we wait for the right one
581 */
582 static int mt9m001_video_probe(struct soc_camera_device *icd,
583 struct i2c_client *client)
584 {
585 struct mt9m001 *mt9m001 = to_mt9m001(client);
586 struct soc_camera_link *icl = to_soc_camera_link(icd);
587 s32 data;
588 unsigned long flags;
589 int ret;
590
591 /*
592 * We must have a parent by now. And it cannot be a wrong one.
593 * So this entire test is completely redundant.
594 */
595 if (!icd->dev.parent ||
596 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
597 return -ENODEV;
598
599 /* Enable the chip */
600 data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
601 dev_dbg(&client->dev, "write: %d\n", data);
602
603 /* Read out the chip version register */
604 data = reg_read(client, MT9M001_CHIP_VERSION);
605
606 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
607 switch (data) {
608 case 0x8411:
609 case 0x8421:
610 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
611 icd->formats = mt9m001_colour_formats;
612 break;
613 case 0x8431:
614 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
615 icd->formats = mt9m001_monochrome_formats;
616 break;
617 default:
618 dev_err(&client->dev,
619 "No MT9M001 chip detected, register read %x\n", data);
620 return -ENODEV;
621 }
622
623 icd->num_formats = 0;
624
625 /*
626 * This is a 10bit sensor, so by default we only allow 10bit.
627 * The platform may support different bus widths due to
628 * different routing of the data lines.
629 */
630 if (icl->query_bus_param)
631 flags = icl->query_bus_param(icl);
632 else
633 flags = SOCAM_DATAWIDTH_10;
634
635 if (flags & SOCAM_DATAWIDTH_10)
636 icd->num_formats++;
637 else
638 icd->formats++;
639
640 if (flags & SOCAM_DATAWIDTH_8)
641 icd->num_formats++;
642
643 mt9m001->fourcc = icd->formats->fourcc;
644
645 dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
646 data == 0x8431 ? "C12STM" : "C12ST");
647
648 ret = mt9m001_init(client);
649 if (ret < 0)
650 dev_err(&client->dev, "Failed to initialise the camera\n");
651
652 /* mt9m001_init() has reset the chip, returning registers to defaults */
653 mt9m001->gain = 64;
654 mt9m001->exposure = 255;
655
656 return ret;
657 }
658
659 static void mt9m001_video_remove(struct soc_camera_device *icd)
660 {
661 struct soc_camera_link *icl = to_soc_camera_link(icd);
662
663 dev_dbg(&icd->dev, "Video removed: %p, %p\n",
664 icd->dev.parent, icd->vdev);
665 if (icl->free_bus)
666 icl->free_bus(icl);
667 }
668
669 static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
670 {
671 struct i2c_client *client = sd->priv;
672 struct mt9m001 *mt9m001 = to_mt9m001(client);
673
674 *lines = mt9m001->y_skip_top;
675
676 return 0;
677 }
678
679 static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
680 .g_ctrl = mt9m001_g_ctrl,
681 .s_ctrl = mt9m001_s_ctrl,
682 .g_chip_ident = mt9m001_g_chip_ident,
683 #ifdef CONFIG_VIDEO_ADV_DEBUG
684 .g_register = mt9m001_g_register,
685 .s_register = mt9m001_s_register,
686 #endif
687 };
688
689 static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
690 .s_stream = mt9m001_s_stream,
691 .s_fmt = mt9m001_s_fmt,
692 .g_fmt = mt9m001_g_fmt,
693 .try_fmt = mt9m001_try_fmt,
694 .s_crop = mt9m001_s_crop,
695 .g_crop = mt9m001_g_crop,
696 .cropcap = mt9m001_cropcap,
697 };
698
699 static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
700 .g_skip_top_lines = mt9m001_g_skip_top_lines,
701 };
702
703 static struct v4l2_subdev_ops mt9m001_subdev_ops = {
704 .core = &mt9m001_subdev_core_ops,
705 .video = &mt9m001_subdev_video_ops,
706 .sensor = &mt9m001_subdev_sensor_ops,
707 };
708
709 static int mt9m001_probe(struct i2c_client *client,
710 const struct i2c_device_id *did)
711 {
712 struct mt9m001 *mt9m001;
713 struct soc_camera_device *icd = client->dev.platform_data;
714 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
715 struct soc_camera_link *icl;
716 int ret;
717
718 if (!icd) {
719 dev_err(&client->dev, "MT9M001: missing soc-camera data!\n");
720 return -EINVAL;
721 }
722
723 icl = to_soc_camera_link(icd);
724 if (!icl) {
725 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
726 return -EINVAL;
727 }
728
729 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
730 dev_warn(&adapter->dev,
731 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
732 return -EIO;
733 }
734
735 mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
736 if (!mt9m001)
737 return -ENOMEM;
738
739 v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
740
741 /* Second stage probe - when a capture adapter is there */
742 icd->ops = &mt9m001_ops;
743
744 mt9m001->y_skip_top = 0;
745 mt9m001->rect.left = MT9M001_COLUMN_SKIP;
746 mt9m001->rect.top = MT9M001_ROW_SKIP;
747 mt9m001->rect.width = MT9M001_MAX_WIDTH;
748 mt9m001->rect.height = MT9M001_MAX_HEIGHT;
749
750 /*
751 * Simulated autoexposure. If enabled, we calculate shutter width
752 * ourselves in the driver based on vertical blanking and frame width
753 */
754 mt9m001->autoexposure = 1;
755
756 ret = mt9m001_video_probe(icd, client);
757 if (ret) {
758 icd->ops = NULL;
759 i2c_set_clientdata(client, NULL);
760 kfree(mt9m001);
761 }
762
763 return ret;
764 }
765
766 static int mt9m001_remove(struct i2c_client *client)
767 {
768 struct mt9m001 *mt9m001 = to_mt9m001(client);
769 struct soc_camera_device *icd = client->dev.platform_data;
770
771 icd->ops = NULL;
772 mt9m001_video_remove(icd);
773 i2c_set_clientdata(client, NULL);
774 client->driver = NULL;
775 kfree(mt9m001);
776
777 return 0;
778 }
779
780 static const struct i2c_device_id mt9m001_id[] = {
781 { "mt9m001", 0 },
782 { }
783 };
784 MODULE_DEVICE_TABLE(i2c, mt9m001_id);
785
786 static struct i2c_driver mt9m001_i2c_driver = {
787 .driver = {
788 .name = "mt9m001",
789 },
790 .probe = mt9m001_probe,
791 .remove = mt9m001_remove,
792 .id_table = mt9m001_id,
793 };
794
795 static int __init mt9m001_mod_init(void)
796 {
797 return i2c_add_driver(&mt9m001_i2c_driver);
798 }
799
800 static void __exit mt9m001_mod_exit(void)
801 {
802 i2c_del_driver(&mt9m001_i2c_driver);
803 }
804
805 module_init(mt9m001_mod_init);
806 module_exit(mt9m001_mod_exit);
807
808 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
809 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
810 MODULE_LICENSE("GPL");
This page took 0.104904 seconds and 5 git commands to generate.