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>
28 * There can only be one set of these, so there's no point in having
29 * them be dynamically allocated...
31 #define VIAFB_NUM_I2C 5
32 static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
33 struct viafb_dev *i2c_vdev; /* Passed in from core */
35 static void via_i2c_setscl(void *data, int state)
38 struct via_port_cfg *adap_data = data;
41 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
42 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
47 switch (adap_data->type) {
55 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
57 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
58 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
61 static int via_i2c_getscl(void *data)
63 struct via_port_cfg *adap_data = data;
67 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
68 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
70 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
74 static int via_i2c_getsda(void *data)
76 struct via_port_cfg *adap_data = data;
80 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
81 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
83 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
87 static void via_i2c_setsda(void *data, int state)
90 struct via_port_cfg *adap_data = data;
93 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
94 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
99 switch (adap_data->type) {
107 printk(KERN_ERR "viafb_i2c: specify wrong i2c type.\n");
109 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
110 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
113 int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
116 struct i2c_msg msgs[2];
118 if (!via_i2c_par[adap].is_active)
122 msgs[1].flags = I2C_M_RD;
123 msgs[0].addr = msgs[1].addr = slave_addr / 2;
125 msgs[0].len = 1; msgs[1].len = 1;
126 msgs[0].buf = mm1; msgs[1].buf = pdata;
127 return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
130 int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
132 u8 msg[2] = { index, data };
135 if (!via_i2c_par[adap].is_active)
138 msgs.addr = slave_addr / 2;
141 return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
144 int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
147 struct i2c_msg msgs[2];
149 if (!via_i2c_par[adap].is_active)
152 msgs[1].flags = I2C_M_RD;
153 msgs[0].addr = msgs[1].addr = slave_addr / 2;
155 msgs[0].len = 1; msgs[1].len = buff_len;
156 msgs[0].buf = mm1; msgs[1].buf = buff;
157 return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
161 * Allow other viafb subdevices to look up a specific adapter
164 struct i2c_adapter *viafb_find_i2c_adapter(enum viafb_i2c_adap which)
166 struct via_i2c_stuff *stuff = &via_i2c_par[which];
168 return &stuff->adapter;
170 EXPORT_SYMBOL_GPL(viafb_find_i2c_adapter);
173 static int create_i2c_bus(struct i2c_adapter *adapter,
174 struct i2c_algo_bit_data *algo,
175 struct via_port_cfg *adap_cfg,
176 struct pci_dev *pdev)
178 algo->setsda = via_i2c_setsda;
179 algo->setscl = via_i2c_setscl;
180 algo->getsda = via_i2c_getsda;
181 algo->getscl = via_i2c_getscl;
184 algo->data = adap_cfg;
186 sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
187 adap_cfg->ioport_index);
188 adapter->owner = THIS_MODULE;
189 adapter->id = 0x01FFFF;
190 adapter->class = I2C_CLASS_DDC;
191 adapter->algo_data = algo;
193 adapter->dev.parent = &pdev->dev;
195 adapter->dev.parent = NULL;
196 /* i2c_set_adapdata(adapter, adap_cfg); */
198 /* Raise SCL and SDA */
199 via_i2c_setsda(adap_cfg, 1);
200 via_i2c_setscl(adap_cfg, 1);
203 return i2c_bit_add_bus(adapter);
206 static int viafb_i2c_probe(struct platform_device *platdev)
209 struct via_port_cfg *configs;
211 i2c_vdev = platdev->dev.platform_data;
212 configs = i2c_vdev->port_cfg;
214 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
215 struct via_port_cfg *adap_cfg = configs++;
216 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
218 i2c_stuff->is_active = 0;
219 if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
221 ret = create_i2c_bus(&i2c_stuff->adapter,
222 &i2c_stuff->algo, adap_cfg,
223 NULL); /* FIXME: PCIDEV */
225 printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
227 continue; /* Still try to make the rest */
229 i2c_stuff->is_active = 1;
235 static int viafb_i2c_remove(struct platform_device *platdev)
239 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
240 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
242 * Only remove those entries in the array that we've
243 * actually used (and thus initialized algo_data)
245 if (i2c_stuff->is_active)
246 i2c_del_adapter(&i2c_stuff->adapter);
251 static struct platform_driver via_i2c_driver = {
255 .probe = viafb_i2c_probe,
256 .remove = viafb_i2c_remove,
259 int viafb_i2c_init(void)
261 return platform_driver_register(&via_i2c_driver);
264 void viafb_i2c_exit(void)
266 platform_driver_unregister(&via_i2c_driver);