Merge commit 'main-jb-2012.08.03-B4' into t114-0806
[linux-2.6.git] / drivers / media / video / tegra / ov9726.c
1 /*
2  * ov9726.c - ov9726 sensor driver
3  *
4  * Copyright (c) 2011, NVIDIA, All Rights Reserved.
5  *
6  * Contributors:
7  *        Charlie Huang <chahuang@nvidia.com>
8  *
9  * This file is licensed under the terms of the GNU General Public License
10  * version 2. This program is licensed "as is" without any warranty of any
11  * kind, whether express or implied.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/delay.h>
16 #include <linux/fs.h>
17 #include <linux/i2c.h>
18 #include <linux/miscdevice.h>
19 #include <linux/slab.h>
20 #include <linux/io.h>
21 #include <linux/uaccess.h>
22 #include <linux/atomic.h>
23 #include <linux/gpio.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/module.h>
26
27 #include <media/ov9726.h>
28
29 struct ov9726_power_rail {
30         struct regulator *sen_1v8_reg;
31         struct regulator *sen_2v8_reg;
32 };
33
34 struct ov9726_devinfo {
35         struct miscdevice               miscdev_info;
36         struct i2c_client               *i2c_client;
37         struct ov9726_platform_data     *pdata;
38         struct ov9726_power_rail        power_rail;
39         atomic_t                        in_use;
40         __u32                           mode;
41         struct ov9726_reg               grphold_temp[10];
42 };
43
44 static struct ov9726_reg mode_1280x720[] = {
45         /*
46         (1) clock setting
47         clock formula:  (Ref_clk / pre_pll_clk_div) * pll_multiplier /
48                         vt_sys_clk_div / vt_pix_clk_div / divmip
49         input clk at        24Mhz
50         pre_pll_clk_div     0305[3:0] => 4
51         pll_multiplier      0307[6:0] => 100
52         vt_sys_clk_div      0303[3:0] => 1
53         vt_pix_clk_div      0301[3:0] => 10
54         divmip              3010[2:0] => 1
55
56         Overall timing:
57         line length:  1664 (reg 0x342/0x343)
58         frame length: 840  (reg 0x340/0x341)
59         coarse integration time: 835 lines (reg 0x202/0x203) => change to 836
60
61         visible pixels: (0,40) - (1280, 720+40) with size 1280x720
62         Output pixels (1280x720)
63
64         Frame rate if MCLK=24MHz:
65         24Mhz/4 *100/1/10/1 = 60 Mhz
66         60Mhz/1664/840 = 42.9 fps
67         */
68
69         {0x0103, 0x01},
70
71         {OV9726_TABLE_WAIT_MS, 10},
72
73         {0x3026, 0x00},
74         {0x3027, 0x00},
75         {0x3705, 0x45},
76         {0x3603, 0xaa},
77         {0x3632, 0x2f},
78         {0x3620, 0x66},
79         {0x3621, 0xc0},
80         {0x0202, 0x03},
81         {0x0203, 0x13},
82         {0x3833, 0x04},
83         {0x3835, 0x02},
84         {0x4702, 0x04},
85         {0x4704, 0x00},
86         {0x4706, 0x08},
87         {0x5052, 0x01},
88         {0x3819, 0x6c},
89         {0x3817, 0x94},
90         {0x404e, 0x7e},
91         {0x3601, 0x40},
92         {0x3610, 0xa0},
93         {0x0344, 0x00},
94         {0x0345, 0x00},
95         {0x0346, 0x00},
96         {0x0347, 0x28},
97
98         {0x034c, 0x05},
99         {0x034d, 0x00},
100         {0x034e, 0x02},
101         {0x034f, 0xd8},
102         {0x3002, 0x00},
103         {0x3004, 0x00},
104         {0x3005, 0x00},
105         {0x4800, 0x44},
106         {0x4801, 0x0f},
107         {0x4803, 0x05},
108         {0x4601, 0x16},
109         {0x3014, 0x05},
110         {0x0101, 0x01},
111         {0x3707, 0x14},
112         {0x3622, 0x9f},
113         {0x4002, 0x45},
114         {0x5001, 0x00},
115         {0x3406, 0x01},
116         {0x3503, 0x17},
117         {0x0205, 0x3f},
118         {0x0100, 0x01},
119         {0x0112, 0x0a},
120         {0x0113, 0x0a},
121         {0x3013, 0x20},
122         {0x4837, 0x2f},
123         {0x3615, 0xf0},
124         {0x0340, 0x03},
125         {0x0341, 0x48},
126         {0x0342, 0x06},
127         {0x0343, 0x80},
128         {0x3702, 0x1e},
129         {0x3703, 0x3c},
130         {0x3704, 0x0e},
131
132         {0x3104, 0x20},
133         {0x0305, 0x04},
134         {0x0307, 0x46},
135         {0x0303, 0x01},
136         {0x0301, 0x0a},
137         {0x3010, 0x01},
138         {0x460e, 0x00},
139
140         {0x5000, 0x00},
141         {0x5002, 0x00},
142         {0x3017, 0xd2},
143         {0x3018, 0x69},
144         {0x3019, 0x96},
145         {0x5047, 0x61},
146         {0x3604, 0x1c},
147         {0x3602, 0x10},
148         {0x3612, 0x21},
149         {0x3630, 0x0a},
150         {0x3631, 0x53},
151         {0x3633, 0x70},
152         {0x4005, 0x1a},
153         {0x4009, 0x10},
154
155         {OV9726_TABLE_END, 0x0000}
156 };
157
158 static struct ov9726_reg mode_1280x800[] = {
159         {0x0103, 0x01},
160
161         {OV9726_TABLE_WAIT_MS, 10},
162
163         {0x3026, 0x00},
164         {0x3027, 0x00},
165         {0x3705, 0x45},
166         {0x3603, 0xaa},
167         {0x3632, 0x2f},
168         {0x3620, 0x66},
169         {0x3621, 0xc0},
170         {0x0202, 0x03},
171         {0x0203, 0x13},
172         {0x3833, 0x04},
173         {0x3835, 0x02},
174         {0x4702, 0x04},
175         {0x4704, 0x00},
176         {0x4706, 0x08},
177         {0x5052, 0x01},
178         {0x3819, 0x6c},
179         {0x3817, 0x94},
180         {0x404e, 0x7e},
181         {0x3601, 0x40},
182         {0x3610, 0xa0},
183
184         {0x0344, 0x00},
185         {0x0345, 0x00},
186         {0x0346, 0x00},
187         {0x0347, 0x00},
188         {0x034c, 0x05},
189         {0x034d, 0x10},
190         {0x034e, 0x03},
191         {0x034f, 0x28},
192
193         {0x3002, 0x00},
194         {0x3004, 0x00},
195         {0x3005, 0x00},
196         {0x4800, 0x44},
197         {0x4801, 0x0f},
198         {0x4803, 0x05},
199         {0x4601, 0x16},
200         {0x3014, 0x05},
201         {0x0101, 0x01},
202         {0x3707, 0x14},
203         {0x3622, 0x9f},
204         {0x4002, 0x45},
205         {0x5001, 0x00},
206         {0x3406, 0x01},
207         {0x3503, 0x17},
208         {0x0205, 0x3f},
209         {0x0100, 0x01},
210         {0x0112, 0x0a},
211         {0x0113, 0x0a},
212         {0x3013, 0x20},
213         {0x4837, 0x2f},
214         {0x3615, 0xf0},
215         {0x0340, 0x03},
216         {0x0341, 0x48},
217         {0x0342, 0x06},
218         {0x0343, 0x80},
219         {0x3702, 0x1e},
220         {0x3703, 0x3c},
221         {0x3704, 0x0e},
222
223         {0x3104, 0x20},
224         {0x0305, 0x04},
225         {0x0307, 0x46},
226         {0x0303, 0x01},
227         {0x0301, 0x0a},
228         {0x3010, 0x01},
229         {0x460e, 0x00},
230
231         {0x5000, 0x00},
232         {0x5002, 0x00},
233         {0x3017, 0xd2},
234         {0x3018, 0x69},
235         {0x3019, 0x96},
236         {0x5047, 0x61},
237         {0x3604, 0x1c},
238         {0x3602, 0x10},
239         {0x3612, 0x21},
240         {0x3630, 0x0a},
241         {0x3631, 0x53},
242         {0x3633, 0x70},
243         {0x4005, 0x1a},
244         {0x4009, 0x10},
245
246         {OV9726_TABLE_END, 0x0000}
247 };
248
249 enum {
250         OV9726_MODE_1280x720,
251         OV9726_MODE_1280x800,
252 };
253
254 static struct ov9726_reg *mode_table[] = {
255         [OV9726_MODE_1280x720] = mode_1280x720,
256         [OV9726_MODE_1280x800] = mode_1280x800,
257 };
258
259 static inline void
260 msleep_range(unsigned int delay_base)
261 {
262         usleep_range(delay_base*1000, delay_base*1000 + 500);
263 }
264
265 static inline int
266 ov9726_power_init(struct ov9726_devinfo *dev)
267 {
268         struct i2c_client *i2c_client = dev->i2c_client;
269         int err = 0;
270
271         dev->power_rail.sen_1v8_reg = regulator_get(&i2c_client->dev, "dovdd");
272         if (IS_ERR_OR_NULL(dev->power_rail.sen_1v8_reg)) {
273                 dev_err(&i2c_client->dev, "%s: failed to get vdd\n",
274                         __func__);
275                 err = PTR_ERR(dev->power_rail.sen_1v8_reg);
276                 goto ov9726_power_init_end;
277         }
278
279         dev->power_rail.sen_2v8_reg = regulator_get(&i2c_client->dev, "avdd");
280         if (IS_ERR_OR_NULL(dev->power_rail.sen_2v8_reg)) {
281                 dev_err(&i2c_client->dev, "%s: failed to get vaa\n",
282                         __func__);
283                 err = PTR_ERR(dev->power_rail.sen_2v8_reg);
284
285                 regulator_put(dev->power_rail.sen_1v8_reg);
286                 dev->power_rail.sen_1v8_reg = NULL;
287         }
288
289 ov9726_power_init_end:
290         return err;
291 }
292
293 static inline void
294 ov9726_power_release(struct ov9726_devinfo *dev)
295 {
296         regulator_put(dev->power_rail.sen_1v8_reg);
297         regulator_put(dev->power_rail.sen_2v8_reg);
298 }
299
300 static int
301 ov9726_power(struct ov9726_devinfo *dev, bool pwr_on)
302 {
303         struct i2c_client *i2c_client = dev->i2c_client;
304         int rst_active_state = dev->pdata->rst_low_active ? 0 : 1;
305         int pwdn_active_state = dev->pdata->pwdn_low_active ? 0 : 1;
306         int ret = 0;
307
308         dev_info(&i2c_client->dev, "%s %s\n", __func__, pwr_on ? "on" : "off");
309
310         if (pwr_on) {
311                 /* pull low the RST pin of ov9726 first */
312                 gpio_set_value(dev->pdata->gpio_rst, rst_active_state);
313                 msleep_range(1);
314                 /* Plug 1.8V and 2.8V power to sensor */
315                 ret = regulator_enable(dev->power_rail.sen_1v8_reg);
316                 if (ret) {
317                         dev_err(&i2c_client->dev, "%s: failed to enable vdd\n",
318                                 __func__);
319                         goto fail_regulator_1v8_reg;
320                 }
321
322                 msleep_range(20);
323
324                 ret = regulator_enable(dev->power_rail.sen_2v8_reg);
325                 if (ret) {
326                         dev_err(&i2c_client->dev, "%s: failed to enable vaa\n",
327                                 __func__);
328                         goto fail_regulator_2v8_reg;
329                 }
330                 msleep_range(1);
331                 /* turn on ov9726 */
332                 gpio_set_value(dev->pdata->gpio_pwdn, !pwdn_active_state);
333
334                 msleep_range(5);
335                 /* release RST pin */
336                 gpio_set_value(dev->pdata->gpio_rst, !rst_active_state);
337                 msleep_range(20);
338
339                 /* Board specific power-on sequence */
340                 dev->pdata->power_on();
341         } else {
342                 /* pull low the RST pin of ov9726 */
343                 gpio_set_value(dev->pdata->gpio_rst, rst_active_state);
344                 msleep_range(1);
345                 /* turn off ov9726 */
346                 gpio_set_value(dev->pdata->gpio_pwdn, pwdn_active_state);
347                 msleep_range(1);
348
349                 /* Unplug 1.8V and 2.8V power from sensor */
350                 regulator_disable(dev->power_rail.sen_2v8_reg);
351                 regulator_disable(dev->power_rail.sen_1v8_reg);
352
353                 /* Board specific power-down sequence */
354                 dev->pdata->power_off();
355         }
356
357         return 0;
358
359 fail_regulator_2v8_reg:
360         regulator_put(dev->power_rail.sen_2v8_reg);
361         dev->power_rail.sen_2v8_reg = NULL;
362         regulator_disable(dev->power_rail.sen_1v8_reg);
363 fail_regulator_1v8_reg:
364         regulator_put(dev->power_rail.sen_1v8_reg);
365         dev->power_rail.sen_1v8_reg = NULL;
366         return ret;
367 }
368
369 static inline void
370 ov9726_get_frame_length_regs(struct ov9726_reg *regs, u32 frame_length)
371 {
372         regs->addr = OV9726_REG_FRAME_LENGTH_HI;
373         regs->val = (frame_length >> 8) & 0xff;
374         regs++;
375         regs->addr = OV9726_REG_FRAME_LENGTH_LO;
376         regs->val = frame_length & 0xff;
377 }
378
379 static inline void
380 ov9726_get_coarse_time_regs(struct ov9726_reg *regs, u32 coarse_time)
381 {
382         regs->addr = OV9726_REG_COARSE_TIME_HI;
383         regs->val = (coarse_time >> 8) & 0xff;
384         regs++;
385         regs->addr = OV9726_REG_COARSE_TIME_LO;
386         regs->val = coarse_time & 0xff;
387 }
388
389 static inline void
390 ov9726_get_gain_reg(struct ov9726_reg *regs, u16 gain)
391 {
392         regs->addr = OV9726_REG_GAIN_HI;
393         regs->val = (gain >> 8) & 0xff;
394         regs++;
395         regs->addr = OV9726_REG_GAIN_LO;
396         regs->val = gain & 0xff;
397 }
398
399 static int
400 ov9726_read_reg8(struct i2c_client *client, u16 addr, u8 *val)
401 {
402         int     err;
403         struct i2c_msg  msg[2];
404         unsigned char   data[3];
405
406         if (!client->adapter)
407                 return -ENODEV;
408
409         msg[0].addr = client->addr;
410         msg[0].flags = 0;
411         msg[0].len = 2;
412         msg[0].buf = data;
413
414         /* high byte goes out first */
415         data[0] = (u8)(addr >> 8);
416         data[1] = (u8)(addr & 0xff);
417
418         msg[1].addr = client->addr;
419         msg[1].flags = I2C_M_RD;
420         msg[1].len = 1;
421         msg[1].buf = data + 2;
422
423         err = i2c_transfer(client->adapter, msg, 2);
424
425         if (err != 2)
426                 err = -EINVAL;
427         else {
428                 *val = data[2];
429                 err = 0;
430         }
431
432         return err;
433 }
434
435 static int
436 ov9726_write_reg8(struct i2c_client *client, u16 addr, u8 val)
437 {
438         int             err;
439         struct i2c_msg  msg;
440         unsigned char   data[3];
441         int             retry = 0;
442
443         if (!client->adapter)
444                 return -ENODEV;
445
446         data[0] = (u8)(addr >> 8);
447         data[1] = (u8)(addr & 0xff);
448         data[2] = (u8)(val & 0xff);
449
450         msg.addr = client->addr;
451         msg.flags = 0;
452         msg.len = 3;
453         msg.buf = data;
454
455         do {
456                 err = i2c_transfer(client->adapter, &msg, 1);
457                 if (err == 1)
458                         break;
459
460                 retry++;
461                 dev_err(&client->dev,
462                         "ov9726: i2c transfer failed, retrying %x %x\n",
463                         addr, val);
464                 msleep_range(3);
465         } while (retry <= OV9726_MAX_RETRIES);
466
467         return (err != 1);
468 }
469
470 static int
471 ov9726_write_reg16(struct i2c_client *client, u16 addr, u16 val)
472 {
473         int             count;
474         struct i2c_msg  msg;
475         unsigned char   data[4];
476         int             retry = 0;
477
478         if (!client->adapter)
479                 return -ENODEV;
480
481         data[0] = (u8)(addr >> 8);
482         data[1] = (u8)(addr & 0xff);
483         data[2] = (u8)(val >> 8);
484         data[3] = (u8)(val & 0xff);
485
486         msg.addr = client->addr;
487         msg.flags = 0;
488         msg.len = 4;
489         msg.buf = data;
490
491         do {
492                 count = i2c_transfer(client->adapter, &msg, 1);
493                 if (count == 1)
494                         return 0;
495
496                 retry++;
497                 dev_err(&client->dev,
498                         "ov9726: i2c transfer failed, retrying %x %x %x\n",
499                         addr, val, count);
500                 msleep_range(3);
501         } while (retry <= OV9726_MAX_RETRIES);
502
503         return -EIO;
504 }
505
506 static int
507 ov9726_write_table(
508         struct i2c_client *client,
509         struct ov9726_reg table[],
510         struct ov9726_reg override_list[],
511         int num_override_regs)
512 {
513         const struct ov9726_reg *next;
514         int                     err = 0;
515         int                     i;
516         u16                     val;
517
518         dev_info(&client->dev, "ov9726_write_table\n");
519
520         for (next = table; next->addr != OV9726_TABLE_END; next++) {
521
522                 if (next->addr == OV9726_TABLE_WAIT_MS) {
523                         msleep_range(next->val);
524                         continue;
525                 }
526
527                 val = next->val;
528
529                 /* When an override list is passed in, replace the reg */
530                 /* value to write if the reg is in the list */
531                 if (override_list) {
532                         for (i = 0; i < num_override_regs; i++) {
533                                 if (next->addr == override_list[i].addr) {
534                                         val = override_list[i].val;
535                                         break;
536                                 }
537                         }
538                 }
539
540                 err = ov9726_write_reg8(client, next->addr, val);
541                 if (err)
542                         break;
543         }
544
545         return err;
546 }
547
548 static int
549 ov9726_set_frame_length(struct i2c_client *i2c_client, u32 frame_length)
550 {
551         int ret;
552
553         dev_info(&i2c_client->dev, "[%s] (0x%08x)\n", __func__,  frame_length);
554         /* hold register value */
555         ret = ov9726_write_reg8(i2c_client, 0x104, 0x01);
556         if (ret)
557                 return ret;
558
559         ret = ov9726_write_reg16(i2c_client,
560                                   OV9726_REG_FRAME_LENGTH_HI,
561                                   frame_length);
562
563         /* release hold, update register value */
564         ret |= ov9726_write_reg8(i2c_client, 0x104, 0x00);
565
566         return ret;
567 }
568
569 static int
570 ov9726_set_coarse_time(struct i2c_client *i2c_client, u32 coarse_time)
571 {
572         int ret;
573
574         dev_info(&i2c_client->dev, "[%s] (0x%08x)\n", __func__,  coarse_time);
575         /* hold register value */
576         ret = ov9726_write_reg8(i2c_client, 0x104, 0x01);
577         if (ret)
578                 return ret;
579
580         ret = ov9726_write_reg16(i2c_client,
581                           OV9726_REG_COARSE_TIME_HI,
582                           coarse_time);
583
584         /* release hold, update register value */
585         ret |= ov9726_write_reg8(i2c_client, 0x104, 0x00);
586
587         return ret;
588 }
589
590 static int ov9726_set_gain(struct i2c_client *i2c_client, u16 gain)
591 {
592         int ret;
593
594         /* hold register value */
595         ret = ov9726_write_reg8(i2c_client, 0x104, 0x01);
596         if (ret)
597                 return ret;
598
599         ret = ov9726_write_reg16(i2c_client, OV9726_REG_GAIN_HI, gain);
600
601         /* release hold, update register value */
602         ret |= ov9726_write_reg8(i2c_client, 0x104, 0x00);
603
604         return ret;
605 }
606
607 static int ov9726_set_group_hold(struct ov9726_devinfo *dev,
608                         struct ov9726_ae *ae)
609 {
610 #define OV9726_REG_PUSH8(p, a, v) \
611         do { \
612                 (p)->addr = (a); \
613                 (p)->val = (v); \
614                 (p)++; \
615         } while (0)
616
617 #define OV9726_REG_PUSH16(ptr, addr, val) do { \
618                 OV9726_REG_PUSH8(ptr, (addr), (val) >> 8); \
619                 OV9726_REG_PUSH8(ptr, (addr) + 1, (val) & 0xff); \
620         } while (0)
621
622         struct ov9726_reg *gptr = &dev->grphold_temp[0];
623
624         if (!ae->gain_enable &&
625                 !ae->coarse_time_enable &&
626                 !ae->frame_length_enable)
627                 return 0;
628
629         OV9726_REG_PUSH8(gptr, 0x0104, 0x01);
630         if (ae->gain_enable)
631                 OV9726_REG_PUSH16(gptr,
632                         OV9726_REG_GAIN_HI, ae->gain);
633         if (ae->coarse_time_enable)
634                 OV9726_REG_PUSH16(gptr,
635                         OV9726_REG_COARSE_TIME_HI, ae->coarse_time);
636         if (ae->frame_length_enable) {
637                 OV9726_REG_PUSH16(gptr,
638                         OV9726_REG_FRAME_LENGTH_HI, ae->frame_length);
639         }
640         OV9726_REG_PUSH8(gptr, 0x0104, 0x00);
641         OV9726_REG_PUSH8(gptr, OV9726_TABLE_END, 0x00);
642
643         return ov9726_write_table(dev->i2c_client,
644                                 dev->grphold_temp, NULL, 0);
645 }
646
647 static int ov9726_get_status(struct i2c_client *i2c_client, u8 *status)
648 {
649         int err;
650
651         err = ov9726_read_reg8(i2c_client, 0x003, status);
652         *status = 0;
653         return err;
654 }
655
656 static int
657 ov9726_set_mode(
658         struct ov9726_devinfo *dev,
659         struct ov9726_mode *mode)
660 {
661         struct i2c_client       *i2c_client = dev->i2c_client;
662         struct ov9726_reg       reg_override[6];
663         int                     err = 0;
664         int                     sensor_mode;
665
666         dev_info(&i2c_client->dev, "%s.\n", __func__);
667
668         if (mode->xres == 1280 && mode->yres == 800)
669                 sensor_mode = OV9726_MODE_1280x800;
670         else if (mode->xres == 1280 && mode->yres == 720)
671                 sensor_mode = OV9726_MODE_1280x720;
672         else {
673                 dev_err(&i2c_client->dev,
674                         "%s: invalid resolution supplied to set mode %d %d\n",
675                         __func__, mode->xres, mode->yres);
676                 return -EINVAL;
677         }
678
679         ov9726_get_frame_length_regs(reg_override, mode->frame_length);
680         ov9726_get_coarse_time_regs(reg_override + 2, mode->coarse_time);
681         ov9726_get_gain_reg(reg_override + 4, mode->gain);
682
683         if (dev->mode != mode->mode_id) {
684                 dev_info(&i2c_client->dev,
685                         "%s: xres %u yres %u framelen %u coarse %u gain %u\n",
686                         __func__, mode->xres, mode->yres, mode->frame_length,
687                         mode->coarse_time, mode->gain);
688
689                 err = ov9726_write_table(i2c_client,
690                         mode_table[sensor_mode], reg_override,
691                         sizeof(reg_override) / sizeof(reg_override[0]));
692                 if (err)
693                         goto ov9726_set_mode_exit;
694
695                 dev->mode = mode->mode_id;
696         }
697
698 ov9726_set_mode_exit:
699         return err;
700 }
701
702 static long
703 ov9726_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
704 {
705         struct ov9726_devinfo   *dev = file->private_data;
706         struct i2c_client       *i2c_client = dev->i2c_client;
707         int                      err = 0;
708
709         switch (cmd) {
710         case OV9726_IOCTL_SET_MODE:
711         {
712                 struct ov9726_mode      mode;
713
714                 if (copy_from_user(&mode,
715                                    (const void __user *)arg,
716                                    sizeof(struct ov9726_mode))) {
717                         err = -EFAULT;
718                         break;
719                 }
720
721                 err = ov9726_set_mode(dev, &mode);
722
723                 break;
724         }
725         case OV9726_IOCTL_SET_FRAME_LENGTH:
726                 err = ov9726_set_frame_length(i2c_client, (u32)arg);
727                 break;
728         case OV9726_IOCTL_SET_COARSE_TIME:
729                 err = ov9726_set_coarse_time(i2c_client, (u32)arg);
730                 break;
731         case OV9726_IOCTL_SET_GAIN:
732                 err = ov9726_set_gain(i2c_client, (u16)arg);
733                 break;
734         case OV9726_IOCTL_SET_GROUP_HOLD:
735         {
736                 struct ov9726_ae ae;
737                 if (copy_from_user(&ae,
738                         (const void __user *)arg, sizeof(struct ov9726_ae))) {
739                         pr_info("%s %d\n", __func__, __LINE__);
740                         return -EFAULT;
741                 }
742                 err = ov9726_set_group_hold(dev, &ae);
743                 break;
744         }
745         case OV9726_IOCTL_GET_STATUS:
746         {
747                 u8 status;
748
749                 err = ov9726_get_status(i2c_client, &status);
750                 if (!err) {
751                         if (copy_to_user((void __user *)arg,
752                                         &status, sizeof(status)))
753                                 err = -EFAULT;
754                 }
755                 break;
756         }
757         default:
758                 err = -EINVAL;
759                 break;
760         }
761
762         return err;
763 }
764
765 static int ov9726_open(struct inode *inode, struct file *file)
766 {
767         struct miscdevice       *miscdev = file->private_data;
768         struct ov9726_devinfo   *dev;
769
770         dev = container_of(miscdev, struct ov9726_devinfo, miscdev_info);
771         /* check if device is in use */
772         if (atomic_xchg(&dev->in_use, 1))
773                 return -EBUSY;
774         dev->mode = (__u32)-1;
775         file->private_data = dev;
776
777         ov9726_power(dev, true);
778
779         return 0;
780 }
781
782 int ov9726_release(struct inode *inode, struct file *file)
783 {
784         struct ov9726_devinfo *dev;
785
786         dev = file->private_data;
787         file->private_data = NULL;
788
789         ov9726_power(dev, false);
790
791         /* warn if device already released */
792         WARN_ON(!atomic_xchg(&dev->in_use, 0));
793         return 0;
794 }
795
796 static const struct file_operations ov9726_fileops = {
797         .owner          = THIS_MODULE,
798         .open           = ov9726_open,
799         .unlocked_ioctl = ov9726_ioctl,
800         .release        = ov9726_release,
801 };
802
803 static struct miscdevice ov9726_device = {
804         .name           = "ov9726",
805         .minor          = MISC_DYNAMIC_MINOR,
806         .fops           = &ov9726_fileops,
807 };
808
809 static int
810 ov9726_probe(struct i2c_client *client, const struct i2c_device_id *id)
811 {
812         struct ov9726_devinfo   *dev;
813         int                     err = 0;
814
815         dev_info(&client->dev, "ov9726: probing sensor.\n");
816
817         dev = kzalloc(sizeof(struct ov9726_devinfo), GFP_KERNEL);
818         if (!dev) {
819                 dev_err(&client->dev, "ov9726: Unable to allocate memory!\n");
820                 err = -ENOMEM;
821                 goto probe_end;
822         }
823
824         memcpy(&(dev->miscdev_info),
825                 &ov9726_device,
826                 sizeof(struct miscdevice));
827
828         err = misc_register(&(dev->miscdev_info));
829         if (err) {
830                 dev_err(&client->dev, "ov9726: Unable to register misc device!\n");
831                 goto probe_end;
832         }
833
834         dev->pdata = client->dev.platform_data;
835         dev->i2c_client = client;
836         atomic_set(&dev->in_use, 0);
837         i2c_set_clientdata(client, dev);
838
839         err = ov9726_power_init(dev);
840
841 probe_end:
842         if (err) {
843                 kfree(dev);
844                 dev_err(&client->dev, "failed.\n");
845         }
846
847         return err;
848 }
849
850 static int ov9726_remove(struct i2c_client *client)
851 {
852         struct ov9726_devinfo   *dev;
853
854         dev = i2c_get_clientdata(client);
855         i2c_set_clientdata(client, NULL);
856         misc_deregister(&ov9726_device);
857         ov9726_power_release(dev);
858         kfree(dev);
859
860         return 0;
861 }
862
863 static const struct i2c_device_id ov9726_id[] = {
864         {"ov9726", 0},
865         {},
866 };
867
868 MODULE_DEVICE_TABLE(i2c, ov9726_id);
869
870 static struct i2c_driver ov9726_i2c_driver = {
871         .driver = {
872                 .name   = "ov9726",
873                 .owner  = THIS_MODULE,
874                 },
875         .probe  = ov9726_probe,
876         .remove = ov9726_remove,
877         .id_table   = ov9726_id,
878 };
879
880 static int __init ov9726_init(void)
881 {
882         pr_info("ov9726 sensor driver loading\n");
883         return i2c_add_driver(&ov9726_i2c_driver);
884 }
885
886 static void __exit ov9726_exit(void)
887 {
888         i2c_del_driver(&ov9726_i2c_driver);
889 }
890
891 module_init(ov9726_init);
892 module_exit(ov9726_exit);
893 MODULE_LICENSE("GPL v2");