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