spmi-pmic-arb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /* Copyright (c) 2012-2013, 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/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/irqchip/chained_irq.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/irq.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/spmi.h>
  25. /* PMIC Arbiter configuration registers */
  26. #define PMIC_ARB_VERSION 0x0000
  27. #define PMIC_ARB_INT_EN 0x0004
  28. /* PMIC Arbiter channel registers */
  29. #define PMIC_ARB_CMD(N) (0x0800 + (0x80 * (N)))
  30. #define PMIC_ARB_CONFIG(N) (0x0804 + (0x80 * (N)))
  31. #define PMIC_ARB_STATUS(N) (0x0808 + (0x80 * (N)))
  32. #define PMIC_ARB_WDATA0(N) (0x0810 + (0x80 * (N)))
  33. #define PMIC_ARB_WDATA1(N) (0x0814 + (0x80 * (N)))
  34. #define PMIC_ARB_RDATA0(N) (0x0818 + (0x80 * (N)))
  35. #define PMIC_ARB_RDATA1(N) (0x081C + (0x80 * (N)))
  36. /* Interrupt Controller */
  37. #define SPMI_PIC_OWNER_ACC_STATUS(M, N) (0x0000 + ((32 * (M)) + (4 * (N))))
  38. #define SPMI_PIC_ACC_ENABLE(N) (0x0200 + (4 * (N)))
  39. #define SPMI_PIC_IRQ_STATUS(N) (0x0600 + (4 * (N)))
  40. #define SPMI_PIC_IRQ_CLEAR(N) (0x0A00 + (4 * (N)))
  41. /* Mapping Table */
  42. #define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
  43. #define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
  44. #define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
  45. #define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
  46. #define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
  47. #define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
  48. #define SPMI_MAPPING_TABLE_LEN 255
  49. #define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
  50. /* Ownership Table */
  51. #define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
  52. #define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
  53. /* Channel Status fields */
  54. enum pmic_arb_chnl_status {
  55. PMIC_ARB_STATUS_DONE = (1 << 0),
  56. PMIC_ARB_STATUS_FAILURE = (1 << 1),
  57. PMIC_ARB_STATUS_DENIED = (1 << 2),
  58. PMIC_ARB_STATUS_DROPPED = (1 << 3),
  59. };
  60. /* Command register fields */
  61. #define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
  62. /* Command Opcodes */
  63. enum pmic_arb_cmd_op_code {
  64. PMIC_ARB_OP_EXT_WRITEL = 0,
  65. PMIC_ARB_OP_EXT_READL = 1,
  66. PMIC_ARB_OP_EXT_WRITE = 2,
  67. PMIC_ARB_OP_RESET = 3,
  68. PMIC_ARB_OP_SLEEP = 4,
  69. PMIC_ARB_OP_SHUTDOWN = 5,
  70. PMIC_ARB_OP_WAKEUP = 6,
  71. PMIC_ARB_OP_AUTHENTICATE = 7,
  72. PMIC_ARB_OP_MSTR_READ = 8,
  73. PMIC_ARB_OP_MSTR_WRITE = 9,
  74. PMIC_ARB_OP_EXT_READ = 13,
  75. PMIC_ARB_OP_WRITE = 14,
  76. PMIC_ARB_OP_READ = 15,
  77. PMIC_ARB_OP_ZERO_WRITE = 16,
  78. };
  79. /* Maximum number of support PMIC peripherals */
  80. #define PMIC_ARB_MAX_PERIPHS 256
  81. #define PMIC_ARB_PERIPH_ID_VALID (1 << 15)
  82. #define PMIC_ARB_TIMEOUT_US 100
  83. #define PMIC_ARB_MAX_TRANS_BYTES (8)
  84. #define PMIC_ARB_APID_MASK 0xFF
  85. #define PMIC_ARB_PPID_MASK 0xFFF
  86. /* interrupt enable bit */
  87. #define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
  88. /**
  89. * spmi_pmic_arb_dev - SPMI PMIC Arbiter object
  90. *
  91. * @base: address of the PMIC Arbiter core registers.
  92. * @intr: address of the SPMI interrupt control registers.
  93. * @cnfg: address of the PMIC Arbiter configuration registers.
  94. * @lock: lock to synchronize accesses.
  95. * @channel: which channel to use for accesses.
  96. * @irq: PMIC ARB interrupt.
  97. * @ee: the current Execution Environment
  98. * @min_apid: minimum APID (used for bounding IRQ search)
  99. * @max_apid: maximum APID
  100. * @mapping_table: in-memory copy of PPID -> APID mapping table.
  101. * @domain: irq domain object for PMIC IRQ domain
  102. * @spmic: SPMI controller object
  103. * @apid_to_ppid: cached mapping from APID to PPID
  104. */
  105. struct spmi_pmic_arb_dev {
  106. void __iomem *base;
  107. void __iomem *intr;
  108. void __iomem *cnfg;
  109. raw_spinlock_t lock;
  110. u8 channel;
  111. int irq;
  112. u8 ee;
  113. u8 min_apid;
  114. u8 max_apid;
  115. u32 mapping_table[SPMI_MAPPING_TABLE_LEN];
  116. struct irq_domain *domain;
  117. struct spmi_controller *spmic;
  118. u16 apid_to_ppid[256];
  119. };
  120. static inline u32 pmic_arb_base_read(struct spmi_pmic_arb_dev *dev, u32 offset)
  121. {
  122. return readl_relaxed(dev->base + offset);
  123. }
  124. static inline void pmic_arb_base_write(struct spmi_pmic_arb_dev *dev,
  125. u32 offset, u32 val)
  126. {
  127. writel_relaxed(val, dev->base + offset);
  128. }
  129. /**
  130. * pa_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
  131. * @bc: byte count -1. range: 0..3
  132. * @reg: register's address
  133. * @buf: output parameter, length must be bc + 1
  134. */
  135. static void pa_read_data(struct spmi_pmic_arb_dev *dev, u8 *buf, u32 reg, u8 bc)
  136. {
  137. u32 data = pmic_arb_base_read(dev, reg);
  138. memcpy(buf, &data, (bc & 3) + 1);
  139. }
  140. /**
  141. * pa_write_data: write 1..4 bytes from buf to pmic-arb's register
  142. * @bc: byte-count -1. range: 0..3.
  143. * @reg: register's address.
  144. * @buf: buffer to write. length must be bc + 1.
  145. */
  146. static void
  147. pa_write_data(struct spmi_pmic_arb_dev *dev, const u8 *buf, u32 reg, u8 bc)
  148. {
  149. u32 data = 0;
  150. memcpy(&data, buf, (bc & 3) + 1);
  151. pmic_arb_base_write(dev, reg, data);
  152. }
  153. static int pmic_arb_wait_for_done(struct spmi_controller *ctrl)
  154. {
  155. struct spmi_pmic_arb_dev *dev = spmi_controller_get_drvdata(ctrl);
  156. u32 status = 0;
  157. u32 timeout = PMIC_ARB_TIMEOUT_US;
  158. u32 offset = PMIC_ARB_STATUS(dev->channel);
  159. while (timeout--) {
  160. status = pmic_arb_base_read(dev, offset);
  161. if (status & PMIC_ARB_STATUS_DONE) {
  162. if (status & PMIC_ARB_STATUS_DENIED) {
  163. dev_err(&ctrl->dev,
  164. "%s: transaction denied (0x%x)\n",
  165. __func__, status);
  166. return -EPERM;
  167. }
  168. if (status & PMIC_ARB_STATUS_FAILURE) {
  169. dev_err(&ctrl->dev,
  170. "%s: transaction failed (0x%x)\n",
  171. __func__, status);
  172. return -EIO;
  173. }
  174. if (status & PMIC_ARB_STATUS_DROPPED) {
  175. dev_err(&ctrl->dev,
  176. "%s: transaction dropped (0x%x)\n",
  177. __func__, status);
  178. return -EIO;
  179. }
  180. return 0;
  181. }
  182. udelay(1);
  183. }
  184. dev_err(&ctrl->dev,
  185. "%s: timeout, status 0x%x\n",
  186. __func__, status);
  187. return -ETIMEDOUT;
  188. }
  189. /* Non-data command */
  190. static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
  191. {
  192. struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
  193. unsigned long flags;
  194. u32 cmd;
  195. int rc;
  196. /* Check for valid non-data command */
  197. if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
  198. return -EINVAL;
  199. cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
  200. raw_spin_lock_irqsave(&pmic_arb->lock, flags);
  201. pmic_arb_base_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
  202. rc = pmic_arb_wait_for_done(ctrl);
  203. raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
  204. return rc;
  205. }
  206. static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  207. u16 addr, u8 *buf, size_t len)
  208. {
  209. struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
  210. unsigned long flags;
  211. u8 bc = len - 1;
  212. u32 cmd;
  213. int rc;
  214. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  215. dev_err(&ctrl->dev,
  216. "pmic-arb supports 1..%d bytes per trans, but %d requested",
  217. PMIC_ARB_MAX_TRANS_BYTES, len);
  218. return -EINVAL;
  219. }
  220. /* Check the opcode */
  221. if (opc >= 0x60 && opc <= 0x7F)
  222. opc = PMIC_ARB_OP_READ;
  223. else if (opc >= 0x20 && opc <= 0x2F)
  224. opc = PMIC_ARB_OP_EXT_READ;
  225. else if (opc >= 0x38 && opc <= 0x3F)
  226. opc = PMIC_ARB_OP_EXT_READL;
  227. else
  228. return -EINVAL;
  229. cmd = (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
  230. raw_spin_lock_irqsave(&pmic_arb->lock, flags);
  231. pmic_arb_base_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
  232. rc = pmic_arb_wait_for_done(ctrl);
  233. if (rc)
  234. goto done;
  235. pa_read_data(pmic_arb, buf, PMIC_ARB_RDATA0(pmic_arb->channel),
  236. min_t(u8, bc, 3));
  237. if (bc > 3)
  238. pa_read_data(pmic_arb, buf + 4,
  239. PMIC_ARB_RDATA1(pmic_arb->channel), bc - 4);
  240. done:
  241. raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
  242. return rc;
  243. }
  244. static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  245. u16 addr, const u8 *buf, size_t len)
  246. {
  247. struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
  248. unsigned long flags;
  249. u8 bc = len - 1;
  250. u32 cmd;
  251. int rc;
  252. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  253. dev_err(&ctrl->dev,
  254. "pmic-arb supports 1..%d bytes per trans, but:%d requested",
  255. PMIC_ARB_MAX_TRANS_BYTES, len);
  256. return -EINVAL;
  257. }
  258. /* Check the opcode */
  259. if (opc >= 0x40 && opc <= 0x5F)
  260. opc = PMIC_ARB_OP_WRITE;
  261. else if (opc >= 0x00 && opc <= 0x0F)
  262. opc = PMIC_ARB_OP_EXT_WRITE;
  263. else if (opc >= 0x30 && opc <= 0x37)
  264. opc = PMIC_ARB_OP_EXT_WRITEL;
  265. else if (opc >= 0x80 && opc <= 0xFF)
  266. opc = PMIC_ARB_OP_ZERO_WRITE;
  267. else
  268. return -EINVAL;
  269. cmd = (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
  270. /* Write data to FIFOs */
  271. raw_spin_lock_irqsave(&pmic_arb->lock, flags);
  272. pa_write_data(pmic_arb, buf, PMIC_ARB_WDATA0(pmic_arb->channel)
  273. , min_t(u8, bc, 3));
  274. if (bc > 3)
  275. pa_write_data(pmic_arb, buf + 4,
  276. PMIC_ARB_WDATA1(pmic_arb->channel), bc - 4);
  277. /* Start the transaction */
  278. pmic_arb_base_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
  279. rc = pmic_arb_wait_for_done(ctrl);
  280. raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
  281. return rc;
  282. }
  283. enum qpnpint_regs {
  284. QPNPINT_REG_RT_STS = 0x10,
  285. QPNPINT_REG_SET_TYPE = 0x11,
  286. QPNPINT_REG_POLARITY_HIGH = 0x12,
  287. QPNPINT_REG_POLARITY_LOW = 0x13,
  288. QPNPINT_REG_LATCHED_CLR = 0x14,
  289. QPNPINT_REG_EN_SET = 0x15,
  290. QPNPINT_REG_EN_CLR = 0x16,
  291. QPNPINT_REG_LATCHED_STS = 0x18,
  292. };
  293. struct spmi_pmic_arb_qpnpint_type {
  294. u8 type; /* 1 -> edge */
  295. u8 polarity_high;
  296. u8 polarity_low;
  297. } __packed;
  298. /* Simplified accessor functions for irqchip callbacks */
  299. static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
  300. size_t len)
  301. {
  302. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  303. u8 sid = d->hwirq >> 24;
  304. u8 per = d->hwirq >> 16;
  305. if (pmic_arb_write_cmd(pa->spmic, SPMI_CMD_EXT_WRITEL, sid,
  306. (per << 8) + reg, buf, len))
  307. dev_err_ratelimited(&pa->spmic->dev,
  308. "failed irqchip transaction on %x\n",
  309. d->irq);
  310. }
  311. static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
  312. {
  313. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  314. u8 sid = d->hwirq >> 24;
  315. u8 per = d->hwirq >> 16;
  316. if (pmic_arb_read_cmd(pa->spmic, SPMI_CMD_EXT_READL, sid,
  317. (per << 8) + reg, buf, len))
  318. dev_err_ratelimited(&pa->spmic->dev,
  319. "failed irqchip transaction on %x\n",
  320. d->irq);
  321. }
  322. static void periph_interrupt(struct spmi_pmic_arb_dev *pa, u8 apid)
  323. {
  324. unsigned int irq;
  325. u32 status;
  326. int id;
  327. status = readl_relaxed(pa->intr + SPMI_PIC_IRQ_STATUS(apid));
  328. while (status) {
  329. id = ffs(status) - 1;
  330. status &= ~(1 << id);
  331. irq = irq_find_mapping(pa->domain,
  332. pa->apid_to_ppid[apid] << 16
  333. | id << 8
  334. | apid);
  335. generic_handle_irq(irq);
  336. }
  337. }
  338. static void pmic_arb_chained_irq(unsigned int irq, struct irq_desc *desc)
  339. {
  340. struct spmi_pmic_arb_dev *pa = irq_get_handler_data(irq);
  341. struct irq_chip *chip = irq_get_chip(irq);
  342. void __iomem *intr = pa->intr;
  343. int first = pa->min_apid >> 5;
  344. int last = pa->max_apid >> 5;
  345. u32 status;
  346. int i, id;
  347. chained_irq_enter(chip, desc);
  348. for (i = first; i <= last; ++i) {
  349. status = readl_relaxed(intr +
  350. SPMI_PIC_OWNER_ACC_STATUS(pa->ee, i));
  351. while (status) {
  352. id = ffs(status) - 1;
  353. status &= ~(1 << id);
  354. periph_interrupt(pa, id + i * 32);
  355. }
  356. }
  357. chained_irq_exit(chip, desc);
  358. }
  359. static void qpnpint_irq_ack(struct irq_data *d)
  360. {
  361. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  362. u8 irq = d->hwirq >> 8;
  363. u8 apid = d->hwirq;
  364. unsigned long flags;
  365. u8 data;
  366. raw_spin_lock_irqsave(&pa->lock, flags);
  367. writel_relaxed(1 << irq, pa->intr + SPMI_PIC_IRQ_CLEAR(apid));
  368. raw_spin_unlock_irqrestore(&pa->lock, flags);
  369. data = 1 << irq;
  370. qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
  371. }
  372. static void qpnpint_irq_mask(struct irq_data *d)
  373. {
  374. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  375. u8 irq = d->hwirq >> 8;
  376. u8 apid = d->hwirq;
  377. unsigned long flags;
  378. u32 status;
  379. u8 data;
  380. raw_spin_lock_irqsave(&pa->lock, flags);
  381. status = readl_relaxed(pa->intr + SPMI_PIC_ACC_ENABLE(apid));
  382. if (status & SPMI_PIC_ACC_ENABLE_BIT) {
  383. status = status & ~SPMI_PIC_ACC_ENABLE_BIT;
  384. writel_relaxed(status, pa->intr + SPMI_PIC_ACC_ENABLE(apid));
  385. }
  386. raw_spin_unlock_irqrestore(&pa->lock, flags);
  387. data = 1 << irq;
  388. qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
  389. }
  390. static void qpnpint_irq_unmask(struct irq_data *d)
  391. {
  392. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  393. u8 irq = d->hwirq >> 8;
  394. u8 apid = d->hwirq;
  395. unsigned long flags;
  396. u32 status;
  397. u8 data;
  398. raw_spin_lock_irqsave(&pa->lock, flags);
  399. status = readl_relaxed(pa->intr + SPMI_PIC_ACC_ENABLE(apid));
  400. if (!(status & SPMI_PIC_ACC_ENABLE_BIT)) {
  401. writel_relaxed(status | SPMI_PIC_ACC_ENABLE_BIT,
  402. pa->intr + SPMI_PIC_ACC_ENABLE(apid));
  403. }
  404. raw_spin_unlock_irqrestore(&pa->lock, flags);
  405. data = 1 << irq;
  406. qpnpint_spmi_write(d, QPNPINT_REG_EN_SET, &data, 1);
  407. }
  408. static void qpnpint_irq_enable(struct irq_data *d)
  409. {
  410. u8 irq = d->hwirq >> 8;
  411. u8 data;
  412. qpnpint_irq_unmask(d);
  413. data = 1 << irq;
  414. qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
  415. }
  416. static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
  417. {
  418. struct spmi_pmic_arb_qpnpint_type type;
  419. u8 irq = d->hwirq >> 8;
  420. qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
  421. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  422. type.type |= 1 << irq;
  423. if (flow_type & IRQF_TRIGGER_RISING)
  424. type.polarity_high |= 1 << irq;
  425. if (flow_type & IRQF_TRIGGER_FALLING)
  426. type.polarity_low |= 1 << irq;
  427. } else {
  428. if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
  429. (flow_type & (IRQF_TRIGGER_LOW)))
  430. return -EINVAL;
  431. type.type &= ~(1 << irq); /* level trig */
  432. if (flow_type & IRQF_TRIGGER_HIGH)
  433. type.polarity_high |= 1 << irq;
  434. else
  435. type.polarity_low |= 1 << irq;
  436. }
  437. qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
  438. return 0;
  439. }
  440. static struct irq_chip pmic_arb_irqchip = {
  441. .name = "pmic_arb",
  442. .irq_enable = qpnpint_irq_enable,
  443. .irq_ack = qpnpint_irq_ack,
  444. .irq_mask = qpnpint_irq_mask,
  445. .irq_unmask = qpnpint_irq_unmask,
  446. .irq_set_type = qpnpint_irq_set_type,
  447. .flags = IRQCHIP_MASK_ON_SUSPEND
  448. | IRQCHIP_SKIP_SET_WAKE,
  449. };
  450. struct spmi_pmic_arb_irq_spec {
  451. unsigned slave:4;
  452. unsigned per:8;
  453. unsigned irq:3;
  454. };
  455. static int search_mapping_table(struct spmi_pmic_arb_dev *pa,
  456. struct spmi_pmic_arb_irq_spec *spec,
  457. u8 *apid)
  458. {
  459. u16 ppid = spec->slave << 8 | spec->per;
  460. u32 *mapping_table = pa->mapping_table;
  461. int index = 0, i;
  462. u32 data;
  463. for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
  464. data = mapping_table[index];
  465. if (ppid & (1 << SPMI_MAPPING_BIT_INDEX(data))) {
  466. if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
  467. index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  468. } else {
  469. *apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  470. return 0;
  471. }
  472. } else {
  473. if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
  474. index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  475. } else {
  476. *apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  477. return 0;
  478. }
  479. }
  480. }
  481. return -ENODEV;
  482. }
  483. static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
  484. struct device_node *controller,
  485. const u32 *intspec,
  486. unsigned int intsize,
  487. unsigned long *out_hwirq,
  488. unsigned int *out_type)
  489. {
  490. struct spmi_pmic_arb_dev *pa = d->host_data;
  491. struct spmi_pmic_arb_irq_spec spec;
  492. int err;
  493. u8 apid;
  494. dev_dbg(&pa->spmic->dev,
  495. "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
  496. intspec[0], intspec[1], intspec[2]);
  497. if (d->of_node != controller)
  498. return -EINVAL;
  499. if (intsize != 4)
  500. return -EINVAL;
  501. if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
  502. return -EINVAL;
  503. spec.slave = intspec[0];
  504. spec.per = intspec[1];
  505. spec.irq = intspec[2];
  506. err = search_mapping_table(pa, &spec, &apid);
  507. if (err)
  508. return err;
  509. pa->apid_to_ppid[apid] = spec.slave << 8 | spec.per;
  510. /* Keep track of {max,min}_apid for bounding search during interrupt */
  511. if (apid > pa->max_apid)
  512. pa->max_apid = apid;
  513. if (apid < pa->min_apid)
  514. pa->min_apid = apid;
  515. *out_hwirq = spec.slave << 24
  516. | spec.per << 16
  517. | spec.irq << 8
  518. | apid;
  519. *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
  520. dev_dbg(&pa->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
  521. return 0;
  522. }
  523. static int qpnpint_irq_domain_map(struct irq_domain *d,
  524. unsigned int virq,
  525. irq_hw_number_t hwirq)
  526. {
  527. struct spmi_pmic_arb_dev *pa = d->host_data;
  528. dev_dbg(&pa->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
  529. irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
  530. irq_set_chip_data(virq, d->host_data);
  531. irq_set_noprobe(virq);
  532. return 0;
  533. }
  534. static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
  535. .map = qpnpint_irq_domain_map,
  536. .xlate = qpnpint_irq_domain_dt_translate,
  537. };
  538. static int spmi_pmic_arb_probe(struct platform_device *pdev)
  539. {
  540. struct spmi_pmic_arb_dev *pa;
  541. struct spmi_controller *ctrl;
  542. struct resource *res;
  543. u32 channel, ee;
  544. int err, i;
  545. ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
  546. if (!ctrl)
  547. return -ENOMEM;
  548. pa = spmi_controller_get_drvdata(ctrl);
  549. pa->spmic = ctrl;
  550. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
  551. pa->base = devm_ioremap_resource(&ctrl->dev, res);
  552. if (IS_ERR(pa->base)) {
  553. err = PTR_ERR(pa->base);
  554. goto err_put_ctrl;
  555. }
  556. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
  557. pa->intr = devm_ioremap_resource(&ctrl->dev, res);
  558. if (IS_ERR(pa->intr)) {
  559. err = PTR_ERR(pa->intr);
  560. goto err_put_ctrl;
  561. }
  562. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
  563. pa->cnfg = devm_ioremap_resource(&ctrl->dev, res);
  564. if (IS_ERR(pa->cnfg)) {
  565. err = PTR_ERR(pa->cnfg);
  566. goto err_put_ctrl;
  567. }
  568. pa->irq = platform_get_irq_byname(pdev, "periph_irq");
  569. if (pa->irq < 0) {
  570. err = pa->irq;
  571. goto err_put_ctrl;
  572. }
  573. err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
  574. if (err) {
  575. dev_err(&pdev->dev, "channel unspecified.\n");
  576. goto err_put_ctrl;
  577. }
  578. if (channel > 5) {
  579. dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
  580. channel);
  581. goto err_put_ctrl;
  582. }
  583. pa->channel = channel;
  584. err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
  585. if (err) {
  586. dev_err(&pdev->dev, "EE unspecified.\n");
  587. goto err_put_ctrl;
  588. }
  589. if (ee > 5) {
  590. dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
  591. err = -EINVAL;
  592. goto err_put_ctrl;
  593. }
  594. pa->ee = ee;
  595. for (i = 0; i < ARRAY_SIZE(pa->mapping_table); ++i)
  596. pa->mapping_table[i] = readl_relaxed(
  597. pa->cnfg + SPMI_MAPPING_TABLE_REG(i));
  598. /* Initialize max_apid/min_apid to the opposite bounds, during
  599. * the irq domain translation, we are sure to update these */
  600. pa->max_apid = 0;
  601. pa->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
  602. platform_set_drvdata(pdev, ctrl);
  603. raw_spin_lock_init(&pa->lock);
  604. ctrl->cmd = pmic_arb_cmd;
  605. ctrl->read_cmd = pmic_arb_read_cmd;
  606. ctrl->write_cmd = pmic_arb_write_cmd;
  607. dev_dbg(&pdev->dev, "adding irq domain\n");
  608. pa->domain = irq_domain_add_tree(pdev->dev.of_node,
  609. &pmic_arb_irq_domain_ops, pa);
  610. if (!pa->domain) {
  611. dev_err(&pdev->dev, "unable to create irq_domain\n");
  612. err = -ENOMEM;
  613. goto err_put_ctrl;
  614. }
  615. irq_set_handler_data(pa->irq, pa);
  616. irq_set_chained_handler(pa->irq, pmic_arb_chained_irq);
  617. err = spmi_controller_add(ctrl);
  618. if (err)
  619. goto err_domain_remove;
  620. dev_dbg(&ctrl->dev, "PMIC Arb Version 0x%x\n",
  621. pmic_arb_base_read(pa, PMIC_ARB_VERSION));
  622. return 0;
  623. err_domain_remove:
  624. irq_set_chained_handler(pa->irq, NULL);
  625. irq_set_handler_data(pa->irq, NULL);
  626. irq_domain_remove(pa->domain);
  627. err_put_ctrl:
  628. spmi_controller_put(ctrl);
  629. return err;
  630. }
  631. static int spmi_pmic_arb_remove(struct platform_device *pdev)
  632. {
  633. struct spmi_controller *ctrl = platform_get_drvdata(pdev);
  634. struct spmi_pmic_arb_dev *pa = spmi_controller_get_drvdata(ctrl);
  635. spmi_controller_remove(ctrl);
  636. irq_set_chained_handler(pa->irq, NULL);
  637. irq_set_handler_data(pa->irq, NULL);
  638. irq_domain_remove(pa->domain);
  639. spmi_controller_put(ctrl);
  640. return 0;
  641. }
  642. static const struct of_device_id spmi_pmic_arb_match_table[] = {
  643. { .compatible = "qcom,spmi-pmic-arb", },
  644. {},
  645. };
  646. MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
  647. static struct platform_driver spmi_pmic_arb_driver = {
  648. .probe = spmi_pmic_arb_probe,
  649. .remove = spmi_pmic_arb_remove,
  650. .driver = {
  651. .name = "spmi_pmic_arb",
  652. .of_match_table = spmi_pmic_arb_match_table,
  653. },
  654. };
  655. module_platform_driver(spmi_pmic_arb_driver);
  656. MODULE_LICENSE("GPL v2");
  657. MODULE_ALIAS("platform:spmi_pmic_arb");