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"
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
;
45 static inline struct glamo_spigpio
*to_sg(struct spi_device
*spi
)
47 return dev_get_drvdata(&spi
->master
->dev
);
50 static inline void setsck(struct spi_device
*dev
, int on
)
52 struct glamo_spigpio
*sg
= to_sg(dev
);
53 glamo_gpio_setpin(sg
->glamo
, sg
->info
->pin_clk
, on
? 1 : 0);
56 static inline void setmosi(struct spi_device
*dev
, int on
)
58 struct glamo_spigpio
*sg
= to_sg(dev
);
59 glamo_gpio_setpin(sg
->glamo
, sg
->info
->pin_mosi
, on
? 1 : 0);
62 static inline u32
getmiso(struct spi_device
*dev
)
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;
71 #define spidelay(x) ndelay(x)
73 #define EXPAND_BITBANG_TXRX
74 #include <linux/spi/spi_bitbang.h>
76 static u32
glamo_spigpio_txrx_mode0(struct spi_device
*spi
,
77 unsigned nsecs
, u32 word
, u8 bits
)
79 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
82 static u32
glamo_spigpio_txrx_mode1(struct spi_device
*spi
,
83 unsigned nsecs
, u32 word
, u8 bits
)
85 return bitbang_txrx_be_cpha1(spi
, nsecs
, 0, word
, bits
);
88 static u32
glamo_spigpio_txrx_mode2(struct spi_device
*spi
,
89 unsigned nsecs
, u32 word
, u8 bits
)
91 return bitbang_txrx_be_cpha0(spi
, nsecs
, 1, word
, bits
);
94 static u32
glamo_spigpio_txrx_mode3(struct spi_device
*spi
,
95 unsigned nsecs
, u32 word
, u8 bits
)
97 return bitbang_txrx_be_cpha1(spi
, nsecs
, 1, word
, bits
);
102 static int glamo_spigpio_setupxfer(struct spi_device
*spi
,
103 struct spi_transfer
*t
)
105 struct glamo_spi
*gs
= to_sg(spi
);
108 bpw
= t
? t
->bits_per_word
: spi
->bits_per_word
;
110 if (bpw
!= 9 && bpw
!= 8) {
111 dev_err(&spi
->dev
, "invalid bits-per-word (%d)\n", bpw
);
119 static void glamo_spigpio_chipsel(struct spi_device
*spi
, int value
)
121 struct glamo_spigpio
*gs
= to_sg(spi
);
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
);
126 glamo_gpio_setpin(gs
->glamo
, gs
->info
->pin_cs
, value
? 0 : 1);
130 static int glamo_spigpio_probe(struct platform_device
*pdev
)
132 struct spi_master
*master
;
133 struct glamo_spigpio
*sp
;
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");
143 sp
= spi_master_get_devdata(master
);
144 platform_set_drvdata(pdev
, sp
);
145 sp
->info
= pdev
->dev
.platform_data
;
147 dev_err(&pdev
->dev
, "can't operate without platform data\n");
152 master
->num_chipselect
= 1;
153 master
->bus_num
= 2; /* FIXME: use dynamic number */
155 sp
->master
= spi_master_get(master
);
156 sp
->glamo
= sp
->info
->glamo
;
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
;
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);
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
);
176 /* bring the LCM panel out of reset if it isn't already */
178 glamo_gpio_setpin(sp
->glamo
, GLAMO_GPIO4
, 1);
179 glamo_gpio_cfgpin(sp
->glamo
, GLAMO_GPIO4_OUTPUT
);
183 sp
->dev
= &pdev
->dev
;
185 sp
->bitbang
.setup_transfer
= glamo_spi_setupxfer
;
186 sp
->bitbang
.txrx_bufs
= glamo_spi_txrx
;
187 sp
->bitbang
.master
->setup
= glamo_spi_setup
;
190 dev_set_drvdata(&sp
->master
->dev
, sp
);
192 ret
= spi_bitbang_start(&sp
->bitbang
);
199 platform_set_drvdata(pdev
, NULL
);
201 spi_master_put(sp
->bitbang
.master
);
207 static int glamo_spigpio_remove(struct platform_device
*pdev
)
209 struct glamo_spigpio
*sp
= platform_get_drvdata(pdev
);
211 spi_bitbang_stop(&sp
->bitbang
);
212 spi_master_put(sp
->bitbang
.master
);
217 /*#define glamo_spigpio_suspend NULL
218 #define glamo_spigpio_resume NULL
223 static int glamo_spigpio_suspend(struct platform_device
*pdev
, pm_message_t state
)
228 static int glamo_spigpio_resume(struct platform_device
*pdev
)
230 struct glamo_spigpio
*sp
= platform_get_drvdata(pdev
);
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);
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
);
250 static struct platform_driver glamo_spi_drv
= {
251 .probe
= glamo_spigpio_probe
,
252 .remove
= glamo_spigpio_remove
,
254 .suspend_late
= glamo_spigpio_suspend
,
255 .resume_early
= glamo_spigpio_resume
,
258 .name
= "glamo-spi-gpio",
259 .owner
= THIS_MODULE
,
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");