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/version.h>
22 #include <linux/gpio.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/videodev2.h>
25 #include <linux/version.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-subdev.h>
29 #include <media/m5mols.h>
32 #include "m5mols_reg.h"
34 static int m5mols_capture_error_handler(struct m5mols_info *info,
39 /* Disable all interrupts and clear relevant interrupt staus bits */
40 ret = m5mols_write(&info->sd, SYSTEM_INT_ENABLE,
41 info->interrupt & ~(REG_INT_CAPTURE));
51 * m5mols_read_rational - I2C read of a rational number
53 * Read numerator and denominator from registers @addr_num and @addr_den
54 * respectively and return the division result in @val.
56 static int m5mols_read_rational(struct v4l2_subdev *sd, u32 addr_num,
57 u32 addr_den, u32 *val)
61 int ret = m5mols_read(sd, addr_num, &num);
63 ret = m5mols_read(sd, addr_den, &den);
66 *val = den == 0 ? 0 : num / den;
71 * m5mols_capture_info - Gather captured image information
73 * For now it gathers only EXIF information and file size.
75 static int m5mols_capture_info(struct m5mols_info *info)
77 struct m5mols_exif *exif = &info->cap.exif;
78 struct v4l2_subdev *sd = &info->sd;
81 ret = m5mols_read_rational(sd, EXIF_INFO_EXPTIME_NU,
82 EXIF_INFO_EXPTIME_DE, &exif->exposure_time);
85 ret = m5mols_read_rational(sd, EXIF_INFO_TV_NU, EXIF_INFO_TV_DE,
86 &exif->shutter_speed);
89 ret = m5mols_read_rational(sd, EXIF_INFO_AV_NU, EXIF_INFO_AV_DE,
93 ret = m5mols_read_rational(sd, EXIF_INFO_BV_NU, EXIF_INFO_BV_DE,
97 ret = m5mols_read_rational(sd, EXIF_INFO_EBV_NU, EXIF_INFO_EBV_DE,
98 &exif->exposure_bias);
102 ret = m5mols_read(sd, EXIF_INFO_ISO, (u32 *)&exif->iso_speed);
104 ret = m5mols_read(sd, EXIF_INFO_FLASH, (u32 *)&exif->flash);
106 ret = m5mols_read(sd, EXIF_INFO_SDR, (u32 *)&exif->sdr);
108 ret = m5mols_read(sd, EXIF_INFO_QVAL, (u32 *)&exif->qval);
113 ret = m5mols_read(sd, CAPC_IMAGE_SIZE, &info->cap.main);
115 ret = m5mols_read(sd, CAPC_THUMB_SIZE, &info->cap.thumb);
117 info->cap.total = info->cap.main + info->cap.thumb;
122 int m5mols_start_capture(struct m5mols_info *info)
124 struct v4l2_subdev *sd = &info->sd;
125 u32 resolution = info->resolution;
130 * Preparing capture. Setting control & interrupt before entering
133 * 1) change to MONITOR mode for operating control & interrupt
134 * 2) set controls (considering v4l2_control value & lock 3A)
136 * 4) change to CAPTURE mode
138 ret = m5mols_mode(info, REG_MONITOR);
140 ret = m5mols_sync_controls(info);
142 ret = m5mols_lock_3a(info, true);
144 ret = m5mols_enable_interrupt(sd, REG_INT_CAPTURE);
146 ret = m5mols_mode(info, REG_CAPTURE);
148 /* Wait for capture interrupt, after changing capture mode */
149 timeout = wait_event_interruptible_timeout(info->irq_waitq,
150 test_bit(ST_CAPT_IRQ, &info->flags),
151 msecs_to_jiffies(2000));
152 if (test_and_clear_bit(ST_CAPT_IRQ, &info->flags))
153 ret = m5mols_capture_error_handler(info, timeout);
156 ret = m5mols_lock_3a(info, false);
160 * Starting capture. Setting capture frame count and resolution and
161 * the format(available format: JPEG, Bayer RAW, YUV).
163 * 1) select single or multi(enable to 25), format, size
165 * 3) start capture(for main image, now)
167 * 5) notify file size to v4l2 device(e.g, to s5p-fimc v4l2 device)
169 ret = m5mols_write(sd, CAPC_SEL_FRAME, 1);
171 ret = m5mols_write(sd, CAPP_YUVOUT_MAIN, REG_JPEG);
173 ret = m5mols_write(sd, CAPP_MAIN_IMAGE_SIZE, resolution);
175 ret = m5mols_enable_interrupt(sd, REG_INT_CAPTURE);
177 ret = m5mols_write(sd, CAPC_START, REG_CAP_START_MAIN);
179 /* Wait for the capture completion interrupt */
180 timeout = wait_event_interruptible_timeout(info->irq_waitq,
181 test_bit(ST_CAPT_IRQ, &info->flags),
182 msecs_to_jiffies(2000));
183 if (test_and_clear_bit(ST_CAPT_IRQ, &info->flags)) {
184 ret = m5mols_capture_info(info);
186 v4l2_subdev_notify(sd, 0, &info->cap.total);
190 return m5mols_capture_error_handler(info, timeout);