Blame view

drivers/spi/spi-gpio.c 14.7 KB
d29389de0   David Brownell   spi_gpio driver
1
  /*
ca632f556   Grant Likely   spi: reorganize d...
2
   * SPI master driver using generic bitbanged GPIO
d29389de0   David Brownell   spi_gpio driver
3
4
5
6
7
8
9
10
11
12
13
14
   *
   * Copyright (C) 2006,2008 David Brownell
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
d29389de0   David Brownell   spi_gpio driver
15
16
   */
  #include <linux/kernel.h>
d7614de42   Paul Gortmaker   spi: Add module.h...
17
  #include <linux/module.h>
d29389de0   David Brownell   spi_gpio driver
18
19
  #include <linux/platform_device.h>
  #include <linux/gpio.h>
ecc77773d   Sachin Kamat   spi: gpio: Includ...
20
  #include <linux/of.h>
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
21
22
  #include <linux/of_device.h>
  #include <linux/of_gpio.h>
d29389de0   David Brownell   spi_gpio driver
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  
  #include <linux/spi/spi.h>
  #include <linux/spi/spi_bitbang.h>
  #include <linux/spi/spi_gpio.h>
  
  
  /*
   * This bitbanging SPI master driver should help make systems usable
   * when a native hardware SPI engine is not available, perhaps because
   * its driver isn't yet working or because the I/O pins it requires
   * are used for other purposes.
   *
   * platform_device->driver_data ... points to spi_gpio
   *
   * spi->controller_state ... reserved for bitbang framework code
   * spi->controller_data ... holds chipselect GPIO
   *
   * spi->master->dev.driver_data ... points to spi_gpio->bitbang
   */
  
  struct spi_gpio {
  	struct spi_bitbang		bitbang;
  	struct spi_gpio_platform_data	pdata;
  	struct platform_device		*pdev;
cfb4bbd8f   Torsten Fleischer   spi: spi-gpio: Fi...
47
  	unsigned long			cs_gpios[0];
d29389de0   David Brownell   spi_gpio driver
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  };
  
  /*----------------------------------------------------------------------*/
  
  /*
   * Because the overhead of going through four GPIO procedure calls
   * per transferred bit can make performance a problem, this code
   * is set up so that you can use it in either of two ways:
   *
   *   - The slow generic way:  set up platform_data to hold the GPIO
   *     numbers used for MISO/MOSI/SCK, and issue procedure calls for
   *     each of them.  This driver can handle several such busses.
   *
   *   - The quicker inlined way:  only helps with platform GPIO code
   *     that inlines operations for constant GPIOs.  This can give
   *     you tight (fast!) inner loops, but each such bus needs a
   *     new driver.  You'll define a new C file, with Makefile and
   *     Kconfig support; the C code can be a total of six lines:
   *
   *		#define DRIVER_NAME	"myboard_spi2"
   *		#define	SPI_MISO_GPIO	119
   *		#define	SPI_MOSI_GPIO	120
   *		#define	SPI_SCK_GPIO	121
   *		#define	SPI_N_CHIPSEL	4
ca632f556   Grant Likely   spi: reorganize d...
72
   *		#include "spi-gpio.c"
d29389de0   David Brownell   spi_gpio driver
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
   */
  
  #ifndef DRIVER_NAME
  #define DRIVER_NAME	"spi_gpio"
  
  #define GENERIC_BITBANG	/* vs tight inlines */
  
  /* all functions referencing these symbols must define pdata */
  #define SPI_MISO_GPIO	((pdata)->miso)
  #define SPI_MOSI_GPIO	((pdata)->mosi)
  #define SPI_SCK_GPIO	((pdata)->sck)
  
  #define SPI_N_CHIPSEL	((pdata)->num_chipselect)
  
  #endif
  
  /*----------------------------------------------------------------------*/
650705cf7   Nizam Haider   spi/gpio: fixed s...
90
  static inline struct spi_gpio *__pure
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
91
  spi_to_spi_gpio(const struct spi_device *spi)
d29389de0   David Brownell   spi_gpio driver
92
93
  {
  	const struct spi_bitbang	*bang;
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
94
  	struct spi_gpio			*spi_gpio;
d29389de0   David Brownell   spi_gpio driver
95
96
97
  
  	bang = spi_master_get_devdata(spi->master);
  	spi_gpio = container_of(bang, struct spi_gpio, bitbang);
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
98
99
  	return spi_gpio;
  }
650705cf7   Nizam Haider   spi/gpio: fixed s...
100
  static inline struct spi_gpio_platform_data *__pure
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
101
102
103
  spi_to_pdata(const struct spi_device *spi)
  {
  	return &spi_to_spi_gpio(spi)->pdata;
d29389de0   David Brownell   spi_gpio driver
104
105
106
107
108
109
110
  }
  
  /* this is #defined to avoid unused-variable warnings when inlining */
  #define pdata		spi_to_pdata(spi)
  
  static inline void setsck(const struct spi_device *spi, int is_on)
  {
d9dda5a19   Ezequiel Garcia   spi: spi-gpio: Us...
111
  	gpio_set_value_cansleep(SPI_SCK_GPIO, is_on);
d29389de0   David Brownell   spi_gpio driver
112
113
114
115
  }
  
  static inline void setmosi(const struct spi_device *spi, int is_on)
  {
d9dda5a19   Ezequiel Garcia   spi: spi-gpio: Us...
116
  	gpio_set_value_cansleep(SPI_MOSI_GPIO, is_on);
d29389de0   David Brownell   spi_gpio driver
117
118
119
120
  }
  
  static inline int getmiso(const struct spi_device *spi)
  {
d9dda5a19   Ezequiel Garcia   spi: spi-gpio: Us...
121
  	return !!gpio_get_value_cansleep(SPI_MISO_GPIO);
d29389de0   David Brownell   spi_gpio driver
122
123
124
125
126
127
128
129
130
131
132
  }
  
  #undef pdata
  
  /*
   * NOTE:  this clocks "as fast as we can".  It "should" be a function of the
   * requested device clock.  Software overhead means we usually have trouble
   * reaching even one Mbit/sec (except when we can inline bitops), so for now
   * we'll just assume we never need additional per-bit slowdowns.
   */
  #define spidelay(nsecs)	do {} while (0)
ca632f556   Grant Likely   spi: reorganize d...
133
  #include "spi-bitbang-txrx.h"
d29389de0   David Brownell   spi_gpio driver
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  
  /*
   * These functions can leverage inline expansion of GPIO calls to shrink
   * costs for a txrx bit, often by factors of around ten (by instruction
   * count).  That is particularly visible for larger word sizes, but helps
   * even with default 8-bit words.
   *
   * REVISIT overheads calling these functions for each word also have
   * significant performance costs.  Having txrx_bufs() calls that inline
   * the txrx_word() logic would help performance, e.g. on larger blocks
   * used with flash storage or MMC/SD.  There should also be ways to make
   * GCC be less stupid about reloading registers inside the I/O loops,
   * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
   */
  
  static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
  		unsigned nsecs, u32 word, u8 bits)
  {
04bb2a031   Marek Szyprowski   spi/bitbang: add ...
152
  	return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
d29389de0   David Brownell   spi_gpio driver
153
154
155
156
157
  }
  
  static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
  		unsigned nsecs, u32 word, u8 bits)
  {
04bb2a031   Marek Szyprowski   spi/bitbang: add ...
158
  	return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits);
d29389de0   David Brownell   spi_gpio driver
159
160
161
162
163
  }
  
  static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
  		unsigned nsecs, u32 word, u8 bits)
  {
04bb2a031   Marek Szyprowski   spi/bitbang: add ...
164
  	return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits);
d29389de0   David Brownell   spi_gpio driver
165
166
167
168
169
  }
  
  static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
  		unsigned nsecs, u32 word, u8 bits)
  {
04bb2a031   Marek Szyprowski   spi/bitbang: add ...
170
  	return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits);
d29389de0   David Brownell   spi_gpio driver
171
  }
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  /*
   * These functions do not call setmosi or getmiso if respective flag
   * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
   * call when such pin is not present or defined in the controller.
   * A separate set of callbacks is defined to get highest possible
   * speed in the generic case (when both MISO and MOSI lines are
   * available), as optimiser will remove the checks when argument is
   * constant.
   */
  
  static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
  		unsigned nsecs, u32 word, u8 bits)
  {
  	unsigned flags = spi->master->flags;
  	return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
  }
  
  static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
  		unsigned nsecs, u32 word, u8 bits)
  {
  	unsigned flags = spi->master->flags;
  	return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
  }
  
  static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
  		unsigned nsecs, u32 word, u8 bits)
  {
  	unsigned flags = spi->master->flags;
  	return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
  }
  
  static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
  		unsigned nsecs, u32 word, u8 bits)
  {
  	unsigned flags = spi->master->flags;
  	return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
  }
d29389de0   David Brownell   spi_gpio driver
209
210
211
212
  /*----------------------------------------------------------------------*/
  
  static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
  {
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
213
  	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
cfb4bbd8f   Torsten Fleischer   spi: spi-gpio: Fi...
214
  	unsigned long cs = spi_gpio->cs_gpios[spi->chip_select];
d29389de0   David Brownell   spi_gpio driver
215
216
217
218
  
  	/* set initial clock polarity */
  	if (is_active)
  		setsck(spi, spi->mode & SPI_CPOL);
bfb9bcdbd   Michael Buesch   spi-gpio: allow o...
219
220
  	if (cs != SPI_GPIO_NO_CHIPSELECT) {
  		/* SPI is normally active-low */
d9dda5a19   Ezequiel Garcia   spi: spi-gpio: Us...
221
  		gpio_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
bfb9bcdbd   Michael Buesch   spi-gpio: allow o...
222
  	}
d29389de0   David Brownell   spi_gpio driver
223
224
225
226
  }
  
  static int spi_gpio_setup(struct spi_device *spi)
  {
cfb4bbd8f   Torsten Fleischer   spi: spi-gpio: Fi...
227
  	unsigned long		cs;
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
228
229
  	int			status = 0;
  	struct spi_gpio		*spi_gpio = spi_to_spi_gpio(spi);
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
230
  	struct device_node	*np = spi->master->dev.of_node;
d29389de0   David Brownell   spi_gpio driver
231

38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
232
233
234
235
236
237
238
239
240
241
  	if (np) {
  		/*
  		 * In DT environments, the CS GPIOs have already been
  		 * initialized from the "cs-gpios" property of the node.
  		 */
  		cs = spi_gpio->cs_gpios[spi->chip_select];
  	} else {
  		/*
  		 * ... otherwise, take it from spi->controller_data
  		 */
cfb4bbd8f   Torsten Fleischer   spi: spi-gpio: Fi...
242
  		cs = (uintptr_t) spi->controller_data;
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
243
  	}
d29389de0   David Brownell   spi_gpio driver
244
  	if (!spi->controller_state) {
bfb9bcdbd   Michael Buesch   spi-gpio: allow o...
245
246
247
248
  		if (cs != SPI_GPIO_NO_CHIPSELECT) {
  			status = gpio_request(cs, dev_name(&spi->dev));
  			if (status)
  				return status;
056441470   Uwe Kleine-König   spi/gpio: start w...
249
250
  			status = gpio_direction_output(cs,
  					!(spi->mode & SPI_CS_HIGH));
bfb9bcdbd   Michael Buesch   spi-gpio: allow o...
251
  		}
d29389de0   David Brownell   spi_gpio driver
252
  	}
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
253
  	if (!status) {
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
254
  		/* in case it was initialized from static board data */
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
255
  		spi_gpio->cs_gpios[spi->chip_select] = cs;
6b8cc3306   Josef Ahmad   spi-gpio: init CS...
256
  		status = spi_bitbang_setup(spi);
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
257
  	}
d29389de0   David Brownell   spi_gpio driver
258
  	if (status) {
bfb9bcdbd   Michael Buesch   spi-gpio: allow o...
259
  		if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
d29389de0   David Brownell   spi_gpio driver
260
261
262
263
264
265
266
  			gpio_free(cs);
  	}
  	return status;
  }
  
  static void spi_gpio_cleanup(struct spi_device *spi)
  {
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
267
  	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
cfb4bbd8f   Torsten Fleischer   spi: spi-gpio: Fi...
268
  	unsigned long cs = spi_gpio->cs_gpios[spi->chip_select];
d29389de0   David Brownell   spi_gpio driver
269

bfb9bcdbd   Michael Buesch   spi-gpio: allow o...
270
271
  	if (cs != SPI_GPIO_NO_CHIPSELECT)
  		gpio_free(cs);
d29389de0   David Brownell   spi_gpio driver
272
273
  	spi_bitbang_cleanup(spi);
  }
fd4a319bc   Grant Likely   spi: Remove HOTPL...
274
  static int spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
d29389de0   David Brownell   spi_gpio driver
275
276
277
278
279
280
281
282
283
284
285
286
  {
  	int value;
  
  	value = gpio_request(pin, label);
  	if (value == 0) {
  		if (is_in)
  			value = gpio_direction_input(pin);
  		else
  			value = gpio_direction_output(pin, 0);
  	}
  	return value;
  }
fd4a319bc   Grant Likely   spi: Remove HOTPL...
287
288
  static int spi_gpio_request(struct spi_gpio_platform_data *pdata,
  			    const char *label, u16 *res_flags)
d29389de0   David Brownell   spi_gpio driver
289
290
291
292
  {
  	int value;
  
  	/* NOTE:  SPI_*_GPIO symbols may reference "pdata" */
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
293
294
295
296
297
298
299
300
  	if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) {
  		value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
  		if (value)
  			goto done;
  	} else {
  		/* HW configuration without MOSI pin */
  		*res_flags |= SPI_MASTER_NO_TX;
  	}
d29389de0   David Brownell   spi_gpio driver
301

3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
302
303
304
305
306
307
308
309
  	if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) {
  		value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
  		if (value)
  			goto free_mosi;
  	} else {
  		/* HW configuration without MISO pin */
  		*res_flags |= SPI_MASTER_NO_RX;
  	}
d29389de0   David Brownell   spi_gpio driver
310
311
312
313
314
315
316
317
  
  	value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
  	if (value)
  		goto free_miso;
  
  	goto done;
  
  free_miso:
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
318
319
  	if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  		gpio_free(SPI_MISO_GPIO);
d29389de0   David Brownell   spi_gpio driver
320
  free_mosi:
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
321
322
  	if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  		gpio_free(SPI_MOSI_GPIO);
d29389de0   David Brownell   spi_gpio driver
323
324
325
  done:
  	return value;
  }
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
326
  #ifdef CONFIG_OF
d9e152818   Jingoo Han   spi: spi-gpio: Ma...
327
  static const struct of_device_id spi_gpio_dt_ids[] = {
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
  	{ .compatible = "spi-gpio" },
  	{}
  };
  MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
  
  static int spi_gpio_probe_dt(struct platform_device *pdev)
  {
  	int ret;
  	u32 tmp;
  	struct spi_gpio_platform_data	*pdata;
  	struct device_node *np = pdev->dev.of_node;
  	const struct of_device_id *of_id =
  			of_match_device(spi_gpio_dt_ids, &pdev->dev);
  
  	if (!of_id)
  		return 0;
  
  	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  	if (!pdata)
  		return -ENOMEM;
0202a32d5   Maxime Ripard   spi: spi-gpio: Ad...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  	ret = of_get_named_gpio(np, "gpio-sck", 0);
  	if (ret < 0) {
  		dev_err(&pdev->dev, "gpio-sck property not found
  ");
  		goto error_free;
  	}
  	pdata->sck = ret;
  
  	ret = of_get_named_gpio(np, "gpio-miso", 0);
  	if (ret < 0) {
  		dev_info(&pdev->dev, "gpio-miso property not found, switching to no-rx mode
  ");
  		pdata->miso = SPI_GPIO_NO_MISO;
  	} else
  		pdata->miso = ret;
  
  	ret = of_get_named_gpio(np, "gpio-mosi", 0);
  	if (ret < 0) {
  		dev_info(&pdev->dev, "gpio-mosi property not found, switching to no-tx mode
  ");
  		pdata->mosi = SPI_GPIO_NO_MOSI;
  	} else
  		pdata->mosi = ret;
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
  
  	ret = of_property_read_u32(np, "num-chipselects", &tmp);
  	if (ret < 0) {
  		dev_err(&pdev->dev, "num-chipselects property not found
  ");
  		goto error_free;
  	}
  
  	pdata->num_chipselect = tmp;
  	pdev->dev.platform_data = pdata;
  
  	return 1;
  
  error_free:
  	devm_kfree(&pdev->dev, pdata);
  	return ret;
  }
  #else
ac2cb30b4   Mark Brown   spi/gpio: Fix stu...
389
  static inline int spi_gpio_probe_dt(struct platform_device *pdev)
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
390
391
392
393
  {
  	return 0;
  }
  #endif
fd4a319bc   Grant Likely   spi: Remove HOTPL...
394
  static int spi_gpio_probe(struct platform_device *pdev)
d29389de0   David Brownell   spi_gpio driver
395
396
397
398
399
  {
  	int				status;
  	struct spi_master		*master;
  	struct spi_gpio			*spi_gpio;
  	struct spi_gpio_platform_data	*pdata;
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
400
  	u16 master_flags = 0;
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
401
  	bool use_of = 0;
d1d818025   Torsten Fleischer   spi: spi-gpio: Ad...
402
  	int num_devices;
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
403
404
405
406
407
408
  
  	status = spi_gpio_probe_dt(pdev);
  	if (status < 0)
  		return status;
  	if (status > 0)
  		use_of = 1;
d29389de0   David Brownell   spi_gpio driver
409

8074cf063   Jingoo Han   spi: use dev_get_...
410
  	pdata = dev_get_platdata(&pdev->dev);
d29389de0   David Brownell   spi_gpio driver
411
  #ifdef GENERIC_BITBANG
d1d818025   Torsten Fleischer   spi: spi-gpio: Ad...
412
  	if (!pdata || (!use_of && !pdata->num_chipselect))
d29389de0   David Brownell   spi_gpio driver
413
414
  		return -ENODEV;
  #endif
d1d818025   Torsten Fleischer   spi: spi-gpio: Ad...
415
416
417
418
  	if (use_of && !SPI_N_CHIPSEL)
  		num_devices = 1;
  	else
  		num_devices = SPI_N_CHIPSEL;
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
419
  	status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags);
d29389de0   David Brownell   spi_gpio driver
420
421
  	if (status < 0)
  		return status;
161c2dd3c   Daniel Mack   spi: spi-gpio: st...
422
  	master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) +
cfb4bbd8f   Torsten Fleischer   spi: spi-gpio: Fi...
423
  					(sizeof(unsigned long) * num_devices));
d29389de0   David Brownell   spi_gpio driver
424
425
426
427
428
429
430
431
432
433
  	if (!master) {
  		status = -ENOMEM;
  		goto gpio_free;
  	}
  	spi_gpio = spi_master_get_devdata(master);
  	platform_set_drvdata(pdev, spi_gpio);
  
  	spi_gpio->pdev = pdev;
  	if (pdata)
  		spi_gpio->pdata = *pdata;
24778be20   Stephen Warren   spi: convert driv...
434
  	master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
435
  	master->flags = master_flags;
d29389de0   David Brownell   spi_gpio driver
436
  	master->bus_num = pdev->id;
d1d818025   Torsten Fleischer   spi: spi-gpio: Ad...
437
  	master->num_chipselect = num_devices;
d29389de0   David Brownell   spi_gpio driver
438
439
  	master->setup = spi_gpio_setup;
  	master->cleanup = spi_gpio_cleanup;
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
440
441
442
443
444
445
446
447
448
449
450
  #ifdef CONFIG_OF
  	master->dev.of_node = pdev->dev.of_node;
  
  	if (use_of) {
  		int i;
  		struct device_node *np = pdev->dev.of_node;
  
  		/*
  		 * In DT environments, take the CS GPIO from the "cs-gpios"
  		 * property of the node.
  		 */
d1d818025   Torsten Fleischer   spi: spi-gpio: Ad...
451
452
453
  		if (!SPI_N_CHIPSEL)
  			spi_gpio->cs_gpios[0] = SPI_GPIO_NO_CHIPSELECT;
  		else
cfb4bbd8f   Torsten Fleischer   spi: spi-gpio: Fi...
454
455
456
457
458
459
460
461
462
463
  			for (i = 0; i < SPI_N_CHIPSEL; i++) {
  				status = of_get_named_gpio(np, "cs-gpios", i);
  				if (status < 0) {
  					dev_err(&pdev->dev,
  						"invalid cs-gpios property
  ");
  					goto gpio_free;
  				}
  				spi_gpio->cs_gpios[i] = status;
  			}
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
464
465
  	}
  #endif
d29389de0   David Brownell   spi_gpio driver
466

94c69f765   Axel Lin   spi: bitbang: Let...
467
  	spi_gpio->bitbang.master = master;
d29389de0   David Brownell   spi_gpio driver
468
  	spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
469

23699f98f   Roel Kluin   spi: spi-gpio.c t...
470
  	if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
471
472
473
474
475
476
477
478
479
480
  		spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  		spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  		spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  		spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  	} else {
  		spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
  		spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
  		spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
  		spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
  	}
d29389de0   David Brownell   spi_gpio driver
481
482
483
484
485
  	spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  	spi_gpio->bitbang.flags = SPI_CS_HIGH;
  
  	status = spi_bitbang_start(&spi_gpio->bitbang);
  	if (status < 0) {
d29389de0   David Brownell   spi_gpio driver
486
  gpio_free:
3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
487
488
489
490
  		if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  			gpio_free(SPI_MISO_GPIO);
  		if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  			gpio_free(SPI_MOSI_GPIO);
d29389de0   David Brownell   spi_gpio driver
491
492
493
494
495
496
  		gpio_free(SPI_SCK_GPIO);
  		spi_master_put(master);
  	}
  
  	return status;
  }
fd4a319bc   Grant Likely   spi: Remove HOTPL...
497
  static int spi_gpio_remove(struct platform_device *pdev)
d29389de0   David Brownell   spi_gpio driver
498
499
500
  {
  	struct spi_gpio			*spi_gpio;
  	struct spi_gpio_platform_data	*pdata;
d29389de0   David Brownell   spi_gpio driver
501
502
  
  	spi_gpio = platform_get_drvdata(pdev);
8074cf063   Jingoo Han   spi: use dev_get_...
503
  	pdata = dev_get_platdata(&pdev->dev);
d29389de0   David Brownell   spi_gpio driver
504
505
  
  	/* stop() unregisters child devices too */
d9721ae14   Axel Lin   spi: bitbang: Mak...
506
  	spi_bitbang_stop(&spi_gpio->bitbang);
d29389de0   David Brownell   spi_gpio driver
507

3c8e1a84f   Marek Szyprowski   spi/spi-gpio: add...
508
509
510
511
  	if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  		gpio_free(SPI_MISO_GPIO);
  	if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  		gpio_free(SPI_MOSI_GPIO);
d29389de0   David Brownell   spi_gpio driver
512
  	gpio_free(SPI_SCK_GPIO);
94c69f765   Axel Lin   spi: bitbang: Let...
513
  	spi_master_put(spi_gpio->bitbang.master);
d29389de0   David Brownell   spi_gpio driver
514

d9721ae14   Axel Lin   spi: bitbang: Mak...
515
  	return 0;
d29389de0   David Brownell   spi_gpio driver
516
517
518
519
520
  }
  
  MODULE_ALIAS("platform:" DRIVER_NAME);
  
  static struct platform_driver spi_gpio_driver = {
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
521
522
  	.driver = {
  		.name	= DRIVER_NAME,
38ab18caa   Daniel Mack   spi: spi-gpio: Ad...
523
524
  		.of_match_table = of_match_ptr(spi_gpio_dt_ids),
  	},
940ab8896   Grant Likely   drivercore: Add h...
525
  	.probe		= spi_gpio_probe,
fd4a319bc   Grant Likely   spi: Remove HOTPL...
526
  	.remove		= spi_gpio_remove,
d29389de0   David Brownell   spi_gpio driver
527
  };
940ab8896   Grant Likely   drivercore: Add h...
528
  module_platform_driver(spi_gpio_driver);
d29389de0   David Brownell   spi_gpio driver
529
530
531
532
  
  MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  MODULE_AUTHOR("David Brownell");
  MODULE_LICENSE("GPL");