Path: blob/master/arch/sh/boards/mach-hp6xx/hp6xx_apm.c
26481 views
// SPDX-License-Identifier: GPL-2.01/*2* bios-less APM driver for hp6803*4* Copyright 2005 (c) Andriy Skulysh <[email protected]>5* Copyright 2008 (c) Kristoffer Ericson <[email protected]>6*/7#include <linux/module.h>8#include <linux/kernel.h>9#include <linux/init.h>10#include <linux/interrupt.h>11#include <linux/apm-emulation.h>12#include <linux/io.h>13#include <asm/adc.h>14#include <mach/hp6xx.h>1516/* percentage values */17#define APM_CRITICAL 1018#define APM_LOW 301920/* resonably sane values */21#define HP680_BATTERY_MAX 89822#define HP680_BATTERY_MIN 48623#define HP680_BATTERY_AC_ON 10232425#define MODNAME "hp6x0_apm"2627#define PGDR 0xa400012c2829static void hp6x0_apm_get_power_status(struct apm_power_info *info)30{31int battery, backup, charging, percentage;32u8 pgdr;3334battery = adc_single(ADC_CHANNEL_BATTERY);35backup = adc_single(ADC_CHANNEL_BACKUP);36charging = adc_single(ADC_CHANNEL_CHARGE);3738percentage = 100 * (battery - HP680_BATTERY_MIN) /39(HP680_BATTERY_MAX - HP680_BATTERY_MIN);4041/* % of full battery */42info->battery_life = percentage;4344/* We want our estimates in minutes */45info->units = 0;4647/* Extremely(!!) rough estimate, we will replace this with a datalist later on */48info->time = (2 * battery);4950info->ac_line_status = (battery > HP680_BATTERY_AC_ON) ?51APM_AC_ONLINE : APM_AC_OFFLINE;5253pgdr = __raw_readb(PGDR);54if (pgdr & PGDR_MAIN_BATTERY_OUT) {55info->battery_status = APM_BATTERY_STATUS_NOT_PRESENT;56info->battery_flag = 0x80;57} else if (charging < 8) {58info->battery_status = APM_BATTERY_STATUS_CHARGING;59info->battery_flag = 0x08;60info->ac_line_status = 0x01;61} else if (percentage <= APM_CRITICAL) {62info->battery_status = APM_BATTERY_STATUS_CRITICAL;63info->battery_flag = 0x04;64} else if (percentage <= APM_LOW) {65info->battery_status = APM_BATTERY_STATUS_LOW;66info->battery_flag = 0x02;67} else {68info->battery_status = APM_BATTERY_STATUS_HIGH;69info->battery_flag = 0x01;70}71}7273static irqreturn_t hp6x0_apm_interrupt(int irq, void *dev)74{75if (!APM_DISABLED)76apm_queue_event(APM_USER_SUSPEND);7778return IRQ_HANDLED;79}8081static int __init hp6x0_apm_init(void)82{83int ret;8485ret = request_irq(HP680_BTN_IRQ, hp6x0_apm_interrupt,860, MODNAME, NULL);87if (unlikely(ret < 0)) {88printk(KERN_ERR MODNAME ": IRQ %d request failed\n",89HP680_BTN_IRQ);90return ret;91}9293apm_get_power_status = hp6x0_apm_get_power_status;9495return ret;96}9798static void __exit hp6x0_apm_exit(void)99{100free_irq(HP680_BTN_IRQ, 0);101}102103module_init(hp6x0_apm_init);104module_exit(hp6x0_apm_exit);105106MODULE_AUTHOR("Adriy Skulysh");107MODULE_DESCRIPTION("hp6xx Advanced Power Management");108MODULE_LICENSE("GPL");109110111