pmic8901.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* Copyright (c) 2010-2012, 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. #include <linux/interrupt.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/mfd/core.h>
  16. #include <linux/msm_ssbi.h>
  17. #include <linux/mfd/pmic8901.h>
  18. #include <linux/mfd/pm8xxx/core.h>
  19. #include <linux/module.h>
  20. /* PMIC8901 Revision */
  21. #define PM8901_REG_REV 0x002
  22. #define PM8901_VERSION_MASK 0xF0
  23. #define PM8901_REVISION_MASK 0x0F
  24. #define PM8901_VERSION_VALUE 0xF0
  25. #define REG_IRQ_BASE 0xD5
  26. #define REG_MPP_BASE 0x27
  27. #define REG_TEMP_ALRM_CTRL 0x23
  28. #define REG_TEMP_ALRM_PWM 0x24
  29. #define SINGLE_IRQ_RESOURCE(_name, _irq) \
  30. { \
  31. .name = _name, \
  32. .start = _irq, \
  33. .end = _irq, \
  34. .flags = IORESOURCE_IRQ, \
  35. }
  36. struct pm8901_chip {
  37. struct pm8901_platform_data pdata;
  38. struct device *dev;
  39. struct pm_irq_chip *irq_chip;
  40. struct mfd_cell *mfd_regulators;
  41. u8 revision;
  42. };
  43. static int pm8901_readb(const struct device *dev, u16 addr, u8 *val)
  44. {
  45. const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
  46. const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
  47. return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
  48. }
  49. static int pm8901_writeb(const struct device *dev, u16 addr, u8 val)
  50. {
  51. const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
  52. const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
  53. return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
  54. }
  55. static int pm8901_read_buf(const struct device *dev, u16 addr, u8 *buf,
  56. int cnt)
  57. {
  58. const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
  59. const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
  60. return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
  61. }
  62. static int pm8901_write_buf(const struct device *dev, u16 addr, u8 *buf,
  63. int cnt)
  64. {
  65. const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
  66. const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
  67. return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
  68. }
  69. static int pm8901_read_irq_stat(const struct device *dev, int irq)
  70. {
  71. const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
  72. const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
  73. return pm8xxx_get_irq_stat(pmic->irq_chip, irq);
  74. return 0;
  75. }
  76. static enum pm8xxx_version pm8901_get_version(const struct device *dev)
  77. {
  78. const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
  79. const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
  80. enum pm8xxx_version version = -ENODEV;
  81. if ((pmic->revision & PM8901_VERSION_MASK) == PM8901_VERSION_VALUE)
  82. version = PM8XXX_VERSION_8901;
  83. return version;
  84. }
  85. static int pm8901_get_revision(const struct device *dev)
  86. {
  87. const struct pm8xxx_drvdata *pm8901_drvdata = dev_get_drvdata(dev);
  88. const struct pm8901_chip *pmic = pm8901_drvdata->pm_chip_data;
  89. return pmic->revision & PM8901_REVISION_MASK;
  90. }
  91. static struct pm8xxx_drvdata pm8901_drvdata = {
  92. .pmic_readb = pm8901_readb,
  93. .pmic_writeb = pm8901_writeb,
  94. .pmic_read_buf = pm8901_read_buf,
  95. .pmic_write_buf = pm8901_write_buf,
  96. .pmic_read_irq_stat = pm8901_read_irq_stat,
  97. .pmic_get_version = pm8901_get_version,
  98. .pmic_get_revision = pm8901_get_revision,
  99. };
  100. static struct mfd_cell misc_cell = {
  101. .name = PM8XXX_MISC_DEV_NAME,
  102. .id = 1,
  103. };
  104. static struct mfd_cell debugfs_cell = {
  105. .name = "pm8xxx-debug",
  106. .id = 1,
  107. .platform_data = "pm8901-dbg",
  108. .pdata_size = sizeof("pm8901-dbg"),
  109. };
  110. static const struct resource thermal_alarm_cell_resources[] = {
  111. SINGLE_IRQ_RESOURCE("pm8901_tempstat_irq", PM8901_TEMPSTAT_IRQ),
  112. SINGLE_IRQ_RESOURCE("pm8901_overtemp_irq", PM8901_OVERTEMP_IRQ),
  113. };
  114. static struct pm8xxx_tm_core_data thermal_alarm_cdata = {
  115. .adc_type = PM8XXX_TM_ADC_NONE,
  116. .reg_addr_temp_alarm_ctrl = REG_TEMP_ALRM_CTRL,
  117. .reg_addr_temp_alarm_pwm = REG_TEMP_ALRM_PWM,
  118. .tm_name = "pm8901_tz",
  119. .irq_name_temp_stat = "pm8901_tempstat_irq",
  120. .irq_name_over_temp = "pm8901_overtemp_irq",
  121. };
  122. static struct mfd_cell thermal_alarm_cell = {
  123. .name = PM8XXX_TM_DEV_NAME,
  124. .id = 1,
  125. .resources = thermal_alarm_cell_resources,
  126. .num_resources = ARRAY_SIZE(thermal_alarm_cell_resources),
  127. .platform_data = &thermal_alarm_cdata,
  128. .pdata_size = sizeof(struct pm8xxx_tm_core_data),
  129. };
  130. static const struct resource mpp_cell_resources[] = {
  131. {
  132. .start = PM8901_IRQ_BLOCK_BIT(PM8901_MPP_BLOCK_START, 0),
  133. .end = PM8901_IRQ_BLOCK_BIT(PM8901_MPP_BLOCK_START, 0)
  134. + PM8901_MPPS - 1,
  135. .flags = IORESOURCE_IRQ,
  136. },
  137. };
  138. static struct mfd_cell mpp_cell = {
  139. .name = PM8XXX_MPP_DEV_NAME,
  140. .id = 1,
  141. .resources = mpp_cell_resources,
  142. .num_resources = ARRAY_SIZE(mpp_cell_resources),
  143. };
  144. static int __devinit
  145. pm8901_add_subdevices(const struct pm8901_platform_data *pdata,
  146. struct pm8901_chip *pmic)
  147. {
  148. int rc = 0, irq_base = 0, i;
  149. struct pm_irq_chip *irq_chip;
  150. static struct mfd_cell *mfd_regulators;
  151. if (pdata->irq_pdata) {
  152. pdata->irq_pdata->irq_cdata.nirqs = PM8901_NR_IRQS;
  153. pdata->irq_pdata->irq_cdata.base_addr = REG_IRQ_BASE;
  154. irq_base = pdata->irq_pdata->irq_base;
  155. irq_chip = pm8xxx_irq_init(pmic->dev, pdata->irq_pdata);
  156. if (IS_ERR(irq_chip)) {
  157. pr_err("Failed to init interrupts ret=%ld\n",
  158. PTR_ERR(irq_chip));
  159. return PTR_ERR(irq_chip);
  160. }
  161. pmic->irq_chip = irq_chip;
  162. }
  163. if (pdata->mpp_pdata) {
  164. pdata->mpp_pdata->core_data.nmpps = PM8901_MPPS;
  165. pdata->mpp_pdata->core_data.base_addr = REG_MPP_BASE;
  166. mpp_cell.platform_data = pdata->mpp_pdata;
  167. mpp_cell.pdata_size = sizeof(struct pm8xxx_mpp_platform_data);
  168. rc = mfd_add_devices(pmic->dev, 0, &mpp_cell, 1, NULL,
  169. irq_base);
  170. if (rc) {
  171. pr_err("Failed to add mpp subdevice ret=%d\n", rc);
  172. goto bail;
  173. }
  174. }
  175. if (pdata->num_regulators > 0 && pdata->regulator_pdatas) {
  176. mfd_regulators = kzalloc(sizeof(struct mfd_cell)
  177. * (pdata->num_regulators), GFP_KERNEL);
  178. if (!mfd_regulators) {
  179. pr_err("Cannot allocate %d bytes for pm8901 regulator "
  180. "mfd cells\n", sizeof(struct mfd_cell)
  181. * (pdata->num_regulators));
  182. rc = -ENOMEM;
  183. goto bail;
  184. }
  185. for (i = 0; i < pdata->num_regulators; i++) {
  186. mfd_regulators[i].name = "pm8901-regulator";
  187. mfd_regulators[i].id = pdata->regulator_pdatas[i].id;
  188. mfd_regulators[i].platform_data =
  189. &(pdata->regulator_pdatas[i]);
  190. mfd_regulators[i].pdata_size =
  191. sizeof(struct pm8901_vreg_pdata);
  192. }
  193. rc = mfd_add_devices(pmic->dev, 0, mfd_regulators,
  194. pdata->num_regulators, NULL, irq_base);
  195. if (rc) {
  196. pr_err("Failed to add regulator subdevices ret=%d\n",
  197. rc);
  198. kfree(mfd_regulators);
  199. goto bail;
  200. }
  201. pmic->mfd_regulators = mfd_regulators;
  202. }
  203. rc = mfd_add_devices(pmic->dev, 0, &thermal_alarm_cell, 1, NULL,
  204. irq_base);
  205. if (rc) {
  206. pr_err("Failed to add thermal alarm subdevice ret=%d\n",
  207. rc);
  208. goto bail;
  209. }
  210. rc = mfd_add_devices(pmic->dev, 0, &debugfs_cell, 1, NULL, irq_base);
  211. if (rc) {
  212. pr_err("Failed to add debugfs subdevice ret=%d\n", rc);
  213. goto bail;
  214. }
  215. if (pdata->misc_pdata) {
  216. misc_cell.platform_data = pdata->misc_pdata;
  217. misc_cell.pdata_size = sizeof(struct pm8xxx_misc_platform_data);
  218. rc = mfd_add_devices(pmic->dev, 0, &misc_cell, 1, NULL,
  219. irq_base);
  220. if (rc) {
  221. pr_err("Failed to add misc subdevice ret=%d\n", rc);
  222. goto bail;
  223. }
  224. }
  225. return rc;
  226. bail:
  227. if (pmic->irq_chip) {
  228. pm8xxx_irq_exit(pmic->irq_chip);
  229. pmic->irq_chip = NULL;
  230. }
  231. return rc;
  232. }
  233. static const char * const pm8901_rev_names[] = {
  234. [PM8XXX_REVISION_8901_TEST] = "test",
  235. [PM8XXX_REVISION_8901_1p0] = "1.0",
  236. [PM8XXX_REVISION_8901_1p1] = "1.1",
  237. [PM8XXX_REVISION_8901_2p0] = "2.0",
  238. [PM8XXX_REVISION_8901_2p1] = "2.1",
  239. [PM8XXX_REVISION_8901_2p2] = "2.2",
  240. [PM8XXX_REVISION_8901_2p3] = "2.3",
  241. };
  242. static int __devinit pm8901_probe(struct platform_device *pdev)
  243. {
  244. int rc;
  245. struct pm8901_platform_data *pdata = pdev->dev.platform_data;
  246. const char *revision_name = "unknown";
  247. struct pm8901_chip *pmic;
  248. int revision;
  249. if (pdata == NULL) {
  250. pr_err("%s: No platform_data or IRQ.\n", __func__);
  251. return -ENODEV;
  252. }
  253. pmic = kzalloc(sizeof *pmic, GFP_KERNEL);
  254. if (pmic == NULL) {
  255. pr_err("%s: kzalloc() failed.\n", __func__);
  256. return -ENOMEM;
  257. }
  258. pmic->dev = &pdev->dev;
  259. pm8901_drvdata.pm_chip_data = pmic;
  260. platform_set_drvdata(pdev, &pm8901_drvdata);
  261. /* Read PMIC chip revision */
  262. rc = pm8901_readb(pmic->dev, PM8901_REG_REV, &pmic->revision);
  263. if (rc)
  264. pr_err("%s: Failed reading version register rc=%d.\n",
  265. __func__, rc);
  266. pr_info("%s: PMIC revision reg: %02X\n", __func__, pmic->revision);
  267. revision = pm8xxx_get_revision(pmic->dev);
  268. if (revision >= 0 && revision < ARRAY_SIZE(pm8901_rev_names))
  269. revision_name = pm8901_rev_names[revision];
  270. pr_info("%s: PMIC version: PM8901 rev %s\n", __func__, revision_name);
  271. (void) memcpy((void *)&pmic->pdata, (const void *)pdata,
  272. sizeof(pmic->pdata));
  273. rc = pm8901_add_subdevices(pdata, pmic);
  274. if (rc) {
  275. pr_err("Cannot add subdevices rc=%d\n", rc);
  276. goto err;
  277. }
  278. return 0;
  279. err:
  280. platform_set_drvdata(pdev, NULL);
  281. kfree(pmic);
  282. return rc;
  283. }
  284. static int __devexit pm8901_remove(struct platform_device *pdev)
  285. {
  286. struct pm8xxx_drvdata *drvdata;
  287. struct pm8901_chip *pmic = NULL;
  288. drvdata = platform_get_drvdata(pdev);
  289. if (drvdata)
  290. pmic = drvdata->pm_chip_data;
  291. if (pmic) {
  292. if (pmic->dev)
  293. mfd_remove_devices(pmic->dev);
  294. if (pmic->irq_chip)
  295. pm8xxx_irq_exit(pmic->irq_chip);
  296. kfree(pmic->mfd_regulators);
  297. kfree(pmic);
  298. }
  299. platform_set_drvdata(pdev, NULL);
  300. return 0;
  301. }
  302. static struct platform_driver pm8901_driver = {
  303. .probe = pm8901_probe,
  304. .remove = __devexit_p(pm8901_remove),
  305. .driver = {
  306. .name = "pm8901-core",
  307. .owner = THIS_MODULE,
  308. },
  309. };
  310. static int __init pm8901_init(void)
  311. {
  312. return platform_driver_register(&pm8901_driver);
  313. }
  314. postcore_initcall(pm8901_init);
  315. static void __exit pm8901_exit(void)
  316. {
  317. platform_driver_unregister(&pm8901_driver);
  318. }
  319. module_exit(pm8901_exit);
  320. MODULE_LICENSE("GPL v2");
  321. MODULE_DESCRIPTION("PMIC8901 core driver");
  322. MODULE_VERSION("1.0");
  323. MODULE_ALIAS("platform:pmic8901-core");