viospmi-pmic-arb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved. */
  3. #include <linux/bitmap.h>
  4. #include <linux/delay.h>
  5. #include <linux/err.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/irqchip/chained_irq.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/irq.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/spmi.h>
  18. #include <linux/mutex.h>
  19. #include <linux/virtio.h>
  20. #include <linux/virtio_spmi.h>
  21. #include <linux/scatterlist.h>
  22. /* Virtio ID of SPMI : 0xC003 */
  23. #define VIRTIO_ID_SPMI 49155
  24. /* Mapping Table */
  25. #define PMIC_ARB_MAX_PPID BIT(12) /* PPID is 12bit */
  26. #define PMIC_ARB_APID_VALID BIT(15)
  27. /* type and subtype registers base address offsets */
  28. #define PMIC_GPIO_REG_TYPE 0x4
  29. #define PMIC_GPIO_REG_SUBTYPE 0x5
  30. /* GPIO peripheral type and subtype out_values */
  31. #define PMIC_GPIO_TYPE 0x10
  32. #define PMIC_GPIO_SUBTYPE_GPIO_4CH 0x1
  33. #define PMIC_GPIO_SUBTYPE_GPIOC_4CH 0x5
  34. #define PMIC_GPIO_SUBTYPE_GPIO_8CH 0x9
  35. #define PMIC_GPIO_SUBTYPE_GPIOC_8CH 0xd
  36. #define PMIC_GPIO_SUBTYPE_GPIO_LV 0x10
  37. #define PMIC_GPIO_SUBTYPE_GPIO_MV 0x11
  38. /* Command Opcodes */
  39. enum pmic_arb_cmd_op_code {
  40. PMIC_ARB_OP_EXT_WRITEL = 0,
  41. PMIC_ARB_OP_EXT_READL = 1,
  42. PMIC_ARB_OP_EXT_WRITE = 2,
  43. PMIC_ARB_OP_RESET = 3,
  44. PMIC_ARB_OP_SLEEP = 4,
  45. PMIC_ARB_OP_SHUTDOWN = 5,
  46. PMIC_ARB_OP_WAKEUP = 6,
  47. PMIC_ARB_OP_AUTHENTICATE = 7,
  48. PMIC_ARB_OP_MSTR_READ = 8,
  49. PMIC_ARB_OP_MSTR_WRITE = 9,
  50. PMIC_ARB_OP_EXT_READ = 13,
  51. PMIC_ARB_OP_WRITE = 14,
  52. PMIC_ARB_OP_READ = 15,
  53. PMIC_ARB_OP_ZERO_WRITE = 16,
  54. };
  55. /* Maximum number of support PMIC peripherals */
  56. #define PMIC_ARB_MAX_PERIPHS 512
  57. #define PMIC_ARB_MAX_TRANS_BYTES (8)
  58. #define PMIC_ARB_APID_MASK 0xFF
  59. #define PMIC_ARB_PPID_MASK 0xFFF
  60. /* interrupt enable bit */
  61. #define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
  62. #define spec_to_hwirq(slave_id, periph_id, irq_id, apid) \
  63. ((((slave_id) & 0xF) << 28) | \
  64. (((periph_id) & 0xFF) << 20) | \
  65. (((irq_id) & 0x7) << 16) | \
  66. (((apid) & 0x1FF) << 0))
  67. #define hwirq_to_sid(hwirq) (((hwirq) >> 28) & 0xF)
  68. #define hwirq_to_per(hwirq) (((hwirq) >> 20) & 0xFF)
  69. #define hwirq_to_irq(hwirq) (((hwirq) >> 16) & 0x7)
  70. #define hwirq_to_apid(hwirq) (((hwirq) >> 0) & 0x1FF)
  71. struct pmic_arb_ver_ops;
  72. struct apid_data {
  73. u16 ppid;
  74. struct irq_desc *desc;
  75. };
  76. struct virtio_spmi {
  77. struct virtio_device *vdev;
  78. struct virtqueue *txq;
  79. struct virtqueue *rxq;
  80. raw_spinlock_t txlock;
  81. raw_spinlock_t rxlock;
  82. struct spmi_pmic_arb *pa;
  83. struct virtio_spmi_config config;
  84. struct virtio_spmi_msg txmsg;
  85. struct virtio_spmi_msg rxmsgs[4];
  86. };
  87. /**
  88. * spmi_pmic_arb - SPMI PMIC Arbiter object
  89. *
  90. * @irq: PMIC ARB interrupt.
  91. * @ee: the current Execution Environment
  92. * @min_apid: minimum APID (used for bounding IRQ search)
  93. * @max_apid: maximum APID
  94. * @domain: irq domain object for PMIC IRQ domain
  95. * @spmic: SPMI controller object
  96. * @ver_ops: version dependent operations.
  97. * @ppid_to_apid in-memory copy of PPID -> APID mapping table.
  98. */
  99. struct spmi_pmic_arb {
  100. int irq;
  101. u8 ee;
  102. u16 min_apid;
  103. u16 max_apid;
  104. struct irq_domain *domain;
  105. struct spmi_controller *spmic;
  106. struct virtio_spmi *vs;
  107. const struct pmic_arb_ver_ops *ver_ops;
  108. u16 *ppid_to_apid;
  109. struct apid_data apid_data[PMIC_ARB_MAX_PERIPHS];
  110. };
  111. /**
  112. * pmic_arb_ver: version dependent functionality.
  113. *
  114. * @ver_str: version string.
  115. * @ppid_to_apid: finds the apid for a given ppid.
  116. * @fmt_cmd: formats a GENI/SPMI command.
  117. */
  118. struct pmic_arb_ver_ops {
  119. const char *ver_str;
  120. int (*ppid_to_apid)(struct spmi_pmic_arb *pa, u16 ppid);
  121. u32 (*fmt_cmd)(u8 opc, u8 sid, u16 addr, u8 bc);
  122. };
  123. static int
  124. vspmi_pmic_arb_xfer(struct spmi_pmic_arb *pa)
  125. {
  126. struct virtio_spmi *vs = pa->vs;
  127. struct virtio_spmi_msg *msg = &vs->txmsg;
  128. struct virtio_spmi_msg *rsp;
  129. struct scatterlist sg[1];
  130. unsigned int len;
  131. int rc = 0;
  132. sg_init_one(sg, msg, sizeof(*msg));
  133. rc = virtqueue_add_outbuf(vs->txq, sg, 1, msg, GFP_ATOMIC);
  134. if (rc) {
  135. dev_err(&vs->vdev->dev, "fail to add output buffer\n");
  136. goto out;
  137. }
  138. virtqueue_kick(vs->txq);
  139. do {
  140. rsp = virtqueue_get_buf(vs->txq, &len);
  141. } while (!rsp);
  142. rc = virtio32_to_cpu(vs->vdev, rsp->res);
  143. out:
  144. return rc;
  145. }
  146. static struct virtio_spmi_msg *vspmi_fill_txmsg(struct spmi_pmic_arb *pa,
  147. u32 type, u32 cmd, u16 ppid, u32 regval)
  148. {
  149. struct virtio_spmi *vs = pa->vs;
  150. struct virtio_spmi_msg *msg = &vs->txmsg;
  151. memset(msg, 0x0, sizeof(*msg));
  152. if (type > VIO_SPMI_BUS_CMDMAX) {
  153. msg->payload.irqd.ppid =
  154. cpu_to_virtio16(vs->vdev, ppid);
  155. msg->payload.irqd.regval =
  156. cpu_to_virtio32(vs->vdev, regval);
  157. } else {
  158. msg->payload.cmdd.cmd =
  159. cpu_to_virtio32(vs->vdev, cmd);
  160. }
  161. msg->type = cpu_to_virtio32(vs->vdev, type);
  162. return msg;
  163. }
  164. static void vspmi_queue_rxmsg(struct virtio_spmi *vspmi,
  165. struct virtio_spmi_msg *msg)
  166. {
  167. struct scatterlist sg[1];
  168. memset(msg, 0x0, sizeof(*msg));
  169. sg_init_one(sg, msg, sizeof(*msg));
  170. virtqueue_add_inbuf(vspmi->rxq, sg, 1, msg, GFP_ATOMIC);
  171. }
  172. static void vspmi_fill_rxmsgs(struct virtio_spmi *vs)
  173. {
  174. unsigned long flags;
  175. int i, size;
  176. raw_spin_lock_irqsave(&vs->rxlock, flags);
  177. size = virtqueue_get_vring_size(vs->rxq);
  178. if (size > ARRAY_SIZE(vs->rxmsgs))
  179. size = ARRAY_SIZE(vs->rxmsgs);
  180. for (i = 0; i < size; i++)
  181. vspmi_queue_rxmsg(vs, &vs->rxmsgs[i]);
  182. virtqueue_kick(vs->rxq);
  183. raw_spin_unlock_irqrestore(&vs->rxlock, flags);
  184. }
  185. static int pmic_arb_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
  186. u16 addr, u8 *buf, size_t len)
  187. {
  188. struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
  189. struct virtio_spmi *vs = pa->vs;
  190. struct virtio_spmi_msg *msg;
  191. u8 bc = len - 1;
  192. u32 data, cmd;
  193. int rc;
  194. unsigned long flags;
  195. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  196. dev_err(&ctrl->dev,
  197. "pmic-arb supports 1..%d bytes per trans, but:%zu requested\n",
  198. PMIC_ARB_MAX_TRANS_BYTES, len);
  199. return -EINVAL;
  200. }
  201. /* Check the opcode */
  202. if (opc >= 0x60 && opc <= 0x7F)
  203. opc = PMIC_ARB_OP_READ;
  204. else if (opc >= 0x20 && opc <= 0x2F)
  205. opc = PMIC_ARB_OP_EXT_READ;
  206. else if (opc >= 0x38 && opc <= 0x3F)
  207. opc = PMIC_ARB_OP_EXT_READL;
  208. else
  209. return -EINVAL;
  210. cmd = pa->ver_ops->fmt_cmd(opc, sid, addr, bc);
  211. raw_spin_lock_irqsave(&vs->txlock, flags);
  212. msg = vspmi_fill_txmsg(pa, VIO_SPMI_BUS_READ, cmd, 0, 0);
  213. rc = vspmi_pmic_arb_xfer(pa);
  214. if (rc)
  215. goto out;
  216. data = virtio32_to_cpu(vs->vdev,
  217. msg->payload.cmdd.data[0]);
  218. memcpy(buf, &data, (bc & 3) + 1);
  219. if (bc > 3) {
  220. data = virtio32_to_cpu(vs->vdev,
  221. msg->payload.cmdd.data[1]);
  222. memcpy((buf + 4), &data, ((bc - 4) & 3) + 1);
  223. }
  224. out:
  225. raw_spin_unlock_irqrestore(&vs->txlock, flags);
  226. if (rc == EPERM) {
  227. /* spmi bus driver try to read disallowd gpio in probe
  228. * give correct type and subtype and ignore other read command
  229. */
  230. if ((addr & 0xff) == PMIC_GPIO_REG_TYPE && len == 1) {
  231. data = PMIC_GPIO_TYPE;
  232. memcpy(buf, &data, 1);
  233. }
  234. if ((addr & 0xff) == PMIC_GPIO_REG_SUBTYPE && len == 1) {
  235. data = PMIC_GPIO_SUBTYPE_GPIO_LV;
  236. memcpy(buf, &data, 1);
  237. }
  238. rc = 0;
  239. }
  240. return rc;
  241. }
  242. static int pmic_arb_write_cmd(struct spmi_controller *ctrl, u8 opc,
  243. u8 sid, u16 addr, const u8 *buf, size_t len)
  244. {
  245. struct spmi_pmic_arb *pa = spmi_controller_get_drvdata(ctrl);
  246. struct virtio_spmi *vs = pa->vs;
  247. struct virtio_spmi_msg *msg;
  248. u8 bc = len - 1;
  249. u32 data, cmd;
  250. int rc;
  251. unsigned long flags;
  252. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  253. dev_err(&ctrl->dev,
  254. "pmic-arb supports 1..%d bytes per trans, but:%zu requested\n",
  255. PMIC_ARB_MAX_TRANS_BYTES, len);
  256. return -EINVAL;
  257. }
  258. /* Check the opcode */
  259. if (opc >= 0x40 && opc <= 0x5F)
  260. opc = PMIC_ARB_OP_WRITE;
  261. else if (opc <= 0x0F)
  262. opc = PMIC_ARB_OP_EXT_WRITE;
  263. else if (opc >= 0x30 && opc <= 0x37)
  264. opc = PMIC_ARB_OP_EXT_WRITEL;
  265. else if (opc >= 0x80)
  266. opc = PMIC_ARB_OP_ZERO_WRITE;
  267. else
  268. return -EINVAL;
  269. cmd = pa->ver_ops->fmt_cmd(opc, sid, addr, bc);
  270. raw_spin_lock_irqsave(&vs->txlock, flags);
  271. msg = vspmi_fill_txmsg(pa, VIO_SPMI_BUS_WRITE, cmd, 0, 0);
  272. memcpy(&data, buf, (bc & 3) + 1);
  273. msg->payload.cmdd.data[0] = cpu_to_virtio32(vs->vdev, data);
  274. if (bc > 3) {
  275. memcpy(&data, (buf + 4), ((bc - 4) & 3) + 1);
  276. msg->payload.cmdd.data[1] =
  277. cpu_to_virtio32(vs->vdev, data);
  278. }
  279. rc = vspmi_pmic_arb_xfer(pa);
  280. raw_spin_unlock_irqrestore(&vs->txlock, flags);
  281. return rc;
  282. }
  283. enum qpnpint_regs {
  284. QPNPINT_REG_RT_STS = 0x10,
  285. QPNPINT_REG_SET_TYPE = 0x11,
  286. QPNPINT_REG_POLARITY_HIGH = 0x12,
  287. QPNPINT_REG_POLARITY_LOW = 0x13,
  288. QPNPINT_REG_LATCHED_CLR = 0x14,
  289. QPNPINT_REG_EN_SET = 0x15,
  290. QPNPINT_REG_EN_CLR = 0x16,
  291. QPNPINT_REG_LATCHED_STS = 0x18,
  292. };
  293. struct spmi_pmic_arb_qpnpint_type {
  294. u8 type; /* 1 -> edge */
  295. u8 polarity_high;
  296. u8 polarity_low;
  297. } __packed;
  298. /* Simplified accessor functions for irqchip callbacks */
  299. static void qpnpint_spmi_write(struct irq_data *d, u8 reg, void *buf,
  300. size_t len)
  301. {
  302. struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
  303. u8 sid = hwirq_to_sid(d->hwirq);
  304. u8 per = hwirq_to_per(d->hwirq);
  305. if (pmic_arb_write_cmd(pa->spmic, SPMI_CMD_EXT_WRITEL, sid,
  306. (per << 8) + reg, buf, len))
  307. dev_err_ratelimited(&pa->spmic->dev,
  308. "failed irqchip transaction on %x\n", d->irq);
  309. }
  310. static void qpnpint_spmi_read(struct irq_data *d, u8 reg, void *buf, size_t len)
  311. {
  312. struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
  313. u8 sid = hwirq_to_sid(d->hwirq);
  314. u8 per = hwirq_to_per(d->hwirq);
  315. if (pmic_arb_read_cmd(pa->spmic, SPMI_CMD_EXT_READL, sid,
  316. (per << 8) + reg, buf, len))
  317. dev_err_ratelimited(&pa->spmic->dev,
  318. "failed irqchip transaction on %x\n", d->irq);
  319. }
  320. static void periph_interrupt(struct spmi_pmic_arb *pa, u16 apid)
  321. {
  322. unsigned int irq;
  323. u32 id = 0;
  324. u8 sid = (pa->apid_data[apid].ppid >> 8) & 0xF;
  325. u8 per = pa->apid_data[apid].ppid & 0xFF;
  326. irq = irq_find_mapping(pa->domain,
  327. spec_to_hwirq(sid, per, id, apid));
  328. generic_handle_irq(irq);
  329. }
  330. static void pmic_arb_chained_irq(struct virtio_spmi *vs,
  331. struct virtio_spmi_msg *msg)
  332. {
  333. struct spmi_pmic_arb *pa = vs->pa;
  334. struct apid_data *apidd = pa->apid_data;
  335. u16 ppid = virtio16_to_cpu(vs->vdev, msg->payload.irqd.ppid);
  336. u16 apid = pa->ver_ops->ppid_to_apid(pa, ppid);
  337. struct irq_desc *desc = apidd[apid].desc;
  338. struct irq_chip *chip = irq_desc_get_chip(desc);
  339. chained_irq_enter(chip, desc);
  340. dev_dbg(&pa->spmic->dev,
  341. "Dispatching IRQ for apid=%x ppid=%x\n",
  342. apid, ppid);
  343. periph_interrupt(pa, apid);
  344. chained_irq_exit(chip, desc);
  345. }
  346. static void qpnpint_irq_ack(struct irq_data *d)
  347. {
  348. struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
  349. u8 irq = hwirq_to_irq(d->hwirq);
  350. u16 apid = hwirq_to_apid(d->hwirq);
  351. u16 ppid = pa->apid_data[apid].ppid;
  352. u8 data;
  353. unsigned long flags;
  354. raw_spin_lock_irqsave(&pa->vs->txlock, flags);
  355. vspmi_fill_txmsg(pa, VIO_IRQ_CLEAR, 0, ppid, BIT(irq));
  356. vspmi_pmic_arb_xfer(pa);
  357. raw_spin_unlock_irqrestore(&pa->vs->txlock, flags);
  358. data = BIT(irq);
  359. qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &data, 1);
  360. }
  361. static void qpnpint_irq_mask(struct irq_data *d)
  362. {
  363. u8 irq = hwirq_to_irq(d->hwirq);
  364. u8 data = BIT(irq);
  365. qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &data, 1);
  366. }
  367. static void qpnpint_irq_unmask(struct irq_data *d)
  368. {
  369. struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
  370. u8 irq = hwirq_to_irq(d->hwirq);
  371. u16 apid = hwirq_to_apid(d->hwirq);
  372. u16 ppid = pa->apid_data[apid].ppid;
  373. struct apid_data *apidd = pa->apid_data;
  374. u8 buf[2] = {0};
  375. unsigned long flags;
  376. apidd[apid].desc = irq_data_to_desc(d);
  377. raw_spin_lock_irqsave(&pa->vs->txlock, flags);
  378. vspmi_fill_txmsg(pa, VIO_ACC_ENABLE_WR, 0,
  379. ppid, SPMI_PIC_ACC_ENABLE_BIT);
  380. vspmi_pmic_arb_xfer(pa);
  381. raw_spin_unlock_irqrestore(&pa->vs->txlock, flags);
  382. qpnpint_spmi_read(d, QPNPINT_REG_EN_SET, &buf[0], 1);
  383. if (!(buf[0] & BIT(irq))) {
  384. /*
  385. * Since the interrupt is currently disabled, write to both the
  386. * LATCHED_CLR and EN_SET registers so that a spurious interrupt
  387. * cannot be triggered when the interrupt is enabled
  388. */
  389. buf[0] = BIT(irq);
  390. buf[1] = BIT(irq);
  391. qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 2);
  392. }
  393. }
  394. static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
  395. {
  396. struct spmi_pmic_arb_qpnpint_type type;
  397. irq_flow_handler_t flow_handler;
  398. u8 irq = hwirq_to_irq(d->hwirq);
  399. qpnpint_spmi_read(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
  400. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  401. type.type |= BIT(irq);
  402. if (flow_type & IRQF_TRIGGER_RISING)
  403. type.polarity_high |= BIT(irq);
  404. else
  405. type.polarity_high &= ~BIT(irq);
  406. if (flow_type & IRQF_TRIGGER_FALLING)
  407. type.polarity_low |= BIT(irq);
  408. else
  409. type.polarity_low &= ~BIT(irq);
  410. flow_handler = handle_edge_irq;
  411. } else {
  412. if ((flow_type & (IRQF_TRIGGER_HIGH)) &&
  413. (flow_type & (IRQF_TRIGGER_LOW)))
  414. return -EINVAL;
  415. type.type &= ~BIT(irq); /* level trig */
  416. if (flow_type & IRQF_TRIGGER_HIGH) {
  417. type.polarity_high |= BIT(irq);
  418. type.polarity_low &= ~BIT(irq);
  419. } else {
  420. type.polarity_low |= BIT(irq);
  421. type.polarity_high &= ~BIT(irq);
  422. }
  423. flow_handler = handle_level_irq;
  424. }
  425. qpnpint_spmi_write(d, QPNPINT_REG_SET_TYPE, &type, sizeof(type));
  426. irq_set_handler_locked(d, flow_handler);
  427. return 0;
  428. }
  429. static int qpnpint_irq_set_wake(struct irq_data *d, unsigned int on)
  430. {
  431. struct spmi_pmic_arb *pa = irq_data_get_irq_chip_data(d);
  432. return irq_set_irq_wake(pa->irq, on);
  433. }
  434. static int qpnpint_get_irqchip_state(struct irq_data *d,
  435. enum irqchip_irq_state which,
  436. bool *state)
  437. {
  438. u8 irq = hwirq_to_irq(d->hwirq);
  439. u8 status = 0;
  440. if (which != IRQCHIP_STATE_LINE_LEVEL)
  441. return -EINVAL;
  442. qpnpint_spmi_read(d, QPNPINT_REG_RT_STS, &status, 1);
  443. *state = !!(status & BIT(irq));
  444. return 0;
  445. }
  446. static int qpnpint_irq_domain_activate(struct irq_domain *domain,
  447. struct irq_data *d, bool reserve)
  448. {
  449. u8 irq = hwirq_to_irq(d->hwirq);
  450. u8 buf;
  451. buf = BIT(irq);
  452. qpnpint_spmi_write(d, QPNPINT_REG_EN_CLR, &buf, 1);
  453. qpnpint_spmi_write(d, QPNPINT_REG_LATCHED_CLR, &buf, 1);
  454. return 0;
  455. }
  456. static struct irq_chip pmic_arb_irqchip = {
  457. .name = "pmic_arb",
  458. .irq_ack = qpnpint_irq_ack,
  459. .irq_mask = qpnpint_irq_mask,
  460. .irq_unmask = qpnpint_irq_unmask,
  461. .irq_set_type = qpnpint_irq_set_type,
  462. .irq_set_wake = qpnpint_irq_set_wake,
  463. .irq_get_irqchip_state = qpnpint_get_irqchip_state,
  464. .flags = IRQCHIP_MASK_ON_SUSPEND,
  465. };
  466. static int qpnpint_irq_domain_translate(struct irq_domain *d,
  467. struct irq_fwspec *fwspec,
  468. unsigned long *out_hwirq,
  469. unsigned int *out_type)
  470. {
  471. struct spmi_pmic_arb *pa = d->host_data;
  472. u32 *intspec = fwspec->param;
  473. u16 apid, ppid;
  474. int rc;
  475. dev_dbg(&pa->spmic->dev,
  476. "intspec[0] 0x%1x intspec[1] 0x%02x intspec[2] 0x%02x\n",
  477. intspec[0], intspec[1], intspec[2]);
  478. if (irq_domain_get_of_node(d) != (pa->vs->vdev->dev.parent)->of_node)
  479. return -EINVAL;
  480. if (fwspec->param_count != 4)
  481. return -EINVAL;
  482. if (intspec[0] > 0xF || intspec[1] > 0xFF || intspec[2] > 0x7)
  483. return -EINVAL;
  484. ppid = intspec[0] << 8 | intspec[1];
  485. rc = pa->ver_ops->ppid_to_apid(pa, ppid);
  486. if (rc < 0) {
  487. dev_err(&pa->spmic->dev,
  488. "failed to xlate sid = %#x, periph = %#x, irq = %u rc = %d\n",
  489. intspec[0], intspec[1], intspec[2], rc);
  490. return rc;
  491. }
  492. apid = rc;
  493. /* Keep track of {max,min}_apid for bounding search during interrupt */
  494. if (apid > pa->max_apid)
  495. pa->max_apid = apid;
  496. if (apid < pa->min_apid)
  497. pa->min_apid = apid;
  498. *out_hwirq = spec_to_hwirq(intspec[0], intspec[1], intspec[2], apid);
  499. *out_type = intspec[3] & IRQ_TYPE_SENSE_MASK;
  500. dev_dbg(&pa->spmic->dev, "out_hwirq = %lu\n", *out_hwirq);
  501. return 0;
  502. }
  503. static struct lock_class_key qpnpint_irq_lock_class, qpnpint_irq_request_class;
  504. static void qpnpint_irq_domain_map(struct spmi_pmic_arb *pa,
  505. struct irq_domain *domain, unsigned int virq,
  506. irq_hw_number_t hwirq, unsigned int type)
  507. {
  508. irq_flow_handler_t handler;
  509. dev_dbg(&pa->spmic->dev, "virq = %u, hwirq = %lu, type = %u\n",
  510. virq, hwirq, type);
  511. if (type & IRQ_TYPE_EDGE_BOTH)
  512. handler = handle_edge_irq;
  513. else
  514. handler = handle_level_irq;
  515. irq_set_lockdep_class(virq, &qpnpint_irq_lock_class,
  516. &qpnpint_irq_request_class);
  517. irq_domain_set_info(domain, virq, hwirq, &pmic_arb_irqchip, pa,
  518. handler, NULL, NULL);
  519. }
  520. static int qpnpint_irq_domain_alloc(struct irq_domain *domain,
  521. unsigned int virq, unsigned int nr_irqs,
  522. void *data)
  523. {
  524. struct spmi_pmic_arb *pa = domain->host_data;
  525. struct irq_fwspec *fwspec = data;
  526. irq_hw_number_t hwirq;
  527. unsigned int type;
  528. int ret, i;
  529. ret = qpnpint_irq_domain_translate(domain, fwspec, &hwirq, &type);
  530. if (ret)
  531. return ret;
  532. for (i = 0; i < nr_irqs; i++)
  533. qpnpint_irq_domain_map(pa, domain, virq + i, hwirq + i, type);
  534. return 0;
  535. }
  536. static int pmic_arb_read_apid_map_v5(struct spmi_pmic_arb *pa)
  537. {
  538. struct virtio_spmi *vs = pa->vs;
  539. struct apid_data *apidd = pa->apid_data;
  540. u16 apid, ppid;
  541. u16 i;
  542. for (i = 0; i < VM_MAX_PERIPHS; i++) {
  543. ppid = vs->config.ppid_allowed[i];
  544. if (!ppid)
  545. break;
  546. apid = i;
  547. pa->ppid_to_apid[ppid] = apid | PMIC_ARB_APID_VALID;
  548. pa->apid_data[apid].ppid = ppid;
  549. pa->apid_data[apid].desc = NULL;
  550. }
  551. /* Dump the mapping table for debug purposes. */
  552. dev_dbg(&pa->spmic->dev, "PPID APID IRQ-DESC\n");
  553. for (ppid = 0; ppid < PMIC_ARB_MAX_PPID; ppid++) {
  554. apid = pa->ppid_to_apid[ppid];
  555. if (apid & PMIC_ARB_APID_VALID) {
  556. apid &= ~PMIC_ARB_APID_VALID;
  557. dev_dbg(&pa->spmic->dev, "%#03X %3u %llx\n",
  558. ppid, apid, apidd[apid].desc);
  559. }
  560. }
  561. return 0;
  562. }
  563. static int pmic_arb_ppid_to_apid_v5(struct spmi_pmic_arb *pa, u16 ppid)
  564. {
  565. if (!(pa->ppid_to_apid[ppid] & PMIC_ARB_APID_VALID))
  566. return -ENODEV;
  567. return pa->ppid_to_apid[ppid] & ~PMIC_ARB_APID_VALID;
  568. }
  569. static u32 pmic_arb_fmt_cmd_v1(u8 opc, u8 sid, u16 addr, u8 bc)
  570. {
  571. return (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
  572. }
  573. static const struct pmic_arb_ver_ops pmic_arb_v5 = {
  574. .ver_str = "v5",
  575. .ppid_to_apid = pmic_arb_ppid_to_apid_v5,
  576. .fmt_cmd = pmic_arb_fmt_cmd_v1,
  577. };
  578. static const struct irq_domain_ops pmic_arb_irq_domain_ops = {
  579. .activate = qpnpint_irq_domain_activate,
  580. .alloc = qpnpint_irq_domain_alloc,
  581. .free = irq_domain_free_irqs_common,
  582. .translate = qpnpint_irq_domain_translate,
  583. };
  584. static void viospmi_rx_isr(struct virtqueue *vq)
  585. {
  586. struct virtio_spmi *vs = vq->vdev->priv;
  587. struct virtio_spmi_msg *msg;
  588. unsigned long flags;
  589. unsigned int len;
  590. raw_spin_lock_irqsave(&vs->rxlock, flags);
  591. while ((msg = virtqueue_get_buf(vs->rxq, &len)) != NULL) {
  592. raw_spin_unlock_irqrestore(&vs->rxlock, flags);
  593. pmic_arb_chained_irq(vs, msg);
  594. raw_spin_lock_irqsave(&vs->rxlock, flags);
  595. vspmi_queue_rxmsg(vs, msg);
  596. }
  597. virtqueue_kick(vs->rxq);
  598. raw_spin_unlock_irqrestore(&vs->rxlock, flags);
  599. }
  600. static int virtio_spmi_init_vqs(struct virtio_spmi *vspmi)
  601. {
  602. struct virtqueue *vqs[2];
  603. vq_callback_t *cbs[] = { NULL, viospmi_rx_isr };
  604. static const char * const names[] = { "vs.tx", "vs.rx" };
  605. int rc;
  606. rc = virtio_find_vqs(vspmi->vdev, 2, vqs, cbs, names, NULL);
  607. if (rc)
  608. return rc;
  609. vspmi->txq = vqs[0];
  610. vspmi->rxq = vqs[1];
  611. return 0;
  612. }
  613. static void virtio_spmi_del_vqs(struct virtio_spmi *vspmi)
  614. {
  615. vspmi->vdev->config->del_vqs(vspmi->vdev);
  616. }
  617. static int virtio_spmi_probe(struct virtio_device *vdev)
  618. {
  619. struct virtio_spmi *vs;
  620. int i;
  621. int ret = 0;
  622. u32 val;
  623. struct spmi_pmic_arb *pa;
  624. struct spmi_controller *ctrl;
  625. int err;
  626. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  627. return -ENODEV;
  628. vs = devm_kzalloc(&vdev->dev, sizeof(*vs), GFP_KERNEL);
  629. if (!vs)
  630. return -ENOMEM;
  631. vdev->priv = vs;
  632. vs->vdev = vdev;
  633. raw_spin_lock_init(&vs->txlock);
  634. raw_spin_lock_init(&vs->rxlock);
  635. ret = virtio_spmi_init_vqs(vs);
  636. if (ret)
  637. goto err_init_vq;
  638. ctrl = spmi_controller_alloc(&vdev->dev, sizeof(*pa));
  639. if (!ctrl)
  640. return -ENOMEM;
  641. pa = vs->pa = spmi_controller_get_drvdata(ctrl);
  642. pa->spmic = ctrl;
  643. pa->vs = vs;
  644. pa->ver_ops = &pmic_arb_v5;
  645. dev_info(&ctrl->dev, "Virtio PMIC arbiter\n");
  646. pa->ppid_to_apid = devm_kcalloc(&ctrl->dev, PMIC_ARB_MAX_PPID,
  647. sizeof(*pa->ppid_to_apid),
  648. GFP_KERNEL);
  649. if (!pa->ppid_to_apid) {
  650. err = -ENOMEM;
  651. goto err_put_ctrl;
  652. }
  653. /* Initialize max_apid/min_apid to the opposite bounds, during
  654. * the irq domain translation, we are sure to update these
  655. */
  656. pa->max_apid = 0;
  657. pa->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
  658. ctrl->read_cmd = pmic_arb_read_cmd;
  659. ctrl->write_cmd = pmic_arb_write_cmd;
  660. ctrl->dev.of_node = (vdev->dev.parent)->of_node->child;
  661. pa->irq = of_irq_get_byname((vdev->dev.parent)->of_node, "periph_irq");
  662. if (pa->irq < 0) {
  663. err = pa->irq;
  664. goto err_put_ctrl;
  665. }
  666. virtio_device_ready(vdev);
  667. vspmi_fill_rxmsgs(vs);
  668. memset(&vs->config, 0x0, sizeof(vs->config));
  669. for (i = 0; i < VM_MAX_PERIPHS; i += 2) {
  670. val = virtio_cread32(vdev,
  671. offsetof(struct virtio_spmi_config, ppid_allowed[i]));
  672. vs->config.ppid_allowed[i] = val & PMIC_ARB_PPID_MASK;
  673. vs->config.ppid_allowed[i + 1] =
  674. (val >> 16) & PMIC_ARB_PPID_MASK;
  675. if ((!vs->config.ppid_allowed[i]) ||
  676. !(vs->config.ppid_allowed[i + 1]))
  677. break;
  678. }
  679. err = pmic_arb_read_apid_map_v5(pa);
  680. if (err) {
  681. dev_err(&vdev->dev, "could not read APID->PPID mapping table, rc= %d\n",
  682. err);
  683. goto err_put_ctrl;
  684. }
  685. dev_dbg(&vdev->dev, "adding irq domain\n");
  686. pa->domain = irq_domain_add_tree((vdev->dev.parent)->of_node,
  687. &pmic_arb_irq_domain_ops, pa);
  688. if (!pa->domain) {
  689. dev_err(&vdev->dev, "unable to create irq_domain\n");
  690. err = -ENOMEM;
  691. goto err_put_ctrl;
  692. }
  693. err = spmi_controller_add(ctrl);
  694. if (err)
  695. goto err_domain_remove;
  696. return 0;
  697. err_domain_remove:
  698. irq_domain_remove(pa->domain);
  699. err_put_ctrl:
  700. spmi_controller_put(ctrl);
  701. return err;
  702. err_init_vq:
  703. virtio_spmi_del_vqs(vs);
  704. return ret;
  705. }
  706. static void virtio_spmi_remove(struct virtio_device *vdev)
  707. {
  708. vdev->config->reset(vdev);
  709. vdev->config->del_vqs(vdev);
  710. }
  711. static unsigned int features[] = {
  712. VIRTIO_SPMI_F_INT,
  713. };
  714. static struct virtio_device_id id_table[] = {
  715. { VIRTIO_ID_SPMI, VIRTIO_DEV_ANY_ID },
  716. { 0 },
  717. };
  718. static struct virtio_driver virtio_spmi_driver = {
  719. .feature_table = features,
  720. .feature_table_size = ARRAY_SIZE(features),
  721. .driver.name = KBUILD_MODNAME,
  722. .driver.owner = THIS_MODULE,
  723. .id_table = id_table,
  724. .probe = virtio_spmi_probe,
  725. .remove = virtio_spmi_remove,
  726. };
  727. static int __init virtio_spmi_init(void)
  728. {
  729. return register_virtio_driver(&virtio_spmi_driver);
  730. }
  731. static void __exit virtio_spmi_exit(void)
  732. {
  733. unregister_virtio_driver(&virtio_spmi_driver);
  734. }
  735. subsys_initcall(virtio_spmi_init);
  736. module_exit(virtio_spmi_exit);
  737. MODULE_DEVICE_TABLE(virtio, id_table);
  738. MODULE_DESCRIPTION("virtio spmi_pmic_arb frontend driver");
  739. MODULE_LICENSE("GPL");