spmi-pmic-arb.c 26 KB

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