.. | .. |
---|
| 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 | + ret = gpiod_direction_input(spi_gpio->mosi); |
---|
| 248 | + if (ret) |
---|
| 249 | + return ret; |
---|
| 250 | + /* |
---|
| 251 | + * Send a turnaround high impedance cycle when switching |
---|
| 252 | + * from output to input. Theoretically there should be |
---|
| 253 | + * a clock delay here, but as has been noted above, the |
---|
| 254 | + * nsec delay function for bit-banged GPIO is simply |
---|
| 255 | + * {} because bit-banging just doesn't get fast enough |
---|
| 256 | + * anyway. |
---|
| 257 | + */ |
---|
| 258 | + if (spi->mode & SPI_3WIRE_HIZ) { |
---|
| 259 | + gpiod_set_value_cansleep(spi_gpio->sck, |
---|
| 260 | + !(spi->mode & SPI_CPOL)); |
---|
| 261 | + gpiod_set_value_cansleep(spi_gpio->sck, |
---|
| 262 | + !!(spi->mode & SPI_CPOL)); |
---|
| 263 | + } |
---|
| 264 | + return 0; |
---|
264 | 265 | } |
---|
265 | 266 | |
---|
266 | 267 | static void spi_gpio_cleanup(struct spi_device *spi) |
---|
.. | .. |
---|
278 | 279 | * floating signals. (A weak pulldown would save power too, but many |
---|
279 | 280 | * drivers expect to see all-ones data as the no slave "response".) |
---|
280 | 281 | */ |
---|
281 | | -static int spi_gpio_request(struct device *dev, |
---|
282 | | - struct spi_gpio *spi_gpio, |
---|
283 | | - unsigned int num_chipselects, |
---|
284 | | - u16 *mflags) |
---|
| 282 | +static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio) |
---|
285 | 283 | { |
---|
286 | | - int i; |
---|
287 | | - |
---|
288 | 284 | spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW); |
---|
289 | 285 | if (IS_ERR(spi_gpio->mosi)) |
---|
290 | 286 | return PTR_ERR(spi_gpio->mosi); |
---|
291 | | - if (!spi_gpio->mosi) |
---|
292 | | - /* HW configuration without MOSI pin */ |
---|
293 | | - *mflags |= SPI_MASTER_NO_TX; |
---|
294 | 287 | |
---|
295 | 288 | spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN); |
---|
296 | 289 | if (IS_ERR(spi_gpio->miso)) |
---|
297 | 290 | 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 | 291 | |
---|
304 | 292 | 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; |
---|
| 293 | + return PTR_ERR_OR_ZERO(spi_gpio->sck); |
---|
316 | 294 | } |
---|
317 | 295 | |
---|
318 | 296 | #ifdef CONFIG_OF |
---|
.. | .. |
---|
322 | 300 | }; |
---|
323 | 301 | MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids); |
---|
324 | 302 | |
---|
325 | | -static int spi_gpio_probe_dt(struct platform_device *pdev) |
---|
| 303 | +static int spi_gpio_probe_dt(struct platform_device *pdev, |
---|
| 304 | + struct spi_master *master) |
---|
326 | 305 | { |
---|
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); |
---|
| 306 | + master->dev.of_node = pdev->dev.of_node; |
---|
| 307 | + master->use_gpio_descriptors = true; |
---|
333 | 308 | |
---|
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; |
---|
| 309 | + return 0; |
---|
356 | 310 | } |
---|
357 | 311 | #else |
---|
358 | | -static inline int spi_gpio_probe_dt(struct platform_device *pdev) |
---|
| 312 | +static inline int spi_gpio_probe_dt(struct platform_device *pdev, |
---|
| 313 | + struct spi_master *master) |
---|
359 | 314 | { |
---|
360 | 315 | return 0; |
---|
361 | 316 | } |
---|
362 | 317 | #endif |
---|
| 318 | + |
---|
| 319 | +static int spi_gpio_probe_pdata(struct platform_device *pdev, |
---|
| 320 | + struct spi_master *master) |
---|
| 321 | +{ |
---|
| 322 | + struct device *dev = &pdev->dev; |
---|
| 323 | + struct spi_gpio_platform_data *pdata = dev_get_platdata(dev); |
---|
| 324 | + struct spi_gpio *spi_gpio = spi_master_get_devdata(master); |
---|
| 325 | + int i; |
---|
| 326 | + |
---|
| 327 | +#ifdef GENERIC_BITBANG |
---|
| 328 | + if (!pdata || !pdata->num_chipselect) |
---|
| 329 | + return -ENODEV; |
---|
| 330 | +#endif |
---|
| 331 | + /* |
---|
| 332 | + * The master needs to think there is a chipselect even if not |
---|
| 333 | + * connected |
---|
| 334 | + */ |
---|
| 335 | + master->num_chipselect = pdata->num_chipselect ?: 1; |
---|
| 336 | + |
---|
| 337 | + spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect, |
---|
| 338 | + sizeof(*spi_gpio->cs_gpios), |
---|
| 339 | + GFP_KERNEL); |
---|
| 340 | + if (!spi_gpio->cs_gpios) |
---|
| 341 | + return -ENOMEM; |
---|
| 342 | + |
---|
| 343 | + for (i = 0; i < master->num_chipselect; i++) { |
---|
| 344 | + spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i, |
---|
| 345 | + GPIOD_OUT_HIGH); |
---|
| 346 | + if (IS_ERR(spi_gpio->cs_gpios[i])) |
---|
| 347 | + return PTR_ERR(spi_gpio->cs_gpios[i]); |
---|
| 348 | + } |
---|
| 349 | + |
---|
| 350 | + return 0; |
---|
| 351 | +} |
---|
363 | 352 | |
---|
364 | 353 | static int spi_gpio_probe(struct platform_device *pdev) |
---|
365 | 354 | { |
---|
366 | 355 | int status; |
---|
367 | 356 | struct spi_master *master; |
---|
368 | 357 | struct spi_gpio *spi_gpio; |
---|
369 | | - struct spi_gpio_platform_data *pdata; |
---|
370 | | - u16 master_flags = 0; |
---|
371 | | - bool use_of = 0; |
---|
| 358 | + struct device *dev = &pdev->dev; |
---|
| 359 | + struct spi_bitbang *bb; |
---|
372 | 360 | |
---|
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)); |
---|
| 361 | + master = devm_spi_alloc_master(dev, sizeof(*spi_gpio)); |
---|
386 | 362 | if (!master) |
---|
387 | 363 | return -ENOMEM; |
---|
388 | 364 | |
---|
| 365 | + if (pdev->dev.of_node) |
---|
| 366 | + status = spi_gpio_probe_dt(pdev, master); |
---|
| 367 | + else |
---|
| 368 | + status = spi_gpio_probe_pdata(pdev, master); |
---|
| 369 | + |
---|
| 370 | + if (status) |
---|
| 371 | + return status; |
---|
| 372 | + |
---|
389 | 373 | spi_gpio = spi_master_get_devdata(master); |
---|
390 | 374 | |
---|
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); |
---|
| 375 | + status = spi_gpio_request(dev, spi_gpio); |
---|
409 | 376 | if (status) |
---|
410 | 377 | return status; |
---|
411 | 378 | |
---|
412 | 379 | 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; |
---|
| 380 | + master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL | |
---|
| 381 | + SPI_CS_HIGH; |
---|
| 382 | + if (!spi_gpio->mosi) { |
---|
| 383 | + /* HW configuration without MOSI pin |
---|
| 384 | + * |
---|
| 385 | + * No setting SPI_MASTER_NO_RX here - if there is only |
---|
| 386 | + * a MOSI pin connected the host can still do RX by |
---|
| 387 | + * changing the direction of the line. |
---|
| 388 | + */ |
---|
| 389 | + master->flags = SPI_MASTER_NO_TX; |
---|
| 390 | + } |
---|
| 391 | + |
---|
415 | 392 | 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 | 393 | master->setup = spi_gpio_setup; |
---|
419 | 394 | master->cleanup = spi_gpio_cleanup; |
---|
420 | | -#ifdef CONFIG_OF |
---|
421 | | - master->dev.of_node = pdev->dev.of_node; |
---|
422 | | -#endif |
---|
423 | 395 | |
---|
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; |
---|
| 396 | + bb = &spi_gpio->bitbang; |
---|
| 397 | + bb->master = master; |
---|
| 398 | + /* |
---|
| 399 | + * There is some additional business, apart from driving the CS GPIO |
---|
| 400 | + * line, that we need to do on selection. This makes the local |
---|
| 401 | + * callback for chipselect always get called. |
---|
| 402 | + */ |
---|
| 403 | + master->flags |= SPI_MASTER_GPIO_SS; |
---|
| 404 | + bb->chipselect = spi_gpio_chipselect; |
---|
| 405 | + bb->set_line_direction = spi_gpio_set_direction; |
---|
427 | 406 | |
---|
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; |
---|
| 407 | + if (master->flags & SPI_MASTER_NO_TX) { |
---|
| 408 | + bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0; |
---|
| 409 | + bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1; |
---|
| 410 | + bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2; |
---|
| 411 | + bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3; |
---|
433 | 412 | } 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; |
---|
| 413 | + bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; |
---|
| 414 | + bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1; |
---|
| 415 | + bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2; |
---|
| 416 | + bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3; |
---|
438 | 417 | } |
---|
439 | | - spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer; |
---|
| 418 | + bb->setup_transfer = spi_bitbang_setup_transfer; |
---|
440 | 419 | |
---|
441 | | - return spi_bitbang_start(&spi_gpio->bitbang); |
---|
442 | | -} |
---|
| 420 | + status = spi_bitbang_init(&spi_gpio->bitbang); |
---|
| 421 | + if (status) |
---|
| 422 | + return status; |
---|
443 | 423 | |
---|
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; |
---|
| 424 | + return devm_spi_register_master(&pdev->dev, master); |
---|
458 | 425 | } |
---|
459 | 426 | |
---|
460 | 427 | MODULE_ALIAS("platform:" DRIVER_NAME); |
---|
.. | .. |
---|
465 | 432 | .of_match_table = of_match_ptr(spi_gpio_dt_ids), |
---|
466 | 433 | }, |
---|
467 | 434 | .probe = spi_gpio_probe, |
---|
468 | | - .remove = spi_gpio_remove, |
---|
469 | 435 | }; |
---|
470 | 436 | module_platform_driver(spi_gpio_driver); |
---|
471 | 437 | |
---|