sbshc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SMBus driver for ACPI Embedded Controller (v0.1)
  4. *
  5. * Copyright (c) 2007 Alexey Starikovskiy
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/wait.h>
  9. #include <linux/slab.h>
  10. #include <linux/delay.h>
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include "sbshc.h"
  14. #define PREFIX "ACPI: "
  15. #define ACPI_SMB_HC_CLASS "smbus_host_ctl"
  16. #define ACPI_SMB_HC_DEVICE_NAME "ACPI SMBus HC"
  17. struct acpi_smb_hc {
  18. struct acpi_ec *ec;
  19. struct mutex lock;
  20. wait_queue_head_t wait;
  21. u8 offset;
  22. u8 query_bit;
  23. smbus_alarm_callback callback;
  24. void *context;
  25. bool done;
  26. };
  27. static int acpi_smbus_hc_add(struct acpi_device *device);
  28. static int acpi_smbus_hc_remove(struct acpi_device *device);
  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 int wait_transaction_complete(struct acpi_smb_hc *hc, int timeout)
  85. {
  86. if (wait_event_timeout(hc->wait, hc->done, msecs_to_jiffies(timeout)))
  87. return 0;
  88. return -ETIME;
  89. }
  90. static int acpi_smbus_transaction(struct acpi_smb_hc *hc, u8 protocol,
  91. u8 address, u8 command, u8 *data, u8 length)
  92. {
  93. int ret = -EFAULT, i;
  94. u8 temp, sz = 0;
  95. if (!hc) {
  96. printk(KERN_ERR PREFIX "host controller is not configured\n");
  97. return ret;
  98. }
  99. mutex_lock(&hc->lock);
  100. hc->done = false;
  101. if (smb_hc_read(hc, ACPI_SMB_PROTOCOL, &temp))
  102. goto end;
  103. if (temp) {
  104. ret = -EBUSY;
  105. goto end;
  106. }
  107. smb_hc_write(hc, ACPI_SMB_COMMAND, command);
  108. if (!(protocol & 0x01)) {
  109. smb_hc_write(hc, ACPI_SMB_BLOCK_COUNT, length);
  110. for (i = 0; i < length; ++i)
  111. smb_hc_write(hc, ACPI_SMB_DATA + i, data[i]);
  112. }
  113. smb_hc_write(hc, ACPI_SMB_ADDRESS, address << 1);
  114. smb_hc_write(hc, ACPI_SMB_PROTOCOL, protocol);
  115. /*
  116. * Wait for completion. Save the status code, data size,
  117. * and data into the return package (if required by the protocol).
  118. */
  119. ret = wait_transaction_complete(hc, 1000);
  120. if (ret || !(protocol & 0x01))
  121. goto end;
  122. switch (protocol) {
  123. case SMBUS_RECEIVE_BYTE:
  124. case SMBUS_READ_BYTE:
  125. sz = 1;
  126. break;
  127. case SMBUS_READ_WORD:
  128. sz = 2;
  129. break;
  130. case SMBUS_READ_BLOCK:
  131. if (smb_hc_read(hc, ACPI_SMB_BLOCK_COUNT, &sz)) {
  132. ret = -EFAULT;
  133. goto end;
  134. }
  135. sz &= 0x1f;
  136. break;
  137. }
  138. for (i = 0; i < sz; ++i)
  139. smb_hc_read(hc, ACPI_SMB_DATA + i, &data[i]);
  140. end:
  141. mutex_unlock(&hc->lock);
  142. return ret;
  143. }
  144. int acpi_smbus_read(struct acpi_smb_hc *hc, u8 protocol, u8 address,
  145. u8 command, u8 *data)
  146. {
  147. return acpi_smbus_transaction(hc, protocol, address, command, data, 0);
  148. }
  149. EXPORT_SYMBOL_GPL(acpi_smbus_read);
  150. int acpi_smbus_write(struct acpi_smb_hc *hc, u8 protocol, u8 address,
  151. u8 command, u8 *data, u8 length)
  152. {
  153. return acpi_smbus_transaction(hc, protocol, address, command, data, length);
  154. }
  155. EXPORT_SYMBOL_GPL(acpi_smbus_write);
  156. int acpi_smbus_register_callback(struct acpi_smb_hc *hc,
  157. smbus_alarm_callback callback, void *context)
  158. {
  159. mutex_lock(&hc->lock);
  160. hc->callback = callback;
  161. hc->context = context;
  162. mutex_unlock(&hc->lock);
  163. return 0;
  164. }
  165. EXPORT_SYMBOL_GPL(acpi_smbus_register_callback);
  166. int acpi_smbus_unregister_callback(struct acpi_smb_hc *hc)
  167. {
  168. mutex_lock(&hc->lock);
  169. hc->callback = NULL;
  170. hc->context = NULL;
  171. mutex_unlock(&hc->lock);
  172. acpi_os_wait_events_complete();
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(acpi_smbus_unregister_callback);
  176. static inline void acpi_smbus_callback(void *context)
  177. {
  178. struct acpi_smb_hc *hc = context;
  179. if (hc->callback)
  180. hc->callback(hc->context);
  181. }
  182. static int smbus_alarm(void *context)
  183. {
  184. struct acpi_smb_hc *hc = context;
  185. union acpi_smb_status status;
  186. u8 address;
  187. if (smb_hc_read(hc, ACPI_SMB_STATUS, &status.raw))
  188. return 0;
  189. /* Check if it is only a completion notify */
  190. if (status.fields.done && status.fields.status == SMBUS_OK) {
  191. hc->done = true;
  192. wake_up(&hc->wait);
  193. }
  194. if (!status.fields.alarm)
  195. return 0;
  196. mutex_lock(&hc->lock);
  197. smb_hc_read(hc, ACPI_SMB_ALARM_ADDRESS, &address);
  198. status.fields.alarm = 0;
  199. smb_hc_write(hc, ACPI_SMB_STATUS, status.raw);
  200. /* We are only interested in events coming from known devices */
  201. switch (address >> 1) {
  202. case ACPI_SBS_CHARGER:
  203. case ACPI_SBS_MANAGER:
  204. case ACPI_SBS_BATTERY:
  205. acpi_os_execute(OSL_NOTIFY_HANDLER,
  206. acpi_smbus_callback, hc);
  207. default:;
  208. }
  209. mutex_unlock(&hc->lock);
  210. return 0;
  211. }
  212. typedef int (*acpi_ec_query_func) (void *data);
  213. extern int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  214. acpi_handle handle, acpi_ec_query_func func,
  215. void *data);
  216. static int acpi_smbus_hc_add(struct acpi_device *device)
  217. {
  218. int status;
  219. unsigned long long val;
  220. struct acpi_smb_hc *hc;
  221. if (!device)
  222. return -EINVAL;
  223. status = acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
  224. if (ACPI_FAILURE(status)) {
  225. printk(KERN_ERR PREFIX "error obtaining _EC.\n");
  226. return -EIO;
  227. }
  228. strcpy(acpi_device_name(device), ACPI_SMB_HC_DEVICE_NAME);
  229. strcpy(acpi_device_class(device), ACPI_SMB_HC_CLASS);
  230. hc = kzalloc(sizeof(struct acpi_smb_hc), GFP_KERNEL);
  231. if (!hc)
  232. return -ENOMEM;
  233. mutex_init(&hc->lock);
  234. init_waitqueue_head(&hc->wait);
  235. hc->ec = acpi_driver_data(device->parent);
  236. hc->offset = (val >> 8) & 0xff;
  237. hc->query_bit = val & 0xff;
  238. device->driver_data = hc;
  239. acpi_ec_add_query_handler(hc->ec, hc->query_bit, NULL, smbus_alarm, hc);
  240. dev_info(&device->dev, "SBS HC: offset = 0x%0x, query_bit = 0x%0x\n",
  241. hc->offset, hc->query_bit);
  242. return 0;
  243. }
  244. extern void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit);
  245. static int acpi_smbus_hc_remove(struct acpi_device *device)
  246. {
  247. struct acpi_smb_hc *hc;
  248. if (!device)
  249. return -EINVAL;
  250. hc = acpi_driver_data(device);
  251. acpi_ec_remove_query_handler(hc->ec, hc->query_bit);
  252. acpi_os_wait_events_complete();
  253. kfree(hc);
  254. device->driver_data = NULL;
  255. return 0;
  256. }
  257. module_acpi_driver(acpi_smb_hc_driver);
  258. MODULE_LICENSE("GPL");
  259. MODULE_AUTHOR("Alexey Starikovskiy");
  260. MODULE_DESCRIPTION("ACPI SMBus HC driver");