cros_ec.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * ChromeOS EC multi-function device
  3. *
  4. * Copyright (C) 2012 Google, Inc
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * The ChromeOS EC multi function device is used to mux all the requests
  16. * to the EC device for its multiple features: keyboard controller,
  17. * battery charging and regulator control, firmware update.
  18. */
  19. #include <linux/of_platform.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/mfd/cros_ec.h>
  25. #include <linux/suspend.h>
  26. #include <asm/unaligned.h>
  27. #define CROS_EC_DEV_EC_INDEX 0
  28. #define CROS_EC_DEV_PD_INDEX 1
  29. static struct cros_ec_platform ec_p = {
  30. .ec_name = CROS_EC_DEV_NAME,
  31. .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
  32. };
  33. static struct cros_ec_platform pd_p = {
  34. .ec_name = CROS_EC_DEV_PD_NAME,
  35. .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
  36. };
  37. static const struct mfd_cell ec_cell = {
  38. .name = "cros-ec-dev",
  39. .platform_data = &ec_p,
  40. .pdata_size = sizeof(ec_p),
  41. };
  42. static const struct mfd_cell ec_pd_cell = {
  43. .name = "cros-ec-dev",
  44. .platform_data = &pd_p,
  45. .pdata_size = sizeof(pd_p),
  46. };
  47. static irqreturn_t ec_irq_thread(int irq, void *data)
  48. {
  49. struct cros_ec_device *ec_dev = data;
  50. bool wake_event = true;
  51. int ret;
  52. ret = cros_ec_get_next_event(ec_dev, &wake_event);
  53. /*
  54. * Signal only if wake host events or any interrupt if
  55. * cros_ec_get_next_event() returned an error (default value for
  56. * wake_event is true)
  57. */
  58. if (wake_event && device_may_wakeup(ec_dev->dev))
  59. pm_wakeup_event(ec_dev->dev, 0);
  60. if (ret > 0)
  61. blocking_notifier_call_chain(&ec_dev->event_notifier,
  62. 0, ec_dev);
  63. return IRQ_HANDLED;
  64. }
  65. static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
  66. {
  67. struct {
  68. struct cros_ec_command msg;
  69. struct ec_params_host_sleep_event req;
  70. } __packed buf;
  71. memset(&buf, 0, sizeof(buf));
  72. buf.req.sleep_event = sleep_event;
  73. buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
  74. buf.msg.version = 0;
  75. buf.msg.outsize = sizeof(buf.req);
  76. return cros_ec_cmd_xfer(ec_dev, &buf.msg);
  77. }
  78. int cros_ec_register(struct cros_ec_device *ec_dev)
  79. {
  80. struct device *dev = ec_dev->dev;
  81. int err = 0;
  82. BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
  83. ec_dev->max_request = sizeof(struct ec_params_hello);
  84. ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
  85. ec_dev->max_passthru = 0;
  86. ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
  87. if (!ec_dev->din)
  88. return -ENOMEM;
  89. ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
  90. if (!ec_dev->dout)
  91. return -ENOMEM;
  92. mutex_init(&ec_dev->lock);
  93. err = cros_ec_query_all(ec_dev);
  94. if (err) {
  95. dev_err(dev, "Cannot identify the EC: error %d\n", err);
  96. return err;
  97. }
  98. if (ec_dev->irq) {
  99. err = devm_request_threaded_irq(dev, ec_dev->irq, NULL,
  100. ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  101. "chromeos-ec", ec_dev);
  102. if (err) {
  103. dev_err(dev, "Failed to request IRQ %d: %d",
  104. ec_dev->irq, err);
  105. return err;
  106. }
  107. }
  108. err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, 1,
  109. NULL, ec_dev->irq, NULL);
  110. if (err) {
  111. dev_err(dev,
  112. "Failed to register Embedded Controller subdevice %d\n",
  113. err);
  114. return err;
  115. }
  116. if (ec_dev->max_passthru) {
  117. /*
  118. * Register a PD device as well on top of this device.
  119. * We make the following assumptions:
  120. * - behind an EC, we have a pd
  121. * - only one device added.
  122. * - the EC is responsive at init time (it is not true for a
  123. * sensor hub.
  124. */
  125. err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
  126. &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
  127. if (err) {
  128. dev_err(dev,
  129. "Failed to register Power Delivery subdevice %d\n",
  130. err);
  131. return err;
  132. }
  133. }
  134. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  135. err = devm_of_platform_populate(dev);
  136. if (err) {
  137. mfd_remove_devices(dev);
  138. dev_err(dev, "Failed to register sub-devices\n");
  139. return err;
  140. }
  141. }
  142. /*
  143. * Clear sleep event - this will fail harmlessly on platforms that
  144. * don't implement the sleep event host command.
  145. */
  146. err = cros_ec_sleep_event(ec_dev, 0);
  147. if (err < 0)
  148. dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
  149. err);
  150. dev_info(dev, "Chrome EC device registered\n");
  151. return 0;
  152. }
  153. EXPORT_SYMBOL(cros_ec_register);
  154. int cros_ec_remove(struct cros_ec_device *ec_dev)
  155. {
  156. mfd_remove_devices(ec_dev->dev);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(cros_ec_remove);
  160. #ifdef CONFIG_PM_SLEEP
  161. int cros_ec_suspend(struct cros_ec_device *ec_dev)
  162. {
  163. struct device *dev = ec_dev->dev;
  164. int ret;
  165. u8 sleep_event;
  166. sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
  167. HOST_SLEEP_EVENT_S3_SUSPEND :
  168. HOST_SLEEP_EVENT_S0IX_SUSPEND;
  169. ret = cros_ec_sleep_event(ec_dev, sleep_event);
  170. if (ret < 0)
  171. dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
  172. ret);
  173. if (device_may_wakeup(dev))
  174. ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
  175. disable_irq(ec_dev->irq);
  176. ec_dev->was_wake_device = ec_dev->wake_enabled;
  177. ec_dev->suspended = true;
  178. return 0;
  179. }
  180. EXPORT_SYMBOL(cros_ec_suspend);
  181. static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
  182. {
  183. while (cros_ec_get_next_event(ec_dev, NULL) > 0)
  184. blocking_notifier_call_chain(&ec_dev->event_notifier,
  185. 1, ec_dev);
  186. }
  187. int cros_ec_resume(struct cros_ec_device *ec_dev)
  188. {
  189. int ret;
  190. u8 sleep_event;
  191. ec_dev->suspended = false;
  192. enable_irq(ec_dev->irq);
  193. sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
  194. HOST_SLEEP_EVENT_S3_RESUME :
  195. HOST_SLEEP_EVENT_S0IX_RESUME;
  196. ret = cros_ec_sleep_event(ec_dev, sleep_event);
  197. if (ret < 0)
  198. dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
  199. ret);
  200. if (ec_dev->wake_enabled) {
  201. disable_irq_wake(ec_dev->irq);
  202. ec_dev->wake_enabled = 0;
  203. }
  204. /*
  205. * Let the mfd devices know about events that occur during
  206. * suspend. This way the clients know what to do with them.
  207. */
  208. cros_ec_report_events_during_suspend(ec_dev);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(cros_ec_resume);
  212. #endif
  213. MODULE_LICENSE("GPL");
  214. MODULE_DESCRIPTION("ChromeOS EC core driver");