Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Battery and Power Management code for the Sharp SL-C7xx and SL-Cxx00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * series of PDAs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2004-2005 Richard Purdie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Based on code written by Sharp for 2.4 kernels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #undef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/apm-emulation.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <asm/mach-types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "pm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <mach/pxa2xx-regs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include "regs-rtc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "sharpsl_pm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * Constants
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define SHARPSL_CHARGE_ON_TIME_INTERVAL        (msecs_to_jiffies(1*60*1000))  /* 1 min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define SHARPSL_CHARGE_FINISH_TIME             (msecs_to_jiffies(10*60*1000)) /* 10 min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define SHARPSL_BATCHK_TIME                    (msecs_to_jiffies(15*1000))    /* 15 sec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define SHARPSL_BATCHK_TIME_SUSPEND            (60*10)                        /* 10 min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define SHARPSL_WAIT_CO_TIME                   15  /* 15 sec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define SHARPSL_WAIT_DISCHARGE_ON              100 /* 100 msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP   10  /* 10 msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT   10  /* 10 msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN   10  /* 10 msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define SHARPSL_CHARGE_WAIT_TIME               15  /* 15 msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define SHARPSL_CHARGE_CO_CHECK_TIME           5   /* 5 msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define SHARPSL_CHARGE_RETRY_CNT               1   /* eqv. 10 min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * Prototypes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static int sharpsl_off_charge_battery(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static int sharpsl_check_battery_voltage(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int sharpsl_check_battery_temp(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static int sharpsl_ac_check(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static int sharpsl_average_value(int ad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static void sharpsl_average_clear(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static void sharpsl_charge_toggle(struct work_struct *private_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static void sharpsl_battery_thread(struct work_struct *private_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * Variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) struct sharpsl_pm_status sharpsl_pm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static DECLARE_DELAYED_WORK(toggle_charger, sharpsl_charge_toggle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static DECLARE_DELAYED_WORK(sharpsl_bat, sharpsl_battery_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) DEFINE_LED_TRIGGER(sharpsl_charge_led_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) struct battery_thresh sharpsl_battery_levels_acin[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	{ 213, 100},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	{ 212,  98},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	{ 211,  95},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	{ 210,  93},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	{ 209,  90},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	{ 208,  88},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	{ 207,  85},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	{ 206,  83},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	{ 205,  80},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	{ 204,  78},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	{ 203,  75},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	{ 202,  73},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	{ 201,  70},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	{ 200,  68},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	{ 199,  65},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	{ 198,  63},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	{ 197,  60},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	{ 196,  58},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	{ 195,  55},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	{ 194,  53},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	{ 193,  50},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	{ 192,  48},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	{ 192,  45},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	{ 191,  43},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	{ 191,  40},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	{ 190,  38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	{ 190,  35},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	{ 189,  33},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	{ 188,  30},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	{ 187,  28},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	{ 186,  25},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	{ 185,  23},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	{ 184,  20},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	{ 183,  18},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	{ 182,  15},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	{ 181,  13},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	{ 180,  10},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	{ 179,   8},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	{ 178,   5},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	{   0,   0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct battery_thresh sharpsl_battery_levels_noac[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	{ 213, 100},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	{ 212,  98},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	{ 211,  95},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	{ 210,  93},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	{ 209,  90},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	{ 208,  88},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	{ 207,  85},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	{ 206,  83},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	{ 205,  80},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	{ 204,  78},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	{ 203,  75},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	{ 202,  73},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	{ 201,  70},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	{ 200,  68},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	{ 199,  65},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	{ 198,  63},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	{ 197,  60},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	{ 196,  58},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	{ 195,  55},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	{ 194,  53},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	{ 193,  50},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	{ 192,  48},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	{ 191,  45},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	{ 190,  43},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	{ 189,  40},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	{ 188,  38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	{ 187,  35},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	{ 186,  33},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	{ 185,  30},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	{ 184,  28},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	{ 183,  25},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	{ 182,  23},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	{ 181,  20},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	{ 180,  18},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	{ 179,  15},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	{ 178,  13},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	{ 177,  10},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	{ 176,   8},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	{ 175,   5},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	{   0,   0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* MAX1111 Commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #define MAXCTRL_PD0      (1u << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #define MAXCTRL_PD1      (1u << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #define MAXCTRL_SGL      (1u << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #define MAXCTRL_UNI      (1u << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #define MAXCTRL_SEL_SH   4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #define MAXCTRL_STR      (1u << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) extern int max1111_read_channel(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * Read MAX1111 ADC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int sharpsl_pm_pxa_read_max1111(int channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	/* Ugly, better move this function into another module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (machine_is_tosa())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	    return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	/* max1111 accepts channels from 0-3, however,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 * it is encoded from 0-7 here in the code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return max1111_read_channel(channel >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int get_percentage(int voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int i = sharpsl_pm.machinfo->bat_levels - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int bl_status = sharpsl_pm.machinfo->backlight_get_status ? sharpsl_pm.machinfo->backlight_get_status() : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct battery_thresh *thresh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (sharpsl_pm.charge_mode == CHRG_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_acin_bl : sharpsl_pm.machinfo->bat_levels_acin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		thresh = bl_status ? sharpsl_pm.machinfo->bat_levels_noac_bl : sharpsl_pm.machinfo->bat_levels_noac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	while (i > 0 && (voltage > thresh[i].voltage))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return thresh[i].percentage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int get_apm_status(int voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	int low_thresh, high_thresh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (sharpsl_pm.charge_mode == CHRG_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		high_thresh = sharpsl_pm.machinfo->status_high_acin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		low_thresh = sharpsl_pm.machinfo->status_low_acin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		high_thresh = sharpsl_pm.machinfo->status_high_noac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		low_thresh = sharpsl_pm.machinfo->status_low_noac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (voltage >= high_thresh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return APM_BATTERY_STATUS_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (voltage >= low_thresh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		return APM_BATTERY_STATUS_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return APM_BATTERY_STATUS_CRITICAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) void sharpsl_battery_kick(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(125));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) EXPORT_SYMBOL(sharpsl_battery_kick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static void sharpsl_battery_thread(struct work_struct *private_)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	int voltage, percent, apm_status, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (!sharpsl_pm.machinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	sharpsl_pm.battstat.ac_status = (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN) ? APM_AC_ONLINE : APM_AC_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	/* Corgi cannot confirm when battery fully charged so periodically kick! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (!sharpsl_pm.machinfo->batfull_irq && (sharpsl_pm.charge_mode == CHRG_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			&& time_after(jiffies, sharpsl_pm.charge_start_time +  SHARPSL_CHARGE_ON_TIME_INTERVAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		schedule_delayed_work(&toggle_charger, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	for (i = 0; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		voltage = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (voltage > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (voltage <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		voltage = sharpsl_pm.machinfo->bat_levels_noac[0].voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		dev_warn(sharpsl_pm.dev, "Warning: Cannot read main battery!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	voltage = sharpsl_average_value(voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	apm_status = get_apm_status(voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	percent = get_percentage(voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	/* At low battery voltages, the voltage has a tendency to start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	   creeping back up so we try to avoid this here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if ((sharpsl_pm.battstat.ac_status == APM_AC_ONLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	    || (apm_status == APM_BATTERY_STATUS_HIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	    || percent <= sharpsl_pm.battstat.mainbat_percent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		sharpsl_pm.battstat.mainbat_voltage = voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		sharpsl_pm.battstat.mainbat_status = apm_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		sharpsl_pm.battstat.mainbat_percent = percent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	dev_dbg(sharpsl_pm.dev, "Battery: voltage: %d, status: %d, percentage: %d, time: %ld\n", voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			sharpsl_pm.battstat.mainbat_status, sharpsl_pm.battstat.mainbat_percent, jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	/* Suspend if critical battery level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if ((sharpsl_pm.battstat.ac_status != APM_AC_ONLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	     && (sharpsl_pm.battstat.mainbat_status == APM_BATTERY_STATUS_CRITICAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	     && !(sharpsl_pm.flags & SHARPSL_APM_QUEUED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		dev_err(sharpsl_pm.dev, "Fatal Off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		apm_queue_event(APM_CRITICAL_SUSPEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	schedule_delayed_work(&sharpsl_bat, SHARPSL_BATCHK_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) void sharpsl_pm_led(int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (val == SHARPSL_LED_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		dev_err(sharpsl_pm.dev, "Charging Error!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	} else if (val == SHARPSL_LED_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		dev_dbg(sharpsl_pm.dev, "Charge LED On\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		led_trigger_event(sharpsl_charge_led_trigger, LED_FULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		dev_dbg(sharpsl_pm.dev, "Charge LED Off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		led_trigger_event(sharpsl_charge_led_trigger, LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static void sharpsl_charge_on(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	dev_dbg(sharpsl_pm.dev, "Turning Charger On\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	sharpsl_pm.full_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	sharpsl_pm.charge_mode = CHRG_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	schedule_delayed_work(&toggle_charger, msecs_to_jiffies(250));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	schedule_delayed_work(&sharpsl_bat, msecs_to_jiffies(500));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void sharpsl_charge_off(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	dev_dbg(sharpsl_pm.dev, "Turning Charger Off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	sharpsl_pm.machinfo->charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	sharpsl_pm_led(SHARPSL_LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	sharpsl_pm.charge_mode = CHRG_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	schedule_delayed_work(&sharpsl_bat, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static void sharpsl_charge_error(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	sharpsl_pm_led(SHARPSL_LED_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	sharpsl_pm.machinfo->charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	sharpsl_pm.charge_mode = CHRG_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static void sharpsl_charge_toggle(struct work_struct *private_)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	dev_dbg(sharpsl_pm.dev, "Toggling Charger at time: %lx\n", jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		sharpsl_charge_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	} else if ((sharpsl_check_battery_temp() < 0) || (sharpsl_ac_check() < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		sharpsl_charge_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	sharpsl_pm_led(SHARPSL_LED_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	sharpsl_pm.machinfo->charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mdelay(SHARPSL_CHARGE_WAIT_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	sharpsl_pm.machinfo->charge(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	sharpsl_pm.charge_start_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static void sharpsl_ac_timer(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	dev_dbg(sharpsl_pm.dev, "AC Status: %d\n", acin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	sharpsl_average_clear();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (acin && (sharpsl_pm.charge_mode != CHRG_ON))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		sharpsl_charge_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	else if (sharpsl_pm.charge_mode == CHRG_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		sharpsl_charge_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	schedule_delayed_work(&sharpsl_bat, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static irqreturn_t sharpsl_ac_isr(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	/* Delay the event slightly to debounce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	/* Must be a smaller delay than the chrg_full_isr below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static void sharpsl_chrg_full_timer(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	dev_dbg(sharpsl_pm.dev, "Charge Full at time: %lx\n", jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	sharpsl_pm.full_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		dev_dbg(sharpsl_pm.dev, "Charge Full: AC removed - stop charging!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		if (sharpsl_pm.charge_mode == CHRG_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			sharpsl_charge_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	} else if (sharpsl_pm.full_count < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		dev_dbg(sharpsl_pm.dev, "Charge Full: Count too low\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		schedule_delayed_work(&toggle_charger, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	} else if (time_after(jiffies, sharpsl_pm.charge_start_time + SHARPSL_CHARGE_FINISH_TIME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		dev_dbg(sharpsl_pm.dev, "Charge Full: Interrupt generated too slowly - retry.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		schedule_delayed_work(&toggle_charger, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		sharpsl_charge_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		sharpsl_pm.charge_mode = CHRG_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		dev_dbg(sharpsl_pm.dev, "Charge Full: Charging Finished\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /* Charging Finished Interrupt (Not present on Corgi) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /* Can trigger at the same time as an AC status change so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)    delay until after that has been processed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static irqreturn_t sharpsl_chrg_full_isr(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (sharpsl_pm.flags & SHARPSL_SUSPENDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	/* delay until after any ac interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	mod_timer(&sharpsl_pm.chrg_full_timer, jiffies + msecs_to_jiffies(500));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) static irqreturn_t sharpsl_fatal_isr(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	int is_fatal = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		dev_err(sharpsl_pm.dev, "Battery now Unlocked! Suspending.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		is_fatal = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	if (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		dev_err(sharpsl_pm.dev, "Fatal Batt Error! Suspending.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		is_fatal = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	if (!(sharpsl_pm.flags & SHARPSL_APM_QUEUED) && is_fatal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		sharpsl_pm.flags |= SHARPSL_APM_QUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		apm_queue_event(APM_CRITICAL_SUSPEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  * Maintain an average of the last 10 readings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) #define SHARPSL_CNV_VALUE_NUM    10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static int sharpsl_ad_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static void sharpsl_average_clear(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	sharpsl_ad_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static int sharpsl_average_value(int ad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	int i, ad_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	static int sharpsl_ad[SHARPSL_CNV_VALUE_NUM+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (sharpsl_pm.battstat.mainbat_status != APM_BATTERY_STATUS_HIGH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		sharpsl_ad_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		return ad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	sharpsl_ad[sharpsl_ad_index] = ad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	sharpsl_ad_index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	if (sharpsl_ad_index >= SHARPSL_CNV_VALUE_NUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		for (i = 0; i < (SHARPSL_CNV_VALUE_NUM-1); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			sharpsl_ad[i] = sharpsl_ad[i+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		sharpsl_ad_index = SHARPSL_CNV_VALUE_NUM - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	for (i = 0; i < sharpsl_ad_index; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		ad_val += sharpsl_ad[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	return ad_val / sharpsl_ad_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)  * Take an array of 5 integers, remove the maximum and minimum values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)  * and return the average.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static int get_select_val(int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	int i, j, k, temp, sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	/* Find MAX val */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	temp = val[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	for (i = 1; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		if (temp < val[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			temp = val[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			j = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	/* Find MIN val */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	temp = val[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	k = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	for (i = 3; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		if (temp > val[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			temp = val[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			k = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	for (i = 0; i < 5; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		if (i != j && i != k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			sum += val[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	dev_dbg(sharpsl_pm.dev, "Average: %d from values: %d, %d, %d, %d, %d\n", sum/3, val[0], val[1], val[2], val[3], val[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	return sum/3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) static int sharpsl_check_battery_temp(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	int val, i, buff[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	/* Check battery temperature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	for (i = 0; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		sharpsl_pm.machinfo->measure_temp(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		sharpsl_pm.machinfo->measure_temp(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	val = get_select_val(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	dev_dbg(sharpsl_pm.dev, "Temperature: %d\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (val > sharpsl_pm.machinfo->charge_on_temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		printk(KERN_WARNING "Not charging: temperature out of limits.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static int sharpsl_check_battery_voltage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	int val, i, buff[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	/* disable charge, enable discharge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	sharpsl_pm.machinfo->charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	sharpsl_pm.machinfo->discharge(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	mdelay(SHARPSL_WAIT_DISCHARGE_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	if (sharpsl_pm.machinfo->discharge1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		sharpsl_pm.machinfo->discharge1(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	/* Check battery voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	for (i = 0; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_BATT_VOLT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_VOLT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	if (sharpsl_pm.machinfo->discharge1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		sharpsl_pm.machinfo->discharge1(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	sharpsl_pm.machinfo->discharge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	val = get_select_val(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	dev_dbg(sharpsl_pm.dev, "Battery Voltage: %d\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	if (val < sharpsl_pm.machinfo->charge_on_volt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) static int sharpsl_ac_check(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	int temp, i, buff[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	for (i = 0; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		buff[i] = sharpsl_pm.machinfo->read_devdata(SHARPSL_ACIN_VOLT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		mdelay(SHARPSL_CHECK_BATTERY_WAIT_TIME_ACIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	temp = get_select_val(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	dev_dbg(sharpsl_pm.dev, "AC Voltage: %d\n", temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if ((temp > sharpsl_pm.machinfo->charge_acin_high) || (temp < sharpsl_pm.machinfo->charge_acin_low)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		dev_err(sharpsl_pm.dev, "Error: AC check failed: voltage %d.\n", temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static int sharpsl_pm_suspend(struct platform_device *pdev, pm_message_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	sharpsl_pm.flags |= SHARPSL_SUSPENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	flush_delayed_work(&toggle_charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	flush_delayed_work(&sharpsl_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	if (sharpsl_pm.charge_mode == CHRG_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) static int sharpsl_pm_resume(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	/* Clear the reset source indicators as they break the bootloader upon reboot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	RCSR = 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	sharpsl_average_clear();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	sharpsl_pm.flags &= ~SHARPSL_APM_QUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	sharpsl_pm.flags &= ~SHARPSL_SUSPENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static void corgi_goto_sleep(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	dev_dbg(sharpsl_pm.dev, "Time is: %08x\n", RCNR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	dev_dbg(sharpsl_pm.dev, "Offline Charge Activate = %d\n", sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	/* not charging and AC-IN! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	if ((sharpsl_pm.flags & SHARPSL_DO_OFFLINE_CHRG) && (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		dev_dbg(sharpsl_pm.dev, "Activating Offline Charger...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		sharpsl_pm.charge_mode = CHRG_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		sharpsl_pm.flags &= ~SHARPSL_DO_OFFLINE_CHRG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		sharpsl_off_charge_battery();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	sharpsl_pm.machinfo->presuspend();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	PEDR = 0xffffffff; /* clear it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	sharpsl_pm.flags &= ~SHARPSL_ALARM_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	if ((sharpsl_pm.charge_mode == CHRG_ON) && ((alarm_enable && ((alarm_time - RCNR) > (SHARPSL_BATCHK_TIME_SUSPEND + 30))) || !alarm_enable)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		RTSR &= RTSR_ALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		RTAR = RCNR + SHARPSL_BATCHK_TIME_SUSPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		dev_dbg(sharpsl_pm.dev, "Charging alarm at: %08x\n", RTAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		sharpsl_pm.flags |= SHARPSL_ALARM_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	} else if (alarm_enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		RTSR &= RTSR_ALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		RTAR = alarm_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		dev_dbg(sharpsl_pm.dev, "User alarm at: %08x\n", RTAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		dev_dbg(sharpsl_pm.dev, "No alarms set.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	pxa_pm_enter(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	sharpsl_pm.machinfo->postsuspend();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	dev_dbg(sharpsl_pm.dev, "Corgi woken up from suspend: %08x\n", PEDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) static int corgi_enter_suspend(unsigned long alarm_time, unsigned int alarm_enable, suspend_state_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	if (!sharpsl_pm.machinfo->should_wakeup(!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE) && alarm_enable)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		if (!(sharpsl_pm.flags & SHARPSL_ALARM_ACTIVE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 			dev_dbg(sharpsl_pm.dev, "No user triggered wakeup events and not charging. Strange. Suspend.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 			corgi_goto_sleep(alarm_time, alarm_enable, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		if (sharpsl_off_charge_battery()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			dev_dbg(sharpsl_pm.dev, "Charging. Suspend...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 			corgi_goto_sleep(alarm_time, alarm_enable, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		dev_dbg(sharpsl_pm.dev, "User triggered wakeup in offline charger.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	if ((!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_LOCK)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	    (!sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_FATAL)))	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		dev_err(sharpsl_pm.dev, "Fatal condition. Suspend.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		corgi_goto_sleep(alarm_time, alarm_enable, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) static int corgi_pxa_pm_enter(suspend_state_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	unsigned long alarm_time = RTAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	unsigned int alarm_status = ((RTSR & RTSR_ALE) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	dev_dbg(sharpsl_pm.dev, "SharpSL suspending for first time.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	corgi_goto_sleep(alarm_time, alarm_status, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	while (corgi_enter_suspend(alarm_time, alarm_status, state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	if (sharpsl_pm.machinfo->earlyresume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 		sharpsl_pm.machinfo->earlyresume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	dev_dbg(sharpsl_pm.dev, "SharpSL resuming...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static int sharpsl_off_charge_error(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	dev_err(sharpsl_pm.dev, "Offline Charger: Error occurred.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	sharpsl_pm.machinfo->charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	sharpsl_pm_led(SHARPSL_LED_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	sharpsl_pm.charge_mode = CHRG_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)  * Charging Control while suspended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)  * Return 1 - go straight to sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)  * Return 0 - sleep or wakeup depending on other factors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) static int sharpsl_off_charge_battery(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	int time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	dev_dbg(sharpsl_pm.dev, "Charge Mode: %d\n", sharpsl_pm.charge_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	if (sharpsl_pm.charge_mode == CHRG_OFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		/* AC Check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		if ((sharpsl_ac_check() < 0) || (sharpsl_check_battery_temp() < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 			return sharpsl_off_charge_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		/* Start Charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		sharpsl_pm_led(SHARPSL_LED_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		sharpsl_pm.machinfo->charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		mdelay(SHARPSL_CHARGE_WAIT_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 		sharpsl_pm.machinfo->charge(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		sharpsl_pm.charge_mode = CHRG_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 		sharpsl_pm.full_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	} else if (sharpsl_pm.charge_mode != CHRG_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	if (sharpsl_pm.full_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		int time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 2\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		if ((sharpsl_check_battery_temp() < 0) || (sharpsl_check_battery_voltage() < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 			return sharpsl_off_charge_error();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		sharpsl_pm.machinfo->charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		mdelay(SHARPSL_CHARGE_WAIT_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		sharpsl_pm.machinfo->charge(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		sharpsl_pm.charge_mode = CHRG_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		time = RCNR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 			/* Check if any wakeup event had occurred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 			if (sharpsl_pm.machinfo->charger_wakeup())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 			/* Check for timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 			if ((RCNR - time) > SHARPSL_WAIT_CO_TIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 			if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 				dev_dbg(sharpsl_pm.dev, "Offline Charger: Charge full occurred. Retrying to check\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 				sharpsl_pm.full_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 				sharpsl_pm.machinfo->charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 				mdelay(SHARPSL_CHARGE_WAIT_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 				sharpsl_pm.machinfo->charge(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	dev_dbg(sharpsl_pm.dev, "Offline Charger: Step 3\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	mdelay(SHARPSL_CHARGE_CO_CHECK_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	time = RCNR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 		/* Check if any wakeup event had occurred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		if (sharpsl_pm.machinfo->charger_wakeup())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		/* Check for timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		if ((RCNR-time) > SHARPSL_WAIT_CO_TIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 			if (sharpsl_pm.full_count > SHARPSL_CHARGE_RETRY_CNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 				dev_dbg(sharpsl_pm.dev, "Offline Charger: Not charged sufficiently. Retrying.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 				sharpsl_pm.full_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 			sharpsl_pm.full_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 		if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_CHRGFULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 			dev_dbg(sharpsl_pm.dev, "Offline Charger: Charging complete.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 			sharpsl_pm_led(SHARPSL_LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 			sharpsl_pm.machinfo->charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 			sharpsl_pm.charge_mode = CHRG_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) #define sharpsl_pm_suspend	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) #define sharpsl_pm_resume	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) static ssize_t battery_percentage_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_percent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static ssize_t battery_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	return sprintf(buf, "%d\n", sharpsl_pm.battstat.mainbat_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) static DEVICE_ATTR_RO(battery_percentage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) static DEVICE_ATTR_RO(battery_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) extern void (*apm_get_power_status)(struct apm_power_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) static void sharpsl_apm_get_power_status(struct apm_power_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	info->ac_line_status = sharpsl_pm.battstat.ac_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	if (sharpsl_pm.charge_mode == CHRG_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		info->battery_status = APM_BATTERY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		info->battery_status = sharpsl_pm.battstat.mainbat_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	info->battery_flag = (1 << info->battery_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	info->battery_life = sharpsl_pm.battstat.mainbat_percent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) static const struct platform_suspend_ops sharpsl_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	.prepare	= pxa_pm_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	.finish		= pxa_pm_finish,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	.enter		= corgi_pxa_pm_enter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	.valid		= suspend_valid_only_mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) static int sharpsl_pm_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	int ret, irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	if (!pdev->dev.platform_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	sharpsl_pm.dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	sharpsl_pm.machinfo = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	sharpsl_pm.charge_mode = CHRG_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	sharpsl_pm.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	timer_setup(&sharpsl_pm.ac_timer, sharpsl_ac_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 	timer_setup(&sharpsl_pm.chrg_full_timer, sharpsl_chrg_full_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	led_trigger_register_simple("sharpsl-charge", &sharpsl_charge_led_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	sharpsl_pm.machinfo->init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	gpio_request(sharpsl_pm.machinfo->gpio_acin, "AC IN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	gpio_direction_input(sharpsl_pm.machinfo->gpio_acin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	gpio_request(sharpsl_pm.machinfo->gpio_batfull, "Battery Full");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	gpio_direction_input(sharpsl_pm.machinfo->gpio_batfull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	gpio_request(sharpsl_pm.machinfo->gpio_batlock, "Battery Lock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 	gpio_direction_input(sharpsl_pm.machinfo->gpio_batlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	/* Register interrupt handlers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 	irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_acin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	if (request_irq(irq, sharpsl_ac_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "AC Input Detect", sharpsl_ac_isr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 		dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_batlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	if (request_irq(irq, sharpsl_fatal_isr, IRQF_TRIGGER_FALLING, "Battery Cover", sharpsl_fatal_isr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 		dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	if (sharpsl_pm.machinfo->gpio_fatal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 		irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_fatal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 		if (request_irq(irq, sharpsl_fatal_isr, IRQF_TRIGGER_FALLING, "Fatal Battery", sharpsl_fatal_isr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 			dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	if (sharpsl_pm.machinfo->batfull_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 		/* Register interrupt handler. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 		irq = gpio_to_irq(sharpsl_pm.machinfo->gpio_batfull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 		if (request_irq(irq, sharpsl_chrg_full_isr, IRQF_TRIGGER_RISING, "CO", sharpsl_chrg_full_isr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 			dev_err(sharpsl_pm.dev, "Could not get irq %d.\n", irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 	ret = device_create_file(&pdev->dev, &dev_attr_battery_percentage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	ret |= device_create_file(&pdev->dev, &dev_attr_battery_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 		dev_warn(&pdev->dev, "Failed to register attributes (%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	apm_get_power_status = sharpsl_apm_get_power_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	suspend_set_ops(&sharpsl_pm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 	mod_timer(&sharpsl_pm.ac_timer, jiffies + msecs_to_jiffies(250));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) static int sharpsl_pm_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	suspend_set_ops(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	device_remove_file(&pdev->dev, &dev_attr_battery_percentage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	device_remove_file(&pdev->dev, &dev_attr_battery_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 	led_trigger_unregister_simple(sharpsl_charge_led_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_acin), sharpsl_ac_isr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 	free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_batlock), sharpsl_fatal_isr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	if (sharpsl_pm.machinfo->gpio_fatal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 		free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_fatal), sharpsl_fatal_isr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 	if (sharpsl_pm.machinfo->batfull_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 		free_irq(gpio_to_irq(sharpsl_pm.machinfo->gpio_batfull), sharpsl_chrg_full_isr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	gpio_free(sharpsl_pm.machinfo->gpio_batlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 	gpio_free(sharpsl_pm.machinfo->gpio_batfull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 	gpio_free(sharpsl_pm.machinfo->gpio_acin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 	if (sharpsl_pm.machinfo->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 		sharpsl_pm.machinfo->exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	del_timer_sync(&sharpsl_pm.chrg_full_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 	del_timer_sync(&sharpsl_pm.ac_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) static struct platform_driver sharpsl_pm_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 	.probe		= sharpsl_pm_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	.remove		= sharpsl_pm_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 	.suspend	= sharpsl_pm_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 	.resume		= sharpsl_pm_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 		.name		= "sharpsl-pm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) static int sharpsl_pm_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 	return platform_driver_register(&sharpsl_pm_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) static void sharpsl_pm_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 	platform_driver_unregister(&sharpsl_pm_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) late_initcall(sharpsl_pm_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) module_exit(sharpsl_pm_exit);