Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / media / i2c / m5mols / m5mols.h
1 /*
2  * Header for M-5MOLS 8M Pixel camera sensor with 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 #ifndef M5MOLS_H
17 #define M5MOLS_H
18
19 #include <linux/sizes.h>
20 #include <media/v4l2-subdev.h>
21 #include "m5mols_reg.h"
22
23
24 /* An amount of data transmitted in addition to the value
25  * determined by CAPP_JPEG_SIZE_MAX register.
26  */
27 #define M5MOLS_JPEG_TAGS_SIZE           0x20000
28 #define M5MOLS_MAIN_JPEG_SIZE_MAX       (5 * SZ_1M)
29
30 extern int m5mols_debug;
31
32 enum m5mols_restype {
33         M5MOLS_RESTYPE_MONITOR,
34         M5MOLS_RESTYPE_CAPTURE,
35         M5MOLS_RESTYPE_MAX,
36 };
37
38 /**
39  * struct m5mols_resolution - structure for the resolution
40  * @type: resolution type according to the pixel code
41  * @width: width of the resolution
42  * @height: height of the resolution
43  * @reg: resolution preset register value
44  */
45 struct m5mols_resolution {
46         u8 reg;
47         enum m5mols_restype type;
48         u16 width;
49         u16 height;
50 };
51
52 /**
53  * struct m5mols_exif - structure for the EXIF information of M-5MOLS
54  * @exposure_time: exposure time register value
55  * @shutter_speed: speed of the shutter register value
56  * @aperture: aperture register value
57  * @exposure_bias: it calls also EV bias
58  * @iso_speed: ISO register value
59  * @flash: status register value of the flash
60  * @sdr: status register value of the Subject Distance Range
61  * @qval: not written exact meaning in document
62  */
63 struct m5mols_exif {
64         u32 exposure_time;
65         u32 shutter_speed;
66         u32 aperture;
67         u32 brightness;
68         u32 exposure_bias;
69         u16 iso_speed;
70         u16 flash;
71         u16 sdr;
72         u16 qval;
73 };
74
75 /**
76  * struct m5mols_capture - Structure for the capture capability
77  * @exif: EXIF information
78  * @buf_size: internal JPEG frame buffer size, in bytes
79  * @main: size in bytes of the main image
80  * @thumb: size in bytes of the thumb image, if it was accompanied
81  * @total: total size in bytes of the produced image
82  */
83 struct m5mols_capture {
84         struct m5mols_exif exif;
85         unsigned int buf_size;
86         u32 main;
87         u32 thumb;
88         u32 total;
89 };
90
91 /**
92  * struct m5mols_scenemode - structure for the scenemode capability
93  * @metering: metering light register value
94  * @ev_bias: EV bias register value
95  * @wb_mode: mode which means the WhiteBalance is Auto or Manual
96  * @wb_preset: whitebalance preset register value in the Manual mode
97  * @chroma_en: register value whether the Chroma capability is enabled or not
98  * @chroma_lvl: chroma's level register value
99  * @edge_en: register value Whether the Edge capability is enabled or not
100  * @edge_lvl: edge's level register value
101  * @af_range: Auto Focus's range
102  * @fd_mode: Face Detection mode
103  * @mcc: Multi-axis Color Conversion which means emotion color
104  * @light: status of the Light
105  * @flash: status of the Flash
106  * @tone: Tone color which means Contrast
107  * @iso: ISO register value
108  * @capt_mode: Mode of the Image Stabilization while the camera capturing
109  * @wdr: Wide Dynamic Range register value
110  *
111  * The each value according to each scenemode is recommended in the documents.
112  */
113 struct m5mols_scenemode {
114         u8 metering;
115         u8 ev_bias;
116         u8 wb_mode;
117         u8 wb_preset;
118         u8 chroma_en;
119         u8 chroma_lvl;
120         u8 edge_en;
121         u8 edge_lvl;
122         u8 af_range;
123         u8 fd_mode;
124         u8 mcc;
125         u8 light;
126         u8 flash;
127         u8 tone;
128         u8 iso;
129         u8 capt_mode;
130         u8 wdr;
131 };
132
133 /**
134  * struct m5mols_version - firmware version information
135  * @customer:   customer information
136  * @project:    version of project information according to customer
137  * @fw:         firmware revision
138  * @hw:         hardware revision
139  * @param:      version of the parameter
140  * @awb:        Auto WhiteBalance algorithm version
141  * @str:        information about manufacturer and packaging vendor
142  * @af:         Auto Focus version
143  *
144  * The register offset starts the customer version at 0x0, and it ends
145  * the awb version at 0x09. The customer, project information occupies 1 bytes
146  * each. And also the fw, hw, param, awb each requires 2 bytes. The str is
147  * unique string associated with firmware's version. It includes information
148  * about manufacturer and the vendor of the sensor's packaging. The least
149  * significant 2 bytes of the string indicate packaging manufacturer.
150  */
151 #define VERSION_STRING_SIZE     22
152 struct m5mols_version {
153         u8      customer;
154         u8      project;
155         u16     fw;
156         u16     hw;
157         u16     param;
158         u16     awb;
159         u8      str[VERSION_STRING_SIZE];
160         u8      af;
161 };
162
163 /**
164  * struct m5mols_info - M-5MOLS driver data structure
165  * @pdata: platform data
166  * @sd: v4l-subdev instance
167  * @pad: media pad
168  * @irq_waitq: waitqueue for the capture
169  * @irq_done: set to 1 in the interrupt handler
170  * @handle: control handler
171  * @auto_exposure: auto/manual exposure control
172  * @exposure_bias: exposure compensation control
173  * @exposure: manual exposure control
174  * @metering: exposure metering control
175  * @auto_iso: auto/manual ISO sensitivity control
176  * @iso: manual ISO sensitivity control
177  * @auto_wb: auto white balance control
178  * @lock_3a: 3A lock control
179  * @colorfx: color effect control
180  * @saturation: saturation control
181  * @zoom: zoom control
182  * @wdr: wide dynamic range control
183  * @stabilization: image stabilization control
184  * @jpeg_quality: JPEG compression quality control
185  * @set_power: optional power callback to the board code
186  * @lock: mutex protecting the structure fields below
187  * @ffmt: current fmt according to resolution type
188  * @res_type: current resolution type
189  * @ver: information of the version
190  * @cap: the capture mode attributes
191  * @isp_ready: 1 when the ISP controller has completed booting
192  * @power: current sensor's power status
193  * @ctrl_sync: 1 when the control handler state is restored in H/W
194  * @resolution: register value for current resolution
195  * @mode: register value for current operation mode
196  */
197 struct m5mols_info {
198         const struct m5mols_platform_data *pdata;
199         struct v4l2_subdev sd;
200         struct media_pad pad;
201
202         wait_queue_head_t irq_waitq;
203         atomic_t irq_done;
204
205         struct v4l2_ctrl_handler handle;
206         struct {
207                 /* exposure/exposure bias/auto exposure cluster */
208                 struct v4l2_ctrl *auto_exposure;
209                 struct v4l2_ctrl *exposure_bias;
210                 struct v4l2_ctrl *exposure;
211                 struct v4l2_ctrl *metering;
212         };
213         struct {
214                 /* iso/auto iso cluster */
215                 struct v4l2_ctrl *auto_iso;
216                 struct v4l2_ctrl *iso;
217         };
218         struct v4l2_ctrl *auto_wb;
219
220         struct v4l2_ctrl *lock_3a;
221         struct v4l2_ctrl *colorfx;
222         struct v4l2_ctrl *saturation;
223         struct v4l2_ctrl *zoom;
224         struct v4l2_ctrl *wdr;
225         struct v4l2_ctrl *stabilization;
226         struct v4l2_ctrl *jpeg_quality;
227
228         int (*set_power)(struct device *dev, int on);
229
230         struct mutex lock;
231
232         struct v4l2_mbus_framefmt ffmt[M5MOLS_RESTYPE_MAX];
233         int res_type;
234
235         struct m5mols_version ver;
236         struct m5mols_capture cap;
237
238         unsigned int isp_ready:1;
239         unsigned int power:1;
240         unsigned int ctrl_sync:1;
241
242         u8 resolution;
243         u8 mode;
244 };
245
246 #define is_available_af(__info) (__info->ver.af)
247 #define is_code(__code, __type) (__code == m5mols_default_ffmt[__type].code)
248 #define is_manufacturer(__info, __manufacturer) \
249                                 (__info->ver.str[0] == __manufacturer[0] && \
250                                  __info->ver.str[1] == __manufacturer[1])
251 /*
252  * I2C operation of the M-5MOLS
253  *
254  * The I2C read operation of the M-5MOLS requires 2 messages. The first
255  * message sends the information about the command, command category, and total
256  * message size. The second message is used to retrieve the data specifed in
257  * the first message
258  *
259  *   1st message                                2nd message
260  *   +-------+---+----------+-----+-------+     +------+------+------+------+
261  *   | size1 | R | category | cmd | size2 |     | d[0] | d[1] | d[2] | d[3] |
262  *   +-------+---+----------+-----+-------+     +------+------+------+------+
263  *   - size1: message data size(5 in this case)
264  *   - size2: desired buffer size of the 2nd message
265  *   - d[0..3]: according to size2
266  *
267  * The I2C write operation needs just one message. The message includes
268  * category, command, total size, and desired data.
269  *
270  *   1st message
271  *   +-------+---+----------+-----+------+------+------+------+
272  *   | size1 | W | category | cmd | d[0] | d[1] | d[2] | d[3] |
273  *   +-------+---+----------+-----+------+------+------+------+
274  *   - d[0..3]: according to size1
275  */
276 int m5mols_read_u8(struct v4l2_subdev *sd, u32 reg_comb, u8 *val);
277 int m5mols_read_u16(struct v4l2_subdev *sd, u32 reg_comb, u16 *val);
278 int m5mols_read_u32(struct v4l2_subdev *sd, u32 reg_comb, u32 *val);
279 int m5mols_write(struct v4l2_subdev *sd, u32 reg_comb, u32 val);
280
281 int m5mols_busy_wait(struct v4l2_subdev *sd, u32 reg, u32 value, u32 mask,
282                      int timeout);
283
284 /* Mask value for busy waiting until M-5MOLS I2C interface is initialized */
285 #define M5MOLS_I2C_RDY_WAIT_FL          (1 << 16)
286 /* ISP state transition timeout, in ms */
287 #define M5MOLS_MODE_CHANGE_TIMEOUT      200
288 #define M5MOLS_BUSY_WAIT_DEF_TIMEOUT    250
289
290 /*
291  * Mode operation of the M-5MOLS
292  *
293  * Changing the mode of the M-5MOLS is needed right executing order.
294  * There are three modes(PARAMETER, MONITOR, CAPTURE) which can be changed
295  * by user. There are various categories associated with each mode.
296  *
297  * +============================================================+
298  * | mode       | category                                      |
299  * +============================================================+
300  * | FLASH      | FLASH(only after Stand-by or Power-on)        |
301  * | SYSTEM     | SYSTEM(only after sensor arm-booting)         |
302  * | PARAMETER  | PARAMETER                                     |
303  * | MONITOR    | MONITOR(preview), Auto Focus, Face Detection  |
304  * | CAPTURE    | Single CAPTURE, Preview(recording)            |
305  * +============================================================+
306  *
307  * The available executing order between each modes are as follows:
308  *   PARAMETER <---> MONITOR <---> CAPTURE
309  */
310 int m5mols_set_mode(struct m5mols_info *info, u8 mode);
311
312 int m5mols_enable_interrupt(struct v4l2_subdev *sd, u8 reg);
313 int m5mols_wait_interrupt(struct v4l2_subdev *sd, u8 condition, u32 timeout);
314 int m5mols_restore_controls(struct m5mols_info *info);
315 int m5mols_start_capture(struct m5mols_info *info);
316 int m5mols_do_scenemode(struct m5mols_info *info, u8 mode);
317 int m5mols_lock_3a(struct m5mols_info *info, bool lock);
318 int m5mols_set_ctrl(struct v4l2_ctrl *ctrl);
319 int m5mols_init_controls(struct v4l2_subdev *sd);
320
321 /* The firmware function */
322 int m5mols_update_fw(struct v4l2_subdev *sd,
323                      int (*set_power)(struct m5mols_info *, bool));
324
325 static inline struct m5mols_info *to_m5mols(struct v4l2_subdev *subdev)
326 {
327         return container_of(subdev, struct m5mols_info, sd);
328 }
329
330 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
331 {
332         struct m5mols_info *info = container_of(ctrl->handler,
333                                                 struct m5mols_info, handle);
334         return &info->sd;
335 }
336
337 static inline void m5mols_set_ctrl_mode(struct v4l2_ctrl *ctrl,
338                                         unsigned int mode)
339 {
340         ctrl->priv = (void *)(uintptr_t)mode;
341 }
342
343 static inline unsigned int m5mols_get_ctrl_mode(struct v4l2_ctrl *ctrl)
344 {
345         return (unsigned int)(uintptr_t)ctrl->priv;
346 }
347
348 #endif  /* M5MOLS_H */