dw9768.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2020 MediaTek Inc.
  3. #include <linux/delay.h>
  4. #include <linux/i2c.h>
  5. #include <linux/module.h>
  6. #include <linux/pm_runtime.h>
  7. #include <linux/regulator/consumer.h>
  8. #include <media/v4l2-async.h>
  9. #include <media/v4l2-ctrls.h>
  10. #include <media/v4l2-device.h>
  11. #include <media/v4l2-fwnode.h>
  12. #include <media/v4l2-subdev.h>
  13. #define DW9768_NAME "dw9768"
  14. #define DW9768_MAX_FOCUS_POS (1024 - 1)
  15. /*
  16. * This sets the minimum granularity for the focus positions.
  17. * A value of 1 gives maximum accuracy for a desired focus position
  18. */
  19. #define DW9768_FOCUS_STEPS 1
  20. /*
  21. * Ring control and Power control register
  22. * Bit[1] RING_EN
  23. * 0: Direct mode
  24. * 1: AAC mode (ringing control mode)
  25. * Bit[0] PD
  26. * 0: Normal operation mode
  27. * 1: Power down mode
  28. * DW9768 requires waiting time of Topr after PD reset takes place.
  29. */
  30. #define DW9768_RING_PD_CONTROL_REG 0x02
  31. #define DW9768_PD_MODE_OFF 0x00
  32. #define DW9768_PD_MODE_EN BIT(0)
  33. #define DW9768_AAC_MODE_EN BIT(1)
  34. /*
  35. * DW9768 separates two registers to control the VCM position.
  36. * One for MSB value, another is LSB value.
  37. * DAC_MSB: D[9:8] (ADD: 0x03)
  38. * DAC_LSB: D[7:0] (ADD: 0x04)
  39. * D[9:0] DAC data input: positive output current = D[9:0] / 1023 * 100[mA]
  40. */
  41. #define DW9768_MSB_ADDR 0x03
  42. #define DW9768_LSB_ADDR 0x04
  43. #define DW9768_STATUS_ADDR 0x05
  44. /*
  45. * AAC mode control & prescale register
  46. * Bit[7:5] Namely AC[2:0], decide the VCM mode and operation time.
  47. * 001 AAC2 0.48 x Tvib
  48. * 010 AAC3 0.70 x Tvib
  49. * 011 AAC4 0.75 x Tvib
  50. * 101 AAC8 1.13 x Tvib
  51. * Bit[2:0] Namely PRESC[2:0], set the internal clock dividing rate as follow.
  52. * 000 2
  53. * 001 1
  54. * 010 1/2
  55. * 011 1/4
  56. * 100 8
  57. * 101 4
  58. */
  59. #define DW9768_AAC_PRESC_REG 0x06
  60. #define DW9768_AAC_MODE_SEL_MASK GENMASK(7, 5)
  61. #define DW9768_CLOCK_PRE_SCALE_SEL_MASK GENMASK(2, 0)
  62. /*
  63. * VCM period of vibration register
  64. * Bit[5:0] Defined as VCM rising periodic time (Tvib) together with PRESC[2:0]
  65. * Tvib = (6.3ms + AACT[5:0] * 0.1ms) * Dividing Rate
  66. * Dividing Rate is the internal clock dividing rate that is defined at
  67. * PRESCALE register (ADD: 0x06)
  68. */
  69. #define DW9768_AAC_TIME_REG 0x07
  70. /*
  71. * DW9768 requires waiting time (delay time) of t_OPR after power-up,
  72. * or in the case of PD reset taking place.
  73. */
  74. #define DW9768_T_OPR_US 1000
  75. #define DW9768_TVIB_MS_BASE10 (64 - 1)
  76. #define DW9768_AAC_MODE_DEFAULT 2
  77. #define DW9768_AAC_TIME_DEFAULT 0x20
  78. #define DW9768_CLOCK_PRE_SCALE_DEFAULT 1
  79. /*
  80. * This acts as the minimum granularity of lens movement.
  81. * Keep this value power of 2, so the control steps can be
  82. * uniformly adjusted for gradual lens movement, with desired
  83. * number of control steps.
  84. */
  85. #define DW9768_MOVE_STEPS 16
  86. static const char * const dw9768_supply_names[] = {
  87. "vin", /* Digital I/O power */
  88. "vdd", /* Digital core power */
  89. };
  90. /* dw9768 device structure */
  91. struct dw9768 {
  92. struct regulator_bulk_data supplies[ARRAY_SIZE(dw9768_supply_names)];
  93. struct v4l2_ctrl_handler ctrls;
  94. struct v4l2_ctrl *focus;
  95. struct v4l2_subdev sd;
  96. u32 aac_mode;
  97. u32 aac_timing;
  98. u32 clock_presc;
  99. u32 move_delay_us;
  100. };
  101. static inline struct dw9768 *sd_to_dw9768(struct v4l2_subdev *subdev)
  102. {
  103. return container_of(subdev, struct dw9768, sd);
  104. }
  105. struct regval_list {
  106. u8 reg_num;
  107. u8 value;
  108. };
  109. struct dw9768_aac_mode_ot_multi {
  110. u32 aac_mode_enum;
  111. u32 ot_multi_base100;
  112. };
  113. struct dw9768_clk_presc_dividing_rate {
  114. u32 clk_presc_enum;
  115. u32 dividing_rate_base100;
  116. };
  117. static const struct dw9768_aac_mode_ot_multi aac_mode_ot_multi[] = {
  118. {1, 48},
  119. {2, 70},
  120. {3, 75},
  121. {5, 113},
  122. };
  123. static const struct dw9768_clk_presc_dividing_rate presc_dividing_rate[] = {
  124. {0, 200},
  125. {1, 100},
  126. {2, 50},
  127. {3, 25},
  128. {4, 800},
  129. {5, 400},
  130. };
  131. static u32 dw9768_find_ot_multi(u32 aac_mode_param)
  132. {
  133. u32 cur_ot_multi_base100 = 70;
  134. unsigned int i;
  135. for (i = 0; i < ARRAY_SIZE(aac_mode_ot_multi); i++) {
  136. if (aac_mode_ot_multi[i].aac_mode_enum == aac_mode_param) {
  137. cur_ot_multi_base100 =
  138. aac_mode_ot_multi[i].ot_multi_base100;
  139. }
  140. }
  141. return cur_ot_multi_base100;
  142. }
  143. static u32 dw9768_find_dividing_rate(u32 presc_param)
  144. {
  145. u32 cur_clk_dividing_rate_base100 = 100;
  146. unsigned int i;
  147. for (i = 0; i < ARRAY_SIZE(presc_dividing_rate); i++) {
  148. if (presc_dividing_rate[i].clk_presc_enum == presc_param) {
  149. cur_clk_dividing_rate_base100 =
  150. presc_dividing_rate[i].dividing_rate_base100;
  151. }
  152. }
  153. return cur_clk_dividing_rate_base100;
  154. }
  155. /*
  156. * DW9768_AAC_PRESC_REG & DW9768_AAC_TIME_REG determine VCM operation time.
  157. * For current VCM mode: AAC3, Operation Time would be 0.70 x Tvib.
  158. * Tvib = (6.3ms + AACT[5:0] * 0.1MS) * Dividing Rate.
  159. * Below is calculation of the operation delay for each step.
  160. */
  161. static inline u32 dw9768_cal_move_delay(u32 aac_mode_param, u32 presc_param,
  162. u32 aac_timing_param)
  163. {
  164. u32 Tvib_us;
  165. u32 ot_multi_base100;
  166. u32 clk_dividing_rate_base100;
  167. ot_multi_base100 = dw9768_find_ot_multi(aac_mode_param);
  168. clk_dividing_rate_base100 = dw9768_find_dividing_rate(presc_param);
  169. Tvib_us = (DW9768_TVIB_MS_BASE10 + aac_timing_param) *
  170. clk_dividing_rate_base100;
  171. return Tvib_us * ot_multi_base100 / 100;
  172. }
  173. static int dw9768_mod_reg(struct dw9768 *dw9768, u8 reg, u8 mask, u8 val)
  174. {
  175. struct i2c_client *client = v4l2_get_subdevdata(&dw9768->sd);
  176. int ret;
  177. ret = i2c_smbus_read_byte_data(client, reg);
  178. if (ret < 0)
  179. return ret;
  180. val = ((unsigned char)ret & ~mask) | (val & mask);
  181. return i2c_smbus_write_byte_data(client, reg, val);
  182. }
  183. static int dw9768_set_dac(struct dw9768 *dw9768, u16 val)
  184. {
  185. struct i2c_client *client = v4l2_get_subdevdata(&dw9768->sd);
  186. /* Write VCM position to registers */
  187. return i2c_smbus_write_word_swapped(client, DW9768_MSB_ADDR, val);
  188. }
  189. static int dw9768_init(struct dw9768 *dw9768)
  190. {
  191. struct i2c_client *client = v4l2_get_subdevdata(&dw9768->sd);
  192. int ret, val;
  193. /* Reset DW9768_RING_PD_CONTROL_REG to default status 0x00 */
  194. ret = i2c_smbus_write_byte_data(client, DW9768_RING_PD_CONTROL_REG,
  195. DW9768_PD_MODE_OFF);
  196. if (ret < 0)
  197. return ret;
  198. /*
  199. * DW9769 requires waiting delay time of t_OPR
  200. * after PD reset takes place.
  201. */
  202. usleep_range(DW9768_T_OPR_US, DW9768_T_OPR_US + 100);
  203. /* Set DW9768_RING_PD_CONTROL_REG to DW9768_AAC_MODE_EN(0x01) */
  204. ret = i2c_smbus_write_byte_data(client, DW9768_RING_PD_CONTROL_REG,
  205. DW9768_AAC_MODE_EN);
  206. if (ret < 0)
  207. return ret;
  208. /* Set AAC mode */
  209. ret = dw9768_mod_reg(dw9768, DW9768_AAC_PRESC_REG,
  210. DW9768_AAC_MODE_SEL_MASK,
  211. dw9768->aac_mode << 5);
  212. if (ret < 0)
  213. return ret;
  214. /* Set clock presc */
  215. if (dw9768->clock_presc != DW9768_CLOCK_PRE_SCALE_DEFAULT) {
  216. ret = dw9768_mod_reg(dw9768, DW9768_AAC_PRESC_REG,
  217. DW9768_CLOCK_PRE_SCALE_SEL_MASK,
  218. dw9768->clock_presc);
  219. if (ret < 0)
  220. return ret;
  221. }
  222. /* Set AAC Timing */
  223. if (dw9768->aac_timing != DW9768_AAC_TIME_DEFAULT) {
  224. ret = i2c_smbus_write_byte_data(client, DW9768_AAC_TIME_REG,
  225. dw9768->aac_timing);
  226. if (ret < 0)
  227. return ret;
  228. }
  229. for (val = dw9768->focus->val % DW9768_MOVE_STEPS;
  230. val <= dw9768->focus->val;
  231. val += DW9768_MOVE_STEPS) {
  232. ret = dw9768_set_dac(dw9768, val);
  233. if (ret) {
  234. dev_err(&client->dev, "I2C failure: %d", ret);
  235. return ret;
  236. }
  237. usleep_range(dw9768->move_delay_us,
  238. dw9768->move_delay_us + 1000);
  239. }
  240. return 0;
  241. }
  242. static int dw9768_release(struct dw9768 *dw9768)
  243. {
  244. struct i2c_client *client = v4l2_get_subdevdata(&dw9768->sd);
  245. int ret, val;
  246. val = round_down(dw9768->focus->val, DW9768_MOVE_STEPS);
  247. for ( ; val >= 0; val -= DW9768_MOVE_STEPS) {
  248. ret = dw9768_set_dac(dw9768, val);
  249. if (ret) {
  250. dev_err(&client->dev, "I2C write fail: %d", ret);
  251. return ret;
  252. }
  253. usleep_range(dw9768->move_delay_us,
  254. dw9768->move_delay_us + 1000);
  255. }
  256. ret = i2c_smbus_write_byte_data(client, DW9768_RING_PD_CONTROL_REG,
  257. DW9768_PD_MODE_EN);
  258. if (ret < 0)
  259. return ret;
  260. /*
  261. * DW9769 requires waiting delay time of t_OPR
  262. * after PD reset takes place.
  263. */
  264. usleep_range(DW9768_T_OPR_US, DW9768_T_OPR_US + 100);
  265. return 0;
  266. }
  267. static int dw9768_runtime_suspend(struct device *dev)
  268. {
  269. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  270. struct dw9768 *dw9768 = sd_to_dw9768(sd);
  271. dw9768_release(dw9768);
  272. regulator_bulk_disable(ARRAY_SIZE(dw9768_supply_names),
  273. dw9768->supplies);
  274. return 0;
  275. }
  276. static int dw9768_runtime_resume(struct device *dev)
  277. {
  278. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  279. struct dw9768 *dw9768 = sd_to_dw9768(sd);
  280. int ret;
  281. ret = regulator_bulk_enable(ARRAY_SIZE(dw9768_supply_names),
  282. dw9768->supplies);
  283. if (ret < 0) {
  284. dev_err(dev, "failed to enable regulators\n");
  285. return ret;
  286. }
  287. /*
  288. * The datasheet refers to t_OPR that needs to be waited before sending
  289. * I2C commands after power-up.
  290. */
  291. usleep_range(DW9768_T_OPR_US, DW9768_T_OPR_US + 100);
  292. ret = dw9768_init(dw9768);
  293. if (ret < 0)
  294. goto disable_regulator;
  295. return 0;
  296. disable_regulator:
  297. regulator_bulk_disable(ARRAY_SIZE(dw9768_supply_names),
  298. dw9768->supplies);
  299. return ret;
  300. }
  301. static int dw9768_set_ctrl(struct v4l2_ctrl *ctrl)
  302. {
  303. struct dw9768 *dw9768 = container_of(ctrl->handler,
  304. struct dw9768, ctrls);
  305. if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
  306. return dw9768_set_dac(dw9768, ctrl->val);
  307. return 0;
  308. }
  309. static const struct v4l2_ctrl_ops dw9768_ctrl_ops = {
  310. .s_ctrl = dw9768_set_ctrl,
  311. };
  312. static int dw9768_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  313. {
  314. return pm_runtime_resume_and_get(sd->dev);
  315. }
  316. static int dw9768_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  317. {
  318. pm_runtime_put(sd->dev);
  319. return 0;
  320. }
  321. static const struct v4l2_subdev_internal_ops dw9768_int_ops = {
  322. .open = dw9768_open,
  323. .close = dw9768_close,
  324. };
  325. static const struct v4l2_subdev_ops dw9768_ops = { };
  326. static int dw9768_init_controls(struct dw9768 *dw9768)
  327. {
  328. struct v4l2_ctrl_handler *hdl = &dw9768->ctrls;
  329. const struct v4l2_ctrl_ops *ops = &dw9768_ctrl_ops;
  330. v4l2_ctrl_handler_init(hdl, 1);
  331. dw9768->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE, 0,
  332. DW9768_MAX_FOCUS_POS,
  333. DW9768_FOCUS_STEPS, 0);
  334. if (hdl->error)
  335. return hdl->error;
  336. dw9768->sd.ctrl_handler = hdl;
  337. return 0;
  338. }
  339. static int dw9768_probe(struct i2c_client *client)
  340. {
  341. struct device *dev = &client->dev;
  342. struct dw9768 *dw9768;
  343. bool full_power;
  344. unsigned int i;
  345. int ret;
  346. dw9768 = devm_kzalloc(dev, sizeof(*dw9768), GFP_KERNEL);
  347. if (!dw9768)
  348. return -ENOMEM;
  349. /* Initialize subdev */
  350. v4l2_i2c_subdev_init(&dw9768->sd, client, &dw9768_ops);
  351. dw9768->aac_mode = DW9768_AAC_MODE_DEFAULT;
  352. dw9768->aac_timing = DW9768_AAC_TIME_DEFAULT;
  353. dw9768->clock_presc = DW9768_CLOCK_PRE_SCALE_DEFAULT;
  354. /* Optional indication of AAC mode select */
  355. fwnode_property_read_u32(dev_fwnode(dev), "dongwoon,aac-mode",
  356. &dw9768->aac_mode);
  357. /* Optional indication of clock pre-scale select */
  358. fwnode_property_read_u32(dev_fwnode(dev), "dongwoon,clock-presc",
  359. &dw9768->clock_presc);
  360. /* Optional indication of AAC Timing */
  361. fwnode_property_read_u32(dev_fwnode(dev), "dongwoon,aac-timing",
  362. &dw9768->aac_timing);
  363. dw9768->move_delay_us = dw9768_cal_move_delay(dw9768->aac_mode,
  364. dw9768->clock_presc,
  365. dw9768->aac_timing);
  366. for (i = 0; i < ARRAY_SIZE(dw9768_supply_names); i++)
  367. dw9768->supplies[i].supply = dw9768_supply_names[i];
  368. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(dw9768_supply_names),
  369. dw9768->supplies);
  370. if (ret) {
  371. dev_err(dev, "failed to get regulators\n");
  372. return ret;
  373. }
  374. /* Initialize controls */
  375. ret = dw9768_init_controls(dw9768);
  376. if (ret)
  377. goto err_free_handler;
  378. /* Initialize subdev */
  379. dw9768->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  380. dw9768->sd.internal_ops = &dw9768_int_ops;
  381. ret = media_entity_pads_init(&dw9768->sd.entity, 0, NULL);
  382. if (ret < 0)
  383. goto err_free_handler;
  384. dw9768->sd.entity.function = MEDIA_ENT_F_LENS;
  385. /*
  386. * Figure out whether we're going to power up the device here. Generally
  387. * this is done if CONFIG_PM is disabled in a DT system or the device is
  388. * to be powered on in an ACPI system. Similarly for power off in
  389. * remove.
  390. */
  391. pm_runtime_enable(dev);
  392. full_power = (is_acpi_node(dev_fwnode(dev)) &&
  393. acpi_dev_state_d0(dev)) ||
  394. (is_of_node(dev_fwnode(dev)) && !pm_runtime_enabled(dev));
  395. if (full_power) {
  396. ret = dw9768_runtime_resume(dev);
  397. if (ret < 0) {
  398. dev_err(dev, "failed to power on: %d\n", ret);
  399. goto err_clean_entity;
  400. }
  401. pm_runtime_set_active(dev);
  402. }
  403. ret = v4l2_async_register_subdev(&dw9768->sd);
  404. if (ret < 0) {
  405. dev_err(dev, "failed to register V4L2 subdev: %d", ret);
  406. goto err_power_off;
  407. }
  408. pm_runtime_idle(dev);
  409. return 0;
  410. err_power_off:
  411. if (full_power) {
  412. dw9768_runtime_suspend(dev);
  413. pm_runtime_set_suspended(dev);
  414. }
  415. err_clean_entity:
  416. pm_runtime_disable(dev);
  417. media_entity_cleanup(&dw9768->sd.entity);
  418. err_free_handler:
  419. v4l2_ctrl_handler_free(&dw9768->ctrls);
  420. return ret;
  421. }
  422. static void dw9768_remove(struct i2c_client *client)
  423. {
  424. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  425. struct dw9768 *dw9768 = sd_to_dw9768(sd);
  426. struct device *dev = &client->dev;
  427. v4l2_async_unregister_subdev(&dw9768->sd);
  428. v4l2_ctrl_handler_free(&dw9768->ctrls);
  429. media_entity_cleanup(&dw9768->sd.entity);
  430. if ((is_acpi_node(dev_fwnode(dev)) && acpi_dev_state_d0(dev)) ||
  431. (is_of_node(dev_fwnode(dev)) && !pm_runtime_enabled(dev))) {
  432. dw9768_runtime_suspend(dev);
  433. pm_runtime_set_suspended(dev);
  434. }
  435. pm_runtime_disable(dev);
  436. }
  437. static const struct of_device_id dw9768_of_table[] = {
  438. { .compatible = "dongwoon,dw9768" },
  439. { .compatible = "giantec,gt9769" },
  440. {}
  441. };
  442. MODULE_DEVICE_TABLE(of, dw9768_of_table);
  443. static const struct dev_pm_ops dw9768_pm_ops = {
  444. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  445. pm_runtime_force_resume)
  446. SET_RUNTIME_PM_OPS(dw9768_runtime_suspend, dw9768_runtime_resume, NULL)
  447. };
  448. static struct i2c_driver dw9768_i2c_driver = {
  449. .driver = {
  450. .name = DW9768_NAME,
  451. .pm = &dw9768_pm_ops,
  452. .of_match_table = dw9768_of_table,
  453. },
  454. .probe_new = dw9768_probe,
  455. .remove = dw9768_remove,
  456. };
  457. module_i2c_driver(dw9768_i2c_driver);
  458. MODULE_AUTHOR("Dongchun Zhu <[email protected]>");
  459. MODULE_DESCRIPTION("DW9768 VCM driver");
  460. MODULE_LICENSE("GPL v2");