Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * OpenCores tiny SPI master driver
  4 *
  5 * https://opencores.org/project,tiny_spi
  6 *
  7 * Copyright (C) 2011 Thomas Chou <thomas@wytron.com.tw>
  8 *
  9 * Based on spi_s3c24xx.c, which is:
 10 * Copyright (c) 2006 Ben Dooks
 11 * Copyright (c) 2006 Simtec Electronics
 12 *	Ben Dooks <ben@simtec.co.uk>
 
 
 
 
 13 */
 14
 
 15#include <linux/interrupt.h>
 16#include <linux/errno.h>
 17#include <linux/module.h>
 18#include <linux/platform_device.h>
 19#include <linux/spi/spi.h>
 20#include <linux/spi/spi_bitbang.h>
 21#include <linux/spi/spi_oc_tiny.h>
 22#include <linux/io.h>
 
 23#include <linux/of.h>
 24
 25#define DRV_NAME "spi_oc_tiny"
 26
 27#define TINY_SPI_RXDATA 0
 28#define TINY_SPI_TXDATA 4
 29#define TINY_SPI_STATUS 8
 30#define TINY_SPI_CONTROL 12
 31#define TINY_SPI_BAUD 16
 32
 33#define TINY_SPI_STATUS_TXE 0x1
 34#define TINY_SPI_STATUS_TXR 0x2
 35
 36struct tiny_spi {
 37	/* bitbang has to be first */
 38	struct spi_bitbang bitbang;
 39	struct completion done;
 40
 41	void __iomem *base;
 42	int irq;
 43	unsigned int freq;
 44	unsigned int baudwidth;
 45	unsigned int baud;
 46	unsigned int speed_hz;
 47	unsigned int mode;
 48	unsigned int len;
 49	unsigned int txc, rxc;
 50	const u8 *txp;
 51	u8 *rxp;
 
 
 52};
 53
 54static inline struct tiny_spi *tiny_spi_to_hw(struct spi_device *sdev)
 55{
 56	return spi_master_get_devdata(sdev->master);
 57}
 58
 59static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
 60{
 61	struct tiny_spi *hw = tiny_spi_to_hw(spi);
 62
 63	return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
 64}
 65
 
 
 
 
 
 
 
 
 
 
 66static int tiny_spi_setup_transfer(struct spi_device *spi,
 67				   struct spi_transfer *t)
 68{
 69	struct tiny_spi *hw = tiny_spi_to_hw(spi);
 70	unsigned int baud = hw->baud;
 71
 72	if (t) {
 73		if (t->speed_hz && t->speed_hz != hw->speed_hz)
 74			baud = tiny_spi_baud(spi, t->speed_hz);
 75	}
 76	writel(baud, hw->base + TINY_SPI_BAUD);
 77	writel(hw->mode, hw->base + TINY_SPI_CONTROL);
 78	return 0;
 79}
 80
 81static int tiny_spi_setup(struct spi_device *spi)
 82{
 83	struct tiny_spi *hw = tiny_spi_to_hw(spi);
 84
 85	if (spi->max_speed_hz != hw->speed_hz) {
 86		hw->speed_hz = spi->max_speed_hz;
 87		hw->baud = tiny_spi_baud(spi, hw->speed_hz);
 88	}
 89	hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
 90	return 0;
 91}
 92
 93static inline void tiny_spi_wait_txr(struct tiny_spi *hw)
 94{
 95	while (!(readb(hw->base + TINY_SPI_STATUS) &
 96		 TINY_SPI_STATUS_TXR))
 97		cpu_relax();
 98}
 99
100static inline void tiny_spi_wait_txe(struct tiny_spi *hw)
101{
102	while (!(readb(hw->base + TINY_SPI_STATUS) &
103		 TINY_SPI_STATUS_TXE))
104		cpu_relax();
105}
106
107static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
108{
109	struct tiny_spi *hw = tiny_spi_to_hw(spi);
110	const u8 *txp = t->tx_buf;
111	u8 *rxp = t->rx_buf;
112	unsigned int i;
113
114	if (hw->irq >= 0) {
115		/* use interrupt driven data transfer */
116		hw->len = t->len;
117		hw->txp = t->tx_buf;
118		hw->rxp = t->rx_buf;
119		hw->txc = 0;
120		hw->rxc = 0;
121
122		/* send the first byte */
123		if (t->len > 1) {
124			writeb(hw->txp ? *hw->txp++ : 0,
125			       hw->base + TINY_SPI_TXDATA);
126			hw->txc++;
127			writeb(hw->txp ? *hw->txp++ : 0,
128			       hw->base + TINY_SPI_TXDATA);
129			hw->txc++;
130			writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
131		} else {
132			writeb(hw->txp ? *hw->txp++ : 0,
133			       hw->base + TINY_SPI_TXDATA);
134			hw->txc++;
135			writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
136		}
137
138		wait_for_completion(&hw->done);
139	} else {
140		/* we need to tighten the transfer loop */
141		writeb(txp ? *txp++ : 0, hw->base + TINY_SPI_TXDATA);
142		for (i = 1; i < t->len; i++) {
143			writeb(txp ? *txp++ : 0, hw->base + TINY_SPI_TXDATA);
144
145			if (rxp || (i != t->len - 1))
146				tiny_spi_wait_txr(hw);
147			if (rxp)
148				*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149		}
150		tiny_spi_wait_txe(hw);
151		if (rxp)
152			*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
153	}
154
155	return t->len;
156}
157
158static irqreturn_t tiny_spi_irq(int irq, void *dev)
159{
160	struct tiny_spi *hw = dev;
161
162	writeb(0, hw->base + TINY_SPI_STATUS);
163	if (hw->rxc + 1 == hw->len) {
164		if (hw->rxp)
165			*hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
166		hw->rxc++;
167		complete(&hw->done);
168	} else {
169		if (hw->rxp)
170			*hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
171		hw->rxc++;
172		if (hw->txc < hw->len) {
173			writeb(hw->txp ? *hw->txp++ : 0,
174			       hw->base + TINY_SPI_TXDATA);
175			hw->txc++;
176			writeb(TINY_SPI_STATUS_TXR,
177			       hw->base + TINY_SPI_STATUS);
178		} else {
179			writeb(TINY_SPI_STATUS_TXE,
180			       hw->base + TINY_SPI_STATUS);
181		}
182	}
183	return IRQ_HANDLED;
184}
185
186#ifdef CONFIG_OF
187#include <linux/of_gpio.h>
188
189static int tiny_spi_of_probe(struct platform_device *pdev)
190{
191	struct tiny_spi *hw = platform_get_drvdata(pdev);
192	struct device_node *np = pdev->dev.of_node;
193	u32 val;
 
 
194
195	if (!np)
196		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
197	hw->bitbang.master->dev.of_node = pdev->dev.of_node;
198	if (!of_property_read_u32(np, "clock-frequency", &val))
199		hw->freq = val;
200	if (!of_property_read_u32(np, "baud-width", &val))
201		hw->baudwidth = val;
 
 
 
202	return 0;
203}
204#else /* !CONFIG_OF */
205static int tiny_spi_of_probe(struct platform_device *pdev)
206{
207	return 0;
208}
209#endif /* CONFIG_OF */
210
211static int tiny_spi_probe(struct platform_device *pdev)
212{
213	struct tiny_spi_platform_data *platp = dev_get_platdata(&pdev->dev);
214	struct tiny_spi *hw;
215	struct spi_master *master;
 
 
216	int err = -ENODEV;
217
218	master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
219	if (!master)
220		return err;
221
222	/* setup the master state. */
223	master->bus_num = pdev->id;
 
224	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
225	master->setup = tiny_spi_setup;
226	master->use_gpio_descriptors = true;
227
228	hw = spi_master_get_devdata(master);
229	platform_set_drvdata(pdev, hw);
230
231	/* setup the state for the bitbang driver */
232	hw->bitbang.master = master;
 
 
233	hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
 
234	hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
235
236	/* find and map our resources */
237	hw->base = devm_platform_ioremap_resource(pdev, 0);
238	if (IS_ERR(hw->base)) {
239		err = PTR_ERR(hw->base);
240		goto exit;
241	}
 
 
 
 
 
242	/* irq is optional */
243	hw->irq = platform_get_irq(pdev, 0);
244	if (hw->irq >= 0) {
245		init_completion(&hw->done);
246		err = devm_request_irq(&pdev->dev, hw->irq, tiny_spi_irq, 0,
247				       pdev->name, hw);
248		if (err)
249			goto exit;
250	}
251	/* find platform data */
252	if (platp) {
 
 
 
 
253		hw->freq = platp->freq;
254		hw->baudwidth = platp->baudwidth;
255	} else {
256		err = tiny_spi_of_probe(pdev);
257		if (err)
258			goto exit;
259	}
 
 
 
 
 
 
 
260
261	/* register our spi controller */
262	err = spi_bitbang_start(&hw->bitbang);
263	if (err)
264		goto exit;
265	dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
266
267	return 0;
268
 
 
 
 
 
269exit:
 
270	spi_master_put(master);
271	return err;
272}
273
274static int tiny_spi_remove(struct platform_device *pdev)
275{
276	struct tiny_spi *hw = platform_get_drvdata(pdev);
277	struct spi_master *master = hw->bitbang.master;
 
278
279	spi_bitbang_stop(&hw->bitbang);
 
 
 
280	spi_master_put(master);
281	return 0;
282}
283
284#ifdef CONFIG_OF
285static const struct of_device_id tiny_spi_match[] = {
286	{ .compatible = "opencores,tiny-spi-rtlsvn2", },
287	{},
288};
289MODULE_DEVICE_TABLE(of, tiny_spi_match);
 
 
290#endif /* CONFIG_OF */
291
292static struct platform_driver tiny_spi_driver = {
293	.probe = tiny_spi_probe,
294	.remove = tiny_spi_remove,
295	.driver = {
296		.name = DRV_NAME,
 
297		.pm = NULL,
298		.of_match_table = of_match_ptr(tiny_spi_match),
299	},
300};
301module_platform_driver(tiny_spi_driver);
302
303MODULE_DESCRIPTION("OpenCores tiny SPI driver");
304MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
305MODULE_LICENSE("GPL");
306MODULE_ALIAS("platform:" DRV_NAME);
v3.5.6
 
  1/*
  2 * OpenCores tiny SPI master driver
  3 *
  4 * http://opencores.org/project,tiny_spi
  5 *
  6 * Copyright (C) 2011 Thomas Chou <thomas@wytron.com.tw>
  7 *
  8 * Based on spi_s3c24xx.c, which is:
  9 * Copyright (c) 2006 Ben Dooks
 10 * Copyright (c) 2006 Simtec Electronics
 11 *	Ben Dooks <ben@simtec.co.uk>
 12 *
 13 * This program is free software; you can redistribute it and/or modify
 14 * it under the terms of the GNU General Public License version 2 as
 15 * published by the Free Software Foundation.
 16 */
 17
 18#include <linux/init.h>
 19#include <linux/interrupt.h>
 20#include <linux/errno.h>
 21#include <linux/module.h>
 22#include <linux/platform_device.h>
 23#include <linux/spi/spi.h>
 24#include <linux/spi/spi_bitbang.h>
 25#include <linux/spi/spi_oc_tiny.h>
 26#include <linux/io.h>
 27#include <linux/gpio.h>
 28#include <linux/of.h>
 29
 30#define DRV_NAME "spi_oc_tiny"
 31
 32#define TINY_SPI_RXDATA 0
 33#define TINY_SPI_TXDATA 4
 34#define TINY_SPI_STATUS 8
 35#define TINY_SPI_CONTROL 12
 36#define TINY_SPI_BAUD 16
 37
 38#define TINY_SPI_STATUS_TXE 0x1
 39#define TINY_SPI_STATUS_TXR 0x2
 40
 41struct tiny_spi {
 42	/* bitbang has to be first */
 43	struct spi_bitbang bitbang;
 44	struct completion done;
 45
 46	void __iomem *base;
 47	int irq;
 48	unsigned int freq;
 49	unsigned int baudwidth;
 50	unsigned int baud;
 51	unsigned int speed_hz;
 52	unsigned int mode;
 53	unsigned int len;
 54	unsigned int txc, rxc;
 55	const u8 *txp;
 56	u8 *rxp;
 57	unsigned int gpio_cs_count;
 58	int *gpio_cs;
 59};
 60
 61static inline struct tiny_spi *tiny_spi_to_hw(struct spi_device *sdev)
 62{
 63	return spi_master_get_devdata(sdev->master);
 64}
 65
 66static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
 67{
 68	struct tiny_spi *hw = tiny_spi_to_hw(spi);
 69
 70	return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
 71}
 72
 73static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
 74{
 75	struct tiny_spi *hw = tiny_spi_to_hw(spi);
 76
 77	if (hw->gpio_cs_count) {
 78		gpio_set_value(hw->gpio_cs[spi->chip_select],
 79			(spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
 80	}
 81}
 82
 83static int tiny_spi_setup_transfer(struct spi_device *spi,
 84				   struct spi_transfer *t)
 85{
 86	struct tiny_spi *hw = tiny_spi_to_hw(spi);
 87	unsigned int baud = hw->baud;
 88
 89	if (t) {
 90		if (t->speed_hz && t->speed_hz != hw->speed_hz)
 91			baud = tiny_spi_baud(spi, t->speed_hz);
 92	}
 93	writel(baud, hw->base + TINY_SPI_BAUD);
 94	writel(hw->mode, hw->base + TINY_SPI_CONTROL);
 95	return 0;
 96}
 97
 98static int tiny_spi_setup(struct spi_device *spi)
 99{
100	struct tiny_spi *hw = tiny_spi_to_hw(spi);
101
102	if (spi->max_speed_hz != hw->speed_hz) {
103		hw->speed_hz = spi->max_speed_hz;
104		hw->baud = tiny_spi_baud(spi, hw->speed_hz);
105	}
106	hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
107	return 0;
108}
109
110static inline void tiny_spi_wait_txr(struct tiny_spi *hw)
111{
112	while (!(readb(hw->base + TINY_SPI_STATUS) &
113		 TINY_SPI_STATUS_TXR))
114		cpu_relax();
115}
116
117static inline void tiny_spi_wait_txe(struct tiny_spi *hw)
118{
119	while (!(readb(hw->base + TINY_SPI_STATUS) &
120		 TINY_SPI_STATUS_TXE))
121		cpu_relax();
122}
123
124static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
125{
126	struct tiny_spi *hw = tiny_spi_to_hw(spi);
127	const u8 *txp = t->tx_buf;
128	u8 *rxp = t->rx_buf;
129	unsigned int i;
130
131	if (hw->irq >= 0) {
132		/* use intrrupt driven data transfer */
133		hw->len = t->len;
134		hw->txp = t->tx_buf;
135		hw->rxp = t->rx_buf;
136		hw->txc = 0;
137		hw->rxc = 0;
138
139		/* send the first byte */
140		if (t->len > 1) {
141			writeb(hw->txp ? *hw->txp++ : 0,
142			       hw->base + TINY_SPI_TXDATA);
143			hw->txc++;
144			writeb(hw->txp ? *hw->txp++ : 0,
145			       hw->base + TINY_SPI_TXDATA);
146			hw->txc++;
147			writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
148		} else {
149			writeb(hw->txp ? *hw->txp++ : 0,
150			       hw->base + TINY_SPI_TXDATA);
151			hw->txc++;
152			writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
153		}
154
155		wait_for_completion(&hw->done);
156	} else if (txp && rxp) {
157		/* we need to tighten the transfer loop */
158		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
159		if (t->len > 1) {
160			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
161			for (i = 2; i < t->len; i++) {
162				u8 rx, tx = *txp++;
163				tiny_spi_wait_txr(hw);
164				rx = readb(hw->base + TINY_SPI_TXDATA);
165				writeb(tx, hw->base + TINY_SPI_TXDATA);
166				*rxp++ = rx;
167			}
168			tiny_spi_wait_txr(hw);
169			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
170		}
171		tiny_spi_wait_txe(hw);
172		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
173	} else if (rxp) {
174		writeb(0, hw->base + TINY_SPI_TXDATA);
175		if (t->len > 1) {
176			writeb(0,
177			       hw->base + TINY_SPI_TXDATA);
178			for (i = 2; i < t->len; i++) {
179				u8 rx;
180				tiny_spi_wait_txr(hw);
181				rx = readb(hw->base + TINY_SPI_TXDATA);
182				writeb(0, hw->base + TINY_SPI_TXDATA);
183				*rxp++ = rx;
184			}
185			tiny_spi_wait_txr(hw);
186			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
187		}
188		tiny_spi_wait_txe(hw);
189		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
190	} else if (txp) {
191		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
192		if (t->len > 1) {
193			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
194			for (i = 2; i < t->len; i++) {
195				u8 tx = *txp++;
196				tiny_spi_wait_txr(hw);
197				writeb(tx, hw->base + TINY_SPI_TXDATA);
198			}
199		}
200		tiny_spi_wait_txe(hw);
201	} else {
202		writeb(0, hw->base + TINY_SPI_TXDATA);
203		if (t->len > 1) {
204			writeb(0, hw->base + TINY_SPI_TXDATA);
205			for (i = 2; i < t->len; i++) {
206				tiny_spi_wait_txr(hw);
207				writeb(0, hw->base + TINY_SPI_TXDATA);
208			}
209		}
210		tiny_spi_wait_txe(hw);
 
 
211	}
 
212	return t->len;
213}
214
215static irqreturn_t tiny_spi_irq(int irq, void *dev)
216{
217	struct tiny_spi *hw = dev;
218
219	writeb(0, hw->base + TINY_SPI_STATUS);
220	if (hw->rxc + 1 == hw->len) {
221		if (hw->rxp)
222			*hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
223		hw->rxc++;
224		complete(&hw->done);
225	} else {
226		if (hw->rxp)
227			*hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
228		hw->rxc++;
229		if (hw->txc < hw->len) {
230			writeb(hw->txp ? *hw->txp++ : 0,
231			       hw->base + TINY_SPI_TXDATA);
232			hw->txc++;
233			writeb(TINY_SPI_STATUS_TXR,
234			       hw->base + TINY_SPI_STATUS);
235		} else {
236			writeb(TINY_SPI_STATUS_TXE,
237			       hw->base + TINY_SPI_STATUS);
238		}
239	}
240	return IRQ_HANDLED;
241}
242
243#ifdef CONFIG_OF
244#include <linux/of_gpio.h>
245
246static int __devinit tiny_spi_of_probe(struct platform_device *pdev)
247{
248	struct tiny_spi *hw = platform_get_drvdata(pdev);
249	struct device_node *np = pdev->dev.of_node;
250	unsigned int i;
251	const __be32 *val;
252	int len;
253
254	if (!np)
255		return 0;
256	hw->gpio_cs_count = of_gpio_count(np);
257	if (hw->gpio_cs_count) {
258		hw->gpio_cs = devm_kzalloc(&pdev->dev,
259				hw->gpio_cs_count * sizeof(unsigned int),
260				GFP_KERNEL);
261		if (!hw->gpio_cs)
262			return -ENOMEM;
263	}
264	for (i = 0; i < hw->gpio_cs_count; i++) {
265		hw->gpio_cs[i] = of_get_gpio_flags(np, i, NULL);
266		if (hw->gpio_cs[i] < 0)
267			return -ENODEV;
268	}
269	hw->bitbang.master->dev.of_node = pdev->dev.of_node;
270	val = of_get_property(pdev->dev.of_node,
271			      "clock-frequency", &len);
272	if (val && len >= sizeof(__be32))
273		hw->freq = be32_to_cpup(val);
274	val = of_get_property(pdev->dev.of_node, "baud-width", &len);
275	if (val && len >= sizeof(__be32))
276		hw->baudwidth = be32_to_cpup(val);
277	return 0;
278}
279#else /* !CONFIG_OF */
280static int __devinit tiny_spi_of_probe(struct platform_device *pdev)
281{
282	return 0;
283}
284#endif /* CONFIG_OF */
285
286static int __devinit tiny_spi_probe(struct platform_device *pdev)
287{
288	struct tiny_spi_platform_data *platp = pdev->dev.platform_data;
289	struct tiny_spi *hw;
290	struct spi_master *master;
291	struct resource *res;
292	unsigned int i;
293	int err = -ENODEV;
294
295	master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
296	if (!master)
297		return err;
298
299	/* setup the master state. */
300	master->bus_num = pdev->id;
301	master->num_chipselect = 255;
302	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
303	master->setup = tiny_spi_setup;
 
304
305	hw = spi_master_get_devdata(master);
306	platform_set_drvdata(pdev, hw);
307
308	/* setup the state for the bitbang driver */
309	hw->bitbang.master = spi_master_get(master);
310	if (!hw->bitbang.master)
311		return err;
312	hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
313	hw->bitbang.chipselect = tiny_spi_chipselect;
314	hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
315
316	/* find and map our resources */
317	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
318	if (!res)
319		goto exit_busy;
320	if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
321				     pdev->name))
322		goto exit_busy;
323	hw->base = devm_ioremap_nocache(&pdev->dev, res->start,
324					resource_size(res));
325	if (!hw->base)
326		goto exit_busy;
327	/* irq is optional */
328	hw->irq = platform_get_irq(pdev, 0);
329	if (hw->irq >= 0) {
330		init_completion(&hw->done);
331		err = devm_request_irq(&pdev->dev, hw->irq, tiny_spi_irq, 0,
332				       pdev->name, hw);
333		if (err)
334			goto exit;
335	}
336	/* find platform data */
337	if (platp) {
338		hw->gpio_cs_count = platp->gpio_cs_count;
339		hw->gpio_cs = platp->gpio_cs;
340		if (platp->gpio_cs_count && !platp->gpio_cs)
341			goto exit_busy;
342		hw->freq = platp->freq;
343		hw->baudwidth = platp->baudwidth;
344	} else {
345		err = tiny_spi_of_probe(pdev);
346		if (err)
347			goto exit;
348	}
349	for (i = 0; i < hw->gpio_cs_count; i++) {
350		err = gpio_request(hw->gpio_cs[i], dev_name(&pdev->dev));
351		if (err)
352			goto exit_gpio;
353		gpio_direction_output(hw->gpio_cs[i], 1);
354	}
355	hw->bitbang.master->num_chipselect = max(1U, hw->gpio_cs_count);
356
357	/* register our spi controller */
358	err = spi_bitbang_start(&hw->bitbang);
359	if (err)
360		goto exit;
361	dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
362
363	return 0;
364
365exit_gpio:
366	while (i-- > 0)
367		gpio_free(hw->gpio_cs[i]);
368exit_busy:
369	err = -EBUSY;
370exit:
371	platform_set_drvdata(pdev, NULL);
372	spi_master_put(master);
373	return err;
374}
375
376static int __devexit tiny_spi_remove(struct platform_device *pdev)
377{
378	struct tiny_spi *hw = platform_get_drvdata(pdev);
379	struct spi_master *master = hw->bitbang.master;
380	unsigned int i;
381
382	spi_bitbang_stop(&hw->bitbang);
383	for (i = 0; i < hw->gpio_cs_count; i++)
384		gpio_free(hw->gpio_cs[i]);
385	platform_set_drvdata(pdev, NULL);
386	spi_master_put(master);
387	return 0;
388}
389
390#ifdef CONFIG_OF
391static const struct of_device_id tiny_spi_match[] = {
392	{ .compatible = "opencores,tiny-spi-rtlsvn2", },
393	{},
394};
395MODULE_DEVICE_TABLE(of, tiny_spi_match);
396#else /* CONFIG_OF */
397#define tiny_spi_match NULL
398#endif /* CONFIG_OF */
399
400static struct platform_driver tiny_spi_driver = {
401	.probe = tiny_spi_probe,
402	.remove = __devexit_p(tiny_spi_remove),
403	.driver = {
404		.name = DRV_NAME,
405		.owner = THIS_MODULE,
406		.pm = NULL,
407		.of_match_table = tiny_spi_match,
408	},
409};
410module_platform_driver(tiny_spi_driver);
411
412MODULE_DESCRIPTION("OpenCores tiny SPI driver");
413MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
414MODULE_LICENSE("GPL");
415MODULE_ALIAS("platform:" DRV_NAME);