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
6 commit 90f7e76eac50c1ae54a445abc6a286837ade46cf upstream
8 Make the driver get needed regulators on probe and enable/disable
9 them on runtime PM callbacks.
11 Signed-off-by: Yassine Oudjana <y.oudjana@protonmail.com>
12 Tested-by: Umang Jain <umang.jain@ideasonboard.com>
14 drivers/media/i2c/ak7375.c | 38 ++++++++++++++++++++++++++++++++++++++
15 1 file changed, 38 insertions(+)
17 --- a/drivers/media/i2c/ak7375.c
18 +++ b/drivers/media/i2c/ak7375.c
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>
29 #define AK7375_CTRL_STEPS 64
30 #define AK7375_CTRL_DELAY_US 1000
32 + * The vcm may take up 10 ms (tDELAY) to power on and start taking
33 + * I2C messages. Based on AK7371 datasheet.
35 +#define AK7375_POWER_DELAY_US 10000
37 #define AK7375_REG_POSITION 0x0
38 #define AK7375_REG_CONT 0x2
39 #define AK7375_MODE_ACTIVE 0x0
40 #define AK7375_MODE_STANDBY 0x40
42 +static const char * const ak7375_supply_names[] = {
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)];
54 /* active or standby mode */
57 @@ -133,12 +146,24 @@ static int ak7375_probe(struct i2c_clien
59 struct ak7375_device *ak7375_dev;
63 ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
68 + for (i = 0; i < ARRAY_SIZE(ak7375_supply_names); i++)
69 + ak7375_dev->supplies[i].supply = ak7375_supply_names[i];
71 + ret = devm_regulator_bulk_get(&client->dev,
72 + ARRAY_SIZE(ak7375_supply_names),
73 + ak7375_dev->supplies);
75 + dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
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
84 dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
86 + ret = regulator_bulk_disable(ARRAY_SIZE(ak7375_supply_names),
87 + ak7375_dev->supplies);
91 ak7375_dev->active = false;
94 @@ -228,6 +258,14 @@ static int __maybe_unused ak7375_vcm_res
95 if (ak7375_dev->active)
98 + ret = regulator_bulk_enable(ARRAY_SIZE(ak7375_supply_names),
99 + ak7375_dev->supplies);
103 + /* Wait for vcm to become ready */
104 + usleep_range(AK7375_POWER_DELAY_US, AK7375_POWER_DELAY_US + 500);
106 ret = ak7375_i2c_write(ak7375_dev, AK7375_REG_CONT,
107 AK7375_MODE_ACTIVE, 1);