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