max77686.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * max77686.c - Regulator driver for the Maxim 77686
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * Chiwoong Byun <woong.byun@smasung.com>
  6. * Jonghwa Lee <jonghwa3.lee@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * This driver is based on max8997.c
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/bug.h>
  26. #include <linux/delay.h>
  27. #include <linux/err.h>
  28. #include <linux/gpio.h>
  29. #include <linux/slab.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/regulator/driver.h>
  32. #include <linux/regulator/machine.h>
  33. #include <linux/mfd/max77686.h>
  34. #include <linux/mfd/max77686-private.h>
  35. #define MAX77686_LDO_MINUV 800000
  36. #define MAX77686_LDO_UVSTEP 50000
  37. #define MAX77686_LDO_LOW_MINUV 800000
  38. #define MAX77686_LDO_LOW_UVSTEP 25000
  39. #define MAX77686_BUCK_MINUV 750000
  40. #define MAX77686_BUCK_UVSTEP 50000
  41. #define MAX77686_RAMP_DELAY 100000 /* uV/us */
  42. #define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */
  43. #define MAX77686_DVS_MINUV 600000
  44. #define MAX77686_DVS_UVSTEP 12500
  45. #define MAX77686_OPMODE_SHIFT 6
  46. #define MAX77686_OPMODE_BUCK234_SHIFT 4
  47. #define MAX77686_OPMODE_MASK 0x3
  48. #define MAX77686_VSEL_MASK 0x3F
  49. #define MAX77686_DVS_VSEL_MASK 0xFF
  50. #define MAX77686_RAMP_RATE_MASK 0xC0
  51. #define MAX77686_REGULATORS MAX77686_REG_MAX
  52. #define MAX77686_LDOS 26
  53. enum max77686_ramp_rate {
  54. RAMP_RATE_13P75MV,
  55. RAMP_RATE_27P5MV,
  56. RAMP_RATE_55MV,
  57. RAMP_RATE_NO_CTRL, /* 100mV/us */
  58. };
  59. struct max77686_data {
  60. struct device *dev;
  61. struct max77686_dev *iodev;
  62. struct regulator_dev **rdev;
  63. };
  64. static struct regulator_ops max77686_ops = {
  65. .list_voltage = regulator_list_voltage_linear,
  66. .map_voltage = regulator_map_voltage_linear,
  67. .is_enabled = regulator_is_enabled_regmap,
  68. .enable = regulator_enable_regmap,
  69. .disable = regulator_disable_regmap,
  70. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  71. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  72. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  73. };
  74. static struct regulator_ops max77686_buck_dvs_ops = {
  75. .list_voltage = regulator_list_voltage_linear,
  76. .map_voltage = regulator_map_voltage_linear,
  77. .is_enabled = regulator_is_enabled_regmap,
  78. .enable = regulator_enable_regmap,
  79. .disable = regulator_disable_regmap,
  80. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  81. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  82. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  83. };
  84. #define regulator_desc_ldo(num) { \
  85. .name = "LDO"#num, \
  86. .id = MAX77686_LDO##num, \
  87. .ops = &max77686_ops, \
  88. .type = REGULATOR_VOLTAGE, \
  89. .owner = THIS_MODULE, \
  90. .min_uV = MAX77686_LDO_MINUV, \
  91. .uV_step = MAX77686_LDO_UVSTEP, \
  92. .ramp_delay = MAX77686_RAMP_DELAY, \
  93. .n_voltages = MAX77686_VSEL_MASK + 1, \
  94. .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
  95. .vsel_mask = MAX77686_VSEL_MASK, \
  96. .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
  97. .enable_mask = MAX77686_OPMODE_MASK \
  98. << MAX77686_OPMODE_SHIFT, \
  99. }
  100. #define regulator_desc_ldo_low(num) { \
  101. .name = "LDO"#num, \
  102. .id = MAX77686_LDO##num, \
  103. .ops = &max77686_ops, \
  104. .type = REGULATOR_VOLTAGE, \
  105. .owner = THIS_MODULE, \
  106. .min_uV = MAX77686_LDO_LOW_MINUV, \
  107. .uV_step = MAX77686_LDO_LOW_UVSTEP, \
  108. .ramp_delay = MAX77686_RAMP_DELAY, \
  109. .n_voltages = MAX77686_VSEL_MASK + 1, \
  110. .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
  111. .vsel_mask = MAX77686_VSEL_MASK, \
  112. .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
  113. .enable_mask = MAX77686_OPMODE_MASK \
  114. << MAX77686_OPMODE_SHIFT, \
  115. }
  116. #define regulator_desc_buck(num) { \
  117. .name = "BUCK"#num, \
  118. .id = MAX77686_BUCK##num, \
  119. .ops = &max77686_ops, \
  120. .type = REGULATOR_VOLTAGE, \
  121. .owner = THIS_MODULE, \
  122. .min_uV = MAX77686_BUCK_MINUV, \
  123. .uV_step = MAX77686_BUCK_UVSTEP, \
  124. .ramp_delay = MAX77686_RAMP_DELAY, \
  125. .n_voltages = MAX77686_VSEL_MASK + 1, \
  126. .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
  127. .vsel_mask = MAX77686_VSEL_MASK, \
  128. .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
  129. .enable_mask = MAX77686_OPMODE_MASK, \
  130. }
  131. #define regulator_desc_buck1(num) { \
  132. .name = "BUCK"#num, \
  133. .id = MAX77686_BUCK##num, \
  134. .ops = &max77686_ops, \
  135. .type = REGULATOR_VOLTAGE, \
  136. .owner = THIS_MODULE, \
  137. .min_uV = MAX77686_BUCK_MINUV, \
  138. .uV_step = MAX77686_BUCK_UVSTEP, \
  139. .ramp_delay = MAX77686_RAMP_DELAY, \
  140. .n_voltages = MAX77686_VSEL_MASK + 1, \
  141. .vsel_reg = MAX77686_REG_BUCK1OUT, \
  142. .vsel_mask = MAX77686_VSEL_MASK, \
  143. .enable_reg = MAX77686_REG_BUCK1CTRL, \
  144. .enable_mask = MAX77686_OPMODE_MASK, \
  145. }
  146. #define regulator_desc_buck_dvs(num) { \
  147. .name = "BUCK"#num, \
  148. .id = MAX77686_BUCK##num, \
  149. .ops = &max77686_buck_dvs_ops, \
  150. .type = REGULATOR_VOLTAGE, \
  151. .owner = THIS_MODULE, \
  152. .min_uV = MAX77686_DVS_MINUV, \
  153. .uV_step = MAX77686_DVS_UVSTEP, \
  154. .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
  155. .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
  156. .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
  157. .vsel_mask = MAX77686_DVS_VSEL_MASK, \
  158. .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
  159. .enable_mask = MAX77686_OPMODE_MASK \
  160. << MAX77686_OPMODE_BUCK234_SHIFT, \
  161. }
  162. static struct regulator_desc regulators[] = {
  163. regulator_desc_ldo_low(1),
  164. regulator_desc_ldo_low(2),
  165. regulator_desc_ldo(3),
  166. regulator_desc_ldo(4),
  167. regulator_desc_ldo(5),
  168. regulator_desc_ldo_low(6),
  169. regulator_desc_ldo_low(7),
  170. regulator_desc_ldo_low(8),
  171. regulator_desc_ldo(9),
  172. regulator_desc_ldo(10),
  173. regulator_desc_ldo(11),
  174. regulator_desc_ldo(12),
  175. regulator_desc_ldo(13),
  176. regulator_desc_ldo(14),
  177. regulator_desc_ldo_low(15),
  178. regulator_desc_ldo(16),
  179. regulator_desc_ldo(17),
  180. regulator_desc_ldo(18),
  181. regulator_desc_ldo(19),
  182. regulator_desc_ldo(20),
  183. regulator_desc_ldo(21),
  184. regulator_desc_ldo(22),
  185. regulator_desc_ldo(23),
  186. regulator_desc_ldo(24),
  187. regulator_desc_ldo(25),
  188. regulator_desc_ldo(26),
  189. regulator_desc_buck1(1),
  190. regulator_desc_buck_dvs(2),
  191. regulator_desc_buck_dvs(3),
  192. regulator_desc_buck_dvs(4),
  193. regulator_desc_buck(5),
  194. regulator_desc_buck(6),
  195. regulator_desc_buck(7),
  196. regulator_desc_buck(8),
  197. regulator_desc_buck(9),
  198. };
  199. static __devinit int max77686_pmic_probe(struct platform_device *pdev)
  200. {
  201. struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  202. struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
  203. struct regulator_dev **rdev;
  204. struct max77686_data *max77686;
  205. int i, size;
  206. int ret = 0;
  207. struct regulator_config config = { };
  208. dev_dbg(&pdev->dev, "%s\n", __func__);
  209. if (!pdata || pdata->num_regulators != MAX77686_REGULATORS) {
  210. dev_err(&pdev->dev,
  211. "Invalid initial data for regulator's initialiation\n");
  212. return -EINVAL;
  213. }
  214. max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
  215. GFP_KERNEL);
  216. if (!max77686)
  217. return -ENOMEM;
  218. size = sizeof(struct regulator_dev *) * MAX77686_REGULATORS;
  219. max77686->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  220. if (!max77686->rdev)
  221. return -ENOMEM;
  222. rdev = max77686->rdev;
  223. max77686->dev = &pdev->dev;
  224. max77686->iodev = iodev;
  225. platform_set_drvdata(pdev, max77686);
  226. for (i = 0; i < MAX77686_REGULATORS; i++) {
  227. config.dev = max77686->dev;
  228. config.regmap = iodev->regmap;
  229. config.driver_data = max77686;
  230. config.init_data = pdata->regulators[i].initdata;
  231. rdev[i] = regulator_register(&regulators[i], &config);
  232. if (IS_ERR(rdev[i])) {
  233. ret = PTR_ERR(rdev[i]);
  234. dev_err(max77686->dev,
  235. "regulator init failed for %d\n", i);
  236. rdev[i] = NULL;
  237. goto err;
  238. }
  239. }
  240. return 0;
  241. err:
  242. while (--i >= 0)
  243. regulator_unregister(rdev[i]);
  244. return ret;
  245. }
  246. static int __devexit max77686_pmic_remove(struct platform_device *pdev)
  247. {
  248. struct max77686_data *max77686 = platform_get_drvdata(pdev);
  249. struct regulator_dev **rdev = max77686->rdev;
  250. int i;
  251. for (i = 0; i < MAX77686_REGULATORS; i++)
  252. if (rdev[i])
  253. regulator_unregister(rdev[i]);
  254. return 0;
  255. }
  256. static const struct platform_device_id max77686_pmic_id[] = {
  257. {"max77686-pmic", 0},
  258. { },
  259. };
  260. MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
  261. static struct platform_driver max77686_pmic_driver = {
  262. .driver = {
  263. .name = "max77686-pmic",
  264. .owner = THIS_MODULE,
  265. },
  266. .probe = max77686_pmic_probe,
  267. .remove = __devexit_p(max77686_pmic_remove),
  268. .id_table = max77686_pmic_id,
  269. };
  270. static int __init max77686_pmic_init(void)
  271. {
  272. return platform_driver_register(&max77686_pmic_driver);
  273. }
  274. subsys_initcall(max77686_pmic_init);
  275. static void __exit max77686_pmic_cleanup(void)
  276. {
  277. platform_driver_unregister(&max77686_pmic_driver);
  278. }
  279. module_exit(max77686_pmic_cleanup);
  280. MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
  281. MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
  282. MODULE_LICENSE("GPL");