Merge branch 'for-2639/i2c/i2c-u2c12' into for-linus/2639/i2c-12
[pandora-kernel.git] / drivers / staging / olpc_dcon / olpc_dcon_xo_1_5.c
1 /*
2  * Copyright (c) 2009,2010       One Laptop per Child
3  *
4  * This program is free software.  You can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  */
8
9 #include <linux/acpi.h>
10 #include <linux/pci.h>
11 #include <linux/gpio.h>
12 #include <asm/olpc.h>
13
14 /* TODO: this eventually belongs in linux/vx855.h */
15 #define NR_VX855_GPI    14
16 #define NR_VX855_GPO    13
17 #define NR_VX855_GPIO   15
18
19 #define VX855_GPI(n)    (n)
20 #define VX855_GPO(n)    (NR_VX855_GPI + (n))
21 #define VX855_GPIO(n)   (NR_VX855_GPI + NR_VX855_GPO + (n))
22
23 #include "olpc_dcon.h"
24
25 /* Hardware setup on the XO 1.5:
26  *      DCONLOAD connects to
27  *              VX855_GPIO1 (not SMBCK2)
28  *      DCONBLANK connects to VX855_GPIO8 (not SSPICLK)  unused in driver
29  *      DCONSTAT0 connects to VX855_GPI10 (not SSPISDI)
30  *      DCONSTAT1 connects to VX855_GPI11 (not nSSPISS)
31  *      DCONIRQ connects to VX855_GPIO12
32  *      DCONSMBDATA connects to VX855 graphics CRTSPD
33  *      DCONSMBCLK connects to VX855 graphics CRTSPCLK
34  */
35
36 #define VX855_GENL_PURPOSE_OUTPUT 0x44c // PMIO_Rx4c-4f
37 #define VX855_GPI_STATUS_CHG 0x450  // PMIO_Rx50
38 #define VX855_GPI_SCI_SMI 0x452  // PMIO_Rx52
39 #define BIT_GPIO12 0x40
40
41 #define PREFIX "OLPC DCON:"
42
43 static void dcon_clear_irq(void)
44 {
45         /* irq status will appear in PMIO_Rx50[6] (RW1C) on gpio12 */
46         outb(BIT_GPIO12, VX855_GPI_STATUS_CHG);
47 }
48
49 static int dcon_was_irq(void)
50 {
51         u_int8_t tmp;
52
53         /* irq status will appear in PMIO_Rx50[6] on gpio12 */
54         tmp = inb(VX855_GPI_STATUS_CHG);
55         return !!(tmp & BIT_GPIO12);
56
57         return 0;
58 }
59
60 static int dcon_init_xo_1_5(struct dcon_priv *dcon)
61 {
62         unsigned int irq;
63         u_int8_t tmp;
64         struct pci_dev *pdev;
65         
66         
67         pdev = pci_get_device(PCI_VENDOR_ID_VIA,
68                               PCI_DEVICE_ID_VIA_VX855, NULL);
69         if (!pdev) {
70                 printk(KERN_ERR "cannot find VX855 PCI ID\n");
71                 return 1;
72         }
73
74         pci_read_config_byte(pdev, 0x95, &tmp);
75         pci_write_config_byte(pdev, 0x95, tmp|0x0c);
76
77         /* Set GPIO8 to GPIO mode, not SSPICLK */
78         pci_read_config_byte(pdev, 0xe3, &tmp);
79         pci_write_config_byte(pdev, 0xe3, tmp | 0x04);
80
81         /* Set GPI10/GPI11 to GPI mode, not SSPISDI/SSPISS */
82         pci_read_config_byte(pdev, 0xe4, &tmp);
83         pci_write_config_byte(pdev, 0xe4, tmp|0x08);
84
85         /* clear PMU_RxE1[6] to select SCI on GPIO12 */
86         /* clear PMU_RxE0[6] to choose falling edge */
87         pci_read_config_byte(pdev, 0xe1, &tmp);
88         pci_write_config_byte(pdev, 0xe1, tmp & ~BIT_GPIO12);
89         pci_read_config_byte(pdev, 0xe0, &tmp);
90         pci_write_config_byte(pdev, 0xe0, tmp & ~BIT_GPIO12);
91
92         dcon_clear_irq();
93
94         /* set   PMIO_Rx52[6] to enable SCI/SMI on gpio12 */
95         outb(inb(VX855_GPI_SCI_SMI)|BIT_GPIO12, VX855_GPI_SCI_SMI);
96
97         /* Determine the current state of DCONLOAD, likely set by firmware */
98         /* GPIO1 */
99         dcon->curr_src = (inl(VX855_GENL_PURPOSE_OUTPUT) & 0x1000) ?
100                         DCON_SOURCE_CPU : DCON_SOURCE_DCON;
101         dcon->pending_src = dcon->curr_src;
102
103         pci_dev_put(pdev);
104
105         /* we're sharing the IRQ with ACPI */
106         irq = acpi_gbl_FADT.sci_interrupt;
107         if (request_irq(irq, &dcon_interrupt, IRQF_SHARED, "DCON", dcon)) {
108                 printk(KERN_ERR PREFIX "DCON (IRQ%d) allocation failed\n", irq);
109                 return 1;
110         }
111
112         return 0;
113 }
114
115 static void set_i2c_line(int sda, int scl)
116 {
117         unsigned char tmp;
118         unsigned int port = 0x26;
119
120         /* FIXME: This directly accesses the CRT GPIO controller !!! */
121         outb(port, 0x3c4);
122         tmp = inb(0x3c5);
123
124         if (scl)
125                 tmp |= 0x20;
126         else
127                 tmp &= ~0x20;
128
129         if (sda)
130                 tmp |= 0x10;
131         else
132                 tmp &= ~0x10;
133
134         tmp |= 0x01;
135
136         outb(port, 0x3c4);
137         outb(tmp, 0x3c5);
138 }
139
140
141 static void dcon_wiggle_xo_1_5(void)
142 {
143         int x;
144
145         /*
146          * According to HiMax, when powering the DCON up we should hold
147          * SMB_DATA high for 8 SMB_CLK cycles.  This will force the DCON
148          * state machine to reset to a (sane) initial state.  Mitch Bradley
149          * did some testing and discovered that holding for 16 SMB_CLK cycles
150          * worked a lot more reliably, so that's what we do here.
151          */
152         set_i2c_line(1, 1);
153
154         for (x = 0; x < 16; x++) {
155                 udelay(5);
156                 set_i2c_line(1, 0);
157                 udelay(5);
158                 set_i2c_line(1, 1);
159         }
160         udelay(5);
161
162         /* set   PMIO_Rx52[6] to enable SCI/SMI on gpio12 */
163         outb(inb(VX855_GPI_SCI_SMI)|BIT_GPIO12, VX855_GPI_SCI_SMI);
164 }
165
166 static void dcon_set_dconload_xo_1_5(int val)
167 {
168         gpio_set_value(VX855_GPIO(1), val);
169 }
170
171 static u8 dcon_read_status_xo_1_5(void)
172 {
173         u8 status;
174         
175         if (!dcon_was_irq())
176                 return -1;
177
178         // i believe this is the same as "inb(0x44b) & 3"
179         status = gpio_get_value(VX855_GPI(10));
180         status |= gpio_get_value(VX855_GPI(11)) << 1; 
181
182         dcon_clear_irq();
183
184         return status;
185 }
186
187 struct dcon_platform_data dcon_pdata_xo_1_5 = {
188         .init = dcon_init_xo_1_5,
189         .bus_stabilize_wiggle = dcon_wiggle_xo_1_5,
190         .set_dconload = dcon_set_dconload_xo_1_5,
191         .read_status = dcon_read_status_xo_1_5,
192 };