4 * Copyright (C) 2005, 2006 Nokia Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/delay.h>
25 #include <linux/spi/spi.h>
26 #include <linux/spi/tsc2301.h>
28 #ifdef CONFIG_ARCH_OMAP
29 #include <asm/arch/gpio.h>
32 u16 tsc2301_read_reg(struct tsc2301 *tsc, int reg)
34 struct spi_transfer t[2];
41 memset(t, 0, sizeof(t));
48 spi_message_add_tail(&t[0], &m);
53 spi_message_add_tail(&t[1], &m);
60 void tsc2301_write_reg(struct tsc2301 *tsc, int reg, u16 val)
62 struct spi_transfer t;
66 /* Now we prepare the command for transferring */
73 memset(&t, 0, sizeof(t));
77 spi_message_add_tail(&t, &m);
82 void tsc2301_write_kbc(struct tsc2301 *tsc, int val)
86 w = tsc->config2_shadow;
88 w |= (val & 0x03) << 14;
89 tsc2301_write_reg(tsc, TSC2301_REG_CONFIG2, w);
90 tsc->config2_shadow = w;
93 void tsc2301_write_pll(struct tsc2301 *tsc,
94 int pll_n, int pll_a, int pll_pdc, int pct_e, int pll_o)
98 w = tsc->config2_shadow;
100 w |= (pll_n & 0x0f) | ((pll_a & 0x0f) << 4) | ((pll_pdc & 0x0f) << 8);
101 w |= pct_e ? (1 << 12) : 0;
102 w |= pll_o ? (1 << 13) : 0;
103 tsc2301_write_reg(tsc, TSC2301_REG_CONFIG2, w);
104 tsc->config2_shadow = w;
107 void tsc2301_read_buf(struct tsc2301 *tsc, int reg, u16 *rx_buf, int len)
109 struct spi_transfer t[2];
110 struct spi_message m;
116 spi_message_init(&m);
119 memset(t, 0, sizeof(t));
123 spi_message_add_tail(&t[0], &m);
126 t[1].rx_buf = rx_buf;
128 spi_message_add_tail(&t[1], &m);
132 for (i = 0; i < len; i++)
133 printk(KERN_DEBUG "rx_buf[%d]: %04x\n", i, rx_buf[i]);
136 static int __devinit tsc2301_probe(struct spi_device *spi)
139 struct tsc2301_platform_data *pdata = spi->dev.platform_data;
144 dev_dbg(&spi->dev, "no platform data?\n");
148 tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
152 dev_set_drvdata(&spi->dev, tsc);
154 spi->dev.power.power_state = PMSG_ON;
156 tsc->enable_clock = pdata->enable_clock;
157 tsc->disable_clock = pdata->disable_clock;
159 if (pdata->reset_gpio >= 0) {
160 tsc->reset_gpio = pdata->reset_gpio;
161 #ifdef CONFIG_ARCH_OMAP
162 r = omap_request_gpio(tsc->reset_gpio);
165 omap_set_gpio_dataout(tsc->reset_gpio, 1);
166 omap_set_gpio_direction(tsc->reset_gpio, 0);
168 omap_set_gpio_dataout(tsc->reset_gpio, 0);
171 tsc->reset_gpio = -1;
173 spi->mode = SPI_MODE_0;
174 spi->bits_per_word = 16;
175 /* The max speed might've been defined by the board-specific
177 if (!spi->max_speed_hz)
178 spi->max_speed_hz = TSC2301_HZ;
182 tsc2301_write_reg(tsc, TSC2301_REG_RESET, 0xbb00);
185 w = tsc2301_read_reg(tsc, TSC2301_REG_ADC);
186 if (!(w & (1 << 14))) {
187 dev_err(&spi->dev, "invalid ADC reg value: %04x\n", w);
192 w = tsc2301_read_reg(tsc, TSC2301_REG_DAC);
193 if (!(w & (1 << 15))) {
194 dev_err(&spi->dev, "invalid DAC reg value: %04x\n", w);
199 /* Stop keypad scanning */
200 tsc2301_write_reg(tsc, TSC2301_REG_KEY, 0x4000);
202 /* We have to cache this for read-modify-write, since we can't
204 w = tsc2301_read_reg(tsc, TSC2301_REG_CONFIG2);
205 /* By default BIT15 is set */
207 tsc->config2_shadow = w;
209 r = tsc2301_kp_init(tsc, pdata);
212 r = tsc2301_ts_init(tsc, pdata);
215 r = tsc2301_mixer_init(tsc, pdata);
221 tsc2301_ts_exit(tsc);
223 tsc2301_kp_exit(tsc);
229 static int __devexit tsc2301_remove(struct spi_device *spi)
231 struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
233 tsc2301_mixer_exit(tsc);
234 tsc2301_ts_exit(tsc);
235 tsc2301_kp_exit(tsc);
242 static int tsc2301_suspend(struct spi_device *spi, pm_message_t mesg)
244 struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
247 if ((r = tsc2301_mixer_suspend(tsc)) < 0)
249 if ((r = tsc2301_kp_suspend(tsc)) < 0)
251 if ((r = tsc2301_ts_suspend(tsc)) < 0)
256 tsc2301_kp_resume(tsc);
258 tsc2301_mixer_resume(tsc);
262 static int tsc2301_resume(struct spi_device *spi)
264 struct tsc2301 *tsc = dev_get_drvdata(&spi->dev);
266 tsc2301_ts_resume(tsc);
267 tsc2301_kp_resume(tsc);
268 tsc2301_mixer_resume(tsc);
273 static struct spi_driver tsc2301_driver = {
276 .bus = &spi_bus_type,
277 .owner = THIS_MODULE,
280 .suspend = tsc2301_suspend,
281 .resume = tsc2301_resume,
283 .probe = tsc2301_probe,
284 .remove = __devexit_p(tsc2301_remove),
287 static int __init tsc2301_init(void)
289 printk("TSC2301 driver initializing\n");
291 return spi_register_driver(&tsc2301_driver);
293 module_init(tsc2301_init);
295 static void __exit tsc2301_exit(void)
297 spi_unregister_driver(&tsc2301_driver);
299 module_exit(tsc2301_exit);
301 MODULE_AUTHOR("Juha Yrjölä <juha.yrjola@nokia.com>");
302 MODULE_LICENSE("GPL");