sbshc.c 7.7 KB

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