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/platform_device.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/spi_bitbang.h>
28 #include <linux/spi/glamo.h>
30 #include <linux/glamofb.h>
32 #include <mach/hardware.h>
34 #include "glamo-core.h"
35 #include "glamo-regs.h"
37 struct glamo_spigpio
{
38 struct spi_bitbang bitbang
;
39 struct spi_master
*master
;
40 struct glamo_spigpio_platform_data
*info
;
43 static inline struct glamo_spigpio
*to_sg(struct spi_device
*spi
)
45 return dev_get_drvdata(&spi
->master
->dev
);
48 static inline void setsck(struct spi_device
*dev
, int on
)
50 struct glamo_spigpio
*sg
= to_sg(dev
);
51 glamo_gpio_setpin(sg
->info
->core
, sg
->info
->pin_clk
, on
? 1 : 0);
54 static inline void setmosi(struct spi_device
*dev
, int on
)
56 struct glamo_spigpio
*sg
= to_sg(dev
);
57 glamo_gpio_setpin(sg
->info
->core
, sg
->info
->pin_mosi
, on
? 1 : 0);
60 static inline u32
getmiso(struct spi_device
*dev
)
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;
69 #define spidelay(x) ndelay(x)
71 #define EXPAND_BITBANG_TXRX
72 #include <linux/spi/spi_bitbang.h>
74 static u32
glamo_spigpio_txrx_mode0(struct spi_device
*spi
,
75 unsigned nsecs
, u32 word
, u8 bits
)
77 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
80 static u32
glamo_spigpio_txrx_mode1(struct spi_device
*spi
,
81 unsigned nsecs
, u32 word
, u8 bits
)
83 return bitbang_txrx_be_cpha1(spi
, nsecs
, 0, word
, bits
);
86 static u32
glamo_spigpio_txrx_mode2(struct spi_device
*spi
,
87 unsigned nsecs
, u32 word
, u8 bits
)
89 return bitbang_txrx_be_cpha0(spi
, nsecs
, 1, word
, bits
);
92 static u32
glamo_spigpio_txrx_mode3(struct spi_device
*spi
,
93 unsigned nsecs
, u32 word
, u8 bits
)
95 return bitbang_txrx_be_cpha1(spi
, nsecs
, 1, word
, bits
);
100 static int glamo_spigpio_setupxfer(struct spi_device
*spi
,
101 struct spi_transfer
*t
)
103 struct glamo_spi
*gs
= to_sg(spi
);
106 bpw
= t
? t
->bits_per_word
: spi
->bits_per_word
;
108 if (bpw
!= 9 && bpw
!= 8) {
109 dev_err(&spi
->dev
, "invalid bits-per-word (%d)\n", bpw
);
117 static void glamo_spigpio_chipsel(struct spi_device
*spi
, int value
)
119 struct glamo_spigpio
*gs
= to_sg(spi
);
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
);
124 glamo_gpio_setpin(gs
->info
->core
, gs
->info
->pin_cs
, value
? 0 : 1);
128 static int glamo_spigpio_probe(struct platform_device
*pdev
)
130 struct spi_master
*master
;
131 struct glamo_spigpio
*sp
;
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");
141 sp
= spi_master_get_devdata(master
);
142 platform_set_drvdata(pdev
, sp
);
143 sp
->info
= pdev
->dev
.platform_data
;
145 dev_err(&pdev
->dev
, "can't operate without platform data\n");
150 master
->num_chipselect
= 1;
151 master
->bus_num
= 2; /* FIXME: use dynamic number */
153 sp
->master
= spi_master_get(master
);
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
;
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);
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
);
173 /* bring the LCM panel out of reset if it isn't already */
175 glamo_gpio_setpin(sp
->info
->core
, GLAMO_GPIO4
, 1);
176 glamo_gpio_cfgpin(sp
->info
->core
, GLAMO_GPIO4_OUTPUT
);
180 sp
->dev
= &pdev
->dev
;
182 sp
->bitbang
.setup_transfer
= glamo_spi_setupxfer
;
183 sp
->bitbang
.txrx_bufs
= glamo_spi_txrx
;
184 sp
->bitbang
.master
->setup
= glamo_spi_setup
;
187 dev_set_drvdata(&sp
->master
->dev
, sp
);
189 ret
= spi_bitbang_start(&sp
->bitbang
);
196 platform_set_drvdata(pdev
, NULL
);
198 spi_master_put(sp
->bitbang
.master
);
204 static int glamo_spigpio_remove(struct platform_device
*pdev
)
206 struct glamo_spigpio
*sp
= platform_get_drvdata(pdev
);
208 spi_bitbang_stop(&sp
->bitbang
);
209 spi_master_put(sp
->bitbang
.master
);
216 /*static int glamo_spigpio_suspend(struct device *dev)
221 static int glamo_spigpio_resume(struct device
*dev
)
223 struct glamo_spigpio
*sp
= dev_get_drvdata(dev
);
228 /* set state of spi pins */
229 glamo_gpio_setpin(sp
->info
->core
, sp
->info
->pin_clk
, 0);
230 glamo_gpio_setpin(sp
->info
->core
, sp
->info
->pin_mosi
, 0);
231 glamo_gpio_setpin(sp
->info
->core
, sp
->info
->pin_cs
, 1);
233 glamo_gpio_cfgpin(sp
->info
->core
, sp
->info
->pin_clk
);
234 glamo_gpio_cfgpin(sp
->info
->core
, sp
->info
->pin_mosi
);
235 glamo_gpio_cfgpin(sp
->info
->core
, sp
->info
->pin_cs
);
236 if (sp
->info
->pin_miso
)
237 glamo_gpio_cfgpin(sp
->info
->core
, sp
->info
->pin_miso
);
242 static struct dev_pm_ops glamo_spigpio_pm_ops
= {
243 /* .suspend = glamo_spiogpio_suspend,*/
244 .resume_noirq
= glamo_spigpio_resume
,
247 #define GLAMO_SPIGPIO_PM_OPS (&glamo_spigpio_pm_ops)
250 #define GLAMO_SPIGPIO_PM_OPS NULL
253 static struct platform_driver glamo_spi_drv
= {
254 .probe
= glamo_spigpio_probe
,
255 .remove
= glamo_spigpio_remove
,
257 .name
= "glamo-spi-gpio",
258 .owner
= THIS_MODULE
,
259 .pm
= GLAMO_SPIGPIO_PM_OPS
,
263 static int __init
glamo_spi_init(void)
265 return platform_driver_register(&glamo_spi_drv
);
268 static void __exit
glamo_spi_exit(void)
270 platform_driver_unregister(&glamo_spi_drv
);
273 module_init(glamo_spi_init
);
274 module_exit(glamo_spi_exit
);
276 MODULE_DESCRIPTION("Smedia Glamo 336x/337x LCM serial command SPI Driver");
277 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>")
278 MODULE_LICENSE("GPL");