gpio/langwell_gpio: ack the correct bit for langwell gpio interrupts
[pandora-kernel.git] / drivers / media / video / m5mols / m5mols_core.c
1 /*
2  * Driver for M-5MOLS 8M Pixel camera sensor with ISP
3  *
4  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5  * Author: HeungJun Kim, riverful.kim@samsung.com
6  *
7  * Copyright (C) 2009 Samsung Electronics Co., Ltd.
8  * Author: Dongsoo Nathaniel Kim, dongsoo45.kim@samsung.com
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  */
15
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <linux/version.h>
22 #include <linux/gpio.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-subdev.h>
28 #include <media/m5mols.h>
29
30 #include "m5mols.h"
31 #include "m5mols_reg.h"
32
33 int m5mols_debug;
34 module_param(m5mols_debug, int, 0644);
35
36 #define MODULE_NAME             "M5MOLS"
37 #define M5MOLS_I2C_CHECK_RETRY  500
38
39 /* The regulator consumer names for external voltage regulators */
40 static struct regulator_bulk_data supplies[] = {
41         {
42                 .supply = "core",       /* ARM core power, 1.2V */
43         }, {
44                 .supply = "dig_18",     /* digital power 1, 1.8V */
45         }, {
46                 .supply = "d_sensor",   /* sensor power 1, 1.8V */
47         }, {
48                 .supply = "dig_28",     /* digital power 2, 2.8V */
49         }, {
50                 .supply = "a_sensor",   /* analog power */
51         }, {
52                 .supply = "dig_12",     /* digital power 3, 1.2V */
53         },
54 };
55
56 static struct v4l2_mbus_framefmt m5mols_default_ffmt[M5MOLS_RESTYPE_MAX] = {
57         [M5MOLS_RESTYPE_MONITOR] = {
58                 .width          = 1920,
59                 .height         = 1080,
60                 .code           = V4L2_MBUS_FMT_VYUY8_2X8,
61                 .field          = V4L2_FIELD_NONE,
62                 .colorspace     = V4L2_COLORSPACE_JPEG,
63         },
64         [M5MOLS_RESTYPE_CAPTURE] = {
65                 .width          = 1920,
66                 .height         = 1080,
67                 .code           = V4L2_MBUS_FMT_JPEG_1X8,
68                 .field          = V4L2_FIELD_NONE,
69                 .colorspace     = V4L2_COLORSPACE_JPEG,
70         },
71 };
72 #define SIZE_DEFAULT_FFMT       ARRAY_SIZE(m5mols_default_ffmt)
73
74 static const struct m5mols_resolution m5mols_reg_res[] = {
75         { 0x01, M5MOLS_RESTYPE_MONITOR, 128, 96 },      /* SUB-QCIF */
76         { 0x03, M5MOLS_RESTYPE_MONITOR, 160, 120 },     /* QQVGA */
77         { 0x05, M5MOLS_RESTYPE_MONITOR, 176, 144 },     /* QCIF */
78         { 0x06, M5MOLS_RESTYPE_MONITOR, 176, 176 },
79         { 0x08, M5MOLS_RESTYPE_MONITOR, 240, 320 },     /* QVGA */
80         { 0x09, M5MOLS_RESTYPE_MONITOR, 320, 240 },     /* QVGA */
81         { 0x0c, M5MOLS_RESTYPE_MONITOR, 240, 400 },     /* WQVGA */
82         { 0x0d, M5MOLS_RESTYPE_MONITOR, 400, 240 },     /* WQVGA */
83         { 0x0e, M5MOLS_RESTYPE_MONITOR, 352, 288 },     /* CIF */
84         { 0x13, M5MOLS_RESTYPE_MONITOR, 480, 360 },
85         { 0x15, M5MOLS_RESTYPE_MONITOR, 640, 360 },     /* qHD */
86         { 0x17, M5MOLS_RESTYPE_MONITOR, 640, 480 },     /* VGA */
87         { 0x18, M5MOLS_RESTYPE_MONITOR, 720, 480 },
88         { 0x1a, M5MOLS_RESTYPE_MONITOR, 800, 480 },     /* WVGA */
89         { 0x1f, M5MOLS_RESTYPE_MONITOR, 800, 600 },     /* SVGA */
90         { 0x21, M5MOLS_RESTYPE_MONITOR, 1280, 720 },    /* HD */
91         { 0x25, M5MOLS_RESTYPE_MONITOR, 1920, 1080 },   /* 1080p */
92         { 0x29, M5MOLS_RESTYPE_MONITOR, 3264, 2448 },   /* 2.63fps 8M */
93         { 0x39, M5MOLS_RESTYPE_MONITOR, 800, 602 },     /* AHS_MON debug */
94
95         { 0x02, M5MOLS_RESTYPE_CAPTURE, 320, 240 },     /* QVGA */
96         { 0x04, M5MOLS_RESTYPE_CAPTURE, 400, 240 },     /* WQVGA */
97         { 0x07, M5MOLS_RESTYPE_CAPTURE, 480, 360 },
98         { 0x08, M5MOLS_RESTYPE_CAPTURE, 640, 360 },     /* qHD */
99         { 0x09, M5MOLS_RESTYPE_CAPTURE, 640, 480 },     /* VGA */
100         { 0x0a, M5MOLS_RESTYPE_CAPTURE, 800, 480 },     /* WVGA */
101         { 0x10, M5MOLS_RESTYPE_CAPTURE, 1280, 720 },    /* HD */
102         { 0x14, M5MOLS_RESTYPE_CAPTURE, 1280, 960 },    /* 1M */
103         { 0x17, M5MOLS_RESTYPE_CAPTURE, 1600, 1200 },   /* 2M */
104         { 0x19, M5MOLS_RESTYPE_CAPTURE, 1920, 1080 },   /* Full-HD */
105         { 0x1a, M5MOLS_RESTYPE_CAPTURE, 2048, 1152 },   /* 3Mega */
106         { 0x1b, M5MOLS_RESTYPE_CAPTURE, 2048, 1536 },
107         { 0x1c, M5MOLS_RESTYPE_CAPTURE, 2560, 1440 },   /* 4Mega */
108         { 0x1d, M5MOLS_RESTYPE_CAPTURE, 2560, 1536 },
109         { 0x1f, M5MOLS_RESTYPE_CAPTURE, 2560, 1920 },   /* 5Mega */
110         { 0x21, M5MOLS_RESTYPE_CAPTURE, 3264, 1836 },   /* 6Mega */
111         { 0x22, M5MOLS_RESTYPE_CAPTURE, 3264, 1960 },
112         { 0x25, M5MOLS_RESTYPE_CAPTURE, 3264, 2448 },   /* 8Mega */
113 };
114
115 /**
116  * m5mols_swap_byte - an byte array to integer conversion function
117  * @size: size in bytes of I2C packet defined in the M-5MOLS datasheet
118  *
119  * Convert I2C data byte array with performing any required byte
120  * reordering to assure proper values for each data type, regardless
121  * of the architecture endianness.
122  */
123 static u32 m5mols_swap_byte(u8 *data, u8 length)
124 {
125         if (length == 1)
126                 return *data;
127         else if (length == 2)
128                 return be16_to_cpu(*((u16 *)data));
129         else
130                 return be32_to_cpu(*((u32 *)data));
131 }
132
133 /**
134  * m5mols_read -  I2C read function
135  * @reg: combination of size, category and command for the I2C packet
136  * @val: read value
137  */
138 int m5mols_read(struct v4l2_subdev *sd, u32 reg, u32 *val)
139 {
140         struct i2c_client *client = v4l2_get_subdevdata(sd);
141         u8 rbuf[M5MOLS_I2C_MAX_SIZE + 1];
142         u8 size = I2C_SIZE(reg);
143         u8 category = I2C_CATEGORY(reg);
144         u8 cmd = I2C_COMMAND(reg);
145         struct i2c_msg msg[2];
146         u8 wbuf[5];
147         int ret;
148
149         if (!client->adapter)
150                 return -ENODEV;
151
152         if (size != 1 && size != 2 && size != 4) {
153                 v4l2_err(sd, "Wrong data size\n");
154                 return -EINVAL;
155         }
156
157         msg[0].addr = client->addr;
158         msg[0].flags = 0;
159         msg[0].len = 5;
160         msg[0].buf = wbuf;
161         wbuf[0] = 5;
162         wbuf[1] = M5MOLS_BYTE_READ;
163         wbuf[2] = category;
164         wbuf[3] = cmd;
165         wbuf[4] = size;
166
167         msg[1].addr = client->addr;
168         msg[1].flags = I2C_M_RD;
169         msg[1].len = size + 1;
170         msg[1].buf = rbuf;
171
172         /* minimum stabilization time */
173         usleep_range(200, 200);
174
175         ret = i2c_transfer(client->adapter, msg, 2);
176         if (ret < 0) {
177                 v4l2_err(sd, "read failed: size:%d cat:%02x cmd:%02x. %d\n",
178                          size, category, cmd, ret);
179                 return ret;
180         }
181
182         *val = m5mols_swap_byte(&rbuf[1], size);
183
184         return 0;
185 }
186
187 /**
188  * m5mols_write - I2C command write function
189  * @reg: combination of size, category and command for the I2C packet
190  * @val: value to write
191  */
192 int m5mols_write(struct v4l2_subdev *sd, u32 reg, u32 val)
193 {
194         struct i2c_client *client = v4l2_get_subdevdata(sd);
195         u8 wbuf[M5MOLS_I2C_MAX_SIZE + 4];
196         u8 category = I2C_CATEGORY(reg);
197         u8 cmd = I2C_COMMAND(reg);
198         u8 size = I2C_SIZE(reg);
199         u32 *buf = (u32 *)&wbuf[4];
200         struct i2c_msg msg[1];
201         int ret;
202
203         if (!client->adapter)
204                 return -ENODEV;
205
206         if (size != 1 && size != 2 && size != 4) {
207                 v4l2_err(sd, "Wrong data size\n");
208                 return -EINVAL;
209         }
210
211         msg->addr = client->addr;
212         msg->flags = 0;
213         msg->len = (u16)size + 4;
214         msg->buf = wbuf;
215         wbuf[0] = size + 4;
216         wbuf[1] = M5MOLS_BYTE_WRITE;
217         wbuf[2] = category;
218         wbuf[3] = cmd;
219
220         *buf = m5mols_swap_byte((u8 *)&val, size);
221
222         usleep_range(200, 200);
223
224         ret = i2c_transfer(client->adapter, msg, 1);
225         if (ret < 0) {
226                 v4l2_err(sd, "write failed: size:%d cat:%02x cmd:%02x. %d\n",
227                         size, category, cmd, ret);
228                 return ret;
229         }
230
231         return 0;
232 }
233
234 int m5mols_busy(struct v4l2_subdev *sd, u8 category, u8 cmd, u32 mask)
235 {
236         u32 busy, i;
237         int ret;
238
239         for (i = 0; i < M5MOLS_I2C_CHECK_RETRY; i++) {
240                 ret = m5mols_read(sd, I2C_REG(category, cmd, 1), &busy);
241                 if (ret < 0)
242                         return ret;
243                 if ((busy & mask) == mask)
244                         return 0;
245         }
246         return -EBUSY;
247 }
248
249 /**
250  * m5mols_enable_interrupt - Clear interrupt pending bits and unmask interrupts
251  *
252  * Before writing desired interrupt value the INT_FACTOR register should
253  * be read to clear pending interrupts.
254  */
255 int m5mols_enable_interrupt(struct v4l2_subdev *sd, u32 reg)
256 {
257         struct m5mols_info *info = to_m5mols(sd);
258         u32 mask = is_available_af(info) ? REG_INT_AF : 0;
259         u32 dummy;
260         int ret;
261
262         ret = m5mols_read(sd, SYSTEM_INT_FACTOR, &dummy);
263         if (!ret)
264                 ret = m5mols_write(sd, SYSTEM_INT_ENABLE, reg & ~mask);
265         return ret;
266 }
267
268 /**
269  * m5mols_reg_mode - Write the mode and check busy status
270  *
271  * It always accompanies a little delay changing the M-5MOLS mode, so it is
272  * needed checking current busy status to guarantee right mode.
273  */
274 static int m5mols_reg_mode(struct v4l2_subdev *sd, u32 mode)
275 {
276         int ret = m5mols_write(sd, SYSTEM_SYSMODE, mode);
277
278         return ret ? ret : m5mols_busy(sd, CAT_SYSTEM, CAT0_SYSMODE, mode);
279 }
280
281 /**
282  * m5mols_mode - manage the M-5MOLS's mode
283  * @mode: the required operation mode
284  *
285  * The commands of M-5MOLS are grouped into specific modes. Each functionality
286  * can be guaranteed only when the sensor is operating in mode which which
287  * a command belongs to.
288  */
289 int m5mols_mode(struct m5mols_info *info, u32 mode)
290 {
291         struct v4l2_subdev *sd = &info->sd;
292         int ret = -EINVAL;
293         u32 reg;
294
295         if (mode < REG_PARAMETER && mode > REG_CAPTURE)
296                 return ret;
297
298         ret = m5mols_read(sd, SYSTEM_SYSMODE, &reg);
299         if ((!ret && reg == mode) || ret)
300                 return ret;
301
302         switch (reg) {
303         case REG_PARAMETER:
304                 ret = m5mols_reg_mode(sd, REG_MONITOR);
305                 if (!ret && mode == REG_MONITOR)
306                         break;
307                 if (!ret)
308                         ret = m5mols_reg_mode(sd, REG_CAPTURE);
309                 break;
310
311         case REG_MONITOR:
312                 if (mode == REG_PARAMETER) {
313                         ret = m5mols_reg_mode(sd, REG_PARAMETER);
314                         break;
315                 }
316
317                 ret = m5mols_reg_mode(sd, REG_CAPTURE);
318                 break;
319
320         case REG_CAPTURE:
321                 ret = m5mols_reg_mode(sd, REG_MONITOR);
322                 if (!ret && mode == REG_MONITOR)
323                         break;
324                 if (!ret)
325                         ret = m5mols_reg_mode(sd, REG_PARAMETER);
326                 break;
327
328         default:
329                 v4l2_warn(sd, "Wrong mode: %d\n", mode);
330         }
331
332         if (!ret)
333                 info->mode = mode;
334
335         return ret;
336 }
337
338 /**
339  * m5mols_get_version - retrieve full revisions information of M-5MOLS
340  *
341  * The version information includes revisions of hardware and firmware,
342  * AutoFocus alghorithm version and the version string.
343  */
344 static int m5mols_get_version(struct v4l2_subdev *sd)
345 {
346         struct m5mols_info *info = to_m5mols(sd);
347         union {
348                 struct m5mols_version ver;
349                 u8 bytes[VERSION_SIZE];
350         } version;
351         u32 *value;
352         u8 cmd = CAT0_VER_CUSTOMER;
353         int ret;
354
355         do {
356                 value = (u32 *)&version.bytes[cmd];
357                 ret = m5mols_read(sd, SYSTEM_CMD(cmd), value);
358                 if (ret)
359                         return ret;
360         } while (cmd++ != CAT0_VER_AWB);
361
362         do {
363                 value = (u32 *)&version.bytes[cmd];
364                 ret = m5mols_read(sd, SYSTEM_VER_STRING, value);
365                 if (ret)
366                         return ret;
367                 if (cmd >= VERSION_SIZE - 1)
368                         return -EINVAL;
369         } while (version.bytes[cmd++]);
370
371         value = (u32 *)&version.bytes[cmd];
372         ret = m5mols_read(sd, AF_VERSION, value);
373         if (ret)
374                 return ret;
375
376         /* store version information swapped for being readable */
377         info->ver       = version.ver;
378         info->ver.fw    = be16_to_cpu(info->ver.fw);
379         info->ver.hw    = be16_to_cpu(info->ver.hw);
380         info->ver.param = be16_to_cpu(info->ver.param);
381         info->ver.awb   = be16_to_cpu(info->ver.awb);
382
383         v4l2_info(sd, "Manufacturer\t[%s]\n",
384                         is_manufacturer(info, REG_SAMSUNG_ELECTRO) ?
385                         "Samsung Electro-Machanics" :
386                         is_manufacturer(info, REG_SAMSUNG_OPTICS) ?
387                         "Samsung Fiber-Optics" :
388                         is_manufacturer(info, REG_SAMSUNG_TECHWIN) ?
389                         "Samsung Techwin" : "None");
390         v4l2_info(sd, "Customer/Project\t[0x%02x/0x%02x]\n",
391                         info->ver.customer, info->ver.project);
392
393         if (!is_available_af(info))
394                 v4l2_info(sd, "No support Auto Focus on this firmware\n");
395
396         return ret;
397 }
398
399 /**
400  * __find_restype - Lookup M-5MOLS resolution type according to pixel code
401  * @code: pixel code
402  */
403 static enum m5mols_restype __find_restype(enum v4l2_mbus_pixelcode code)
404 {
405         enum m5mols_restype type = M5MOLS_RESTYPE_MONITOR;
406
407         do {
408                 if (code == m5mols_default_ffmt[type].code)
409                         return type;
410         } while (type++ != SIZE_DEFAULT_FFMT);
411
412         return 0;
413 }
414
415 /**
416  * __find_resolution - Lookup preset and type of M-5MOLS's resolution
417  * @mf: pixel format to find/negotiate the resolution preset for
418  * @type: M-5MOLS resolution type
419  * @resolution: M-5MOLS resolution preset register value
420  *
421  * Find nearest resolution matching resolution preset and adjust mf
422  * to supported values.
423  */
424 static int __find_resolution(struct v4l2_subdev *sd,
425                              struct v4l2_mbus_framefmt *mf,
426                              enum m5mols_restype *type,
427                              u32 *resolution)
428 {
429         const struct m5mols_resolution *fsize = &m5mols_reg_res[0];
430         const struct m5mols_resolution *match = NULL;
431         enum m5mols_restype stype = __find_restype(mf->code);
432         int i = ARRAY_SIZE(m5mols_reg_res);
433         unsigned int min_err = ~0;
434
435         while (i--) {
436                 int err;
437                 if (stype == fsize->type) {
438                         err = abs(fsize->width - mf->width)
439                                 + abs(fsize->height - mf->height);
440
441                         if (err < min_err) {
442                                 min_err = err;
443                                 match = fsize;
444                         }
445                 }
446                 fsize++;
447         }
448         if (match) {
449                 mf->width  = match->width;
450                 mf->height = match->height;
451                 *resolution = match->reg;
452                 *type = stype;
453                 return 0;
454         }
455
456         return -EINVAL;
457 }
458
459 static struct v4l2_mbus_framefmt *__find_format(struct m5mols_info *info,
460                                 struct v4l2_subdev_fh *fh,
461                                 enum v4l2_subdev_format_whence which,
462                                 enum m5mols_restype type)
463 {
464         if (which == V4L2_SUBDEV_FORMAT_TRY)
465                 return fh ? v4l2_subdev_get_try_format(fh, 0) : NULL;
466
467         return &info->ffmt[type];
468 }
469
470 static int m5mols_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
471                           struct v4l2_subdev_format *fmt)
472 {
473         struct m5mols_info *info = to_m5mols(sd);
474         struct v4l2_mbus_framefmt *format;
475
476         if (fmt->pad != 0)
477                 return -EINVAL;
478
479         format = __find_format(info, fh, fmt->which, info->res_type);
480         if (!format)
481                 return -EINVAL;
482
483         fmt->format = *format;
484         return 0;
485 }
486
487 static int m5mols_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
488                           struct v4l2_subdev_format *fmt)
489 {
490         struct m5mols_info *info = to_m5mols(sd);
491         struct v4l2_mbus_framefmt *format = &fmt->format;
492         struct v4l2_mbus_framefmt *sfmt;
493         enum m5mols_restype type;
494         u32 resolution = 0;
495         int ret;
496
497         if (fmt->pad != 0)
498                 return -EINVAL;
499
500         ret = __find_resolution(sd, format, &type, &resolution);
501         if (ret < 0)
502                 return ret;
503
504         sfmt = __find_format(info, fh, fmt->which, type);
505         if (!sfmt)
506                 return 0;
507
508         *sfmt           = m5mols_default_ffmt[type];
509         sfmt->width     = format->width;
510         sfmt->height    = format->height;
511
512         if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
513                 info->resolution = resolution;
514                 info->code = format->code;
515                 info->res_type = type;
516         }
517
518         return 0;
519 }
520
521 static int m5mols_enum_mbus_code(struct v4l2_subdev *sd,
522                                  struct v4l2_subdev_fh *fh,
523                                  struct v4l2_subdev_mbus_code_enum *code)
524 {
525         if (!code || code->index >= SIZE_DEFAULT_FFMT)
526                 return -EINVAL;
527
528         code->code = m5mols_default_ffmt[code->index].code;
529
530         return 0;
531 }
532
533 static struct v4l2_subdev_pad_ops m5mols_pad_ops = {
534         .enum_mbus_code = m5mols_enum_mbus_code,
535         .get_fmt        = m5mols_get_fmt,
536         .set_fmt        = m5mols_set_fmt,
537 };
538
539 /**
540  * m5mols_sync_controls - Apply default scene mode and the current controls
541  *
542  * This is used only streaming for syncing between v4l2_ctrl framework and
543  * m5mols's controls. First, do the scenemode to the sensor, then call
544  * v4l2_ctrl_handler_setup. It can be same between some commands and
545  * the scenemode's in the default v4l2_ctrls. But, such commands of control
546  * should be prior to the scenemode's one.
547  */
548 int m5mols_sync_controls(struct m5mols_info *info)
549 {
550         int ret = -EINVAL;
551
552         if (!is_ctrl_synced(info)) {
553                 ret = m5mols_do_scenemode(info, REG_SCENE_NORMAL);
554                 if (ret)
555                         return ret;
556
557                 v4l2_ctrl_handler_setup(&info->handle);
558                 info->ctrl_sync = true;
559         }
560
561         return ret;
562 }
563
564 /**
565  * m5mols_start_monitor - Start the monitor mode
566  *
567  * Before applying the controls setup the resolution and frame rate
568  * in PARAMETER mode, and then switch over to MONITOR mode.
569  */
570 static int m5mols_start_monitor(struct m5mols_info *info)
571 {
572         struct v4l2_subdev *sd = &info->sd;
573         int ret;
574
575         ret = m5mols_mode(info, REG_PARAMETER);
576         if (!ret)
577                 ret = m5mols_write(sd, PARM_MON_SIZE, info->resolution);
578         if (!ret)
579                 ret = m5mols_write(sd, PARM_MON_FPS, REG_FPS_30);
580         if (!ret)
581                 ret = m5mols_mode(info, REG_MONITOR);
582         if (!ret)
583                 ret = m5mols_sync_controls(info);
584
585         return ret;
586 }
587
588 static int m5mols_s_stream(struct v4l2_subdev *sd, int enable)
589 {
590         struct m5mols_info *info = to_m5mols(sd);
591
592         if (enable) {
593                 int ret = -EINVAL;
594
595                 if (is_code(info->code, M5MOLS_RESTYPE_MONITOR))
596                         ret = m5mols_start_monitor(info);
597                 if (is_code(info->code, M5MOLS_RESTYPE_CAPTURE))
598                         ret = m5mols_start_capture(info);
599
600                 return ret;
601         }
602
603         return m5mols_mode(info, REG_PARAMETER);
604 }
605
606 static const struct v4l2_subdev_video_ops m5mols_video_ops = {
607         .s_stream       = m5mols_s_stream,
608 };
609
610 static int m5mols_s_ctrl(struct v4l2_ctrl *ctrl)
611 {
612         struct v4l2_subdev *sd = to_sd(ctrl);
613         struct m5mols_info *info = to_m5mols(sd);
614         int ret;
615
616         info->mode_save = info->mode;
617
618         ret = m5mols_mode(info, REG_PARAMETER);
619         if (!ret)
620                 ret = m5mols_set_ctrl(ctrl);
621         if (!ret)
622                 ret = m5mols_mode(info, info->mode_save);
623
624         return ret;
625 }
626
627 static const struct v4l2_ctrl_ops m5mols_ctrl_ops = {
628         .s_ctrl = m5mols_s_ctrl,
629 };
630
631 static int m5mols_sensor_power(struct m5mols_info *info, bool enable)
632 {
633         struct v4l2_subdev *sd = &info->sd;
634         struct i2c_client *client = v4l2_get_subdevdata(sd);
635         const struct m5mols_platform_data *pdata = info->pdata;
636         int ret;
637
638         if (enable) {
639                 if (is_powered(info))
640                         return 0;
641
642                 if (info->set_power) {
643                         ret = info->set_power(&client->dev, 1);
644                         if (ret)
645                                 return ret;
646                 }
647
648                 ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
649                 if (ret) {
650                         info->set_power(&client->dev, 0);
651                         return ret;
652                 }
653
654                 gpio_set_value(pdata->gpio_reset, !pdata->reset_polarity);
655                 usleep_range(1000, 1000);
656                 info->power = true;
657
658                 return ret;
659         }
660
661         if (!is_powered(info))
662                 return 0;
663
664         ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
665         if (ret)
666                 return ret;
667
668         if (info->set_power)
669                 info->set_power(&client->dev, 0);
670
671         gpio_set_value(pdata->gpio_reset, pdata->reset_polarity);
672         usleep_range(1000, 1000);
673         info->power = false;
674
675         return ret;
676 }
677
678 /* m5mols_update_fw - optional firmware update routine */
679 int __attribute__ ((weak)) m5mols_update_fw(struct v4l2_subdev *sd,
680                 int (*set_power)(struct m5mols_info *, bool))
681 {
682         return 0;
683 }
684
685 /**
686  * m5mols_sensor_armboot - Booting M-5MOLS internal ARM core.
687  *
688  * Booting internal ARM core makes the M-5MOLS is ready for getting commands
689  * with I2C. It's the first thing to be done after it powered up. It must wait
690  * at least 520ms recommended by M-5MOLS datasheet, after executing arm booting.
691  */
692 static int m5mols_sensor_armboot(struct v4l2_subdev *sd)
693 {
694         int ret;
695
696         ret = m5mols_write(sd, FLASH_CAM_START, REG_START_ARM_BOOT);
697         if (ret < 0)
698                 return ret;
699
700         msleep(520);
701
702         ret = m5mols_get_version(sd);
703         if (!ret)
704                 ret = m5mols_update_fw(sd, m5mols_sensor_power);
705         if (ret)
706                 return ret;
707
708         v4l2_dbg(1, m5mols_debug, sd, "Success ARM Booting\n");
709
710         ret = m5mols_write(sd, PARM_INTERFACE, REG_INTERFACE_MIPI);
711         if (!ret)
712                 ret = m5mols_enable_interrupt(sd, REG_INT_AF);
713
714         return ret;
715 }
716
717 static int m5mols_init_controls(struct m5mols_info *info)
718 {
719         struct v4l2_subdev *sd = &info->sd;
720         u16 max_exposure;
721         u16 step_zoom;
722         int ret;
723
724         /* Determine value's range & step of controls for various FW version */
725         ret = m5mols_read(sd, AE_MAX_GAIN_MON, (u32 *)&max_exposure);
726         if (!ret)
727                 step_zoom = is_manufacturer(info, REG_SAMSUNG_OPTICS) ? 31 : 1;
728         if (ret)
729                 return ret;
730
731         v4l2_ctrl_handler_init(&info->handle, 6);
732         info->autowb = v4l2_ctrl_new_std(&info->handle,
733                         &m5mols_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE,
734                         0, 1, 1, 0);
735         info->saturation = v4l2_ctrl_new_std(&info->handle,
736                         &m5mols_ctrl_ops, V4L2_CID_SATURATION,
737                         1, 5, 1, 3);
738         info->zoom = v4l2_ctrl_new_std(&info->handle,
739                         &m5mols_ctrl_ops, V4L2_CID_ZOOM_ABSOLUTE,
740                         1, 70, step_zoom, 1);
741         info->exposure = v4l2_ctrl_new_std(&info->handle,
742                         &m5mols_ctrl_ops, V4L2_CID_EXPOSURE,
743                         0, max_exposure, 1, (int)max_exposure/2);
744         info->colorfx = v4l2_ctrl_new_std_menu(&info->handle,
745                         &m5mols_ctrl_ops, V4L2_CID_COLORFX,
746                         4, (1 << V4L2_COLORFX_BW), V4L2_COLORFX_NONE);
747         info->autoexposure = v4l2_ctrl_new_std_menu(&info->handle,
748                         &m5mols_ctrl_ops, V4L2_CID_EXPOSURE_AUTO,
749                         1, 0, V4L2_EXPOSURE_MANUAL);
750
751         sd->ctrl_handler = &info->handle;
752         if (info->handle.error) {
753                 v4l2_err(sd, "Failed to initialize controls: %d\n", ret);
754                 v4l2_ctrl_handler_free(&info->handle);
755                 return info->handle.error;
756         }
757
758         v4l2_ctrl_cluster(2, &info->autoexposure);
759
760         return 0;
761 }
762
763 /**
764  * m5mols_s_power - Main sensor power control function
765  *
766  * To prevent breaking the lens when the sensor is powered off the Soft-Landing
767  * algorithm is called where available. The Soft-Landing algorithm availability
768  * dependends on the firmware provider.
769  */
770 static int m5mols_s_power(struct v4l2_subdev *sd, int on)
771 {
772         struct m5mols_info *info = to_m5mols(sd);
773         int ret;
774
775         if (on) {
776                 ret = m5mols_sensor_power(info, true);
777                 if (!ret)
778                         ret = m5mols_sensor_armboot(sd);
779                 if (!ret)
780                         ret = m5mols_init_controls(info);
781                 if (ret)
782                         return ret;
783
784                 info->ffmt[M5MOLS_RESTYPE_MONITOR] =
785                         m5mols_default_ffmt[M5MOLS_RESTYPE_MONITOR];
786                 info->ffmt[M5MOLS_RESTYPE_CAPTURE] =
787                         m5mols_default_ffmt[M5MOLS_RESTYPE_CAPTURE];
788                 return ret;
789         }
790
791         if (is_manufacturer(info, REG_SAMSUNG_TECHWIN)) {
792                 ret = m5mols_mode(info, REG_MONITOR);
793                 if (!ret)
794                         ret = m5mols_write(sd, AF_EXECUTE, REG_AF_STOP);
795                 if (!ret)
796                         ret = m5mols_write(sd, AF_MODE, REG_AF_POWEROFF);
797                 if (!ret)
798                         ret = m5mols_busy(sd, CAT_SYSTEM, CAT0_STATUS,
799                                         REG_AF_IDLE);
800                 if (!ret)
801                         v4l2_info(sd, "Success soft-landing lens\n");
802         }
803
804         ret = m5mols_sensor_power(info, false);
805         if (!ret) {
806                 v4l2_ctrl_handler_free(&info->handle);
807                 info->ctrl_sync = false;
808         }
809
810         return ret;
811 }
812
813 static int m5mols_log_status(struct v4l2_subdev *sd)
814 {
815         struct m5mols_info *info = to_m5mols(sd);
816
817         v4l2_ctrl_handler_log_status(&info->handle, sd->name);
818
819         return 0;
820 }
821
822 static const struct v4l2_subdev_core_ops m5mols_core_ops = {
823         .s_power        = m5mols_s_power,
824         .g_ctrl         = v4l2_subdev_g_ctrl,
825         .s_ctrl         = v4l2_subdev_s_ctrl,
826         .queryctrl      = v4l2_subdev_queryctrl,
827         .querymenu      = v4l2_subdev_querymenu,
828         .g_ext_ctrls    = v4l2_subdev_g_ext_ctrls,
829         .try_ext_ctrls  = v4l2_subdev_try_ext_ctrls,
830         .s_ext_ctrls    = v4l2_subdev_s_ext_ctrls,
831         .log_status     = m5mols_log_status,
832 };
833
834 static const struct v4l2_subdev_ops m5mols_ops = {
835         .core           = &m5mols_core_ops,
836         .pad            = &m5mols_pad_ops,
837         .video          = &m5mols_video_ops,
838 };
839
840 static void m5mols_irq_work(struct work_struct *work)
841 {
842         struct m5mols_info *info =
843                 container_of(work, struct m5mols_info, work_irq);
844         struct v4l2_subdev *sd = &info->sd;
845         u32 reg;
846         int ret;
847
848         if (!is_powered(info) ||
849                         m5mols_read(sd, SYSTEM_INT_FACTOR, &info->interrupt))
850                 return;
851
852         switch (info->interrupt & REG_INT_MASK) {
853         case REG_INT_AF:
854                 if (!is_available_af(info))
855                         break;
856                 ret = m5mols_read(sd, AF_STATUS, &reg);
857                 v4l2_dbg(2, m5mols_debug, sd, "AF %s\n",
858                          reg == REG_AF_FAIL ? "Failed" :
859                          reg == REG_AF_SUCCESS ? "Success" :
860                          reg == REG_AF_IDLE ? "Idle" : "Busy");
861                 break;
862         case REG_INT_CAPTURE:
863                 if (!test_and_set_bit(ST_CAPT_IRQ, &info->flags))
864                         wake_up_interruptible(&info->irq_waitq);
865
866                 v4l2_dbg(2, m5mols_debug, sd, "CAPTURE\n");
867                 break;
868         default:
869                 v4l2_dbg(2, m5mols_debug, sd, "Undefined: %02x\n", reg);
870                 break;
871         };
872 }
873
874 static irqreturn_t m5mols_irq_handler(int irq, void *data)
875 {
876         struct v4l2_subdev *sd = data;
877         struct m5mols_info *info = to_m5mols(sd);
878
879         schedule_work(&info->work_irq);
880
881         return IRQ_HANDLED;
882 }
883
884 static int __devinit m5mols_probe(struct i2c_client *client,
885                                   const struct i2c_device_id *id)
886 {
887         const struct m5mols_platform_data *pdata = client->dev.platform_data;
888         struct m5mols_info *info;
889         struct v4l2_subdev *sd;
890         int ret;
891
892         if (pdata == NULL) {
893                 dev_err(&client->dev, "No platform data\n");
894                 return -EINVAL;
895         }
896
897         if (!gpio_is_valid(pdata->gpio_reset)) {
898                 dev_err(&client->dev, "No valid RESET GPIO specified\n");
899                 return -EINVAL;
900         }
901
902         if (!pdata->irq) {
903                 dev_err(&client->dev, "Interrupt not assigned\n");
904                 return -EINVAL;
905         }
906
907         info = kzalloc(sizeof(struct m5mols_info), GFP_KERNEL);
908         if (!info)
909                 return -ENOMEM;
910
911         info->pdata = pdata;
912         info->set_power = pdata->set_power;
913
914         ret = gpio_request(pdata->gpio_reset, "M5MOLS_NRST");
915         if (ret) {
916                 dev_err(&client->dev, "Failed to request gpio: %d\n", ret);
917                 goto out_free;
918         }
919         gpio_direction_output(pdata->gpio_reset, pdata->reset_polarity);
920
921         ret = regulator_bulk_get(&client->dev, ARRAY_SIZE(supplies), supplies);
922         if (ret) {
923                 dev_err(&client->dev, "Failed to get regulators: %d\n", ret);
924                 goto out_gpio;
925         }
926
927         sd = &info->sd;
928         strlcpy(sd->name, MODULE_NAME, sizeof(sd->name));
929         v4l2_i2c_subdev_init(sd, client, &m5mols_ops);
930
931         info->pad.flags = MEDIA_PAD_FL_SOURCE;
932         ret = media_entity_init(&sd->entity, 1, &info->pad, 0);
933         if (ret < 0)
934                 goto out_reg;
935         sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
936
937         init_waitqueue_head(&info->irq_waitq);
938         INIT_WORK(&info->work_irq, m5mols_irq_work);
939         ret = request_irq(pdata->irq, m5mols_irq_handler,
940                           IRQF_TRIGGER_RISING, MODULE_NAME, sd);
941         if (ret) {
942                 dev_err(&client->dev, "Interrupt request failed: %d\n", ret);
943                 goto out_me;
944         }
945         info->res_type = M5MOLS_RESTYPE_MONITOR;
946         return 0;
947 out_me:
948         media_entity_cleanup(&sd->entity);
949 out_reg:
950         regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
951 out_gpio:
952         gpio_free(pdata->gpio_reset);
953 out_free:
954         kfree(info);
955         return ret;
956 }
957
958 static int __devexit m5mols_remove(struct i2c_client *client)
959 {
960         struct v4l2_subdev *sd = i2c_get_clientdata(client);
961         struct m5mols_info *info = to_m5mols(sd);
962
963         v4l2_device_unregister_subdev(sd);
964         free_irq(info->pdata->irq, sd);
965
966         regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
967         gpio_free(info->pdata->gpio_reset);
968         media_entity_cleanup(&sd->entity);
969         kfree(info);
970         return 0;
971 }
972
973 static const struct i2c_device_id m5mols_id[] = {
974         { MODULE_NAME, 0 },
975         { },
976 };
977 MODULE_DEVICE_TABLE(i2c, m5mols_id);
978
979 static struct i2c_driver m5mols_i2c_driver = {
980         .driver = {
981                 .name   = MODULE_NAME,
982         },
983         .probe          = m5mols_probe,
984         .remove         = __devexit_p(m5mols_remove),
985         .id_table       = m5mols_id,
986 };
987
988 static int __init m5mols_mod_init(void)
989 {
990         return i2c_add_driver(&m5mols_i2c_driver);
991 }
992
993 static void __exit m5mols_mod_exit(void)
994 {
995         i2c_del_driver(&m5mols_i2c_driver);
996 }
997
998 module_init(m5mols_mod_init);
999 module_exit(m5mols_mod_exit);
1000
1001 MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
1002 MODULE_AUTHOR("Dongsoo Kim <dongsoo45.kim@samsung.com>");
1003 MODULE_DESCRIPTION("Fujitsu M-5MOLS 8M Pixel camera driver");
1004 MODULE_LICENSE("GPL");