[s3c24xx] Cleanup glamo platform_data.
[openwrt.git] / target / linux / s3c24xx / files-2.6.30 / drivers / mfd / glamo / glamo-spi-gpio.c
1 /*
2  * Copyright (C) 2007 Openmoko, Inc.
3  * Author: Harald Welte <laforge@openmoko.org>
4  *
5  * Smedia Glamo GPIO based SPI driver
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This driver currently only implements a minimum subset of the hardware
12  * features, esp. those features that are required to drive the jbt6k74
13  * LCM controller asic in the TD028TTEC1 LCM.
14  *
15 */
16
17 #define DEBUG
18
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/spinlock.h>
24 #include <linux/platform_device.h>
25
26 #include <linux/spi/spi.h>
27 #include <linux/spi/spi_bitbang.h>
28 #include <linux/spi/glamo.h>
29
30 #include <linux/glamofb.h>
31
32 #include <mach/hardware.h>
33
34 #include "glamo-core.h"
35 #include "glamo-regs.h"
36
37 struct glamo_spigpio {
38         struct spi_bitbang              bitbang;
39         struct spi_master               *master;
40         struct glamo_spigpio_platform_data      *info;
41 };
42
43 static inline struct glamo_spigpio *to_sg(struct spi_device *spi)
44 {
45         return dev_get_drvdata(&spi->master->dev);
46 }
47
48 static inline void setsck(struct spi_device *dev, int on)
49 {
50         struct glamo_spigpio *sg = to_sg(dev);
51         glamo_gpio_setpin(sg->info->core, sg->info->pin_clk, on ? 1 : 0);
52 }
53
54 static inline void setmosi(struct spi_device *dev, int on)
55 {
56         struct glamo_spigpio *sg = to_sg(dev);
57         glamo_gpio_setpin(sg->info->core, sg->info->pin_mosi, on ? 1 : 0);
58 }
59
60 static inline u32 getmiso(struct spi_device *dev)
61 {
62         struct glamo_spigpio *sg = to_sg(dev);
63         if (sg->info->pin_miso)
64                 return glamo_gpio_getpin(sg->info->core, sg->info->pin_miso) ? 1 : 0;
65         else
66                 return 0;
67 }
68
69 #define spidelay(x) ndelay(x)
70
71 #define EXPAND_BITBANG_TXRX
72 #include <linux/spi/spi_bitbang.h>
73
74 static u32 glamo_spigpio_txrx_mode0(struct spi_device *spi,
75                                     unsigned nsecs, u32 word, u8 bits)
76 {
77         return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
78 }
79
80 static u32 glamo_spigpio_txrx_mode1(struct spi_device *spi,
81                                     unsigned nsecs, u32 word, u8 bits)
82 {
83         return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
84 }
85
86 static u32 glamo_spigpio_txrx_mode2(struct spi_device *spi,
87                                     unsigned nsecs, u32 word, u8 bits)
88 {
89         return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
90 }
91
92 static u32 glamo_spigpio_txrx_mode3(struct spi_device *spi,
93                                     unsigned nsecs, u32 word, u8 bits)
94 {
95         return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
96 }
97
98
99 #if 0
100 static int glamo_spigpio_setupxfer(struct spi_device *spi,
101                                    struct spi_transfer *t)
102 {
103         struct glamo_spi *gs = to_sg(spi);
104         unsigned int bpw;
105
106         bpw = t ? t->bits_per_word : spi->bits_per_word;
107
108         if (bpw != 9 && bpw != 8) {
109                 dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
110                 return -EINVAL;
111         }
112
113         return 0;
114 }
115 #endif
116
117 static void glamo_spigpio_chipsel(struct spi_device *spi, int value)
118 {
119         struct glamo_spigpio *gs = to_sg(spi);
120 #if 0
121         dev_dbg(&spi->dev, "chipsel %d: spi=%p, gs=%p, info=%p, handle=%p\n",
122                 value, spi, gs, gs->info, gs->info->glamo);
123 #endif
124         glamo_gpio_setpin(gs->info->core, gs->info->pin_cs, value ? 0 : 1);
125 }
126
127
128 static int glamo_spigpio_probe(struct platform_device *pdev)
129 {
130         struct spi_master *master;
131         struct glamo_spigpio *sp;
132         int ret;
133
134         master = spi_alloc_master(&pdev->dev, sizeof(struct glamo_spigpio));
135         if (master == NULL) {
136                 dev_err(&pdev->dev, "failed to allocate spi master\n");
137                 ret = -ENOMEM;
138                 goto err;
139         }
140
141         sp = spi_master_get_devdata(master);
142         platform_set_drvdata(pdev, sp);
143         sp->info = pdev->dev.platform_data;
144         if (!sp->info) {
145                 dev_err(&pdev->dev, "can't operate without platform data\n");
146                 ret = -EIO;
147                 goto err_no_pdev;
148         }
149
150         master->num_chipselect = 1;
151         master->bus_num = 2; /* FIXME: use dynamic number */
152
153         sp->master = spi_master_get(master);
154
155         sp->bitbang.master = sp->master;
156         sp->bitbang.chipselect = glamo_spigpio_chipsel;
157         sp->bitbang.txrx_word[SPI_MODE_0] = glamo_spigpio_txrx_mode0;
158         sp->bitbang.txrx_word[SPI_MODE_1] = glamo_spigpio_txrx_mode1;
159         sp->bitbang.txrx_word[SPI_MODE_2] = glamo_spigpio_txrx_mode2;
160         sp->bitbang.txrx_word[SPI_MODE_3] = glamo_spigpio_txrx_mode3;
161
162         /* set state of spi pins */
163         glamo_gpio_setpin(sp->info->core, sp->info->pin_clk, 0);
164         glamo_gpio_setpin(sp->info->core, sp->info->pin_mosi, 0);
165         glamo_gpio_setpin(sp->info->core, sp->info->pin_cs, 1);
166
167         glamo_gpio_cfgpin(sp->info->core, sp->info->pin_clk);
168         glamo_gpio_cfgpin(sp->info->core, sp->info->pin_mosi);
169         glamo_gpio_cfgpin(sp->info->core, sp->info->pin_cs);
170         if (sp->info->pin_miso)
171                 glamo_gpio_cfgpin(sp->info->core, sp->info->pin_miso);
172
173         /* bring the LCM panel out of reset if it isn't already */
174
175         glamo_gpio_setpin(sp->info->core, GLAMO_GPIO4, 1);
176         glamo_gpio_cfgpin(sp->info->core, GLAMO_GPIO4_OUTPUT);
177         msleep(90);
178
179 #if 0
180         sp->dev = &pdev->dev;
181
182         sp->bitbang.setup_transfer = glamo_spi_setupxfer;
183         sp->bitbang.txrx_bufs = glamo_spi_txrx;
184         sp->bitbang.master->setup = glamo_spi_setup;
185 #endif
186
187         dev_set_drvdata(&sp->master->dev, sp);
188
189         ret = spi_bitbang_start(&sp->bitbang);
190         if (ret)
191                 goto err_no_bitbang;
192
193         return 0;
194
195 err_no_bitbang:
196         platform_set_drvdata(pdev, NULL);
197 err_no_pdev:
198         spi_master_put(sp->bitbang.master);
199 err:
200         return ret;
201
202 }
203
204 static int glamo_spigpio_remove(struct platform_device *pdev)
205 {
206         struct glamo_spigpio *sp = platform_get_drvdata(pdev);
207
208         spi_bitbang_stop(&sp->bitbang);
209         spi_master_put(sp->bitbang.master);
210
211         return 0;
212 }
213
214 /*#define glamo_spigpio_suspend NULL
215 #define glamo_spigpio_resume NULL
216 */
217
218
219 #ifdef CONFIG_PM
220 static int glamo_spigpio_suspend(struct platform_device *pdev, pm_message_t state)
221 {
222         return 0;
223 }
224
225 static int glamo_spigpio_resume(struct platform_device *pdev)
226 {
227         struct glamo_spigpio *sp = platform_get_drvdata(pdev);
228
229         if (!sp)
230                 return 0;
231
232         /* set state of spi pins */
233         glamo_gpio_setpin(sp->info->core, sp->info->pin_clk, 0);
234         glamo_gpio_setpin(sp->info->core, sp->info->pin_mosi, 0);
235         glamo_gpio_setpin(sp->info->core, sp->info->pin_cs, 1);
236
237         glamo_gpio_cfgpin(sp->info->core, sp->info->pin_clk);
238         glamo_gpio_cfgpin(sp->info->core, sp->info->pin_mosi);
239         glamo_gpio_cfgpin(sp->info->core, sp->info->pin_cs);
240         if (sp->info->pin_miso)
241                 glamo_gpio_cfgpin(sp->info->core, sp->info->pin_miso);
242
243         return 0;
244 }
245 #endif
246
247 static struct platform_driver glamo_spi_drv = {
248         .probe          = glamo_spigpio_probe,
249         .remove         = glamo_spigpio_remove,
250 #ifdef CONFIG_PM
251         .suspend_late   = glamo_spigpio_suspend,
252         .resume_early   = glamo_spigpio_resume,
253 #endif
254         .driver         = {
255                 .name   = "glamo-spi-gpio",
256                 .owner  = THIS_MODULE,
257         },
258 };
259
260 static int __init glamo_spi_init(void)
261 {
262         return platform_driver_register(&glamo_spi_drv);
263 }
264
265 static void __exit glamo_spi_exit(void)
266 {
267         platform_driver_unregister(&glamo_spi_drv);
268 }
269
270 module_init(glamo_spi_init);
271 module_exit(glamo_spi_exit);
272
273 MODULE_DESCRIPTION("Smedia Glamo 336x/337x LCM serial command SPI Driver");
274 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>")
275 MODULE_LICENSE("GPL");