blob: 72872d1e63efb6c864e49c6bec333081e5e3c3b6 [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 *
Jean Delvareee56d972008-04-29 23:11:38 +02004 * Copyright (C) 2004, 2008 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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>
Ingo Molnar5c085d32006-01-18 23:16:04 +010041#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43extern struct i2c_adapter amd756_smbus;
44
45static struct i2c_adapter *s4882_adapter;
46static struct i2c_algorithm *s4882_algo;
47
48/* Wrapper access functions for multiplexed SMBus */
Jean Delvared7d2fe32006-02-27 23:09:22 +010049static DEFINE_MUTEX(amd756_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static 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)
55{
56 int error;
57
58 /* We exclude the multiplexed addresses */
59 if (addr == 0x4c || (addr & 0xfc) == 0x50 || (addr & 0xfc) == 0x30
60 || addr == 0x18)
David Brownell97140342008-07-14 22:38:25 +020061 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Ingo Molnar5c085d32006-01-18 23:16:04 +010063 mutex_lock(&amd756_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
66 command, size, data);
67
Ingo Molnar5c085d32006-01-18 23:16:04 +010068 mutex_unlock(&amd756_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 return error;
71}
72
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. */
77static u8 last_channels;
78
79static 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)
84{
85 int error;
86
87 /* We exclude the non-multiplexed addresses */
88 if (addr != 0x4c && (addr & 0xfc) != 0x50 && (addr & 0xfc) != 0x30)
David Brownell97140342008-07-14 22:38:25 +020089 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Ingo Molnar5c085d32006-01-18 23:16:04 +010091 mutex_lock(&amd756_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 if (last_channels != channels) {
94 union i2c_smbus_data mplxdata;
95 mplxdata.byte = channels;
96
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;
104 }
105 error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
106 command, size, data);
107
108UNLOCK:
Ingo Molnar5c085d32006-01-18 23:16:04 +0100109 mutex_unlock(&amd756_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return error;
111}
112
113static 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)
117{
118 /* CPU0: channels 1 and 0 enabled */
119 return amd756_access_channel(adap, addr, flags, read_write, command,
120 size, data, 0x03);
121}
122
123static 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)
127{
128 /* CPU1: channels 2 and 5 enabled */
129 return amd756_access_channel(adap, addr, flags, read_write, command,
130 size, data, 0x24);
131}
132
133static 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)
137{
138 /* CPU2: channels 3 and 6 enabled */
139 return amd756_access_channel(adap, addr, flags, read_write, command,
140 size, data, 0x48);
141}
142
143static 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)
147{
148 /* CPU3: channels 4 and 7 enabled */
149 return amd756_access_channel(adap, addr, flags, read_write, command,
150 size, data, 0x90);
151}
152
153static int __init amd756_s4882_init(void)
154{
155 int i, error;
156 union i2c_smbus_data ioconfig;
157
Jean Delvarebd4bc3d2008-07-16 19:30:05 +0200158 /* Configure the PCA9556 multiplexer */
159 ioconfig.byte = 0x00; /* All I/O to output mode */
160 error = i2c_smbus_xfer(&amd756_smbus, 0x18, 0, I2C_SMBUS_WRITE, 0x03,
161 I2C_SMBUS_BYTE_DATA, &ioconfig);
162 if (error) {
163 dev_err(&amd756_smbus.dev, "PCA9556 configuration failed\n");
164 error = -EIO;
165 goto ERROR0;
166 }
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* Unregister physical bus */
169 error = i2c_del_adapter(&amd756_smbus);
170 if (error) {
171 if (error == -EINVAL)
172 error = -ENODEV;
173 else
174 dev_err(&amd756_smbus.dev, "Physical bus removal "
175 "failed\n");
176 goto ERROR0;
177 }
178
179 printk(KERN_INFO "Enabling SMBus multiplexing for Tyan S4882\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* Define the 5 virtual adapters and algorithms structures */
Jean Delvaredeb875c72005-10-17 23:14:31 +0200181 if (!(s4882_adapter = kzalloc(5 * sizeof(struct i2c_adapter),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 GFP_KERNEL))) {
183 error = -ENOMEM;
184 goto ERROR1;
185 }
Jean Delvaredeb875c72005-10-17 23:14:31 +0200186 if (!(s4882_algo = kzalloc(5 * sizeof(struct i2c_algorithm),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 GFP_KERNEL))) {
188 error = -ENOMEM;
189 goto ERROR2;
190 }
191
192 /* Fill in the new structures */
193 s4882_algo[0] = *(amd756_smbus.algo);
194 s4882_algo[0].smbus_xfer = amd756_access_virt0;
195 s4882_adapter[0] = amd756_smbus;
196 s4882_adapter[0].algo = s4882_algo;
Jean Delvare12a917f2007-02-13 22:09:03 +0100197 s4882_adapter[0].dev.parent = amd756_smbus.dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 for (i = 1; i < 5; i++) {
199 s4882_algo[i] = *(amd756_smbus.algo);
200 s4882_adapter[i] = amd756_smbus;
201 sprintf(s4882_adapter[i].name,
202 "SMBus 8111 adapter (CPU%d)", i-1);
203 s4882_adapter[i].algo = s4882_algo+i;
Jean Delvare12a917f2007-02-13 22:09:03 +0100204 s4882_adapter[i].dev.parent = amd756_smbus.dev.parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 s4882_algo[1].smbus_xfer = amd756_access_virt1;
207 s4882_algo[2].smbus_xfer = amd756_access_virt2;
208 s4882_algo[3].smbus_xfer = amd756_access_virt3;
209 s4882_algo[4].smbus_xfer = amd756_access_virt4;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Register virtual adapters */
212 for (i = 0; i < 5; i++) {
213 error = i2c_add_adapter(s4882_adapter+i);
214 if (error) {
Jean Delvarebd4bc3d2008-07-16 19:30:05 +0200215 printk(KERN_ERR "i2c-amd756-s4882: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 "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:
Jean Delvareee56d972008-04-29 23:11:38 +0200233 /* Restore physical bus */
234 i2c_add_adapter(&amd756_smbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235ERROR0:
236 return error;
237}
238
239static void __exit amd756_s4882_exit(void)
240{
241 if (s4882_adapter) {
242 int i;
243
244 for (i = 0; i < 5; i++)
245 i2c_del_adapter(s4882_adapter+i);
246 kfree(s4882_adapter);
247 s4882_adapter = NULL;
248 }
Jesper Juhl6044ec82005-11-07 01:01:32 -0800249 kfree(s4882_algo);
250 s4882_algo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Restore physical bus */
253 if (i2c_add_adapter(&amd756_smbus))
Jean Delvarebd4bc3d2008-07-16 19:30:05 +0200254 printk(KERN_ERR "i2c-amd756-s4882: "
255 "Physical bus restoration failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
259MODULE_DESCRIPTION("S4882 SMBus multiplexing");
260MODULE_LICENSE("GPL");
261
262module_init(amd756_s4882_init);
263module_exit(amd756_s4882_exit);