max8925-regulator.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Regulators driver for Maxim max8925
  3. *
  4. * Copyright (C) 2009 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regulator/driver.h>
  18. #include <linux/regulator/machine.h>
  19. #include <linux/mfd/max8925.h>
  20. #include <linux/of.h>
  21. #include <linux/regulator/of_regulator.h>
  22. #define SD1_DVM_VMIN 850000
  23. #define SD1_DVM_VMAX 1000000
  24. #define SD1_DVM_STEP 50000
  25. #define SD1_DVM_SHIFT 5 /* SDCTL1 bit5 */
  26. #define SD1_DVM_EN 6 /* SDV1 bit 6 */
  27. /* bit definitions in LDO control registers */
  28. #define LDO_SEQ_I2C 0x7 /* Power U/D by i2c */
  29. #define LDO_SEQ_MASK 0x7 /* Power U/D sequence mask */
  30. #define LDO_SEQ_SHIFT 2 /* Power U/D sequence offset */
  31. #define LDO_I2C_EN 0x1 /* Enable by i2c */
  32. #define LDO_I2C_EN_MASK 0x1 /* Enable mask by i2c */
  33. #define LDO_I2C_EN_SHIFT 0 /* Enable offset by i2c */
  34. struct max8925_regulator_info {
  35. struct regulator_desc desc;
  36. struct i2c_client *i2c;
  37. int vol_reg;
  38. int enable_reg;
  39. };
  40. static int max8925_set_voltage_sel(struct regulator_dev *rdev,
  41. unsigned int selector)
  42. {
  43. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  44. unsigned char mask = rdev->desc->n_voltages - 1;
  45. return max8925_set_bits(info->i2c, info->vol_reg, mask, selector);
  46. }
  47. static int max8925_get_voltage_sel(struct regulator_dev *rdev)
  48. {
  49. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  50. unsigned char data, mask;
  51. int ret;
  52. ret = max8925_reg_read(info->i2c, info->vol_reg);
  53. if (ret < 0)
  54. return ret;
  55. mask = rdev->desc->n_voltages - 1;
  56. data = ret & mask;
  57. return data;
  58. }
  59. static int max8925_enable(struct regulator_dev *rdev)
  60. {
  61. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  62. return max8925_set_bits(info->i2c, info->enable_reg,
  63. LDO_SEQ_MASK << LDO_SEQ_SHIFT |
  64. LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
  65. LDO_SEQ_I2C << LDO_SEQ_SHIFT |
  66. LDO_I2C_EN << LDO_I2C_EN_SHIFT);
  67. }
  68. static int max8925_disable(struct regulator_dev *rdev)
  69. {
  70. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  71. return max8925_set_bits(info->i2c, info->enable_reg,
  72. LDO_SEQ_MASK << LDO_SEQ_SHIFT |
  73. LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
  74. LDO_SEQ_I2C << LDO_SEQ_SHIFT);
  75. }
  76. static int max8925_is_enabled(struct regulator_dev *rdev)
  77. {
  78. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  79. int ldo_seq, ret;
  80. ret = max8925_reg_read(info->i2c, info->enable_reg);
  81. if (ret < 0)
  82. return ret;
  83. ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK;
  84. if (ldo_seq != LDO_SEQ_I2C)
  85. return 1;
  86. else
  87. return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT);
  88. }
  89. static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
  90. {
  91. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  92. unsigned char data, mask;
  93. if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
  94. return -EINVAL;
  95. data = DIV_ROUND_UP(uV - SD1_DVM_VMIN, SD1_DVM_STEP);
  96. data <<= SD1_DVM_SHIFT;
  97. mask = 3 << SD1_DVM_SHIFT;
  98. return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
  99. }
  100. static int max8925_set_dvm_enable(struct regulator_dev *rdev)
  101. {
  102. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  103. return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
  104. 1 << SD1_DVM_EN);
  105. }
  106. static int max8925_set_dvm_disable(struct regulator_dev *rdev)
  107. {
  108. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  109. return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
  110. }
  111. static const struct regulator_ops max8925_regulator_sdv_ops = {
  112. .map_voltage = regulator_map_voltage_linear,
  113. .list_voltage = regulator_list_voltage_linear,
  114. .set_voltage_sel = max8925_set_voltage_sel,
  115. .get_voltage_sel = max8925_get_voltage_sel,
  116. .enable = max8925_enable,
  117. .disable = max8925_disable,
  118. .is_enabled = max8925_is_enabled,
  119. .set_suspend_voltage = max8925_set_dvm_voltage,
  120. .set_suspend_enable = max8925_set_dvm_enable,
  121. .set_suspend_disable = max8925_set_dvm_disable,
  122. };
  123. static const struct regulator_ops max8925_regulator_ldo_ops = {
  124. .map_voltage = regulator_map_voltage_linear,
  125. .list_voltage = regulator_list_voltage_linear,
  126. .set_voltage_sel = max8925_set_voltage_sel,
  127. .get_voltage_sel = max8925_get_voltage_sel,
  128. .enable = max8925_enable,
  129. .disable = max8925_disable,
  130. .is_enabled = max8925_is_enabled,
  131. };
  132. #define MAX8925_SDV(_id, min, max, step) \
  133. { \
  134. .desc = { \
  135. .name = "SDV" #_id, \
  136. .ops = &max8925_regulator_sdv_ops, \
  137. .type = REGULATOR_VOLTAGE, \
  138. .id = MAX8925_ID_SD##_id, \
  139. .owner = THIS_MODULE, \
  140. .n_voltages = 64, \
  141. .min_uV = min * 1000, \
  142. .uV_step = step * 1000, \
  143. }, \
  144. .vol_reg = MAX8925_SDV##_id, \
  145. .enable_reg = MAX8925_SDCTL##_id, \
  146. }
  147. #define MAX8925_LDO(_id, min, max, step) \
  148. { \
  149. .desc = { \
  150. .name = "LDO" #_id, \
  151. .ops = &max8925_regulator_ldo_ops, \
  152. .type = REGULATOR_VOLTAGE, \
  153. .id = MAX8925_ID_LDO##_id, \
  154. .owner = THIS_MODULE, \
  155. .n_voltages = 64, \
  156. .min_uV = min * 1000, \
  157. .uV_step = step * 1000, \
  158. }, \
  159. .vol_reg = MAX8925_LDOVOUT##_id, \
  160. .enable_reg = MAX8925_LDOCTL##_id, \
  161. }
  162. #ifdef CONFIG_OF
  163. static struct of_regulator_match max8925_regulator_matches[] = {
  164. { .name = "SDV1",},
  165. { .name = "SDV2",},
  166. { .name = "SDV3",},
  167. { .name = "LDO1",},
  168. { .name = "LDO2",},
  169. { .name = "LDO3",},
  170. { .name = "LDO4",},
  171. { .name = "LDO5",},
  172. { .name = "LDO6",},
  173. { .name = "LDO7",},
  174. { .name = "LDO8",},
  175. { .name = "LDO9",},
  176. { .name = "LDO10",},
  177. { .name = "LDO11",},
  178. { .name = "LDO12",},
  179. { .name = "LDO13",},
  180. { .name = "LDO14",},
  181. { .name = "LDO15",},
  182. { .name = "LDO16",},
  183. { .name = "LDO17",},
  184. { .name = "LDO18",},
  185. { .name = "LDO19",},
  186. { .name = "LDO20",},
  187. };
  188. #endif
  189. static struct max8925_regulator_info max8925_regulator_info[] = {
  190. MAX8925_SDV(1, 637.5, 1425, 12.5),
  191. MAX8925_SDV(2, 650, 2225, 25),
  192. MAX8925_SDV(3, 750, 3900, 50),
  193. MAX8925_LDO(1, 750, 3900, 50),
  194. MAX8925_LDO(2, 650, 2250, 25),
  195. MAX8925_LDO(3, 650, 2250, 25),
  196. MAX8925_LDO(4, 750, 3900, 50),
  197. MAX8925_LDO(5, 750, 3900, 50),
  198. MAX8925_LDO(6, 750, 3900, 50),
  199. MAX8925_LDO(7, 750, 3900, 50),
  200. MAX8925_LDO(8, 750, 3900, 50),
  201. MAX8925_LDO(9, 750, 3900, 50),
  202. MAX8925_LDO(10, 750, 3900, 50),
  203. MAX8925_LDO(11, 750, 3900, 50),
  204. MAX8925_LDO(12, 750, 3900, 50),
  205. MAX8925_LDO(13, 750, 3900, 50),
  206. MAX8925_LDO(14, 750, 3900, 50),
  207. MAX8925_LDO(15, 750, 3900, 50),
  208. MAX8925_LDO(16, 750, 3900, 50),
  209. MAX8925_LDO(17, 650, 2250, 25),
  210. MAX8925_LDO(18, 650, 2250, 25),
  211. MAX8925_LDO(19, 750, 3900, 50),
  212. MAX8925_LDO(20, 750, 3900, 50),
  213. };
  214. #ifdef CONFIG_OF
  215. static int max8925_regulator_dt_init(struct platform_device *pdev,
  216. struct regulator_config *config,
  217. int ridx)
  218. {
  219. struct device_node *nproot, *np;
  220. int rcount;
  221. nproot = pdev->dev.parent->of_node;
  222. if (!nproot)
  223. return -ENODEV;
  224. np = of_get_child_by_name(nproot, "regulators");
  225. if (!np) {
  226. dev_err(&pdev->dev, "failed to find regulators node\n");
  227. return -ENODEV;
  228. }
  229. rcount = of_regulator_match(&pdev->dev, np,
  230. &max8925_regulator_matches[ridx], 1);
  231. of_node_put(np);
  232. if (rcount < 0)
  233. return rcount;
  234. config->init_data = max8925_regulator_matches[ridx].init_data;
  235. config->of_node = max8925_regulator_matches[ridx].of_node;
  236. return 0;
  237. }
  238. #else
  239. #define max8925_regulator_dt_init(x, y, z) (-1)
  240. #endif
  241. static int max8925_regulator_probe(struct platform_device *pdev)
  242. {
  243. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  244. struct regulator_init_data *pdata = dev_get_platdata(&pdev->dev);
  245. struct regulator_config config = { };
  246. struct max8925_regulator_info *ri;
  247. struct resource *res;
  248. struct regulator_dev *rdev;
  249. int i, regulator_idx;
  250. res = platform_get_resource(pdev, IORESOURCE_REG, 0);
  251. if (!res) {
  252. dev_err(&pdev->dev, "No REG resource!\n");
  253. return -EINVAL;
  254. }
  255. for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
  256. ri = &max8925_regulator_info[i];
  257. if (ri->vol_reg == res->start) {
  258. regulator_idx = i;
  259. break;
  260. }
  261. }
  262. if (i == ARRAY_SIZE(max8925_regulator_info)) {
  263. dev_err(&pdev->dev, "Failed to find regulator %llu\n",
  264. (unsigned long long)res->start);
  265. return -EINVAL;
  266. }
  267. ri->i2c = chip->i2c;
  268. config.dev = &pdev->dev;
  269. config.driver_data = ri;
  270. if (max8925_regulator_dt_init(pdev, &config, regulator_idx))
  271. if (pdata)
  272. config.init_data = pdata;
  273. rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
  274. if (IS_ERR(rdev)) {
  275. dev_err(&pdev->dev, "failed to register regulator %s\n",
  276. ri->desc.name);
  277. return PTR_ERR(rdev);
  278. }
  279. platform_set_drvdata(pdev, rdev);
  280. return 0;
  281. }
  282. static struct platform_driver max8925_regulator_driver = {
  283. .driver = {
  284. .name = "max8925-regulator",
  285. },
  286. .probe = max8925_regulator_probe,
  287. };
  288. static int __init max8925_regulator_init(void)
  289. {
  290. return platform_driver_register(&max8925_regulator_driver);
  291. }
  292. subsys_initcall(max8925_regulator_init);
  293. static void __exit max8925_regulator_exit(void)
  294. {
  295. platform_driver_unregister(&max8925_regulator_driver);
  296. }
  297. module_exit(max8925_regulator_exit);
  298. MODULE_LICENSE("GPL");
  299. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  300. MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
  301. MODULE_ALIAS("platform:max8925-regulator");