qcom-pm8xxx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 PM8821_SSBI_REG_ADDR_IRQ_BASE 0x100
  38. #define PM8821_SSBI_REG_ADDR_IRQ_MASTER0 (PM8821_SSBI_REG_ADDR_IRQ_BASE + 0x30)
  39. #define PM8821_SSBI_REG_ADDR_IRQ_MASTER1 (PM8821_SSBI_REG_ADDR_IRQ_BASE + 0xb0)
  40. #define PM8821_SSBI_REG(m, b, offset) \
  41. ((m == 0) ? \
  42. (PM8821_SSBI_REG_ADDR_IRQ_MASTER0 + b + offset) : \
  43. (PM8821_SSBI_REG_ADDR_IRQ_MASTER1 + b + offset))
  44. #define PM8821_SSBI_ADDR_IRQ_ROOT(m, b) PM8821_SSBI_REG(m, b, 0x0)
  45. #define PM8821_SSBI_ADDR_IRQ_CLEAR(m, b) PM8821_SSBI_REG(m, b, 0x01)
  46. #define PM8821_SSBI_ADDR_IRQ_MASK(m, b) PM8821_SSBI_REG(m, b, 0x08)
  47. #define PM8821_SSBI_ADDR_IRQ_RT_STATUS(m, b) PM8821_SSBI_REG(m, b, 0x0f)
  48. #define PM8821_BLOCKS_PER_MASTER 7
  49. #define PM_IRQF_LVL_SEL 0x01 /* level select */
  50. #define PM_IRQF_MASK_FE 0x02 /* mask falling edge */
  51. #define PM_IRQF_MASK_RE 0x04 /* mask rising edge */
  52. #define PM_IRQF_CLR 0x08 /* clear interrupt */
  53. #define PM_IRQF_BITS_MASK 0x70
  54. #define PM_IRQF_BITS_SHIFT 4
  55. #define PM_IRQF_WRITE 0x80
  56. #define PM_IRQF_MASK_ALL (PM_IRQF_MASK_FE | \
  57. PM_IRQF_MASK_RE)
  58. #define REG_HWREV 0x002 /* PMIC4 revision */
  59. #define REG_HWREV_2 0x0E8 /* PMIC4 revision 2 */
  60. #define PM8XXX_NR_IRQS 256
  61. #define PM8821_NR_IRQS 112
  62. struct pm_irq_chip {
  63. struct regmap *regmap;
  64. spinlock_t pm_irq_lock;
  65. struct irq_domain *irqdomain;
  66. unsigned int num_irqs;
  67. unsigned int num_blocks;
  68. unsigned int num_masters;
  69. u8 config[0];
  70. };
  71. struct pm_irq_data {
  72. int num_irqs;
  73. const struct irq_domain_ops *irq_domain_ops;
  74. void (*irq_handler)(struct irq_desc *desc);
  75. };
  76. static int pm8xxx_read_block_irq(struct pm_irq_chip *chip, unsigned int bp,
  77. unsigned int *ip)
  78. {
  79. int rc;
  80. spin_lock(&chip->pm_irq_lock);
  81. rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, bp);
  82. if (rc) {
  83. pr_err("Failed Selecting Block %d rc=%d\n", bp, rc);
  84. goto bail;
  85. }
  86. rc = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_IT_STATUS, ip);
  87. if (rc)
  88. pr_err("Failed Reading Status rc=%d\n", rc);
  89. bail:
  90. spin_unlock(&chip->pm_irq_lock);
  91. return rc;
  92. }
  93. static int
  94. pm8xxx_config_irq(struct pm_irq_chip *chip, unsigned int bp, unsigned int cp)
  95. {
  96. int rc;
  97. spin_lock(&chip->pm_irq_lock);
  98. rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, bp);
  99. if (rc) {
  100. pr_err("Failed Selecting Block %d rc=%d\n", bp, rc);
  101. goto bail;
  102. }
  103. cp |= PM_IRQF_WRITE;
  104. rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_CONFIG, cp);
  105. if (rc)
  106. pr_err("Failed Configuring IRQ rc=%d\n", rc);
  107. bail:
  108. spin_unlock(&chip->pm_irq_lock);
  109. return rc;
  110. }
  111. static int pm8xxx_irq_block_handler(struct pm_irq_chip *chip, int block)
  112. {
  113. int pmirq, irq, i, ret = 0;
  114. unsigned int bits;
  115. ret = pm8xxx_read_block_irq(chip, block, &bits);
  116. if (ret) {
  117. pr_err("Failed reading %d block ret=%d", block, ret);
  118. return ret;
  119. }
  120. if (!bits) {
  121. pr_err("block bit set in master but no irqs: %d", block);
  122. return 0;
  123. }
  124. /* Check IRQ bits */
  125. for (i = 0; i < 8; i++) {
  126. if (bits & (1 << i)) {
  127. pmirq = block * 8 + i;
  128. irq = irq_find_mapping(chip->irqdomain, pmirq);
  129. generic_handle_irq(irq);
  130. }
  131. }
  132. return 0;
  133. }
  134. static int pm8xxx_irq_master_handler(struct pm_irq_chip *chip, int master)
  135. {
  136. unsigned int blockbits;
  137. int block_number, i, ret = 0;
  138. ret = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_M_STATUS1 + master,
  139. &blockbits);
  140. if (ret) {
  141. pr_err("Failed to read master %d ret=%d\n", master, ret);
  142. return ret;
  143. }
  144. if (!blockbits) {
  145. pr_err("master bit set in root but no blocks: %d", master);
  146. return 0;
  147. }
  148. for (i = 0; i < 8; i++)
  149. if (blockbits & (1 << i)) {
  150. block_number = master * 8 + i; /* block # */
  151. ret |= pm8xxx_irq_block_handler(chip, block_number);
  152. }
  153. return ret;
  154. }
  155. static void pm8xxx_irq_handler(struct irq_desc *desc)
  156. {
  157. struct pm_irq_chip *chip = irq_desc_get_handler_data(desc);
  158. struct irq_chip *irq_chip = irq_desc_get_chip(desc);
  159. unsigned int root;
  160. int i, ret, masters = 0;
  161. chained_irq_enter(irq_chip, desc);
  162. ret = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_ROOT, &root);
  163. if (ret) {
  164. pr_err("Can't read root status ret=%d\n", ret);
  165. return;
  166. }
  167. /* on pm8xxx series masters start from bit 1 of the root */
  168. masters = root >> 1;
  169. /* Read allowed masters for blocks. */
  170. for (i = 0; i < chip->num_masters; i++)
  171. if (masters & (1 << i))
  172. pm8xxx_irq_master_handler(chip, i);
  173. chained_irq_exit(irq_chip, desc);
  174. }
  175. static void pm8821_irq_block_handler(struct pm_irq_chip *chip,
  176. int master, int block)
  177. {
  178. int pmirq, irq, i, ret;
  179. unsigned int bits;
  180. ret = regmap_read(chip->regmap,
  181. PM8821_SSBI_ADDR_IRQ_ROOT(master, block), &bits);
  182. if (ret) {
  183. pr_err("Reading block %d failed ret=%d", block, ret);
  184. return;
  185. }
  186. /* Convert block offset to global block number */
  187. block += (master * PM8821_BLOCKS_PER_MASTER) - 1;
  188. /* Check IRQ bits */
  189. for (i = 0; i < 8; i++) {
  190. if (bits & BIT(i)) {
  191. pmirq = block * 8 + i;
  192. irq = irq_find_mapping(chip->irqdomain, pmirq);
  193. generic_handle_irq(irq);
  194. }
  195. }
  196. }
  197. static inline void pm8821_irq_master_handler(struct pm_irq_chip *chip,
  198. int master, u8 master_val)
  199. {
  200. int block;
  201. for (block = 1; block < 8; block++)
  202. if (master_val & BIT(block))
  203. pm8821_irq_block_handler(chip, master, block);
  204. }
  205. static void pm8821_irq_handler(struct irq_desc *desc)
  206. {
  207. struct pm_irq_chip *chip = irq_desc_get_handler_data(desc);
  208. struct irq_chip *irq_chip = irq_desc_get_chip(desc);
  209. unsigned int master;
  210. int ret;
  211. chained_irq_enter(irq_chip, desc);
  212. ret = regmap_read(chip->regmap,
  213. PM8821_SSBI_REG_ADDR_IRQ_MASTER0, &master);
  214. if (ret) {
  215. pr_err("Failed to read master 0 ret=%d\n", ret);
  216. goto done;
  217. }
  218. /* bits 1 through 7 marks the first 7 blocks in master 0 */
  219. if (master & GENMASK(7, 1))
  220. pm8821_irq_master_handler(chip, 0, master);
  221. /* bit 0 marks if master 1 contains any bits */
  222. if (!(master & BIT(0)))
  223. goto done;
  224. ret = regmap_read(chip->regmap,
  225. PM8821_SSBI_REG_ADDR_IRQ_MASTER1, &master);
  226. if (ret) {
  227. pr_err("Failed to read master 1 ret=%d\n", ret);
  228. goto done;
  229. }
  230. pm8821_irq_master_handler(chip, 1, master);
  231. done:
  232. chained_irq_exit(irq_chip, desc);
  233. }
  234. static void pm8xxx_irq_mask_ack(struct irq_data *d)
  235. {
  236. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  237. unsigned int pmirq = irqd_to_hwirq(d);
  238. u8 block, config;
  239. block = pmirq / 8;
  240. config = chip->config[pmirq] | PM_IRQF_MASK_ALL | PM_IRQF_CLR;
  241. pm8xxx_config_irq(chip, block, config);
  242. }
  243. static void pm8xxx_irq_unmask(struct irq_data *d)
  244. {
  245. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  246. unsigned int pmirq = irqd_to_hwirq(d);
  247. u8 block, config;
  248. block = pmirq / 8;
  249. config = chip->config[pmirq];
  250. pm8xxx_config_irq(chip, block, config);
  251. }
  252. static int pm8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  253. {
  254. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  255. unsigned int pmirq = irqd_to_hwirq(d);
  256. int irq_bit;
  257. u8 block, config;
  258. block = pmirq / 8;
  259. irq_bit = pmirq % 8;
  260. chip->config[pmirq] = (irq_bit << PM_IRQF_BITS_SHIFT)
  261. | PM_IRQF_MASK_ALL;
  262. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  263. if (flow_type & IRQF_TRIGGER_RISING)
  264. chip->config[pmirq] &= ~PM_IRQF_MASK_RE;
  265. if (flow_type & IRQF_TRIGGER_FALLING)
  266. chip->config[pmirq] &= ~PM_IRQF_MASK_FE;
  267. } else {
  268. chip->config[pmirq] |= PM_IRQF_LVL_SEL;
  269. if (flow_type & IRQF_TRIGGER_HIGH)
  270. chip->config[pmirq] &= ~PM_IRQF_MASK_RE;
  271. else
  272. chip->config[pmirq] &= ~PM_IRQF_MASK_FE;
  273. }
  274. config = chip->config[pmirq] | PM_IRQF_CLR;
  275. return pm8xxx_config_irq(chip, block, config);
  276. }
  277. static int pm8xxx_irq_get_irqchip_state(struct irq_data *d,
  278. enum irqchip_irq_state which,
  279. bool *state)
  280. {
  281. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  282. unsigned int pmirq = irqd_to_hwirq(d);
  283. unsigned int bits;
  284. int irq_bit;
  285. u8 block;
  286. int rc;
  287. if (which != IRQCHIP_STATE_LINE_LEVEL)
  288. return -EINVAL;
  289. block = pmirq / 8;
  290. irq_bit = pmirq % 8;
  291. spin_lock(&chip->pm_irq_lock);
  292. rc = regmap_write(chip->regmap, SSBI_REG_ADDR_IRQ_BLK_SEL, block);
  293. if (rc) {
  294. pr_err("Failed Selecting Block %d rc=%d\n", block, rc);
  295. goto bail;
  296. }
  297. rc = regmap_read(chip->regmap, SSBI_REG_ADDR_IRQ_RT_STATUS, &bits);
  298. if (rc) {
  299. pr_err("Failed Reading Status rc=%d\n", rc);
  300. goto bail;
  301. }
  302. *state = !!(bits & BIT(irq_bit));
  303. bail:
  304. spin_unlock(&chip->pm_irq_lock);
  305. return rc;
  306. }
  307. static struct irq_chip pm8xxx_irq_chip = {
  308. .name = "pm8xxx",
  309. .irq_mask_ack = pm8xxx_irq_mask_ack,
  310. .irq_unmask = pm8xxx_irq_unmask,
  311. .irq_set_type = pm8xxx_irq_set_type,
  312. .irq_get_irqchip_state = pm8xxx_irq_get_irqchip_state,
  313. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE,
  314. };
  315. static int pm8xxx_irq_domain_map(struct irq_domain *d, unsigned int irq,
  316. irq_hw_number_t hwirq)
  317. {
  318. struct pm_irq_chip *chip = d->host_data;
  319. irq_set_chip_and_handler(irq, &pm8xxx_irq_chip, handle_level_irq);
  320. irq_set_chip_data(irq, chip);
  321. irq_set_noprobe(irq);
  322. return 0;
  323. }
  324. static const struct irq_domain_ops pm8xxx_irq_domain_ops = {
  325. .xlate = irq_domain_xlate_twocell,
  326. .map = pm8xxx_irq_domain_map,
  327. };
  328. static void pm8821_irq_mask_ack(struct irq_data *d)
  329. {
  330. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  331. unsigned int pmirq = irqd_to_hwirq(d);
  332. u8 block, master;
  333. int irq_bit, rc;
  334. block = pmirq / 8;
  335. master = block / PM8821_BLOCKS_PER_MASTER;
  336. irq_bit = pmirq % 8;
  337. block %= PM8821_BLOCKS_PER_MASTER;
  338. rc = regmap_update_bits(chip->regmap,
  339. PM8821_SSBI_ADDR_IRQ_MASK(master, block),
  340. BIT(irq_bit), BIT(irq_bit));
  341. if (rc) {
  342. pr_err("Failed to mask IRQ:%d rc=%d\n", pmirq, rc);
  343. return;
  344. }
  345. rc = regmap_update_bits(chip->regmap,
  346. PM8821_SSBI_ADDR_IRQ_CLEAR(master, block),
  347. BIT(irq_bit), BIT(irq_bit));
  348. if (rc)
  349. pr_err("Failed to CLEAR IRQ:%d rc=%d\n", pmirq, rc);
  350. }
  351. static void pm8821_irq_unmask(struct irq_data *d)
  352. {
  353. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  354. unsigned int pmirq = irqd_to_hwirq(d);
  355. int irq_bit, rc;
  356. u8 block, master;
  357. block = pmirq / 8;
  358. master = block / PM8821_BLOCKS_PER_MASTER;
  359. irq_bit = pmirq % 8;
  360. block %= PM8821_BLOCKS_PER_MASTER;
  361. rc = regmap_update_bits(chip->regmap,
  362. PM8821_SSBI_ADDR_IRQ_MASK(master, block),
  363. BIT(irq_bit), ~BIT(irq_bit));
  364. if (rc)
  365. pr_err("Failed to read/write unmask IRQ:%d rc=%d\n", pmirq, rc);
  366. }
  367. static int pm8821_irq_get_irqchip_state(struct irq_data *d,
  368. enum irqchip_irq_state which,
  369. bool *state)
  370. {
  371. struct pm_irq_chip *chip = irq_data_get_irq_chip_data(d);
  372. int rc, pmirq = irqd_to_hwirq(d);
  373. u8 block, irq_bit, master;
  374. unsigned int bits;
  375. block = pmirq / 8;
  376. master = block / PM8821_BLOCKS_PER_MASTER;
  377. irq_bit = pmirq % 8;
  378. block %= PM8821_BLOCKS_PER_MASTER;
  379. rc = regmap_read(chip->regmap,
  380. PM8821_SSBI_ADDR_IRQ_RT_STATUS(master, block), &bits);
  381. if (rc) {
  382. pr_err("Reading Status of IRQ %d failed rc=%d\n", pmirq, rc);
  383. return rc;
  384. }
  385. *state = !!(bits & BIT(irq_bit));
  386. return rc;
  387. }
  388. static struct irq_chip pm8821_irq_chip = {
  389. .name = "pm8821",
  390. .irq_mask_ack = pm8821_irq_mask_ack,
  391. .irq_unmask = pm8821_irq_unmask,
  392. .irq_get_irqchip_state = pm8821_irq_get_irqchip_state,
  393. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_SKIP_SET_WAKE,
  394. };
  395. static int pm8821_irq_domain_map(struct irq_domain *d, unsigned int irq,
  396. irq_hw_number_t hwirq)
  397. {
  398. struct pm_irq_chip *chip = d->host_data;
  399. irq_set_chip_and_handler(irq, &pm8821_irq_chip, handle_level_irq);
  400. irq_set_chip_data(irq, chip);
  401. irq_set_noprobe(irq);
  402. return 0;
  403. }
  404. static const struct irq_domain_ops pm8821_irq_domain_ops = {
  405. .xlate = irq_domain_xlate_twocell,
  406. .map = pm8821_irq_domain_map,
  407. };
  408. static const struct regmap_config ssbi_regmap_config = {
  409. .reg_bits = 16,
  410. .val_bits = 8,
  411. .max_register = 0x3ff,
  412. .fast_io = true,
  413. .reg_read = ssbi_reg_read,
  414. .reg_write = ssbi_reg_write
  415. };
  416. static const struct pm_irq_data pm8xxx_data = {
  417. .num_irqs = PM8XXX_NR_IRQS,
  418. .irq_domain_ops = &pm8xxx_irq_domain_ops,
  419. .irq_handler = pm8xxx_irq_handler,
  420. };
  421. static const struct pm_irq_data pm8821_data = {
  422. .num_irqs = PM8821_NR_IRQS,
  423. .irq_domain_ops = &pm8821_irq_domain_ops,
  424. .irq_handler = pm8821_irq_handler,
  425. };
  426. static const struct of_device_id pm8xxx_id_table[] = {
  427. { .compatible = "qcom,pm8018", .data = &pm8xxx_data},
  428. { .compatible = "qcom,pm8058", .data = &pm8xxx_data},
  429. { .compatible = "qcom,pm8821", .data = &pm8821_data},
  430. { .compatible = "qcom,pm8921", .data = &pm8xxx_data},
  431. { }
  432. };
  433. MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
  434. static int pm8xxx_probe(struct platform_device *pdev)
  435. {
  436. const struct pm_irq_data *data;
  437. struct regmap *regmap;
  438. int irq, rc;
  439. unsigned int val;
  440. u32 rev;
  441. struct pm_irq_chip *chip;
  442. data = of_device_get_match_data(&pdev->dev);
  443. if (!data) {
  444. dev_err(&pdev->dev, "No matching driver data found\n");
  445. return -EINVAL;
  446. }
  447. irq = platform_get_irq(pdev, 0);
  448. if (irq < 0)
  449. return irq;
  450. regmap = devm_regmap_init(&pdev->dev, NULL, pdev->dev.parent,
  451. &ssbi_regmap_config);
  452. if (IS_ERR(regmap))
  453. return PTR_ERR(regmap);
  454. /* Read PMIC chip revision */
  455. rc = regmap_read(regmap, REG_HWREV, &val);
  456. if (rc) {
  457. pr_err("Failed to read hw rev reg %d:rc=%d\n", REG_HWREV, rc);
  458. return rc;
  459. }
  460. pr_info("PMIC revision 1: %02X\n", val);
  461. rev = val;
  462. /* Read PMIC chip revision 2 */
  463. rc = regmap_read(regmap, REG_HWREV_2, &val);
  464. if (rc) {
  465. pr_err("Failed to read hw rev 2 reg %d:rc=%d\n",
  466. REG_HWREV_2, rc);
  467. return rc;
  468. }
  469. pr_info("PMIC revision 2: %02X\n", val);
  470. rev |= val << BITS_PER_BYTE;
  471. chip = devm_kzalloc(&pdev->dev,
  472. struct_size(chip, config, data->num_irqs),
  473. GFP_KERNEL);
  474. if (!chip)
  475. return -ENOMEM;
  476. platform_set_drvdata(pdev, chip);
  477. chip->regmap = regmap;
  478. chip->num_irqs = data->num_irqs;
  479. chip->num_blocks = DIV_ROUND_UP(chip->num_irqs, 8);
  480. chip->num_masters = DIV_ROUND_UP(chip->num_blocks, 8);
  481. spin_lock_init(&chip->pm_irq_lock);
  482. chip->irqdomain = irq_domain_add_linear(pdev->dev.of_node,
  483. data->num_irqs,
  484. data->irq_domain_ops,
  485. chip);
  486. if (!chip->irqdomain)
  487. return -ENODEV;
  488. irq_set_chained_handler_and_data(irq, data->irq_handler, chip);
  489. irq_set_irq_wake(irq, 1);
  490. rc = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  491. if (rc) {
  492. irq_set_chained_handler_and_data(irq, NULL, NULL);
  493. irq_domain_remove(chip->irqdomain);
  494. }
  495. return rc;
  496. }
  497. static int pm8xxx_remove_child(struct device *dev, void *unused)
  498. {
  499. platform_device_unregister(to_platform_device(dev));
  500. return 0;
  501. }
  502. static int pm8xxx_remove(struct platform_device *pdev)
  503. {
  504. int irq = platform_get_irq(pdev, 0);
  505. struct pm_irq_chip *chip = platform_get_drvdata(pdev);
  506. device_for_each_child(&pdev->dev, NULL, pm8xxx_remove_child);
  507. irq_set_chained_handler_and_data(irq, NULL, NULL);
  508. irq_domain_remove(chip->irqdomain);
  509. return 0;
  510. }
  511. static struct platform_driver pm8xxx_driver = {
  512. .probe = pm8xxx_probe,
  513. .remove = pm8xxx_remove,
  514. .driver = {
  515. .name = "pm8xxx-core",
  516. .of_match_table = pm8xxx_id_table,
  517. },
  518. };
  519. static int __init pm8xxx_init(void)
  520. {
  521. return platform_driver_register(&pm8xxx_driver);
  522. }
  523. subsys_initcall(pm8xxx_init);
  524. static void __exit pm8xxx_exit(void)
  525. {
  526. platform_driver_unregister(&pm8xxx_driver);
  527. }
  528. module_exit(pm8xxx_exit);
  529. MODULE_LICENSE("GPL v2");
  530. MODULE_DESCRIPTION("PMIC 8xxx core driver");
  531. MODULE_VERSION("1.0");
  532. MODULE_ALIAS("platform:pm8xxx-core");