i2c-scmi.c 11 KB

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