diff options
Diffstat (limited to 'drivers/media/i2c/dw9714.c')
| -rw-r--r-- | drivers/media/i2c/dw9714.c | 108 |
1 files changed, 93 insertions, 15 deletions
diff --git a/drivers/media/i2c/dw9714.c b/drivers/media/i2c/dw9714.c index c8b4292512dc..1e7ad355a388 100644 --- a/drivers/media/i2c/dw9714.c +++ b/drivers/media/i2c/dw9714.c @@ -2,11 +2,14 @@ // Copyright (c) 2015--2017 Intel Corporation. #include <linux/delay.h> +#include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/module.h> #include <linux/pm_runtime.h> +#include <linux/regulator/consumer.h> #include <media/v4l2-ctrls.h> #include <media/v4l2-device.h> +#include <media/v4l2-event.h> #define DW9714_NAME "dw9714" #define DW9714_MAX_FOCUS_POS 1023 @@ -35,6 +38,8 @@ struct dw9714_device { struct v4l2_ctrl_handler ctrls_vcm; struct v4l2_subdev sd; u16 current_val; + struct regulator *vcc; + struct gpio_desc *powerdown_gpio; }; static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl) @@ -100,7 +105,15 @@ static const struct v4l2_subdev_internal_ops dw9714_int_ops = { .close = dw9714_close, }; -static const struct v4l2_subdev_ops dw9714_ops = { }; +static const struct v4l2_subdev_core_ops dw9714_core_ops = { + .log_status = v4l2_ctrl_subdev_log_status, + .subscribe_event = v4l2_ctrl_subdev_subscribe_event, + .unsubscribe_event = v4l2_event_subdev_unsubscribe, +}; + +static const struct v4l2_subdev_ops dw9714_ops = { + .core = &dw9714_core_ops, +}; static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev) { @@ -126,6 +139,28 @@ static int dw9714_init_controls(struct dw9714_device *dev_vcm) return hdl->error; } +static int dw9714_power_up(struct dw9714_device *dw9714_dev) +{ + int ret; + + ret = regulator_enable(dw9714_dev->vcc); + if (ret) + return ret; + + gpiod_set_value_cansleep(dw9714_dev->powerdown_gpio, 0); + + usleep_range(1000, 2000); + + return 0; +} + +static int dw9714_power_down(struct dw9714_device *dw9714_dev) +{ + gpiod_set_value_cansleep(dw9714_dev->powerdown_gpio, 1); + + return regulator_disable(dw9714_dev->vcc); +} + static int dw9714_probe(struct i2c_client *client) { struct dw9714_device *dw9714_dev; @@ -133,11 +168,29 @@ static int dw9714_probe(struct i2c_client *client) dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev), GFP_KERNEL); - if (dw9714_dev == NULL) + if (!dw9714_dev) return -ENOMEM; + dw9714_dev->vcc = devm_regulator_get(&client->dev, "vcc"); + if (IS_ERR(dw9714_dev->vcc)) + return PTR_ERR(dw9714_dev->vcc); + + dw9714_dev->powerdown_gpio = devm_gpiod_get_optional(&client->dev, + "powerdown", + GPIOD_OUT_HIGH); + if (IS_ERR(dw9714_dev->powerdown_gpio)) + return dev_err_probe(&client->dev, + PTR_ERR(dw9714_dev->powerdown_gpio), + "could not get powerdown gpio\n"); + + rval = dw9714_power_up(dw9714_dev); + if (rval) + return dev_err_probe(&client->dev, rval, + "failed to power up: %d\n", rval); + v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops); - dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; + dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | + V4L2_SUBDEV_FL_HAS_EVENTS; dw9714_dev->sd.internal_ops = &dw9714_int_ops; rval = dw9714_init_controls(dw9714_dev); @@ -161,21 +214,29 @@ static int dw9714_probe(struct i2c_client *client) return 0; err_cleanup: + dw9714_power_down(dw9714_dev); v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm); media_entity_cleanup(&dw9714_dev->sd.entity); return rval; } -static int dw9714_remove(struct i2c_client *client) +static void 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 = dw9714_power_down(dw9714_dev); + if (ret) { + dev_err(&client->dev, + "Failed to power down: %d\n", ret); + } + } + pm_runtime_set_suspended(&client->dev); dw9714_subdev_cleanup(dw9714_dev); - - return 0; } /* @@ -190,6 +251,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, @@ -198,7 +262,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 = dw9714_power_down(dw9714_dev); + if (ret) + dev_err(dev, "Failed to power down: %d\n", ret); + + return ret; } /* @@ -207,13 +276,22 @@ static int __maybe_unused dw9714_vcm_suspend(struct device *dev) * The lens position is gradually moved in units of DW9714_CTRL_STEPS, * to make the movements smoothly. */ -static int __maybe_unused dw9714_vcm_resume(struct device *dev) +static int __maybe_unused dw9714_vcm_resume(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); struct v4l2_subdev *sd = i2c_get_clientdata(client); struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); int ret, val; + if (pm_runtime_suspended(&client->dev)) + return 0; + + ret = dw9714_power_up(dw9714_dev); + if (ret) { + dev_err(dev, "Failed to power up: %d\n", ret); + return ret; + } + for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS; val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1; val += DW9714_CTRL_STEPS) { @@ -221,7 +299,7 @@ static int __maybe_unused dw9714_vcm_resume(struct device *dev) DW9714_VAL(val, DW9714_DEFAULT_S)); if (ret) dev_err_ratelimited(dev, "%s I2C failure: %d", - __func__, ret); + __func__, ret); usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); } @@ -229,8 +307,8 @@ static int __maybe_unused dw9714_vcm_resume(struct device *dev) } static const struct i2c_device_id dw9714_id_table[] = { - { DW9714_NAME, 0 }, - { { 0 } } + { DW9714_NAME }, + { } }; MODULE_DEVICE_TABLE(i2c, dw9714_id_table); @@ -251,7 +329,7 @@ static struct i2c_driver dw9714_i2c_driver = { .pm = &dw9714_pm_ops, .of_match_table = dw9714_of_table, }, - .probe_new = dw9714_probe, + .probe = dw9714_probe, .remove = dw9714_remove, .id_table = dw9714_id_table, }; @@ -260,8 +338,8 @@ module_i2c_driver(dw9714_i2c_driver); MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>"); MODULE_AUTHOR("Jian Xu Zheng"); -MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>"); -MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>"); -MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>"); +MODULE_AUTHOR("Yuning Pu"); +MODULE_AUTHOR("Jouni Ukkonen"); +MODULE_AUTHOR("Tommi Franttila"); MODULE_DESCRIPTION("DW9714 VCM driver"); MODULE_LICENSE("GPL v2"); |
