[media] V4L: soc-camera: remove soc-camera bus and devices on it
[pandora-kernel.git] / drivers / media / video / mt9m111.c
1 /*
2  * Driver for MT9M111/MT9M112/MT9M131 CMOS Image Sensor from Micron/Aptina
3  *
4  * Copyright (C) 2008, Robert Jarzmik <robert.jarzmik@free.fr>
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 #include <linux/videodev2.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/log2.h>
14 #include <linux/gpio.h>
15 #include <linux/delay.h>
16
17 #include <media/v4l2-common.h>
18 #include <media/v4l2-chip-ident.h>
19 #include <media/soc_camera.h>
20
21 /*
22  * MT9M111, MT9M112 and MT9M131:
23  * i2c address is 0x48 or 0x5d (depending on SADDR pin)
24  * The platform has to define i2c_board_info and call i2c_register_board_info()
25  */
26
27 /*
28  * Sensor core register addresses (0x000..0x0ff)
29  */
30 #define MT9M111_CHIP_VERSION            0x000
31 #define MT9M111_ROW_START               0x001
32 #define MT9M111_COLUMN_START            0x002
33 #define MT9M111_WINDOW_HEIGHT           0x003
34 #define MT9M111_WINDOW_WIDTH            0x004
35 #define MT9M111_HORIZONTAL_BLANKING_B   0x005
36 #define MT9M111_VERTICAL_BLANKING_B     0x006
37 #define MT9M111_HORIZONTAL_BLANKING_A   0x007
38 #define MT9M111_VERTICAL_BLANKING_A     0x008
39 #define MT9M111_SHUTTER_WIDTH           0x009
40 #define MT9M111_ROW_SPEED               0x00a
41 #define MT9M111_EXTRA_DELAY             0x00b
42 #define MT9M111_SHUTTER_DELAY           0x00c
43 #define MT9M111_RESET                   0x00d
44 #define MT9M111_READ_MODE_B             0x020
45 #define MT9M111_READ_MODE_A             0x021
46 #define MT9M111_FLASH_CONTROL           0x023
47 #define MT9M111_GREEN1_GAIN             0x02b
48 #define MT9M111_BLUE_GAIN               0x02c
49 #define MT9M111_RED_GAIN                0x02d
50 #define MT9M111_GREEN2_GAIN             0x02e
51 #define MT9M111_GLOBAL_GAIN             0x02f
52 #define MT9M111_CONTEXT_CONTROL         0x0c8
53 #define MT9M111_PAGE_MAP                0x0f0
54 #define MT9M111_BYTE_WISE_ADDR          0x0f1
55
56 #define MT9M111_RESET_SYNC_CHANGES      (1 << 15)
57 #define MT9M111_RESET_RESTART_BAD_FRAME (1 << 9)
58 #define MT9M111_RESET_SHOW_BAD_FRAMES   (1 << 8)
59 #define MT9M111_RESET_RESET_SOC         (1 << 5)
60 #define MT9M111_RESET_OUTPUT_DISABLE    (1 << 4)
61 #define MT9M111_RESET_CHIP_ENABLE       (1 << 3)
62 #define MT9M111_RESET_ANALOG_STANDBY    (1 << 2)
63 #define MT9M111_RESET_RESTART_FRAME     (1 << 1)
64 #define MT9M111_RESET_RESET_MODE        (1 << 0)
65
66 #define MT9M111_RMB_MIRROR_COLS         (1 << 1)
67 #define MT9M111_RMB_MIRROR_ROWS         (1 << 0)
68 #define MT9M111_CTXT_CTRL_RESTART       (1 << 15)
69 #define MT9M111_CTXT_CTRL_DEFECTCOR_B   (1 << 12)
70 #define MT9M111_CTXT_CTRL_RESIZE_B      (1 << 10)
71 #define MT9M111_CTXT_CTRL_CTRL2_B       (1 << 9)
72 #define MT9M111_CTXT_CTRL_GAMMA_B       (1 << 8)
73 #define MT9M111_CTXT_CTRL_XENON_EN      (1 << 7)
74 #define MT9M111_CTXT_CTRL_READ_MODE_B   (1 << 3)
75 #define MT9M111_CTXT_CTRL_LED_FLASH_EN  (1 << 2)
76 #define MT9M111_CTXT_CTRL_VBLANK_SEL_B  (1 << 1)
77 #define MT9M111_CTXT_CTRL_HBLANK_SEL_B  (1 << 0)
78
79 /*
80  * Colorpipe register addresses (0x100..0x1ff)
81  */
82 #define MT9M111_OPER_MODE_CTRL          0x106
83 #define MT9M111_OUTPUT_FORMAT_CTRL      0x108
84 #define MT9M111_REDUCER_XZOOM_B         0x1a0
85 #define MT9M111_REDUCER_XSIZE_B         0x1a1
86 #define MT9M111_REDUCER_YZOOM_B         0x1a3
87 #define MT9M111_REDUCER_YSIZE_B         0x1a4
88 #define MT9M111_REDUCER_XZOOM_A         0x1a6
89 #define MT9M111_REDUCER_XSIZE_A         0x1a7
90 #define MT9M111_REDUCER_YZOOM_A         0x1a9
91 #define MT9M111_REDUCER_YSIZE_A         0x1aa
92
93 #define MT9M111_OUTPUT_FORMAT_CTRL2_A   0x13a
94 #define MT9M111_OUTPUT_FORMAT_CTRL2_B   0x19b
95
96 #define MT9M111_OPMODE_AUTOEXPO_EN      (1 << 14)
97 #define MT9M111_OPMODE_AUTOWHITEBAL_EN  (1 << 1)
98
99 #define MT9M111_OUTFMT_PROCESSED_BAYER  (1 << 14)
100 #define MT9M111_OUTFMT_BYPASS_IFP       (1 << 10)
101 #define MT9M111_OUTFMT_INV_PIX_CLOCK    (1 << 9)
102 #define MT9M111_OUTFMT_RGB              (1 << 8)
103 #define MT9M111_OUTFMT_RGB565           (0 << 6)
104 #define MT9M111_OUTFMT_RGB555           (1 << 6)
105 #define MT9M111_OUTFMT_RGB444x          (2 << 6)
106 #define MT9M111_OUTFMT_RGBx444          (3 << 6)
107 #define MT9M111_OUTFMT_TST_RAMP_OFF     (0 << 4)
108 #define MT9M111_OUTFMT_TST_RAMP_COL     (1 << 4)
109 #define MT9M111_OUTFMT_TST_RAMP_ROW     (2 << 4)
110 #define MT9M111_OUTFMT_TST_RAMP_FRAME   (3 << 4)
111 #define MT9M111_OUTFMT_SHIFT_3_UP       (1 << 3)
112 #define MT9M111_OUTFMT_AVG_CHROMA       (1 << 2)
113 #define MT9M111_OUTFMT_SWAP_YCbCr_C_Y   (1 << 1)
114 #define MT9M111_OUTFMT_SWAP_RGB_EVEN    (1 << 1)
115 #define MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr (1 << 0)
116
117 /*
118  * Camera control register addresses (0x200..0x2ff not implemented)
119  */
120
121 #define reg_read(reg) mt9m111_reg_read(client, MT9M111_##reg)
122 #define reg_write(reg, val) mt9m111_reg_write(client, MT9M111_##reg, (val))
123 #define reg_set(reg, val) mt9m111_reg_set(client, MT9M111_##reg, (val))
124 #define reg_clear(reg, val) mt9m111_reg_clear(client, MT9M111_##reg, (val))
125
126 #define MT9M111_MIN_DARK_ROWS   8
127 #define MT9M111_MIN_DARK_COLS   26
128 #define MT9M111_MAX_HEIGHT      1024
129 #define MT9M111_MAX_WIDTH       1280
130
131 /* MT9M111 has only one fixed colorspace per pixelcode */
132 struct mt9m111_datafmt {
133         enum v4l2_mbus_pixelcode        code;
134         enum v4l2_colorspace            colorspace;
135 };
136
137 /* Find a data format by a pixel code in an array */
138 static const struct mt9m111_datafmt *mt9m111_find_datafmt(
139         enum v4l2_mbus_pixelcode code, const struct mt9m111_datafmt *fmt,
140         int n)
141 {
142         int i;
143         for (i = 0; i < n; i++)
144                 if (fmt[i].code == code)
145                         return fmt + i;
146
147         return NULL;
148 }
149
150 static const struct mt9m111_datafmt mt9m111_colour_fmts[] = {
151         {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG},
152         {V4L2_MBUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG},
153         {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
154         {V4L2_MBUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG},
155         {V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
156         {V4L2_MBUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB},
157         {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
158         {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
159 };
160
161 enum mt9m111_context {
162         HIGHPOWER = 0,
163         LOWPOWER,
164 };
165
166 struct mt9m111 {
167         struct v4l2_subdev subdev;
168         int model;      /* V4L2_IDENT_MT9M111 or V4L2_IDENT_MT9M112 code
169                          * from v4l2-chip-ident.h */
170         enum mt9m111_context context;
171         struct v4l2_rect rect;
172         struct mutex power_lock; /* lock to protect power_count */
173         int power_count;
174         const struct mt9m111_datafmt *fmt;
175         unsigned int gain;
176         unsigned char autoexposure;
177         unsigned char datawidth;
178         unsigned int powered:1;
179         unsigned int hflip:1;
180         unsigned int vflip:1;
181         unsigned int swap_rgb_even_odd:1;
182         unsigned int swap_rgb_red_blue:1;
183         unsigned int swap_yuv_y_chromas:1;
184         unsigned int swap_yuv_cb_cr:1;
185         unsigned int autowhitebalance:1;
186 };
187
188 static struct mt9m111 *to_mt9m111(const struct i2c_client *client)
189 {
190         return container_of(i2c_get_clientdata(client), struct mt9m111, subdev);
191 }
192
193 static int reg_page_map_set(struct i2c_client *client, const u16 reg)
194 {
195         int ret;
196         u16 page;
197         static int lastpage = -1;       /* PageMap cache value */
198
199         page = (reg >> 8);
200         if (page == lastpage)
201                 return 0;
202         if (page > 2)
203                 return -EINVAL;
204
205         ret = i2c_smbus_write_word_data(client, MT9M111_PAGE_MAP, swab16(page));
206         if (!ret)
207                 lastpage = page;
208         return ret;
209 }
210
211 static int mt9m111_reg_read(struct i2c_client *client, const u16 reg)
212 {
213         int ret;
214
215         ret = reg_page_map_set(client, reg);
216         if (!ret)
217                 ret = swab16(i2c_smbus_read_word_data(client, reg & 0xff));
218
219         dev_dbg(&client->dev, "read  reg.%03x -> %04x\n", reg, ret);
220         return ret;
221 }
222
223 static int mt9m111_reg_write(struct i2c_client *client, const u16 reg,
224                              const u16 data)
225 {
226         int ret;
227
228         ret = reg_page_map_set(client, reg);
229         if (!ret)
230                 ret = i2c_smbus_write_word_data(client, reg & 0xff,
231                                                 swab16(data));
232         dev_dbg(&client->dev, "write reg.%03x = %04x -> %d\n", reg, data, ret);
233         return ret;
234 }
235
236 static int mt9m111_reg_set(struct i2c_client *client, const u16 reg,
237                            const u16 data)
238 {
239         int ret;
240
241         ret = mt9m111_reg_read(client, reg);
242         if (ret >= 0)
243                 ret = mt9m111_reg_write(client, reg, ret | data);
244         return ret;
245 }
246
247 static int mt9m111_reg_clear(struct i2c_client *client, const u16 reg,
248                              const u16 data)
249 {
250         int ret;
251
252         ret = mt9m111_reg_read(client, reg);
253         return mt9m111_reg_write(client, reg, ret & ~data);
254 }
255
256 static int mt9m111_set_context(struct mt9m111 *mt9m111,
257                                enum mt9m111_context ctxt)
258 {
259         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
260         int valB = MT9M111_CTXT_CTRL_RESTART | MT9M111_CTXT_CTRL_DEFECTCOR_B
261                 | MT9M111_CTXT_CTRL_RESIZE_B | MT9M111_CTXT_CTRL_CTRL2_B
262                 | MT9M111_CTXT_CTRL_GAMMA_B | MT9M111_CTXT_CTRL_READ_MODE_B
263                 | MT9M111_CTXT_CTRL_VBLANK_SEL_B
264                 | MT9M111_CTXT_CTRL_HBLANK_SEL_B;
265         int valA = MT9M111_CTXT_CTRL_RESTART;
266
267         if (ctxt == HIGHPOWER)
268                 return reg_write(CONTEXT_CONTROL, valB);
269         else
270                 return reg_write(CONTEXT_CONTROL, valA);
271 }
272
273 static int mt9m111_setup_rect(struct mt9m111 *mt9m111,
274                               struct v4l2_rect *rect)
275 {
276         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
277         int ret, is_raw_format;
278         int width = rect->width;
279         int height = rect->height;
280
281         if (mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
282             mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE)
283                 is_raw_format = 1;
284         else
285                 is_raw_format = 0;
286
287         ret = reg_write(COLUMN_START, rect->left);
288         if (!ret)
289                 ret = reg_write(ROW_START, rect->top);
290
291         if (is_raw_format) {
292                 if (!ret)
293                         ret = reg_write(WINDOW_WIDTH, width);
294                 if (!ret)
295                         ret = reg_write(WINDOW_HEIGHT, height);
296         } else {
297                 if (!ret)
298                         ret = reg_write(REDUCER_XZOOM_B, MT9M111_MAX_WIDTH);
299                 if (!ret)
300                         ret = reg_write(REDUCER_YZOOM_B, MT9M111_MAX_HEIGHT);
301                 if (!ret)
302                         ret = reg_write(REDUCER_XSIZE_B, width);
303                 if (!ret)
304                         ret = reg_write(REDUCER_YSIZE_B, height);
305                 if (!ret)
306                         ret = reg_write(REDUCER_XZOOM_A, MT9M111_MAX_WIDTH);
307                 if (!ret)
308                         ret = reg_write(REDUCER_YZOOM_A, MT9M111_MAX_HEIGHT);
309                 if (!ret)
310                         ret = reg_write(REDUCER_XSIZE_A, width);
311                 if (!ret)
312                         ret = reg_write(REDUCER_YSIZE_A, height);
313         }
314
315         return ret;
316 }
317
318 static int mt9m111_setup_pixfmt(struct i2c_client *client, u16 outfmt)
319 {
320         int ret;
321         u16 mask = MT9M111_OUTFMT_PROCESSED_BAYER | MT9M111_OUTFMT_RGB |
322                 MT9M111_OUTFMT_BYPASS_IFP | MT9M111_OUTFMT_SWAP_RGB_EVEN |
323                 MT9M111_OUTFMT_RGB565 | MT9M111_OUTFMT_RGB555 |
324                 MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr |
325                 MT9M111_OUTFMT_SWAP_YCbCr_C_Y;
326
327         ret = reg_read(OUTPUT_FORMAT_CTRL2_A);
328         if (ret >= 0)
329                 ret = reg_write(OUTPUT_FORMAT_CTRL2_A, (ret & ~mask) | outfmt);
330         if (!ret)
331                 ret = reg_read(OUTPUT_FORMAT_CTRL2_B);
332         if (ret >= 0)
333                 ret = reg_write(OUTPUT_FORMAT_CTRL2_B, (ret & ~mask) | outfmt);
334
335         return ret;
336 }
337
338 static int mt9m111_setfmt_bayer8(struct mt9m111 *mt9m111)
339 {
340         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
341
342         return mt9m111_setup_pixfmt(client, MT9M111_OUTFMT_PROCESSED_BAYER |
343                                     MT9M111_OUTFMT_RGB);
344 }
345
346 static int mt9m111_setfmt_bayer10(struct mt9m111 *mt9m111)
347 {
348         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
349
350         return mt9m111_setup_pixfmt(client, MT9M111_OUTFMT_BYPASS_IFP);
351 }
352
353 static int mt9m111_setfmt_rgb565(struct mt9m111 *mt9m111)
354 {
355         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
356         int val = MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB565;
357
358         if (mt9m111->swap_rgb_red_blue)
359                 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
360         if (mt9m111->swap_rgb_even_odd)
361                 val |= MT9M111_OUTFMT_SWAP_RGB_EVEN;
362
363         return mt9m111_setup_pixfmt(client, val);
364 }
365
366 static int mt9m111_setfmt_rgb555(struct mt9m111 *mt9m111)
367 {
368         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
369         int val = MT9M111_OUTFMT_RGB | MT9M111_OUTFMT_RGB555;
370
371         if (mt9m111->swap_rgb_red_blue)
372                 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
373         if (mt9m111->swap_rgb_even_odd)
374                 val |= MT9M111_OUTFMT_SWAP_RGB_EVEN;
375
376         return mt9m111_setup_pixfmt(client, val);
377 }
378
379 static int mt9m111_setfmt_yuv(struct mt9m111 *mt9m111)
380 {
381         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
382         int val = 0;
383
384         if (mt9m111->swap_yuv_cb_cr)
385                 val |= MT9M111_OUTFMT_SWAP_YCbCr_Cb_Cr;
386         if (mt9m111->swap_yuv_y_chromas)
387                 val |= MT9M111_OUTFMT_SWAP_YCbCr_C_Y;
388
389         return mt9m111_setup_pixfmt(client, val);
390 }
391
392 static int mt9m111_enable(struct mt9m111 *mt9m111)
393 {
394         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
395         int ret;
396
397         ret = reg_set(RESET, MT9M111_RESET_CHIP_ENABLE);
398         if (!ret)
399                 mt9m111->powered = 1;
400         return ret;
401 }
402
403 static int mt9m111_reset(struct mt9m111 *mt9m111)
404 {
405         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
406         int ret;
407
408         ret = reg_set(RESET, MT9M111_RESET_RESET_MODE);
409         if (!ret)
410                 ret = reg_set(RESET, MT9M111_RESET_RESET_SOC);
411         if (!ret)
412                 ret = reg_clear(RESET, MT9M111_RESET_RESET_MODE
413                                 | MT9M111_RESET_RESET_SOC);
414
415         return ret;
416 }
417
418 static unsigned long mt9m111_query_bus_param(struct soc_camera_device *icd)
419 {
420         struct soc_camera_link *icl = to_soc_camera_link(icd);
421         unsigned long flags = SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |
422                 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
423                 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8;
424
425         return soc_camera_apply_sensor_flags(icl, flags);
426 }
427
428 static int mt9m111_set_bus_param(struct soc_camera_device *icd, unsigned long f)
429 {
430         return 0;
431 }
432
433 static int mt9m111_make_rect(struct mt9m111 *mt9m111,
434                              struct v4l2_rect *rect)
435 {
436         if (mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
437             mt9m111->fmt->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE) {
438                 /* Bayer format - even size lengths */
439                 rect->width     = ALIGN(rect->width, 2);
440                 rect->height    = ALIGN(rect->height, 2);
441                 /* Let the user play with the starting pixel */
442         }
443
444         /* FIXME: the datasheet doesn't specify minimum sizes */
445         soc_camera_limit_side(&rect->left, &rect->width,
446                      MT9M111_MIN_DARK_COLS, 2, MT9M111_MAX_WIDTH);
447
448         soc_camera_limit_side(&rect->top, &rect->height,
449                      MT9M111_MIN_DARK_ROWS, 2, MT9M111_MAX_HEIGHT);
450
451         return mt9m111_setup_rect(mt9m111, rect);
452 }
453
454 static int mt9m111_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
455 {
456         struct v4l2_rect rect = a->c;
457         struct i2c_client *client = v4l2_get_subdevdata(sd);
458         struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
459         int ret;
460
461         dev_dbg(&client->dev, "%s left=%d, top=%d, width=%d, height=%d\n",
462                 __func__, rect.left, rect.top, rect.width, rect.height);
463
464         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
465                 return -EINVAL;
466
467         ret = mt9m111_make_rect(mt9m111, &rect);
468         if (!ret)
469                 mt9m111->rect = rect;
470         return ret;
471 }
472
473 static int mt9m111_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
474 {
475         struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
476
477         a->c    = mt9m111->rect;
478         a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
479
480         return 0;
481 }
482
483 static int mt9m111_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
484 {
485         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
486                 return -EINVAL;
487
488         a->bounds.left                  = MT9M111_MIN_DARK_COLS;
489         a->bounds.top                   = MT9M111_MIN_DARK_ROWS;
490         a->bounds.width                 = MT9M111_MAX_WIDTH;
491         a->bounds.height                = MT9M111_MAX_HEIGHT;
492         a->defrect                      = a->bounds;
493         a->pixelaspect.numerator        = 1;
494         a->pixelaspect.denominator      = 1;
495
496         return 0;
497 }
498
499 static int mt9m111_g_fmt(struct v4l2_subdev *sd,
500                          struct v4l2_mbus_framefmt *mf)
501 {
502         struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
503
504         mf->width       = mt9m111->rect.width;
505         mf->height      = mt9m111->rect.height;
506         mf->code        = mt9m111->fmt->code;
507         mf->colorspace  = mt9m111->fmt->colorspace;
508         mf->field       = V4L2_FIELD_NONE;
509
510         return 0;
511 }
512
513 static int mt9m111_set_pixfmt(struct mt9m111 *mt9m111,
514                               enum v4l2_mbus_pixelcode code)
515 {
516         struct i2c_client *client;
517         int ret;
518
519         switch (code) {
520         case V4L2_MBUS_FMT_SBGGR8_1X8:
521                 ret = mt9m111_setfmt_bayer8(mt9m111);
522                 break;
523         case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE:
524                 ret = mt9m111_setfmt_bayer10(mt9m111);
525                 break;
526         case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
527                 ret = mt9m111_setfmt_rgb555(mt9m111);
528                 break;
529         case V4L2_MBUS_FMT_RGB565_2X8_LE:
530                 ret = mt9m111_setfmt_rgb565(mt9m111);
531                 break;
532         case V4L2_MBUS_FMT_UYVY8_2X8:
533                 mt9m111->swap_yuv_y_chromas = 0;
534                 mt9m111->swap_yuv_cb_cr = 0;
535                 ret = mt9m111_setfmt_yuv(mt9m111);
536                 break;
537         case V4L2_MBUS_FMT_VYUY8_2X8:
538                 mt9m111->swap_yuv_y_chromas = 0;
539                 mt9m111->swap_yuv_cb_cr = 1;
540                 ret = mt9m111_setfmt_yuv(mt9m111);
541                 break;
542         case V4L2_MBUS_FMT_YUYV8_2X8:
543                 mt9m111->swap_yuv_y_chromas = 1;
544                 mt9m111->swap_yuv_cb_cr = 0;
545                 ret = mt9m111_setfmt_yuv(mt9m111);
546                 break;
547         case V4L2_MBUS_FMT_YVYU8_2X8:
548                 mt9m111->swap_yuv_y_chromas = 1;
549                 mt9m111->swap_yuv_cb_cr = 1;
550                 ret = mt9m111_setfmt_yuv(mt9m111);
551                 break;
552         default:
553                 client = v4l2_get_subdevdata(&mt9m111->subdev);
554                 dev_err(&client->dev, "Pixel format not handled : %x\n",
555                         code);
556                 ret = -EINVAL;
557         }
558
559         return ret;
560 }
561
562 static int mt9m111_s_fmt(struct v4l2_subdev *sd,
563                          struct v4l2_mbus_framefmt *mf)
564 {
565         struct i2c_client *client = v4l2_get_subdevdata(sd);
566         const struct mt9m111_datafmt *fmt;
567         struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
568         struct v4l2_rect rect = {
569                 .left   = mt9m111->rect.left,
570                 .top    = mt9m111->rect.top,
571                 .width  = mf->width,
572                 .height = mf->height,
573         };
574         int ret;
575
576         fmt = mt9m111_find_datafmt(mf->code, mt9m111_colour_fmts,
577                                    ARRAY_SIZE(mt9m111_colour_fmts));
578         if (!fmt)
579                 return -EINVAL;
580
581         dev_dbg(&client->dev,
582                 "%s code=%x left=%d, top=%d, width=%d, height=%d\n", __func__,
583                 mf->code, rect.left, rect.top, rect.width, rect.height);
584
585         ret = mt9m111_make_rect(mt9m111, &rect);
586         if (!ret)
587                 ret = mt9m111_set_pixfmt(mt9m111, mf->code);
588         if (!ret) {
589                 mt9m111->rect   = rect;
590                 mt9m111->fmt    = fmt;
591                 mf->colorspace  = fmt->colorspace;
592         }
593
594         return ret;
595 }
596
597 static int mt9m111_try_fmt(struct v4l2_subdev *sd,
598                            struct v4l2_mbus_framefmt *mf)
599 {
600         struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
601         const struct mt9m111_datafmt *fmt;
602         bool bayer = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
603                 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE;
604
605         fmt = mt9m111_find_datafmt(mf->code, mt9m111_colour_fmts,
606                                    ARRAY_SIZE(mt9m111_colour_fmts));
607         if (!fmt) {
608                 fmt = mt9m111->fmt;
609                 mf->code = fmt->code;
610         }
611
612         /*
613          * With Bayer format enforce even side lengths, but let the user play
614          * with the starting pixel
615          */
616
617         if (mf->height > MT9M111_MAX_HEIGHT)
618                 mf->height = MT9M111_MAX_HEIGHT;
619         else if (mf->height < 2)
620                 mf->height = 2;
621         else if (bayer)
622                 mf->height = ALIGN(mf->height, 2);
623
624         if (mf->width > MT9M111_MAX_WIDTH)
625                 mf->width = MT9M111_MAX_WIDTH;
626         else if (mf->width < 2)
627                 mf->width = 2;
628         else if (bayer)
629                 mf->width = ALIGN(mf->width, 2);
630
631         mf->colorspace = fmt->colorspace;
632
633         return 0;
634 }
635
636 static int mt9m111_g_chip_ident(struct v4l2_subdev *sd,
637                                 struct v4l2_dbg_chip_ident *id)
638 {
639         struct i2c_client *client = v4l2_get_subdevdata(sd);
640         struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
641
642         if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
643                 return -EINVAL;
644
645         if (id->match.addr != client->addr)
646                 return -ENODEV;
647
648         id->ident       = mt9m111->model;
649         id->revision    = 0;
650
651         return 0;
652 }
653
654 #ifdef CONFIG_VIDEO_ADV_DEBUG
655 static int mt9m111_g_register(struct v4l2_subdev *sd,
656                               struct v4l2_dbg_register *reg)
657 {
658         struct i2c_client *client = v4l2_get_subdevdata(sd);
659         int val;
660
661         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
662                 return -EINVAL;
663         if (reg->match.addr != client->addr)
664                 return -ENODEV;
665
666         val = mt9m111_reg_read(client, reg->reg);
667         reg->size = 2;
668         reg->val = (u64)val;
669
670         if (reg->val > 0xffff)
671                 return -EIO;
672
673         return 0;
674 }
675
676 static int mt9m111_s_register(struct v4l2_subdev *sd,
677                               struct v4l2_dbg_register *reg)
678 {
679         struct i2c_client *client = v4l2_get_subdevdata(sd);
680
681         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)
682                 return -EINVAL;
683
684         if (reg->match.addr != client->addr)
685                 return -ENODEV;
686
687         if (mt9m111_reg_write(client, reg->reg, reg->val) < 0)
688                 return -EIO;
689
690         return 0;
691 }
692 #endif
693
694 static const struct v4l2_queryctrl mt9m111_controls[] = {
695         {
696                 .id             = V4L2_CID_VFLIP,
697                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
698                 .name           = "Flip Verticaly",
699                 .minimum        = 0,
700                 .maximum        = 1,
701                 .step           = 1,
702                 .default_value  = 0,
703         }, {
704                 .id             = V4L2_CID_HFLIP,
705                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
706                 .name           = "Flip Horizontaly",
707                 .minimum        = 0,
708                 .maximum        = 1,
709                 .step           = 1,
710                 .default_value  = 0,
711         }, {    /* gain = 1/32*val (=>gain=1 if val==32) */
712                 .id             = V4L2_CID_GAIN,
713                 .type           = V4L2_CTRL_TYPE_INTEGER,
714                 .name           = "Gain",
715                 .minimum        = 0,
716                 .maximum        = 63 * 2 * 2,
717                 .step           = 1,
718                 .default_value  = 32,
719                 .flags          = V4L2_CTRL_FLAG_SLIDER,
720         }, {
721                 .id             = V4L2_CID_EXPOSURE_AUTO,
722                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
723                 .name           = "Auto Exposure",
724                 .minimum        = 0,
725                 .maximum        = 1,
726                 .step           = 1,
727                 .default_value  = 1,
728         }
729 };
730
731 static struct soc_camera_ops mt9m111_ops = {
732         .query_bus_param        = mt9m111_query_bus_param,
733         .set_bus_param          = mt9m111_set_bus_param,
734         .controls               = mt9m111_controls,
735         .num_controls           = ARRAY_SIZE(mt9m111_controls),
736 };
737
738 static int mt9m111_set_flip(struct mt9m111 *mt9m111, int flip, int mask)
739 {
740         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
741         int ret;
742
743         if (mt9m111->context == HIGHPOWER) {
744                 if (flip)
745                         ret = reg_set(READ_MODE_B, mask);
746                 else
747                         ret = reg_clear(READ_MODE_B, mask);
748         } else {
749                 if (flip)
750                         ret = reg_set(READ_MODE_A, mask);
751                 else
752                         ret = reg_clear(READ_MODE_A, mask);
753         }
754
755         return ret;
756 }
757
758 static int mt9m111_get_global_gain(struct mt9m111 *mt9m111)
759 {
760         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
761         int data;
762
763         data = reg_read(GLOBAL_GAIN);
764         if (data >= 0)
765                 return (data & 0x2f) * (1 << ((data >> 10) & 1)) *
766                         (1 << ((data >> 9) & 1));
767         return data;
768 }
769
770 static int mt9m111_set_global_gain(struct mt9m111 *mt9m111, int gain)
771 {
772         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
773         u16 val;
774
775         if (gain > 63 * 2 * 2)
776                 return -EINVAL;
777
778         mt9m111->gain = gain;
779         if ((gain >= 64 * 2) && (gain < 63 * 2 * 2))
780                 val = (1 << 10) | (1 << 9) | (gain / 4);
781         else if ((gain >= 64) && (gain < 64 * 2))
782                 val = (1 << 9) | (gain / 2);
783         else
784                 val = gain;
785
786         return reg_write(GLOBAL_GAIN, val);
787 }
788
789 static int mt9m111_set_autoexposure(struct mt9m111 *mt9m111, int on)
790 {
791         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
792         int ret;
793
794         if (on)
795                 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
796         else
797                 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);
798
799         if (!ret)
800                 mt9m111->autoexposure = on;
801
802         return ret;
803 }
804
805 static int mt9m111_set_autowhitebalance(struct mt9m111 *mt9m111, int on)
806 {
807         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
808         int ret;
809
810         if (on)
811                 ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
812         else
813                 ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOWHITEBAL_EN);
814
815         if (!ret)
816                 mt9m111->autowhitebalance = on;
817
818         return ret;
819 }
820
821 static int mt9m111_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
822 {
823         struct i2c_client *client = v4l2_get_subdevdata(sd);
824         struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
825         int data;
826
827         switch (ctrl->id) {
828         case V4L2_CID_VFLIP:
829                 if (mt9m111->context == HIGHPOWER)
830                         data = reg_read(READ_MODE_B);
831                 else
832                         data = reg_read(READ_MODE_A);
833
834                 if (data < 0)
835                         return -EIO;
836                 ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS);
837                 break;
838         case V4L2_CID_HFLIP:
839                 if (mt9m111->context == HIGHPOWER)
840                         data = reg_read(READ_MODE_B);
841                 else
842                         data = reg_read(READ_MODE_A);
843
844                 if (data < 0)
845                         return -EIO;
846                 ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS);
847                 break;
848         case V4L2_CID_GAIN:
849                 data = mt9m111_get_global_gain(mt9m111);
850                 if (data < 0)
851                         return data;
852                 ctrl->value = data;
853                 break;
854         case V4L2_CID_EXPOSURE_AUTO:
855                 ctrl->value = mt9m111->autoexposure;
856                 break;
857         case V4L2_CID_AUTO_WHITE_BALANCE:
858                 ctrl->value = mt9m111->autowhitebalance;
859                 break;
860         }
861         return 0;
862 }
863
864 static int mt9m111_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
865 {
866         struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
867         const struct v4l2_queryctrl *qctrl;
868         int ret;
869
870         qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id);
871         if (!qctrl)
872                 return -EINVAL;
873
874         switch (ctrl->id) {
875         case V4L2_CID_VFLIP:
876                 mt9m111->vflip = ctrl->value;
877                 ret = mt9m111_set_flip(mt9m111, ctrl->value,
878                                         MT9M111_RMB_MIRROR_ROWS);
879                 break;
880         case V4L2_CID_HFLIP:
881                 mt9m111->hflip = ctrl->value;
882                 ret = mt9m111_set_flip(mt9m111, ctrl->value,
883                                         MT9M111_RMB_MIRROR_COLS);
884                 break;
885         case V4L2_CID_GAIN:
886                 ret = mt9m111_set_global_gain(mt9m111, ctrl->value);
887                 break;
888         case V4L2_CID_EXPOSURE_AUTO:
889                 ret =  mt9m111_set_autoexposure(mt9m111, ctrl->value);
890                 break;
891         case V4L2_CID_AUTO_WHITE_BALANCE:
892                 ret =  mt9m111_set_autowhitebalance(mt9m111, ctrl->value);
893                 break;
894         default:
895                 ret = -EINVAL;
896         }
897
898         return ret;
899 }
900
901 static int mt9m111_suspend(struct mt9m111 *mt9m111)
902 {
903         mt9m111->gain = mt9m111_get_global_gain(mt9m111);
904
905         return 0;
906 }
907
908 static void mt9m111_restore_state(struct mt9m111 *mt9m111)
909 {
910         mt9m111_set_context(mt9m111, mt9m111->context);
911         mt9m111_set_pixfmt(mt9m111, mt9m111->fmt->code);
912         mt9m111_setup_rect(mt9m111, &mt9m111->rect);
913         mt9m111_set_flip(mt9m111, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS);
914         mt9m111_set_flip(mt9m111, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS);
915         mt9m111_set_global_gain(mt9m111, mt9m111->gain);
916         mt9m111_set_autoexposure(mt9m111, mt9m111->autoexposure);
917         mt9m111_set_autowhitebalance(mt9m111, mt9m111->autowhitebalance);
918 }
919
920 static int mt9m111_resume(struct mt9m111 *mt9m111)
921 {
922         int ret = 0;
923
924         if (mt9m111->powered) {
925                 ret = mt9m111_enable(mt9m111);
926                 if (!ret)
927                         ret = mt9m111_reset(mt9m111);
928                 if (!ret)
929                         mt9m111_restore_state(mt9m111);
930         }
931         return ret;
932 }
933
934 static int mt9m111_init(struct mt9m111 *mt9m111)
935 {
936         struct i2c_client *client = v4l2_get_subdevdata(&mt9m111->subdev);
937         int ret;
938
939         mt9m111->context = HIGHPOWER;
940         ret = mt9m111_enable(mt9m111);
941         if (!ret)
942                 ret = mt9m111_reset(mt9m111);
943         if (!ret)
944                 ret = mt9m111_set_context(mt9m111, mt9m111->context);
945         if (!ret)
946                 ret = mt9m111_set_autoexposure(mt9m111, mt9m111->autoexposure);
947         if (ret)
948                 dev_err(&client->dev, "mt9m111 init failed: %d\n", ret);
949         return ret;
950 }
951
952 /*
953  * Interface active, can use i2c. If it fails, it can indeed mean, that
954  * this wasn't our capture interface, so, we wait for the right one
955  */
956 static int mt9m111_video_probe(struct soc_camera_device *icd,
957                                struct i2c_client *client)
958 {
959         struct mt9m111 *mt9m111 = to_mt9m111(client);
960         s32 data;
961         int ret;
962
963         /* We must have a parent by now. And it cannot be a wrong one. */
964         BUG_ON(!icd->parent ||
965                to_soc_camera_host(icd->parent)->nr != icd->iface);
966
967         mt9m111->autoexposure = 1;
968         mt9m111->autowhitebalance = 1;
969
970         mt9m111->swap_rgb_even_odd = 1;
971         mt9m111->swap_rgb_red_blue = 1;
972
973         data = reg_read(CHIP_VERSION);
974
975         switch (data) {
976         case 0x143a: /* MT9M111 or MT9M131 */
977                 mt9m111->model = V4L2_IDENT_MT9M111;
978                 dev_info(&client->dev,
979                         "Detected a MT9M111/MT9M131 chip ID %x\n", data);
980                 break;
981         case 0x148c: /* MT9M112 */
982                 mt9m111->model = V4L2_IDENT_MT9M112;
983                 dev_info(&client->dev, "Detected a MT9M112 chip ID %x\n", data);
984                 break;
985         default:
986                 ret = -ENODEV;
987                 dev_err(&client->dev,
988                         "No MT9M111/MT9M112/MT9M131 chip detected register read %x\n",
989                         data);
990                 goto ei2c;
991         }
992
993         ret = mt9m111_init(mt9m111);
994
995 ei2c:
996         return ret;
997 }
998
999 static int mt9m111_s_power(struct v4l2_subdev *sd, int on)
1000 {
1001         struct mt9m111 *mt9m111 = container_of(sd, struct mt9m111, subdev);
1002         struct i2c_client *client = v4l2_get_subdevdata(sd);
1003         int ret = 0;
1004
1005         mutex_lock(&mt9m111->power_lock);
1006
1007         /*
1008          * If the power count is modified from 0 to != 0 or from != 0 to 0,
1009          * update the power state.
1010          */
1011         if (mt9m111->power_count == !on) {
1012                 if (on) {
1013                         ret = mt9m111_resume(mt9m111);
1014                         if (ret) {
1015                                 dev_err(&client->dev,
1016                                         "Failed to resume the sensor: %d\n", ret);
1017                                 goto out;
1018                         }
1019                 } else {
1020                         mt9m111_suspend(mt9m111);
1021                 }
1022         }
1023
1024         /* Update the power count. */
1025         mt9m111->power_count += on ? 1 : -1;
1026         WARN_ON(mt9m111->power_count < 0);
1027
1028 out:
1029         mutex_unlock(&mt9m111->power_lock);
1030         return ret;
1031 }
1032
1033 static struct v4l2_subdev_core_ops mt9m111_subdev_core_ops = {
1034         .g_ctrl         = mt9m111_g_ctrl,
1035         .s_ctrl         = mt9m111_s_ctrl,
1036         .g_chip_ident   = mt9m111_g_chip_ident,
1037         .s_power        = mt9m111_s_power,
1038 #ifdef CONFIG_VIDEO_ADV_DEBUG
1039         .g_register     = mt9m111_g_register,
1040         .s_register     = mt9m111_s_register,
1041 #endif
1042 };
1043
1044 static int mt9m111_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
1045                             enum v4l2_mbus_pixelcode *code)
1046 {
1047         if (index >= ARRAY_SIZE(mt9m111_colour_fmts))
1048                 return -EINVAL;
1049
1050         *code = mt9m111_colour_fmts[index].code;
1051         return 0;
1052 }
1053
1054 static struct v4l2_subdev_video_ops mt9m111_subdev_video_ops = {
1055         .s_mbus_fmt     = mt9m111_s_fmt,
1056         .g_mbus_fmt     = mt9m111_g_fmt,
1057         .try_mbus_fmt   = mt9m111_try_fmt,
1058         .s_crop         = mt9m111_s_crop,
1059         .g_crop         = mt9m111_g_crop,
1060         .cropcap        = mt9m111_cropcap,
1061         .enum_mbus_fmt  = mt9m111_enum_fmt,
1062 };
1063
1064 static struct v4l2_subdev_ops mt9m111_subdev_ops = {
1065         .core   = &mt9m111_subdev_core_ops,
1066         .video  = &mt9m111_subdev_video_ops,
1067 };
1068
1069 static int mt9m111_probe(struct i2c_client *client,
1070                          const struct i2c_device_id *did)
1071 {
1072         struct mt9m111 *mt9m111;
1073         struct soc_camera_device *icd = client->dev.platform_data;
1074         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
1075         struct soc_camera_link *icl;
1076         int ret;
1077
1078         if (!icd) {
1079                 dev_err(&client->dev, "mt9m111: soc-camera data missing!\n");
1080                 return -EINVAL;
1081         }
1082
1083         icl = to_soc_camera_link(icd);
1084         if (!icl) {
1085                 dev_err(&client->dev, "mt9m111: driver needs platform data\n");
1086                 return -EINVAL;
1087         }
1088
1089         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
1090                 dev_warn(&adapter->dev,
1091                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
1092                 return -EIO;
1093         }
1094
1095         mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL);
1096         if (!mt9m111)
1097                 return -ENOMEM;
1098
1099         v4l2_i2c_subdev_init(&mt9m111->subdev, client, &mt9m111_subdev_ops);
1100
1101         /* Second stage probe - when a capture adapter is there */
1102         icd->ops                = &mt9m111_ops;
1103
1104         mt9m111->rect.left      = MT9M111_MIN_DARK_COLS;
1105         mt9m111->rect.top       = MT9M111_MIN_DARK_ROWS;
1106         mt9m111->rect.width     = MT9M111_MAX_WIDTH;
1107         mt9m111->rect.height    = MT9M111_MAX_HEIGHT;
1108         mt9m111->fmt            = &mt9m111_colour_fmts[0];
1109
1110         ret = mt9m111_video_probe(icd, client);
1111         if (ret) {
1112                 icd->ops = NULL;
1113                 kfree(mt9m111);
1114         }
1115
1116         return ret;
1117 }
1118
1119 static int mt9m111_remove(struct i2c_client *client)
1120 {
1121         struct mt9m111 *mt9m111 = to_mt9m111(client);
1122         struct soc_camera_device *icd = client->dev.platform_data;
1123
1124         icd->ops = NULL;
1125         kfree(mt9m111);
1126
1127         return 0;
1128 }
1129
1130 static const struct i2c_device_id mt9m111_id[] = {
1131         { "mt9m111", 0 },
1132         { }
1133 };
1134 MODULE_DEVICE_TABLE(i2c, mt9m111_id);
1135
1136 static struct i2c_driver mt9m111_i2c_driver = {
1137         .driver = {
1138                 .name = "mt9m111",
1139         },
1140         .probe          = mt9m111_probe,
1141         .remove         = mt9m111_remove,
1142         .id_table       = mt9m111_id,
1143 };
1144
1145 static int __init mt9m111_mod_init(void)
1146 {
1147         return i2c_add_driver(&mt9m111_i2c_driver);
1148 }
1149
1150 static void __exit mt9m111_mod_exit(void)
1151 {
1152         i2c_del_driver(&mt9m111_i2c_driver);
1153 }
1154
1155 module_init(mt9m111_mod_init);
1156 module_exit(mt9m111_mod_exit);
1157
1158 MODULE_DESCRIPTION("Micron/Aptina MT9M111/MT9M112/MT9M131 Camera driver");
1159 MODULE_AUTHOR("Robert Jarzmik");
1160 MODULE_LICENSE("GPL");