Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[pandora-kernel.git] / drivers / tty / serial / max3107-aava.c
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>
7  *
8  *  Copyright (C) Aavamobile 2009
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
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.
16  *
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.
21  *
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
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  *
28  */
29
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"
42
43 /* GPIO direction to input function */
44 static int max3107_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
45 {
46         struct max3107_port *s = container_of(chip, struct max3107_port, chip);
47         u16 buf[1];             /* Buffer for SPI transfer */
48
49         if (offset >= MAX3107_GPIO_COUNT) {
50                 dev_err(&s->spi->dev, "Invalid GPIO\n");
51                 return -EINVAL;
52         }
53
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;
60         }
61         buf[0] &= MAX3107_SPI_RX_DATA_MASK;
62
63         /* Set GPIO to input */
64         buf[0] &= ~(0x0001 << offset);
65
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;
72         }
73         return 0;
74 }
75
76 /* GPIO direction to output function */
77 static int max3107_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
78                                         int value)
79 {
80         struct max3107_port *s = container_of(chip, struct max3107_port, chip);
81         u16 buf[2];     /* Buffer for SPI transfers */
82
83         if (offset >= MAX3107_GPIO_COUNT) {
84                 dev_err(&s->spi->dev, "Invalid GPIO\n");
85                 return -EINVAL;
86         }
87
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;
95         }
96         buf[0] &= MAX3107_SPI_RX_DATA_MASK;
97         buf[1] &= MAX3107_SPI_RX_DATA_MASK;
98
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);
106
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;
115         }
116         return 0;
117 }
118
119 /* GPIO value query function */
120 static int max3107_gpio_get(struct gpio_chip *chip, unsigned offset)
121 {
122         struct max3107_port *s = container_of(chip, struct max3107_port, chip);
123         u16 buf[1];     /* Buffer for SPI transfer */
124
125         if (offset >= MAX3107_GPIO_COUNT) {
126                 dev_err(&s->spi->dev, "Invalid GPIO\n");
127                 return -EINVAL;
128         }
129
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;
136         }
137         buf[0] &= MAX3107_SPI_RX_DATA_MASK;
138
139         /* Return value */
140         return buf[0] & (0x0001 << offset);
141 }
142
143 /* GPIO value set function */
144 static void max3107_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
145 {
146         struct max3107_port *s = container_of(chip, struct max3107_port, chip);
147         u16 buf[2];     /* Buffer for SPI transfers */
148
149         if (offset >= MAX3107_GPIO_COUNT) {
150                 dev_err(&s->spi->dev, "Invalid GPIO\n");
151                 return;
152         }
153
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;
162         }
163         buf[0] &= MAX3107_SPI_RX_DATA_MASK;
164         buf[1] &= MAX3107_SPI_RX_DATA_MASK;
165
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;
171         }
172
173         /* Set value */
174         if (value)
175                 buf[0] |= (0x0001 << offset);
176         else
177                 buf[0] &= ~(0x0001 << offset);
178
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");
184 }
185
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,
196 };
197
198 /**
199  *      max3107_aava_reset      -       reset on AAVA systems
200  *      @spi: The SPI device we are probing
201  *
202  *      Reset the device ready for probing.
203  */
204
205 static int max3107_aava_reset(struct spi_device *spi)
206 {
207         /* Reset the chip */
208         if (gpio_request(MAX3107_RESET_GPIO, "max3107")) {
209                 pr_err("Requesting RESET GPIO failed\n");
210                 return -EIO;
211         }
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;
216         }
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;
222         }
223         gpio_free(MAX3107_RESET_GPIO);
224         msleep(MAX3107_WAKEUP_DELAY);
225         return 0;
226 }
227
228 static int max3107_aava_configure(struct max3107_port *s)
229 {
230         int retval;
231
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;
236
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;
242         }
243
244         /* Temporary fix for EV2 boot problems, set modem reset to 0 */
245         max3107_gpio_direction_out(&s->chip, 3, 0);
246         return 0;
247 }
248
249 #if 0
250 /* This will get enabled once we have the board stuff merged for this
251    specific case */
252
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 }
267 };
268
269 static void max3107_aava_init(struct max3107_port *s)
270 {
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;
277                         }
278         }
279 }
280 #endif
281
282 static int __devexit max3107_aava_remove(struct spi_device *spi)
283 {
284         struct max3107_port *s = dev_get_drvdata(&spi->dev);
285
286         /* Remove GPIO chip */
287         if (gpiochip_remove(&s->chip))
288                 dev_warn(&spi->dev, "Removing GPIO chip failed\n");
289
290         /* Then do the default remove */
291         return max3107_remove(spi);
292 }
293
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,
303 };
304
305
306 static int __devinit max3107_probe_aava(struct spi_device *spi)
307 {
308         int err = max3107_aava_reset(spi);
309         if (err < 0)
310                 return err;
311         return max3107_probe(spi, &aava_plat_data);
312 }
313
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,
320         },
321         .probe          = max3107_probe_aava,
322         .remove         = __devexit_p(max3107_aava_remove),
323         .suspend        = max3107_suspend,
324         .resume         = max3107_resume,
325 };
326
327 /* Driver init function */
328 static int __init max3107_init(void)
329 {
330         return spi_register_driver(&max3107_driver);
331 }
332
333 /* Driver exit function */
334 static void __exit max3107_exit(void)
335 {
336         spi_unregister_driver(&max3107_driver);
337 }
338
339 module_init(max3107_init);
340 module_exit(max3107_exit);
341
342 MODULE_DESCRIPTION("MAX3107 driver");
343 MODULE_AUTHOR("Aavamobile");
344 MODULE_ALIAS("spi:aava-max3107");
345 MODULE_LICENSE("GPL v2");