Date:2010-04-24 12:23:01 (13 years 7 months ago)
Author:Lars C.
Commit:1ee508478f093692d56f588fbff6bdbc7c856ae9
Message:gpio spi 3wire

Files: drivers/spi/spi_bitbang.c (1 diff)
drivers/spi/spi_gpio.c (9 diffs)
include/linux/spi/spi_bitbang.h (1 diff)

Change Details

drivers/spi/spi_bitbang.c
335335                 */
336336                if (!m->is_dma_mapped)
337337                    t->rx_dma = t->tx_dma = 0;
338                if ((spi->mode & SPI_3WIRE) && bitbang->set_direction)
339                    bitbang->set_direction(spi, t->tx_buf != NULL);
338340                status = bitbang->txrx_bufs(spi, t);
339341            }
340342            if (status > 0)
drivers/spi/spi_gpio.c
4545    struct spi_bitbang bitbang;
4646    struct spi_gpio_platform_data pdata;
4747    struct platform_device *pdev;
48
49    int miso_pin;
4850};
4951
5052/*----------------------------------------------------------------------*/
...... 
8890
8991/*----------------------------------------------------------------------*/
9092
91static inline const struct spi_gpio_platform_data * __pure
92spi_to_pdata(const struct spi_device *spi)
93static inline const struct spi_gpio * __pure
94spi_to_spi_gpio(const struct spi_device *spi)
9395{
9496    const struct spi_bitbang *bang;
95    const struct spi_gpio *spi_gpio;
9697
9798    bang = spi_master_get_devdata(spi->master);
98    spi_gpio = container_of(bang, struct spi_gpio, bitbang);
99    return &spi_gpio->pdata;
99    return container_of(bang, struct spi_gpio, bitbang);
100100}
101101
102/* this is #defined to avoid unused-variable warnings when inlining */
103#define pdata spi_to_pdata(spi)
102#define pdata &(spi_to_spi_gpio(spi)->pdata)
104103
105104static inline void setsck(const struct spi_device *spi, int is_on)
106105{
...... 
114113
115114static inline int getmiso(const struct spi_device *spi)
116115{
117    return !!gpio_get_value(SPI_MISO_GPIO);
116    return !!gpio_get_value(spi_to_spi_gpio(spi)->miso_pin);
118117}
119118
120#undef pdata
121119
122120/*
123121 * NOTE: this clocks "as fast as we can". It "should" be a function of the
...... 
173171static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
174172{
175173    unsigned long cs = (unsigned long) spi->controller_data;
174    struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
176175
177176    /* set initial clock polarity */
178    if (is_active)
177    if (is_active) {
179178        setsck(spi, spi->mode & SPI_CPOL);
179        if (spi->mode & SPI_3WIRE)
180            spi_gpio->miso_pin = SPI_MOSI_GPIO;
181        else
182            spi_gpio->miso_pin = SPI_MISO_GPIO;
183    }
180184
181185    if (cs != SPI_GPIO_NO_CHIPSELECT) {
182186        /* SPI is normally active-low */
...... 
192196    if (spi->bits_per_word > 32)
193197        return -EINVAL;
194198
199    if (!(spi->mode & SPI_3WIRE) && !gpio_is_valid(SPI_MISO_GPIO))
200        return -EINVAL;
201
195202    if (!spi->controller_state) {
196203        if (cs != SPI_GPIO_NO_CHIPSELECT) {
197204            status = gpio_request(cs, dev_name(&spi->dev));
...... 
209216    return status;
210217}
211218
219static void spi_gpio_set_direction(struct spi_device *spi, bool is_tx)
220{
221    if (is_tx)
222        gpio_direction_output(SPI_MISO_GPIO, 0);
223    else
224        gpio_direction_input(SPI_MISO_GPIO);
225}
226
227#undef pdata
228
212229static void spi_gpio_cleanup(struct spi_device *spi)
213230{
214231    unsigned long cs = (unsigned long) spi->controller_data;
...... 
243260    if (value)
244261        goto done;
245262
246    value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
263    value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
247264    if (value)
248265        goto free_mosi;
249266
250    value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
251    if (value)
252        goto free_miso;
267    if (gpio_is_valid(SPI_MISO_GPIO)) {
268        value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
269        if (value)
270            goto free_sck;
271    }
253272
254273    goto done;
255274
256free_miso:
257    gpio_free(SPI_MISO_GPIO);
275free_sck:
276    gpio_free(SPI_SCK_GPIO);
258277free_mosi:
259278    gpio_free(SPI_MOSI_GPIO);
260279done:
...... 
302321    spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
303322    spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
304323    spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
305    spi_gpio->bitbang.flags = SPI_CS_HIGH;
324    spi_gpio->bitbang.set_direction = spi_gpio_set_direction;
325    spi_gpio->bitbang.flags = SPI_CS_HIGH | SPI_3WIRE;
306326
307327    status = spi_bitbang_start(&spi_gpio->bitbang);
308328    if (status < 0) {
309329        spi_master_put(spi_gpio->bitbang.master);
310330gpio_free:
311        gpio_free(SPI_MISO_GPIO);
331        if (gpio_is_valid(SPI_MOSI_GPIO))
332            gpio_free(SPI_MISO_GPIO);
312333        gpio_free(SPI_MOSI_GPIO);
313334        gpio_free(SPI_SCK_GPIO);
314335        spi_master_put(master);
...... 
332353
333354    platform_set_drvdata(pdev, NULL);
334355
335    gpio_free(SPI_MISO_GPIO);
356    if (gpio_is_valid(SPI_MISO_GPIO))
357        gpio_free(SPI_MISO_GPIO);
336358    gpio_free(SPI_MOSI_GPIO);
337359    gpio_free(SPI_SCK_GPIO);
338360
include/linux/spi/spi_bitbang.h
5252    u32 (*txrx_word[4])(struct spi_device *spi,
5353            unsigned nsecs,
5454            u32 word, u8 bits);
55
56    void (*set_direction)(struct spi_device *, bool is_tx);
5557};
5658
5759/* you can call these default bitbang->master methods from your custom

Archive Download the corresponding diff file



interactive