goldfish_battery.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Power supply driver for the goldfish emulator
  3. *
  4. * Copyright (C) 2008 Google, Inc.
  5. * Copyright (C) 2012 Intel, Inc.
  6. * Copyright (C) 2013 Intel, Inc.
  7. * Author: Mike Lockwood <lockwood@android.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/err.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/power_supply.h>
  22. #include <linux/types.h>
  23. #include <linux/pci.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/acpi.h>
  27. struct goldfish_battery_data {
  28. void __iomem *reg_base;
  29. int irq;
  30. spinlock_t lock;
  31. struct power_supply *battery;
  32. struct power_supply *ac;
  33. };
  34. #define GOLDFISH_BATTERY_READ(data, addr) \
  35. (readl(data->reg_base + addr))
  36. #define GOLDFISH_BATTERY_WRITE(data, addr, x) \
  37. (writel(x, data->reg_base + addr))
  38. /*
  39. * Temporary variable used between goldfish_battery_probe() and
  40. * goldfish_battery_open().
  41. */
  42. static struct goldfish_battery_data *battery_data;
  43. enum {
  44. /* status register */
  45. BATTERY_INT_STATUS = 0x00,
  46. /* set this to enable IRQ */
  47. BATTERY_INT_ENABLE = 0x04,
  48. BATTERY_AC_ONLINE = 0x08,
  49. BATTERY_STATUS = 0x0C,
  50. BATTERY_HEALTH = 0x10,
  51. BATTERY_PRESENT = 0x14,
  52. BATTERY_CAPACITY = 0x18,
  53. BATTERY_STATUS_CHANGED = 1U << 0,
  54. AC_STATUS_CHANGED = 1U << 1,
  55. BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
  56. };
  57. static int goldfish_ac_get_property(struct power_supply *psy,
  58. enum power_supply_property psp,
  59. union power_supply_propval *val)
  60. {
  61. struct goldfish_battery_data *data = power_supply_get_drvdata(psy);
  62. int ret = 0;
  63. switch (psp) {
  64. case POWER_SUPPLY_PROP_ONLINE:
  65. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_AC_ONLINE);
  66. break;
  67. default:
  68. ret = -EINVAL;
  69. break;
  70. }
  71. return ret;
  72. }
  73. static int goldfish_battery_get_property(struct power_supply *psy,
  74. enum power_supply_property psp,
  75. union power_supply_propval *val)
  76. {
  77. struct goldfish_battery_data *data = power_supply_get_drvdata(psy);
  78. int ret = 0;
  79. switch (psp) {
  80. case POWER_SUPPLY_PROP_STATUS:
  81. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_STATUS);
  82. break;
  83. case POWER_SUPPLY_PROP_HEALTH:
  84. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_HEALTH);
  85. break;
  86. case POWER_SUPPLY_PROP_PRESENT:
  87. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_PRESENT);
  88. break;
  89. case POWER_SUPPLY_PROP_TECHNOLOGY:
  90. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  91. break;
  92. case POWER_SUPPLY_PROP_CAPACITY:
  93. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_CAPACITY);
  94. break;
  95. default:
  96. ret = -EINVAL;
  97. break;
  98. }
  99. return ret;
  100. }
  101. static enum power_supply_property goldfish_battery_props[] = {
  102. POWER_SUPPLY_PROP_STATUS,
  103. POWER_SUPPLY_PROP_HEALTH,
  104. POWER_SUPPLY_PROP_PRESENT,
  105. POWER_SUPPLY_PROP_TECHNOLOGY,
  106. POWER_SUPPLY_PROP_CAPACITY,
  107. };
  108. static enum power_supply_property goldfish_ac_props[] = {
  109. POWER_SUPPLY_PROP_ONLINE,
  110. };
  111. static irqreturn_t goldfish_battery_interrupt(int irq, void *dev_id)
  112. {
  113. unsigned long irq_flags;
  114. struct goldfish_battery_data *data = dev_id;
  115. uint32_t status;
  116. spin_lock_irqsave(&data->lock, irq_flags);
  117. /* read status flags, which will clear the interrupt */
  118. status = GOLDFISH_BATTERY_READ(data, BATTERY_INT_STATUS);
  119. status &= BATTERY_INT_MASK;
  120. if (status & BATTERY_STATUS_CHANGED)
  121. power_supply_changed(data->battery);
  122. if (status & AC_STATUS_CHANGED)
  123. power_supply_changed(data->ac);
  124. spin_unlock_irqrestore(&data->lock, irq_flags);
  125. return status ? IRQ_HANDLED : IRQ_NONE;
  126. }
  127. static const struct power_supply_desc battery_desc = {
  128. .properties = goldfish_battery_props,
  129. .num_properties = ARRAY_SIZE(goldfish_battery_props),
  130. .get_property = goldfish_battery_get_property,
  131. .name = "battery",
  132. .type = POWER_SUPPLY_TYPE_BATTERY,
  133. };
  134. static const struct power_supply_desc ac_desc = {
  135. .properties = goldfish_ac_props,
  136. .num_properties = ARRAY_SIZE(goldfish_ac_props),
  137. .get_property = goldfish_ac_get_property,
  138. .name = "ac",
  139. .type = POWER_SUPPLY_TYPE_MAINS,
  140. };
  141. static int goldfish_battery_probe(struct platform_device *pdev)
  142. {
  143. int ret;
  144. struct resource *r;
  145. struct goldfish_battery_data *data;
  146. struct power_supply_config psy_cfg = {};
  147. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  148. if (data == NULL)
  149. return -ENOMEM;
  150. spin_lock_init(&data->lock);
  151. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  152. if (r == NULL) {
  153. dev_err(&pdev->dev, "platform_get_resource failed\n");
  154. return -ENODEV;
  155. }
  156. data->reg_base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  157. if (data->reg_base == NULL) {
  158. dev_err(&pdev->dev, "unable to remap MMIO\n");
  159. return -ENOMEM;
  160. }
  161. data->irq = platform_get_irq(pdev, 0);
  162. if (data->irq < 0) {
  163. dev_err(&pdev->dev, "platform_get_irq failed\n");
  164. return -ENODEV;
  165. }
  166. ret = devm_request_irq(&pdev->dev, data->irq, goldfish_battery_interrupt,
  167. IRQF_SHARED, pdev->name, data);
  168. if (ret)
  169. return ret;
  170. psy_cfg.drv_data = data;
  171. data->ac = power_supply_register(&pdev->dev, &ac_desc, &psy_cfg);
  172. if (IS_ERR(data->ac))
  173. return PTR_ERR(data->ac);
  174. data->battery = power_supply_register(&pdev->dev, &battery_desc,
  175. &psy_cfg);
  176. if (IS_ERR(data->battery)) {
  177. power_supply_unregister(data->ac);
  178. return PTR_ERR(data->battery);
  179. }
  180. platform_set_drvdata(pdev, data);
  181. battery_data = data;
  182. GOLDFISH_BATTERY_WRITE(data, BATTERY_INT_ENABLE, BATTERY_INT_MASK);
  183. return 0;
  184. }
  185. static int goldfish_battery_remove(struct platform_device *pdev)
  186. {
  187. struct goldfish_battery_data *data = platform_get_drvdata(pdev);
  188. power_supply_unregister(data->battery);
  189. power_supply_unregister(data->ac);
  190. battery_data = NULL;
  191. return 0;
  192. }
  193. static const struct of_device_id goldfish_battery_of_match[] = {
  194. { .compatible = "google,goldfish-battery", },
  195. {},
  196. };
  197. MODULE_DEVICE_TABLE(of, goldfish_battery_of_match);
  198. static const struct acpi_device_id goldfish_battery_acpi_match[] = {
  199. { "GFSH0001", 0 },
  200. { },
  201. };
  202. MODULE_DEVICE_TABLE(acpi, goldfish_battery_acpi_match);
  203. static struct platform_driver goldfish_battery_device = {
  204. .probe = goldfish_battery_probe,
  205. .remove = goldfish_battery_remove,
  206. .driver = {
  207. .name = "goldfish-battery",
  208. .of_match_table = goldfish_battery_of_match,
  209. .acpi_match_table = ACPI_PTR(goldfish_battery_acpi_match),
  210. }
  211. };
  212. module_platform_driver(goldfish_battery_device);
  213. MODULE_AUTHOR("Mike Lockwood lockwood@android.com");
  214. MODULE_LICENSE("GPL");
  215. MODULE_DESCRIPTION("Battery driver for the Goldfish emulator");