]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - drivers/spi/spi-clps711x.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-kernels.git] / drivers / spi / spi-clps711x.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * CLPS711X SPI bus driver
4 *
5 * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru>
6 */
7
8 #include <linux/io.h>
9 #include <linux/clk.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/mfd/syscon/clps711x.h>
17 #include <linux/spi/spi.h>
18
19 #define DRIVER_NAME "clps711x-spi"
20
21 #define SYNCIO_FRMLEN(x) ((x) << 8)
22 #define SYNCIO_TXFRMEN (1 << 14)
23
24 struct spi_clps711x_data {
25 void __iomem *syncio;
26 struct regmap *syscon;
27 struct clk *spi_clk;
28
29 u8 *tx_buf;
30 u8 *rx_buf;
31 unsigned int bpw;
32 int len;
33 };
34
35 static int spi_clps711x_prepare_message(struct spi_master *master,
36 struct spi_message *msg)
37 {
38 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
39 struct spi_device *spi = msg->spi;
40
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);
45 }
46
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
54 clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz);
55
56 hw->len = xfer->len;
57 hw->bpw = xfer->bits_per_word;
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;
63 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
64
65 return 1;
66 }
67
68 static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
69 {
70 struct spi_master *master = dev_id;
71 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
72 u8 data;
73
74 /* Handle RX */
75 data = readb(hw->syncio);
76 if (hw->rx_buf)
77 *hw->rx_buf++ = data;
78
79 /* Handle TX */
80 if (--hw->len > 0) {
81 data = hw->tx_buf ? *hw->tx_buf++ : 0;
82 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
83 hw->syncio);
84 } else
85 spi_finalize_current_transfer(master);
86
87 return IRQ_HANDLED;
88 }
89
90 static int spi_clps711x_probe(struct platform_device *pdev)
91 {
92 struct spi_clps711x_data *hw;
93 struct spi_master *master;
94 struct resource *res;
95 int irq, ret;
96
97 irq = platform_get_irq(pdev, 0);
98 if (irq < 0)
99 return irq;
100
101 master = spi_alloc_master(&pdev->dev, sizeof(*hw));
102 if (!master)
103 return -ENOMEM;
104
105 master->use_gpio_descriptors = true;
106 master->bus_num = -1;
107 master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
108 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
109 master->dev.of_node = pdev->dev.of_node;
110 master->prepare_message = spi_clps711x_prepare_message;
111 master->transfer_one = spi_clps711x_transfer_one;
112
113 hw = spi_master_get_devdata(master);
114
115 hw->spi_clk = devm_clk_get(&pdev->dev, NULL);
116 if (IS_ERR(hw->spi_clk)) {
117 ret = PTR_ERR(hw->spi_clk);
118 goto err_out;
119 }
120
121 hw->syscon =
122 syscon_regmap_lookup_by_compatible("cirrus,ep7209-syscon3");
123 if (IS_ERR(hw->syscon)) {
124 ret = PTR_ERR(hw->syscon);
125 goto err_out;
126 }
127
128 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
129 hw->syncio = devm_ioremap_resource(&pdev->dev, res);
130 if (IS_ERR(hw->syncio)) {
131 ret = PTR_ERR(hw->syncio);
132 goto err_out;
133 }
134
135 /* Disable extended mode due hardware problems */
136 regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
137
138 /* Clear possible pending interrupt */
139 readl(hw->syncio);
140
141 ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
142 dev_name(&pdev->dev), master);
143 if (ret)
144 goto err_out;
145
146 ret = devm_spi_register_master(&pdev->dev, master);
147 if (!ret)
148 return 0;
149
150 err_out:
151 spi_master_put(master);
152
153 return ret;
154 }
155
156 static const struct of_device_id clps711x_spi_dt_ids[] = {
157 { .compatible = "cirrus,ep7209-spi", },
158 { }
159 };
160 MODULE_DEVICE_TABLE(of, clps711x_spi_dt_ids);
161
162 static struct platform_driver clps711x_spi_driver = {
163 .driver = {
164 .name = DRIVER_NAME,
165 .of_match_table = clps711x_spi_dt_ids,
166 },
167 .probe = spi_clps711x_probe,
168 };
169 module_platform_driver(clps711x_spi_driver);
170
171 MODULE_LICENSE("GPL");
172 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
173 MODULE_DESCRIPTION("CLPS711X SPI bus driver");
174 MODULE_ALIAS("platform:" DRIVER_NAME);