viafb: Convert GPIO and i2c to the new indexed port ops
[pandora-kernel.git] / drivers / video / via / via_i2c.c
1 /*
2  * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
3  * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
4
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License as published by the Free Software Foundation;
8  * either version 2, or (at your option) any later version.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTIES OR REPRESENTATIONS; without even
12  * the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE.See the GNU General Public License
14  * for more details.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc.,
19  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #include "via-core.h"
23 #include "via_i2c.h"
24 #include "global.h"
25
26 /*
27  * There can only be one set of these, so there's no point in having
28  * them be dynamically allocated...
29  */
30 #define VIAFB_NUM_I2C           5
31 static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
32 struct viafb_dev *i2c_vdev;  /* Passed in from core */
33
34 static void via_i2c_setscl(void *data, int state)
35 {
36         u8 val;
37         struct via_port_cfg *adap_data = data;
38         unsigned long flags;
39
40         spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
41         val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
42         if (state)
43                 val |= 0x20;
44         else
45                 val &= ~0x20;
46         switch (adap_data->type) {
47         case VIA_PORT_I2C:
48                 val |= 0x01;
49                 break;
50         case VIA_PORT_GPIO:
51                 val |= 0x80;
52                 break;
53         default:
54                 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
55         }
56         via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
57         spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
58 }
59
60 static int via_i2c_getscl(void *data)
61 {
62         struct via_port_cfg *adap_data = data;
63         unsigned long flags;
64         int ret = 0;
65
66         spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
67         if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
68                 ret = 1;
69         spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
70         return ret;
71 }
72
73 static int via_i2c_getsda(void *data)
74 {
75         struct via_port_cfg *adap_data = data;
76         unsigned long flags;
77         int ret = 0;
78
79         spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
80         if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
81                 ret = 1;
82         spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
83         return ret;
84 }
85
86 static void via_i2c_setsda(void *data, int state)
87 {
88         u8 val;
89         struct via_port_cfg *adap_data = data;
90         unsigned long flags;
91
92         spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
93         val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
94         if (state)
95                 val |= 0x10;
96         else
97                 val &= ~0x10;
98         switch (adap_data->type) {
99         case VIA_PORT_I2C:
100                 val |= 0x01;
101                 break;
102         case VIA_PORT_GPIO:
103                 val |= 0x40;
104                 break;
105         default:
106                 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
107         }
108         via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
109         spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
110 }
111
112 int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
113 {
114         u8 mm1[] = {0x00};
115         struct i2c_msg msgs[2];
116
117         *pdata = 0;
118         msgs[0].flags = 0;
119         msgs[1].flags = I2C_M_RD;
120         msgs[0].addr = msgs[1].addr = slave_addr / 2;
121         mm1[0] = index;
122         msgs[0].len = 1; msgs[1].len = 1;
123         msgs[0].buf = mm1; msgs[1].buf = pdata;
124         return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
125 }
126
127 int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
128 {
129         u8 msg[2] = { index, data };
130         struct i2c_msg msgs;
131
132         msgs.flags = 0;
133         msgs.addr = slave_addr / 2;
134         msgs.len = 2;
135         msgs.buf = msg;
136         return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
137 }
138
139 int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
140 {
141         u8 mm1[] = {0x00};
142         struct i2c_msg msgs[2];
143
144         msgs[0].flags = 0;
145         msgs[1].flags = I2C_M_RD;
146         msgs[0].addr = msgs[1].addr = slave_addr / 2;
147         mm1[0] = index;
148         msgs[0].len = 1; msgs[1].len = buff_len;
149         msgs[0].buf = mm1; msgs[1].buf = buff;
150         return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
151 }
152
153 static int create_i2c_bus(struct i2c_adapter *adapter,
154                           struct i2c_algo_bit_data *algo,
155                           struct via_port_cfg *adap_cfg,
156                           struct pci_dev *pdev)
157 {
158         DEBUG_MSG(KERN_DEBUG "viafb: creating bus adap=0x%p, algo_bit_data=0x%p, adap_cfg=0x%p\n", adapter, algo, adap_cfg);
159
160         algo->setsda = via_i2c_setsda;
161         algo->setscl = via_i2c_setscl;
162         algo->getsda = via_i2c_getsda;
163         algo->getscl = via_i2c_getscl;
164         algo->udelay = 40;
165         algo->timeout = 20;
166         algo->data = adap_cfg;
167
168         sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
169                 adap_cfg->ioport_index);
170         adapter->owner = THIS_MODULE;
171         adapter->id = 0x01FFFF;
172         adapter->class = I2C_CLASS_DDC;
173         adapter->algo_data = algo;
174         if (pdev)
175                 adapter->dev.parent = &pdev->dev;
176         else
177                 adapter->dev.parent = NULL;
178         /* i2c_set_adapdata(adapter, adap_cfg); */
179
180         /* Raise SCL and SDA */
181         via_i2c_setsda(adap_cfg, 1);
182         via_i2c_setscl(adap_cfg, 1);
183         udelay(20);
184
185         return i2c_bit_add_bus(adapter);
186 }
187
188 int viafb_create_i2c_busses(struct viafb_dev *dev, struct via_port_cfg *configs)
189 {
190         int i, ret;
191
192         i2c_vdev = dev;
193         for (i = 0; i < VIAFB_NUM_PORTS; i++) {
194                 struct via_port_cfg *adap_cfg = configs++;
195                 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
196
197                 if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
198                         continue;
199
200                 ret = create_i2c_bus(&i2c_stuff->adapter,
201                                      &i2c_stuff->algo, adap_cfg,
202                                 NULL); /* FIXME: PCIDEV */
203                 if (ret < 0) {
204                         printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
205                                 i, ret);
206                         /* FIXME: properly release previous busses */
207                         return ret;
208                 }
209         }
210
211         return 0;
212 }
213
214 void viafb_delete_i2c_busses(void)
215 {
216         int i;
217
218         for (i = 0; i < VIAFB_NUM_PORTS; i++) {
219                 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
220                 /*
221                  * Only remove those entries in the array that we've
222                  * actually used (and thus initialized algo_data)
223                  */
224                 if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
225                         i2c_del_adapter(&i2c_stuff->adapter);
226         }
227 }