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 return pm_runtime_resume_and_get(sd->dev); 89 } 90 91 static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 92 { 93 pm_runtime_put(sd->dev); 94 95 return 0; 96 } 97 98 static const struct v4l2_subdev_internal_ops dw9714_int_ops = { 99 .open = dw9714_open, 100 .close = dw9714_close, 101 }; 102 103 static const struct v4l2_subdev_ops dw9714_ops = { }; 104 105 static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev) 106 { 107 v4l2_async_unregister_subdev(&dw9714_dev->sd); 108 v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm); 109 media_entity_cleanup(&dw9714_dev->sd.entity); 110 } 111 112 static int dw9714_init_controls(struct dw9714_device *dev_vcm) 113 { 114 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm; 115 const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops; 116 117 v4l2_ctrl_handler_init(hdl, 1); 118 119 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE, 120 0, DW9714_MAX_FOCUS_POS, DW9714_FOCUS_STEPS, 0); 121 122 if (hdl->error) 123 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n", 124 __func__, hdl->error); 125 dev_vcm->sd.ctrl_handler = hdl; 126 return hdl->error; 127 } 128 129 static int dw9714_probe(struct i2c_client *client) 130 { 131 struct dw9714_device *dw9714_dev; 132 int rval; 133 134 dw9714_dev = devm_kzalloc(&client->dev, sizeof(*dw9714_dev), 135 GFP_KERNEL); 136 if (dw9714_dev == NULL) 137 return -ENOMEM; 138 139 v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops); 140 dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 141 dw9714_dev->sd.internal_ops = &dw9714_int_ops; 142 143 rval = dw9714_init_controls(dw9714_dev); 144 if (rval) 145 goto err_cleanup; 146 147 rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL); 148 if (rval < 0) 149 goto err_cleanup; 150 151 dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS; 152 153 rval = v4l2_async_register_subdev(&dw9714_dev->sd); 154 if (rval < 0) 155 goto err_cleanup; 156 157 pm_runtime_set_active(&client->dev); 158 pm_runtime_enable(&client->dev); 159 pm_runtime_idle(&client->dev); 160 161 return 0; 162 163 err_cleanup: 164 v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm); 165 media_entity_cleanup(&dw9714_dev->sd.entity); 166 167 return rval; 168 } 169 170 static int dw9714_remove(struct i2c_client *client) 171 { 172 struct v4l2_subdev *sd = i2c_get_clientdata(client); 173 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 174 175 pm_runtime_disable(&client->dev); 176 dw9714_subdev_cleanup(dw9714_dev); 177 178 return 0; 179 } 180 181 /* 182 * This function sets the vcm position, so it consumes least current 183 * The lens position is gradually moved in units of DW9714_CTRL_STEPS, 184 * to make the movements smoothly. 185 */ 186 static int __maybe_unused dw9714_vcm_suspend(struct device *dev) 187 { 188 struct i2c_client *client = to_i2c_client(dev); 189 struct v4l2_subdev *sd = i2c_get_clientdata(client); 190 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 191 int ret, val; 192 193 for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1); 194 val >= 0; val -= DW9714_CTRL_STEPS) { 195 ret = dw9714_i2c_write(client, 196 DW9714_VAL(val, DW9714_DEFAULT_S)); 197 if (ret) 198 dev_err_once(dev, "%s I2C failure: %d", __func__, ret); 199 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); 200 } 201 return 0; 202 } 203 204 /* 205 * This function sets the vcm position to the value set by the user 206 * through v4l2_ctrl_ops s_ctrl handler 207 * The lens position is gradually moved in units of DW9714_CTRL_STEPS, 208 * to make the movements smoothly. 209 */ 210 static int __maybe_unused dw9714_vcm_resume(struct device *dev) 211 { 212 struct i2c_client *client = to_i2c_client(dev); 213 struct v4l2_subdev *sd = i2c_get_clientdata(client); 214 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); 215 int ret, val; 216 217 for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS; 218 val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1; 219 val += DW9714_CTRL_STEPS) { 220 ret = dw9714_i2c_write(client, 221 DW9714_VAL(val, DW9714_DEFAULT_S)); 222 if (ret) 223 dev_err_ratelimited(dev, "%s I2C failure: %d", 224 __func__, ret); 225 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); 226 } 227 228 return 0; 229 } 230 231 static const struct i2c_device_id dw9714_id_table[] = { 232 { DW9714_NAME, 0 }, 233 { { 0 } } 234 }; 235 MODULE_DEVICE_TABLE(i2c, dw9714_id_table); 236 237 static const struct of_device_id dw9714_of_table[] = { 238 { .compatible = "dongwoon,dw9714" }, 239 { { 0 } } 240 }; 241 MODULE_DEVICE_TABLE(of, dw9714_of_table); 242 243 static const struct dev_pm_ops dw9714_pm_ops = { 244 SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume) 245 SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL) 246 }; 247 248 static struct i2c_driver dw9714_i2c_driver = { 249 .driver = { 250 .name = DW9714_NAME, 251 .pm = &dw9714_pm_ops, 252 .of_match_table = dw9714_of_table, 253 }, 254 .probe_new = dw9714_probe, 255 .remove = dw9714_remove, 256 .id_table = dw9714_id_table, 257 }; 258 259 module_i2c_driver(dw9714_i2c_driver); 260 261 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>"); 262 MODULE_AUTHOR("Jian Xu Zheng"); 263 MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>"); 264 MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>"); 265 MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>"); 266 MODULE_DESCRIPTION("DW9714 VCM driver"); 267 MODULE_LICENSE("GPL v2"); 268