sbshc.c 7.8 KB

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