blob: 4788292815629c6b38187791da70da11fb84e176 [file] [log] [blame]
Joseph Chan9f291632008-10-15 22:03:29 -07001/*
Harald Welte277d32a2009-05-23 00:35:39 +08002 * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
Joseph Chan9f291632008-10-15 22:03:29 -07003 * 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 "global.h"
23
Jonathan Corbetf045f772009-12-01 20:29:39 -070024/*
25 * There can only be one set of these, so there's no point in having
26 * them be dynamically allocated...
27 */
28#define VIAFB_NUM_I2C 5
29static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C];
30
Joseph Chan9f291632008-10-15 22:03:29 -070031static void via_i2c_setscl(void *data, int state)
32{
33 u8 val;
Jonathan Corbetf045f772009-12-01 20:29:39 -070034 struct via_port_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070035
Harald Welte277d32a2009-05-23 00:35:39 +080036 val = viafb_read_reg(adap_data->io_port,
37 adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -070038 if (state)
39 val |= 0x20;
40 else
41 val &= ~0x20;
Harald Welte277d32a2009-05-23 00:35:39 +080042 switch (adap_data->type) {
Jonathan Corbetf045f772009-12-01 20:29:39 -070043 case VIA_PORT_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -070044 val |= 0x01;
45 break;
Jonathan Corbetf045f772009-12-01 20:29:39 -070046 case VIA_PORT_GPIO:
Joseph Chan9f291632008-10-15 22:03:29 -070047 val |= 0x80;
48 break;
49 default:
Harald Welte277d32a2009-05-23 00:35:39 +080050 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -070051 }
Harald Welte277d32a2009-05-23 00:35:39 +080052 viafb_write_reg(adap_data->ioport_index,
53 adap_data->io_port, val);
Joseph Chan9f291632008-10-15 22:03:29 -070054}
55
56static int via_i2c_getscl(void *data)
57{
Jonathan Corbetf045f772009-12-01 20:29:39 -070058 struct via_port_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070059
Harald Welte277d32a2009-05-23 00:35:39 +080060 if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
Joseph Chan9f291632008-10-15 22:03:29 -070061 return 1;
62 return 0;
63}
64
65static int via_i2c_getsda(void *data)
66{
Jonathan Corbetf045f772009-12-01 20:29:39 -070067 struct via_port_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070068
Harald Welte277d32a2009-05-23 00:35:39 +080069 if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
Joseph Chan9f291632008-10-15 22:03:29 -070070 return 1;
71 return 0;
72}
73
74static void via_i2c_setsda(void *data, int state)
75{
76 u8 val;
Jonathan Corbetf045f772009-12-01 20:29:39 -070077 struct via_port_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070078
Harald Welte277d32a2009-05-23 00:35:39 +080079 val = viafb_read_reg(adap_data->io_port,
80 adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -070081 if (state)
82 val |= 0x10;
83 else
84 val &= ~0x10;
Harald Welte277d32a2009-05-23 00:35:39 +080085 switch (adap_data->type) {
Jonathan Corbetf045f772009-12-01 20:29:39 -070086 case VIA_PORT_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -070087 val |= 0x01;
88 break;
Jonathan Corbetf045f772009-12-01 20:29:39 -070089 case VIA_PORT_GPIO:
Joseph Chan9f291632008-10-15 22:03:29 -070090 val |= 0x40;
91 break;
92 default:
Harald Welte277d32a2009-05-23 00:35:39 +080093 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -070094 }
Harald Welte277d32a2009-05-23 00:35:39 +080095 viafb_write_reg(adap_data->ioport_index,
96 adap_data->io_port, val);
Joseph Chan9f291632008-10-15 22:03:29 -070097}
98
Harald Welte277d32a2009-05-23 00:35:39 +080099int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
Joseph Chan9f291632008-10-15 22:03:29 -0700100{
101 u8 mm1[] = {0x00};
102 struct i2c_msg msgs[2];
103
104 *pdata = 0;
105 msgs[0].flags = 0;
106 msgs[1].flags = I2C_M_RD;
107 msgs[0].addr = msgs[1].addr = slave_addr / 2;
108 mm1[0] = index;
109 msgs[0].len = 1; msgs[1].len = 1;
110 msgs[0].buf = mm1; msgs[1].buf = pdata;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700111 return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
Joseph Chan9f291632008-10-15 22:03:29 -0700112}
113
Harald Welte277d32a2009-05-23 00:35:39 +0800114int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
Joseph Chan9f291632008-10-15 22:03:29 -0700115{
116 u8 msg[2] = { index, data };
117 struct i2c_msg msgs;
118
119 msgs.flags = 0;
120 msgs.addr = slave_addr / 2;
121 msgs.len = 2;
122 msgs.buf = msg;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700123 return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1);
Joseph Chan9f291632008-10-15 22:03:29 -0700124}
125
Harald Welte277d32a2009-05-23 00:35:39 +0800126int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
Joseph Chan9f291632008-10-15 22:03:29 -0700127{
128 u8 mm1[] = {0x00};
129 struct i2c_msg msgs[2];
130
131 msgs[0].flags = 0;
132 msgs[1].flags = I2C_M_RD;
133 msgs[0].addr = msgs[1].addr = slave_addr / 2;
134 mm1[0] = index;
135 msgs[0].len = 1; msgs[1].len = buff_len;
136 msgs[0].buf = mm1; msgs[1].buf = buff;
Jonathan Corbetf045f772009-12-01 20:29:39 -0700137 return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2);
Harald Welte277d32a2009-05-23 00:35:39 +0800138}
139
140static int create_i2c_bus(struct i2c_adapter *adapter,
141 struct i2c_algo_bit_data *algo,
Jonathan Corbetf045f772009-12-01 20:29:39 -0700142 struct via_port_cfg *adap_cfg,
Harald Welte277d32a2009-05-23 00:35:39 +0800143 struct pci_dev *pdev)
144{
Paul Foxc774c132009-10-07 11:13:01 -0400145 DEBUG_MSG(KERN_DEBUG "viafb: creating bus adap=0x%p, algo_bit_data=0x%p, adap_cfg=0x%p\n", adapter, algo, adap_cfg);
Harald Welte277d32a2009-05-23 00:35:39 +0800146
147 algo->setsda = via_i2c_setsda;
148 algo->setscl = via_i2c_setscl;
149 algo->getsda = via_i2c_getsda;
150 algo->getscl = via_i2c_getscl;
151 algo->udelay = 40;
152 algo->timeout = 20;
153 algo->data = adap_cfg;
154
155 sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
156 adap_cfg->ioport_index);
157 adapter->owner = THIS_MODULE;
158 adapter->id = 0x01FFFF;
159 adapter->class = I2C_CLASS_DDC;
160 adapter->algo_data = algo;
161 if (pdev)
162 adapter->dev.parent = &pdev->dev;
163 else
164 adapter->dev.parent = NULL;
165 /* i2c_set_adapdata(adapter, adap_cfg); */
166
167 /* Raise SCL and SDA */
168 via_i2c_setsda(adap_cfg, 1);
169 via_i2c_setscl(adap_cfg, 1);
170 udelay(20);
171
172 return i2c_bit_add_bus(adapter);
173}
174
Jonathan Corbetf045f772009-12-01 20:29:39 -0700175int viafb_create_i2c_busses(struct via_port_cfg *configs)
Harald Welte277d32a2009-05-23 00:35:39 +0800176{
177 int i, ret;
178
Jonathan Corbetf045f772009-12-01 20:29:39 -0700179 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
180 struct via_port_cfg *adap_cfg = configs++;
181 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
Harald Welte277d32a2009-05-23 00:35:39 +0800182
Jonathan Corbetf045f772009-12-01 20:29:39 -0700183 if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C)
Jonathan Corbet4da62e62010-04-25 08:30:41 -0600184 continue;
Harald Welte277d32a2009-05-23 00:35:39 +0800185
186 ret = create_i2c_bus(&i2c_stuff->adapter,
187 &i2c_stuff->algo, adap_cfg,
188 NULL); /* FIXME: PCIDEV */
189 if (ret < 0) {
190 printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
191 i, ret);
192 /* FIXME: properly release previous busses */
193 return ret;
194 }
195 }
196
Joseph Chan9f291632008-10-15 22:03:29 -0700197 return 0;
198}
199
Jonathan Corbetf045f772009-12-01 20:29:39 -0700200void viafb_delete_i2c_busses(void)
Joseph Chan9f291632008-10-15 22:03:29 -0700201{
Harald Welte277d32a2009-05-23 00:35:39 +0800202 int i;
Joseph Chan9f291632008-10-15 22:03:29 -0700203
Jonathan Corbetf045f772009-12-01 20:29:39 -0700204 for (i = 0; i < VIAFB_NUM_PORTS; i++) {
205 struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i];
206 /*
207 * Only remove those entries in the array that we've
208 * actually used (and thus initialized algo_data)
209 */
Harald Welte277d32a2009-05-23 00:35:39 +0800210 if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
211 i2c_del_adapter(&i2c_stuff->adapter);
212 }
Joseph Chan9f291632008-10-15 22:03:29 -0700213}