2 * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
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.
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
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
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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>
30 * There can only be one set of these, so there's no point in having
31 * them be dynamically allocated...
33 #define VIAFB_NUM_I2C 5
34 static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
35 static struct viafb_dev *i2c_vdev; /* Passed in from core */
37 static void via_i2c_setscl(void *data, int state)
40 struct via_port_cfg *adap_data = data;
43 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
44 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
49 switch (adap_data->type) {
57 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
59 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
60 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
63 static int via_i2c_getscl(void *data)
65 struct via_port_cfg *adap_data = data;
69 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
70 if (adap_data->type == VIA_PORT_GPIO)
71 via_write_reg_mask(adap_data->io_port, adap_data->ioport_index,
73 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
75 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
79 static int via_i2c_getsda(void *data)
81 struct via_port_cfg *adap_data = data;
85 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
86 if (adap_data->type == VIA_PORT_GPIO)
87 via_write_reg_mask(adap_data->io_port, adap_data->ioport_index,
89 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
91 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
95 static void via_i2c_setsda(void *data, int state)
98 struct via_port_cfg *adap_data = data;
101 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
102 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
107 switch (adap_data->type) {
115 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
117 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
118 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
121 int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
125 struct i2c_msg msgs[2];
127 if (!via_i2c_par[adap].is_active)
131 msgs[1].flags = I2C_M_RD;
132 msgs[0].addr = msgs[1].addr = slave_addr / 2;
134 msgs[0].len = 1; msgs[1].len = 1;
135 msgs[0].buf = mm1; msgs[1].buf = pdata;
136 ret = i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
145 int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
148 u8 msg[2] = { index, data };
151 if (!via_i2c_par[adap].is_active)
154 msgs.addr = slave_addr / 2;
157 ret = i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
166 int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
170 struct i2c_msg msgs[2];
172 if (!via_i2c_par[adap].is_active)
175 msgs[1].flags = I2C_M_RD;
176 msgs[0].addr = msgs[1].addr = slave_addr / 2;
178 msgs[0].len = 1; msgs[1].len = buff_len;
179 msgs[0].buf = mm1; msgs[1].buf = buff;
180 ret = i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
190 * Allow other viafb subdevices to look up a specific adapter
193 struct i2c_adapter *viafb_find_i2c_adapter(enum viafb_i2c_adap which)
195 struct via_i2c_stuff *stuff = &via_i2c_par[which];
197 return &stuff->adapter;
199 EXPORT_SYMBOL_GPL(viafb_find_i2c_adapter);
202 static int create_i2c_bus(struct i2c_adapter *adapter,
203 struct i2c_algo_bit_data *algo,
204 struct via_port_cfg *adap_cfg,
205 struct pci_dev *pdev)
207 algo->setsda = via_i2c_setsda;
208 algo->setscl = via_i2c_setscl;
209 algo->getsda = via_i2c_getsda;
210 algo->getscl = via_i2c_getscl;
213 algo->data = adap_cfg;
215 sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
216 adap_cfg->ioport_index);
217 adapter->owner = THIS_MODULE;
218 adapter->class = I2C_CLASS_DDC;
219 adapter->algo_data = algo;
221 adapter->dev.parent = &pdev->dev;
223 adapter->dev.parent = NULL;
224 /* i2c_set_adapdata(adapter, adap_cfg); */
226 /* Raise SCL and SDA */
227 via_i2c_setsda(adap_cfg, 1);
228 via_i2c_setscl(adap_cfg, 1);
231 return i2c_bit_add_bus(adapter);
234 static int viafb_i2c_probe(struct platform_device *platdev)
237 struct via_port_cfg *configs;
239 i2c_vdev = platdev->dev.platform_data;
240 configs = i2c_vdev->port_cfg;
242 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
243 struct via_port_cfg *adap_cfg = configs++;
244 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
246 i2c_stuff->is_active = 0;
247 if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
249 ret = create_i2c_bus(&i2c_stuff->adapter,
250 &i2c_stuff->algo, adap_cfg,
251 NULL); /* FIXME: PCIDEV */
253 printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
255 continue; /* Still try to make the rest */
257 i2c_stuff->is_active = 1;
263 static int viafb_i2c_remove(struct platform_device *platdev)
267 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
268 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
270 * Only remove those entries in the array that we've
271 * actually used (and thus initialized algo_data)
273 if (i2c_stuff->is_active)
274 i2c_del_adapter(&i2c_stuff->adapter);
279 static struct platform_driver via_i2c_driver = {
283 .probe = viafb_i2c_probe,
284 .remove = viafb_i2c_remove,
287 int viafb_i2c_init(void)
289 return platform_driver_register(&via_i2c_driver);
292 void viafb_i2c_exit(void)
294 platform_driver_unregister(&via_i2c_driver);