blob: 865d8d6e823f89036402139b896f4465c12e85f9 [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 Mundt7639a452008-10-20 13:02:48 +090017#include <mach/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 Mundtdd4f99b2008-03-06 13:48:08 +090030#define PGDR 0xa400012c
31
Paul Mundt0a9b0db2007-01-24 21:56:20 +090032static void hp6x0_apm_get_power_status(struct apm_power_info *info)
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090033{
Paul Mundt0a9b0db2007-01-24 21:56:20 +090034 int battery, backup, charging, percentage;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090035 u8 pgdr;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090036
Paul Mundt0a9b0db2007-01-24 21:56:20 +090037 battery = adc_single(ADC_CHANNEL_BATTERY);
38 backup = adc_single(ADC_CHANNEL_BACKUP);
39 charging = adc_single(ADC_CHANNEL_CHARGE);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090040
41 percentage = 100 * (battery - HP680_BATTERY_MIN) /
42 (HP680_BATTERY_MAX - HP680_BATTERY_MIN);
43
Kristoffer Ericson8b03c042008-03-04 23:09:25 -080044 /* % of full battery */
45 info->battery_life = percentage;
46
47 /* We want our estimates in minutes */
48 info->units = 0;
49
50 /* Extremely(!!) rough estimate, we will replace this with a datalist later on */
51 info->time = (2 * battery);
52
Paul Mundt0a9b0db2007-01-24 21:56:20 +090053 info->ac_line_status = (battery > HP680_BATTERY_AC_ON) ?
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090054 APM_AC_ONLINE : APM_AC_OFFLINE;
55
Paul Mundt9d56dd32010-01-26 12:58:40 +090056 pgdr = __raw_readb(PGDR);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090057 if (pgdr & PGDR_MAIN_BATTERY_OUT) {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090058 info->battery_status = APM_BATTERY_STATUS_NOT_PRESENT;
59 info->battery_flag = 0x80;
60 } else if (charging < 8) {
61 info->battery_status = APM_BATTERY_STATUS_CHARGING;
62 info->battery_flag = 0x08;
Kristoffer Ericson8b03c042008-03-04 23:09:25 -080063 info->ac_line_status = 0x01;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090064 } else if (percentage <= APM_CRITICAL) {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090065 info->battery_status = APM_BATTERY_STATUS_CRITICAL;
66 info->battery_flag = 0x04;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090067 } else if (percentage <= APM_LOW) {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090068 info->battery_status = APM_BATTERY_STATUS_LOW;
69 info->battery_flag = 0x02;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090070 } else {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090071 info->battery_status = APM_BATTERY_STATUS_HIGH;
72 info->battery_flag = 0x01;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090073 }
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090074}
75
Paul Mundt35f3c512006-10-06 15:31:16 +090076static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev)
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090077{
Kristoffer Ericson8c8ee822007-09-11 12:43:33 +090078 if (!APM_DISABLED)
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090079 apm_queue_event(APM_USER_SUSPEND);
80
81 return IRQ_HANDLED;
82}
83
84static int __init hp6x0_apm_init(void)
85{
86 int ret;
87
88 ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt,
Yong Zhangd11584a2011-10-22 17:56:28 +080089 0, MODNAME, NULL);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090090 if (unlikely(ret < 0)) {
91 printk(KERN_ERR MODNAME ": IRQ %d request failed\n",
92 HP680_BTN_IRQ);
93 return ret;
94 }
95
Paul Mundt0a9b0db2007-01-24 21:56:20 +090096 apm_get_power_status = hp6x0_apm_get_power_status;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090097
98 return ret;
99}
100
101static void __exit hp6x0_apm_exit(void)
102{
103 free_irq(HP680_BTN_IRQ, 0);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +0900104}
105
106module_init(hp6x0_apm_init);
107module_exit(hp6x0_apm_exit);
108
109MODULE_AUTHOR("Adriy Skulysh");
110MODULE_DESCRIPTION("hp6xx Advanced Power Management");
111MODULE_LICENSE("GPL");