Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[pandora-kernel.git] / drivers / media / video / m5mols / m5mols_capture.c
1 /*
2  * The Capture code for Fujitsu M-5MOLS 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/gpio.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-subdev.h>
27 #include <media/m5mols.h>
28
29 #include "m5mols.h"
30 #include "m5mols_reg.h"
31
32 static int m5mols_capture_error_handler(struct m5mols_info *info,
33                                         int timeout)
34 {
35         int ret;
36
37         /* Disable all interrupts and clear relevant interrupt staus bits */
38         ret = m5mols_write(&info->sd, SYSTEM_INT_ENABLE,
39                            info->interrupt & ~(REG_INT_CAPTURE));
40         if (ret)
41                 return ret;
42
43         if (timeout == 0)
44                 return -ETIMEDOUT;
45
46         return 0;
47 }
48 /**
49  * m5mols_read_rational - I2C read of a rational number
50  *
51  * Read numerator and denominator from registers @addr_num and @addr_den
52  * respectively and return the division result in @val.
53  */
54 static int m5mols_read_rational(struct v4l2_subdev *sd, u32 addr_num,
55                                 u32 addr_den, u32 *val)
56 {
57         u32 num, den;
58
59         int ret = m5mols_read_u32(sd, addr_num, &num);
60         if (!ret)
61                 ret = m5mols_read_u32(sd, addr_den, &den);
62         if (ret)
63                 return ret;
64         *val = den == 0 ? 0 : num / den;
65         return ret;
66 }
67
68 /**
69  * m5mols_capture_info - Gather captured image information
70  *
71  * For now it gathers only EXIF information and file size.
72  */
73 static int m5mols_capture_info(struct m5mols_info *info)
74 {
75         struct m5mols_exif *exif = &info->cap.exif;
76         struct v4l2_subdev *sd = &info->sd;
77         int ret;
78
79         ret = m5mols_read_rational(sd, EXIF_INFO_EXPTIME_NU,
80                                    EXIF_INFO_EXPTIME_DE, &exif->exposure_time);
81         if (ret)
82                 return ret;
83         ret = m5mols_read_rational(sd, EXIF_INFO_TV_NU, EXIF_INFO_TV_DE,
84                                    &exif->shutter_speed);
85         if (ret)
86                 return ret;
87         ret = m5mols_read_rational(sd, EXIF_INFO_AV_NU, EXIF_INFO_AV_DE,
88                                    &exif->aperture);
89         if (ret)
90                 return ret;
91         ret = m5mols_read_rational(sd, EXIF_INFO_BV_NU, EXIF_INFO_BV_DE,
92                                    &exif->brightness);
93         if (ret)
94                 return ret;
95         ret = m5mols_read_rational(sd, EXIF_INFO_EBV_NU, EXIF_INFO_EBV_DE,
96                                    &exif->exposure_bias);
97         if (ret)
98                 return ret;
99
100         ret = m5mols_read_u16(sd, EXIF_INFO_ISO, &exif->iso_speed);
101         if (!ret)
102                 ret = m5mols_read_u16(sd, EXIF_INFO_FLASH, &exif->flash);
103         if (!ret)
104                 ret = m5mols_read_u16(sd, EXIF_INFO_SDR, &exif->sdr);
105         if (!ret)
106                 ret = m5mols_read_u16(sd, EXIF_INFO_QVAL, &exif->qval);
107         if (ret)
108                 return ret;
109
110         if (!ret)
111                 ret = m5mols_read_u32(sd, CAPC_IMAGE_SIZE, &info->cap.main);
112         if (!ret)
113                 ret = m5mols_read_u32(sd, CAPC_THUMB_SIZE, &info->cap.thumb);
114         if (!ret)
115                 info->cap.total = info->cap.main + info->cap.thumb;
116
117         return ret;
118 }
119
120 int m5mols_start_capture(struct m5mols_info *info)
121 {
122         struct v4l2_subdev *sd = &info->sd;
123         u8 resolution = info->resolution;
124         int timeout;
125         int ret;
126
127         /*
128          * Preparing capture. Setting control & interrupt before entering
129          * capture mode
130          *
131          * 1) change to MONITOR mode for operating control & interrupt
132          * 2) set controls (considering v4l2_control value & lock 3A)
133          * 3) set interrupt
134          * 4) change to CAPTURE mode
135          */
136         ret = m5mols_mode(info, REG_MONITOR);
137         if (!ret)
138                 ret = m5mols_sync_controls(info);
139         if (!ret)
140                 ret = m5mols_lock_3a(info, true);
141         if (!ret)
142                 ret = m5mols_enable_interrupt(sd, REG_INT_CAPTURE);
143         if (!ret)
144                 ret = m5mols_mode(info, REG_CAPTURE);
145         if (!ret) {
146                 /* Wait for capture interrupt, after changing capture mode */
147                 timeout = wait_event_interruptible_timeout(info->irq_waitq,
148                                            test_bit(ST_CAPT_IRQ, &info->flags),
149                                            msecs_to_jiffies(2000));
150                 if (test_and_clear_bit(ST_CAPT_IRQ, &info->flags))
151                         ret = m5mols_capture_error_handler(info, timeout);
152         }
153         if (!ret)
154                 ret = m5mols_lock_3a(info, false);
155         if (ret)
156                 return ret;
157         /*
158          * Starting capture. Setting capture frame count and resolution and
159          * the format(available format: JPEG, Bayer RAW, YUV).
160          *
161          * 1) select single or multi(enable to 25), format, size
162          * 2) set interrupt
163          * 3) start capture(for main image, now)
164          * 4) get information
165          * 5) notify file size to v4l2 device(e.g, to s5p-fimc v4l2 device)
166          */
167         ret = m5mols_write(sd, CAPC_SEL_FRAME, 1);
168         if (!ret)
169                 ret = m5mols_write(sd, CAPP_YUVOUT_MAIN, REG_JPEG);
170         if (!ret)
171                 ret = m5mols_write(sd, CAPP_MAIN_IMAGE_SIZE, resolution);
172         if (!ret)
173                 ret = m5mols_enable_interrupt(sd, REG_INT_CAPTURE);
174         if (!ret)
175                 ret = m5mols_write(sd, CAPC_START, REG_CAP_START_MAIN);
176         if (!ret) {
177                 /* Wait for the capture completion interrupt */
178                 timeout = wait_event_interruptible_timeout(info->irq_waitq,
179                                            test_bit(ST_CAPT_IRQ, &info->flags),
180                                            msecs_to_jiffies(2000));
181                 if (test_and_clear_bit(ST_CAPT_IRQ, &info->flags)) {
182                         ret = m5mols_capture_info(info);
183                         if (!ret)
184                                 v4l2_subdev_notify(sd, 0, &info->cap.total);
185                 }
186         }
187
188         return m5mols_capture_error_handler(info, timeout);
189 }