sbshc.c 7.9 KB

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