pm8821-core.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2011-2012, The Linux Foundation. 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/msm_ssbi.h>
  20. #include <linux/mfd/core.h>
  21. #include <linux/mfd/pm8xxx/pm8821.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. #define REG_MPP_BASE 0x050
  26. #define REG_IRQ_BASE 0x100
  27. #define REG_TEMP_ALARM_CTRL 0x01B
  28. #define REG_TEMP_ALARM_PWM 0x09B
  29. #define PM8821_VERSION_MASK 0xFFF0
  30. #define PM8821_VERSION_VALUE 0x0BF0
  31. #define PM8821_REVISION_MASK 0x000F
  32. #define SINGLE_IRQ_RESOURCE(_name, _irq) \
  33. { \
  34. .name = _name, \
  35. .start = _irq, \
  36. .end = _irq, \
  37. .flags = IORESOURCE_IRQ, \
  38. }
  39. struct pm8821 {
  40. struct device *dev;
  41. struct pm_irq_chip *irq_chip;
  42. u32 rev_registers;
  43. };
  44. static int pm8821_readb(const struct device *dev, u16 addr, u8 *val)
  45. {
  46. const struct pm8xxx_drvdata *pm8821_drvdata = dev_get_drvdata(dev);
  47. const struct pm8821 *pmic = pm8821_drvdata->pm_chip_data;
  48. return msm_ssbi_read(pmic->dev->parent, addr, val, 1);
  49. }
  50. static int pm8821_writeb(const struct device *dev, u16 addr, u8 val)
  51. {
  52. const struct pm8xxx_drvdata *pm8821_drvdata = dev_get_drvdata(dev);
  53. const struct pm8821 *pmic = pm8821_drvdata->pm_chip_data;
  54. return msm_ssbi_write(pmic->dev->parent, addr, &val, 1);
  55. }
  56. static int pm8821_read_buf(const struct device *dev, u16 addr, u8 *buf,
  57. int cnt)
  58. {
  59. const struct pm8xxx_drvdata *pm8821_drvdata = dev_get_drvdata(dev);
  60. const struct pm8821 *pmic = pm8821_drvdata->pm_chip_data;
  61. return msm_ssbi_read(pmic->dev->parent, addr, buf, cnt);
  62. }
  63. static int pm8821_write_buf(const struct device *dev, u16 addr, u8 *buf,
  64. int cnt)
  65. {
  66. const struct pm8xxx_drvdata *pm8821_drvdata = dev_get_drvdata(dev);
  67. const struct pm8821 *pmic = pm8821_drvdata->pm_chip_data;
  68. return msm_ssbi_write(pmic->dev->parent, addr, buf, cnt);
  69. }
  70. static int pm8821_read_irq_stat(const struct device *dev, int irq)
  71. {
  72. const struct pm8xxx_drvdata *pm8821_drvdata = dev_get_drvdata(dev);
  73. const struct pm8821 *pmic = pm8821_drvdata->pm_chip_data;
  74. return pm8821_get_irq_stat(pmic->irq_chip, irq);
  75. }
  76. static enum pm8xxx_version pm8821_get_version(const struct device *dev)
  77. {
  78. const struct pm8xxx_drvdata *pm8821_drvdata = dev_get_drvdata(dev);
  79. const struct pm8821 *pmic = pm8821_drvdata->pm_chip_data;
  80. enum pm8xxx_version version = -ENODEV;
  81. if ((pmic->rev_registers & PM8821_VERSION_MASK) == PM8821_VERSION_VALUE)
  82. version = PM8XXX_VERSION_8821;
  83. return version;
  84. }
  85. static int pm8821_get_revision(const struct device *dev)
  86. {
  87. const struct pm8xxx_drvdata *pm8821_drvdata = dev_get_drvdata(dev);
  88. const struct pm8821 *pmic = pm8821_drvdata->pm_chip_data;
  89. return pmic->rev_registers & PM8821_REVISION_MASK;
  90. }
  91. static struct pm8xxx_drvdata pm8821_drvdata = {
  92. .pmic_readb = pm8821_readb,
  93. .pmic_writeb = pm8821_writeb,
  94. .pmic_read_buf = pm8821_read_buf,
  95. .pmic_write_buf = pm8821_write_buf,
  96. .pmic_read_irq_stat = pm8821_read_irq_stat,
  97. .pmic_get_version = pm8821_get_version,
  98. .pmic_get_revision = pm8821_get_revision,
  99. };
  100. static const struct resource mpp_cell_resources[] __devinitconst = {
  101. {
  102. .start = PM8821_IRQ_BLOCK_BIT(PM8821_MPP_BLOCK_START, 0),
  103. .end = PM8821_IRQ_BLOCK_BIT(PM8821_MPP_BLOCK_START, 0)
  104. + PM8821_NR_MPPS - 1,
  105. .flags = IORESOURCE_IRQ,
  106. },
  107. };
  108. static struct mfd_cell mpp_cell __devinitdata = {
  109. .name = PM8XXX_MPP_DEV_NAME,
  110. .id = 1,
  111. .resources = mpp_cell_resources,
  112. .num_resources = ARRAY_SIZE(mpp_cell_resources),
  113. };
  114. static struct mfd_cell debugfs_cell __devinitdata = {
  115. .name = "pm8xxx-debug",
  116. .id = 1,
  117. .platform_data = "pm8821-dbg",
  118. .pdata_size = sizeof("pm8821-dbg"),
  119. };
  120. static const struct resource thermal_alarm_cell_resources[] __devinitconst = {
  121. SINGLE_IRQ_RESOURCE("pm8821_tempstat_irq", PM8821_TEMPSTAT_IRQ),
  122. SINGLE_IRQ_RESOURCE("pm8821_overtemp_irq", PM8821_OVERTEMP_IRQ),
  123. };
  124. static struct pm8xxx_tm_core_data thermal_alarm_cdata = {
  125. .adc_type = PM8XXX_TM_ADC_NONE,
  126. .reg_addr_temp_alarm_ctrl = REG_TEMP_ALARM_CTRL,
  127. .reg_addr_temp_alarm_pwm = REG_TEMP_ALARM_PWM,
  128. .tm_name = "pm8821_tz",
  129. .irq_name_temp_stat = "pm8821_tempstat_irq",
  130. .irq_name_over_temp = "pm8821_overtemp_irq",
  131. .default_no_adc_temp = 37000,
  132. };
  133. static struct mfd_cell thermal_alarm_cell __devinitdata = {
  134. .name = PM8XXX_TM_DEV_NAME,
  135. .id = 1,
  136. .resources = thermal_alarm_cell_resources,
  137. .num_resources = ARRAY_SIZE(thermal_alarm_cell_resources),
  138. .platform_data = &thermal_alarm_cdata,
  139. .pdata_size = sizeof(struct pm8xxx_tm_core_data),
  140. };
  141. static int __devinit
  142. pm8821_add_subdevices(const struct pm8821_platform_data *pdata,
  143. struct pm8821 *pmic)
  144. {
  145. int ret = 0, irq_base = 0;
  146. struct pm_irq_chip *irq_chip;
  147. if (pdata->irq_pdata) {
  148. pdata->irq_pdata->irq_cdata.nirqs = PM8821_NR_IRQS;
  149. pdata->irq_pdata->irq_cdata.base_addr = REG_IRQ_BASE;
  150. irq_base = pdata->irq_pdata->irq_base;
  151. irq_chip = pm8821_irq_init(pmic->dev, pdata->irq_pdata);
  152. if (IS_ERR(irq_chip)) {
  153. pr_err("Failed to init interrupts ret=%ld\n",
  154. PTR_ERR(irq_chip));
  155. return PTR_ERR(irq_chip);
  156. }
  157. pmic->irq_chip = irq_chip;
  158. }
  159. if (pdata->mpp_pdata) {
  160. pdata->mpp_pdata->core_data.nmpps = PM8821_NR_MPPS;
  161. pdata->mpp_pdata->core_data.base_addr = REG_MPP_BASE;
  162. mpp_cell.platform_data = pdata->mpp_pdata;
  163. mpp_cell.pdata_size = sizeof(struct pm8xxx_mpp_platform_data);
  164. ret = mfd_add_devices(pmic->dev, 0, &mpp_cell, 1, NULL,
  165. irq_base);
  166. if (ret) {
  167. pr_err("Failed to add mpp subdevice ret=%d\n", ret);
  168. goto bail;
  169. }
  170. }
  171. ret = mfd_add_devices(pmic->dev, 0, &debugfs_cell, 1, NULL, irq_base);
  172. if (ret) {
  173. pr_err("Failed to add debugfs subdevice ret=%d\n", ret);
  174. goto bail;
  175. }
  176. ret = mfd_add_devices(pmic->dev, 0, &thermal_alarm_cell, 1, NULL,
  177. irq_base);
  178. if (ret) {
  179. pr_err("Failed to add thermal alarm subdevice ret=%d\n",
  180. ret);
  181. goto bail;
  182. }
  183. return 0;
  184. bail:
  185. if (pmic->irq_chip) {
  186. pm8821_irq_exit(pmic->irq_chip);
  187. pmic->irq_chip = NULL;
  188. }
  189. return ret;
  190. }
  191. static const char * const pm8821_rev_names[] = {
  192. [PM8XXX_REVISION_8821_TEST] = "test",
  193. [PM8XXX_REVISION_8821_1p0] = "1.0",
  194. [PM8XXX_REVISION_8821_2p0] = "2.0",
  195. [PM8XXX_REVISION_8821_2p1] = "2.1",
  196. };
  197. static int __devinit pm8821_probe(struct platform_device *pdev)
  198. {
  199. const struct pm8821_platform_data *pdata = pdev->dev.platform_data;
  200. const char *revision_name = "unknown";
  201. struct pm8821 *pmic;
  202. enum pm8xxx_version version;
  203. int revision;
  204. int rc;
  205. u8 val;
  206. if (!pdata) {
  207. pr_err("missing platform data\n");
  208. return -EINVAL;
  209. }
  210. pmic = kzalloc(sizeof(struct pm8821), GFP_KERNEL);
  211. if (!pmic) {
  212. pr_err("Cannot alloc pm8821 struct\n");
  213. return -ENOMEM;
  214. }
  215. /* Read PMIC chip revision */
  216. rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV, &val, sizeof(val));
  217. if (rc) {
  218. pr_err("Failed to read hw rev reg %d:rc=%d\n", REG_HWREV, rc);
  219. goto err_read_rev;
  220. }
  221. pr_info("PMIC revision 1: PM8821 rev %02X\n", val);
  222. pmic->rev_registers = val;
  223. /* Read PMIC chip revision 2 */
  224. rc = msm_ssbi_read(pdev->dev.parent, REG_HWREV_2, &val, sizeof(val));
  225. if (rc) {
  226. pr_err("Failed to read hw rev 2 reg %d:rc=%d\n",
  227. REG_HWREV_2, rc);
  228. goto err_read_rev;
  229. }
  230. pr_info("PMIC revision 2: PM8821 rev %02X\n", val);
  231. pmic->rev_registers |= val << BITS_PER_BYTE;
  232. pmic->dev = &pdev->dev;
  233. pm8821_drvdata.pm_chip_data = pmic;
  234. platform_set_drvdata(pdev, &pm8821_drvdata);
  235. /* Print out human readable version and revision names. */
  236. version = pm8xxx_get_version(pmic->dev);
  237. if (version == PM8XXX_VERSION_8821) {
  238. revision = pm8xxx_get_revision(pmic->dev);
  239. if (revision >= 0 && revision < ARRAY_SIZE(pm8821_rev_names))
  240. revision_name = pm8821_rev_names[revision];
  241. pr_info("PMIC version: PM8821 ver %s\n", revision_name);
  242. } else {
  243. WARN_ON(version != PM8XXX_VERSION_8821);
  244. }
  245. rc = pm8821_add_subdevices(pdata, pmic);
  246. if (rc) {
  247. pr_err("Cannot add subdevices rc=%d\n", rc);
  248. goto err;
  249. }
  250. return 0;
  251. err:
  252. mfd_remove_devices(pmic->dev);
  253. platform_set_drvdata(pdev, NULL);
  254. err_read_rev:
  255. kfree(pmic);
  256. return rc;
  257. }
  258. static int __devexit pm8821_remove(struct platform_device *pdev)
  259. {
  260. struct pm8xxx_drvdata *drvdata;
  261. struct pm8821 *pmic = NULL;
  262. drvdata = platform_get_drvdata(pdev);
  263. if (drvdata)
  264. pmic = drvdata->pm_chip_data;
  265. if (pmic) {
  266. if (pmic->dev)
  267. mfd_remove_devices(pmic->dev);
  268. if (pmic->irq_chip)
  269. pm8821_irq_exit(pmic->irq_chip);
  270. }
  271. platform_set_drvdata(pdev, NULL);
  272. kfree(pmic);
  273. return 0;
  274. }
  275. static struct platform_driver pm8821_driver = {
  276. .probe = pm8821_probe,
  277. .remove = __devexit_p(pm8821_remove),
  278. .driver = {
  279. .name = "pm8821-core",
  280. .owner = THIS_MODULE,
  281. },
  282. };
  283. static int __init pm8821_init(void)
  284. {
  285. return platform_driver_register(&pm8821_driver);
  286. }
  287. postcore_initcall(pm8821_init);
  288. static void __exit pm8821_exit(void)
  289. {
  290. platform_driver_unregister(&pm8821_driver);
  291. }
  292. module_exit(pm8821_exit);
  293. MODULE_LICENSE("GPL v2");
  294. MODULE_DESCRIPTION("PMIC 8821 core driver");
  295. MODULE_VERSION("1.0");
  296. MODULE_ALIAS("platform:pm8821-core");