Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Spreadtrum Communications Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include <linux/hwspinlock.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/of.h> |
| 13 | #include <linux/of_device.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/spi/spi.h> |
| 16 | #include <linux/sizes.h> |
| 17 | |
| 18 | /* Registers definitions for ADI controller */ |
| 19 | #define REG_ADI_CTRL0 0x4 |
| 20 | #define REG_ADI_CHN_PRIL 0x8 |
| 21 | #define REG_ADI_CHN_PRIH 0xc |
| 22 | #define REG_ADI_INT_EN 0x10 |
| 23 | #define REG_ADI_INT_RAW 0x14 |
| 24 | #define REG_ADI_INT_MASK 0x18 |
| 25 | #define REG_ADI_INT_CLR 0x1c |
| 26 | #define REG_ADI_GSSI_CFG0 0x20 |
| 27 | #define REG_ADI_GSSI_CFG1 0x24 |
| 28 | #define REG_ADI_RD_CMD 0x28 |
| 29 | #define REG_ADI_RD_DATA 0x2c |
| 30 | #define REG_ADI_ARM_FIFO_STS 0x30 |
| 31 | #define REG_ADI_STS 0x34 |
| 32 | #define REG_ADI_EVT_FIFO_STS 0x38 |
| 33 | #define REG_ADI_ARM_CMD_STS 0x3c |
| 34 | #define REG_ADI_CHN_EN 0x40 |
| 35 | #define REG_ADI_CHN_ADDR(id) (0x44 + (id - 2) * 4) |
| 36 | #define REG_ADI_CHN_EN1 0x20c |
| 37 | |
| 38 | /* Bits definitions for register REG_ADI_GSSI_CFG0 */ |
| 39 | #define BIT_CLK_ALL_ON BIT(30) |
| 40 | |
| 41 | /* Bits definitions for register REG_ADI_RD_DATA */ |
| 42 | #define BIT_RD_CMD_BUSY BIT(31) |
| 43 | #define RD_ADDR_SHIFT 16 |
| 44 | #define RD_VALUE_MASK GENMASK(15, 0) |
| 45 | #define RD_ADDR_MASK GENMASK(30, 16) |
| 46 | |
| 47 | /* Bits definitions for register REG_ADI_ARM_FIFO_STS */ |
| 48 | #define BIT_FIFO_FULL BIT(11) |
| 49 | #define BIT_FIFO_EMPTY BIT(10) |
| 50 | |
| 51 | /* |
| 52 | * ADI slave devices include RTC, ADC, regulator, charger, thermal and so on. |
| 53 | * The slave devices address offset is always 0x8000 and size is 4K. |
| 54 | */ |
| 55 | #define ADI_SLAVE_ADDR_SIZE SZ_4K |
| 56 | #define ADI_SLAVE_OFFSET 0x8000 |
| 57 | |
| 58 | /* Timeout (ms) for the trylock of hardware spinlocks */ |
| 59 | #define ADI_HWSPINLOCK_TIMEOUT 5000 |
| 60 | /* |
| 61 | * ADI controller has 50 channels including 2 software channels |
| 62 | * and 48 hardware channels. |
| 63 | */ |
| 64 | #define ADI_HW_CHNS 50 |
| 65 | |
| 66 | #define ADI_FIFO_DRAIN_TIMEOUT 1000 |
| 67 | #define ADI_READ_TIMEOUT 2000 |
| 68 | #define REG_ADDR_LOW_MASK GENMASK(11, 0) |
| 69 | |
| 70 | struct sprd_adi { |
| 71 | struct spi_controller *ctlr; |
| 72 | struct device *dev; |
| 73 | void __iomem *base; |
| 74 | struct hwspinlock *hwlock; |
| 75 | unsigned long slave_vbase; |
| 76 | unsigned long slave_pbase; |
| 77 | }; |
| 78 | |
| 79 | static int sprd_adi_check_paddr(struct sprd_adi *sadi, u32 paddr) |
| 80 | { |
| 81 | if (paddr < sadi->slave_pbase || paddr > |
| 82 | (sadi->slave_pbase + ADI_SLAVE_ADDR_SIZE)) { |
| 83 | dev_err(sadi->dev, |
| 84 | "slave physical address is incorrect, addr = 0x%x\n", |
| 85 | paddr); |
| 86 | return -EINVAL; |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static unsigned long sprd_adi_to_vaddr(struct sprd_adi *sadi, u32 paddr) |
| 93 | { |
| 94 | return (paddr - sadi->slave_pbase + sadi->slave_vbase); |
| 95 | } |
| 96 | |
| 97 | static int sprd_adi_drain_fifo(struct sprd_adi *sadi) |
| 98 | { |
| 99 | u32 timeout = ADI_FIFO_DRAIN_TIMEOUT; |
| 100 | u32 sts; |
| 101 | |
| 102 | do { |
| 103 | sts = readl_relaxed(sadi->base + REG_ADI_ARM_FIFO_STS); |
| 104 | if (sts & BIT_FIFO_EMPTY) |
| 105 | break; |
| 106 | |
| 107 | cpu_relax(); |
| 108 | } while (--timeout); |
| 109 | |
| 110 | if (timeout == 0) { |
| 111 | dev_err(sadi->dev, "drain write fifo timeout\n"); |
| 112 | return -EBUSY; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int sprd_adi_fifo_is_full(struct sprd_adi *sadi) |
| 119 | { |
| 120 | return readl_relaxed(sadi->base + REG_ADI_ARM_FIFO_STS) & BIT_FIFO_FULL; |
| 121 | } |
| 122 | |
| 123 | static int sprd_adi_read(struct sprd_adi *sadi, u32 reg_paddr, u32 *read_val) |
| 124 | { |
| 125 | int read_timeout = ADI_READ_TIMEOUT; |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 126 | unsigned long flags; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 127 | u32 val, rd_addr; |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 128 | int ret; |
| 129 | |
| 130 | ret = hwspin_lock_timeout_irqsave(sadi->hwlock, |
| 131 | ADI_HWSPINLOCK_TIMEOUT, |
| 132 | &flags); |
| 133 | if (ret) { |
| 134 | dev_err(sadi->dev, "get the hw lock failed\n"); |
| 135 | return ret; |
| 136 | } |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Set the physical register address need to read into RD_CMD register, |
| 140 | * then ADI controller will start to transfer automatically. |
| 141 | */ |
| 142 | writel_relaxed(reg_paddr, sadi->base + REG_ADI_RD_CMD); |
| 143 | |
| 144 | /* |
| 145 | * Wait read operation complete, the BIT_RD_CMD_BUSY will be set |
| 146 | * simultaneously when writing read command to register, and the |
| 147 | * BIT_RD_CMD_BUSY will be cleared after the read operation is |
| 148 | * completed. |
| 149 | */ |
| 150 | do { |
| 151 | val = readl_relaxed(sadi->base + REG_ADI_RD_DATA); |
| 152 | if (!(val & BIT_RD_CMD_BUSY)) |
| 153 | break; |
| 154 | |
| 155 | cpu_relax(); |
| 156 | } while (--read_timeout); |
| 157 | |
| 158 | if (read_timeout == 0) { |
| 159 | dev_err(sadi->dev, "ADI read timeout\n"); |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 160 | ret = -EBUSY; |
| 161 | goto out; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * The return value includes data and read register address, from bit 0 |
| 166 | * to bit 15 are data, and from bit 16 to bit 30 are read register |
| 167 | * address. Then we can check the returned register address to validate |
| 168 | * data. |
| 169 | */ |
| 170 | rd_addr = (val & RD_ADDR_MASK ) >> RD_ADDR_SHIFT; |
| 171 | |
| 172 | if (rd_addr != (reg_paddr & REG_ADDR_LOW_MASK)) { |
| 173 | dev_err(sadi->dev, "read error, reg addr = 0x%x, val = 0x%x\n", |
| 174 | reg_paddr, val); |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 175 | ret = -EIO; |
| 176 | goto out; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | *read_val = val & RD_VALUE_MASK; |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 180 | |
| 181 | out: |
| 182 | hwspin_unlock_irqrestore(sadi->hwlock, &flags); |
| 183 | return ret; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 184 | } |
| 185 | |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 186 | static int sprd_adi_write(struct sprd_adi *sadi, u32 reg_paddr, u32 val) |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 187 | { |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 188 | unsigned long reg = sprd_adi_to_vaddr(sadi, reg_paddr); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 189 | u32 timeout = ADI_FIFO_DRAIN_TIMEOUT; |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 190 | unsigned long flags; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 191 | int ret; |
| 192 | |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 193 | ret = hwspin_lock_timeout_irqsave(sadi->hwlock, |
| 194 | ADI_HWSPINLOCK_TIMEOUT, |
| 195 | &flags); |
| 196 | if (ret) { |
| 197 | dev_err(sadi->dev, "get the hw lock failed\n"); |
| 198 | return ret; |
| 199 | } |
| 200 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 201 | ret = sprd_adi_drain_fifo(sadi); |
| 202 | if (ret < 0) |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 203 | goto out; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * we should wait for write fifo is empty before writing data to PMIC |
| 207 | * registers. |
| 208 | */ |
| 209 | do { |
| 210 | if (!sprd_adi_fifo_is_full(sadi)) { |
| 211 | writel_relaxed(val, (void __iomem *)reg); |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | cpu_relax(); |
| 216 | } while (--timeout); |
| 217 | |
| 218 | if (timeout == 0) { |
| 219 | dev_err(sadi->dev, "write fifo is full\n"); |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 220 | ret = -EBUSY; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 221 | } |
| 222 | |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 223 | out: |
| 224 | hwspin_unlock_irqrestore(sadi->hwlock, &flags); |
| 225 | return ret; |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static int sprd_adi_transfer_one(struct spi_controller *ctlr, |
| 229 | struct spi_device *spi_dev, |
| 230 | struct spi_transfer *t) |
| 231 | { |
| 232 | struct sprd_adi *sadi = spi_controller_get_devdata(ctlr); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 233 | u32 phy_reg, val; |
| 234 | int ret; |
| 235 | |
| 236 | if (t->rx_buf) { |
| 237 | phy_reg = *(u32 *)t->rx_buf + sadi->slave_pbase; |
| 238 | |
| 239 | ret = sprd_adi_check_paddr(sadi, phy_reg); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 243 | ret = sprd_adi_read(sadi, phy_reg, &val); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 244 | if (ret) |
| 245 | return ret; |
| 246 | |
| 247 | *(u32 *)t->rx_buf = val; |
| 248 | } else if (t->tx_buf) { |
| 249 | u32 *p = (u32 *)t->tx_buf; |
| 250 | |
| 251 | /* |
| 252 | * Get the physical register address need to write and convert |
| 253 | * the physical address to virtual address. Since we need |
| 254 | * virtual register address to write. |
| 255 | */ |
| 256 | phy_reg = *p++ + sadi->slave_pbase; |
| 257 | ret = sprd_adi_check_paddr(sadi, phy_reg); |
| 258 | if (ret) |
| 259 | return ret; |
| 260 | |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 261 | val = *p; |
Baolin Wang | a61aa68 | 2018-03-20 10:42:13 +0800 | [diff] [blame^] | 262 | ret = sprd_adi_write(sadi, phy_reg, val); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 263 | if (ret) |
| 264 | return ret; |
| 265 | } else { |
| 266 | dev_err(sadi->dev, "no buffer for transfer\n"); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static void sprd_adi_hw_init(struct sprd_adi *sadi) |
| 274 | { |
| 275 | struct device_node *np = sadi->dev->of_node; |
| 276 | int i, size, chn_cnt; |
| 277 | const __be32 *list; |
| 278 | u32 tmp; |
| 279 | |
| 280 | /* Address bits select default 12 bits */ |
| 281 | writel_relaxed(0, sadi->base + REG_ADI_CTRL0); |
| 282 | |
| 283 | /* Set all channels as default priority */ |
| 284 | writel_relaxed(0, sadi->base + REG_ADI_CHN_PRIL); |
| 285 | writel_relaxed(0, sadi->base + REG_ADI_CHN_PRIH); |
| 286 | |
| 287 | /* Set clock auto gate mode */ |
| 288 | tmp = readl_relaxed(sadi->base + REG_ADI_GSSI_CFG0); |
| 289 | tmp &= ~BIT_CLK_ALL_ON; |
| 290 | writel_relaxed(tmp, sadi->base + REG_ADI_GSSI_CFG0); |
| 291 | |
| 292 | /* Set hardware channels setting */ |
| 293 | list = of_get_property(np, "sprd,hw-channels", &size); |
Dan Carpenter | b0d6e09 | 2017-09-22 23:48:08 +0300 | [diff] [blame] | 294 | if (!list || !size) { |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 295 | dev_info(sadi->dev, "no hw channels setting in node\n"); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | chn_cnt = size / 8; |
| 300 | for (i = 0; i < chn_cnt; i++) { |
| 301 | u32 value; |
| 302 | u32 chn_id = be32_to_cpu(*list++); |
| 303 | u32 chn_config = be32_to_cpu(*list++); |
| 304 | |
| 305 | /* Channel 0 and 1 are software channels */ |
| 306 | if (chn_id < 2) |
| 307 | continue; |
| 308 | |
| 309 | writel_relaxed(chn_config, sadi->base + |
| 310 | REG_ADI_CHN_ADDR(chn_id)); |
| 311 | |
Baolin Wang | 54e2fc2 | 2017-10-25 19:25:09 +0800 | [diff] [blame] | 312 | if (chn_id < 32) { |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 313 | value = readl_relaxed(sadi->base + REG_ADI_CHN_EN); |
| 314 | value |= BIT(chn_id); |
| 315 | writel_relaxed(value, sadi->base + REG_ADI_CHN_EN); |
| 316 | } else if (chn_id < ADI_HW_CHNS) { |
| 317 | value = readl_relaxed(sadi->base + REG_ADI_CHN_EN1); |
| 318 | value |= BIT(chn_id - 32); |
| 319 | writel_relaxed(value, sadi->base + REG_ADI_CHN_EN1); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static int sprd_adi_probe(struct platform_device *pdev) |
| 325 | { |
| 326 | struct device_node *np = pdev->dev.of_node; |
| 327 | struct spi_controller *ctlr; |
| 328 | struct sprd_adi *sadi; |
| 329 | struct resource *res; |
| 330 | u32 num_chipselect; |
| 331 | int ret; |
| 332 | |
| 333 | if (!np) { |
| 334 | dev_err(&pdev->dev, "can not find the adi bus node\n"); |
| 335 | return -ENODEV; |
| 336 | } |
| 337 | |
| 338 | pdev->id = of_alias_get_id(np, "spi"); |
| 339 | num_chipselect = of_get_child_count(np); |
| 340 | |
| 341 | ctlr = spi_alloc_master(&pdev->dev, sizeof(struct sprd_adi)); |
| 342 | if (!ctlr) |
| 343 | return -ENOMEM; |
| 344 | |
| 345 | dev_set_drvdata(&pdev->dev, ctlr); |
| 346 | sadi = spi_controller_get_devdata(ctlr); |
| 347 | |
| 348 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 349 | sadi->base = devm_ioremap_resource(&pdev->dev, res); |
Dan Carpenter | 04063a0 | 2017-09-25 13:21:33 +0300 | [diff] [blame] | 350 | if (IS_ERR(sadi->base)) { |
| 351 | ret = PTR_ERR(sadi->base); |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 352 | goto put_ctlr; |
| 353 | } |
| 354 | |
| 355 | sadi->slave_vbase = (unsigned long)sadi->base + ADI_SLAVE_OFFSET; |
| 356 | sadi->slave_pbase = res->start + ADI_SLAVE_OFFSET; |
| 357 | sadi->ctlr = ctlr; |
| 358 | sadi->dev = &pdev->dev; |
| 359 | ret = of_hwspin_lock_get_id(np, 0); |
| 360 | if (ret < 0) { |
| 361 | dev_err(&pdev->dev, "can not get the hardware spinlock\n"); |
| 362 | goto put_ctlr; |
| 363 | } |
| 364 | |
| 365 | sadi->hwlock = hwspin_lock_request_specific(ret); |
| 366 | if (!sadi->hwlock) { |
| 367 | ret = -ENXIO; |
| 368 | goto put_ctlr; |
| 369 | } |
| 370 | |
| 371 | sprd_adi_hw_init(sadi); |
| 372 | |
| 373 | ctlr->dev.of_node = pdev->dev.of_node; |
| 374 | ctlr->bus_num = pdev->id; |
| 375 | ctlr->num_chipselect = num_chipselect; |
| 376 | ctlr->flags = SPI_MASTER_HALF_DUPLEX; |
| 377 | ctlr->bits_per_word_mask = 0; |
| 378 | ctlr->transfer_one = sprd_adi_transfer_one; |
| 379 | |
| 380 | ret = devm_spi_register_controller(&pdev->dev, ctlr); |
| 381 | if (ret) { |
| 382 | dev_err(&pdev->dev, "failed to register SPI controller\n"); |
| 383 | goto free_hwlock; |
| 384 | } |
| 385 | |
| 386 | return 0; |
| 387 | |
| 388 | free_hwlock: |
| 389 | hwspin_lock_free(sadi->hwlock); |
| 390 | put_ctlr: |
| 391 | spi_controller_put(ctlr); |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | static int sprd_adi_remove(struct platform_device *pdev) |
| 396 | { |
| 397 | struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev); |
| 398 | struct sprd_adi *sadi = spi_controller_get_devdata(ctlr); |
| 399 | |
| 400 | hwspin_lock_free(sadi->hwlock); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static const struct of_device_id sprd_adi_of_match[] = { |
| 405 | { |
| 406 | .compatible = "sprd,sc9860-adi", |
| 407 | }, |
| 408 | { }, |
| 409 | }; |
| 410 | MODULE_DEVICE_TABLE(of, sprd_adi_of_match); |
| 411 | |
| 412 | static struct platform_driver sprd_adi_driver = { |
| 413 | .driver = { |
| 414 | .name = "sprd-adi", |
Baolin Wang | 7e2903c | 2017-09-15 15:29:16 +0800 | [diff] [blame] | 415 | .of_match_table = sprd_adi_of_match, |
| 416 | }, |
| 417 | .probe = sprd_adi_probe, |
| 418 | .remove = sprd_adi_remove, |
| 419 | }; |
| 420 | module_platform_driver(sprd_adi_driver); |
| 421 | |
| 422 | MODULE_DESCRIPTION("Spreadtrum ADI Controller Driver"); |
| 423 | MODULE_AUTHOR("Baolin Wang <Baolin.Wang@spreadtrum.com>"); |
| 424 | MODULE_LICENSE("GPL v2"); |