blob: 76b6776235df7b587338c3bae2b17d4a003d3758 [file] [log] [blame]
Andriy Skulysh3aa770e2006-09-27 16:20:22 +09001/*
2 * bios-less APM driver for hp680
3 *
4 * Copyright 2005 (c) Andriy Skulysh <askulysh@gmail.com>
Kristoffer Ericson8b03c042008-03-04 23:09:25 -08005 * Copyright 2008 (c) Kristoffer Ericson <kristoffer.ericson@gmail.com>
Andriy Skulysh3aa770e2006-09-27 16:20:22 +09006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License.
9 */
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090010#include <linux/module.h>
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090011#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
Paul Mundt0a9b0db2007-01-24 21:56:20 +090014#include <linux/apm-emulation.h>
15#include <linux/io.h>
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090016#include <asm/adc.h>
Paul Mundt082c44d2006-10-19 16:16:18 +090017#include <asm/hp6xx.h>
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090018
Kristoffer Ericson8b03c042008-03-04 23:09:25 -080019/* percentage values */
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090020#define APM_CRITICAL 10
21#define APM_LOW 30
22
Kristoffer Ericson8b03c042008-03-04 23:09:25 -080023/* resonably sane values */
Kristoffer Ericson8c8ee822007-09-11 12:43:33 +090024#define HP680_BATTERY_MAX 898
25#define HP680_BATTERY_MIN 486
26#define HP680_BATTERY_AC_ON 1023
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090027
28#define MODNAME "hp6x0_apm"
29
Paul Mundt0a9b0db2007-01-24 21:56:20 +090030static void hp6x0_apm_get_power_status(struct apm_power_info *info)
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090031{
Paul Mundt0a9b0db2007-01-24 21:56:20 +090032 int battery, backup, charging, percentage;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090033 u8 pgdr;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090034
Paul Mundt0a9b0db2007-01-24 21:56:20 +090035 battery = adc_single(ADC_CHANNEL_BATTERY);
36 backup = adc_single(ADC_CHANNEL_BACKUP);
37 charging = adc_single(ADC_CHANNEL_CHARGE);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090038
39 percentage = 100 * (battery - HP680_BATTERY_MIN) /
40 (HP680_BATTERY_MAX - HP680_BATTERY_MIN);
41
Kristoffer Ericson8b03c042008-03-04 23:09:25 -080042 /* % of full battery */
43 info->battery_life = percentage;
44
45 /* We want our estimates in minutes */
46 info->units = 0;
47
48 /* Extremely(!!) rough estimate, we will replace this with a datalist later on */
49 info->time = (2 * battery);
50
Paul Mundt0a9b0db2007-01-24 21:56:20 +090051 info->ac_line_status = (battery > HP680_BATTERY_AC_ON) ?
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090052 APM_AC_ONLINE : APM_AC_OFFLINE;
53
Kristoffer Ericson8b03c042008-03-04 23:09:25 -080054 pgdr = ctrl_inb(PGDR);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090055 if (pgdr & PGDR_MAIN_BATTERY_OUT) {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090056 info->battery_status = APM_BATTERY_STATUS_NOT_PRESENT;
57 info->battery_flag = 0x80;
58 } else if (charging < 8) {
59 info->battery_status = APM_BATTERY_STATUS_CHARGING;
60 info->battery_flag = 0x08;
Kristoffer Ericson8b03c042008-03-04 23:09:25 -080061 info->ac_line_status = 0x01;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090062 } else if (percentage <= APM_CRITICAL) {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090063 info->battery_status = APM_BATTERY_STATUS_CRITICAL;
64 info->battery_flag = 0x04;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090065 } else if (percentage <= APM_LOW) {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090066 info->battery_status = APM_BATTERY_STATUS_LOW;
67 info->battery_flag = 0x02;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090068 } else {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090069 info->battery_status = APM_BATTERY_STATUS_HIGH;
70 info->battery_flag = 0x01;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090071 }
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090072}
73
Paul Mundt35f3c512006-10-06 15:31:16 +090074static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev)
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090075{
Kristoffer Ericson8c8ee822007-09-11 12:43:33 +090076 if (!APM_DISABLED)
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090077 apm_queue_event(APM_USER_SUSPEND);
78
79 return IRQ_HANDLED;
80}
81
82static int __init hp6x0_apm_init(void)
83{
84 int ret;
85
86 ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt,
Paul Mundt0a9b0db2007-01-24 21:56:20 +090087 IRQF_DISABLED, MODNAME, NULL);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090088 if (unlikely(ret < 0)) {
89 printk(KERN_ERR MODNAME ": IRQ %d request failed\n",
90 HP680_BTN_IRQ);
91 return ret;
92 }
93
Paul Mundt0a9b0db2007-01-24 21:56:20 +090094 apm_get_power_status = hp6x0_apm_get_power_status;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090095
96 return ret;
97}
98
99static void __exit hp6x0_apm_exit(void)
100{
101 free_irq(HP680_BTN_IRQ, 0);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +0900102}
103
104module_init(hp6x0_apm_init);
105module_exit(hp6x0_apm_exit);
106
107MODULE_AUTHOR("Adriy Skulysh");
108MODULE_DESCRIPTION("hp6xx Advanced Power Management");
109MODULE_LICENSE("GPL");