pm8921-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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/interrupt.h>
  16. #include <linux/irqchip/chained_irq.h>
  17. #include <linux/irq.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/err.h>
  23. #include <linux/ssbi.h>
  24. #include <linux/regmap.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/mfd/core.h>
  27. #define SSBI_REG_ADDR_IRQ_BASE 0x1BB
  28. #define SSBI_REG_ADDR_IRQ_ROOT (SSBI_REG_ADDR_IRQ_BASE + 0)
  29. #define SSBI_REG_ADDR_IRQ_M_STATUS1 (SSBI_REG_ADDR_IRQ_BASE + 1)
  30. #define SSBI_REG_ADDR_IRQ_M_STATUS2 (SSBI_REG_ADDR_IRQ_BASE + 2)
  31. #define SSBI_REG_ADDR_IRQ_M_STATUS3 (SSBI_REG_ADDR_IRQ_BASE + 3)
  32. #define SSBI_REG_ADDR_IRQ_M_STATUS4 (SSBI_REG_ADDR_IRQ_BASE + 4)
  33. #define SSBI_REG_ADDR_IRQ_BLK_SEL (SSBI_REG_ADDR_IRQ_BASE + 5)
  34. #define SSBI_REG_ADDR_IRQ_IT_STATUS (SSBI_REG_ADDR_IRQ_BASE + 6)
  35. #define SSBI_REG_ADDR_IRQ_CONFIG (SSBI_REG_ADDR_IRQ_BASE + 7)
  36. #define SSBI_REG_ADDR_IRQ_RT_STATUS (SSBI_REG_ADDR_IRQ_BASE + 8)
  37. #define PM_IRQF_LVL_SEL 0x01 /* level select */
  38. #define PM_IRQF_MASK_FE 0x02 /* mask falling edge */
  39. #define PM_IRQF_MASK_RE 0x04 /* mask rising edge */
  40. #define PM_IRQF_CLR 0x08 /* clear interrupt */
  41. #define PM_IRQF_BITS_MASK 0x70
  42. #define PM_IRQF_BITS_SHIFT 4
  43. #define PM_IRQF_WRITE 0x80
  44. #define PM_IRQF_MASK_ALL (PM_IRQF_MASK_FE | \
  45. PM_IRQF_MASK_RE)
  46. #define REG_HWREV 0x002 /* PMIC4 revision */
  47. #define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */
  48. #define PM8921_NR_IRQS 256
  49. struct pm_irq_chip {
  50. struct regmap *regmap;
  51. spinlock_t pm_irq_lock;
  52. struct irq_domain *irqdomain;
  53. unsigned int num_irqs;
  54. unsigned int num_blocks;
  55. unsigned int num_masters;
  56. u8 config[0];
  57. };
  58. static int pm8xxx_read_block_irq(struct pm_irq_chip *chip, unsigned int bp,
  59. unsigned int *ip)
  60. {
  61. int rc;
  62. spin_lock(&chip->pm_irq_lock);
  63. rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, bp);
  64. if (rc) {
  65. pr_err("Failed Selecting Block %d rc=%d\n", bp, rc);
  66. goto bail;
  67. }
  68. rc = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_IT_STATUS, ip);
  69. if (rc)
  70. pr_err("Failed Reading Status rc=%d\n", rc);
  71. bail:
  72. spin_unlock(&chip->pm_irq_lock);
  73. return rc;
  74. }
  75. static int
  76. pm8xxx_config_irq(struct pm_irq_chip *chip, unsigned int bp, unsigned int cp)
  77. {
  78. int rc;
  79. spin_lock(&chip->pm_irq_lock);
  80. rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, bp);
  81. if (rc) {
  82. pr_err("Failed Selecting Block %d rc=%d\n", bp, rc);
  83. goto bail;
  84. }
  85. cp |= PM_IRQF_WRITE;
  86. rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_CONFIG, cp);
  87. if (rc)
  88. pr_err("Failed Configuring IRQ rc=%d\n", rc);
  89. bail:
  90. spin_unlock(&chip->pm_irq_lock);
  91. return rc;
  92. }
  93. static int pm8xxx_irq_block_handler(struct pm_irq_chip *chip, int block)
  94. {
  95. int pmirq, irq, i, ret = 0;
  96. unsigned int bits;
  97. ret = pm8xxx_read_block_irq(chip, block, &bits);
  98. if (ret) {
  99. pr_err("Failed reading %d block ret=%d", block, ret);
  100. return ret;
  101. }
  102. if (!bits) {
  103. pr_err("block bit set in master but no irqs: %d", block);
  104. return 0;
  105. }
  106. /* Check IRQ bits */
  107. for (i = 0; i < 8; i++) {
  108. if (bits & (1 << i)) {
  109. pmirq = block * 8 + i;
  110. irq = irq_find_mapping(chip->irqdomain, pmirq);
  111. generic_handle_irq(irq);
  112. }
  113. }
  114. return 0;
  115. }
  116. static int pm8xxx_irq_master_handler(struct pm_irq_chip *chip, int master)
  117. {
  118. unsigned int blockbits;
  119. int block_number, i, ret = 0;
  120. ret = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_M_STATUS1 + master,
  121. &blockbits);
  122. if (ret) {
  123. pr_err("Failed to read master %d ret=%d\n", master, ret);
  124. return ret;
  125. }
  126. if (!blockbits) {
  127. pr_err("master bit set in root but no blocks: %d", master);
  128. return 0;
  129. }
  130. for (i = 0; i < 8; i++)
  131. if (blockbits & (1 << i)) {
  132. block_number = master * 8 + i; /* block # */
  133. ret |= pm8xxx_irq_block_handler(chip, block_number);
  134. }
  135. return ret;
  136. }
  137. static void pm8xxx_irq_handler(struct irq_desc *desc)
  138. {
  139. struct pm_irq_chip *chip = irq_desc_get_handler_data(desc);
  140. struct irq_chip *irq_chip = irq_desc_get_chip(desc);
  141. unsigned int root;
  142. int i, ret, masters = 0;
  143. chained_irq_enter(irq_chip, desc);
  144. ret = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_ROOT, &root);
  145. if (ret) {
  146. pr_err("Can't read root status ret=%d\n", ret);
  147. return;
  148. }
  149. /* on pm8xxx series masters start from bit 1 of the root */
  150. masters = root >> 1;
  151. /* Read allowed masters for blocks. */
  152. for (i = 0; i < chip->num_masters; i++)
  153. if (masters & (1 << i))
  154. pm8xxx_irq_master_handler(chip, i);
  155. chained_irq_exit(irq_chip, desc);
  156. }
  157. static void pm8xxx_irq_mask_ack(struct irq_data *d)
  158. {
  159. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  160. unsigned int pmirq = irqd_to_hwirq(d);
  161. u8 block, config;
  162. block = pmirq / 8;
  163. config = chip->config[pmirq] | PM_IRQF_MASK_ALL | PM_IRQF_CLR;
  164. pm8xxx_config_irq(chip, block, config);
  165. }
  166. static void pm8xxx_irq_unmask(struct irq_data *d)
  167. {
  168. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  169. unsigned int pmirq = irqd_to_hwirq(d);
  170. u8 block, config;
  171. block = pmirq / 8;
  172. config = chip->config[pmirq];
  173. pm8xxx_config_irq(chip, block, config);
  174. }
  175. static int pm8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  176. {
  177. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  178. unsigned int pmirq = irqd_to_hwirq(d);
  179. int irq_bit;
  180. u8 block, config;
  181. block = pmirq / 8;
  182. irq_bit = pmirq % 8;
  183. chip->config[pmirq] = (irq_bit << PM_IRQF_BITS_SHIFT)
  184. | PM_IRQF_MASK_ALL;
  185. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  186. if (flow_type & IRQF_TRIGGER_RISING)
  187. chip->config[pmirq] &= ~PM_IRQF_MASK_RE;
  188. if (flow_type & IRQF_TRIGGER_FALLING)
  189. chip->config[pmirq] &= ~PM_IRQF_MASK_FE;
  190. } else {
  191. chip->config[pmirq] |= PM_IRQF_LVL_SEL;
  192. if (flow_type & IRQF_TRIGGER_HIGH)
  193. chip->config[pmirq] &= ~PM_IRQF_MASK_RE;
  194. else
  195. chip->config[pmirq] &= ~PM_IRQF_MASK_FE;
  196. }
  197. config = chip->config[pmirq] | PM_IRQF_CLR;
  198. return pm8xxx_config_irq(chip, block, config);
  199. }
  200. static int pm8xxx_irq_get_irqchip_state(struct irq_data *d,
  201. enum irqchip_irq_state which,
  202. bool *state)
  203. {
  204. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  205. unsigned int pmirq = irqd_to_hwirq(d);
  206. unsigned int bits;
  207. int irq_bit;
  208. u8 block;
  209. int rc;
  210. if (which != IRQCHIP_STATE_LINE_LEVEL)
  211. return -EINVAL;
  212. block = pmirq / 8;
  213. irq_bit = pmirq % 8;
  214. spin_lock(&chip->pm_irq_lock);
  215. rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, block);
  216. if (rc) {
  217. pr_err("Failed Selecting Block %d rc=%d\n", block, rc);
  218. goto bail;
  219. }
  220. rc = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_RT_STATUS, &bits);
  221. if (rc) {
  222. pr_err("Failed Reading Status rc=%d\n", rc);
  223. goto bail;
  224. }
  225. *state = !!(bits & BIT(irq_bit));
  226. bail:
  227. spin_unlock(&chip->pm_irq_lock);
  228. return rc;
  229. }
  230. static struct irq_chip pm8xxx_irq_chip = {
  231. .name = "pm8xxx",
  232. .irq_mask_ack = pm8xxx_irq_mask_ack,
  233. .irq_unmask = pm8xxx_irq_unmask,
  234. .irq_set_type = pm8xxx_irq_set_type,
  235. .irq_get_irqchip_state = pm8xxx_irq_get_irqchip_state,
  236. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE,
  237. };
  238. static int pm8xxx_irq_domain_map(struct irq_domain *d, unsigned int irq,
  239. irq_hw_number_t hwirq)
  240. {
  241. struct pm_irq_chip *chip = d->host_data;
  242. irq_set_chip_and_handler(irq, &pm8xxx_irq_chip, handle_level_irq);
  243. irq_set_chip_data(irq, chip);
  244. irq_set_noprobe(irq);
  245. return 0;
  246. }
  247. static const struct irq_domain_ops pm8xxx_irq_domain_ops = {
  248. .xlate = irq_domain_xlate_twocell,
  249. .map = pm8xxx_irq_domain_map,
  250. };
  251. static const struct regmap_config ssbi_regmap_config = {
  252. .reg_bits = 16,
  253. .val_bits = 8,
  254. .max_register = 0x3ff,
  255. .fast_io = true,
  256. .reg_read = ssbi_reg_read,
  257. .reg_write = ssbi_reg_write
  258. };
  259. static const struct of_device_id pm8921_id_table[] = {
  260. { .compatible = "qcom,pm8058", },
  261. { .compatible = "qcom,pm8921", },
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(of, pm8921_id_table);
  265. static int pm8921_probe(struct platform_device *pdev)
  266. {
  267. struct regmap *regmap;
  268. int irq, rc;
  269. unsigned int val;
  270. u32 rev;
  271. struct pm_irq_chip *chip;
  272. unsigned int nirqs = PM8921_NR_IRQS;
  273. irq = platform_get_irq(pdev, 0);
  274. if (irq < 0)
  275. return irq;
  276. regmap = devm_regmap_init(&pdev->dev, NULL, pdev->dev.parent,
  277. &ssbi_regmap_config);
  278. if (IS_ERR(regmap))
  279. return PTR_ERR(regmap);
  280. /* Read PMIC chip revision */
  281. rc = regmap_read(regmap, REG_HWREV, &val);
  282. if (rc) {
  283. pr_err("Failed to read hw rev reg %d:rc=%d\n", REG_HWREV, rc);
  284. return rc;
  285. }
  286. pr_info("PMIC revision 1: %02X\n", val);
  287. rev = val;
  288. /* Read PMIC chip revision 2 */
  289. rc = regmap_read(regmap, REG_HWREV_2, &val);
  290. if (rc) {
  291. pr_err("Failed to read hw rev 2 reg %d:rc=%d\n",
  292. REG_HWREV_2, rc);
  293. return rc;
  294. }
  295. pr_info("PMIC revision 2: %02X\n", val);
  296. rev |= val << BITS_PER_BYTE;
  297. chip = devm_kzalloc(&pdev->dev, sizeof(*chip) +
  298. sizeof(chip->config[0]) * nirqs,
  299. GFP_KERNEL);
  300. if (!chip)
  301. return -ENOMEM;
  302. platform_set_drvdata(pdev, chip);
  303. chip->regmap = regmap;
  304. chip->num_irqs = nirqs;
  305. chip->num_blocks = DIV_ROUND_UP(chip->num_irqs, 8);
  306. chip->num_masters = DIV_ROUND_UP(chip->num_blocks, 8);
  307. spin_lock_init(&chip->pm_irq_lock);
  308. chip->irqdomain = irq_domain_add_linear(pdev->dev.of_node, nirqs,
  309. &pm8xxx_irq_domain_ops,
  310. chip);
  311. if (!chip->irqdomain)
  312. return -ENODEV;
  313. irq_set_chained_handler_and_data(irq, pm8xxx_irq_handler, chip);
  314. irq_set_irq_wake(irq, 1);
  315. rc = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  316. if (rc) {
  317. irq_set_chained_handler_and_data(irq, NULL, NULL);
  318. irq_domain_remove(chip->irqdomain);
  319. }
  320. return rc;
  321. }
  322. static int pm8921_remove_child(struct device *dev, void *unused)
  323. {
  324. platform_device_unregister(to_platform_device(dev));
  325. return 0;
  326. }
  327. static int pm8921_remove(struct platform_device *pdev)
  328. {
  329. int irq = platform_get_irq(pdev, 0);
  330. struct pm_irq_chip *chip = platform_get_drvdata(pdev);
  331. device_for_each_child(&pdev->dev, NULL, pm8921_remove_child);
  332. irq_set_chained_handler_and_data(irq, NULL, NULL);
  333. irq_domain_remove(chip->irqdomain);
  334. return 0;
  335. }
  336. static struct platform_driver pm8921_driver = {
  337. .probe = pm8921_probe,
  338. .remove = pm8921_remove,
  339. .driver = {
  340. .name = "pm8921-core",
  341. .of_match_table = pm8921_id_table,
  342. },
  343. };
  344. static int __init pm8921_init(void)
  345. {
  346. return platform_driver_register(&pm8921_driver);
  347. }
  348. subsys_initcall(pm8921_init);
  349. static void __exit pm8921_exit(void)
  350. {
  351. platform_driver_unregister(&pm8921_driver);
  352. }
  353. module_exit(pm8921_exit);
  354. MODULE_LICENSE("GPL v2");
  355. MODULE_DESCRIPTION("PMIC 8921 core driver");
  356. MODULE_VERSION("1.0");
  357. MODULE_ALIAS("platform:pm8921-core");