| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * SPI_PPC4XX SPI controller driver. |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 10 | 11 | * Copyright (c) 2006 Ben Dooks |
|---|
| 11 | 12 | * Copyright (c) 2006 Simtec Electronics |
|---|
| 12 | 13 | * Ben Dooks <ben@simtec.co.uk> |
|---|
| 13 | | - * |
|---|
| 14 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 15 | | - * under the terms of the GNU General Public License version 2 as published |
|---|
| 16 | | - * by the Free Software Foundation. |
|---|
| 17 | 14 | */ |
|---|
| 18 | 15 | |
|---|
| 19 | 16 | /* |
|---|
| .. | .. |
|---|
| 31 | 28 | #include <linux/of_address.h> |
|---|
| 32 | 29 | #include <linux/of_irq.h> |
|---|
| 33 | 30 | #include <linux/of_platform.h> |
|---|
| 34 | | -#include <linux/of_gpio.h> |
|---|
| 35 | 31 | #include <linux/interrupt.h> |
|---|
| 36 | 32 | #include <linux/delay.h> |
|---|
| 37 | 33 | |
|---|
| 38 | | -#include <linux/gpio.h> |
|---|
| 39 | 34 | #include <linux/spi/spi.h> |
|---|
| 40 | 35 | #include <linux/spi/spi_bitbang.h> |
|---|
| 41 | 36 | |
|---|
| .. | .. |
|---|
| 129 | 124 | /* data buffers */ |
|---|
| 130 | 125 | const unsigned char *tx; |
|---|
| 131 | 126 | unsigned char *rx; |
|---|
| 132 | | - |
|---|
| 133 | | - int *gpios; |
|---|
| 134 | 127 | |
|---|
| 135 | 128 | struct spi_ppc4xx_regs __iomem *regs; /* pointer to the registers */ |
|---|
| 136 | 129 | struct spi_master *master; |
|---|
| .. | .. |
|---|
| 263 | 256 | return 0; |
|---|
| 264 | 257 | } |
|---|
| 265 | 258 | |
|---|
| 266 | | -static void spi_ppc4xx_chipsel(struct spi_device *spi, int value) |
|---|
| 267 | | -{ |
|---|
| 268 | | - struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master); |
|---|
| 269 | | - unsigned int cs = spi->chip_select; |
|---|
| 270 | | - unsigned int cspol; |
|---|
| 271 | | - |
|---|
| 272 | | - /* |
|---|
| 273 | | - * If there are no chip selects at all, or if this is the special |
|---|
| 274 | | - * case of a non-existent (dummy) chip select, do nothing. |
|---|
| 275 | | - */ |
|---|
| 276 | | - |
|---|
| 277 | | - if (!hw->master->num_chipselect || hw->gpios[cs] == -EEXIST) |
|---|
| 278 | | - return; |
|---|
| 279 | | - |
|---|
| 280 | | - cspol = spi->mode & SPI_CS_HIGH ? 1 : 0; |
|---|
| 281 | | - if (value == BITBANG_CS_INACTIVE) |
|---|
| 282 | | - cspol = !cspol; |
|---|
| 283 | | - |
|---|
| 284 | | - gpio_set_value(hw->gpios[cs], cspol); |
|---|
| 285 | | -} |
|---|
| 286 | | - |
|---|
| 287 | 259 | static irqreturn_t spi_ppc4xx_int(int irq, void *dev_id) |
|---|
| 288 | 260 | { |
|---|
| 289 | 261 | struct ppc4xx_spi *hw; |
|---|
| .. | .. |
|---|
| 362 | 334 | dcri_clrset(SDR0, SDR0_PFC1, 0x80000000 >> 14, 0); |
|---|
| 363 | 335 | } |
|---|
| 364 | 336 | |
|---|
| 365 | | -static void free_gpios(struct ppc4xx_spi *hw) |
|---|
| 366 | | -{ |
|---|
| 367 | | - if (hw->master->num_chipselect) { |
|---|
| 368 | | - int i; |
|---|
| 369 | | - for (i = 0; i < hw->master->num_chipselect; i++) |
|---|
| 370 | | - if (gpio_is_valid(hw->gpios[i])) |
|---|
| 371 | | - gpio_free(hw->gpios[i]); |
|---|
| 372 | | - |
|---|
| 373 | | - kfree(hw->gpios); |
|---|
| 374 | | - hw->gpios = NULL; |
|---|
| 375 | | - } |
|---|
| 376 | | -} |
|---|
| 377 | | - |
|---|
| 378 | 337 | /* |
|---|
| 379 | 338 | * platform_device layer stuff... |
|---|
| 380 | 339 | */ |
|---|
| .. | .. |
|---|
| 388 | 347 | struct device *dev = &op->dev; |
|---|
| 389 | 348 | struct device_node *opbnp; |
|---|
| 390 | 349 | int ret; |
|---|
| 391 | | - int num_gpios; |
|---|
| 392 | 350 | const unsigned int *clk; |
|---|
| 393 | 351 | |
|---|
| 394 | 352 | master = spi_alloc_master(dev, sizeof *hw); |
|---|
| .. | .. |
|---|
| 402 | 360 | |
|---|
| 403 | 361 | init_completion(&hw->done); |
|---|
| 404 | 362 | |
|---|
| 405 | | - /* |
|---|
| 406 | | - * A count of zero implies a single SPI device without any chip-select. |
|---|
| 407 | | - * Note that of_gpio_count counts all gpios assigned to this spi master. |
|---|
| 408 | | - * This includes both "null" gpio's and real ones. |
|---|
| 409 | | - */ |
|---|
| 410 | | - num_gpios = of_gpio_count(np); |
|---|
| 411 | | - if (num_gpios > 0) { |
|---|
| 412 | | - int i; |
|---|
| 413 | | - |
|---|
| 414 | | - hw->gpios = kcalloc(num_gpios, sizeof(*hw->gpios), GFP_KERNEL); |
|---|
| 415 | | - if (!hw->gpios) { |
|---|
| 416 | | - ret = -ENOMEM; |
|---|
| 417 | | - goto free_master; |
|---|
| 418 | | - } |
|---|
| 419 | | - |
|---|
| 420 | | - for (i = 0; i < num_gpios; i++) { |
|---|
| 421 | | - int gpio; |
|---|
| 422 | | - enum of_gpio_flags flags; |
|---|
| 423 | | - |
|---|
| 424 | | - gpio = of_get_gpio_flags(np, i, &flags); |
|---|
| 425 | | - hw->gpios[i] = gpio; |
|---|
| 426 | | - |
|---|
| 427 | | - if (gpio_is_valid(gpio)) { |
|---|
| 428 | | - /* Real CS - set the initial state. */ |
|---|
| 429 | | - ret = gpio_request(gpio, np->name); |
|---|
| 430 | | - if (ret < 0) { |
|---|
| 431 | | - dev_err(dev, |
|---|
| 432 | | - "can't request gpio #%d: %d\n", |
|---|
| 433 | | - i, ret); |
|---|
| 434 | | - goto free_gpios; |
|---|
| 435 | | - } |
|---|
| 436 | | - |
|---|
| 437 | | - gpio_direction_output(gpio, |
|---|
| 438 | | - !!(flags & OF_GPIO_ACTIVE_LOW)); |
|---|
| 439 | | - } else if (gpio == -EEXIST) { |
|---|
| 440 | | - ; /* No CS, but that's OK. */ |
|---|
| 441 | | - } else { |
|---|
| 442 | | - dev_err(dev, "invalid gpio #%d: %d\n", i, gpio); |
|---|
| 443 | | - ret = -EINVAL; |
|---|
| 444 | | - goto free_gpios; |
|---|
| 445 | | - } |
|---|
| 446 | | - } |
|---|
| 447 | | - } |
|---|
| 448 | | - |
|---|
| 449 | 363 | /* Setup the state for the bitbang driver */ |
|---|
| 450 | 364 | bbp = &hw->bitbang; |
|---|
| 451 | 365 | bbp->master = hw->master; |
|---|
| 452 | 366 | bbp->setup_transfer = spi_ppc4xx_setupxfer; |
|---|
| 453 | | - bbp->chipselect = spi_ppc4xx_chipsel; |
|---|
| 454 | 367 | bbp->txrx_bufs = spi_ppc4xx_txrx; |
|---|
| 455 | 368 | bbp->use_dma = 0; |
|---|
| 456 | 369 | bbp->master->setup = spi_ppc4xx_setup; |
|---|
| 457 | 370 | bbp->master->cleanup = spi_ppc4xx_cleanup; |
|---|
| 458 | 371 | bbp->master->bits_per_word_mask = SPI_BPW_MASK(8); |
|---|
| 372 | + bbp->master->use_gpio_descriptors = true; |
|---|
| 373 | + /* |
|---|
| 374 | + * The SPI core will count the number of GPIO descriptors to figure |
|---|
| 375 | + * out the number of chip selects available on the platform. |
|---|
| 376 | + */ |
|---|
| 377 | + bbp->master->num_chipselect = 0; |
|---|
| 459 | 378 | |
|---|
| 460 | 379 | /* the spi->mode bits understood by this driver: */ |
|---|
| 461 | 380 | bbp->master->mode_bits = |
|---|
| 462 | 381 | SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST; |
|---|
| 463 | | - |
|---|
| 464 | | - /* this many pins in all GPIO controllers */ |
|---|
| 465 | | - bbp->master->num_chipselect = num_gpios > 0 ? num_gpios : 0; |
|---|
| 466 | 382 | |
|---|
| 467 | 383 | /* Get the clock for the OPB */ |
|---|
| 468 | 384 | opbnp = of_find_compatible_node(NULL, NULL, "ibm,opb"); |
|---|
| 469 | 385 | if (opbnp == NULL) { |
|---|
| 470 | 386 | dev_err(dev, "OPB: cannot find node\n"); |
|---|
| 471 | 387 | ret = -ENODEV; |
|---|
| 472 | | - goto free_gpios; |
|---|
| 388 | + goto free_master; |
|---|
| 473 | 389 | } |
|---|
| 474 | 390 | /* Get the clock (Hz) for the OPB */ |
|---|
| 475 | 391 | clk = of_get_property(opbnp, "clock-frequency", NULL); |
|---|
| .. | .. |
|---|
| 477 | 393 | dev_err(dev, "OPB: no clock-frequency property set\n"); |
|---|
| 478 | 394 | of_node_put(opbnp); |
|---|
| 479 | 395 | ret = -ENODEV; |
|---|
| 480 | | - goto free_gpios; |
|---|
| 396 | + goto free_master; |
|---|
| 481 | 397 | } |
|---|
| 482 | 398 | hw->opb_freq = *clk; |
|---|
| 483 | 399 | hw->opb_freq >>= 2; |
|---|
| .. | .. |
|---|
| 486 | 402 | ret = of_address_to_resource(np, 0, &resource); |
|---|
| 487 | 403 | if (ret) { |
|---|
| 488 | 404 | dev_err(dev, "error while parsing device node resource\n"); |
|---|
| 489 | | - goto free_gpios; |
|---|
| 405 | + goto free_master; |
|---|
| 490 | 406 | } |
|---|
| 491 | 407 | hw->mapbase = resource.start; |
|---|
| 492 | 408 | hw->mapsize = resource_size(&resource); |
|---|
| .. | .. |
|---|
| 495 | 411 | if (hw->mapsize < sizeof(struct spi_ppc4xx_regs)) { |
|---|
| 496 | 412 | dev_err(dev, "too small to map registers\n"); |
|---|
| 497 | 413 | ret = -EINVAL; |
|---|
| 498 | | - goto free_gpios; |
|---|
| 414 | + goto free_master; |
|---|
| 499 | 415 | } |
|---|
| 500 | 416 | |
|---|
| 501 | 417 | /* Request IRQ */ |
|---|
| .. | .. |
|---|
| 504 | 420 | 0, "spi_ppc4xx_of", (void *)hw); |
|---|
| 505 | 421 | if (ret) { |
|---|
| 506 | 422 | dev_err(dev, "unable to allocate interrupt\n"); |
|---|
| 507 | | - goto free_gpios; |
|---|
| 423 | + goto free_master; |
|---|
| 508 | 424 | } |
|---|
| 509 | 425 | |
|---|
| 510 | 426 | if (!request_mem_region(hw->mapbase, hw->mapsize, DRIVER_NAME)) { |
|---|
| .. | .. |
|---|
| 541 | 457 | release_mem_region(hw->mapbase, hw->mapsize); |
|---|
| 542 | 458 | request_mem_error: |
|---|
| 543 | 459 | free_irq(hw->irqnum, hw); |
|---|
| 544 | | -free_gpios: |
|---|
| 545 | | - free_gpios(hw); |
|---|
| 546 | 460 | free_master: |
|---|
| 547 | 461 | spi_master_put(master); |
|---|
| 548 | 462 | |
|---|
| .. | .. |
|---|
| 559 | 473 | release_mem_region(hw->mapbase, hw->mapsize); |
|---|
| 560 | 474 | free_irq(hw->irqnum, hw); |
|---|
| 561 | 475 | iounmap(hw->regs); |
|---|
| 562 | | - free_gpios(hw); |
|---|
| 563 | 476 | spi_master_put(master); |
|---|
| 564 | 477 | return 0; |
|---|
| 565 | 478 | } |
|---|