spmi-pmic-arb.c 27 KB

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