Lines Matching +full:spi +full:- +full:gpio
2 * SPI_PPC4XX SPI controller driver.
8 * Based in part on drivers/spi/spi_s3c24xx.c
20 * The PPC4xx SPI controller has no FIFO so each sent/received byte will
23 * during SPI transfers by setting max_speed_hz via the device tree.
38 #include <linux/gpio.h>
39 #include <linux/spi/spi.h>
40 #include <linux/spi/spi_bitbang.h>
44 #include <asm/dcr-regs.h>
46 /* bits in mode register - bit 0 is MSb */
59 * SPI_PPC4XX_MODE_RD = 0 means "MSB first" - this is the normal mode
60 * SPI_PPC4XX_MODE_RD = 1 means "LSB first" - this is bit-reversed mode
88 /* clock settings (SCP and CI) for various SPI modes */
108 * CDM = (OPBCLK/4*SCPClkOut) - 1
114 /* SPI Controller driver's private data. */
123 /* need this to set the SPI clock */
145 static int spi_ppc4xx_txrx(struct spi_device *spi, struct spi_transfer *t) in spi_ppc4xx_txrx() argument
150 dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n", in spi_ppc4xx_txrx()
151 t->tx_buf, t->rx_buf, t->len); in spi_ppc4xx_txrx()
153 hw = spi_master_get_devdata(spi->master); in spi_ppc4xx_txrx()
155 hw->tx = t->tx_buf; in spi_ppc4xx_txrx()
156 hw->rx = t->rx_buf; in spi_ppc4xx_txrx()
157 hw->len = t->len; in spi_ppc4xx_txrx()
158 hw->count = 0; in spi_ppc4xx_txrx()
161 data = hw->tx ? hw->tx[0] : 0; in spi_ppc4xx_txrx()
162 out_8(&hw->regs->txd, data); in spi_ppc4xx_txrx()
163 out_8(&hw->regs->cr, SPI_PPC4XX_CR_STR); in spi_ppc4xx_txrx()
164 wait_for_completion(&hw->done); in spi_ppc4xx_txrx()
166 return hw->count; in spi_ppc4xx_txrx()
169 static int spi_ppc4xx_setupxfer(struct spi_device *spi, struct spi_transfer *t) in spi_ppc4xx_setupxfer() argument
171 struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master); in spi_ppc4xx_setupxfer()
172 struct spi_ppc4xx_cs *cs = spi->controller_state; in spi_ppc4xx_setupxfer()
179 bits_per_word = spi->bits_per_word; in spi_ppc4xx_setupxfer()
180 speed = spi->max_speed_hz; in spi_ppc4xx_setupxfer()
187 if (t->bits_per_word) in spi_ppc4xx_setupxfer()
188 bits_per_word = t->bits_per_word; in spi_ppc4xx_setupxfer()
190 if (t->speed_hz) in spi_ppc4xx_setupxfer()
191 speed = min(t->speed_hz, spi->max_speed_hz); in spi_ppc4xx_setupxfer()
195 dev_err(&spi->dev, "invalid bits-per-word (%d)\n", in spi_ppc4xx_setupxfer()
197 return -EINVAL; in spi_ppc4xx_setupxfer()
200 if (!speed || (speed > spi->max_speed_hz)) { in spi_ppc4xx_setupxfer()
201 dev_err(&spi->dev, "invalid speed_hz (%d)\n", speed); in spi_ppc4xx_setupxfer()
202 return -EINVAL; in spi_ppc4xx_setupxfer()
206 out_8(&hw->regs->mode, cs->mode); in spi_ppc4xx_setupxfer()
210 scr = (hw->opb_freq / speed) - 1; in spi_ppc4xx_setupxfer()
214 dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", cdm, speed); in spi_ppc4xx_setupxfer()
216 if (in_8(&hw->regs->cdm) != cdm) in spi_ppc4xx_setupxfer()
217 out_8(&hw->regs->cdm, cdm); in spi_ppc4xx_setupxfer()
219 spin_lock(&hw->bitbang.lock); in spi_ppc4xx_setupxfer()
220 if (!hw->bitbang.busy) { in spi_ppc4xx_setupxfer()
221 hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE); in spi_ppc4xx_setupxfer()
224 spin_unlock(&hw->bitbang.lock); in spi_ppc4xx_setupxfer()
229 static int spi_ppc4xx_setup(struct spi_device *spi) in spi_ppc4xx_setup() argument
231 struct spi_ppc4xx_cs *cs = spi->controller_state; in spi_ppc4xx_setup()
233 if (spi->bits_per_word != 8) { in spi_ppc4xx_setup()
234 dev_err(&spi->dev, "invalid bits-per-word (%d)\n", in spi_ppc4xx_setup()
235 spi->bits_per_word); in spi_ppc4xx_setup()
236 return -EINVAL; in spi_ppc4xx_setup()
239 if (!spi->max_speed_hz) { in spi_ppc4xx_setup()
240 dev_err(&spi->dev, "invalid max_speed_hz (must be non-zero)\n"); in spi_ppc4xx_setup()
241 return -EINVAL; in spi_ppc4xx_setup()
247 return -ENOMEM; in spi_ppc4xx_setup()
248 spi->controller_state = cs; in spi_ppc4xx_setup()
253 * no need to read-modify-write in spi_ppc4xx_setup()
255 cs->mode = SPI_PPC4XX_MODE_SPE; in spi_ppc4xx_setup()
257 switch (spi->mode & (SPI_CPHA | SPI_CPOL)) { in spi_ppc4xx_setup()
259 cs->mode |= SPI_CLK_MODE0; in spi_ppc4xx_setup()
262 cs->mode |= SPI_CLK_MODE1; in spi_ppc4xx_setup()
265 cs->mode |= SPI_CLK_MODE2; in spi_ppc4xx_setup()
268 cs->mode |= SPI_CLK_MODE3; in spi_ppc4xx_setup()
272 if (spi->mode & SPI_LSB_FIRST) in spi_ppc4xx_setup()
273 cs->mode |= SPI_PPC4XX_MODE_RD; in spi_ppc4xx_setup()
278 static void spi_ppc4xx_chipsel(struct spi_device *spi, int value) in spi_ppc4xx_chipsel() argument
280 struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master); in spi_ppc4xx_chipsel()
281 unsigned int cs = spi->chip_select; in spi_ppc4xx_chipsel()
286 * case of a non-existent (dummy) chip select, do nothing. in spi_ppc4xx_chipsel()
289 if (!hw->master->num_chipselect || hw->gpios[cs] == -EEXIST) in spi_ppc4xx_chipsel()
292 cspol = spi->mode & SPI_CS_HIGH ? 1 : 0; in spi_ppc4xx_chipsel()
296 gpio_set_value(hw->gpios[cs], cspol); in spi_ppc4xx_chipsel()
308 status = in_8(&hw->regs->sr); in spi_ppc4xx_int()
313 * BSY de-asserts one cycle after the transfer is complete. The in spi_ppc4xx_int()
322 dev_dbg(hw->dev, "got interrupt but spi still busy?\n"); in spi_ppc4xx_int()
325 lstatus = in_8(&hw->regs->sr); in spi_ppc4xx_int()
329 dev_err(hw->dev, "busywait: too many loops!\n"); in spi_ppc4xx_int()
330 complete(&hw->done); in spi_ppc4xx_int()
334 status = in_8(&hw->regs->sr); in spi_ppc4xx_int()
335 dev_dbg(hw->dev, "loops %d status %x\n", cnt, status); in spi_ppc4xx_int()
339 count = hw->count; in spi_ppc4xx_int()
340 hw->count++; in spi_ppc4xx_int()
343 data = in_8(&hw->regs->rxd); in spi_ppc4xx_int()
344 if (hw->rx) in spi_ppc4xx_int()
345 hw->rx[count] = data; in spi_ppc4xx_int()
349 if (count < hw->len) { in spi_ppc4xx_int()
350 data = hw->tx ? hw->tx[count] : 0; in spi_ppc4xx_int()
351 out_8(&hw->regs->txd, data); in spi_ppc4xx_int()
352 out_8(&hw->regs->cr, SPI_PPC4XX_CR_STR); in spi_ppc4xx_int()
354 complete(&hw->done); in spi_ppc4xx_int()
360 static void spi_ppc4xx_cleanup(struct spi_device *spi) in spi_ppc4xx_cleanup() argument
362 kfree(spi->controller_state); in spi_ppc4xx_cleanup()
368 * On all 4xx PPC's the SPI bus is shared/multiplexed with in spi_ppc4xx_enable()
369 * the 2nd I2C bus. We need to enable the the SPI bus before in spi_ppc4xx_enable()
379 if (hw->master->num_chipselect) { in free_gpios()
381 for (i = 0; i < hw->master->num_chipselect; i++) in free_gpios()
382 if (gpio_is_valid(hw->gpios[i])) in free_gpios()
383 gpio_free(hw->gpios[i]); in free_gpios()
385 kfree(hw->gpios); in free_gpios()
386 hw->gpios = NULL; in free_gpios()
399 struct device_node *np = op->dev.of_node; in spi_ppc4xx_of_probe()
400 struct device *dev = &op->dev; in spi_ppc4xx_of_probe()
408 return -ENOMEM; in spi_ppc4xx_of_probe()
409 master->dev.of_node = np; in spi_ppc4xx_of_probe()
412 hw->master = spi_master_get(master); in spi_ppc4xx_of_probe()
413 hw->dev = dev; in spi_ppc4xx_of_probe()
415 init_completion(&hw->done); in spi_ppc4xx_of_probe()
418 * A count of zero implies a single SPI device without any chip-select. in spi_ppc4xx_of_probe()
419 * Note that of_gpio_count counts all gpios assigned to this spi master. in spi_ppc4xx_of_probe()
420 * This includes both "null" gpio's and real ones. in spi_ppc4xx_of_probe()
426 hw->gpios = kzalloc(sizeof(int) * num_gpios, GFP_KERNEL); in spi_ppc4xx_of_probe()
427 if (!hw->gpios) { in spi_ppc4xx_of_probe()
428 ret = -ENOMEM; in spi_ppc4xx_of_probe()
433 int gpio; in spi_ppc4xx_of_probe() local
436 gpio = of_get_gpio_flags(np, i, &flags); in spi_ppc4xx_of_probe()
437 hw->gpios[i] = gpio; in spi_ppc4xx_of_probe()
439 if (gpio_is_valid(gpio)) { in spi_ppc4xx_of_probe()
440 /* Real CS - set the initial state. */ in spi_ppc4xx_of_probe()
441 ret = gpio_request(gpio, np->name); in spi_ppc4xx_of_probe()
443 dev_err(dev, "can't request gpio " in spi_ppc4xx_of_probe()
448 gpio_direction_output(gpio, in spi_ppc4xx_of_probe()
450 } else if (gpio == -EEXIST) { in spi_ppc4xx_of_probe()
453 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio); in spi_ppc4xx_of_probe()
454 ret = -EINVAL; in spi_ppc4xx_of_probe()
461 bbp = &hw->bitbang; in spi_ppc4xx_of_probe()
462 bbp->master = hw->master; in spi_ppc4xx_of_probe()
463 bbp->setup_transfer = spi_ppc4xx_setupxfer; in spi_ppc4xx_of_probe()
464 bbp->chipselect = spi_ppc4xx_chipsel; in spi_ppc4xx_of_probe()
465 bbp->txrx_bufs = spi_ppc4xx_txrx; in spi_ppc4xx_of_probe()
466 bbp->use_dma = 0; in spi_ppc4xx_of_probe()
467 bbp->master->setup = spi_ppc4xx_setup; in spi_ppc4xx_of_probe()
468 bbp->master->cleanup = spi_ppc4xx_cleanup; in spi_ppc4xx_of_probe()
471 bbp->master->bus_num = -1; in spi_ppc4xx_of_probe()
473 /* the spi->mode bits understood by this driver: */ in spi_ppc4xx_of_probe()
474 bbp->master->mode_bits = in spi_ppc4xx_of_probe()
477 /* this many pins in all GPIO controllers */ in spi_ppc4xx_of_probe()
478 bbp->master->num_chipselect = num_gpios; in spi_ppc4xx_of_probe()
484 ret = -ENODEV; in spi_ppc4xx_of_probe()
488 clk = of_get_property(opbnp, "clock-frequency", NULL); in spi_ppc4xx_of_probe()
490 dev_err(dev, "OPB: no clock-frequency property set\n"); in spi_ppc4xx_of_probe()
492 ret = -ENODEV; in spi_ppc4xx_of_probe()
495 hw->opb_freq = *clk; in spi_ppc4xx_of_probe()
496 hw->opb_freq >>= 2; in spi_ppc4xx_of_probe()
504 hw->mapbase = resource.start; in spi_ppc4xx_of_probe()
505 hw->mapsize = resource_size(&resource); in spi_ppc4xx_of_probe()
508 if (hw->mapsize < sizeof(struct spi_ppc4xx_regs)) { in spi_ppc4xx_of_probe()
510 ret = -EINVAL; in spi_ppc4xx_of_probe()
515 hw->irqnum = irq_of_parse_and_map(np, 0); in spi_ppc4xx_of_probe()
516 ret = request_irq(hw->irqnum, spi_ppc4xx_int, in spi_ppc4xx_of_probe()
523 if (!request_mem_region(hw->mapbase, hw->mapsize, DRIVER_NAME)) { in spi_ppc4xx_of_probe()
525 ret = -EBUSY; in spi_ppc4xx_of_probe()
529 hw->regs = ioremap(hw->mapbase, sizeof(struct spi_ppc4xx_regs)); in spi_ppc4xx_of_probe()
531 if (!hw->regs) { in spi_ppc4xx_of_probe()
533 ret = -ENXIO; in spi_ppc4xx_of_probe()
539 /* Finally register our spi controller */ in spi_ppc4xx_of_probe()
540 dev->dma_mask = 0; in spi_ppc4xx_of_probe()
543 dev_err(dev, "failed to register SPI master\n"); in spi_ppc4xx_of_probe()
552 iounmap(hw->regs); in spi_ppc4xx_of_probe()
554 release_mem_region(hw->mapbase, hw->mapsize); in spi_ppc4xx_of_probe()
556 free_irq(hw->irqnum, hw); in spi_ppc4xx_of_probe()
569 struct spi_master *master = dev_get_drvdata(&op->dev); in spi_ppc4xx_of_remove()
572 spi_bitbang_stop(&hw->bitbang); in spi_ppc4xx_of_remove()
573 dev_set_drvdata(&op->dev, NULL); in spi_ppc4xx_of_remove()
574 release_mem_region(hw->mapbase, hw->mapsize); in spi_ppc4xx_of_remove()
575 free_irq(hw->irqnum, hw); in spi_ppc4xx_of_remove()
576 iounmap(hw->regs); in spi_ppc4xx_of_remove()
582 { .compatible = "ibm,ppc4xx-spi", },
600 MODULE_DESCRIPTION("Simple PPC4xx SPI Driver");