Merge git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-2.6-mn10300
[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 <linux/platform_device.h>
23 #include <linux/delay.h>
24 #include <linux/spinlock.h>
25 #include <linux/module.h>
26 #include <linux/via-core.h>
27 #include <linux/via_i2c.h>
28
29 /*
30  * There can only be one set of these, so there's no point in having
31  * them be dynamically allocated...
32  */
33 #define VIAFB_NUM_I2C           5
34 static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
35 struct viafb_dev *i2c_vdev;  /* Passed in from core */
36
37 static void via_i2c_setscl(void *data, int state)
38 {
39         u8 val;
40         struct via_port_cfg *adap_data = data;
41         unsigned long flags;
42
43         spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
44         val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
45         if (state)
46                 val |= 0x20;
47         else
48                 val &= ~0x20;
49         switch (adap_data->type) {
50         case VIA_PORT_I2C:
51                 val |= 0x01;
52                 break;
53         case VIA_PORT_GPIO:
54                 val |= 0x80;
55                 break;
56         default:
57                 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
58         }
59         via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
60         spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
61 }
62
63 static int via_i2c_getscl(void *data)
64 {
65         struct via_port_cfg *adap_data = data;
66         unsigned long flags;
67         int ret = 0;
68
69         spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
70         if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
71                 ret = 1;
72         spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
73         return ret;
74 }
75
76 static int via_i2c_getsda(void *data)
77 {
78         struct via_port_cfg *adap_data = data;
79         unsigned long flags;
80         int ret = 0;
81
82         spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
83         if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
84                 ret = 1;
85         spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
86         return ret;
87 }
88
89 static void via_i2c_setsda(void *data, int state)
90 {
91         u8 val;
92         struct via_port_cfg *adap_data = data;
93         unsigned long flags;
94
95         spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
96         val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
97         if (state)
98                 val |= 0x10;
99         else
100                 val &= ~0x10;
101         switch (adap_data->type) {
102         case VIA_PORT_I2C:
103                 val |= 0x01;
104                 break;
105         case VIA_PORT_GPIO:
106                 val |= 0x40;
107                 break;
108         default:
109                 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
110         }
111         via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
112         spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
113 }
114
115 int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
116 {
117         u8 mm1[] = {0x00};
118         struct i2c_msg msgs[2];
119
120         if (!via_i2c_par[adap].is_active)
121                 return -ENODEV;
122         *pdata = 0;
123         msgs[0].flags = 0;
124         msgs[1].flags = I2C_M_RD;
125         msgs[0].addr = msgs[1].addr = slave_addr / 2;
126         mm1[0] = index;
127         msgs[0].len = 1; msgs[1].len = 1;
128         msgs[0].buf = mm1; msgs[1].buf = pdata;
129         return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
130 }
131
132 int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
133 {
134         u8 msg[2] = { index, data };
135         struct i2c_msg msgs;
136
137         if (!via_i2c_par[adap].is_active)
138                 return -ENODEV;
139         msgs.flags = 0;
140         msgs.addr = slave_addr / 2;
141         msgs.len = 2;
142         msgs.buf = msg;
143         return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
144 }
145
146 int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
147 {
148         u8 mm1[] = {0x00};
149         struct i2c_msg msgs[2];
150
151         if (!via_i2c_par[adap].is_active)
152                 return -ENODEV;
153         msgs[0].flags = 0;
154         msgs[1].flags = I2C_M_RD;
155         msgs[0].addr = msgs[1].addr = slave_addr / 2;
156         mm1[0] = index;
157         msgs[0].len = 1; msgs[1].len = buff_len;
158         msgs[0].buf = mm1; msgs[1].buf = buff;
159         return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
160 }
161
162 /*
163  * Allow other viafb subdevices to look up a specific adapter
164  * by port name.
165  */
166 struct i2c_adapter *viafb_find_i2c_adapter(enum viafb_i2c_adap which)
167 {
168         struct via_i2c_stuff *stuff = &via_i2c_par[which];
169
170         return &stuff->adapter;
171 }
172 EXPORT_SYMBOL_GPL(viafb_find_i2c_adapter);
173
174
175 static int create_i2c_bus(struct i2c_adapter *adapter,
176                           struct i2c_algo_bit_data *algo,
177                           struct via_port_cfg *adap_cfg,
178                           struct pci_dev *pdev)
179 {
180         algo->setsda = via_i2c_setsda;
181         algo->setscl = via_i2c_setscl;
182         algo->getsda = via_i2c_getsda;
183         algo->getscl = via_i2c_getscl;
184         algo->udelay = 40;
185         algo->timeout = 20;
186         algo->data = adap_cfg;
187
188         sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
189                 adap_cfg->ioport_index);
190         adapter->owner = THIS_MODULE;
191         adapter->id = 0x01FFFF;
192         adapter->class = I2C_CLASS_DDC;
193         adapter->algo_data = algo;
194         if (pdev)
195                 adapter->dev.parent = &pdev->dev;
196         else
197                 adapter->dev.parent = NULL;
198         /* i2c_set_adapdata(adapter, adap_cfg); */
199
200         /* Raise SCL and SDA */
201         via_i2c_setsda(adap_cfg, 1);
202         via_i2c_setscl(adap_cfg, 1);
203         udelay(20);
204
205         return i2c_bit_add_bus(adapter);
206 }
207
208 static int viafb_i2c_probe(struct platform_device *platdev)
209 {
210         int i, ret;
211         struct via_port_cfg *configs;
212
213         i2c_vdev = platdev->dev.platform_data;
214         configs = i2c_vdev->port_cfg;
215
216         for (i = 0; i < VIAFB_NUM_PORTS; i++) {
217                 struct via_port_cfg *adap_cfg = configs++;
218                 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
219
220                 i2c_stuff->is_active = 0;
221                 if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
222                         continue;
223                 ret = create_i2c_bus(&i2c_stuff->adapter,
224                                      &i2c_stuff->algo, adap_cfg,
225                                 NULL); /* FIXME: PCIDEV */
226                 if (ret < 0) {
227                         printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
228                                 i, ret);
229                         continue;  /* Still try to make the rest */
230                 }
231                 i2c_stuff->is_active = 1;
232         }
233
234         return 0;
235 }
236
237 static int viafb_i2c_remove(struct platform_device *platdev)
238 {
239         int i;
240
241         for (i = 0; i < VIAFB_NUM_PORTS; i++) {
242                 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
243                 /*
244                  * Only remove those entries in the array that we've
245                  * actually used (and thus initialized algo_data)
246                  */
247                 if (i2c_stuff->is_active)
248                         i2c_del_adapter(&i2c_stuff->adapter);
249         }
250         return 0;
251 }
252
253 static struct platform_driver via_i2c_driver = {
254         .driver = {
255                 .name = "viafb-i2c",
256         },
257         .probe = viafb_i2c_probe,
258         .remove = viafb_i2c_remove,
259 };
260
261 int viafb_i2c_init(void)
262 {
263         return platform_driver_register(&via_i2c_driver);
264 }
265
266 void viafb_i2c_exit(void)
267 {
268         platform_driver_unregister(&via_i2c_driver);
269 }