1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery and Power Management code for the Sharp SL-C7xx
5 * Copyright (c) 2005 Richard Purdie
8 #include <linux/module.h>
9 #include <linux/stat.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/gpio.h>
14 #include <linux/gpio-pxa.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/apm-emulation.h>
21 #include <asm/mach-types.h>
22 #include <mach/hardware.h>
24 #include <mach/corgi.h>
25 #include <mach/pxa2xx-regs.h>
26 #include "sharpsl_pm.h"
30 #define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
31 #define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
32 #define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
33 #define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
34 #define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
35 #define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
37 static struct gpio charger_gpios
[] = {
38 { CORGI_GPIO_ADC_TEMP_ON
, GPIOF_OUT_INIT_LOW
, "ADC Temp On" },
39 { CORGI_GPIO_CHRG_ON
, GPIOF_OUT_INIT_LOW
, "Charger On" },
40 { CORGI_GPIO_CHRG_UKN
, GPIOF_OUT_INIT_LOW
, "Charger Unknown" },
41 { CORGI_GPIO_AC_IN
, GPIOF_IN
, "Charger Detection" },
42 { CORGI_GPIO_KEY_INT
, GPIOF_IN
, "Key Interrupt" },
43 { CORGI_GPIO_WAKEUP
, GPIOF_IN
, "System wakeup notification" },
46 static void corgi_charger_init(void)
48 gpio_request_array(ARRAY_AND_SIZE(charger_gpios
));
51 static void corgi_measure_temp(int on
)
53 gpio_set_value(CORGI_GPIO_ADC_TEMP_ON
, on
);
56 static void corgi_charge(int on
)
59 if (machine_is_corgi() && (sharpsl_pm
.flags
& SHARPSL_SUSPENDED
)) {
60 gpio_set_value(CORGI_GPIO_CHRG_ON
, 0);
61 gpio_set_value(CORGI_GPIO_CHRG_UKN
, 1);
63 gpio_set_value(CORGI_GPIO_CHRG_ON
, 1);
64 gpio_set_value(CORGI_GPIO_CHRG_UKN
, 0);
67 gpio_set_value(CORGI_GPIO_CHRG_ON
, 0);
68 gpio_set_value(CORGI_GPIO_CHRG_UKN
, 0);
72 static void corgi_discharge(int on
)
74 gpio_set_value(CORGI_GPIO_DISCHARGE_ON
, on
);
77 static void corgi_presuspend(void)
81 static void corgi_postsuspend(void)
86 * Check what brought us out of the suspend.
87 * Return: 0 to sleep, otherwise wake
89 static int corgi_should_wakeup(unsigned int resume_on_alarm
)
93 dev_dbg(sharpsl_pm
.dev
, "PEDR = %x, GPIO_AC_IN = %d, "
94 "GPIO_CHRG_FULL = %d, GPIO_KEY_INT = %d, GPIO_WAKEUP = %d\n",
95 PEDR
, gpio_get_value(CORGI_GPIO_AC_IN
),
96 gpio_get_value(CORGI_GPIO_CHRG_FULL
),
97 gpio_get_value(CORGI_GPIO_KEY_INT
),
98 gpio_get_value(CORGI_GPIO_WAKEUP
));
100 if ((PEDR
& GPIO_bit(CORGI_GPIO_AC_IN
))) {
101 if (sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
)) {
103 dev_dbg(sharpsl_pm
.dev
, "ac insert\n");
104 sharpsl_pm
.flags
|= SHARPSL_DO_OFFLINE_CHRG
;
107 dev_dbg(sharpsl_pm
.dev
, "ac remove\n");
108 sharpsl_pm_led(SHARPSL_LED_OFF
);
109 sharpsl_pm
.machinfo
->charge(0);
110 sharpsl_pm
.charge_mode
= CHRG_OFF
;
114 if ((PEDR
& GPIO_bit(CORGI_GPIO_CHRG_FULL
)))
115 dev_dbg(sharpsl_pm
.dev
, "Charge full interrupt\n");
117 if (PEDR
& GPIO_bit(CORGI_GPIO_KEY_INT
))
118 is_resume
|= GPIO_bit(CORGI_GPIO_KEY_INT
);
120 if (PEDR
& GPIO_bit(CORGI_GPIO_WAKEUP
))
121 is_resume
|= GPIO_bit(CORGI_GPIO_WAKEUP
);
123 if (resume_on_alarm
&& (PEDR
& PWER_RTC
))
124 is_resume
|= PWER_RTC
;
126 dev_dbg(sharpsl_pm
.dev
, "is_resume: %x\n",is_resume
);
130 static bool corgi_charger_wakeup(void)
132 return !gpio_get_value(CORGI_GPIO_AC_IN
) ||
133 !gpio_get_value(CORGI_GPIO_KEY_INT
) ||
134 !gpio_get_value(CORGI_GPIO_WAKEUP
);
137 unsigned long corgipm_read_devdata(int type
)
140 case SHARPSL_STATUS_ACIN
:
141 return !gpio_get_value(CORGI_GPIO_AC_IN
);
142 case SHARPSL_STATUS_LOCK
:
143 return gpio_get_value(sharpsl_pm
.machinfo
->gpio_batlock
);
144 case SHARPSL_STATUS_CHRGFULL
:
145 return gpio_get_value(sharpsl_pm
.machinfo
->gpio_batfull
);
146 case SHARPSL_STATUS_FATAL
:
147 return gpio_get_value(sharpsl_pm
.machinfo
->gpio_fatal
);
148 case SHARPSL_ACIN_VOLT
:
149 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT
);
150 case SHARPSL_BATT_TEMP
:
151 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP
);
152 case SHARPSL_BATT_VOLT
:
154 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT
);
158 static struct sharpsl_charger_machinfo corgi_pm_machinfo
= {
159 .init
= corgi_charger_init
,
161 .gpio_batlock
= CORGI_GPIO_BAT_COVER
,
162 .gpio_acin
= CORGI_GPIO_AC_IN
,
163 .gpio_batfull
= CORGI_GPIO_CHRG_FULL
,
164 .discharge
= corgi_discharge
,
165 .charge
= corgi_charge
,
166 .measure_temp
= corgi_measure_temp
,
167 .presuspend
= corgi_presuspend
,
168 .postsuspend
= corgi_postsuspend
,
169 .read_devdata
= corgipm_read_devdata
,
170 .charger_wakeup
= corgi_charger_wakeup
,
171 .should_wakeup
= corgi_should_wakeup
,
172 #if defined(CONFIG_LCD_CORGI)
173 .backlight_limit
= corgi_lcd_limit_intensity
,
175 .charge_on_volt
= SHARPSL_CHARGE_ON_VOLT
,
176 .charge_on_temp
= SHARPSL_CHARGE_ON_TEMP
,
177 .charge_acin_high
= SHARPSL_CHARGE_ON_ACIN_HIGH
,
178 .charge_acin_low
= SHARPSL_CHARGE_ON_ACIN_LOW
,
179 .fatal_acin_volt
= SHARPSL_FATAL_ACIN_VOLT
,
180 .fatal_noacin_volt
= SHARPSL_FATAL_NOACIN_VOLT
,
182 .bat_levels_noac
= sharpsl_battery_levels_noac
,
183 .bat_levels_acin
= sharpsl_battery_levels_acin
,
184 .status_high_acin
= 188,
185 .status_low_acin
= 178,
186 .status_high_noac
= 185,
187 .status_low_noac
= 175,
190 static struct platform_device
*corgipm_device
;
192 static int corgipm_init(void)
196 if (!machine_is_corgi() && !machine_is_shepherd()
197 && !machine_is_husky())
200 corgipm_device
= platform_device_alloc("sharpsl-pm", -1);
204 if (!machine_is_corgi())
205 corgi_pm_machinfo
.batfull_irq
= 1;
207 corgipm_device
->dev
.platform_data
= &corgi_pm_machinfo
;
208 ret
= platform_device_add(corgipm_device
);
211 platform_device_put(corgipm_device
);
216 static void corgipm_exit(void)
218 platform_device_unregister(corgipm_device
);
221 module_init(corgipm_init
);
222 module_exit(corgipm_exit
);