1 /*
2  *  Copyright (C) 2010 Christian Glindkamp <christian.glindkamp@taskit.de>
3  *                     taskit GmbH
4  *                2010 Igor Plyatov <plyatov@gmail.com>
5  *                     GeoSIG Ltd
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 #include <linux/platform_device.h>
23 #include <linux/gpio.h>
24 #include <linux/w1-gpio.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c/pcf857x.h>
27 #include <linux/gpio_keys.h>
28 #include <linux/input.h>
29 
30 #include <asm/mach-types.h>
31 #include <asm/mach/arch.h>
32 
33 #include <mach/board.h>
34 #include <mach/at91sam9_smc.h>
35 #include <mach/gsia18s.h>
36 #include <mach/stamp9g20.h>
37 
38 #include "sam9_smc.h"
39 #include "generic.h"
40 
gsia18s_init_early(void)41 static void __init gsia18s_init_early(void)
42 {
43 	stamp9g20_init_early();
44 
45 	/*
46 	 * USART0 on ttyS1 (Rx, Tx, CTS, RTS, DTR, DSR, DCD, RI).
47 	 * Used for Internal Analog Modem.
48 	 */
49 	at91_register_uart(AT91SAM9260_ID_US0, 1,
50 				ATMEL_UART_CTS | ATMEL_UART_RTS |
51 				ATMEL_UART_DTR | ATMEL_UART_DSR |
52 				ATMEL_UART_DCD | ATMEL_UART_RI);
53 	/*
54 	 * USART1 on ttyS2 (Rx, Tx, CTS, RTS).
55 	 * Used for GPS or WiFi or Data stream.
56 	 */
57 	at91_register_uart(AT91SAM9260_ID_US1, 2,
58 				ATMEL_UART_CTS | ATMEL_UART_RTS);
59 	/*
60 	 * USART2 on ttyS3 (Rx, Tx, CTS, RTS).
61 	 * Used for External Modem.
62 	 */
63 	at91_register_uart(AT91SAM9260_ID_US2, 3,
64 				ATMEL_UART_CTS | ATMEL_UART_RTS);
65 	/*
66 	 * USART3 on ttyS4 (Rx, Tx, RTS).
67 	 * Used for RS-485.
68 	 */
69 	at91_register_uart(AT91SAM9260_ID_US3, 4, ATMEL_UART_RTS);
70 
71 	/*
72 	 * USART4 on ttyS5 (Rx, Tx).
73 	 * Used for TRX433 Radio Module.
74 	 */
75 	at91_register_uart(AT91SAM9260_ID_US4, 5, 0);
76 }
77 
78 /*
79  * Two USB Host ports
80  */
81 static struct at91_usbh_data __initdata usbh_data = {
82 	.ports		= 2,
83 	.vbus_pin	= {-EINVAL, -EINVAL},
84 	.overcurrent_pin= {-EINVAL, -EINVAL},
85 };
86 
87 /*
88  * USB Device port
89  */
90 static struct at91_udc_data __initdata udc_data = {
91 	.vbus_pin	= AT91_PIN_PA22,
92 	.pullup_pin	= -EINVAL,		/* pull-up driven by UDC */
93 };
94 
95 /*
96  * MACB Ethernet device
97  */
98 static struct macb_platform_data __initdata macb_data = {
99 	.phy_irq_pin	= AT91_PIN_PA28,
100 	.is_rmii	= 1,
101 };
102 
103 /*
104  * LEDs and GPOs
105  */
106 static struct gpio_led gpio_leds[] = {
107 	{
108 		.name			= "gpo:spi1reset",
109 		.gpio			= AT91_PIN_PC1,
110 		.active_low		= 0,
111 		.default_trigger	= "none",
112 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
113 	},
114 	{
115 		.name			= "gpo:trig_net_out",
116 		.gpio			= AT91_PIN_PB20,
117 		.active_low		= 0,
118 		.default_trigger	= "none",
119 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
120 	},
121 	{
122 		.name			= "gpo:trig_net_dir",
123 		.gpio			= AT91_PIN_PB19,
124 		.active_low		= 0,
125 		.default_trigger	= "none",
126 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
127 	},
128 	{
129 		.name			= "gpo:charge_dis",
130 		.gpio			= AT91_PIN_PC2,
131 		.active_low		= 0,
132 		.default_trigger	= "none",
133 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
134 	},
135 	{
136 		.name			= "led:event",
137 		.gpio			= AT91_PIN_PB17,
138 		.active_low		= 1,
139 		.default_trigger	= "none",
140 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
141 	},
142 	{
143 		.name			= "led:lan",
144 		.gpio			= AT91_PIN_PB18,
145 		.active_low		= 1,
146 		.default_trigger	= "none",
147 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
148 	},
149 	{
150 		.name			= "led:error",
151 		.gpio			= AT91_PIN_PB16,
152 		.active_low		= 1,
153 		.default_trigger	= "none",
154 		.default_state		= LEDS_GPIO_DEFSTATE_ON,
155 	}
156 };
157 
158 static struct gpio_led_platform_data gpio_led_info = {
159 	.leds		= gpio_leds,
160 	.num_leds	= ARRAY_SIZE(gpio_leds),
161 };
162 
163 static struct platform_device leds = {
164 	.name	= "leds-gpio",
165 	.id	= 0,
166 	.dev	= {
167 		.platform_data	= &gpio_led_info,
168 	}
169 };
170 
gsia18s_leds_init(void)171 static void __init gsia18s_leds_init(void)
172 {
173 	platform_device_register(&leds);
174 }
175 
176 /* PCF8574 0x20 GPIO - U1 on the GS_IA18-CB_V3 board */
177 static struct gpio_led pcf_gpio_leds1[] = {
178 	{ /* bit 0 */
179 		.name			= "gpo:hdc_power",
180 		.gpio			= PCF_GPIO_HDC_POWER,
181 		.active_low		= 0,
182 		.default_trigger	= "none",
183 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
184 	},
185 	{ /* bit 1 */
186 		.name			= "gpo:wifi_setup",
187 		.gpio			= PCF_GPIO_WIFI_SETUP,
188 		.active_low		= 1,
189 		.default_trigger	= "none",
190 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
191 	},
192 	{ /* bit 2 */
193 		.name			= "gpo:wifi_enable",
194 		.gpio			= PCF_GPIO_WIFI_ENABLE,
195 		.active_low		= 1,
196 		.default_trigger	= "none",
197 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
198 	},
199 	{ /* bit 3	*/
200 		.name			= "gpo:wifi_reset",
201 		.gpio			= PCF_GPIO_WIFI_RESET,
202 		.active_low		= 1,
203 		.default_trigger	= "none",
204 		.default_state		= LEDS_GPIO_DEFSTATE_ON,
205 	},
206 	/* bit 4 used as GPI	*/
207 	{ /* bit 5 */
208 		.name			= "gpo:gps_setup",
209 		.gpio			= PCF_GPIO_GPS_SETUP,
210 		.active_low		= 1,
211 		.default_trigger	= "none",
212 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
213 	},
214 	{ /* bit 6 */
215 		.name			= "gpo:gps_standby",
216 		.gpio			= PCF_GPIO_GPS_STANDBY,
217 		.active_low		= 0,
218 		.default_trigger	= "none",
219 		.default_state		= LEDS_GPIO_DEFSTATE_ON,
220 	},
221 	{ /* bit 7 */
222 		.name			= "gpo:gps_power",
223 		.gpio			= PCF_GPIO_GPS_POWER,
224 		.active_low		= 0,
225 		.default_trigger	= "none",
226 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
227 	}
228 };
229 
230 static struct gpio_led_platform_data pcf_gpio_led_info1 = {
231 	.leds		= pcf_gpio_leds1,
232 	.num_leds	= ARRAY_SIZE(pcf_gpio_leds1),
233 };
234 
235 static struct platform_device pcf_leds1 = {
236 	.name	= "leds-gpio", /* GS_IA18-CB_board */
237 	.id	= 1,
238 	.dev	= {
239 		.platform_data	= &pcf_gpio_led_info1,
240 	}
241 };
242 
243 /* PCF8574 0x22 GPIO - U1 on the GS_2G_OPT1-A_V0 board (Alarm) */
244 static struct gpio_led pcf_gpio_leds2[] = {
245 	{ /* bit 0 */
246 		.name			= "gpo:alarm_1",
247 		.gpio			= PCF_GPIO_ALARM1,
248 		.active_low		= 1,
249 		.default_trigger	= "none",
250 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
251 	},
252 	{ /* bit 1 */
253 		.name			= "gpo:alarm_2",
254 		.gpio			= PCF_GPIO_ALARM2,
255 		.active_low		= 1,
256 		.default_trigger	= "none",
257 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
258 	},
259 	{ /* bit 2 */
260 		.name			= "gpo:alarm_3",
261 		.gpio			= PCF_GPIO_ALARM3,
262 		.active_low		= 1,
263 		.default_trigger	= "none",
264 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
265 	},
266 	{ /* bit 3 */
267 		.name			= "gpo:alarm_4",
268 		.gpio			= PCF_GPIO_ALARM4,
269 		.active_low		= 1,
270 		.default_trigger	= "none",
271 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
272 	},
273 	/* bits 4, 5, 6 not used */
274 	{ /* bit 7 */
275 		.name			= "gpo:alarm_v_relay_on",
276 		.gpio			= PCF_GPIO_ALARM_V_RELAY_ON,
277 		.active_low		= 0,
278 		.default_trigger	= "none",
279 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
280 	},
281 };
282 
283 static struct gpio_led_platform_data pcf_gpio_led_info2 = {
284 	.leds		= pcf_gpio_leds2,
285 	.num_leds	= ARRAY_SIZE(pcf_gpio_leds2),
286 };
287 
288 static struct platform_device pcf_leds2 = {
289 	.name	= "leds-gpio",
290 	.id	= 2,
291 	.dev	= {
292 		.platform_data	= &pcf_gpio_led_info2,
293 	}
294 };
295 
296 /* PCF8574 0x24 GPIO U1 on the GS_2G-OPT23-A_V0 board (Modem) */
297 static struct gpio_led pcf_gpio_leds3[] = {
298 	{ /* bit 0 */
299 		.name			= "gpo:modem_power",
300 		.gpio			= PCF_GPIO_MODEM_POWER,
301 		.active_low		= 1,
302 		.default_trigger	= "none",
303 		.default_state		= LEDS_GPIO_DEFSTATE_OFF,
304 	},
305 		/* bits 1 and 2 not used */
306 	{ /* bit 3 */
307 		.name			= "gpo:modem_reset",
308 		.gpio			= PCF_GPIO_MODEM_RESET,
309 		.active_low		= 1,
310 		.default_trigger	= "none",
311 		.default_state		= LEDS_GPIO_DEFSTATE_ON,
312 	},
313 		/* bits 4, 5 and 6 not used */
314 	{ /* bit 7 */
315 		.name			= "gpo:trx_reset",
316 		.gpio			= PCF_GPIO_TRX_RESET,
317 		.active_low		= 1,
318 		.default_trigger	= "none",
319 		.default_state		= LEDS_GPIO_DEFSTATE_ON,
320 	}
321 };
322 
323 static struct gpio_led_platform_data pcf_gpio_led_info3 = {
324 	.leds		= pcf_gpio_leds3,
325 	.num_leds	= ARRAY_SIZE(pcf_gpio_leds3),
326 };
327 
328 static struct platform_device pcf_leds3 = {
329 	.name	= "leds-gpio",
330 	.id	= 3,
331 	.dev	= {
332 		.platform_data	= &pcf_gpio_led_info3,
333 	}
334 };
335 
gsia18s_pcf_leds_init(void)336 static void __init gsia18s_pcf_leds_init(void)
337 {
338 	platform_device_register(&pcf_leds1);
339 	platform_device_register(&pcf_leds2);
340 	platform_device_register(&pcf_leds3);
341 }
342 
343 /*
344  * SPI busses.
345  */
346 static struct spi_board_info gsia18s_spi_devices[] = {
347 	{ /* User accessible spi0, cs0 used for communication with MSP RTC */
348 		.modalias	= "spidev",
349 		.bus_num	= 0,
350 		.chip_select	= 0,
351 		.max_speed_hz	= 580000,
352 		.mode		= SPI_MODE_1,
353 	},
354 	{ /* User accessible spi1, cs0 used for communication with int. DSP */
355 		.modalias	= "spidev",
356 		.bus_num	= 1,
357 		.chip_select	= 0,
358 		.max_speed_hz	= 5600000,
359 		.mode		= SPI_MODE_0,
360 	},
361 	{ /* User accessible spi1, cs1 used for communication with ext. DSP */
362 		.modalias	= "spidev",
363 		.bus_num	= 1,
364 		.chip_select	= 1,
365 		.max_speed_hz	= 5600000,
366 		.mode		= SPI_MODE_0,
367 	},
368 	{ /* User accessible spi1, cs2 used for communication with ext. DSP */
369 		.modalias	= "spidev",
370 		.bus_num	= 1,
371 		.chip_select	= 2,
372 		.max_speed_hz	= 5600000,
373 		.mode		= SPI_MODE_0,
374 	},
375 	{ /* User accessible spi1, cs3 used for communication with ext. DSP */
376 		.modalias	= "spidev",
377 		.bus_num	= 1,
378 		.chip_select	= 3,
379 		.max_speed_hz	= 5600000,
380 		.mode		= SPI_MODE_0,
381 	}
382 };
383 
384 /*
385  * GPI Buttons
386  */
387 static struct gpio_keys_button buttons[] = {
388 	{
389 		.gpio		= GPIO_TRIG_NET_IN,
390 		.code		= BTN_1,
391 		.desc		= "TRIG_NET_IN",
392 		.type		= EV_KEY,
393 		.active_low	= 0,
394 		.wakeup		= 1,
395 	},
396 	{ /* SW80 on the GS_IA18_S-MN board*/
397 		.gpio		= GPIO_CARD_UNMOUNT_0,
398 		.code		= BTN_2,
399 		.desc		= "Card umount 0",
400 		.type		= EV_KEY,
401 		.active_low	= 1,
402 		.wakeup		= 1,
403 	},
404 	{ /* SW79 on the GS_IA18_S-MN board*/
405 		.gpio		= GPIO_CARD_UNMOUNT_1,
406 		.code		= BTN_3,
407 		.desc		= "Card umount 1",
408 		.type		= EV_KEY,
409 		.active_low	= 1,
410 		.wakeup		= 1,
411 	},
412 	{ /* SW280 on the GS_IA18-CB board*/
413 		.gpio		= GPIO_KEY_POWER,
414 		.code		= KEY_POWER,
415 		.desc		= "Power Off Button",
416 		.type		= EV_KEY,
417 		.active_low	= 0,
418 		.wakeup		= 1,
419 	}
420 };
421 
422 static struct gpio_keys_platform_data button_data = {
423 	.buttons	= buttons,
424 	.nbuttons	= ARRAY_SIZE(buttons),
425 };
426 
427 static struct platform_device button_device = {
428 	.name		= "gpio-keys",
429 	.id		= -1,
430 	.num_resources	= 0,
431 	.dev		= {
432 		.platform_data	= &button_data,
433 	}
434 };
435 
gsia18s_add_device_buttons(void)436 static void __init gsia18s_add_device_buttons(void)
437 {
438 	at91_set_gpio_input(GPIO_TRIG_NET_IN, 1);
439 	at91_set_deglitch(GPIO_TRIG_NET_IN, 1);
440 	at91_set_gpio_input(GPIO_CARD_UNMOUNT_0, 1);
441 	at91_set_deglitch(GPIO_CARD_UNMOUNT_0, 1);
442 	at91_set_gpio_input(GPIO_CARD_UNMOUNT_1, 1);
443 	at91_set_deglitch(GPIO_CARD_UNMOUNT_1, 1);
444 	at91_set_gpio_input(GPIO_KEY_POWER, 0);
445 	at91_set_deglitch(GPIO_KEY_POWER, 1);
446 
447 	platform_device_register(&button_device);
448 }
449 
450 /*
451  * I2C
452  */
pcf8574x_0x20_setup(struct i2c_client * client,int gpio,unsigned int ngpio,void * context)453 static int pcf8574x_0x20_setup(struct i2c_client *client, int gpio,
454 				unsigned int ngpio, void *context)
455 {
456 	int status;
457 
458 	status = gpio_request(gpio + PCF_GPIO_ETH_DETECT, "eth_det");
459 	if (status < 0) {
460 		pr_err("error: can't request GPIO%d\n",
461 			gpio + PCF_GPIO_ETH_DETECT);
462 		return status;
463 	}
464 	status = gpio_direction_input(gpio + PCF_GPIO_ETH_DETECT);
465 	if (status < 0) {
466 		pr_err("error: can't setup GPIO%d as input\n",
467 			gpio + PCF_GPIO_ETH_DETECT);
468 		return status;
469 	}
470 	status = gpio_export(gpio + PCF_GPIO_ETH_DETECT, false);
471 	if (status < 0) {
472 		pr_err("error: can't export GPIO%d\n",
473 			gpio + PCF_GPIO_ETH_DETECT);
474 		return status;
475 	}
476 	status = gpio_sysfs_set_active_low(gpio + PCF_GPIO_ETH_DETECT, 1);
477 	if (status < 0) {
478 		pr_err("error: gpio_sysfs_set active_low(GPIO%d, 1)\n",
479 			gpio + PCF_GPIO_ETH_DETECT);
480 		return status;
481 	}
482 
483 	return 0;
484 }
485 
pcf8574x_0x20_teardown(struct i2c_client * client,int gpio,unsigned ngpio,void * context)486 static int pcf8574x_0x20_teardown(struct i2c_client *client, int gpio,
487 					unsigned ngpio, void *context)
488 {
489 	gpio_free(gpio + PCF_GPIO_ETH_DETECT);
490 	return 0;
491 }
492 
493 static struct pcf857x_platform_data pcf20_pdata = {
494 	.gpio_base	= GS_IA18_S_PCF_GPIO_BASE0,
495 	.n_latch	= (1 << 4),
496 	.setup		= pcf8574x_0x20_setup,
497 	.teardown	= pcf8574x_0x20_teardown,
498 };
499 
500 static struct pcf857x_platform_data pcf22_pdata = {
501 	.gpio_base	= GS_IA18_S_PCF_GPIO_BASE1,
502 };
503 
504 static struct pcf857x_platform_data pcf24_pdata = {
505 	.gpio_base	= GS_IA18_S_PCF_GPIO_BASE2,
506 };
507 
508 static struct i2c_board_info __initdata gsia18s_i2c_devices[] = {
509 	{ /* U1 on the GS_IA18-CB_V3 board */
510 		I2C_BOARD_INFO("pcf8574", 0x20),
511 		.platform_data = &pcf20_pdata,
512 	},
513 	{ /* U1 on the GS_2G_OPT1-A_V0 board (Alarm) */
514 		I2C_BOARD_INFO("pcf8574", 0x22),
515 		.platform_data = &pcf22_pdata,
516 	},
517 	{ /* U1 on the GS_2G-OPT23-A_V0 board (Modem) */
518 		I2C_BOARD_INFO("pcf8574", 0x24),
519 		.platform_data = &pcf24_pdata,
520 	},
521 	{ /* U161 on the GS_IA18_S-MN board */
522 		I2C_BOARD_INFO("24c1024", 0x50),
523 	},
524 	{ /* U162 on the GS_IA18_S-MN board */
525 		I2C_BOARD_INFO("24c01", 0x53),
526 	},
527 };
528 
529 /*
530  * Compact Flash
531  */
532 static struct at91_cf_data __initdata gsia18s_cf1_data = {
533 	.irq_pin	= AT91_PIN_PA27,
534 	.det_pin	= AT91_PIN_PB30,
535 	.vcc_pin	= -EINVAL,
536 	.rst_pin	= AT91_PIN_PB31,
537 	.chipselect	= 5,
538 	.flags		= AT91_CF_TRUE_IDE,
539 };
540 
541 /* Power Off by RTC */
gsia18s_power_off(void)542 static void gsia18s_power_off(void)
543 {
544 	pr_notice("Power supply will be switched off automatically now or after 60 seconds without ArmDAS.\n");
545 	at91_set_gpio_output(AT91_PIN_PA25, 1);
546 	/* Spin to death... */
547 	while (1)
548 		;
549 }
550 
gsia18s_power_off_init(void)551 static int __init gsia18s_power_off_init(void)
552 {
553 	pm_power_off = gsia18s_power_off;
554 	return 0;
555 }
556 
557 /* ---------------------------------------------------------------------------*/
558 
gsia18s_board_init(void)559 static void __init gsia18s_board_init(void)
560 {
561 	stamp9g20_board_init();
562 	at91_add_device_usbh(&usbh_data);
563 	at91_add_device_udc(&udc_data);
564 	at91_add_device_eth(&macb_data);
565 	gsia18s_leds_init();
566 	gsia18s_pcf_leds_init();
567 	gsia18s_add_device_buttons();
568 	at91_add_device_i2c(gsia18s_i2c_devices,
569 				ARRAY_SIZE(gsia18s_i2c_devices));
570 	at91_add_device_cf(&gsia18s_cf1_data);
571 	at91_add_device_spi(gsia18s_spi_devices,
572 				ARRAY_SIZE(gsia18s_spi_devices));
573 	gsia18s_power_off_init();
574 }
575 
576 MACHINE_START(GSIA18S, "GS_IA18_S")
577 	.timer		= &at91sam926x_timer,
578 	.map_io		= at91_map_io,
579 	.init_early	= gsia18s_init_early,
580 	.init_irq	= at91_init_irq_default,
581 	.init_machine	= gsia18s_board_init,
582 MACHINE_END
583