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