.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * SPI master driver using generic bitbanged GPIO |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2006,2008 David Brownell |
---|
5 | 6 | * Copyright (C) 2017 Linus Walleij |
---|
6 | | - * |
---|
7 | | - * This program is free software; you can redistribute it and/or modify |
---|
8 | | - * it under the terms of the GNU General Public License as published by |
---|
9 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
10 | | - * (at your option) any later version. |
---|
11 | | - * |
---|
12 | | - * This program is distributed in the hope that it will be useful, |
---|
13 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | | - * GNU General Public License for more details. |
---|
16 | 7 | */ |
---|
17 | 8 | #include <linux/kernel.h> |
---|
18 | 9 | #include <linux/module.h> |
---|
.. | .. |
---|
35 | 26 | * platform_device->driver_data ... points to spi_gpio |
---|
36 | 27 | * |
---|
37 | 28 | * spi->controller_state ... reserved for bitbang framework code |
---|
38 | | - * spi->controller_data ... holds chipselect GPIO |
---|
39 | 29 | * |
---|
40 | 30 | * spi->master->dev.driver_data ... points to spi_gpio->bitbang |
---|
41 | 31 | */ |
---|
42 | 32 | |
---|
43 | 33 | struct spi_gpio { |
---|
44 | 34 | struct spi_bitbang bitbang; |
---|
45 | | - struct spi_gpio_platform_data pdata; |
---|
46 | | - struct platform_device *pdev; |
---|
47 | 35 | struct gpio_desc *sck; |
---|
48 | 36 | struct gpio_desc *miso; |
---|
49 | 37 | struct gpio_desc *mosi; |
---|
50 | 38 | struct gpio_desc **cs_gpios; |
---|
51 | | - bool has_cs; |
---|
52 | 39 | }; |
---|
53 | 40 | |
---|
54 | 41 | /*----------------------------------------------------------------------*/ |
---|
.. | .. |
---|
94 | 81 | bang = spi_master_get_devdata(spi->master); |
---|
95 | 82 | spi_gpio = container_of(bang, struct spi_gpio, bitbang); |
---|
96 | 83 | return spi_gpio; |
---|
97 | | -} |
---|
98 | | - |
---|
99 | | -static inline struct spi_gpio_platform_data *__pure |
---|
100 | | -spi_to_pdata(const struct spi_device *spi) |
---|
101 | | -{ |
---|
102 | | - return &spi_to_spi_gpio(spi)->pdata; |
---|
103 | 84 | } |
---|
104 | 85 | |
---|
105 | 86 | /* These helpers are in turn called by the bitbang inlines */ |
---|
.. | .. |
---|
224 | 205 | gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL); |
---|
225 | 206 | |
---|
226 | 207 | /* Drive chip select line, if we have one */ |
---|
227 | | - if (spi_gpio->has_cs) { |
---|
| 208 | + if (spi_gpio->cs_gpios) { |
---|
228 | 209 | struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select]; |
---|
229 | 210 | |
---|
230 | 211 | /* SPI chip selects are normally active-low */ |
---|
.. | .. |
---|
242 | 223 | * The CS GPIOs have already been |
---|
243 | 224 | * initialized from the descriptor lookup. |
---|
244 | 225 | */ |
---|
245 | | - cs = spi_gpio->cs_gpios[spi->chip_select]; |
---|
246 | | - if (!spi->controller_state && cs) |
---|
247 | | - status = gpiod_direction_output(cs, |
---|
248 | | - !(spi->mode & SPI_CS_HIGH)); |
---|
| 226 | + if (spi_gpio->cs_gpios) { |
---|
| 227 | + cs = spi_gpio->cs_gpios[spi->chip_select]; |
---|
| 228 | + if (!spi->controller_state && cs) |
---|
| 229 | + status = gpiod_direction_output(cs, |
---|
| 230 | + !(spi->mode & SPI_CS_HIGH)); |
---|
| 231 | + } |
---|
249 | 232 | |
---|
250 | 233 | if (!status) |
---|
251 | 234 | status = spi_bitbang_setup(spi); |
---|
.. | .. |
---|
256 | 239 | static int spi_gpio_set_direction(struct spi_device *spi, bool output) |
---|
257 | 240 | { |
---|
258 | 241 | struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); |
---|
| 242 | + int ret; |
---|
259 | 243 | |
---|
260 | 244 | if (output) |
---|
261 | 245 | return gpiod_direction_output(spi_gpio->mosi, 1); |
---|
262 | | - else |
---|
263 | | - return gpiod_direction_input(spi_gpio->mosi); |
---|
| 246 | + |
---|
| 247 | + /* |
---|
| 248 | + * Only change MOSI to an input if using 3WIRE mode. |
---|
| 249 | + * Otherwise, MOSI could be left floating if there is |
---|
| 250 | + * no pull resistor connected to the I/O pin, or could |
---|
| 251 | + * be left logic high if there is a pull-up. Transmitting |
---|
| 252 | + * logic high when only clocking MISO data in can put some |
---|
| 253 | + * SPI devices in to a bad state. |
---|
| 254 | + */ |
---|
| 255 | + if (spi->mode & SPI_3WIRE) { |
---|
| 256 | + ret = gpiod_direction_input(spi_gpio->mosi); |
---|
| 257 | + if (ret) |
---|
| 258 | + return ret; |
---|
| 259 | + } |
---|
| 260 | + /* |
---|
| 261 | + * Send a turnaround high impedance cycle when switching |
---|
| 262 | + * from output to input. Theoretically there should be |
---|
| 263 | + * a clock delay here, but as has been noted above, the |
---|
| 264 | + * nsec delay function for bit-banged GPIO is simply |
---|
| 265 | + * {} because bit-banging just doesn't get fast enough |
---|
| 266 | + * anyway. |
---|
| 267 | + */ |
---|
| 268 | + if (spi->mode & SPI_3WIRE_HIZ) { |
---|
| 269 | + gpiod_set_value_cansleep(spi_gpio->sck, |
---|
| 270 | + !(spi->mode & SPI_CPOL)); |
---|
| 271 | + gpiod_set_value_cansleep(spi_gpio->sck, |
---|
| 272 | + !!(spi->mode & SPI_CPOL)); |
---|
| 273 | + } |
---|
| 274 | + return 0; |
---|
264 | 275 | } |
---|
265 | 276 | |
---|
266 | 277 | static void spi_gpio_cleanup(struct spi_device *spi) |
---|
.. | .. |
---|
278 | 289 | * floating signals. (A weak pulldown would save power too, but many |
---|
279 | 290 | * drivers expect to see all-ones data as the no slave "response".) |
---|
280 | 291 | */ |
---|
281 | | -static int spi_gpio_request(struct device *dev, |
---|
282 | | - struct spi_gpio *spi_gpio, |
---|
283 | | - unsigned int num_chipselects, |
---|
284 | | - u16 *mflags) |
---|
| 292 | +static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio) |
---|
285 | 293 | { |
---|
286 | | - int i; |
---|
287 | | - |
---|
288 | 294 | spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW); |
---|
289 | 295 | if (IS_ERR(spi_gpio->mosi)) |
---|
290 | 296 | return PTR_ERR(spi_gpio->mosi); |
---|
291 | | - if (!spi_gpio->mosi) |
---|
292 | | - /* HW configuration without MOSI pin */ |
---|
293 | | - *mflags |= SPI_MASTER_NO_TX; |
---|
294 | 297 | |
---|
295 | 298 | spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN); |
---|
296 | 299 | if (IS_ERR(spi_gpio->miso)) |
---|
297 | 300 | return PTR_ERR(spi_gpio->miso); |
---|
298 | | - /* |
---|
299 | | - * No setting SPI_MASTER_NO_RX here - if there is only a MOSI |
---|
300 | | - * pin connected the host can still do RX by changing the |
---|
301 | | - * direction of the line. |
---|
302 | | - */ |
---|
303 | 301 | |
---|
304 | 302 | spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); |
---|
305 | | - if (IS_ERR(spi_gpio->sck)) |
---|
306 | | - return PTR_ERR(spi_gpio->sck); |
---|
307 | | - |
---|
308 | | - for (i = 0; i < num_chipselects; i++) { |
---|
309 | | - spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", |
---|
310 | | - i, GPIOD_OUT_HIGH); |
---|
311 | | - if (IS_ERR(spi_gpio->cs_gpios[i])) |
---|
312 | | - return PTR_ERR(spi_gpio->cs_gpios[i]); |
---|
313 | | - } |
---|
314 | | - |
---|
315 | | - return 0; |
---|
| 303 | + return PTR_ERR_OR_ZERO(spi_gpio->sck); |
---|
316 | 304 | } |
---|
317 | 305 | |
---|
318 | 306 | #ifdef CONFIG_OF |
---|
.. | .. |
---|
322 | 310 | }; |
---|
323 | 311 | MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids); |
---|
324 | 312 | |
---|
325 | | -static int spi_gpio_probe_dt(struct platform_device *pdev) |
---|
| 313 | +static int spi_gpio_probe_dt(struct platform_device *pdev, |
---|
| 314 | + struct spi_master *master) |
---|
326 | 315 | { |
---|
327 | | - int ret; |
---|
328 | | - u32 tmp; |
---|
329 | | - struct spi_gpio_platform_data *pdata; |
---|
330 | | - struct device_node *np = pdev->dev.of_node; |
---|
331 | | - const struct of_device_id *of_id = |
---|
332 | | - of_match_device(spi_gpio_dt_ids, &pdev->dev); |
---|
| 316 | + master->dev.of_node = pdev->dev.of_node; |
---|
| 317 | + master->use_gpio_descriptors = true; |
---|
333 | 318 | |
---|
334 | | - if (!of_id) |
---|
335 | | - return 0; |
---|
336 | | - |
---|
337 | | - pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
---|
338 | | - if (!pdata) |
---|
339 | | - return -ENOMEM; |
---|
340 | | - |
---|
341 | | - |
---|
342 | | - ret = of_property_read_u32(np, "num-chipselects", &tmp); |
---|
343 | | - if (ret < 0) { |
---|
344 | | - dev_err(&pdev->dev, "num-chipselects property not found\n"); |
---|
345 | | - goto error_free; |
---|
346 | | - } |
---|
347 | | - |
---|
348 | | - pdata->num_chipselect = tmp; |
---|
349 | | - pdev->dev.platform_data = pdata; |
---|
350 | | - |
---|
351 | | - return 1; |
---|
352 | | - |
---|
353 | | -error_free: |
---|
354 | | - devm_kfree(&pdev->dev, pdata); |
---|
355 | | - return ret; |
---|
| 319 | + return 0; |
---|
356 | 320 | } |
---|
357 | 321 | #else |
---|
358 | | -static inline int spi_gpio_probe_dt(struct platform_device *pdev) |
---|
| 322 | +static inline int spi_gpio_probe_dt(struct platform_device *pdev, |
---|
| 323 | + struct spi_master *master) |
---|
359 | 324 | { |
---|
360 | 325 | return 0; |
---|
361 | 326 | } |
---|
362 | 327 | #endif |
---|
| 328 | + |
---|
| 329 | +static int spi_gpio_probe_pdata(struct platform_device *pdev, |
---|
| 330 | + struct spi_master *master) |
---|
| 331 | +{ |
---|
| 332 | + struct device *dev = &pdev->dev; |
---|
| 333 | + struct spi_gpio_platform_data *pdata = dev_get_platdata(dev); |
---|
| 334 | + struct spi_gpio *spi_gpio = spi_master_get_devdata(master); |
---|
| 335 | + int i; |
---|
| 336 | + |
---|
| 337 | +#ifdef GENERIC_BITBANG |
---|
| 338 | + if (!pdata || !pdata->num_chipselect) |
---|
| 339 | + return -ENODEV; |
---|
| 340 | +#endif |
---|
| 341 | + /* |
---|
| 342 | + * The master needs to think there is a chipselect even if not |
---|
| 343 | + * connected |
---|
| 344 | + */ |
---|
| 345 | + master->num_chipselect = pdata->num_chipselect ?: 1; |
---|
| 346 | + |
---|
| 347 | + spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect, |
---|
| 348 | + sizeof(*spi_gpio->cs_gpios), |
---|
| 349 | + GFP_KERNEL); |
---|
| 350 | + if (!spi_gpio->cs_gpios) |
---|
| 351 | + return -ENOMEM; |
---|
| 352 | + |
---|
| 353 | + for (i = 0; i < master->num_chipselect; i++) { |
---|
| 354 | + spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i, |
---|
| 355 | + GPIOD_OUT_HIGH); |
---|
| 356 | + if (IS_ERR(spi_gpio->cs_gpios[i])) |
---|
| 357 | + return PTR_ERR(spi_gpio->cs_gpios[i]); |
---|
| 358 | + } |
---|
| 359 | + |
---|
| 360 | + return 0; |
---|
| 361 | +} |
---|
363 | 362 | |
---|
364 | 363 | static int spi_gpio_probe(struct platform_device *pdev) |
---|
365 | 364 | { |
---|
366 | 365 | int status; |
---|
367 | 366 | struct spi_master *master; |
---|
368 | 367 | struct spi_gpio *spi_gpio; |
---|
369 | | - struct spi_gpio_platform_data *pdata; |
---|
370 | | - u16 master_flags = 0; |
---|
371 | | - bool use_of = 0; |
---|
| 368 | + struct device *dev = &pdev->dev; |
---|
| 369 | + struct spi_bitbang *bb; |
---|
372 | 370 | |
---|
373 | | - status = spi_gpio_probe_dt(pdev); |
---|
374 | | - if (status < 0) |
---|
375 | | - return status; |
---|
376 | | - if (status > 0) |
---|
377 | | - use_of = 1; |
---|
378 | | - |
---|
379 | | - pdata = dev_get_platdata(&pdev->dev); |
---|
380 | | -#ifdef GENERIC_BITBANG |
---|
381 | | - if (!pdata || (!use_of && !pdata->num_chipselect)) |
---|
382 | | - return -ENODEV; |
---|
383 | | -#endif |
---|
384 | | - |
---|
385 | | - master = devm_spi_alloc_master(&pdev->dev, sizeof(*spi_gpio)); |
---|
| 371 | + master = devm_spi_alloc_master(dev, sizeof(*spi_gpio)); |
---|
386 | 372 | if (!master) |
---|
387 | 373 | return -ENOMEM; |
---|
388 | 374 | |
---|
| 375 | + if (pdev->dev.of_node) |
---|
| 376 | + status = spi_gpio_probe_dt(pdev, master); |
---|
| 377 | + else |
---|
| 378 | + status = spi_gpio_probe_pdata(pdev, master); |
---|
| 379 | + |
---|
| 380 | + if (status) |
---|
| 381 | + return status; |
---|
| 382 | + |
---|
389 | 383 | spi_gpio = spi_master_get_devdata(master); |
---|
390 | 384 | |
---|
391 | | - spi_gpio->cs_gpios = devm_kcalloc(&pdev->dev, |
---|
392 | | - pdata->num_chipselect, |
---|
393 | | - sizeof(*spi_gpio->cs_gpios), |
---|
394 | | - GFP_KERNEL); |
---|
395 | | - if (!spi_gpio->cs_gpios) |
---|
396 | | - return -ENOMEM; |
---|
397 | | - |
---|
398 | | - platform_set_drvdata(pdev, spi_gpio); |
---|
399 | | - |
---|
400 | | - /* Determine if we have chip selects connected */ |
---|
401 | | - spi_gpio->has_cs = !!pdata->num_chipselect; |
---|
402 | | - |
---|
403 | | - spi_gpio->pdev = pdev; |
---|
404 | | - if (pdata) |
---|
405 | | - spi_gpio->pdata = *pdata; |
---|
406 | | - |
---|
407 | | - status = spi_gpio_request(&pdev->dev, spi_gpio, |
---|
408 | | - pdata->num_chipselect, &master_flags); |
---|
| 385 | + status = spi_gpio_request(dev, spi_gpio); |
---|
409 | 386 | if (status) |
---|
410 | 387 | return status; |
---|
411 | 388 | |
---|
412 | 389 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); |
---|
413 | | - master->mode_bits = SPI_3WIRE | SPI_CPHA | SPI_CPOL | SPI_CS_HIGH; |
---|
414 | | - master->flags = master_flags; |
---|
| 390 | + master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL | |
---|
| 391 | + SPI_CS_HIGH; |
---|
| 392 | + if (!spi_gpio->mosi) { |
---|
| 393 | + /* HW configuration without MOSI pin |
---|
| 394 | + * |
---|
| 395 | + * No setting SPI_MASTER_NO_RX here - if there is only |
---|
| 396 | + * a MOSI pin connected the host can still do RX by |
---|
| 397 | + * changing the direction of the line. |
---|
| 398 | + */ |
---|
| 399 | + master->flags = SPI_MASTER_NO_TX; |
---|
| 400 | + } |
---|
| 401 | + |
---|
415 | 402 | master->bus_num = pdev->id; |
---|
416 | | - /* The master needs to think there is a chipselect even if not connected */ |
---|
417 | | - master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1; |
---|
418 | 403 | master->setup = spi_gpio_setup; |
---|
419 | 404 | master->cleanup = spi_gpio_cleanup; |
---|
420 | | -#ifdef CONFIG_OF |
---|
421 | | - master->dev.of_node = pdev->dev.of_node; |
---|
422 | | -#endif |
---|
423 | 405 | |
---|
424 | | - spi_gpio->bitbang.master = master; |
---|
425 | | - spi_gpio->bitbang.chipselect = spi_gpio_chipselect; |
---|
426 | | - spi_gpio->bitbang.set_line_direction = spi_gpio_set_direction; |
---|
| 406 | + bb = &spi_gpio->bitbang; |
---|
| 407 | + bb->master = master; |
---|
| 408 | + /* |
---|
| 409 | + * There is some additional business, apart from driving the CS GPIO |
---|
| 410 | + * line, that we need to do on selection. This makes the local |
---|
| 411 | + * callback for chipselect always get called. |
---|
| 412 | + */ |
---|
| 413 | + master->flags |= SPI_MASTER_GPIO_SS; |
---|
| 414 | + bb->chipselect = spi_gpio_chipselect; |
---|
| 415 | + bb->set_line_direction = spi_gpio_set_direction; |
---|
427 | 416 | |
---|
428 | | - if ((master_flags & SPI_MASTER_NO_TX) == 0) { |
---|
429 | | - spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; |
---|
430 | | - spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; |
---|
431 | | - spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; |
---|
432 | | - spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; |
---|
| 417 | + if (master->flags & SPI_MASTER_NO_TX) { |
---|
| 418 | + bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; |
---|
| 419 | + bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; |
---|
| 420 | + bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; |
---|
| 421 | + bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; |
---|
433 | 422 | } else { |
---|
434 | | - spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; |
---|
435 | | - spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; |
---|
436 | | - spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; |
---|
437 | | - spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; |
---|
| 423 | + bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; |
---|
| 424 | + bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; |
---|
| 425 | + bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; |
---|
| 426 | + bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; |
---|
438 | 427 | } |
---|
439 | | - spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer; |
---|
| 428 | + bb->setup_transfer = spi_bitbang_setup_transfer; |
---|
440 | 429 | |
---|
441 | | - return spi_bitbang_start(&spi_gpio->bitbang); |
---|
442 | | -} |
---|
| 430 | + status = spi_bitbang_init(&spi_gpio->bitbang); |
---|
| 431 | + if (status) |
---|
| 432 | + return status; |
---|
443 | 433 | |
---|
444 | | -static int spi_gpio_remove(struct platform_device *pdev) |
---|
445 | | -{ |
---|
446 | | - struct spi_gpio *spi_gpio; |
---|
447 | | - struct spi_gpio_platform_data *pdata; |
---|
448 | | - |
---|
449 | | - spi_gpio = platform_get_drvdata(pdev); |
---|
450 | | - pdata = dev_get_platdata(&pdev->dev); |
---|
451 | | - |
---|
452 | | - /* stop() unregisters child devices too */ |
---|
453 | | - spi_bitbang_stop(&spi_gpio->bitbang); |
---|
454 | | - |
---|
455 | | - spi_master_put(spi_gpio->bitbang.master); |
---|
456 | | - |
---|
457 | | - return 0; |
---|
| 434 | + return devm_spi_register_master(&pdev->dev, master); |
---|
458 | 435 | } |
---|
459 | 436 | |
---|
460 | 437 | MODULE_ALIAS("platform:" DRIVER_NAME); |
---|
.. | .. |
---|
465 | 442 | .of_match_table = of_match_ptr(spi_gpio_dt_ids), |
---|
466 | 443 | }, |
---|
467 | 444 | .probe = spi_gpio_probe, |
---|
468 | | - .remove = spi_gpio_remove, |
---|
469 | 445 | }; |
---|
470 | 446 | module_platform_driver(spi_gpio_driver); |
---|
471 | 447 | |
---|