spmi-pmic-arb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /*
  2. * Copyright (c) 2012-2015, 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. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/irqchip/chained_irq.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/irq.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/spmi.h>
  26. /* PMIC Arbiter configuration registers */
  27. #define PMIC_ARB_VERSION 0x0000
  28. #define PMIC_ARB_VERSION_V2_MIN 0x20010000
  29. #define PMIC_ARB_INT_EN 0x0004
  30. /* PMIC Arbiter channel registers offsets */
  31. #define PMIC_ARB_CMD 0x00
  32. #define PMIC_ARB_CONFIG 0x04
  33. #define PMIC_ARB_STATUS 0x08
  34. #define PMIC_ARB_WDATA0 0x10
  35. #define PMIC_ARB_WDATA1 0x14
  36. #define PMIC_ARB_RDATA0 0x18
  37. #define PMIC_ARB_RDATA1 0x1C
  38. #define PMIC_ARB_REG_CHNL(N) (0x800 + 0x4 * (N))
  39. /* Mapping Table */
  40. #define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
  41. #define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
  42. #define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
  43. #define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
  44. #define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
  45. #define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
  46. #define SPMI_MAPPING_TABLE_LEN 255
  47. #define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
  48. #define PPID_TO_CHAN_TABLE_SZ BIT(12) /* PPID is 12bit chan is 1byte*/
  49. /* Ownership Table */
  50. #define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
  51. #define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
  52. /* Channel Status fields */
  53. enum pmic_arb_chnl_status {
  54. PMIC_ARB_STATUS_DONE = (1 << 0),
  55. PMIC_ARB_STATUS_FAILURE = (1 << 1),
  56. PMIC_ARB_STATUS_DENIED = (1 << 2),
  57. PMIC_ARB_STATUS_DROPPED = (1 << 3),
  58. };
  59. /* Command register fields */
  60. #define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
  61. /* Command Opcodes */
  62. enum pmic_arb_cmd_op_code {
  63. PMIC_ARB_OP_EXT_WRITEL = 0,
  64. PMIC_ARB_OP_EXT_READL = 1,
  65. PMIC_ARB_OP_EXT_WRITE = 2,
  66. PMIC_ARB_OP_RESET = 3,
  67. PMIC_ARB_OP_SLEEP = 4,
  68. PMIC_ARB_OP_SHUTDOWN = 5,
  69. PMIC_ARB_OP_WAKEUP = 6,
  70. PMIC_ARB_OP_AUTHENTICATE = 7,
  71. PMIC_ARB_OP_MSTR_READ = 8,
  72. PMIC_ARB_OP_MSTR_WRITE = 9,
  73. PMIC_ARB_OP_EXT_READ = 13,
  74. PMIC_ARB_OP_WRITE = 14,
  75. PMIC_ARB_OP_READ = 15,
  76. PMIC_ARB_OP_ZERO_WRITE = 16,
  77. };
  78. /* Maximum number of support PMIC peripherals */
  79. #define PMIC_ARB_MAX_PERIPHS 256
  80. #define PMIC_ARB_MAX_CHNL 128
  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. struct pmic_arb_ver_ops;
  89. /**
  90. * spmi_pmic_arb_dev - SPMI PMIC Arbiter object
  91. *
  92. * @rd_base: on v1 "core", on v2 "observer" register base off DT.
  93. * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
  94. * @intr: address of the SPMI interrupt control registers.
  95. * @cnfg: address of the PMIC Arbiter configuration registers.
  96. * @lock: lock to synchronize accesses.
  97. * @channel: execution environment channel to use for accesses.
  98. * @irq: PMIC ARB interrupt.
  99. * @ee: the current Execution Environment
  100. * @min_apid: minimum APID (used for bounding IRQ search)
  101. * @max_apid: maximum APID
  102. * @mapping_table: in-memory copy of PPID -> APID mapping table.
  103. * @domain: irq domain object for PMIC IRQ domain
  104. * @spmic: SPMI controller object
  105. * @apid_to_ppid: in-memory copy of APID -> PPID mapping table.
  106. * @ver_ops: version dependent operations.
  107. * @ppid_to_chan in-memory copy of PPID -> channel (APID) mapping table.
  108. * v2 only.
  109. */
  110. struct spmi_pmic_arb_dev {
  111. void __iomem *rd_base;
  112. void __iomem *wr_base;
  113. void __iomem *intr;
  114. void __iomem *cnfg;
  115. raw_spinlock_t lock;
  116. u8 channel;
  117. int irq;
  118. u8 ee;
  119. u8 min_apid;
  120. u8 max_apid;
  121. u32 mapping_table[SPMI_MAPPING_TABLE_LEN];
  122. struct irq_domain *domain;
  123. struct spmi_controller *spmic;
  124. u16 apid_to_ppid[256];
  125. const struct pmic_arb_ver_ops *ver_ops;
  126. u8 *ppid_to_chan;
  127. };
  128. /**
  129. * pmic_arb_ver: version dependent functionality.
  130. *
  131. * @non_data_cmd: on v1 issues an spmi non-data command.
  132. * on v2 no HW support, returns -EOPNOTSUPP.
  133. * @offset: on v1 offset of per-ee channel.
  134. * on v2 offset of per-ee and per-ppid channel.
  135. * @fmt_cmd: formats a GENI/SPMI command.
  136. * @owner_acc_status: on v1 offset of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
  137. * on v2 offset of SPMI_PIC_OWNERm_ACC_STATUSn.
  138. * @acc_enable: on v1 offset of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
  139. * on v2 offset of SPMI_PIC_ACC_ENABLEn.
  140. * @irq_status: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
  141. * on v2 offset of SPMI_PIC_IRQ_STATUSn.
  142. * @irq_clear: on v1 offset of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
  143. * on v2 offset of SPMI_PIC_IRQ_CLEARn.
  144. */
  145. struct pmic_arb_ver_ops {
  146. /* spmi commands (read_cmd, write_cmd, cmd) functionality */
  147. u32 (*offset)(struct spmi_pmic_arb_dev *dev, u8 sid, u16 addr);
  148. u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
  149. int (*non_data_cmd)(struct spmi_controller *ctrl, u8 opc, u8 sid);
  150. /* Interrupts controller functionality (offset of PIC registers) */
  151. u32 (*owner_acc_status)(u8 m, u8 n);
  152. u32 (*acc_enable)(u8 n);
  153. u32 (*irq_status)(u8 n);
  154. u32 (*irq_clear)(u8 n);
  155. };
  156. static inline void pmic_arb_base_write(struct spmi_pmic_arb_dev *dev,
  157. u32 offset, u32 val)
  158. {
  159. writel_relaxed(val, dev->wr_base + offset);
  160. }
  161. static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb_dev *dev,
  162. u32 offset, u32 val)
  163. {
  164. writel_relaxed(val, dev->rd_base + offset);
  165. }
  166. /**
  167. * pa_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
  168. * @bc: byte count -1. range: 0..3
  169. * @reg: register's address
  170. * @buf: output parameter, length must be bc + 1
  171. */
  172. static void pa_read_data(struct spmi_pmic_arb_dev *dev, u8 *buf, u32 reg, u8 bc)
  173. {
  174. u32 data = __raw_readl(dev->rd_base + reg);
  175. memcpy(buf, &data, (bc & 3) + 1);
  176. }
  177. /**
  178. * pa_write_data: write 1..4 bytes from buf to pmic-arb's register
  179. * @bc: byte-count -1. range: 0..3.
  180. * @reg: register's address.
  181. * @buf: buffer to write. length must be bc + 1.
  182. */
  183. static void
  184. pa_write_data(struct spmi_pmic_arb_dev *dev, const u8 *buf, u32 reg, u8 bc)
  185. {
  186. u32 data = 0;
  187. memcpy(&data, buf, (bc & 3) + 1);
  188. __raw_writel(data, dev->wr_base + reg);
  189. }
  190. static int pmic_arb_wait_for_done(struct spmi_controller *ctrl,
  191. void __iomem *base, u8 sid, u16 addr)
  192. {
  193. struct spmi_pmic_arb_dev *dev = spmi_controller_get_drvdata(ctrl);
  194. u32 status = 0;
  195. u32 timeout = PMIC_ARB_TIMEOUT_US;
  196. u32 offset = dev->ver_ops->offset(dev, sid, addr) + PMIC_ARB_STATUS;
  197. while (timeout--) {
  198. status = readl_relaxed(base + offset);
  199. if (status & PMIC_ARB_STATUS_DONE) {
  200. if (status & PMIC_ARB_STATUS_DENIED) {
  201. dev_err(&ctrl->dev,
  202. "%s: transaction denied (0x%x)\n",
  203. __func__, status);
  204. return -EPERM;
  205. }
  206. if (status & PMIC_ARB_STATUS_FAILURE) {
  207. dev_err(&ctrl->dev,
  208. "%s: transaction failed (0x%x)\n",
  209. __func__, status);
  210. return -EIO;
  211. }
  212. if (status & PMIC_ARB_STATUS_DROPPED) {
  213. dev_err(&ctrl->dev,
  214. "%s: transaction dropped (0x%x)\n",
  215. __func__, status);
  216. return -EIO;
  217. }
  218. return 0;
  219. }
  220. udelay(1);
  221. }
  222. dev_err(&ctrl->dev,
  223. "%s: timeout, status 0x%x\n",
  224. __func__, status);
  225. return -ETIMEDOUT;
  226. }
  227. static int
  228. pmic_arb_non_data_cmd_v1(struct spmi_controller *ctrl, u8 opc, u8 sid)
  229. {
  230. struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
  231. unsigned long flags;
  232. u32 cmd;
  233. int rc;
  234. u32 offset = pmic_arb->ver_ops->offset(pmic_arb, sid, 0);
  235. cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
  236. raw_spin_lock_irqsave(&pmic_arb->lock, flags);
  237. pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
  238. rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, 0);
  239. raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
  240. return rc;
  241. }
  242. static int
  243. pmic_arb_non_data_cmd_v2(struct spmi_controller *ctrl, u8 opc, u8 sid)
  244. {
  245. return -EOPNOTSUPP;
  246. }
  247. /* Non-data command */
  248. static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
  249. {
  250. struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
  251. dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
  252. /* Check for valid non-data command */
  253. if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
  254. return -EINVAL;
  255. return pmic_arb->ver_ops->non_data_cmd(ctrl, opc, sid);
  256. }
  257. static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  258. u16 addr, u8 *buf, size_t len)
  259. {
  260. struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
  261. unsigned long flags;
  262. u8 bc = len - 1;
  263. u32 cmd;
  264. int rc;
  265. u32 offset = pmic_arb->ver_ops->offset(pmic_arb, sid, addr);
  266. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  267. dev_err(&ctrl->dev,
  268. "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
  269. PMIC_ARB_MAX_TRANS_BYTES, len);
  270. return -EINVAL;
  271. }
  272. /* Check the opcode */
  273. if (opc >= 0x60 && opc <= 0x7F)
  274. opc = PMIC_ARB_OP_READ;
  275. else if (opc >= 0x20 && opc <= 0x2F)
  276. opc = PMIC_ARB_OP_EXT_READ;
  277. else if (opc >= 0x38 && opc <= 0x3F)
  278. opc = PMIC_ARB_OP_EXT_READL;
  279. else
  280. return -EINVAL;
  281. cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
  282. raw_spin_lock_irqsave(&pmic_arb->lock, flags);
  283. pmic_arb_set_rd_cmd(pmic_arb, offset + PMIC_ARB_CMD, cmd);
  284. rc = pmic_arb_wait_for_done(ctrl, pmic_arb->rd_base, sid, addr);
  285. if (rc)
  286. goto done;
  287. pa_read_data(pmic_arb, buf, offset + PMIC_ARB_RDATA0,
  288. min_t(u8, bc, 3));
  289. if (bc > 3)
  290. pa_read_data(pmic_arb, buf + 4,
  291. offset + PMIC_ARB_RDATA1, bc - 4);
  292. done:
  293. raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
  294. return rc;
  295. }
  296. static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  297. u16 addr, const u8 *buf, size_t len)
  298. {
  299. struct spmi_pmic_arb_dev *pmic_arb = spmi_controller_get_drvdata(ctrl);
  300. unsigned long flags;
  301. u8 bc = len - 1;
  302. u32 cmd;
  303. int rc;
  304. u32 offset = pmic_arb->ver_ops->offset(pmic_arb, sid, addr);
  305. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  306. dev_err(&ctrl->dev,
  307. "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
  308. PMIC_ARB_MAX_TRANS_BYTES, len);
  309. return -EINVAL;
  310. }
  311. /* Check the opcode */
  312. if (opc >= 0x40 && opc <= 0x5F)
  313. opc = PMIC_ARB_OP_WRITE;
  314. else if (opc >= 0x00 && opc <= 0x0F)
  315. opc = PMIC_ARB_OP_EXT_WRITE;
  316. else if (opc >= 0x30 && opc <= 0x37)
  317. opc = PMIC_ARB_OP_EXT_WRITEL;
  318. else if (opc >= 0x80)
  319. opc = PMIC_ARB_OP_ZERO_WRITE;
  320. else
  321. return -EINVAL;
  322. cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
  323. /* Write data to FIFOs */
  324. raw_spin_lock_irqsave(&pmic_arb->lock, flags);
  325. pa_write_data(pmic_arb, buf, offset + PMIC_ARB_WDATA0,
  326. min_t(u8, bc, 3));
  327. if (bc > 3)
  328. pa_write_data(pmic_arb, buf + 4,
  329. offset + PMIC_ARB_WDATA1, bc - 4);
  330. /* Start the transaction */
  331. pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
  332. rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, addr);
  333. raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
  334. return rc;
  335. }
  336. enum qpnpint_regs {
  337. QPNPINT_REG_RT_STS = 0x10,
  338. QPNPINT_REG_SET_TYPE = 0x11,
  339. QPNPINT_REG_POLARITY_HIGH = 0x12,
  340. QPNPINT_REG_POLARITY_LOW = 0x13,
  341. QPNPINT_REG_LATCHED_CLR = 0x14,
  342. QPNPINT_REG_EN_SET = 0x15,
  343. QPNPINT_REG_EN_CLR = 0x16,
  344. QPNPINT_REG_LATCHED_STS = 0x18,
  345. };
  346. struct spmi_pmic_arb_qpnpint_type {
  347. u8 type; /* 1 -> edge */
  348. u8 polarity_high;
  349. u8 polarity_low;
  350. } __packed;
  351. /* Simplified accessor functions for irqchip callbacks */
  352. static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
  353. size_t len)
  354. {
  355. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  356. u8 sid = d->hwirq >> 24;
  357. u8 per = d->hwirq >> 16;
  358. if (pmic_arb_write_cmd(pa->spmic, SPMI_CMD_EXT_WRITEL, sid,
  359. (per << 8) + reg, buf, len))
  360. dev_err_ratelimited(&pa->spmic->dev,
  361. "failed irqchip transaction on %x\n",
  362. d->irq);
  363. }
  364. static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
  365. {
  366. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  367. u8 sid = d->hwirq >> 24;
  368. u8 per = d->hwirq >> 16;
  369. if (pmic_arb_read_cmd(pa->spmic, SPMI_CMD_EXT_READL, sid,
  370. (per << 8) + reg, buf, len))
  371. dev_err_ratelimited(&pa->spmic->dev,
  372. "failed irqchip transaction on %x\n",
  373. d->irq);
  374. }
  375. static void periph_interrupt(struct spmi_pmic_arb_dev *pa, u8 apid)
  376. {
  377. unsigned int irq;
  378. u32 status;
  379. int id;
  380. status = readl_relaxed(pa->intr + pa->ver_ops->irq_status(apid));
  381. while (status) {
  382. id = ffs(status) - 1;
  383. status &= ~(1 << id);
  384. irq = irq_find_mapping(pa->domain,
  385. pa->apid_to_ppid[apid] << 16
  386. | id << 8
  387. | apid);
  388. generic_handle_irq(irq);
  389. }
  390. }
  391. static void pmic_arb_chained_irq(struct irq_desc *desc)
  392. {
  393. struct spmi_pmic_arb_dev *pa = irq_desc_get_handler_data(desc);
  394. struct irq_chip *chip = irq_desc_get_chip(desc);
  395. void __iomem *intr = pa->intr;
  396. int first = pa->min_apid >> 5;
  397. int last = pa->max_apid >> 5;
  398. u32 status;
  399. int i, id;
  400. chained_irq_enter(chip, desc);
  401. for (i = first; i <= last; ++i) {
  402. status = readl_relaxed(intr +
  403. pa->ver_ops->owner_acc_status(pa->ee, i));
  404. while (status) {
  405. id = ffs(status) - 1;
  406. status &= ~(1 << id);
  407. periph_interrupt(pa, id + i * 32);
  408. }
  409. }
  410. chained_irq_exit(chip, desc);
  411. }
  412. static void qpnpint_irq_ack(struct irq_data *d)
  413. {
  414. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  415. u8 irq = d->hwirq >> 8;
  416. u8 apid = d->hwirq;
  417. unsigned long flags;
  418. u8 data;
  419. raw_spin_lock_irqsave(&pa->lock, flags);
  420. writel_relaxed(1 << irq, pa->intr + pa->ver_ops->irq_clear(apid));
  421. raw_spin_unlock_irqrestore(&pa->lock, flags);
  422. data = 1 << irq;
  423. qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
  424. }
  425. static void qpnpint_irq_mask(struct irq_data *d)
  426. {
  427. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  428. u8 irq = d->hwirq >> 8;
  429. u8 apid = d->hwirq;
  430. unsigned long flags;
  431. u32 status;
  432. u8 data;
  433. raw_spin_lock_irqsave(&pa->lock, flags);
  434. status = readl_relaxed(pa->intr + pa->ver_ops->acc_enable(apid));
  435. if (status & SPMI_PIC_ACC_ENABLE_BIT) {
  436. status = status & ~SPMI_PIC_ACC_ENABLE_BIT;
  437. writel_relaxed(status, pa->intr +
  438. pa->ver_ops->acc_enable(apid));
  439. }
  440. raw_spin_unlock_irqrestore(&pa->lock, flags);
  441. data = 1 << irq;
  442. qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
  443. }
  444. static void qpnpint_irq_unmask(struct irq_data *d)
  445. {
  446. struct spmi_pmic_arb_dev *pa = irq_data_get_irq_chip_data(d);
  447. u8 irq = d->hwirq >> 8;
  448. u8 apid = d->hwirq;
  449. unsigned long flags;
  450. u32 status;
  451. u8 data;
  452. raw_spin_lock_irqsave(&pa->lock, flags);
  453. status = readl_relaxed(pa->intr + pa->ver_ops->acc_enable(apid));
  454. if (!(status & SPMI_PIC_ACC_ENABLE_BIT)) {
  455. writel_relaxed(status | SPMI_PIC_ACC_ENABLE_BIT,
  456. pa->intr + pa->ver_ops->acc_enable(apid));
  457. }
  458. raw_spin_unlock_irqrestore(&pa->lock, flags);
  459. data = 1 << irq;
  460. qpnpint_spmi_write(d, QPNPINT_REG_EN_SET, &data, 1);
  461. }
  462. static void qpnpint_irq_enable(struct irq_data *d)
  463. {
  464. u8 irq = d->hwirq >> 8;
  465. u8 data;
  466. qpnpint_irq_unmask(d);
  467. data = 1 << irq;
  468. qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
  469. }
  470. static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
  471. {
  472. struct spmi_pmic_arb_qpnpint_type type;
  473. u8 irq = d->hwirq >> 8;
  474. qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
  475. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  476. type.type |= 1 << irq;
  477. if (flow_type & IRQF_TRIGGER_RISING)
  478. type.polarity_high |= 1 << irq;
  479. if (flow_type & IRQF_TRIGGER_FALLING)
  480. type.polarity_low |= 1 << irq;
  481. } else {
  482. if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
  483. (flow_type & (IRQF_TRIGGER_LOW)))
  484. return -EINVAL;
  485. type.type &= ~(1 << irq); /* level trig */
  486. if (flow_type & IRQF_TRIGGER_HIGH)
  487. type.polarity_high |= 1 << irq;
  488. else
  489. type.polarity_low |= 1 << irq;
  490. }
  491. qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
  492. return 0;
  493. }
  494. static int qpnpint_get_irqchip_state(struct irq_data *d,
  495. enum irqchip_irq_state which,
  496. bool *state)
  497. {
  498. u8 irq = d->hwirq >> 8;
  499. u8 status = 0;
  500. if (which != IRQCHIP_STATE_LINE_LEVEL)
  501. return -EINVAL;
  502. qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
  503. *state = !!(status & BIT(irq));
  504. return 0;
  505. }
  506. static struct irq_chip pmic_arb_irqchip = {
  507. .name = "pmic_arb",
  508. .irq_enable = qpnpint_irq_enable,
  509. .irq_ack = qpnpint_irq_ack,
  510. .irq_mask = qpnpint_irq_mask,
  511. .irq_unmask = qpnpint_irq_unmask,
  512. .irq_set_type = qpnpint_irq_set_type,
  513. .irq_get_irqchip_state = qpnpint_get_irqchip_state,
  514. .flags = IRQCHIP_MASK_ON_SUSPEND
  515. | IRQCHIP_SKIP_SET_WAKE,
  516. };
  517. struct spmi_pmic_arb_irq_spec {
  518. unsigned slave:4;
  519. unsigned per:8;
  520. unsigned irq:3;
  521. };
  522. static int search_mapping_table(struct spmi_pmic_arb_dev *pa,
  523. struct spmi_pmic_arb_irq_spec *spec,
  524. u8 *apid)
  525. {
  526. u16 ppid = spec->slave << 8 | spec->per;
  527. u32 *mapping_table = pa->mapping_table;
  528. int index = 0, i;
  529. u32 data;
  530. for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
  531. data = mapping_table[index];
  532. if (ppid & (1 << SPMI_MAPPING_BIT_INDEX(data))) {
  533. if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
  534. index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  535. } else {
  536. *apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  537. return 0;
  538. }
  539. } else {
  540. if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
  541. index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  542. } else {
  543. *apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  544. return 0;
  545. }
  546. }
  547. }
  548. return -ENODEV;
  549. }
  550. static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
  551. struct device_node *controller,
  552. const u32 *intspec,
  553. unsigned int intsize,
  554. unsigned long *out_hwirq,
  555. unsigned int *out_type)
  556. {
  557. struct spmi_pmic_arb_dev *pa = d->host_data;
  558. struct spmi_pmic_arb_irq_spec spec;
  559. int err;
  560. u8 apid;
  561. dev_dbg(&pa->spmic->dev,
  562. "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
  563. intspec[0], intspec[1], intspec[2]);
  564. if (irq_domain_get_of_node(d) != controller)
  565. return -EINVAL;
  566. if (intsize != 4)
  567. return -EINVAL;
  568. if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
  569. return -EINVAL;
  570. spec.slave = intspec[0];
  571. spec.per = intspec[1];
  572. spec.irq = intspec[2];
  573. err = search_mapping_table(pa, &spec, &apid);
  574. if (err)
  575. return err;
  576. pa->apid_to_ppid[apid] = spec.slave << 8 | spec.per;
  577. /* Keep track of {max,min}_apid for bounding search during interrupt */
  578. if (apid > pa->max_apid)
  579. pa->max_apid = apid;
  580. if (apid < pa->min_apid)
  581. pa->min_apid = apid;
  582. *out_hwirq = spec.slave << 24
  583. | spec.per << 16
  584. | spec.irq << 8
  585. | apid;
  586. *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
  587. dev_dbg(&pa->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
  588. return 0;
  589. }
  590. static int qpnpint_irq_domain_map(struct irq_domain *d,
  591. unsigned int virq,
  592. irq_hw_number_t hwirq)
  593. {
  594. struct spmi_pmic_arb_dev *pa = d->host_data;
  595. dev_dbg(&pa->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
  596. irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
  597. irq_set_chip_data(virq, d->host_data);
  598. irq_set_noprobe(virq);
  599. return 0;
  600. }
  601. /* v1 offset per ee */
  602. static u32 pmic_arb_offset_v1(struct spmi_pmic_arb_dev *pa, u8 sid, u16 addr)
  603. {
  604. return 0x800 + 0x80 * pa->channel;
  605. }
  606. /* v2 offset per ppid (chan) and per ee */
  607. static u32 pmic_arb_offset_v2(struct spmi_pmic_arb_dev *pa, u8 sid, u16 addr)
  608. {
  609. u16 ppid = (sid << 8) | (addr >> 8);
  610. u8 chan = pa->ppid_to_chan[ppid];
  611. return 0x1000 * pa->ee + 0x8000 * chan;
  612. }
  613. static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
  614. {
  615. return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
  616. }
  617. static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
  618. {
  619. return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
  620. }
  621. static u32 pmic_arb_owner_acc_status_v1(u8 m, u8 n)
  622. {
  623. return 0x20 * m + 0x4 * n;
  624. }
  625. static u32 pmic_arb_owner_acc_status_v2(u8 m, u8 n)
  626. {
  627. return 0x100000 + 0x1000 * m + 0x4 * n;
  628. }
  629. static u32 pmic_arb_acc_enable_v1(u8 n)
  630. {
  631. return 0x200 + 0x4 * n;
  632. }
  633. static u32 pmic_arb_acc_enable_v2(u8 n)
  634. {
  635. return 0x1000 * n;
  636. }
  637. static u32 pmic_arb_irq_status_v1(u8 n)
  638. {
  639. return 0x600 + 0x4 * n;
  640. }
  641. static u32 pmic_arb_irq_status_v2(u8 n)
  642. {
  643. return 0x4 + 0x1000 * n;
  644. }
  645. static u32 pmic_arb_irq_clear_v1(u8 n)
  646. {
  647. return 0xA00 + 0x4 * n;
  648. }
  649. static u32 pmic_arb_irq_clear_v2(u8 n)
  650. {
  651. return 0x8 + 0x1000 * n;
  652. }
  653. static const struct pmic_arb_ver_ops pmic_arb_v1 = {
  654. .non_data_cmd = pmic_arb_non_data_cmd_v1,
  655. .offset = pmic_arb_offset_v1,
  656. .fmt_cmd = pmic_arb_fmt_cmd_v1,
  657. .owner_acc_status = pmic_arb_owner_acc_status_v1,
  658. .acc_enable = pmic_arb_acc_enable_v1,
  659. .irq_status = pmic_arb_irq_status_v1,
  660. .irq_clear = pmic_arb_irq_clear_v1,
  661. };
  662. static const struct pmic_arb_ver_ops pmic_arb_v2 = {
  663. .non_data_cmd = pmic_arb_non_data_cmd_v2,
  664. .offset = pmic_arb_offset_v2,
  665. .fmt_cmd = pmic_arb_fmt_cmd_v2,
  666. .owner_acc_status = pmic_arb_owner_acc_status_v2,
  667. .acc_enable = pmic_arb_acc_enable_v2,
  668. .irq_status = pmic_arb_irq_status_v2,
  669. .irq_clear = pmic_arb_irq_clear_v2,
  670. };
  671. static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
  672. .map = qpnpint_irq_domain_map,
  673. .xlate = qpnpint_irq_domain_dt_translate,
  674. };
  675. static int spmi_pmic_arb_probe(struct platform_device *pdev)
  676. {
  677. struct spmi_pmic_arb_dev *pa;
  678. struct spmi_controller *ctrl;
  679. struct resource *res;
  680. void __iomem *core;
  681. u32 channel, ee, hw_ver;
  682. int err, i;
  683. bool is_v1;
  684. ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
  685. if (!ctrl)
  686. return -ENOMEM;
  687. pa = spmi_controller_get_drvdata(ctrl);
  688. pa->spmic = ctrl;
  689. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
  690. core = devm_ioremap_resource(&ctrl->dev, res);
  691. if (IS_ERR(core)) {
  692. err = PTR_ERR(core);
  693. goto err_put_ctrl;
  694. }
  695. hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
  696. is_v1 = (hw_ver < PMIC_ARB_VERSION_V2_MIN);
  697. dev_info(&ctrl->dev, "PMIC Arb Version-%d (0x%x)\n", (is_v1 ? 1 : 2),
  698. hw_ver);
  699. if (is_v1) {
  700. pa->ver_ops = &pmic_arb_v1;
  701. pa->wr_base = core;
  702. pa->rd_base = core;
  703. } else {
  704. u8 chan;
  705. u16 ppid;
  706. u32 regval;
  707. pa->ver_ops = &pmic_arb_v2;
  708. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  709. "obsrvr");
  710. pa->rd_base = devm_ioremap_resource(&ctrl->dev, res);
  711. if (IS_ERR(pa->rd_base)) {
  712. err = PTR_ERR(pa->rd_base);
  713. goto err_put_ctrl;
  714. }
  715. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  716. "chnls");
  717. pa->wr_base = devm_ioremap_resource(&ctrl->dev, res);
  718. if (IS_ERR(pa->wr_base)) {
  719. err = PTR_ERR(pa->wr_base);
  720. goto err_put_ctrl;
  721. }
  722. pa->ppid_to_chan = devm_kzalloc(&ctrl->dev,
  723. PPID_TO_CHAN_TABLE_SZ, GFP_KERNEL);
  724. if (!pa->ppid_to_chan) {
  725. err = -ENOMEM;
  726. goto err_put_ctrl;
  727. }
  728. /*
  729. * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid.
  730. * ppid_to_chan is an in-memory invert of that table.
  731. */
  732. for (chan = 0; chan < PMIC_ARB_MAX_CHNL; ++chan) {
  733. regval = readl_relaxed(core + PMIC_ARB_REG_CHNL(chan));
  734. if (!regval)
  735. continue;
  736. ppid = (regval >> 8) & 0xFFF;
  737. pa->ppid_to_chan[ppid] = chan;
  738. }
  739. }
  740. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
  741. pa->intr = devm_ioremap_resource(&ctrl->dev, res);
  742. if (IS_ERR(pa->intr)) {
  743. err = PTR_ERR(pa->intr);
  744. goto err_put_ctrl;
  745. }
  746. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
  747. pa->cnfg = devm_ioremap_resource(&ctrl->dev, res);
  748. if (IS_ERR(pa->cnfg)) {
  749. err = PTR_ERR(pa->cnfg);
  750. goto err_put_ctrl;
  751. }
  752. pa->irq = platform_get_irq_byname(pdev, "periph_irq");
  753. if (pa->irq < 0) {
  754. err = pa->irq;
  755. goto err_put_ctrl;
  756. }
  757. err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
  758. if (err) {
  759. dev_err(&pdev->dev, "channel unspecified.\n");
  760. goto err_put_ctrl;
  761. }
  762. if (channel > 5) {
  763. dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
  764. channel);
  765. goto err_put_ctrl;
  766. }
  767. pa->channel = channel;
  768. err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
  769. if (err) {
  770. dev_err(&pdev->dev, "EE unspecified.\n");
  771. goto err_put_ctrl;
  772. }
  773. if (ee > 5) {
  774. dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
  775. err = -EINVAL;
  776. goto err_put_ctrl;
  777. }
  778. pa->ee = ee;
  779. for (i = 0; i < ARRAY_SIZE(pa->mapping_table); ++i)
  780. pa->mapping_table[i] = readl_relaxed(
  781. pa->cnfg + SPMI_MAPPING_TABLE_REG(i));
  782. /* Initialize max_apid/min_apid to the opposite bounds, during
  783. * the irq domain translation, we are sure to update these */
  784. pa->max_apid = 0;
  785. pa->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
  786. platform_set_drvdata(pdev, ctrl);
  787. raw_spin_lock_init(&pa->lock);
  788. ctrl->cmd = pmic_arb_cmd;
  789. ctrl->read_cmd = pmic_arb_read_cmd;
  790. ctrl->write_cmd = pmic_arb_write_cmd;
  791. dev_dbg(&pdev->dev, "adding irq domain\n");
  792. pa->domain = irq_domain_add_tree(pdev->dev.of_node,
  793. &pmic_arb_irq_domain_ops, pa);
  794. if (!pa->domain) {
  795. dev_err(&pdev->dev, "unable to create irq_domain\n");
  796. err = -ENOMEM;
  797. goto err_put_ctrl;
  798. }
  799. irq_set_chained_handler_and_data(pa->irq, pmic_arb_chained_irq, pa);
  800. err = spmi_controller_add(ctrl);
  801. if (err)
  802. goto err_domain_remove;
  803. return 0;
  804. err_domain_remove:
  805. irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
  806. irq_domain_remove(pa->domain);
  807. err_put_ctrl:
  808. spmi_controller_put(ctrl);
  809. return err;
  810. }
  811. static int spmi_pmic_arb_remove(struct platform_device *pdev)
  812. {
  813. struct spmi_controller *ctrl = platform_get_drvdata(pdev);
  814. struct spmi_pmic_arb_dev *pa = spmi_controller_get_drvdata(ctrl);
  815. spmi_controller_remove(ctrl);
  816. irq_set_chained_handler_and_data(pa->irq, NULL, NULL);
  817. irq_domain_remove(pa->domain);
  818. spmi_controller_put(ctrl);
  819. return 0;
  820. }
  821. static const struct of_device_id spmi_pmic_arb_match_table[] = {
  822. { .compatible = "qcom,spmi-pmic-arb", },
  823. {},
  824. };
  825. MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
  826. static struct platform_driver spmi_pmic_arb_driver = {
  827. .probe = spmi_pmic_arb_probe,
  828. .remove = spmi_pmic_arb_remove,
  829. .driver = {
  830. .name = "spmi_pmic_arb",
  831. .of_match_table = spmi_pmic_arb_match_table,
  832. },
  833. };
  834. module_platform_driver(spmi_pmic_arb_driver);
  835. MODULE_LICENSE("GPL v2");
  836. MODULE_ALIAS("platform:spmi_pmic_arb");