hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/media/i2c/ov2685.c
....@@ -1,15 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * ov2685 driver
34 *
45 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version.
10
- * V0.0X01.0X01 add enum_frame_interval function.
11
- * V0.0X01.0X02 add quick stream on/off
12
- * V0.0X01.0X03 add function g_mbus_config
136 */
147
158 #include <linux/clk.h>
....@@ -21,18 +14,15 @@
2114 #include <linux/pm_runtime.h>
2215 #include <linux/regulator/consumer.h>
2316 #include <linux/sysfs.h>
24
-#include <linux/slab.h>
25
-#include <linux/version.h>
26
-#include <linux/rk-camera-module.h>
2717 #include <media/media-entity.h>
2818 #include <media/v4l2-async.h>
2919 #include <media/v4l2-ctrls.h>
3020 #include <media/v4l2-subdev.h>
3121
32
-#define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x3)
33
-
3422 #define CHIP_ID 0x2685
3523 #define OV2685_REG_CHIP_ID 0x300a
24
+
25
+#define OV2685_XVCLK_FREQ 24000000
3626
3727 #define REG_SC_CTRL_MODE 0x0100
3828 #define SC_CTRL_MODE_STANDBY 0x0
....@@ -54,7 +44,7 @@
5444 #define OV2685_REG_TEST_PATTERN 0x5080
5545 #define OV2685_TEST_PATTERN_DISABLED 0x00
5646 #define OV2685_TEST_PATTERN_COLOR_BAR 0x80
57
-#define OV2685_TEST_PATTERN_RND 0x81
47
+#define OV2685_TEST_PATTERN_RANDOM 0x81
5848 #define OV2685_TEST_PATTERN_COLOR_BAR_FADE 0x88
5949 #define OV2685_TEST_PATTERN_BW_SQUARE 0x92
6050 #define OV2685_TEST_PATTERN_COLOR_SQUARE 0x82
....@@ -68,7 +58,13 @@
6858 #define OV2685_LANES 1
6959 #define OV2685_BITS_PER_SAMPLE 10
7060
71
-#define OV2685_NAME "ov2685"
61
+static const char * const ov2685_supply_names[] = {
62
+ "avdd", /* Analog power */
63
+ "dovdd", /* Digital I/O power */
64
+ "dvdd", /* Digital core power */
65
+};
66
+
67
+#define OV2685_NUM_SUPPLIES ARRAY_SIZE(ov2685_supply_names)
7268
7369 struct regval {
7470 u16 addr;
....@@ -81,17 +77,14 @@
8177 u32 exp_def;
8278 u32 hts_def;
8379 u32 vts_def;
84
- struct v4l2_fract max_fps;
8580 const struct regval *reg_list;
8681 };
8782
8883 struct ov2685 {
8984 struct i2c_client *client;
9085 struct clk *xvclk;
91
- struct regulator *avdd_regulator; /* Analog power */
92
- struct regulator *dovdd_regulator; /* Digital I/O power */
93
- /* use internal DVDD power */
9486 struct gpio_desc *reset_gpio;
87
+ struct regulator_bulk_data supplies[OV2685_NUM_SUPPLIES];
9588
9689 bool streaming;
9790 struct mutex mutex;
....@@ -105,11 +98,8 @@
10598 struct v4l2_ctrl_handler ctrl_handler;
10699
107100 const struct ov2685_mode *cur_mode;
108
- u32 module_index;
109
- const char *module_facing;
110
- const char *module_name;
111
- const char *len_name;
112101 };
102
+
113103 #define to_ov2685(sd) container_of(sd, struct ov2685, subdev)
114104
115105 /* PLL settings bases on 24M xvclk */
....@@ -129,7 +119,7 @@
129119 {0x3087, 0x00},
130120 {0x3501, 0x4e},
131121 {0x3502, 0xe0},
132
- {0x3503, 0x07},
122
+ {0x3503, 0x27},
133123 {0x350b, 0x36},
134124 {0x3600, 0xb4},
135125 {0x3603, 0x35},
....@@ -225,17 +215,17 @@
225215 static const char * const ov2685_test_pattern_menu[] = {
226216 "Disabled",
227217 "Color Bar",
228
- "RND PATTERN",
229218 "Color Bar FADE",
230
- "BW SQUARE",
231
- "COLOR SQUARE"
219
+ "Random Data",
220
+ "Black White Square",
221
+ "Color Square"
232222 };
233223
234224 static const int ov2685_test_pattern_val[] = {
235225 OV2685_TEST_PATTERN_DISABLED,
236226 OV2685_TEST_PATTERN_COLOR_BAR,
237
- OV2685_TEST_PATTERN_RND,
238227 OV2685_TEST_PATTERN_COLOR_BAR_FADE,
228
+ OV2685_TEST_PATTERN_RANDOM,
239229 OV2685_TEST_PATTERN_BW_SQUARE,
240230 OV2685_TEST_PATTERN_COLOR_SQUARE,
241231 };
....@@ -247,20 +237,15 @@
247237 .exp_def = 0x04ee,
248238 .hts_def = 0x06a4,
249239 .vts_def = 0x050e,
250
- .max_fps = {
251
- .numerator = 10000,
252
- .denominator = 300000,
253
- },
254240 .reg_list = ov2685_1600x1200_regs,
255241 },
256242 };
257243
258244 /* Write registers up to 4 at a time */
259245 static int ov2685_write_reg(struct i2c_client *client, u16 reg,
260
- unsigned int len, u32 val)
246
+ u32 len, u32 val)
261247 {
262
- int buf_i;
263
- int val_i;
248
+ u32 val_i, buf_i;
264249 u8 buf[6];
265250 u8 *val_p;
266251 __be32 val_be;
....@@ -288,7 +273,8 @@
288273 static int ov2685_write_array(struct i2c_client *client,
289274 const struct regval *regs)
290275 {
291
- int i, ret = 0;
276
+ int ret = 0;
277
+ u32 i;
292278
293279 for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
294280 ret = ov2685_write_reg(client, regs[i].addr,
....@@ -299,7 +285,7 @@
299285
300286 /* Read registers up to 4 at a time */
301287 static int ov2685_read_reg(struct i2c_client *client, u16 reg,
302
- unsigned int len, u32 *val)
288
+ u32 len, u32 *val)
303289 {
304290 struct i2c_msg msgs[2];
305291 u8 *data_be_p;
....@@ -332,12 +318,12 @@
332318 return 0;
333319 }
334320
335
-static void ov2685_fill_fmt(struct ov2685 *ov2685,
321
+static void ov2685_fill_fmt(const struct ov2685_mode *mode,
336322 struct v4l2_mbus_framefmt *fmt)
337323 {
338324 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
339
- fmt->width = ov2685->cur_mode->width;
340
- fmt->height = ov2685->cur_mode->height;
325
+ fmt->width = mode->width;
326
+ fmt->height = mode->height;
341327 fmt->field = V4L2_FIELD_NONE;
342328 }
343329
....@@ -348,7 +334,8 @@
348334 struct ov2685 *ov2685 = to_ov2685(sd);
349335 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
350336
351
- ov2685_fill_fmt(ov2685, mbus_fmt);
337
+ /* only one mode supported for now */
338
+ ov2685_fill_fmt(ov2685->cur_mode, mbus_fmt);
352339
353340 return 0;
354341 }
....@@ -360,7 +347,7 @@
360347 struct ov2685 *ov2685 = to_ov2685(sd);
361348 struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
362349
363
- ov2685_fill_fmt(ov2685, mbus_fmt);
350
+ ov2685_fill_fmt(ov2685->cur_mode, mbus_fmt);
364351
365352 return 0;
366353 }
....@@ -373,6 +360,7 @@
373360 return -EINVAL;
374361
375362 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
363
+
376364 return 0;
377365 }
378366
....@@ -395,34 +383,16 @@
395383 return 0;
396384 }
397385
398
-static inline void ov2685_set_exposure(struct ov2685 *ov2685, s32 val)
386
+/* Calculate the delay in us by clock rate and clock cycles */
387
+static inline u32 ov2685_cal_delay(u32 cycles)
399388 {
400
- ov2685_write_reg(ov2685->client, OV2685_REG_EXPOSURE,
401
- OV2685_REG_VALUE_24BIT, val << 4);
402
-}
403
-
404
-static inline void ov2685_set_gain(struct ov2685 *ov2685, s32 val)
405
-{
406
- ov2685_write_reg(ov2685->client, OV2685_REG_GAIN,
407
- OV2685_REG_VALUE_16BIT, val & OV2685_GAIN_MAX);
408
-}
409
-
410
-static inline void ov2685_set_vts(struct ov2685 *ov2685, s32 val)
411
-{
412
- val += ov2685->cur_mode->height;
413
- ov2685_write_reg(ov2685->client, OV2685_REG_VTS,
414
- OV2685_REG_VALUE_16BIT, val);
415
-}
416
-
417
-static inline void ov2685_enable_test_pattern(struct ov2685 *ov2685, u32 pat)
418
-{
419
- ov2685_write_reg(ov2685->client, OV2685_REG_TEST_PATTERN,
420
- OV2685_REG_VALUE_08BIT, ov2685_test_pattern_val[pat]);
389
+ return DIV_ROUND_UP(cycles, OV2685_XVCLK_FREQ / 1000 / 1000);
421390 }
422391
423392 static int __ov2685_power_on(struct ov2685 *ov2685)
424393 {
425394 int ret;
395
+ u32 delay_us;
426396 struct device *dev = &ov2685->client->dev;
427397
428398 ret = clk_prepare_enable(ov2685->xvclk);
....@@ -430,37 +400,33 @@
430400 dev_err(dev, "Failed to enable xvclk\n");
431401 return ret;
432402 }
433
- clk_set_rate(ov2685->xvclk, 24000000);
434403
435404 gpiod_set_value_cansleep(ov2685->reset_gpio, 1);
436
- /* AVDD and DOVDD may rise in any order */
437
- ret = regulator_enable(ov2685->avdd_regulator);
405
+
406
+ ret = regulator_bulk_enable(OV2685_NUM_SUPPLIES, ov2685->supplies);
438407 if (ret < 0) {
439
- dev_err(dev, "Failed to enable AVDD regulator\n");
440
- goto disable_xvclk;
408
+ dev_err(dev, "Failed to enable regulators\n");
409
+ goto disable_clk;
441410 }
442
- ret = regulator_enable(ov2685->dovdd_regulator);
443
- if (ret < 0) {
444
- dev_err(dev, "Failed to enable DOVDD regulator\n");
445
- goto disable_avdd;
446
- }
447
- /* The minimum delay between AVDD and reset rising can be 0 */
411
+
412
+ /* The minimum delay between power supplies and reset rising can be 0 */
448413 gpiod_set_value_cansleep(ov2685->reset_gpio, 0);
449
- /* 8192 xvclk cycles prior to the first SCCB transaction.
450
- * NOTE: An additional 1ms must be added to wait for
451
- * SCCB to become stable when using internal DVDD.
452
- */
453
- usleep_range(1350, 1500);
414
+ /* 8192 xvclk cycles prior to the first SCCB transaction */
415
+ delay_us = ov2685_cal_delay(8192);
416
+ usleep_range(delay_us, delay_us * 2);
454417
455418 /* HACK: ov2685 would output messy data after reset(R0103),
456419 * writing register before .s_stream() as a workaround
457420 */
458421 ret = ov2685_write_array(ov2685->client, ov2685->cur_mode->reg_list);
422
+ if (ret)
423
+ goto disable_supplies;
459424
460
- return ret;
461
-disable_avdd:
462
- regulator_disable(ov2685->avdd_regulator);
463
-disable_xvclk:
425
+ return 0;
426
+
427
+disable_supplies:
428
+ regulator_bulk_disable(OV2685_NUM_SUPPLIES, ov2685->supplies);
429
+disable_clk:
464430 clk_disable_unprepare(ov2685->xvclk);
465431
466432 return ret;
....@@ -469,117 +435,13 @@
469435 static void __ov2685_power_off(struct ov2685 *ov2685)
470436 {
471437 /* 512 xvclk cycles after the last SCCB transaction or MIPI frame end */
472
- usleep_range(30, 50);
438
+ u32 delay_us = ov2685_cal_delay(512);
439
+
440
+ usleep_range(delay_us, delay_us * 2);
473441 clk_disable_unprepare(ov2685->xvclk);
474442 gpiod_set_value_cansleep(ov2685->reset_gpio, 1);
475
- regulator_disable(ov2685->dovdd_regulator);
476
- regulator_disable(ov2685->avdd_regulator);
443
+ regulator_bulk_disable(OV2685_NUM_SUPPLIES, ov2685->supplies);
477444 }
478
-
479
-static int ov2685_s_power(struct v4l2_subdev *sd, int on)
480
-{
481
- struct ov2685 *ov2685 = to_ov2685(sd);
482
- int ret = 0;
483
-
484
- mutex_lock(&ov2685->mutex);
485
-
486
- if (on)
487
- ret = pm_runtime_get_sync(&ov2685->client->dev);
488
- else
489
- ret = pm_runtime_put(&ov2685->client->dev);
490
-
491
- mutex_unlock(&ov2685->mutex);
492
-
493
- return ret;
494
-}
495
-
496
-static void ov2685_get_module_inf(struct ov2685 *ov2685,
497
- struct rkmodule_inf *inf)
498
-{
499
- memset(inf, 0, sizeof(*inf));
500
- strlcpy(inf->base.sensor, OV2685_NAME, sizeof(inf->base.sensor));
501
- strlcpy(inf->base.module, ov2685->module_name,
502
- sizeof(inf->base.module));
503
- strlcpy(inf->base.lens, ov2685->len_name, sizeof(inf->base.lens));
504
-}
505
-
506
-static long ov2685_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
507
-{
508
- struct ov2685 *ov2685 = to_ov2685(sd);
509
- long ret = 0;
510
- u32 stream = 0;
511
-
512
- switch (cmd) {
513
- case RKMODULE_GET_MODULE_INFO:
514
- ov2685_get_module_inf(ov2685, (struct rkmodule_inf *)arg);
515
- break;
516
- case RKMODULE_SET_QUICK_STREAM:
517
-
518
- stream = *((u32 *)arg);
519
-
520
- if (stream)
521
- ret = ov2685_write_reg(ov2685->client, REG_SC_CTRL_MODE,
522
- OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STREAMING);
523
- else
524
- ret = ov2685_write_reg(ov2685->client, REG_SC_CTRL_MODE,
525
- OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STANDBY);
526
- break;
527
- default:
528
- ret = -ENOIOCTLCMD;
529
- break;
530
- }
531
-
532
- return ret;
533
-}
534
-
535
-#ifdef CONFIG_COMPAT
536
-static long ov2685_compat_ioctl32(struct v4l2_subdev *sd,
537
- unsigned int cmd, unsigned long arg)
538
-{
539
- void __user *up = compat_ptr(arg);
540
- struct rkmodule_inf *inf;
541
- struct rkmodule_awb_cfg *cfg;
542
- long ret;
543
- u32 stream = 0;
544
-
545
- switch (cmd) {
546
- case RKMODULE_GET_MODULE_INFO:
547
- inf = kzalloc(sizeof(*inf), GFP_KERNEL);
548
- if (!inf) {
549
- ret = -ENOMEM;
550
- return ret;
551
- }
552
-
553
- ret = ov2685_ioctl(sd, cmd, inf);
554
- if (!ret)
555
- ret = copy_to_user(up, inf, sizeof(*inf));
556
- kfree(inf);
557
- break;
558
- case RKMODULE_AWB_CFG:
559
- cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
560
- if (!cfg) {
561
- ret = -ENOMEM;
562
- return ret;
563
- }
564
-
565
- ret = copy_from_user(cfg, up, sizeof(*cfg));
566
- if (!ret)
567
- ret = ov2685_ioctl(sd, cmd, cfg);
568
- kfree(cfg);
569
- break;
570
- case RKMODULE_SET_QUICK_STREAM:
571
- ret = copy_from_user(&stream, up, sizeof(u32));
572
- if (!ret)
573
- ret = ov2685_ioctl(sd, cmd, &stream);
574
- break;
575
- default:
576
- ret = -ENOIOCTLCMD;
577
- break;
578
- }
579
-
580
- return ret;
581
-}
582
-#endif
583445
584446 static int ov2685_s_stream(struct v4l2_subdev *sd, int on)
585447 {
....@@ -594,27 +456,33 @@
594456 goto unlock_and_return;
595457
596458 if (on) {
597
- /* In case these controls are set before streaming */
598
- ov2685_set_exposure(ov2685, ov2685->exposure->val);
599
- ov2685_set_gain(ov2685, ov2685->anal_gain->val);
600
- ov2685_set_vts(ov2685, ov2685->vblank->val);
601
- ov2685_enable_test_pattern(ov2685, ov2685->test_pattern->val);
602
-
459
+ ret = pm_runtime_get_sync(&ov2685->client->dev);
460
+ if (ret < 0) {
461
+ pm_runtime_put_noidle(&client->dev);
462
+ goto unlock_and_return;
463
+ }
464
+ ret = __v4l2_ctrl_handler_setup(&ov2685->ctrl_handler);
465
+ if (ret) {
466
+ pm_runtime_put(&client->dev);
467
+ goto unlock_and_return;
468
+ }
603469 ret = ov2685_write_reg(client, REG_SC_CTRL_MODE,
604470 OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STREAMING);
605
- if (ret)
471
+ if (ret) {
472
+ pm_runtime_put(&client->dev);
606473 goto unlock_and_return;
474
+ }
607475 } else {
608
- ret = ov2685_write_reg(client, REG_SC_CTRL_MODE,
476
+ ov2685_write_reg(client, REG_SC_CTRL_MODE,
609477 OV2685_REG_VALUE_08BIT, SC_CTRL_MODE_STANDBY);
610
- if (ret)
611
- goto unlock_and_return;
478
+ pm_runtime_put(&ov2685->client->dev);
612479 }
613480
614481 ov2685->streaming = on;
615482
616483 unlock_and_return:
617484 mutex_unlock(&ov2685->mutex);
485
+
618486 return ret;
619487 }
620488
....@@ -628,7 +496,7 @@
628496
629497 try_fmt = v4l2_subdev_get_try_format(sd, fh->pad, 0);
630498 /* Initialize try_fmt */
631
- ov2685_fill_fmt(ov2685, try_fmt);
499
+ ov2685_fill_fmt(&supported_modes[0], try_fmt);
632500
633501 mutex_unlock(&ov2685->mutex);
634502
....@@ -636,27 +504,16 @@
636504 }
637505 #endif
638506
639
-static int ov2685_runtime_resume(struct device *dev)
507
+static int __maybe_unused ov2685_runtime_resume(struct device *dev)
640508 {
641509 struct i2c_client *client = to_i2c_client(dev);
642510 struct v4l2_subdev *sd = i2c_get_clientdata(client);
643511 struct ov2685 *ov2685 = to_ov2685(sd);
644
- int ret;
645512
646
- ret = __ov2685_power_on(ov2685);
647
- if (ret)
648
- return ret;
649
-
650
- if (ov2685->streaming) {
651
- ret = ov2685_s_stream(sd, 1);
652
- if (ret)
653
- __ov2685_power_off(ov2685);
654
- }
655
-
656
- return ret;
513
+ return __ov2685_power_on(ov2685);
657514 }
658515
659
-static int ov2685_runtime_suspend(struct device *dev)
516
+static int __maybe_unused ov2685_runtime_suspend(struct device *dev)
660517 {
661518 struct i2c_client *client = to_i2c_client(dev);
662519 struct v4l2_subdev *sd = i2c_get_clientdata(client);
....@@ -677,16 +534,16 @@
677534 struct ov2685 *ov2685 = container_of(ctrl->handler,
678535 struct ov2685, ctrl_handler);
679536 struct i2c_client *client = ov2685->client;
680
- s64 max;
681
- int ret = 0;
537
+ s64 max_expo;
538
+ int ret;
682539
683540 /* Propagate change of current control to all related controls */
684541 switch (ctrl->id) {
685542 case V4L2_CID_VBLANK:
686543 /* Update max exposure while meeting expected vblanking */
687
- max = ov2685->cur_mode->height + ctrl->val - 4;
544
+ max_expo = ov2685->cur_mode->height + ctrl->val - 4;
688545 __v4l2_ctrl_modify_range(ov2685->exposure,
689
- ov2685->exposure->minimum, max,
546
+ ov2685->exposure->minimum, max_expo,
690547 ov2685->exposure->step,
691548 ov2685->exposure->default_value);
692549 break;
....@@ -694,82 +551,50 @@
694551
695552 if (!pm_runtime_get_if_in_use(&client->dev))
696553 return 0;
554
+
697555 switch (ctrl->id) {
698556 case V4L2_CID_EXPOSURE:
699
- ov2685_set_exposure(ov2685, ctrl->val);
557
+ ret = ov2685_write_reg(ov2685->client, OV2685_REG_EXPOSURE,
558
+ OV2685_REG_VALUE_24BIT, ctrl->val << 4);
700559 break;
701560 case V4L2_CID_ANALOGUE_GAIN:
702
- ov2685_set_gain(ov2685, ctrl->val);
561
+ ret = ov2685_write_reg(ov2685->client, OV2685_REG_GAIN,
562
+ OV2685_REG_VALUE_16BIT, ctrl->val);
703563 break;
704564 case V4L2_CID_VBLANK:
705
- ov2685_set_vts(ov2685, ctrl->val);
565
+ ret = ov2685_write_reg(ov2685->client, OV2685_REG_VTS,
566
+ OV2685_REG_VALUE_16BIT,
567
+ ctrl->val + ov2685->cur_mode->height);
706568 break;
707569 case V4L2_CID_TEST_PATTERN:
708
- ov2685_enable_test_pattern(ov2685, ctrl->val);
570
+ ret = ov2685_write_reg(ov2685->client, OV2685_REG_TEST_PATTERN,
571
+ OV2685_REG_VALUE_08BIT,
572
+ ov2685_test_pattern_val[ctrl->val]);
709573 break;
710574 default:
711575 dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
712576 __func__, ctrl->id, ctrl->val);
577
+ ret = -EINVAL;
713578 break;
714
- };
579
+ }
580
+
715581 pm_runtime_put(&client->dev);
716582
717583 return ret;
718584 }
719585
720
-static int ov2685_enum_frame_interval(struct v4l2_subdev *sd,
721
- struct v4l2_subdev_pad_config *cfg,
722
- struct v4l2_subdev_frame_interval_enum *fie)
723
-{
724
- if (fie->index >= ARRAY_SIZE(supported_modes))
725
- return -EINVAL;
726
-
727
- if (fie->code != MEDIA_BUS_FMT_SBGGR10_1X10)
728
- return -EINVAL;
729
-
730
- fie->width = supported_modes[fie->index].width;
731
- fie->height = supported_modes[fie->index].height;
732
- fie->interval = supported_modes[fie->index].max_fps;
733
- return 0;
734
-}
735
-
736
-static int ov2685_g_mbus_config(struct v4l2_subdev *sd,
737
- struct v4l2_mbus_config *config)
738
-{
739
- u32 val = 0;
740
-
741
- val = 1 << (OV2685_LANES - 1) |
742
- V4L2_MBUS_CSI2_CHANNEL_0 |
743
- V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
744
- config->type = V4L2_MBUS_CSI2;
745
- config->flags = val;
746
-
747
- return 0;
748
-}
749
-
750
-static struct v4l2_subdev_core_ops ov2685_core_ops = {
751
- .s_power = ov2685_s_power,
752
- .ioctl = ov2685_ioctl,
753
-#ifdef CONFIG_COMPAT
754
- .compat_ioctl32 = ov2685_compat_ioctl32,
755
-#endif
756
-};
757
-
758
-static struct v4l2_subdev_video_ops ov2685_video_ops = {
586
+static const struct v4l2_subdev_video_ops ov2685_video_ops = {
759587 .s_stream = ov2685_s_stream,
760
- .g_mbus_config = ov2685_g_mbus_config,
761588 };
762589
763
-static struct v4l2_subdev_pad_ops ov2685_pad_ops = {
590
+static const struct v4l2_subdev_pad_ops ov2685_pad_ops = {
764591 .enum_mbus_code = ov2685_enum_mbus_code,
765592 .enum_frame_size = ov2685_enum_frame_sizes,
766
- .enum_frame_interval = ov2685_enum_frame_interval,
767593 .get_fmt = ov2685_get_fmt,
768594 .set_fmt = ov2685_set_fmt,
769595 };
770596
771
-static struct v4l2_subdev_ops ov2685_subdev_ops = {
772
- .core = &ov2685_core_ops,
597
+static const struct v4l2_subdev_ops ov2685_subdev_ops = {
773598 .video = &ov2685_video_ops,
774599 .pad = &ov2685_pad_ops,
775600 };
....@@ -795,7 +620,7 @@
795620
796621 handler = &ov2685->ctrl_handler;
797622 mode = ov2685->cur_mode;
798
- ret = v4l2_ctrl_handler_init(handler, 1);
623
+ ret = v4l2_ctrl_handler_init(handler, 8);
799624 if (ret)
800625 return ret;
801626 handler->lock = &ov2685->mutex;
....@@ -838,31 +663,34 @@
838663 0, 0, ov2685_test_pattern_menu);
839664
840665 if (handler->error) {
841
- v4l2_ctrl_handler_free(handler);
842
- return handler->error;
666
+ ret = handler->error;
667
+ dev_err(&ov2685->client->dev,
668
+ "Failed to init controls(%d)\n", ret);
669
+ goto err_free_handler;
843670 }
844671
845672 ov2685->subdev.ctrl_handler = handler;
846673
847674 return 0;
675
+
676
+err_free_handler:
677
+ v4l2_ctrl_handler_free(handler);
678
+
679
+ return ret;
848680 }
849681
850682 static int ov2685_check_sensor_id(struct ov2685 *ov2685,
851683 struct i2c_client *client)
852684 {
853685 struct device *dev = &ov2685->client->dev;
854
- int id, ret;
686
+ int ret;
687
+ u32 id = 0;
855688
856
- ret = __ov2685_power_on(ov2685);
857
- if (ret)
858
- return ret;
859
- ov2685_read_reg(client, OV2685_REG_CHIP_ID,
860
- OV2685_REG_VALUE_16BIT, &id);
861
- __ov2685_power_off(ov2685);
862
-
689
+ ret = ov2685_read_reg(client, OV2685_REG_CHIP_ID,
690
+ OV2685_REG_VALUE_16BIT, &id);
863691 if (id != CHIP_ID) {
864
- dev_err(dev, "Wrong camera sensor id(%04x)\n", id);
865
- return -EINVAL;
692
+ dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
693
+ return ret;
866694 }
867695
868696 dev_info(dev, "Detected OV%04x sensor\n", CHIP_ID);
....@@ -870,37 +698,28 @@
870698 return 0;
871699 }
872700
701
+static int ov2685_configure_regulators(struct ov2685 *ov2685)
702
+{
703
+ int i;
704
+
705
+ for (i = 0; i < OV2685_NUM_SUPPLIES; i++)
706
+ ov2685->supplies[i].supply = ov2685_supply_names[i];
707
+
708
+ return devm_regulator_bulk_get(&ov2685->client->dev,
709
+ OV2685_NUM_SUPPLIES,
710
+ ov2685->supplies);
711
+}
712
+
873713 static int ov2685_probe(struct i2c_client *client,
874714 const struct i2c_device_id *id)
875715 {
876716 struct device *dev = &client->dev;
877
- struct device_node *node = dev->of_node;
878717 struct ov2685 *ov2685;
879
- struct v4l2_subdev *sd;
880
- char facing[2];
881718 int ret;
882
-
883
- dev_info(dev, "driver version: %02x.%02x.%02x",
884
- DRIVER_VERSION >> 16,
885
- (DRIVER_VERSION & 0xff00) >> 8,
886
- DRIVER_VERSION & 0x00ff);
887719
888720 ov2685 = devm_kzalloc(dev, sizeof(*ov2685), GFP_KERNEL);
889721 if (!ov2685)
890722 return -ENOMEM;
891
-
892
- ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
893
- &ov2685->module_index);
894
- ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
895
- &ov2685->module_facing);
896
- ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
897
- &ov2685->module_name);
898
- ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
899
- &ov2685->len_name);
900
- if (ret) {
901
- dev_err(dev, "could not get module information!\n");
902
- return -EINVAL;
903
- }
904723
905724 ov2685->client = client;
906725 ov2685->cur_mode = &supported_modes[0];
....@@ -910,6 +729,13 @@
910729 dev_err(dev, "Failed to get xvclk\n");
911730 return -EINVAL;
912731 }
732
+ ret = clk_set_rate(ov2685->xvclk, OV2685_XVCLK_FREQ);
733
+ if (ret < 0) {
734
+ dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
735
+ return ret;
736
+ }
737
+ if (clk_get_rate(ov2685->xvclk) != OV2685_XVCLK_FREQ)
738
+ dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
913739
914740 ov2685->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
915741 if (IS_ERR(ov2685->reset_gpio)) {
....@@ -917,66 +743,59 @@
917743 return -EINVAL;
918744 }
919745
920
- ov2685->avdd_regulator = devm_regulator_get(dev, "avdd");
921
- if (IS_ERR(ov2685->avdd_regulator)) {
922
- dev_err(dev, "Failed to get avdd-supply\n");
923
- return -EINVAL;
924
- }
925
- ov2685->dovdd_regulator = devm_regulator_get(dev, "dovdd");
926
- if (IS_ERR(ov2685->dovdd_regulator)) {
927
- dev_err(dev, "Failed to get dovdd-supply\n");
928
- return -EINVAL;
746
+ ret = ov2685_configure_regulators(ov2685);
747
+ if (ret) {
748
+ dev_err(dev, "Failed to get power regulators\n");
749
+ return ret;
929750 }
930751
931752 mutex_init(&ov2685->mutex);
932753 v4l2_i2c_subdev_init(&ov2685->subdev, client, &ov2685_subdev_ops);
933754 ret = ov2685_initialize_controls(ov2685);
934755 if (ret)
935
- goto destroy_mutex;
756
+ goto err_destroy_mutex;
757
+
758
+ ret = __ov2685_power_on(ov2685);
759
+ if (ret)
760
+ goto err_free_handler;
936761
937762 ret = ov2685_check_sensor_id(ov2685, client);
938763 if (ret)
939
- return ret;
764
+ goto err_power_off;
940765
941766 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
942767 ov2685->subdev.internal_ops = &ov2685_internal_ops;
768
+ ov2685->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
943769 #endif
944770 #if defined(CONFIG_MEDIA_CONTROLLER)
945771 ov2685->pad.flags = MEDIA_PAD_FL_SOURCE;
946
- ov2685->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
947
- V4L2_SUBDEV_FL_HAS_EVENTS;
948772 ov2685->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
949773 ret = media_entity_pads_init(&ov2685->subdev.entity, 1, &ov2685->pad);
950774 if (ret < 0)
951
- goto free_ctrl_handler;
775
+ goto err_power_off;
952776 #endif
953777
954
- sd = &ov2685->subdev;
955
- memset(facing, 0, sizeof(facing));
956
- if (strcmp(ov2685->module_facing, "back") == 0)
957
- facing[0] = 'b';
958
- else
959
- facing[0] = 'f';
960
-
961
- snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
962
- ov2685->module_index, facing,
963
- OV2685_NAME, dev_name(sd->dev));
964
- ret = v4l2_async_register_subdev_sensor_common(sd);
778
+ ret = v4l2_async_register_subdev(&ov2685->subdev);
965779 if (ret) {
966780 dev_err(dev, "v4l2 async register subdev failed\n");
967
- goto clean_entity;
781
+ goto err_clean_entity;
968782 }
969783
784
+ pm_runtime_set_active(dev);
970785 pm_runtime_enable(dev);
786
+ pm_runtime_idle(dev);
787
+
971788 return 0;
972789
973
-clean_entity:
790
+err_clean_entity:
974791 #if defined(CONFIG_MEDIA_CONTROLLER)
975792 media_entity_cleanup(&ov2685->subdev.entity);
976793 #endif
977
-free_ctrl_handler:
794
+err_power_off:
795
+ __ov2685_power_off(ov2685);
796
+err_free_handler:
978797 v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
979
-destroy_mutex:
798
+err_destroy_mutex:
980799 mutex_destroy(&ov2685->mutex);
981800
982801 return ret;
....@@ -984,28 +803,37 @@
984803
985804 static int ov2685_remove(struct i2c_client *client)
986805 {
987
- struct ov2685 *ov2685 = i2c_get_clientdata(client);
806
+ struct v4l2_subdev *sd = i2c_get_clientdata(client);
807
+ struct ov2685 *ov2685 = to_ov2685(sd);
988808
989
- __ov2685_power_off(ov2685);
990
- v4l2_async_unregister_subdev(&ov2685->subdev);
991
- media_entity_cleanup(&ov2685->subdev.entity);
809
+ v4l2_async_unregister_subdev(sd);
810
+#if defined(CONFIG_MEDIA_CONTROLLER)
811
+ media_entity_cleanup(&sd->entity);
812
+#endif
992813 v4l2_ctrl_handler_free(&ov2685->ctrl_handler);
993814 mutex_destroy(&ov2685->mutex);
815
+
816
+ pm_runtime_disable(&client->dev);
817
+ if (!pm_runtime_status_suspended(&client->dev))
818
+ __ov2685_power_off(ov2685);
819
+ pm_runtime_set_suspended(&client->dev);
994820
995821 return 0;
996822 }
997823
824
+#if IS_ENABLED(CONFIG_OF)
998825 static const struct of_device_id ov2685_of_match[] = {
999826 { .compatible = "ovti,ov2685" },
1000827 {},
1001828 };
829
+MODULE_DEVICE_TABLE(of, ov2685_of_match);
830
+#endif
1002831
1003832 static struct i2c_driver ov2685_i2c_driver = {
1004833 .driver = {
1005
- .name = OV2685_NAME,
1006
- .owner = THIS_MODULE,
834
+ .name = "ov2685",
1007835 .pm = &ov2685_pm_ops,
1008
- .of_match_table = ov2685_of_match
836
+ .of_match_table = of_match_ptr(ov2685_of_match),
1009837 },
1010838 .probe = &ov2685_probe,
1011839 .remove = &ov2685_remove,