1 // SPDX-License-Identifier: GPL-2.0-only
3 * SMBus driver for ACPI SMBus CMI
5 * Copyright (C) 2009 Crane Cai <crane.cai@amd.com>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/kernel.h>
11 #include <linux/stddef.h>
12 #include <linux/i2c.h>
13 #include <linux/acpi.h>
15 #define ACPI_SMBUS_HC_CLASS "smbus"
16 #define ACPI_SMBUS_HC_DEVICE_NAME "cmi"
18 /* SMBUS HID definition as supported by Microsoft Windows */
19 #define ACPI_SMBUS_MS_HID "SMB0001"
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
},
54 {ACPI_SMBUS_MS_HID
, (kernel_ulong_t
)&smbus_methods
},
57 MODULE_DEVICE_TABLE(acpi
, acpi_smbus_cmi_ids
);
59 #define ACPI_SMBUS_STATUS_OK 0x00
60 #define ACPI_SMBUS_STATUS_FAIL 0x07
61 #define ACPI_SMBUS_STATUS_DNAK 0x10
62 #define ACPI_SMBUS_STATUS_DERR 0x11
63 #define ACPI_SMBUS_STATUS_CMD_DENY 0x12
64 #define ACPI_SMBUS_STATUS_UNKNOWN 0x13
65 #define ACPI_SMBUS_STATUS_ACC_DENY 0x17
66 #define ACPI_SMBUS_STATUS_TIMEOUT 0x18
67 #define ACPI_SMBUS_STATUS_NOTSUP 0x19
68 #define ACPI_SMBUS_STATUS_BUSY 0x1a
69 #define ACPI_SMBUS_STATUS_PEC 0x1f
71 #define ACPI_SMBUS_PRTCL_WRITE 0x00
72 #define ACPI_SMBUS_PRTCL_READ 0x01
73 #define ACPI_SMBUS_PRTCL_QUICK 0x02
74 #define ACPI_SMBUS_PRTCL_BYTE 0x04
75 #define ACPI_SMBUS_PRTCL_BYTE_DATA 0x06
76 #define ACPI_SMBUS_PRTCL_WORD_DATA 0x08
77 #define ACPI_SMBUS_PRTCL_BLOCK_DATA 0x0a
81 acpi_smbus_cmi_access(struct i2c_adapter
*adap
, u16 addr
, unsigned short flags
,
82 char read_write
, u8 command
, int size
,
83 union i2c_smbus_data
*data
)
86 struct acpi_smbus_cmi
*smbus_cmi
= adap
->algo_data
;
87 unsigned char protocol
;
88 acpi_status status
= 0;
89 struct acpi_object_list input
;
90 union acpi_object mt_params
[5];
91 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
92 union acpi_object
*obj
;
93 union acpi_object
*pkg
;
97 dev_dbg(&adap
->dev
, "access size: %d %s\n", size
,
98 (read_write
) ? "READ" : "WRITE");
100 case I2C_SMBUS_QUICK
:
101 protocol
= ACPI_SMBUS_PRTCL_QUICK
;
103 if (read_write
== I2C_SMBUS_WRITE
) {
104 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
105 mt_params
[3].integer
.value
= 0;
106 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
107 mt_params
[4].integer
.value
= 0;
112 protocol
= ACPI_SMBUS_PRTCL_BYTE
;
113 if (read_write
== I2C_SMBUS_WRITE
) {
114 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
115 mt_params
[3].integer
.value
= 0;
116 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
117 mt_params
[4].integer
.value
= 0;
123 case I2C_SMBUS_BYTE_DATA
:
124 protocol
= ACPI_SMBUS_PRTCL_BYTE_DATA
;
125 if (read_write
== I2C_SMBUS_WRITE
) {
126 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
127 mt_params
[3].integer
.value
= 1;
128 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
129 mt_params
[4].integer
.value
= data
->byte
;
133 case I2C_SMBUS_WORD_DATA
:
134 protocol
= ACPI_SMBUS_PRTCL_WORD_DATA
;
135 if (read_write
== I2C_SMBUS_WRITE
) {
136 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
137 mt_params
[3].integer
.value
= 2;
138 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
139 mt_params
[4].integer
.value
= data
->word
;
143 case I2C_SMBUS_BLOCK_DATA
:
144 protocol
= ACPI_SMBUS_PRTCL_BLOCK_DATA
;
145 if (read_write
== I2C_SMBUS_WRITE
) {
146 len
= data
->block
[0];
147 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
149 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
150 mt_params
[3].integer
.value
= len
;
151 mt_params
[4].type
= ACPI_TYPE_BUFFER
;
152 mt_params
[4].buffer
.length
= len
;
153 mt_params
[4].buffer
.pointer
= data
->block
+ 1;
158 dev_warn(&adap
->dev
, "Unsupported transaction %d\n", size
);
162 if (read_write
== I2C_SMBUS_READ
) {
163 protocol
|= ACPI_SMBUS_PRTCL_READ
;
164 method
= smbus_cmi
->methods
->mt_sbr
;
167 protocol
|= ACPI_SMBUS_PRTCL_WRITE
;
168 method
= smbus_cmi
->methods
->mt_sbw
;
172 input
.pointer
= mt_params
;
173 mt_params
[0].type
= ACPI_TYPE_INTEGER
;
174 mt_params
[0].integer
.value
= protocol
;
175 mt_params
[1].type
= ACPI_TYPE_INTEGER
;
176 mt_params
[1].integer
.value
= addr
;
177 mt_params
[2].type
= ACPI_TYPE_INTEGER
;
178 mt_params
[2].integer
.value
= command
;
180 status
= acpi_evaluate_object(smbus_cmi
->handle
, method
, &input
,
182 if (ACPI_FAILURE(status
)) {
183 acpi_handle_err(smbus_cmi
->handle
,
184 "Failed to evaluate %s: %i\n", method
, status
);
188 pkg
= buffer
.pointer
;
189 if (pkg
&& pkg
->type
== ACPI_TYPE_PACKAGE
)
190 obj
= pkg
->package
.elements
;
192 acpi_handle_err(smbus_cmi
->handle
, "Invalid argument type\n");
196 if (obj
== NULL
|| obj
->type
!= ACPI_TYPE_INTEGER
) {
197 acpi_handle_err(smbus_cmi
->handle
, "Invalid argument type\n");
202 result
= obj
->integer
.value
;
203 acpi_handle_debug(smbus_cmi
->handle
, "%s return status: %i\n", method
,
207 case ACPI_SMBUS_STATUS_OK
:
210 case ACPI_SMBUS_STATUS_BUSY
:
213 case ACPI_SMBUS_STATUS_TIMEOUT
:
216 case ACPI_SMBUS_STATUS_DNAK
:
224 if (read_write
== I2C_SMBUS_WRITE
|| size
== I2C_SMBUS_QUICK
)
227 obj
= pkg
->package
.elements
+ 1;
228 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
229 acpi_handle_err(smbus_cmi
->handle
, "Invalid argument type\n");
234 len
= obj
->integer
.value
;
235 obj
= pkg
->package
.elements
+ 2;
238 case I2C_SMBUS_BYTE_DATA
:
239 case I2C_SMBUS_WORD_DATA
:
240 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
241 acpi_handle_err(smbus_cmi
->handle
,
242 "Invalid argument type\n");
247 data
->word
= obj
->integer
.value
;
249 data
->byte
= obj
->integer
.value
;
251 case I2C_SMBUS_BLOCK_DATA
:
252 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
253 acpi_handle_err(smbus_cmi
->handle
,
254 "Invalid argument type\n");
258 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
260 data
->block
[0] = len
;
261 memcpy(data
->block
+ 1, obj
->buffer
.pointer
, len
);
266 kfree(buffer
.pointer
);
267 dev_dbg(&adap
->dev
, "Transaction status: %i\n", result
);
271 static u32
acpi_smbus_cmi_func(struct i2c_adapter
*adapter
)
273 struct acpi_smbus_cmi
*smbus_cmi
= adapter
->algo_data
;
276 ret
= smbus_cmi
->cap_read
| smbus_cmi
->cap_write
?
277 I2C_FUNC_SMBUS_QUICK
: 0;
279 ret
|= smbus_cmi
->cap_read
?
280 (I2C_FUNC_SMBUS_READ_BYTE
|
281 I2C_FUNC_SMBUS_READ_BYTE_DATA
|
282 I2C_FUNC_SMBUS_READ_WORD_DATA
|
283 I2C_FUNC_SMBUS_READ_BLOCK_DATA
) : 0;
285 ret
|= smbus_cmi
->cap_write
?
286 (I2C_FUNC_SMBUS_WRITE_BYTE
|
287 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
288 I2C_FUNC_SMBUS_WRITE_WORD_DATA
|
289 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
) : 0;
294 static const struct i2c_algorithm acpi_smbus_cmi_algorithm
= {
295 .smbus_xfer
= acpi_smbus_cmi_access
,
296 .functionality
= acpi_smbus_cmi_func
,
300 static int acpi_smbus_cmi_add_cap(struct acpi_smbus_cmi
*smbus_cmi
,
303 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
304 struct acpi_handle
*handle
= smbus_cmi
->handle
;
305 union acpi_object
*obj
;
308 if (!strcmp(name
, smbus_cmi
->methods
->mt_info
)) {
309 status
= acpi_evaluate_object(smbus_cmi
->handle
,
310 smbus_cmi
->methods
->mt_info
,
312 if (ACPI_FAILURE(status
)) {
313 acpi_handle_err(handle
, "Failed to evaluate %s: %i\n",
314 smbus_cmi
->methods
->mt_info
, status
);
318 obj
= buffer
.pointer
;
319 if (obj
&& obj
->type
== ACPI_TYPE_PACKAGE
)
320 obj
= obj
->package
.elements
;
322 acpi_handle_err(handle
, "Invalid argument type\n");
323 kfree(buffer
.pointer
);
327 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
328 acpi_handle_err(handle
, "Invalid argument type\n");
329 kfree(buffer
.pointer
);
332 acpi_handle_debug(handle
, "SMBus CMI Version %x\n",
333 (int)obj
->integer
.value
);
335 kfree(buffer
.pointer
);
336 smbus_cmi
->cap_info
= 1;
337 } else if (!strcmp(name
, smbus_cmi
->methods
->mt_sbr
))
338 smbus_cmi
->cap_read
= 1;
339 else if (!strcmp(name
, smbus_cmi
->methods
->mt_sbw
))
340 smbus_cmi
->cap_write
= 1;
342 acpi_handle_debug(handle
, "Unsupported CMI method: %s\n", name
);
347 static acpi_status
acpi_smbus_cmi_query_methods(acpi_handle handle
, u32 level
,
348 void *context
, void **return_value
)
351 struct acpi_buffer buffer
= { sizeof(node_name
), node_name
};
352 struct acpi_smbus_cmi
*smbus_cmi
= context
;
355 status
= acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
);
357 if (ACPI_SUCCESS(status
))
358 acpi_smbus_cmi_add_cap(smbus_cmi
, node_name
);
363 static int acpi_smbus_cmi_add(struct acpi_device
*device
)
365 struct acpi_smbus_cmi
*smbus_cmi
;
366 const struct acpi_device_id
*id
;
369 smbus_cmi
= kzalloc(sizeof(struct acpi_smbus_cmi
), GFP_KERNEL
);
373 smbus_cmi
->handle
= device
->handle
;
374 strcpy(acpi_device_name(device
), ACPI_SMBUS_HC_DEVICE_NAME
);
375 strcpy(acpi_device_class(device
), ACPI_SMBUS_HC_CLASS
);
376 device
->driver_data
= smbus_cmi
;
377 smbus_cmi
->cap_info
= 0;
378 smbus_cmi
->cap_read
= 0;
379 smbus_cmi
->cap_write
= 0;
381 for (id
= acpi_smbus_cmi_ids
; id
->id
[0]; id
++)
382 if (!strcmp(id
->id
, acpi_device_hid(device
)))
384 (struct smbus_methods_t
*) id
->driver_data
;
386 acpi_walk_namespace(ACPI_TYPE_METHOD
, smbus_cmi
->handle
, 1,
387 acpi_smbus_cmi_query_methods
, NULL
, smbus_cmi
, NULL
);
389 if (smbus_cmi
->cap_info
== 0) {
394 snprintf(smbus_cmi
->adapter
.name
, sizeof(smbus_cmi
->adapter
.name
),
395 "SMBus CMI adapter %s",
396 acpi_device_name(device
));
397 smbus_cmi
->adapter
.owner
= THIS_MODULE
;
398 smbus_cmi
->adapter
.algo
= &acpi_smbus_cmi_algorithm
;
399 smbus_cmi
->adapter
.algo_data
= smbus_cmi
;
400 smbus_cmi
->adapter
.class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
401 smbus_cmi
->adapter
.dev
.parent
= &device
->dev
;
403 ret
= i2c_add_adapter(&smbus_cmi
->adapter
);
405 dev_err(&device
->dev
, "Couldn't register adapter!\n");
413 device
->driver_data
= NULL
;
417 static int acpi_smbus_cmi_remove(struct acpi_device
*device
)
419 struct acpi_smbus_cmi
*smbus_cmi
= acpi_driver_data(device
);
421 i2c_del_adapter(&smbus_cmi
->adapter
);
423 device
->driver_data
= NULL
;
428 static struct acpi_driver acpi_smbus_cmi_driver
= {
429 .name
= ACPI_SMBUS_HC_DEVICE_NAME
,
430 .class = ACPI_SMBUS_HC_CLASS
,
431 .ids
= acpi_smbus_cmi_ids
,
433 .add
= acpi_smbus_cmi_add
,
434 .remove
= acpi_smbus_cmi_remove
,
437 module_acpi_driver(acpi_smbus_cmi_driver
);
439 MODULE_LICENSE("GPL");
440 MODULE_AUTHOR("Crane Cai <crane.cai@amd.com>");
441 MODULE_DESCRIPTION("ACPI SMBus CMI driver");