sbshc.c 6.8 KB

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