2 * The Capture code for Fujitsu M-5MOLS ISP
4 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5 * Author: HeungJun Kim <riverful.kim@samsung.com>
7 * Copyright (C) 2009 Samsung Electronics Co., Ltd.
8 * Author: Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com>
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.
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>
30 #include "m5mols_reg.h"
32 static int m5mols_capture_error_handler(struct m5mols_info
*info
,
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
));
49 * m5mols_read_rational - I2C read of a rational number
51 * Read numerator and denominator from registers @addr_num and @addr_den
52 * respectively and return the division result in @val.
54 static int m5mols_read_rational(struct v4l2_subdev
*sd
, u32 addr_num
,
55 u32 addr_den
, u32
*val
)
59 int ret
= m5mols_read_u32(sd
, addr_num
, &num
);
61 ret
= m5mols_read_u32(sd
, addr_den
, &den
);
64 *val
= den
== 0 ? 0 : num
/ den
;
69 * m5mols_capture_info - Gather captured image information
71 * For now it gathers only EXIF information and file size.
73 static int m5mols_capture_info(struct m5mols_info
*info
)
75 struct m5mols_exif
*exif
= &info
->cap
.exif
;
76 struct v4l2_subdev
*sd
= &info
->sd
;
79 ret
= m5mols_read_rational(sd
, EXIF_INFO_EXPTIME_NU
,
80 EXIF_INFO_EXPTIME_DE
, &exif
->exposure_time
);
83 ret
= m5mols_read_rational(sd
, EXIF_INFO_TV_NU
, EXIF_INFO_TV_DE
,
84 &exif
->shutter_speed
);
87 ret
= m5mols_read_rational(sd
, EXIF_INFO_AV_NU
, EXIF_INFO_AV_DE
,
91 ret
= m5mols_read_rational(sd
, EXIF_INFO_BV_NU
, EXIF_INFO_BV_DE
,
95 ret
= m5mols_read_rational(sd
, EXIF_INFO_EBV_NU
, EXIF_INFO_EBV_DE
,
96 &exif
->exposure_bias
);
100 ret
= m5mols_read_u16(sd
, EXIF_INFO_ISO
, &exif
->iso_speed
);
102 ret
= m5mols_read_u16(sd
, EXIF_INFO_FLASH
, &exif
->flash
);
104 ret
= m5mols_read_u16(sd
, EXIF_INFO_SDR
, &exif
->sdr
);
106 ret
= m5mols_read_u16(sd
, EXIF_INFO_QVAL
, &exif
->qval
);
111 ret
= m5mols_read_u32(sd
, CAPC_IMAGE_SIZE
, &info
->cap
.main
);
113 ret
= m5mols_read_u32(sd
, CAPC_THUMB_SIZE
, &info
->cap
.thumb
);
115 info
->cap
.total
= info
->cap
.main
+ info
->cap
.thumb
;
120 int m5mols_start_capture(struct m5mols_info
*info
)
122 struct v4l2_subdev
*sd
= &info
->sd
;
123 u8 resolution
= info
->resolution
;
128 * Preparing capture. Setting control & interrupt before entering
131 * 1) change to MONITOR mode for operating control & interrupt
132 * 2) set controls (considering v4l2_control value & lock 3A)
134 * 4) change to CAPTURE mode
136 ret
= m5mols_mode(info
, REG_MONITOR
);
138 ret
= m5mols_sync_controls(info
);
140 ret
= m5mols_lock_3a(info
, true);
142 ret
= m5mols_enable_interrupt(sd
, REG_INT_CAPTURE
);
144 ret
= m5mols_mode(info
, REG_CAPTURE
);
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
);
154 ret
= m5mols_lock_3a(info
, false);
158 * Starting capture. Setting capture frame count and resolution and
159 * the format(available format: JPEG, Bayer RAW, YUV).
161 * 1) select single or multi(enable to 25), format, size
163 * 3) start capture(for main image, now)
165 * 5) notify file size to v4l2 device(e.g, to s5p-fimc v4l2 device)
167 ret
= m5mols_write(sd
, CAPC_SEL_FRAME
, 1);
169 ret
= m5mols_write(sd
, CAPP_YUVOUT_MAIN
, REG_JPEG
);
171 ret
= m5mols_write(sd
, CAPP_MAIN_IMAGE_SIZE
, resolution
);
173 ret
= m5mols_enable_interrupt(sd
, REG_INT_CAPTURE
);
175 ret
= m5mols_write(sd
, CAPC_START
, REG_CAP_START_MAIN
);
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
);
184 v4l2_subdev_notify(sd
, 0, &info
->cap
.total
);
188 return m5mols_capture_error_handler(info
, timeout
);