sbshc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 <linux/dmi.h>
  17. #include "sbshc.h"
  18. #define PREFIX "ACPI: "
  19. #define ACPI_SMB_HC_CLASS "smbus_host_ctl"
  20. #define ACPI_SMB_HC_DEVICE_NAME "ACPI SMBus HC"
  21. struct acpi_smb_hc {
  22. struct acpi_ec *ec;
  23. struct mutex lock;
  24. wait_queue_head_t wait;
  25. u8 offset;
  26. u8 query_bit;
  27. smbus_alarm_callback callback;
  28. void *context;
  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 bool macbook;
  80. static inline int smb_hc_read(struct acpi_smb_hc *hc, u8 address, u8 *data)
  81. {
  82. return ec_read(hc->offset + address, data);
  83. }
  84. static inline int smb_hc_write(struct acpi_smb_hc *hc, u8 address, u8 data)
  85. {
  86. return ec_write(hc->offset + address, data);
  87. }
  88. static inline int smb_check_done(struct acpi_smb_hc *hc)
  89. {
  90. union acpi_smb_status status = {.raw = 0};
  91. smb_hc_read(hc, ACPI_SMB_STATUS, &status.raw);
  92. return status.fields.done && (status.fields.status == SMBUS_OK);
  93. }
  94. static int wait_transaction_complete(struct acpi_smb_hc *hc, int timeout)
  95. {
  96. if (wait_event_timeout(hc->wait, smb_check_done(hc),
  97. msecs_to_jiffies(timeout)))
  98. return 0;
  99. /*
  100. * After the timeout happens, OS will try to check the status of SMbus.
  101. * If the status is what OS expected, it will be regarded as the bogus
  102. * timeout.
  103. */
  104. if (smb_check_done(hc))
  105. return 0;
  106. else
  107. return -ETIME;
  108. }
  109. static int acpi_smbus_transaction(struct acpi_smb_hc *hc, u8 protocol,
  110. u8 address, u8 command, u8 *data, u8 length)
  111. {
  112. int ret = -EFAULT, i;
  113. u8 temp, sz = 0;
  114. if (!hc) {
  115. printk(KERN_ERR PREFIX "host controller is not configured\n");
  116. return ret;
  117. }
  118. mutex_lock(&hc->lock);
  119. if (macbook)
  120. udelay(5);
  121. if (smb_hc_read(hc, ACPI_SMB_PROTOCOL, &temp))
  122. goto end;
  123. if (temp) {
  124. ret = -EBUSY;
  125. goto end;
  126. }
  127. smb_hc_write(hc, ACPI_SMB_COMMAND, command);
  128. if (!(protocol & 0x01)) {
  129. smb_hc_write(hc, ACPI_SMB_BLOCK_COUNT, length);
  130. for (i = 0; i < length; ++i)
  131. smb_hc_write(hc, ACPI_SMB_DATA + i, data[i]);
  132. }
  133. smb_hc_write(hc, ACPI_SMB_ADDRESS, address << 1);
  134. smb_hc_write(hc, ACPI_SMB_PROTOCOL, protocol);
  135. /*
  136. * Wait for completion. Save the status code, data size,
  137. * and data into the return package (if required by the protocol).
  138. */
  139. ret = wait_transaction_complete(hc, 1000);
  140. if (ret || !(protocol & 0x01))
  141. goto end;
  142. switch (protocol) {
  143. case SMBUS_RECEIVE_BYTE:
  144. case SMBUS_READ_BYTE:
  145. sz = 1;
  146. break;
  147. case SMBUS_READ_WORD:
  148. sz = 2;
  149. break;
  150. case SMBUS_READ_BLOCK:
  151. if (smb_hc_read(hc, ACPI_SMB_BLOCK_COUNT, &sz)) {
  152. ret = -EFAULT;
  153. goto end;
  154. }
  155. sz &= 0x1f;
  156. break;
  157. }
  158. for (i = 0; i < sz; ++i)
  159. smb_hc_read(hc, ACPI_SMB_DATA + i, &data[i]);
  160. end:
  161. mutex_unlock(&hc->lock);
  162. return ret;
  163. }
  164. int acpi_smbus_read(struct acpi_smb_hc *hc, u8 protocol, u8 address,
  165. u8 command, u8 *data)
  166. {
  167. return acpi_smbus_transaction(hc, protocol, address, command, data, 0);
  168. }
  169. EXPORT_SYMBOL_GPL(acpi_smbus_read);
  170. int acpi_smbus_write(struct acpi_smb_hc *hc, u8 protocol, u8 address,
  171. u8 command, u8 *data, u8 length)
  172. {
  173. return acpi_smbus_transaction(hc, protocol, address, command, data, length);
  174. }
  175. EXPORT_SYMBOL_GPL(acpi_smbus_write);
  176. int acpi_smbus_register_callback(struct acpi_smb_hc *hc,
  177. smbus_alarm_callback callback, void *context)
  178. {
  179. mutex_lock(&hc->lock);
  180. hc->callback = callback;
  181. hc->context = context;
  182. mutex_unlock(&hc->lock);
  183. return 0;
  184. }
  185. EXPORT_SYMBOL_GPL(acpi_smbus_register_callback);
  186. int acpi_smbus_unregister_callback(struct acpi_smb_hc *hc)
  187. {
  188. mutex_lock(&hc->lock);
  189. hc->callback = NULL;
  190. hc->context = NULL;
  191. mutex_unlock(&hc->lock);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL_GPL(acpi_smbus_unregister_callback);
  195. static inline void acpi_smbus_callback(void *context)
  196. {
  197. struct acpi_smb_hc *hc = context;
  198. if (hc->callback)
  199. hc->callback(hc->context);
  200. }
  201. static int smbus_alarm(void *context)
  202. {
  203. struct acpi_smb_hc *hc = context;
  204. union acpi_smb_status status;
  205. u8 address;
  206. if (smb_hc_read(hc, ACPI_SMB_STATUS, &status.raw))
  207. return 0;
  208. /* Check if it is only a completion notify */
  209. if (status.fields.done)
  210. wake_up(&hc->wait);
  211. if (!status.fields.alarm)
  212. return 0;
  213. mutex_lock(&hc->lock);
  214. smb_hc_read(hc, ACPI_SMB_ALARM_ADDRESS, &address);
  215. status.fields.alarm = 0;
  216. smb_hc_write(hc, ACPI_SMB_STATUS, status.raw);
  217. /* We are only interested in events coming from known devices */
  218. switch (address >> 1) {
  219. case ACPI_SBS_CHARGER:
  220. case ACPI_SBS_MANAGER:
  221. case ACPI_SBS_BATTERY:
  222. acpi_os_execute(OSL_NOTIFY_HANDLER,
  223. acpi_smbus_callback, hc);
  224. default:;
  225. }
  226. mutex_unlock(&hc->lock);
  227. return 0;
  228. }
  229. typedef int (*acpi_ec_query_func) (void *data);
  230. extern int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  231. acpi_handle handle, acpi_ec_query_func func,
  232. void *data);
  233. static int macbook_dmi_match(const struct dmi_system_id *d)
  234. {
  235. pr_debug("Detected MacBook, enabling workaround\n");
  236. macbook = true;
  237. return 0;
  238. }
  239. static struct dmi_system_id acpi_smbus_dmi_table[] = {
  240. { macbook_dmi_match, "Apple MacBook", {
  241. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  242. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
  243. },
  244. { },
  245. };
  246. static int acpi_smbus_hc_add(struct acpi_device *device)
  247. {
  248. int status;
  249. unsigned long long val;
  250. struct acpi_smb_hc *hc;
  251. dmi_check_system(acpi_smbus_dmi_table);
  252. if (!device)
  253. return -EINVAL;
  254. status = acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
  255. if (ACPI_FAILURE(status)) {
  256. printk(KERN_ERR PREFIX "error obtaining _EC.\n");
  257. return -EIO;
  258. }
  259. strcpy(acpi_device_name(device), ACPI_SMB_HC_DEVICE_NAME);
  260. strcpy(acpi_device_class(device), ACPI_SMB_HC_CLASS);
  261. hc = kzalloc(sizeof(struct acpi_smb_hc), GFP_KERNEL);
  262. if (!hc)
  263. return -ENOMEM;
  264. mutex_init(&hc->lock);
  265. init_waitqueue_head(&hc->wait);
  266. hc->ec = acpi_driver_data(device->parent);
  267. hc->offset = (val >> 8) & 0xff;
  268. hc->query_bit = val & 0xff;
  269. device->driver_data = hc;
  270. acpi_ec_add_query_handler(hc->ec, hc->query_bit, NULL, smbus_alarm, hc);
  271. printk(KERN_INFO PREFIX "SBS HC: EC = 0x%p, offset = 0x%0x, query_bit = 0x%0x\n",
  272. hc->ec, hc->offset, hc->query_bit);
  273. return 0;
  274. }
  275. extern void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit);
  276. static int acpi_smbus_hc_remove(struct acpi_device *device)
  277. {
  278. struct acpi_smb_hc *hc;
  279. if (!device)
  280. return -EINVAL;
  281. hc = acpi_driver_data(device);
  282. acpi_ec_remove_query_handler(hc->ec, hc->query_bit);
  283. kfree(hc);
  284. device->driver_data = NULL;
  285. return 0;
  286. }
  287. module_acpi_driver(acpi_smb_hc_driver);
  288. MODULE_LICENSE("GPL");
  289. MODULE_AUTHOR("Alexey Starikovskiy");
  290. MODULE_DESCRIPTION("ACPI SMBus HC driver");