2 * Atheros AP83 board specific 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 AP83 board SPI Controller driver"
28 #define DRV_VERSION "0.1.0"
29 #define DRV_NAME "ap83-spi"
31 #define AP83_SPI_CLK_HIGH (1 << 23)
32 #define AP83_SPI_CLK_LOW 0
33 #define AP83_SPI_MOSI_HIGH (1 << 22)
34 #define AP83_SPI_MOSI_LOW 0
36 #define AP83_SPI_GPIO_CS 1
37 #define AP83_SPI_GPIO_MISO 3
40 struct spi_bitbang bitbang
;
44 struct platform_device
*pdev
;
47 static inline u32
ap83_spi_rr(struct ap83_spi
*sp
, u32 reg
)
49 return __raw_readl(sp
->base
+ reg
);
52 static inline struct ap83_spi
*spidev_to_sp(struct spi_device
*spi
)
54 return spi_master_get_devdata(spi
->master
);
57 static inline void setsck(struct spi_device
*spi
, int val
)
59 struct ap83_spi
*sp
= spidev_to_sp(spi
);
62 sp
->addr
|= AP83_SPI_CLK_HIGH
;
64 sp
->addr
&= ~AP83_SPI_CLK_HIGH
;
66 dev_dbg(&spi
->dev
, "addr=%08x, SCK set to %s\n",
67 sp
->addr
, (val
) ? "HIGH" : "LOW");
69 ap83_spi_rr(sp
, sp
->addr
);
72 static inline void setmosi(struct spi_device
*spi
, int val
)
74 struct ap83_spi
*sp
= spidev_to_sp(spi
);
77 sp
->addr
|= AP83_SPI_MOSI_HIGH
;
79 sp
->addr
&= ~AP83_SPI_MOSI_HIGH
;
81 dev_dbg(&spi
->dev
, "addr=%08x, MOSI set to %s\n",
82 sp
->addr
, (val
) ? "HIGH" : "LOW");
84 ap83_spi_rr(sp
, sp
->addr
);
87 static inline u32
getmiso(struct spi_device
*spi
)
91 ret
= gpio_get_value(AP83_SPI_GPIO_MISO
) ? 1 : 0;
92 dev_dbg(&spi
->dev
, "get MISO: %d\n", ret
);
97 static inline void do_spidelay(struct spi_device
*spi
, unsigned nsecs
)
102 static void ap83_spi_chipselect(struct spi_device
*spi
, int on
)
104 struct ap83_spi
*sp
= spidev_to_sp(spi
);
106 dev_dbg(&spi
->dev
, "set CS to %d\n", (on
) ? 0 : 1);
109 ar71xx_flash_acquire();
112 ap83_spi_rr(sp
, sp
->addr
);
114 gpio_set_value(AP83_SPI_GPIO_CS
, 0);
116 gpio_set_value(AP83_SPI_GPIO_CS
, 1);
117 ar71xx_flash_release();
121 #define spidelay(nsecs) \
123 /* Steal the spi_device pointer from our caller. \
124 * The bitbang-API should probably get fixed here... */ \
125 do_spidelay(spi, nsecs); \
128 #define EXPAND_BITBANG_TXRX
129 #include <linux/spi/spi_bitbang.h>
131 static u32
ap83_spi_txrx_mode0(struct spi_device
*spi
,
132 unsigned nsecs
, u32 word
, u8 bits
)
134 dev_dbg(&spi
->dev
, "TXRX0 word=%08x, bits=%u\n", word
, bits
);
135 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
138 static u32
ap83_spi_txrx_mode1(struct spi_device
*spi
,
139 unsigned nsecs
, u32 word
, u8 bits
)
141 dev_dbg(&spi
->dev
, "TXRX1 word=%08x, bits=%u\n", word
, bits
);
142 return bitbang_txrx_be_cpha1(spi
, nsecs
, 0, word
, bits
);
145 static u32
ap83_spi_txrx_mode2(struct spi_device
*spi
,
146 unsigned nsecs
, u32 word
, u8 bits
)
148 dev_dbg(&spi
->dev
, "TXRX2 word=%08x, bits=%u\n", word
, bits
);
149 return bitbang_txrx_be_cpha0(spi
, nsecs
, 1, word
, bits
);
152 static u32
ap83_spi_txrx_mode3(struct spi_device
*spi
,
153 unsigned nsecs
, u32 word
, u8 bits
)
155 dev_dbg(&spi
->dev
, "TXRX3 word=%08x, bits=%u\n", word
, bits
);
156 return bitbang_txrx_be_cpha1(spi
, nsecs
, 1, word
, bits
);
159 static int ap83_spi_probe(struct platform_device
*pdev
)
161 struct spi_master
*master
;
163 struct ap83_spi_platform_data
*pdata
;
167 ret
= gpio_request(AP83_SPI_GPIO_MISO
, "spi-miso");
169 dev_err(&pdev
->dev
, "gpio request failed for MISO\n");
173 ret
= gpio_request(AP83_SPI_GPIO_CS
, "spi-cs");
175 dev_err(&pdev
->dev
, "gpio request failed for CS\n");
179 ret
= gpio_direction_input(AP83_SPI_GPIO_MISO
);
181 dev_err(&pdev
->dev
, "unable to set direction of MISO\n");
185 ret
= gpio_direction_output(AP83_SPI_GPIO_CS
, 0);
187 dev_err(&pdev
->dev
, "unable to set direction of CS\n");
191 master
= spi_alloc_master(&pdev
->dev
, sizeof(*sp
));
192 if (master
== NULL
) {
193 dev_err(&pdev
->dev
, "failed to allocate spi master\n");
197 sp
= spi_master_get_devdata(master
);
198 platform_set_drvdata(pdev
, sp
);
200 pdata
= pdev
->dev
.platform_data
;
202 sp
->bitbang
.master
= spi_master_get(master
);
203 sp
->bitbang
.chipselect
= ap83_spi_chipselect
;
204 sp
->bitbang
.txrx_word
[SPI_MODE_0
] = ap83_spi_txrx_mode0
;
205 sp
->bitbang
.txrx_word
[SPI_MODE_1
] = ap83_spi_txrx_mode1
;
206 sp
->bitbang
.txrx_word
[SPI_MODE_2
] = ap83_spi_txrx_mode2
;
207 sp
->bitbang
.txrx_word
[SPI_MODE_3
] = ap83_spi_txrx_mode3
;
209 sp
->bitbang
.master
->bus_num
= pdev
->id
;
210 sp
->bitbang
.master
->num_chipselect
= 1;
212 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
218 sp
->base
= ioremap_nocache(r
->start
, r
->end
- r
->start
+ 1);
224 ret
= spi_bitbang_start(&sp
->bitbang
);
228 dev_info(&pdev
->dev
, "AP83 SPI adapter at %08x\n", r
->start
);
235 platform_set_drvdata(pdev
, NULL
);
236 spi_master_put(sp
->bitbang
.master
);
239 gpio_free(AP83_SPI_GPIO_CS
);
241 gpio_free(AP83_SPI_GPIO_MISO
);
245 static int ap83_spi_remove(struct platform_device
*pdev
)
247 struct ap83_spi
*sp
= platform_get_drvdata(pdev
);
249 spi_bitbang_stop(&sp
->bitbang
);
251 platform_set_drvdata(pdev
, NULL
);
252 spi_master_put(sp
->bitbang
.master
);
257 static struct platform_driver ap83_spi_drv
= {
258 .probe
= ap83_spi_probe
,
259 .remove
= ap83_spi_remove
,
262 .owner
= THIS_MODULE
,
266 static int __init
ap83_spi_init(void)
268 return platform_driver_register(&ap83_spi_drv
);
270 module_init(ap83_spi_init
);
272 static void __exit
ap83_spi_exit(void)
274 platform_driver_unregister(&ap83_spi_drv
);
276 module_exit(ap83_spi_exit
);
278 MODULE_ALIAS("platform:" DRV_NAME
);
279 MODULE_DESCRIPTION(DRV_DESC
);
280 MODULE_VERSION(DRV_VERSION
);
281 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
282 MODULE_LICENSE("GPL v2");