sbshc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * SMBus driver for ACPI Embedded Controller (v0.1)
  3. *
  4. * Copyright (c) 2007 Alexey Starikovskiy
  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/acpi.h>
  11. #include <linux/wait.h>
  12. #include <linux/slab.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include "sbshc.h"
  17. #define PREFIX "ACPI: "
  18. #define ACPI_SMB_HC_CLASS "smbus_host_ctl"
  19. #define ACPI_SMB_HC_DEVICE_NAME "ACPI SMBus HC"
  20. struct acpi_smb_hc {
  21. struct acpi_ec *ec;
  22. struct mutex lock;
  23. wait_queue_head_t wait;
  24. u8 offset;
  25. u8 query_bit;
  26. smbus_alarm_callback callback;
  27. void *context;
  28. bool done;
  29. };
  30. static int acpi_smbus_hc_add(struct acpi_device *device);
  31. static int acpi_smbus_hc_remove(struct acpi_device *device);
  32. static const struct acpi_device_id sbs_device_ids[] = {
  33. {"ACPI0001", 0},
  34. {"ACPI0005", 0},
  35. {"", 0},
  36. };
  37. MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
  38. static struct acpi_driver acpi_smb_hc_driver = {
  39. .name = "smbus_hc",
  40. .class = ACPI_SMB_HC_CLASS,
  41. .ids = sbs_device_ids,
  42. .ops = {
  43. .add = acpi_smbus_hc_add,
  44. .remove = acpi_smbus_hc_remove,
  45. },
  46. };
  47. union acpi_smb_status {
  48. u8 raw;
  49. struct {
  50. u8 status:5;
  51. u8 reserved:1;
  52. u8 alarm:1;
  53. u8 done:1;
  54. } fields;
  55. };
  56. enum acpi_smb_status_codes {
  57. SMBUS_OK = 0,
  58. SMBUS_UNKNOWN_FAILURE = 0x07,
  59. SMBUS_DEVICE_ADDRESS_NACK = 0x10,
  60. SMBUS_DEVICE_ERROR = 0x11,
  61. SMBUS_DEVICE_COMMAND_ACCESS_DENIED = 0x12,
  62. SMBUS_UNKNOWN_ERROR = 0x13,
  63. SMBUS_DEVICE_ACCESS_DENIED = 0x17,
  64. SMBUS_TIMEOUT = 0x18,
  65. SMBUS_HOST_UNSUPPORTED_PROTOCOL = 0x19,
  66. SMBUS_BUSY = 0x1a,
  67. SMBUS_PEC_ERROR = 0x1f,
  68. };
  69. enum acpi_smb_offset {
  70. ACPI_SMB_PROTOCOL = 0, /* protocol, PEC */
  71. ACPI_SMB_STATUS = 1, /* status */
  72. ACPI_SMB_ADDRESS = 2, /* address */
  73. ACPI_SMB_COMMAND = 3, /* command */
  74. ACPI_SMB_DATA = 4, /* 32 data registers */
  75. ACPI_SMB_BLOCK_COUNT = 0x24, /* number of data bytes */
  76. ACPI_SMB_ALARM_ADDRESS = 0x25, /* alarm address */
  77. ACPI_SMB_ALARM_DATA = 0x26, /* 2 bytes alarm data */
  78. };
  79. static inline int smb_hc_read(struct acpi_smb_hc *hc, u8 address, u8 *data)
  80. {
  81. return ec_read(hc->offset + address, data);
  82. }
  83. static inline int smb_hc_write(struct acpi_smb_hc *hc, u8 address, u8 data)
  84. {
  85. return ec_write(hc->offset + address, data);
  86. }
  87. static int wait_transaction_complete(struct acpi_smb_hc *hc, int timeout)
  88. {
  89. if (wait_event_timeout(hc->wait, hc->done, msecs_to_jiffies(timeout)))
  90. return 0;
  91. return -ETIME;
  92. }
  93. static int acpi_smbus_transaction(struct acpi_smb_hc *hc, u8 protocol,
  94. u8 address, u8 command, u8 *data, u8 length)
  95. {
  96. int ret = -EFAULT, i;
  97. u8 temp, sz = 0;
  98. if (!hc) {
  99. printk(KERN_ERR PREFIX "host controller is not configured\n");
  100. return ret;
  101. }
  102. mutex_lock(&hc->lock);
  103. hc->done = false;
  104. if (smb_hc_read(hc, ACPI_SMB_PROTOCOL, &temp))
  105. goto end;
  106. if (temp) {
  107. ret = -EBUSY;
  108. goto end;
  109. }
  110. smb_hc_write(hc, ACPI_SMB_COMMAND, command);
  111. if (!(protocol & 0x01)) {
  112. smb_hc_write(hc, ACPI_SMB_BLOCK_COUNT, length);
  113. for (i = 0; i < length; ++i)
  114. smb_hc_write(hc, ACPI_SMB_DATA + i, data[i]);
  115. }
  116. smb_hc_write(hc, ACPI_SMB_ADDRESS, address << 1);
  117. smb_hc_write(hc, ACPI_SMB_PROTOCOL, protocol);
  118. /*
  119. * Wait for completion. Save the status code, data size,
  120. * and data into the return package (if required by the protocol).
  121. */
  122. ret = wait_transaction_complete(hc, 1000);
  123. if (ret || !(protocol & 0x01))
  124. goto end;
  125. switch (protocol) {
  126. case SMBUS_RECEIVE_BYTE:
  127. case SMBUS_READ_BYTE:
  128. sz = 1;
  129. break;
  130. case SMBUS_READ_WORD:
  131. sz = 2;
  132. break;
  133. case SMBUS_READ_BLOCK:
  134. if (smb_hc_read(hc, ACPI_SMB_BLOCK_COUNT, &sz)) {
  135. ret = -EFAULT;
  136. goto end;
  137. }
  138. sz &= 0x1f;
  139. break;
  140. }
  141. for (i = 0; i < sz; ++i)
  142. smb_hc_read(hc, ACPI_SMB_DATA + i, &data[i]);
  143. end:
  144. mutex_unlock(&hc->lock);
  145. return ret;
  146. }
  147. int acpi_smbus_read(struct acpi_smb_hc *hc, u8 protocol, u8 address,
  148. u8 command, u8 *data)
  149. {
  150. return acpi_smbus_transaction(hc, protocol, address, command, data, 0);
  151. }
  152. EXPORT_SYMBOL_GPL(acpi_smbus_read);
  153. int acpi_smbus_write(struct acpi_smb_hc *hc, u8 protocol, u8 address,
  154. u8 command, u8 *data, u8 length)
  155. {
  156. return acpi_smbus_transaction(hc, protocol, address, command, data, length);
  157. }
  158. EXPORT_SYMBOL_GPL(acpi_smbus_write);
  159. int acpi_smbus_register_callback(struct acpi_smb_hc *hc,
  160. smbus_alarm_callback callback, void *context)
  161. {
  162. mutex_lock(&hc->lock);
  163. hc->callback = callback;
  164. hc->context = context;
  165. mutex_unlock(&hc->lock);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(acpi_smbus_register_callback);
  169. int acpi_smbus_unregister_callback(struct acpi_smb_hc *hc)
  170. {
  171. mutex_lock(&hc->lock);
  172. hc->callback = NULL;
  173. hc->context = NULL;
  174. mutex_unlock(&hc->lock);
  175. return 0;
  176. }
  177. EXPORT_SYMBOL_GPL(acpi_smbus_unregister_callback);
  178. static inline void acpi_smbus_callback(void *context)
  179. {
  180. struct acpi_smb_hc *hc = context;
  181. if (hc->callback)
  182. hc->callback(hc->context);
  183. }
  184. static int smbus_alarm(void *context)
  185. {
  186. struct acpi_smb_hc *hc = context;
  187. union acpi_smb_status status;
  188. u8 address;
  189. if (smb_hc_read(hc, ACPI_SMB_STATUS, &status.raw))
  190. return 0;
  191. /* Check if it is only a completion notify */
  192. if (status.fields.done && status.fields.status == SMBUS_OK) {
  193. hc->done = true;
  194. wake_up(&hc->wait);
  195. }
  196. if (!status.fields.alarm)
  197. return 0;
  198. mutex_lock(&hc->lock);
  199. smb_hc_read(hc, ACPI_SMB_ALARM_ADDRESS, &address);
  200. status.fields.alarm = 0;
  201. smb_hc_write(hc, ACPI_SMB_STATUS, status.raw);
  202. /* We are only interested in events coming from known devices */
  203. switch (address >> 1) {
  204. case ACPI_SBS_CHARGER:
  205. case ACPI_SBS_MANAGER:
  206. case ACPI_SBS_BATTERY:
  207. acpi_os_execute(OSL_NOTIFY_HANDLER,
  208. acpi_smbus_callback, hc);
  209. default:;
  210. }
  211. mutex_unlock(&hc->lock);
  212. return 0;
  213. }
  214. typedef int (*acpi_ec_query_func) (void *data);
  215. extern int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  216. acpi_handle handle, acpi_ec_query_func func,
  217. void *data);
  218. static int acpi_smbus_hc_add(struct acpi_device *device)
  219. {
  220. int status;
  221. unsigned long long val;
  222. struct acpi_smb_hc *hc;
  223. if (!device)
  224. return -EINVAL;
  225. status = acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
  226. if (ACPI_FAILURE(status)) {
  227. printk(KERN_ERR PREFIX "error obtaining _EC.\n");
  228. return -EIO;
  229. }
  230. strcpy(acpi_device_name(device), ACPI_SMB_HC_DEVICE_NAME);
  231. strcpy(acpi_device_class(device), ACPI_SMB_HC_CLASS);
  232. hc = kzalloc(sizeof(struct acpi_smb_hc), GFP_KERNEL);
  233. if (!hc)
  234. return -ENOMEM;
  235. mutex_init(&hc->lock);
  236. init_waitqueue_head(&hc->wait);
  237. hc->ec = acpi_driver_data(device->parent);
  238. hc->offset = (val >> 8) & 0xff;
  239. hc->query_bit = val & 0xff;
  240. device->driver_data = hc;
  241. acpi_ec_add_query_handler(hc->ec, hc->query_bit, NULL, smbus_alarm, hc);
  242. dev_info(&device->dev, "SBS HC: offset = 0x%0x, query_bit = 0x%0x\n",
  243. hc->offset, hc->query_bit);
  244. return 0;
  245. }
  246. extern void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit);
  247. static int acpi_smbus_hc_remove(struct acpi_device *device)
  248. {
  249. struct acpi_smb_hc *hc;
  250. if (!device)
  251. return -EINVAL;
  252. hc = acpi_driver_data(device);
  253. acpi_ec_remove_query_handler(hc->ec, hc->query_bit);
  254. kfree(hc);
  255. device->driver_data = NULL;
  256. return 0;
  257. }
  258. module_acpi_driver(acpi_smb_hc_driver);
  259. MODULE_LICENSE("GPL");
  260. MODULE_AUTHOR("Alexey Starikovskiy");
  261. MODULE_DESCRIPTION("ACPI SMBus HC driver");