1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery and Power Management code for the Sharp SL-Cxx00
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>
18 #include <linux/spi/corgi_lcd.h>
21 #include <asm/mach-types.h>
25 #include "sharpsl_pm.h"
29 #define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
30 #define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
31 #define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
32 #define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
33 #define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
34 #define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
36 static int spitz_last_ac_status
;
38 static void spitz_charger_init(void)
40 gpio_request(SPITZ_GPIO_KEY_INT
, "Keyboard Interrupt");
41 gpio_direction_input(SPITZ_GPIO_KEY_INT
);
42 gpio_request(SPITZ_GPIO_SYNC
, "Sync");
43 gpio_direction_input(SPITZ_GPIO_SYNC
);
44 gpio_request(SPITZ_GPIO_AC_IN
, "Charger Detection");
45 gpio_direction_input(SPITZ_GPIO_AC_IN
);
46 gpio_request(SPITZ_GPIO_ADC_TEMP_ON
, "ADC Temp On");
47 gpio_direction_output(SPITZ_GPIO_ADC_TEMP_ON
, 0);
48 gpio_request(SPITZ_GPIO_JK_B
, "JK B");
49 gpio_direction_output(SPITZ_GPIO_JK_B
, 0);
50 gpio_request(SPITZ_GPIO_CHRG_ON
, "Charger On");
51 gpio_direction_output(SPITZ_GPIO_CHRG_ON
, 0);
54 static void spitz_measure_temp(int on
)
56 gpio_set_value(SPITZ_GPIO_ADC_TEMP_ON
, on
);
59 static void spitz_charge(int on
)
62 if (sharpsl_pm
.flags
& SHARPSL_SUSPENDED
) {
63 gpio_set_value(SPITZ_GPIO_JK_B
, 1);
64 gpio_set_value(SPITZ_GPIO_CHRG_ON
, 0);
66 gpio_set_value(SPITZ_GPIO_JK_B
, 0);
67 gpio_set_value(SPITZ_GPIO_CHRG_ON
, 0);
70 gpio_set_value(SPITZ_GPIO_JK_B
, 0);
71 gpio_set_value(SPITZ_GPIO_CHRG_ON
, 1);
75 static void spitz_discharge(int on
)
77 gpio_set_value(SPITZ_GPIO_JK_A
, on
);
80 /* HACK - For unknown reasons, accurate voltage readings are only made with a load
81 on the power bus which the green led on spitz provides */
82 static void spitz_discharge1(int on
)
84 gpio_set_value(SPITZ_GPIO_LED_GREEN
, on
);
87 static unsigned long gpio18_config
= GPIO18_GPIO
;
89 static void spitz_presuspend(void)
91 spitz_last_ac_status
= sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
);
93 /* GPIO Sleep Register */
96 if (machine_is_akita()) {
104 PGSR0
&= ~SPITZ_GPIO_G0_STROBE_BIT
;
105 PGSR1
&= ~SPITZ_GPIO_G1_STROBE_BIT
;
106 PGSR2
&= ~SPITZ_GPIO_G2_STROBE_BIT
;
107 PGSR3
&= ~SPITZ_GPIO_G3_STROBE_BIT
;
108 PGSR2
|= GPIO_bit(SPITZ_GPIO_KEY_STROBE0
);
110 pxa2xx_mfp_config(&gpio18_config
, 1);
111 gpio_request_one(18, GPIOF_OUT_INIT_HIGH
, "Unknown");
114 PRER
= GPIO_bit(SPITZ_GPIO_KEY_INT
);
115 PFER
= GPIO_bit(SPITZ_GPIO_KEY_INT
) | GPIO_bit(SPITZ_GPIO_RESET
);
116 PWER
= GPIO_bit(SPITZ_GPIO_KEY_INT
) | GPIO_bit(SPITZ_GPIO_RESET
) | PWER_RTC
;
117 PKWR
= GPIO_bit(SPITZ_GPIO_SYNC
) | GPIO_bit(SPITZ_GPIO_KEY_INT
) | GPIO_bit(SPITZ_GPIO_RESET
);
118 PKSR
= 0xffffffff; /* clear */
120 /* nRESET_OUT Disable */
123 /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
124 PCFR
= PCFR_GPR_EN
| PCFR_OPDE
;
127 static void spitz_postsuspend(void)
131 static int spitz_should_wakeup(unsigned int resume_on_alarm
)
134 int acin
= sharpsl_pm
.machinfo
->read_devdata(SHARPSL_STATUS_ACIN
);
136 if (spitz_last_ac_status
!= acin
) {
139 sharpsl_pm
.flags
|= SHARPSL_DO_OFFLINE_CHRG
;
140 dev_dbg(sharpsl_pm
.dev
, "AC Inserted\n");
143 dev_dbg(sharpsl_pm
.dev
, "AC Removed\n");
144 sharpsl_pm_led(SHARPSL_LED_OFF
);
145 sharpsl_pm
.machinfo
->charge(0);
146 sharpsl_pm
.charge_mode
= CHRG_OFF
;
148 spitz_last_ac_status
= acin
;
149 /* Return to suspend as this must be what we were woken for */
153 if (PEDR
& GPIO_bit(SPITZ_GPIO_KEY_INT
))
154 is_resume
|= GPIO_bit(SPITZ_GPIO_KEY_INT
);
156 if (PKSR
& GPIO_bit(SPITZ_GPIO_SYNC
))
157 is_resume
|= GPIO_bit(SPITZ_GPIO_SYNC
);
159 if (resume_on_alarm
&& (PEDR
& PWER_RTC
))
160 is_resume
|= PWER_RTC
;
162 dev_dbg(sharpsl_pm
.dev
, "is_resume: %x\n", is_resume
);
166 static bool spitz_charger_wakeup(void)
168 return !gpio_get_value(SPITZ_GPIO_KEY_INT
) ||
169 gpio_get_value(SPITZ_GPIO_SYNC
);
172 static unsigned long spitzpm_read_devdata(int type
)
175 case SHARPSL_STATUS_ACIN
:
176 return !gpio_get_value(SPITZ_GPIO_AC_IN
);
177 case SHARPSL_STATUS_LOCK
:
178 return gpio_get_value(sharpsl_pm
.machinfo
->gpio_batlock
);
179 case SHARPSL_STATUS_CHRGFULL
:
180 return gpio_get_value(sharpsl_pm
.machinfo
->gpio_batfull
);
181 case SHARPSL_STATUS_FATAL
:
182 return gpio_get_value(sharpsl_pm
.machinfo
->gpio_fatal
);
183 case SHARPSL_ACIN_VOLT
:
184 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT
);
185 case SHARPSL_BATT_TEMP
:
186 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP
);
187 case SHARPSL_BATT_VOLT
:
189 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT
);
193 struct sharpsl_charger_machinfo spitz_pm_machinfo
= {
194 .init
= spitz_charger_init
,
196 .gpio_batlock
= SPITZ_GPIO_BAT_COVER
,
197 .gpio_acin
= SPITZ_GPIO_AC_IN
,
198 .gpio_batfull
= SPITZ_GPIO_CHRG_FULL
,
200 .gpio_fatal
= SPITZ_GPIO_FATAL_BAT
,
201 .discharge
= spitz_discharge
,
202 .discharge1
= spitz_discharge1
,
203 .charge
= spitz_charge
,
204 .measure_temp
= spitz_measure_temp
,
205 .presuspend
= spitz_presuspend
,
206 .postsuspend
= spitz_postsuspend
,
207 .read_devdata
= spitzpm_read_devdata
,
208 .charger_wakeup
= spitz_charger_wakeup
,
209 .should_wakeup
= spitz_should_wakeup
,
210 #if defined(CONFIG_LCD_CORGI)
211 .backlight_limit
= corgi_lcd_limit_intensity
,
213 .charge_on_volt
= SHARPSL_CHARGE_ON_VOLT
,
214 .charge_on_temp
= SHARPSL_CHARGE_ON_TEMP
,
215 .charge_acin_high
= SHARPSL_CHARGE_ON_ACIN_HIGH
,
216 .charge_acin_low
= SHARPSL_CHARGE_ON_ACIN_LOW
,
217 .fatal_acin_volt
= SHARPSL_FATAL_ACIN_VOLT
,
218 .fatal_noacin_volt
= SHARPSL_FATAL_NOACIN_VOLT
,
220 .bat_levels_noac
= sharpsl_battery_levels_noac
,
221 .bat_levels_acin
= sharpsl_battery_levels_acin
,
222 .status_high_acin
= 188,
223 .status_low_acin
= 178,
224 .status_high_noac
= 185,
225 .status_low_noac
= 175,
228 static struct platform_device
*spitzpm_device
;
230 static int spitzpm_init(void)
234 if (!machine_is_spitz() && !machine_is_akita()
235 && !machine_is_borzoi())
238 spitzpm_device
= platform_device_alloc("sharpsl-pm", -1);
242 spitzpm_device
->dev
.platform_data
= &spitz_pm_machinfo
;
243 ret
= platform_device_add(spitzpm_device
);
246 platform_device_put(spitzpm_device
);
251 static void spitzpm_exit(void)
253 platform_device_unregister(spitzpm_device
);
256 module_init(spitzpm_init
);
257 module_exit(spitzpm_exit
);