2 * Copyright (C) 2007 Openmoko, Inc.
3 * Author: Harald Welte <laforge@openmoko.org>
5 * Smedia Glamo GPIO based SPI driver
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.
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.
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>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/spi_bitbang.h>
29 #include <linux/spi/glamo.h>
31 #include <linux/glamofb.h>
33 #include <mach/hardware.h>
35 #include "glamo-core.h"
36 #include "glamo-regs.h"
39 struct spi_bitbang bitbang;
40 struct spi_master *master;
41 struct glamo_spi_info *info;
45 static inline struct glamo_spi *to_gs(struct spi_device *spi)
47 return spi->controller_data;
50 static int glamo_spi_setupxfer(struct spi_device *spi, struct spi_transfer *t)
54 bpw = t ? t->bits_per_word : spi->bits_per_word;
56 if (bpw != 9 && bpw != 8) {
57 dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
64 static void glamo_spi_chipsel(struct spi_device *spi, int value)
67 struct glamo_spi *gs = to_gs(spi);
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);
72 glamofb_cmd_mode(gs->info->glamofb_handle, value);
76 static int glamo_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
78 struct glamo_spi *gs = to_gs(spi);
79 const u_int16_t *ui16 = (const u_int16_t *) t->tx_buf;
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);
86 if (spi->bits_per_word == 9)
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);
97 for (i = 0; i < t->len/sizeof(u_int16_t); i++) {
98 /* actually transfer the data */
100 glamofb_cmd_write(gs->info->glamofb_handle,
101 GLAMO_LCD_CMD_TYPE_SERIAL | nine_bits |
102 (1 << 10) | (1 << 11) | (ui16[i] & 0x1ff));
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);
114 static int glamo_spi_setup(struct spi_device *spi)
118 if (!spi->bits_per_word)
119 spi->bits_per_word = 9;
121 /* FIXME: hardware can do this */
122 if (spi->mode & SPI_LSB_FIRST)
125 ret = glamo_spi_setupxfer(spi, NULL);
127 dev_err(&spi->dev, "setupxfer returned %d\n", ret);
131 dev_dbg(&spi->dev, "%s: mode %d, %u bpw\n",
132 __FUNCTION__, spi->mode, spi->bits_per_word);
137 static int glamo_spi_probe(struct platform_device *pdev)
139 struct spi_master *master;
140 struct glamo_spi *sp;
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");
151 sp = spi_master_get_devdata(master);
152 memset(sp, 0, sizeof(struct glamo_spi));
154 sp->master = spi_master_get(master);
155 sp->info = pdev->dev.platform_data;
157 dev_err(&pdev->dev, "can't operate without platform data\n");
161 dev_dbg(&pdev->dev, "sp->info(pdata) = %p\n", sp->info);
163 sp->dev = &pdev->dev;
165 platform_set_drvdata(pdev, sp);
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;
173 ret = spi_bitbang_start(&sp->bitbang);
177 /* register the chips to go with the board */
179 glamofb_cmd_mode(sp->info->glamofb_handle, 1);
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);
186 sp->info->board_info[i].controller_data = sp;
187 spi_new_device(master, sp->info->board_info + i);
193 platform_set_drvdata(pdev, NULL);
195 spi_master_put(sp->bitbang.master);
201 static int glamo_spi_remove(struct platform_device *pdev)
203 struct glamo_spi *sp = platform_get_drvdata(pdev);
205 spi_bitbang_stop(&sp->bitbang);
206 spi_master_put(sp->bitbang.master);
211 #define glamo_spi_suspend NULL
212 #define glamo_spi_resume NULL
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,
220 .name = "glamo-lcm-spi",
221 .owner = THIS_MODULE,
225 static int __init glamo_spi_init(void)
227 return platform_driver_register(&glamo_spi_drv);
230 static void __exit glamo_spi_exit(void)
232 platform_driver_unregister(&glamo_spi_drv);
235 module_init(glamo_spi_init);
236 module_exit(glamo_spi_exit);
238 MODULE_DESCRIPTION("Smedia Glamo 336x/337x LCM serial command SPI Driver");
239 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>")
240 MODULE_LICENSE("GPL");