[PATCH] I2C: i2c-amd756-s4882: Improve static mutex initialization
[linux-2.6/mini2440.git] / drivers / i2c / busses / i2c-amd756-s4882.c
blob08e915730caf94505e44bd27845c193c50dd394f
1 /*
2 * i2c-amd756-s4882.c - i2c-amd756 extras for the Tyan S4882 motherboard
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
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.
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.
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.
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
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 #include <linux/mutex.h>
43 extern struct i2c_adapter amd756_smbus;
45 static struct i2c_adapter *s4882_adapter;
46 static struct i2c_algorithm *s4882_algo;
48 /* Wrapper access functions for multiplexed SMBus */
49 static DEFINE_MUTEX(amd756_lock);
51 static s32 amd756_access_virt0(struct i2c_adapter * adap, u16 addr,
52 unsigned short flags, char read_write,
53 u8 command, int size,
54 union i2c_smbus_data * data)
56 int error;
58 /* We exclude the multiplexed addresses */
59 if (addr == 0x4c || (addr & 0xfc) == 0x50 || (addr & 0xfc) == 0x30
60 || addr == 0x18)
61 return -1;
63 mutex_lock(&amd756_lock);
65 error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
66 command, size, data);
68 mutex_unlock(&amd756_lock);
70 return error;
73 /* We remember the last used channels combination so as to only switch
74 channels when it is really needed. This greatly reduces the SMBus
75 overhead, but also assumes that nobody will be writing to the PCA9556
76 in our back. */
77 static u8 last_channels;
79 static inline s32 amd756_access_channel(struct i2c_adapter * adap, u16 addr,
80 unsigned short flags, char read_write,
81 u8 command, int size,
82 union i2c_smbus_data * data,
83 u8 channels)
85 int error;
87 /* We exclude the non-multiplexed addresses */
88 if (addr != 0x4c && (addr & 0xfc) != 0x50 && (addr & 0xfc) != 0x30)
89 return -1;
91 mutex_lock(&amd756_lock);
93 if (last_channels != channels) {
94 union i2c_smbus_data mplxdata;
95 mplxdata.byte = channels;
97 error = amd756_smbus.algo->smbus_xfer(adap, 0x18, 0,
98 I2C_SMBUS_WRITE, 0x01,
99 I2C_SMBUS_BYTE_DATA,
100 &mplxdata);
101 if (error)
102 goto UNLOCK;
103 last_channels = channels;
105 error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
106 command, size, data);
108 UNLOCK:
109 mutex_unlock(&amd756_lock);
110 return error;
113 static s32 amd756_access_virt1(struct i2c_adapter * adap, u16 addr,
114 unsigned short flags, char read_write,
115 u8 command, int size,
116 union i2c_smbus_data * data)
118 /* CPU0: channels 1 and 0 enabled */
119 return amd756_access_channel(adap, addr, flags, read_write, command,
120 size, data, 0x03);
123 static s32 amd756_access_virt2(struct i2c_adapter * adap, u16 addr,
124 unsigned short flags, char read_write,
125 u8 command, int size,
126 union i2c_smbus_data * data)
128 /* CPU1: channels 2 and 5 enabled */
129 return amd756_access_channel(adap, addr, flags, read_write, command,
130 size, data, 0x24);
133 static s32 amd756_access_virt3(struct i2c_adapter * adap, u16 addr,
134 unsigned short flags, char read_write,
135 u8 command, int size,
136 union i2c_smbus_data * data)
138 /* CPU2: channels 3 and 6 enabled */
139 return amd756_access_channel(adap, addr, flags, read_write, command,
140 size, data, 0x48);
143 static s32 amd756_access_virt4(struct i2c_adapter * adap, u16 addr,
144 unsigned short flags, char read_write,
145 u8 command, int size,
146 union i2c_smbus_data * data)
148 /* CPU3: channels 4 and 7 enabled */
149 return amd756_access_channel(adap, addr, flags, read_write, command,
150 size, data, 0x90);
153 static int __init amd756_s4882_init(void)
155 int i, error;
156 union i2c_smbus_data ioconfig;
158 /* Unregister physical bus */
159 error = i2c_del_adapter(&amd756_smbus);
160 if (error) {
161 if (error == -EINVAL)
162 error = -ENODEV;
163 else
164 dev_err(&amd756_smbus.dev, "Physical bus removal "
165 "failed\n");
166 goto ERROR0;
169 printk(KERN_INFO "Enabling SMBus multiplexing for Tyan S4882\n");
170 /* Define the 5 virtual adapters and algorithms structures */
171 if (!(s4882_adapter = kzalloc(5 * sizeof(struct i2c_adapter),
172 GFP_KERNEL))) {
173 error = -ENOMEM;
174 goto ERROR1;
176 if (!(s4882_algo = kzalloc(5 * sizeof(struct i2c_algorithm),
177 GFP_KERNEL))) {
178 error = -ENOMEM;
179 goto ERROR2;
182 /* Fill in the new structures */
183 s4882_algo[0] = *(amd756_smbus.algo);
184 s4882_algo[0].smbus_xfer = amd756_access_virt0;
185 s4882_adapter[0] = amd756_smbus;
186 s4882_adapter[0].algo = s4882_algo;
187 for (i = 1; i < 5; i++) {
188 s4882_algo[i] = *(amd756_smbus.algo);
189 s4882_adapter[i] = amd756_smbus;
190 sprintf(s4882_adapter[i].name,
191 "SMBus 8111 adapter (CPU%d)", i-1);
192 s4882_adapter[i].algo = s4882_algo+i;
194 s4882_algo[1].smbus_xfer = amd756_access_virt1;
195 s4882_algo[2].smbus_xfer = amd756_access_virt2;
196 s4882_algo[3].smbus_xfer = amd756_access_virt3;
197 s4882_algo[4].smbus_xfer = amd756_access_virt4;
199 /* Configure the PCA9556 multiplexer */
200 ioconfig.byte = 0x00; /* All I/O to output mode */
201 error = amd756_smbus.algo->smbus_xfer(&amd756_smbus, 0x18, 0,
202 I2C_SMBUS_WRITE, 0x03,
203 I2C_SMBUS_BYTE_DATA, &ioconfig);
204 if (error) {
205 dev_err(&amd756_smbus.dev, "PCA9556 configuration failed\n");
206 error = -EIO;
207 goto ERROR3;
210 /* Register virtual adapters */
211 for (i = 0; i < 5; i++) {
212 error = i2c_add_adapter(s4882_adapter+i);
213 if (error) {
214 dev_err(&amd756_smbus.dev,
215 "Virtual adapter %d registration "
216 "failed, module not inserted\n", i);
217 for (i--; i >= 0; i--)
218 i2c_del_adapter(s4882_adapter+i);
219 goto ERROR3;
223 return 0;
225 ERROR3:
226 kfree(s4882_algo);
227 s4882_algo = NULL;
228 ERROR2:
229 kfree(s4882_adapter);
230 s4882_adapter = NULL;
231 ERROR1:
232 i2c_del_adapter(&amd756_smbus);
233 ERROR0:
234 return error;
237 static void __exit amd756_s4882_exit(void)
239 if (s4882_adapter) {
240 int i;
242 for (i = 0; i < 5; i++)
243 i2c_del_adapter(s4882_adapter+i);
244 kfree(s4882_adapter);
245 s4882_adapter = NULL;
247 kfree(s4882_algo);
248 s4882_algo = NULL;
250 /* Restore physical bus */
251 if (i2c_add_adapter(&amd756_smbus))
252 dev_err(&amd756_smbus.dev, "Physical bus restoration "
253 "failed\n");
256 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
257 MODULE_DESCRIPTION("S4882 SMBus multiplexing");
258 MODULE_LICENSE("GPL");
260 module_init(amd756_s4882_init);
261 module_exit(amd756_s4882_exit);