gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / media / i2c / ad5820.c
blob19c74db0649fc04ef56f5c287dc87045c1e46664
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/media/i2c/ad5820.c
5 * AD5820 DAC driver for camera voice coil focus.
7 * Copyright (C) 2008 Nokia Corporation
8 * Copyright (C) 2007 Texas Instruments
9 * Copyright (C) 2016 Pavel Machek <pavel@ucw.cz>
11 * Contact: Tuukka Toivonen <tuukkat76@gmail.com>
12 * Sakari Ailus <sakari.ailus@iki.fi>
14 * Based on af_d88.c by Texas Instruments.
17 #include <linux/errno.h>
18 #include <linux/i2c.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/gpio/consumer.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-subdev.h>
28 /* Register definitions */
29 #define AD5820_POWER_DOWN (1 << 15)
30 #define AD5820_DAC_SHIFT 4
31 #define AD5820_RAMP_MODE_LINEAR (0 << 3)
32 #define AD5820_RAMP_MODE_64_16 (1 << 3)
34 #define CODE_TO_RAMP_US(s) ((s) == 0 ? 0 : (1 << ((s) - 1)) * 50)
35 #define RAMP_US_TO_CODE(c) fls(((c) + ((c)>>1)) / 50)
37 #define to_ad5820_device(sd) container_of(sd, struct ad5820_device, subdev)
39 struct ad5820_device {
40 struct v4l2_subdev subdev;
41 struct ad5820_platform_data *platform_data;
42 struct regulator *vana;
44 struct v4l2_ctrl_handler ctrls;
45 u32 focus_absolute;
46 u32 focus_ramp_time;
47 u32 focus_ramp_mode;
49 struct gpio_desc *enable_gpio;
51 struct mutex power_lock;
52 int power_count;
54 bool standby;
57 static int ad5820_write(struct ad5820_device *coil, u16 data)
59 struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
60 struct i2c_msg msg;
61 __be16 be_data;
62 int r;
64 if (!client->adapter)
65 return -ENODEV;
67 be_data = cpu_to_be16(data);
68 msg.addr = client->addr;
69 msg.flags = 0;
70 msg.len = 2;
71 msg.buf = (u8 *)&be_data;
73 r = i2c_transfer(client->adapter, &msg, 1);
74 if (r < 0) {
75 dev_err(&client->dev, "write failed, error %d\n", r);
76 return r;
79 return 0;
83 * Calculate status word and write it to the device based on current
84 * values of V4L2 controls. It is assumed that the stored V4L2 control
85 * values are properly limited and rounded.
87 static int ad5820_update_hw(struct ad5820_device *coil)
89 u16 status;
91 status = RAMP_US_TO_CODE(coil->focus_ramp_time);
92 status |= coil->focus_ramp_mode
93 ? AD5820_RAMP_MODE_64_16 : AD5820_RAMP_MODE_LINEAR;
94 status |= coil->focus_absolute << AD5820_DAC_SHIFT;
96 if (coil->standby)
97 status |= AD5820_POWER_DOWN;
99 return ad5820_write(coil, status);
103 * Power handling
105 static int ad5820_power_off(struct ad5820_device *coil, bool standby)
107 int ret = 0, ret2;
110 * Go to standby first as real power off my be denied by the hardware
111 * (single power line control for both coil and sensor).
113 if (standby) {
114 coil->standby = true;
115 ret = ad5820_update_hw(coil);
118 gpiod_set_value_cansleep(coil->enable_gpio, 0);
120 ret2 = regulator_disable(coil->vana);
121 if (ret)
122 return ret;
123 return ret2;
126 static int ad5820_power_on(struct ad5820_device *coil, bool restore)
128 int ret;
130 ret = regulator_enable(coil->vana);
131 if (ret < 0)
132 return ret;
134 gpiod_set_value_cansleep(coil->enable_gpio, 1);
136 if (restore) {
137 /* Restore the hardware settings. */
138 coil->standby = false;
139 ret = ad5820_update_hw(coil);
140 if (ret)
141 goto fail;
143 return 0;
145 fail:
146 gpiod_set_value_cansleep(coil->enable_gpio, 0);
147 coil->standby = true;
148 regulator_disable(coil->vana);
150 return ret;
154 * V4L2 controls
156 static int ad5820_set_ctrl(struct v4l2_ctrl *ctrl)
158 struct ad5820_device *coil =
159 container_of(ctrl->handler, struct ad5820_device, ctrls);
161 switch (ctrl->id) {
162 case V4L2_CID_FOCUS_ABSOLUTE:
163 coil->focus_absolute = ctrl->val;
164 return ad5820_update_hw(coil);
167 return 0;
170 static const struct v4l2_ctrl_ops ad5820_ctrl_ops = {
171 .s_ctrl = ad5820_set_ctrl,
175 static int ad5820_init_controls(struct ad5820_device *coil)
177 v4l2_ctrl_handler_init(&coil->ctrls, 1);
180 * V4L2_CID_FOCUS_ABSOLUTE
182 * Minimum current is 0 mA, maximum is 100 mA. Thus, 1 code is
183 * equivalent to 100/1023 = 0.0978 mA. Nevertheless, we do not use [mA]
184 * for focus position, because it is meaningless for user. Meaningful
185 * would be to use focus distance or even its inverse, but since the
186 * driver doesn't have sufficiently knowledge to do the conversion, we
187 * will just use abstract codes here. In any case, smaller value = focus
188 * position farther from camera. The default zero value means focus at
189 * infinity, and also least current consumption.
191 v4l2_ctrl_new_std(&coil->ctrls, &ad5820_ctrl_ops,
192 V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
194 if (coil->ctrls.error)
195 return coil->ctrls.error;
197 coil->focus_absolute = 0;
198 coil->focus_ramp_time = 0;
199 coil->focus_ramp_mode = 0;
201 coil->subdev.ctrl_handler = &coil->ctrls;
203 return 0;
207 * V4L2 subdev operations
209 static int ad5820_registered(struct v4l2_subdev *subdev)
211 struct ad5820_device *coil = to_ad5820_device(subdev);
213 return ad5820_init_controls(coil);
216 static int
217 ad5820_set_power(struct v4l2_subdev *subdev, int on)
219 struct ad5820_device *coil = to_ad5820_device(subdev);
220 int ret = 0;
222 mutex_lock(&coil->power_lock);
225 * If the power count is modified from 0 to != 0 or from != 0 to 0,
226 * update the power state.
228 if (coil->power_count == !on) {
229 ret = on ? ad5820_power_on(coil, true) :
230 ad5820_power_off(coil, true);
231 if (ret < 0)
232 goto done;
235 /* Update the power count. */
236 coil->power_count += on ? 1 : -1;
237 WARN_ON(coil->power_count < 0);
239 done:
240 mutex_unlock(&coil->power_lock);
241 return ret;
244 static int ad5820_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
246 return ad5820_set_power(sd, 1);
249 static int ad5820_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
251 return ad5820_set_power(sd, 0);
254 static const struct v4l2_subdev_core_ops ad5820_core_ops = {
255 .s_power = ad5820_set_power,
258 static const struct v4l2_subdev_ops ad5820_ops = {
259 .core = &ad5820_core_ops,
262 static const struct v4l2_subdev_internal_ops ad5820_internal_ops = {
263 .registered = ad5820_registered,
264 .open = ad5820_open,
265 .close = ad5820_close,
269 * I2C driver
271 static int __maybe_unused ad5820_suspend(struct device *dev)
273 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
274 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
275 struct ad5820_device *coil = to_ad5820_device(subdev);
277 if (!coil->power_count)
278 return 0;
280 return ad5820_power_off(coil, false);
283 static int __maybe_unused ad5820_resume(struct device *dev)
285 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
286 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
287 struct ad5820_device *coil = to_ad5820_device(subdev);
289 if (!coil->power_count)
290 return 0;
292 return ad5820_power_on(coil, true);
295 static int ad5820_probe(struct i2c_client *client,
296 const struct i2c_device_id *devid)
298 struct ad5820_device *coil;
299 int ret;
301 coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
302 if (!coil)
303 return -ENOMEM;
305 coil->vana = devm_regulator_get(&client->dev, "VANA");
306 if (IS_ERR(coil->vana)) {
307 ret = PTR_ERR(coil->vana);
308 if (ret != -EPROBE_DEFER)
309 dev_err(&client->dev, "could not get regulator for vana\n");
310 return ret;
313 coil->enable_gpio = devm_gpiod_get_optional(&client->dev, "enable",
314 GPIOD_OUT_LOW);
315 if (IS_ERR(coil->enable_gpio)) {
316 ret = PTR_ERR(coil->enable_gpio);
317 if (ret != -EPROBE_DEFER)
318 dev_err(&client->dev, "could not get enable gpio\n");
319 return ret;
322 mutex_init(&coil->power_lock);
324 v4l2_i2c_subdev_init(&coil->subdev, client, &ad5820_ops);
325 coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
326 coil->subdev.internal_ops = &ad5820_internal_ops;
327 coil->subdev.entity.function = MEDIA_ENT_F_LENS;
328 strscpy(coil->subdev.name, "ad5820 focus", sizeof(coil->subdev.name));
330 ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
331 if (ret < 0)
332 goto cleanup2;
334 ret = v4l2_async_register_subdev(&coil->subdev);
335 if (ret < 0)
336 goto cleanup;
338 return ret;
340 cleanup2:
341 mutex_destroy(&coil->power_lock);
342 cleanup:
343 media_entity_cleanup(&coil->subdev.entity);
344 return ret;
347 static int ad5820_remove(struct i2c_client *client)
349 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
350 struct ad5820_device *coil = to_ad5820_device(subdev);
352 v4l2_async_unregister_subdev(&coil->subdev);
353 v4l2_ctrl_handler_free(&coil->ctrls);
354 media_entity_cleanup(&coil->subdev.entity);
355 mutex_destroy(&coil->power_lock);
356 return 0;
359 static const struct i2c_device_id ad5820_id_table[] = {
360 { "ad5820", 0 },
361 { "ad5821", 0 },
362 { "ad5823", 0 },
365 MODULE_DEVICE_TABLE(i2c, ad5820_id_table);
367 static const struct of_device_id ad5820_of_table[] = {
368 { .compatible = "adi,ad5820" },
369 { .compatible = "adi,ad5821" },
370 { .compatible = "adi,ad5823" },
373 MODULE_DEVICE_TABLE(of, ad5820_of_table);
375 static SIMPLE_DEV_PM_OPS(ad5820_pm, ad5820_suspend, ad5820_resume);
377 static struct i2c_driver ad5820_i2c_driver = {
378 .driver = {
379 .name = "ad5820",
380 .pm = &ad5820_pm,
381 .of_match_table = ad5820_of_table,
383 .probe = ad5820_probe,
384 .remove = ad5820_remove,
385 .id_table = ad5820_id_table,
388 module_i2c_driver(ad5820_i2c_driver);
390 MODULE_AUTHOR("Tuukka Toivonen");
391 MODULE_DESCRIPTION("AD5820 camera lens driver");
392 MODULE_LICENSE("GPL");