2 * Freescale SPI controller driver.
4 * Maintainer: Kumar Gala
6 * Copyright (C) 2006 Polycom, Inc.
7 * Copyright 2010 Freescale Semiconductor, Inc.
9 * CPM SPI and QE buffer descriptors mode support:
10 * Copyright (c) 2009 MontaVista Software, Inc.
11 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
14 * Copyright (c) 2012 Aeroflex Gaisler AB.
15 * Author: Andreas Larsson <andreas@gaisler.com>
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 of the License, or (at your
20 * option) any later version.
22 #include <linux/delay.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/fsl_devices.h>
25 #include <linux/gpio.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
33 #include <linux/of_address.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_gpio.h>
36 #include <linux/of_platform.h>
37 #include <linux/platform_device.h>
38 #include <linux/spi/spi.h>
39 #include <linux/spi/spi_bitbang.h>
40 #include <linux/types.h>
42 #include "spi-fsl-lib.h"
43 #include "spi-fsl-cpm.h"
44 #include "spi-fsl-spi.h"
49 struct fsl_spi_match_data {
53 static struct fsl_spi_match_data of_fsl_spi_fsl_config = {
57 static struct fsl_spi_match_data of_fsl_spi_grlib_config = {
61 static const struct of_device_id of_fsl_spi_match[] = {
63 .compatible = "fsl,spi",
64 .data = &of_fsl_spi_fsl_config,
67 .compatible = "aeroflexgaisler,spictrl",
68 .data = &of_fsl_spi_grlib_config,
72 MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
74 static int fsl_spi_get_type(struct device *dev)
76 const struct of_device_id *match;
79 match = of_match_node(of_fsl_spi_match, dev->of_node);
80 if (match && match->data)
81 return ((struct fsl_spi_match_data *)match->data)->type;
86 static void fsl_spi_change_mode(struct spi_device *spi)
88 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
89 struct spi_mpc8xxx_cs *cs = spi->controller_state;
90 struct fsl_spi_reg *reg_base = mspi->reg_base;
91 __be32 __iomem *mode = ®_base->mode;
94 if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
97 /* Turn off IRQs locally to minimize time that SPI is disabled. */
98 local_irq_save(flags);
100 /* Turn off SPI unit prior changing mode */
101 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
103 /* When in CPM mode, we need to reinit tx and rx. */
104 if (mspi->flags & SPI_CPM_MODE) {
105 fsl_spi_cpm_reinit_txrx(mspi);
107 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
108 local_irq_restore(flags);
111 static void fsl_spi_chipselect(struct spi_device *spi, int value)
113 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
114 struct fsl_spi_platform_data *pdata;
115 bool pol = spi->mode & SPI_CS_HIGH;
116 struct spi_mpc8xxx_cs *cs = spi->controller_state;
118 pdata = spi->dev.parent->parent->platform_data;
120 if (value == BITBANG_CS_INACTIVE) {
121 if (pdata->cs_control)
122 pdata->cs_control(spi, !pol);
125 if (value == BITBANG_CS_ACTIVE) {
126 mpc8xxx_spi->rx_shift = cs->rx_shift;
127 mpc8xxx_spi->tx_shift = cs->tx_shift;
128 mpc8xxx_spi->get_rx = cs->get_rx;
129 mpc8xxx_spi->get_tx = cs->get_tx;
131 fsl_spi_change_mode(spi);
133 if (pdata->cs_control)
134 pdata->cs_control(spi, pol);
138 static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift,
139 int bits_per_word, int msb_first)
144 if (bits_per_word <= 8) {
147 } else if (bits_per_word <= 16) {
152 if (bits_per_word <= 8)
157 static void fsl_spi_grlib_set_shifts(u32 *rx_shift, u32 *tx_shift,
158 int bits_per_word, int msb_first)
162 if (bits_per_word <= 16) {
164 *rx_shift = 16; /* LSB in bit 16 */
165 *tx_shift = 32 - bits_per_word; /* MSB in bit 31 */
167 *rx_shift = 16 - bits_per_word; /* MSB in bit 15 */
172 static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
173 struct spi_device *spi,
174 struct mpc8xxx_spi *mpc8xxx_spi,
179 if (bits_per_word <= 8) {
180 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
181 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
182 } else if (bits_per_word <= 16) {
183 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
184 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
185 } else if (bits_per_word <= 32) {
186 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
187 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
191 if (mpc8xxx_spi->set_shifts)
192 mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift,
194 !(spi->mode & SPI_LSB_FIRST));
196 mpc8xxx_spi->rx_shift = cs->rx_shift;
197 mpc8xxx_spi->tx_shift = cs->tx_shift;
198 mpc8xxx_spi->get_rx = cs->get_rx;
199 mpc8xxx_spi->get_tx = cs->get_tx;
201 return bits_per_word;
204 static int fsl_spi_setup_transfer(struct spi_device *spi,
205 struct spi_transfer *t)
207 struct mpc8xxx_spi *mpc8xxx_spi;
208 int bits_per_word = 0;
211 struct spi_mpc8xxx_cs *cs = spi->controller_state;
213 mpc8xxx_spi = spi_master_get_devdata(spi->master);
216 bits_per_word = t->bits_per_word;
220 /* spi_transfer level calls that work per-word */
222 bits_per_word = spi->bits_per_word;
225 hz = spi->max_speed_hz;
227 if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
228 bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
232 if (bits_per_word < 0)
233 return bits_per_word;
235 if (bits_per_word == 32)
238 bits_per_word = bits_per_word - 1;
240 /* mask out bits we are going to set */
241 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
244 cs->hw_mode |= SPMODE_LEN(bits_per_word);
246 if ((mpc8xxx_spi->spibrg / hz) > 64) {
247 cs->hw_mode |= SPMODE_DIV16;
248 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
250 "%s: Requested speed is too low: %d Hz. Will use %d Hz instead.\n",
251 dev_name(&spi->dev), hz, mpc8xxx_spi->spibrg / 1024);
255 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
260 cs->hw_mode |= SPMODE_PM(pm);
262 fsl_spi_change_mode(spi);
266 static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
267 struct spi_transfer *t, unsigned int len)
270 struct fsl_spi_reg *reg_base = mspi->reg_base;
275 mpc8xxx_spi_write_reg(®_base->mask, SPIM_NE);
278 word = mspi->get_tx(mspi);
279 mpc8xxx_spi_write_reg(®_base->transmit, word);
284 static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
287 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
288 struct fsl_spi_reg *reg_base;
289 unsigned int len = t->len;
293 reg_base = mpc8xxx_spi->reg_base;
294 bits_per_word = spi->bits_per_word;
295 if (t->bits_per_word)
296 bits_per_word = t->bits_per_word;
298 if (bits_per_word > 8) {
299 /* invalid length? */
304 if (bits_per_word > 16) {
305 /* invalid length? */
311 mpc8xxx_spi->tx = t->tx_buf;
312 mpc8xxx_spi->rx = t->rx_buf;
314 reinit_completion(&mpc8xxx_spi->done);
316 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
317 ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
319 ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
323 wait_for_completion(&mpc8xxx_spi->done);
325 /* disable rx ints */
326 mpc8xxx_spi_write_reg(®_base->mask, 0);
328 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
329 fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
331 return mpc8xxx_spi->count;
334 static int fsl_spi_do_one_msg(struct spi_master *master,
335 struct spi_message *m)
337 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
338 struct spi_device *spi = m->spi;
339 struct spi_transfer *t, *first;
340 unsigned int cs_change;
341 const int nsecs = 50;
342 int status, last_bpw;
345 * In CPU mode, optimize large byte transfers to use larger
346 * bits_per_word values to reduce number of interrupts taken.
348 list_for_each_entry(t, &m->transfers, transfer_list) {
349 if (!(mpc8xxx_spi->flags & SPI_CPM_MODE)) {
350 if (t->len < 256 || t->bits_per_word != 8)
352 if ((t->len & 3) == 0)
353 t->bits_per_word = 32;
354 else if ((t->len & 1) == 0)
355 t->bits_per_word = 16;
358 * CPM/QE uses Little Endian for words > 8
359 * so transform 16 and 32 bits words into 8 bits
360 * Unfortnatly that doesn't work for LSB so
361 * reject these for now
362 * Note: 32 bits word, LSB works iff
363 * tfcr/rfcr is set to CPMFCR_GBL
365 if (m->spi->mode & SPI_LSB_FIRST && t->bits_per_word > 8)
367 if (t->bits_per_word == 16 || t->bits_per_word == 32)
368 t->bits_per_word = 8; /* pretend its 8 bits */
369 if (t->bits_per_word == 8 && t->len >= 256 &&
370 (mpc8xxx_spi->flags & SPI_CPM1))
371 t->bits_per_word = 16;
375 /* Don't allow changes if CS is active */
377 list_for_each_entry(t, &m->transfers, transfer_list) {
380 cs_change = t->cs_change;
381 if (first->speed_hz != t->speed_hz) {
383 "speed_hz cannot change while CS is active\n");
391 list_for_each_entry(t, &m->transfers, transfer_list) {
392 if (cs_change || last_bpw != t->bits_per_word)
393 status = fsl_spi_setup_transfer(spi, t);
396 last_bpw = t->bits_per_word;
399 fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
402 cs_change = t->cs_change;
404 status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
409 m->actual_length += t->len;
412 udelay(t->delay_usecs);
416 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
423 if (status || !cs_change) {
425 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
428 fsl_spi_setup_transfer(spi, NULL);
429 spi_finalize_current_message(master);
433 static int fsl_spi_setup(struct spi_device *spi)
435 struct mpc8xxx_spi *mpc8xxx_spi;
436 struct fsl_spi_reg *reg_base;
439 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
441 if (!spi->max_speed_hz)
445 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
448 spi_set_ctldata(spi, cs);
450 mpc8xxx_spi = spi_master_get_devdata(spi->master);
452 reg_base = mpc8xxx_spi->reg_base;
454 hw_mode = cs->hw_mode; /* Save original settings */
455 cs->hw_mode = mpc8xxx_spi_read_reg(®_base->mode);
456 /* mask out bits we are going to set */
457 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
458 | SPMODE_REV | SPMODE_LOOP);
460 if (spi->mode & SPI_CPHA)
461 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
462 if (spi->mode & SPI_CPOL)
463 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
464 if (!(spi->mode & SPI_LSB_FIRST))
465 cs->hw_mode |= SPMODE_REV;
466 if (spi->mode & SPI_LOOP)
467 cs->hw_mode |= SPMODE_LOOP;
469 retval = fsl_spi_setup_transfer(spi, NULL);
471 cs->hw_mode = hw_mode; /* Restore settings */
475 if (mpc8xxx_spi->type == TYPE_GRLIB) {
476 if (gpio_is_valid(spi->cs_gpio)) {
479 retval = gpio_request(spi->cs_gpio,
480 dev_name(&spi->dev));
484 desel = !(spi->mode & SPI_CS_HIGH);
485 retval = gpio_direction_output(spi->cs_gpio, desel);
487 gpio_free(spi->cs_gpio);
490 } else if (spi->cs_gpio != -ENOENT) {
491 if (spi->cs_gpio < 0)
495 /* When spi->cs_gpio == -ENOENT, a hole in the phandle list
496 * indicates to use native chipselect if present, or allow for
497 * an always selected chip
501 /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
502 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
507 static void fsl_spi_cleanup(struct spi_device *spi)
509 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
510 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
512 if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio))
513 gpio_free(spi->cs_gpio);
516 spi_set_ctldata(spi, NULL);
519 static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
521 struct fsl_spi_reg *reg_base = mspi->reg_base;
523 /* We need handle RX first */
524 if (events & SPIE_NE) {
525 u32 rx_data = mpc8xxx_spi_read_reg(®_base->receive);
528 mspi->get_rx(rx_data, mspi);
531 if ((events & SPIE_NF) == 0)
532 /* spin until TX is done */
534 mpc8xxx_spi_read_reg(®_base->event)) &
538 /* Clear the events */
539 mpc8xxx_spi_write_reg(®_base->event, events);
543 u32 word = mspi->get_tx(mspi);
545 mpc8xxx_spi_write_reg(®_base->transmit, word);
547 complete(&mspi->done);
551 static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
553 struct mpc8xxx_spi *mspi = context_data;
554 irqreturn_t ret = IRQ_NONE;
556 struct fsl_spi_reg *reg_base = mspi->reg_base;
558 /* Get interrupt events(tx/rx) */
559 events = mpc8xxx_spi_read_reg(®_base->event);
563 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
565 if (mspi->flags & SPI_CPM_MODE)
566 fsl_spi_cpm_irq(mspi, events);
568 fsl_spi_cpu_irq(mspi, events);
573 static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on)
575 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
576 struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
578 u16 cs = spi->chip_select;
580 if (gpio_is_valid(spi->cs_gpio)) {
581 gpio_set_value(spi->cs_gpio, on);
582 } else if (cs < mpc8xxx_spi->native_chipselects) {
583 slvsel = mpc8xxx_spi_read_reg(®_base->slvsel);
584 slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs));
585 mpc8xxx_spi_write_reg(®_base->slvsel, slvsel);
589 static void fsl_spi_grlib_probe(struct device *dev)
591 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
592 struct spi_master *master = dev_get_drvdata(dev);
593 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
594 struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
598 capabilities = mpc8xxx_spi_read_reg(®_base->cap);
600 mpc8xxx_spi->set_shifts = fsl_spi_grlib_set_shifts;
601 mbits = SPCAP_MAXWLEN(capabilities);
603 mpc8xxx_spi->max_bits_per_word = mbits + 1;
605 mpc8xxx_spi->native_chipselects = 0;
606 if (SPCAP_SSEN(capabilities)) {
607 mpc8xxx_spi->native_chipselects = SPCAP_SSSZ(capabilities);
608 mpc8xxx_spi_write_reg(®_base->slvsel, 0xffffffff);
610 master->num_chipselect = mpc8xxx_spi->native_chipselects;
611 pdata->cs_control = fsl_spi_grlib_cs_control;
614 static struct spi_master * fsl_spi_probe(struct device *dev,
615 struct resource *mem, unsigned int irq)
617 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
618 struct spi_master *master;
619 struct mpc8xxx_spi *mpc8xxx_spi;
620 struct fsl_spi_reg *reg_base;
624 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
625 if (master == NULL) {
630 dev_set_drvdata(dev, master);
632 mpc8xxx_spi_probe(dev, mem, irq);
634 master->setup = fsl_spi_setup;
635 master->cleanup = fsl_spi_cleanup;
636 master->transfer_one_message = fsl_spi_do_one_msg;
638 mpc8xxx_spi = spi_master_get_devdata(master);
639 mpc8xxx_spi->max_bits_per_word = 32;
640 mpc8xxx_spi->type = fsl_spi_get_type(dev);
642 ret = fsl_spi_cpm_init(mpc8xxx_spi);
646 mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
647 if (IS_ERR(mpc8xxx_spi->reg_base)) {
648 ret = PTR_ERR(mpc8xxx_spi->reg_base);
652 if (mpc8xxx_spi->type == TYPE_GRLIB)
653 fsl_spi_grlib_probe(dev);
655 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
656 master->bits_per_word_mask =
657 (SPI_BPW_RANGE_MASK(4, 8) | SPI_BPW_MASK(16) | SPI_BPW_MASK(32));
659 master->bits_per_word_mask =
660 (SPI_BPW_RANGE_MASK(4, 16) | SPI_BPW_MASK(32));
662 master->bits_per_word_mask &=
663 SPI_BPW_RANGE_MASK(1, mpc8xxx_spi->max_bits_per_word);
665 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
666 mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
668 if (mpc8xxx_spi->set_shifts)
669 /* 8 bits per word and MSB first */
670 mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
671 &mpc8xxx_spi->tx_shift, 8, 1);
673 /* Register for SPI Interrupt */
674 ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_spi_irq,
675 0, "fsl_spi", mpc8xxx_spi);
680 reg_base = mpc8xxx_spi->reg_base;
682 /* SPI controller initializations */
683 mpc8xxx_spi_write_reg(®_base->mode, 0);
684 mpc8xxx_spi_write_reg(®_base->mask, 0);
685 mpc8xxx_spi_write_reg(®_base->command, 0);
686 mpc8xxx_spi_write_reg(®_base->event, 0xffffffff);
688 /* Enable SPI interface */
689 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
690 if (mpc8xxx_spi->max_bits_per_word < 8) {
691 regval &= ~SPMODE_LEN(0xF);
692 regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1);
694 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
697 mpc8xxx_spi_write_reg(®_base->mode, regval);
699 ret = devm_spi_register_master(dev, master);
703 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
704 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
709 fsl_spi_cpm_free(mpc8xxx_spi);
711 spi_master_put(master);
716 static void fsl_spi_cs_control(struct spi_device *spi, bool on)
718 struct device *dev = spi->dev.parent->parent;
719 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
720 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
721 u16 cs = spi->chip_select;
722 int gpio = pinfo->gpios[cs];
723 bool alow = pinfo->alow_flags[cs];
725 gpio_set_value(gpio, on ^ alow);
728 static int of_fsl_spi_get_chipselects(struct device *dev)
730 struct device_node *np = dev->of_node;
731 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
732 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
737 ngpios = of_gpio_count(np);
740 * SPI w/o chip-select line. One SPI device is still permitted
743 pdata->max_chipselect = 1;
747 pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios),
751 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
753 pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags),
755 if (!pinfo->alow_flags) {
757 goto err_alloc_flags;
760 for (; i < ngpios; i++) {
762 enum of_gpio_flags flags;
764 gpio = of_get_gpio_flags(np, i, &flags);
765 if (!gpio_is_valid(gpio)) {
766 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
771 ret = gpio_request(gpio, dev_name(dev));
773 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
777 pinfo->gpios[i] = gpio;
778 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
780 ret = gpio_direction_output(pinfo->gpios[i],
781 pinfo->alow_flags[i]);
784 "can't set output direction for gpio #%d: %d\n",
790 pdata->max_chipselect = ngpios;
791 pdata->cs_control = fsl_spi_cs_control;
797 if (gpio_is_valid(pinfo->gpios[i]))
798 gpio_free(pinfo->gpios[i]);
802 kfree(pinfo->alow_flags);
803 pinfo->alow_flags = NULL;
810 static int of_fsl_spi_free_chipselects(struct device *dev)
812 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
813 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
819 for (i = 0; i < pdata->max_chipselect; i++) {
820 if (gpio_is_valid(pinfo->gpios[i]))
821 gpio_free(pinfo->gpios[i]);
825 kfree(pinfo->alow_flags);
829 static int of_fsl_spi_probe(struct platform_device *ofdev)
831 struct device *dev = &ofdev->dev;
832 struct device_node *np = ofdev->dev.of_node;
833 struct spi_master *master;
838 ret = of_mpc8xxx_spi_probe(ofdev);
842 type = fsl_spi_get_type(&ofdev->dev);
843 if (type == TYPE_FSL) {
844 ret = of_fsl_spi_get_chipselects(dev);
849 ret = of_address_to_resource(np, 0, &mem);
853 irq = platform_get_irq(ofdev, 0);
859 master = fsl_spi_probe(dev, &mem, irq);
860 if (IS_ERR(master)) {
861 ret = PTR_ERR(master);
868 if (type == TYPE_FSL)
869 of_fsl_spi_free_chipselects(dev);
873 static int of_fsl_spi_remove(struct platform_device *ofdev)
875 struct spi_master *master = platform_get_drvdata(ofdev);
876 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
878 fsl_spi_cpm_free(mpc8xxx_spi);
879 if (mpc8xxx_spi->type == TYPE_FSL)
880 of_fsl_spi_free_chipselects(&ofdev->dev);
884 static struct platform_driver of_fsl_spi_driver = {
887 .of_match_table = of_fsl_spi_match,
889 .probe = of_fsl_spi_probe,
890 .remove = of_fsl_spi_remove,
893 #ifdef CONFIG_MPC832x_RDB
896 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
897 * only. The driver should go away soon, since newer MPC8323E-RDB's device
898 * tree can work with OpenFirmware driver. But for now we support old trees
901 static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
903 struct resource *mem;
905 struct spi_master *master;
907 if (!dev_get_platdata(&pdev->dev))
910 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
914 irq = platform_get_irq(pdev, 0);
918 master = fsl_spi_probe(&pdev->dev, mem, irq);
919 return PTR_ERR_OR_ZERO(master);
922 static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
924 struct spi_master *master = platform_get_drvdata(pdev);
925 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
927 fsl_spi_cpm_free(mpc8xxx_spi);
932 MODULE_ALIAS("platform:mpc8xxx_spi");
933 static struct platform_driver mpc8xxx_spi_driver = {
934 .probe = plat_mpc8xxx_spi_probe,
935 .remove = plat_mpc8xxx_spi_remove,
937 .name = "mpc8xxx_spi",
941 static bool legacy_driver_failed;
943 static void __init legacy_driver_register(void)
945 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
948 static void __exit legacy_driver_unregister(void)
950 if (legacy_driver_failed)
952 platform_driver_unregister(&mpc8xxx_spi_driver);
955 static void __init legacy_driver_register(void) {}
956 static void __exit legacy_driver_unregister(void) {}
957 #endif /* CONFIG_MPC832x_RDB */
959 static int __init fsl_spi_init(void)
961 legacy_driver_register();
962 return platform_driver_register(&of_fsl_spi_driver);
964 module_init(fsl_spi_init);
966 static void __exit fsl_spi_exit(void)
968 platform_driver_unregister(&of_fsl_spi_driver);
969 legacy_driver_unregister();
971 module_exit(fsl_spi_exit);
973 MODULE_AUTHOR("Kumar Gala");
974 MODULE_DESCRIPTION("Simple Freescale SPI Driver");
975 MODULE_LICENSE("GPL");