pm8921-core.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/ssbi.h>
  20. #include <linux/mfd/core.h>
  21. #include <linux/mfd/pm8xxx/pm8921.h>
  22. #include <linux/mfd/pm8xxx/core.h>
  23. #define REG_HWREV 0x002 /* PMIC4 revision */
  24. #define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */
  25. struct pm8921 {
  26. struct device *dev;
  27. struct pm_irq_chip *irq_chip;
  28. };
  29. static int pm8921_readb(const struct device *dev, u16 addr, u8 *val)
  30. {
  31. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  32. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  33. return ssbi_read(pmic->dev->parent, addr, val, 1);
  34. }
  35. static int pm8921_writeb(const struct device *dev, u16 addr, u8 val)
  36. {
  37. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  38. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  39. return ssbi_write(pmic->dev->parent, addr, &val, 1);
  40. }
  41. static int pm8921_read_buf(const struct device *dev, u16 addr, u8 *buf,
  42. int cnt)
  43. {
  44. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  45. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  46. return ssbi_read(pmic->dev->parent, addr, buf, cnt);
  47. }
  48. static int pm8921_write_buf(const struct device *dev, u16 addr, u8 *buf,
  49. int cnt)
  50. {
  51. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  52. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  53. return ssbi_write(pmic->dev->parent, addr, buf, cnt);
  54. }
  55. static int pm8921_read_irq_stat(const struct device *dev, int irq)
  56. {
  57. const struct pm8xxx_drvdata *pm8921_drvdata = dev_get_drvdata(dev);
  58. const struct pm8921 *pmic = pm8921_drvdata->pm_chip_data;
  59. return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
  60. }
  61. static struct pm8xxx_drvdata pm8921_drvdata = {
  62. .pmic_readb = pm8921_readb,
  63. .pmic_writeb = pm8921_writeb,
  64. .pmic_read_buf = pm8921_read_buf,
  65. .pmic_write_buf = pm8921_write_buf,
  66. .pmic_read_irq_stat = pm8921_read_irq_stat,
  67. };
  68. static int pm8921_add_subdevices(const struct pm8921_platform_data
  69. *pdata,
  70. struct pm8921 *pmic,
  71. u32 rev)
  72. {
  73. int ret = 0, irq_base = 0;
  74. struct pm_irq_chip *irq_chip;
  75. if (pdata->irq_pdata) {
  76. pdata->irq_pdata->irq_cdata.nirqs = PM8921_NR_IRQS;
  77. pdata->irq_pdata->irq_cdata.rev = rev;
  78. irq_base = pdata->irq_pdata->irq_base;
  79. irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
  80. if (IS_ERR(irq_chip)) {
  81. pr_err("Failed to init interrupts ret=%ld\n",
  82. PTR_ERR(irq_chip));
  83. return PTR_ERR(irq_chip);
  84. }
  85. pmic->irq_chip = irq_chip;
  86. }
  87. return ret;
  88. }
  89. static int pm8921_probe(struct platform_device *pdev)
  90. {
  91. const struct pm8921_platform_data *pdata = dev_get_platdata(&pdev->dev);
  92. struct pm8921 *pmic;
  93. int rc;
  94. u8 val;
  95. u32 rev;
  96. if (!pdata) {
  97. pr_err("missing platform data\n");
  98. return -EINVAL;
  99. }
  100. pmic = devm_kzalloc(&pdev->dev, sizeof(struct pm8921), GFP_KERNEL);
  101. if (!pmic) {
  102. pr_err("Cannot alloc pm8921 struct\n");
  103. return -ENOMEM;
  104. }
  105. /* Read PMIC chip revision */
  106. rc = ssbi_read(pdev->dev.parent, REG_HWREV, &val, sizeof(val));
  107. if (rc) {
  108. pr_err("Failed to read hw rev reg %d:rc=%d\n", REG_HWREV, rc);
  109. return rc;
  110. }
  111. pr_info("PMIC revision 1: %02X\n", val);
  112. rev = val;
  113. /* Read PMIC chip revision 2 */
  114. rc = ssbi_read(pdev->dev.parent, REG_HWREV_2, &val, sizeof(val));
  115. if (rc) {
  116. pr_err("Failed to read hw rev 2 reg %d:rc=%d\n",
  117. REG_HWREV_2, rc);
  118. return rc;
  119. }
  120. pr_info("PMIC revision 2: %02X\n", val);
  121. rev |= val << BITS_PER_BYTE;
  122. pmic->dev = &pdev->dev;
  123. pm8921_drvdata.pm_chip_data = pmic;
  124. platform_set_drvdata(pdev, &pm8921_drvdata);
  125. rc = pm8921_add_subdevices(pdata, pmic, rev);
  126. if (rc) {
  127. pr_err("Cannot add subdevices rc=%d\n", rc);
  128. goto err;
  129. }
  130. /* gpio might not work if no irq device is found */
  131. WARN_ON(pmic->irq_chip == NULL);
  132. return 0;
  133. err:
  134. mfd_remove_devices(pmic->dev);
  135. return rc;
  136. }
  137. static int pm8921_remove(struct platform_device *pdev)
  138. {
  139. struct pm8xxx_drvdata *drvdata;
  140. struct pm8921 *pmic = NULL;
  141. drvdata = platform_get_drvdata(pdev);
  142. if (drvdata)
  143. pmic = drvdata->pm_chip_data;
  144. if (pmic)
  145. mfd_remove_devices(pmic->dev);
  146. if (pmic->irq_chip) {
  147. pm8xxx_irq_exit(pmic->irq_chip);
  148. pmic->irq_chip = NULL;
  149. }
  150. return 0;
  151. }
  152. static struct platform_driver pm8921_driver = {
  153. .probe = pm8921_probe,
  154. .remove = pm8921_remove,
  155. .driver = {
  156. .name = "pm8921-core",
  157. .owner = THIS_MODULE,
  158. },
  159. };
  160. static int __init pm8921_init(void)
  161. {
  162. return platform_driver_register(&pm8921_driver);
  163. }
  164. subsys_initcall(pm8921_init);
  165. static void __exit pm8921_exit(void)
  166. {
  167. platform_driver_unregister(&pm8921_driver);
  168. }
  169. module_exit(pm8921_exit);
  170. MODULE_LICENSE("GPL v2");
  171. MODULE_DESCRIPTION("PMIC 8921 core driver");
  172. MODULE_VERSION("1.0");
  173. MODULE_ALIAS("platform:pm8921-core");