max8925-regulator.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #define SD1_DVM_VMIN 850000
  21. #define SD1_DVM_VMAX 1000000
  22. #define SD1_DVM_STEP 50000
  23. #define SD1_DVM_SHIFT 5 /* SDCTL1 bit5 */
  24. #define SD1_DVM_EN 6 /* SDV1 bit 6 */
  25. /* bit definitions in LDO control registers */
  26. #define LDO_SEQ_I2C 0x7 /* Power U/D by i2c */
  27. #define LDO_SEQ_MASK 0x7 /* Power U/D sequence mask */
  28. #define LDO_SEQ_SHIFT 2 /* Power U/D sequence offset */
  29. #define LDO_I2C_EN 0x1 /* Enable by i2c */
  30. #define LDO_I2C_EN_MASK 0x1 /* Enable mask by i2c */
  31. #define LDO_I2C_EN_SHIFT 0 /* Enable offset by i2c */
  32. struct max8925_regulator_info {
  33. struct regulator_desc desc;
  34. struct regulator_dev *regulator;
  35. struct i2c_client *i2c;
  36. struct max8925_chip *chip;
  37. int min_uV;
  38. int max_uV;
  39. int step_uV;
  40. int vol_reg;
  41. int vol_shift;
  42. int vol_nbits;
  43. int enable_reg;
  44. };
  45. static inline int check_range(struct max8925_regulator_info *info,
  46. int min_uV, int max_uV)
  47. {
  48. if (min_uV < info->min_uV || min_uV > info->max_uV)
  49. return -EINVAL;
  50. return 0;
  51. }
  52. static int max8925_list_voltage(struct regulator_dev *rdev, unsigned index)
  53. {
  54. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  55. return info->min_uV + index * info->step_uV;
  56. }
  57. static int max8925_set_voltage(struct regulator_dev *rdev,
  58. int min_uV, int max_uV, unsigned int *selector)
  59. {
  60. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  61. unsigned char data, mask;
  62. if (check_range(info, min_uV, max_uV)) {
  63. dev_err(info->chip->dev, "invalid voltage range (%d, %d) uV\n",
  64. min_uV, max_uV);
  65. return -EINVAL;
  66. }
  67. data = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
  68. *selector = data;
  69. data <<= info->vol_shift;
  70. mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
  71. return max8925_set_bits(info->i2c, info->vol_reg, mask, data);
  72. }
  73. static int max8925_get_voltage(struct regulator_dev *rdev)
  74. {
  75. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  76. unsigned char data, mask;
  77. int ret;
  78. ret = max8925_reg_read(info->i2c, info->vol_reg);
  79. if (ret < 0)
  80. return ret;
  81. mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
  82. data = (ret & mask) >> info->vol_shift;
  83. return max8925_list_voltage(rdev, data);
  84. }
  85. static int max8925_enable(struct regulator_dev *rdev)
  86. {
  87. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  88. return max8925_set_bits(info->i2c, info->enable_reg,
  89. LDO_SEQ_MASK << LDO_SEQ_SHIFT |
  90. LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
  91. LDO_SEQ_I2C << LDO_SEQ_SHIFT |
  92. LDO_I2C_EN << LDO_I2C_EN_SHIFT);
  93. }
  94. static int max8925_disable(struct regulator_dev *rdev)
  95. {
  96. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  97. return max8925_set_bits(info->i2c, info->enable_reg,
  98. LDO_SEQ_MASK << LDO_SEQ_SHIFT |
  99. LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
  100. LDO_SEQ_I2C << LDO_SEQ_SHIFT);
  101. }
  102. static int max8925_is_enabled(struct regulator_dev *rdev)
  103. {
  104. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  105. int ldo_seq, ret;
  106. ret = max8925_reg_read(info->i2c, info->enable_reg);
  107. if (ret < 0)
  108. return ret;
  109. ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK;
  110. if (ldo_seq != LDO_SEQ_I2C)
  111. return 1;
  112. else
  113. return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT);
  114. }
  115. static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
  116. {
  117. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  118. unsigned char data, mask;
  119. if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
  120. return -EINVAL;
  121. data = DIV_ROUND_UP(uV - SD1_DVM_VMIN, SD1_DVM_STEP);
  122. data <<= SD1_DVM_SHIFT;
  123. mask = 3 << SD1_DVM_SHIFT;
  124. return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
  125. }
  126. static int max8925_set_dvm_enable(struct regulator_dev *rdev)
  127. {
  128. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  129. return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
  130. 1 << SD1_DVM_EN);
  131. }
  132. static int max8925_set_dvm_disable(struct regulator_dev *rdev)
  133. {
  134. struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
  135. return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
  136. }
  137. static struct regulator_ops max8925_regulator_sdv_ops = {
  138. .set_voltage = max8925_set_voltage,
  139. .get_voltage = max8925_get_voltage,
  140. .enable = max8925_enable,
  141. .disable = max8925_disable,
  142. .is_enabled = max8925_is_enabled,
  143. .set_suspend_voltage = max8925_set_dvm_voltage,
  144. .set_suspend_enable = max8925_set_dvm_enable,
  145. .set_suspend_disable = max8925_set_dvm_disable,
  146. };
  147. static struct regulator_ops max8925_regulator_ldo_ops = {
  148. .set_voltage = max8925_set_voltage,
  149. .get_voltage = max8925_get_voltage,
  150. .enable = max8925_enable,
  151. .disable = max8925_disable,
  152. .is_enabled = max8925_is_enabled,
  153. };
  154. #define MAX8925_SDV(_id, min, max, step) \
  155. { \
  156. .desc = { \
  157. .name = "SDV" #_id, \
  158. .ops = &max8925_regulator_sdv_ops, \
  159. .type = REGULATOR_VOLTAGE, \
  160. .id = MAX8925_ID_SD##_id, \
  161. .owner = THIS_MODULE, \
  162. }, \
  163. .min_uV = min * 1000, \
  164. .max_uV = max * 1000, \
  165. .step_uV = step * 1000, \
  166. .vol_reg = MAX8925_SDV##_id, \
  167. .vol_shift = 0, \
  168. .vol_nbits = 6, \
  169. .enable_reg = MAX8925_SDCTL##_id, \
  170. }
  171. #define MAX8925_LDO(_id, min, max, step) \
  172. { \
  173. .desc = { \
  174. .name = "LDO" #_id, \
  175. .ops = &max8925_regulator_ldo_ops, \
  176. .type = REGULATOR_VOLTAGE, \
  177. .id = MAX8925_ID_LDO##_id, \
  178. .owner = THIS_MODULE, \
  179. }, \
  180. .min_uV = min * 1000, \
  181. .max_uV = max * 1000, \
  182. .step_uV = step * 1000, \
  183. .vol_reg = MAX8925_LDOVOUT##_id, \
  184. .vol_shift = 0, \
  185. .vol_nbits = 6, \
  186. .enable_reg = MAX8925_LDOCTL##_id, \
  187. }
  188. static struct max8925_regulator_info max8925_regulator_info[] = {
  189. MAX8925_SDV(1, 637.5, 1425, 12.5),
  190. MAX8925_SDV(2, 650, 2225, 25),
  191. MAX8925_SDV(3, 750, 3900, 50),
  192. MAX8925_LDO(1, 750, 3900, 50),
  193. MAX8925_LDO(2, 650, 2250, 25),
  194. MAX8925_LDO(3, 650, 2250, 25),
  195. MAX8925_LDO(4, 750, 3900, 50),
  196. MAX8925_LDO(5, 750, 3900, 50),
  197. MAX8925_LDO(6, 750, 3900, 50),
  198. MAX8925_LDO(7, 750, 3900, 50),
  199. MAX8925_LDO(8, 750, 3900, 50),
  200. MAX8925_LDO(9, 750, 3900, 50),
  201. MAX8925_LDO(10, 750, 3900, 50),
  202. MAX8925_LDO(11, 750, 3900, 50),
  203. MAX8925_LDO(12, 750, 3900, 50),
  204. MAX8925_LDO(13, 750, 3900, 50),
  205. MAX8925_LDO(14, 750, 3900, 50),
  206. MAX8925_LDO(15, 750, 3900, 50),
  207. MAX8925_LDO(16, 750, 3900, 50),
  208. MAX8925_LDO(17, 650, 2250, 25),
  209. MAX8925_LDO(18, 650, 2250, 25),
  210. MAX8925_LDO(19, 750, 3900, 50),
  211. MAX8925_LDO(20, 750, 3900, 50),
  212. };
  213. static struct max8925_regulator_info * __devinit find_regulator_info(int id)
  214. {
  215. struct max8925_regulator_info *ri;
  216. int i;
  217. for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
  218. ri = &max8925_regulator_info[i];
  219. if (ri->desc.id == id)
  220. return ri;
  221. }
  222. return NULL;
  223. }
  224. static int __devinit max8925_regulator_probe(struct platform_device *pdev)
  225. {
  226. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  227. struct max8925_platform_data *pdata = chip->dev->platform_data;
  228. struct max8925_regulator_info *ri;
  229. struct regulator_dev *rdev;
  230. ri = find_regulator_info(pdev->id);
  231. if (ri == NULL) {
  232. dev_err(&pdev->dev, "invalid regulator ID specified\n");
  233. return -EINVAL;
  234. }
  235. ri->i2c = chip->i2c;
  236. ri->chip = chip;
  237. rdev = regulator_register(&ri->desc, &pdev->dev,
  238. pdata->regulator[pdev->id], ri, NULL);
  239. if (IS_ERR(rdev)) {
  240. dev_err(&pdev->dev, "failed to register regulator %s\n",
  241. ri->desc.name);
  242. return PTR_ERR(rdev);
  243. }
  244. platform_set_drvdata(pdev, rdev);
  245. return 0;
  246. }
  247. static int __devexit max8925_regulator_remove(struct platform_device *pdev)
  248. {
  249. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  250. platform_set_drvdata(pdev, NULL);
  251. regulator_unregister(rdev);
  252. return 0;
  253. }
  254. static struct platform_driver max8925_regulator_driver = {
  255. .driver = {
  256. .name = "max8925-regulator",
  257. .owner = THIS_MODULE,
  258. },
  259. .probe = max8925_regulator_probe,
  260. .remove = __devexit_p(max8925_regulator_remove),
  261. };
  262. static int __init max8925_regulator_init(void)
  263. {
  264. return platform_driver_register(&max8925_regulator_driver);
  265. }
  266. subsys_initcall(max8925_regulator_init);
  267. static void __exit max8925_regulator_exit(void)
  268. {
  269. platform_driver_unregister(&max8925_regulator_driver);
  270. }
  271. module_exit(max8925_regulator_exit);
  272. MODULE_LICENSE("GPL");
  273. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  274. MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
  275. MODULE_ALIAS("platform:max8925-regulator");