-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathi2c-acpi-sbus.c
228 lines (196 loc) · 5.64 KB
/
i2c-acpi-sbus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Copyright 2013 David Bartley <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
Implements an i2c bus on top of the ACPI SBUS device.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/acpi.h>
#include <linux/i2c.h>
static s32 acpi_sbus_access(struct i2c_adapter *adapter, u16 addr,
unsigned short flags, char read_write,
u8 command, int size, union i2c_smbus_data *data)
{
acpi_handle handle = adapter->algo_data;
char *cmd;
struct acpi_object_list input;
union acpi_object in_params[4];
acpi_status status;
struct acpi_buffer out_buf = {ACPI_ALLOCATE_BUFFER, NULL};
union acpi_object *out_obj;
unsigned long long out_int;
if (size == I2C_SMBUS_BYTE) {
if (read_write == I2C_SMBUS_READ) {
cmd = "SRXB";
input.count = 1;
} else {
cmd = "SSXB";
input.count = 2;
in_params[1].type = ACPI_TYPE_INTEGER;
in_params[1].integer.value = command;
}
} else {
switch (size) {
case I2C_SMBUS_BYTE_DATA:
if (read_write == I2C_SMBUS_READ) {
cmd = "SRDB";
input.count = 2;
} else {
cmd = "SWRB";
input.count = 3;
in_params[2].type = ACPI_TYPE_INTEGER;
in_params[2].integer.value = data->byte;
}
break;
case I2C_SMBUS_WORD_DATA:
if (read_write == I2C_SMBUS_READ) {
cmd = "SRDW";
input.count = 2;
} else {
cmd = "SWRW";
input.count = 3;
in_params[2].type = ACPI_TYPE_INTEGER;
in_params[2].integer.value = data->word;
}
break;
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
cmd = "SBLR";
input.count = 2;
} else {
cmd = "SBLW";
input.count = 3;
in_params[2].buffer.length = data->block[0];
in_params[2].buffer.pointer = data->block + 1;
}
break;
default:
dev_warn(&adapter->dev, "Unsupported size %d\n", size);
return -EOPNOTSUPP;
}
in_params[1].type = ACPI_TYPE_INTEGER;
in_params[1].integer.value = command;
}
in_params[0].type = ACPI_TYPE_INTEGER;
in_params[0].integer.value = ((addr & 0x7f) << 1) | (read_write & 0x01);
input.pointer = in_params;
if (read_write == I2C_SMBUS_WRITE || size != I2C_SMBUS_BLOCK_DATA) {
status = acpi_evaluate_integer(handle, cmd, &input, &out_int);
} else {
status = acpi_evaluate_object(handle, cmd, &input, &out_buf);
}
if (ACPI_FAILURE(status))
return -EIO;
if (read_write == I2C_SMBUS_WRITE)
return (out_int == 0) ? -EIO : 0;
switch (size) {
case I2C_SMBUS_BYTE:
case I2C_SMBUS_BYTE_DATA:
if (out_int > 0xff)
return -EIO;
data->byte = out_int;
break;
case I2C_SMBUS_WORD_DATA:
if (out_int > 0xffff)
return -EIO;
data->word = out_int;
break;
case I2C_SMBUS_BLOCK_DATA:
if (out_buf.length < 1 || out_buf.length > I2C_SMBUS_BLOCK_MAX)
return -EPROTO;
out_obj = out_buf.pointer;
if (out_obj->type != ACPI_TYPE_BUFFER)
return -EIO;
memcpy(data->block, out_obj->buffer.pointer,
out_obj->buffer.length);
break;
}
return 0;
}
static u32 acpi_sbus_func(struct i2c_adapter *adapter)
{
return I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA;
}
static const struct i2c_algorithm smbus_algorithm = {
.smbus_xfer = acpi_sbus_access,
.functionality = acpi_sbus_func,
};
struct i2c_adapter acpi_sbus = {
.owner = THIS_MODULE,
.class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
.algo = &smbus_algorithm,
.name = "ACPI SBUS i2c",
};
static bool added_acpi_sbus;
static acpi_status find_acpi_sbus_devices(acpi_handle handle, u32 level,
void *context, void **return_value)
{
acpi_status status;
char node_name[5];
struct acpi_buffer buffer = { sizeof(node_name), node_name };
struct acpi_device *device = NULL;
status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
if (ACPI_FAILURE(status) || strcmp("SBUS", node_name) != 0)
return AE_OK;
if (added_acpi_sbus) {
pr_warn("ACPI: only one SBUS device supported!\n");
return AE_ALREADY_EXISTS;
}
acpi_bus_get_device(handle, &device);
if (!device) {
pr_err("ACPI: failed to get device for SBUS!\n");
return AE_NOT_FOUND;
}
acpi_sbus.dev.parent = &device->dev;
acpi_sbus.algo_data = handle;
if (i2c_add_adapter(&acpi_sbus)) {
pr_err("ACPI: failed to add i2c adapter for SBUS device!\n");
return AE_ERROR;
}
pr_info("ACPI: found SBUS i2c device.\n");
added_acpi_sbus = true;
return AE_OK;
}
static int __init i2c_acpi_sbus_init(void)
{
acpi_status status;
if (acpi_disabled)
return 0;
status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
ACPI_UINT32_MAX, find_acpi_sbus_devices,
NULL, NULL, NULL);
if (status == AE_OK) {
return 0;
} else if (status == AE_ALREADY_EXISTS) {
return -EEXIST;
} else if (status == AE_NOT_FOUND) {
return -ENXIO;
} else {
return -EIO;
}
return 0;
}
static void __exit i2c_acpi_sbus_exit(void)
{
if (added_acpi_sbus) {
i2c_del_adapter(&acpi_sbus);
}
}
MODULE_AUTHOR("David Bartley <[email protected]>");
MODULE_DESCRIPTION("ACPI SBUS i2c device driver");
MODULE_LICENSE("GPL");
module_init(i2c_acpi_sbus_init);
module_exit(i2c_acpi_sbus_exit);