spmi-pmic-arb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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(unsigned int irq, struct irq_desc *desc)
  396. {
  397. struct spmi_pmic_arb_dev *pa = irq_get_handler_data(irq);
  398. struct irq_chip *chip = irq_get_chip(irq);
  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 struct irq_chip pmic_arb_irqchip = {
  499. .name = "pmic_arb",
  500. .irq_enable = qpnpint_irq_enable,
  501. .irq_ack = qpnpint_irq_ack,
  502. .irq_mask = qpnpint_irq_mask,
  503. .irq_unmask = qpnpint_irq_unmask,
  504. .irq_set_type = qpnpint_irq_set_type,
  505. .flags = IRQCHIP_MASK_ON_SUSPEND
  506. | IRQCHIP_SKIP_SET_WAKE,
  507. };
  508. struct spmi_pmic_arb_irq_spec {
  509. unsigned slave:4;
  510. unsigned per:8;
  511. unsigned irq:3;
  512. };
  513. static int search_mapping_table(struct spmi_pmic_arb_dev *pa,
  514. struct spmi_pmic_arb_irq_spec *spec,
  515. u8 *apid)
  516. {
  517. u16 ppid = spec->slave << 8 | spec->per;
  518. u32 *mapping_table = pa->mapping_table;
  519. int index = 0, i;
  520. u32 data;
  521. for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
  522. data = mapping_table[index];
  523. if (ppid & (1 << SPMI_MAPPING_BIT_INDEX(data))) {
  524. if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
  525. index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  526. } else {
  527. *apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  528. return 0;
  529. }
  530. } else {
  531. if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
  532. index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  533. } else {
  534. *apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  535. return 0;
  536. }
  537. }
  538. }
  539. return -ENODEV;
  540. }
  541. static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
  542. struct device_node *controller,
  543. const u32 *intspec,
  544. unsigned int intsize,
  545. unsigned long *out_hwirq,
  546. unsigned int *out_type)
  547. {
  548. struct spmi_pmic_arb_dev *pa = d->host_data;
  549. struct spmi_pmic_arb_irq_spec spec;
  550. int err;
  551. u8 apid;
  552. dev_dbg(&pa->spmic->dev,
  553. "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
  554. intspec[0], intspec[1], intspec[2]);
  555. if (d->of_node != controller)
  556. return -EINVAL;
  557. if (intsize != 4)
  558. return -EINVAL;
  559. if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
  560. return -EINVAL;
  561. spec.slave = intspec[0];
  562. spec.per = intspec[1];
  563. spec.irq = intspec[2];
  564. err = search_mapping_table(pa, &spec, &apid);
  565. if (err)
  566. return err;
  567. pa->apid_to_ppid[apid] = spec.slave << 8 | spec.per;
  568. /* Keep track of {max,min}_apid for bounding search during interrupt */
  569. if (apid > pa->max_apid)
  570. pa->max_apid = apid;
  571. if (apid < pa->min_apid)
  572. pa->min_apid = apid;
  573. *out_hwirq = spec.slave << 24
  574. | spec.per << 16
  575. | spec.irq << 8
  576. | apid;
  577. *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
  578. dev_dbg(&pa->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
  579. return 0;
  580. }
  581. static int qpnpint_irq_domain_map(struct irq_domain *d,
  582. unsigned int virq,
  583. irq_hw_number_t hwirq)
  584. {
  585. struct spmi_pmic_arb_dev *pa = d->host_data;
  586. dev_dbg(&pa->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
  587. irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
  588. irq_set_chip_data(virq, d->host_data);
  589. irq_set_noprobe(virq);
  590. return 0;
  591. }
  592. /* v1 offset per ee */
  593. static u32 pmic_arb_offset_v1(struct spmi_pmic_arb_dev *pa, u8 sid, u16 addr)
  594. {
  595. return 0x800 + 0x80 * pa->channel;
  596. }
  597. /* v2 offset per ppid (chan) and per ee */
  598. static u32 pmic_arb_offset_v2(struct spmi_pmic_arb_dev *pa, u8 sid, u16 addr)
  599. {
  600. u16 ppid = (sid << 8) | (addr >> 8);
  601. u8 chan = pa->ppid_to_chan[ppid];
  602. return 0x1000 * pa->ee + 0x8000 * chan;
  603. }
  604. static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
  605. {
  606. return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
  607. }
  608. static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
  609. {
  610. return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
  611. }
  612. static u32 pmic_arb_owner_acc_status_v1(u8 m, u8 n)
  613. {
  614. return 0x20 * m + 0x4 * n;
  615. }
  616. static u32 pmic_arb_owner_acc_status_v2(u8 m, u8 n)
  617. {
  618. return 0x100000 + 0x1000 * m + 0x4 * n;
  619. }
  620. static u32 pmic_arb_acc_enable_v1(u8 n)
  621. {
  622. return 0x200 + 0x4 * n;
  623. }
  624. static u32 pmic_arb_acc_enable_v2(u8 n)
  625. {
  626. return 0x1000 * n;
  627. }
  628. static u32 pmic_arb_irq_status_v1(u8 n)
  629. {
  630. return 0x600 + 0x4 * n;
  631. }
  632. static u32 pmic_arb_irq_status_v2(u8 n)
  633. {
  634. return 0x4 + 0x1000 * n;
  635. }
  636. static u32 pmic_arb_irq_clear_v1(u8 n)
  637. {
  638. return 0xA00 + 0x4 * n;
  639. }
  640. static u32 pmic_arb_irq_clear_v2(u8 n)
  641. {
  642. return 0x8 + 0x1000 * n;
  643. }
  644. static const struct pmic_arb_ver_ops pmic_arb_v1 = {
  645. .non_data_cmd = pmic_arb_non_data_cmd_v1,
  646. .offset = pmic_arb_offset_v1,
  647. .fmt_cmd = pmic_arb_fmt_cmd_v1,
  648. .owner_acc_status = pmic_arb_owner_acc_status_v1,
  649. .acc_enable = pmic_arb_acc_enable_v1,
  650. .irq_status = pmic_arb_irq_status_v1,
  651. .irq_clear = pmic_arb_irq_clear_v1,
  652. };
  653. static const struct pmic_arb_ver_ops pmic_arb_v2 = {
  654. .non_data_cmd = pmic_arb_non_data_cmd_v2,
  655. .offset = pmic_arb_offset_v2,
  656. .fmt_cmd = pmic_arb_fmt_cmd_v2,
  657. .owner_acc_status = pmic_arb_owner_acc_status_v2,
  658. .acc_enable = pmic_arb_acc_enable_v2,
  659. .irq_status = pmic_arb_irq_status_v2,
  660. .irq_clear = pmic_arb_irq_clear_v2,
  661. };
  662. static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
  663. .map = qpnpint_irq_domain_map,
  664. .xlate = qpnpint_irq_domain_dt_translate,
  665. };
  666. static int spmi_pmic_arb_probe(struct platform_device *pdev)
  667. {
  668. struct spmi_pmic_arb_dev *pa;
  669. struct spmi_controller *ctrl;
  670. struct resource *res;
  671. void __iomem *core;
  672. u32 channel, ee, hw_ver;
  673. int err, i;
  674. bool is_v1;
  675. ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pa));
  676. if (!ctrl)
  677. return -ENOMEM;
  678. pa = spmi_controller_get_drvdata(ctrl);
  679. pa->spmic = ctrl;
  680. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
  681. core = devm_ioremap_resource(&ctrl->dev, res);
  682. if (IS_ERR(core)) {
  683. err = PTR_ERR(core);
  684. goto err_put_ctrl;
  685. }
  686. hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
  687. is_v1 = (hw_ver < PMIC_ARB_VERSION_V2_MIN);
  688. dev_info(&ctrl->dev, "PMIC Arb Version-%d (0x%x)\n", (is_v1 ? 1 : 2),
  689. hw_ver);
  690. if (is_v1) {
  691. pa->ver_ops = &pmic_arb_v1;
  692. pa->wr_base = core;
  693. pa->rd_base = core;
  694. } else {
  695. u8 chan;
  696. u16 ppid;
  697. u32 regval;
  698. pa->ver_ops = &pmic_arb_v2;
  699. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  700. "obsrvr");
  701. pa->rd_base = devm_ioremap_resource(&ctrl->dev, res);
  702. if (IS_ERR(pa->rd_base)) {
  703. err = PTR_ERR(pa->rd_base);
  704. goto err_put_ctrl;
  705. }
  706. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  707. "chnls");
  708. pa->wr_base = devm_ioremap_resource(&ctrl->dev, res);
  709. if (IS_ERR(pa->wr_base)) {
  710. err = PTR_ERR(pa->wr_base);
  711. goto err_put_ctrl;
  712. }
  713. pa->ppid_to_chan = devm_kzalloc(&ctrl->dev,
  714. PPID_TO_CHAN_TABLE_SZ, GFP_KERNEL);
  715. if (!pa->ppid_to_chan) {
  716. err = -ENOMEM;
  717. goto err_put_ctrl;
  718. }
  719. /*
  720. * PMIC_ARB_REG_CHNL is a table in HW mapping channel to ppid.
  721. * ppid_to_chan is an in-memory invert of that table.
  722. */
  723. for (chan = 0; chan < PMIC_ARB_MAX_CHNL; ++chan) {
  724. regval = readl_relaxed(core + PMIC_ARB_REG_CHNL(chan));
  725. if (!regval)
  726. continue;
  727. ppid = (regval >> 8) & 0xFFF;
  728. pa->ppid_to_chan[ppid] = chan;
  729. }
  730. }
  731. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
  732. pa->intr = devm_ioremap_resource(&ctrl->dev, res);
  733. if (IS_ERR(pa->intr)) {
  734. err = PTR_ERR(pa->intr);
  735. goto err_put_ctrl;
  736. }
  737. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
  738. pa->cnfg = devm_ioremap_resource(&ctrl->dev, res);
  739. if (IS_ERR(pa->cnfg)) {
  740. err = PTR_ERR(pa->cnfg);
  741. goto err_put_ctrl;
  742. }
  743. pa->irq = platform_get_irq_byname(pdev, "periph_irq");
  744. if (pa->irq < 0) {
  745. err = pa->irq;
  746. goto err_put_ctrl;
  747. }
  748. err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
  749. if (err) {
  750. dev_err(&pdev->dev, "channel unspecified.\n");
  751. goto err_put_ctrl;
  752. }
  753. if (channel > 5) {
  754. dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
  755. channel);
  756. goto err_put_ctrl;
  757. }
  758. pa->channel = channel;
  759. err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
  760. if (err) {
  761. dev_err(&pdev->dev, "EE unspecified.\n");
  762. goto err_put_ctrl;
  763. }
  764. if (ee > 5) {
  765. dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
  766. err = -EINVAL;
  767. goto err_put_ctrl;
  768. }
  769. pa->ee = ee;
  770. for (i = 0; i < ARRAY_SIZE(pa->mapping_table); ++i)
  771. pa->mapping_table[i] = readl_relaxed(
  772. pa->cnfg + SPMI_MAPPING_TABLE_REG(i));
  773. /* Initialize max_apid/min_apid to the opposite bounds, during
  774. * the irq domain translation, we are sure to update these */
  775. pa->max_apid = 0;
  776. pa->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
  777. platform_set_drvdata(pdev, ctrl);
  778. raw_spin_lock_init(&pa->lock);
  779. ctrl->cmd = pmic_arb_cmd;
  780. ctrl->read_cmd = pmic_arb_read_cmd;
  781. ctrl->write_cmd = pmic_arb_write_cmd;
  782. dev_dbg(&pdev->dev, "adding irq domain\n");
  783. pa->domain = irq_domain_add_tree(pdev->dev.of_node,
  784. &pmic_arb_irq_domain_ops, pa);
  785. if (!pa->domain) {
  786. dev_err(&pdev->dev, "unable to create irq_domain\n");
  787. err = -ENOMEM;
  788. goto err_put_ctrl;
  789. }
  790. irq_set_handler_data(pa->irq, pa);
  791. irq_set_chained_handler(pa->irq, pmic_arb_chained_irq);
  792. err = spmi_controller_add(ctrl);
  793. if (err)
  794. goto err_domain_remove;
  795. return 0;
  796. err_domain_remove:
  797. irq_set_chained_handler(pa->irq, NULL);
  798. irq_set_handler_data(pa->irq, NULL);
  799. irq_domain_remove(pa->domain);
  800. err_put_ctrl:
  801. spmi_controller_put(ctrl);
  802. return err;
  803. }
  804. static int spmi_pmic_arb_remove(struct platform_device *pdev)
  805. {
  806. struct spmi_controller *ctrl = platform_get_drvdata(pdev);
  807. struct spmi_pmic_arb_dev *pa = spmi_controller_get_drvdata(ctrl);
  808. spmi_controller_remove(ctrl);
  809. irq_set_chained_handler(pa->irq, NULL);
  810. irq_set_handler_data(pa->irq, NULL);
  811. irq_domain_remove(pa->domain);
  812. spmi_controller_put(ctrl);
  813. return 0;
  814. }
  815. static const struct of_device_id spmi_pmic_arb_match_table[] = {
  816. { .compatible = "qcom,spmi-pmic-arb", },
  817. {},
  818. };
  819. MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
  820. static struct platform_driver spmi_pmic_arb_driver = {
  821. .probe = spmi_pmic_arb_probe,
  822. .remove = spmi_pmic_arb_remove,
  823. .driver = {
  824. .name = "spmi_pmic_arb",
  825. .of_match_table = spmi_pmic_arb_match_table,
  826. },
  827. };
  828. module_platform_driver(spmi_pmic_arb_driver);
  829. MODULE_LICENSE("GPL v2");
  830. MODULE_ALIAS("platform:spmi_pmic_arb");