madwifi: fix a race condition in the ibss merge
[oweals/openwrt.git] / target / linux / s3c24xx / files-2.6.30 / drivers / mfd / glamo / glamo-lcm-spi.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 #if 0
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_spi {
39         struct spi_bitbang      bitbang;
40         struct spi_master       *master;
41         struct glamo_spi_info   *info;
42         struct device           *dev;
43 };
44
45 static inline struct glamo_spi *to_gs(struct spi_device *spi)
46 {
47         return spi->controller_data;
48 }
49
50 static int glamo_spi_setupxfer(struct spi_device *spi, struct spi_transfer *t)
51 {
52         unsigned int bpw;
53
54         bpw = t ? t->bits_per_word : spi->bits_per_word;
55
56         if (bpw != 9 && bpw != 8) {
57                 dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
58                 return -EINVAL;
59         }
60
61         return 0;
62 }
63
64 static void glamo_spi_chipsel(struct spi_device *spi, int value)
65 {
66 #if 0
67         struct glamo_spi *gs = to_gs(spi);
68
69         dev_dbg(&spi->dev, "chipsel %d: spi=%p, gs=%p, info=%p, handle=%p\n",
70                 value, spi, gs, gs->info, gs->info->glamofb_handle);
71
72         glamofb_cmd_mode(gs->info->glamofb_handle, value);
73 #endif
74 }
75
76 static int glamo_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
77 {
78         struct glamo_spi *gs = to_gs(spi);
79         const u_int16_t *ui16 = (const u_int16_t *) t->tx_buf;
80         u_int16_t nine_bits;
81         int i;
82
83         dev_dbg(&spi->dev, "txrx: tx %p, rx %p, bpw %d, len %d\n",
84                 t->tx_buf, t->rx_buf, t->bits_per_word, t->len);
85
86         if (spi->bits_per_word == 9)
87                 nine_bits = (1 << 9);
88         else
89                 nine_bits = 0;
90
91         if (t->len > 3 * sizeof(u_int16_t)) {
92                 dev_err(&spi->dev, "this driver doesn't support "
93                         "%u sized xfers\n", t->len);
94                 return -EINVAL;
95         }
96
97         for (i = 0; i < t->len/sizeof(u_int16_t); i++) {
98                 /* actually transfer the data */
99 #if 1
100                 glamofb_cmd_write(gs->info->glamofb_handle,
101                                   GLAMO_LCD_CMD_TYPE_SERIAL | nine_bits |
102                                   (1 << 10) | (1 << 11) | (ui16[i] & 0x1ff));
103 #endif
104                 /* FIXME: fire ?!? */
105                 if (i == 0 && (ui16[i] & 0x1ff) == 0x29) {
106                         dev_dbg(&spi->dev, "leaving command mode\n");
107                         glamofb_cmd_mode(gs->info->glamofb_handle, 0);
108                 }
109         }
110
111         return t->len;
112 }
113
114 static int glamo_spi_setup(struct spi_device *spi)
115 {
116         int ret;
117
118         if (!spi->bits_per_word)
119                 spi->bits_per_word = 9;
120
121         /* FIXME: hardware can do this */
122         if (spi->mode & SPI_LSB_FIRST)
123                 return -EINVAL;
124
125         ret = glamo_spi_setupxfer(spi, NULL);
126         if (ret < 0) {
127                 dev_err(&spi->dev, "setupxfer returned %d\n", ret);
128                 return ret;
129         }
130
131         dev_dbg(&spi->dev, "%s: mode %d, %u bpw\n",
132                 __FUNCTION__, spi->mode, spi->bits_per_word);
133
134         return 0;
135 }
136
137 static int glamo_spi_probe(struct platform_device *pdev)
138 {
139         struct spi_master *master;
140         struct glamo_spi *sp;
141         int ret;
142         int i;
143
144         master = spi_alloc_master(&pdev->dev, sizeof(struct glamo_spi));
145         if (master == NULL) {
146                 dev_err(&pdev->dev, "failed to allocate spi master\n");
147                 ret = -ENOMEM;
148                 goto err;
149         }
150
151         sp = spi_master_get_devdata(master);
152         memset(sp, 0, sizeof(struct glamo_spi));
153
154         sp->master = spi_master_get(master);
155         sp->info = pdev->dev.platform_data;
156         if (!sp->info) {
157                 dev_err(&pdev->dev, "can't operate without platform data\n");
158                 ret = -EIO;
159                 goto err_no_pdev;
160         }
161         dev_dbg(&pdev->dev, "sp->info(pdata) = %p\n", sp->info);
162
163         sp->dev = &pdev->dev;
164
165         platform_set_drvdata(pdev, sp);
166
167         sp->bitbang.master = sp->master;
168         sp->bitbang.setup_transfer = glamo_spi_setupxfer;
169         sp->bitbang.chipselect = glamo_spi_chipsel;
170         sp->bitbang.txrx_bufs = glamo_spi_txrx;
171         sp->bitbang.master->setup = glamo_spi_setup;
172
173         ret = spi_bitbang_start(&sp->bitbang);
174         if (ret)
175                 goto err_no_bitbang;
176
177         /* register the chips to go with the board */
178
179         glamofb_cmd_mode(sp->info->glamofb_handle, 1);
180
181         for (i = 0; i < sp->info->board_size; i++) {
182                 dev_info(&pdev->dev, "registering %p: %s\n",
183                          &sp->info->board_info[i],
184                          sp->info->board_info[i].modalias);
185
186                 sp->info->board_info[i].controller_data = sp;
187                 spi_new_device(master, sp->info->board_info + i);
188         }
189
190         return 0;
191
192 err_no_bitbang:
193         platform_set_drvdata(pdev, NULL);
194 err_no_pdev:
195         spi_master_put(sp->bitbang.master);
196 err:
197         return ret;
198
199 }
200
201 static int glamo_spi_remove(struct platform_device *pdev)
202 {
203         struct glamo_spi *sp = platform_get_drvdata(pdev);
204
205         spi_bitbang_stop(&sp->bitbang);
206         spi_master_put(sp->bitbang.master);
207
208         return 0;
209 }
210
211 #define glamo_spi_suspend NULL
212 #define glamo_spi_resume NULL
213
214 static struct platform_driver glamo_spi_drv = {
215         .probe          = glamo_spi_probe,
216         .remove         = glamo_spi_remove,
217         .suspend        = glamo_spi_suspend,
218         .resume         = glamo_spi_resume,
219         .driver         = {
220                 .name   = "glamo-lcm-spi",
221                 .owner  = THIS_MODULE,
222         },
223 };
224
225 static int __init glamo_spi_init(void)
226 {
227         return platform_driver_register(&glamo_spi_drv);
228 }
229
230 static void __exit glamo_spi_exit(void)
231 {
232         platform_driver_unregister(&glamo_spi_drv);
233 }
234
235 module_init(glamo_spi_init);
236 module_exit(glamo_spi_exit);
237
238 MODULE_DESCRIPTION("Smedia Glamo 336x/337x LCM serial command SPI Driver");
239 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>")
240 MODULE_LICENSE("GPL");
241 #endif