blob: 8dbbf0f917dffbd8188be766a0d66dd447099b1e [file] [log] [blame]
Rajmohan Manicc95d342017-06-03 05:11:40 -03001/*
2 * Copyright (c) 2015--2017 Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Rajmohan Manicc95d342017-06-03 05:11:40 -030014#include <linux/delay.h>
15#include <linux/i2c.h>
16#include <linux/module.h>
17#include <linux/pm_runtime.h>
18#include <media/v4l2-ctrls.h>
19#include <media/v4l2-device.h>
20
21#define DW9714_NAME "dw9714"
22#define DW9714_MAX_FOCUS_POS 1023
23/*
Rajmohan Mani1a58fbf2017-08-30 14:48:52 -030024 * This sets the minimum granularity for the focus positions.
25 * A value of 1 gives maximum accuracy for a desired focus position
26 */
27#define DW9714_FOCUS_STEPS 1
28/*
Rajmohan Manicc95d342017-06-03 05:11:40 -030029 * This acts as the minimum granularity of lens movement.
30 * Keep this value power of 2, so the control steps can be
31 * uniformly adjusted for gradual lens movement, with desired
32 * number of control steps.
33 */
34#define DW9714_CTRL_STEPS 16
35#define DW9714_CTRL_DELAY_US 1000
36/*
37 * S[3:2] = 0x00, codes per step for "Linear Slope Control"
38 * S[1:0] = 0x00, step period
39 */
40#define DW9714_DEFAULT_S 0x0
41#define DW9714_VAL(data, s) ((data) << 4 | (s))
42
43/* dw9714 device structure */
44struct dw9714_device {
Rajmohan Manicc95d342017-06-03 05:11:40 -030045 struct v4l2_ctrl_handler ctrls_vcm;
46 struct v4l2_subdev sd;
47 u16 current_val;
48};
49
50static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl)
51{
52 return container_of(ctrl->handler, struct dw9714_device, ctrls_vcm);
53}
54
55static inline struct dw9714_device *sd_to_dw9714_vcm(struct v4l2_subdev *subdev)
56{
57 return container_of(subdev, struct dw9714_device, sd);
58}
59
60static int dw9714_i2c_write(struct i2c_client *client, u16 data)
61{
62 int ret;
Mauro Carvalho Chehaba4a02b62018-01-23 07:46:07 -050063 __be16 val = cpu_to_be16(data);
Rajmohan Manicc95d342017-06-03 05:11:40 -030064
65 ret = i2c_master_send(client, (const char *)&val, sizeof(val));
66 if (ret != sizeof(val)) {
67 dev_err(&client->dev, "I2C write fail\n");
68 return -EIO;
69 }
70 return 0;
71}
72
73static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val)
74{
Sakari Ailusa69e9972018-01-02 05:55:17 -050075 struct i2c_client *client = v4l2_get_subdevdata(&dw9714_dev->sd);
Rajmohan Manicc95d342017-06-03 05:11:40 -030076
77 dw9714_dev->current_val = val;
78
79 return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));
80}
81
82static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl)
83{
84 struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl);
85
86 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
87 return dw9714_t_focus_vcm(dev_vcm, ctrl->val);
88
89 return -EINVAL;
90}
91
92static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = {
93 .s_ctrl = dw9714_set_ctrl,
94};
95
96static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
97{
Rajmohan Manicc95d342017-06-03 05:11:40 -030098 int rval;
99
Sakari Ailusa69e9972018-01-02 05:55:17 -0500100 rval = pm_runtime_get_sync(sd->dev);
Rajmohan Manicc95d342017-06-03 05:11:40 -0300101 if (rval < 0) {
Sakari Ailusa69e9972018-01-02 05:55:17 -0500102 pm_runtime_put_noidle(sd->dev);
Rajmohan Manicc95d342017-06-03 05:11:40 -0300103 return rval;
104 }
105
106 return 0;
107}
108
109static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
110{
Sakari Ailusa69e9972018-01-02 05:55:17 -0500111 pm_runtime_put(sd->dev);
Rajmohan Manicc95d342017-06-03 05:11:40 -0300112
113 return 0;
114}
115
116static const struct v4l2_subdev_internal_ops dw9714_int_ops = {
117 .open = dw9714_open,
118 .close = dw9714_close,
119};
120
121static const struct v4l2_subdev_ops dw9714_ops = { };
122
123static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev)
124{
125 v4l2_async_unregister_subdev(&dw9714_dev->sd);
126 v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);
127 media_entity_cleanup(&dw9714_dev->sd.entity);
128}
129
130static int dw9714_init_controls(struct dw9714_device *dev_vcm)
131{
132 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
133 const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops;
Rajmohan Manicc95d342017-06-03 05:11:40 -0300134
135 v4l2_ctrl_handler_init(hdl, 1);
136
137 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
Rajmohan Mani1a58fbf2017-08-30 14:48:52 -0300138 0, DW9714_MAX_FOCUS_POS, DW9714_FOCUS_STEPS, 0);
Rajmohan Manicc95d342017-06-03 05:11:40 -0300139
140 if (hdl->error)
Sakari Ailusa69e9972018-01-02 05:55:17 -0500141 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
Rajmohan Manicc95d342017-06-03 05:11:40 -0300142 __func__, hdl->error);
143 dev_vcm->sd.ctrl_handler = hdl;
144 return hdl->error;
145}
146
Sakari Ailusf758eb22017-08-15 05:08:52 -0400147static int dw9714_probe(struct i2c_client *client)
Rajmohan Manicc95d342017-06-03 05:11:40 -0300148{
149 struct dw9714_device *dw9714_dev;
150 int rval;
151
152 dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev),
153 GFP_KERNEL);
154 if (dw9714_dev == NULL)
155 return -ENOMEM;
156
Rajmohan Manicc95d342017-06-03 05:11:40 -0300157 v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops);
158 dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
159 dw9714_dev->sd.internal_ops = &dw9714_int_ops;
160
161 rval = dw9714_init_controls(dw9714_dev);
162 if (rval)
163 goto err_cleanup;
164
165 rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL);
166 if (rval < 0)
167 goto err_cleanup;
168
169 dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
170
171 rval = v4l2_async_register_subdev(&dw9714_dev->sd);
172 if (rval < 0)
173 goto err_cleanup;
174
175 pm_runtime_set_active(&client->dev);
176 pm_runtime_enable(&client->dev);
Sakari Ailusfa429332018-01-02 05:51:57 -0500177 pm_runtime_idle(&client->dev);
Rajmohan Manicc95d342017-06-03 05:11:40 -0300178
179 return 0;
180
181err_cleanup:
182 dw9714_subdev_cleanup(dw9714_dev);
183 dev_err(&client->dev, "Probe failed: %d\n", rval);
184 return rval;
185}
186
187static int dw9714_remove(struct i2c_client *client)
188{
189 struct v4l2_subdev *sd = i2c_get_clientdata(client);
190 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
191
192 pm_runtime_disable(&client->dev);
193 dw9714_subdev_cleanup(dw9714_dev);
194
195 return 0;
196}
197
198/*
199 * This function sets the vcm position, so it consumes least current
200 * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
201 * to make the movements smoothly.
202 */
203static int __maybe_unused dw9714_vcm_suspend(struct device *dev)
204{
205 struct i2c_client *client = to_i2c_client(dev);
206 struct v4l2_subdev *sd = i2c_get_clientdata(client);
207 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
208 int ret, val;
209
210 for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
211 val >= 0; val -= DW9714_CTRL_STEPS) {
212 ret = dw9714_i2c_write(client,
213 DW9714_VAL(val, DW9714_DEFAULT_S));
214 if (ret)
215 dev_err_once(dev, "%s I2C failure: %d", __func__, ret);
216 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
217 }
218 return 0;
219}
220
221/*
222 * This function sets the vcm position to the value set by the user
223 * through v4l2_ctrl_ops s_ctrl handler
224 * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
225 * to make the movements smoothly.
226 */
227static int __maybe_unused dw9714_vcm_resume(struct device *dev)
228{
229 struct i2c_client *client = to_i2c_client(dev);
230 struct v4l2_subdev *sd = i2c_get_clientdata(client);
231 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
232 int ret, val;
233
234 for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
235 val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
236 val += DW9714_CTRL_STEPS) {
237 ret = dw9714_i2c_write(client,
238 DW9714_VAL(val, DW9714_DEFAULT_S));
239 if (ret)
240 dev_err_ratelimited(dev, "%s I2C failure: %d",
241 __func__, ret);
242 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
243 }
244
245 return 0;
246}
247
Rajmohan Manicc95d342017-06-03 05:11:40 -0300248static const struct i2c_device_id dw9714_id_table[] = {
Sakari Ailusf758eb22017-08-15 05:08:52 -0400249 { DW9714_NAME, 0 },
250 { { 0 } }
Rajmohan Manicc95d342017-06-03 05:11:40 -0300251};
Rajmohan Manicc95d342017-06-03 05:11:40 -0300252MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
253
Sakari Ailusc2bc8b02017-08-15 05:06:59 -0400254static const struct of_device_id dw9714_of_table[] = {
255 { .compatible = "dongwoon,dw9714" },
256 { { 0 } }
257};
258MODULE_DEVICE_TABLE(of, dw9714_of_table);
259
Rajmohan Manicc95d342017-06-03 05:11:40 -0300260static const struct dev_pm_ops dw9714_pm_ops = {
261 SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume)
262 SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL)
263};
264
265static struct i2c_driver dw9714_i2c_driver = {
266 .driver = {
267 .name = DW9714_NAME,
268 .pm = &dw9714_pm_ops,
Sakari Ailusc2bc8b02017-08-15 05:06:59 -0400269 .of_match_table = dw9714_of_table,
Rajmohan Manicc95d342017-06-03 05:11:40 -0300270 },
Sakari Ailusf758eb22017-08-15 05:08:52 -0400271 .probe_new = dw9714_probe,
Rajmohan Manicc95d342017-06-03 05:11:40 -0300272 .remove = dw9714_remove,
273 .id_table = dw9714_id_table,
274};
275
276module_i2c_driver(dw9714_i2c_driver);
277
278MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
279MODULE_AUTHOR("Jian Xu Zheng <jian.xu.zheng@intel.com>");
280MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>");
281MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>");
282MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>");
283MODULE_DESCRIPTION("DW9714 VCM driver");
284MODULE_LICENSE("GPL v2");