1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2018 Intel Corporation
#include <linux/acpi.h>
#include <linux/delay.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>
struct ak73xx_chipdef {
u8 reg_position;
u8 reg_cont;
u8 shift_pos;
u8 mode_active;
u8 mode_standby;
bool has_standby; /* Some chips may not have standby mode */
u16 focus_pos_max;
/*
* This sets the minimum granularity for the focus positions.
* A value of 1 gives maximum accuracy for a desired focus position
*/
u16 focus_steps;
/*
* This acts as the minimum granularity of lens movement.
* Keep this value power of 2, so the control steps can be
* uniformly adjusted for gradual lens movement, with desired
* number of control steps.
*/
u16 ctrl_steps;
u16 ctrl_delay_us;
/*
* The vcm may take time (tDELAY) to power on and start taking
* I2C messages.
*/
u16 power_delay_us;
};
static const struct ak73xx_chipdef ak7345_cdef = {
.reg_position = 0x0,
.reg_cont = 0x2,
.shift_pos = 7, /* 9 bits position values, need to << 7 */
.mode_active = 0x0,
.has_standby = false,
.focus_pos_max = 511,
.focus_steps = 1,
.ctrl_steps = 16,
.ctrl_delay_us = 1000,
.power_delay_us = 20000,
};
static const struct ak73xx_chipdef ak7375_cdef = {
.reg_position = 0x0,
.reg_cont = 0x2,
.shift_pos = 4, /* 12 bits position values, need to << 4 */
.mode_active = 0x0,
.mode_standby = 0x40,
.has_standby = true,
.focus_pos_max = 4095,
.focus_steps = 1,
.ctrl_steps = 64,
.ctrl_delay_us = 1000,
.power_delay_us = 10000,
};
static const char * const ak7375_supply_names[] = {
"vdd",
"vio",
};
/* ak7375 device structure */
struct ak7375_device {
const struct ak73xx_chipdef *cdef;
struct v4l2_ctrl_handler ctrls_vcm;
struct v4l2_subdev sd;
struct v4l2_ctrl *focus;
struct regulator_bulk_data supplies[ARRAY_SIZE(ak7375_supply_names)];
/* active or standby mode */
bool active;
};
static inline struct ak7375_device *to_ak7375_vcm(struct v4l2_ctrl *ctrl)
{
return container_of(ctrl->handler, struct ak7375_device, ctrls_vcm);
}
static inline struct ak7375_device *sd_to_ak7375_vcm(struct v4l2_subdev *subdev)
{
return container_of(subdev, struct ak7375_device, sd);
}
static int ak7375_i2c_write(struct ak7375_device *ak7375,
u8 addr, u16 data, u8 size)
{
struct i2c_client *client = v4l2_get_subdevdata(&ak7375->sd);
u8 buf[3];
int ret;
if (size != 1 && size != 2)
return -EINVAL;
buf[0] = addr;
buf[size] = data & 0xff;
if (size == 2)
buf[1] = (data >> 8) & 0xff;
ret = i2c_master_send(client, (const char *)buf, size + 1);
if (ret < 0)
return ret;
if (ret != size + 1)
return -EIO;
return 0;
}
static int ak7375_set_ctrl(struct v4l2_ctrl *ctrl)
{
struct ak7375_device *dev_vcm = to_ak7375_vcm(ctrl);
const struct ak73xx_chipdef *cdef = dev_vcm->cdef;
if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
return ak7375_i2c_write(dev_vcm, cdef->reg_position,
ctrl->val << cdef->shift_pos, 2);
return -EINVAL;
}
static const struct v4l2_ctrl_ops ak7375_vcm_ctrl_ops = {
.s_ctrl = ak7375_set_ctrl,
};
static int ak7375_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
return pm_runtime_resume_and_get(sd->dev);
}
static int ak7375_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
pm_runtime_put(sd->dev);
return 0;
}
static const struct v4l2_subdev_internal_ops ak7375_int_ops = {
.open = ak7375_open,
.close = ak7375_close,
};
static const struct v4l2_subdev_ops ak7375_ops = { };
static void ak7375_subdev_cleanup(struct ak7375_device *ak7375_dev)
{
v4l2_async_unregister_subdev(&ak7375_dev->sd);
v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
media_entity_cleanup(&ak7375_dev->sd.entity);
}
static int ak7375_init_controls(struct ak7375_device *dev_vcm)
{
struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
const struct v4l2_ctrl_ops *ops = &ak7375_vcm_ctrl_ops;
const struct ak73xx_chipdef *cdef = dev_vcm->cdef;
v4l2_ctrl_handler_init(hdl, 1);
dev_vcm->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
0, cdef->focus_pos_max, cdef->focus_steps, 0);
if (hdl->error)
dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
__func__, hdl->error);
dev_vcm->sd.ctrl_handler = hdl;
return hdl->error;
}
static int ak7375_probe(struct i2c_client *client)
{
struct ak7375_device *ak7375_dev;
int ret;
unsigned int i;
ak7375_dev = devm_kzalloc(&client->dev, sizeof(*ak7375_dev),
GFP_KERNEL);
if (!ak7375_dev)
return -ENOMEM;
ak7375_dev->cdef = device_get_match_data(&client->dev);
for (i = 0; i < ARRAY_SIZE(ak7375_supply_names); i++)
ak7375_dev->supplies[i].supply = ak7375_supply_names[i];
ret = devm_regulator_bulk_get(&client->dev,
ARRAY_SIZE(ak7375_supply_names),
ak7375_dev->supplies);
if (ret) {
dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
return ret;
}
v4l2_i2c_subdev_init(&ak7375_dev->sd, client, &ak7375_ops);
ak7375_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
ak7375_dev->sd.internal_ops = &ak7375_int_ops;
ak7375_dev->sd.entity.function = MEDIA_ENT_F_LENS;
ret = ak7375_init_controls(ak7375_dev);
if (ret)
goto err_cleanup;
ret = media_entity_pads_init(&ak7375_dev->sd.entity, 0, NULL);
if (ret < 0)
goto err_cleanup;
ret = v4l2_async_register_subdev(&ak7375_dev->sd);
if (ret < 0)
goto err_cleanup;
pm_runtime_set_active(&client->dev);
pm_runtime_enable(&client->dev);
pm_runtime_idle(&client->dev);
return 0;
err_cleanup:
v4l2_ctrl_handler_free(&ak7375_dev->ctrls_vcm);
media_entity_cleanup(&ak7375_dev->sd.entity);
return ret;
}
static void ak7375_remove(struct i2c_client *client)
{
struct v4l2_subdev *sd = i2c_get_clientdata(client);
struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
ak7375_subdev_cleanup(ak7375_dev);
pm_runtime_disable(&client->dev);
pm_runtime_set_suspended(&client->dev);
}
/*
* This function sets the vcm position, so it consumes least current
* The lens position is gradually moved in units of ctrl_steps,
* to make the movements smoothly.
*/
static int __maybe_unused ak7375_vcm_suspend(struct device *dev)
{
struct v4l2_subdev *sd = dev_get_drvdata(dev);
struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
const struct ak73xx_chipdef *cdef = ak7375_dev->cdef;
int ret, val;
if (!ak7375_dev->active)
return 0;
for (val = ak7375_dev->focus->val & ~(cdef->ctrl_steps - 1);
val >= 0; val -= cdef->ctrl_steps) {
ret = ak7375_i2c_write(ak7375_dev, cdef->reg_position,
val << cdef->shift_pos, 2);
if (ret)
dev_err_once(dev, "%s I2C failure: %d\n",
__func__, ret);
usleep_range(cdef->ctrl_delay_us, cdef->ctrl_delay_us + 10);
}
if (cdef->has_standby) {
ret = ak7375_i2c_write(ak7375_dev, cdef->reg_cont,
cdef->mode_standby, 1);
if (ret)
dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
}
ret = regulator_bulk_disable(ARRAY_SIZE(ak7375_supply_names),
ak7375_dev->supplies);
if (ret)
return ret;
ak7375_dev->active = false;
return 0;
}
/*
* This function sets the vcm position to the value set by the user
* through v4l2_ctrl_ops s_ctrl handler
* The lens position is gradually moved in units of ctrl_steps,
* to make the movements smoothly.
*/
static int __maybe_unused ak7375_vcm_resume(struct device *dev)
{
struct v4l2_subdev *sd = dev_get_drvdata(dev);
struct ak7375_device *ak7375_dev = sd_to_ak7375_vcm(sd);
const struct ak73xx_chipdef *cdef = ak7375_dev->cdef;
int ret, val;
if (ak7375_dev->active)
return 0;
ret = regulator_bulk_enable(ARRAY_SIZE(ak7375_supply_names),
ak7375_dev->supplies);
if (ret)
return ret;
/* Wait for vcm to become ready */
usleep_range(cdef->power_delay_us, cdef->power_delay_us + 500);
ret = ak7375_i2c_write(ak7375_dev, cdef->reg_cont,
cdef->mode_active, 1);
if (ret) {
dev_err(dev, "%s I2C failure: %d\n", __func__, ret);
return ret;
}
for (val = ak7375_dev->focus->val % cdef->ctrl_steps;
val <= ak7375_dev->focus->val;
val += cdef->ctrl_steps) {
ret = ak7375_i2c_write(ak7375_dev, cdef->reg_position,
val << cdef->shift_pos, 2);
if (ret)
dev_err_ratelimited(dev, "%s I2C failure: %d\n",
__func__, ret);
usleep_range(cdef->ctrl_delay_us, cdef->ctrl_delay_us + 10);
}
ak7375_dev->active = true;
return 0;
}
static const struct of_device_id ak7375_of_table[] = {
{ .compatible = "asahi-kasei,ak7345", .data = &ak7345_cdef, },
{ .compatible = "asahi-kasei,ak7375", .data = &ak7375_cdef, },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, ak7375_of_table);
static const struct dev_pm_ops ak7375_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume)
SET_RUNTIME_PM_OPS(ak7375_vcm_suspend, ak7375_vcm_resume, NULL)
};
static struct i2c_driver ak7375_i2c_driver = {
.driver = {
.name = "ak7375",
.pm = &ak7375_pm_ops,
.of_match_table = ak7375_of_table,
},
.probe = ak7375_probe,
.remove = ak7375_remove,
};
module_i2c_driver(ak7375_i2c_driver);
MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
MODULE_DESCRIPTION("AK7375 VCM driver");
MODULE_LICENSE("GPL v2");
|