spmi-pmic-arb.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /*
  2. * Copyright (c) 2012-2015, 2017, 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_VERSION_V3_MIN 0x30000000
  31. #define PMIC_ARB_VERSION_V5_MIN 0x50000000
  32. #define PMIC_ARB_INT_EN 0x0004
  33. /* PMIC Arbiter channel registers offsets */
  34. #define PMIC_ARB_CMD 0x00
  35. #define PMIC_ARB_CONFIG 0x04
  36. #define PMIC_ARB_STATUS 0x08
  37. #define PMIC_ARB_WDATA0 0x10
  38. #define PMIC_ARB_WDATA1 0x14
  39. #define PMIC_ARB_RDATA0 0x18
  40. #define PMIC_ARB_RDATA1 0x1C
  41. /* Mapping Table */
  42. #define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
  43. #define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
  44. #define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
  45. #define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
  46. #define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
  47. #define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
  48. #define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
  49. #define PMIC_ARB_MAX_PPID BIT(12) /* PPID is 12bit */
  50. #define PMIC_ARB_APID_VALID BIT(15)
  51. #define PMIC_ARB_CHAN_IS_IRQ_OWNER(reg) ((reg) & BIT(24))
  52. #define INVALID_EE 0xFF
  53. /* Ownership Table */
  54. #define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
  55. #define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
  56. /* Channel Status fields */
  57. enum pmic_arb_chnl_status {
  58. PMIC_ARB_STATUS_DONE = BIT(0),
  59. PMIC_ARB_STATUS_FAILURE = BIT(1),
  60. PMIC_ARB_STATUS_DENIED = BIT(2),
  61. PMIC_ARB_STATUS_DROPPED = BIT(3),
  62. };
  63. /* Command register fields */
  64. #define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
  65. /* Command Opcodes */
  66. enum pmic_arb_cmd_op_code {
  67. PMIC_ARB_OP_EXT_WRITEL = 0,
  68. PMIC_ARB_OP_EXT_READL = 1,
  69. PMIC_ARB_OP_EXT_WRITE = 2,
  70. PMIC_ARB_OP_RESET = 3,
  71. PMIC_ARB_OP_SLEEP = 4,
  72. PMIC_ARB_OP_SHUTDOWN = 5,
  73. PMIC_ARB_OP_WAKEUP = 6,
  74. PMIC_ARB_OP_AUTHENTICATE = 7,
  75. PMIC_ARB_OP_MSTR_READ = 8,
  76. PMIC_ARB_OP_MSTR_WRITE = 9,
  77. PMIC_ARB_OP_EXT_READ = 13,
  78. PMIC_ARB_OP_WRITE = 14,
  79. PMIC_ARB_OP_READ = 15,
  80. PMIC_ARB_OP_ZERO_WRITE = 16,
  81. };
  82. /*
  83. * PMIC arbiter version 5 uses different register offsets for read/write vs
  84. * observer channels.
  85. */
  86. enum pmic_arb_channel {
  87. PMIC_ARB_CHANNEL_RW,
  88. PMIC_ARB_CHANNEL_OBS,
  89. };
  90. /* Maximum number of support PMIC peripherals */
  91. #define PMIC_ARB_MAX_PERIPHS 512
  92. #define PMIC_ARB_TIMEOUT_US 100
  93. #define PMIC_ARB_MAX_TRANS_BYTES (8)
  94. #define PMIC_ARB_APID_MASK 0xFF
  95. #define PMIC_ARB_PPID_MASK 0xFFF
  96. /* interrupt enable bit */
  97. #define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
  98. #define spec_to_hwirq(slave_id, periph_id, irq_id, apid) \
  99. ((((slave_id) & 0xF) << 28) | \
  100. (((periph_id) & 0xFF) << 20) | \
  101. (((irq_id) & 0x7) << 16) | \
  102. (((apid) & 0x1FF) << 0))
  103. #define hwirq_to_sid(hwirq) (((hwirq) >> 28) & 0xF)
  104. #define hwirq_to_per(hwirq) (((hwirq) >> 20) & 0xFF)
  105. #define hwirq_to_irq(hwirq) (((hwirq) >> 16) & 0x7)
  106. #define hwirq_to_apid(hwirq) (((hwirq) >> 0) & 0x1FF)
  107. struct pmic_arb_ver_ops;
  108. struct apid_data {
  109. u16 ppid;
  110. u8 write_ee;
  111. u8 irq_ee;
  112. };
  113. /**
  114. * spmi_pmic_arb - SPMI PMIC Arbiter object
  115. *
  116. * @rd_base: on v1 "core", on v2 "observer" register base off DT.
  117. * @wr_base: on v1 "core", on v2 "chnls" register base off DT.
  118. * @intr: address of the SPMI interrupt control registers.
  119. * @cnfg: address of the PMIC Arbiter configuration registers.
  120. * @lock: lock to synchronize accesses.
  121. * @channel: execution environment channel to use for accesses.
  122. * @irq: PMIC ARB interrupt.
  123. * @ee: the current Execution Environment
  124. * @min_apid: minimum APID (used for bounding IRQ search)
  125. * @max_apid: maximum APID
  126. * @mapping_table: in-memory copy of PPID -> APID mapping table.
  127. * @domain: irq domain object for PMIC IRQ domain
  128. * @spmic: SPMI controller object
  129. * @ver_ops: version dependent operations.
  130. * @ppid_to_apid in-memory copy of PPID -> APID mapping table.
  131. */
  132. struct spmi_pmic_arb {
  133. void __iomem *rd_base;
  134. void __iomem *wr_base;
  135. void __iomem *intr;
  136. void __iomem *cnfg;
  137. void __iomem *core;
  138. resource_size_t core_size;
  139. raw_spinlock_t lock;
  140. u8 channel;
  141. int irq;
  142. u8 ee;
  143. u16 min_apid;
  144. u16 max_apid;
  145. u32 *mapping_table;
  146. DECLARE_BITMAP(mapping_table_valid, PMIC_ARB_MAX_PERIPHS);
  147. struct irq_domain *domain;
  148. struct spmi_controller *spmic;
  149. const struct pmic_arb_ver_ops *ver_ops;
  150. u16 *ppid_to_apid;
  151. u16 last_apid;
  152. struct apid_data apid_data[PMIC_ARB_MAX_PERIPHS];
  153. };
  154. /**
  155. * pmic_arb_ver: version dependent functionality.
  156. *
  157. * @ver_str: version string.
  158. * @ppid_to_apid: finds the apid for a given ppid.
  159. * @non_data_cmd: on v1 issues an spmi non-data command.
  160. * on v2 no HW support, returns -EOPNOTSUPP.
  161. * @offset: on v1 offset of per-ee channel.
  162. * on v2 offset of per-ee and per-ppid channel.
  163. * @fmt_cmd: formats a GENI/SPMI command.
  164. * @owner_acc_status: on v1 address of PMIC_ARB_SPMI_PIC_OWNERm_ACC_STATUSn
  165. * on v2 address of SPMI_PIC_OWNERm_ACC_STATUSn.
  166. * @acc_enable: on v1 address of PMIC_ARB_SPMI_PIC_ACC_ENABLEn
  167. * on v2 address of SPMI_PIC_ACC_ENABLEn.
  168. * @irq_status: on v1 address of PMIC_ARB_SPMI_PIC_IRQ_STATUSn
  169. * on v2 address of SPMI_PIC_IRQ_STATUSn.
  170. * @irq_clear: on v1 address of PMIC_ARB_SPMI_PIC_IRQ_CLEARn
  171. * on v2 address of SPMI_PIC_IRQ_CLEARn.
  172. * @apid_map_offset: offset of PMIC_ARB_REG_CHNLn
  173. */
  174. struct pmic_arb_ver_ops {
  175. const char *ver_str;
  176. int (*ppid_to_apid)(struct spmi_pmic_arb *pmic_arb, u16 ppid);
  177. /* spmi commands (read_cmd, write_cmd, cmd) functionality */
  178. int (*offset)(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
  179. enum pmic_arb_channel ch_type);
  180. u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
  181. int (*non_data_cmd)(struct spmi_controller *ctrl, u8 opc, u8 sid);
  182. /* Interrupts controller functionality (offset of PIC registers) */
  183. void __iomem *(*owner_acc_status)(struct spmi_pmic_arb *pmic_arb, u8 m,
  184. u16 n);
  185. void __iomem *(*acc_enable)(struct spmi_pmic_arb *pmic_arb, u16 n);
  186. void __iomem *(*irq_status)(struct spmi_pmic_arb *pmic_arb, u16 n);
  187. void __iomem *(*irq_clear)(struct spmi_pmic_arb *pmic_arb, u16 n);
  188. u32 (*apid_map_offset)(u16 n);
  189. };
  190. static inline void pmic_arb_base_write(struct spmi_pmic_arb *pmic_arb,
  191. u32 offset, u32 val)
  192. {
  193. writel_relaxed(val, pmic_arb->wr_base + offset);
  194. }
  195. static inline void pmic_arb_set_rd_cmd(struct spmi_pmic_arb *pmic_arb,
  196. u32 offset, u32 val)
  197. {
  198. writel_relaxed(val, pmic_arb->rd_base + offset);
  199. }
  200. /**
  201. * pmic_arb_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
  202. * @bc: byte count -1. range: 0..3
  203. * @reg: register's address
  204. * @buf: output parameter, length must be bc + 1
  205. */
  206. static void
  207. pmic_arb_read_data(struct spmi_pmic_arb *pmic_arb, u8 *buf, u32 reg, u8 bc)
  208. {
  209. u32 data = __raw_readl(pmic_arb->rd_base + reg);
  210. memcpy(buf, &data, (bc & 3) + 1);
  211. }
  212. /**
  213. * pmic_arb_write_data: write 1..4 bytes from buf to pmic-arb's register
  214. * @bc: byte-count -1. range: 0..3.
  215. * @reg: register's address.
  216. * @buf: buffer to write. length must be bc + 1.
  217. */
  218. static void pmic_arb_write_data(struct spmi_pmic_arb *pmic_arb, const u8 *buf,
  219. u32 reg, u8 bc)
  220. {
  221. u32 data = 0;
  222. memcpy(&data, buf, (bc & 3) + 1);
  223. __raw_writel(data, pmic_arb->wr_base + reg);
  224. }
  225. static int pmic_arb_wait_for_done(struct spmi_controller *ctrl,
  226. void __iomem *base, u8 sid, u16 addr,
  227. enum pmic_arb_channel ch_type)
  228. {
  229. struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
  230. u32 status = 0;
  231. u32 timeout = PMIC_ARB_TIMEOUT_US;
  232. u32 offset;
  233. int rc;
  234. rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr, ch_type);
  235. if (rc < 0)
  236. return rc;
  237. offset = rc;
  238. offset += PMIC_ARB_STATUS;
  239. while (timeout--) {
  240. status = readl_relaxed(base + offset);
  241. if (status & PMIC_ARB_STATUS_DONE) {
  242. if (status & PMIC_ARB_STATUS_DENIED) {
  243. dev_err(&ctrl->dev, "%s: transaction denied (0x%x)\n",
  244. __func__, status);
  245. return -EPERM;
  246. }
  247. if (status & PMIC_ARB_STATUS_FAILURE) {
  248. dev_err(&ctrl->dev, "%s: transaction failed (0x%x)\n",
  249. __func__, status);
  250. return -EIO;
  251. }
  252. if (status & PMIC_ARB_STATUS_DROPPED) {
  253. dev_err(&ctrl->dev, "%s: transaction dropped (0x%x)\n",
  254. __func__, status);
  255. return -EIO;
  256. }
  257. return 0;
  258. }
  259. udelay(1);
  260. }
  261. dev_err(&ctrl->dev, "%s: timeout, status 0x%x\n",
  262. __func__, status);
  263. return -ETIMEDOUT;
  264. }
  265. static int
  266. pmic_arb_non_data_cmd_v1(struct spmi_controller *ctrl, u8 opc, u8 sid)
  267. {
  268. struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
  269. unsigned long flags;
  270. u32 cmd;
  271. int rc;
  272. u32 offset;
  273. rc = pmic_arb->ver_ops->offset(pmic_arb, sid, 0, PMIC_ARB_CHANNEL_RW);
  274. if (rc < 0)
  275. return rc;
  276. offset = rc;
  277. cmd = ((opc | 0x40) << 27) | ((sid & 0xf) << 20);
  278. raw_spin_lock_irqsave(&pmic_arb->lock, flags);
  279. pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
  280. rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, 0,
  281. PMIC_ARB_CHANNEL_RW);
  282. raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
  283. return rc;
  284. }
  285. static int
  286. pmic_arb_non_data_cmd_v2(struct spmi_controller *ctrl, u8 opc, u8 sid)
  287. {
  288. return -EOPNOTSUPP;
  289. }
  290. /* Non-data command */
  291. static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
  292. {
  293. struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
  294. dev_dbg(&ctrl->dev, "cmd op:0x%x sid:%d\n", opc, sid);
  295. /* Check for valid non-data command */
  296. if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
  297. return -EINVAL;
  298. return pmic_arb->ver_ops->non_data_cmd(ctrl, opc, sid);
  299. }
  300. static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  301. u16 addr, u8 *buf, size_t len)
  302. {
  303. struct spmi_pmic_arb *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;
  309. rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr,
  310. PMIC_ARB_CHANNEL_OBS);
  311. if (rc < 0)
  312. return rc;
  313. offset = rc;
  314. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  315. dev_err(&ctrl->dev, "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
  316. PMIC_ARB_MAX_TRANS_BYTES, len);
  317. return -EINVAL;
  318. }
  319. /* Check the opcode */
  320. if (opc >= 0x60 && opc <= 0x7F)
  321. opc = PMIC_ARB_OP_READ;
  322. else if (opc >= 0x20 && opc <= 0x2F)
  323. opc = PMIC_ARB_OP_EXT_READ;
  324. else if (opc >= 0x38 && opc <= 0x3F)
  325. opc = PMIC_ARB_OP_EXT_READL;
  326. else
  327. return -EINVAL;
  328. cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
  329. raw_spin_lock_irqsave(&pmic_arb->lock, flags);
  330. pmic_arb_set_rd_cmd(pmic_arb, offset + PMIC_ARB_CMD, cmd);
  331. rc = pmic_arb_wait_for_done(ctrl, pmic_arb->rd_base, sid, addr,
  332. PMIC_ARB_CHANNEL_OBS);
  333. if (rc)
  334. goto done;
  335. pmic_arb_read_data(pmic_arb, buf, offset + PMIC_ARB_RDATA0,
  336. min_t(u8, bc, 3));
  337. if (bc > 3)
  338. pmic_arb_read_data(pmic_arb, buf + 4, offset + PMIC_ARB_RDATA1,
  339. bc - 4);
  340. done:
  341. raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
  342. return rc;
  343. }
  344. static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  345. u16 addr, const u8 *buf, size_t len)
  346. {
  347. struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
  348. unsigned long flags;
  349. u8 bc = len - 1;
  350. u32 cmd;
  351. int rc;
  352. u32 offset;
  353. rc = pmic_arb->ver_ops->offset(pmic_arb, sid, addr,
  354. PMIC_ARB_CHANNEL_RW);
  355. if (rc < 0)
  356. return rc;
  357. offset = rc;
  358. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  359. dev_err(&ctrl->dev, "pmic-arb supports 1..%d bytes per trans, but:%zu requested",
  360. PMIC_ARB_MAX_TRANS_BYTES, len);
  361. return -EINVAL;
  362. }
  363. /* Check the opcode */
  364. if (opc >= 0x40 && opc <= 0x5F)
  365. opc = PMIC_ARB_OP_WRITE;
  366. else if (opc <= 0x0F)
  367. opc = PMIC_ARB_OP_EXT_WRITE;
  368. else if (opc >= 0x30 && opc <= 0x37)
  369. opc = PMIC_ARB_OP_EXT_WRITEL;
  370. else if (opc >= 0x80)
  371. opc = PMIC_ARB_OP_ZERO_WRITE;
  372. else
  373. return -EINVAL;
  374. cmd = pmic_arb->ver_ops->fmt_cmd(opc, sid, addr, bc);
  375. /* Write data to FIFOs */
  376. raw_spin_lock_irqsave(&pmic_arb->lock, flags);
  377. pmic_arb_write_data(pmic_arb, buf, offset + PMIC_ARB_WDATA0,
  378. min_t(u8, bc, 3));
  379. if (bc > 3)
  380. pmic_arb_write_data(pmic_arb, buf + 4, offset + PMIC_ARB_WDATA1,
  381. bc - 4);
  382. /* Start the transaction */
  383. pmic_arb_base_write(pmic_arb, offset + PMIC_ARB_CMD, cmd);
  384. rc = pmic_arb_wait_for_done(ctrl, pmic_arb->wr_base, sid, addr,
  385. PMIC_ARB_CHANNEL_RW);
  386. raw_spin_unlock_irqrestore(&pmic_arb->lock, flags);
  387. return rc;
  388. }
  389. enum qpnpint_regs {
  390. QPNPINT_REG_RT_STS = 0x10,
  391. QPNPINT_REG_SET_TYPE = 0x11,
  392. QPNPINT_REG_POLARITY_HIGH = 0x12,
  393. QPNPINT_REG_POLARITY_LOW = 0x13,
  394. QPNPINT_REG_LATCHED_CLR = 0x14,
  395. QPNPINT_REG_EN_SET = 0x15,
  396. QPNPINT_REG_EN_CLR = 0x16,
  397. QPNPINT_REG_LATCHED_STS = 0x18,
  398. };
  399. struct spmi_pmic_arb_qpnpint_type {
  400. u8 type; /* 1 -> edge */
  401. u8 polarity_high;
  402. u8 polarity_low;
  403. } __packed;
  404. /* Simplified accessor functions for irqchip callbacks */
  405. static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
  406. size_t len)
  407. {
  408. struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
  409. u8 sid = hwirq_to_sid(d->hwirq);
  410. u8 per = hwirq_to_per(d->hwirq);
  411. if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
  412. (per << 8) + reg, buf, len))
  413. dev_err_ratelimited(&pmic_arb->spmic->dev, "failed irqchip transaction on %x\n",
  414. d->irq);
  415. }
  416. static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
  417. {
  418. struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
  419. u8 sid = hwirq_to_sid(d->hwirq);
  420. u8 per = hwirq_to_per(d->hwirq);
  421. if (pmic_arb_read_cmd(pmic_arb->spmic, SPMI_CMD_EXT_READL, sid,
  422. (per << 8) + reg, buf, len))
  423. dev_err_ratelimited(&pmic_arb->spmic->dev, "failed irqchip transaction on %x\n",
  424. d->irq);
  425. }
  426. static void cleanup_irq(struct spmi_pmic_arb *pmic_arb, u16 apid, int id)
  427. {
  428. u16 ppid = pmic_arb->apid_data[apid].ppid;
  429. u8 sid = ppid >> 8;
  430. u8 per = ppid & 0xFF;
  431. u8 irq_mask = BIT(id);
  432. writel_relaxed(irq_mask, pmic_arb->ver_ops->irq_clear(pmic_arb, apid));
  433. if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
  434. (per << 8) + QPNPINT_REG_LATCHED_CLR, &irq_mask, 1))
  435. dev_err_ratelimited(&pmic_arb->spmic->dev, "failed to ack irq_mask = 0x%x for ppid = %x\n",
  436. irq_mask, ppid);
  437. if (pmic_arb_write_cmd(pmic_arb->spmic, SPMI_CMD_EXT_WRITEL, sid,
  438. (per << 8) + QPNPINT_REG_EN_CLR, &irq_mask, 1))
  439. dev_err_ratelimited(&pmic_arb->spmic->dev, "failed to ack irq_mask = 0x%x for ppid = %x\n",
  440. irq_mask, ppid);
  441. }
  442. static void periph_interrupt(struct spmi_pmic_arb *pmic_arb, u16 apid)
  443. {
  444. unsigned int irq;
  445. u32 status;
  446. int id;
  447. u8 sid = (pmic_arb->apid_data[apid].ppid >> 8) & 0xF;
  448. u8 per = pmic_arb->apid_data[apid].ppid & 0xFF;
  449. status = readl_relaxed(pmic_arb->ver_ops->irq_status(pmic_arb, apid));
  450. while (status) {
  451. id = ffs(status) - 1;
  452. status &= ~BIT(id);
  453. irq = irq_find_mapping(pmic_arb->domain,
  454. spec_to_hwirq(sid, per, id, apid));
  455. if (irq == 0) {
  456. cleanup_irq(pmic_arb, apid, id);
  457. continue;
  458. }
  459. generic_handle_irq(irq);
  460. }
  461. }
  462. static void pmic_arb_chained_irq(struct irq_desc *desc)
  463. {
  464. struct spmi_pmic_arb *pmic_arb = irq_desc_get_handler_data(desc);
  465. const struct pmic_arb_ver_ops *ver_ops = pmic_arb->ver_ops;
  466. struct irq_chip *chip = irq_desc_get_chip(desc);
  467. int first = pmic_arb->min_apid >> 5;
  468. int last = pmic_arb->max_apid >> 5;
  469. u8 ee = pmic_arb->ee;
  470. u32 status, enable;
  471. int i, id, apid;
  472. chained_irq_enter(chip, desc);
  473. for (i = first; i <= last; ++i) {
  474. status = readl_relaxed(
  475. ver_ops->owner_acc_status(pmic_arb, ee, i));
  476. while (status) {
  477. id = ffs(status) - 1;
  478. status &= ~BIT(id);
  479. apid = id + i * 32;
  480. enable = readl_relaxed(
  481. ver_ops->acc_enable(pmic_arb, apid));
  482. if (enable & SPMI_PIC_ACC_ENABLE_BIT)
  483. periph_interrupt(pmic_arb, apid);
  484. }
  485. }
  486. chained_irq_exit(chip, desc);
  487. }
  488. static void qpnpint_irq_ack(struct irq_data *d)
  489. {
  490. struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
  491. u8 irq = hwirq_to_irq(d->hwirq);
  492. u16 apid = hwirq_to_apid(d->hwirq);
  493. u8 data;
  494. writel_relaxed(BIT(irq), pmic_arb->ver_ops->irq_clear(pmic_arb, apid));
  495. data = BIT(irq);
  496. qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
  497. }
  498. static void qpnpint_irq_mask(struct irq_data *d)
  499. {
  500. u8 irq = hwirq_to_irq(d->hwirq);
  501. u8 data = BIT(irq);
  502. qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
  503. }
  504. static void qpnpint_irq_unmask(struct irq_data *d)
  505. {
  506. struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
  507. const struct pmic_arb_ver_ops *ver_ops = pmic_arb->ver_ops;
  508. u8 irq = hwirq_to_irq(d->hwirq);
  509. u16 apid = hwirq_to_apid(d->hwirq);
  510. u8 buf[2];
  511. writel_relaxed(SPMI_PIC_ACC_ENABLE_BIT,
  512. ver_ops->acc_enable(pmic_arb, apid));
  513. qpnpint_spmi_read(d, QPNPINT_REG_EN_SET, &buf[0], 1);
  514. if (!(buf[0] & BIT(irq))) {
  515. /*
  516. * Since the interrupt is currently disabled, write to both the
  517. * LATCHED_CLR and EN_SET registers so that a spurious interrupt
  518. * cannot be triggered when the interrupt is enabled
  519. */
  520. buf[0] = BIT(irq);
  521. buf[1] = BIT(irq);
  522. qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 2);
  523. }
  524. }
  525. static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
  526. {
  527. struct spmi_pmic_arb_qpnpint_type type;
  528. irq_flow_handler_t flow_handler;
  529. u8 irq = hwirq_to_irq(d->hwirq);
  530. qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
  531. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  532. type.type |= BIT(irq);
  533. if (flow_type & IRQF_TRIGGER_RISING)
  534. type.polarity_high |= BIT(irq);
  535. if (flow_type & IRQF_TRIGGER_FALLING)
  536. type.polarity_low |= BIT(irq);
  537. flow_handler = handle_edge_irq;
  538. } else {
  539. if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
  540. (flow_type & (IRQF_TRIGGER_LOW)))
  541. return -EINVAL;
  542. type.type &= ~BIT(irq); /* level trig */
  543. if (flow_type & IRQF_TRIGGER_HIGH)
  544. type.polarity_high |= BIT(irq);
  545. else
  546. type.polarity_low |= BIT(irq);
  547. flow_handler = handle_level_irq;
  548. }
  549. qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
  550. irq_set_handler_locked(d, flow_handler);
  551. return 0;
  552. }
  553. static int qpnpint_irq_set_wake(struct irq_data *d, unsigned int on)
  554. {
  555. struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
  556. return irq_set_irq_wake(pmic_arb->irq, on);
  557. }
  558. static int qpnpint_get_irqchip_state(struct irq_data *d,
  559. enum irqchip_irq_state which,
  560. bool *state)
  561. {
  562. u8 irq = hwirq_to_irq(d->hwirq);
  563. u8 status = 0;
  564. if (which != IRQCHIP_STATE_LINE_LEVEL)
  565. return -EINVAL;
  566. qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
  567. *state = !!(status & BIT(irq));
  568. return 0;
  569. }
  570. static int qpnpint_irq_request_resources(struct irq_data *d)
  571. {
  572. struct spmi_pmic_arb *pmic_arb = irq_data_get_irq_chip_data(d);
  573. u16 periph = hwirq_to_per(d->hwirq);
  574. u16 apid = hwirq_to_apid(d->hwirq);
  575. u16 sid = hwirq_to_sid(d->hwirq);
  576. u16 irq = hwirq_to_irq(d->hwirq);
  577. if (pmic_arb->apid_data[apid].irq_ee != pmic_arb->ee) {
  578. dev_err(&pmic_arb->spmic->dev, "failed to xlate sid = %#x, periph = %#x, irq = %u: ee=%u but owner=%u\n",
  579. sid, periph, irq, pmic_arb->ee,
  580. pmic_arb->apid_data[apid].irq_ee);
  581. return -ENODEV;
  582. }
  583. return 0;
  584. }
  585. static struct irq_chip pmic_arb_irqchip = {
  586. .name = "pmic_arb",
  587. .irq_ack = qpnpint_irq_ack,
  588. .irq_mask = qpnpint_irq_mask,
  589. .irq_unmask = qpnpint_irq_unmask,
  590. .irq_set_type = qpnpint_irq_set_type,
  591. .irq_set_wake = qpnpint_irq_set_wake,
  592. .irq_get_irqchip_state = qpnpint_get_irqchip_state,
  593. .irq_request_resources = qpnpint_irq_request_resources,
  594. .flags = IRQCHIP_MASK_ON_SUSPEND,
  595. };
  596. static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
  597. struct device_node *controller,
  598. const u32 *intspec,
  599. unsigned int intsize,
  600. unsigned long *out_hwirq,
  601. unsigned int *out_type)
  602. {
  603. struct spmi_pmic_arb *pmic_arb = d->host_data;
  604. u16 apid, ppid;
  605. int rc;
  606. dev_dbg(&pmic_arb->spmic->dev, "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
  607. intspec[0], intspec[1], intspec[2]);
  608. if (irq_domain_get_of_node(d) != controller)
  609. return -EINVAL;
  610. if (intsize != 4)
  611. return -EINVAL;
  612. if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
  613. return -EINVAL;
  614. ppid = intspec[0] << 8 | intspec[1];
  615. rc = pmic_arb->ver_ops->ppid_to_apid(pmic_arb, ppid);
  616. if (rc < 0) {
  617. dev_err(&pmic_arb->spmic->dev, "failed to xlate sid = %#x, periph = %#x, irq = %u rc = %d\n",
  618. intspec[0], intspec[1], intspec[2], rc);
  619. return rc;
  620. }
  621. apid = rc;
  622. /* Keep track of {max,min}_apid for bounding search during interrupt */
  623. if (apid > pmic_arb->max_apid)
  624. pmic_arb->max_apid = apid;
  625. if (apid < pmic_arb->min_apid)
  626. pmic_arb->min_apid = apid;
  627. *out_hwirq = spec_to_hwirq(intspec[0], intspec[1], intspec[2], apid);
  628. *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
  629. dev_dbg(&pmic_arb->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
  630. return 0;
  631. }
  632. static int qpnpint_irq_domain_map(struct irq_domain *d,
  633. unsigned int virq,
  634. irq_hw_number_t hwirq)
  635. {
  636. struct spmi_pmic_arb *pmic_arb = d->host_data;
  637. dev_dbg(&pmic_arb->spmic->dev, "virq = %u, hwirq = %lu\n", virq, hwirq);
  638. irq_set_chip_and_handler(virq, &pmic_arb_irqchip, handle_level_irq);
  639. irq_set_chip_data(virq, d->host_data);
  640. irq_set_noprobe(virq);
  641. return 0;
  642. }
  643. static int pmic_arb_ppid_to_apid_v1(struct spmi_pmic_arb *pmic_arb, u16 ppid)
  644. {
  645. u32 *mapping_table = pmic_arb->mapping_table;
  646. int index = 0, i;
  647. u16 apid_valid;
  648. u16 apid;
  649. u32 data;
  650. apid_valid = pmic_arb->ppid_to_apid[ppid];
  651. if (apid_valid & PMIC_ARB_APID_VALID) {
  652. apid = apid_valid & ~PMIC_ARB_APID_VALID;
  653. return apid;
  654. }
  655. for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
  656. if (!test_and_set_bit(index, pmic_arb->mapping_table_valid))
  657. mapping_table[index] = readl_relaxed(pmic_arb->cnfg +
  658. SPMI_MAPPING_TABLE_REG(index));
  659. data = mapping_table[index];
  660. if (ppid & BIT(SPMI_MAPPING_BIT_INDEX(data))) {
  661. if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
  662. index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  663. } else {
  664. apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  665. pmic_arb->ppid_to_apid[ppid]
  666. = apid | PMIC_ARB_APID_VALID;
  667. pmic_arb->apid_data[apid].ppid = ppid;
  668. return apid;
  669. }
  670. } else {
  671. if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
  672. index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  673. } else {
  674. apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  675. pmic_arb->ppid_to_apid[ppid]
  676. = apid | PMIC_ARB_APID_VALID;
  677. pmic_arb->apid_data[apid].ppid = ppid;
  678. return apid;
  679. }
  680. }
  681. }
  682. return -ENODEV;
  683. }
  684. /* v1 offset per ee */
  685. static int pmic_arb_offset_v1(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
  686. enum pmic_arb_channel ch_type)
  687. {
  688. return 0x800 + 0x80 * pmic_arb->channel;
  689. }
  690. static u16 pmic_arb_find_apid(struct spmi_pmic_arb *pmic_arb, u16 ppid)
  691. {
  692. struct apid_data *apidd = &pmic_arb->apid_data[pmic_arb->last_apid];
  693. u32 regval, offset;
  694. u16 id, apid;
  695. for (apid = pmic_arb->last_apid; ; apid++, apidd++) {
  696. offset = pmic_arb->ver_ops->apid_map_offset(apid);
  697. if (offset >= pmic_arb->core_size)
  698. break;
  699. regval = readl_relaxed(pmic_arb->cnfg +
  700. SPMI_OWNERSHIP_TABLE_REG(apid));
  701. apidd->irq_ee = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
  702. apidd->write_ee = apidd->irq_ee;
  703. regval = readl_relaxed(pmic_arb->core + offset);
  704. if (!regval)
  705. continue;
  706. id = (regval >> 8) & PMIC_ARB_PPID_MASK;
  707. pmic_arb->ppid_to_apid[id] = apid | PMIC_ARB_APID_VALID;
  708. apidd->ppid = id;
  709. if (id == ppid) {
  710. apid |= PMIC_ARB_APID_VALID;
  711. break;
  712. }
  713. }
  714. pmic_arb->last_apid = apid & ~PMIC_ARB_APID_VALID;
  715. return apid;
  716. }
  717. static int pmic_arb_ppid_to_apid_v2(struct spmi_pmic_arb *pmic_arb, u16 ppid)
  718. {
  719. u16 apid_valid;
  720. apid_valid = pmic_arb->ppid_to_apid[ppid];
  721. if (!(apid_valid & PMIC_ARB_APID_VALID))
  722. apid_valid = pmic_arb_find_apid(pmic_arb, ppid);
  723. if (!(apid_valid & PMIC_ARB_APID_VALID))
  724. return -ENODEV;
  725. return apid_valid & ~PMIC_ARB_APID_VALID;
  726. }
  727. static int pmic_arb_read_apid_map_v5(struct spmi_pmic_arb *pmic_arb)
  728. {
  729. struct apid_data *apidd = pmic_arb->apid_data;
  730. struct apid_data *prev_apidd;
  731. u16 i, apid, ppid;
  732. bool valid, is_irq_ee;
  733. u32 regval, offset;
  734. /*
  735. * In order to allow multiple EEs to write to a single PPID in arbiter
  736. * version 5, there is more than one APID mapped to each PPID.
  737. * The owner field for each of these mappings specifies the EE which is
  738. * allowed to write to the APID. The owner of the last (highest) APID
  739. * for a given PPID will receive interrupts from the PPID.
  740. */
  741. for (i = 0; ; i++, apidd++) {
  742. offset = pmic_arb->ver_ops->apid_map_offset(i);
  743. if (offset >= pmic_arb->core_size)
  744. break;
  745. regval = readl_relaxed(pmic_arb->core + offset);
  746. if (!regval)
  747. continue;
  748. ppid = (regval >> 8) & PMIC_ARB_PPID_MASK;
  749. is_irq_ee = PMIC_ARB_CHAN_IS_IRQ_OWNER(regval);
  750. regval = readl_relaxed(pmic_arb->cnfg +
  751. SPMI_OWNERSHIP_TABLE_REG(i));
  752. apidd->write_ee = SPMI_OWNERSHIP_PERIPH2OWNER(regval);
  753. apidd->irq_ee = is_irq_ee ? apidd->write_ee : INVALID_EE;
  754. valid = pmic_arb->ppid_to_apid[ppid] & PMIC_ARB_APID_VALID;
  755. apid = pmic_arb->ppid_to_apid[ppid] & ~PMIC_ARB_APID_VALID;
  756. prev_apidd = &pmic_arb->apid_data[apid];
  757. if (valid && is_irq_ee &&
  758. prev_apidd->write_ee == pmic_arb->ee) {
  759. /*
  760. * Duplicate PPID mapping after the one for this EE;
  761. * override the irq owner
  762. */
  763. prev_apidd->irq_ee = apidd->irq_ee;
  764. } else if (!valid || is_irq_ee) {
  765. /* First PPID mapping or duplicate for another EE */
  766. pmic_arb->ppid_to_apid[ppid] = i | PMIC_ARB_APID_VALID;
  767. }
  768. apidd->ppid = ppid;
  769. pmic_arb->last_apid = i;
  770. }
  771. /* Dump the mapping table for debug purposes. */
  772. dev_dbg(&pmic_arb->spmic->dev, "PPID APID Write-EE IRQ-EE\n");
  773. for (ppid = 0; ppid < PMIC_ARB_MAX_PPID; ppid++) {
  774. apid = pmic_arb->ppid_to_apid[ppid];
  775. if (apid & PMIC_ARB_APID_VALID) {
  776. apid &= ~PMIC_ARB_APID_VALID;
  777. apidd = &pmic_arb->apid_data[apid];
  778. dev_dbg(&pmic_arb->spmic->dev, "%#03X %3u %2u %2u\n",
  779. ppid, apid, apidd->write_ee, apidd->irq_ee);
  780. }
  781. }
  782. return 0;
  783. }
  784. static int pmic_arb_ppid_to_apid_v5(struct spmi_pmic_arb *pmic_arb, u16 ppid)
  785. {
  786. if (!(pmic_arb->ppid_to_apid[ppid] & PMIC_ARB_APID_VALID))
  787. return -ENODEV;
  788. return pmic_arb->ppid_to_apid[ppid] & ~PMIC_ARB_APID_VALID;
  789. }
  790. /* v2 offset per ppid and per ee */
  791. static int pmic_arb_offset_v2(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
  792. enum pmic_arb_channel ch_type)
  793. {
  794. u16 apid;
  795. u16 ppid;
  796. int rc;
  797. ppid = sid << 8 | ((addr >> 8) & 0xFF);
  798. rc = pmic_arb_ppid_to_apid_v2(pmic_arb, ppid);
  799. if (rc < 0)
  800. return rc;
  801. apid = rc;
  802. return 0x1000 * pmic_arb->ee + 0x8000 * apid;
  803. }
  804. /*
  805. * v5 offset per ee and per apid for observer channels and per apid for
  806. * read/write channels.
  807. */
  808. static int pmic_arb_offset_v5(struct spmi_pmic_arb *pmic_arb, u8 sid, u16 addr,
  809. enum pmic_arb_channel ch_type)
  810. {
  811. u16 apid;
  812. int rc;
  813. u32 offset = 0;
  814. u16 ppid = (sid << 8) | (addr >> 8);
  815. rc = pmic_arb_ppid_to_apid_v5(pmic_arb, ppid);
  816. if (rc < 0)
  817. return rc;
  818. apid = rc;
  819. switch (ch_type) {
  820. case PMIC_ARB_CHANNEL_OBS:
  821. offset = 0x10000 * pmic_arb->ee + 0x80 * apid;
  822. break;
  823. case PMIC_ARB_CHANNEL_RW:
  824. offset = 0x10000 * apid;
  825. break;
  826. }
  827. return offset;
  828. }
  829. static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
  830. {
  831. return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
  832. }
  833. static u32 pmic_arb_fmt_cmd_v2(u8 opc, u8 sid, u16 addr, u8 bc)
  834. {
  835. return (opc << 27) | ((addr & 0xff) << 4) | (bc & 0x7);
  836. }
  837. static void __iomem *
  838. pmic_arb_owner_acc_status_v1(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
  839. {
  840. return pmic_arb->intr + 0x20 * m + 0x4 * n;
  841. }
  842. static void __iomem *
  843. pmic_arb_owner_acc_status_v2(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
  844. {
  845. return pmic_arb->intr + 0x100000 + 0x1000 * m + 0x4 * n;
  846. }
  847. static void __iomem *
  848. pmic_arb_owner_acc_status_v3(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
  849. {
  850. return pmic_arb->intr + 0x200000 + 0x1000 * m + 0x4 * n;
  851. }
  852. static void __iomem *
  853. pmic_arb_owner_acc_status_v5(struct spmi_pmic_arb *pmic_arb, u8 m, u16 n)
  854. {
  855. return pmic_arb->intr + 0x10000 * m + 0x4 * n;
  856. }
  857. static void __iomem *
  858. pmic_arb_acc_enable_v1(struct spmi_pmic_arb *pmic_arb, u16 n)
  859. {
  860. return pmic_arb->intr + 0x200 + 0x4 * n;
  861. }
  862. static void __iomem *
  863. pmic_arb_acc_enable_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
  864. {
  865. return pmic_arb->intr + 0x1000 * n;
  866. }
  867. static void __iomem *
  868. pmic_arb_acc_enable_v5(struct spmi_pmic_arb *pmic_arb, u16 n)
  869. {
  870. return pmic_arb->wr_base + 0x100 + 0x10000 * n;
  871. }
  872. static void __iomem *
  873. pmic_arb_irq_status_v1(struct spmi_pmic_arb *pmic_arb, u16 n)
  874. {
  875. return pmic_arb->intr + 0x600 + 0x4 * n;
  876. }
  877. static void __iomem *
  878. pmic_arb_irq_status_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
  879. {
  880. return pmic_arb->intr + 0x4 + 0x1000 * n;
  881. }
  882. static void __iomem *
  883. pmic_arb_irq_status_v5(struct spmi_pmic_arb *pmic_arb, u16 n)
  884. {
  885. return pmic_arb->wr_base + 0x104 + 0x10000 * n;
  886. }
  887. static void __iomem *
  888. pmic_arb_irq_clear_v1(struct spmi_pmic_arb *pmic_arb, u16 n)
  889. {
  890. return pmic_arb->intr + 0xA00 + 0x4 * n;
  891. }
  892. static void __iomem *
  893. pmic_arb_irq_clear_v2(struct spmi_pmic_arb *pmic_arb, u16 n)
  894. {
  895. return pmic_arb->intr + 0x8 + 0x1000 * n;
  896. }
  897. static void __iomem *
  898. pmic_arb_irq_clear_v5(struct spmi_pmic_arb *pmic_arb, u16 n)
  899. {
  900. return pmic_arb->wr_base + 0x108 + 0x10000 * n;
  901. }
  902. static u32 pmic_arb_apid_map_offset_v2(u16 n)
  903. {
  904. return 0x800 + 0x4 * n;
  905. }
  906. static u32 pmic_arb_apid_map_offset_v5(u16 n)
  907. {
  908. return 0x900 + 0x4 * n;
  909. }
  910. static const struct pmic_arb_ver_ops pmic_arb_v1 = {
  911. .ver_str = "v1",
  912. .ppid_to_apid = pmic_arb_ppid_to_apid_v1,
  913. .non_data_cmd = pmic_arb_non_data_cmd_v1,
  914. .offset = pmic_arb_offset_v1,
  915. .fmt_cmd = pmic_arb_fmt_cmd_v1,
  916. .owner_acc_status = pmic_arb_owner_acc_status_v1,
  917. .acc_enable = pmic_arb_acc_enable_v1,
  918. .irq_status = pmic_arb_irq_status_v1,
  919. .irq_clear = pmic_arb_irq_clear_v1,
  920. .apid_map_offset = pmic_arb_apid_map_offset_v2,
  921. };
  922. static const struct pmic_arb_ver_ops pmic_arb_v2 = {
  923. .ver_str = "v2",
  924. .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
  925. .non_data_cmd = pmic_arb_non_data_cmd_v2,
  926. .offset = pmic_arb_offset_v2,
  927. .fmt_cmd = pmic_arb_fmt_cmd_v2,
  928. .owner_acc_status = pmic_arb_owner_acc_status_v2,
  929. .acc_enable = pmic_arb_acc_enable_v2,
  930. .irq_status = pmic_arb_irq_status_v2,
  931. .irq_clear = pmic_arb_irq_clear_v2,
  932. .apid_map_offset = pmic_arb_apid_map_offset_v2,
  933. };
  934. static const struct pmic_arb_ver_ops pmic_arb_v3 = {
  935. .ver_str = "v3",
  936. .ppid_to_apid = pmic_arb_ppid_to_apid_v2,
  937. .non_data_cmd = pmic_arb_non_data_cmd_v2,
  938. .offset = pmic_arb_offset_v2,
  939. .fmt_cmd = pmic_arb_fmt_cmd_v2,
  940. .owner_acc_status = pmic_arb_owner_acc_status_v3,
  941. .acc_enable = pmic_arb_acc_enable_v2,
  942. .irq_status = pmic_arb_irq_status_v2,
  943. .irq_clear = pmic_arb_irq_clear_v2,
  944. .apid_map_offset = pmic_arb_apid_map_offset_v2,
  945. };
  946. static const struct pmic_arb_ver_ops pmic_arb_v5 = {
  947. .ver_str = "v5",
  948. .ppid_to_apid = pmic_arb_ppid_to_apid_v5,
  949. .non_data_cmd = pmic_arb_non_data_cmd_v2,
  950. .offset = pmic_arb_offset_v5,
  951. .fmt_cmd = pmic_arb_fmt_cmd_v2,
  952. .owner_acc_status = pmic_arb_owner_acc_status_v5,
  953. .acc_enable = pmic_arb_acc_enable_v5,
  954. .irq_status = pmic_arb_irq_status_v5,
  955. .irq_clear = pmic_arb_irq_clear_v5,
  956. .apid_map_offset = pmic_arb_apid_map_offset_v5,
  957. };
  958. static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
  959. .map = qpnpint_irq_domain_map,
  960. .xlate = qpnpint_irq_domain_dt_translate,
  961. };
  962. static int spmi_pmic_arb_probe(struct platform_device *pdev)
  963. {
  964. struct spmi_pmic_arb *pmic_arb;
  965. struct spmi_controller *ctrl;
  966. struct resource *res;
  967. void __iomem *core;
  968. u32 *mapping_table;
  969. u32 channel, ee, hw_ver;
  970. int err;
  971. ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*pmic_arb));
  972. if (!ctrl)
  973. return -ENOMEM;
  974. pmic_arb = spmi_controller_get_drvdata(ctrl);
  975. pmic_arb->spmic = ctrl;
  976. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
  977. core = devm_ioremap_resource(&ctrl->dev, res);
  978. if (IS_ERR(core)) {
  979. err = PTR_ERR(core);
  980. goto err_put_ctrl;
  981. }
  982. pmic_arb->core_size = resource_size(res);
  983. pmic_arb->ppid_to_apid = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PPID,
  984. sizeof(*pmic_arb->ppid_to_apid),
  985. GFP_KERNEL);
  986. if (!pmic_arb->ppid_to_apid) {
  987. err = -ENOMEM;
  988. goto err_put_ctrl;
  989. }
  990. hw_ver = readl_relaxed(core + PMIC_ARB_VERSION);
  991. if (hw_ver < PMIC_ARB_VERSION_V2_MIN) {
  992. pmic_arb->ver_ops = &pmic_arb_v1;
  993. pmic_arb->wr_base = core;
  994. pmic_arb->rd_base = core;
  995. } else {
  996. pmic_arb->core = core;
  997. if (hw_ver < PMIC_ARB_VERSION_V3_MIN)
  998. pmic_arb->ver_ops = &pmic_arb_v2;
  999. else if (hw_ver < PMIC_ARB_VERSION_V5_MIN)
  1000. pmic_arb->ver_ops = &pmic_arb_v3;
  1001. else
  1002. pmic_arb->ver_ops = &pmic_arb_v5;
  1003. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1004. "obsrvr");
  1005. pmic_arb->rd_base = devm_ioremap_resource(&ctrl->dev, res);
  1006. if (IS_ERR(pmic_arb->rd_base)) {
  1007. err = PTR_ERR(pmic_arb->rd_base);
  1008. goto err_put_ctrl;
  1009. }
  1010. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1011. "chnls");
  1012. pmic_arb->wr_base = devm_ioremap_resource(&ctrl->dev, res);
  1013. if (IS_ERR(pmic_arb->wr_base)) {
  1014. err = PTR_ERR(pmic_arb->wr_base);
  1015. goto err_put_ctrl;
  1016. }
  1017. }
  1018. dev_info(&ctrl->dev, "PMIC arbiter version %s (0x%x)\n",
  1019. pmic_arb->ver_ops->ver_str, hw_ver);
  1020. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
  1021. pmic_arb->intr = devm_ioremap_resource(&ctrl->dev, res);
  1022. if (IS_ERR(pmic_arb->intr)) {
  1023. err = PTR_ERR(pmic_arb->intr);
  1024. goto err_put_ctrl;
  1025. }
  1026. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
  1027. pmic_arb->cnfg = devm_ioremap_resource(&ctrl->dev, res);
  1028. if (IS_ERR(pmic_arb->cnfg)) {
  1029. err = PTR_ERR(pmic_arb->cnfg);
  1030. goto err_put_ctrl;
  1031. }
  1032. pmic_arb->irq = platform_get_irq_byname(pdev, "periph_irq");
  1033. if (pmic_arb->irq < 0) {
  1034. err = pmic_arb->irq;
  1035. goto err_put_ctrl;
  1036. }
  1037. err = of_property_read_u32(pdev->dev.of_node, "qcom,channel", &channel);
  1038. if (err) {
  1039. dev_err(&pdev->dev, "channel unspecified.\n");
  1040. goto err_put_ctrl;
  1041. }
  1042. if (channel > 5) {
  1043. dev_err(&pdev->dev, "invalid channel (%u) specified.\n",
  1044. channel);
  1045. err = -EINVAL;
  1046. goto err_put_ctrl;
  1047. }
  1048. pmic_arb->channel = channel;
  1049. err = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &ee);
  1050. if (err) {
  1051. dev_err(&pdev->dev, "EE unspecified.\n");
  1052. goto err_put_ctrl;
  1053. }
  1054. if (ee > 5) {
  1055. dev_err(&pdev->dev, "invalid EE (%u) specified\n", ee);
  1056. err = -EINVAL;
  1057. goto err_put_ctrl;
  1058. }
  1059. pmic_arb->ee = ee;
  1060. mapping_table = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PERIPHS,
  1061. sizeof(*mapping_table), GFP_KERNEL);
  1062. if (!mapping_table) {
  1063. err = -ENOMEM;
  1064. goto err_put_ctrl;
  1065. }
  1066. pmic_arb->mapping_table = mapping_table;
  1067. /* Initialize max_apid/min_apid to the opposite bounds, during
  1068. * the irq domain translation, we are sure to update these */
  1069. pmic_arb->max_apid = 0;
  1070. pmic_arb->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
  1071. platform_set_drvdata(pdev, ctrl);
  1072. raw_spin_lock_init(&pmic_arb->lock);
  1073. ctrl->cmd = pmic_arb_cmd;
  1074. ctrl->read_cmd = pmic_arb_read_cmd;
  1075. ctrl->write_cmd = pmic_arb_write_cmd;
  1076. if (hw_ver >= PMIC_ARB_VERSION_V5_MIN) {
  1077. err = pmic_arb_read_apid_map_v5(pmic_arb);
  1078. if (err) {
  1079. dev_err(&pdev->dev, "could not read APID->PPID mapping table, rc= %d\n",
  1080. err);
  1081. goto err_put_ctrl;
  1082. }
  1083. }
  1084. dev_dbg(&pdev->dev, "adding irq domain\n");
  1085. pmic_arb->domain = irq_domain_add_tree(pdev->dev.of_node,
  1086. &pmic_arb_irq_domain_ops, pmic_arb);
  1087. if (!pmic_arb->domain) {
  1088. dev_err(&pdev->dev, "unable to create irq_domain\n");
  1089. err = -ENOMEM;
  1090. goto err_put_ctrl;
  1091. }
  1092. irq_set_chained_handler_and_data(pmic_arb->irq, pmic_arb_chained_irq,
  1093. pmic_arb);
  1094. err = spmi_controller_add(ctrl);
  1095. if (err)
  1096. goto err_domain_remove;
  1097. return 0;
  1098. err_domain_remove:
  1099. irq_set_chained_handler_and_data(pmic_arb->irq, NULL, NULL);
  1100. irq_domain_remove(pmic_arb->domain);
  1101. err_put_ctrl:
  1102. spmi_controller_put(ctrl);
  1103. return err;
  1104. }
  1105. static int spmi_pmic_arb_remove(struct platform_device *pdev)
  1106. {
  1107. struct spmi_controller *ctrl = platform_get_drvdata(pdev);
  1108. struct spmi_pmic_arb *pmic_arb = spmi_controller_get_drvdata(ctrl);
  1109. spmi_controller_remove(ctrl);
  1110. irq_set_chained_handler_and_data(pmic_arb->irq, NULL, NULL);
  1111. irq_domain_remove(pmic_arb->domain);
  1112. spmi_controller_put(ctrl);
  1113. return 0;
  1114. }
  1115. static const struct of_device_id spmi_pmic_arb_match_table[] = {
  1116. { .compatible = "qcom,spmi-pmic-arb", },
  1117. {},
  1118. };
  1119. MODULE_DEVICE_TABLE(of, spmi_pmic_arb_match_table);
  1120. static struct platform_driver spmi_pmic_arb_driver = {
  1121. .probe = spmi_pmic_arb_probe,
  1122. .remove = spmi_pmic_arb_remove,
  1123. .driver = {
  1124. .name = "spmi_pmic_arb",
  1125. .of_match_table = spmi_pmic_arb_match_table,
  1126. },
  1127. };
  1128. module_platform_driver(spmi_pmic_arb_driver);
  1129. MODULE_LICENSE("GPL v2");
  1130. MODULE_ALIAS("platform:spmi_pmic_arb");