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.
27 * There can only be one set of these, so there's no point in having
28 * them be dynamically allocated...
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 */
34 static void via_i2c_setscl(void *data, int state)
37 struct via_port_cfg *adap_data = data;
40 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
41 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
46 switch (adap_data->type) {
54 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
56 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
57 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
60 static int via_i2c_getscl(void *data)
62 struct via_port_cfg *adap_data = data;
66 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
67 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
69 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
73 static int via_i2c_getsda(void *data)
75 struct via_port_cfg *adap_data = data;
79 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
80 if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
82 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
86 static void via_i2c_setsda(void *data, int state)
89 struct via_port_cfg *adap_data = data;
92 spin_lock_irqsave(&i2c_vdev->reg_lock, flags);
93 val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0;
98 switch (adap_data->type) {
106 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
108 via_write_reg(adap_data->io_port, adap_data->ioport_index, val);
109 spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags);
112 int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
115 struct i2c_msg msgs[2];
119 msgs[1].flags = I2C_M_RD;
120 msgs[0].addr = msgs[1].addr = slave_addr / 2;
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);
127 int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
129 u8 msg[2] = { index, data };
133 msgs.addr = slave_addr / 2;
136 return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
139 int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
142 struct i2c_msg msgs[2];
145 msgs[1].flags = I2C_M_RD;
146 msgs[0].addr = msgs[1].addr = slave_addr / 2;
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);
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)
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);
160 algo->setsda = via_i2c_setsda;
161 algo->setscl = via_i2c_setscl;
162 algo->getsda = via_i2c_getsda;
163 algo->getscl = via_i2c_getscl;
166 algo->data = adap_cfg;
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;
175 adapter->dev.parent = &pdev->dev;
177 adapter->dev.parent = NULL;
178 /* i2c_set_adapdata(adapter, adap_cfg); */
180 /* Raise SCL and SDA */
181 via_i2c_setsda(adap_cfg, 1);
182 via_i2c_setscl(adap_cfg, 1);
185 return i2c_bit_add_bus(adapter);
188 int viafb_create_i2c_busses(struct viafb_dev *dev, struct via_port_cfg *configs)
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];
197 if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
200 ret = create_i2c_bus(&i2c_stuff->adapter,
201 &i2c_stuff->algo, adap_cfg,
202 NULL); /* FIXME: PCIDEV */
204 printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
206 /* FIXME: properly release previous busses */
214 void viafb_delete_i2c_busses(void)
218 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
219 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
221 * Only remove those entries in the array that we've
222 * actually used (and thus initialized algo_data)
224 if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
225 i2c_del_adapter(&i2c_stuff->adapter);