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