Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 1 | /* linux/drivers/spi/spi_s3c24xx_gpio.c |
| 2 | * |
| 3 | * Copyright (c) 2006 Ben Dooks |
| 4 | * Copyright (c) 2006 Simtec Electronics |
| 5 | * |
| 6 | * S3C24XX GPIO based SPI driver |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | |
Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | |
| 20 | #include <linux/spi/spi.h> |
| 21 | #include <linux/spi/spi_bitbang.h> |
| 22 | |
| 23 | #include <asm/arch/regs-gpio.h> |
| 24 | #include <asm/arch/spi-gpio.h> |
| 25 | #include <asm/arch/hardware.h> |
| 26 | |
| 27 | struct s3c2410_spigpio { |
| 28 | struct spi_bitbang bitbang; |
| 29 | |
| 30 | struct s3c2410_spigpio_info *info; |
| 31 | struct platform_device *dev; |
| 32 | }; |
| 33 | |
| 34 | static inline struct s3c2410_spigpio *spidev_to_sg(struct spi_device *spi) |
| 35 | { |
| 36 | return spi->controller_data; |
| 37 | } |
| 38 | |
| 39 | static inline void setsck(struct spi_device *dev, int on) |
| 40 | { |
| 41 | struct s3c2410_spigpio *sg = spidev_to_sg(dev); |
| 42 | s3c2410_gpio_setpin(sg->info->pin_clk, on ? 1 : 0); |
| 43 | } |
| 44 | |
| 45 | static inline void setmosi(struct spi_device *dev, int on) |
| 46 | { |
| 47 | struct s3c2410_spigpio *sg = spidev_to_sg(dev); |
| 48 | s3c2410_gpio_setpin(sg->info->pin_mosi, on ? 1 : 0); |
| 49 | } |
| 50 | |
| 51 | static inline u32 getmiso(struct spi_device *dev) |
| 52 | { |
| 53 | struct s3c2410_spigpio *sg = spidev_to_sg(dev); |
| 54 | return s3c2410_gpio_getpin(sg->info->pin_miso) ? 1 : 0; |
| 55 | } |
| 56 | |
| 57 | #define spidelay(x) ndelay(x) |
| 58 | |
| 59 | #define EXPAND_BITBANG_TXRX |
| 60 | #include <linux/spi/spi_bitbang.h> |
| 61 | |
| 62 | |
| 63 | static u32 s3c2410_spigpio_txrx_mode0(struct spi_device *spi, |
| 64 | unsigned nsecs, u32 word, u8 bits) |
| 65 | { |
| 66 | return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits); |
| 67 | } |
| 68 | |
| 69 | static u32 s3c2410_spigpio_txrx_mode1(struct spi_device *spi, |
| 70 | unsigned nsecs, u32 word, u8 bits) |
| 71 | { |
| 72 | return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits); |
| 73 | } |
| 74 | |
| 75 | static void s3c2410_spigpio_chipselect(struct spi_device *dev, int value) |
| 76 | { |
| 77 | struct s3c2410_spigpio *sg = spidev_to_sg(dev); |
| 78 | |
| 79 | if (sg->info && sg->info->chip_select) |
| 80 | (sg->info->chip_select)(sg->info, value); |
| 81 | } |
| 82 | |
| 83 | static int s3c2410_spigpio_probe(struct platform_device *dev) |
| 84 | { |
| 85 | struct spi_master *master; |
| 86 | struct s3c2410_spigpio *sp; |
| 87 | int ret; |
| 88 | int i; |
| 89 | |
| 90 | master = spi_alloc_master(&dev->dev, sizeof(struct s3c2410_spigpio)); |
| 91 | if (master == NULL) { |
| 92 | dev_err(&dev->dev, "failed to allocate spi master\n"); |
| 93 | ret = -ENOMEM; |
| 94 | goto err; |
| 95 | } |
| 96 | |
| 97 | sp = spi_master_get_devdata(master); |
| 98 | |
| 99 | platform_set_drvdata(dev, sp); |
| 100 | |
| 101 | /* copy in the plkatform data */ |
| 102 | sp->info = dev->dev.platform_data; |
| 103 | |
| 104 | /* setup spi bitbang adaptor */ |
| 105 | sp->bitbang.master = spi_master_get(master); |
| 106 | sp->bitbang.chipselect = s3c2410_spigpio_chipselect; |
| 107 | |
| 108 | sp->bitbang.txrx_word[SPI_MODE_0] = s3c2410_spigpio_txrx_mode0; |
| 109 | sp->bitbang.txrx_word[SPI_MODE_1] = s3c2410_spigpio_txrx_mode1; |
| 110 | |
| 111 | /* set state of spi pins */ |
| 112 | s3c2410_gpio_setpin(sp->info->pin_clk, 0); |
| 113 | s3c2410_gpio_setpin(sp->info->pin_mosi, 0); |
| 114 | |
| 115 | s3c2410_gpio_cfgpin(sp->info->pin_clk, S3C2410_GPIO_OUTPUT); |
| 116 | s3c2410_gpio_cfgpin(sp->info->pin_mosi, S3C2410_GPIO_OUTPUT); |
| 117 | s3c2410_gpio_cfgpin(sp->info->pin_miso, S3C2410_GPIO_INPUT); |
| 118 | |
| 119 | ret = spi_bitbang_start(&sp->bitbang); |
| 120 | if (ret) |
| 121 | goto err_no_bitbang; |
| 122 | |
| 123 | /* register the chips to go with the board */ |
| 124 | |
| 125 | for (i = 0; i < sp->info->board_size; i++) { |
| 126 | dev_info(&dev->dev, "registering %p: %s\n", |
| 127 | &sp->info->board_info[i], |
| 128 | sp->info->board_info[i].modalias); |
| 129 | |
| 130 | sp->info->board_info[i].controller_data = sp; |
| 131 | spi_new_device(master, sp->info->board_info + i); |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | |
| 136 | err_no_bitbang: |
| 137 | spi_master_put(sp->bitbang.master); |
| 138 | err: |
| 139 | return ret; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | static int s3c2410_spigpio_remove(struct platform_device *dev) |
| 144 | { |
| 145 | struct s3c2410_spigpio *sp = platform_get_drvdata(dev); |
| 146 | |
| 147 | spi_bitbang_stop(&sp->bitbang); |
| 148 | spi_master_put(sp->bitbang.master); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* all gpio should be held over suspend/resume, so we should |
| 154 | * not need to deal with this |
| 155 | */ |
| 156 | |
| 157 | #define s3c2410_spigpio_suspend NULL |
| 158 | #define s3c2410_spigpio_resume NULL |
| 159 | |
| 160 | |
| 161 | static struct platform_driver s3c2410_spigpio_drv = { |
| 162 | .probe = s3c2410_spigpio_probe, |
| 163 | .remove = s3c2410_spigpio_remove, |
| 164 | .suspend = s3c2410_spigpio_suspend, |
| 165 | .resume = s3c2410_spigpio_resume, |
| 166 | .driver = { |
| 167 | .name = "s3c24xx-spi-gpio", |
| 168 | .owner = THIS_MODULE, |
| 169 | }, |
| 170 | }; |
| 171 | |
| 172 | static int __init s3c2410_spigpio_init(void) |
| 173 | { |
| 174 | return platform_driver_register(&s3c2410_spigpio_drv); |
| 175 | } |
| 176 | |
| 177 | static void __exit s3c2410_spigpio_exit(void) |
| 178 | { |
| 179 | platform_driver_unregister(&s3c2410_spigpio_drv); |
| 180 | } |
| 181 | |
| 182 | module_init(s3c2410_spigpio_init); |
| 183 | module_exit(s3c2410_spigpio_exit); |
| 184 | |
| 185 | MODULE_DESCRIPTION("S3C24XX SPI Driver"); |
| 186 | MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); |
| 187 | MODULE_LICENSE("GPL"); |