Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
161b96c3 AS |
2 | /* |
3 | * CLPS711X SPI bus driver | |
4 | * | |
6acaadc8 | 5 | * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru> |
161b96c3 AS |
6 | */ |
7 | ||
8 | #include <linux/io.h> | |
9 | #include <linux/clk.h> | |
054320b2 | 10 | #include <linux/gpio/consumer.h> |
161b96c3 AS |
11 | #include <linux/module.h> |
12 | #include <linux/interrupt.h> | |
13 | #include <linux/platform_device.h> | |
3dc92594 AS |
14 | #include <linux/regmap.h> |
15 | #include <linux/mfd/syscon.h> | |
16 | #include <linux/mfd/syscon/clps711x.h> | |
161b96c3 | 17 | #include <linux/spi/spi.h> |
161b96c3 | 18 | |
6acaadc8 | 19 | #define DRIVER_NAME "clps711x-spi" |
161b96c3 | 20 | |
3dc92594 AS |
21 | #define SYNCIO_FRMLEN(x) ((x) << 8) |
22 | #define SYNCIO_TXFRMEN (1 << 14) | |
23 | ||
161b96c3 | 24 | struct spi_clps711x_data { |
3dc92594 AS |
25 | void __iomem *syncio; |
26 | struct regmap *syscon; | |
161b96c3 | 27 | struct clk *spi_clk; |
161b96c3 AS |
28 | |
29 | u8 *tx_buf; | |
30 | u8 *rx_buf; | |
8dda9d9a | 31 | unsigned int bpw; |
161b96c3 | 32 | int len; |
161b96c3 AS |
33 | }; |
34 | ||
bf5c2e27 AL |
35 | static int spi_clps711x_prepare_message(struct spi_master *master, |
36 | struct spi_message *msg) | |
161b96c3 | 37 | { |
3dc92594 | 38 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
8dda9d9a | 39 | struct spi_device *spi = msg->spi; |
161b96c3 | 40 | |
3dc92594 AS |
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); | |
bf5c2e27 | 45 | } |
161b96c3 | 46 | |
bf5c2e27 AL |
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; | |
161b96c3 | 53 | |
a5b4b234 | 54 | clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz); |
161b96c3 | 55 | |
bf5c2e27 | 56 | hw->len = xfer->len; |
bed890b4 | 57 | hw->bpw = xfer->bits_per_word; |
bf5c2e27 AL |
58 | hw->tx_buf = (u8 *)xfer->tx_buf; |
59 | hw->rx_buf = (u8 *)xfer->rx_buf; | |
161b96c3 | 60 | |
bf5c2e27 AL |
61 | /* Initiate transfer */ |
62 | data = hw->tx_buf ? *hw->tx_buf++ : 0; | |
3dc92594 AS |
63 | writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio); |
64 | ||
bf5c2e27 | 65 | return 1; |
161b96c3 AS |
66 | } |
67 | ||
68 | static irqreturn_t spi_clps711x_isr(int irq, void *dev_id) | |
69 | { | |
bf5c2e27 AL |
70 | struct spi_master *master = dev_id; |
71 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); | |
c7a26f12 | 72 | u8 data; |
161b96c3 AS |
73 | |
74 | /* Handle RX */ | |
3dc92594 | 75 | data = readb(hw->syncio); |
161b96c3 | 76 | if (hw->rx_buf) |
c7a26f12 | 77 | *hw->rx_buf++ = data; |
161b96c3 AS |
78 | |
79 | /* Handle TX */ | |
c7a26f12 AS |
80 | if (--hw->len > 0) { |
81 | data = hw->tx_buf ? *hw->tx_buf++ : 0; | |
3dc92594 AS |
82 | writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, |
83 | hw->syncio); | |
161b96c3 | 84 | } else |
bf5c2e27 | 85 | spi_finalize_current_transfer(master); |
161b96c3 AS |
86 | |
87 | return IRQ_HANDLED; | |
88 | } | |
89 | ||
fd4a319b | 90 | static int spi_clps711x_probe(struct platform_device *pdev) |
161b96c3 | 91 | { |
161b96c3 | 92 | struct spi_clps711x_data *hw; |
3dc92594 | 93 | struct spi_master *master; |
6acaadc8 | 94 | int irq, ret; |
161b96c3 | 95 | |
3dc92594 AS |
96 | irq = platform_get_irq(pdev, 0); |
97 | if (irq < 0) | |
98 | return irq; | |
99 | ||
3e9ea4b4 AS |
100 | master = spi_alloc_master(&pdev->dev, sizeof(*hw)); |
101 | if (!master) | |
161b96c3 | 102 | return -ENOMEM; |
3e9ea4b4 | 103 | |
054320b2 | 104 | master->use_gpio_descriptors = true; |
6acaadc8 | 105 | master->bus_num = -1; |
161b96c3 | 106 | master->mode_bits = SPI_CPHA | SPI_CS_HIGH; |
8dda9d9a | 107 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8); |
6acaadc8 | 108 | master->dev.of_node = pdev->dev.of_node; |
bf5c2e27 AL |
109 | master->prepare_message = spi_clps711x_prepare_message; |
110 | master->transfer_one = spi_clps711x_transfer_one; | |
161b96c3 AS |
111 | |
112 | hw = spi_master_get_devdata(master); | |
113 | ||
a5b4b234 | 114 | hw->spi_clk = devm_clk_get(&pdev->dev, NULL); |
161b96c3 | 115 | if (IS_ERR(hw->spi_clk)) { |
161b96c3 AS |
116 | ret = PTR_ERR(hw->spi_clk); |
117 | goto err_out; | |
118 | } | |
161b96c3 | 119 | |
6acaadc8 AS |
120 | hw->syscon = |
121 | syscon_regmap_lookup_by_compatible("cirrus,ep7209-syscon3"); | |
3dc92594 AS |
122 | if (IS_ERR(hw->syscon)) { |
123 | ret = PTR_ERR(hw->syscon); | |
124 | goto err_out; | |
125 | } | |
126 | ||
ae43724d | 127 | hw->syncio = devm_platform_ioremap_resource(pdev, 0); |
3dc92594 AS |
128 | if (IS_ERR(hw->syncio)) { |
129 | ret = PTR_ERR(hw->syncio); | |
130 | goto err_out; | |
131 | } | |
132 | ||
161b96c3 | 133 | /* Disable extended mode due hardware problems */ |
3dc92594 | 134 | regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0); |
161b96c3 AS |
135 | |
136 | /* Clear possible pending interrupt */ | |
3dc92594 | 137 | readl(hw->syncio); |
161b96c3 | 138 | |
3dc92594 | 139 | ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0, |
bf5c2e27 | 140 | dev_name(&pdev->dev), master); |
3dc92594 | 141 | if (ret) |
c7083790 | 142 | goto err_out; |
161b96c3 | 143 | |
c493fc4b | 144 | ret = devm_spi_register_master(&pdev->dev, master); |
6acaadc8 | 145 | if (!ret) |
161b96c3 | 146 | return 0; |
161b96c3 | 147 | |
161b96c3 | 148 | err_out: |
161b96c3 | 149 | spi_master_put(master); |
161b96c3 AS |
150 | |
151 | return ret; | |
152 | } | |
153 | ||
6acaadc8 AS |
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 | ||
161b96c3 AS |
160 | static struct platform_driver clps711x_spi_driver = { |
161 | .driver = { | |
162 | .name = DRIVER_NAME, | |
6acaadc8 | 163 | .of_match_table = clps711x_spi_dt_ids, |
161b96c3 AS |
164 | }, |
165 | .probe = spi_clps711x_probe, | |
161b96c3 AS |
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"); | |
350a9b33 | 172 | MODULE_ALIAS("platform:" DRIVER_NAME); |