Commit | Line | Data |
---|---|---|
9f291634 | 1 | /* |
277d32a3 | 2 | * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved. |
9f291634 JC |
3 | * 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 | ||
7582eb9b | 22 | #include <linux/platform_device.h> |
24b4d82e JC |
23 | #include "via-core.h" |
24 | #include "via_i2c.h" | |
9f291634 JC |
25 | #include "global.h" |
26 | ||
f045f77b JC |
27 | /* |
28 | * There can only be one set of these, so there's no point in having | |
29 | * them be dynamically allocated... | |
30 | */ | |
31 | #define VIAFB_NUM_I2C 5 | |
32 | static struct via_i2c_stuff via_i2c_par[VIAFB_NUM_I2C]; | |
75b035ac | 33 | struct viafb_dev *i2c_vdev; /* Passed in from core */ |
f045f77b | 34 | |
9f291634 JC |
35 | static void via_i2c_setscl(void *data, int state) |
36 | { | |
37 | u8 val; | |
f045f77b | 38 | struct via_port_cfg *adap_data = data; |
75b035ac | 39 | unsigned long flags; |
9f291634 | 40 | |
75b035ac JC |
41 | spin_lock_irqsave(&i2c_vdev->reg_lock, flags); |
42 | val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0; | |
9f291634 JC |
43 | if (state) |
44 | val |= 0x20; | |
45 | else | |
46 | val &= ~0x20; | |
277d32a3 | 47 | switch (adap_data->type) { |
f045f77b | 48 | case VIA_PORT_I2C: |
9f291634 JC |
49 | val |= 0x01; |
50 | break; | |
f045f77b | 51 | case VIA_PORT_GPIO: |
9f291634 JC |
52 | val |= 0x80; |
53 | break; | |
54 | default: | |
277d32a3 | 55 | DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n"); |
9f291634 | 56 | } |
75b035ac JC |
57 | via_write_reg(adap_data->io_port, adap_data->ioport_index, val); |
58 | spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags); | |
9f291634 JC |
59 | } |
60 | ||
61 | static int via_i2c_getscl(void *data) | |
62 | { | |
f045f77b | 63 | struct via_port_cfg *adap_data = data; |
75b035ac JC |
64 | unsigned long flags; |
65 | int ret = 0; | |
66 | ||
67 | spin_lock_irqsave(&i2c_vdev->reg_lock, flags); | |
68 | if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x08) | |
69 | ret = 1; | |
70 | spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags); | |
71 | return ret; | |
9f291634 JC |
72 | } |
73 | ||
74 | static int via_i2c_getsda(void *data) | |
75 | { | |
f045f77b | 76 | struct via_port_cfg *adap_data = data; |
75b035ac JC |
77 | unsigned long flags; |
78 | int ret = 0; | |
79 | ||
80 | spin_lock_irqsave(&i2c_vdev->reg_lock, flags); | |
81 | if (via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0x04) | |
82 | ret = 1; | |
83 | spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags); | |
84 | return ret; | |
9f291634 JC |
85 | } |
86 | ||
87 | static void via_i2c_setsda(void *data, int state) | |
88 | { | |
89 | u8 val; | |
f045f77b | 90 | struct via_port_cfg *adap_data = data; |
75b035ac | 91 | unsigned long flags; |
9f291634 | 92 | |
75b035ac JC |
93 | spin_lock_irqsave(&i2c_vdev->reg_lock, flags); |
94 | val = via_read_reg(adap_data->io_port, adap_data->ioport_index) & 0xF0; | |
9f291634 JC |
95 | if (state) |
96 | val |= 0x10; | |
97 | else | |
98 | val &= ~0x10; | |
277d32a3 | 99 | switch (adap_data->type) { |
f045f77b | 100 | case VIA_PORT_I2C: |
9f291634 JC |
101 | val |= 0x01; |
102 | break; | |
f045f77b | 103 | case VIA_PORT_GPIO: |
9f291634 JC |
104 | val |= 0x40; |
105 | break; | |
106 | default: | |
277d32a3 | 107 | DEBUG_MSG("viafb_i2c: specify wrong i2c type.\n"); |
9f291634 | 108 | } |
75b035ac JC |
109 | via_write_reg(adap_data->io_port, adap_data->ioport_index, val); |
110 | spin_unlock_irqrestore(&i2c_vdev->reg_lock, flags); | |
9f291634 JC |
111 | } |
112 | ||
277d32a3 | 113 | int viafb_i2c_readbyte(u8 adap, u8 slave_addr, u8 index, u8 *pdata) |
9f291634 JC |
114 | { |
115 | u8 mm1[] = {0x00}; | |
116 | struct i2c_msg msgs[2]; | |
117 | ||
b052d7f8 JC |
118 | if (!via_i2c_par[adap].is_active) |
119 | return -ENODEV; | |
9f291634 JC |
120 | *pdata = 0; |
121 | msgs[0].flags = 0; | |
122 | msgs[1].flags = I2C_M_RD; | |
123 | msgs[0].addr = msgs[1].addr = slave_addr / 2; | |
124 | mm1[0] = index; | |
125 | msgs[0].len = 1; msgs[1].len = 1; | |
126 | msgs[0].buf = mm1; msgs[1].buf = pdata; | |
f045f77b | 127 | return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2); |
9f291634 JC |
128 | } |
129 | ||
277d32a3 | 130 | int viafb_i2c_writebyte(u8 adap, u8 slave_addr, u8 index, u8 data) |
9f291634 JC |
131 | { |
132 | u8 msg[2] = { index, data }; | |
133 | struct i2c_msg msgs; | |
134 | ||
b052d7f8 JC |
135 | if (!via_i2c_par[adap].is_active) |
136 | return -ENODEV; | |
9f291634 JC |
137 | msgs.flags = 0; |
138 | msgs.addr = slave_addr / 2; | |
139 | msgs.len = 2; | |
140 | msgs.buf = msg; | |
f045f77b | 141 | return i2c_transfer(&via_i2c_par[adap].adapter, &msgs, 1); |
9f291634 JC |
142 | } |
143 | ||
277d32a3 | 144 | int viafb_i2c_readbytes(u8 adap, u8 slave_addr, u8 index, u8 *buff, int buff_len) |
9f291634 JC |
145 | { |
146 | u8 mm1[] = {0x00}; | |
147 | struct i2c_msg msgs[2]; | |
148 | ||
b052d7f8 JC |
149 | if (!via_i2c_par[adap].is_active) |
150 | return -ENODEV; | |
9f291634 JC |
151 | msgs[0].flags = 0; |
152 | msgs[1].flags = I2C_M_RD; | |
153 | msgs[0].addr = msgs[1].addr = slave_addr / 2; | |
154 | mm1[0] = index; | |
155 | msgs[0].len = 1; msgs[1].len = buff_len; | |
156 | msgs[0].buf = mm1; msgs[1].buf = buff; | |
f045f77b | 157 | return i2c_transfer(&via_i2c_par[adap].adapter, msgs, 2); |
9f291634 JC |
158 | } |
159 | ||
277d32a3 HW |
160 | static int create_i2c_bus(struct i2c_adapter *adapter, |
161 | struct i2c_algo_bit_data *algo, | |
f045f77b | 162 | struct via_port_cfg *adap_cfg, |
277d32a3 | 163 | struct pci_dev *pdev) |
9f291634 | 164 | { |
c774c13d | 165 | DEBUG_MSG(KERN_DEBUG "viafb: creating bus adap=0x%p, algo_bit_data=0x%p, adap_cfg=0x%p\n", adapter, algo, adap_cfg); |
277d32a3 HW |
166 | |
167 | algo->setsda = via_i2c_setsda; | |
168 | algo->setscl = via_i2c_setscl; | |
169 | algo->getsda = via_i2c_getsda; | |
170 | algo->getscl = via_i2c_getscl; | |
171 | algo->udelay = 40; | |
172 | algo->timeout = 20; | |
173 | algo->data = adap_cfg; | |
174 | ||
175 | sprintf(adapter->name, "viafb i2c io_port idx 0x%02x", | |
176 | adap_cfg->ioport_index); | |
177 | adapter->owner = THIS_MODULE; | |
178 | adapter->id = 0x01FFFF; | |
179 | adapter->class = I2C_CLASS_DDC; | |
180 | adapter->algo_data = algo; | |
181 | if (pdev) | |
182 | adapter->dev.parent = &pdev->dev; | |
183 | else | |
184 | adapter->dev.parent = NULL; | |
185 | /* i2c_set_adapdata(adapter, adap_cfg); */ | |
9f291634 JC |
186 | |
187 | /* Raise SCL and SDA */ | |
277d32a3 HW |
188 | via_i2c_setsda(adap_cfg, 1); |
189 | via_i2c_setscl(adap_cfg, 1); | |
9f291634 JC |
190 | udelay(20); |
191 | ||
277d32a3 HW |
192 | return i2c_bit_add_bus(adapter); |
193 | } | |
194 | ||
7582eb9b | 195 | static int viafb_i2c_probe(struct platform_device *platdev) |
277d32a3 HW |
196 | { |
197 | int i, ret; | |
7582eb9b JC |
198 | struct via_port_cfg *configs; |
199 | ||
200 | i2c_vdev = platdev->dev.platform_data; | |
201 | configs = i2c_vdev->port_cfg; | |
277d32a3 | 202 | |
f045f77b JC |
203 | for (i = 0; i < VIAFB_NUM_PORTS; i++) { |
204 | struct via_port_cfg *adap_cfg = configs++; | |
205 | struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i]; | |
277d32a3 | 206 | |
b052d7f8 | 207 | i2c_stuff->is_active = 0; |
f045f77b | 208 | if (adap_cfg->type == 0 || adap_cfg->mode != VIA_MODE_I2C) |
4da62e6c | 209 | continue; |
277d32a3 HW |
210 | ret = create_i2c_bus(&i2c_stuff->adapter, |
211 | &i2c_stuff->algo, adap_cfg, | |
212 | NULL); /* FIXME: PCIDEV */ | |
213 | if (ret < 0) { | |
214 | printk(KERN_ERR "viafb: cannot create i2c bus %u:%d\n", | |
215 | i, ret); | |
b052d7f8 | 216 | continue; /* Still try to make the rest */ |
277d32a3 | 217 | } |
b052d7f8 | 218 | i2c_stuff->is_active = 1; |
277d32a3 HW |
219 | } |
220 | ||
221 | return 0; | |
9f291634 JC |
222 | } |
223 | ||
7582eb9b | 224 | static int viafb_i2c_remove(struct platform_device *platdev) |
9f291634 | 225 | { |
277d32a3 HW |
226 | int i; |
227 | ||
f045f77b JC |
228 | for (i = 0; i < VIAFB_NUM_PORTS; i++) { |
229 | struct via_i2c_stuff *i2c_stuff = &via_i2c_par[i]; | |
230 | /* | |
231 | * Only remove those entries in the array that we've | |
232 | * actually used (and thus initialized algo_data) | |
233 | */ | |
b052d7f8 | 234 | if (i2c_stuff->is_active) |
277d32a3 HW |
235 | i2c_del_adapter(&i2c_stuff->adapter); |
236 | } | |
7582eb9b JC |
237 | return 0; |
238 | } | |
239 | ||
240 | static struct platform_driver via_i2c_driver = { | |
241 | .driver = { | |
242 | .name = "viafb-i2c", | |
243 | }, | |
244 | .probe = viafb_i2c_probe, | |
245 | .remove = viafb_i2c_remove, | |
246 | }; | |
247 | ||
248 | int viafb_i2c_init(void) | |
249 | { | |
250 | return platform_driver_register(&via_i2c_driver); | |
251 | } | |
252 | ||
253 | void viafb_i2c_exit(void) | |
254 | { | |
255 | platform_driver_unregister(&via_i2c_driver); | |
9f291634 | 256 | } |