1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Battery and Power Management code for the Sharp SL-C7xx
4 *
5 * Copyright (c) 2005 Richard Purdie
6 */
7
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/io.h>
19
20 #include <asm/irq.h>
21 #include <asm/mach-types.h>
22 #include <mach/hardware.h>
23
24 #include <mach/corgi.h>
25 #include <mach/pxa2xx-regs.h>
26 #include "sharpsl_pm.h"
27
28 #include "generic.h"
29
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 */
36
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" },
44 };
45
corgi_charger_init(void)46 static void corgi_charger_init(void)
47 {
48 gpio_request_array(ARRAY_AND_SIZE(charger_gpios));
49 }
50
corgi_measure_temp(int on)51 static void corgi_measure_temp(int on)
52 {
53 gpio_set_value(CORGI_GPIO_ADC_TEMP_ON, on);
54 }
55
corgi_charge(int on)56 static void corgi_charge(int on)
57 {
58 if (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);
62 } else {
63 gpio_set_value(CORGI_GPIO_CHRG_ON, 1);
64 gpio_set_value(CORGI_GPIO_CHRG_UKN, 0);
65 }
66 } else {
67 gpio_set_value(CORGI_GPIO_CHRG_ON, 0);
68 gpio_set_value(CORGI_GPIO_CHRG_UKN, 0);
69 }
70 }
71
corgi_discharge(int on)72 static void corgi_discharge(int on)
73 {
74 gpio_set_value(CORGI_GPIO_DISCHARGE_ON, on);
75 }
76
corgi_presuspend(void)77 static void corgi_presuspend(void)
78 {
79 }
80
corgi_postsuspend(void)81 static void corgi_postsuspend(void)
82 {
83 }
84
85 /*
86 * Check what brought us out of the suspend.
87 * Return: 0 to sleep, otherwise wake
88 */
corgi_should_wakeup(unsigned int resume_on_alarm)89 static int corgi_should_wakeup(unsigned int resume_on_alarm)
90 {
91 int is_resume = 0;
92
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));
99
100 if ((PEDR & GPIO_bit(CORGI_GPIO_AC_IN))) {
101 if (sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN)) {
102 /* charge on */
103 dev_dbg(sharpsl_pm.dev, "ac insert\n");
104 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
105 } else {
106 /* charge off */
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;
111 }
112 }
113
114 if ((PEDR & GPIO_bit(CORGI_GPIO_CHRG_FULL)))
115 dev_dbg(sharpsl_pm.dev, "Charge full interrupt\n");
116
117 if (PEDR & GPIO_bit(CORGI_GPIO_KEY_INT))
118 is_resume |= GPIO_bit(CORGI_GPIO_KEY_INT);
119
120 if (PEDR & GPIO_bit(CORGI_GPIO_WAKEUP))
121 is_resume |= GPIO_bit(CORGI_GPIO_WAKEUP);
122
123 if (resume_on_alarm && (PEDR & PWER_RTC))
124 is_resume |= PWER_RTC;
125
126 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
127 return is_resume;
128 }
129
corgi_charger_wakeup(void)130 static bool corgi_charger_wakeup(void)
131 {
132 return !gpio_get_value(CORGI_GPIO_AC_IN) ||
133 !gpio_get_value(CORGI_GPIO_KEY_INT) ||
134 !gpio_get_value(CORGI_GPIO_WAKEUP);
135 }
136
corgipm_read_devdata(int type)137 unsigned long corgipm_read_devdata(int type)
138 {
139 switch(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:
153 default:
154 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
155 }
156 }
157
158 static struct sharpsl_charger_machinfo corgi_pm_machinfo = {
159 .init = corgi_charger_init,
160 .exit = NULL,
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,
174 #endif
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,
181 .bat_levels = 40,
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,
188 };
189
190 static struct platform_device *corgipm_device;
191
corgipm_init(void)192 static int corgipm_init(void)
193 {
194 int ret;
195
196 if (!machine_is_corgi() && !machine_is_shepherd()
197 && !machine_is_husky())
198 return -ENODEV;
199
200 corgipm_device = platform_device_alloc("sharpsl-pm", -1);
201 if (!corgipm_device)
202 return -ENOMEM;
203
204 if (!machine_is_corgi())
205 corgi_pm_machinfo.batfull_irq = 1;
206
207 corgipm_device->dev.platform_data = &corgi_pm_machinfo;
208 ret = platform_device_add(corgipm_device);
209
210 if (ret)
211 platform_device_put(corgipm_device);
212
213 return ret;
214 }
215
corgipm_exit(void)216 static void corgipm_exit(void)
217 {
218 platform_device_unregister(corgipm_device);
219 }
220
221 module_init(corgipm_init);
222 module_exit(corgipm_exit);
223