]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/i2c/ov5647.c
UBUNTU: Ubuntu-5.15.0-39.42
[mirror_ubuntu-jammy-kernel.git] / drivers / media / i2c / ov5647.c
CommitLineData
24169a5a 1// SPDX-License-Identifier: GPL-2.0
3c2472a3
RO
2/*
3 * A V4L2 driver for OmniVision OV5647 cameras.
4 *
5 * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
6 * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com>
7 *
8 * Based on Omnivision OV7670 Camera Driver
9 * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
10 *
11 * Copyright (C) 2016, Synopsys, Inc.
3c2472a3
RO
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
b050791d 16#include <linux/gpio/consumer.h>
3c2472a3
RO
17#include <linux/i2c.h>
18#include <linux/init.h>
19#include <linux/io.h>
20#include <linux/module.h>
859969b3 21#include <linux/of_graph.h>
089b7c70 22#include <linux/pm_runtime.h>
3c2472a3
RO
23#include <linux/slab.h>
24#include <linux/videodev2.h>
4974c2f1 25#include <media/v4l2-ctrls.h>
3c2472a3 26#include <media/v4l2-device.h>
dc337308 27#include <media/v4l2-event.h>
859969b3 28#include <media/v4l2-fwnode.h>
3c2472a3
RO
29#include <media/v4l2-image-sizes.h>
30#include <media/v4l2-mediabus.h>
3c2472a3 31
b050791d
DS
32/*
33 * From the datasheet, "20ms after PWDN goes low or 20ms after RESETB goes
34 * high if reset is inserted after PWDN goes high, host can access sensor's
35 * SCCB to initialize sensor."
36 */
37#define PWDN_ACTIVE_DELAY_MS 20
38
cef66734 39#define MIPI_CTRL00_CLOCK_LANE_GATE BIT(5)
dea4fcfe 40#define MIPI_CTRL00_LINE_SYNC_ENABLE BIT(4)
cef66734
JC
41#define MIPI_CTRL00_BUS_IDLE BIT(2)
42#define MIPI_CTRL00_CLOCK_LANE_DISABLE BIT(0)
43
44#define OV5647_SW_STANDBY 0x0100
45#define OV5647_SW_RESET 0x0103
c9a05cec
JM
46#define OV5647_REG_CHIPID_H 0x300a
47#define OV5647_REG_CHIPID_L 0x300b
48#define OV5640_REG_PAD_OUT 0x300d
4974c2f1
DP
49#define OV5647_REG_EXP_HI 0x3500
50#define OV5647_REG_EXP_MID 0x3501
51#define OV5647_REG_EXP_LO 0x3502
52#define OV5647_REG_AEC_AGC 0x3503
53#define OV5647_REG_GAIN_HI 0x350a
54#define OV5647_REG_GAIN_LO 0x350b
2512c064
DS
55#define OV5647_REG_VTS_HI 0x380e
56#define OV5647_REG_VTS_LO 0x380f
cef66734
JC
57#define OV5647_REG_FRAME_OFF_NUMBER 0x4202
58#define OV5647_REG_MIPI_CTRL00 0x4800
59#define OV5647_REG_MIPI_CTRL14 0x4814
4974c2f1 60#define OV5647_REG_AWB 0x5001
3c2472a3
RO
61
62#define REG_TERM 0xfffe
63#define VAL_TERM 0xfe
64#define REG_DLY 0xffff
65
14f70a32
DS
66/* OV5647 native and active pixel array size */
67#define OV5647_NATIVE_WIDTH 2624U
68#define OV5647_NATIVE_HEIGHT 1956U
69
70#define OV5647_PIXEL_ARRAY_LEFT 16U
71#define OV5647_PIXEL_ARRAY_TOP 16U
72#define OV5647_PIXEL_ARRAY_WIDTH 2592U
73#define OV5647_PIXEL_ARRAY_HEIGHT 1944U
3c2472a3 74
2512c064
DS
75#define OV5647_VBLANK_MIN 4
76#define OV5647_VTS_MAX 32767
77
646a0249
DS
78#define OV5647_EXPOSURE_MIN 4
79#define OV5647_EXPOSURE_STEP 1
80#define OV5647_EXPOSURE_DEFAULT 1000
81#define OV5647_EXPOSURE_MAX 65535
82
3c2472a3
RO
83struct regval_list {
84 u16 addr;
85 u8 data;
86};
87
d7d6074e
JM
88struct ov5647_mode {
89 struct v4l2_mbus_framefmt format;
14f70a32 90 struct v4l2_rect crop;
911f4516 91 u64 pixel_rate;
c6da1ae4 92 int hts;
2512c064 93 int vts;
d7d6074e
JM
94 const struct regval_list *reg_list;
95 unsigned int num_regs;
96};
97
3c2472a3
RO
98struct ov5647 {
99 struct v4l2_subdev sd;
100 struct media_pad pad;
101 struct mutex lock;
3c2472a3 102 struct clk *xclk;
b050791d 103 struct gpio_desc *pwdn;
dea4fcfe 104 bool clock_ncont;
4974c2f1 105 struct v4l2_ctrl_handler ctrls;
d7d6074e 106 const struct ov5647_mode *mode;
911f4516 107 struct v4l2_ctrl *pixel_rate;
c6da1ae4 108 struct v4l2_ctrl *hblank;
2512c064 109 struct v4l2_ctrl *vblank;
646a0249 110 struct v4l2_ctrl *exposure;
2f038c97 111 bool streaming;
3c2472a3
RO
112};
113
5bc5ca71 114static inline struct ov5647 *to_sensor(struct v4l2_subdev *sd)
3c2472a3
RO
115{
116 return container_of(sd, struct ov5647, sd);
117}
118
d0744070 119static const struct regval_list sensor_oe_disable_regs[] = {
3c2472a3
RO
120 {0x3000, 0x00},
121 {0x3001, 0x00},
122 {0x3002, 0x00},
123};
124
d0744070 125static const struct regval_list sensor_oe_enable_regs[] = {
3c2472a3
RO
126 {0x3000, 0x0f},
127 {0x3001, 0xff},
128 {0x3002, 0xe4},
129};
130
a8df5af6
JM
131static struct regval_list ov5647_2592x1944_10bpp[] = {
132 {0x0100, 0x00},
133 {0x0103, 0x01},
134 {0x3034, 0x1a},
135 {0x3035, 0x21},
136 {0x3036, 0x69},
137 {0x303c, 0x11},
138 {0x3106, 0xf5},
139 {0x3821, 0x06},
140 {0x3820, 0x00},
141 {0x3827, 0xec},
142 {0x370c, 0x03},
143 {0x3612, 0x5b},
144 {0x3618, 0x04},
145 {0x5000, 0x06},
146 {0x5002, 0x41},
147 {0x5003, 0x08},
148 {0x5a00, 0x08},
149 {0x3000, 0x00},
150 {0x3001, 0x00},
151 {0x3002, 0x00},
152 {0x3016, 0x08},
153 {0x3017, 0xe0},
154 {0x3018, 0x44},
155 {0x301c, 0xf8},
156 {0x301d, 0xf0},
157 {0x3a18, 0x00},
158 {0x3a19, 0xf8},
159 {0x3c01, 0x80},
160 {0x3b07, 0x0c},
161 {0x380c, 0x0b},
162 {0x380d, 0x1c},
163 {0x3814, 0x11},
164 {0x3815, 0x11},
165 {0x3708, 0x64},
166 {0x3709, 0x12},
167 {0x3808, 0x0a},
168 {0x3809, 0x20},
169 {0x380a, 0x07},
170 {0x380b, 0x98},
171 {0x3800, 0x00},
172 {0x3801, 0x00},
173 {0x3802, 0x00},
174 {0x3803, 0x00},
175 {0x3804, 0x0a},
176 {0x3805, 0x3f},
177 {0x3806, 0x07},
178 {0x3807, 0xa3},
179 {0x3811, 0x10},
180 {0x3813, 0x06},
181 {0x3630, 0x2e},
182 {0x3632, 0xe2},
183 {0x3633, 0x23},
184 {0x3634, 0x44},
185 {0x3636, 0x06},
186 {0x3620, 0x64},
187 {0x3621, 0xe0},
188 {0x3600, 0x37},
189 {0x3704, 0xa0},
190 {0x3703, 0x5a},
191 {0x3715, 0x78},
192 {0x3717, 0x01},
193 {0x3731, 0x02},
194 {0x370b, 0x60},
195 {0x3705, 0x1a},
196 {0x3f05, 0x02},
197 {0x3f06, 0x10},
198 {0x3f01, 0x0a},
199 {0x3a08, 0x01},
200 {0x3a09, 0x28},
201 {0x3a0a, 0x00},
202 {0x3a0b, 0xf6},
203 {0x3a0d, 0x08},
204 {0x3a0e, 0x06},
205 {0x3a0f, 0x58},
206 {0x3a10, 0x50},
207 {0x3a1b, 0x58},
208 {0x3a1e, 0x50},
209 {0x3a11, 0x60},
210 {0x3a1f, 0x28},
211 {0x4001, 0x02},
212 {0x4004, 0x04},
213 {0x4000, 0x09},
214 {0x4837, 0x19},
215 {0x4800, 0x24},
216 {0x3503, 0x03},
217 {0x0100, 0x01},
218};
219
220static struct regval_list ov5647_1080p30_10bpp[] = {
221 {0x0100, 0x00},
222 {0x0103, 0x01},
223 {0x3034, 0x1a},
224 {0x3035, 0x21},
225 {0x3036, 0x62},
226 {0x303c, 0x11},
227 {0x3106, 0xf5},
228 {0x3821, 0x06},
229 {0x3820, 0x00},
230 {0x3827, 0xec},
231 {0x370c, 0x03},
232 {0x3612, 0x5b},
233 {0x3618, 0x04},
234 {0x5000, 0x06},
235 {0x5002, 0x41},
236 {0x5003, 0x08},
237 {0x5a00, 0x08},
238 {0x3000, 0x00},
239 {0x3001, 0x00},
240 {0x3002, 0x00},
241 {0x3016, 0x08},
242 {0x3017, 0xe0},
243 {0x3018, 0x44},
244 {0x301c, 0xf8},
245 {0x301d, 0xf0},
246 {0x3a18, 0x00},
247 {0x3a19, 0xf8},
248 {0x3c01, 0x80},
249 {0x3b07, 0x0c},
250 {0x380c, 0x09},
251 {0x380d, 0x70},
252 {0x3814, 0x11},
253 {0x3815, 0x11},
254 {0x3708, 0x64},
255 {0x3709, 0x12},
256 {0x3808, 0x07},
257 {0x3809, 0x80},
258 {0x380a, 0x04},
259 {0x380b, 0x38},
260 {0x3800, 0x01},
261 {0x3801, 0x5c},
262 {0x3802, 0x01},
263 {0x3803, 0xb2},
264 {0x3804, 0x08},
265 {0x3805, 0xe3},
266 {0x3806, 0x05},
267 {0x3807, 0xf1},
268 {0x3811, 0x04},
269 {0x3813, 0x02},
270 {0x3630, 0x2e},
271 {0x3632, 0xe2},
272 {0x3633, 0x23},
273 {0x3634, 0x44},
274 {0x3636, 0x06},
275 {0x3620, 0x64},
276 {0x3621, 0xe0},
277 {0x3600, 0x37},
278 {0x3704, 0xa0},
279 {0x3703, 0x5a},
280 {0x3715, 0x78},
281 {0x3717, 0x01},
282 {0x3731, 0x02},
283 {0x370b, 0x60},
284 {0x3705, 0x1a},
285 {0x3f05, 0x02},
286 {0x3f06, 0x10},
287 {0x3f01, 0x0a},
288 {0x3a08, 0x01},
289 {0x3a09, 0x4b},
290 {0x3a0a, 0x01},
291 {0x3a0b, 0x13},
292 {0x3a0d, 0x04},
293 {0x3a0e, 0x03},
294 {0x3a0f, 0x58},
295 {0x3a10, 0x50},
296 {0x3a1b, 0x58},
297 {0x3a1e, 0x50},
298 {0x3a11, 0x60},
299 {0x3a1f, 0x28},
300 {0x4001, 0x02},
301 {0x4004, 0x04},
302 {0x4000, 0x09},
303 {0x4837, 0x19},
304 {0x4800, 0x34},
305 {0x3503, 0x03},
306 {0x0100, 0x01},
307};
308
309static struct regval_list ov5647_2x2binned_10bpp[] = {
310 {0x0100, 0x00},
311 {0x0103, 0x01},
312 {0x3034, 0x1a},
313 {0x3035, 0x21},
314 {0x3036, 0x62},
315 {0x303c, 0x11},
316 {0x3106, 0xf5},
317 {0x3827, 0xec},
318 {0x370c, 0x03},
319 {0x3612, 0x59},
320 {0x3618, 0x00},
321 {0x5000, 0x06},
322 {0x5002, 0x41},
323 {0x5003, 0x08},
324 {0x5a00, 0x08},
325 {0x3000, 0x00},
326 {0x3001, 0x00},
327 {0x3002, 0x00},
328 {0x3016, 0x08},
329 {0x3017, 0xe0},
330 {0x3018, 0x44},
331 {0x301c, 0xf8},
332 {0x301d, 0xf0},
333 {0x3a18, 0x00},
334 {0x3a19, 0xf8},
335 {0x3c01, 0x80},
336 {0x3b07, 0x0c},
337 {0x3800, 0x00},
338 {0x3801, 0x00},
339 {0x3802, 0x00},
340 {0x3803, 0x00},
341 {0x3804, 0x0a},
342 {0x3805, 0x3f},
343 {0x3806, 0x07},
344 {0x3807, 0xa3},
345 {0x3808, 0x05},
346 {0x3809, 0x10},
347 {0x380a, 0x03},
348 {0x380b, 0xcc},
349 {0x380c, 0x07},
350 {0x380d, 0x68},
351 {0x3811, 0x0c},
352 {0x3813, 0x06},
353 {0x3814, 0x31},
354 {0x3815, 0x31},
355 {0x3630, 0x2e},
356 {0x3632, 0xe2},
357 {0x3633, 0x23},
358 {0x3634, 0x44},
359 {0x3636, 0x06},
360 {0x3620, 0x64},
361 {0x3621, 0xe0},
362 {0x3600, 0x37},
363 {0x3704, 0xa0},
364 {0x3703, 0x5a},
365 {0x3715, 0x78},
366 {0x3717, 0x01},
367 {0x3731, 0x02},
368 {0x370b, 0x60},
369 {0x3705, 0x1a},
370 {0x3f05, 0x02},
371 {0x3f06, 0x10},
372 {0x3f01, 0x0a},
373 {0x3a08, 0x01},
374 {0x3a09, 0x28},
375 {0x3a0a, 0x00},
376 {0x3a0b, 0xf6},
377 {0x3a0d, 0x08},
378 {0x3a0e, 0x06},
379 {0x3a0f, 0x58},
380 {0x3a10, 0x50},
381 {0x3a1b, 0x58},
382 {0x3a1e, 0x50},
383 {0x3a11, 0x60},
384 {0x3a1f, 0x28},
385 {0x4001, 0x02},
386 {0x4004, 0x04},
387 {0x4000, 0x09},
388 {0x4837, 0x16},
389 {0x4800, 0x24},
390 {0x3503, 0x03},
391 {0x3820, 0x41},
392 {0x3821, 0x07},
393 {0x350a, 0x00},
394 {0x350b, 0x10},
395 {0x3500, 0x00},
396 {0x3501, 0x1a},
397 {0x3502, 0xf0},
398 {0x3212, 0xa0},
399 {0x0100, 0x01},
400};
401
402static struct regval_list ov5647_640x480_10bpp[] = {
403 {0x0100, 0x00},
404 {0x0103, 0x01},
405 {0x3035, 0x11},
406 {0x3036, 0x46},
407 {0x303c, 0x11},
408 {0x3821, 0x07},
409 {0x3820, 0x41},
410 {0x370c, 0x03},
411 {0x3612, 0x59},
412 {0x3618, 0x00},
413 {0x5000, 0x06},
414 {0x5003, 0x08},
415 {0x5a00, 0x08},
416 {0x3000, 0xff},
417 {0x3001, 0xff},
418 {0x3002, 0xff},
419 {0x301d, 0xf0},
420 {0x3a18, 0x00},
421 {0x3a19, 0xf8},
422 {0x3c01, 0x80},
423 {0x3b07, 0x0c},
424 {0x380c, 0x07},
425 {0x380d, 0x3c},
426 {0x3814, 0x35},
427 {0x3815, 0x35},
428 {0x3708, 0x64},
429 {0x3709, 0x52},
430 {0x3808, 0x02},
431 {0x3809, 0x80},
432 {0x380a, 0x01},
433 {0x380b, 0xe0},
434 {0x3800, 0x00},
435 {0x3801, 0x10},
436 {0x3802, 0x00},
437 {0x3803, 0x00},
438 {0x3804, 0x0a},
439 {0x3805, 0x2f},
440 {0x3806, 0x07},
441 {0x3807, 0x9f},
442 {0x3630, 0x2e},
443 {0x3632, 0xe2},
444 {0x3633, 0x23},
445 {0x3634, 0x44},
446 {0x3620, 0x64},
447 {0x3621, 0xe0},
448 {0x3600, 0x37},
449 {0x3704, 0xa0},
450 {0x3703, 0x5a},
451 {0x3715, 0x78},
452 {0x3717, 0x01},
453 {0x3731, 0x02},
454 {0x370b, 0x60},
455 {0x3705, 0x1a},
456 {0x3f05, 0x02},
457 {0x3f06, 0x10},
458 {0x3f01, 0x0a},
459 {0x3a08, 0x01},
460 {0x3a09, 0x2e},
461 {0x3a0a, 0x00},
462 {0x3a0b, 0xfb},
463 {0x3a0d, 0x02},
464 {0x3a0e, 0x01},
465 {0x3a0f, 0x58},
466 {0x3a10, 0x50},
467 {0x3a1b, 0x58},
468 {0x3a1e, 0x50},
469 {0x3a11, 0x60},
470 {0x3a1f, 0x28},
471 {0x4001, 0x02},
472 {0x4004, 0x02},
473 {0x4000, 0x09},
474 {0x3000, 0x00},
475 {0x3001, 0x00},
476 {0x3002, 0x00},
477 {0x3017, 0xe0},
478 {0x301c, 0xfc},
479 {0x3636, 0x06},
480 {0x3016, 0x08},
481 {0x3827, 0xec},
482 {0x3018, 0x44},
483 {0x3035, 0x21},
484 {0x3106, 0xf5},
485 {0x3034, 0x1a},
486 {0x301c, 0xf8},
487 {0x4800, 0x34},
488 {0x3503, 0x03},
489 {0x0100, 0x01},
490};
491
38c22308 492static const struct ov5647_mode ov5647_modes[] = {
a8df5af6
JM
493 /* 2592x1944 full resolution full FOV 10-bit mode. */
494 {
495 .format = {
496 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
497 .colorspace = V4L2_COLORSPACE_SRGB,
498 .field = V4L2_FIELD_NONE,
499 .width = 2592,
500 .height = 1944
501 },
502 .crop = {
503 .left = OV5647_PIXEL_ARRAY_LEFT,
504 .top = OV5647_PIXEL_ARRAY_TOP,
505 .width = 2592,
506 .height = 1944
507 },
911f4516 508 .pixel_rate = 87500000,
c6da1ae4 509 .hts = 2844,
2512c064 510 .vts = 0x7b0,
a8df5af6
JM
511 .reg_list = ov5647_2592x1944_10bpp,
512 .num_regs = ARRAY_SIZE(ov5647_2592x1944_10bpp)
513 },
514 /* 1080p30 10-bit mode. Full resolution centre-cropped down to 1080p. */
515 {
516 .format = {
517 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
518 .colorspace = V4L2_COLORSPACE_SRGB,
519 .field = V4L2_FIELD_NONE,
520 .width = 1920,
521 .height = 1080
522 },
523 .crop = {
524 .left = 348 + OV5647_PIXEL_ARRAY_LEFT,
525 .top = 434 + OV5647_PIXEL_ARRAY_TOP,
526 .width = 1928,
527 .height = 1080,
528 },
911f4516 529 .pixel_rate = 81666700,
c6da1ae4 530 .hts = 2416,
2512c064 531 .vts = 0x450,
a8df5af6
JM
532 .reg_list = ov5647_1080p30_10bpp,
533 .num_regs = ARRAY_SIZE(ov5647_1080p30_10bpp)
534 },
535 /* 2x2 binned full FOV 10-bit mode. */
536 {
537 .format = {
538 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
539 .colorspace = V4L2_COLORSPACE_SRGB,
540 .field = V4L2_FIELD_NONE,
541 .width = 1296,
542 .height = 972
543 },
544 .crop = {
545 .left = OV5647_PIXEL_ARRAY_LEFT,
546 .top = OV5647_PIXEL_ARRAY_TOP,
547 .width = 2592,
548 .height = 1944,
549 },
911f4516 550 .pixel_rate = 81666700,
c6da1ae4 551 .hts = 1896,
2512c064 552 .vts = 0x59b,
a8df5af6
JM
553 .reg_list = ov5647_2x2binned_10bpp,
554 .num_regs = ARRAY_SIZE(ov5647_2x2binned_10bpp)
555 },
556 /* 10-bit VGA full FOV 60fps. 2x2 binned and subsampled down to VGA. */
557 {
558 .format = {
559 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
560 .colorspace = V4L2_COLORSPACE_SRGB,
561 .field = V4L2_FIELD_NONE,
562 .width = 640,
563 .height = 480
564 },
565 .crop = {
566 .left = 16 + OV5647_PIXEL_ARRAY_LEFT,
567 .top = OV5647_PIXEL_ARRAY_TOP,
568 .width = 2560,
569 .height = 1920,
570 },
911f4516 571 .pixel_rate = 55000000,
c6da1ae4 572 .hts = 1852,
2512c064 573 .vts = 0x1f8,
a8df5af6
JM
574 .reg_list = ov5647_640x480_10bpp,
575 .num_regs = ARRAY_SIZE(ov5647_640x480_10bpp)
576 },
577};
578
87576ac6 579/* Default sensor mode is 2x2 binned 640x480 SBGGR10_1X10. */
38c22308
JM
580#define OV5647_DEFAULT_MODE (&ov5647_modes[3])
581#define OV5647_DEFAULT_FORMAT (ov5647_modes[3].format)
d7d6074e 582
2512c064
DS
583static int ov5647_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
584{
585 unsigned char data[4] = { reg >> 8, reg & 0xff, val >> 8, val & 0xff};
586 struct i2c_client *client = v4l2_get_subdevdata(sd);
587 int ret;
588
589 ret = i2c_master_send(client, data, 4);
590 if (ret < 0) {
591 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
592 __func__, reg);
593 return ret;
594 }
595
596 return 0;
597}
598
3c2472a3
RO
599static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
600{
3c2472a3
RO
601 unsigned char data[3] = { reg >> 8, reg & 0xff, val};
602 struct i2c_client *client = v4l2_get_subdevdata(sd);
c9a05cec 603 int ret;
3c2472a3
RO
604
605 ret = i2c_master_send(client, data, 3);
2b18cbcf 606 if (ret < 0) {
3c2472a3
RO
607 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
608 __func__, reg);
2b18cbcf
JM
609 return ret;
610 }
3c2472a3 611
2b18cbcf 612 return 0;
3c2472a3
RO
613}
614
615static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
616{
3c2472a3
RO
617 unsigned char data_w[2] = { reg >> 8, reg & 0xff };
618 struct i2c_client *client = v4l2_get_subdevdata(sd);
c9a05cec 619 int ret;
3c2472a3
RO
620
621 ret = i2c_master_send(client, data_w, 2);
622 if (ret < 0) {
623 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
624 __func__, reg);
625 return ret;
626 }
627
628 ret = i2c_master_recv(client, val, 1);
2b18cbcf 629 if (ret < 0) {
3c2472a3
RO
630 dev_dbg(&client->dev, "%s: i2c read error, reg: %x\n",
631 __func__, reg);
2b18cbcf
JM
632 return ret;
633 }
3c2472a3 634
2b18cbcf 635 return 0;
3c2472a3
RO
636}
637
638static int ov5647_write_array(struct v4l2_subdev *sd,
d7d6074e 639 const struct regval_list *regs, int array_size)
3c2472a3
RO
640{
641 int i, ret;
642
643 for (i = 0; i < array_size; i++) {
644 ret = ov5647_write(sd, regs[i].addr, regs[i].data);
645 if (ret < 0)
646 return ret;
647 }
648
649 return 0;
650}
651
652static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
653{
654 u8 channel_id;
655 int ret;
656
cef66734 657 ret = ov5647_read(sd, OV5647_REG_MIPI_CTRL14, &channel_id);
3c2472a3
RO
658 if (ret < 0)
659 return ret;
660
661 channel_id &= ~(3 << 6);
c9a05cec
JM
662
663 return ov5647_write(sd, OV5647_REG_MIPI_CTRL14,
664 channel_id | (channel << 6));
3c2472a3
RO
665}
666
f7a70f9a
JM
667static int ov5647_set_mode(struct v4l2_subdev *sd)
668{
669 struct i2c_client *client = v4l2_get_subdevdata(sd);
d7d6074e 670 struct ov5647 *sensor = to_sensor(sd);
f7a70f9a
JM
671 u8 resetval, rdval;
672 int ret;
673
674 ret = ov5647_read(sd, OV5647_SW_STANDBY, &rdval);
675 if (ret < 0)
676 return ret;
677
d7d6074e
JM
678 ret = ov5647_write_array(sd, sensor->mode->reg_list,
679 sensor->mode->num_regs);
f7a70f9a
JM
680 if (ret < 0) {
681 dev_err(&client->dev, "write sensor default regs error\n");
682 return ret;
683 }
684
685 ret = ov5647_set_virtual_channel(sd, 0);
686 if (ret < 0)
687 return ret;
688
689 ret = ov5647_read(sd, OV5647_SW_STANDBY, &resetval);
690 if (ret < 0)
691 return ret;
692
693 if (!(resetval & 0x01)) {
694 dev_err(&client->dev, "Device was in SW standby");
695 ret = ov5647_write(sd, OV5647_SW_STANDBY, 0x01);
696 if (ret < 0)
697 return ret;
698 }
699
700 return 0;
701}
702
3c2472a3
RO
703static int ov5647_stream_on(struct v4l2_subdev *sd)
704{
f7a70f9a 705 struct i2c_client *client = v4l2_get_subdevdata(sd);
5bc5ca71 706 struct ov5647 *sensor = to_sensor(sd);
dea4fcfe 707 u8 val = MIPI_CTRL00_BUS_IDLE;
3c2472a3
RO
708 int ret;
709
f7a70f9a
JM
710 ret = ov5647_set_mode(sd);
711 if (ret) {
712 dev_err(&client->dev, "Failed to program sensor mode: %d\n", ret);
713 return ret;
714 }
715
4974c2f1
DP
716 /* Apply customized values from user when stream starts. */
717 ret = __v4l2_ctrl_handler_setup(sd->ctrl_handler);
718 if (ret)
719 return ret;
720
5bc5ca71 721 if (sensor->clock_ncont)
dea4fcfe
DS
722 val |= MIPI_CTRL00_CLOCK_LANE_GATE |
723 MIPI_CTRL00_LINE_SYNC_ENABLE;
724
725 ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00, val);
7bc9f038
JC
726 if (ret < 0)
727 return ret;
728
cef66734 729 ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x00);
3c2472a3
RO
730 if (ret < 0)
731 return ret;
732
cef66734 733 return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x00);
3c2472a3
RO
734}
735
736static int ov5647_stream_off(struct v4l2_subdev *sd)
737{
738 int ret;
739
c9a05cec
JM
740 ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00,
741 MIPI_CTRL00_CLOCK_LANE_GATE | MIPI_CTRL00_BUS_IDLE |
742 MIPI_CTRL00_CLOCK_LANE_DISABLE);
7bc9f038
JC
743 if (ret < 0)
744 return ret;
745
cef66734 746 ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x0f);
3c2472a3
RO
747 if (ret < 0)
748 return ret;
749
cef66734 750 return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x01);
3c2472a3
RO
751}
752
089b7c70 753static int ov5647_power_on(struct device *dev)
3c2472a3 754{
089b7c70 755 struct ov5647 *sensor = dev_get_drvdata(dev);
3c2472a3 756 int ret;
3c2472a3 757
089b7c70 758 dev_dbg(dev, "OV5647 power on\n");
3c2472a3 759
089b7c70
JM
760 if (sensor->pwdn) {
761 gpiod_set_value_cansleep(sensor->pwdn, 0);
762 msleep(PWDN_ACTIVE_DELAY_MS);
763 }
3c2472a3 764
089b7c70
JM
765 ret = clk_prepare_enable(sensor->xclk);
766 if (ret < 0) {
767 dev_err(dev, "clk prepare enable failed\n");
768 goto error_pwdn;
769 }
3c2472a3 770
089b7c70
JM
771 ret = ov5647_write_array(&sensor->sd, sensor_oe_enable_regs,
772 ARRAY_SIZE(sensor_oe_enable_regs));
773 if (ret < 0) {
774 dev_err(dev, "write sensor_oe_enable_regs error\n");
775 goto error_clk_disable;
776 }
3c2472a3 777
089b7c70
JM
778 /* Stream off to coax lanes into LP-11 state. */
779 ret = ov5647_stream_off(&sensor->sd);
780 if (ret < 0) {
781 dev_err(dev, "camera not available, check power\n");
782 goto error_clk_disable;
783 }
3c2472a3 784
089b7c70 785 return 0;
b050791d 786
089b7c70
JM
787error_clk_disable:
788 clk_disable_unprepare(sensor->xclk);
789error_pwdn:
790 gpiod_set_value_cansleep(sensor->pwdn, 1);
3c2472a3 791
089b7c70
JM
792 return ret;
793}
3c2472a3 794
089b7c70
JM
795static int ov5647_power_off(struct device *dev)
796{
797 struct ov5647 *sensor = dev_get_drvdata(dev);
798 u8 rdval;
799 int ret;
3c2472a3 800
089b7c70 801 dev_dbg(dev, "OV5647 power off\n");
3c2472a3 802
089b7c70
JM
803 ret = ov5647_write_array(&sensor->sd, sensor_oe_disable_regs,
804 ARRAY_SIZE(sensor_oe_disable_regs));
805 if (ret < 0)
806 dev_dbg(dev, "disable oe failed\n");
3c2472a3 807
089b7c70
JM
808 /* Enter software standby */
809 ret = ov5647_read(&sensor->sd, OV5647_SW_STANDBY, &rdval);
810 if (ret < 0)
811 dev_dbg(dev, "software standby failed\n");
3c2472a3 812
089b7c70
JM
813 rdval &= ~0x01;
814 ret = ov5647_write(&sensor->sd, OV5647_SW_STANDBY, rdval);
815 if (ret < 0)
816 dev_dbg(dev, "software standby failed\n");
3c2472a3 817
089b7c70
JM
818 clk_disable_unprepare(sensor->xclk);
819 gpiod_set_value_cansleep(sensor->pwdn, 1);
3c2472a3 820
089b7c70 821 return 0;
3c2472a3
RO
822}
823
824#ifdef CONFIG_VIDEO_ADV_DEBUG
825static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
c9a05cec 826 struct v4l2_dbg_register *reg)
3c2472a3 827{
3c2472a3 828 int ret;
c9a05cec 829 u8 val;
3c2472a3
RO
830
831 ret = ov5647_read(sd, reg->reg & 0xff, &val);
832 if (ret < 0)
833 return ret;
834
835 reg->val = val;
836 reg->size = 1;
837
838 return 0;
839}
840
841static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
c9a05cec 842 const struct v4l2_dbg_register *reg)
3c2472a3
RO
843{
844 return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
845}
846#endif
847
c9a05cec 848/* Subdev core operations registration */
3c2472a3 849static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = {
dc337308
JM
850 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
851 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
3c2472a3
RO
852#ifdef CONFIG_VIDEO_ADV_DEBUG
853 .g_register = ov5647_sensor_get_register,
854 .s_register = ov5647_sensor_set_register,
855#endif
856};
857
14f70a32 858static const struct v4l2_rect *
0d346d2a
TV
859__ov5647_get_pad_crop(struct ov5647 *ov5647,
860 struct v4l2_subdev_state *sd_state,
14f70a32
DS
861 unsigned int pad, enum v4l2_subdev_format_whence which)
862{
863 switch (which) {
864 case V4L2_SUBDEV_FORMAT_TRY:
0d346d2a 865 return v4l2_subdev_get_try_crop(&ov5647->sd, sd_state, pad);
14f70a32
DS
866 case V4L2_SUBDEV_FORMAT_ACTIVE:
867 return &ov5647->mode->crop;
868 }
869
870 return NULL;
871}
872
3c2472a3
RO
873static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
874{
2f038c97 875 struct i2c_client *client = v4l2_get_subdevdata(sd);
5bc5ca71 876 struct ov5647 *sensor = to_sensor(sd);
ab614f27
JM
877 int ret;
878
879 mutex_lock(&sensor->lock);
2f038c97
JM
880 if (sensor->streaming == enable) {
881 mutex_unlock(&sensor->lock);
882 return 0;
883 }
884
885 if (enable) {
5187df40 886 ret = pm_runtime_resume_and_get(&client->dev);
2f038c97
JM
887 if (ret < 0)
888 goto error_unlock;
889
ab614f27 890 ret = ov5647_stream_on(sd);
2f038c97
JM
891 if (ret < 0) {
892 dev_err(&client->dev, "stream start failed: %d\n", ret);
5187df40 893 goto error_pm;
2f038c97
JM
894 }
895 } else {
ab614f27 896 ret = ov5647_stream_off(sd);
2f038c97
JM
897 if (ret < 0) {
898 dev_err(&client->dev, "stream stop failed: %d\n", ret);
5187df40 899 goto error_pm;
2f038c97
JM
900 }
901 pm_runtime_put(&client->dev);
902 }
903
904 sensor->streaming = enable;
905 mutex_unlock(&sensor->lock);
906
907 return 0;
908
5187df40 909error_pm:
2f038c97 910 pm_runtime_put(&client->dev);
5187df40 911error_unlock:
ab614f27
JM
912 mutex_unlock(&sensor->lock);
913
914 return ret;
3c2472a3
RO
915}
916
917static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
918 .s_stream = ov5647_s_stream,
919};
920
921static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
0d346d2a 922 struct v4l2_subdev_state *sd_state,
c9a05cec 923 struct v4l2_subdev_mbus_code_enum *code)
3c2472a3 924{
38c22308 925 if (code->index > 0)
3c2472a3
RO
926 return -EINVAL;
927
38c22308 928 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
3c2472a3
RO
929
930 return 0;
931}
932
464090c0 933static int ov5647_enum_frame_size(struct v4l2_subdev *sd,
0d346d2a 934 struct v4l2_subdev_state *sd_state,
464090c0
JM
935 struct v4l2_subdev_frame_size_enum *fse)
936{
d7d6074e 937 const struct v4l2_mbus_framefmt *fmt;
464090c0 938
38c22308
JM
939 if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10 ||
940 fse->index >= ARRAY_SIZE(ov5647_modes))
464090c0
JM
941 return -EINVAL;
942
38c22308 943 fmt = &ov5647_modes[fse->index].format;
d7d6074e
JM
944 fse->min_width = fmt->width;
945 fse->max_width = fmt->width;
946 fse->min_height = fmt->height;
947 fse->max_height = fmt->height;
464090c0
JM
948
949 return 0;
950}
951
6869e971 952static int ov5647_get_pad_fmt(struct v4l2_subdev *sd,
0d346d2a 953 struct v4l2_subdev_state *sd_state,
0f87233a
DS
954 struct v4l2_subdev_format *format)
955{
956 struct v4l2_mbus_framefmt *fmt = &format->format;
6869e971
JM
957 const struct v4l2_mbus_framefmt *sensor_format;
958 struct ov5647 *sensor = to_sensor(sd);
959
960 mutex_lock(&sensor->lock);
961 switch (format->which) {
962 case V4L2_SUBDEV_FORMAT_TRY:
0d346d2a
TV
963 sensor_format = v4l2_subdev_get_try_format(sd, sd_state,
964 format->pad);
6869e971
JM
965 break;
966 default:
967 sensor_format = &sensor->mode->format;
968 break;
969 }
970
971 *fmt = *sensor_format;
972 mutex_unlock(&sensor->lock);
0f87233a 973
6869e971
JM
974 return 0;
975}
976
977static int ov5647_set_pad_fmt(struct v4l2_subdev *sd,
0d346d2a 978 struct v4l2_subdev_state *sd_state,
6869e971
JM
979 struct v4l2_subdev_format *format)
980{
981 struct v4l2_mbus_framefmt *fmt = &format->format;
6869e971
JM
982 struct ov5647 *sensor = to_sensor(sd);
983 const struct ov5647_mode *mode;
6869e971 984
38c22308 985 mode = v4l2_find_nearest_size(ov5647_modes, ARRAY_SIZE(ov5647_modes),
6869e971
JM
986 format.width, format.height,
987 fmt->width, fmt->height);
988
989 /* Update the sensor mode and apply at it at streamon time. */
990 mutex_lock(&sensor->lock);
911f4516 991 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
0d346d2a 992 *v4l2_subdev_get_try_format(sd, sd_state, format->pad) = mode->format;
911f4516 993 } else {
646a0249 994 int exposure_max, exposure_def;
2512c064 995 int hblank, vblank;
c6da1ae4 996
6869e971 997 sensor->mode = mode;
911f4516
DS
998 __v4l2_ctrl_modify_range(sensor->pixel_rate, mode->pixel_rate,
999 mode->pixel_rate, 1, mode->pixel_rate);
c6da1ae4
JM
1000
1001 hblank = mode->hts - mode->format.width;
1002 __v4l2_ctrl_modify_range(sensor->hblank, hblank, hblank, 1,
1003 hblank);
2512c064
DS
1004
1005 vblank = mode->vts - mode->format.height;
1006 __v4l2_ctrl_modify_range(sensor->vblank, OV5647_VBLANK_MIN,
1007 OV5647_VTS_MAX - mode->format.height,
1008 1, vblank);
1009 __v4l2_ctrl_s_ctrl(sensor->vblank, vblank);
646a0249
DS
1010
1011 exposure_max = mode->vts - 4;
1012 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1013 __v4l2_ctrl_modify_range(sensor->exposure,
1014 sensor->exposure->minimum,
1015 exposure_max, sensor->exposure->step,
1016 exposure_def);
911f4516 1017 }
6869e971
JM
1018 *fmt = mode->format;
1019 mutex_unlock(&sensor->lock);
0f87233a
DS
1020
1021 return 0;
1022}
1023
14f70a32 1024static int ov5647_get_selection(struct v4l2_subdev *sd,
0d346d2a 1025 struct v4l2_subdev_state *sd_state,
14f70a32
DS
1026 struct v4l2_subdev_selection *sel)
1027{
1028 switch (sel->target) {
1029 case V4L2_SEL_TGT_CROP: {
1030 struct ov5647 *sensor = to_sensor(sd);
1031
1032 mutex_lock(&sensor->lock);
0d346d2a 1033 sel->r = *__ov5647_get_pad_crop(sensor, sd_state, sel->pad,
14f70a32
DS
1034 sel->which);
1035 mutex_unlock(&sensor->lock);
1036
1037 return 0;
1038 }
1039
1040 case V4L2_SEL_TGT_NATIVE_SIZE:
1041 sel->r.top = 0;
1042 sel->r.left = 0;
1043 sel->r.width = OV5647_NATIVE_WIDTH;
1044 sel->r.height = OV5647_NATIVE_HEIGHT;
1045
1046 return 0;
1047
1048 case V4L2_SEL_TGT_CROP_DEFAULT:
1049 case V4L2_SEL_TGT_CROP_BOUNDS:
1050 sel->r.top = OV5647_PIXEL_ARRAY_TOP;
1051 sel->r.left = OV5647_PIXEL_ARRAY_LEFT;
1052 sel->r.width = OV5647_PIXEL_ARRAY_WIDTH;
1053 sel->r.height = OV5647_PIXEL_ARRAY_HEIGHT;
1054
1055 return 0;
1056 }
1057
1058 return -EINVAL;
1059}
1060
3c2472a3 1061static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
464090c0
JM
1062 .enum_mbus_code = ov5647_enum_mbus_code,
1063 .enum_frame_size = ov5647_enum_frame_size,
6869e971
JM
1064 .set_fmt = ov5647_set_pad_fmt,
1065 .get_fmt = ov5647_get_pad_fmt,
14f70a32 1066 .get_selection = ov5647_get_selection,
3c2472a3
RO
1067};
1068
1069static const struct v4l2_subdev_ops ov5647_subdev_ops = {
1070 .core = &ov5647_subdev_core_ops,
1071 .video = &ov5647_subdev_video_ops,
1072 .pad = &ov5647_subdev_pad_ops,
1073};
1074
1075static int ov5647_detect(struct v4l2_subdev *sd)
1076{
c9a05cec 1077 struct i2c_client *client = v4l2_get_subdevdata(sd);
3c2472a3
RO
1078 u8 read;
1079 int ret;
3c2472a3
RO
1080
1081 ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
1082 if (ret < 0)
1083 return ret;
1084
1085 ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
1086 if (ret < 0)
1087 return ret;
1088
1089 if (read != 0x56) {
1090 dev_err(&client->dev, "ID High expected 0x56 got %x", read);
1091 return -ENODEV;
1092 }
1093
1094 ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
1095 if (ret < 0)
1096 return ret;
1097
1098 if (read != 0x47) {
1099 dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
1100 return -ENODEV;
1101 }
1102
1103 return ov5647_write(sd, OV5647_SW_RESET, 0x00);
1104}
1105
1106static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1107{
1108 struct v4l2_mbus_framefmt *format =
0d346d2a
TV
1109 v4l2_subdev_get_try_format(sd, fh->state, 0);
1110 struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
3c2472a3 1111
14f70a32
DS
1112 crop->left = OV5647_PIXEL_ARRAY_LEFT;
1113 crop->top = OV5647_PIXEL_ARRAY_TOP;
1114 crop->width = OV5647_PIXEL_ARRAY_WIDTH;
1115 crop->height = OV5647_PIXEL_ARRAY_HEIGHT;
3c2472a3 1116
d7d6074e 1117 *format = OV5647_DEFAULT_FORMAT;
3c2472a3
RO
1118
1119 return 0;
1120}
1121
1122static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
1123 .open = ov5647_open,
1124};
1125
4974c2f1
DP
1126static int ov5647_s_auto_white_balance(struct v4l2_subdev *sd, u32 val)
1127{
1128 return ov5647_write(sd, OV5647_REG_AWB, val ? 1 : 0);
1129}
1130
1131static int ov5647_s_autogain(struct v4l2_subdev *sd, u32 val)
1132{
1133 int ret;
1134 u8 reg;
1135
1136 /* Non-zero turns on AGC by clearing bit 1.*/
1137 ret = ov5647_read(sd, OV5647_REG_AEC_AGC, &reg);
1138 if (ret)
1139 return ret;
1140
1141 return ov5647_write(sd, OV5647_REG_AEC_AGC, val ? reg & ~BIT(1)
1142 : reg | BIT(1));
1143}
1144
1145static int ov5647_s_exposure_auto(struct v4l2_subdev *sd, u32 val)
1146{
1147 int ret;
1148 u8 reg;
1149
1150 /*
1151 * Everything except V4L2_EXPOSURE_MANUAL turns on AEC by
1152 * clearing bit 0.
1153 */
1154 ret = ov5647_read(sd, OV5647_REG_AEC_AGC, &reg);
1155 if (ret)
1156 return ret;
1157
1158 return ov5647_write(sd, OV5647_REG_AEC_AGC,
1159 val == V4L2_EXPOSURE_MANUAL ? reg | BIT(0)
1160 : reg & ~BIT(0));
1161}
1162
1163static int ov5647_s_analogue_gain(struct v4l2_subdev *sd, u32 val)
1164{
1165 int ret;
1166
1167 /* 10 bits of gain, 2 in the high register. */
1168 ret = ov5647_write(sd, OV5647_REG_GAIN_HI, (val >> 8) & 3);
1169 if (ret)
1170 return ret;
1171
1172 return ov5647_write(sd, OV5647_REG_GAIN_LO, val & 0xff);
1173}
1174
1175static int ov5647_s_exposure(struct v4l2_subdev *sd, u32 val)
1176{
1177 int ret;
1178
1179 /*
1180 * Sensor has 20 bits, but the bottom 4 bits are fractions of a line
1181 * which we leave as zero (and don't receive in "val").
1182 */
1183 ret = ov5647_write(sd, OV5647_REG_EXP_HI, (val >> 12) & 0xf);
1184 if (ret)
1185 return ret;
1186
1187 ret = ov5647_write(sd, OV5647_REG_EXP_MID, (val >> 4) & 0xff);
1188 if (ret)
1189 return ret;
1190
1191 return ov5647_write(sd, OV5647_REG_EXP_LO, (val & 0xf) << 4);
1192}
1193
1194static int ov5647_s_ctrl(struct v4l2_ctrl *ctrl)
1195{
1196 struct ov5647 *sensor = container_of(ctrl->handler,
1197 struct ov5647, ctrls);
1198 struct v4l2_subdev *sd = &sensor->sd;
1199 struct i2c_client *client = v4l2_get_subdevdata(sd);
4eec1919
JM
1200 int ret = 0;
1201
4974c2f1
DP
1202
1203 /* v4l2_ctrl_lock() locks our own mutex */
1204
646a0249
DS
1205 if (ctrl->id == V4L2_CID_VBLANK) {
1206 int exposure_max, exposure_def;
1207
1208 /* Update max exposure while meeting expected vblanking */
1209 exposure_max = sensor->mode->format.height + ctrl->val - 4;
1210 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1211 __v4l2_ctrl_modify_range(sensor->exposure,
1212 sensor->exposure->minimum,
1213 exposure_max, sensor->exposure->step,
1214 exposure_def);
1215 }
1216
4974c2f1 1217 /*
4eec1919
JM
1218 * If the device is not powered up do not apply any controls
1219 * to H/W at this time. Instead the controls will be restored
1220 * at s_stream(1) time.
4974c2f1 1221 */
4eec1919 1222 if (pm_runtime_get_if_in_use(&client->dev) == 0)
4974c2f1
DP
1223 return 0;
1224
1225 switch (ctrl->id) {
1226 case V4L2_CID_AUTO_WHITE_BALANCE:
4eec1919
JM
1227 ret = ov5647_s_auto_white_balance(sd, ctrl->val);
1228 break;
4974c2f1 1229 case V4L2_CID_AUTOGAIN:
4eec1919
JM
1230 ret = ov5647_s_autogain(sd, ctrl->val);
1231 break;
4974c2f1 1232 case V4L2_CID_EXPOSURE_AUTO:
4eec1919
JM
1233 ret = ov5647_s_exposure_auto(sd, ctrl->val);
1234 break;
4974c2f1 1235 case V4L2_CID_ANALOGUE_GAIN:
4eec1919
JM
1236 ret = ov5647_s_analogue_gain(sd, ctrl->val);
1237 break;
4974c2f1 1238 case V4L2_CID_EXPOSURE:
4eec1919
JM
1239 ret = ov5647_s_exposure(sd, ctrl->val);
1240 break;
1241 case V4L2_CID_VBLANK:
1242 ret = ov5647_write16(sd, OV5647_REG_VTS_HI,
1243 sensor->mode->format.height + ctrl->val);
1244 break;
1245
1246 /* Read-only, but we adjust it based on mode. */
911f4516 1247 case V4L2_CID_PIXEL_RATE:
c6da1ae4
JM
1248 case V4L2_CID_HBLANK:
1249 /* Read-only, but we adjust it based on mode. */
4eec1919
JM
1250 break;
1251
4974c2f1
DP
1252 default:
1253 dev_info(&client->dev,
1254 "Control (id:0x%x, val:0x%x) not supported\n",
1255 ctrl->id, ctrl->val);
1256 return -EINVAL;
1257 }
1258
4eec1919
JM
1259 pm_runtime_put(&client->dev);
1260
1261 return ret;
4974c2f1
DP
1262}
1263
1264static const struct v4l2_ctrl_ops ov5647_ctrl_ops = {
1265 .s_ctrl = ov5647_s_ctrl,
1266};
1267
1268static int ov5647_init_controls(struct ov5647 *sensor)
1269{
1270 struct i2c_client *client = v4l2_get_subdevdata(&sensor->sd);
646a0249 1271 int hblank, exposure_max, exposure_def;
4974c2f1 1272
2512c064 1273 v4l2_ctrl_handler_init(&sensor->ctrls, 8);
4974c2f1
DP
1274
1275 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1276 V4L2_CID_AUTOGAIN, 0, 1, 1, 0);
1277
1278 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1279 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 0);
1280
1281 v4l2_ctrl_new_std_menu(&sensor->ctrls, &ov5647_ctrl_ops,
1282 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
1283 0, V4L2_EXPOSURE_MANUAL);
1284
646a0249
DS
1285 exposure_max = sensor->mode->vts - 4;
1286 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1287 sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1288 V4L2_CID_EXPOSURE,
1289 OV5647_EXPOSURE_MIN,
1290 exposure_max, OV5647_EXPOSURE_STEP,
1291 exposure_def);
4974c2f1
DP
1292
1293 /* min: 16 = 1.0x; max (10 bits); default: 32 = 2.0x. */
1294 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1295 V4L2_CID_ANALOGUE_GAIN, 16, 1023, 1, 32);
1296
911f4516
DS
1297 /* By default, PIXEL_RATE is read only, but it does change per mode */
1298 sensor->pixel_rate = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1299 V4L2_CID_PIXEL_RATE,
1300 sensor->mode->pixel_rate,
1301 sensor->mode->pixel_rate, 1,
1302 sensor->mode->pixel_rate);
c6da1ae4
JM
1303
1304 /* By default, HBLANK is read only, but it does change per mode. */
1305 hblank = sensor->mode->hts - sensor->mode->format.width;
1306 sensor->hblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1307 V4L2_CID_HBLANK, hblank, hblank, 1,
1308 hblank);
1309
2512c064
DS
1310 sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1311 V4L2_CID_VBLANK, OV5647_VBLANK_MIN,
1312 OV5647_VTS_MAX -
1313 sensor->mode->format.height, 1,
1314 sensor->mode->vts -
1315 sensor->mode->format.height);
1316
911f4516
DS
1317 if (sensor->ctrls.error)
1318 goto handler_free;
4974c2f1 1319
911f4516 1320 sensor->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
c6da1ae4 1321 sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
4974c2f1
DP
1322 sensor->sd.ctrl_handler = &sensor->ctrls;
1323
1324 return 0;
911f4516
DS
1325
1326handler_free:
1327 dev_err(&client->dev, "%s Controls initialization failed (%d)\n",
1328 __func__, sensor->ctrls.error);
1329 v4l2_ctrl_handler_free(&sensor->ctrls);
1330
1331 return sensor->ctrls.error;
4974c2f1
DP
1332}
1333
dea4fcfe 1334static int ov5647_parse_dt(struct ov5647 *sensor, struct device_node *np)
3c2472a3 1335{
dea4fcfe
DS
1336 struct v4l2_fwnode_endpoint bus_cfg = {
1337 .bus_type = V4L2_MBUS_CSI2_DPHY,
1338 };
3c2472a3 1339 struct device_node *ep;
3c2472a3
RO
1340 int ret;
1341
1342 ep = of_graph_get_next_endpoint(np, NULL);
1343 if (!ep)
1344 return -EINVAL;
1345
859969b3 1346 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
dea4fcfe
DS
1347 if (ret)
1348 goto out;
3c2472a3 1349
dea4fcfe
DS
1350 sensor->clock_ncont = bus_cfg.bus.mipi_csi2.flags &
1351 V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1352
1353out:
3c2472a3 1354 of_node_put(ep);
c9a05cec 1355
3c2472a3
RO
1356 return ret;
1357}
1358
e6714993 1359static int ov5647_probe(struct i2c_client *client)
3c2472a3 1360{
c9a05cec 1361 struct device_node *np = client->dev.of_node;
3c2472a3
RO
1362 struct device *dev = &client->dev;
1363 struct ov5647 *sensor;
3c2472a3 1364 struct v4l2_subdev *sd;
3c2472a3 1365 u32 xclk_freq;
c9a05cec 1366 int ret;
3c2472a3
RO
1367
1368 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1369 if (!sensor)
1370 return -ENOMEM;
1371
1372 if (IS_ENABLED(CONFIG_OF) && np) {
dea4fcfe 1373 ret = ov5647_parse_dt(sensor, np);
3c2472a3
RO
1374 if (ret) {
1375 dev_err(dev, "DT parsing error: %d\n", ret);
1376 return ret;
1377 }
1378 }
1379
3c2472a3
RO
1380 sensor->xclk = devm_clk_get(dev, NULL);
1381 if (IS_ERR(sensor->xclk)) {
1382 dev_err(dev, "could not get xclk");
1383 return PTR_ERR(sensor->xclk);
1384 }
1385
1386 xclk_freq = clk_get_rate(sensor->xclk);
1387 if (xclk_freq != 25000000) {
1388 dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
1389 return -EINVAL;
1390 }
1391
c9a05cec
JM
1392 /* Request the power down GPIO asserted. */
1393 sensor->pwdn = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_HIGH);
b050791d
DS
1394 if (IS_ERR(sensor->pwdn)) {
1395 dev_err(dev, "Failed to get 'pwdn' gpio\n");
1396 return -EINVAL;
1397 }
1398
3c2472a3
RO
1399 mutex_init(&sensor->lock);
1400
d7d6074e
JM
1401 sensor->mode = OV5647_DEFAULT_MODE;
1402
4974c2f1
DP
1403 ret = ov5647_init_controls(sensor);
1404 if (ret)
1405 goto mutex_destroy;
1406
3c2472a3
RO
1407 sd = &sensor->sd;
1408 v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops);
c9a05cec 1409 sd->internal_ops = &ov5647_subdev_internal_ops;
7ef761a0 1410 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
3c2472a3
RO
1411
1412 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1413 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1414 ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
1415 if (ret < 0)
4974c2f1 1416 goto ctrl_handler_free;
3c2472a3 1417
089b7c70
JM
1418 ret = ov5647_power_on(dev);
1419 if (ret)
1420 goto entity_cleanup;
b050791d 1421
3c2472a3
RO
1422 ret = ov5647_detect(sd);
1423 if (ret < 0)
089b7c70 1424 goto power_off;
3c2472a3
RO
1425
1426 ret = v4l2_async_register_subdev(sd);
1427 if (ret < 0)
089b7c70
JM
1428 goto power_off;
1429
1430 /* Enable runtime PM and turn off the device */
1431 pm_runtime_set_active(dev);
1432 pm_runtime_enable(dev);
1433 pm_runtime_idle(dev);
3c2472a3
RO
1434
1435 dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
c9a05cec 1436
3c2472a3 1437 return 0;
c9a05cec 1438
089b7c70
JM
1439power_off:
1440 ov5647_power_off(dev);
c9a05cec 1441entity_cleanup:
3c2472a3 1442 media_entity_cleanup(&sd->entity);
4974c2f1
DP
1443ctrl_handler_free:
1444 v4l2_ctrl_handler_free(&sensor->ctrls);
c9a05cec 1445mutex_destroy:
3c2472a3 1446 mutex_destroy(&sensor->lock);
c9a05cec 1447
3c2472a3
RO
1448 return ret;
1449}
1450
1451static int ov5647_remove(struct i2c_client *client)
1452{
1453 struct v4l2_subdev *sd = i2c_get_clientdata(client);
5bc5ca71 1454 struct ov5647 *sensor = to_sensor(sd);
3c2472a3 1455
5bc5ca71
JM
1456 v4l2_async_unregister_subdev(&sensor->sd);
1457 media_entity_cleanup(&sensor->sd.entity);
1458 v4l2_ctrl_handler_free(&sensor->ctrls);
3c2472a3 1459 v4l2_device_unregister_subdev(sd);
089b7c70 1460 pm_runtime_disable(&client->dev);
5bc5ca71 1461 mutex_destroy(&sensor->lock);
3c2472a3
RO
1462
1463 return 0;
1464}
1465
089b7c70
JM
1466static const struct dev_pm_ops ov5647_pm_ops = {
1467 SET_RUNTIME_PM_OPS(ov5647_power_off, ov5647_power_on, NULL)
1468};
1469
3c2472a3
RO
1470static const struct i2c_device_id ov5647_id[] = {
1471 { "ov5647", 0 },
c9a05cec 1472 { /* sentinel */ }
3c2472a3
RO
1473};
1474MODULE_DEVICE_TABLE(i2c, ov5647_id);
1475
1476#if IS_ENABLED(CONFIG_OF)
1477static const struct of_device_id ov5647_of_match[] = {
1478 { .compatible = "ovti,ov5647" },
1479 { /* sentinel */ },
1480};
1481MODULE_DEVICE_TABLE(of, ov5647_of_match);
1482#endif
1483
1484static struct i2c_driver ov5647_driver = {
1485 .driver = {
1486 .of_match_table = of_match_ptr(ov5647_of_match),
c9a05cec 1487 .name = "ov5647",
089b7c70 1488 .pm = &ov5647_pm_ops,
3c2472a3 1489 },
e6714993 1490 .probe_new = ov5647_probe,
3c2472a3
RO
1491 .remove = ov5647_remove,
1492 .id_table = ov5647_id,
1493};
1494
1495module_i2c_driver(ov5647_driver);
1496
1497MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>");
1498MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
1499MODULE_LICENSE("GPL v2");