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/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/stddef.h>
13 #include <linux/i2c.h>
14 #include <linux/acpi.h>
16 struct smbus_methods_t
{
22 struct acpi_smbus_cmi
{
24 struct i2c_adapter adapter
;
28 const struct smbus_methods_t
*methods
;
31 static const struct smbus_methods_t smbus_methods
= {
37 /* Some IBM BIOSes omit the leading underscore */
38 static const struct smbus_methods_t ibm_smbus_methods
= {
44 static const struct acpi_device_id acpi_smbus_cmi_ids
[] = {
45 {"SMBUS01", (kernel_ulong_t
)&smbus_methods
},
46 {ACPI_SMBUS_IBM_HID
, (kernel_ulong_t
)&ibm_smbus_methods
},
47 {ACPI_SMBUS_MS_HID
, (kernel_ulong_t
)&smbus_methods
},
50 MODULE_DEVICE_TABLE(acpi
, acpi_smbus_cmi_ids
);
52 #define ACPI_SMBUS_STATUS_OK 0x00
53 #define ACPI_SMBUS_STATUS_FAIL 0x07
54 #define ACPI_SMBUS_STATUS_DNAK 0x10
55 #define ACPI_SMBUS_STATUS_DERR 0x11
56 #define ACPI_SMBUS_STATUS_CMD_DENY 0x12
57 #define ACPI_SMBUS_STATUS_UNKNOWN 0x13
58 #define ACPI_SMBUS_STATUS_ACC_DENY 0x17
59 #define ACPI_SMBUS_STATUS_TIMEOUT 0x18
60 #define ACPI_SMBUS_STATUS_NOTSUP 0x19
61 #define ACPI_SMBUS_STATUS_BUSY 0x1a
62 #define ACPI_SMBUS_STATUS_PEC 0x1f
64 #define ACPI_SMBUS_PRTCL_WRITE 0x00
65 #define ACPI_SMBUS_PRTCL_READ 0x01
66 #define ACPI_SMBUS_PRTCL_QUICK 0x02
67 #define ACPI_SMBUS_PRTCL_BYTE 0x04
68 #define ACPI_SMBUS_PRTCL_BYTE_DATA 0x06
69 #define ACPI_SMBUS_PRTCL_WORD_DATA 0x08
70 #define ACPI_SMBUS_PRTCL_BLOCK_DATA 0x0a
74 acpi_smbus_cmi_access(struct i2c_adapter
*adap
, u16 addr
, unsigned short flags
,
75 char read_write
, u8 command
, int size
,
76 union i2c_smbus_data
*data
)
79 struct acpi_smbus_cmi
*smbus_cmi
= adap
->algo_data
;
80 unsigned char protocol
;
81 acpi_status status
= 0;
82 struct acpi_object_list input
;
83 union acpi_object mt_params
[5];
84 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
85 union acpi_object
*obj
;
86 union acpi_object
*pkg
;
90 dev_dbg(&adap
->dev
, "access size: %d %s\n", size
,
91 (read_write
) ? "READ" : "WRITE");
94 protocol
= ACPI_SMBUS_PRTCL_QUICK
;
96 if (read_write
== I2C_SMBUS_WRITE
) {
97 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
98 mt_params
[3].integer
.value
= 0;
99 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
100 mt_params
[4].integer
.value
= 0;
105 protocol
= ACPI_SMBUS_PRTCL_BYTE
;
106 if (read_write
== I2C_SMBUS_WRITE
) {
107 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
108 mt_params
[3].integer
.value
= 0;
109 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
110 mt_params
[4].integer
.value
= 0;
116 case I2C_SMBUS_BYTE_DATA
:
117 protocol
= ACPI_SMBUS_PRTCL_BYTE_DATA
;
118 if (read_write
== I2C_SMBUS_WRITE
) {
119 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
120 mt_params
[3].integer
.value
= 1;
121 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
122 mt_params
[4].integer
.value
= data
->byte
;
126 case I2C_SMBUS_WORD_DATA
:
127 protocol
= ACPI_SMBUS_PRTCL_WORD_DATA
;
128 if (read_write
== I2C_SMBUS_WRITE
) {
129 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
130 mt_params
[3].integer
.value
= 2;
131 mt_params
[4].type
= ACPI_TYPE_INTEGER
;
132 mt_params
[4].integer
.value
= data
->word
;
136 case I2C_SMBUS_BLOCK_DATA
:
137 protocol
= ACPI_SMBUS_PRTCL_BLOCK_DATA
;
138 if (read_write
== I2C_SMBUS_WRITE
) {
139 len
= data
->block
[0];
140 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
142 mt_params
[3].type
= ACPI_TYPE_INTEGER
;
143 mt_params
[3].integer
.value
= len
;
144 mt_params
[4].type
= ACPI_TYPE_BUFFER
;
145 mt_params
[4].buffer
.length
= len
;
146 mt_params
[4].buffer
.pointer
= data
->block
+ 1;
151 dev_warn(&adap
->dev
, "Unsupported transaction %d\n", size
);
155 if (read_write
== I2C_SMBUS_READ
) {
156 protocol
|= ACPI_SMBUS_PRTCL_READ
;
157 method
= smbus_cmi
->methods
->mt_sbr
;
160 protocol
|= ACPI_SMBUS_PRTCL_WRITE
;
161 method
= smbus_cmi
->methods
->mt_sbw
;
165 input
.pointer
= mt_params
;
166 mt_params
[0].type
= ACPI_TYPE_INTEGER
;
167 mt_params
[0].integer
.value
= protocol
;
168 mt_params
[1].type
= ACPI_TYPE_INTEGER
;
169 mt_params
[1].integer
.value
= addr
;
170 mt_params
[2].type
= ACPI_TYPE_INTEGER
;
171 mt_params
[2].integer
.value
= command
;
173 status
= acpi_evaluate_object(smbus_cmi
->handle
, method
, &input
,
175 if (ACPI_FAILURE(status
)) {
176 acpi_handle_err(smbus_cmi
->handle
,
177 "Failed to evaluate %s: %i\n", method
, status
);
181 pkg
= buffer
.pointer
;
182 if (pkg
&& pkg
->type
== ACPI_TYPE_PACKAGE
)
183 obj
= pkg
->package
.elements
;
185 acpi_handle_err(smbus_cmi
->handle
, "Invalid argument type\n");
189 if (obj
== NULL
|| obj
->type
!= ACPI_TYPE_INTEGER
) {
190 acpi_handle_err(smbus_cmi
->handle
, "Invalid argument type\n");
195 result
= obj
->integer
.value
;
196 acpi_handle_debug(smbus_cmi
->handle
, "%s return status: %i\n", method
,
200 case ACPI_SMBUS_STATUS_OK
:
203 case ACPI_SMBUS_STATUS_BUSY
:
206 case ACPI_SMBUS_STATUS_TIMEOUT
:
209 case ACPI_SMBUS_STATUS_DNAK
:
217 if (read_write
== I2C_SMBUS_WRITE
|| size
== I2C_SMBUS_QUICK
)
220 obj
= pkg
->package
.elements
+ 1;
221 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
222 acpi_handle_err(smbus_cmi
->handle
, "Invalid argument type\n");
227 len
= obj
->integer
.value
;
228 obj
= pkg
->package
.elements
+ 2;
231 case I2C_SMBUS_BYTE_DATA
:
232 case I2C_SMBUS_WORD_DATA
:
233 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
234 acpi_handle_err(smbus_cmi
->handle
,
235 "Invalid argument type\n");
240 data
->word
= obj
->integer
.value
;
242 data
->byte
= obj
->integer
.value
;
244 case I2C_SMBUS_BLOCK_DATA
:
245 if (obj
->type
!= ACPI_TYPE_BUFFER
) {
246 acpi_handle_err(smbus_cmi
->handle
,
247 "Invalid argument type\n");
251 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
253 data
->block
[0] = len
;
254 memcpy(data
->block
+ 1, obj
->buffer
.pointer
, len
);
259 kfree(buffer
.pointer
);
260 dev_dbg(&adap
->dev
, "Transaction status: %i\n", result
);
264 static u32
acpi_smbus_cmi_func(struct i2c_adapter
*adapter
)
266 struct acpi_smbus_cmi
*smbus_cmi
= adapter
->algo_data
;
269 ret
= smbus_cmi
->cap_read
| smbus_cmi
->cap_write
?
270 I2C_FUNC_SMBUS_QUICK
: 0;
272 ret
|= smbus_cmi
->cap_read
?
273 (I2C_FUNC_SMBUS_READ_BYTE
|
274 I2C_FUNC_SMBUS_READ_BYTE_DATA
|
275 I2C_FUNC_SMBUS_READ_WORD_DATA
|
276 I2C_FUNC_SMBUS_READ_BLOCK_DATA
) : 0;
278 ret
|= smbus_cmi
->cap_write
?
279 (I2C_FUNC_SMBUS_WRITE_BYTE
|
280 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
281 I2C_FUNC_SMBUS_WRITE_WORD_DATA
|
282 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
) : 0;
287 static const struct i2c_algorithm acpi_smbus_cmi_algorithm
= {
288 .smbus_xfer
= acpi_smbus_cmi_access
,
289 .functionality
= acpi_smbus_cmi_func
,
293 static int acpi_smbus_cmi_add_cap(struct acpi_smbus_cmi
*smbus_cmi
,
296 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
297 struct acpi_handle
*handle
= smbus_cmi
->handle
;
298 union acpi_object
*obj
;
301 if (!strcmp(name
, smbus_cmi
->methods
->mt_info
)) {
302 status
= acpi_evaluate_object(smbus_cmi
->handle
,
303 smbus_cmi
->methods
->mt_info
,
305 if (ACPI_FAILURE(status
)) {
306 acpi_handle_err(handle
, "Failed to evaluate %s: %i\n",
307 smbus_cmi
->methods
->mt_info
, status
);
311 obj
= buffer
.pointer
;
312 if (obj
&& obj
->type
== ACPI_TYPE_PACKAGE
)
313 obj
= obj
->package
.elements
;
315 acpi_handle_err(handle
, "Invalid argument type\n");
316 kfree(buffer
.pointer
);
320 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
321 acpi_handle_err(handle
, "Invalid argument type\n");
322 kfree(buffer
.pointer
);
325 acpi_handle_debug(handle
, "SMBus CMI Version %x\n",
326 (int)obj
->integer
.value
);
328 kfree(buffer
.pointer
);
329 smbus_cmi
->cap_info
= 1;
330 } else if (!strcmp(name
, smbus_cmi
->methods
->mt_sbr
))
331 smbus_cmi
->cap_read
= 1;
332 else if (!strcmp(name
, smbus_cmi
->methods
->mt_sbw
))
333 smbus_cmi
->cap_write
= 1;
335 acpi_handle_debug(handle
, "Unsupported CMI method: %s\n", name
);
340 static acpi_status
acpi_smbus_cmi_query_methods(acpi_handle handle
, u32 level
,
341 void *context
, void **return_value
)
344 struct acpi_buffer buffer
= { sizeof(node_name
), node_name
};
345 struct acpi_smbus_cmi
*smbus_cmi
= context
;
348 status
= acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
);
350 if (ACPI_SUCCESS(status
))
351 acpi_smbus_cmi_add_cap(smbus_cmi
, node_name
);
356 static int smbus_cmi_probe(struct platform_device
*device
)
358 struct device
*dev
= &device
->dev
;
359 struct acpi_smbus_cmi
*smbus_cmi
;
362 smbus_cmi
= kzalloc(sizeof(struct acpi_smbus_cmi
), GFP_KERNEL
);
366 smbus_cmi
->handle
= ACPI_HANDLE(dev
);
367 smbus_cmi
->methods
= device_get_match_data(dev
);
369 platform_set_drvdata(device
, smbus_cmi
);
371 smbus_cmi
->cap_info
= 0;
372 smbus_cmi
->cap_read
= 0;
373 smbus_cmi
->cap_write
= 0;
375 acpi_walk_namespace(ACPI_TYPE_METHOD
, smbus_cmi
->handle
, 1,
376 acpi_smbus_cmi_query_methods
, NULL
, smbus_cmi
, NULL
);
378 if (smbus_cmi
->cap_info
== 0) {
383 snprintf(smbus_cmi
->adapter
.name
, sizeof(smbus_cmi
->adapter
.name
),
384 "SMBus CMI adapter %s", dev_name(dev
));
385 smbus_cmi
->adapter
.owner
= THIS_MODULE
;
386 smbus_cmi
->adapter
.algo
= &acpi_smbus_cmi_algorithm
;
387 smbus_cmi
->adapter
.algo_data
= smbus_cmi
;
388 smbus_cmi
->adapter
.class = I2C_CLASS_HWMON
;
389 smbus_cmi
->adapter
.dev
.parent
= &device
->dev
;
391 ret
= i2c_add_adapter(&smbus_cmi
->adapter
);
393 dev_err(&device
->dev
, "Couldn't register adapter!\n");
404 static void smbus_cmi_remove(struct platform_device
*device
)
406 struct acpi_smbus_cmi
*smbus_cmi
= platform_get_drvdata(device
);
408 i2c_del_adapter(&smbus_cmi
->adapter
);
412 static struct platform_driver smbus_cmi_driver
= {
413 .probe
= smbus_cmi_probe
,
414 .remove_new
= smbus_cmi_remove
,
417 .acpi_match_table
= acpi_smbus_cmi_ids
,
420 module_platform_driver(smbus_cmi_driver
);
422 MODULE_LICENSE("GPL");
423 MODULE_AUTHOR("Crane Cai <crane.cai@amd.com>");
424 MODULE_DESCRIPTION("ACPI SMBus CMI driver");