]> git.ipfire.org Git - thirdparty/openwrt.git/blob
471ffbceaddeb955e2405cf9ba60b9d02602ad94
[thirdparty/openwrt.git] /
1 From c1708ed0e6b86066b0b510aec54ca38687014c73 Mon Sep 17 00:00:00 2001
2 From: Kieran Bingham <kieran.bingham@ideasonboard.com>
3 Date: Wed, 13 Sep 2023 17:53:54 +0100
4 Subject: [PATCH 0769/1085] media: i2c: Add ROHM BU64754 Camera Autofocus
5 Actuator
6
7 Add support for the ROHM BU64754 Motor Driver for Camera Autofocus. A
8 V4L2 Subdevice is registered and provides a single
9 V4L2_CID_FOCUS_ABSOLUTE control.
10
11 Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
12 Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
13 ---
14 drivers/media/i2c/Kconfig | 13 ++
15 drivers/media/i2c/Makefile | 1 +
16 drivers/media/i2c/bu64754.c | 315 ++++++++++++++++++++++++++++++++++++
17 3 files changed, 329 insertions(+)
18 create mode 100644 drivers/media/i2c/bu64754.c
19
20 --- a/drivers/media/i2c/Kconfig
21 +++ b/drivers/media/i2c/Kconfig
22 @@ -732,6 +732,19 @@ config VIDEO_AK7375
23 capability. This is designed for linear control of
24 voice coil motors, controlled via I2C serial interface.
25
26 +config VIDEO_BU64754
27 + tristate "BU64754 Motor Driver for Camera Autofocus"
28 + depends on I2C && VIDEO_DEV
29 + select MEDIA_CONTROLLER
30 + select VIDEO_V4L2_SUBDEV_API
31 + select V4L2_ASYNC
32 + select V4L2_CCI_I2C
33 + help
34 + This is a driver for the BU64754 Motor Driver for Camera
35 + Autofocus. The BU64754GWZ is an actuator driver IC which
36 + can be controlled the actuator position precisely using
37 + with internal Hall Sensor.
38 +
39 config VIDEO_DW9714
40 tristate "DW9714 lens voice coil support"
41 depends on I2C && VIDEO_DEV
42 --- a/drivers/media/i2c/Makefile
43 +++ b/drivers/media/i2c/Makefile
44 @@ -25,6 +25,7 @@ obj-$(CONFIG_VIDEO_ARDUCAM_PIVARIETY) +=
45 obj-$(CONFIG_VIDEO_BT819) += bt819.o
46 obj-$(CONFIG_VIDEO_BT856) += bt856.o
47 obj-$(CONFIG_VIDEO_BT866) += bt866.o
48 +obj-$(CONFIG_VIDEO_BU64754) += bu64754.o
49 obj-$(CONFIG_VIDEO_CCS) += ccs/
50 obj-$(CONFIG_VIDEO_CCS_PLL) += ccs-pll.o
51 obj-$(CONFIG_VIDEO_CS3308) += cs3308.o
52 --- /dev/null
53 +++ b/drivers/media/i2c/bu64754.c
54 @@ -0,0 +1,315 @@
55 +// SPDX-License-Identifier: GPL-2.0
56 +/*
57 + * The BU64754GWZ is an actuator driver IC which can control the
58 + * actuator position precisely using an internal Hall Sensor.
59 + */
60 +
61 +#include <linux/delay.h>
62 +#include <linux/i2c.h>
63 +#include <linux/module.h>
64 +#include <linux/pm_runtime.h>
65 +#include <linux/regulator/consumer.h>
66 +
67 +#include <media/v4l2-cci.h>
68 +#include <media/v4l2-ctrls.h>
69 +#include <media/v4l2-device.h>
70 +
71 +#define BU64754_REG_ACTIVE CCI_REG16(0x07)
72 +#define BU64754_ACTIVE_MODE 0x8080
73 +
74 +#define BU64754_REG_SERVE CCI_REG16(0xd9)
75 +#define BU64754_SERVE_ON 0x0404
76 +
77 +#define BU64754_REG_POSITION CCI_REG16(0x45)
78 +#define BU64753_POSITION_MAX 1023 /* 0x3ff */
79 +#define BU64753_POSITION_STEPS 1
80 +
81 +#define BU64754_POWER_ON_DELAY 800 /* uS : t1, t3 */
82 +
83 +struct bu64754 {
84 + struct device *dev;
85 +
86 + struct v4l2_ctrl_handler ctrls_vcm;
87 + struct v4l2_subdev sd;
88 + struct regmap *cci;
89 +
90 + u16 current_val;
91 + struct regulator *vdd;
92 + struct notifier_block notifier;
93 +};
94 +
95 +static inline struct bu64754 *sd_to_bu64754(struct v4l2_subdev *subdev)
96 +{
97 + return container_of(subdev, struct bu64754, sd);
98 +}
99 +
100 +static int bu64754_set(struct bu64754 *bu64754, u16 position)
101 +{
102 + int ret;
103 +
104 + position &= 0x3ff; /* BU64753_POSITION_MAX */
105 + ret = cci_write(bu64754->cci, BU64754_REG_POSITION, position, NULL);
106 + if (ret) {
107 + dev_err(bu64754->dev, "Set position failed ret=%d\n", ret);
108 + return ret;
109 + }
110 +
111 + return 0;
112 +}
113 +
114 +static int bu64754_active(struct bu64754 *bu64754)
115 +{
116 + int ret;
117 +
118 + /* Power on */
119 + ret = cci_write(bu64754->cci, BU64754_REG_ACTIVE, BU64754_ACTIVE_MODE, NULL);
120 + if (ret < 0) {
121 + dev_err(bu64754->dev, "Failed to set active mode ret = %d\n",
122 + ret);
123 + return ret;
124 + }
125 +
126 + /* Serve on */
127 + ret = cci_write(bu64754->cci, BU64754_REG_SERVE, BU64754_SERVE_ON, NULL);
128 + if (ret < 0) {
129 + dev_err(bu64754->dev, "Failed to enable serve ret = %d\n",
130 + ret);
131 + return ret;
132 + }
133 +
134 + return bu64754_set(bu64754, bu64754->current_val);
135 +}
136 +
137 +static int bu64754_standby(struct bu64754 *bu64754)
138 +{
139 + int ret;
140 +
141 + ret = cci_write(bu64754->cci, BU64754_REG_ACTIVE, 0, NULL);
142 + if (ret < 0)
143 + dev_err(bu64754->dev, "Failed to enter standby mode ret = %d\n",
144 + ret);
145 +
146 + return ret;
147 +}
148 +
149 +static int bu64754_regulator_event(struct notifier_block *nb,
150 + unsigned long action, void *data)
151 +{
152 + struct bu64754 *bu64754 = container_of(nb, struct bu64754, notifier);
153 +
154 + if (action & REGULATOR_EVENT_ENABLE) {
155 + /*
156 + * Initialisation delay between VDD low->high and availability
157 + * i2c operation.
158 + */
159 + usleep_range(BU64754_POWER_ON_DELAY,
160 + BU64754_POWER_ON_DELAY + 100);
161 +
162 + bu64754_active(bu64754);
163 + } else if (action & REGULATOR_EVENT_PRE_DISABLE) {
164 + bu64754_standby(bu64754);
165 + }
166 +
167 + return 0;
168 +}
169 +
170 +static int bu64754_set_ctrl(struct v4l2_ctrl *ctrl)
171 +{
172 + struct bu64754 *bu64754 = container_of(ctrl->handler,
173 + struct bu64754, ctrls_vcm);
174 +
175 + if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
176 + bu64754->current_val = ctrl->val;
177 + return bu64754_set(bu64754, ctrl->val);
178 + }
179 +
180 + return -EINVAL;
181 +}
182 +
183 +static const struct v4l2_ctrl_ops bu64754_vcm_ctrl_ops = {
184 + .s_ctrl = bu64754_set_ctrl,
185 +};
186 +
187 +static int bu64754_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
188 +{
189 + return pm_runtime_resume_and_get(sd->dev);
190 +}
191 +
192 +static int bu64754_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
193 +{
194 + pm_runtime_put(sd->dev);
195 + return 0;
196 +}
197 +
198 +static const struct v4l2_subdev_internal_ops bu64754_int_ops = {
199 + .open = bu64754_open,
200 + .close = bu64754_close,
201 +};
202 +
203 +static const struct v4l2_subdev_ops bu64754_ops = { };
204 +
205 +static void bu64754_subdev_cleanup(struct bu64754 *bu64754)
206 +{
207 + v4l2_async_unregister_subdev(&bu64754->sd);
208 + v4l2_ctrl_handler_free(&bu64754->ctrls_vcm);
209 + media_entity_cleanup(&bu64754->sd.entity);
210 +}
211 +
212 +static int bu64754_init_controls(struct bu64754 *bu64754)
213 +{
214 + struct v4l2_ctrl_handler *hdl = &bu64754->ctrls_vcm;
215 + const struct v4l2_ctrl_ops *ops = &bu64754_vcm_ctrl_ops;
216 +
217 + v4l2_ctrl_handler_init(hdl, 1);
218 +
219 + v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
220 + 0, BU64753_POSITION_MAX, BU64753_POSITION_STEPS,
221 + 0);
222 +
223 + bu64754->current_val = 0;
224 +
225 + bu64754->sd.ctrl_handler = hdl;
226 + if (hdl->error) {
227 + dev_err(bu64754->dev, "%s fail error: 0x%x\n",
228 + __func__, hdl->error);
229 + return hdl->error;
230 + }
231 +
232 + return 0;
233 +}
234 +
235 +static int bu64754_probe(struct i2c_client *client)
236 +{
237 + struct bu64754 *bu64754;
238 + int ret;
239 +
240 + bu64754 = devm_kzalloc(&client->dev, sizeof(*bu64754), GFP_KERNEL);
241 + if (!bu64754)
242 + return -ENOMEM;
243 +
244 + bu64754->dev = &client->dev;
245 +
246 + bu64754->cci = devm_cci_regmap_init_i2c(client, 8);
247 + if (IS_ERR(bu64754->cci)) {
248 + dev_err(bu64754->dev, "Failed to initialize CCI\n");
249 + return PTR_ERR(bu64754->cci);
250 + }
251 +
252 + bu64754->vdd = devm_regulator_get_optional(&client->dev, "vdd");
253 + if (IS_ERR(bu64754->vdd)) {
254 + if (PTR_ERR(bu64754->vdd) != -ENODEV)
255 + return PTR_ERR(bu64754->vdd);
256 +
257 + bu64754->vdd = NULL;
258 + } else {
259 + bu64754->notifier.notifier_call = bu64754_regulator_event;
260 +
261 + ret = regulator_register_notifier(bu64754->vdd,
262 + &bu64754->notifier);
263 + if (ret) {
264 + dev_err(bu64754->dev,
265 + "could not register regulator notifier\n");
266 + return ret;
267 + }
268 + }
269 +
270 + v4l2_i2c_subdev_init(&bu64754->sd, client, &bu64754_ops);
271 + bu64754->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
272 + bu64754->sd.internal_ops = &bu64754_int_ops;
273 + bu64754->sd.entity.function = MEDIA_ENT_F_LENS;
274 +
275 + ret = bu64754_init_controls(bu64754);
276 + if (ret)
277 + goto err_cleanup;
278 +
279 + ret = media_entity_pads_init(&bu64754->sd.entity, 0, NULL);
280 + if (ret < 0)
281 + goto err_cleanup;
282 +
283 + bu64754->sd.entity.function = MEDIA_ENT_F_LENS;
284 +
285 + ret = v4l2_async_register_subdev(&bu64754->sd);
286 + if (ret < 0)
287 + goto err_cleanup;
288 +
289 + if (!bu64754->vdd)
290 + pm_runtime_set_active(&client->dev);
291 +
292 + pm_runtime_enable(&client->dev);
293 + pm_runtime_idle(&client->dev);
294 +
295 + return 0;
296 +
297 +err_cleanup:
298 + v4l2_ctrl_handler_free(&bu64754->ctrls_vcm);
299 + media_entity_cleanup(&bu64754->sd.entity);
300 +
301 + return ret;
302 +}
303 +
304 +static void bu64754_remove(struct i2c_client *client)
305 +{
306 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
307 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
308 +
309 + if (bu64754->vdd)
310 + regulator_unregister_notifier(bu64754->vdd,
311 + &bu64754->notifier);
312 +
313 + pm_runtime_disable(&client->dev);
314 +
315 + bu64754_subdev_cleanup(bu64754);
316 +}
317 +
318 +static int __maybe_unused bu64754_vcm_suspend(struct device *dev)
319 +{
320 + struct i2c_client *client = to_i2c_client(dev);
321 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
322 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
323 +
324 + if (bu64754->vdd)
325 + return regulator_disable(bu64754->vdd);
326 +
327 + return bu64754_standby(bu64754);
328 +}
329 +
330 +static int __maybe_unused bu64754_vcm_resume(struct device *dev)
331 +{
332 + struct i2c_client *client = to_i2c_client(dev);
333 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
334 + struct bu64754 *bu64754 = sd_to_bu64754(sd);
335 +
336 + if (bu64754->vdd)
337 + return regulator_enable(bu64754->vdd);
338 +
339 + return bu64754_active(bu64754);
340 +}
341 +
342 +static const struct of_device_id bu64754_of_table[] = {
343 + { .compatible = "rohm,bu64754", },
344 + { /* sentinel */ }
345 +};
346 +
347 +MODULE_DEVICE_TABLE(of, bu64754_of_table);
348 +
349 +static const struct dev_pm_ops bu64754_pm_ops = {
350 + SET_SYSTEM_SLEEP_PM_OPS(bu64754_vcm_suspend, bu64754_vcm_resume)
351 + SET_RUNTIME_PM_OPS(bu64754_vcm_suspend, bu64754_vcm_resume, NULL)
352 +};
353 +
354 +static struct i2c_driver bu64754_i2c_driver = {
355 + .driver = {
356 + .name = "bu64754",
357 + .pm = &bu64754_pm_ops,
358 + .of_match_table = bu64754_of_table,
359 + },
360 + .probe = bu64754_probe,
361 + .remove = bu64754_remove,
362 +};
363 +
364 +module_i2c_driver(bu64754_i2c_driver);
365 +
366 +MODULE_AUTHOR("Kieran Bingham");
367 +MODULE_DESCRIPTION("BU64754 VCM driver");
368 +MODULE_LICENSE("GPL");
369 +