[media] mt9v022: convert to the control framework
[pandora-kernel.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/soc_camera.h>
18 #include <media/soc_mediabus.h>
19 #include <media/v4l2-subdev.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/v4l2-ctrls.h>
22
23 /*
24  * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
25  * The platform has to define ctruct i2c_board_info objects and link to them
26  * from struct soc_camera_link
27  */
28
29 static char *sensor_type;
30 module_param(sensor_type, charp, S_IRUGO);
31 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
32
33 /* mt9v022 selected register addresses */
34 #define MT9V022_CHIP_VERSION            0x00
35 #define MT9V022_COLUMN_START            0x01
36 #define MT9V022_ROW_START               0x02
37 #define MT9V022_WINDOW_HEIGHT           0x03
38 #define MT9V022_WINDOW_WIDTH            0x04
39 #define MT9V022_HORIZONTAL_BLANKING     0x05
40 #define MT9V022_VERTICAL_BLANKING       0x06
41 #define MT9V022_CHIP_CONTROL            0x07
42 #define MT9V022_SHUTTER_WIDTH1          0x08
43 #define MT9V022_SHUTTER_WIDTH2          0x09
44 #define MT9V022_SHUTTER_WIDTH_CTRL      0x0a
45 #define MT9V022_TOTAL_SHUTTER_WIDTH     0x0b
46 #define MT9V022_RESET                   0x0c
47 #define MT9V022_READ_MODE               0x0d
48 #define MT9V022_MONITOR_MODE            0x0e
49 #define MT9V022_PIXEL_OPERATION_MODE    0x0f
50 #define MT9V022_LED_OUT_CONTROL         0x1b
51 #define MT9V022_ADC_MODE_CONTROL        0x1c
52 #define MT9V022_ANALOG_GAIN             0x35
53 #define MT9V022_BLACK_LEVEL_CALIB_CTRL  0x47
54 #define MT9V022_PIXCLK_FV_LV            0x74
55 #define MT9V022_DIGITAL_TEST_PATTERN    0x7f
56 #define MT9V022_AEC_AGC_ENABLE          0xAF
57 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
58
59 /* Progressive scan, master, defaults */
60 #define MT9V022_CHIP_CONTROL_DEFAULT    0x188
61
62 #define MT9V022_MAX_WIDTH               752
63 #define MT9V022_MAX_HEIGHT              480
64 #define MT9V022_MIN_WIDTH               48
65 #define MT9V022_MIN_HEIGHT              32
66 #define MT9V022_COLUMN_SKIP             1
67 #define MT9V022_ROW_SKIP                4
68
69 /* MT9V022 has only one fixed colorspace per pixelcode */
70 struct mt9v022_datafmt {
71         enum v4l2_mbus_pixelcode        code;
72         enum v4l2_colorspace            colorspace;
73 };
74
75 /* Find a data format by a pixel code in an array */
76 static const struct mt9v022_datafmt *mt9v022_find_datafmt(
77         enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
78         int n)
79 {
80         int i;
81         for (i = 0; i < n; i++)
82                 if (fmt[i].code == code)
83                         return fmt + i;
84
85         return NULL;
86 }
87
88 static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
89         /*
90          * Order important: first natively supported,
91          * second supported with a GPIO extender
92          */
93         {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
94         {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
95 };
96
97 static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
98         /* Order important - see above */
99         {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
100         {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
101 };
102
103 struct mt9v022 {
104         struct v4l2_subdev subdev;
105         struct v4l2_ctrl_handler hdl;
106         struct {
107                 /* exposure/auto-exposure cluster */
108                 struct v4l2_ctrl *autoexposure;
109                 struct v4l2_ctrl *exposure;
110         };
111         struct {
112                 /* gain/auto-gain cluster */
113                 struct v4l2_ctrl *autogain;
114                 struct v4l2_ctrl *gain;
115         };
116         struct v4l2_rect rect;  /* Sensor window */
117         const struct mt9v022_datafmt *fmt;
118         const struct mt9v022_datafmt *fmts;
119         int num_fmts;
120         int model;      /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
121         u16 chip_control;
122         unsigned short y_skip_top;      /* Lines to skip at the top */
123 };
124
125 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
126 {
127         return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
128 }
129
130 static int reg_read(struct i2c_client *client, const u8 reg)
131 {
132         s32 data = i2c_smbus_read_word_data(client, reg);
133         return data < 0 ? data : swab16(data);
134 }
135
136 static int reg_write(struct i2c_client *client, const u8 reg,
137                      const u16 data)
138 {
139         return i2c_smbus_write_word_data(client, reg, swab16(data));
140 }
141
142 static int reg_set(struct i2c_client *client, const u8 reg,
143                    const u16 data)
144 {
145         int ret;
146
147         ret = reg_read(client, reg);
148         if (ret < 0)
149                 return ret;
150         return reg_write(client, reg, ret | data);
151 }
152
153 static int reg_clear(struct i2c_client *client, const u8 reg,
154                      const u16 data)
155 {
156         int ret;
157
158         ret = reg_read(client, reg);
159         if (ret < 0)
160                 return ret;
161         return reg_write(client, reg, ret & ~data);
162 }
163
164 static int mt9v022_init(struct i2c_client *client)
165 {
166         struct mt9v022 *mt9v022 = to_mt9v022(client);
167         int ret;
168
169         /*
170          * Almost the default mode: master, parallel, simultaneous, and an
171          * undocumented bit 0x200, which is present in table 7, but not in 8,
172          * plus snapshot mode to disable scan for now
173          */
174         mt9v022->chip_control |= 0x10;
175         ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
176         if (!ret)
177                 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
178
179         /* All defaults */
180         if (!ret)
181                 /* AEC, AGC on */
182                 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
183         if (!ret)
184                 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
185         if (!ret)
186                 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
187         if (!ret)
188                 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
189         if (!ret)
190                 /* default - auto */
191                 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
192         if (!ret)
193                 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
194         if (!ret)
195                 return v4l2_ctrl_handler_setup(&mt9v022->hdl);
196
197         return ret;
198 }
199
200 static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
201 {
202         struct i2c_client *client = v4l2_get_subdevdata(sd);
203         struct mt9v022 *mt9v022 = to_mt9v022(client);
204
205         if (enable)
206                 /* Switch to master "normal" mode */
207                 mt9v022->chip_control &= ~0x10;
208         else
209                 /* Switch to snapshot mode */
210                 mt9v022->chip_control |= 0x10;
211
212         if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
213                 return -EIO;
214         return 0;
215 }
216
217 static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
218 {
219         struct i2c_client *client = v4l2_get_subdevdata(sd);
220         struct mt9v022 *mt9v022 = to_mt9v022(client);
221         struct v4l2_rect rect = a->c;
222         int ret;
223
224         /* Bayer format - even size lengths */
225         if (mt9v022->fmts == mt9v022_colour_fmts) {
226                 rect.width      = ALIGN(rect.width, 2);
227                 rect.height     = ALIGN(rect.height, 2);
228                 /* Let the user play with the starting pixel */
229         }
230
231         soc_camera_limit_side(&rect.left, &rect.width,
232                      MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
233
234         soc_camera_limit_side(&rect.top, &rect.height,
235                      MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
236
237         /* Like in example app. Contradicts the datasheet though */
238         ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
239         if (ret >= 0) {
240                 if (ret & 1) /* Autoexposure */
241                         ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
242                                         rect.height + mt9v022->y_skip_top + 43);
243                 else
244                         ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
245                                         rect.height + mt9v022->y_skip_top + 43);
246         }
247         /* Setup frame format: defaults apart from width and height */
248         if (!ret)
249                 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
250         if (!ret)
251                 ret = reg_write(client, MT9V022_ROW_START, rect.top);
252         if (!ret)
253                 /*
254                  * Default 94, Phytec driver says:
255                  * "width + horizontal blank >= 660"
256                  */
257                 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
258                                 rect.width > 660 - 43 ? 43 :
259                                 660 - rect.width);
260         if (!ret)
261                 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
262         if (!ret)
263                 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
264         if (!ret)
265                 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
266                                 rect.height + mt9v022->y_skip_top);
267
268         if (ret < 0)
269                 return ret;
270
271         dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
272
273         mt9v022->rect = rect;
274
275         return 0;
276 }
277
278 static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
279 {
280         struct i2c_client *client = v4l2_get_subdevdata(sd);
281         struct mt9v022 *mt9v022 = to_mt9v022(client);
282
283         a->c    = mt9v022->rect;
284         a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
285
286         return 0;
287 }
288
289 static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
290 {
291         a->bounds.left                  = MT9V022_COLUMN_SKIP;
292         a->bounds.top                   = MT9V022_ROW_SKIP;
293         a->bounds.width                 = MT9V022_MAX_WIDTH;
294         a->bounds.height                = MT9V022_MAX_HEIGHT;
295         a->defrect                      = a->bounds;
296         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
297         a->pixelaspect.numerator        = 1;
298         a->pixelaspect.denominator      = 1;
299
300         return 0;
301 }
302
303 static int mt9v022_g_fmt(struct v4l2_subdev *sd,
304                          struct v4l2_mbus_framefmt *mf)
305 {
306         struct i2c_client *client = v4l2_get_subdevdata(sd);
307         struct mt9v022 *mt9v022 = to_mt9v022(client);
308
309         mf->width       = mt9v022->rect.width;
310         mf->height      = mt9v022->rect.height;
311         mf->code        = mt9v022->fmt->code;
312         mf->colorspace  = mt9v022->fmt->colorspace;
313         mf->field       = V4L2_FIELD_NONE;
314
315         return 0;
316 }
317
318 static int mt9v022_s_fmt(struct v4l2_subdev *sd,
319                          struct v4l2_mbus_framefmt *mf)
320 {
321         struct i2c_client *client = v4l2_get_subdevdata(sd);
322         struct mt9v022 *mt9v022 = to_mt9v022(client);
323         struct v4l2_crop a = {
324                 .c = {
325                         .left   = mt9v022->rect.left,
326                         .top    = mt9v022->rect.top,
327                         .width  = mf->width,
328                         .height = mf->height,
329                 },
330         };
331         int ret;
332
333         /*
334          * The caller provides a supported format, as verified per call to
335          * icd->try_fmt(), datawidth is from our supported format list
336          */
337         switch (mf->code) {
338         case V4L2_MBUS_FMT_Y8_1X8:
339         case V4L2_MBUS_FMT_Y10_1X10:
340                 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
341                         return -EINVAL;
342                 break;
343         case V4L2_MBUS_FMT_SBGGR8_1X8:
344         case V4L2_MBUS_FMT_SBGGR10_1X10:
345                 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
346                         return -EINVAL;
347                 break;
348         default:
349                 return -EINVAL;
350         }
351
352         /* No support for scaling on this camera, just crop. */
353         ret = mt9v022_s_crop(sd, &a);
354         if (!ret) {
355                 mf->width       = mt9v022->rect.width;
356                 mf->height      = mt9v022->rect.height;
357                 mt9v022->fmt    = mt9v022_find_datafmt(mf->code,
358                                         mt9v022->fmts, mt9v022->num_fmts);
359                 mf->colorspace  = mt9v022->fmt->colorspace;
360         }
361
362         return ret;
363 }
364
365 static int mt9v022_try_fmt(struct v4l2_subdev *sd,
366                            struct v4l2_mbus_framefmt *mf)
367 {
368         struct i2c_client *client = v4l2_get_subdevdata(sd);
369         struct mt9v022 *mt9v022 = to_mt9v022(client);
370         const struct mt9v022_datafmt *fmt;
371         int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
372                 mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
373
374         v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
375                 MT9V022_MAX_WIDTH, align,
376                 &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
377                 MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
378
379         fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
380                                    mt9v022->num_fmts);
381         if (!fmt) {
382                 fmt = mt9v022->fmt;
383                 mf->code = fmt->code;
384         }
385
386         mf->colorspace  = fmt->colorspace;
387
388         return 0;
389 }
390
391 static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
392                                 struct v4l2_dbg_chip_ident *id)
393 {
394         struct i2c_client *client = v4l2_get_subdevdata(sd);
395         struct mt9v022 *mt9v022 = to_mt9v022(client);
396
397         if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
398                 return -EINVAL;
399
400         if (id->match.addr != client->addr)
401                 return -ENODEV;
402
403         id->ident       = mt9v022->model;
404         id->revision    = 0;
405
406         return 0;
407 }
408
409 #ifdef CONFIG_VIDEO_ADV_DEBUG
410 static int mt9v022_g_register(struct v4l2_subdev *sd,
411                               struct v4l2_dbg_register *reg)
412 {
413         struct i2c_client *client = v4l2_get_subdevdata(sd);
414
415         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
416                 return -EINVAL;
417
418         if (reg->match.addr != client->addr)
419                 return -ENODEV;
420
421         reg->size = 2;
422         reg->val = reg_read(client, reg->reg);
423
424         if (reg->val > 0xffff)
425                 return -EIO;
426
427         return 0;
428 }
429
430 static int mt9v022_s_register(struct v4l2_subdev *sd,
431                               struct v4l2_dbg_register *reg)
432 {
433         struct i2c_client *client = v4l2_get_subdevdata(sd);
434
435         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
436                 return -EINVAL;
437
438         if (reg->match.addr != client->addr)
439                 return -ENODEV;
440
441         if (reg_write(client, reg->reg, reg->val) < 0)
442                 return -EIO;
443
444         return 0;
445 }
446 #endif
447
448 static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
449 {
450         struct mt9v022 *mt9v022 = container_of(ctrl->handler,
451                                                struct mt9v022, hdl);
452         struct v4l2_subdev *sd = &mt9v022->subdev;
453         struct i2c_client *client = v4l2_get_subdevdata(sd);
454         struct v4l2_ctrl *gain = mt9v022->gain;
455         struct v4l2_ctrl *exp = mt9v022->exposure;
456         unsigned long range;
457         int data;
458
459         switch (ctrl->id) {
460         case V4L2_CID_AUTOGAIN:
461                 data = reg_read(client, MT9V022_ANALOG_GAIN);
462                 if (data < 0)
463                         return -EIO;
464
465                 range = gain->maximum - gain->minimum;
466                 gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
467                 return 0;
468         case V4L2_CID_EXPOSURE_AUTO:
469                 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
470                 if (data < 0)
471                         return -EIO;
472
473                 range = exp->maximum - exp->minimum;
474                 exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
475                 return 0;
476         }
477         return -EINVAL;
478 }
479
480 static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
481 {
482         struct mt9v022 *mt9v022 = container_of(ctrl->handler,
483                                                struct mt9v022, hdl);
484         struct v4l2_subdev *sd = &mt9v022->subdev;
485         struct i2c_client *client = v4l2_get_subdevdata(sd);
486         int data;
487
488         switch (ctrl->id) {
489         case V4L2_CID_VFLIP:
490                 if (ctrl->val)
491                         data = reg_set(client, MT9V022_READ_MODE, 0x10);
492                 else
493                         data = reg_clear(client, MT9V022_READ_MODE, 0x10);
494                 if (data < 0)
495                         return -EIO;
496                 return 0;
497         case V4L2_CID_HFLIP:
498                 if (ctrl->val)
499                         data = reg_set(client, MT9V022_READ_MODE, 0x20);
500                 else
501                         data = reg_clear(client, MT9V022_READ_MODE, 0x20);
502                 if (data < 0)
503                         return -EIO;
504                 return 0;
505         case V4L2_CID_AUTOGAIN:
506                 if (ctrl->val) {
507                         if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
508                                 return -EIO;
509                 } else {
510                         struct v4l2_ctrl *gain = mt9v022->gain;
511                         /* mt9v022 has minimum == default */
512                         unsigned long range = gain->maximum - gain->minimum;
513                         /* Valid values 16 to 64, 32 to 64 must be even. */
514                         unsigned long gain_val = ((gain->val - gain->minimum) *
515                                               48 + range / 2) / range + 16;
516
517                         if (gain_val >= 32)
518                                 gain_val &= ~1;
519
520                         /*
521                          * The user wants to set gain manually, hope, she
522                          * knows, what she's doing... Switch AGC off.
523                          */
524                         if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
525                                 return -EIO;
526
527                         dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
528                                 reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
529                         if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
530                                 return -EIO;
531                 }
532                 return 0;
533         case V4L2_CID_EXPOSURE_AUTO:
534                 if (ctrl->val == V4L2_EXPOSURE_AUTO) {
535                         data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
536                 } else {
537                         struct v4l2_ctrl *exp = mt9v022->exposure;
538                         unsigned long range = exp->maximum - exp->minimum;
539                         unsigned long shutter = ((exp->val - exp->minimum) *
540                                         479 + range / 2) / range + 1;
541
542                         /*
543                          * The user wants to set shutter width manually, hope,
544                          * she knows, what she's doing... Switch AEC off.
545                          */
546                         data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
547                         if (data < 0)
548                                 return -EIO;
549                         dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
550                                         reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
551                                         shutter);
552                         if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
553                                                 shutter) < 0)
554                                 return -EIO;
555                 }
556                 return 0;
557         }
558         return -EINVAL;
559 }
560
561 /*
562  * Interface active, can use i2c. If it fails, it can indeed mean, that
563  * this wasn't our capture interface, so, we wait for the right one
564  */
565 static int mt9v022_video_probe(struct soc_camera_device *icd,
566                                struct i2c_client *client)
567 {
568         struct mt9v022 *mt9v022 = to_mt9v022(client);
569         struct soc_camera_link *icl = to_soc_camera_link(icd);
570         s32 data;
571         int ret;
572         unsigned long flags;
573
574         /* We must have a parent by now. And it cannot be a wrong one. */
575         BUG_ON(!icd->parent ||
576                to_soc_camera_host(icd->parent)->nr != icd->iface);
577
578         /* Read out the chip version register */
579         data = reg_read(client, MT9V022_CHIP_VERSION);
580
581         /* must be 0x1311 or 0x1313 */
582         if (data != 0x1311 && data != 0x1313) {
583                 ret = -ENODEV;
584                 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
585                          data);
586                 goto ei2c;
587         }
588
589         /* Soft reset */
590         ret = reg_write(client, MT9V022_RESET, 1);
591         if (ret < 0)
592                 goto ei2c;
593         /* 15 clock cycles */
594         udelay(200);
595         if (reg_read(client, MT9V022_RESET)) {
596                 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
597                 if (ret > 0)
598                         ret = -EIO;
599                 goto ei2c;
600         }
601
602         /* Set monochrome or colour sensor type */
603         if (sensor_type && (!strcmp("colour", sensor_type) ||
604                             !strcmp("color", sensor_type))) {
605                 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
606                 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
607                 mt9v022->fmts = mt9v022_colour_fmts;
608         } else {
609                 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
610                 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
611                 mt9v022->fmts = mt9v022_monochrome_fmts;
612         }
613
614         if (ret < 0)
615                 goto ei2c;
616
617         mt9v022->num_fmts = 0;
618
619         /*
620          * This is a 10bit sensor, so by default we only allow 10bit.
621          * The platform may support different bus widths due to
622          * different routing of the data lines.
623          */
624         if (icl->query_bus_param)
625                 flags = icl->query_bus_param(icl);
626         else
627                 flags = SOCAM_DATAWIDTH_10;
628
629         if (flags & SOCAM_DATAWIDTH_10)
630                 mt9v022->num_fmts++;
631         else
632                 mt9v022->fmts++;
633
634         if (flags & SOCAM_DATAWIDTH_8)
635                 mt9v022->num_fmts++;
636
637         mt9v022->fmt = &mt9v022->fmts[0];
638
639         dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
640                  data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
641                  "monochrome" : "colour");
642
643         ret = mt9v022_init(client);
644         if (ret < 0)
645                 dev_err(&client->dev, "Failed to initialise the camera\n");
646
647 ei2c:
648         return ret;
649 }
650
651 static void mt9v022_video_remove(struct soc_camera_device *icd)
652 {
653         struct soc_camera_link *icl = to_soc_camera_link(icd);
654
655         dev_dbg(icd->pdev, "Video removed: %p, %p\n",
656                 icd->parent, icd->vdev);
657         if (icl->free_bus)
658                 icl->free_bus(icl);
659 }
660
661 static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
662 {
663         struct i2c_client *client = v4l2_get_subdevdata(sd);
664         struct mt9v022 *mt9v022 = to_mt9v022(client);
665
666         *lines = mt9v022->y_skip_top;
667
668         return 0;
669 }
670
671 static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
672         .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
673         .s_ctrl = mt9v022_s_ctrl,
674 };
675
676 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
677         .g_chip_ident   = mt9v022_g_chip_ident,
678 #ifdef CONFIG_VIDEO_ADV_DEBUG
679         .g_register     = mt9v022_g_register,
680         .s_register     = mt9v022_s_register,
681 #endif
682 };
683
684 static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
685                             enum v4l2_mbus_pixelcode *code)
686 {
687         struct i2c_client *client = v4l2_get_subdevdata(sd);
688         struct mt9v022 *mt9v022 = to_mt9v022(client);
689
690         if (index >= mt9v022->num_fmts)
691                 return -EINVAL;
692
693         *code = mt9v022->fmts[index].code;
694         return 0;
695 }
696
697 static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
698                                 struct v4l2_mbus_config *cfg)
699 {
700         struct i2c_client *client = v4l2_get_subdevdata(sd);
701         struct soc_camera_device *icd = client->dev.platform_data;
702         struct soc_camera_link *icl = to_soc_camera_link(icd);
703
704         cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
705                 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
706                 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
707                 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
708                 V4L2_MBUS_DATA_ACTIVE_HIGH;
709         cfg->type = V4L2_MBUS_PARALLEL;
710         cfg->flags = soc_camera_apply_board_flags(icl, cfg);
711
712         return 0;
713 }
714
715 static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
716                                  const struct v4l2_mbus_config *cfg)
717 {
718         struct i2c_client *client = v4l2_get_subdevdata(sd);
719         struct soc_camera_device *icd = client->dev.platform_data;
720         struct soc_camera_link *icl = to_soc_camera_link(icd);
721         struct mt9v022 *mt9v022 = to_mt9v022(client);
722         unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
723         /*
724          * Cannot use icd->current_fmt->host_fmt->bits_per_sample, because that
725          * is the number of bits, that the host has to sample, not the number of
726          * bits, that we have to send. See mx3_camera.c for an example of 10-bit
727          * formats being truncated to 8 bits by the host.
728          */
729         unsigned int bps = soc_mbus_get_fmtdesc(icd->current_fmt->code)->bits_per_sample;
730         int ret;
731         u16 pixclk = 0;
732
733         dev_info(icd->pdev, "set %d: %s, %dbps\n", icd->current_fmt->code,
734                  icd->current_fmt->host_fmt->name, bps);
735
736         if (icl->set_bus_param) {
737                 ret = icl->set_bus_param(icl, 1 << (bps - 1));
738                 if (ret)
739                         return ret;
740         } else if (bps != 10) {
741                 /*
742                  * Without board specific bus width settings we only support the
743                  * sensors native bus width
744                  */
745                 return -EINVAL;
746         }
747
748         if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
749                 pixclk |= 0x10;
750
751         if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
752                 pixclk |= 0x1;
753
754         if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
755                 pixclk |= 0x2;
756
757         ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
758         if (ret < 0)
759                 return ret;
760
761         if (!(flags & V4L2_MBUS_MASTER))
762                 mt9v022->chip_control &= ~0x8;
763
764         ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
765         if (ret < 0)
766                 return ret;
767
768         dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
769                 pixclk, mt9v022->chip_control);
770
771         return 0;
772 }
773
774 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
775         .s_stream       = mt9v022_s_stream,
776         .s_mbus_fmt     = mt9v022_s_fmt,
777         .g_mbus_fmt     = mt9v022_g_fmt,
778         .try_mbus_fmt   = mt9v022_try_fmt,
779         .s_crop         = mt9v022_s_crop,
780         .g_crop         = mt9v022_g_crop,
781         .cropcap        = mt9v022_cropcap,
782         .enum_mbus_fmt  = mt9v022_enum_fmt,
783         .g_mbus_config  = mt9v022_g_mbus_config,
784         .s_mbus_config  = mt9v022_s_mbus_config,
785 };
786
787 static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
788         .g_skip_top_lines       = mt9v022_g_skip_top_lines,
789 };
790
791 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
792         .core   = &mt9v022_subdev_core_ops,
793         .video  = &mt9v022_subdev_video_ops,
794         .sensor = &mt9v022_subdev_sensor_ops,
795 };
796
797 static int mt9v022_probe(struct i2c_client *client,
798                          const struct i2c_device_id *did)
799 {
800         struct mt9v022 *mt9v022;
801         struct soc_camera_device *icd = client->dev.platform_data;
802         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
803         struct soc_camera_link *icl;
804         int ret;
805
806         if (!icd) {
807                 dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
808                 return -EINVAL;
809         }
810
811         icl = to_soc_camera_link(icd);
812         if (!icl) {
813                 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
814                 return -EINVAL;
815         }
816
817         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
818                 dev_warn(&adapter->dev,
819                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
820                 return -EIO;
821         }
822
823         mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
824         if (!mt9v022)
825                 return -ENOMEM;
826
827         v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
828         v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
829         v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
830                         V4L2_CID_VFLIP, 0, 1, 1, 0);
831         v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
832                         V4L2_CID_HFLIP, 0, 1, 1, 0);
833         mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
834                         V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
835         mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
836                         V4L2_CID_GAIN, 0, 127, 1, 64);
837
838         /*
839          * Simulated autoexposure. If enabled, we calculate shutter width
840          * ourselves in the driver based on vertical blanking and frame width
841          */
842         mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
843                         &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
844                         V4L2_EXPOSURE_AUTO);
845         mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
846                         V4L2_CID_EXPOSURE, 1, 255, 1, 255);
847
848         mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
849         if (mt9v022->hdl.error) {
850                 int err = mt9v022->hdl.error;
851
852                 kfree(mt9v022);
853                 return err;
854         }
855         v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
856                                 V4L2_EXPOSURE_MANUAL, true);
857         v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
858
859         mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
860
861         /*
862          * MT9V022 _really_ corrupts the first read out line.
863          * TODO: verify on i.MX31
864          */
865         mt9v022->y_skip_top     = 1;
866         mt9v022->rect.left      = MT9V022_COLUMN_SKIP;
867         mt9v022->rect.top       = MT9V022_ROW_SKIP;
868         mt9v022->rect.width     = MT9V022_MAX_WIDTH;
869         mt9v022->rect.height    = MT9V022_MAX_HEIGHT;
870
871         ret = mt9v022_video_probe(icd, client);
872         if (ret) {
873                 v4l2_ctrl_handler_free(&mt9v022->hdl);
874                 kfree(mt9v022);
875         }
876
877         return ret;
878 }
879
880 static int mt9v022_remove(struct i2c_client *client)
881 {
882         struct mt9v022 *mt9v022 = to_mt9v022(client);
883         struct soc_camera_device *icd = client->dev.platform_data;
884
885         v4l2_device_unregister_subdev(&mt9v022->subdev);
886         mt9v022_video_remove(icd);
887         v4l2_ctrl_handler_free(&mt9v022->hdl);
888         kfree(mt9v022);
889
890         return 0;
891 }
892 static const struct i2c_device_id mt9v022_id[] = {
893         { "mt9v022", 0 },
894         { }
895 };
896 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
897
898 static struct i2c_driver mt9v022_i2c_driver = {
899         .driver = {
900                 .name = "mt9v022",
901         },
902         .probe          = mt9v022_probe,
903         .remove         = mt9v022_remove,
904         .id_table       = mt9v022_id,
905 };
906
907 static int __init mt9v022_mod_init(void)
908 {
909         return i2c_add_driver(&mt9v022_i2c_driver);
910 }
911
912 static void __exit mt9v022_mod_exit(void)
913 {
914         i2c_del_driver(&mt9v022_i2c_driver);
915 }
916
917 module_init(mt9v022_mod_init);
918 module_exit(mt9v022_mod_exit);
919
920 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
921 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
922 MODULE_LICENSE("GPL");