Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / media / i2c / ad5398_vcm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AD5398 DAC driver for camera voice coil focus.
4  * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
5  *
6  * Based on AD5820 DAC driver by Nokia and TI.
7  *
8  * This driver uses the regulator framework notification hooks on the
9  * assumption that the VCM and sensor share a regulator. This means the VCM
10  * position will be restored when either the sensor or VCM subdevices are opened
11  * or powered up. The client can therefore choose to ignore the VCM subdevice,
12  * and the lens position will be as previously requested. Without that, there
13  * is a hard requirement to have the VCM subdevice open in order for the VCM
14  * to be powered and at the requested position.
15  */
16
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>
23
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-subdev.h>
27
28 /* Register definitions */
29 #define AD5398_POWER_DOWN               BIT(15)
30 #define AD5398_DAC_SHIFT                4
31
32 #define to_ad5398_device(sd)    container_of(sd, struct ad5398_device, subdev)
33
34 struct ad5398_device {
35         struct v4l2_subdev subdev;
36         struct ad5398_platform_data *platform_data;
37         struct regulator *vana;
38         struct notifier_block nb;
39
40         struct v4l2_ctrl_handler ctrls;
41         u32 focus_absolute;
42
43         bool standby;
44 };
45
46 static int ad5398_write(struct ad5398_device *coil, u16 data)
47 {
48         struct i2c_client *client = v4l2_get_subdevdata(&coil->subdev);
49         struct i2c_msg msg;
50         __be16 be_data;
51         int r;
52
53         if (!client->adapter)
54                 return -ENODEV;
55
56         be_data = cpu_to_be16(data);
57         msg.addr  = client->addr;
58         msg.flags = 0;
59         msg.len   = 2;
60         msg.buf   = (u8 *)&be_data;
61
62         r = i2c_transfer(client->adapter, &msg, 1);
63         if (r < 0) {
64                 dev_err(&client->dev, "write failed, error %d\n", r);
65                 return r;
66         }
67
68         return 0;
69 }
70
71 /*
72  * Calculate status word and write it to the device based on current
73  * values of V4L2 controls. It is assumed that the stored V4L2 control
74  * values are properly limited and rounded.
75  */
76 static int ad5398_update_hw(struct ad5398_device *coil)
77 {
78         u16 status;
79
80         status = coil->focus_absolute << AD5398_DAC_SHIFT;
81
82         if (coil->standby)
83                 status |= AD5398_POWER_DOWN;
84
85         return ad5398_write(coil, status);
86 }
87
88 /*
89  * Power handling
90  */
91 static int ad5398_power_off(struct ad5398_device *coil)
92 {
93         int ret = 0;
94
95         coil->standby = true;
96         ret = ad5398_update_hw(coil);
97
98         return ret;
99 }
100
101 static int ad5398_power_on(struct ad5398_device *coil)
102 {
103         int ret;
104
105         /* Restore the hardware settings. */
106         coil->standby = false;
107         ret = ad5398_update_hw(coil);
108         if (ret)
109                 goto fail;
110
111         return 0;
112
113 fail:
114         coil->standby = true;
115
116         return ret;
117 }
118
119 /*
120  * V4L2 controls
121  */
122 static int ad5398_set_ctrl(struct v4l2_ctrl *ctrl)
123 {
124         struct ad5398_device *coil =
125                 container_of(ctrl->handler, struct ad5398_device, ctrls);
126
127         switch (ctrl->id) {
128         case V4L2_CID_FOCUS_ABSOLUTE:
129                 coil->focus_absolute = ctrl->val;
130                 return ad5398_update_hw(coil);
131         }
132
133         return 0;
134 }
135
136 static const struct v4l2_ctrl_ops ad5398_ctrl_ops = {
137         .s_ctrl = ad5398_set_ctrl,
138 };
139
140 static int ad5398_init_controls(struct ad5398_device *coil)
141 {
142         v4l2_ctrl_handler_init(&coil->ctrls, 1);
143
144         /*
145          * V4L2_CID_FOCUS_ABSOLUTE
146          *
147          * Minimum current is 0 mA, maximum is 120 mA. Thus, 1 code is
148          * equivalent to 120/1023 = 0.1173 mA. Nevertheless, we do not use [mA]
149          * for focus position, because it is meaningless for user. Meaningful
150          * would be to use focus distance or even its inverse, but since the
151          * driver doesn't have sufficient knowledge to do the conversion, we
152          * will just use abstract codes here. In any case, smaller value = focus
153          * position farther from camera. The default zero value means focus at
154          * infinity, and also least current consumption.
155          */
156         v4l2_ctrl_new_std(&coil->ctrls, &ad5398_ctrl_ops,
157                           V4L2_CID_FOCUS_ABSOLUTE, 0, 1023, 1, 0);
158
159         if (coil->ctrls.error)
160                 return coil->ctrls.error;
161
162         coil->focus_absolute = 0;
163
164         coil->subdev.ctrl_handler = &coil->ctrls;
165
166         return 0;
167 }
168
169 /*
170  * V4L2 subdev operations
171  */
172 static int ad5398_registered(struct v4l2_subdev *subdev)
173 {
174         struct ad5398_device *coil = to_ad5398_device(subdev);
175
176         return ad5398_init_controls(coil);
177 }
178
179 static int
180 ad5398_set_power(struct v4l2_subdev *subdev, int on)
181 {
182         struct ad5398_device *coil = to_ad5398_device(subdev);
183         int ret;
184
185         if (on)
186                 ret = regulator_enable(coil->vana);
187         else
188                 ret = regulator_disable(coil->vana);
189
190         return ret;
191 }
192
193 static int ad5398_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
194 {
195         struct ad5398_device *coil = to_ad5398_device(sd);
196
197         return regulator_enable(coil->vana);
198 }
199
200 static int ad5398_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
201 {
202         struct ad5398_device *coil = to_ad5398_device(sd);
203
204         return regulator_disable(coil->vana);
205 }
206
207 static const struct v4l2_subdev_core_ops ad5398_core_ops = {
208         .s_power = ad5398_set_power,
209 };
210
211 static const struct v4l2_subdev_ops ad5398_ops = {
212         .core = &ad5398_core_ops,
213 };
214
215 static const struct v4l2_subdev_internal_ops ad5398_internal_ops = {
216         .registered = ad5398_registered,
217         .open = ad5398_open,
218         .close = ad5398_close,
219 };
220
221 /*
222  * I2C driver
223  */
224 static int __maybe_unused ad5398_suspend(struct device *dev)
225 {
226         struct i2c_client *client = container_of(dev, struct i2c_client, dev);
227         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
228         struct ad5398_device *coil = to_ad5398_device(subdev);
229
230         return regulator_enable(coil->vana);
231 }
232
233 static int __maybe_unused ad5398_resume(struct device *dev)
234 {
235         struct i2c_client *client = container_of(dev, struct i2c_client, dev);
236         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
237         struct ad5398_device *coil = to_ad5398_device(subdev);
238
239         return regulator_disable(coil->vana);
240 }
241
242 static int ad5398_regulator_notifier(struct notifier_block *nb,
243                                      unsigned long event,
244                                      void *ignored)
245 {
246         struct ad5398_device *coil = container_of(nb, struct ad5398_device, nb);
247
248         if (event == REGULATOR_EVENT_ENABLE)
249                 ad5398_power_on(coil);
250         else if (event == REGULATOR_EVENT_PRE_DISABLE)
251                 ad5398_power_off(coil);
252
253         return NOTIFY_OK;
254 }
255
256 static int ad5398_probe(struct i2c_client *client,
257                         const struct i2c_device_id *devid)
258 {
259         struct ad5398_device *coil;
260         int ret;
261
262         coil = devm_kzalloc(&client->dev, sizeof(*coil), GFP_KERNEL);
263         if (!coil)
264                 return -ENOMEM;
265
266         coil->vana = devm_regulator_get(&client->dev, "VANA");
267         if (IS_ERR(coil->vana)) {
268                 ret = PTR_ERR(coil->vana);
269                 if (ret != -EPROBE_DEFER)
270                         dev_err(&client->dev, "could not get regulator for vana\n");
271                 return ret;
272         }
273
274         v4l2_i2c_subdev_init(&coil->subdev, client, &ad5398_ops);
275         coil->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
276         coil->subdev.internal_ops = &ad5398_internal_ops;
277         coil->subdev.entity.function = MEDIA_ENT_F_LENS;
278         strscpy(coil->subdev.name, "ad5398 focus", sizeof(coil->subdev.name));
279
280         coil->nb.notifier_call = &ad5398_regulator_notifier;
281         ret = regulator_register_notifier(coil->vana, &coil->nb);
282         if (ret < 0)
283                 return ret;
284
285         ret = media_entity_pads_init(&coil->subdev.entity, 0, NULL);
286         if (ret < 0)
287                 goto cleanup2;
288
289         ret = v4l2_async_register_subdev(&coil->subdev);
290         if (ret < 0)
291                 goto cleanup;
292
293         return ret;
294
295 cleanup:
296         media_entity_cleanup(&coil->subdev.entity);
297 cleanup2:
298         regulator_unregister_notifier(coil->vana, &coil->nb);
299         return ret;
300 }
301
302 static int ad5398_remove(struct i2c_client *client)
303 {
304         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
305         struct ad5398_device *coil = to_ad5398_device(subdev);
306
307         v4l2_async_unregister_subdev(&coil->subdev);
308         v4l2_ctrl_handler_free(&coil->ctrls);
309         media_entity_cleanup(&coil->subdev.entity);
310         return 0;
311 }
312
313 static const struct i2c_device_id ad5398_id_table[] = {
314         { "ad5398", 0 },
315         { }
316 };
317 MODULE_DEVICE_TABLE(i2c, ad5398_id_table);
318
319 static const struct of_device_id ad5398_of_table[] = {
320         { .compatible = "adi,ad5398" },
321         { }
322 };
323 MODULE_DEVICE_TABLE(of, ad5398_of_table);
324
325 static SIMPLE_DEV_PM_OPS(ad5398_pm, ad5398_suspend, ad5398_resume);
326
327 static struct i2c_driver ad5398_i2c_driver = {
328         .driver         = {
329                 .name   = "ad5398",
330                 .pm     = &ad5398_pm,
331                 .of_match_table = ad5398_of_table,
332         },
333         .probe          = ad5398_probe,
334         .remove         = ad5398_remove,
335         .id_table       = ad5398_id_table,
336 };
337
338 module_i2c_driver(ad5398_i2c_driver);
339
340 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
341 MODULE_DESCRIPTION("AD5398 camera lens driver");
342 MODULE_LICENSE("GPL");