goldfish_battery.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. struct goldfish_battery_data {
  27. void __iomem *reg_base;
  28. int irq;
  29. spinlock_t lock;
  30. struct power_supply *battery;
  31. struct power_supply *ac;
  32. };
  33. #define GOLDFISH_BATTERY_READ(data, addr) \
  34. (readl(data->reg_base + addr))
  35. #define GOLDFISH_BATTERY_WRITE(data, addr, x) \
  36. (writel(x, data->reg_base + addr))
  37. /*
  38. * Temporary variable used between goldfish_battery_probe() and
  39. * goldfish_battery_open().
  40. */
  41. static struct goldfish_battery_data *battery_data;
  42. enum {
  43. /* status register */
  44. BATTERY_INT_STATUS = 0x00,
  45. /* set this to enable IRQ */
  46. BATTERY_INT_ENABLE = 0x04,
  47. BATTERY_AC_ONLINE = 0x08,
  48. BATTERY_STATUS = 0x0C,
  49. BATTERY_HEALTH = 0x10,
  50. BATTERY_PRESENT = 0x14,
  51. BATTERY_CAPACITY = 0x18,
  52. BATTERY_STATUS_CHANGED = 1U << 0,
  53. AC_STATUS_CHANGED = 1U << 1,
  54. BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
  55. };
  56. static int goldfish_ac_get_property(struct power_supply *psy,
  57. enum power_supply_property psp,
  58. union power_supply_propval *val)
  59. {
  60. struct goldfish_battery_data *data = power_supply_get_drvdata(psy);
  61. int ret = 0;
  62. switch (psp) {
  63. case POWER_SUPPLY_PROP_ONLINE:
  64. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_AC_ONLINE);
  65. break;
  66. default:
  67. ret = -EINVAL;
  68. break;
  69. }
  70. return ret;
  71. }
  72. static int goldfish_battery_get_property(struct power_supply *psy,
  73. enum power_supply_property psp,
  74. union power_supply_propval *val)
  75. {
  76. struct goldfish_battery_data *data = power_supply_get_drvdata(psy);
  77. int ret = 0;
  78. switch (psp) {
  79. case POWER_SUPPLY_PROP_STATUS:
  80. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_STATUS);
  81. break;
  82. case POWER_SUPPLY_PROP_HEALTH:
  83. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_HEALTH);
  84. break;
  85. case POWER_SUPPLY_PROP_PRESENT:
  86. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_PRESENT);
  87. break;
  88. case POWER_SUPPLY_PROP_TECHNOLOGY:
  89. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  90. break;
  91. case POWER_SUPPLY_PROP_CAPACITY:
  92. val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_CAPACITY);
  93. break;
  94. default:
  95. ret = -EINVAL;
  96. break;
  97. }
  98. return ret;
  99. }
  100. static enum power_supply_property goldfish_battery_props[] = {
  101. POWER_SUPPLY_PROP_STATUS,
  102. POWER_SUPPLY_PROP_HEALTH,
  103. POWER_SUPPLY_PROP_PRESENT,
  104. POWER_SUPPLY_PROP_TECHNOLOGY,
  105. POWER_SUPPLY_PROP_CAPACITY,
  106. };
  107. static enum power_supply_property goldfish_ac_props[] = {
  108. POWER_SUPPLY_PROP_ONLINE,
  109. };
  110. static irqreturn_t goldfish_battery_interrupt(int irq, void *dev_id)
  111. {
  112. unsigned long irq_flags;
  113. struct goldfish_battery_data *data = dev_id;
  114. uint32_t status;
  115. spin_lock_irqsave(&data->lock, irq_flags);
  116. /* read status flags, which will clear the interrupt */
  117. status = GOLDFISH_BATTERY_READ(data, BATTERY_INT_STATUS);
  118. status &= BATTERY_INT_MASK;
  119. if (status & BATTERY_STATUS_CHANGED)
  120. power_supply_changed(data->battery);
  121. if (status & AC_STATUS_CHANGED)
  122. power_supply_changed(data->ac);
  123. spin_unlock_irqrestore(&data->lock, irq_flags);
  124. return status ? IRQ_HANDLED : IRQ_NONE;
  125. }
  126. static const struct power_supply_desc battery_desc = {
  127. .properties = goldfish_battery_props,
  128. .num_properties = ARRAY_SIZE(goldfish_battery_props),
  129. .get_property = goldfish_battery_get_property,
  130. .name = "battery",
  131. .type = POWER_SUPPLY_TYPE_BATTERY,
  132. };
  133. static const struct power_supply_desc ac_desc = {
  134. .properties = goldfish_ac_props,
  135. .num_properties = ARRAY_SIZE(goldfish_ac_props),
  136. .get_property = goldfish_ac_get_property,
  137. .name = "ac",
  138. .type = POWER_SUPPLY_TYPE_MAINS,
  139. };
  140. static int goldfish_battery_probe(struct platform_device *pdev)
  141. {
  142. int ret;
  143. struct resource *r;
  144. struct goldfish_battery_data *data;
  145. struct power_supply_config psy_cfg = {};
  146. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  147. if (data == NULL)
  148. return -ENOMEM;
  149. spin_lock_init(&data->lock);
  150. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  151. if (r == NULL) {
  152. dev_err(&pdev->dev, "platform_get_resource failed\n");
  153. return -ENODEV;
  154. }
  155. data->reg_base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  156. if (data->reg_base == NULL) {
  157. dev_err(&pdev->dev, "unable to remap MMIO\n");
  158. return -ENOMEM;
  159. }
  160. data->irq = platform_get_irq(pdev, 0);
  161. if (data->irq < 0) {
  162. dev_err(&pdev->dev, "platform_get_irq failed\n");
  163. return -ENODEV;
  164. }
  165. ret = devm_request_irq(&pdev->dev, data->irq, goldfish_battery_interrupt,
  166. IRQF_SHARED, pdev->name, data);
  167. if (ret)
  168. return ret;
  169. psy_cfg.drv_data = data;
  170. data->ac = power_supply_register(&pdev->dev, &ac_desc, &psy_cfg);
  171. if (IS_ERR(data->ac))
  172. return PTR_ERR(data->ac);
  173. data->battery = power_supply_register(&pdev->dev, &battery_desc,
  174. &psy_cfg);
  175. if (IS_ERR(data->battery)) {
  176. power_supply_unregister(data->ac);
  177. return PTR_ERR(data->battery);
  178. }
  179. platform_set_drvdata(pdev, data);
  180. battery_data = data;
  181. GOLDFISH_BATTERY_WRITE(data, BATTERY_INT_ENABLE, BATTERY_INT_MASK);
  182. return 0;
  183. }
  184. static int goldfish_battery_remove(struct platform_device *pdev)
  185. {
  186. struct goldfish_battery_data *data = platform_get_drvdata(pdev);
  187. power_supply_unregister(data->battery);
  188. power_supply_unregister(data->ac);
  189. battery_data = NULL;
  190. return 0;
  191. }
  192. static struct platform_driver goldfish_battery_device = {
  193. .probe = goldfish_battery_probe,
  194. .remove = goldfish_battery_remove,
  195. .driver = {
  196. .name = "goldfish-battery"
  197. }
  198. };
  199. module_platform_driver(goldfish_battery_device);
  200. MODULE_AUTHOR("Mike Lockwood lockwood@android.com");
  201. MODULE_LICENSE("GPL");
  202. MODULE_DESCRIPTION("Battery driver for the Goldfish emulator");