blob: b5253e3099f898b184f64d8e383419269b09c20d [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
24static void via_i2c_setscl(void *data, int state)
25{
26 u8 val;
Harald Welte277d32a2009-05-23 00:35:39 +080027 struct via_i2c_adap_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070028
Paul Foxc774c132009-10-07 11:13:01 -040029 DEBUG_MSG(KERN_DEBUG "reading index 0x%02x from IO 0x%x\n",
Harald Welte277d32a2009-05-23 00:35:39 +080030 adap_data->ioport_index, adap_data->io_port);
31 val = viafb_read_reg(adap_data->io_port,
32 adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -070033 if (state)
34 val |= 0x20;
35 else
36 val &= ~0x20;
Harald Welte277d32a2009-05-23 00:35:39 +080037 switch (adap_data->type) {
38 case VIA_I2C_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -070039 val |= 0x01;
40 break;
Harald Welte277d32a2009-05-23 00:35:39 +080041 case VIA_I2C_GPIO:
Joseph Chan9f291632008-10-15 22:03:29 -070042 val |= 0x80;
43 break;
44 default:
Harald Welte277d32a2009-05-23 00:35:39 +080045 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -070046 }
Harald Welte277d32a2009-05-23 00:35:39 +080047 viafb_write_reg(adap_data->ioport_index,
48 adap_data->io_port, val);
Joseph Chan9f291632008-10-15 22:03:29 -070049}
50
51static int via_i2c_getscl(void *data)
52{
Harald Welte277d32a2009-05-23 00:35:39 +080053 struct via_i2c_adap_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070054
Harald Welte277d32a2009-05-23 00:35:39 +080055 if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08)
Joseph Chan9f291632008-10-15 22:03:29 -070056 return 1;
57 return 0;
58}
59
60static int via_i2c_getsda(void *data)
61{
Harald Welte277d32a2009-05-23 00:35:39 +080062 struct via_i2c_adap_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070063
Harald Welte277d32a2009-05-23 00:35:39 +080064 if (viafb_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04)
Joseph Chan9f291632008-10-15 22:03:29 -070065 return 1;
66 return 0;
67}
68
69static void via_i2c_setsda(void *data, int state)
70{
71 u8 val;
Harald Welte277d32a2009-05-23 00:35:39 +080072 struct via_i2c_adap_cfg *adap_data = data;
Joseph Chan9f291632008-10-15 22:03:29 -070073
Harald Welte277d32a2009-05-23 00:35:39 +080074 val = viafb_read_reg(adap_data->io_port,
75 adap_data->ioport_index) & 0xF0;
Joseph Chan9f291632008-10-15 22:03:29 -070076 if (state)
77 val |= 0x10;
78 else
79 val &= ~0x10;
Harald Welte277d32a2009-05-23 00:35:39 +080080 switch (adap_data->type) {
81 case VIA_I2C_I2C:
Joseph Chan9f291632008-10-15 22:03:29 -070082 val |= 0x01;
83 break;
Harald Welte277d32a2009-05-23 00:35:39 +080084 case VIA_I2C_GPIO:
Joseph Chan9f291632008-10-15 22:03:29 -070085 val |= 0x40;
86 break;
87 default:
Harald Welte277d32a2009-05-23 00:35:39 +080088 DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n");
Joseph Chan9f291632008-10-15 22:03:29 -070089 }
Harald Welte277d32a2009-05-23 00:35:39 +080090 viafb_write_reg(adap_data->ioport_index,
91 adap_data->io_port, val);
Joseph Chan9f291632008-10-15 22:03:29 -070092}
93
Harald Welte277d32a2009-05-23 00:35:39 +080094int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata)
Joseph Chan9f291632008-10-15 22:03:29 -070095{
96 u8 mm1[] = {0x00};
97 struct i2c_msg msgs[2];
98
99 *pdata = 0;
100 msgs[0].flags = 0;
101 msgs[1].flags = I2C_M_RD;
102 msgs[0].addr = msgs[1].addr = slave_addr / 2;
103 mm1[0] = index;
104 msgs[0].len = 1; msgs[1].len = 1;
105 msgs[0].buf = mm1; msgs[1].buf = pdata;
Harald Welte277d32a2009-05-23 00:35:39 +0800106 return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter,
107 msgs, 2);
Joseph Chan9f291632008-10-15 22:03:29 -0700108}
109
Harald Welte277d32a2009-05-23 00:35:39 +0800110int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data)
Joseph Chan9f291632008-10-15 22:03:29 -0700111{
112 u8 msg[2] = { index, data };
113 struct i2c_msg msgs;
114
115 msgs.flags = 0;
116 msgs.addr = slave_addr / 2;
117 msgs.len = 2;
118 msgs.buf = msg;
Harald Welte277d32a2009-05-23 00:35:39 +0800119 return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter,
120 &msgs, 1);
Joseph Chan9f291632008-10-15 22:03:29 -0700121}
122
Harald Welte277d32a2009-05-23 00:35:39 +0800123int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len)
Joseph Chan9f291632008-10-15 22:03:29 -0700124{
125 u8 mm1[] = {0x00};
126 struct i2c_msg msgs[2];
127
128 msgs[0].flags = 0;
129 msgs[1].flags = I2C_M_RD;
130 msgs[0].addr = msgs[1].addr = slave_addr / 2;
131 mm1[0] = index;
132 msgs[0].len = 1; msgs[1].len = buff_len;
133 msgs[0].buf = mm1; msgs[1].buf = buff;
Harald Welte277d32a2009-05-23 00:35:39 +0800134 return i2c_transfer(&viaparinfo->shared->i2c_stuff[adap].adapter,
135 msgs, 2);
136}
137
138static int create_i2c_bus(struct i2c_adapter *adapter,
139 struct i2c_algo_bit_data *algo,
140 struct via_i2c_adap_cfg *adap_cfg,
141 struct pci_dev *pdev)
142{
Paul Foxc774c132009-10-07 11:13:01 -0400143 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 +0800144
145 algo->setsda = via_i2c_setsda;
146 algo->setscl = via_i2c_setscl;
147 algo->getsda = via_i2c_getsda;
148 algo->getscl = via_i2c_getscl;
149 algo->udelay = 40;
150 algo->timeout = 20;
151 algo->data = adap_cfg;
152
153 sprintf(adapter->name, "viafb i2c io_port idx 0x%02x",
154 adap_cfg->ioport_index);
155 adapter->owner = THIS_MODULE;
156 adapter->id = 0x01FFFF;
157 adapter->class = I2C_CLASS_DDC;
158 adapter->algo_data = algo;
159 if (pdev)
160 adapter->dev.parent = &pdev->dev;
161 else
162 adapter->dev.parent = NULL;
163 /* i2c_set_adapdata(adapter, adap_cfg); */
164
165 /* Raise SCL and SDA */
166 via_i2c_setsda(adap_cfg, 1);
167 via_i2c_setscl(adap_cfg, 1);
168 udelay(20);
169
170 return i2c_bit_add_bus(adapter);
171}
172
Jonathan Corbet4da62e62010-04-25 08:30:41 -0600173/*
174 * By default, we only activate busses on ports 2c and 31 to avoid
175 * conflicts with other possible users; that default can be changed
176 * below.
177 */
Harald Welte277d32a2009-05-23 00:35:39 +0800178static struct via_i2c_adap_cfg adap_configs[] = {
Jonathan Corbet4da62e62010-04-25 08:30:41 -0600179 [VIA_I2C_ADAP_26] = { VIA_I2C_I2C, VIASR, 0x26, 0 },
180 [VIA_I2C_ADAP_31] = { VIA_I2C_I2C, VIASR, 0x31, 1 },
181 [VIA_I2C_ADAP_25] = { VIA_I2C_GPIO, VIASR, 0x25, 0 },
182 [VIA_I2C_ADAP_2C] = { VIA_I2C_GPIO, VIASR, 0x2c, 1 },
183 [VIA_I2C_ADAP_3D] = { VIA_I2C_GPIO, VIASR, 0x3d, 0 },
184 { 0, 0, 0, 0 }
Harald Welte277d32a2009-05-23 00:35:39 +0800185};
186
187int viafb_create_i2c_busses(struct viafb_par *viapar)
188{
189 int i, ret;
190
191 for (i = 0; i < VIAFB_NUM_I2C; i++) {
192 struct via_i2c_adap_cfg *adap_cfg = &adap_configs[i];
193 struct via_i2c_stuff *i2c_stuff = &viapar->shared->i2c_stuff[i];
194
195 if (adap_cfg->type == 0)
196 break;
Jonathan Corbet4da62e62010-04-25 08:30:41 -0600197 if (!adap_cfg->is_active)
198 continue;
Harald Welte277d32a2009-05-23 00:35:39 +0800199
200 ret = create_i2c_bus(&i2c_stuff->adapter,
201 &i2c_stuff->algo, adap_cfg,
202 NULL); /* FIXME: PCIDEV */
203 if (ret < 0) {
204 printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n",
205 i, ret);
206 /* FIXME: properly release previous busses */
207 return ret;
208 }
209 }
210
Joseph Chan9f291632008-10-15 22:03:29 -0700211 return 0;
212}
213
Harald Welte277d32a2009-05-23 00:35:39 +0800214void viafb_delete_i2c_busses(struct viafb_par *par)
Joseph Chan9f291632008-10-15 22:03:29 -0700215{
Harald Welte277d32a2009-05-23 00:35:39 +0800216 int i;
Joseph Chan9f291632008-10-15 22:03:29 -0700217
Harald Welte277d32a2009-05-23 00:35:39 +0800218 for (i = 0; i < ARRAY_SIZE(par->shared->i2c_stuff); i++) {
219 struct via_i2c_stuff *i2c_stuff = &par->shared->i2c_stuff[i];
220 /* only remove those entries in the array that we've
221 * actually used (and thus initialized algo_data) */
222 if (i2c_stuff->adapter.algo_data == &i2c_stuff->algo)
223 i2c_del_adapter(&i2c_stuff->adapter);
224 }
Joseph Chan9f291632008-10-15 22:03:29 -0700225}