aboutsummaryrefslogtreecommitdiff
path: root/drivers/tty/serial/max3107-aava.c
blob: d73aadd7a9ad6614385b62f92582c5c7b87c7b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 *  max3107.c - spi uart protocol driver for Maxim 3107
 *  Based on max3100.c
 *	by Christian Pellegrin <chripell@evolware.org>
 *  and	max3110.c
 *	by Feng Tang <feng.tang@intel.com>
 *
 *  Copyright (C) Aavamobile 2009
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 */

#include <linux/delay.h>
#include <linux/device.h>
#include <linux/serial_core.h>
#include <linux/serial.h>
#include <linux/spi/spi.h>
#include <linux/freezer.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/sfi.h>
#include <asm/mrst.h>
#include "max3107.h"

/* GPIO direction to input function */
static int max3107_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
{
	struct max3107_port *s = container_of(chip, struct max3107_port, chip);
	u16 buf[1];		/* Buffer for SPI transfer */

	if (offset >= MAX3107_GPIO_COUNT) {
		dev_err(&s->spi->dev, "Invalid GPIO\n");
		return -EINVAL;
	}

	/* Read current GPIO configuration register */
	buf[0] = MAX3107_GPIOCFG_REG;
	/* Perform SPI transfer */
	if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
		dev_err(&s->spi->dev, "SPI transfer GPIO read failed\n");
		return -EIO;
	}
	buf[0] &= MAX3107_SPI_RX_DATA_MASK;

	/* Set GPIO to input */
	buf[0] &= ~(0x0001 << offset);

	/* Write new GPIO configuration register value */
	buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
	/* Perform SPI transfer */
	if (max3107_rw(s, (u8 *)buf, NULL, 2)) {
		dev_err(&s->spi->dev, "SPI transfer GPIO write failed\n");
		return -EIO;
	}
	return 0;
}

/* GPIO direction to output function */
static int max3107_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
					int value)
{
	struct max3107_port *s = container_of(chip, struct max3107_port, chip);
	u16 buf[2];	/* Buffer for SPI transfers */

	if (offset >= MAX3107_GPIO_COUNT) {
		dev_err(&s->spi->dev, "Invalid GPIO\n");
		return -EINVAL;
	}

	/* Read current GPIO configuration and data registers */
	buf[0] = MAX3107_GPIOCFG_REG;
	buf[1] = MAX3107_GPIODATA_REG;
	/* Perform SPI transfer */
	if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
		dev_err(&s->spi->dev, "SPI transfer gpio failed\n");
		return -EIO;
	}
	buf[0] &= MAX3107_SPI_RX_DATA_MASK;
	buf[1] &= MAX3107_SPI_RX_DATA_MASK;

	/* Set GPIO to output */
	buf[0] |= (0x0001 << offset);
	/* Set value */
	if (value)
		buf[1] |= (0x0001 << offset);
	else
		buf[1] &= ~(0x0001 << offset);

	/* Write new GPIO configuration and data register values */
	buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
	buf[1] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
	/* Perform SPI transfer */
	if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
		dev_err(&s->spi->dev,
			"SPI transfer for GPIO conf data w failed\n");
		return -EIO;
	}
	return 0;
}

/* GPIO value query function */
static int max3107_gpio_get(struct gpio_chip *chip, unsigned offset)
{
	struct max3107_port *s = container_of(chip, struct max3107_port, chip);
	u16 buf[1];	/* Buffer for SPI transfer */

	if (offset >= MAX3107_GPIO_COUNT) {
		dev_err(&s->spi->dev, "Invalid GPIO\n");
		return -EINVAL;
	}

	/* Read current GPIO data register */
	buf[0] = MAX3107_GPIODATA_REG;
	/* Perform SPI transfer */
	if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
		dev_err(&s->spi->dev, "SPI transfer GPIO data r failed\n");
		return -EIO;
	}
	buf[0] &= MAX3107_SPI_RX_DATA_MASK;

	/* Return value */
	return buf[0] & (0x0001 << offset);
}

/* GPIO value set function */
static void max3107_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	struct max3107_port *s = container_of(chip, struct max3107_port, chip);
	u16 buf[2];	/* Buffer for SPI transfers */

	if (offset >= MAX3107_GPIO_COUNT) {
		dev_err(&s->spi->dev, "Invalid GPIO\n");
		return;
	}

	/* Read current GPIO configuration registers*/
	buf[0] = MAX3107_GPIODATA_REG;
	buf[1] = MAX3107_GPIOCFG_REG;
	/* Perform SPI transfer */
	if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
		dev_err(&s->spi->dev,
			"SPI transfer for GPIO data and config read failed\n");
		return;
	}
	buf[0] &= MAX3107_SPI_RX_DATA_MASK;
	buf[1] &= MAX3107_SPI_RX_DATA_MASK;

	if (!(buf[1] & (0x0001 << offset))) {
		/* Configured as input, can't set value */
		dev_warn(&s->spi->dev,
				"Trying to set value for input GPIO\n");
		return;
	}

	/* Set value */
	if (value)
		buf[0] |= (0x0001 << offset);
	else
		buf[0] &= ~(0x0001 << offset);

	/* Write new GPIO data register value */
	buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
	/* Perform SPI transfer */
	if (max3107_rw(s, (u8 *)buf, NULL, 2))
		dev_err(&s->spi->dev, "SPI transfer GPIO data w failed\n");
}

/* GPIO chip data */
static struct gpio_chip max3107_gpio_chip = {
	.owner			= THIS_MODULE,
	.direction_input	= max3107_gpio_direction_in,
	.direction_output	= max3107_gpio_direction_out,
	.get			= max3107_gpio_get,
	.set			= max3107_gpio_set,
	.can_sleep		= 1,
	.base			= MAX3107_GPIO_BASE,
	.ngpio			= MAX3107_GPIO_COUNT,
};

/**
 *	max3107_aava_reset	-	reset on AAVA systems
 *	@spi: The SPI device we are probing
 *
 *	Reset the device ready for probing.
 */

static int max3107_aava_reset(struct spi_device *spi)
{
	/* Reset the chip */
	if (gpio_request(MAX3107_RESET_GPIO, "max3107")) {
		pr_err("Requesting RESET GPIO failed\n");
		return -EIO;
	}
	if (gpio_direction_output(MAX3107_RESET_GPIO, 0)) {
		pr_err("Setting RESET GPIO to 0 failed\n");
		gpio_free(MAX3107_RESET_GPIO);
		return -EIO;
	}
	msleep(MAX3107_RESET_DELAY);
	if (gpio_direction_output(MAX3107_RESET_GPIO, 1)) {
		pr_err("Setting RESET GPIO to 1 failed\n");
		gpio_free(MAX3107_RESET_GPIO);
		return -EIO;
	}
	gpio_free(MAX3107_RESET_GPIO);
	msleep(MAX3107_WAKEUP_DELAY);
	return 0;
}

static int max3107_aava_configure(struct max3107_port *s)
{
	int retval;

	/* Initialize GPIO chip data */
	s->chip = max3107_gpio_chip;
	s->chip.label = s->spi->modalias;
	s->chip.dev = &s->spi->dev;

	/* Add GPIO chip */
	retval = gpiochip_add(&s->chip);
	if (retval) {
		dev_err(&s->spi->dev, "Adding GPIO chip failed\n");
		return retval;
	}

	/* Temporary fix for EV2 boot problems, set modem reset to 0 */
	max3107_gpio_direction_out(&s->chip, 3, 0);
	return 0;
}

#if 0
/* This will get enabled once we have the board stuff merged for this
   specific case */

static const struct baud_table brg13_ext[] = {
	{ 300,    MAX3107_BRG13_B300 },
	{ 600,    MAX3107_BRG13_B600 },
	{ 1200,   MAX3107_BRG13_B1200 },
	{ 2400,   MAX3107_BRG13_B2400 },
	{ 4800,   MAX3107_BRG13_B4800 },
	{ 9600,   MAX3107_BRG13_B9600 },
	{ 19200,  MAX3107_BRG13_B19200 },
	{ 57600,  MAX3107_BRG13_B57600 },
	{ 115200, MAX3107_BRG13_B115200 },
	{ 230400, MAX3107_BRG13_B230400 },
	{ 460800, MAX3107_BRG13_B460800 },
	{ 921600, MAX3107_BRG13_B921600 },
	{ 0, 0 }
};

static void max3107_aava_init(struct max3107_port *s)
{
	/*override for AAVA SC specific*/
	if (mrst_platform_id() == MRST_PLATFORM_AAVA_SC) {
		if (get_koski_build_id() <= KOSKI_EV2)
			if (s->ext_clk) {
				s->brg_cfg = MAX3107_BRG13_B9600;
				s->baud_tbl = (struct baud_table *)brg13_ext;
			}
	}
}
#endif

static int __devexit max3107_aava_remove(struct spi_device *spi)
{
	struct max3107_port *s = dev_get_drvdata(&spi->dev);

	/* Remove GPIO chip */
	if (gpiochip_remove(&s->chip))
		dev_warn(&spi->dev, "Removing GPIO chip failed\n");

	/* Then do the default remove */
	return max3107_remove(spi);
}

/* Platform data */
static struct max3107_plat aava_plat_data = {
	.loopback               = 0,
	.ext_clk                = 1,
/*	.init			= max3107_aava_init, */
	.configure		= max3107_aava_configure,
	.hw_suspend		= max3107_hw_susp,
	.polled_mode            = 0,
	.poll_time              = 0,
};


static int __devinit max3107_probe_aava(struct spi_device *spi)
{
	int err = max3107_aava_reset(spi);
	if (err < 0)
		return err;
	return max3107_probe(spi, &aava_plat_data);
}

/* Spi driver data */
static struct spi_driver max3107_driver = {
	.driver = {
		.name		= "aava-max3107",
		.bus		= &spi_bus_type,
		.owner		= THIS_MODULE,
	},
	.probe		= max3107_probe_aava,
	.remove		= __devexit_p(max3107_aava_remove),
	.suspend	= max3107_suspend,
	.resume		= max3107_resume,
};

/* Driver init function */
static int __init max3107_init(void)
{
	return spi_register_driver(&max3107_driver);
}

/* Driver exit function */
static void __exit max3107_exit(void)
{
	spi_unregister_driver(&max3107_driver);
}

module_init(max3107_init);
module_exit(max3107_exit);

MODULE_DESCRIPTION("MAX3107 driver");
MODULE_AUTHOR("Aavamobile");
MODULE_ALIAS("spi:aava-max3107");
MODULE_LICENSE("GPL v2");