hp6xx_apm.c 2.8 KB

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