erge tag 'libnvdimm-fixes-4.19-rc6' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / media / i2c / dw9714.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2015--2017 Intel Corporation.
3
4 #include <linux/delay.h>
5 #include <linux/i2c.h>
6 #include <linux/module.h>
7 #include <linux/pm_runtime.h>
8 #include <media/v4l2-ctrls.h>
9 #include <media/v4l2-device.h>
10
11 #define DW9714_NAME             "dw9714"
12 #define DW9714_MAX_FOCUS_POS    1023
13 /*
14  * This sets the minimum granularity for the focus positions.
15  * A value of 1 gives maximum accuracy for a desired focus position
16  */
17 #define DW9714_FOCUS_STEPS      1
18 /*
19  * This acts as the minimum granularity of lens movement.
20  * Keep this value power of 2, so the control steps can be
21  * uniformly adjusted for gradual lens movement, with desired
22  * number of control steps.
23  */
24 #define DW9714_CTRL_STEPS       16
25 #define DW9714_CTRL_DELAY_US    1000
26 /*
27  * S[3:2] = 0x00, codes per step for "Linear Slope Control"
28  * S[1:0] = 0x00, step period
29  */
30 #define DW9714_DEFAULT_S 0x0
31 #define DW9714_VAL(data, s) ((data) << 4 | (s))
32
33 /* dw9714 device structure */
34 struct dw9714_device {
35         struct v4l2_ctrl_handler ctrls_vcm;
36         struct v4l2_subdev sd;
37         u16 current_val;
38 };
39
40 static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl)
41 {
42         return container_of(ctrl->handler, struct dw9714_device, ctrls_vcm);
43 }
44
45 static inline struct dw9714_device *sd_to_dw9714_vcm(struct v4l2_subdev *subdev)
46 {
47         return container_of(subdev, struct dw9714_device, sd);
48 }
49
50 static int dw9714_i2c_write(struct i2c_client *client, u16 data)
51 {
52         int ret;
53         __be16 val = cpu_to_be16(data);
54
55         ret = i2c_master_send(client, (const char *)&val, sizeof(val));
56         if (ret != sizeof(val)) {
57                 dev_err(&client->dev, "I2C write fail\n");
58                 return -EIO;
59         }
60         return 0;
61 }
62
63 static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val)
64 {
65         struct i2c_client *client = v4l2_get_subdevdata(&dw9714_dev->sd);
66
67         dw9714_dev->current_val = val;
68
69         return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));
70 }
71
72 static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl)
73 {
74         struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl);
75
76         if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
77                 return dw9714_t_focus_vcm(dev_vcm, ctrl->val);
78
79         return -EINVAL;
80 }
81
82 static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = {
83         .s_ctrl = dw9714_set_ctrl,
84 };
85
86 static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
87 {
88         int rval;
89
90         rval = pm_runtime_get_sync(sd->dev);
91         if (rval < 0) {
92                 pm_runtime_put_noidle(sd->dev);
93                 return rval;
94         }
95
96         return 0;
97 }
98
99 static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
100 {
101         pm_runtime_put(sd->dev);
102
103         return 0;
104 }
105
106 static const struct v4l2_subdev_internal_ops dw9714_int_ops = {
107         .open = dw9714_open,
108         .close = dw9714_close,
109 };
110
111 static const struct v4l2_subdev_ops dw9714_ops = { };
112
113 static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev)
114 {
115         v4l2_async_unregister_subdev(&dw9714_dev->sd);
116         v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);
117         media_entity_cleanup(&dw9714_dev->sd.entity);
118 }
119
120 static int dw9714_init_controls(struct dw9714_device *dev_vcm)
121 {
122         struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
123         const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops;
124
125         v4l2_ctrl_handler_init(hdl, 1);
126
127         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
128                           0, DW9714_MAX_FOCUS_POS, DW9714_FOCUS_STEPS, 0);
129
130         if (hdl->error)
131                 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
132                         __func__, hdl->error);
133         dev_vcm->sd.ctrl_handler = hdl;
134         return hdl->error;
135 }
136
137 static int dw9714_probe(struct i2c_client *client)
138 {
139         struct dw9714_device *dw9714_dev;
140         int rval;
141
142         dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev),
143                                   GFP_KERNEL);
144         if (dw9714_dev == NULL)
145                 return -ENOMEM;
146
147         v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops);
148         dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
149         dw9714_dev->sd.internal_ops = &dw9714_int_ops;
150
151         rval = dw9714_init_controls(dw9714_dev);
152         if (rval)
153                 goto err_cleanup;
154
155         rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL);
156         if (rval < 0)
157                 goto err_cleanup;
158
159         dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
160
161         rval = v4l2_async_register_subdev(&dw9714_dev->sd);
162         if (rval < 0)
163                 goto err_cleanup;
164
165         pm_runtime_set_active(&client->dev);
166         pm_runtime_enable(&client->dev);
167         pm_runtime_idle(&client->dev);
168
169         return 0;
170
171 err_cleanup:
172         dw9714_subdev_cleanup(dw9714_dev);
173         dev_err(&client->dev, "Probe failed: %d\n", rval);
174         return rval;
175 }
176
177 static int dw9714_remove(struct i2c_client *client)
178 {
179         struct v4l2_subdev *sd = i2c_get_clientdata(client);
180         struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
181
182         pm_runtime_disable(&client->dev);
183         dw9714_subdev_cleanup(dw9714_dev);
184
185         return 0;
186 }
187
188 /*
189  * This function sets the vcm position, so it consumes least current
190  * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
191  * to make the movements smoothly.
192  */
193 static int __maybe_unused dw9714_vcm_suspend(struct device *dev)
194 {
195         struct i2c_client *client = to_i2c_client(dev);
196         struct v4l2_subdev *sd = i2c_get_clientdata(client);
197         struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
198         int ret, val;
199
200         for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
201              val >= 0; val -= DW9714_CTRL_STEPS) {
202                 ret = dw9714_i2c_write(client,
203                                        DW9714_VAL(val, DW9714_DEFAULT_S));
204                 if (ret)
205                         dev_err_once(dev, "%s I2C failure: %d", __func__, ret);
206                 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
207         }
208         return 0;
209 }
210
211 /*
212  * This function sets the vcm position to the value set by the user
213  * through v4l2_ctrl_ops s_ctrl handler
214  * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
215  * to make the movements smoothly.
216  */
217 static int  __maybe_unused dw9714_vcm_resume(struct device *dev)
218 {
219         struct i2c_client *client = to_i2c_client(dev);
220         struct v4l2_subdev *sd = i2c_get_clientdata(client);
221         struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
222         int ret, val;
223
224         for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
225              val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
226              val += DW9714_CTRL_STEPS) {
227                 ret = dw9714_i2c_write(client,
228                                        DW9714_VAL(val, DW9714_DEFAULT_S));
229                 if (ret)
230                         dev_err_ratelimited(dev, "%s I2C failure: %d",
231                                                 __func__, ret);
232                 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
233         }
234
235         return 0;
236 }
237
238 static const struct i2c_device_id dw9714_id_table[] = {
239         { DW9714_NAME, 0 },
240         { { 0 } }
241 };
242 MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
243
244 static const struct of_device_id dw9714_of_table[] = {
245         { .compatible = "dongwoon,dw9714" },
246         { { 0 } }
247 };
248 MODULE_DEVICE_TABLE(of, dw9714_of_table);
249
250 static const struct dev_pm_ops dw9714_pm_ops = {
251         SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume)
252         SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL)
253 };
254
255 static struct i2c_driver dw9714_i2c_driver = {
256         .driver = {
257                 .name = DW9714_NAME,
258                 .pm = &dw9714_pm_ops,
259                 .of_match_table = dw9714_of_table,
260         },
261         .probe_new = dw9714_probe,
262         .remove = dw9714_remove,
263         .id_table = dw9714_id_table,
264 };
265
266 module_i2c_driver(dw9714_i2c_driver);
267
268 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
269 MODULE_AUTHOR("Jian Xu Zheng <jian.xu.zheng@intel.com>");
270 MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>");
271 MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>");
272 MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>");
273 MODULE_DESCRIPTION("DW9714 VCM driver");
274 MODULE_LICENSE("GPL v2");