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