blob: 640ca2a74f163c07ef42ac570e11cb254da76e0f [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>
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 Skulysh3aa770e2006-09-27 16:20:22 +09009#include <linux/module.h>
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090010#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/interrupt.h>
Paul Mundt0a9b0db2007-01-24 21:56:20 +090013#include <linux/apm-emulation.h>
14#include <linux/io.h>
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090015#include <asm/adc.h>
Paul Mundt082c44d2006-10-19 16:16:18 +090016#include <asm/hp6xx.h>
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090017
18#define SH7709_PGDR 0xa400012c
19
20#define APM_CRITICAL 10
21#define APM_LOW 30
22
Kristoffer Ericson8c8ee822007-09-11 12:43:33 +090023#define HP680_BATTERY_MAX 898
24#define HP680_BATTERY_MIN 486
25#define HP680_BATTERY_AC_ON 1023
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090026
27#define MODNAME "hp6x0_apm"
28
Paul Mundt0a9b0db2007-01-24 21:56:20 +090029static void hp6x0_apm_get_power_status(struct apm_power_info *info)
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090030{
Paul Mundt0a9b0db2007-01-24 21:56:20 +090031 int battery, backup, charging, percentage;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090032 u8 pgdr;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090033
Paul Mundt0a9b0db2007-01-24 21:56:20 +090034 battery = adc_single(ADC_CHANNEL_BATTERY);
35 backup = adc_single(ADC_CHANNEL_BACKUP);
36 charging = adc_single(ADC_CHANNEL_CHARGE);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090037
38 percentage = 100 * (battery - HP680_BATTERY_MIN) /
39 (HP680_BATTERY_MAX - HP680_BATTERY_MIN);
40
Paul Mundt0a9b0db2007-01-24 21:56:20 +090041 info->ac_line_status = (battery > HP680_BATTERY_AC_ON) ?
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090042 APM_AC_ONLINE : APM_AC_OFFLINE;
43
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090044 pgdr = ctrl_inb(SH7709_PGDR);
45 if (pgdr & PGDR_MAIN_BATTERY_OUT) {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090046 info->battery_status = APM_BATTERY_STATUS_NOT_PRESENT;
47 info->battery_flag = 0x80;
48 } else if (charging < 8) {
49 info->battery_status = APM_BATTERY_STATUS_CHARGING;
50 info->battery_flag = 0x08;
51 info->ac_line_status = 0xff;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090052 } else if (percentage <= APM_CRITICAL) {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090053 info->battery_status = APM_BATTERY_STATUS_CRITICAL;
54 info->battery_flag = 0x04;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090055 } else if (percentage <= APM_LOW) {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090056 info->battery_status = APM_BATTERY_STATUS_LOW;
57 info->battery_flag = 0x02;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090058 } else {
Paul Mundt0a9b0db2007-01-24 21:56:20 +090059 info->battery_status = APM_BATTERY_STATUS_HIGH;
60 info->battery_flag = 0x01;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090061 }
62
Paul Mundt0a9b0db2007-01-24 21:56:20 +090063 info->units = 0;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090064}
65
Paul Mundt35f3c512006-10-06 15:31:16 +090066static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev)
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090067{
Kristoffer Ericson8c8ee822007-09-11 12:43:33 +090068 if (!APM_DISABLED)
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090069 apm_queue_event(APM_USER_SUSPEND);
70
71 return IRQ_HANDLED;
72}
73
74static int __init hp6x0_apm_init(void)
75{
76 int ret;
77
78 ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt,
Paul Mundt0a9b0db2007-01-24 21:56:20 +090079 IRQF_DISABLED, MODNAME, NULL);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090080 if (unlikely(ret < 0)) {
81 printk(KERN_ERR MODNAME ": IRQ %d request failed\n",
82 HP680_BTN_IRQ);
83 return ret;
84 }
85
Paul Mundt0a9b0db2007-01-24 21:56:20 +090086 apm_get_power_status = hp6x0_apm_get_power_status;
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090087
88 return ret;
89}
90
91static void __exit hp6x0_apm_exit(void)
92{
93 free_irq(HP680_BTN_IRQ, 0);
Andriy Skulysh3aa770e2006-09-27 16:20:22 +090094}
95
96module_init(hp6x0_apm_init);
97module_exit(hp6x0_apm_exit);
98
99MODULE_AUTHOR("Adriy Skulysh");
100MODULE_DESCRIPTION("hp6xx Advanced Power Management");
101MODULE_LICENSE("GPL");