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