Path: blob/master/arch/sh/boards/mach-hp6xx/hp6xx_apm.c
15126 views
/*1* bios-less APM driver for hp6802*3* Copyright 2005 (c) Andriy Skulysh <[email protected]>4* Copyright 2008 (c) Kristoffer Ericson <[email protected]>5*6* This program is free software; you can redistribute it and/or7* 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 <mach/hp6xx.h>1718/* percentage values */19#define APM_CRITICAL 1020#define APM_LOW 302122/* resonably sane values */23#define HP680_BATTERY_MAX 89824#define HP680_BATTERY_MIN 48625#define HP680_BATTERY_AC_ON 10232627#define MODNAME "hp6x0_apm"2829#define PGDR 0xa400012c3031static void hp6x0_apm_get_power_status(struct apm_power_info *info)32{33int battery, backup, charging, percentage;34u8 pgdr;3536battery = adc_single(ADC_CHANNEL_BATTERY);37backup = adc_single(ADC_CHANNEL_BACKUP);38charging = adc_single(ADC_CHANNEL_CHARGE);3940percentage = 100 * (battery - HP680_BATTERY_MIN) /41(HP680_BATTERY_MAX - HP680_BATTERY_MIN);4243/* % of full battery */44info->battery_life = percentage;4546/* We want our estimates in minutes */47info->units = 0;4849/* Extremely(!!) rough estimate, we will replace this with a datalist later on */50info->time = (2 * battery);5152info->ac_line_status = (battery > HP680_BATTERY_AC_ON) ?53APM_AC_ONLINE : APM_AC_OFFLINE;5455pgdr = __raw_readb(PGDR);56if (pgdr & PGDR_MAIN_BATTERY_OUT) {57info->battery_status = APM_BATTERY_STATUS_NOT_PRESENT;58info->battery_flag = 0x80;59} else if (charging < 8) {60info->battery_status = APM_BATTERY_STATUS_CHARGING;61info->battery_flag = 0x08;62info->ac_line_status = 0x01;63} else if (percentage <= APM_CRITICAL) {64info->battery_status = APM_BATTERY_STATUS_CRITICAL;65info->battery_flag = 0x04;66} else if (percentage <= APM_LOW) {67info->battery_status = APM_BATTERY_STATUS_LOW;68info->battery_flag = 0x02;69} else {70info->battery_status = APM_BATTERY_STATUS_HIGH;71info->battery_flag = 0x01;72}73}7475static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev)76{77if (!APM_DISABLED)78apm_queue_event(APM_USER_SUSPEND);7980return IRQ_HANDLED;81}8283static int __init hp6x0_apm_init(void)84{85int ret;8687ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt,88IRQF_DISABLED, MODNAME, NULL);89if (unlikely(ret < 0)) {90printk(KERN_ERR MODNAME ": IRQ %d request failed\n",91HP680_BTN_IRQ);92return ret;93}9495apm_get_power_status = hp6x0_apm_get_power_status;9697return ret;98}99100static void __exit hp6x0_apm_exit(void)101{102free_irq(HP680_BTN_IRQ, 0);103}104105module_init(hp6x0_apm_init);106module_exit(hp6x0_apm_exit);107108MODULE_AUTHOR("Adriy Skulysh");109MODULE_DESCRIPTION("hp6xx Advanced Power Management");110MODULE_LICENSE("GPL");111112113