rk808-regulator.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * Regulator driver for Rockchip RK808/RK818
  3. *
  4. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  5. *
  6. * Author: Chris Zhong <zyw@rock-chips.com>
  7. * Author: Zhang Qing <zhangqing@rock-chips.com>
  8. *
  9. * Copyright (C) 2016 PHYTEC Messtechnik GmbH
  10. *
  11. * Author: Wadim Egorov <w.egorov@phytec.de>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms and conditions of the GNU General Public License,
  15. * version 2, as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. * more details.
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/gpio.h>
  24. #include <linux/i2c.h>
  25. #include <linux/module.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/mfd/rk808.h>
  29. #include <linux/regulator/driver.h>
  30. #include <linux/regulator/of_regulator.h>
  31. #include <linux/gpio/consumer.h>
  32. /* Field Definitions */
  33. #define RK808_BUCK_VSEL_MASK 0x3f
  34. #define RK808_BUCK4_VSEL_MASK 0xf
  35. #define RK808_LDO_VSEL_MASK 0x1f
  36. #define RK818_BUCK_VSEL_MASK 0x3f
  37. #define RK818_BUCK4_VSEL_MASK 0x1f
  38. #define RK818_LDO_VSEL_MASK 0x1f
  39. #define RK818_LDO3_ON_VSEL_MASK 0xf
  40. #define RK818_BOOST_ON_VSEL_MASK 0xe0
  41. /* Ramp rate definitions for buck1 / buck2 only */
  42. #define RK808_RAMP_RATE_OFFSET 3
  43. #define RK808_RAMP_RATE_MASK (3 << RK808_RAMP_RATE_OFFSET)
  44. #define RK808_RAMP_RATE_2MV_PER_US (0 << RK808_RAMP_RATE_OFFSET)
  45. #define RK808_RAMP_RATE_4MV_PER_US (1 << RK808_RAMP_RATE_OFFSET)
  46. #define RK808_RAMP_RATE_6MV_PER_US (2 << RK808_RAMP_RATE_OFFSET)
  47. #define RK808_RAMP_RATE_10MV_PER_US (3 << RK808_RAMP_RATE_OFFSET)
  48. #define RK808_DVS2_POL BIT(2)
  49. #define RK808_DVS1_POL BIT(1)
  50. /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
  51. #define RK808_SLP_REG_OFFSET 1
  52. /* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
  53. #define RK808_DVS_REG_OFFSET 2
  54. /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
  55. #define RK808_SLP_SET_OFF_REG_OFFSET 2
  56. /* max steps for increase voltage of Buck1/2, equal 100mv*/
  57. #define MAX_STEPS_ONE_TIME 8
  58. #define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg, \
  59. _vmask, _ereg, _emask, _etime) \
  60. [_id] = { \
  61. .name = (_match), \
  62. .supply_name = (_supply), \
  63. .of_match = of_match_ptr(_match), \
  64. .regulators_node = of_match_ptr("regulators"), \
  65. .type = REGULATOR_VOLTAGE, \
  66. .id = (_id), \
  67. .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  68. .owner = THIS_MODULE, \
  69. .min_uV = (_min) * 1000, \
  70. .uV_step = (_step) * 1000, \
  71. .vsel_reg = (_vreg), \
  72. .vsel_mask = (_vmask), \
  73. .enable_reg = (_ereg), \
  74. .enable_mask = (_emask), \
  75. .enable_time = (_etime), \
  76. .ops = &rk808_reg_ops, \
  77. }
  78. #define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask) \
  79. [_id] = { \
  80. .name = (_match), \
  81. .supply_name = (_supply), \
  82. .of_match = of_match_ptr(_match), \
  83. .regulators_node = of_match_ptr("regulators"), \
  84. .type = REGULATOR_VOLTAGE, \
  85. .id = (_id), \
  86. .enable_reg = (_ereg), \
  87. .enable_mask = (_emask), \
  88. .owner = THIS_MODULE, \
  89. .ops = &rk808_switch_ops \
  90. }
  91. struct rk808_regulator_data {
  92. struct gpio_desc *dvs_gpio[2];
  93. };
  94. static const int rk808_buck_config_regs[] = {
  95. RK808_BUCK1_CONFIG_REG,
  96. RK808_BUCK2_CONFIG_REG,
  97. RK808_BUCK3_CONFIG_REG,
  98. RK808_BUCK4_CONFIG_REG,
  99. };
  100. static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
  101. REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
  102. REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
  103. };
  104. static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
  105. {
  106. struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
  107. int id = rdev->desc->id - RK808_ID_DCDC1;
  108. struct gpio_desc *gpio = pdata->dvs_gpio[id];
  109. unsigned int val;
  110. int ret;
  111. if (!gpio || gpiod_get_value(gpio) == 0)
  112. return regulator_get_voltage_sel_regmap(rdev);
  113. ret = regmap_read(rdev->regmap,
  114. rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
  115. &val);
  116. if (ret != 0)
  117. return ret;
  118. val &= rdev->desc->vsel_mask;
  119. val >>= ffs(rdev->desc->vsel_mask) - 1;
  120. return val;
  121. }
  122. static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
  123. unsigned sel)
  124. {
  125. int ret, delta_sel;
  126. unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
  127. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
  128. if (ret != 0)
  129. return ret;
  130. tmp = val & ~mask;
  131. old_sel = val & mask;
  132. old_sel >>= ffs(mask) - 1;
  133. delta_sel = sel - old_sel;
  134. /*
  135. * If directly modify the register to change the voltage, we will face
  136. * the risk of overshoot. Put it into a multi-step, can effectively
  137. * avoid this problem, a step is 100mv here.
  138. */
  139. while (delta_sel > MAX_STEPS_ONE_TIME) {
  140. old_sel += MAX_STEPS_ONE_TIME;
  141. val = old_sel << (ffs(mask) - 1);
  142. val |= tmp;
  143. /*
  144. * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
  145. * 3 bytes (24 bits) plus start and stop so 26 bits. So we've
  146. * got more than 65 us between each voltage change and thus
  147. * won't ramp faster than ~1500 uV / us.
  148. */
  149. ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
  150. delta_sel = sel - old_sel;
  151. }
  152. sel <<= ffs(mask) - 1;
  153. val = tmp | sel;
  154. ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
  155. /*
  156. * When we change the voltage register directly, the ramp rate is about
  157. * 100000uv/us, wait 1us to make sure the target voltage to be stable,
  158. * so we needn't wait extra time after that.
  159. */
  160. udelay(1);
  161. return ret;
  162. }
  163. static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
  164. unsigned sel)
  165. {
  166. struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
  167. int id = rdev->desc->id - RK808_ID_DCDC1;
  168. struct gpio_desc *gpio = pdata->dvs_gpio[id];
  169. unsigned int reg = rdev->desc->vsel_reg;
  170. unsigned old_sel;
  171. int ret, gpio_level;
  172. if (!gpio)
  173. return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
  174. gpio_level = gpiod_get_value(gpio);
  175. if (gpio_level == 0) {
  176. reg += RK808_DVS_REG_OFFSET;
  177. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
  178. } else {
  179. ret = regmap_read(rdev->regmap,
  180. reg + RK808_DVS_REG_OFFSET,
  181. &old_sel);
  182. }
  183. if (ret != 0)
  184. return ret;
  185. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  186. sel |= old_sel & ~rdev->desc->vsel_mask;
  187. ret = regmap_write(rdev->regmap, reg, sel);
  188. if (ret)
  189. return ret;
  190. gpiod_set_value(gpio, !gpio_level);
  191. return ret;
  192. }
  193. static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
  194. unsigned int old_selector,
  195. unsigned int new_selector)
  196. {
  197. struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
  198. int id = rdev->desc->id - RK808_ID_DCDC1;
  199. struct gpio_desc *gpio = pdata->dvs_gpio[id];
  200. /* if there is no dvs1/2 pin, we don't need wait extra time here. */
  201. if (!gpio)
  202. return 0;
  203. return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
  204. }
  205. static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  206. {
  207. unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
  208. unsigned int reg = rk808_buck_config_regs[rdev->desc->id -
  209. RK808_ID_DCDC1];
  210. switch (ramp_delay) {
  211. case 1 ... 2000:
  212. ramp_value = RK808_RAMP_RATE_2MV_PER_US;
  213. break;
  214. case 2001 ... 4000:
  215. ramp_value = RK808_RAMP_RATE_4MV_PER_US;
  216. break;
  217. case 4001 ... 6000:
  218. ramp_value = RK808_RAMP_RATE_6MV_PER_US;
  219. break;
  220. case 6001 ... 10000:
  221. break;
  222. default:
  223. pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
  224. rdev->desc->name, ramp_delay);
  225. }
  226. return regmap_update_bits(rdev->regmap, reg,
  227. RK808_RAMP_RATE_MASK, ramp_value);
  228. }
  229. static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
  230. {
  231. unsigned int reg;
  232. int sel = regulator_map_voltage_linear(rdev, uv, uv);
  233. if (sel < 0)
  234. return -EINVAL;
  235. reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
  236. return regmap_update_bits(rdev->regmap, reg,
  237. rdev->desc->vsel_mask,
  238. sel);
  239. }
  240. static int rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv)
  241. {
  242. unsigned int reg;
  243. int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
  244. if (sel < 0)
  245. return -EINVAL;
  246. reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
  247. return regmap_update_bits(rdev->regmap, reg,
  248. rdev->desc->vsel_mask,
  249. sel);
  250. }
  251. static int rk808_set_suspend_enable(struct regulator_dev *rdev)
  252. {
  253. unsigned int reg;
  254. reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
  255. return regmap_update_bits(rdev->regmap, reg,
  256. rdev->desc->enable_mask,
  257. 0);
  258. }
  259. static int rk808_set_suspend_disable(struct regulator_dev *rdev)
  260. {
  261. unsigned int reg;
  262. reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
  263. return regmap_update_bits(rdev->regmap, reg,
  264. rdev->desc->enable_mask,
  265. rdev->desc->enable_mask);
  266. }
  267. static struct regulator_ops rk808_buck1_2_ops = {
  268. .list_voltage = regulator_list_voltage_linear,
  269. .map_voltage = regulator_map_voltage_linear,
  270. .get_voltage_sel = rk808_buck1_2_get_voltage_sel_regmap,
  271. .set_voltage_sel = rk808_buck1_2_set_voltage_sel,
  272. .set_voltage_time_sel = rk808_buck1_2_set_voltage_time_sel,
  273. .enable = regulator_enable_regmap,
  274. .disable = regulator_disable_regmap,
  275. .is_enabled = regulator_is_enabled_regmap,
  276. .set_ramp_delay = rk808_set_ramp_delay,
  277. .set_suspend_voltage = rk808_set_suspend_voltage,
  278. .set_suspend_enable = rk808_set_suspend_enable,
  279. .set_suspend_disable = rk808_set_suspend_disable,
  280. };
  281. static struct regulator_ops rk808_reg_ops = {
  282. .list_voltage = regulator_list_voltage_linear,
  283. .map_voltage = regulator_map_voltage_linear,
  284. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  285. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  286. .enable = regulator_enable_regmap,
  287. .disable = regulator_disable_regmap,
  288. .is_enabled = regulator_is_enabled_regmap,
  289. .set_suspend_voltage = rk808_set_suspend_voltage,
  290. .set_suspend_enable = rk808_set_suspend_enable,
  291. .set_suspend_disable = rk808_set_suspend_disable,
  292. };
  293. static struct regulator_ops rk808_reg_ops_ranges = {
  294. .list_voltage = regulator_list_voltage_linear_range,
  295. .map_voltage = regulator_map_voltage_linear_range,
  296. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  297. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  298. .enable = regulator_enable_regmap,
  299. .disable = regulator_disable_regmap,
  300. .is_enabled = regulator_is_enabled_regmap,
  301. .set_suspend_voltage = rk808_set_suspend_voltage_range,
  302. .set_suspend_enable = rk808_set_suspend_enable,
  303. .set_suspend_disable = rk808_set_suspend_disable,
  304. };
  305. static struct regulator_ops rk808_switch_ops = {
  306. .enable = regulator_enable_regmap,
  307. .disable = regulator_disable_regmap,
  308. .is_enabled = regulator_is_enabled_regmap,
  309. .set_suspend_enable = rk808_set_suspend_enable,
  310. .set_suspend_disable = rk808_set_suspend_disable,
  311. };
  312. static const struct regulator_desc rk808_reg[] = {
  313. {
  314. .name = "DCDC_REG1",
  315. .supply_name = "vcc1",
  316. .of_match = of_match_ptr("DCDC_REG1"),
  317. .regulators_node = of_match_ptr("regulators"),
  318. .id = RK808_ID_DCDC1,
  319. .ops = &rk808_buck1_2_ops,
  320. .type = REGULATOR_VOLTAGE,
  321. .min_uV = 712500,
  322. .uV_step = 12500,
  323. .n_voltages = 64,
  324. .vsel_reg = RK808_BUCK1_ON_VSEL_REG,
  325. .vsel_mask = RK808_BUCK_VSEL_MASK,
  326. .enable_reg = RK808_DCDC_EN_REG,
  327. .enable_mask = BIT(0),
  328. .owner = THIS_MODULE,
  329. }, {
  330. .name = "DCDC_REG2",
  331. .supply_name = "vcc2",
  332. .of_match = of_match_ptr("DCDC_REG2"),
  333. .regulators_node = of_match_ptr("regulators"),
  334. .id = RK808_ID_DCDC2,
  335. .ops = &rk808_buck1_2_ops,
  336. .type = REGULATOR_VOLTAGE,
  337. .min_uV = 712500,
  338. .uV_step = 12500,
  339. .n_voltages = 64,
  340. .vsel_reg = RK808_BUCK2_ON_VSEL_REG,
  341. .vsel_mask = RK808_BUCK_VSEL_MASK,
  342. .enable_reg = RK808_DCDC_EN_REG,
  343. .enable_mask = BIT(1),
  344. .owner = THIS_MODULE,
  345. }, {
  346. .name = "DCDC_REG3",
  347. .supply_name = "vcc3",
  348. .of_match = of_match_ptr("DCDC_REG3"),
  349. .regulators_node = of_match_ptr("regulators"),
  350. .id = RK808_ID_DCDC3,
  351. .ops = &rk808_switch_ops,
  352. .type = REGULATOR_VOLTAGE,
  353. .n_voltages = 1,
  354. .enable_reg = RK808_DCDC_EN_REG,
  355. .enable_mask = BIT(2),
  356. .owner = THIS_MODULE,
  357. },
  358. RK8XX_DESC(RK808_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3300, 100,
  359. RK808_BUCK4_ON_VSEL_REG, RK808_BUCK4_VSEL_MASK,
  360. RK808_DCDC_EN_REG, BIT(3), 0),
  361. RK8XX_DESC(RK808_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
  362. RK808_LDO1_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
  363. BIT(0), 400),
  364. RK8XX_DESC(RK808_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
  365. RK808_LDO2_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
  366. BIT(1), 400),
  367. {
  368. .name = "LDO_REG3",
  369. .supply_name = "vcc7",
  370. .of_match = of_match_ptr("LDO_REG3"),
  371. .regulators_node = of_match_ptr("regulators"),
  372. .id = RK808_ID_LDO3,
  373. .ops = &rk808_reg_ops_ranges,
  374. .type = REGULATOR_VOLTAGE,
  375. .n_voltages = 16,
  376. .linear_ranges = rk808_ldo3_voltage_ranges,
  377. .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
  378. .vsel_reg = RK808_LDO3_ON_VSEL_REG,
  379. .vsel_mask = RK808_BUCK4_VSEL_MASK,
  380. .enable_reg = RK808_LDO_EN_REG,
  381. .enable_mask = BIT(2),
  382. .enable_time = 400,
  383. .owner = THIS_MODULE,
  384. },
  385. RK8XX_DESC(RK808_ID_LDO4, "LDO_REG4", "vcc9", 1800, 3400, 100,
  386. RK808_LDO4_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
  387. BIT(3), 400),
  388. RK8XX_DESC(RK808_ID_LDO5, "LDO_REG5", "vcc9", 1800, 3400, 100,
  389. RK808_LDO5_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
  390. BIT(4), 400),
  391. RK8XX_DESC(RK808_ID_LDO6, "LDO_REG6", "vcc10", 800, 2500, 100,
  392. RK808_LDO6_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
  393. BIT(5), 400),
  394. RK8XX_DESC(RK808_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
  395. RK808_LDO7_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
  396. BIT(6), 400),
  397. RK8XX_DESC(RK808_ID_LDO8, "LDO_REG8", "vcc11", 1800, 3400, 100,
  398. RK808_LDO8_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
  399. BIT(7), 400),
  400. RK8XX_DESC_SWITCH(RK808_ID_SWITCH1, "SWITCH_REG1", "vcc8",
  401. RK808_DCDC_EN_REG, BIT(5)),
  402. RK8XX_DESC_SWITCH(RK808_ID_SWITCH2, "SWITCH_REG2", "vcc12",
  403. RK808_DCDC_EN_REG, BIT(6)),
  404. };
  405. static const struct regulator_desc rk818_reg[] = {
  406. {
  407. .name = "DCDC_REG1",
  408. .supply_name = "vcc1",
  409. .of_match = of_match_ptr("DCDC_REG1"),
  410. .regulators_node = of_match_ptr("regulators"),
  411. .id = RK818_ID_DCDC1,
  412. .ops = &rk808_reg_ops,
  413. .type = REGULATOR_VOLTAGE,
  414. .min_uV = 712500,
  415. .uV_step = 12500,
  416. .n_voltages = 64,
  417. .vsel_reg = RK818_BUCK1_ON_VSEL_REG,
  418. .vsel_mask = RK818_BUCK_VSEL_MASK,
  419. .enable_reg = RK818_DCDC_EN_REG,
  420. .enable_mask = BIT(0),
  421. .owner = THIS_MODULE,
  422. }, {
  423. .name = "DCDC_REG2",
  424. .supply_name = "vcc2",
  425. .of_match = of_match_ptr("DCDC_REG2"),
  426. .regulators_node = of_match_ptr("regulators"),
  427. .id = RK818_ID_DCDC2,
  428. .ops = &rk808_reg_ops,
  429. .type = REGULATOR_VOLTAGE,
  430. .min_uV = 712500,
  431. .uV_step = 12500,
  432. .n_voltages = 64,
  433. .vsel_reg = RK818_BUCK2_ON_VSEL_REG,
  434. .vsel_mask = RK818_BUCK_VSEL_MASK,
  435. .enable_reg = RK818_DCDC_EN_REG,
  436. .enable_mask = BIT(1),
  437. .owner = THIS_MODULE,
  438. }, {
  439. .name = "DCDC_REG3",
  440. .supply_name = "vcc3",
  441. .of_match = of_match_ptr("DCDC_REG3"),
  442. .regulators_node = of_match_ptr("regulators"),
  443. .id = RK818_ID_DCDC3,
  444. .ops = &rk808_switch_ops,
  445. .type = REGULATOR_VOLTAGE,
  446. .n_voltages = 1,
  447. .enable_reg = RK818_DCDC_EN_REG,
  448. .enable_mask = BIT(2),
  449. .owner = THIS_MODULE,
  450. },
  451. RK8XX_DESC(RK818_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3600, 100,
  452. RK818_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
  453. RK818_DCDC_EN_REG, BIT(3), 0),
  454. RK8XX_DESC(RK818_ID_BOOST, "DCDC_BOOST", "boost", 4700, 5400, 100,
  455. RK818_BOOST_LDO9_ON_VSEL_REG, RK818_BOOST_ON_VSEL_MASK,
  456. RK818_DCDC_EN_REG, BIT(4), 0),
  457. RK8XX_DESC(RK818_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
  458. RK818_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
  459. BIT(0), 400),
  460. RK8XX_DESC(RK818_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
  461. RK818_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
  462. BIT(1), 400),
  463. {
  464. .name = "LDO_REG3",
  465. .supply_name = "vcc7",
  466. .of_match = of_match_ptr("LDO_REG3"),
  467. .regulators_node = of_match_ptr("regulators"),
  468. .id = RK818_ID_LDO3,
  469. .ops = &rk808_reg_ops_ranges,
  470. .type = REGULATOR_VOLTAGE,
  471. .n_voltages = 16,
  472. .linear_ranges = rk808_ldo3_voltage_ranges,
  473. .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
  474. .vsel_reg = RK818_LDO3_ON_VSEL_REG,
  475. .vsel_mask = RK818_LDO3_ON_VSEL_MASK,
  476. .enable_reg = RK818_LDO_EN_REG,
  477. .enable_mask = BIT(2),
  478. .enable_time = 400,
  479. .owner = THIS_MODULE,
  480. },
  481. RK8XX_DESC(RK818_ID_LDO4, "LDO_REG4", "vcc8", 1800, 3400, 100,
  482. RK818_LDO4_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
  483. BIT(3), 400),
  484. RK8XX_DESC(RK818_ID_LDO5, "LDO_REG5", "vcc7", 1800, 3400, 100,
  485. RK818_LDO5_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
  486. BIT(4), 400),
  487. RK8XX_DESC(RK818_ID_LDO6, "LDO_REG6", "vcc8", 800, 2500, 100,
  488. RK818_LDO6_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
  489. BIT(5), 400),
  490. RK8XX_DESC(RK818_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
  491. RK818_LDO7_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
  492. BIT(6), 400),
  493. RK8XX_DESC(RK818_ID_LDO8, "LDO_REG8", "vcc8", 1800, 3400, 100,
  494. RK818_LDO8_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
  495. BIT(7), 400),
  496. RK8XX_DESC(RK818_ID_LDO9, "LDO_REG9", "vcc9", 1800, 3400, 100,
  497. RK818_BOOST_LDO9_ON_VSEL_REG, RK818_LDO_VSEL_MASK,
  498. RK818_DCDC_EN_REG, BIT(5), 400),
  499. RK8XX_DESC_SWITCH(RK818_ID_SWITCH, "SWITCH_REG", "vcc9",
  500. RK818_DCDC_EN_REG, BIT(6)),
  501. RK8XX_DESC_SWITCH(RK818_ID_HDMI_SWITCH, "HDMI_SWITCH", "h_5v",
  502. RK818_H5V_EN_REG, BIT(0)),
  503. RK8XX_DESC_SWITCH(RK818_ID_OTG_SWITCH, "OTG_SWITCH", "usb",
  504. RK818_DCDC_EN_REG, BIT(7)),
  505. };
  506. static int rk808_regulator_dt_parse_pdata(struct device *dev,
  507. struct device *client_dev,
  508. struct regmap *map,
  509. struct rk808_regulator_data *pdata)
  510. {
  511. struct device_node *np;
  512. int tmp, ret = 0, i;
  513. np = of_get_child_by_name(client_dev->of_node, "regulators");
  514. if (!np)
  515. return -ENXIO;
  516. for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
  517. pdata->dvs_gpio[i] =
  518. devm_gpiod_get_index_optional(client_dev, "dvs", i,
  519. GPIOD_OUT_LOW);
  520. if (IS_ERR(pdata->dvs_gpio[i])) {
  521. ret = PTR_ERR(pdata->dvs_gpio[i]);
  522. dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
  523. goto dt_parse_end;
  524. }
  525. if (!pdata->dvs_gpio[i]) {
  526. dev_warn(dev, "there is no dvs%d gpio\n", i);
  527. continue;
  528. }
  529. tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
  530. ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
  531. gpiod_is_active_low(pdata->dvs_gpio[i]) ?
  532. 0 : tmp);
  533. }
  534. dt_parse_end:
  535. of_node_put(np);
  536. return ret;
  537. }
  538. static int rk808_regulator_probe(struct platform_device *pdev)
  539. {
  540. struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
  541. struct i2c_client *client = rk808->i2c;
  542. struct regulator_config config = {};
  543. struct regulator_dev *rk808_rdev;
  544. struct rk808_regulator_data *pdata;
  545. const struct regulator_desc *regulators;
  546. int ret, i, nregulators;
  547. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  548. if (!pdata)
  549. return -ENOMEM;
  550. ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
  551. rk808->regmap, pdata);
  552. if (ret < 0)
  553. return ret;
  554. platform_set_drvdata(pdev, pdata);
  555. switch (rk808->variant) {
  556. case RK808_ID:
  557. regulators = rk808_reg;
  558. nregulators = RK808_NUM_REGULATORS;
  559. break;
  560. case RK818_ID:
  561. regulators = rk818_reg;
  562. nregulators = RK818_NUM_REGULATORS;
  563. break;
  564. default:
  565. dev_err(&client->dev, "unsupported RK8XX ID %lu\n",
  566. rk808->variant);
  567. return -EINVAL;
  568. }
  569. config.dev = &client->dev;
  570. config.driver_data = pdata;
  571. config.regmap = rk808->regmap;
  572. /* Instantiate the regulators */
  573. for (i = 0; i < nregulators; i++) {
  574. rk808_rdev = devm_regulator_register(&pdev->dev,
  575. &regulators[i], &config);
  576. if (IS_ERR(rk808_rdev)) {
  577. dev_err(&client->dev,
  578. "failed to register %d regulator\n", i);
  579. return PTR_ERR(rk808_rdev);
  580. }
  581. }
  582. return 0;
  583. }
  584. static struct platform_driver rk808_regulator_driver = {
  585. .probe = rk808_regulator_probe,
  586. .driver = {
  587. .name = "rk808-regulator"
  588. },
  589. };
  590. module_platform_driver(rk808_regulator_driver);
  591. MODULE_DESCRIPTION("regulator driver for the RK808/RK818 series PMICs");
  592. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  593. MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
  594. MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
  595. MODULE_LICENSE("GPL");
  596. MODULE_ALIAS("platform:rk808-regulator");