cros_ec.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #define CROS_EC_DEV_EC_INDEX 0
  26. #define CROS_EC_DEV_PD_INDEX 1
  27. static struct cros_ec_platform ec_p = {
  28. .ec_name = CROS_EC_DEV_NAME,
  29. .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
  30. };
  31. static struct cros_ec_platform pd_p = {
  32. .ec_name = CROS_EC_DEV_PD_NAME,
  33. .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
  34. };
  35. static const struct mfd_cell ec_cell = {
  36. .name = "cros-ec-ctl",
  37. .platform_data = &ec_p,
  38. .pdata_size = sizeof(ec_p),
  39. };
  40. static const struct mfd_cell ec_pd_cell = {
  41. .name = "cros-ec-ctl",
  42. .platform_data = &pd_p,
  43. .pdata_size = sizeof(pd_p),
  44. };
  45. int cros_ec_register(struct cros_ec_device *ec_dev)
  46. {
  47. struct device *dev = ec_dev->dev;
  48. int err = 0;
  49. ec_dev->max_request = sizeof(struct ec_params_hello);
  50. ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
  51. ec_dev->max_passthru = 0;
  52. ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
  53. if (!ec_dev->din)
  54. return -ENOMEM;
  55. ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
  56. if (!ec_dev->dout)
  57. return -ENOMEM;
  58. mutex_init(&ec_dev->lock);
  59. cros_ec_query_all(ec_dev);
  60. err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, 1,
  61. NULL, ec_dev->irq, NULL);
  62. if (err) {
  63. dev_err(dev,
  64. "Failed to register Embedded Controller subdevice %d\n",
  65. err);
  66. return err;
  67. }
  68. if (ec_dev->max_passthru) {
  69. /*
  70. * Register a PD device as well on top of this device.
  71. * We make the following assumptions:
  72. * - behind an EC, we have a pd
  73. * - only one device added.
  74. * - the EC is responsive at init time (it is not true for a
  75. * sensor hub.
  76. */
  77. err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
  78. &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
  79. if (err) {
  80. dev_err(dev,
  81. "Failed to register Power Delivery subdevice %d\n",
  82. err);
  83. return err;
  84. }
  85. }
  86. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  87. err = of_platform_populate(dev->of_node, NULL, NULL, dev);
  88. if (err) {
  89. mfd_remove_devices(dev);
  90. dev_err(dev, "Failed to register sub-devices\n");
  91. return err;
  92. }
  93. }
  94. dev_info(dev, "Chrome EC device registered\n");
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(cros_ec_register);
  98. int cros_ec_remove(struct cros_ec_device *ec_dev)
  99. {
  100. mfd_remove_devices(ec_dev->dev);
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(cros_ec_remove);
  104. #ifdef CONFIG_PM_SLEEP
  105. int cros_ec_suspend(struct cros_ec_device *ec_dev)
  106. {
  107. struct device *dev = ec_dev->dev;
  108. if (device_may_wakeup(dev))
  109. ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
  110. disable_irq(ec_dev->irq);
  111. ec_dev->was_wake_device = ec_dev->wake_enabled;
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(cros_ec_suspend);
  115. int cros_ec_resume(struct cros_ec_device *ec_dev)
  116. {
  117. enable_irq(ec_dev->irq);
  118. if (ec_dev->wake_enabled) {
  119. disable_irq_wake(ec_dev->irq);
  120. ec_dev->wake_enabled = 0;
  121. }
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(cros_ec_resume);
  125. #endif
  126. MODULE_LICENSE("GPL");
  127. MODULE_DESCRIPTION("ChromeOS EC core driver");