USB: cp210x: call generic open last in open
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / tty / serial / max3107-aava.c
blob90c40f22ec709422e51a52648db2ea2fde4bfd9a
1 /*
2 * max3107.c - spi uart protocol driver for Maxim 3107
3 * Based on max3100.c
4 * by Christian Pellegrin <chripell@evolware.org>
5 * and max3110.c
6 * by Feng Tang <feng.tang@intel.com>
8 * Copyright (C) Aavamobile 2009
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/serial_core.h>
33 #include <linux/serial.h>
34 #include <linux/spi/spi.h>
35 #include <linux/freezer.h>
36 #include <linux/platform_device.h>
37 #include <linux/gpio.h>
38 #include <linux/sfi.h>
39 #include <linux/module.h>
40 #include <asm/mrst.h>
41 #include "max3107.h"
43 /* GPIO direction to input function */
44 static int max3107_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
46 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
47 u16 buf[1]; /* Buffer for SPI transfer */
49 if (offset >= MAX3107_GPIO_COUNT) {
50 dev_err(&s->spi->dev, "Invalid GPIO\n");
51 return -EINVAL;
54 /* Read current GPIO configuration register */
55 buf[0] = MAX3107_GPIOCFG_REG;
56 /* Perform SPI transfer */
57 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
58 dev_err(&s->spi->dev, "SPI transfer GPIO read failed\n");
59 return -EIO;
61 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
63 /* Set GPIO to input */
64 buf[0] &= ~(0x0001 << offset);
66 /* Write new GPIO configuration register value */
67 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
68 /* Perform SPI transfer */
69 if (max3107_rw(s, (u8 *)buf, NULL, 2)) {
70 dev_err(&s->spi->dev, "SPI transfer GPIO write failed\n");
71 return -EIO;
73 return 0;
76 /* GPIO direction to output function */
77 static int max3107_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
78 int value)
80 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
81 u16 buf[2]; /* Buffer for SPI transfers */
83 if (offset >= MAX3107_GPIO_COUNT) {
84 dev_err(&s->spi->dev, "Invalid GPIO\n");
85 return -EINVAL;
88 /* Read current GPIO configuration and data registers */
89 buf[0] = MAX3107_GPIOCFG_REG;
90 buf[1] = MAX3107_GPIODATA_REG;
91 /* Perform SPI transfer */
92 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
93 dev_err(&s->spi->dev, "SPI transfer gpio failed\n");
94 return -EIO;
96 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
97 buf[1] &= MAX3107_SPI_RX_DATA_MASK;
99 /* Set GPIO to output */
100 buf[0] |= (0x0001 << offset);
101 /* Set value */
102 if (value)
103 buf[1] |= (0x0001 << offset);
104 else
105 buf[1] &= ~(0x0001 << offset);
107 /* Write new GPIO configuration and data register values */
108 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
109 buf[1] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
110 /* Perform SPI transfer */
111 if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
112 dev_err(&s->spi->dev,
113 "SPI transfer for GPIO conf data w failed\n");
114 return -EIO;
116 return 0;
119 /* GPIO value query function */
120 static int max3107_gpio_get(struct gpio_chip *chip, unsigned offset)
122 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
123 u16 buf[1]; /* Buffer for SPI transfer */
125 if (offset >= MAX3107_GPIO_COUNT) {
126 dev_err(&s->spi->dev, "Invalid GPIO\n");
127 return -EINVAL;
130 /* Read current GPIO data register */
131 buf[0] = MAX3107_GPIODATA_REG;
132 /* Perform SPI transfer */
133 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
134 dev_err(&s->spi->dev, "SPI transfer GPIO data r failed\n");
135 return -EIO;
137 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
139 /* Return value */
140 return buf[0] & (0x0001 << offset);
143 /* GPIO value set function */
144 static void max3107_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
146 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
147 u16 buf[2]; /* Buffer for SPI transfers */
149 if (offset >= MAX3107_GPIO_COUNT) {
150 dev_err(&s->spi->dev, "Invalid GPIO\n");
151 return;
154 /* Read current GPIO configuration registers*/
155 buf[0] = MAX3107_GPIODATA_REG;
156 buf[1] = MAX3107_GPIOCFG_REG;
157 /* Perform SPI transfer */
158 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
159 dev_err(&s->spi->dev,
160 "SPI transfer for GPIO data and config read failed\n");
161 return;
163 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
164 buf[1] &= MAX3107_SPI_RX_DATA_MASK;
166 if (!(buf[1] & (0x0001 << offset))) {
167 /* Configured as input, can't set value */
168 dev_warn(&s->spi->dev,
169 "Trying to set value for input GPIO\n");
170 return;
173 /* Set value */
174 if (value)
175 buf[0] |= (0x0001 << offset);
176 else
177 buf[0] &= ~(0x0001 << offset);
179 /* Write new GPIO data register value */
180 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
181 /* Perform SPI transfer */
182 if (max3107_rw(s, (u8 *)buf, NULL, 2))
183 dev_err(&s->spi->dev, "SPI transfer GPIO data w failed\n");
186 /* GPIO chip data */
187 static struct gpio_chip max3107_gpio_chip = {
188 .owner = THIS_MODULE,
189 .direction_input = max3107_gpio_direction_in,
190 .direction_output = max3107_gpio_direction_out,
191 .get = max3107_gpio_get,
192 .set = max3107_gpio_set,
193 .can_sleep = 1,
194 .base = MAX3107_GPIO_BASE,
195 .ngpio = MAX3107_GPIO_COUNT,
199 * max3107_aava_reset - reset on AAVA systems
200 * @spi: The SPI device we are probing
202 * Reset the device ready for probing.
205 static int max3107_aava_reset(struct spi_device *spi)
207 /* Reset the chip */
208 if (gpio_request(MAX3107_RESET_GPIO, "max3107")) {
209 pr_err("Requesting RESET GPIO failed\n");
210 return -EIO;
212 if (gpio_direction_output(MAX3107_RESET_GPIO, 0)) {
213 pr_err("Setting RESET GPIO to 0 failed\n");
214 gpio_free(MAX3107_RESET_GPIO);
215 return -EIO;
217 msleep(MAX3107_RESET_DELAY);
218 if (gpio_direction_output(MAX3107_RESET_GPIO, 1)) {
219 pr_err("Setting RESET GPIO to 1 failed\n");
220 gpio_free(MAX3107_RESET_GPIO);
221 return -EIO;
223 gpio_free(MAX3107_RESET_GPIO);
224 msleep(MAX3107_WAKEUP_DELAY);
225 return 0;
228 static int max3107_aava_configure(struct max3107_port *s)
230 int retval;
232 /* Initialize GPIO chip data */
233 s->chip = max3107_gpio_chip;
234 s->chip.label = s->spi->modalias;
235 s->chip.dev = &s->spi->dev;
237 /* Add GPIO chip */
238 retval = gpiochip_add(&s->chip);
239 if (retval) {
240 dev_err(&s->spi->dev, "Adding GPIO chip failed\n");
241 return retval;
244 /* Temporary fix for EV2 boot problems, set modem reset to 0 */
245 max3107_gpio_direction_out(&s->chip, 3, 0);
246 return 0;
249 #if 0
250 /* This will get enabled once we have the board stuff merged for this
251 specific case */
253 static const struct baud_table brg13_ext[] = {
254 { 300, MAX3107_BRG13_B300 },
255 { 600, MAX3107_BRG13_B600 },
256 { 1200, MAX3107_BRG13_B1200 },
257 { 2400, MAX3107_BRG13_B2400 },
258 { 4800, MAX3107_BRG13_B4800 },
259 { 9600, MAX3107_BRG13_B9600 },
260 { 19200, MAX3107_BRG13_B19200 },
261 { 57600, MAX3107_BRG13_B57600 },
262 { 115200, MAX3107_BRG13_B115200 },
263 { 230400, MAX3107_BRG13_B230400 },
264 { 460800, MAX3107_BRG13_B460800 },
265 { 921600, MAX3107_BRG13_B921600 },
266 { 0, 0 }
269 static void max3107_aava_init(struct max3107_port *s)
271 /*override for AAVA SC specific*/
272 if (mrst_platform_id() == MRST_PLATFORM_AAVA_SC) {
273 if (get_koski_build_id() <= KOSKI_EV2)
274 if (s->ext_clk) {
275 s->brg_cfg = MAX3107_BRG13_B9600;
276 s->baud_tbl = (struct baud_table *)brg13_ext;
280 #endif
282 static int __devexit max3107_aava_remove(struct spi_device *spi)
284 struct max3107_port *s = dev_get_drvdata(&spi->dev);
286 /* Remove GPIO chip */
287 if (gpiochip_remove(&s->chip))
288 dev_warn(&spi->dev, "Removing GPIO chip failed\n");
290 /* Then do the default remove */
291 return max3107_remove(spi);
294 /* Platform data */
295 static struct max3107_plat aava_plat_data = {
296 .loopback = 0,
297 .ext_clk = 1,
298 /* .init = max3107_aava_init, */
299 .configure = max3107_aava_configure,
300 .hw_suspend = max3107_hw_susp,
301 .polled_mode = 0,
302 .poll_time = 0,
306 static int __devinit max3107_probe_aava(struct spi_device *spi)
308 int err = max3107_aava_reset(spi);
309 if (err < 0)
310 return err;
311 return max3107_probe(spi, &aava_plat_data);
314 /* Spi driver data */
315 static struct spi_driver max3107_driver = {
316 .driver = {
317 .name = "aava-max3107",
318 .bus = &spi_bus_type,
319 .owner = THIS_MODULE,
321 .probe = max3107_probe_aava,
322 .remove = __devexit_p(max3107_aava_remove),
323 .suspend = max3107_suspend,
324 .resume = max3107_resume,
327 /* Driver init function */
328 static int __init max3107_init(void)
330 return spi_register_driver(&max3107_driver);
333 /* Driver exit function */
334 static void __exit max3107_exit(void)
336 spi_unregister_driver(&max3107_driver);
339 module_init(max3107_init);
340 module_exit(max3107_exit);
342 MODULE_DESCRIPTION("MAX3107 driver");
343 MODULE_AUTHOR("Aavamobile");
344 MODULE_ALIAS("spi:aava-max3107");
345 MODULE_LICENSE("GPL v2");