kernel: bump 6.1 to 6.1.66
[openwrt/staging/stintel.git] / target / linux / bcm27xx / patches-6.1 / 950-0562-media-i2c-ak7375-Add-regulator-management.patch
1 From 159ef6a1740ded1bfd542bab7d69208cf0865814 Mon Sep 17 00:00:00 2001
2 From: Yassine Oudjana <y.oudjana@protonmail.com>
3 Date: Fri, 9 Dec 2022 17:37:41 +0300
4 Subject: [PATCH] media: i2c: ak7375: Add regulator management
5
6 commit 90f7e76eac50c1ae54a445abc6a286837ade46cf upstream
7
8 Make the driver get needed regulators on probe and enable/disable
9 them on runtime PM callbacks.
10
11 Signed-off-by: Yassine Oudjana <y.oudjana@protonmail.com>
12 Tested-by: Umang Jain <umang.jain@ideasonboard.com>
13 ---
14 drivers/media/i2c/ak7375.c | 38 ++++++++++++++++++++++++++++++++++++++
15 1 file changed, 38 insertions(+)
16
17 --- a/drivers/media/i2c/ak7375.c
18 +++ b/drivers/media/i2c/ak7375.c
19 @@ -6,6 +6,7 @@
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
23 +#include <linux/regulator/consumer.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26
27 @@ -23,17 +24,29 @@
28 */
29 #define AK7375_CTRL_STEPS 64
30 #define AK7375_CTRL_DELAY_US 1000
31 +/*
32 + * The vcm may take up 10 ms (tDELAY) to power on and start taking
33 + * I2C messages. Based on AK7371 datasheet.
34 + */
35 +#define AK7375_POWER_DELAY_US 10000
36
37 #define AK7375_REG_POSITION 0x0
38 #define AK7375_REG_CONT 0x2
39 #define AK7375_MODE_ACTIVE 0x0
40 #define AK7375_MODE_STANDBY 0x40
41
42 +static const char * const ak7375_supply_names[] = {
43 + "vdd",
44 + "vio",
45 +};
46 +
47 /* ak7375 device structure */
48 struct ak7375_device {
49 struct v4l2_ctrl_handler ctrls_vcm;
50 struct v4l2_subdev sd;
51 struct v4l2_ctrl *focus;
52 + struct regulator_bulk_data supplies[ARRAY_SIZE(ak7375_supply_names)];
53 +
54 /* active or standby mode */
55 bool active;
56 };
57 @@ -133,12 +146,24 @@ static int ak7375_probe(struct i2c_clien
58 {
59 struct ak7375_device *ak7375_dev;
60 int ret;
61 + unsigned int i;
62
63 ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
64 GFP_KERNEL);
65 if (!ak7375_dev)
66 return -ENOMEM;
67
68 + for (i = 0; i < ARRAY_SIZE(ak7375_supply_names); i++)
69 + ak7375_dev->supplies[i].supply = ak7375_supply_names[i];
70 +
71 + ret = devm_regulator_bulk_get(&client->dev,
72 + ARRAY_SIZE(ak7375_supply_names),
73 + ak7375_dev->supplies);
74 + if (ret) {
75 + dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
76 + return ret;
77 + }
78 +
79 v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops);
80 ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
81 ak7375_dev->sd.internal_ops = &ak7375_int_ops;
82 @@ -208,6 +233,11 @@ static int __maybe_unused ak7375_vcm_sus
83 if (ret)
84 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
85
86 + ret = regulator_bulk_disable(ARRAY_SIZE(ak7375_supply_names),
87 + ak7375_dev->supplies);
88 + if (ret)
89 + return ret;
90 +
91 ak7375_dev->active = false;
92
93 return 0;
94 @@ -228,6 +258,14 @@ static int __maybe_unused ak7375_vcm_res
95 if (ak7375_dev->active)
96 return 0;
97
98 + ret = regulator_bulk_enable(ARRAY_SIZE(ak7375_supply_names),
99 + ak7375_dev->supplies);
100 + if (ret)
101 + return ret;
102 +
103 + /* Wait for vcm to become ready */
104 + usleep_range(AK7375_POWER_DELAY_US, AK7375_POWER_DELAY_US + 500);
105 +
106 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
107 AK7375_MODE_ACTIVE, 1);
108 if (ret) {