Andriy Skulysh | 3aa770e | 2006-09-27 16:20:22 +0900 | [diff] [blame] | 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 | */ |
Andriy Skulysh | 3aa770e | 2006-09-27 16:20:22 +0900 | [diff] [blame] | 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/hp6xx.h> |
| 18 | |
| 19 | #define SH7709_PGDR 0xa400012c |
| 20 | |
| 21 | #define APM_CRITICAL 10 |
| 22 | #define APM_LOW 30 |
| 23 | |
| 24 | #define HP680_BATTERY_MAX 875 |
| 25 | #define HP680_BATTERY_MIN 600 |
| 26 | #define HP680_BATTERY_AC_ON 900 |
| 27 | |
| 28 | #define MODNAME "hp6x0_apm" |
| 29 | |
| 30 | static int hp6x0_apm_get_info(char *buf, char **start, off_t fpos, int length) |
| 31 | { |
| 32 | u8 pgdr; |
| 33 | char *p; |
| 34 | int battery_status; |
| 35 | int battery_flag; |
| 36 | int ac_line_status; |
| 37 | int time_units = APM_BATTERY_LIFE_UNKNOWN; |
| 38 | |
| 39 | int battery = adc_single(ADC_CHANNEL_BATTERY); |
| 40 | int backup = adc_single(ADC_CHANNEL_BACKUP); |
| 41 | int charging = adc_single(ADC_CHANNEL_CHARGE); |
| 42 | int percentage; |
| 43 | |
| 44 | percentage = 100 * (battery - HP680_BATTERY_MIN) / |
| 45 | (HP680_BATTERY_MAX - HP680_BATTERY_MIN); |
| 46 | |
| 47 | ac_line_status = (battery > HP680_BATTERY_AC_ON) ? |
| 48 | APM_AC_ONLINE : APM_AC_OFFLINE; |
| 49 | |
| 50 | p = buf; |
| 51 | |
| 52 | pgdr = ctrl_inb(SH7709_PGDR); |
| 53 | if (pgdr & PGDR_MAIN_BATTERY_OUT) { |
| 54 | battery_status = APM_BATTERY_STATUS_NOT_PRESENT; |
| 55 | battery_flag = 0x80; |
| 56 | percentage = -1; |
| 57 | } else if (charging < 8 ) { |
| 58 | battery_status = APM_BATTERY_STATUS_CHARGING; |
| 59 | battery_flag = 0x08; |
| 60 | ac_line_status = 0xff; |
| 61 | } else if (percentage <= APM_CRITICAL) { |
| 62 | battery_status = APM_BATTERY_STATUS_CRITICAL; |
| 63 | battery_flag = 0x04; |
| 64 | } else if (percentage <= APM_LOW) { |
| 65 | battery_status = APM_BATTERY_STATUS_LOW; |
| 66 | battery_flag = 0x02; |
| 67 | } else { |
| 68 | battery_status = APM_BATTERY_STATUS_HIGH; |
| 69 | battery_flag = 0x01; |
| 70 | } |
| 71 | |
| 72 | p += sprintf(p, "1.0 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n", |
| 73 | APM_32_BIT_SUPPORT, |
| 74 | ac_line_status, |
| 75 | battery_status, |
| 76 | battery_flag, |
| 77 | percentage, |
| 78 | time_units, |
| 79 | "min"); |
| 80 | p += sprintf(p, "bat=%d backup=%d charge=%d\n", |
| 81 | battery, backup, charging); |
| 82 | |
| 83 | return p - buf; |
| 84 | } |
| 85 | |
Paul Mundt | 35f3c51 | 2006-10-06 15:31:16 +0900 | [diff] [blame^] | 86 | static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev) |
Andriy Skulysh | 3aa770e | 2006-09-27 16:20:22 +0900 | [diff] [blame] | 87 | { |
| 88 | if (!apm_suspended) |
| 89 | apm_queue_event(APM_USER_SUSPEND); |
| 90 | |
| 91 | return IRQ_HANDLED; |
| 92 | } |
| 93 | |
| 94 | static int __init hp6x0_apm_init(void) |
| 95 | { |
| 96 | int ret; |
| 97 | |
| 98 | ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt, |
Paul Mundt | 35f3c51 | 2006-10-06 15:31:16 +0900 | [diff] [blame^] | 99 | IRQF_DISABLED, MODNAME, 0); |
Andriy Skulysh | 3aa770e | 2006-09-27 16:20:22 +0900 | [diff] [blame] | 100 | if (unlikely(ret < 0)) { |
| 101 | printk(KERN_ERR MODNAME ": IRQ %d request failed\n", |
| 102 | HP680_BTN_IRQ); |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | apm_get_info = hp6x0_apm_get_info; |
| 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | static void __exit hp6x0_apm_exit(void) |
| 112 | { |
| 113 | free_irq(HP680_BTN_IRQ, 0); |
| 114 | apm_get_info = 0; |
| 115 | } |
| 116 | |
| 117 | module_init(hp6x0_apm_init); |
| 118 | module_exit(hp6x0_apm_exit); |
| 119 | |
| 120 | MODULE_AUTHOR("Adriy Skulysh"); |
| 121 | MODULE_DESCRIPTION("hp6xx Advanced Power Management"); |
| 122 | MODULE_LICENSE("GPL"); |