dw9768.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 dw9768_aac_mode_ot_multi {
  106. u32 aac_mode_enum;
  107. u32 ot_multi_base100;
  108. };
  109. struct dw9768_clk_presc_dividing_rate {
  110. u32 clk_presc_enum;
  111. u32 dividing_rate_base100;
  112. };
  113. static const struct dw9768_aac_mode_ot_multi aac_mode_ot_multi[] = {
  114. {1, 48},
  115. {2, 70},
  116. {3, 75},
  117. {5, 113},
  118. };
  119. static const struct dw9768_clk_presc_dividing_rate presc_dividing_rate[] = {
  120. {0, 200},
  121. {1, 100},
  122. {2, 50},
  123. {3, 25},
  124. {4, 800},
  125. {5, 400},
  126. };
  127. static u32 dw9768_find_ot_multi(u32 aac_mode_param)
  128. {
  129. u32 cur_ot_multi_base100 = 70;
  130. unsigned int i;
  131. for (i = 0; i < ARRAY_SIZE(aac_mode_ot_multi); i++) {
  132. if (aac_mode_ot_multi[i].aac_mode_enum == aac_mode_param) {
  133. cur_ot_multi_base100 =
  134. aac_mode_ot_multi[i].ot_multi_base100;
  135. }
  136. }
  137. return cur_ot_multi_base100;
  138. }
  139. static u32 dw9768_find_dividing_rate(u32 presc_param)
  140. {
  141. u32 cur_clk_dividing_rate_base100 = 100;
  142. unsigned int i;
  143. for (i = 0; i < ARRAY_SIZE(presc_dividing_rate); i++) {
  144. if (presc_dividing_rate[i].clk_presc_enum == presc_param) {
  145. cur_clk_dividing_rate_base100 =
  146. presc_dividing_rate[i].dividing_rate_base100;
  147. }
  148. }
  149. return cur_clk_dividing_rate_base100;
  150. }
  151. /*
  152. * DW9768_AAC_PRESC_REG & DW9768_AAC_TIME_REG determine VCM operation time.
  153. * For current VCM mode: AAC3, Operation Time would be 0.70 x Tvib.
  154. * Tvib = (6.3ms + AACT[5:0] * 0.1MS) * Dividing Rate.
  155. * Below is calculation of the operation delay for each step.
  156. */
  157. static inline u32 dw9768_cal_move_delay(u32 aac_mode_param, u32 presc_param,
  158. u32 aac_timing_param)
  159. {
  160. u32 Tvib_us;
  161. u32 ot_multi_base100;
  162. u32 clk_dividing_rate_base100;
  163. ot_multi_base100 = dw9768_find_ot_multi(aac_mode_param);
  164. clk_dividing_rate_base100 = dw9768_find_dividing_rate(presc_param);
  165. Tvib_us = (DW9768_TVIB_MS_BASE10 + aac_timing_param) *
  166. clk_dividing_rate_base100;
  167. return Tvib_us * ot_multi_base100 / 100;
  168. }
  169. static int dw9768_mod_reg(struct dw9768 *dw9768, u8 reg, u8 mask, u8 val)
  170. {
  171. struct i2c_client *client = v4l2_get_subdevdata(&dw9768->sd);
  172. int ret;
  173. ret = i2c_smbus_read_byte_data(client, reg);
  174. if (ret < 0)
  175. return ret;
  176. val = ((unsigned char)ret & ~mask) | (val & mask);
  177. return i2c_smbus_write_byte_data(client, reg, val);
  178. }
  179. static int dw9768_set_dac(struct dw9768 *dw9768, u16 val)
  180. {
  181. struct i2c_client *client = v4l2_get_subdevdata(&dw9768->sd);
  182. /* Write VCM position to registers */
  183. return i2c_smbus_write_word_swapped(client, DW9768_MSB_ADDR, val);
  184. }
  185. static int dw9768_init(struct dw9768 *dw9768)
  186. {
  187. struct i2c_client *client = v4l2_get_subdevdata(&dw9768->sd);
  188. int ret, val;
  189. /* Reset DW9768_RING_PD_CONTROL_REG to default status 0x00 */
  190. ret = i2c_smbus_write_byte_data(client, DW9768_RING_PD_CONTROL_REG,
  191. DW9768_PD_MODE_OFF);
  192. if (ret < 0)
  193. return ret;
  194. /*
  195. * DW9769 requires waiting delay time of t_OPR
  196. * after PD reset takes place.
  197. */
  198. usleep_range(DW9768_T_OPR_US, DW9768_T_OPR_US + 100);
  199. /* Set DW9768_RING_PD_CONTROL_REG to DW9768_AAC_MODE_EN(0x01) */
  200. ret = i2c_smbus_write_byte_data(client, DW9768_RING_PD_CONTROL_REG,
  201. DW9768_AAC_MODE_EN);
  202. if (ret < 0)
  203. return ret;
  204. /* Set AAC mode */
  205. ret = dw9768_mod_reg(dw9768, DW9768_AAC_PRESC_REG,
  206. DW9768_AAC_MODE_SEL_MASK,
  207. dw9768->aac_mode << 5);
  208. if (ret < 0)
  209. return ret;
  210. /* Set clock presc */
  211. if (dw9768->clock_presc != DW9768_CLOCK_PRE_SCALE_DEFAULT) {
  212. ret = dw9768_mod_reg(dw9768, DW9768_AAC_PRESC_REG,
  213. DW9768_CLOCK_PRE_SCALE_SEL_MASK,
  214. dw9768->clock_presc);
  215. if (ret < 0)
  216. return ret;
  217. }
  218. /* Set AAC Timing */
  219. if (dw9768->aac_timing != DW9768_AAC_TIME_DEFAULT) {
  220. ret = i2c_smbus_write_byte_data(client, DW9768_AAC_TIME_REG,
  221. dw9768->aac_timing);
  222. if (ret < 0)
  223. return ret;
  224. }
  225. for (val = dw9768->focus->val % DW9768_MOVE_STEPS;
  226. val <= dw9768->focus->val;
  227. val += DW9768_MOVE_STEPS) {
  228. ret = dw9768_set_dac(dw9768, val);
  229. if (ret) {
  230. dev_err(&client->dev, "I2C failure: %d", ret);
  231. return ret;
  232. }
  233. usleep_range(dw9768->move_delay_us,
  234. dw9768->move_delay_us + 1000);
  235. }
  236. return 0;
  237. }
  238. static int dw9768_release(struct dw9768 *dw9768)
  239. {
  240. struct i2c_client *client = v4l2_get_subdevdata(&dw9768->sd);
  241. int ret, val;
  242. val = round_down(dw9768->focus->val, DW9768_MOVE_STEPS);
  243. for ( ; val >= 0; val -= DW9768_MOVE_STEPS) {
  244. ret = dw9768_set_dac(dw9768, val);
  245. if (ret) {
  246. dev_err(&client->dev, "I2C write fail: %d", ret);
  247. return ret;
  248. }
  249. usleep_range(dw9768->move_delay_us,
  250. dw9768->move_delay_us + 1000);
  251. }
  252. ret = i2c_smbus_write_byte_data(client, DW9768_RING_PD_CONTROL_REG,
  253. DW9768_PD_MODE_EN);
  254. if (ret < 0)
  255. return ret;
  256. /*
  257. * DW9769 requires waiting delay time of t_OPR
  258. * after PD reset takes place.
  259. */
  260. usleep_range(DW9768_T_OPR_US, DW9768_T_OPR_US + 100);
  261. return 0;
  262. }
  263. static int dw9768_runtime_suspend(struct device *dev)
  264. {
  265. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  266. struct dw9768 *dw9768 = sd_to_dw9768(sd);
  267. dw9768_release(dw9768);
  268. regulator_bulk_disable(ARRAY_SIZE(dw9768_supply_names),
  269. dw9768->supplies);
  270. return 0;
  271. }
  272. static int dw9768_runtime_resume(struct device *dev)
  273. {
  274. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  275. struct dw9768 *dw9768 = sd_to_dw9768(sd);
  276. int ret;
  277. ret = regulator_bulk_enable(ARRAY_SIZE(dw9768_supply_names),
  278. dw9768->supplies);
  279. if (ret < 0) {
  280. dev_err(dev, "failed to enable regulators\n");
  281. return ret;
  282. }
  283. /*
  284. * The datasheet refers to t_OPR that needs to be waited before sending
  285. * I2C commands after power-up.
  286. */
  287. usleep_range(DW9768_T_OPR_US, DW9768_T_OPR_US + 100);
  288. ret = dw9768_init(dw9768);
  289. if (ret < 0)
  290. goto disable_regulator;
  291. return 0;
  292. disable_regulator:
  293. regulator_bulk_disable(ARRAY_SIZE(dw9768_supply_names),
  294. dw9768->supplies);
  295. return ret;
  296. }
  297. static int dw9768_set_ctrl(struct v4l2_ctrl *ctrl)
  298. {
  299. struct dw9768 *dw9768 = container_of(ctrl->handler,
  300. struct dw9768, ctrls);
  301. if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
  302. return dw9768_set_dac(dw9768, ctrl->val);
  303. return 0;
  304. }
  305. static const struct v4l2_ctrl_ops dw9768_ctrl_ops = {
  306. .s_ctrl = dw9768_set_ctrl,
  307. };
  308. static int dw9768_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  309. {
  310. return pm_runtime_resume_and_get(sd->dev);
  311. }
  312. static int dw9768_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  313. {
  314. pm_runtime_put_autosuspend(sd->dev);
  315. return 0;
  316. }
  317. static const struct v4l2_subdev_internal_ops dw9768_int_ops = {
  318. .open = dw9768_open,
  319. .close = dw9768_close,
  320. };
  321. static const struct v4l2_subdev_ops dw9768_ops = { };
  322. static int dw9768_init_controls(struct dw9768 *dw9768)
  323. {
  324. struct v4l2_ctrl_handler *hdl = &dw9768->ctrls;
  325. const struct v4l2_ctrl_ops *ops = &dw9768_ctrl_ops;
  326. v4l2_ctrl_handler_init(hdl, 1);
  327. dw9768->focus = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE, 0,
  328. DW9768_MAX_FOCUS_POS,
  329. DW9768_FOCUS_STEPS, 0);
  330. if (hdl->error)
  331. return hdl->error;
  332. dw9768->sd.ctrl_handler = hdl;
  333. return 0;
  334. }
  335. static int dw9768_probe(struct i2c_client *client)
  336. {
  337. struct device *dev = &client->dev;
  338. struct dw9768 *dw9768;
  339. bool full_power;
  340. unsigned int i;
  341. int ret;
  342. dw9768 = devm_kzalloc(dev, sizeof(*dw9768), GFP_KERNEL);
  343. if (!dw9768)
  344. return -ENOMEM;
  345. /* Initialize subdev */
  346. v4l2_i2c_subdev_init(&dw9768->sd, client, &dw9768_ops);
  347. dw9768->aac_mode = DW9768_AAC_MODE_DEFAULT;
  348. dw9768->aac_timing = DW9768_AAC_TIME_DEFAULT;
  349. dw9768->clock_presc = DW9768_CLOCK_PRE_SCALE_DEFAULT;
  350. /* Optional indication of AAC mode select */
  351. fwnode_property_read_u32(dev_fwnode(dev), "dongwoon,aac-mode",
  352. &dw9768->aac_mode);
  353. /* Optional indication of clock pre-scale select */
  354. fwnode_property_read_u32(dev_fwnode(dev), "dongwoon,clock-presc",
  355. &dw9768->clock_presc);
  356. /* Optional indication of AAC Timing */
  357. fwnode_property_read_u32(dev_fwnode(dev), "dongwoon,aac-timing",
  358. &dw9768->aac_timing);
  359. dw9768->move_delay_us = dw9768_cal_move_delay(dw9768->aac_mode,
  360. dw9768->clock_presc,
  361. dw9768->aac_timing);
  362. for (i = 0; i < ARRAY_SIZE(dw9768_supply_names); i++)
  363. dw9768->supplies[i].supply = dw9768_supply_names[i];
  364. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(dw9768_supply_names),
  365. dw9768->supplies);
  366. if (ret) {
  367. dev_err(dev, "failed to get regulators\n");
  368. return ret;
  369. }
  370. /* Initialize controls */
  371. ret = dw9768_init_controls(dw9768);
  372. if (ret)
  373. goto err_free_handler;
  374. /* Initialize subdev */
  375. dw9768->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  376. dw9768->sd.internal_ops = &dw9768_int_ops;
  377. ret = media_entity_pads_init(&dw9768->sd.entity, 0, NULL);
  378. if (ret < 0)
  379. goto err_free_handler;
  380. dw9768->sd.entity.function = MEDIA_ENT_F_LENS;
  381. /*
  382. * Figure out whether we're going to power up the device here. Generally
  383. * this is done if CONFIG_PM is disabled in a DT system or the device is
  384. * to be powered on in an ACPI system. Similarly for power off in
  385. * remove.
  386. */
  387. full_power = (is_acpi_node(dev_fwnode(dev)) &&
  388. acpi_dev_state_d0(dev)) ||
  389. (is_of_node(dev_fwnode(dev)) && !IS_ENABLED(CONFIG_PM));
  390. if (full_power) {
  391. ret = dw9768_runtime_resume(dev);
  392. if (ret < 0) {
  393. dev_err(dev, "failed to power on: %d\n", ret);
  394. goto err_clean_entity;
  395. }
  396. pm_runtime_set_active(dev);
  397. }
  398. pm_runtime_enable(dev);
  399. ret = v4l2_async_register_subdev(&dw9768->sd);
  400. if (ret < 0) {
  401. dev_err(dev, "failed to register V4L2 subdev: %d", ret);
  402. goto err_power_off;
  403. }
  404. pm_runtime_set_autosuspend_delay(dev, 1000);
  405. pm_runtime_use_autosuspend(dev);
  406. pm_runtime_idle(dev);
  407. return 0;
  408. err_power_off:
  409. pm_runtime_disable(dev);
  410. if (full_power) {
  411. dw9768_runtime_suspend(dev);
  412. pm_runtime_set_suspended(dev);
  413. }
  414. err_clean_entity:
  415. media_entity_cleanup(&dw9768->sd.entity);
  416. err_free_handler:
  417. v4l2_ctrl_handler_free(&dw9768->ctrls);
  418. return ret;
  419. }
  420. static void dw9768_remove(struct i2c_client *client)
  421. {
  422. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  423. struct dw9768 *dw9768 = sd_to_dw9768(sd);
  424. struct device *dev = &client->dev;
  425. v4l2_async_unregister_subdev(&dw9768->sd);
  426. v4l2_ctrl_handler_free(&dw9768->ctrls);
  427. media_entity_cleanup(&dw9768->sd.entity);
  428. pm_runtime_disable(dev);
  429. if ((is_acpi_node(dev_fwnode(dev)) && acpi_dev_state_d0(dev)) ||
  430. (is_of_node(dev_fwnode(dev)) && !IS_ENABLED(CONFIG_PM))) {
  431. dw9768_runtime_suspend(dev);
  432. pm_runtime_set_suspended(dev);
  433. }
  434. }
  435. static const struct of_device_id dw9768_of_table[] = {
  436. { .compatible = "dongwoon,dw9768" },
  437. { .compatible = "giantec,gt9769" },
  438. {}
  439. };
  440. MODULE_DEVICE_TABLE(of, dw9768_of_table);
  441. static const struct dev_pm_ops dw9768_pm_ops = {
  442. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  443. pm_runtime_force_resume)
  444. SET_RUNTIME_PM_OPS(dw9768_runtime_suspend, dw9768_runtime_resume, NULL)
  445. };
  446. static struct i2c_driver dw9768_i2c_driver = {
  447. .driver = {
  448. .name = DW9768_NAME,
  449. .pm = &dw9768_pm_ops,
  450. .of_match_table = dw9768_of_table,
  451. },
  452. .probe = dw9768_probe,
  453. .remove = dw9768_remove,
  454. };
  455. module_i2c_driver(dw9768_i2c_driver);
  456. MODULE_AUTHOR("Dongchun Zhu <dongchun.zhu@mediatek.com>");
  457. MODULE_DESCRIPTION("DW9768 VCM driver");
  458. MODULE_LICENSE("GPL v2");