Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 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 | */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 13 | #include <linux/cs5535.h> |
| 14 | #include <linux/gpio.h> |
Jeff Mahoney | 97232fd | 2011-04-12 14:55:11 -0400 | [diff] [blame] | 15 | #include <linux/delay.h> |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 16 | #include <asm/olpc.h> |
| 17 | |
| 18 | #include "olpc_dcon.h" |
| 19 | |
Andres Salomon | bbe963f | 2011-02-10 17:54:24 -0800 | [diff] [blame] | 20 | static int dcon_init_xo_1(struct dcon_priv *dcon) |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 21 | { |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 22 | unsigned char lob; |
| 23 | |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 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; |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 45 | /* Turn off the event enable for GPIO7 just to be safe */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 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 | */ |
Andres Salomon | bbe963f | 2011-02-10 17:54:24 -0800 | [diff] [blame] | 58 | dcon->curr_src = cs5535_gpio_isset(OLPC_GPIO_DCON_LOAD, GPIO_OUTPUT_VAL) |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 59 | ? DCON_SOURCE_CPU |
| 60 | : DCON_SOURCE_DCON; |
Andres Salomon | bbe963f | 2011-02-10 17:54:24 -0800 | [diff] [blame] | 61 | dcon->pending_src = dcon->curr_src; |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 62 | |
| 63 | /* Set the directions for the GPIO pins */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 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, |
Andres Salomon | bbe963f | 2011-02-10 17:54:24 -0800 | [diff] [blame] | 69 | dcon->curr_src == DCON_SOURCE_CPU); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 70 | |
| 71 | /* Set up the interrupt mappings */ |
| 72 | |
| 73 | /* Set the IRQ to pair 2 */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 74 | cs5535_gpio_setup_event(OLPC_GPIO_DCON_IRQ, 2, 0); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 75 | |
| 76 | /* Enable group 2 to trigger the DCON interrupt */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 77 | cs5535_gpio_set_irq(2, DCON_IRQ); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 78 | |
| 79 | /* Select edge level for interrupt (in PIC) */ |
| 80 | lob = inb(0x4d0); |
| 81 | lob &= ~(1 << DCON_IRQ); |
| 82 | outb(lob, 0x4d0); |
| 83 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 84 | /* Register the interrupt handler */ |
Andres Salomon | bbe963f | 2011-02-10 17:54:24 -0800 | [diff] [blame] | 85 | if (request_irq(DCON_IRQ, &dcon_interrupt, 0, "DCON", dcon)) { |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 86 | printk(KERN_ERR "olpc-dcon: failed to request DCON's irq\n"); |
| 87 | goto err_req_irq; |
| 88 | } |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 89 | |
| 90 | /* Clear INV_EN for GPIO7 (DCONIRQ) */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 91 | cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_INPUT_INVERT); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 92 | |
| 93 | /* Enable filter for GPIO12 (DCONBLANK) */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 94 | cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_INPUT_FILTER); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 95 | |
| 96 | /* Disable filter for GPIO7 */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 97 | cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_INPUT_FILTER); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 98 | |
| 99 | /* Disable event counter for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 100 | cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_INPUT_EVENT_COUNT); |
| 101 | cs5535_gpio_clear(OLPC_GPIO_DCON_BLANK, GPIO_INPUT_EVENT_COUNT); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 102 | |
| 103 | /* Add GPIO12 to the Filter Event Pair #7 */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 104 | cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_FE7_SEL); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 105 | |
| 106 | /* Turn off negative Edge Enable for GPIO12 */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 107 | cs5535_gpio_clear(OLPC_GPIO_DCON_BLANK, GPIO_NEGATIVE_EDGE_EN); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 108 | |
| 109 | /* Enable negative Edge Enable for GPIO7 */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 110 | cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_NEGATIVE_EDGE_EN); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 111 | |
| 112 | /* Zero the filter amount for Filter Event Pair #7 */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 113 | cs5535_gpio_set(0, GPIO_FLTR7_AMOUNT); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 114 | |
| 115 | /* Clear the negative edge status for GPIO7 and GPIO12 */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 116 | cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_NEGATIVE_EDGE_STS); |
| 117 | cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_NEGATIVE_EDGE_STS); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 118 | |
| 119 | /* FIXME: Clear the posiitive status as well, just to be sure */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 120 | cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_POSITIVE_EDGE_STS); |
| 121 | cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_POSITIVE_EDGE_STS); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 122 | |
| 123 | /* Enable events for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 124 | cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_EVENTS_ENABLE); |
| 125 | cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_EVENTS_ENABLE); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 126 | |
| 127 | return 0; |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 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; |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 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. |
Stefan Brähler | a3712f4 | 2011-04-12 19:53:31 +0200 | [diff] [blame] | 156 | */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 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); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 167 | |
| 168 | for (x = 0; x < 16; x++) { |
| 169 | udelay(5); |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 170 | cs5535_gpio_clear(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_VAL); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 171 | udelay(5); |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 172 | cs5535_gpio_set(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_VAL); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 173 | } |
| 174 | udelay(5); |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 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); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void dcon_set_dconload_1(int val) |
| 182 | { |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 183 | gpio_set_value(OLPC_GPIO_DCON_LOAD, val); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Xi Wang | 9176205 | 2011-12-02 16:28:43 -0500 | [diff] [blame^] | 186 | static int dcon_read_status_xo_1(u8 *status) |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 187 | { |
Xi Wang | 9176205 | 2011-12-02 16:28:43 -0500 | [diff] [blame^] | 188 | *status = gpio_get_value(OLPC_GPIO_DCON_STAT0); |
| 189 | *status |= gpio_get_value(OLPC_GPIO_DCON_STAT1) << 1; |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 190 | |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 191 | /* Clear the negative edge status for GPIO7 */ |
Andres Salomon | 7637c92 | 2011-01-12 17:00:11 -0800 | [diff] [blame] | 192 | cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_NEGATIVE_EDGE_STS); |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 193 | |
Xi Wang | 9176205 | 2011-12-02 16:28:43 -0500 | [diff] [blame^] | 194 | return 0; |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Andres Salomon | 097cd83 | 2011-02-10 17:53:24 -0800 | [diff] [blame] | 197 | struct dcon_platform_data dcon_pdata_xo_1 = { |
Andres Salomon | eecb3e4 | 2010-09-24 19:13:42 -0700 | [diff] [blame] | 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 | }; |