hp6xx_apm.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * bios-less APM driver for hp680
  3. *
  4. * Copyright 2005 (c) Andriy Skulysh <askulysh@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/apm_bios.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <asm/io.h>
  15. #include <asm/apm.h>
  16. #include <asm/adc.h>
  17. #include <asm/hp6xx.h>
  18. #define SH7709_PGDR 0xa400012c
  19. #define APM_CRITICAL 10
  20. #define APM_LOW 30
  21. #define HP680_BATTERY_MAX 875
  22. #define HP680_BATTERY_MIN 600
  23. #define HP680_BATTERY_AC_ON 900
  24. #define MODNAME "hp6x0_apm"
  25. static int hp6x0_apm_get_info(char *buf, char **start, off_t fpos, int length)
  26. {
  27. u8 pgdr;
  28. char *p;
  29. int battery_status;
  30. int battery_flag;
  31. int ac_line_status;
  32. int time_units = APM_BATTERY_LIFE_UNKNOWN;
  33. int battery = adc_single(ADC_CHANNEL_BATTERY);
  34. int backup = adc_single(ADC_CHANNEL_BACKUP);
  35. int charging = adc_single(ADC_CHANNEL_CHARGE);
  36. int percentage;
  37. percentage = 100 * (battery - HP680_BATTERY_MIN) /
  38. (HP680_BATTERY_MAX - HP680_BATTERY_MIN);
  39. ac_line_status = (battery > HP680_BATTERY_AC_ON) ?
  40. APM_AC_ONLINE : APM_AC_OFFLINE;
  41. p = buf;
  42. pgdr = ctrl_inb(SH7709_PGDR);
  43. if (pgdr & PGDR_MAIN_BATTERY_OUT) {
  44. battery_status = APM_BATTERY_STATUS_NOT_PRESENT;
  45. battery_flag = 0x80;
  46. percentage = -1;
  47. } else if (charging < 8 ) {
  48. battery_status = APM_BATTERY_STATUS_CHARGING;
  49. battery_flag = 0x08;
  50. ac_line_status = 0xff;
  51. } else if (percentage <= APM_CRITICAL) {
  52. battery_status = APM_BATTERY_STATUS_CRITICAL;
  53. battery_flag = 0x04;
  54. } else if (percentage <= APM_LOW) {
  55. battery_status = APM_BATTERY_STATUS_LOW;
  56. battery_flag = 0x02;
  57. } else {
  58. battery_status = APM_BATTERY_STATUS_HIGH;
  59. battery_flag = 0x01;
  60. }
  61. p += sprintf(p, "1.0 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
  62. APM_32_BIT_SUPPORT,
  63. ac_line_status,
  64. battery_status,
  65. battery_flag,
  66. percentage,
  67. time_units,
  68. "min");
  69. p += sprintf(p, "bat=%d backup=%d charge=%d\n",
  70. battery, backup, charging);
  71. return p - buf;
  72. }
  73. static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev)
  74. {
  75. if (!apm_suspended)
  76. apm_queue_event(APM_USER_SUSPEND);
  77. return IRQ_HANDLED;
  78. }
  79. static int __init hp6x0_apm_init(void)
  80. {
  81. int ret;
  82. ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt,
  83. IRQF_DISABLED, MODNAME, 0);
  84. if (unlikely(ret < 0)) {
  85. printk(KERN_ERR MODNAME ": IRQ %d request failed\n",
  86. HP680_BTN_IRQ);
  87. return ret;
  88. }
  89. apm_get_info = hp6x0_apm_get_info;
  90. return ret;
  91. }
  92. static void __exit hp6x0_apm_exit(void)
  93. {
  94. free_irq(HP680_BTN_IRQ, 0);
  95. apm_get_info = 0;
  96. }
  97. module_init(hp6x0_apm_init);
  98. module_exit(hp6x0_apm_exit);
  99. MODULE_AUTHOR("Adriy Skulysh");
  100. MODULE_DESCRIPTION("hp6xx Advanced Power Management");
  101. MODULE_LICENSE("GPL");