2 * Atheros PB44 board SPI controller driver
4 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
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.
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>
19 #include <linux/spi/spi.h>
20 #include <linux/spi/spi_bitbang.h>
21 #include <linux/bitops.h>
22 #include <linux/gpio.h>
24 #include <asm/mach-ar71xx/ar71xx.h>
25 #include <asm/mach-ar71xx/platform.h>
27 #define DRV_DESC "Atheros PB44 SPI Controller driver"
28 #define DRV_VERSION "0.1.0"
29 #define DRV_NAME "pb44-spi"
34 struct spi_bitbang bitbang
;
40 struct platform_device
*pdev
;
43 static inline u32
pb44_spi_rr(struct ar71xx_spi
*sp
, unsigned reg
)
45 return __raw_readl(sp
->base
+ reg
);
48 static inline void pb44_spi_wr(struct ar71xx_spi
*sp
, unsigned reg
, u32 val
)
50 __raw_writel(val
, sp
->base
+ reg
);
53 static inline struct ar71xx_spi
*spidev_to_sp(struct spi_device
*spi
)
55 return spi_master_get_devdata(spi
->master
);
58 static void pb44_spi_chipselect(struct spi_device
*spi
, int is_active
)
60 struct ar71xx_spi
*sp
= spidev_to_sp(spi
);
61 int cs_high
= (spi
->mode
& SPI_CS_HIGH
) ? is_active
: !is_active
;
64 /* set initial clock polarity */
65 if (spi
->mode
& SPI_CPOL
)
66 sp
->ioc_base
|= SPI_IOC_CLK
;
68 sp
->ioc_base
&= ~SPI_IOC_CLK
;
70 pb44_spi_wr(sp
, SPI_REG_IOC
, sp
->ioc_base
);
73 if (spi
->chip_select
) {
74 unsigned long gpio
= (unsigned long) spi
->controller_data
;
76 /* SPI is normally active-low */
77 gpio_set_value(gpio
, cs_high
);
80 sp
->ioc_base
|= SPI_IOC_CS0
;
82 sp
->ioc_base
&= ~SPI_IOC_CS0
;
84 pb44_spi_wr(sp
, SPI_REG_IOC
, sp
->ioc_base
);
89 static int pb44_spi_setup_cs(struct spi_device
*spi
)
91 struct ar71xx_spi
*sp
= spidev_to_sp(spi
);
93 /* enable GPIO mode */
94 pb44_spi_wr(sp
, SPI_REG_FS
, SPI_FS_GPIO
);
96 /* save CTRL register */
97 sp
->reg_ctrl
= pb44_spi_rr(sp
, SPI_REG_CTRL
);
98 sp
->ioc_base
= pb44_spi_rr(sp
, SPI_REG_IOC
);
100 /* TODO: setup speed? */
101 pb44_spi_wr(sp
, SPI_REG_CTRL
, 0x43);
103 if (spi
->chip_select
) {
104 unsigned long gpio
= (unsigned long) spi
->controller_data
;
107 status
= gpio_request(gpio
, dev_name(&spi
->dev
));
111 status
= gpio_direction_output(gpio
, spi
->mode
& SPI_CS_HIGH
);
117 if (spi
->mode
& SPI_CS_HIGH
)
118 sp
->ioc_base
|= SPI_IOC_CS0
;
120 sp
->ioc_base
&= ~SPI_IOC_CS0
;
121 pb44_spi_wr(sp
, SPI_REG_IOC
, sp
->ioc_base
);
127 static void pb44_spi_cleanup_cs(struct spi_device
*spi
)
129 struct ar71xx_spi
*sp
= spidev_to_sp(spi
);
131 if (spi
->chip_select
) {
132 unsigned long gpio
= (unsigned long) spi
->controller_data
;
136 /* restore CTRL register */
137 pb44_spi_wr(sp
, SPI_REG_CTRL
, sp
->reg_ctrl
);
138 /* disable GPIO mode */
139 pb44_spi_wr(sp
, SPI_REG_FS
, 0);
142 static int pb44_spi_setup(struct spi_device
*spi
)
146 if (spi
->bits_per_word
> 32)
149 if (!spi
->controller_state
) {
150 status
= pb44_spi_setup_cs(spi
);
155 status
= spi_bitbang_setup(spi
);
156 if (status
&& !spi
->controller_state
)
157 pb44_spi_cleanup_cs(spi
);
162 static void pb44_spi_cleanup(struct spi_device
*spi
)
164 pb44_spi_cleanup_cs(spi
);
165 spi_bitbang_cleanup(spi
);
168 static u32
pb44_spi_txrx_mode0(struct spi_device
*spi
, unsigned nsecs
,
171 struct ar71xx_spi
*sp
= spidev_to_sp(spi
);
172 u32 ioc
= sp
->ioc_base
;
175 /* clock starts at inactive polarity */
176 for (word
<<= (32 - bits
); likely(bits
); bits
--) {
179 if (word
& (1 << 31))
180 out
= ioc
| SPI_IOC_DO
;
182 out
= ioc
& ~SPI_IOC_DO
;
184 /* setup MSB (to slave) on trailing edge */
185 pb44_spi_wr(sp
, SPI_REG_IOC
, out
);
186 pb44_spi_wr(sp
, SPI_REG_IOC
, out
| SPI_IOC_CLK
);
191 /* sample MSB (from slave) on leading edge */
192 ret
= pb44_spi_rr(sp
, SPI_REG_RDS
);
193 pb44_spi_wr(sp
, SPI_REG_IOC
, out
);
198 ret
= pb44_spi_rr(sp
, SPI_REG_RDS
);
203 static int pb44_spi_probe(struct platform_device
*pdev
)
205 struct spi_master
*master
;
206 struct ar71xx_spi
*sp
;
207 struct ar71xx_spi_platform_data
*pdata
;
211 master
= spi_alloc_master(&pdev
->dev
, sizeof(*sp
));
212 if (master
== NULL
) {
213 dev_err(&pdev
->dev
, "failed to allocate spi master\n");
217 sp
= spi_master_get_devdata(master
);
218 platform_set_drvdata(pdev
, sp
);
220 pdata
= pdev
->dev
.platform_data
;
222 master
->setup
= pb44_spi_setup
;
223 master
->cleanup
= pb44_spi_cleanup
;
225 master
->bus_num
= pdata
->bus_num
;
226 master
->num_chipselect
= pdata
->num_chipselect
;
229 master
->num_chipselect
= 1;
232 sp
->bitbang
.master
= spi_master_get(master
);
233 sp
->bitbang
.chipselect
= pb44_spi_chipselect
;
234 sp
->bitbang
.txrx_word
[SPI_MODE_0
] = pb44_spi_txrx_mode0
;
235 sp
->bitbang
.setup_transfer
= spi_bitbang_setup_transfer
;
236 sp
->bitbang
.flags
= SPI_CS_HIGH
;
238 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
244 sp
->base
= ioremap_nocache(r
->start
, r
->end
- r
->start
+ 1);
250 ret
= spi_bitbang_start(&sp
->bitbang
);
256 platform_set_drvdata(pdev
, NULL
);
257 spi_master_put(sp
->bitbang
.master
);
262 static int pb44_spi_remove(struct platform_device
*pdev
)
264 struct ar71xx_spi
*sp
= platform_get_drvdata(pdev
);
266 spi_bitbang_stop(&sp
->bitbang
);
268 platform_set_drvdata(pdev
, NULL
);
269 spi_master_put(sp
->bitbang
.master
);
274 static struct platform_driver pb44_spi_drv
= {
275 .probe
= pb44_spi_probe
,
276 .remove
= pb44_spi_remove
,
279 .owner
= THIS_MODULE
,
283 static int __init
pb44_spi_init(void)
285 return platform_driver_register(&pb44_spi_drv
);
287 module_init(pb44_spi_init
);
289 static void __exit
pb44_spi_exit(void)
291 platform_driver_unregister(&pb44_spi_drv
);
293 module_exit(pb44_spi_exit
);
295 MODULE_ALIAS("platform:" DRV_NAME
);
296 MODULE_DESCRIPTION(DRV_DESC
);
297 MODULE_VERSION(DRV_VERSION
);
298 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
299 MODULE_LICENSE("GPL v2");