s2mpa01.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright (c) 2013 Samsung Electronics Co., Ltd
  3. * http://www.samsung.com
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. */
  11. #include <linux/bug.h>
  12. #include <linux/err.h>
  13. #include <linux/gpio.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/regmap.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/regulator/of_regulator.h>
  22. #include <linux/mfd/samsung/core.h>
  23. #include <linux/mfd/samsung/s2mpa01.h>
  24. #define S2MPA01_REGULATOR_CNT ARRAY_SIZE(regulators)
  25. struct s2mpa01_info {
  26. int ramp_delay24;
  27. int ramp_delay3;
  28. int ramp_delay5;
  29. int ramp_delay16;
  30. int ramp_delay7;
  31. int ramp_delay8910;
  32. };
  33. static int get_ramp_delay(int ramp_delay)
  34. {
  35. unsigned char cnt = 0;
  36. ramp_delay /= 6250;
  37. while (true) {
  38. ramp_delay = ramp_delay >> 1;
  39. if (ramp_delay == 0)
  40. break;
  41. cnt++;
  42. }
  43. if (cnt > 3)
  44. cnt = 3;
  45. return cnt;
  46. }
  47. static int s2mpa01_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  48. unsigned int old_selector,
  49. unsigned int new_selector)
  50. {
  51. struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
  52. unsigned int ramp_delay = 0;
  53. int old_volt, new_volt;
  54. switch (rdev_get_id(rdev)) {
  55. case S2MPA01_BUCK2:
  56. case S2MPA01_BUCK4:
  57. ramp_delay = s2mpa01->ramp_delay24;
  58. break;
  59. case S2MPA01_BUCK3:
  60. ramp_delay = s2mpa01->ramp_delay3;
  61. break;
  62. case S2MPA01_BUCK5:
  63. ramp_delay = s2mpa01->ramp_delay5;
  64. break;
  65. case S2MPA01_BUCK1:
  66. case S2MPA01_BUCK6:
  67. ramp_delay = s2mpa01->ramp_delay16;
  68. break;
  69. case S2MPA01_BUCK7:
  70. ramp_delay = s2mpa01->ramp_delay7;
  71. break;
  72. case S2MPA01_BUCK8:
  73. case S2MPA01_BUCK9:
  74. case S2MPA01_BUCK10:
  75. ramp_delay = s2mpa01->ramp_delay8910;
  76. break;
  77. }
  78. if (ramp_delay == 0)
  79. ramp_delay = rdev->desc->ramp_delay;
  80. old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
  81. new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
  82. return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
  83. }
  84. static int s2mpa01_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  85. {
  86. struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
  87. unsigned int ramp_val, ramp_shift, ramp_reg = S2MPA01_REG_RAMP2;
  88. unsigned int ramp_enable = 1, enable_shift = 0;
  89. int ret;
  90. switch (rdev_get_id(rdev)) {
  91. case S2MPA01_BUCK1:
  92. enable_shift = S2MPA01_BUCK1_RAMP_EN_SHIFT;
  93. if (!ramp_delay) {
  94. ramp_enable = 0;
  95. break;
  96. }
  97. if (ramp_delay > s2mpa01->ramp_delay16)
  98. s2mpa01->ramp_delay16 = ramp_delay;
  99. else
  100. ramp_delay = s2mpa01->ramp_delay16;
  101. ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
  102. break;
  103. case S2MPA01_BUCK2:
  104. enable_shift = S2MPA01_BUCK2_RAMP_EN_SHIFT;
  105. if (!ramp_delay) {
  106. ramp_enable = 0;
  107. break;
  108. }
  109. if (ramp_delay > s2mpa01->ramp_delay24)
  110. s2mpa01->ramp_delay24 = ramp_delay;
  111. else
  112. ramp_delay = s2mpa01->ramp_delay24;
  113. ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
  114. ramp_reg = S2MPA01_REG_RAMP1;
  115. break;
  116. case S2MPA01_BUCK3:
  117. enable_shift = S2MPA01_BUCK3_RAMP_EN_SHIFT;
  118. if (!ramp_delay) {
  119. ramp_enable = 0;
  120. break;
  121. }
  122. s2mpa01->ramp_delay3 = ramp_delay;
  123. ramp_shift = S2MPA01_BUCK3_RAMP_SHIFT;
  124. ramp_reg = S2MPA01_REG_RAMP1;
  125. break;
  126. case S2MPA01_BUCK4:
  127. enable_shift = S2MPA01_BUCK4_RAMP_EN_SHIFT;
  128. if (!ramp_delay) {
  129. ramp_enable = 0;
  130. break;
  131. }
  132. if (ramp_delay > s2mpa01->ramp_delay24)
  133. s2mpa01->ramp_delay24 = ramp_delay;
  134. else
  135. ramp_delay = s2mpa01->ramp_delay24;
  136. ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
  137. ramp_reg = S2MPA01_REG_RAMP1;
  138. break;
  139. case S2MPA01_BUCK5:
  140. s2mpa01->ramp_delay5 = ramp_delay;
  141. ramp_shift = S2MPA01_BUCK5_RAMP_SHIFT;
  142. break;
  143. case S2MPA01_BUCK6:
  144. if (ramp_delay > s2mpa01->ramp_delay16)
  145. s2mpa01->ramp_delay16 = ramp_delay;
  146. else
  147. ramp_delay = s2mpa01->ramp_delay16;
  148. ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
  149. break;
  150. case S2MPA01_BUCK7:
  151. s2mpa01->ramp_delay7 = ramp_delay;
  152. ramp_shift = S2MPA01_BUCK7_RAMP_SHIFT;
  153. break;
  154. case S2MPA01_BUCK8:
  155. case S2MPA01_BUCK9:
  156. case S2MPA01_BUCK10:
  157. if (ramp_delay > s2mpa01->ramp_delay8910)
  158. s2mpa01->ramp_delay8910 = ramp_delay;
  159. else
  160. ramp_delay = s2mpa01->ramp_delay8910;
  161. ramp_shift = S2MPA01_BUCK8910_RAMP_SHIFT;
  162. break;
  163. default:
  164. return 0;
  165. }
  166. if (!ramp_enable)
  167. goto ramp_disable;
  168. /* Ramp delay can be enabled/disabled only for buck[1234] */
  169. if (rdev_get_id(rdev) >= S2MPA01_BUCK1 &&
  170. rdev_get_id(rdev) <= S2MPA01_BUCK4) {
  171. ret = regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
  172. 1 << enable_shift, 1 << enable_shift);
  173. if (ret) {
  174. dev_err(&rdev->dev, "failed to enable ramp rate\n");
  175. return ret;
  176. }
  177. }
  178. ramp_val = get_ramp_delay(ramp_delay);
  179. return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
  180. ramp_val << ramp_shift);
  181. ramp_disable:
  182. return regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
  183. 1 << enable_shift, 0);
  184. }
  185. static struct regulator_ops s2mpa01_ldo_ops = {
  186. .list_voltage = regulator_list_voltage_linear,
  187. .map_voltage = regulator_map_voltage_linear,
  188. .is_enabled = regulator_is_enabled_regmap,
  189. .enable = regulator_enable_regmap,
  190. .disable = regulator_disable_regmap,
  191. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  192. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  193. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  194. };
  195. static struct regulator_ops s2mpa01_buck_ops = {
  196. .list_voltage = regulator_list_voltage_linear,
  197. .map_voltage = regulator_map_voltage_linear,
  198. .is_enabled = regulator_is_enabled_regmap,
  199. .enable = regulator_enable_regmap,
  200. .disable = regulator_disable_regmap,
  201. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  202. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  203. .set_voltage_time_sel = s2mpa01_regulator_set_voltage_time_sel,
  204. .set_ramp_delay = s2mpa01_set_ramp_delay,
  205. };
  206. #define regulator_desc_ldo(num, step) { \
  207. .name = "LDO"#num, \
  208. .id = S2MPA01_LDO##num, \
  209. .ops = &s2mpa01_ldo_ops, \
  210. .type = REGULATOR_VOLTAGE, \
  211. .owner = THIS_MODULE, \
  212. .min_uV = MIN_800_MV, \
  213. .uV_step = step, \
  214. .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
  215. .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
  216. .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
  217. .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
  218. .enable_mask = S2MPA01_ENABLE_MASK \
  219. }
  220. #define regulator_desc_buck1_4(num) { \
  221. .name = "BUCK"#num, \
  222. .id = S2MPA01_BUCK##num, \
  223. .ops = &s2mpa01_buck_ops, \
  224. .type = REGULATOR_VOLTAGE, \
  225. .owner = THIS_MODULE, \
  226. .min_uV = MIN_600_MV, \
  227. .uV_step = STEP_6_25_MV, \
  228. .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
  229. .ramp_delay = S2MPA01_RAMP_DELAY, \
  230. .vsel_reg = S2MPA01_REG_B1CTRL2 + (num - 1) * 2, \
  231. .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
  232. .enable_reg = S2MPA01_REG_B1CTRL1 + (num - 1) * 2, \
  233. .enable_mask = S2MPA01_ENABLE_MASK \
  234. }
  235. #define regulator_desc_buck5 { \
  236. .name = "BUCK5", \
  237. .id = S2MPA01_BUCK5, \
  238. .ops = &s2mpa01_buck_ops, \
  239. .type = REGULATOR_VOLTAGE, \
  240. .owner = THIS_MODULE, \
  241. .min_uV = MIN_800_MV, \
  242. .uV_step = STEP_6_25_MV, \
  243. .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
  244. .ramp_delay = S2MPA01_RAMP_DELAY, \
  245. .vsel_reg = S2MPA01_REG_B5CTRL2, \
  246. .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
  247. .enable_reg = S2MPA01_REG_B5CTRL1, \
  248. .enable_mask = S2MPA01_ENABLE_MASK \
  249. }
  250. #define regulator_desc_buck6_10(num, min, step) { \
  251. .name = "BUCK"#num, \
  252. .id = S2MPA01_BUCK##num, \
  253. .ops = &s2mpa01_buck_ops, \
  254. .type = REGULATOR_VOLTAGE, \
  255. .owner = THIS_MODULE, \
  256. .min_uV = min, \
  257. .uV_step = step, \
  258. .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
  259. .ramp_delay = S2MPA01_RAMP_DELAY, \
  260. .vsel_reg = S2MPA01_REG_B6CTRL2 + (num - 6) * 2, \
  261. .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
  262. .enable_reg = S2MPA01_REG_B6CTRL1 + (num - 6) * 2, \
  263. .enable_mask = S2MPA01_ENABLE_MASK \
  264. }
  265. static const struct regulator_desc regulators[] = {
  266. regulator_desc_ldo(1, STEP_25_MV),
  267. regulator_desc_ldo(2, STEP_50_MV),
  268. regulator_desc_ldo(3, STEP_50_MV),
  269. regulator_desc_ldo(4, STEP_50_MV),
  270. regulator_desc_ldo(5, STEP_50_MV),
  271. regulator_desc_ldo(6, STEP_25_MV),
  272. regulator_desc_ldo(7, STEP_50_MV),
  273. regulator_desc_ldo(8, STEP_50_MV),
  274. regulator_desc_ldo(9, STEP_50_MV),
  275. regulator_desc_ldo(10, STEP_50_MV),
  276. regulator_desc_ldo(11, STEP_25_MV),
  277. regulator_desc_ldo(12, STEP_50_MV),
  278. regulator_desc_ldo(13, STEP_50_MV),
  279. regulator_desc_ldo(14, STEP_50_MV),
  280. regulator_desc_ldo(15, STEP_50_MV),
  281. regulator_desc_ldo(16, STEP_50_MV),
  282. regulator_desc_ldo(17, STEP_50_MV),
  283. regulator_desc_ldo(18, STEP_50_MV),
  284. regulator_desc_ldo(19, STEP_50_MV),
  285. regulator_desc_ldo(20, STEP_50_MV),
  286. regulator_desc_ldo(21, STEP_50_MV),
  287. regulator_desc_ldo(22, STEP_25_MV),
  288. regulator_desc_ldo(23, STEP_25_MV),
  289. regulator_desc_ldo(24, STEP_50_MV),
  290. regulator_desc_ldo(25, STEP_50_MV),
  291. regulator_desc_ldo(26, STEP_50_MV),
  292. regulator_desc_buck1_4(1),
  293. regulator_desc_buck1_4(2),
  294. regulator_desc_buck1_4(3),
  295. regulator_desc_buck1_4(4),
  296. regulator_desc_buck5,
  297. regulator_desc_buck6_10(6, MIN_600_MV, STEP_6_25_MV),
  298. regulator_desc_buck6_10(7, MIN_600_MV, STEP_6_25_MV),
  299. regulator_desc_buck6_10(8, MIN_800_MV, STEP_12_5_MV),
  300. regulator_desc_buck6_10(9, MIN_1500_MV, STEP_12_5_MV),
  301. regulator_desc_buck6_10(10, MIN_1000_MV, STEP_12_5_MV),
  302. };
  303. static int s2mpa01_pmic_probe(struct platform_device *pdev)
  304. {
  305. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  306. struct sec_platform_data *pdata = dev_get_platdata(iodev->dev);
  307. struct of_regulator_match rdata[S2MPA01_REGULATOR_MAX] = { };
  308. struct device_node *reg_np = NULL;
  309. struct regulator_config config = { };
  310. struct s2mpa01_info *s2mpa01;
  311. int i;
  312. s2mpa01 = devm_kzalloc(&pdev->dev, sizeof(*s2mpa01), GFP_KERNEL);
  313. if (!s2mpa01)
  314. return -ENOMEM;
  315. for (i = 0; i < S2MPA01_REGULATOR_CNT; i++)
  316. rdata[i].name = regulators[i].name;
  317. if (iodev->dev->of_node) {
  318. reg_np = of_get_child_by_name(iodev->dev->of_node,
  319. "regulators");
  320. if (!reg_np) {
  321. dev_err(&pdev->dev,
  322. "could not find regulators sub-node\n");
  323. return -EINVAL;
  324. }
  325. of_regulator_match(&pdev->dev, reg_np, rdata,
  326. S2MPA01_REGULATOR_MAX);
  327. of_node_put(reg_np);
  328. }
  329. platform_set_drvdata(pdev, s2mpa01);
  330. config.dev = &pdev->dev;
  331. config.regmap = iodev->regmap_pmic;
  332. config.driver_data = s2mpa01;
  333. for (i = 0; i < S2MPA01_REGULATOR_MAX; i++) {
  334. struct regulator_dev *rdev;
  335. if (pdata)
  336. config.init_data = pdata->regulators[i].initdata;
  337. else
  338. config.init_data = rdata[i].init_data;
  339. if (reg_np)
  340. config.of_node = rdata[i].of_node;
  341. rdev = devm_regulator_register(&pdev->dev,
  342. &regulators[i], &config);
  343. if (IS_ERR(rdev)) {
  344. dev_err(&pdev->dev, "regulator init failed for %d\n",
  345. i);
  346. return PTR_ERR(rdev);
  347. }
  348. }
  349. return 0;
  350. }
  351. static const struct platform_device_id s2mpa01_pmic_id[] = {
  352. { "s2mpa01-pmic", 0},
  353. { },
  354. };
  355. MODULE_DEVICE_TABLE(platform, s2mpa01_pmic_id);
  356. static struct platform_driver s2mpa01_pmic_driver = {
  357. .driver = {
  358. .name = "s2mpa01-pmic",
  359. },
  360. .probe = s2mpa01_pmic_probe,
  361. .id_table = s2mpa01_pmic_id,
  362. };
  363. module_platform_driver(s2mpa01_pmic_driver);
  364. /* Module information */
  365. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  366. MODULE_AUTHOR("Sachin Kamat <sachin.kamat@samsung.com>");
  367. MODULE_DESCRIPTION("SAMSUNG S2MPA01 Regulator Driver");
  368. MODULE_LICENSE("GPL");