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