• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * SMBus driver for ACPI SMBus CMI
3  *
4  * Copyright (C) 2009 Crane Cai <crane.cai@amd.com>
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 version 2.
9  */
10 
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>
17 
18 #define ACPI_SMBUS_HC_CLASS		"smbus"
19 #define ACPI_SMBUS_HC_DEVICE_NAME	"cmi"
20 
21 /* SMBUS HID definition as supported by Microsoft Windows */
22 #define ACPI_SMBUS_MS_HID		"SMB0001"
23 
24 ACPI_MODULE_NAME("smbus_cmi");
25 
26 struct smbus_methods_t {
27 	char *mt_info;
28 	char *mt_sbr;
29 	char *mt_sbw;
30 };
31 
32 struct acpi_smbus_cmi {
33 	acpi_handle handle;
34 	struct i2c_adapter adapter;
35 	u8 cap_info:1;
36 	u8 cap_read:1;
37 	u8 cap_write:1;
38 	struct smbus_methods_t *methods;
39 };
40 
41 static const struct smbus_methods_t smbus_methods = {
42 	.mt_info = "_SBI",
43 	.mt_sbr  = "_SBR",
44 	.mt_sbw  = "_SBW",
45 };
46 
47 /* Some IBM BIOSes omit the leading underscore */
48 static const struct smbus_methods_t ibm_smbus_methods = {
49 	.mt_info = "SBI_",
50 	.mt_sbr  = "SBR_",
51 	.mt_sbw  = "SBW_",
52 };
53 
54 static const struct acpi_device_id acpi_smbus_cmi_ids[] = {
55 	{"SMBUS01", (kernel_ulong_t)&smbus_methods},
56 	{ACPI_SMBUS_IBM_HID, (kernel_ulong_t)&ibm_smbus_methods},
57 	{ACPI_SMBUS_MS_HID, (kernel_ulong_t)&smbus_methods},
58 	{"", 0}
59 };
60 MODULE_DEVICE_TABLE(acpi, acpi_smbus_cmi_ids);
61 
62 #define ACPI_SMBUS_STATUS_OK			0x00
63 #define ACPI_SMBUS_STATUS_FAIL			0x07
64 #define ACPI_SMBUS_STATUS_DNAK			0x10
65 #define ACPI_SMBUS_STATUS_DERR			0x11
66 #define ACPI_SMBUS_STATUS_CMD_DENY		0x12
67 #define ACPI_SMBUS_STATUS_UNKNOWN		0x13
68 #define ACPI_SMBUS_STATUS_ACC_DENY		0x17
69 #define ACPI_SMBUS_STATUS_TIMEOUT		0x18
70 #define ACPI_SMBUS_STATUS_NOTSUP		0x19
71 #define ACPI_SMBUS_STATUS_BUSY			0x1a
72 #define ACPI_SMBUS_STATUS_PEC			0x1f
73 
74 #define ACPI_SMBUS_PRTCL_WRITE			0x00
75 #define ACPI_SMBUS_PRTCL_READ			0x01
76 #define ACPI_SMBUS_PRTCL_QUICK			0x02
77 #define ACPI_SMBUS_PRTCL_BYTE			0x04
78 #define ACPI_SMBUS_PRTCL_BYTE_DATA		0x06
79 #define ACPI_SMBUS_PRTCL_WORD_DATA		0x08
80 #define ACPI_SMBUS_PRTCL_BLOCK_DATA		0x0a
81 
82 
83 static int
acpi_smbus_cmi_access(struct i2c_adapter * adap,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)84 acpi_smbus_cmi_access(struct i2c_adapter *adap, u16 addr, unsigned short flags,
85 		   char read_write, u8 command, int size,
86 		   union i2c_smbus_data *data)
87 {
88 	int result = 0;
89 	struct acpi_smbus_cmi *smbus_cmi = adap->algo_data;
90 	unsigned char protocol;
91 	acpi_status status = 0;
92 	struct acpi_object_list input;
93 	union acpi_object mt_params[5];
94 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
95 	union acpi_object *obj;
96 	union acpi_object *pkg;
97 	char *method;
98 	int len = 0;
99 
100 	dev_dbg(&adap->dev, "access size: %d %s\n", size,
101 		(read_write) ? "READ" : "WRITE");
102 	switch (size) {
103 	case I2C_SMBUS_QUICK:
104 		protocol = ACPI_SMBUS_PRTCL_QUICK;
105 		command = 0;
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;
111 		}
112 		break;
113 
114 	case I2C_SMBUS_BYTE:
115 		protocol = ACPI_SMBUS_PRTCL_BYTE;
116 		if (read_write == I2C_SMBUS_WRITE) {
117 			mt_params[3].type = ACPI_TYPE_INTEGER;
118 			mt_params[3].integer.value = 0;
119 			mt_params[4].type = ACPI_TYPE_INTEGER;
120 			mt_params[4].integer.value = 0;
121 		} else {
122 			command = 0;
123 		}
124 		break;
125 
126 	case I2C_SMBUS_BYTE_DATA:
127 		protocol = ACPI_SMBUS_PRTCL_BYTE_DATA;
128 		if (read_write == I2C_SMBUS_WRITE) {
129 			mt_params[3].type = ACPI_TYPE_INTEGER;
130 			mt_params[3].integer.value = 1;
131 			mt_params[4].type = ACPI_TYPE_INTEGER;
132 			mt_params[4].integer.value = data->byte;
133 		}
134 		break;
135 
136 	case I2C_SMBUS_WORD_DATA:
137 		protocol = ACPI_SMBUS_PRTCL_WORD_DATA;
138 		if (read_write == I2C_SMBUS_WRITE) {
139 			mt_params[3].type = ACPI_TYPE_INTEGER;
140 			mt_params[3].integer.value = 2;
141 			mt_params[4].type = ACPI_TYPE_INTEGER;
142 			mt_params[4].integer.value = data->word;
143 		}
144 		break;
145 
146 	case I2C_SMBUS_BLOCK_DATA:
147 		protocol = ACPI_SMBUS_PRTCL_BLOCK_DATA;
148 		if (read_write == I2C_SMBUS_WRITE) {
149 			len = data->block[0];
150 			if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
151 				return -EINVAL;
152 			mt_params[3].type = ACPI_TYPE_INTEGER;
153 			mt_params[3].integer.value = len;
154 			mt_params[4].type = ACPI_TYPE_BUFFER;
155 			mt_params[4].buffer.pointer = data->block + 1;
156 		}
157 		break;
158 
159 	default:
160 		dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
161 		return -EOPNOTSUPP;
162 	}
163 
164 	if (read_write == I2C_SMBUS_READ) {
165 		protocol |= ACPI_SMBUS_PRTCL_READ;
166 		method = smbus_cmi->methods->mt_sbr;
167 		input.count = 3;
168 	} else {
169 		protocol |= ACPI_SMBUS_PRTCL_WRITE;
170 		method = smbus_cmi->methods->mt_sbw;
171 		input.count = 5;
172 	}
173 
174 	input.pointer = mt_params;
175 	mt_params[0].type = ACPI_TYPE_INTEGER;
176 	mt_params[0].integer.value = protocol;
177 	mt_params[1].type = ACPI_TYPE_INTEGER;
178 	mt_params[1].integer.value = addr;
179 	mt_params[2].type = ACPI_TYPE_INTEGER;
180 	mt_params[2].integer.value = command;
181 
182 	status = acpi_evaluate_object(smbus_cmi->handle, method, &input,
183 				      &buffer);
184 	if (ACPI_FAILURE(status)) {
185 		ACPI_ERROR((AE_INFO, "Evaluating %s: %i", method, status));
186 		return -EIO;
187 	}
188 
189 	pkg = buffer.pointer;
190 	if (pkg && pkg->type == ACPI_TYPE_PACKAGE)
191 		obj = pkg->package.elements;
192 	else {
193 		ACPI_ERROR((AE_INFO, "Invalid argument type"));
194 		result = -EIO;
195 		goto out;
196 	}
197 	if (obj == NULL || obj->type != ACPI_TYPE_INTEGER) {
198 		ACPI_ERROR((AE_INFO, "Invalid argument type"));
199 		result = -EIO;
200 		goto out;
201 	}
202 
203 	result = obj->integer.value;
204 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s return status: %i\n",
205 			  method, result));
206 
207 	switch (result) {
208 	case ACPI_SMBUS_STATUS_OK:
209 		result = 0;
210 		break;
211 	case ACPI_SMBUS_STATUS_BUSY:
212 		result = -EBUSY;
213 		goto out;
214 	case ACPI_SMBUS_STATUS_TIMEOUT:
215 		result = -ETIMEDOUT;
216 		goto out;
217 	case ACPI_SMBUS_STATUS_DNAK:
218 		result = -ENXIO;
219 		goto out;
220 	default:
221 		result = -EIO;
222 		goto out;
223 	}
224 
225 	if (read_write == I2C_SMBUS_WRITE || size == I2C_SMBUS_QUICK)
226 		goto out;
227 
228 	obj = pkg->package.elements + 1;
229 	if (obj->type != ACPI_TYPE_INTEGER) {
230 		ACPI_ERROR((AE_INFO, "Invalid argument type"));
231 		result = -EIO;
232 		goto out;
233 	}
234 
235 	len = obj->integer.value;
236 	obj = pkg->package.elements + 2;
237 	switch (size) {
238 	case I2C_SMBUS_BYTE:
239 	case I2C_SMBUS_BYTE_DATA:
240 	case I2C_SMBUS_WORD_DATA:
241 		if (obj->type != ACPI_TYPE_INTEGER) {
242 			ACPI_ERROR((AE_INFO, "Invalid argument type"));
243 			result = -EIO;
244 			goto out;
245 		}
246 		if (len == 2)
247 			data->word = obj->integer.value;
248 		else
249 			data->byte = obj->integer.value;
250 		break;
251 	case I2C_SMBUS_BLOCK_DATA:
252 		if (obj->type != ACPI_TYPE_BUFFER) {
253 			ACPI_ERROR((AE_INFO, "Invalid argument type"));
254 			result = -EIO;
255 			goto out;
256 		}
257 		if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
258 			return -EPROTO;
259 		data->block[0] = len;
260 		memcpy(data->block + 1, obj->buffer.pointer, len);
261 		break;
262 	}
263 
264 out:
265 	kfree(buffer.pointer);
266 	dev_dbg(&adap->dev, "Transaction status: %i\n", result);
267 	return result;
268 }
269 
acpi_smbus_cmi_func(struct i2c_adapter * adapter)270 static u32 acpi_smbus_cmi_func(struct i2c_adapter *adapter)
271 {
272 	struct acpi_smbus_cmi *smbus_cmi = adapter->algo_data;
273 	u32 ret;
274 
275 	ret = smbus_cmi->cap_read | smbus_cmi->cap_write ?
276 		I2C_FUNC_SMBUS_QUICK : 0;
277 
278 	ret |= smbus_cmi->cap_read ?
279 		(I2C_FUNC_SMBUS_READ_BYTE |
280 		I2C_FUNC_SMBUS_READ_BYTE_DATA |
281 		I2C_FUNC_SMBUS_READ_WORD_DATA |
282 		I2C_FUNC_SMBUS_READ_BLOCK_DATA) : 0;
283 
284 	ret |= smbus_cmi->cap_write ?
285 		(I2C_FUNC_SMBUS_WRITE_BYTE |
286 		I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
287 		I2C_FUNC_SMBUS_WRITE_WORD_DATA |
288 		I2C_FUNC_SMBUS_WRITE_BLOCK_DATA) : 0;
289 
290 	return ret;
291 }
292 
293 static const struct i2c_algorithm acpi_smbus_cmi_algorithm = {
294 	.smbus_xfer = acpi_smbus_cmi_access,
295 	.functionality = acpi_smbus_cmi_func,
296 };
297 
298 
acpi_smbus_cmi_add_cap(struct acpi_smbus_cmi * smbus_cmi,const char * name)299 static int acpi_smbus_cmi_add_cap(struct acpi_smbus_cmi *smbus_cmi,
300 				  const char *name)
301 {
302 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
303 	union acpi_object *obj;
304 	acpi_status status;
305 
306 	if (!strcmp(name, smbus_cmi->methods->mt_info)) {
307 		status = acpi_evaluate_object(smbus_cmi->handle,
308 					smbus_cmi->methods->mt_info,
309 					NULL, &buffer);
310 		if (ACPI_FAILURE(status)) {
311 			ACPI_ERROR((AE_INFO, "Evaluating %s: %i",
312 				   smbus_cmi->methods->mt_info, status));
313 			return -EIO;
314 		}
315 
316 		obj = buffer.pointer;
317 		if (obj && obj->type == ACPI_TYPE_PACKAGE)
318 			obj = obj->package.elements;
319 		else {
320 			ACPI_ERROR((AE_INFO, "Invalid argument type"));
321 			kfree(buffer.pointer);
322 			return -EIO;
323 		}
324 
325 		if (obj->type != ACPI_TYPE_INTEGER) {
326 			ACPI_ERROR((AE_INFO, "Invalid argument type"));
327 			kfree(buffer.pointer);
328 			return -EIO;
329 		} else
330 			ACPI_DEBUG_PRINT((ACPI_DB_INFO, "SMBus CMI Version %x"
331 					  "\n", (int)obj->integer.value));
332 
333 		kfree(buffer.pointer);
334 		smbus_cmi->cap_info = 1;
335 	} else if (!strcmp(name, smbus_cmi->methods->mt_sbr))
336 		smbus_cmi->cap_read = 1;
337 	else if (!strcmp(name, smbus_cmi->methods->mt_sbw))
338 		smbus_cmi->cap_write = 1;
339 	else
340 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unsupported CMI method: %s\n",
341 				 name));
342 
343 	return 0;
344 }
345 
acpi_smbus_cmi_query_methods(acpi_handle handle,u32 level,void * context,void ** return_value)346 static acpi_status acpi_smbus_cmi_query_methods(acpi_handle handle, u32 level,
347 			void *context, void **return_value)
348 {
349 	char node_name[5];
350 	struct acpi_buffer buffer = { sizeof(node_name), node_name };
351 	struct acpi_smbus_cmi *smbus_cmi = context;
352 	acpi_status status;
353 
354 	status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
355 
356 	if (ACPI_SUCCESS(status))
357 		acpi_smbus_cmi_add_cap(smbus_cmi, node_name);
358 
359 	return AE_OK;
360 }
361 
acpi_smbus_cmi_add(struct acpi_device * device)362 static int acpi_smbus_cmi_add(struct acpi_device *device)
363 {
364 	struct acpi_smbus_cmi *smbus_cmi;
365 	const struct acpi_device_id *id;
366 
367 	smbus_cmi = kzalloc(sizeof(struct acpi_smbus_cmi), GFP_KERNEL);
368 	if (!smbus_cmi)
369 		return -ENOMEM;
370 
371 	smbus_cmi->handle = device->handle;
372 	strcpy(acpi_device_name(device), ACPI_SMBUS_HC_DEVICE_NAME);
373 	strcpy(acpi_device_class(device), ACPI_SMBUS_HC_CLASS);
374 	device->driver_data = smbus_cmi;
375 	smbus_cmi->cap_info = 0;
376 	smbus_cmi->cap_read = 0;
377 	smbus_cmi->cap_write = 0;
378 
379 	for (id = acpi_smbus_cmi_ids; id->id[0]; id++)
380 		if (!strcmp(id->id, acpi_device_hid(device)))
381 			smbus_cmi->methods =
382 				(struct smbus_methods_t *) id->driver_data;
383 
384 	acpi_walk_namespace(ACPI_TYPE_METHOD, smbus_cmi->handle, 1,
385 			    acpi_smbus_cmi_query_methods, NULL, smbus_cmi, NULL);
386 
387 	if (smbus_cmi->cap_info == 0)
388 		goto err;
389 
390 	snprintf(smbus_cmi->adapter.name, sizeof(smbus_cmi->adapter.name),
391 		"SMBus CMI adapter %s",
392 		acpi_device_name(device));
393 	smbus_cmi->adapter.owner = THIS_MODULE;
394 	smbus_cmi->adapter.algo = &acpi_smbus_cmi_algorithm;
395 	smbus_cmi->adapter.algo_data = smbus_cmi;
396 	smbus_cmi->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
397 	smbus_cmi->adapter.dev.parent = &device->dev;
398 
399 	if (i2c_add_adapter(&smbus_cmi->adapter)) {
400 		dev_err(&device->dev, "Couldn't register adapter!\n");
401 		goto err;
402 	}
403 
404 	return 0;
405 
406 err:
407 	kfree(smbus_cmi);
408 	device->driver_data = NULL;
409 	return -EIO;
410 }
411 
acpi_smbus_cmi_remove(struct acpi_device * device)412 static int acpi_smbus_cmi_remove(struct acpi_device *device)
413 {
414 	struct acpi_smbus_cmi *smbus_cmi = acpi_driver_data(device);
415 
416 	i2c_del_adapter(&smbus_cmi->adapter);
417 	kfree(smbus_cmi);
418 	device->driver_data = NULL;
419 
420 	return 0;
421 }
422 
423 static struct acpi_driver acpi_smbus_cmi_driver = {
424 	.name = ACPI_SMBUS_HC_DEVICE_NAME,
425 	.class = ACPI_SMBUS_HC_CLASS,
426 	.ids = acpi_smbus_cmi_ids,
427 	.ops = {
428 		.add = acpi_smbus_cmi_add,
429 		.remove = acpi_smbus_cmi_remove,
430 	},
431 };
432 module_acpi_driver(acpi_smbus_cmi_driver);
433 
434 MODULE_LICENSE("GPL");
435 MODULE_AUTHOR("Crane Cai <crane.cai@amd.com>");
436 MODULE_DESCRIPTION("ACPI SMBus CMI driver");
437