pm8058-xo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (c) 2011, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/err.h>
  14. #include <linux/string.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/bitops.h>
  19. #include <linux/mfd/pmic8058.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/mfd/pm8xxx/core.h>
  22. #include <linux/regulator/pm8058-xo.h>
  23. /* XO buffer masks and values */
  24. #define XO_PULLDOWN_MASK 0x08
  25. #define XO_PULLDOWN_ENABLE 0x08
  26. #define XO_PULLDOWN_DISABLE 0x00
  27. #define XO_BUFFER_MASK 0x04
  28. #define XO_BUFFER_ENABLE 0x04
  29. #define XO_BUFFER_DISABLE 0x00
  30. #define XO_MODE_MASK 0x01
  31. #define XO_MODE_MANUAL 0x00
  32. #define XO_ENABLE_MASK (XO_MODE_MASK | XO_BUFFER_MASK)
  33. #define XO_ENABLE (XO_MODE_MANUAL | XO_BUFFER_ENABLE)
  34. #define XO_DISABLE (XO_MODE_MANUAL | XO_BUFFER_DISABLE)
  35. struct pm8058_xo_buffer {
  36. struct device *dev;
  37. struct pm8058_xo_pdata *pdata;
  38. struct regulator_dev *rdev;
  39. u16 ctrl_addr;
  40. u8 ctrl_reg;
  41. };
  42. #define XO_BUFFER(_id, _ctrl_addr) \
  43. [PM8058_XO_ID_##_id] = { \
  44. .ctrl_addr = _ctrl_addr, \
  45. }
  46. static struct pm8058_xo_buffer pm8058_xo_buffer[] = {
  47. XO_BUFFER(A0, 0x185),
  48. XO_BUFFER(A1, 0x186),
  49. };
  50. static int pm8058_xo_buffer_write(struct pm8058_xo_buffer *xo,
  51. u16 addr, u8 val, u8 mask, u8 *reg_save)
  52. {
  53. u8 reg;
  54. int rc = 0;
  55. reg = (*reg_save & ~mask) | (val & mask);
  56. if (reg != *reg_save)
  57. rc = pm8xxx_writeb(xo->dev->parent, addr, reg);
  58. if (rc)
  59. pr_err("FAIL: pm8xxx_write: rc=%d\n", rc);
  60. else
  61. *reg_save = reg;
  62. return rc;
  63. }
  64. static int pm8058_xo_buffer_enable(struct regulator_dev *dev)
  65. {
  66. struct pm8058_xo_buffer *xo = rdev_get_drvdata(dev);
  67. int rc;
  68. rc = pm8058_xo_buffer_write(xo, xo->ctrl_addr, XO_ENABLE,
  69. XO_ENABLE_MASK, &xo->ctrl_reg);
  70. if (rc)
  71. pr_err("FAIL: pm8058_xo_buffer_write: rc=%d\n", rc);
  72. return rc;
  73. }
  74. static int pm8058_xo_buffer_is_enabled(struct regulator_dev *dev)
  75. {
  76. struct pm8058_xo_buffer *xo = rdev_get_drvdata(dev);
  77. if (xo->ctrl_reg & XO_BUFFER_ENABLE)
  78. return 1;
  79. else
  80. return 0;
  81. }
  82. static int pm8058_xo_buffer_disable(struct regulator_dev *dev)
  83. {
  84. struct pm8058_xo_buffer *xo = rdev_get_drvdata(dev);
  85. int rc;
  86. rc = pm8058_xo_buffer_write(xo, xo->ctrl_addr, XO_DISABLE,
  87. XO_ENABLE_MASK, &xo->ctrl_reg);
  88. if (rc)
  89. pr_err("FAIL: pm8058_xo_buffer_write: rc=%d\n", rc);
  90. return rc;
  91. }
  92. static struct regulator_ops pm8058_xo_ops = {
  93. .enable = pm8058_xo_buffer_enable,
  94. .disable = pm8058_xo_buffer_disable,
  95. .is_enabled = pm8058_xo_buffer_is_enabled,
  96. };
  97. #define VREG_DESCRIP(_id, _name, _ops) \
  98. [_id] = { \
  99. .id = _id, \
  100. .name = _name, \
  101. .ops = _ops, \
  102. .type = REGULATOR_VOLTAGE, \
  103. .owner = THIS_MODULE, \
  104. }
  105. static struct regulator_desc pm8058_xo_buffer_desc[] = {
  106. VREG_DESCRIP(PM8058_XO_ID_A0, "8058_xo_a0", &pm8058_xo_ops),
  107. VREG_DESCRIP(PM8058_XO_ID_A1, "8058_xo_a1", &pm8058_xo_ops),
  108. };
  109. static int pm8058_init_xo_buffer(struct pm8058_xo_buffer *xo)
  110. {
  111. int rc;
  112. /* Save the current control register state */
  113. rc = pm8xxx_readb(xo->dev->parent, xo->ctrl_addr, &xo->ctrl_reg);
  114. if (rc)
  115. pr_err("FAIL: pm8xxx_read: rc=%d\n", rc);
  116. return rc;
  117. }
  118. static int __devinit pm8058_xo_buffer_probe(struct platform_device *pdev)
  119. {
  120. struct regulator_desc *rdesc;
  121. struct pm8058_xo_buffer *xo;
  122. int rc = 0;
  123. if (pdev == NULL)
  124. return -EINVAL;
  125. if (pdev->id >= 0 && pdev->id < PM8058_XO_ID_MAX) {
  126. rdesc = &pm8058_xo_buffer_desc[pdev->id];
  127. xo = &pm8058_xo_buffer[pdev->id];
  128. xo->pdata = pdev->dev.platform_data;
  129. xo->dev = &pdev->dev;
  130. rc = pm8058_init_xo_buffer(xo);
  131. if (rc)
  132. goto bail;
  133. xo->rdev = regulator_register(rdesc, &pdev->dev,
  134. &xo->pdata->init_data, xo, NULL);
  135. if (IS_ERR(xo->rdev)) {
  136. rc = PTR_ERR(xo->rdev);
  137. pr_err("FAIL: regulator_register(%s): rc=%d\n",
  138. pm8058_xo_buffer_desc[pdev->id].name, rc);
  139. }
  140. } else {
  141. rc = -ENODEV;
  142. }
  143. bail:
  144. if (rc)
  145. pr_err("Error: xo-id=%d, rc=%d\n", pdev->id, rc);
  146. return rc;
  147. }
  148. static int __devexit pm8058_xo_buffer_remove(struct platform_device *pdev)
  149. {
  150. regulator_unregister(pm8058_xo_buffer[pdev->id].rdev);
  151. return 0;
  152. }
  153. static struct platform_driver pm8058_xo_buffer_driver = {
  154. .probe = pm8058_xo_buffer_probe,
  155. .remove = __devexit_p(pm8058_xo_buffer_remove),
  156. .driver = {
  157. .name = PM8058_XO_BUFFER_DEV_NAME,
  158. .owner = THIS_MODULE,
  159. },
  160. };
  161. static int __init pm8058_xo_buffer_init(void)
  162. {
  163. return platform_driver_register(&pm8058_xo_buffer_driver);
  164. }
  165. static void __exit pm8058_xo_buffer_exit(void)
  166. {
  167. platform_driver_unregister(&pm8058_xo_buffer_driver);
  168. }
  169. subsys_initcall(pm8058_xo_buffer_init);
  170. module_exit(pm8058_xo_buffer_exit);
  171. MODULE_LICENSE("GPL v2");
  172. MODULE_DESCRIPTION("PMIC8058 XO buffer driver");
  173. MODULE_VERSION("1.0");
  174. MODULE_ALIAS("platform:" PM8058_XO_BUFFER_DEV_NAME);