spmi-pmic-arb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/spmi.h>
  20. #include <linux/of.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/of_spmi.h>
  23. #include <linux/module.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/syscore_ops.h>
  26. #include <mach/qpnp-int.h>
  27. #include "spmi-dbgfs.h"
  28. #define SPMI_PMIC_ARB_NAME "spmi_pmic_arb"
  29. /* PMIC Arbiter configuration registers */
  30. #define PMIC_ARB_VERSION 0x0000
  31. #define PMIC_ARB_INT_EN 0x0004
  32. #define PMIC_ARB_PROTOCOL_IRQ_STATUS (0x700 + 0x820)
  33. #define PMIC_ARB_GENI_CTRL 0x0024
  34. #define PMIC_ARB_GENI_STATUS 0x0028
  35. /* PMIC Arbiter channel registers */
  36. #define PMIC_ARB_CMD(N) (0x0800 + (0x80 * (N)))
  37. #define PMIC_ARB_CONFIG(N) (0x0804 + (0x80 * (N)))
  38. #define PMIC_ARB_STATUS(N) (0x0808 + (0x80 * (N)))
  39. #define PMIC_ARB_WDATA0(N) (0x0810 + (0x80 * (N)))
  40. #define PMIC_ARB_WDATA1(N) (0x0814 + (0x80 * (N)))
  41. #define PMIC_ARB_RDATA0(N) (0x0818 + (0x80 * (N)))
  42. #define PMIC_ARB_RDATA1(N) (0x081C + (0x80 * (N)))
  43. /* Interrupt Controller */
  44. #define SPMI_PIC_OWNER_ACC_STATUS(M, N) (0x0000 + ((32 * (M)) + (4 * (N))))
  45. #define SPMI_PIC_ACC_ENABLE(N) (0x0200 + (4 * (N)))
  46. #define SPMI_PIC_IRQ_STATUS(N) (0x0600 + (4 * (N)))
  47. #define SPMI_PIC_IRQ_CLEAR(N) (0x0A00 + (4 * (N)))
  48. /* Mapping Table */
  49. #define SPMI_MAPPING_TABLE_REG(N) (0x0B00 + (4 * (N)))
  50. #define SPMI_MAPPING_BIT_INDEX(X) (((X) >> 18) & 0xF)
  51. #define SPMI_MAPPING_BIT_IS_0_FLAG(X) (((X) >> 17) & 0x1)
  52. #define SPMI_MAPPING_BIT_IS_0_RESULT(X) (((X) >> 9) & 0xFF)
  53. #define SPMI_MAPPING_BIT_IS_1_FLAG(X) (((X) >> 8) & 0x1)
  54. #define SPMI_MAPPING_BIT_IS_1_RESULT(X) (((X) >> 0) & 0xFF)
  55. #define SPMI_MAPPING_TABLE_LEN 255
  56. #define SPMI_MAPPING_TABLE_TREE_DEPTH 16 /* Maximum of 16-bits */
  57. /* Ownership Table */
  58. #define SPMI_OWNERSHIP_TABLE_REG(N) (0x0700 + (4 * (N)))
  59. #define SPMI_OWNERSHIP_PERIPH2OWNER(X) ((X) & 0x7)
  60. /* Channel Status fields */
  61. enum pmic_arb_chnl_status {
  62. PMIC_ARB_STATUS_DONE = (1 << 0),
  63. PMIC_ARB_STATUS_FAILURE = (1 << 1),
  64. PMIC_ARB_STATUS_DENIED = (1 << 2),
  65. PMIC_ARB_STATUS_DROPPED = (1 << 3),
  66. };
  67. /* Command register fields */
  68. #define PMIC_ARB_CMD_MAX_BYTE_COUNT 8
  69. /* Command Opcodes */
  70. enum pmic_arb_cmd_op_code {
  71. PMIC_ARB_OP_EXT_WRITEL = 0,
  72. PMIC_ARB_OP_EXT_READL = 1,
  73. PMIC_ARB_OP_EXT_WRITE = 2,
  74. PMIC_ARB_OP_RESET = 3,
  75. PMIC_ARB_OP_SLEEP = 4,
  76. PMIC_ARB_OP_SHUTDOWN = 5,
  77. PMIC_ARB_OP_WAKEUP = 6,
  78. PMIC_ARB_OP_AUTHENTICATE = 7,
  79. PMIC_ARB_OP_MSTR_READ = 8,
  80. PMIC_ARB_OP_MSTR_WRITE = 9,
  81. PMIC_ARB_OP_EXT_READ = 13,
  82. PMIC_ARB_OP_WRITE = 14,
  83. PMIC_ARB_OP_READ = 15,
  84. PMIC_ARB_OP_ZERO_WRITE = 16,
  85. };
  86. /* Maximum number of support PMIC peripherals */
  87. #define PMIC_ARB_MAX_PERIPHS 256
  88. #define PMIC_ARB_PERIPH_ID_VALID (1 << 15)
  89. #define PMIC_ARB_TIMEOUT_US 100
  90. #define PMIC_ARB_MAX_TRANS_BYTES (8)
  91. #define PMIC_ARB_APID_MASK 0xFF
  92. #define PMIC_ARB_PPID_MASK 0xFFF
  93. /* interrupt enable bit */
  94. #define SPMI_PIC_ACC_ENABLE_BIT BIT(0)
  95. /**
  96. * base - base address of the PMIC Arbiter core registers.
  97. * intr - base address of the SPMI interrupt control registers
  98. */
  99. struct spmi_pmic_arb_dev {
  100. struct spmi_controller controller;
  101. struct device *dev;
  102. struct device *slave;
  103. void __iomem *base;
  104. void __iomem *intr;
  105. void __iomem *cnfg;
  106. int pic_irq;
  107. bool allow_wakeup;
  108. spinlock_t lock;
  109. u8 owner;
  110. u8 channel;
  111. u8 min_apid;
  112. u8 max_apid;
  113. u16 periph_id_map[PMIC_ARB_MAX_PERIPHS];
  114. u32 mapping_table[SPMI_MAPPING_TABLE_LEN];
  115. u32 prev_prtcl_irq_stat;
  116. };
  117. static struct spmi_pmic_arb_dev *the_pmic_arb;
  118. static u32 pmic_arb_read(struct spmi_pmic_arb_dev *dev, u32 offset)
  119. {
  120. u32 val = readl_relaxed(dev->base + offset);
  121. pr_debug("address 0x%p, val 0x%x\n", dev->base + offset, val);
  122. return val;
  123. }
  124. static void pmic_arb_write(struct spmi_pmic_arb_dev *dev, u32 offset, u32 val)
  125. {
  126. pr_debug("address 0x%p, val 0x%x\n", dev->base + offset, val);
  127. writel_relaxed(val, dev->base + offset);
  128. }
  129. static void pmic_arb_save_stat_before_txn(struct spmi_pmic_arb_dev *dev)
  130. {
  131. dev->prev_prtcl_irq_stat =
  132. readl_relaxed(dev->cnfg + PMIC_ARB_PROTOCOL_IRQ_STATUS);
  133. }
  134. static int pmic_arb_diagnosis(struct spmi_pmic_arb_dev *dev, u32 status)
  135. {
  136. if (status & PMIC_ARB_STATUS_DENIED) {
  137. dev_err(dev->dev,
  138. "wait_for_done: transaction denied by SPMI master (0x%x)\n",
  139. status);
  140. return -EPERM;
  141. }
  142. if (status & PMIC_ARB_STATUS_FAILURE) {
  143. dev_err(dev->dev,
  144. "wait_for_done: transaction failed (0x%x)\n", status);
  145. return -EIO;
  146. }
  147. if (status & PMIC_ARB_STATUS_DROPPED) {
  148. dev_err(dev->dev,
  149. "wait_for_done: transaction dropped pmic-arb busy (0x%x)\n",
  150. status);
  151. return -EAGAIN;
  152. }
  153. return 0;
  154. }
  155. static int pmic_arb_wait_for_done(struct spmi_pmic_arb_dev *dev)
  156. {
  157. u32 status = 0;
  158. u32 timeout = PMIC_ARB_TIMEOUT_US;
  159. u32 offset = PMIC_ARB_STATUS(dev->channel);
  160. while (timeout--) {
  161. status = pmic_arb_read(dev, offset);
  162. if (status & PMIC_ARB_STATUS_DONE)
  163. return pmic_arb_diagnosis(dev, status);
  164. udelay(1);
  165. }
  166. dev_err(dev->dev, "wait_for_done:: timeout, status 0x%x\n", status);
  167. return -ETIMEDOUT;
  168. }
  169. /**
  170. * pa_read_data: reads pmic-arb's register and copy 1..4 bytes to buf
  171. * @bc byte count -1. range: 0..3
  172. * @reg register's address
  173. * @buf output parameter, length must be bc+1
  174. */
  175. static void pa_read_data(struct spmi_pmic_arb_dev *dev, u8 *buf, u32 reg, u8 bc)
  176. {
  177. u32 data = pmic_arb_read(dev, reg);
  178. memcpy(buf, &data, (bc & 3) + 1);
  179. }
  180. /**
  181. * pa_write_data: write 1..4 bytes from buf to pmic-arb's register
  182. * @bc byte-count -1. range: 0..3
  183. * @reg register's address
  184. * @buf buffer to write. length must be bc+1
  185. */
  186. static void
  187. pa_write_data(struct spmi_pmic_arb_dev *dev, u8 *buf, u32 reg, u8 bc)
  188. {
  189. u32 data = 0;
  190. memcpy(&data, buf, (bc & 3) + 1);
  191. pmic_arb_write(dev, reg, data);
  192. }
  193. static void pmic_arb_dbg_err_dump(struct spmi_pmic_arb_dev *pmic_arb, int ret,
  194. const char *msg, u8 opc, u8 sid, u16 addr, u8 bc, u8 *buf)
  195. {
  196. u32 irq_stat = readl_relaxed(pmic_arb->cnfg
  197. + PMIC_ARB_PROTOCOL_IRQ_STATUS);
  198. u32 geni_stat = readl_relaxed(pmic_arb->cnfg + PMIC_ARB_GENI_STATUS);
  199. u32 geni_ctrl = readl_relaxed(pmic_arb->cnfg + PMIC_ARB_GENI_CTRL);
  200. bc += 1; /* actual byte count */
  201. if (buf)
  202. dev_err(pmic_arb->dev,
  203. "error:%d on data %s opcode:0x%x sid:%d addr:0x%x bc:%d buf:%*phC\n",
  204. ret, msg, opc, sid, addr, bc, bc, buf);
  205. else
  206. dev_err(pmic_arb->dev,
  207. "error:%d on non-data-cmd opcode:0x%x sid:%d\n",
  208. ret, opc, sid);
  209. dev_err(pmic_arb->dev,
  210. "PROTOCOL_IRQ_STATUS before:0x%x after:0x%x GENI_STATUS:0x%x GENI_CTRL:0x%x\n",
  211. irq_stat, pmic_arb->prev_prtcl_irq_stat, geni_stat, geni_ctrl);
  212. }
  213. /* Non-data command */
  214. static int pmic_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
  215. {
  216. struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
  217. unsigned long flags;
  218. u32 cmd;
  219. int rc;
  220. pr_debug("op:0x%x sid:%d\n", opc, sid);
  221. /* Check for valid non-data command */
  222. if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
  223. return -EINVAL;
  224. opc -= SPMI_CMD_RESET - PMIC_ARB_OP_RESET;
  225. cmd = (opc << 27) | ((sid & 0xf) << 20);
  226. spin_lock_irqsave(&pmic_arb->lock, flags);
  227. pmic_arb_save_stat_before_txn(pmic_arb);
  228. pmic_arb_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
  229. rc = pmic_arb_wait_for_done(pmic_arb);
  230. spin_unlock_irqrestore(&pmic_arb->lock, flags);
  231. if (rc)
  232. pmic_arb_dbg_err_dump(pmic_arb, rc, "cmd", opc, sid, 0, 0, 0);
  233. return rc;
  234. }
  235. static int pmic_arb_read_cmd(struct spmi_controller *ctrl,
  236. u8 opc, u8 sid, u16 addr, u8 bc, u8 *buf)
  237. {
  238. struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
  239. unsigned long flags;
  240. u32 cmd;
  241. int rc;
  242. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  243. dev_err(pmic_arb->dev
  244. , "pmic-arb supports 1..%d bytes per trans, but:%d requested"
  245. , PMIC_ARB_MAX_TRANS_BYTES, bc+1);
  246. return -EINVAL;
  247. }
  248. dev_dbg(pmic_arb->dev, "client-rd op:0x%x sid:%d addr:0x%x bc:%d\n",
  249. opc, sid, addr, bc + 1);
  250. /* Check the opcode */
  251. if (opc >= 0x60 && opc <= 0x7F)
  252. opc = PMIC_ARB_OP_READ;
  253. else if (opc >= 0x20 && opc <= 0x2F)
  254. opc = PMIC_ARB_OP_EXT_READ;
  255. else if (opc >= 0x38 && opc <= 0x3F)
  256. opc = PMIC_ARB_OP_EXT_READL;
  257. else
  258. return -EINVAL;
  259. cmd = (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
  260. spin_lock_irqsave(&pmic_arb->lock, flags);
  261. pmic_arb_save_stat_before_txn(pmic_arb);
  262. pmic_arb_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
  263. rc = pmic_arb_wait_for_done(pmic_arb);
  264. if (rc)
  265. goto done;
  266. /* Read from FIFO, note 'bc' is actually number of bytes minus 1 */
  267. pa_read_data(pmic_arb, buf, PMIC_ARB_RDATA0(pmic_arb->channel)
  268. , min_t(u8, bc, 3));
  269. if (bc > 3)
  270. pa_read_data(pmic_arb, buf + 4,
  271. PMIC_ARB_RDATA1(pmic_arb->channel), bc - 4);
  272. done:
  273. spin_unlock_irqrestore(&pmic_arb->lock, flags);
  274. if (rc)
  275. pmic_arb_dbg_err_dump(pmic_arb, rc, "read", opc, sid, addr, bc,
  276. buf);
  277. return rc;
  278. }
  279. static int pmic_arb_write_cmd(struct spmi_controller *ctrl,
  280. u8 opc, u8 sid, u16 addr, u8 bc, u8 *buf)
  281. {
  282. struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
  283. unsigned long flags;
  284. u32 cmd;
  285. int rc;
  286. if (bc >= PMIC_ARB_MAX_TRANS_BYTES) {
  287. dev_err(pmic_arb->dev
  288. , "pmic-arb supports 1..%d bytes per trans, but:%d requested"
  289. , PMIC_ARB_MAX_TRANS_BYTES, bc+1);
  290. return -EINVAL;
  291. }
  292. dev_dbg(pmic_arb->dev, "client-wr op:0x%x sid:%d addr:0x%x bc:%d\n",
  293. opc, sid, addr, bc + 1);
  294. /* Check the opcode */
  295. if (opc >= 0x40 && opc <= 0x5F)
  296. opc = PMIC_ARB_OP_WRITE;
  297. else if (opc >= 0x00 && opc <= 0x0F)
  298. opc = PMIC_ARB_OP_EXT_WRITE;
  299. else if (opc >= 0x30 && opc <= 0x37)
  300. opc = PMIC_ARB_OP_EXT_WRITEL;
  301. else if (opc >= 0x80 && opc <= 0xFF)
  302. opc = PMIC_ARB_OP_ZERO_WRITE;
  303. else
  304. return -EINVAL;
  305. cmd = (opc << 27) | ((sid & 0xf) << 20) | (addr << 4) | (bc & 0x7);
  306. /* Write data to FIFOs */
  307. spin_lock_irqsave(&pmic_arb->lock, flags);
  308. pmic_arb_save_stat_before_txn(pmic_arb);
  309. pa_write_data(pmic_arb, buf, PMIC_ARB_WDATA0(pmic_arb->channel)
  310. , min_t(u8, bc, 3));
  311. if (bc > 3)
  312. pa_write_data(pmic_arb, buf + 4,
  313. PMIC_ARB_WDATA1(pmic_arb->channel), bc - 4);
  314. /* Start the transaction */
  315. pmic_arb_write(pmic_arb, PMIC_ARB_CMD(pmic_arb->channel), cmd);
  316. rc = pmic_arb_wait_for_done(pmic_arb);
  317. spin_unlock_irqrestore(&pmic_arb->lock, flags);
  318. if (rc)
  319. pmic_arb_dbg_err_dump(pmic_arb, rc, "write", opc, sid, addr, bc,
  320. buf);
  321. return rc;
  322. }
  323. /* APID to PPID */
  324. static u16 get_peripheral_id(struct spmi_pmic_arb_dev *pmic_arb, u8 apid)
  325. {
  326. return pmic_arb->periph_id_map[apid] & PMIC_ARB_PPID_MASK;
  327. }
  328. /* APID to PPID, returns valid flag */
  329. static int is_apid_valid(struct spmi_pmic_arb_dev *pmic_arb, u8 apid)
  330. {
  331. return pmic_arb->periph_id_map[apid] & PMIC_ARB_PERIPH_ID_VALID;
  332. }
  333. static u32 search_mapping_table(struct spmi_pmic_arb_dev *pmic_arb, u16 ppid)
  334. {
  335. u32 *mapping_table = pmic_arb->mapping_table;
  336. u32 apid = PMIC_ARB_MAX_PERIPHS;
  337. int index = 0;
  338. u32 data;
  339. int i;
  340. for (i = 0; i < SPMI_MAPPING_TABLE_TREE_DEPTH; ++i) {
  341. data = mapping_table[index];
  342. if (ppid & (1 << SPMI_MAPPING_BIT_INDEX(data))) {
  343. if (SPMI_MAPPING_BIT_IS_1_FLAG(data)) {
  344. index = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  345. } else {
  346. apid = SPMI_MAPPING_BIT_IS_1_RESULT(data);
  347. break;
  348. }
  349. } else {
  350. if (SPMI_MAPPING_BIT_IS_0_FLAG(data)) {
  351. index = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  352. } else {
  353. apid = SPMI_MAPPING_BIT_IS_0_RESULT(data);
  354. break;
  355. }
  356. }
  357. }
  358. return apid;
  359. }
  360. /* PPID to APID */
  361. static uint32_t map_peripheral_id(struct spmi_pmic_arb_dev *pmic_arb, u16 ppid)
  362. {
  363. u32 apid = search_mapping_table(pmic_arb, ppid);
  364. u32 old_ppid;
  365. u32 owner;
  366. /* If the apid was found, add it to the lookup table */
  367. if (apid < PMIC_ARB_MAX_PERIPHS) {
  368. old_ppid = get_peripheral_id(pmic_arb, apid);
  369. owner = SPMI_OWNERSHIP_PERIPH2OWNER(
  370. readl_relaxed(pmic_arb->cnfg +
  371. SPMI_OWNERSHIP_TABLE_REG(apid)));
  372. /* Check ownership */
  373. if (owner != pmic_arb->owner) {
  374. dev_err(pmic_arb->dev, "PPID 0x%x incorrect owner %d\n",
  375. ppid, owner);
  376. return PMIC_ARB_MAX_PERIPHS;
  377. }
  378. /* Check if already mapped */
  379. if (pmic_arb->periph_id_map[apid] & PMIC_ARB_PERIPH_ID_VALID) {
  380. if (ppid != old_ppid) {
  381. dev_err(pmic_arb->dev,
  382. "PPID 0x%x: APID 0x%x already mapped\n",
  383. ppid, apid);
  384. return PMIC_ARB_MAX_PERIPHS;
  385. }
  386. return apid;
  387. }
  388. pmic_arb->periph_id_map[apid] = ppid | PMIC_ARB_PERIPH_ID_VALID;
  389. if (apid > pmic_arb->max_apid)
  390. pmic_arb->max_apid = apid;
  391. if (apid < pmic_arb->min_apid)
  392. pmic_arb->min_apid = apid;
  393. return apid;
  394. }
  395. dev_err(pmic_arb->dev, "Unknown ppid 0x%x\n", ppid);
  396. return PMIC_ARB_MAX_PERIPHS;
  397. }
  398. /* Enable interrupt at the PMIC Arbiter PIC */
  399. static int pmic_arb_pic_enable(struct spmi_controller *ctrl,
  400. struct qpnp_irq_spec *spec, uint32_t data)
  401. {
  402. struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
  403. u8 apid = data & PMIC_ARB_APID_MASK;
  404. unsigned long flags;
  405. u32 status;
  406. dev_dbg(pmic_arb->dev, "PIC enable, apid:0x%x, sid:0x%x, pid:0x%x\n",
  407. apid, spec->slave, spec->per);
  408. if (data < pmic_arb->min_apid || data > pmic_arb->max_apid) {
  409. dev_err(pmic_arb->dev, "int enable: invalid APID %d\n", data);
  410. return -EINVAL;
  411. }
  412. if (!is_apid_valid(pmic_arb, apid)) {
  413. dev_err(pmic_arb->dev, "int enable: int not supported\n");
  414. return -EINVAL;
  415. }
  416. spin_lock_irqsave(&pmic_arb->lock, flags);
  417. status = readl_relaxed(pmic_arb->intr + SPMI_PIC_ACC_ENABLE(apid));
  418. if (!(status & SPMI_PIC_ACC_ENABLE_BIT)) {
  419. status = status | SPMI_PIC_ACC_ENABLE_BIT;
  420. writel_relaxed(status,
  421. pmic_arb->intr + SPMI_PIC_ACC_ENABLE(apid));
  422. /* Interrupt needs to be enabled before returning to caller */
  423. wmb();
  424. }
  425. spin_unlock_irqrestore(&pmic_arb->lock, flags);
  426. return 0;
  427. }
  428. /* Disable interrupt at the PMIC Arbiter PIC */
  429. static int pmic_arb_pic_disable(struct spmi_controller *ctrl,
  430. struct qpnp_irq_spec *spec, uint32_t data)
  431. {
  432. struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
  433. u8 apid = data & PMIC_ARB_APID_MASK;
  434. unsigned long flags;
  435. u32 status;
  436. dev_dbg(pmic_arb->dev, "PIC disable, apid:0x%x, sid:0x%x, pid:0x%x\n",
  437. apid, spec->slave, spec->per);
  438. if (data < pmic_arb->min_apid || data > pmic_arb->max_apid) {
  439. dev_err(pmic_arb->dev, "int disable: invalid APID %d\n", data);
  440. return -EINVAL;
  441. }
  442. if (!is_apid_valid(pmic_arb, apid)) {
  443. dev_err(pmic_arb->dev, "int disable: int not supported\n");
  444. return -EINVAL;
  445. }
  446. spin_lock_irqsave(&pmic_arb->lock, flags);
  447. status = readl_relaxed(pmic_arb->intr + SPMI_PIC_ACC_ENABLE(apid));
  448. if (status & SPMI_PIC_ACC_ENABLE_BIT) {
  449. /* clear the enable bit and write */
  450. status = status & ~SPMI_PIC_ACC_ENABLE_BIT;
  451. writel_relaxed(status,
  452. pmic_arb->intr + SPMI_PIC_ACC_ENABLE(apid));
  453. /* Interrupt needs to be disabled before returning to caller */
  454. wmb();
  455. }
  456. spin_unlock_irqrestore(&pmic_arb->lock, flags);
  457. return 0;
  458. }
  459. static irqreturn_t
  460. periph_interrupt(struct spmi_pmic_arb_dev *pmic_arb, u8 apid, bool show)
  461. {
  462. u16 ppid = get_peripheral_id(pmic_arb, apid);
  463. void __iomem *intr = pmic_arb->intr;
  464. u8 sid = (ppid >> 8) & 0x0F;
  465. u8 pid = ppid & 0xFF;
  466. u32 status;
  467. int i;
  468. if (!is_apid_valid(pmic_arb, apid)) {
  469. dev_err(pmic_arb->dev,
  470. "periph_interrupt(apid:0x%x sid:0x%x pid:0x%x) unknown peripheral\n",
  471. apid, sid, pid);
  472. /* return IRQ_NONE; */
  473. }
  474. status = readl_relaxed(intr + SPMI_PIC_ACC_ENABLE(apid));
  475. if (!(status & SPMI_PIC_ACC_ENABLE_BIT)) {
  476. /*
  477. * All interrupts from this peripheral are disabled
  478. * don't bother calling the qpnpint handler
  479. */
  480. return IRQ_HANDLED;
  481. }
  482. /* Read the peripheral specific interrupt bits */
  483. status = readl_relaxed(intr + SPMI_PIC_IRQ_STATUS(apid));
  484. if (!show) {
  485. /* Clear the peripheral interrupts */
  486. writel_relaxed(status, intr + SPMI_PIC_IRQ_CLEAR(apid));
  487. /* Irq needs to be cleared/acknowledged before exiting ISR */
  488. mb();
  489. }
  490. dev_dbg(pmic_arb->dev,
  491. "interrupt, apid:0x%x, sid:0x%x, pid:0x%x, intr:0x%x\n",
  492. apid, sid, pid, status);
  493. /* Send interrupt notification */
  494. for (i = 0; status && i < 8; ++i, status >>= 1) {
  495. if (status & 0x1) {
  496. struct qpnp_irq_spec irq_spec = {
  497. .slave = sid,
  498. .per = pid,
  499. .irq = i,
  500. };
  501. if (show)
  502. qpnpint_show_irq(&pmic_arb->controller,
  503. &irq_spec);
  504. else
  505. qpnpint_handle_irq(&pmic_arb->controller,
  506. &irq_spec);
  507. }
  508. }
  509. return IRQ_HANDLED;
  510. }
  511. /* Peripheral interrupt handler */
  512. static irqreturn_t
  513. __pmic_arb_periph_irq(int irq, void *dev_id, bool show)
  514. {
  515. struct spmi_pmic_arb_dev *pmic_arb = dev_id;
  516. void __iomem *intr = pmic_arb->intr;
  517. u8 ee = pmic_arb->owner;
  518. u32 ret = IRQ_NONE;
  519. u32 status;
  520. int first = pmic_arb->min_apid >> 5;
  521. int last = pmic_arb->max_apid >> 5;
  522. int i, j;
  523. dev_dbg(pmic_arb->dev, "Peripheral interrupt detected\n");
  524. /* Check the accumulated interrupt status */
  525. for (i = first; i <= last; ++i) {
  526. status = readl_relaxed(intr + SPMI_PIC_OWNER_ACC_STATUS(ee, i));
  527. for (j = 0; status && j < 32; ++j, status >>= 1) {
  528. if (status & 0x1) {
  529. u8 id = (i * 32) + j;
  530. ret |= periph_interrupt(pmic_arb, id, show);
  531. }
  532. }
  533. }
  534. return ret;
  535. }
  536. static irqreturn_t pmic_arb_periph_irq(int irq, void *dev_id)
  537. {
  538. return __pmic_arb_periph_irq(irq, dev_id, false);
  539. }
  540. static void spmi_pmic_arb_resume(void)
  541. {
  542. if (qpnpint_show_resume_irq())
  543. __pmic_arb_periph_irq(the_pmic_arb->pic_irq,
  544. the_pmic_arb, true);
  545. }
  546. static struct syscore_ops spmi_pmic_arb_syscore_ops = {
  547. .resume = spmi_pmic_arb_resume,
  548. };
  549. /* Callback to register an APID for specific slave/peripheral */
  550. static int pmic_arb_intr_priv_data(struct spmi_controller *ctrl,
  551. struct qpnp_irq_spec *spec, uint32_t *data)
  552. {
  553. struct spmi_pmic_arb_dev *pmic_arb = spmi_get_ctrldata(ctrl);
  554. u16 ppid = ((spec->slave & 0x0F) << 8) | (spec->per & 0xFF);
  555. *data = map_peripheral_id(pmic_arb, ppid);
  556. return 0;
  557. }
  558. static int pmic_arb_mapping_data_show(struct seq_file *file, void *unused)
  559. {
  560. struct spmi_pmic_arb_dev *pmic_arb = file->private;
  561. int first = pmic_arb->min_apid;
  562. int last = pmic_arb->max_apid;
  563. int i;
  564. for (i = first; i <= last; ++i) {
  565. if (!is_apid_valid(pmic_arb, i))
  566. continue;
  567. seq_printf(file, "APID 0x%.2x = PPID 0x%.3x. Enabled:%d\n",
  568. i, get_peripheral_id(pmic_arb, i),
  569. readl_relaxed(pmic_arb->intr + SPMI_PIC_ACC_ENABLE(i)));
  570. }
  571. return 0;
  572. }
  573. static int pmic_arb_mapping_data_open(struct inode *inode, struct file *file)
  574. {
  575. return single_open(file, pmic_arb_mapping_data_show, inode->i_private);
  576. }
  577. static const struct file_operations pmic_arb_dfs_fops = {
  578. .open = pmic_arb_mapping_data_open,
  579. .read = seq_read,
  580. .llseek = seq_lseek,
  581. .release = seq_release,
  582. };
  583. static int __devinit
  584. spmi_pmic_arb_get_property(struct platform_device *pdev, char *pname, u32 *prop)
  585. {
  586. int ret = of_property_read_u32(pdev->dev.of_node, pname, prop);
  587. if (ret)
  588. dev_err(&pdev->dev, "missing property: %s\n", pname);
  589. else
  590. pr_debug("%s = 0x%x\n", pname, *prop);
  591. return ret;
  592. }
  593. static struct qpnp_local_int spmi_pmic_arb_intr_cb = {
  594. .mask = pmic_arb_pic_disable,
  595. .unmask = pmic_arb_pic_enable,
  596. .register_priv_data = pmic_arb_intr_priv_data,
  597. };
  598. static int __devinit spmi_pmic_arb_probe(struct platform_device *pdev)
  599. {
  600. struct spmi_pmic_arb_dev *pmic_arb;
  601. struct resource *mem_res;
  602. u32 cell_index;
  603. u32 prop;
  604. int ret = 0;
  605. int i;
  606. pr_debug("SPMI PMIC Arbiter\n");
  607. pmic_arb = devm_kzalloc(&pdev->dev,
  608. sizeof(struct spmi_pmic_arb_dev), GFP_KERNEL);
  609. if (!pmic_arb) {
  610. dev_err(&pdev->dev, "can not allocate pmic_arb data\n");
  611. return -ENOMEM;
  612. }
  613. mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
  614. if (!mem_res) {
  615. dev_err(&pdev->dev, "missing base memory resource\n");
  616. return -ENODEV;
  617. }
  618. pmic_arb->base = devm_ioremap(&pdev->dev,
  619. mem_res->start, resource_size(mem_res));
  620. if (!pmic_arb->base) {
  621. dev_err(&pdev->dev, "ioremap of 'base' failed\n");
  622. return -ENOMEM;
  623. }
  624. mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "intr");
  625. if (!mem_res) {
  626. dev_err(&pdev->dev, "missing mem resource (interrupts)\n");
  627. return -ENODEV;
  628. }
  629. pmic_arb->intr = devm_ioremap(&pdev->dev,
  630. mem_res->start, resource_size(mem_res));
  631. if (!pmic_arb->intr) {
  632. dev_err(&pdev->dev, "ioremap of 'intr' failed\n");
  633. return -ENOMEM;
  634. }
  635. mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cnfg");
  636. if (!mem_res) {
  637. dev_err(&pdev->dev, "missing mem resource (configuration)\n");
  638. return -ENODEV;
  639. }
  640. pmic_arb->cnfg = devm_ioremap(&pdev->dev,
  641. mem_res->start, resource_size(mem_res));
  642. if (!pmic_arb->cnfg) {
  643. dev_err(&pdev->dev, "ioremap of 'cnfg' failed\n");
  644. return -ENOMEM;
  645. }
  646. for (i = 0; i < ARRAY_SIZE(pmic_arb->mapping_table); ++i)
  647. pmic_arb->mapping_table[i] = readl_relaxed(
  648. pmic_arb->cnfg + SPMI_MAPPING_TABLE_REG(i));
  649. pmic_arb->pic_irq = platform_get_irq(pdev, 0);
  650. if (!pmic_arb->pic_irq) {
  651. dev_err(&pdev->dev, "missing IRQ resource\n");
  652. return -ENODEV;
  653. }
  654. ret = devm_request_irq(&pdev->dev, pmic_arb->pic_irq,
  655. pmic_arb_periph_irq, IRQF_TRIGGER_HIGH, pdev->name, pmic_arb);
  656. if (ret) {
  657. dev_err(&pdev->dev, "request IRQ failed\n");
  658. return ret;
  659. }
  660. /* Get properties from the device tree */
  661. ret = spmi_pmic_arb_get_property(pdev, "cell-index", &cell_index);
  662. if (ret)
  663. return -ENODEV;
  664. ret = spmi_pmic_arb_get_property(pdev, "qcom,pmic-arb-ee", &prop);
  665. if (ret)
  666. return -ENODEV;
  667. pmic_arb->owner = (u8)prop;
  668. ret = spmi_pmic_arb_get_property(pdev, "qcom,pmic-arb-channel", &prop);
  669. if (ret)
  670. return -ENODEV;
  671. pmic_arb->channel = (u8)prop;
  672. pmic_arb->allow_wakeup = !of_property_read_bool(pdev->dev.of_node,
  673. "qcom,not-wakeup");
  674. if (pmic_arb->allow_wakeup) {
  675. ret = irq_set_irq_wake(pmic_arb->pic_irq, 1);
  676. if (unlikely(ret)) {
  677. pr_err("Unable to set wakeup irq, err=%d\n", ret);
  678. return -ENODEV;
  679. }
  680. }
  681. pmic_arb->max_apid = 0;
  682. pmic_arb->min_apid = PMIC_ARB_MAX_PERIPHS - 1;
  683. pmic_arb->dev = &pdev->dev;
  684. platform_set_drvdata(pdev, pmic_arb);
  685. spmi_set_ctrldata(&pmic_arb->controller, pmic_arb);
  686. spin_lock_init(&pmic_arb->lock);
  687. pmic_arb->controller.nr = cell_index;
  688. pmic_arb->controller.dev.parent = pdev->dev.parent;
  689. pmic_arb->controller.dev.of_node = of_node_get(pdev->dev.of_node);
  690. /* Callbacks */
  691. pmic_arb->controller.cmd = pmic_arb_cmd;
  692. pmic_arb->controller.read_cmd = pmic_arb_read_cmd;
  693. pmic_arb->controller.write_cmd = pmic_arb_write_cmd;
  694. ret = spmi_add_controller(&pmic_arb->controller);
  695. if (ret)
  696. goto err_add_controller;
  697. /* Register the interrupt enable/disable functions */
  698. ret = qpnpint_register_controller(pmic_arb->controller.dev.of_node,
  699. &pmic_arb->controller,
  700. &spmi_pmic_arb_intr_cb);
  701. if (ret) {
  702. dev_err(&pdev->dev, "Unable to register controller %d\n",
  703. cell_index);
  704. goto err_reg_controller;
  705. }
  706. /* Register device(s) from the device tree */
  707. of_spmi_register_devices(&pmic_arb->controller);
  708. /* Add debugfs file for mapping data */
  709. if (spmi_dfs_create_file(&pmic_arb->controller, "mapping",
  710. pmic_arb, &pmic_arb_dfs_fops) == NULL)
  711. dev_err(&pdev->dev, "error creating 'mapping' debugfs file\n");
  712. pr_debug("PMIC Arb Version 0x%x\n",
  713. pmic_arb_read(pmic_arb, PMIC_ARB_VERSION));
  714. the_pmic_arb = pmic_arb;
  715. register_syscore_ops(&spmi_pmic_arb_syscore_ops);
  716. return 0;
  717. err_reg_controller:
  718. spmi_del_controller(&pmic_arb->controller);
  719. err_add_controller:
  720. platform_set_drvdata(pdev, NULL);
  721. if (pmic_arb->allow_wakeup)
  722. irq_set_irq_wake(pmic_arb->pic_irq, 0);
  723. return ret;
  724. }
  725. static int __devexit spmi_pmic_arb_remove(struct platform_device *pdev)
  726. {
  727. struct spmi_pmic_arb_dev *pmic_arb = platform_get_drvdata(pdev);
  728. int ret;
  729. ret = qpnpint_unregister_controller(pmic_arb->controller.dev.of_node);
  730. if (ret)
  731. dev_err(&pdev->dev, "Unable to unregister controller %d\n",
  732. pmic_arb->controller.nr);
  733. if (pmic_arb->allow_wakeup)
  734. irq_set_irq_wake(pmic_arb->pic_irq, 0);
  735. platform_set_drvdata(pdev, NULL);
  736. spmi_del_controller(&pmic_arb->controller);
  737. return ret;
  738. }
  739. static struct of_device_id spmi_pmic_arb_match_table[] = {
  740. { .compatible = "qcom,spmi-pmic-arb",
  741. },
  742. {}
  743. };
  744. static struct platform_driver spmi_pmic_arb_driver = {
  745. .probe = spmi_pmic_arb_probe,
  746. .remove = __exit_p(spmi_pmic_arb_remove),
  747. .driver = {
  748. .name = SPMI_PMIC_ARB_NAME,
  749. .owner = THIS_MODULE,
  750. .of_match_table = spmi_pmic_arb_match_table,
  751. },
  752. };
  753. static int __init spmi_pmic_arb_init(void)
  754. {
  755. return platform_driver_register(&spmi_pmic_arb_driver);
  756. }
  757. postcore_initcall(spmi_pmic_arb_init);
  758. static void __exit spmi_pmic_arb_exit(void)
  759. {
  760. platform_driver_unregister(&spmi_pmic_arb_driver);
  761. }
  762. module_exit(spmi_pmic_arb_exit);
  763. MODULE_LICENSE("GPL v2");
  764. MODULE_VERSION("1.0");
  765. MODULE_ALIAS("platform:spmi_pmic_arb");