1 /*
2  * Mainly by David Woodhouse, somewhat modified by Jordan Crouse
3  *
4  * Copyright © 2006-2007  Red Hat, Inc.
5  * Copyright © 2006-2007  Advanced Micro Devices, Inc.
6  * Copyright © 2009       VIA Technology, Inc.
7  * Copyright (c) 2010  Andres Salomon <dilinger@queued.net>
8  *
9  * This program is free software.  You can redistribute it and/or
10  * modify it under the terms of version 2 of the GNU General Public
11  * License as published by the Free Software Foundation.
12  */
13 #include <linux/cs5535.h>
14 #include <linux/gpio.h>
15 #include <linux/delay.h>
16 #include <asm/olpc.h>
17 
18 #include "olpc_dcon.h"
19 
dcon_init_xo_1(struct dcon_priv * dcon)20 static int dcon_init_xo_1(struct dcon_priv *dcon)
21 {
22 	unsigned char lob;
23 
24 	if (gpio_request(OLPC_GPIO_DCON_STAT0, "OLPC-DCON")) {
25 		printk(KERN_ERR "olpc-dcon: failed to request STAT0 GPIO\n");
26 		return -EIO;
27 	}
28 	if (gpio_request(OLPC_GPIO_DCON_STAT1, "OLPC-DCON")) {
29 		printk(KERN_ERR "olpc-dcon: failed to request STAT1 GPIO\n");
30 		goto err_gp_stat1;
31 	}
32 	if (gpio_request(OLPC_GPIO_DCON_IRQ, "OLPC-DCON")) {
33 		printk(KERN_ERR "olpc-dcon: failed to request IRQ GPIO\n");
34 		goto err_gp_irq;
35 	}
36 	if (gpio_request(OLPC_GPIO_DCON_LOAD, "OLPC-DCON")) {
37 		printk(KERN_ERR "olpc-dcon: failed to request LOAD GPIO\n");
38 		goto err_gp_load;
39 	}
40 	if (gpio_request(OLPC_GPIO_DCON_BLANK, "OLPC-DCON")) {
41 		printk(KERN_ERR "olpc-dcon: failed to request BLANK GPIO\n");
42 		goto err_gp_blank;
43 	}
44 
45 	/* Turn off the event enable for GPIO7 just to be safe */
46 	cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_EVENTS_ENABLE);
47 
48 	/*
49 	 * Determine the current state by reading the GPIO bit; earlier
50 	 * stages of the boot process have established the state.
51 	 *
52 	 * Note that we read GPIO_OUPUT_VAL rather than GPIO_READ_BACK here;
53 	 * this is because OFW will disable input for the pin and set a value..
54 	 * READ_BACK will only contain a valid value if input is enabled and
55 	 * then a value is set.  So, future readings of the pin can use
56 	 * READ_BACK, but the first one cannot.  Awesome, huh?
57 	 */
58 	dcon->curr_src = cs5535_gpio_isset(OLPC_GPIO_DCON_LOAD, GPIO_OUTPUT_VAL)
59 		? DCON_SOURCE_CPU
60 		: DCON_SOURCE_DCON;
61 	dcon->pending_src = dcon->curr_src;
62 
63 	/* Set the directions for the GPIO pins */
64 	gpio_direction_input(OLPC_GPIO_DCON_STAT0);
65 	gpio_direction_input(OLPC_GPIO_DCON_STAT1);
66 	gpio_direction_input(OLPC_GPIO_DCON_IRQ);
67 	gpio_direction_input(OLPC_GPIO_DCON_BLANK);
68 	gpio_direction_output(OLPC_GPIO_DCON_LOAD,
69 			dcon->curr_src == DCON_SOURCE_CPU);
70 
71 	/* Set up the interrupt mappings */
72 
73 	/* Set the IRQ to pair 2 */
74 	cs5535_gpio_setup_event(OLPC_GPIO_DCON_IRQ, 2, 0);
75 
76 	/* Enable group 2 to trigger the DCON interrupt */
77 	cs5535_gpio_set_irq(2, DCON_IRQ);
78 
79 	/* Select edge level for interrupt (in PIC) */
80 	lob = inb(0x4d0);
81 	lob &= ~(1 << DCON_IRQ);
82 	outb(lob, 0x4d0);
83 
84 	/* Register the interrupt handler */
85 	if (request_irq(DCON_IRQ, &dcon_interrupt, 0, "DCON", dcon)) {
86 		printk(KERN_ERR "olpc-dcon: failed to request DCON's irq\n");
87 		goto err_req_irq;
88 	}
89 
90 	/* Clear INV_EN for GPIO7 (DCONIRQ) */
91 	cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_INPUT_INVERT);
92 
93 	/* Enable filter for GPIO12 (DCONBLANK) */
94 	cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_INPUT_FILTER);
95 
96 	/* Disable filter for GPIO7 */
97 	cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_INPUT_FILTER);
98 
99 	/* Disable event counter for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */
100 	cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_INPUT_EVENT_COUNT);
101 	cs5535_gpio_clear(OLPC_GPIO_DCON_BLANK, GPIO_INPUT_EVENT_COUNT);
102 
103 	/* Add GPIO12 to the Filter Event Pair #7 */
104 	cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_FE7_SEL);
105 
106 	/* Turn off negative Edge Enable for GPIO12 */
107 	cs5535_gpio_clear(OLPC_GPIO_DCON_BLANK, GPIO_NEGATIVE_EDGE_EN);
108 
109 	/* Enable negative Edge Enable for GPIO7 */
110 	cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_NEGATIVE_EDGE_EN);
111 
112 	/* Zero the filter amount for Filter Event Pair #7 */
113 	cs5535_gpio_set(0, GPIO_FLTR7_AMOUNT);
114 
115 	/* Clear the negative edge status for GPIO7 and GPIO12 */
116 	cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_NEGATIVE_EDGE_STS);
117 	cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_NEGATIVE_EDGE_STS);
118 
119 	/* FIXME:  Clear the posiitive status as well, just to be sure */
120 	cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_POSITIVE_EDGE_STS);
121 	cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_POSITIVE_EDGE_STS);
122 
123 	/* Enable events for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */
124 	cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_EVENTS_ENABLE);
125 	cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_EVENTS_ENABLE);
126 
127 	return 0;
128 
129 err_req_irq:
130 	gpio_free(OLPC_GPIO_DCON_BLANK);
131 err_gp_blank:
132 	gpio_free(OLPC_GPIO_DCON_LOAD);
133 err_gp_load:
134 	gpio_free(OLPC_GPIO_DCON_IRQ);
135 err_gp_irq:
136 	gpio_free(OLPC_GPIO_DCON_STAT1);
137 err_gp_stat1:
138 	gpio_free(OLPC_GPIO_DCON_STAT0);
139 	return -EIO;
140 }
141 
dcon_wiggle_xo_1(void)142 static void dcon_wiggle_xo_1(void)
143 {
144 	int x;
145 
146 	/*
147 	 * According to HiMax, when powering the DCON up we should hold
148 	 * SMB_DATA high for 8 SMB_CLK cycles.  This will force the DCON
149 	 * state machine to reset to a (sane) initial state.  Mitch Bradley
150 	 * did some testing and discovered that holding for 16 SMB_CLK cycles
151 	 * worked a lot more reliably, so that's what we do here.
152 	 *
153 	 * According to the cs5536 spec, to set GPIO14 to SMB_CLK we must
154 	 * simultaneously set AUX1 IN/OUT to GPIO14; ditto for SMB_DATA and
155 	 * GPIO15.
156 	 */
157 	cs5535_gpio_set(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_VAL);
158 	cs5535_gpio_set(OLPC_GPIO_SMB_DATA, GPIO_OUTPUT_VAL);
159 	cs5535_gpio_set(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_ENABLE);
160 	cs5535_gpio_set(OLPC_GPIO_SMB_DATA, GPIO_OUTPUT_ENABLE);
161 	cs5535_gpio_clear(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_AUX1);
162 	cs5535_gpio_clear(OLPC_GPIO_SMB_DATA, GPIO_OUTPUT_AUX1);
163 	cs5535_gpio_clear(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_AUX2);
164 	cs5535_gpio_clear(OLPC_GPIO_SMB_DATA, GPIO_OUTPUT_AUX2);
165 	cs5535_gpio_clear(OLPC_GPIO_SMB_CLK, GPIO_INPUT_AUX1);
166 	cs5535_gpio_clear(OLPC_GPIO_SMB_DATA, GPIO_INPUT_AUX1);
167 
168 	for (x = 0; x < 16; x++) {
169 		udelay(5);
170 		cs5535_gpio_clear(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_VAL);
171 		udelay(5);
172 		cs5535_gpio_set(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_VAL);
173 	}
174 	udelay(5);
175 	cs5535_gpio_set(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_AUX1);
176 	cs5535_gpio_set(OLPC_GPIO_SMB_DATA, GPIO_OUTPUT_AUX1);
177 	cs5535_gpio_set(OLPC_GPIO_SMB_CLK, GPIO_INPUT_AUX1);
178 	cs5535_gpio_set(OLPC_GPIO_SMB_DATA, GPIO_INPUT_AUX1);
179 }
180 
dcon_set_dconload_1(int val)181 static void dcon_set_dconload_1(int val)
182 {
183 	gpio_set_value(OLPC_GPIO_DCON_LOAD, val);
184 }
185 
dcon_read_status_xo_1(u8 * status)186 static int dcon_read_status_xo_1(u8 *status)
187 {
188 	*status = gpio_get_value(OLPC_GPIO_DCON_STAT0);
189 	*status |= gpio_get_value(OLPC_GPIO_DCON_STAT1) << 1;
190 
191 	/* Clear the negative edge status for GPIO7 */
192 	cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_NEGATIVE_EDGE_STS);
193 
194 	return 0;
195 }
196 
197 struct dcon_platform_data dcon_pdata_xo_1 = {
198 	.init = dcon_init_xo_1,
199 	.bus_stabilize_wiggle = dcon_wiggle_xo_1,
200 	.set_dconload = dcon_set_dconload_1,
201 	.read_status = dcon_read_status_xo_1,
202 };
203