2 * SMBus driver for ACPI SMBus CMI
4 * Copyright (C) 2009 Crane Cai <crane.cai@amd.com>
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 version 2.
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/stddef.h>
15 #include <linux/i2c.h>
16 #include <linux/acpi.h>
18 #define ACPI_SMBUS_HC_CLASS "smbus"
19 #define ACPI_SMBUS_HC_DEVICE_NAME "cmi"
21 ACPI_MODULE_NAME("smbus_cmi");
23 struct smbus_methods_t
{
29 struct acpi_smbus_cmi
{
31 struct i2c_adapter adapter
;
35 struct smbus_methods_t
*methods
;
38 static const struct smbus_methods_t smbus_methods
= {
44 /* Some IBM BIOSes omit the leading underscore */
45 static const struct smbus_methods_t ibm_smbus_methods
= {
51 static const struct acpi_device_id acpi_smbus_cmi_ids
[] = {
52 {"SMBUS01", (kernel_ulong_t
)&smbus_methods
},
53 {ACPI_SMBUS_IBM_HID
, (kernel_ulong_t
)&ibm_smbus_methods
},
56 MODULE_DEVICE_TABLE(acpi
, acpi_smbus_cmi_ids
);
58 #define ACPI_SMBUS_STATUS_OK 0x00
59 #define ACPI_SMBUS_STATUS_FAIL 0x07
60 #define ACPI_SMBUS_STATUS_DNAK 0x10
61 #define ACPI_SMBUS_STATUS_DERR 0x11
62 #define ACPI_SMBUS_STATUS_CMD_DENY 0x12
63 #define ACPI_SMBUS_STATUS_UNKNOWN 0x13
64 #define ACPI_SMBUS_STATUS_ACC_DENY 0x17
65 #define ACPI_SMBUS_STATUS_TIMEOUT 0x18
66 #define ACPI_SMBUS_STATUS_NOTSUP 0x19
67 #define ACPI_SMBUS_STATUS_BUSY 0x1a
68 #define ACPI_SMBUS_STATUS_PEC 0x1f
70 #define ACPI_SMBUS_PRTCL_WRITE 0x00
71 #define ACPI_SMBUS_PRTCL_READ 0x01
72 #define ACPI_SMBUS_PRTCL_QUICK 0x02
73 #define ACPI_SMBUS_PRTCL_BYTE 0x04
74 #define ACPI_SMBUS_PRTCL_BYTE_DATA 0x06
75 #define ACPI_SMBUS_PRTCL_WORD_DATA 0x08
76 #define ACPI_SMBUS_PRTCL_BLOCK_DATA 0x0a
80 acpi_smbus_cmi_access(struct i2c_adapter
*adap
, u16 addr
, unsigned short flags
,
81 char read_write
, u8 command
, int size
,
82 union i2c_smbus_data
*data
)
85 struct acpi_smbus_cmi
*smbus_cmi
= adap
->algo_data
;
86 unsigned char protocol
;
87 acpi_status status
= 0;
88 struct acpi_object_list input
;
89 union acpi_object mt_params
[5];
90 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
91 union acpi_object
*obj
;
92 union acpi_object
*pkg
;
96 dev_dbg(&adap
->dev
, "access size: %d %s\n", size
,
97 (read_write
) ? "READ" : "WRITE");
100 protocol
= ACPI_SMBUS_PRTCL_QUICK
;
102 if (read_write
== I2C_SMBUS_WRITE
) {
103 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
104 mt_params
[3].integer
.value
= 0;
105 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
106 mt_params
[4].integer
.value
= 0;
111 protocol
= ACPI_SMBUS_PRTCL_BYTE
;
112 if (read_write
== I2C_SMBUS_WRITE
) {
113 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
114 mt_params
[3].integer
.value
= 0;
115 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
116 mt_params
[4].integer
.value
= 0;
122 case I2C_SMBUS_BYTE_DATA
:
123 protocol
= ACPI_SMBUS_PRTCL_BYTE_DATA
;
124 if (read_write
== I2C_SMBUS_WRITE
) {
125 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
126 mt_params
[3].integer
.value
= 1;
127 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
128 mt_params
[4].integer
.value
= data
->byte
;
132 case I2C_SMBUS_WORD_DATA
:
133 protocol
= ACPI_SMBUS_PRTCL_WORD_DATA
;
134 if (read_write
== I2C_SMBUS_WRITE
) {
135 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
136 mt_params
[3].integer
.value
= 2;
137 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
138 mt_params
[4].integer
.value
= data
->word
;
142 case I2C_SMBUS_BLOCK_DATA
:
143 protocol
= ACPI_SMBUS_PRTCL_BLOCK_DATA
;
144 if (read_write
== I2C_SMBUS_WRITE
) {
145 len
= data
->block
[0];
146 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
148 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
149 mt_params
[3].integer
.value
= len
;
150 mt_params
[4].type
= ACPI_TYPE_BUFFER
;
151 mt_params
[4].buffer
.pointer
= data
->block
+ 1;
156 dev_warn(&adap
->dev
, "Unsupported transaction %d\n", size
);
160 if (read_write
== I2C_SMBUS_READ
) {
161 protocol
|= ACPI_SMBUS_PRTCL_READ
;
162 method
= smbus_cmi
->methods
->mt_sbr
;
165 protocol
|= ACPI_SMBUS_PRTCL_WRITE
;
166 method
= smbus_cmi
->methods
->mt_sbw
;
170 input
.pointer
= mt_params
;
171 mt_params
[0].type
= ACPI_TYPE_INTEGER
;
172 mt_params
[0].integer
.value
= protocol
;
173 mt_params
[1].type
= ACPI_TYPE_INTEGER
;
174 mt_params
[1].integer
.value
= addr
;
175 mt_params
[2].type
= ACPI_TYPE_INTEGER
;
176 mt_params
[2].integer
.value
= command
;
178 status
= acpi_evaluate_object(smbus_cmi
->handle
, method
, &input
,
180 if (ACPI_FAILURE(status
)) {
181 ACPI_ERROR((AE_INFO
, "Evaluating %s: %i", method
, status
));
185 pkg
= buffer
.pointer
;
186 if (pkg
&& pkg
->type
== ACPI_TYPE_PACKAGE
)
187 obj
= pkg
->package
.elements
;
189 ACPI_ERROR((AE_INFO
, "Invalid argument type"));
193 if (obj
== NULL
|| obj
->type
!= ACPI_TYPE_INTEGER
) {
194 ACPI_ERROR((AE_INFO
, "Invalid argument type"));
199 result
= obj
->integer
.value
;
200 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "%s return status: %i\n",
204 case ACPI_SMBUS_STATUS_OK
:
207 case ACPI_SMBUS_STATUS_BUSY
:
210 case ACPI_SMBUS_STATUS_TIMEOUT
:
213 case ACPI_SMBUS_STATUS_DNAK
:
221 if (read_write
== I2C_SMBUS_WRITE
|| size
== I2C_SMBUS_QUICK
)
224 obj
= pkg
->package
.elements
+ 1;
225 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
226 ACPI_ERROR((AE_INFO
, "Invalid argument type"));
231 len
= obj
->integer
.value
;
232 obj
= pkg
->package
.elements
+ 2;
235 case I2C_SMBUS_BYTE_DATA
:
236 case I2C_SMBUS_WORD_DATA
:
237 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
238 ACPI_ERROR((AE_INFO
, "Invalid argument type"));
243 data
->word
= obj
->integer
.value
;
245 data
->byte
= obj
->integer
.value
;
247 case I2C_SMBUS_BLOCK_DATA
:
248 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
249 ACPI_ERROR((AE_INFO
, "Invalid argument type"));
253 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
255 data
->block
[0] = len
;
256 memcpy(data
->block
+ 1, obj
->buffer
.pointer
, len
);
261 kfree(buffer
.pointer
);
262 dev_dbg(&adap
->dev
, "Transaction status: %i\n", result
);
266 static u32
acpi_smbus_cmi_func(struct i2c_adapter
*adapter
)
268 struct acpi_smbus_cmi
*smbus_cmi
= adapter
->algo_data
;
271 ret
= smbus_cmi
->cap_read
| smbus_cmi
->cap_write
?
272 I2C_FUNC_SMBUS_QUICK
: 0;
274 ret
|= smbus_cmi
->cap_read
?
275 (I2C_FUNC_SMBUS_READ_BYTE
|
276 I2C_FUNC_SMBUS_READ_BYTE_DATA
|
277 I2C_FUNC_SMBUS_READ_WORD_DATA
|
278 I2C_FUNC_SMBUS_READ_BLOCK_DATA
) : 0;
280 ret
|= smbus_cmi
->cap_write
?
281 (I2C_FUNC_SMBUS_WRITE_BYTE
|
282 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
283 I2C_FUNC_SMBUS_WRITE_WORD_DATA
|
284 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
) : 0;
289 static const struct i2c_algorithm acpi_smbus_cmi_algorithm
= {
290 .smbus_xfer
= acpi_smbus_cmi_access
,
291 .functionality
= acpi_smbus_cmi_func
,
295 static int acpi_smbus_cmi_add_cap(struct acpi_smbus_cmi
*smbus_cmi
,
298 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
299 union acpi_object
*obj
;
302 if (!strcmp(name
, smbus_cmi
->methods
->mt_info
)) {
303 status
= acpi_evaluate_object(smbus_cmi
->handle
,
304 smbus_cmi
->methods
->mt_info
,
306 if (ACPI_FAILURE(status
)) {
307 ACPI_ERROR((AE_INFO
, "Evaluating %s: %i",
308 smbus_cmi
->methods
->mt_info
, status
));
312 obj
= buffer
.pointer
;
313 if (obj
&& obj
->type
== ACPI_TYPE_PACKAGE
)
314 obj
= obj
->package
.elements
;
316 ACPI_ERROR((AE_INFO
, "Invalid argument type"));
317 kfree(buffer
.pointer
);
321 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
322 ACPI_ERROR((AE_INFO
, "Invalid argument type"));
323 kfree(buffer
.pointer
);
326 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "SMBus CMI Version %x"
327 "\n", (int)obj
->integer
.value
));
329 kfree(buffer
.pointer
);
330 smbus_cmi
->cap_info
= 1;
331 } else if (!strcmp(name
, smbus_cmi
->methods
->mt_sbr
))
332 smbus_cmi
->cap_read
= 1;
333 else if (!strcmp(name
, smbus_cmi
->methods
->mt_sbw
))
334 smbus_cmi
->cap_write
= 1;
336 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Unsupported CMI method: %s\n",
342 static acpi_status
acpi_smbus_cmi_query_methods(acpi_handle handle
, u32 level
,
343 void *context
, void **return_value
)
346 struct acpi_buffer buffer
= { sizeof(node_name
), node_name
};
347 struct acpi_smbus_cmi
*smbus_cmi
= context
;
350 status
= acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
);
352 if (ACPI_SUCCESS(status
))
353 acpi_smbus_cmi_add_cap(smbus_cmi
, node_name
);
358 static int acpi_smbus_cmi_add(struct acpi_device
*device
)
360 struct acpi_smbus_cmi
*smbus_cmi
;
361 const struct acpi_device_id
*id
;
363 smbus_cmi
= kzalloc(sizeof(struct acpi_smbus_cmi
), GFP_KERNEL
);
367 smbus_cmi
->handle
= device
->handle
;
368 strcpy(acpi_device_name(device
), ACPI_SMBUS_HC_DEVICE_NAME
);
369 strcpy(acpi_device_class(device
), ACPI_SMBUS_HC_CLASS
);
370 device
->driver_data
= smbus_cmi
;
371 smbus_cmi
->cap_info
= 0;
372 smbus_cmi
->cap_read
= 0;
373 smbus_cmi
->cap_write
= 0;
375 for (id
= acpi_smbus_cmi_ids
; id
->id
[0]; id
++)
376 if (!strcmp(id
->id
, acpi_device_hid(device
)))
378 (struct smbus_methods_t
*) id
->driver_data
;
380 acpi_walk_namespace(ACPI_TYPE_METHOD
, smbus_cmi
->handle
, 1,
381 acpi_smbus_cmi_query_methods
, NULL
, smbus_cmi
, NULL
);
383 if (smbus_cmi
->cap_info
== 0)
386 snprintf(smbus_cmi
->adapter
.name
, sizeof(smbus_cmi
->adapter
.name
),
387 "SMBus CMI adapter %s",
388 acpi_device_name(device
));
389 smbus_cmi
->adapter
.owner
= THIS_MODULE
;
390 smbus_cmi
->adapter
.algo
= &acpi_smbus_cmi_algorithm
;
391 smbus_cmi
->adapter
.algo_data
= smbus_cmi
;
392 smbus_cmi
->adapter
.class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
393 smbus_cmi
->adapter
.dev
.parent
= &device
->dev
;
395 if (i2c_add_adapter(&smbus_cmi
->adapter
)) {
396 dev_err(&device
->dev
, "Couldn't register adapter!\n");
404 device
->driver_data
= NULL
;
408 static int acpi_smbus_cmi_remove(struct acpi_device
*device
)
410 struct acpi_smbus_cmi
*smbus_cmi
= acpi_driver_data(device
);
412 i2c_del_adapter(&smbus_cmi
->adapter
);
414 device
->driver_data
= NULL
;
419 static struct acpi_driver acpi_smbus_cmi_driver
= {
420 .name
= ACPI_SMBUS_HC_DEVICE_NAME
,
421 .class = ACPI_SMBUS_HC_CLASS
,
422 .ids
= acpi_smbus_cmi_ids
,
424 .add
= acpi_smbus_cmi_add
,
425 .remove
= acpi_smbus_cmi_remove
,
428 module_acpi_driver(acpi_smbus_cmi_driver
);
430 MODULE_LICENSE("GPL");
431 MODULE_AUTHOR("Crane Cai <crane.cai@amd.com>");
432 MODULE_DESCRIPTION("ACPI SMBus CMI driver");