palmtx_battery.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * linux/drivers/power/palmtx_battery.c
  3. *
  4. * Battery measurement code for Palm T|X Handheld computer
  5. *
  6. * based on tosa_battery.c
  7. *
  8. * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/wm97xx.h>
  19. #include <linux/delay.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/gpio.h>
  23. #include <asm/mach-types.h>
  24. #include <asm/arch/palmtx.h>
  25. static DEFINE_MUTEX(bat_lock);
  26. static struct work_struct bat_work;
  27. struct mutex work_lock;
  28. int bat_status = POWER_SUPPLY_STATUS_DISCHARGING;
  29. static unsigned long palmtx_read_bat(struct power_supply *bat_ps)
  30. {
  31. return wm97xx_read_aux_adc(bat_ps->dev->parent->driver_data,
  32. WM97XX_AUX_ID3) * 1000 / 414;
  33. }
  34. static unsigned long palmtx_read_temp(struct power_supply *bat_ps)
  35. {
  36. return wm97xx_read_aux_adc(bat_ps->dev->parent->driver_data,
  37. WM97XX_AUX_ID2);
  38. }
  39. static int palmtx_bat_get_property(struct power_supply *bat_ps,
  40. enum power_supply_property psp,
  41. union power_supply_propval *val)
  42. {
  43. switch (psp) {
  44. case POWER_SUPPLY_PROP_STATUS:
  45. val->intval = bat_status;
  46. break;
  47. case POWER_SUPPLY_PROP_TECHNOLOGY:
  48. val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
  49. break;
  50. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  51. val->intval = palmtx_read_bat(bat_ps);
  52. break;
  53. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  54. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  55. val->intval = PALMTX_BAT_MAX_VOLTAGE;
  56. break;
  57. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  58. val->intval = PALMTX_BAT_MIN_VOLTAGE;
  59. break;
  60. case POWER_SUPPLY_PROP_TEMP:
  61. val->intval = palmtx_read_temp(bat_ps);
  62. break;
  63. case POWER_SUPPLY_PROP_PRESENT:
  64. val->intval = 1;
  65. break;
  66. default:
  67. return -EINVAL;
  68. }
  69. return 0;
  70. }
  71. static void palmtx_bat_external_power_changed(struct power_supply *bat_ps)
  72. {
  73. schedule_work(&bat_work);
  74. }
  75. static char *status_text[] = {
  76. [POWER_SUPPLY_STATUS_UNKNOWN] = "Unknown",
  77. [POWER_SUPPLY_STATUS_CHARGING] = "Charging",
  78. [POWER_SUPPLY_STATUS_DISCHARGING] = "Discharging",
  79. };
  80. static void palmtx_bat_update(struct power_supply *bat_ps)
  81. {
  82. int old_status = bat_status;
  83. mutex_lock(&work_lock);
  84. bat_status = gpio_get_value(GPIO_NR_PALMTX_POWER_DETECT) ?
  85. POWER_SUPPLY_STATUS_CHARGING :
  86. POWER_SUPPLY_STATUS_DISCHARGING;
  87. if (old_status != bat_status) {
  88. pr_debug("%s %s -> %s\n", bat_ps->name,
  89. status_text[old_status],
  90. status_text[bat_status]);
  91. power_supply_changed(bat_ps);
  92. }
  93. mutex_unlock(&work_lock);
  94. }
  95. static enum power_supply_property palmtx_bat_main_props[] = {
  96. POWER_SUPPLY_PROP_STATUS,
  97. POWER_SUPPLY_PROP_TECHNOLOGY,
  98. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  99. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  100. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  101. POWER_SUPPLY_PROP_TEMP,
  102. POWER_SUPPLY_PROP_PRESENT,
  103. };
  104. struct power_supply bat_ps = {
  105. .name = "main-battery",
  106. .type = POWER_SUPPLY_TYPE_BATTERY,
  107. .properties = palmtx_bat_main_props,
  108. .num_properties = ARRAY_SIZE(palmtx_bat_main_props),
  109. .get_property = palmtx_bat_get_property,
  110. .external_power_changed = palmtx_bat_external_power_changed,
  111. .use_for_apm = 1,
  112. };
  113. static void palmtx_bat_work(struct work_struct *work)
  114. {
  115. palmtx_bat_update(&bat_ps);
  116. }
  117. #ifdef CONFIG_PM
  118. static int palmtx_bat_suspend(struct platform_device *dev, pm_message_t state)
  119. {
  120. flush_scheduled_work();
  121. return 0;
  122. }
  123. static int palmtx_bat_resume(struct platform_device *dev)
  124. {
  125. schedule_work(&bat_work);
  126. return 0;
  127. }
  128. #else
  129. #define palmtx_bat_suspend NULL
  130. #define palmtx_bat_resume NULL
  131. #endif
  132. static int __devinit palmtx_bat_probe(struct platform_device *dev)
  133. {
  134. int ret = 0;
  135. if (!machine_is_palmtx())
  136. return -ENODEV;
  137. mutex_init(&work_lock);
  138. INIT_WORK(&bat_work, palmtx_bat_work);
  139. ret = power_supply_register(&dev->dev, &bat_ps);
  140. if (!ret)
  141. schedule_work(&bat_work);
  142. return ret;
  143. }
  144. static int __devexit palmtx_bat_remove(struct platform_device *dev)
  145. {
  146. power_supply_unregister(&bat_ps);
  147. return 0;
  148. }
  149. static struct platform_driver palmtx_bat_driver = {
  150. .driver.name = "wm97xx-battery",
  151. .driver.owner = THIS_MODULE,
  152. .probe = palmtx_bat_probe,
  153. .remove = __devexit_p(palmtx_bat_remove),
  154. .suspend = palmtx_bat_suspend,
  155. .resume = palmtx_bat_resume,
  156. };
  157. static int __init palmtx_bat_init(void)
  158. {
  159. return platform_driver_register(&palmtx_bat_driver);
  160. }
  161. static void __exit palmtx_bat_exit(void)
  162. {
  163. platform_driver_unregister(&palmtx_bat_driver);
  164. }
  165. module_init(palmtx_bat_init);
  166. module_exit(palmtx_bat_exit);
  167. MODULE_LICENSE("GPL");
  168. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  169. MODULE_DESCRIPTION("Palm T|X battery driver");