From: Angus Ainslie Date: Thu, 20 Jan 2022 11:14:53 +0000 (+0100) Subject: media: i2c: dw9714: add optional regulator support X-Git-Tag: v6.1-rc5~1763^2~170 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=932de2cd49943faf03b2c99eaebcd30c402bed0c;p=platform%2Fkernel%2Flinux-starfive.git media: i2c: dw9714: add optional regulator support Allow the dw9714 to control a regulator and adjust suspend() and resume() to support both runtime and system pm. Signed-off-by: Angus Ainslie Signed-off-by: Martin Kepplinger Signed-off-by: Sakari Ailus Signed-off-by: Mauro Carvalho Chehab --- diff --git a/drivers/media/i2c/dw9714.c b/drivers/media/i2c/dw9714.c index 3863dfe..cd7008a 100644 --- a/drivers/media/i2c/dw9714.c +++ b/drivers/media/i2c/dw9714.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,7 @@ struct dw9714_device { struct v4l2_ctrl_handler ctrls_vcm; struct v4l2_subdev sd; u16 current_val; + struct regulator *vcc; }; static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl) @@ -145,6 +147,16 @@ static int dw9714_probe(struct i2c_client *client) if (dw9714_dev == NULL) return -ENOMEM; + dw9714_dev->vcc = devm_regulator_get(&client->dev, "vcc"); + if (IS_ERR(dw9714_dev->vcc)) + return PTR_ERR(dw9714_dev->vcc); + + rval = regulator_enable(dw9714_dev->vcc); + if (rval < 0) { + dev_err(&client->dev, "failed to enable vcc: %d\n", rval); + return rval; + } + v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops); dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; @@ -181,8 +193,18 @@ static int dw9714_remove(struct i2c_client *client) { struct v4l2_subdev *sd = i2c_get_clientdata(client); struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); + int ret; pm_runtime_disable(&client->dev); + if (!pm_runtime_status_suspended(&client->dev)) { + ret = regulator_disable(dw9714_dev->vcc); + if (ret) { + dev_err(&client->dev, + "Failed to disable vcc: %d\n", ret); + return ret; + } + } + pm_runtime_set_suspended(&client->dev); dw9714_subdev_cleanup(dw9714_dev); return 0; @@ -200,6 +222,9 @@ static int __maybe_unused dw9714_vcm_suspend(struct device *dev) struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); int ret, val; + if (pm_runtime_suspended(&client->dev)) + return 0; + for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1); val >= 0; val -= DW9714_CTRL_STEPS) { ret = dw9714_i2c_write(client, @@ -208,7 +233,12 @@ static int __maybe_unused dw9714_vcm_suspend(struct device *dev) dev_err_once(dev, "%s I2C failure: %d", __func__, ret); usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); } - return 0; + + ret = regulator_disable(dw9714_dev->vcc); + if (ret) + dev_err(dev, "Failed to disable vcc: %d\n", ret); + + return ret; } /* @@ -224,6 +254,16 @@ static int __maybe_unused dw9714_vcm_resume(struct device *dev) struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); int ret, val; + if (pm_runtime_suspended(&client->dev)) + return 0; + + ret = regulator_enable(dw9714_dev->vcc); + if (ret) { + dev_err(dev, "Failed to enable vcc: %d\n", ret); + return ret; + } + usleep_range(1000, 2000); + for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS; val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1; val += DW9714_CTRL_STEPS) {