Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 2 | |
| 3 | #include <linux/delay.h> |
| 4 | #include <linux/err.h> |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 5 | #include <linux/interrupt.h> |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 6 | #include <linux/io.h> |
| 7 | #include <linux/kernel.h> |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 8 | #include <linux/module.h> |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 9 | #include <linux/of.h> |
| 10 | #include <linux/platform_device.h> |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 11 | #include <linux/seq_file.h> |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 12 | #include <linux/slab.h> |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 13 | #include <linux/spmi.h> |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 14 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 15 | /* |
| 16 | * SPMI register addr |
| 17 | */ |
Mauro Carvalho Chehab | 974e3bd | 2020-08-17 09:10:24 +0200 | [diff] [blame] | 18 | #define SPMI_CHANNEL_OFFSET 0x0300 |
| 19 | #define SPMI_SLAVE_OFFSET 0x20 |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 20 | |
Mauro Carvalho Chehab | 974e3bd | 2020-08-17 09:10:24 +0200 | [diff] [blame] | 21 | #define SPMI_APB_SPMI_CMD_BASE_ADDR 0x0100 |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 22 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 23 | #define SPMI_APB_SPMI_WDATA0_BASE_ADDR 0x0104 |
| 24 | #define SPMI_APB_SPMI_WDATA1_BASE_ADDR 0x0108 |
| 25 | #define SPMI_APB_SPMI_WDATA2_BASE_ADDR 0x010c |
| 26 | #define SPMI_APB_SPMI_WDATA3_BASE_ADDR 0x0110 |
| 27 | |
| 28 | #define SPMI_APB_SPMI_STATUS_BASE_ADDR 0x0200 |
| 29 | |
| 30 | #define SPMI_APB_SPMI_RDATA0_BASE_ADDR 0x0204 |
| 31 | #define SPMI_APB_SPMI_RDATA1_BASE_ADDR 0x0208 |
| 32 | #define SPMI_APB_SPMI_RDATA2_BASE_ADDR 0x020c |
| 33 | #define SPMI_APB_SPMI_RDATA3_BASE_ADDR 0x0210 |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 34 | |
Mauro Carvalho Chehab | 974e3bd | 2020-08-17 09:10:24 +0200 | [diff] [blame] | 35 | #define SPMI_PER_DATAREG_BYTE 4 |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 36 | /* |
| 37 | * SPMI cmd register |
| 38 | */ |
Mauro Carvalho Chehab | 974e3bd | 2020-08-17 09:10:24 +0200 | [diff] [blame] | 39 | #define SPMI_APB_SPMI_CMD_EN BIT(31) |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 40 | #define SPMI_APB_SPMI_CMD_TYPE_OFFSET 24 |
| 41 | #define SPMI_APB_SPMI_CMD_LENGTH_OFFSET 20 |
Mauro Carvalho Chehab | 974e3bd | 2020-08-17 09:10:24 +0200 | [diff] [blame] | 42 | #define SPMI_APB_SPMI_CMD_SLAVEID_OFFSET 16 |
| 43 | #define SPMI_APB_SPMI_CMD_ADDR_OFFSET 0 |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 44 | |
| 45 | /* Command Opcodes */ |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 46 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 47 | enum spmi_controller_cmd_op_code { |
| 48 | SPMI_CMD_REG_ZERO_WRITE = 0, |
| 49 | SPMI_CMD_REG_WRITE = 1, |
| 50 | SPMI_CMD_REG_READ = 2, |
| 51 | SPMI_CMD_EXT_REG_WRITE = 3, |
| 52 | SPMI_CMD_EXT_REG_READ = 4, |
| 53 | SPMI_CMD_EXT_REG_WRITE_L = 5, |
| 54 | SPMI_CMD_EXT_REG_READ_L = 6, |
| 55 | SPMI_CMD_REG_RESET = 7, |
| 56 | SPMI_CMD_REG_SLEEP = 8, |
| 57 | SPMI_CMD_REG_SHUTDOWN = 9, |
| 58 | SPMI_CMD_REG_WAKEUP = 10, |
| 59 | }; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * SPMI status register |
| 63 | */ |
Mauro Carvalho Chehab | 974e3bd | 2020-08-17 09:10:24 +0200 | [diff] [blame] | 64 | #define SPMI_APB_TRANS_DONE BIT(0) |
| 65 | #define SPMI_APB_TRANS_FAIL BIT(2) |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 66 | |
| 67 | /* Command register fields */ |
| 68 | #define SPMI_CONTROLLER_CMD_MAX_BYTE_COUNT 16 |
| 69 | |
| 70 | /* Maximum number of support PMIC peripherals */ |
| 71 | #define SPMI_CONTROLLER_TIMEOUT_US 1000 |
Mauro Carvalho Chehab | 974e3bd | 2020-08-17 09:10:24 +0200 | [diff] [blame] | 72 | #define SPMI_CONTROLLER_MAX_TRANS_BYTES 16 |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 73 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 74 | struct spmi_controller_dev { |
| 75 | struct spmi_controller *controller; |
| 76 | struct device *dev; |
| 77 | void __iomem *base; |
| 78 | spinlock_t lock; |
| 79 | u32 channel; |
| 80 | }; |
| 81 | |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 82 | static int spmi_controller_wait_for_done(struct device *dev, |
| 83 | struct spmi_controller_dev *ctrl_dev, |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 84 | void __iomem *base, u8 sid, u16 addr) |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 85 | { |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 86 | u32 timeout = SPMI_CONTROLLER_TIMEOUT_US; |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 87 | u32 status, offset; |
Mauro Carvalho Chehab | 974e3bd | 2020-08-17 09:10:24 +0200 | [diff] [blame] | 88 | |
| 89 | offset = SPMI_APB_SPMI_STATUS_BASE_ADDR; |
| 90 | offset += SPMI_CHANNEL_OFFSET * ctrl_dev->channel + SPMI_SLAVE_OFFSET * sid; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 91 | |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 92 | do { |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 93 | status = readl(base + offset); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 94 | |
| 95 | if (status & SPMI_APB_TRANS_DONE) { |
| 96 | if (status & SPMI_APB_TRANS_FAIL) { |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 97 | dev_err(dev, "%s: transaction failed (0x%x)\n", |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 98 | __func__, status); |
| 99 | return -EIO; |
| 100 | } |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 101 | dev_dbg(dev, "%s: status 0x%x\n", __func__, status); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | } |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 104 | udelay(1); |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 105 | } while (timeout--); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 106 | |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 107 | dev_err(dev, "%s: timeout, status 0x%x\n", __func__, status); |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 108 | return -ETIMEDOUT; |
| 109 | } |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 110 | |
| 111 | static int spmi_read_cmd(struct spmi_controller *ctrl, |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 112 | u8 opc, u8 slave_id, u16 slave_addr, u8 *__buf, size_t bc) |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 113 | { |
| 114 | struct spmi_controller_dev *spmi_controller = dev_get_drvdata(&ctrl->dev); |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 115 | u32 chnl_ofst = SPMI_CHANNEL_OFFSET * spmi_controller->channel; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 116 | unsigned long flags; |
Mauro Carvalho Chehab | 6af3645 | 2020-08-17 09:10:26 +0200 | [diff] [blame] | 117 | u8 *buf = __buf; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 118 | u32 cmd, data; |
| 119 | int rc; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 120 | u8 op_code, i; |
| 121 | |
| 122 | if (bc > SPMI_CONTROLLER_MAX_TRANS_BYTES) { |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 123 | dev_err(&ctrl->dev, |
YueHaibing | 4c6491a | 2020-09-01 11:57:22 +0800 | [diff] [blame] | 124 | "spmi_controller supports 1..%d bytes per trans, but:%zu requested\n", |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 125 | SPMI_CONTROLLER_MAX_TRANS_BYTES, bc); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 126 | return -EINVAL; |
| 127 | } |
| 128 | |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 129 | switch (opc) { |
| 130 | case SPMI_CMD_READ: |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 131 | op_code = SPMI_CMD_REG_READ; |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 132 | break; |
| 133 | case SPMI_CMD_EXT_READ: |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 134 | op_code = SPMI_CMD_EXT_REG_READ; |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 135 | break; |
| 136 | case SPMI_CMD_EXT_READL: |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 137 | op_code = SPMI_CMD_EXT_REG_READ_L; |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 138 | break; |
| 139 | default: |
| 140 | dev_err(&ctrl->dev, "invalid read cmd 0x%x\n", opc); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 141 | return -EINVAL; |
| 142 | } |
| 143 | |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 144 | cmd = SPMI_APB_SPMI_CMD_EN | |
| 145 | (op_code << SPMI_APB_SPMI_CMD_TYPE_OFFSET) | |
| 146 | ((bc - 1) << SPMI_APB_SPMI_CMD_LENGTH_OFFSET) | |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 147 | ((slave_id & 0xf) << SPMI_APB_SPMI_CMD_SLAVEID_OFFSET) | /* slvid */ |
| 148 | ((slave_addr & 0xffff) << SPMI_APB_SPMI_CMD_ADDR_OFFSET); /* slave_addr */ |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 149 | |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 150 | spin_lock_irqsave(&spmi_controller->lock, flags); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 151 | |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 152 | writel(cmd, spmi_controller->base + chnl_ofst + SPMI_APB_SPMI_CMD_BASE_ADDR); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 153 | |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 154 | rc = spmi_controller_wait_for_done(&ctrl->dev, spmi_controller, |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 155 | spmi_controller->base, slave_id, slave_addr); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 156 | if (rc) |
| 157 | goto done; |
| 158 | |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 159 | for (i = 0; bc > i * SPMI_PER_DATAREG_BYTE; i++) { |
| 160 | data = readl(spmi_controller->base + chnl_ofst + |
| 161 | SPMI_SLAVE_OFFSET * slave_id + |
| 162 | SPMI_APB_SPMI_RDATA0_BASE_ADDR + |
| 163 | i * SPMI_PER_DATAREG_BYTE); |
Juan Antonio Aldea-Armenteros | 1b9419d | 2020-11-19 13:27:38 +0100 | [diff] [blame] | 164 | data = be32_to_cpu((__be32 __force)data); |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 165 | if ((bc - i * SPMI_PER_DATAREG_BYTE) >> 2) { |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 166 | memcpy(buf, &data, sizeof(data)); |
| 167 | buf += sizeof(data); |
| 168 | } else { |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 169 | memcpy(buf, &data, bc % SPMI_PER_DATAREG_BYTE); |
| 170 | buf += (bc % SPMI_PER_DATAREG_BYTE); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 171 | } |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 172 | } |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 173 | |
| 174 | done: |
| 175 | spin_unlock_irqrestore(&spmi_controller->lock, flags); |
| 176 | if (rc) |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 177 | dev_err(&ctrl->dev, |
YueHaibing | 4c6491a | 2020-09-01 11:57:22 +0800 | [diff] [blame] | 178 | "spmi read wait timeout op:0x%x slave_id:%d slave_addr:0x%x bc:%zu\n", |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 179 | opc, slave_id, slave_addr, bc + 1); |
Mauro Carvalho Chehab | 6af3645 | 2020-08-17 09:10:26 +0200 | [diff] [blame] | 180 | else |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 181 | dev_dbg(&ctrl->dev, "%s: id:%d slave_addr:0x%x, read value: %*ph\n", |
| 182 | __func__, slave_id, slave_addr, (int)bc, __buf); |
Mauro Carvalho Chehab | 6af3645 | 2020-08-17 09:10:26 +0200 | [diff] [blame] | 183 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 184 | return rc; |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 185 | } |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 186 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 187 | static int spmi_write_cmd(struct spmi_controller *ctrl, |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 188 | u8 opc, u8 slave_id, u16 slave_addr, const u8 *__buf, size_t bc) |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 189 | { |
| 190 | struct spmi_controller_dev *spmi_controller = dev_get_drvdata(&ctrl->dev); |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 191 | u32 chnl_ofst = SPMI_CHANNEL_OFFSET * spmi_controller->channel; |
Mauro Carvalho Chehab | 6af3645 | 2020-08-17 09:10:26 +0200 | [diff] [blame] | 192 | const u8 *buf = __buf; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 193 | unsigned long flags; |
Mauro Carvalho Chehab | 8788a30 | 2020-08-17 09:10:25 +0200 | [diff] [blame] | 194 | u32 cmd, data; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 195 | int rc; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 196 | u8 op_code, i; |
| 197 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 198 | if (bc > SPMI_CONTROLLER_MAX_TRANS_BYTES) { |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 199 | dev_err(&ctrl->dev, |
YueHaibing | 4c6491a | 2020-09-01 11:57:22 +0800 | [diff] [blame] | 200 | "spmi_controller supports 1..%d bytes per trans, but:%zu requested\n", |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 201 | SPMI_CONTROLLER_MAX_TRANS_BYTES, bc); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 202 | return -EINVAL; |
| 203 | } |
| 204 | |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 205 | switch (opc) { |
| 206 | case SPMI_CMD_WRITE: |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 207 | op_code = SPMI_CMD_REG_WRITE; |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 208 | break; |
| 209 | case SPMI_CMD_EXT_WRITE: |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 210 | op_code = SPMI_CMD_EXT_REG_WRITE; |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 211 | break; |
| 212 | case SPMI_CMD_EXT_WRITEL: |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 213 | op_code = SPMI_CMD_EXT_REG_WRITE_L; |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 214 | break; |
| 215 | default: |
| 216 | dev_err(&ctrl->dev, "invalid write cmd 0x%x\n", opc); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | } |
| 219 | |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 220 | cmd = SPMI_APB_SPMI_CMD_EN | |
| 221 | (op_code << SPMI_APB_SPMI_CMD_TYPE_OFFSET) | |
| 222 | ((bc - 1) << SPMI_APB_SPMI_CMD_LENGTH_OFFSET) | |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 223 | ((slave_id & 0xf) << SPMI_APB_SPMI_CMD_SLAVEID_OFFSET) | |
| 224 | ((slave_addr & 0xffff) << SPMI_APB_SPMI_CMD_ADDR_OFFSET); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 225 | |
| 226 | /* Write data to FIFOs */ |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 227 | spin_lock_irqsave(&spmi_controller->lock, flags); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 228 | |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 229 | for (i = 0; bc > i * SPMI_PER_DATAREG_BYTE; i++) { |
Mauro Carvalho Chehab | 8788a30 | 2020-08-17 09:10:25 +0200 | [diff] [blame] | 230 | data = 0; |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 231 | if ((bc - i * SPMI_PER_DATAREG_BYTE) >> 2) { |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 232 | memcpy(&data, buf, sizeof(data)); |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 233 | buf += sizeof(data); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 234 | } else { |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 235 | memcpy(&data, buf, bc % SPMI_PER_DATAREG_BYTE); |
| 236 | buf += (bc % SPMI_PER_DATAREG_BYTE); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Juan Antonio Aldea-Armenteros | 1b9419d | 2020-11-19 13:27:38 +0100 | [diff] [blame] | 239 | writel((u32 __force)cpu_to_be32(data), |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 240 | spmi_controller->base + chnl_ofst + |
| 241 | SPMI_APB_SPMI_WDATA0_BASE_ADDR + |
| 242 | SPMI_PER_DATAREG_BYTE * i); |
| 243 | } |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 244 | |
| 245 | /* Start the transaction */ |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 246 | writel(cmd, spmi_controller->base + chnl_ofst + SPMI_APB_SPMI_CMD_BASE_ADDR); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 247 | |
Mauro Carvalho Chehab | 4d914a8 | 2020-08-17 09:10:27 +0200 | [diff] [blame] | 248 | rc = spmi_controller_wait_for_done(&ctrl->dev, spmi_controller, |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 249 | spmi_controller->base, slave_id, |
| 250 | slave_addr); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 251 | spin_unlock_irqrestore(&spmi_controller->lock, flags); |
| 252 | |
| 253 | if (rc) |
YueHaibing | 4c6491a | 2020-09-01 11:57:22 +0800 | [diff] [blame] | 254 | dev_err(&ctrl->dev, "spmi write wait timeout op:0x%x slave_id:%d slave_addr:0x%x bc:%zu\n", |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 255 | opc, slave_id, slave_addr, bc); |
Mauro Carvalho Chehab | 6af3645 | 2020-08-17 09:10:26 +0200 | [diff] [blame] | 256 | else |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 257 | dev_dbg(&ctrl->dev, "%s: id:%d slave_addr:0x%x, wrote value: %*ph\n", |
| 258 | __func__, slave_id, slave_addr, (int)bc, __buf); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 259 | |
| 260 | return rc; |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 263 | static int spmi_controller_probe(struct platform_device *pdev) |
| 264 | { |
| 265 | struct spmi_controller_dev *spmi_controller; |
| 266 | struct spmi_controller *ctrl; |
| 267 | struct resource *iores; |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 268 | int ret; |
Mauro Carvalho Chehab | 6af3645 | 2020-08-17 09:10:26 +0200 | [diff] [blame] | 269 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 270 | ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*spmi_controller)); |
| 271 | if (!ctrl) { |
| 272 | dev_err(&pdev->dev, "can not allocate spmi_controller data\n"); |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 273 | return -ENOMEM; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 274 | } |
| 275 | spmi_controller = spmi_controller_get_drvdata(ctrl); |
| 276 | spmi_controller->controller = ctrl; |
| 277 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 278 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 279 | if (!iores) { |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 280 | dev_err(&pdev->dev, "can not get resource!\n"); |
Christophe JAILLET | 12b38ea | 2020-12-13 16:11:05 +0100 | [diff] [blame] | 281 | ret = -EINVAL; |
| 282 | goto err_put_controller; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Dan Carpenter | dbbc8fd | 2020-09-18 17:33:38 +0300 | [diff] [blame] | 285 | spmi_controller->base = devm_ioremap(&pdev->dev, iores->start, |
| 286 | resource_size(iores)); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 287 | if (!spmi_controller->base) { |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 288 | dev_err(&pdev->dev, "can not remap base addr!\n"); |
Christophe JAILLET | 12b38ea | 2020-12-13 16:11:05 +0100 | [diff] [blame] | 289 | ret = -EADDRNOTAVAIL; |
| 290 | goto err_put_controller; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 291 | } |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 292 | |
Mauro Carvalho Chehab | fcc84fe | 2021-06-24 16:01:31 +0200 | [diff] [blame] | 293 | ret = of_property_read_u32(pdev->dev.of_node, "hisilicon,spmi-channel", |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 294 | &spmi_controller->channel); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 295 | if (ret) { |
Mauro Carvalho Chehab | 6196331 | 2020-08-17 09:10:23 +0200 | [diff] [blame] | 296 | dev_err(&pdev->dev, "can not get channel\n"); |
Christophe JAILLET | 12b38ea | 2020-12-13 16:11:05 +0100 | [diff] [blame] | 297 | ret = -ENODEV; |
| 298 | goto err_put_controller; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | platform_set_drvdata(pdev, spmi_controller); |
| 302 | dev_set_drvdata(&ctrl->dev, spmi_controller); |
| 303 | |
| 304 | spin_lock_init(&spmi_controller->lock); |
| 305 | |
| 306 | ctrl->nr = spmi_controller->channel; |
| 307 | ctrl->dev.parent = pdev->dev.parent; |
| 308 | ctrl->dev.of_node = of_node_get(pdev->dev.of_node); |
| 309 | |
| 310 | /* Callbacks */ |
| 311 | ctrl->read_cmd = spmi_read_cmd; |
| 312 | ctrl->write_cmd = spmi_write_cmd; |
| 313 | |
| 314 | ret = spmi_controller_add(ctrl); |
Christophe JAILLET | 12b38ea | 2020-12-13 16:11:05 +0100 | [diff] [blame] | 315 | if (ret) { |
| 316 | dev_err(&pdev->dev, "spmi_controller_add failed with error %d!\n", ret); |
| 317 | goto err_put_controller; |
| 318 | } |
Mauro Carvalho Chehab | 85eb534 | 2020-08-17 09:10:22 +0200 | [diff] [blame] | 319 | |
Christophe JAILLET | 12b38ea | 2020-12-13 16:11:05 +0100 | [diff] [blame] | 320 | return 0; |
| 321 | |
| 322 | err_put_controller: |
| 323 | spmi_controller_put(ctrl); |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 324 | return ret; |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static int spmi_del_controller(struct platform_device *pdev) |
| 328 | { |
| 329 | struct spmi_controller *ctrl = platform_get_drvdata(pdev); |
| 330 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 331 | spmi_controller_remove(ctrl); |
Christophe JAILLET | 12b38ea | 2020-12-13 16:11:05 +0100 | [diff] [blame] | 332 | spmi_controller_put(ctrl); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 336 | static const struct of_device_id spmi_controller_match_table[] = { |
Mauro Carvalho Chehab | de1a93b | 2020-08-18 16:58:55 +0200 | [diff] [blame] | 337 | { |
| 338 | .compatible = "hisilicon,kirin970-spmi-controller", |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 339 | }, |
| 340 | {} |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 341 | }; |
Mauro Carvalho Chehab | 85eb534 | 2020-08-17 09:10:22 +0200 | [diff] [blame] | 342 | MODULE_DEVICE_TABLE(of, spmi_controller_match_table); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 343 | |
| 344 | static struct platform_driver spmi_controller_driver = { |
| 345 | .probe = spmi_controller_probe, |
| 346 | .remove = spmi_del_controller, |
| 347 | .driver = { |
Mauro Carvalho Chehab | 7f3ac6c | 2020-08-17 09:10:29 +0200 | [diff] [blame] | 348 | .name = "hisi_spmi_controller", |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 349 | .of_match_table = spmi_controller_match_table, |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 350 | }, |
| 351 | }; |
| 352 | |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 353 | static int __init spmi_controller_init(void) |
| 354 | { |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 355 | return platform_driver_register(&spmi_controller_driver); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 356 | } |
| 357 | postcore_initcall(spmi_controller_init); |
| 358 | |
| 359 | static void __exit spmi_controller_exit(void) |
| 360 | { |
| 361 | platform_driver_unregister(&spmi_controller_driver); |
| 362 | } |
| 363 | module_exit(spmi_controller_exit); |
Mayulong | 70f59c9 | 2020-08-17 09:10:20 +0200 | [diff] [blame] | 364 | |
Mauro Carvalho Chehab | 2ea3f6a | 2020-08-17 09:10:21 +0200 | [diff] [blame] | 365 | MODULE_LICENSE("GPL v2"); |
| 366 | MODULE_VERSION("1.0"); |
Colin Ian King | e4cebca | 2020-08-20 08:51:36 +0100 | [diff] [blame] | 367 | MODULE_ALIAS("platform:spmi_controller"); |