blob: 4e553e8c5cbada81053978ba6b1f12bdf2b1404a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * i2c-amd756-s4882.c - i2c-amd756 extras for the Tyan S4882 motherboard
3 *
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License 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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21/*
22 * We select the channels by sending commands to the Philips
23 * PCA9556 chip at I2C address 0x18. The main adapter is used for
24 * the non-multiplexed part of the bus, and 4 virtual adapters
25 * are defined for the multiplexed addresses: 0x50-0x53 (memory
26 * module EEPROM) located on channels 1-4, and 0x4c (LM63)
27 * located on multiplexed channels 0 and 5-7. We define one
28 * virtual adapter per CPU, which corresponds to two multiplexed
29 * channels:
30 * CPU0: virtual adapter 1, channels 1 and 0
31 * CPU1: virtual adapter 2, channels 2 and 5
32 * CPU2: virtual adapter 3, channels 3 and 6
33 * CPU3: virtual adapter 4, channels 4 and 7
34 */
35
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/slab.h>
39#include <linux/init.h>
40#include <linux/i2c.h>
41
42extern struct i2c_adapter amd756_smbus;
43
44static struct i2c_adapter *s4882_adapter;
45static struct i2c_algorithm *s4882_algo;
46
47/* Wrapper access functions for multiplexed SMBus */
48static struct semaphore amd756_lock;
49
50static s32 amd756_access_virt0(struct i2c_adapter * adap, u16 addr,
51 unsigned short flags, char read_write,
52 u8 command, int size,
53 union i2c_smbus_data * data)
54{
55 int error;
56
57 /* We exclude the multiplexed addresses */
58 if (addr == 0x4c || (addr & 0xfc) == 0x50 || (addr & 0xfc) == 0x30
59 || addr == 0x18)
60 return -1;
61
62 down(&amd756_lock);
63
64 error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
65 command, size, data);
66
67 up(&amd756_lock);
68
69 return error;
70}
71
72/* We remember the last used channels combination so as to only switch
73 channels when it is really needed. This greatly reduces the SMBus
74 overhead, but also assumes that nobody will be writing to the PCA9556
75 in our back. */
76static u8 last_channels;
77
78static inline s32 amd756_access_channel(struct i2c_adapter * adap, u16 addr,
79 unsigned short flags, char read_write,
80 u8 command, int size,
81 union i2c_smbus_data * data,
82 u8 channels)
83{
84 int error;
85
86 /* We exclude the non-multiplexed addresses */
87 if (addr != 0x4c && (addr & 0xfc) != 0x50 && (addr & 0xfc) != 0x30)
88 return -1;
89
90 down(&amd756_lock);
91
92 if (last_channels != channels) {
93 union i2c_smbus_data mplxdata;
94 mplxdata.byte = channels;
95
96 error = amd756_smbus.algo->smbus_xfer(adap, 0x18, 0,
97 I2C_SMBUS_WRITE, 0x01,
98 I2C_SMBUS_BYTE_DATA,
99 &mplxdata);
100 if (error)
101 goto UNLOCK;
102 last_channels = channels;
103 }
104 error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
105 command, size, data);
106
107UNLOCK:
108 up(&amd756_lock);
109 return error;
110}
111
112static s32 amd756_access_virt1(struct i2c_adapter * adap, u16 addr,
113 unsigned short flags, char read_write,
114 u8 command, int size,
115 union i2c_smbus_data * data)
116{
117 /* CPU0: channels 1 and 0 enabled */
118 return amd756_access_channel(adap, addr, flags, read_write, command,
119 size, data, 0x03);
120}
121
122static s32 amd756_access_virt2(struct i2c_adapter * adap, u16 addr,
123 unsigned short flags, char read_write,
124 u8 command, int size,
125 union i2c_smbus_data * data)
126{
127 /* CPU1: channels 2 and 5 enabled */
128 return amd756_access_channel(adap, addr, flags, read_write, command,
129 size, data, 0x24);
130}
131
132static s32 amd756_access_virt3(struct i2c_adapter * adap, u16 addr,
133 unsigned short flags, char read_write,
134 u8 command, int size,
135 union i2c_smbus_data * data)
136{
137 /* CPU2: channels 3 and 6 enabled */
138 return amd756_access_channel(adap, addr, flags, read_write, command,
139 size, data, 0x48);
140}
141
142static s32 amd756_access_virt4(struct i2c_adapter * adap, u16 addr,
143 unsigned short flags, char read_write,
144 u8 command, int size,
145 union i2c_smbus_data * data)
146{
147 /* CPU3: channels 4 and 7 enabled */
148 return amd756_access_channel(adap, addr, flags, read_write, command,
149 size, data, 0x90);
150}
151
152static int __init amd756_s4882_init(void)
153{
154 int i, error;
155 union i2c_smbus_data ioconfig;
156
157 /* Unregister physical bus */
158 error = i2c_del_adapter(&amd756_smbus);
159 if (error) {
160 if (error == -EINVAL)
161 error = -ENODEV;
162 else
163 dev_err(&amd756_smbus.dev, "Physical bus removal "
164 "failed\n");
165 goto ERROR0;
166 }
167
168 printk(KERN_INFO "Enabling SMBus multiplexing for Tyan S4882\n");
169 init_MUTEX(&amd756_lock);
170
171 /* Define the 5 virtual adapters and algorithms structures */
172 if (!(s4882_adapter = kmalloc(5 * sizeof(struct i2c_adapter),
173 GFP_KERNEL))) {
174 error = -ENOMEM;
175 goto ERROR1;
176 }
177 if (!(s4882_algo = kmalloc(5 * sizeof(struct i2c_algorithm),
178 GFP_KERNEL))) {
179 error = -ENOMEM;
180 goto ERROR2;
181 }
182
183 /* Fill in the new structures */
184 s4882_algo[0] = *(amd756_smbus.algo);
185 s4882_algo[0].smbus_xfer = amd756_access_virt0;
186 s4882_adapter[0] = amd756_smbus;
187 s4882_adapter[0].algo = s4882_algo;
188 for (i = 1; i < 5; i++) {
189 s4882_algo[i] = *(amd756_smbus.algo);
190 s4882_adapter[i] = amd756_smbus;
191 sprintf(s4882_adapter[i].name,
192 "SMBus 8111 adapter (CPU%d)", i-1);
193 s4882_adapter[i].algo = s4882_algo+i;
194 }
195 s4882_algo[1].smbus_xfer = amd756_access_virt1;
196 s4882_algo[2].smbus_xfer = amd756_access_virt2;
197 s4882_algo[3].smbus_xfer = amd756_access_virt3;
198 s4882_algo[4].smbus_xfer = amd756_access_virt4;
199
200 /* Configure the PCA9556 multiplexer */
201 ioconfig.byte = 0x00; /* All I/O to output mode */
202 error = amd756_smbus.algo->smbus_xfer(&amd756_smbus, 0x18, 0,
203 I2C_SMBUS_WRITE, 0x03,
204 I2C_SMBUS_BYTE_DATA, &ioconfig);
205 if (error) {
206 dev_err(&amd756_smbus.dev, "PCA9556 configuration failed\n");
207 error = -EIO;
208 goto ERROR3;
209 }
210
211 /* Register virtual adapters */
212 for (i = 0; i < 5; i++) {
213 error = i2c_add_adapter(s4882_adapter+i);
214 if (error) {
215 dev_err(&amd756_smbus.dev,
216 "Virtual adapter %d registration "
217 "failed, module not inserted\n", i);
218 for (i--; i >= 0; i--)
219 i2c_del_adapter(s4882_adapter+i);
220 goto ERROR3;
221 }
222 }
223
224 return 0;
225
226ERROR3:
227 kfree(s4882_algo);
228 s4882_algo = NULL;
229ERROR2:
230 kfree(s4882_adapter);
231 s4882_adapter = NULL;
232ERROR1:
233 i2c_del_adapter(&amd756_smbus);
234ERROR0:
235 return error;
236}
237
238static void __exit amd756_s4882_exit(void)
239{
240 if (s4882_adapter) {
241 int i;
242
243 for (i = 0; i < 5; i++)
244 i2c_del_adapter(s4882_adapter+i);
245 kfree(s4882_adapter);
246 s4882_adapter = NULL;
247 }
248 if (s4882_algo) {
249 kfree(s4882_algo);
250 s4882_algo = NULL;
251 }
252
253 /* Restore physical bus */
254 if (i2c_add_adapter(&amd756_smbus))
255 dev_err(&amd756_smbus.dev, "Physical bus restoration "
256 "failed\n");
257}
258
259MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
260MODULE_DESCRIPTION("S4882 SMBus multiplexing");
261MODULE_LICENSE("GPL");
262
263module_init(amd756_s4882_init);
264module_exit(amd756_s4882_exit);