hp6xx_apm.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/apm-emulation.h>
  14. #include <linux/io.h>
  15. #include <asm/adc.h>
  16. #include <asm/hp6xx.h>
  17. #define SH7709_PGDR 0xa400012c
  18. #define APM_CRITICAL 10
  19. #define APM_LOW 30
  20. #define HP680_BATTERY_MAX 898
  21. #define HP680_BATTERY_MIN 486
  22. #define HP680_BATTERY_AC_ON 1023
  23. #define MODNAME "hp6x0_apm"
  24. static void hp6x0_apm_get_power_status(struct apm_power_info *info)
  25. {
  26. int battery, backup, charging, percentage;
  27. u8 pgdr;
  28. battery = adc_single(ADC_CHANNEL_BATTERY);
  29. backup = adc_single(ADC_CHANNEL_BACKUP);
  30. charging = adc_single(ADC_CHANNEL_CHARGE);
  31. percentage = 100 * (battery - HP680_BATTERY_MIN) /
  32. (HP680_BATTERY_MAX - HP680_BATTERY_MIN);
  33. info->ac_line_status = (battery > HP680_BATTERY_AC_ON) ?
  34. APM_AC_ONLINE : APM_AC_OFFLINE;
  35. pgdr = ctrl_inb(SH7709_PGDR);
  36. if (pgdr & PGDR_MAIN_BATTERY_OUT) {
  37. info->battery_status = APM_BATTERY_STATUS_NOT_PRESENT;
  38. info->battery_flag = 0x80;
  39. } else if (charging < 8) {
  40. info->battery_status = APM_BATTERY_STATUS_CHARGING;
  41. info->battery_flag = 0x08;
  42. info->ac_line_status = 0xff;
  43. } else if (percentage <= APM_CRITICAL) {
  44. info->battery_status = APM_BATTERY_STATUS_CRITICAL;
  45. info->battery_flag = 0x04;
  46. } else if (percentage <= APM_LOW) {
  47. info->battery_status = APM_BATTERY_STATUS_LOW;
  48. info->battery_flag = 0x02;
  49. } else {
  50. info->battery_status = APM_BATTERY_STATUS_HIGH;
  51. info->battery_flag = 0x01;
  52. }
  53. info->units = 0;
  54. }
  55. static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev)
  56. {
  57. if (!APM_DISABLED)
  58. apm_queue_event(APM_USER_SUSPEND);
  59. return IRQ_HANDLED;
  60. }
  61. static int __init hp6x0_apm_init(void)
  62. {
  63. int ret;
  64. ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt,
  65. IRQF_DISABLED, MODNAME, NULL);
  66. if (unlikely(ret < 0)) {
  67. printk(KERN_ERR MODNAME ": IRQ %d request failed\n",
  68. HP680_BTN_IRQ);
  69. return ret;
  70. }
  71. apm_get_power_status = hp6x0_apm_get_power_status;
  72. return ret;
  73. }
  74. static void __exit hp6x0_apm_exit(void)
  75. {
  76. free_irq(HP680_BTN_IRQ, 0);
  77. }
  78. module_init(hp6x0_apm_init);
  79. module_exit(hp6x0_apm_exit);
  80. MODULE_AUTHOR("Adriy Skulysh");
  81. MODULE_DESCRIPTION("hp6xx Advanced Power Management");
  82. MODULE_LICENSE("GPL");