1 /* 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 14 #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 /* 24 * 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 /* 29 * 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 */ 44 struct dw9714_device { 45 struct i2c_client *client; 46 struct v4l2_ctrl_handler ctrls_vcm; 47 struct v4l2_subdev sd; 48 u16 current_val; 49 }; 50 51 static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl) 52 { 53 return container_of(ctrl->handler, struct dw9714_device, ctrls_vcm); 54 } 55 56 static inline struct dw9714_device *sd_to_dw9714_vcm(struct v4l2_subdev *subdev) 57 { 58 return container_of(subdev, struct dw9714_device, sd); 59 } 60 61 static int dw9714_i2c_write(struct i2c_client *client, u16 data) 62 { 63 int ret; 64 u16 val = cpu_to_be16(data); 65 66 ret = i2c_master_send(client, (const char *)&val, sizeof(val)); 67 if (ret != sizeof(val)) { 68 dev_err(&client->dev, "I2C write fail\n"); 69 return -EIO; 70 } 71 return 0; 72 } 73 74 static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val) 75 { 76 struct i2c_client *client = dw9714_dev->client; 77 78 dw9714_dev->current_val = val; 79 80 return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S)); 81 } 82 83 static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl) 84 { 85 struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl); 86 87 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) 88 return dw9714_t_focus_vcm(dev_vcm, ctrl->val); 89 90 return -EINVAL; 91 } 92 93 static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = { 94 .s_ctrl = dw9714_set_ctrl, 95 }; 96 97 static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 98 { 99 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 100 struct device *dev = &dw9714_dev->client->dev; 101 int rval; 102 103 rval = pm_runtime_get_sync(dev); 104 if (rval < 0) { 105 pm_runtime_put_noidle(dev); 106 return rval; 107 } 108 109 return 0; 110 } 111 112 static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 113 { 114 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 115 struct device *dev = &dw9714_dev->client->dev; 116 117 pm_runtime_put(dev); 118 119 return 0; 120 } 121 122 static const struct v4l2_subdev_internal_ops dw9714_int_ops = { 123 .open = dw9714_open, 124 .close = dw9714_close, 125 }; 126 127 static const struct v4l2_subdev_ops dw9714_ops = { }; 128 129 static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev) 130 { 131 v4l2_async_unregister_subdev(&dw9714_dev->sd); 132 v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm); 133 media_entity_cleanup(&dw9714_dev->sd.entity); 134 } 135 136 static int dw9714_init_controls(struct dw9714_device *dev_vcm) 137 { 138 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm; 139 const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops; 140 struct i2c_client *client = dev_vcm->client; 141 142 v4l2_ctrl_handler_init(hdl, 1); 143 144 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE, 145 0, DW9714_MAX_FOCUS_POS, DW9714_FOCUS_STEPS, 0); 146 147 if (hdl->error) 148 dev_err(&client->dev, "%s fail error: 0x%x\n", 149 __func__, hdl->error); 150 dev_vcm->sd.ctrl_handler = hdl; 151 return hdl->error; 152 } 153 154 static int dw9714_probe(struct i2c_client *client) 155 { 156 struct dw9714_device *dw9714_dev; 157 int rval; 158 159 dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev), 160 GFP_KERNEL); 161 if (dw9714_dev == NULL) 162 return -ENOMEM; 163 164 dw9714_dev->client = client; 165 166 v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops); 167 dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 168 dw9714_dev->sd.internal_ops = &dw9714_int_ops; 169 170 rval = dw9714_init_controls(dw9714_dev); 171 if (rval) 172 goto err_cleanup; 173 174 rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL); 175 if (rval < 0) 176 goto err_cleanup; 177 178 dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS; 179 180 rval = v4l2_async_register_subdev(&dw9714_dev->sd); 181 if (rval < 0) 182 goto err_cleanup; 183 184 pm_runtime_set_active(&client->dev); 185 pm_runtime_enable(&client->dev); 186 187 return 0; 188 189 err_cleanup: 190 dw9714_subdev_cleanup(dw9714_dev); 191 dev_err(&client->dev, "Probe failed: %d\n", rval); 192 return rval; 193 } 194 195 static int dw9714_remove(struct i2c_client *client) 196 { 197 struct v4l2_subdev *sd = i2c_get_clientdata(client); 198 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 199 200 pm_runtime_disable(&client->dev); 201 dw9714_subdev_cleanup(dw9714_dev); 202 203 return 0; 204 } 205 206 /* 207 * This function sets the vcm position, so it consumes least current 208 * The lens position is gradually moved in units of DW9714_CTRL_STEPS, 209 * to make the movements smoothly. 210 */ 211 static int __maybe_unused dw9714_vcm_suspend(struct device *dev) 212 { 213 struct i2c_client *client = to_i2c_client(dev); 214 struct v4l2_subdev *sd = i2c_get_clientdata(client); 215 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 216 int ret, val; 217 218 for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1); 219 val >= 0; val -= DW9714_CTRL_STEPS) { 220 ret = dw9714_i2c_write(client, 221 DW9714_VAL(val, DW9714_DEFAULT_S)); 222 if (ret) 223 dev_err_once(dev, "%s I2C failure: %d", __func__, ret); 224 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); 225 } 226 return 0; 227 } 228 229 /* 230 * This function sets the vcm position to the value set by the user 231 * through v4l2_ctrl_ops s_ctrl handler 232 * The lens position is gradually moved in units of DW9714_CTRL_STEPS, 233 * to make the movements smoothly. 234 */ 235 static int __maybe_unused dw9714_vcm_resume(struct device *dev) 236 { 237 struct i2c_client *client = to_i2c_client(dev); 238 struct v4l2_subdev *sd = i2c_get_clientdata(client); 239 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 240 int ret, val; 241 242 for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS; 243 val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1; 244 val += DW9714_CTRL_STEPS) { 245 ret = dw9714_i2c_write(client, 246 DW9714_VAL(val, DW9714_DEFAULT_S)); 247 if (ret) 248 dev_err_ratelimited(dev, "%s I2C failure: %d", 249 __func__, ret); 250 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); 251 } 252 253 return 0; 254 } 255 256 static const struct i2c_device_id dw9714_id_table[] = { 257 { DW9714_NAME, 0 }, 258 { { 0 } } 259 }; 260 MODULE_DEVICE_TABLE(i2c, dw9714_id_table); 261 262 static const struct of_device_id dw9714_of_table[] = { 263 { .compatible = "dongwoon,dw9714" }, 264 { { 0 } } 265 }; 266 MODULE_DEVICE_TABLE(of, dw9714_of_table); 267 268 static const struct dev_pm_ops dw9714_pm_ops = { 269 SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume) 270 SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL) 271 }; 272 273 static struct i2c_driver dw9714_i2c_driver = { 274 .driver = { 275 .name = DW9714_NAME, 276 .pm = &dw9714_pm_ops, 277 .of_match_table = dw9714_of_table, 278 }, 279 .probe_new = dw9714_probe, 280 .remove = dw9714_remove, 281 .id_table = dw9714_id_table, 282 }; 283 284 module_i2c_driver(dw9714_i2c_driver); 285 286 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>"); 287 MODULE_AUTHOR("Jian Xu Zheng <jian.xu.zheng@intel.com>"); 288 MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>"); 289 MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>"); 290 MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>"); 291 MODULE_DESCRIPTION("DW9714 VCM driver"); 292 MODULE_LICENSE("GPL v2"); 293