ar71xx: rename rb4xx spi drivers
[openwrt.git] / target / linux / ar71xx / files / drivers / spi / pb44_spi.c
1 /*
2  * Atheros PB44 board SPI controller driver
3  *
4  * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/spinlock.h>
16 #include <linux/workqueue.h>
17 #include <linux/platform_device.h>
18 #include <linux/io.h>
19 #include <linux/spi/spi.h>
20 #include <linux/spi/spi_bitbang.h>
21 #include <linux/bitops.h>
22 #include <linux/gpio.h>
23
24 #include <asm/mach-ar71xx/ar71xx.h>
25 #include <asm/mach-ar71xx/platform.h>
26
27 #define DRV_DESC        "Atheros PB44 SPI Controller driver"
28 #define DRV_VERSION     "0.1.0"
29 #define DRV_NAME        "pb44-spi"
30
31 #undef PER_BIT_READ
32
33 struct ar71xx_spi {
34         struct  spi_bitbang     bitbang;
35         u32                     ioc_base;
36         u32                     reg_ctrl;
37
38         void __iomem            *base;
39
40         struct platform_device  *pdev;
41 };
42
43 static inline u32 pb44_spi_rr(struct ar71xx_spi *sp, unsigned reg)
44 {
45         return __raw_readl(sp->base + reg);
46 }
47
48 static inline void pb44_spi_wr(struct ar71xx_spi *sp, unsigned reg, u32 val)
49 {
50         __raw_writel(val, sp->base + reg);
51 }
52
53 static inline struct ar71xx_spi *spidev_to_sp(struct spi_device *spi)
54 {
55         return spi_master_get_devdata(spi->master);
56 }
57
58 static void pb44_spi_chipselect(struct spi_device *spi, int is_active)
59 {
60         struct ar71xx_spi *sp = spidev_to_sp(spi);
61         int cs_high = (spi->mode & SPI_CS_HIGH) ? is_active : !is_active;
62
63         if (is_active) {
64                 /* set initial clock polarity */
65                 if (spi->mode & SPI_CPOL)
66                         sp->ioc_base |= SPI_IOC_CLK;
67                 else
68                         sp->ioc_base &= ~SPI_IOC_CLK;
69
70                 pb44_spi_wr(sp, SPI_REG_IOC, sp->ioc_base);
71         }
72
73         if (spi->chip_select) {
74                 unsigned long gpio = (unsigned long) spi->controller_data;
75
76                 /* SPI is normally active-low */
77                 gpio_set_value(gpio, cs_high);
78         } else {
79                 if (cs_high)
80                         sp->ioc_base |= SPI_IOC_CS0;
81                 else
82                         sp->ioc_base &= ~SPI_IOC_CS0;
83
84                 pb44_spi_wr(sp, SPI_REG_IOC, sp->ioc_base);
85         }
86
87 }
88
89 static void pb44_spi_enable(struct ar71xx_spi *sp)
90 {
91         /* enable GPIO mode */
92         pb44_spi_wr(sp, SPI_REG_FS, SPI_FS_GPIO);
93
94         /* save CTRL register */
95         sp->reg_ctrl = pb44_spi_rr(sp, SPI_REG_CTRL);
96         sp->ioc_base = pb44_spi_rr(sp, SPI_REG_IOC);
97
98         pb44_spi_wr(sp, SPI_REG_CTRL, 0x43);
99 }
100
101 static void pb44_spi_disable(struct ar71xx_spi *sp)
102 {
103         /* restore CTRL register */
104         pb44_spi_wr(sp, SPI_REG_CTRL, sp->reg_ctrl);
105         /* disable GPIO mode */
106         pb44_spi_wr(sp, SPI_REG_FS, 0);
107 }
108
109 static int pb44_spi_setup_cs(struct spi_device *spi)
110 {
111         struct ar71xx_spi *sp = spidev_to_sp(spi);
112
113         if (spi->chip_select) {
114                 unsigned long gpio = (unsigned long) spi->controller_data;
115                 int status = 0;
116
117                 status = gpio_request(gpio, dev_name(&spi->dev));
118                 if (status)
119                         return status;
120
121                 status = gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH);
122                 if (status) {
123                         gpio_free(gpio);
124                         return status;
125                 }
126         } else {
127                 if (spi->mode & SPI_CS_HIGH)
128                         sp->ioc_base |= SPI_IOC_CS0;
129                 else
130                         sp->ioc_base &= ~SPI_IOC_CS0;
131                 pb44_spi_wr(sp, SPI_REG_IOC, sp->ioc_base);
132         }
133
134         return 0;
135 }
136
137 static void pb44_spi_cleanup_cs(struct spi_device *spi)
138 {
139         if (spi->chip_select) {
140                 unsigned long gpio = (unsigned long) spi->controller_data;
141                 gpio_free(gpio);
142         }
143 }
144
145 static int pb44_spi_setup(struct spi_device *spi)
146 {
147         int status = 0;
148
149         if (spi->bits_per_word > 32)
150                 return -EINVAL;
151
152         if (!spi->controller_state) {
153                 status = pb44_spi_setup_cs(spi);
154                 if (status)
155                         return status;
156         }
157
158         status = spi_bitbang_setup(spi);
159         if (status && !spi->controller_state)
160                 pb44_spi_cleanup_cs(spi);
161
162         return status;
163 }
164
165 static void pb44_spi_cleanup(struct spi_device *spi)
166 {
167         pb44_spi_cleanup_cs(spi);
168         spi_bitbang_cleanup(spi);
169 }
170
171 static u32 pb44_spi_txrx_mode0(struct spi_device *spi, unsigned nsecs,
172                                u32 word, u8 bits)
173 {
174         struct ar71xx_spi *sp = spidev_to_sp(spi);
175         u32 ioc = sp->ioc_base;
176         u32 ret;
177
178         /* clock starts at inactive polarity */
179         for (word <<= (32 - bits); likely(bits); bits--) {
180                 u32 out;
181
182                 if (word & (1 << 31))
183                         out = ioc | SPI_IOC_DO;
184                 else
185                         out = ioc & ~SPI_IOC_DO;
186
187                 /* setup MSB (to slave) on trailing edge */
188                 pb44_spi_wr(sp, SPI_REG_IOC, out);
189                 pb44_spi_wr(sp, SPI_REG_IOC, out | SPI_IOC_CLK);
190
191                 word <<= 1;
192
193 #ifdef PER_BIT_READ
194                 /* sample MSB (from slave) on leading edge */
195                 ret = pb44_spi_rr(sp, SPI_REG_RDS);
196                 pb44_spi_wr(sp, SPI_REG_IOC, out);
197 #endif
198         }
199
200 #ifndef PER_BIT_READ
201         ret = pb44_spi_rr(sp, SPI_REG_RDS);
202 #endif
203         return ret;
204 }
205
206 static int pb44_spi_probe(struct platform_device *pdev)
207 {
208         struct spi_master *master;
209         struct ar71xx_spi *sp;
210         struct ar71xx_spi_platform_data *pdata;
211         struct resource *r;
212         int ret;
213
214         master = spi_alloc_master(&pdev->dev, sizeof(*sp));
215         if (master == NULL) {
216                 dev_err(&pdev->dev, "failed to allocate spi master\n");
217                 return -ENOMEM;
218         }
219
220         sp = spi_master_get_devdata(master);
221         platform_set_drvdata(pdev, sp);
222
223         pdata = pdev->dev.platform_data;
224
225         master->setup = pb44_spi_setup;
226         master->cleanup = pb44_spi_cleanup;
227         if (pdata) {
228                 master->bus_num = pdata->bus_num;
229                 master->num_chipselect = pdata->num_chipselect;
230         } else {
231                 master->bus_num = 0;
232                 master->num_chipselect = 1;
233         }
234
235         sp->bitbang.master = spi_master_get(master);
236         sp->bitbang.chipselect = pb44_spi_chipselect;
237         sp->bitbang.txrx_word[SPI_MODE_0] = pb44_spi_txrx_mode0;
238         sp->bitbang.setup_transfer = spi_bitbang_setup_transfer;
239         sp->bitbang.flags = SPI_CS_HIGH;
240
241         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
242         if (r == NULL) {
243                 ret = -ENOENT;
244                 goto err1;
245         }
246
247         sp->base = ioremap_nocache(r->start, r->end - r->start + 1);
248         if (!sp->base) {
249                 ret = -ENXIO;
250                 goto err1;
251         }
252
253         pb44_spi_enable(sp);
254
255         ret = spi_bitbang_start(&sp->bitbang);
256         if (!ret)
257                 return 0;
258
259         pb44_spi_disable(sp);
260         iounmap(sp->base);
261 err1:
262         platform_set_drvdata(pdev, NULL);
263         spi_master_put(sp->bitbang.master);
264
265         return ret;
266 }
267
268 static int pb44_spi_remove(struct platform_device *pdev)
269 {
270         struct ar71xx_spi *sp = platform_get_drvdata(pdev);
271
272         spi_bitbang_stop(&sp->bitbang);
273         pb44_spi_disable(sp);
274         iounmap(sp->base);
275         platform_set_drvdata(pdev, NULL);
276         spi_master_put(sp->bitbang.master);
277
278         return 0;
279 }
280
281 static void pb44_spi_shutdown(struct platform_device *pdev)
282 {
283         int ret;
284
285         ret = pb44_spi_remove(pdev);
286         if (ret)
287                 dev_err(&pdev->dev, "shutdown failed with %d\n", ret);
288 }
289
290 static struct platform_driver pb44_spi_drv = {
291         .probe          = pb44_spi_probe,
292         .remove         = pb44_spi_remove,
293         .shutdown       = pb44_spi_shutdown,
294         .driver         = {
295                 .name   = DRV_NAME,
296                 .owner  = THIS_MODULE,
297         },
298 };
299
300 static int __init pb44_spi_init(void)
301 {
302         return platform_driver_register(&pb44_spi_drv);
303 }
304 module_init(pb44_spi_init);
305
306 static void __exit pb44_spi_exit(void)
307 {
308         platform_driver_unregister(&pb44_spi_drv);
309 }
310 module_exit(pb44_spi_exit);
311
312 MODULE_ALIAS("platform:" DRV_NAME);
313 MODULE_DESCRIPTION(DRV_DESC);
314 MODULE_VERSION(DRV_VERSION);
315 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
316 MODULE_LICENSE("GPL v2");