pm8921-core.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/err.h>
  18. #include <linux/msm_ssbi.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/mfd/pm8xxx/pm8921.h>
  21. #include <linux/mfd/pm8xxx/core.h>
  22. #define REG_HWREV 0x002 /* PMIC4 revision */
  23. #define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */
  24. struct pm8921 {
  25. struct device *dev;
  26. struct pm_irq_chip *irq_chip;
  27. };
  28. static int pm8921_readb(const struct device *dev, u16 addr, u8 *val)
  29. {
  30. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  31. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  32. return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
  33. }
  34. static int pm8921_writeb(const struct device *dev, u16 addr, u8 val)
  35. {
  36. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  37. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  38. return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
  39. }
  40. static int pm8921_read_buf(const struct device *dev, u16 addr, u8 *buf,
  41. int cnt)
  42. {
  43. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  44. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  45. return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
  46. }
  47. static int pm8921_write_buf(const struct device *dev, u16 addr, u8 *buf,
  48. int cnt)
  49. {
  50. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  51. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  52. return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
  53. }
  54. static int pm8921_read_irq_stat(const struct device *dev, int irq)
  55. {
  56. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  57. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  58. return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
  59. }
  60. static struct pm8xxx_drvdata pm8921_drvdata = {
  61. .pmic_readb = pm8921_readb,
  62. .pmic_writeb = pm8921_writeb,
  63. .pmic_read_buf = pm8921_read_buf,
  64. .pmic_write_buf = pm8921_write_buf,
  65. .pmic_read_irq_stat = pm8921_read_irq_stat,
  66. };
  67. static int __devinit pm8921_add_subdevices(const struct pm8921_platform_data
  68. *pdata,
  69. struct pm8921 *pmic,
  70. u32 rev)
  71. {
  72. int ret = 0, irq_base = 0;
  73. struct pm_irq_chip *irq_chip;
  74. if (pdata->irq_pdata) {
  75. pdata->irq_pdata->irq_cdata.nirqs = PM8921_NR_IRQS;
  76. pdata->irq_pdata->irq_cdata.rev = rev;
  77. irq_base = pdata->irq_pdata->irq_base;
  78. irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
  79. if (IS_ERR(irq_chip)) {
  80. pr_err("Failed to init interrupts ret=%ld\n",
  81. PTR_ERR(irq_chip));
  82. return PTR_ERR(irq_chip);
  83. }
  84. pmic->irq_chip = irq_chip;
  85. }
  86. return ret;
  87. }
  88. static int __devinit pm8921_probe(struct platform_device *pdev)
  89. {
  90. const struct pm8921_platform_data *pdata = pdev->dev.platform_data;
  91. struct pm8921 *pmic;
  92. int rc;
  93. u8 val;
  94. u32 rev;
  95. if (!pdata) {
  96. pr_err("missing platform data\n");
  97. return -EINVAL;
  98. }
  99. pmic = kzalloc(sizeof(struct pm8921), GFP_KERNEL);
  100. if (!pmic) {
  101. pr_err("Cannot alloc pm8921 struct\n");
  102. return -ENOMEM;
  103. }
  104. /* Read PMIC chip revision */
  105. rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV, &val, sizeof(val));
  106. if (rc) {
  107. pr_err("Failed to read hw rev reg %d:rc=%d\n", REG_HWREV, rc);
  108. goto err_read_rev;
  109. }
  110. pr_info("PMIC revision 1: %02X\n", val);
  111. rev = val;
  112. /* Read PMIC chip revision 2 */
  113. rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV_2, &val, sizeof(val));
  114. if (rc) {
  115. pr_err("Failed to read hw rev 2 reg %d:rc=%d\n",
  116. REG_HWREV_2, rc);
  117. goto err_read_rev;
  118. }
  119. pr_info("PMIC revision 2: %02X\n", val);
  120. rev |= val << BITS_PER_BYTE;
  121. pmic->dev = &pdev->dev;
  122. pm8921_drvdata.pm_chip_data = pmic;
  123. platform_set_drvdata(pdev, &pm8921_drvdata);
  124. rc = pm8921_add_subdevices(pdata, pmic, rev);
  125. if (rc) {
  126. pr_err("Cannot add subdevices rc=%d\n", rc);
  127. goto err;
  128. }
  129. /* gpio might not work if no irq device is found */
  130. WARN_ON(pmic->irq_chip == NULL);
  131. return 0;
  132. err:
  133. mfd_remove_devices(pmic->dev);
  134. platform_set_drvdata(pdev, NULL);
  135. err_read_rev:
  136. kfree(pmic);
  137. return rc;
  138. }
  139. static int __devexit pm8921_remove(struct platform_device *pdev)
  140. {
  141. struct pm8xxx_drvdata *drvdata;
  142. struct pm8921 *pmic = NULL;
  143. drvdata = platform_get_drvdata(pdev);
  144. if (drvdata)
  145. pmic = drvdata->pm_chip_data;
  146. if (pmic)
  147. mfd_remove_devices(pmic->dev);
  148. if (pmic->irq_chip) {
  149. pm8xxx_irq_exit(pmic->irq_chip);
  150. pmic->irq_chip = NULL;
  151. }
  152. platform_set_drvdata(pdev, NULL);
  153. kfree(pmic);
  154. return 0;
  155. }
  156. static struct platform_driver pm8921_driver = {
  157. .probe = pm8921_probe,
  158. .remove = __devexit_p(pm8921_remove),
  159. .driver = {
  160. .name = "pm8921-core",
  161. .owner = THIS_MODULE,
  162. },
  163. };
  164. static int __init pm8921_init(void)
  165. {
  166. return platform_driver_register(&pm8921_driver);
  167. }
  168. subsys_initcall(pm8921_init);
  169. static void __exit pm8921_exit(void)
  170. {
  171. platform_driver_unregister(&pm8921_driver);
  172. }
  173. module_exit(pm8921_exit);
  174. MODULE_LICENSE("GPL v2");
  175. MODULE_DESCRIPTION("PMIC 8921 core driver");
  176. MODULE_VERSION("1.0");
  177. MODULE_ALIAS("platform:pm8921-core");