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