cros_ec.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <asm/unaligned.h>
  26. #define CROS_EC_DEV_EC_INDEX 0
  27. #define CROS_EC_DEV_PD_INDEX 1
  28. static struct cros_ec_platform ec_p = {
  29. .ec_name = CROS_EC_DEV_NAME,
  30. .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
  31. };
  32. static struct cros_ec_platform pd_p = {
  33. .ec_name = CROS_EC_DEV_PD_NAME,
  34. .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
  35. };
  36. static const struct mfd_cell ec_cell = {
  37. .name = "cros-ec-ctl",
  38. .platform_data = &ec_p,
  39. .pdata_size = sizeof(ec_p),
  40. };
  41. static const struct mfd_cell ec_pd_cell = {
  42. .name = "cros-ec-ctl",
  43. .platform_data = &pd_p,
  44. .pdata_size = sizeof(pd_p),
  45. };
  46. static irqreturn_t ec_irq_thread(int irq, void *data)
  47. {
  48. struct cros_ec_device *ec_dev = data;
  49. int ret;
  50. if (device_may_wakeup(ec_dev->dev))
  51. pm_wakeup_event(ec_dev->dev, 0);
  52. ret = cros_ec_get_next_event(ec_dev);
  53. if (ret > 0)
  54. blocking_notifier_call_chain(&ec_dev->event_notifier,
  55. 0, ec_dev);
  56. return IRQ_HANDLED;
  57. }
  58. int cros_ec_register(struct cros_ec_device *ec_dev)
  59. {
  60. struct device *dev = ec_dev->dev;
  61. int err = 0;
  62. BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
  63. ec_dev->max_request = sizeof(struct ec_params_hello);
  64. ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
  65. ec_dev->max_passthru = 0;
  66. ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
  67. if (!ec_dev->din)
  68. return -ENOMEM;
  69. ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
  70. if (!ec_dev->dout)
  71. return -ENOMEM;
  72. mutex_init(&ec_dev->lock);
  73. err = cros_ec_query_all(ec_dev);
  74. if (err) {
  75. dev_err(dev, "Cannot identify the EC: error %d\n", err);
  76. return err;
  77. }
  78. if (ec_dev->irq) {
  79. err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread,
  80. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  81. "chromeos-ec", ec_dev);
  82. if (err) {
  83. dev_err(dev, "Failed to request IRQ %d: %d",
  84. ec_dev->irq, err);
  85. return err;
  86. }
  87. }
  88. err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, 1,
  89. NULL, ec_dev->irq, NULL);
  90. if (err) {
  91. dev_err(dev,
  92. "Failed to register Embedded Controller subdevice %d\n",
  93. err);
  94. goto fail_mfd;
  95. }
  96. if (ec_dev->max_passthru) {
  97. /*
  98. * Register a PD device as well on top of this device.
  99. * We make the following assumptions:
  100. * - behind an EC, we have a pd
  101. * - only one device added.
  102. * - the EC is responsive at init time (it is not true for a
  103. * sensor hub.
  104. */
  105. err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
  106. &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
  107. if (err) {
  108. dev_err(dev,
  109. "Failed to register Power Delivery subdevice %d\n",
  110. err);
  111. goto fail_mfd;
  112. }
  113. }
  114. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  115. err = of_platform_populate(dev->of_node, NULL, NULL, dev);
  116. if (err) {
  117. mfd_remove_devices(dev);
  118. dev_err(dev, "Failed to register sub-devices\n");
  119. goto fail_mfd;
  120. }
  121. }
  122. dev_info(dev, "Chrome EC device registered\n");
  123. return 0;
  124. fail_mfd:
  125. if (ec_dev->irq)
  126. free_irq(ec_dev->irq, ec_dev);
  127. return err;
  128. }
  129. EXPORT_SYMBOL(cros_ec_register);
  130. int cros_ec_remove(struct cros_ec_device *ec_dev)
  131. {
  132. mfd_remove_devices(ec_dev->dev);
  133. return 0;
  134. }
  135. EXPORT_SYMBOL(cros_ec_remove);
  136. #ifdef CONFIG_PM_SLEEP
  137. int cros_ec_suspend(struct cros_ec_device *ec_dev)
  138. {
  139. struct device *dev = ec_dev->dev;
  140. if (device_may_wakeup(dev))
  141. ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
  142. disable_irq(ec_dev->irq);
  143. ec_dev->was_wake_device = ec_dev->wake_enabled;
  144. return 0;
  145. }
  146. EXPORT_SYMBOL(cros_ec_suspend);
  147. static void cros_ec_drain_events(struct cros_ec_device *ec_dev)
  148. {
  149. while (cros_ec_get_next_event(ec_dev) > 0)
  150. blocking_notifier_call_chain(&ec_dev->event_notifier,
  151. 1, ec_dev);
  152. }
  153. int cros_ec_resume(struct cros_ec_device *ec_dev)
  154. {
  155. enable_irq(ec_dev->irq);
  156. /*
  157. * In some cases, we need to distinguish between events that occur
  158. * during suspend if the EC is not a wake source. For example,
  159. * keypresses during suspend should be discarded if it does not wake
  160. * the system.
  161. *
  162. * If the EC is not a wake source, drain the event queue and mark them
  163. * as "queued during suspend".
  164. */
  165. if (ec_dev->wake_enabled) {
  166. disable_irq_wake(ec_dev->irq);
  167. ec_dev->wake_enabled = 0;
  168. } else {
  169. cros_ec_drain_events(ec_dev);
  170. }
  171. return 0;
  172. }
  173. EXPORT_SYMBOL(cros_ec_resume);
  174. #endif
  175. MODULE_LICENSE("GPL");
  176. MODULE_DESCRIPTION("ChromeOS EC core driver");