This source file includes following definitions.
- to_ov5640_dev
- ctrl_to_sd
- ov5640_init_slave_id
- ov5640_write_reg
- ov5640_read_reg
- ov5640_read_reg16
- ov5640_write_reg16
- ov5640_mod_reg
- ov5640_compute_sys_clk
- ov5640_calc_sys_clk
- ov5640_set_mipi_pclk
- ov5640_calc_pclk
- ov5640_set_dvp_pclk
- ov5640_set_jpeg_timings
- ov5640_set_timings
- ov5640_load_regs
- ov5640_set_autoexposure
- ov5640_get_exposure
- ov5640_set_exposure
- ov5640_get_gain
- ov5640_set_gain
- ov5640_set_autogain
- ov5640_set_stream_dvp
- ov5640_set_stream_mipi
- ov5640_get_sysclk
- ov5640_set_night_mode
- ov5640_get_hts
- ov5640_get_vts
- ov5640_set_vts
- ov5640_get_light_freq
- ov5640_set_bandingfilter
- ov5640_set_ae_target
- ov5640_get_binning
- ov5640_set_binning
- ov5640_set_virtual_channel
- ov5640_find_mode
- ov5640_set_mode_exposure_calc
- ov5640_set_mode_direct
- ov5640_set_mode
- ov5640_restore_mode
- ov5640_power
- ov5640_reset
- ov5640_set_power_on
- ov5640_set_power_off
- ov5640_set_power
- ov5640_s_power
- ov5640_try_frame_interval
- ov5640_get_fmt
- ov5640_try_fmt_internal
- ov5640_set_fmt
- ov5640_set_framefmt
- ov5640_set_ctrl_hue
- ov5640_set_ctrl_contrast
- ov5640_set_ctrl_saturation
- ov5640_set_ctrl_white_balance
- ov5640_set_ctrl_exposure
- ov5640_set_ctrl_gain
- ov5640_set_ctrl_test_pattern
- ov5640_set_ctrl_light_freq
- ov5640_set_ctrl_hflip
- ov5640_set_ctrl_vflip
- ov5640_g_volatile_ctrl
- ov5640_s_ctrl
- ov5640_init_controls
- ov5640_enum_frame_size
- ov5640_enum_frame_interval
- ov5640_g_frame_interval
- ov5640_s_frame_interval
- ov5640_enum_mbus_code
- ov5640_s_stream
- ov5640_get_regulators
- ov5640_check_chip_id
- ov5640_probe
- ov5640_remove
1
2
3
4
5
6
7 #include <linux/clk.h>
8 #include <linux/clk-provider.h>
9 #include <linux/clkdev.h>
10 #include <linux/ctype.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <media/v4l2-async.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-fwnode.h>
26 #include <media/v4l2-subdev.h>
27
28
29 #define OV5640_XCLK_MIN 6000000
30 #define OV5640_XCLK_MAX 54000000
31
32 #define OV5640_DEFAULT_SLAVE_ID 0x3c
33
34 #define OV5640_REG_SYS_RESET02 0x3002
35 #define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006
36 #define OV5640_REG_SYS_CTRL0 0x3008
37 #define OV5640_REG_CHIP_ID 0x300a
38 #define OV5640_REG_IO_MIPI_CTRL00 0x300e
39 #define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017
40 #define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018
41 #define OV5640_REG_PAD_OUTPUT00 0x3019
42 #define OV5640_REG_SYSTEM_CONTROL1 0x302e
43 #define OV5640_REG_SC_PLL_CTRL0 0x3034
44 #define OV5640_REG_SC_PLL_CTRL1 0x3035
45 #define OV5640_REG_SC_PLL_CTRL2 0x3036
46 #define OV5640_REG_SC_PLL_CTRL3 0x3037
47 #define OV5640_REG_SLAVE_ID 0x3100
48 #define OV5640_REG_SCCB_SYS_CTRL1 0x3103
49 #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
50 #define OV5640_REG_AWB_R_GAIN 0x3400
51 #define OV5640_REG_AWB_G_GAIN 0x3402
52 #define OV5640_REG_AWB_B_GAIN 0x3404
53 #define OV5640_REG_AWB_MANUAL_CTRL 0x3406
54 #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
55 #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
56 #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
57 #define OV5640_REG_AEC_PK_MANUAL 0x3503
58 #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
59 #define OV5640_REG_AEC_PK_VTS 0x350c
60 #define OV5640_REG_TIMING_DVPHO 0x3808
61 #define OV5640_REG_TIMING_DVPVO 0x380a
62 #define OV5640_REG_TIMING_HTS 0x380c
63 #define OV5640_REG_TIMING_VTS 0x380e
64 #define OV5640_REG_TIMING_TC_REG20 0x3820
65 #define OV5640_REG_TIMING_TC_REG21 0x3821
66 #define OV5640_REG_AEC_CTRL00 0x3a00
67 #define OV5640_REG_AEC_B50_STEP 0x3a08
68 #define OV5640_REG_AEC_B60_STEP 0x3a0a
69 #define OV5640_REG_AEC_CTRL0D 0x3a0d
70 #define OV5640_REG_AEC_CTRL0E 0x3a0e
71 #define OV5640_REG_AEC_CTRL0F 0x3a0f
72 #define OV5640_REG_AEC_CTRL10 0x3a10
73 #define OV5640_REG_AEC_CTRL11 0x3a11
74 #define OV5640_REG_AEC_CTRL1B 0x3a1b
75 #define OV5640_REG_AEC_CTRL1E 0x3a1e
76 #define OV5640_REG_AEC_CTRL1F 0x3a1f
77 #define OV5640_REG_HZ5060_CTRL00 0x3c00
78 #define OV5640_REG_HZ5060_CTRL01 0x3c01
79 #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
80 #define OV5640_REG_FRAME_CTRL01 0x4202
81 #define OV5640_REG_FORMAT_CONTROL00 0x4300
82 #define OV5640_REG_VFIFO_HSIZE 0x4602
83 #define OV5640_REG_VFIFO_VSIZE 0x4604
84 #define OV5640_REG_JPG_MODE_SELECT 0x4713
85 #define OV5640_REG_POLARITY_CTRL00 0x4740
86 #define OV5640_REG_MIPI_CTRL00 0x4800
87 #define OV5640_REG_DEBUG_MODE 0x4814
88 #define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f
89 #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
90 #define OV5640_REG_SDE_CTRL0 0x5580
91 #define OV5640_REG_SDE_CTRL1 0x5581
92 #define OV5640_REG_SDE_CTRL3 0x5583
93 #define OV5640_REG_SDE_CTRL4 0x5584
94 #define OV5640_REG_SDE_CTRL5 0x5585
95 #define OV5640_REG_AVG_READOUT 0x56a1
96
97 enum ov5640_mode_id {
98 OV5640_MODE_QCIF_176_144 = 0,
99 OV5640_MODE_QVGA_320_240,
100 OV5640_MODE_VGA_640_480,
101 OV5640_MODE_NTSC_720_480,
102 OV5640_MODE_PAL_720_576,
103 OV5640_MODE_XGA_1024_768,
104 OV5640_MODE_720P_1280_720,
105 OV5640_MODE_1080P_1920_1080,
106 OV5640_MODE_QSXGA_2592_1944,
107 OV5640_NUM_MODES,
108 };
109
110 enum ov5640_frame_rate {
111 OV5640_15_FPS = 0,
112 OV5640_30_FPS,
113 OV5640_60_FPS,
114 OV5640_NUM_FRAMERATES,
115 };
116
117 enum ov5640_format_mux {
118 OV5640_FMT_MUX_YUV422 = 0,
119 OV5640_FMT_MUX_RGB,
120 OV5640_FMT_MUX_DITHER,
121 OV5640_FMT_MUX_RAW_DPC,
122 OV5640_FMT_MUX_SNR_RAW,
123 OV5640_FMT_MUX_RAW_CIP,
124 };
125
126 struct ov5640_pixfmt {
127 u32 code;
128 u32 colorspace;
129 };
130
131 static const struct ov5640_pixfmt ov5640_formats[] = {
132 { MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
133 { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
134 { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
135 { MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
136 { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
137 { MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB, },
138 { MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_COLORSPACE_SRGB, },
139 { MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_COLORSPACE_SRGB, },
140 { MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_COLORSPACE_SRGB, },
141 };
142
143
144
145
146
147 static unsigned int virtual_channel;
148 module_param(virtual_channel, uint, 0444);
149 MODULE_PARM_DESC(virtual_channel,
150 "MIPI CSI-2 virtual channel (0..3), default 0");
151
152 static const int ov5640_framerates[] = {
153 [OV5640_15_FPS] = 15,
154 [OV5640_30_FPS] = 30,
155 [OV5640_60_FPS] = 60,
156 };
157
158
159 static const char * const ov5640_supply_name[] = {
160 "DOVDD",
161 "AVDD",
162 "DVDD",
163 };
164
165 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
166
167
168
169
170
171 enum ov5640_downsize_mode {
172 SUBSAMPLING,
173 SCALING,
174 };
175
176 struct reg_value {
177 u16 reg_addr;
178 u8 val;
179 u8 mask;
180 u32 delay_ms;
181 };
182
183 struct ov5640_mode_info {
184 enum ov5640_mode_id id;
185 enum ov5640_downsize_mode dn_mode;
186 u32 hact;
187 u32 htot;
188 u32 vact;
189 u32 vtot;
190 const struct reg_value *reg_data;
191 u32 reg_data_size;
192 };
193
194 struct ov5640_ctrls {
195 struct v4l2_ctrl_handler handler;
196 struct {
197 struct v4l2_ctrl *auto_exp;
198 struct v4l2_ctrl *exposure;
199 };
200 struct {
201 struct v4l2_ctrl *auto_wb;
202 struct v4l2_ctrl *blue_balance;
203 struct v4l2_ctrl *red_balance;
204 };
205 struct {
206 struct v4l2_ctrl *auto_gain;
207 struct v4l2_ctrl *gain;
208 };
209 struct v4l2_ctrl *brightness;
210 struct v4l2_ctrl *light_freq;
211 struct v4l2_ctrl *saturation;
212 struct v4l2_ctrl *contrast;
213 struct v4l2_ctrl *hue;
214 struct v4l2_ctrl *test_pattern;
215 struct v4l2_ctrl *hflip;
216 struct v4l2_ctrl *vflip;
217 };
218
219 struct ov5640_dev {
220 struct i2c_client *i2c_client;
221 struct v4l2_subdev sd;
222 struct media_pad pad;
223 struct v4l2_fwnode_endpoint ep;
224 struct clk *xclk;
225 u32 xclk_freq;
226
227 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
228 struct gpio_desc *reset_gpio;
229 struct gpio_desc *pwdn_gpio;
230 bool upside_down;
231
232
233 struct mutex lock;
234
235 int power_count;
236
237 struct v4l2_mbus_framefmt fmt;
238 bool pending_fmt_change;
239
240 const struct ov5640_mode_info *current_mode;
241 const struct ov5640_mode_info *last_mode;
242 enum ov5640_frame_rate current_fr;
243 struct v4l2_fract frame_interval;
244
245 struct ov5640_ctrls ctrls;
246
247 u32 prev_sysclk, prev_hts;
248 u32 ae_low, ae_high, ae_target;
249
250 bool pending_mode_change;
251 bool streaming;
252 };
253
254 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
255 {
256 return container_of(sd, struct ov5640_dev, sd);
257 }
258
259 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
260 {
261 return &container_of(ctrl->handler, struct ov5640_dev,
262 ctrls.handler)->sd;
263 }
264
265
266
267
268
269
270
271
272
273 static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
274 {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
275 {0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
276 {0x3630, 0x36, 0, 0},
277 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
278 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
279 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
280 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
281 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
282 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
283 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
284 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
285 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
286 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
287 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
288 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
289 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
290 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
291 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
292 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
293 {0x3810, 0x00, 0, 0},
294 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
295 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
296 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
297 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
298 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
299 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
300 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
301 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
302 {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
303 {0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0},
304 {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
305 {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
306 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
307 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
308 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
309 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
310 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
311 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
312 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
313 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
314 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
315 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
316 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
317 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
318 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
319 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
320 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
321 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
322 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
323 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
324 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
325 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
326 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
327 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
328 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
329 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
330 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
331 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
332 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
333 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
334 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
335 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
336 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
337 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
338 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
339 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
340 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
341 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
342 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
343 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
344 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
345 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
346 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
347 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
348 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
349 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
350 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
351 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
352 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
353 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
354 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
355 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
356 };
357
358 static const struct reg_value ov5640_setting_VGA_640_480[] = {
359 {0x3c07, 0x08, 0, 0},
360 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
361 {0x3814, 0x31, 0, 0},
362 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
363 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
364 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
365 {0x3810, 0x00, 0, 0},
366 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
367 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
368 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
369 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
370 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
371 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
372 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
373 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
374 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
375 };
376
377 static const struct reg_value ov5640_setting_XGA_1024_768[] = {
378 {0x3c07, 0x08, 0, 0},
379 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
380 {0x3814, 0x31, 0, 0},
381 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
382 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
383 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
384 {0x3810, 0x00, 0, 0},
385 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
386 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
387 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
388 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
389 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
390 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
391 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
392 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
393 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
394 };
395
396 static const struct reg_value ov5640_setting_QVGA_320_240[] = {
397 {0x3c07, 0x08, 0, 0},
398 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
399 {0x3814, 0x31, 0, 0},
400 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
401 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
402 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
403 {0x3810, 0x00, 0, 0},
404 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
405 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
406 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
407 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
408 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
409 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
410 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
411 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
412 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
413 };
414
415 static const struct reg_value ov5640_setting_QCIF_176_144[] = {
416 {0x3c07, 0x08, 0, 0},
417 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
418 {0x3814, 0x31, 0, 0},
419 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
420 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
421 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
422 {0x3810, 0x00, 0, 0},
423 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
424 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
425 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
426 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
427 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
428 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
429 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
430 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
431 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
432 };
433
434 static const struct reg_value ov5640_setting_NTSC_720_480[] = {
435 {0x3c07, 0x08, 0, 0},
436 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
437 {0x3814, 0x31, 0, 0},
438 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
439 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
440 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
441 {0x3810, 0x00, 0, 0},
442 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
443 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
444 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
445 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
446 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
447 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
448 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
449 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
450 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
451 };
452
453 static const struct reg_value ov5640_setting_PAL_720_576[] = {
454 {0x3c07, 0x08, 0, 0},
455 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
456 {0x3814, 0x31, 0, 0},
457 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
458 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
459 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
460 {0x3810, 0x00, 0, 0},
461 {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
462 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
463 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
464 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
465 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
466 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
467 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
468 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
469 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
470 };
471
472 static const struct reg_value ov5640_setting_720P_1280_720[] = {
473 {0x3c07, 0x07, 0, 0},
474 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
475 {0x3814, 0x31, 0, 0},
476 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
477 {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
478 {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
479 {0x3810, 0x00, 0, 0},
480 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
481 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
482 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
483 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
484 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
485 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
486 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
487 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
488 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
489 };
490
491 static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
492 {0x3008, 0x42, 0, 0},
493 {0x3c07, 0x08, 0, 0},
494 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
495 {0x3814, 0x11, 0, 0},
496 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
497 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
498 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
499 {0x3810, 0x00, 0, 0},
500 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
501 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
502 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
503 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
504 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
505 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
506 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
507 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
508 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
509 {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
510 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
511 {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
512 {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
513 {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
514 {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
515 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
516 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
517 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
518 {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0},
519 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
520 {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
521 };
522
523 static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
524 {0x3c07, 0x08, 0, 0},
525 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
526 {0x3814, 0x11, 0, 0},
527 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
528 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
529 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
530 {0x3810, 0x00, 0, 0},
531 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
532 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
533 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
534 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
535 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
536 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
537 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
538 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
539 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
540 };
541
542
543 static const struct ov5640_mode_info ov5640_mode_init_data = {
544 0, SUBSAMPLING, 640, 1896, 480, 984,
545 ov5640_init_setting_30fps_VGA,
546 ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
547 };
548
549 static const struct ov5640_mode_info
550 ov5640_mode_data[OV5640_NUM_MODES] = {
551 {OV5640_MODE_QCIF_176_144, SUBSAMPLING,
552 176, 1896, 144, 984,
553 ov5640_setting_QCIF_176_144,
554 ARRAY_SIZE(ov5640_setting_QCIF_176_144)},
555 {OV5640_MODE_QVGA_320_240, SUBSAMPLING,
556 320, 1896, 240, 984,
557 ov5640_setting_QVGA_320_240,
558 ARRAY_SIZE(ov5640_setting_QVGA_320_240)},
559 {OV5640_MODE_VGA_640_480, SUBSAMPLING,
560 640, 1896, 480, 1080,
561 ov5640_setting_VGA_640_480,
562 ARRAY_SIZE(ov5640_setting_VGA_640_480)},
563 {OV5640_MODE_NTSC_720_480, SUBSAMPLING,
564 720, 1896, 480, 984,
565 ov5640_setting_NTSC_720_480,
566 ARRAY_SIZE(ov5640_setting_NTSC_720_480)},
567 {OV5640_MODE_PAL_720_576, SUBSAMPLING,
568 720, 1896, 576, 984,
569 ov5640_setting_PAL_720_576,
570 ARRAY_SIZE(ov5640_setting_PAL_720_576)},
571 {OV5640_MODE_XGA_1024_768, SUBSAMPLING,
572 1024, 1896, 768, 1080,
573 ov5640_setting_XGA_1024_768,
574 ARRAY_SIZE(ov5640_setting_XGA_1024_768)},
575 {OV5640_MODE_720P_1280_720, SUBSAMPLING,
576 1280, 1892, 720, 740,
577 ov5640_setting_720P_1280_720,
578 ARRAY_SIZE(ov5640_setting_720P_1280_720)},
579 {OV5640_MODE_1080P_1920_1080, SCALING,
580 1920, 2500, 1080, 1120,
581 ov5640_setting_1080P_1920_1080,
582 ARRAY_SIZE(ov5640_setting_1080P_1920_1080)},
583 {OV5640_MODE_QSXGA_2592_1944, SCALING,
584 2592, 2844, 1944, 1968,
585 ov5640_setting_QSXGA_2592_1944,
586 ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944)},
587 };
588
589 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
590 {
591 struct i2c_client *client = sensor->i2c_client;
592 struct i2c_msg msg;
593 u8 buf[3];
594 int ret;
595
596 if (client->addr == OV5640_DEFAULT_SLAVE_ID)
597 return 0;
598
599 buf[0] = OV5640_REG_SLAVE_ID >> 8;
600 buf[1] = OV5640_REG_SLAVE_ID & 0xff;
601 buf[2] = client->addr << 1;
602
603 msg.addr = OV5640_DEFAULT_SLAVE_ID;
604 msg.flags = 0;
605 msg.buf = buf;
606 msg.len = sizeof(buf);
607
608 ret = i2c_transfer(client->adapter, &msg, 1);
609 if (ret < 0) {
610 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
611 return ret;
612 }
613
614 return 0;
615 }
616
617 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
618 {
619 struct i2c_client *client = sensor->i2c_client;
620 struct i2c_msg msg;
621 u8 buf[3];
622 int ret;
623
624 buf[0] = reg >> 8;
625 buf[1] = reg & 0xff;
626 buf[2] = val;
627
628 msg.addr = client->addr;
629 msg.flags = client->flags;
630 msg.buf = buf;
631 msg.len = sizeof(buf);
632
633 ret = i2c_transfer(client->adapter, &msg, 1);
634 if (ret < 0) {
635 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
636 __func__, reg, val);
637 return ret;
638 }
639
640 return 0;
641 }
642
643 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
644 {
645 struct i2c_client *client = sensor->i2c_client;
646 struct i2c_msg msg[2];
647 u8 buf[2];
648 int ret;
649
650 buf[0] = reg >> 8;
651 buf[1] = reg & 0xff;
652
653 msg[0].addr = client->addr;
654 msg[0].flags = client->flags;
655 msg[0].buf = buf;
656 msg[0].len = sizeof(buf);
657
658 msg[1].addr = client->addr;
659 msg[1].flags = client->flags | I2C_M_RD;
660 msg[1].buf = buf;
661 msg[1].len = 1;
662
663 ret = i2c_transfer(client->adapter, msg, 2);
664 if (ret < 0) {
665 dev_err(&client->dev, "%s: error: reg=%x\n",
666 __func__, reg);
667 return ret;
668 }
669
670 *val = buf[0];
671 return 0;
672 }
673
674 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
675 {
676 u8 hi, lo;
677 int ret;
678
679 ret = ov5640_read_reg(sensor, reg, &hi);
680 if (ret)
681 return ret;
682 ret = ov5640_read_reg(sensor, reg + 1, &lo);
683 if (ret)
684 return ret;
685
686 *val = ((u16)hi << 8) | (u16)lo;
687 return 0;
688 }
689
690 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
691 {
692 int ret;
693
694 ret = ov5640_write_reg(sensor, reg, val >> 8);
695 if (ret)
696 return ret;
697
698 return ov5640_write_reg(sensor, reg + 1, val & 0xff);
699 }
700
701 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
702 u8 mask, u8 val)
703 {
704 u8 readval;
705 int ret;
706
707 ret = ov5640_read_reg(sensor, reg, &readval);
708 if (ret)
709 return ret;
710
711 readval &= ~mask;
712 val &= mask;
713 val |= readval;
714
715 return ov5640_write_reg(sensor, reg, val);
716 }
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781 #define OV5640_PLL_PREDIV 3
782
783 #define OV5640_PLL_MULT_MIN 4
784 #define OV5640_PLL_MULT_MAX 252
785
786
787
788
789
790 #define OV5640_SYSDIV_MIN 1
791 #define OV5640_SYSDIV_MAX 16
792
793
794
795
796
797 #define OV5640_MIPI_DIV_PCLK 2
798 #define OV5640_MIPI_DIV_SCLK 1
799
800
801
802
803
804 #define OV5640_PLL_ROOT_DIV 2
805 #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4)
806
807
808
809
810 #define OV5640_BIT_DIV 2
811 #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08
812
813
814
815
816
817 #define OV5640_SCLK_ROOT_DIV 2
818
819
820
821
822
823 #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
824
825
826
827
828
829 #define OV5640_PCLK_ROOT_DIV 1
830 #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00
831
832 static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
833 u8 pll_prediv, u8 pll_mult,
834 u8 sysdiv)
835 {
836 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
837
838
839 if (sysclk / 1000000 > 1000)
840 return 0;
841
842 return sysclk / sysdiv;
843 }
844
845 static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
846 unsigned long rate,
847 u8 *pll_prediv, u8 *pll_mult,
848 u8 *sysdiv)
849 {
850 unsigned long best = ~0;
851 u8 best_sysdiv = 1, best_mult = 1;
852 u8 _sysdiv, _pll_mult;
853
854 for (_sysdiv = OV5640_SYSDIV_MIN;
855 _sysdiv <= OV5640_SYSDIV_MAX;
856 _sysdiv++) {
857 for (_pll_mult = OV5640_PLL_MULT_MIN;
858 _pll_mult <= OV5640_PLL_MULT_MAX;
859 _pll_mult++) {
860 unsigned long _rate;
861
862
863
864
865
866 if (_pll_mult > 127 && (_pll_mult % 2))
867 continue;
868
869 _rate = ov5640_compute_sys_clk(sensor,
870 OV5640_PLL_PREDIV,
871 _pll_mult, _sysdiv);
872
873
874
875
876
877 if (!_rate)
878 break;
879
880
881
882
883
884 if (_rate < rate)
885 continue;
886
887 if (abs(rate - _rate) < abs(rate - best)) {
888 best = _rate;
889 best_sysdiv = _sysdiv;
890 best_mult = _pll_mult;
891 }
892
893 if (_rate == rate)
894 goto out;
895 }
896 }
897
898 out:
899 *sysdiv = best_sysdiv;
900 *pll_prediv = OV5640_PLL_PREDIV;
901 *pll_mult = best_mult;
902
903 return best;
904 }
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947 static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor,
948 unsigned long rate)
949 {
950 const struct ov5640_mode_info *mode = sensor->current_mode;
951 u8 prediv, mult, sysdiv;
952 u8 mipi_div;
953 int ret;
954
955
956
957
958
959
960 if (mode->dn_mode == SCALING ||
961 (mode->id == OV5640_MODE_720P_1280_720))
962 mipi_div = OV5640_MIPI_DIV_SCLK;
963 else
964 mipi_div = OV5640_MIPI_DIV_PCLK;
965
966 ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv);
967
968 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
969 0x0f, OV5640_PLL_CTRL0_MIPI_MODE_8BIT);
970
971 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
972 0xff, sysdiv << 4 | mipi_div);
973 if (ret)
974 return ret;
975
976 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
977 if (ret)
978 return ret;
979
980 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
981 0x1f, OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 | prediv);
982 if (ret)
983 return ret;
984
985 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER,
986 0x30, OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS);
987 }
988
989 static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
990 unsigned long rate,
991 u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
992 u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
993 {
994 unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
995 OV5640_PCLK_ROOT_DIV;
996
997 _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
998 sysdiv);
999 *pll_rdiv = OV5640_PLL_ROOT_DIV;
1000 *bit_div = OV5640_BIT_DIV;
1001 *pclk_div = OV5640_PCLK_ROOT_DIV;
1002
1003 return _rate / *pll_rdiv / *bit_div / *pclk_div;
1004 }
1005
1006 static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate)
1007 {
1008 u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1009 int ret;
1010
1011 ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1012 &bit_div, &pclk_div);
1013
1014 if (bit_div == 2)
1015 bit_div = 8;
1016
1017 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1018 0x0f, bit_div);
1019 if (ret)
1020 return ret;
1021
1022
1023
1024
1025
1026 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1027 0xff, sysdiv << 4);
1028 if (ret)
1029 return ret;
1030
1031 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1032 0xff, mult);
1033 if (ret)
1034 return ret;
1035
1036 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1037 0x1f, prediv | ((pll_rdiv - 1) << 4));
1038 if (ret)
1039 return ret;
1040
1041 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1042 (ilog2(pclk_div) << 4));
1043 }
1044
1045
1046 static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
1047 const struct ov5640_mode_info *mode)
1048 {
1049 int ret;
1050
1051
1052
1053
1054
1055
1056
1057
1058 ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
1059 if (ret < 0)
1060 return ret;
1061
1062 ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->hact);
1063 if (ret < 0)
1064 return ret;
1065
1066 return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->vact);
1067 }
1068
1069
1070 static int ov5640_set_timings(struct ov5640_dev *sensor,
1071 const struct ov5640_mode_info *mode)
1072 {
1073 int ret;
1074
1075 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
1076 ret = ov5640_set_jpeg_timings(sensor, mode);
1077 if (ret < 0)
1078 return ret;
1079 }
1080
1081 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact);
1082 if (ret < 0)
1083 return ret;
1084
1085 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact);
1086 if (ret < 0)
1087 return ret;
1088
1089 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
1090 if (ret < 0)
1091 return ret;
1092
1093 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot);
1094 }
1095
1096 static int ov5640_load_regs(struct ov5640_dev *sensor,
1097 const struct ov5640_mode_info *mode)
1098 {
1099 const struct reg_value *regs = mode->reg_data;
1100 unsigned int i;
1101 u32 delay_ms;
1102 u16 reg_addr;
1103 u8 mask, val;
1104 int ret = 0;
1105
1106 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
1107 delay_ms = regs->delay_ms;
1108 reg_addr = regs->reg_addr;
1109 val = regs->val;
1110 mask = regs->mask;
1111
1112 if (mask)
1113 ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1114 else
1115 ret = ov5640_write_reg(sensor, reg_addr, val);
1116 if (ret)
1117 break;
1118
1119 if (delay_ms)
1120 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
1121 }
1122
1123 return ov5640_set_timings(sensor, mode);
1124 }
1125
1126 static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1127 {
1128 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1129 BIT(0), on ? 0 : BIT(0));
1130 }
1131
1132
1133 static int ov5640_get_exposure(struct ov5640_dev *sensor)
1134 {
1135 int exp, ret;
1136 u8 temp;
1137
1138 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1139 if (ret)
1140 return ret;
1141 exp = ((int)temp & 0x0f) << 16;
1142 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1143 if (ret)
1144 return ret;
1145 exp |= ((int)temp << 8);
1146 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1147 if (ret)
1148 return ret;
1149 exp |= (int)temp;
1150
1151 return exp >> 4;
1152 }
1153
1154
1155 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1156 {
1157 int ret;
1158
1159 exposure <<= 4;
1160
1161 ret = ov5640_write_reg(sensor,
1162 OV5640_REG_AEC_PK_EXPOSURE_LO,
1163 exposure & 0xff);
1164 if (ret)
1165 return ret;
1166 ret = ov5640_write_reg(sensor,
1167 OV5640_REG_AEC_PK_EXPOSURE_MED,
1168 (exposure >> 8) & 0xff);
1169 if (ret)
1170 return ret;
1171 return ov5640_write_reg(sensor,
1172 OV5640_REG_AEC_PK_EXPOSURE_HI,
1173 (exposure >> 16) & 0x0f);
1174 }
1175
1176 static int ov5640_get_gain(struct ov5640_dev *sensor)
1177 {
1178 u16 gain;
1179 int ret;
1180
1181 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1182 if (ret)
1183 return ret;
1184
1185 return gain & 0x3ff;
1186 }
1187
1188 static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1189 {
1190 return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1191 (u16)gain & 0x3ff);
1192 }
1193
1194 static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1195 {
1196 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1197 BIT(1), on ? 0 : BIT(1));
1198 }
1199
1200 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1201 {
1202 int ret;
1203 unsigned int flags = sensor->ep.bus.parallel.flags;
1204 u8 pclk_pol = 0;
1205 u8 hsync_pol = 0;
1206 u8 vsync_pol = 0;
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226 if (on) {
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1238 pclk_pol = 1;
1239 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1240 hsync_pol = 1;
1241 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1242 vsync_pol = 1;
1243
1244 ret = ov5640_write_reg(sensor,
1245 OV5640_REG_POLARITY_CTRL00,
1246 (pclk_pol << 5) |
1247 (hsync_pol << 1) |
1248 vsync_pol);
1249
1250 if (ret)
1251 return ret;
1252 }
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262 ret = ov5640_write_reg(sensor,
1263 OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1264 if (ret)
1265 return ret;
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277 ret = ov5640_write_reg(sensor,
1278 OV5640_REG_PAD_OUTPUT_ENABLE01,
1279 on ? 0x7f : 0);
1280 if (ret)
1281 return ret;
1282
1283
1284
1285
1286
1287
1288
1289 return ov5640_write_reg(sensor,
1290 OV5640_REG_PAD_OUTPUT_ENABLE02,
1291 on ? 0xfc : 0);
1292 }
1293
1294 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1295 {
1296 int ret;
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1316 on ? 0x45 : 0x40);
1317 if (ret)
1318 return ret;
1319
1320 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1321 on ? 0x00 : 0x0f);
1322 }
1323
1324 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1325 {
1326
1327 u32 xvclk = sensor->xclk_freq / 10000;
1328 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1329 u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1330 u32 bit_div2x = 1, sclk_rdiv, sysclk;
1331 u8 temp1, temp2;
1332 int ret;
1333
1334 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1335 if (ret)
1336 return ret;
1337 temp2 = temp1 & 0x0f;
1338 if (temp2 == 8 || temp2 == 10)
1339 bit_div2x = temp2 / 2;
1340
1341 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1342 if (ret)
1343 return ret;
1344 sysdiv = temp1 >> 4;
1345 if (sysdiv == 0)
1346 sysdiv = 16;
1347
1348 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1349 if (ret)
1350 return ret;
1351 multiplier = temp1;
1352
1353 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1354 if (ret)
1355 return ret;
1356 prediv = temp1 & 0x0f;
1357 pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1358
1359 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1360 if (ret)
1361 return ret;
1362 temp2 = temp1 & 0x03;
1363 sclk_rdiv = sclk_rdiv_map[temp2];
1364
1365 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1366 return -EINVAL;
1367
1368 VCO = xvclk * multiplier / prediv;
1369
1370 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1371
1372 return sysclk;
1373 }
1374
1375 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1376 {
1377
1378 u8 mode;
1379 int ret;
1380
1381 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1382 if (ret)
1383 return ret;
1384 mode &= 0xfb;
1385 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1386 }
1387
1388 static int ov5640_get_hts(struct ov5640_dev *sensor)
1389 {
1390
1391 u16 hts;
1392 int ret;
1393
1394 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1395 if (ret)
1396 return ret;
1397 return hts;
1398 }
1399
1400 static int ov5640_get_vts(struct ov5640_dev *sensor)
1401 {
1402 u16 vts;
1403 int ret;
1404
1405 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1406 if (ret)
1407 return ret;
1408 return vts;
1409 }
1410
1411 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1412 {
1413 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1414 }
1415
1416 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1417 {
1418
1419 int ret, light_freq = 0;
1420 u8 temp, temp1;
1421
1422 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1423 if (ret)
1424 return ret;
1425
1426 if (temp & 0x80) {
1427
1428 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1429 &temp1);
1430 if (ret)
1431 return ret;
1432 if (temp1 & 0x04) {
1433
1434 light_freq = 50;
1435 } else {
1436
1437 light_freq = 60;
1438 }
1439 } else {
1440
1441 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1442 &temp1);
1443 if (ret)
1444 return ret;
1445
1446 if (temp1 & 0x01) {
1447
1448 light_freq = 50;
1449 } else {
1450
1451 }
1452 }
1453
1454 return light_freq;
1455 }
1456
1457 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1458 {
1459 u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1460 int ret;
1461
1462
1463 ret = ov5640_get_sysclk(sensor);
1464 if (ret < 0)
1465 return ret;
1466 if (ret == 0)
1467 return -EINVAL;
1468 sensor->prev_sysclk = ret;
1469
1470 ret = ov5640_get_hts(sensor);
1471 if (ret < 0)
1472 return ret;
1473 if (ret == 0)
1474 return -EINVAL;
1475 sensor->prev_hts = ret;
1476
1477
1478 ret = ov5640_get_vts(sensor);
1479 if (ret < 0)
1480 return ret;
1481 prev_vts = ret;
1482
1483
1484
1485 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
1486 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
1487 if (ret)
1488 return ret;
1489 if (!band_step60)
1490 return -EINVAL;
1491 max_band60 = (int)((prev_vts - 4) / band_step60);
1492 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1493 if (ret)
1494 return ret;
1495
1496
1497 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1498 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1499 if (ret)
1500 return ret;
1501 if (!band_step50)
1502 return -EINVAL;
1503 max_band50 = (int)((prev_vts - 4) / band_step50);
1504 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1505 }
1506
1507 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1508 {
1509
1510 u32 fast_high, fast_low;
1511 int ret;
1512
1513 sensor->ae_low = target * 23 / 25;
1514 sensor->ae_high = target * 27 / 25;
1515
1516 fast_high = sensor->ae_high << 1;
1517 if (fast_high > 255)
1518 fast_high = 255;
1519
1520 fast_low = sensor->ae_low >> 1;
1521
1522 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1523 if (ret)
1524 return ret;
1525 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1526 if (ret)
1527 return ret;
1528 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1529 if (ret)
1530 return ret;
1531 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1532 if (ret)
1533 return ret;
1534 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1535 if (ret)
1536 return ret;
1537 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1538 }
1539
1540 static int ov5640_get_binning(struct ov5640_dev *sensor)
1541 {
1542 u8 temp;
1543 int ret;
1544
1545 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1546 if (ret)
1547 return ret;
1548
1549 return temp & BIT(0);
1550 }
1551
1552 static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1553 {
1554 int ret;
1555
1556
1557
1558
1559
1560 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1561 BIT(0), enable ? BIT(0) : 0);
1562 if (ret)
1563 return ret;
1564
1565
1566
1567
1568
1569 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1570 BIT(0), enable ? BIT(0) : 0);
1571 }
1572
1573 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1574 {
1575 struct i2c_client *client = sensor->i2c_client;
1576 u8 temp, channel = virtual_channel;
1577 int ret;
1578
1579 if (channel > 3) {
1580 dev_err(&client->dev,
1581 "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
1582 __func__, channel);
1583 return -EINVAL;
1584 }
1585
1586 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1587 if (ret)
1588 return ret;
1589 temp &= ~(3 << 6);
1590 temp |= (channel << 6);
1591 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1592 }
1593
1594 static const struct ov5640_mode_info *
1595 ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
1596 int width, int height, bool nearest)
1597 {
1598 const struct ov5640_mode_info *mode;
1599
1600 mode = v4l2_find_nearest_size(ov5640_mode_data,
1601 ARRAY_SIZE(ov5640_mode_data),
1602 hact, vact,
1603 width, height);
1604
1605 if (!mode ||
1606 (!nearest && (mode->hact != width || mode->vact != height)))
1607 return NULL;
1608
1609
1610 if (fr == OV5640_60_FPS &&
1611 !(mode->hact == 640 && mode->vact == 480))
1612 return NULL;
1613
1614
1615 if ((mode->hact == 2592 && mode->vact == 1944) &&
1616 fr > OV5640_15_FPS)
1617 return NULL;
1618
1619 return mode;
1620 }
1621
1622
1623
1624
1625
1626 static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
1627 const struct ov5640_mode_info *mode)
1628 {
1629 u32 prev_shutter, prev_gain16;
1630 u32 cap_shutter, cap_gain16;
1631 u32 cap_sysclk, cap_hts, cap_vts;
1632 u32 light_freq, cap_bandfilt, cap_maxband;
1633 u32 cap_gain16_shutter;
1634 u8 average;
1635 int ret;
1636
1637 if (!mode->reg_data)
1638 return -EINVAL;
1639
1640
1641 ret = ov5640_get_exposure(sensor);
1642 if (ret < 0)
1643 return ret;
1644 prev_shutter = ret;
1645 ret = ov5640_get_binning(sensor);
1646 if (ret < 0)
1647 return ret;
1648 if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
1649 mode->id != OV5640_MODE_1080P_1920_1080)
1650 prev_shutter *= 2;
1651
1652
1653 ret = ov5640_get_gain(sensor);
1654 if (ret < 0)
1655 return ret;
1656 prev_gain16 = ret;
1657
1658
1659 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
1660 if (ret)
1661 return ret;
1662
1663
1664 ret = ov5640_set_night_mode(sensor);
1665 if (ret < 0)
1666 return ret;
1667
1668
1669 ret = ov5640_load_regs(sensor, mode);
1670 if (ret < 0)
1671 return ret;
1672
1673
1674 ret = ov5640_get_vts(sensor);
1675 if (ret < 0)
1676 return ret;
1677 cap_vts = ret;
1678 ret = ov5640_get_hts(sensor);
1679 if (ret < 0)
1680 return ret;
1681 if (ret == 0)
1682 return -EINVAL;
1683 cap_hts = ret;
1684
1685 ret = ov5640_get_sysclk(sensor);
1686 if (ret < 0)
1687 return ret;
1688 if (ret == 0)
1689 return -EINVAL;
1690 cap_sysclk = ret;
1691
1692
1693 ret = ov5640_get_light_freq(sensor);
1694 if (ret < 0)
1695 return ret;
1696 light_freq = ret;
1697
1698 if (light_freq == 60) {
1699
1700 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
1701 } else {
1702
1703 cap_bandfilt = cap_sysclk * 100 / cap_hts;
1704 }
1705
1706 if (!sensor->prev_sysclk) {
1707 ret = ov5640_get_sysclk(sensor);
1708 if (ret < 0)
1709 return ret;
1710 if (ret == 0)
1711 return -EINVAL;
1712 sensor->prev_sysclk = ret;
1713 }
1714
1715 if (!cap_bandfilt)
1716 return -EINVAL;
1717
1718 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
1719
1720
1721 if (average > sensor->ae_low && average < sensor->ae_high) {
1722
1723 cap_gain16_shutter =
1724 prev_gain16 * prev_shutter *
1725 cap_sysclk / sensor->prev_sysclk *
1726 sensor->prev_hts / cap_hts *
1727 sensor->ae_target / average;
1728 } else {
1729 cap_gain16_shutter =
1730 prev_gain16 * prev_shutter *
1731 cap_sysclk / sensor->prev_sysclk *
1732 sensor->prev_hts / cap_hts;
1733 }
1734
1735
1736 if (cap_gain16_shutter < (cap_bandfilt * 16)) {
1737
1738 cap_shutter = cap_gain16_shutter / 16;
1739 if (cap_shutter < 1)
1740 cap_shutter = 1;
1741
1742 cap_gain16 = cap_gain16_shutter / cap_shutter;
1743 if (cap_gain16 < 16)
1744 cap_gain16 = 16;
1745 } else {
1746 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
1747
1748 cap_shutter = cap_bandfilt * cap_maxband;
1749 if (!cap_shutter)
1750 return -EINVAL;
1751
1752 cap_gain16 = cap_gain16_shutter / cap_shutter;
1753 } else {
1754
1755 cap_shutter =
1756 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
1757 * cap_bandfilt;
1758 if (!cap_shutter)
1759 return -EINVAL;
1760
1761 cap_gain16 = cap_gain16_shutter / cap_shutter;
1762 }
1763 }
1764
1765
1766 ret = ov5640_set_gain(sensor, cap_gain16);
1767 if (ret)
1768 return ret;
1769
1770
1771 if (cap_shutter > (cap_vts - 4)) {
1772 cap_vts = cap_shutter + 4;
1773 ret = ov5640_set_vts(sensor, cap_vts);
1774 if (ret < 0)
1775 return ret;
1776 }
1777
1778
1779 return ov5640_set_exposure(sensor, cap_shutter);
1780 }
1781
1782
1783
1784
1785
1786 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
1787 const struct ov5640_mode_info *mode)
1788 {
1789 if (!mode->reg_data)
1790 return -EINVAL;
1791
1792
1793 return ov5640_load_regs(sensor, mode);
1794 }
1795
1796 static int ov5640_set_mode(struct ov5640_dev *sensor)
1797 {
1798 const struct ov5640_mode_info *mode = sensor->current_mode;
1799 const struct ov5640_mode_info *orig_mode = sensor->last_mode;
1800 enum ov5640_downsize_mode dn_mode, orig_dn_mode;
1801 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
1802 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
1803 unsigned long rate;
1804 int ret;
1805
1806 dn_mode = mode->dn_mode;
1807 orig_dn_mode = orig_mode->dn_mode;
1808
1809
1810 if (auto_gain) {
1811 ret = ov5640_set_autogain(sensor, false);
1812 if (ret)
1813 return ret;
1814 }
1815
1816 if (auto_exp) {
1817 ret = ov5640_set_autoexposure(sensor, false);
1818 if (ret)
1819 goto restore_auto_gain;
1820 }
1821
1822
1823
1824
1825
1826 rate = mode->vtot * mode->htot * 16;
1827 rate *= ov5640_framerates[sensor->current_fr];
1828 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
1829 rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes;
1830 ret = ov5640_set_mipi_pclk(sensor, rate);
1831 } else {
1832 rate = rate / sensor->ep.bus.parallel.bus_width;
1833 ret = ov5640_set_dvp_pclk(sensor, rate);
1834 }
1835
1836 if (ret < 0)
1837 return 0;
1838
1839 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
1840 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
1841
1842
1843
1844
1845 ret = ov5640_set_mode_exposure_calc(sensor, mode);
1846 } else {
1847
1848
1849
1850
1851 ret = ov5640_set_mode_direct(sensor, mode);
1852 }
1853 if (ret < 0)
1854 goto restore_auto_exp_gain;
1855
1856
1857 if (auto_gain)
1858 ov5640_set_autogain(sensor, true);
1859 if (auto_exp)
1860 ov5640_set_autoexposure(sensor, true);
1861
1862 ret = ov5640_set_binning(sensor, dn_mode != SCALING);
1863 if (ret < 0)
1864 return ret;
1865 ret = ov5640_set_ae_target(sensor, sensor->ae_target);
1866 if (ret < 0)
1867 return ret;
1868 ret = ov5640_get_light_freq(sensor);
1869 if (ret < 0)
1870 return ret;
1871 ret = ov5640_set_bandingfilter(sensor);
1872 if (ret < 0)
1873 return ret;
1874 ret = ov5640_set_virtual_channel(sensor);
1875 if (ret < 0)
1876 return ret;
1877
1878 sensor->pending_mode_change = false;
1879 sensor->last_mode = mode;
1880
1881 return 0;
1882
1883 restore_auto_exp_gain:
1884 if (auto_exp)
1885 ov5640_set_autoexposure(sensor, true);
1886 restore_auto_gain:
1887 if (auto_gain)
1888 ov5640_set_autogain(sensor, true);
1889
1890 return ret;
1891 }
1892
1893 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
1894 struct v4l2_mbus_framefmt *format);
1895
1896
1897 static int ov5640_restore_mode(struct ov5640_dev *sensor)
1898 {
1899 int ret;
1900
1901
1902 ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
1903 if (ret < 0)
1904 return ret;
1905 sensor->last_mode = &ov5640_mode_init_data;
1906
1907 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
1908 (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
1909 ilog2(OV5640_SCLK_ROOT_DIV));
1910 if (ret)
1911 return ret;
1912
1913
1914 ret = ov5640_set_mode(sensor);
1915 if (ret < 0)
1916 return ret;
1917
1918 return ov5640_set_framefmt(sensor, &sensor->fmt);
1919 }
1920
1921 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
1922 {
1923 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
1924 }
1925
1926 static void ov5640_reset(struct ov5640_dev *sensor)
1927 {
1928 if (!sensor->reset_gpio)
1929 return;
1930
1931 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1932
1933
1934 ov5640_power(sensor, false);
1935 usleep_range(5000, 10000);
1936 ov5640_power(sensor, true);
1937 usleep_range(5000, 10000);
1938
1939 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
1940 usleep_range(1000, 2000);
1941
1942 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1943 usleep_range(20000, 25000);
1944 }
1945
1946 static int ov5640_set_power_on(struct ov5640_dev *sensor)
1947 {
1948 struct i2c_client *client = sensor->i2c_client;
1949 int ret;
1950
1951 ret = clk_prepare_enable(sensor->xclk);
1952 if (ret) {
1953 dev_err(&client->dev, "%s: failed to enable clock\n",
1954 __func__);
1955 return ret;
1956 }
1957
1958 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
1959 sensor->supplies);
1960 if (ret) {
1961 dev_err(&client->dev, "%s: failed to enable regulators\n",
1962 __func__);
1963 goto xclk_off;
1964 }
1965
1966 ov5640_reset(sensor);
1967 ov5640_power(sensor, true);
1968
1969 ret = ov5640_init_slave_id(sensor);
1970 if (ret)
1971 goto power_off;
1972
1973 return 0;
1974
1975 power_off:
1976 ov5640_power(sensor, false);
1977 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1978 xclk_off:
1979 clk_disable_unprepare(sensor->xclk);
1980 return ret;
1981 }
1982
1983 static void ov5640_set_power_off(struct ov5640_dev *sensor)
1984 {
1985 ov5640_power(sensor, false);
1986 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1987 clk_disable_unprepare(sensor->xclk);
1988 }
1989
1990 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
1991 {
1992 int ret = 0;
1993
1994 if (on) {
1995 ret = ov5640_set_power_on(sensor);
1996 if (ret)
1997 return ret;
1998
1999 ret = ov5640_restore_mode(sensor);
2000 if (ret)
2001 goto power_off;
2002
2003
2004 if (sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY)
2005 return 0;
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017 ret = ov5640_write_reg(sensor,
2018 OV5640_REG_IO_MIPI_CTRL00, 0x40);
2019 if (ret)
2020 goto power_off;
2021
2022
2023
2024
2025
2026
2027
2028
2029 ret = ov5640_write_reg(sensor,
2030 OV5640_REG_MIPI_CTRL00, 0x24);
2031 if (ret)
2032 goto power_off;
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042 ret = ov5640_write_reg(sensor,
2043 OV5640_REG_PAD_OUTPUT00, 0x70);
2044 if (ret)
2045 goto power_off;
2046
2047
2048 usleep_range(500, 1000);
2049
2050 } else {
2051 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
2052
2053 ov5640_write_reg(sensor,
2054 OV5640_REG_IO_MIPI_CTRL00, 0x58);
2055 ov5640_write_reg(sensor,
2056 OV5640_REG_MIPI_CTRL00, 0x04);
2057 ov5640_write_reg(sensor,
2058 OV5640_REG_PAD_OUTPUT00, 0x00);
2059 }
2060
2061 ov5640_set_power_off(sensor);
2062 }
2063
2064 return 0;
2065
2066 power_off:
2067 ov5640_set_power_off(sensor);
2068 return ret;
2069 }
2070
2071
2072
2073 static int ov5640_s_power(struct v4l2_subdev *sd, int on)
2074 {
2075 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2076 int ret = 0;
2077
2078 mutex_lock(&sensor->lock);
2079
2080
2081
2082
2083
2084 if (sensor->power_count == !on) {
2085 ret = ov5640_set_power(sensor, !!on);
2086 if (ret)
2087 goto out;
2088 }
2089
2090
2091 sensor->power_count += on ? 1 : -1;
2092 WARN_ON(sensor->power_count < 0);
2093 out:
2094 mutex_unlock(&sensor->lock);
2095
2096 if (on && !ret && sensor->power_count == 1) {
2097
2098 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
2099 }
2100
2101 return ret;
2102 }
2103
2104 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2105 struct v4l2_fract *fi,
2106 u32 width, u32 height)
2107 {
2108 const struct ov5640_mode_info *mode;
2109 enum ov5640_frame_rate rate = OV5640_15_FPS;
2110 int minfps, maxfps, best_fps, fps;
2111 int i;
2112
2113 minfps = ov5640_framerates[OV5640_15_FPS];
2114 maxfps = ov5640_framerates[OV5640_60_FPS];
2115
2116 if (fi->numerator == 0) {
2117 fi->denominator = maxfps;
2118 fi->numerator = 1;
2119 rate = OV5640_60_FPS;
2120 goto find_mode;
2121 }
2122
2123 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2124 minfps, maxfps);
2125
2126 best_fps = minfps;
2127 for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2128 int curr_fps = ov5640_framerates[i];
2129
2130 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2131 best_fps = curr_fps;
2132 rate = i;
2133 }
2134 }
2135
2136 fi->numerator = 1;
2137 fi->denominator = best_fps;
2138
2139 find_mode:
2140 mode = ov5640_find_mode(sensor, rate, width, height, false);
2141 return mode ? rate : -EINVAL;
2142 }
2143
2144 static int ov5640_get_fmt(struct v4l2_subdev *sd,
2145 struct v4l2_subdev_pad_config *cfg,
2146 struct v4l2_subdev_format *format)
2147 {
2148 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2149 struct v4l2_mbus_framefmt *fmt;
2150
2151 if (format->pad != 0)
2152 return -EINVAL;
2153
2154 mutex_lock(&sensor->lock);
2155
2156 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2157 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
2158 format->pad);
2159 else
2160 fmt = &sensor->fmt;
2161
2162 format->format = *fmt;
2163
2164 mutex_unlock(&sensor->lock);
2165
2166 return 0;
2167 }
2168
2169 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2170 struct v4l2_mbus_framefmt *fmt,
2171 enum ov5640_frame_rate fr,
2172 const struct ov5640_mode_info **new_mode)
2173 {
2174 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2175 const struct ov5640_mode_info *mode;
2176 int i;
2177
2178 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
2179 if (!mode)
2180 return -EINVAL;
2181 fmt->width = mode->hact;
2182 fmt->height = mode->vact;
2183
2184 if (new_mode)
2185 *new_mode = mode;
2186
2187 for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
2188 if (ov5640_formats[i].code == fmt->code)
2189 break;
2190 if (i >= ARRAY_SIZE(ov5640_formats))
2191 i = 0;
2192
2193 fmt->code = ov5640_formats[i].code;
2194 fmt->colorspace = ov5640_formats[i].colorspace;
2195 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2196 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2197 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2198
2199 return 0;
2200 }
2201
2202 static int ov5640_set_fmt(struct v4l2_subdev *sd,
2203 struct v4l2_subdev_pad_config *cfg,
2204 struct v4l2_subdev_format *format)
2205 {
2206 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2207 const struct ov5640_mode_info *new_mode;
2208 struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
2209 struct v4l2_mbus_framefmt *fmt;
2210 int ret;
2211
2212 if (format->pad != 0)
2213 return -EINVAL;
2214
2215 mutex_lock(&sensor->lock);
2216
2217 if (sensor->streaming) {
2218 ret = -EBUSY;
2219 goto out;
2220 }
2221
2222 ret = ov5640_try_fmt_internal(sd, mbus_fmt,
2223 sensor->current_fr, &new_mode);
2224 if (ret)
2225 goto out;
2226
2227 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2228 fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
2229 else
2230 fmt = &sensor->fmt;
2231
2232 *fmt = *mbus_fmt;
2233
2234 if (new_mode != sensor->current_mode) {
2235 sensor->current_mode = new_mode;
2236 sensor->pending_mode_change = true;
2237 }
2238 if (mbus_fmt->code != sensor->fmt.code)
2239 sensor->pending_fmt_change = true;
2240
2241 out:
2242 mutex_unlock(&sensor->lock);
2243 return ret;
2244 }
2245
2246 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2247 struct v4l2_mbus_framefmt *format)
2248 {
2249 int ret = 0;
2250 bool is_jpeg = false;
2251 u8 fmt, mux;
2252
2253 switch (format->code) {
2254 case MEDIA_BUS_FMT_UYVY8_2X8:
2255
2256 fmt = 0x3f;
2257 mux = OV5640_FMT_MUX_YUV422;
2258 break;
2259 case MEDIA_BUS_FMT_YUYV8_2X8:
2260
2261 fmt = 0x30;
2262 mux = OV5640_FMT_MUX_YUV422;
2263 break;
2264 case MEDIA_BUS_FMT_RGB565_2X8_LE:
2265
2266 fmt = 0x6F;
2267 mux = OV5640_FMT_MUX_RGB;
2268 break;
2269 case MEDIA_BUS_FMT_RGB565_2X8_BE:
2270
2271 fmt = 0x61;
2272 mux = OV5640_FMT_MUX_RGB;
2273 break;
2274 case MEDIA_BUS_FMT_JPEG_1X8:
2275
2276 fmt = 0x30;
2277 mux = OV5640_FMT_MUX_YUV422;
2278 is_jpeg = true;
2279 break;
2280 case MEDIA_BUS_FMT_SBGGR8_1X8:
2281
2282 fmt = 0x00;
2283 mux = OV5640_FMT_MUX_RAW_DPC;
2284 break;
2285 case MEDIA_BUS_FMT_SGBRG8_1X8:
2286
2287 fmt = 0x01;
2288 mux = OV5640_FMT_MUX_RAW_DPC;
2289 break;
2290 case MEDIA_BUS_FMT_SGRBG8_1X8:
2291
2292 fmt = 0x02;
2293 mux = OV5640_FMT_MUX_RAW_DPC;
2294 break;
2295 case MEDIA_BUS_FMT_SRGGB8_1X8:
2296
2297 fmt = 0x03;
2298 mux = OV5640_FMT_MUX_RAW_DPC;
2299 break;
2300 default:
2301 return -EINVAL;
2302 }
2303
2304
2305 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt);
2306 if (ret)
2307 return ret;
2308
2309
2310 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux);
2311 if (ret)
2312 return ret;
2313
2314
2315
2316
2317
2318 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2319 BIT(5), is_jpeg ? BIT(5) : 0);
2320 if (ret)
2321 return ret;
2322
2323
2324
2325
2326
2327
2328
2329 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2330 BIT(4) | BIT(3) | BIT(2),
2331 is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2332 if (ret)
2333 return ret;
2334
2335
2336
2337
2338
2339
2340 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2341 BIT(5) | BIT(3),
2342 is_jpeg ? (BIT(5) | BIT(3)) : 0);
2343 }
2344
2345
2346
2347
2348
2349 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
2350 {
2351 int ret;
2352
2353 if (value) {
2354 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2355 BIT(0), BIT(0));
2356 if (ret)
2357 return ret;
2358 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
2359 } else {
2360 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
2361 }
2362
2363 return ret;
2364 }
2365
2366 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
2367 {
2368 int ret;
2369
2370 if (value) {
2371 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2372 BIT(2), BIT(2));
2373 if (ret)
2374 return ret;
2375 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
2376 value & 0xff);
2377 } else {
2378 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
2379 }
2380
2381 return ret;
2382 }
2383
2384 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
2385 {
2386 int ret;
2387
2388 if (value) {
2389 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2390 BIT(1), BIT(1));
2391 if (ret)
2392 return ret;
2393 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
2394 value & 0xff);
2395 if (ret)
2396 return ret;
2397 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
2398 value & 0xff);
2399 } else {
2400 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
2401 }
2402
2403 return ret;
2404 }
2405
2406 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
2407 {
2408 int ret;
2409
2410 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
2411 BIT(0), awb ? 0 : 1);
2412 if (ret)
2413 return ret;
2414
2415 if (!awb) {
2416 u16 red = (u16)sensor->ctrls.red_balance->val;
2417 u16 blue = (u16)sensor->ctrls.blue_balance->val;
2418
2419 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
2420 if (ret)
2421 return ret;
2422 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
2423 }
2424
2425 return ret;
2426 }
2427
2428 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
2429 enum v4l2_exposure_auto_type auto_exposure)
2430 {
2431 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2432 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
2433 int ret = 0;
2434
2435 if (ctrls->auto_exp->is_new) {
2436 ret = ov5640_set_autoexposure(sensor, auto_exp);
2437 if (ret)
2438 return ret;
2439 }
2440
2441 if (!auto_exp && ctrls->exposure->is_new) {
2442 u16 max_exp;
2443
2444 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
2445 &max_exp);
2446 if (ret)
2447 return ret;
2448 ret = ov5640_get_vts(sensor);
2449 if (ret < 0)
2450 return ret;
2451 max_exp += ret;
2452 ret = 0;
2453
2454 if (ctrls->exposure->val < max_exp)
2455 ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
2456 }
2457
2458 return ret;
2459 }
2460
2461 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
2462 {
2463 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2464 int ret = 0;
2465
2466 if (ctrls->auto_gain->is_new) {
2467 ret = ov5640_set_autogain(sensor, auto_gain);
2468 if (ret)
2469 return ret;
2470 }
2471
2472 if (!auto_gain && ctrls->gain->is_new)
2473 ret = ov5640_set_gain(sensor, ctrls->gain->val);
2474
2475 return ret;
2476 }
2477
2478 static const char * const test_pattern_menu[] = {
2479 "Disabled",
2480 "Color bars",
2481 "Color bars w/ rolling bar",
2482 "Color squares",
2483 "Color squares w/ rolling bar",
2484 };
2485
2486 #define OV5640_TEST_ENABLE BIT(7)
2487 #define OV5640_TEST_ROLLING BIT(6)
2488 #define OV5640_TEST_TRANSPARENT BIT(5)
2489 #define OV5640_TEST_SQUARE_BW BIT(4)
2490 #define OV5640_TEST_BAR_STANDARD (0 << 2)
2491 #define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2)
2492 #define OV5640_TEST_BAR_HOR_CHANGE (2 << 2)
2493 #define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2)
2494 #define OV5640_TEST_BAR (0 << 0)
2495 #define OV5640_TEST_RANDOM (1 << 0)
2496 #define OV5640_TEST_SQUARE (2 << 0)
2497 #define OV5640_TEST_BLACK (3 << 0)
2498
2499 static const u8 test_pattern_val[] = {
2500 0,
2501 OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
2502 OV5640_TEST_BAR,
2503 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
2504 OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
2505 OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
2506 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
2507 };
2508
2509 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
2510 {
2511 return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
2512 test_pattern_val[value]);
2513 }
2514
2515 static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
2516 {
2517 int ret;
2518
2519 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
2520 (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
2521 0 : BIT(7));
2522 if (ret)
2523 return ret;
2524
2525 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
2526 (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
2527 BIT(2) : 0);
2528 }
2529
2530 static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
2531 {
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2547 BIT(2) | BIT(1),
2548 (!(value ^ sensor->upside_down)) ?
2549 (BIT(2) | BIT(1)) : 0);
2550 }
2551
2552 static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
2553 {
2554
2555
2556
2557
2558
2559
2560
2561 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2562 BIT(2) | BIT(1),
2563 (value ^ sensor->upside_down) ?
2564 (BIT(2) | BIT(1)) : 0);
2565 }
2566
2567 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
2568 {
2569 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2570 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2571 int val;
2572
2573
2574
2575 switch (ctrl->id) {
2576 case V4L2_CID_AUTOGAIN:
2577 val = ov5640_get_gain(sensor);
2578 if (val < 0)
2579 return val;
2580 sensor->ctrls.gain->val = val;
2581 break;
2582 case V4L2_CID_EXPOSURE_AUTO:
2583 val = ov5640_get_exposure(sensor);
2584 if (val < 0)
2585 return val;
2586 sensor->ctrls.exposure->val = val;
2587 break;
2588 }
2589
2590 return 0;
2591 }
2592
2593 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
2594 {
2595 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2596 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2597 int ret;
2598
2599
2600
2601
2602
2603
2604
2605
2606 if (sensor->power_count == 0)
2607 return 0;
2608
2609 switch (ctrl->id) {
2610 case V4L2_CID_AUTOGAIN:
2611 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
2612 break;
2613 case V4L2_CID_EXPOSURE_AUTO:
2614 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
2615 break;
2616 case V4L2_CID_AUTO_WHITE_BALANCE:
2617 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
2618 break;
2619 case V4L2_CID_HUE:
2620 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
2621 break;
2622 case V4L2_CID_CONTRAST:
2623 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
2624 break;
2625 case V4L2_CID_SATURATION:
2626 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
2627 break;
2628 case V4L2_CID_TEST_PATTERN:
2629 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
2630 break;
2631 case V4L2_CID_POWER_LINE_FREQUENCY:
2632 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
2633 break;
2634 case V4L2_CID_HFLIP:
2635 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
2636 break;
2637 case V4L2_CID_VFLIP:
2638 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
2639 break;
2640 default:
2641 ret = -EINVAL;
2642 break;
2643 }
2644
2645 return ret;
2646 }
2647
2648 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
2649 .g_volatile_ctrl = ov5640_g_volatile_ctrl,
2650 .s_ctrl = ov5640_s_ctrl,
2651 };
2652
2653 static int ov5640_init_controls(struct ov5640_dev *sensor)
2654 {
2655 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
2656 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2657 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2658 int ret;
2659
2660 v4l2_ctrl_handler_init(hdl, 32);
2661
2662
2663 hdl->lock = &sensor->lock;
2664
2665
2666 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
2667 V4L2_CID_AUTO_WHITE_BALANCE,
2668 0, 1, 1, 1);
2669 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
2670 0, 4095, 1, 0);
2671 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
2672 0, 4095, 1, 0);
2673
2674 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
2675 V4L2_CID_EXPOSURE_AUTO,
2676 V4L2_EXPOSURE_MANUAL, 0,
2677 V4L2_EXPOSURE_AUTO);
2678 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
2679 0, 65535, 1, 0);
2680
2681 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
2682 0, 1, 1, 1);
2683 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
2684 0, 1023, 1, 0);
2685
2686 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
2687 0, 255, 1, 64);
2688 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
2689 0, 359, 1, 0);
2690 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
2691 0, 255, 1, 0);
2692 ctrls->test_pattern =
2693 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
2694 ARRAY_SIZE(test_pattern_menu) - 1,
2695 0, 0, test_pattern_menu);
2696 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
2697 0, 1, 1, 0);
2698 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
2699 0, 1, 1, 0);
2700
2701 ctrls->light_freq =
2702 v4l2_ctrl_new_std_menu(hdl, ops,
2703 V4L2_CID_POWER_LINE_FREQUENCY,
2704 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
2705 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
2706
2707 if (hdl->error) {
2708 ret = hdl->error;
2709 goto free_ctrls;
2710 }
2711
2712 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
2713 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
2714
2715 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
2716 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
2717 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
2718
2719 sensor->sd.ctrl_handler = hdl;
2720 return 0;
2721
2722 free_ctrls:
2723 v4l2_ctrl_handler_free(hdl);
2724 return ret;
2725 }
2726
2727 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
2728 struct v4l2_subdev_pad_config *cfg,
2729 struct v4l2_subdev_frame_size_enum *fse)
2730 {
2731 if (fse->pad != 0)
2732 return -EINVAL;
2733 if (fse->index >= OV5640_NUM_MODES)
2734 return -EINVAL;
2735
2736 fse->min_width =
2737 ov5640_mode_data[fse->index].hact;
2738 fse->max_width = fse->min_width;
2739 fse->min_height =
2740 ov5640_mode_data[fse->index].vact;
2741 fse->max_height = fse->min_height;
2742
2743 return 0;
2744 }
2745
2746 static int ov5640_enum_frame_interval(
2747 struct v4l2_subdev *sd,
2748 struct v4l2_subdev_pad_config *cfg,
2749 struct v4l2_subdev_frame_interval_enum *fie)
2750 {
2751 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2752 struct v4l2_fract tpf;
2753 int ret;
2754
2755 if (fie->pad != 0)
2756 return -EINVAL;
2757 if (fie->index >= OV5640_NUM_FRAMERATES)
2758 return -EINVAL;
2759
2760 tpf.numerator = 1;
2761 tpf.denominator = ov5640_framerates[fie->index];
2762
2763 ret = ov5640_try_frame_interval(sensor, &tpf,
2764 fie->width, fie->height);
2765 if (ret < 0)
2766 return -EINVAL;
2767
2768 fie->interval = tpf;
2769 return 0;
2770 }
2771
2772 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
2773 struct v4l2_subdev_frame_interval *fi)
2774 {
2775 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2776
2777 mutex_lock(&sensor->lock);
2778 fi->interval = sensor->frame_interval;
2779 mutex_unlock(&sensor->lock);
2780
2781 return 0;
2782 }
2783
2784 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
2785 struct v4l2_subdev_frame_interval *fi)
2786 {
2787 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2788 const struct ov5640_mode_info *mode;
2789 int frame_rate, ret = 0;
2790
2791 if (fi->pad != 0)
2792 return -EINVAL;
2793
2794 mutex_lock(&sensor->lock);
2795
2796 if (sensor->streaming) {
2797 ret = -EBUSY;
2798 goto out;
2799 }
2800
2801 mode = sensor->current_mode;
2802
2803 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2804 mode->hact, mode->vact);
2805 if (frame_rate < 0) {
2806
2807 fi->interval = sensor->frame_interval;
2808 goto out;
2809 }
2810
2811 mode = ov5640_find_mode(sensor, frame_rate, mode->hact,
2812 mode->vact, true);
2813 if (!mode) {
2814 ret = -EINVAL;
2815 goto out;
2816 }
2817
2818 if (mode != sensor->current_mode ||
2819 frame_rate != sensor->current_fr) {
2820 sensor->current_fr = frame_rate;
2821 sensor->frame_interval = fi->interval;
2822 sensor->current_mode = mode;
2823 sensor->pending_mode_change = true;
2824 }
2825 out:
2826 mutex_unlock(&sensor->lock);
2827 return ret;
2828 }
2829
2830 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
2831 struct v4l2_subdev_pad_config *cfg,
2832 struct v4l2_subdev_mbus_code_enum *code)
2833 {
2834 if (code->pad != 0)
2835 return -EINVAL;
2836 if (code->index >= ARRAY_SIZE(ov5640_formats))
2837 return -EINVAL;
2838
2839 code->code = ov5640_formats[code->index].code;
2840 return 0;
2841 }
2842
2843 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
2844 {
2845 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2846 int ret = 0;
2847
2848 mutex_lock(&sensor->lock);
2849
2850 if (sensor->streaming == !enable) {
2851 if (enable && sensor->pending_mode_change) {
2852 ret = ov5640_set_mode(sensor);
2853 if (ret)
2854 goto out;
2855 }
2856
2857 if (enable && sensor->pending_fmt_change) {
2858 ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2859 if (ret)
2860 goto out;
2861 sensor->pending_fmt_change = false;
2862 }
2863
2864 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2865 ret = ov5640_set_stream_mipi(sensor, enable);
2866 else
2867 ret = ov5640_set_stream_dvp(sensor, enable);
2868
2869 if (!ret)
2870 sensor->streaming = enable;
2871 }
2872 out:
2873 mutex_unlock(&sensor->lock);
2874 return ret;
2875 }
2876
2877 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
2878 .s_power = ov5640_s_power,
2879 .log_status = v4l2_ctrl_subdev_log_status,
2880 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2881 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2882 };
2883
2884 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
2885 .g_frame_interval = ov5640_g_frame_interval,
2886 .s_frame_interval = ov5640_s_frame_interval,
2887 .s_stream = ov5640_s_stream,
2888 };
2889
2890 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
2891 .enum_mbus_code = ov5640_enum_mbus_code,
2892 .get_fmt = ov5640_get_fmt,
2893 .set_fmt = ov5640_set_fmt,
2894 .enum_frame_size = ov5640_enum_frame_size,
2895 .enum_frame_interval = ov5640_enum_frame_interval,
2896 };
2897
2898 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
2899 .core = &ov5640_core_ops,
2900 .video = &ov5640_video_ops,
2901 .pad = &ov5640_pad_ops,
2902 };
2903
2904 static int ov5640_get_regulators(struct ov5640_dev *sensor)
2905 {
2906 int i;
2907
2908 for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
2909 sensor->supplies[i].supply = ov5640_supply_name[i];
2910
2911 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
2912 OV5640_NUM_SUPPLIES,
2913 sensor->supplies);
2914 }
2915
2916 static int ov5640_check_chip_id(struct ov5640_dev *sensor)
2917 {
2918 struct i2c_client *client = sensor->i2c_client;
2919 int ret = 0;
2920 u16 chip_id;
2921
2922 ret = ov5640_set_power_on(sensor);
2923 if (ret)
2924 return ret;
2925
2926 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
2927 if (ret) {
2928 dev_err(&client->dev, "%s: failed to read chip identifier\n",
2929 __func__);
2930 goto power_off;
2931 }
2932
2933 if (chip_id != 0x5640) {
2934 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
2935 __func__, chip_id);
2936 ret = -ENXIO;
2937 }
2938
2939 power_off:
2940 ov5640_set_power_off(sensor);
2941 return ret;
2942 }
2943
2944 static int ov5640_probe(struct i2c_client *client)
2945 {
2946 struct device *dev = &client->dev;
2947 struct fwnode_handle *endpoint;
2948 struct ov5640_dev *sensor;
2949 struct v4l2_mbus_framefmt *fmt;
2950 u32 rotation;
2951 int ret;
2952
2953 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
2954 if (!sensor)
2955 return -ENOMEM;
2956
2957 sensor->i2c_client = client;
2958
2959
2960
2961
2962
2963 fmt = &sensor->fmt;
2964 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
2965 fmt->colorspace = V4L2_COLORSPACE_SRGB;
2966 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2967 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2968 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2969 fmt->width = 640;
2970 fmt->height = 480;
2971 fmt->field = V4L2_FIELD_NONE;
2972 sensor->frame_interval.numerator = 1;
2973 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
2974 sensor->current_fr = OV5640_30_FPS;
2975 sensor->current_mode =
2976 &ov5640_mode_data[OV5640_MODE_VGA_640_480];
2977 sensor->last_mode = sensor->current_mode;
2978
2979 sensor->ae_target = 52;
2980
2981
2982 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
2983 &rotation);
2984 if (!ret) {
2985 switch (rotation) {
2986 case 180:
2987 sensor->upside_down = true;
2988
2989 case 0:
2990 break;
2991 default:
2992 dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
2993 rotation);
2994 }
2995 }
2996
2997 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
2998 NULL);
2999 if (!endpoint) {
3000 dev_err(dev, "endpoint node not found\n");
3001 return -EINVAL;
3002 }
3003
3004 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
3005 fwnode_handle_put(endpoint);
3006 if (ret) {
3007 dev_err(dev, "Could not parse endpoint\n");
3008 return ret;
3009 }
3010
3011
3012 sensor->xclk = devm_clk_get(dev, "xclk");
3013 if (IS_ERR(sensor->xclk)) {
3014 dev_err(dev, "failed to get xclk\n");
3015 return PTR_ERR(sensor->xclk);
3016 }
3017
3018 sensor->xclk_freq = clk_get_rate(sensor->xclk);
3019 if (sensor->xclk_freq < OV5640_XCLK_MIN ||
3020 sensor->xclk_freq > OV5640_XCLK_MAX) {
3021 dev_err(dev, "xclk frequency out of range: %d Hz\n",
3022 sensor->xclk_freq);
3023 return -EINVAL;
3024 }
3025
3026
3027 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
3028 GPIOD_OUT_HIGH);
3029 if (IS_ERR(sensor->pwdn_gpio))
3030 return PTR_ERR(sensor->pwdn_gpio);
3031
3032
3033 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
3034 GPIOD_OUT_HIGH);
3035 if (IS_ERR(sensor->reset_gpio))
3036 return PTR_ERR(sensor->reset_gpio);
3037
3038 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
3039
3040 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3041 V4L2_SUBDEV_FL_HAS_EVENTS;
3042 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
3043 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3044 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3045 if (ret)
3046 return ret;
3047
3048 ret = ov5640_get_regulators(sensor);
3049 if (ret)
3050 return ret;
3051
3052 mutex_init(&sensor->lock);
3053
3054 ret = ov5640_check_chip_id(sensor);
3055 if (ret)
3056 goto entity_cleanup;
3057
3058 ret = ov5640_init_controls(sensor);
3059 if (ret)
3060 goto entity_cleanup;
3061
3062 ret = v4l2_async_register_subdev_sensor_common(&sensor->sd);
3063 if (ret)
3064 goto free_ctrls;
3065
3066 return 0;
3067
3068 free_ctrls:
3069 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3070 entity_cleanup:
3071 mutex_destroy(&sensor->lock);
3072 media_entity_cleanup(&sensor->sd.entity);
3073 return ret;
3074 }
3075
3076 static int ov5640_remove(struct i2c_client *client)
3077 {
3078 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3079 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3080
3081 v4l2_async_unregister_subdev(&sensor->sd);
3082 mutex_destroy(&sensor->lock);
3083 media_entity_cleanup(&sensor->sd.entity);
3084 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3085
3086 return 0;
3087 }
3088
3089 static const struct i2c_device_id ov5640_id[] = {
3090 {"ov5640", 0},
3091 {},
3092 };
3093 MODULE_DEVICE_TABLE(i2c, ov5640_id);
3094
3095 static const struct of_device_id ov5640_dt_ids[] = {
3096 { .compatible = "ovti,ov5640" },
3097 { }
3098 };
3099 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
3100
3101 static struct i2c_driver ov5640_i2c_driver = {
3102 .driver = {
3103 .name = "ov5640",
3104 .of_match_table = ov5640_dt_ids,
3105 },
3106 .id_table = ov5640_id,
3107 .probe_new = ov5640_probe,
3108 .remove = ov5640_remove,
3109 };
3110
3111 module_i2c_driver(ov5640_i2c_driver);
3112
3113 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
3114 MODULE_LICENSE("GPL");