blob: c87fdfac4855302c57d0d13d5c82b15487eb9382 [file] [log] [blame]
Andres Salomoneecb3e42010-09-24 19:13:42 -07001/*
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 Salomon7637c922011-01-12 17:00:11 -080013#include <linux/cs5535.h>
14#include <linux/gpio.h>
Jeff Mahoney97232fd2011-04-12 14:55:11 -040015#include <linux/delay.h>
Andres Salomoneecb3e42010-09-24 19:13:42 -070016#include <asm/olpc.h>
17
18#include "olpc_dcon.h"
19
Andres Salomonbbe963f2011-02-10 17:54:24 -080020static int dcon_init_xo_1(struct dcon_priv *dcon)
Andres Salomoneecb3e42010-09-24 19:13:42 -070021{
Andres Salomoneecb3e42010-09-24 19:13:42 -070022 unsigned char lob;
23
Andres Salomon7637c922011-01-12 17:00:11 -080024 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 Salomoneecb3e42010-09-24 19:13:42 -070043 }
44
Andres Salomoneecb3e42010-09-24 19:13:42 -070045 /* Turn off the event enable for GPIO7 just to be safe */
Andres Salomon7637c922011-01-12 17:00:11 -080046 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 Salomonbbe963f2011-02-10 17:54:24 -080058 dcon->curr_src = cs5535_gpio_isset(OLPC_GPIO_DCON_LOAD, GPIO_OUTPUT_VAL)
Andres Salomon7637c922011-01-12 17:00:11 -080059 ? DCON_SOURCE_CPU
60 : DCON_SOURCE_DCON;
Andres Salomonbbe963f2011-02-10 17:54:24 -080061 dcon->pending_src = dcon->curr_src;
Andres Salomoneecb3e42010-09-24 19:13:42 -070062
63 /* Set the directions for the GPIO pins */
Andres Salomon7637c922011-01-12 17:00:11 -080064 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 Salomonbbe963f2011-02-10 17:54:24 -080069 dcon->curr_src == DCON_SOURCE_CPU);
Andres Salomoneecb3e42010-09-24 19:13:42 -070070
71 /* Set up the interrupt mappings */
72
73 /* Set the IRQ to pair 2 */
Andres Salomon7637c922011-01-12 17:00:11 -080074 cs5535_gpio_setup_event(OLPC_GPIO_DCON_IRQ, 2, 0);
Andres Salomoneecb3e42010-09-24 19:13:42 -070075
76 /* Enable group 2 to trigger the DCON interrupt */
Andres Salomon7637c922011-01-12 17:00:11 -080077 cs5535_gpio_set_irq(2, DCON_IRQ);
Andres Salomoneecb3e42010-09-24 19:13:42 -070078
79 /* Select edge level for interrupt (in PIC) */
80 lob = inb(0x4d0);
81 lob &= ~(1 << DCON_IRQ);
82 outb(lob, 0x4d0);
83
Lucas De Marchi25985ed2011-03-30 22:57:33 -030084 /* Register the interrupt handler */
Andres Salomonbbe963f2011-02-10 17:54:24 -080085 if (request_irq(DCON_IRQ, &dcon_interrupt, 0, "DCON", dcon)) {
Andres Salomon7637c922011-01-12 17:00:11 -080086 printk(KERN_ERR "olpc-dcon: failed to request DCON's irq\n");
87 goto err_req_irq;
88 }
Andres Salomoneecb3e42010-09-24 19:13:42 -070089
90 /* Clear INV_EN for GPIO7 (DCONIRQ) */
Andres Salomon7637c922011-01-12 17:00:11 -080091 cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_INPUT_INVERT);
Andres Salomoneecb3e42010-09-24 19:13:42 -070092
93 /* Enable filter for GPIO12 (DCONBLANK) */
Andres Salomon7637c922011-01-12 17:00:11 -080094 cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_INPUT_FILTER);
Andres Salomoneecb3e42010-09-24 19:13:42 -070095
96 /* Disable filter for GPIO7 */
Andres Salomon7637c922011-01-12 17:00:11 -080097 cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_INPUT_FILTER);
Andres Salomoneecb3e42010-09-24 19:13:42 -070098
99 /* Disable event counter for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */
Andres Salomon7637c922011-01-12 17:00:11 -0800100 cs5535_gpio_clear(OLPC_GPIO_DCON_IRQ, GPIO_INPUT_EVENT_COUNT);
101 cs5535_gpio_clear(OLPC_GPIO_DCON_BLANK, GPIO_INPUT_EVENT_COUNT);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700102
103 /* Add GPIO12 to the Filter Event Pair #7 */
Andres Salomon7637c922011-01-12 17:00:11 -0800104 cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_FE7_SEL);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700105
106 /* Turn off negative Edge Enable for GPIO12 */
Andres Salomon7637c922011-01-12 17:00:11 -0800107 cs5535_gpio_clear(OLPC_GPIO_DCON_BLANK, GPIO_NEGATIVE_EDGE_EN);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700108
109 /* Enable negative Edge Enable for GPIO7 */
Andres Salomon7637c922011-01-12 17:00:11 -0800110 cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_NEGATIVE_EDGE_EN);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700111
112 /* Zero the filter amount for Filter Event Pair #7 */
Andres Salomon7637c922011-01-12 17:00:11 -0800113 cs5535_gpio_set(0, GPIO_FLTR7_AMOUNT);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700114
115 /* Clear the negative edge status for GPIO7 and GPIO12 */
Andres Salomon7637c922011-01-12 17:00:11 -0800116 cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_NEGATIVE_EDGE_STS);
117 cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_NEGATIVE_EDGE_STS);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700118
Justin P. Mattock5d252872012-03-19 08:17:51 -0700119 /* FIXME: Clear the positive status as well, just to be sure */
Andres Salomon7637c922011-01-12 17:00:11 -0800120 cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_POSITIVE_EDGE_STS);
121 cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_POSITIVE_EDGE_STS);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700122
123 /* Enable events for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */
Andres Salomon7637c922011-01-12 17:00:11 -0800124 cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_EVENTS_ENABLE);
125 cs5535_gpio_set(OLPC_GPIO_DCON_BLANK, GPIO_EVENTS_ENABLE);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700126
127 return 0;
Andres Salomon7637c922011-01-12 17:00:11 -0800128
129err_req_irq:
130 gpio_free(OLPC_GPIO_DCON_BLANK);
131err_gp_blank:
132 gpio_free(OLPC_GPIO_DCON_LOAD);
133err_gp_load:
134 gpio_free(OLPC_GPIO_DCON_IRQ);
135err_gp_irq:
136 gpio_free(OLPC_GPIO_DCON_STAT1);
137err_gp_stat1:
138 gpio_free(OLPC_GPIO_DCON_STAT0);
139 return -EIO;
Andres Salomoneecb3e42010-09-24 19:13:42 -0700140}
141
142static 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ählera3712f42011-04-12 19:53:31 +0200156 */
Andres Salomon7637c922011-01-12 17:00:11 -0800157 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 Salomoneecb3e42010-09-24 19:13:42 -0700167
168 for (x = 0; x < 16; x++) {
169 udelay(5);
Andres Salomon7637c922011-01-12 17:00:11 -0800170 cs5535_gpio_clear(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_VAL);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700171 udelay(5);
Andres Salomon7637c922011-01-12 17:00:11 -0800172 cs5535_gpio_set(OLPC_GPIO_SMB_CLK, GPIO_OUTPUT_VAL);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700173 }
174 udelay(5);
Andres Salomon7637c922011-01-12 17:00:11 -0800175 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 Salomoneecb3e42010-09-24 19:13:42 -0700179}
180
181static void dcon_set_dconload_1(int val)
182{
Andres Salomon7637c922011-01-12 17:00:11 -0800183 gpio_set_value(OLPC_GPIO_DCON_LOAD, val);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700184}
185
Xi Wang91762052011-12-02 16:28:43 -0500186static int dcon_read_status_xo_1(u8 *status)
Andres Salomoneecb3e42010-09-24 19:13:42 -0700187{
Xi Wang91762052011-12-02 16:28:43 -0500188 *status = gpio_get_value(OLPC_GPIO_DCON_STAT0);
189 *status |= gpio_get_value(OLPC_GPIO_DCON_STAT1) << 1;
Andres Salomon7637c922011-01-12 17:00:11 -0800190
Andres Salomoneecb3e42010-09-24 19:13:42 -0700191 /* Clear the negative edge status for GPIO7 */
Andres Salomon7637c922011-01-12 17:00:11 -0800192 cs5535_gpio_set(OLPC_GPIO_DCON_IRQ, GPIO_NEGATIVE_EDGE_STS);
Andres Salomoneecb3e42010-09-24 19:13:42 -0700193
Xi Wang91762052011-12-02 16:28:43 -0500194 return 0;
Andres Salomoneecb3e42010-09-24 19:13:42 -0700195}
196
Andres Salomon097cd832011-02-10 17:53:24 -0800197struct dcon_platform_data dcon_pdata_xo_1 = {
Andres Salomoneecb3e42010-09-24 19:13:42 -0700198 .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};