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