Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 2 | /* |
| 3 | * CLPS711X SPI bus driver |
| 4 | * |
Alexander Shiyan | 6acaadc | 2016-07-06 16:53:12 +0200 | [diff] [blame] | 5 | * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru> |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/io.h> |
| 9 | #include <linux/clk.h> |
Linus Walleij | 054320b | 2019-01-07 16:51:54 +0100 | [diff] [blame] | 10 | #include <linux/gpio/consumer.h> |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/platform_device.h> |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 14 | #include <linux/regmap.h> |
| 15 | #include <linux/mfd/syscon.h> |
| 16 | #include <linux/mfd/syscon/clps711x.h> |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 17 | #include <linux/spi/spi.h> |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 18 | |
Alexander Shiyan | 6acaadc | 2016-07-06 16:53:12 +0200 | [diff] [blame] | 19 | #define DRIVER_NAME "clps711x-spi" |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 20 | |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 21 | #define SYNCIO_FRMLEN(x) ((x) << 8) |
| 22 | #define SYNCIO_TXFRMEN (1 << 14) |
| 23 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 24 | struct spi_clps711x_data { |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 25 | void __iomem *syncio; |
| 26 | struct regmap *syscon; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 27 | struct clk *spi_clk; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 28 | |
| 29 | u8 *tx_buf; |
| 30 | u8 *rx_buf; |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 31 | unsigned int bpw; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 32 | int len; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 33 | }; |
| 34 | |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 35 | static int spi_clps711x_prepare_message(struct spi_master *master, |
| 36 | struct spi_message *msg) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 37 | { |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 38 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 39 | struct spi_device *spi = msg->spi; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 40 | |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 41 | /* Setup mode for transfer */ |
| 42 | return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN, |
| 43 | (spi->mode & SPI_CPHA) ? |
| 44 | SYSCON3_ADCCKNSEN : 0); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 45 | } |
| 46 | |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 47 | static int spi_clps711x_transfer_one(struct spi_master *master, |
| 48 | struct spi_device *spi, |
| 49 | struct spi_transfer *xfer) |
| 50 | { |
| 51 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
| 52 | u8 data; |
| 53 | |
Alexander Shiyan | a5b4b23 | 2014-09-20 11:05:09 +0400 | [diff] [blame] | 54 | clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz); |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 55 | |
| 56 | hw->len = xfer->len; |
Axel Lin | bed890b | 2014-03-02 23:24:18 +0800 | [diff] [blame] | 57 | hw->bpw = xfer->bits_per_word; |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 58 | hw->tx_buf = (u8 *)xfer->tx_buf; |
| 59 | hw->rx_buf = (u8 *)xfer->rx_buf; |
| 60 | |
| 61 | /* Initiate transfer */ |
| 62 | data = hw->tx_buf ? *hw->tx_buf++ : 0; |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 63 | writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio); |
| 64 | |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 65 | return 1; |
| 66 | } |
| 67 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 68 | static irqreturn_t spi_clps711x_isr(int irq, void *dev_id) |
| 69 | { |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 70 | struct spi_master *master = dev_id; |
| 71 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 72 | u8 data; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 73 | |
| 74 | /* Handle RX */ |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 75 | data = readb(hw->syncio); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 76 | if (hw->rx_buf) |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 77 | *hw->rx_buf++ = data; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 78 | |
| 79 | /* Handle TX */ |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 80 | if (--hw->len > 0) { |
| 81 | data = hw->tx_buf ? *hw->tx_buf++ : 0; |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 82 | writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, |
| 83 | hw->syncio); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 84 | } else |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 85 | spi_finalize_current_transfer(master); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 86 | |
| 87 | return IRQ_HANDLED; |
| 88 | } |
| 89 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 90 | static int spi_clps711x_probe(struct platform_device *pdev) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 91 | { |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 92 | struct spi_clps711x_data *hw; |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 93 | struct spi_master *master; |
Alexander Shiyan | 6acaadc | 2016-07-06 16:53:12 +0200 | [diff] [blame] | 94 | int irq, ret; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 95 | |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 96 | irq = platform_get_irq(pdev, 0); |
| 97 | if (irq < 0) |
| 98 | return irq; |
| 99 | |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame] | 100 | master = spi_alloc_master(&pdev->dev, sizeof(*hw)); |
| 101 | if (!master) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 102 | return -ENOMEM; |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame] | 103 | |
Linus Walleij | 054320b | 2019-01-07 16:51:54 +0100 | [diff] [blame] | 104 | master->use_gpio_descriptors = true; |
Alexander Shiyan | 6acaadc | 2016-07-06 16:53:12 +0200 | [diff] [blame] | 105 | master->bus_num = -1; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 106 | master->mode_bits = SPI_CPHA | SPI_CS_HIGH; |
Junhao He | 3906241 | 2021-02-04 19:08:06 +0800 | [diff] [blame] | 107 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8); |
Alexander Shiyan | 6acaadc | 2016-07-06 16:53:12 +0200 | [diff] [blame] | 108 | master->dev.of_node = pdev->dev.of_node; |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 109 | master->prepare_message = spi_clps711x_prepare_message; |
| 110 | master->transfer_one = spi_clps711x_transfer_one; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 111 | |
| 112 | hw = spi_master_get_devdata(master); |
| 113 | |
Alexander Shiyan | a5b4b23 | 2014-09-20 11:05:09 +0400 | [diff] [blame] | 114 | hw->spi_clk = devm_clk_get(&pdev->dev, NULL); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 115 | if (IS_ERR(hw->spi_clk)) { |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 116 | ret = PTR_ERR(hw->spi_clk); |
| 117 | goto err_out; |
| 118 | } |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 119 | |
Alexander Shiyan | 6acaadc | 2016-07-06 16:53:12 +0200 | [diff] [blame] | 120 | hw->syscon = |
| 121 | syscon_regmap_lookup_by_compatible("cirrus,ep7209-syscon3"); |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 122 | if (IS_ERR(hw->syscon)) { |
| 123 | ret = PTR_ERR(hw->syscon); |
Sachin Kamat | c708379 | 2013-09-27 15:32:53 +0530 | [diff] [blame] | 124 | goto err_out; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 125 | } |
| 126 | |
YueHaibing | ae43724 | 2019-09-04 21:58:52 +0800 | [diff] [blame] | 127 | hw->syncio = devm_platform_ioremap_resource(pdev, 0); |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 128 | if (IS_ERR(hw->syncio)) { |
| 129 | ret = PTR_ERR(hw->syncio); |
| 130 | goto err_out; |
| 131 | } |
| 132 | |
| 133 | /* Disable extended mode due hardware problems */ |
| 134 | regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0); |
| 135 | |
| 136 | /* Clear possible pending interrupt */ |
| 137 | readl(hw->syncio); |
| 138 | |
| 139 | ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0, |
| 140 | dev_name(&pdev->dev), master); |
| 141 | if (ret) |
| 142 | goto err_out; |
| 143 | |
Jingoo Han | c493fc4 | 2013-09-24 13:27:48 +0900 | [diff] [blame] | 144 | ret = devm_spi_register_master(&pdev->dev, master); |
Alexander Shiyan | 6acaadc | 2016-07-06 16:53:12 +0200 | [diff] [blame] | 145 | if (!ret) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 146 | return 0; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 147 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 148 | err_out: |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 149 | spi_master_put(master); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
Alexander Shiyan | 6acaadc | 2016-07-06 16:53:12 +0200 | [diff] [blame] | 154 | static const struct of_device_id clps711x_spi_dt_ids[] = { |
| 155 | { .compatible = "cirrus,ep7209-spi", }, |
| 156 | { } |
| 157 | }; |
| 158 | MODULE_DEVICE_TABLE(of, clps711x_spi_dt_ids); |
| 159 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 160 | static struct platform_driver clps711x_spi_driver = { |
| 161 | .driver = { |
| 162 | .name = DRIVER_NAME, |
Alexander Shiyan | 6acaadc | 2016-07-06 16:53:12 +0200 | [diff] [blame] | 163 | .of_match_table = clps711x_spi_dt_ids, |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 164 | }, |
| 165 | .probe = spi_clps711x_probe, |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 166 | }; |
| 167 | module_platform_driver(clps711x_spi_driver); |
| 168 | |
| 169 | MODULE_LICENSE("GPL"); |
| 170 | MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); |
| 171 | MODULE_DESCRIPTION("CLPS711X SPI bus driver"); |
Axel Lin | 350a9b3 | 2014-01-14 17:01:54 +0800 | [diff] [blame] | 172 | MODULE_ALIAS("platform:" DRIVER_NAME); |