Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Battery and Power Management code for the Sharp SL-Cxx00
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/spi/corgi_lcd.h>
19
20#include <asm/irq.h>
21#include <asm/mach-types.h>
22
23#include "spitz.h"
24#include "pxa27x.h"
25#include "sharpsl_pm.h"
26
27#include "generic.h"
28
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 */
35
36static int spitz_last_ac_status;
37
38static void spitz_charger_init(void)
39{
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);
52}
53
54static void spitz_measure_temp(int on)
55{
56 gpio_set_value(SPITZ_GPIO_ADC_TEMP_ON, on);
57}
58
59static void spitz_charge(int on)
60{
61 if (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);
65 } else {
66 gpio_set_value(SPITZ_GPIO_JK_B, 0);
67 gpio_set_value(SPITZ_GPIO_CHRG_ON, 0);
68 }
69 } else {
70 gpio_set_value(SPITZ_GPIO_JK_B, 0);
71 gpio_set_value(SPITZ_GPIO_CHRG_ON, 1);
72 }
73}
74
75static void spitz_discharge(int on)
76{
77 gpio_set_value(SPITZ_GPIO_JK_A, on);
78}
79
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 */
82static void spitz_discharge1(int on)
83{
84 gpio_set_value(SPITZ_GPIO_LED_GREEN, on);
85}
86
87static unsigned long gpio18_config = GPIO18_GPIO;
88
89static void spitz_presuspend(void)
90{
91 spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
92
93 /* GPIO Sleep Register */
94 PGSR0 = 0x00144018;
95 PGSR1 = 0x00EF0000;
96 if (machine_is_akita()) {
97 PGSR2 = 0x2121C000;
98 PGSR3 = 0x00600400;
99 } else {
100 PGSR2 = 0x0121C000;
101 PGSR3 = 0x00600000;
102 }
103
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);
109
110 pxa2xx_mfp_config(&gpio18_config, 1);
111 gpio_request_one(18, GPIOF_OUT_INIT_HIGH, "Unknown");
112 gpio_free(18);
113
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 */
119
120 /* nRESET_OUT Disable */
121 PSLR |= PSLR_SL_ROD;
122
123 /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
124 PCFR = PCFR_GPR_EN | PCFR_OPDE;
125}
126
127static void spitz_postsuspend(void)
128{
129}
130
131static int spitz_should_wakeup(unsigned int resume_on_alarm)
132{
133 int is_resume = 0;
134 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
135
136 if (spitz_last_ac_status != acin) {
137 if (acin) {
138 /* charge on */
139 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
140 dev_dbg(sharpsl_pm.dev, "AC Inserted\n");
141 } else {
142 /* charge off */
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;
147 }
148 spitz_last_ac_status = acin;
149 /* Return to suspend as this must be what we were woken for */
150 return 0;
151 }
152
153 if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT))
154 is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT);
155
156 if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC))
157 is_resume |= GPIO_bit(SPITZ_GPIO_SYNC);
158
159 if (resume_on_alarm && (PEDR & PWER_RTC))
160 is_resume |= PWER_RTC;
161
162 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n", is_resume);
163 return is_resume;
164}
165
166static bool spitz_charger_wakeup(void)
167{
168 return !gpio_get_value(SPITZ_GPIO_KEY_INT) ||
169 gpio_get_value(SPITZ_GPIO_SYNC);
170}
171
172static unsigned long spitzpm_read_devdata(int type)
173{
174 switch (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:
188 default:
189 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
190 }
191}
192
193struct sharpsl_charger_machinfo spitz_pm_machinfo = {
194 .init = spitz_charger_init,
195 .exit = NULL,
196 .gpio_batlock = SPITZ_GPIO_BAT_COVER,
197 .gpio_acin = SPITZ_GPIO_AC_IN,
198 .gpio_batfull = SPITZ_GPIO_CHRG_FULL,
199 .batfull_irq = 1,
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,
212#endif
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,
219 .bat_levels = 40,
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,
226};
227
228static struct platform_device *spitzpm_device;
229
230static int spitzpm_init(void)
231{
232 int ret;
233
234 if (!machine_is_spitz() && !machine_is_akita()
235 && !machine_is_borzoi())
236 return -ENODEV;
237
238 spitzpm_device = platform_device_alloc("sharpsl-pm", -1);
239 if (!spitzpm_device)
240 return -ENOMEM;
241
242 spitzpm_device->dev.platform_data = &spitz_pm_machinfo;
243 ret = platform_device_add(spitzpm_device);
244
245 if (ret)
246 platform_device_put(spitzpm_device);
247
248 return ret;
249}
250
251static void spitzpm_exit(void)
252{
253 platform_device_unregister(spitzpm_device);
254}
255
256module_init(spitzpm_init);
257module_exit(spitzpm_exit);
1/*
2 * Battery and Power Management code for the Sharp SL-Cxx00
3 *
4 * Copyright (c) 2005 Richard Purdie
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#include <linux/module.h>
13#include <linux/stat.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/gpio-pxa.h>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21#include <linux/apm-emulation.h>
22
23#include <asm/irq.h>
24#include <asm/mach-types.h>
25#include <mach/hardware.h>
26
27#include <mach/spitz.h>
28#include <mach/pxa27x.h>
29#include <mach/sharpsl_pm.h>
30
31#include "generic.h"
32
33#define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
34#define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
35#define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
36#define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
37#define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
38#define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
39
40static int spitz_last_ac_status;
41
42static struct gpio spitz_charger_gpios[] = {
43 { SPITZ_GPIO_KEY_INT, GPIOF_IN, "Keyboard Interrupt" },
44 { SPITZ_GPIO_SYNC, GPIOF_IN, "Sync" },
45 { SPITZ_GPIO_AC_IN, GPIOF_IN, "Charger Detection" },
46 { SPITZ_GPIO_ADC_TEMP_ON, GPIOF_OUT_INIT_LOW, "ADC Temp On" },
47 { SPITZ_GPIO_JK_B, GPIOF_OUT_INIT_LOW, "JK B" },
48 { SPITZ_GPIO_CHRG_ON, GPIOF_OUT_INIT_LOW, "Charger On" },
49};
50
51static void spitz_charger_init(void)
52{
53 gpio_request_array(ARRAY_AND_SIZE(spitz_charger_gpios));
54}
55
56static void spitz_measure_temp(int on)
57{
58 gpio_set_value(SPITZ_GPIO_ADC_TEMP_ON, on);
59}
60
61static void spitz_charge(int on)
62{
63 if (on) {
64 if (sharpsl_pm.flags & SHARPSL_SUSPENDED) {
65 gpio_set_value(SPITZ_GPIO_JK_B, 1);
66 gpio_set_value(SPITZ_GPIO_CHRG_ON, 0);
67 } else {
68 gpio_set_value(SPITZ_GPIO_JK_B, 0);
69 gpio_set_value(SPITZ_GPIO_CHRG_ON, 0);
70 }
71 } else {
72 gpio_set_value(SPITZ_GPIO_JK_B, 0);
73 gpio_set_value(SPITZ_GPIO_CHRG_ON, 1);
74 }
75}
76
77static void spitz_discharge(int on)
78{
79 gpio_set_value(SPITZ_GPIO_JK_A, on);
80}
81
82/* HACK - For unknown reasons, accurate voltage readings are only made with a load
83 on the power bus which the green led on spitz provides */
84static void spitz_discharge1(int on)
85{
86 gpio_set_value(SPITZ_GPIO_LED_GREEN, on);
87}
88
89static unsigned long gpio18_config[] = {
90 GPIO18_RDY,
91 GPIO18_GPIO,
92};
93
94static void spitz_presuspend(void)
95{
96 spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
97
98 /* GPIO Sleep Register */
99 PGSR0 = 0x00144018;
100 PGSR1 = 0x00EF0000;
101 if (machine_is_akita()) {
102 PGSR2 = 0x2121C000;
103 PGSR3 = 0x00600400;
104 } else {
105 PGSR2 = 0x0121C000;
106 PGSR3 = 0x00600000;
107 }
108
109 PGSR0 &= ~SPITZ_GPIO_G0_STROBE_BIT;
110 PGSR1 &= ~SPITZ_GPIO_G1_STROBE_BIT;
111 PGSR2 &= ~SPITZ_GPIO_G2_STROBE_BIT;
112 PGSR3 &= ~SPITZ_GPIO_G3_STROBE_BIT;
113 PGSR2 |= GPIO_bit(SPITZ_GPIO_KEY_STROBE0);
114
115 pxa2xx_mfp_config(&gpio18_config[0], 1);
116 gpio_request_one(18, GPIOF_OUT_INIT_HIGH, "Unknown");
117 gpio_free(18);
118
119 PRER = GPIO_bit(SPITZ_GPIO_KEY_INT);
120 PFER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
121 PWER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET) | PWER_RTC;
122 PKWR = GPIO_bit(SPITZ_GPIO_SYNC) | GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
123 PKSR = 0xffffffff; /* clear */
124
125 /* nRESET_OUT Disable */
126 PSLR |= PSLR_SL_ROD;
127
128 /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
129 PCFR = PCFR_GPR_EN | PCFR_OPDE;
130}
131
132static void spitz_postsuspend(void)
133{
134 pxa2xx_mfp_config(&gpio18_config[1], 1);
135}
136
137static int spitz_should_wakeup(unsigned int resume_on_alarm)
138{
139 int is_resume = 0;
140 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
141
142 if (spitz_last_ac_status != acin) {
143 if (acin) {
144 /* charge on */
145 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
146 dev_dbg(sharpsl_pm.dev, "AC Inserted\n");
147 } else {
148 /* charge off */
149 dev_dbg(sharpsl_pm.dev, "AC Removed\n");
150 sharpsl_pm_led(SHARPSL_LED_OFF);
151 sharpsl_pm.machinfo->charge(0);
152 sharpsl_pm.charge_mode = CHRG_OFF;
153 }
154 spitz_last_ac_status = acin;
155 /* Return to suspend as this must be what we were woken for */
156 return 0;
157 }
158
159 if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT))
160 is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT);
161
162 if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC))
163 is_resume |= GPIO_bit(SPITZ_GPIO_SYNC);
164
165 if (resume_on_alarm && (PEDR & PWER_RTC))
166 is_resume |= PWER_RTC;
167
168 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n", is_resume);
169 return is_resume;
170}
171
172static unsigned long spitz_charger_wakeup(void)
173{
174 unsigned long ret;
175 ret = ((!gpio_get_value(SPITZ_GPIO_KEY_INT)
176 << GPIO_bit(SPITZ_GPIO_KEY_INT))
177 | gpio_get_value(SPITZ_GPIO_SYNC));
178 return ret;
179}
180
181unsigned long spitzpm_read_devdata(int type)
182{
183 switch (type) {
184 case SHARPSL_STATUS_ACIN:
185 return !gpio_get_value(SPITZ_GPIO_AC_IN);
186 case SHARPSL_STATUS_LOCK:
187 return gpio_get_value(sharpsl_pm.machinfo->gpio_batlock);
188 case SHARPSL_STATUS_CHRGFULL:
189 return gpio_get_value(sharpsl_pm.machinfo->gpio_batfull);
190 case SHARPSL_STATUS_FATAL:
191 return gpio_get_value(sharpsl_pm.machinfo->gpio_fatal);
192 case SHARPSL_ACIN_VOLT:
193 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
194 case SHARPSL_BATT_TEMP:
195 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
196 case SHARPSL_BATT_VOLT:
197 default:
198 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
199 }
200}
201
202struct sharpsl_charger_machinfo spitz_pm_machinfo = {
203 .init = spitz_charger_init,
204 .exit = NULL,
205 .gpio_batlock = SPITZ_GPIO_BAT_COVER,
206 .gpio_acin = SPITZ_GPIO_AC_IN,
207 .gpio_batfull = SPITZ_GPIO_CHRG_FULL,
208 .batfull_irq = 1,
209 .gpio_fatal = SPITZ_GPIO_FATAL_BAT,
210 .discharge = spitz_discharge,
211 .discharge1 = spitz_discharge1,
212 .charge = spitz_charge,
213 .measure_temp = spitz_measure_temp,
214 .presuspend = spitz_presuspend,
215 .postsuspend = spitz_postsuspend,
216 .read_devdata = spitzpm_read_devdata,
217 .charger_wakeup = spitz_charger_wakeup,
218 .should_wakeup = spitz_should_wakeup,
219#if defined(CONFIG_LCD_CORGI)
220 .backlight_limit = corgi_lcd_limit_intensity,
221#endif
222 .charge_on_volt = SHARPSL_CHARGE_ON_VOLT,
223 .charge_on_temp = SHARPSL_CHARGE_ON_TEMP,
224 .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH,
225 .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW,
226 .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT,
227 .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT,
228 .bat_levels = 40,
229 .bat_levels_noac = sharpsl_battery_levels_noac,
230 .bat_levels_acin = sharpsl_battery_levels_acin,
231 .status_high_acin = 188,
232 .status_low_acin = 178,
233 .status_high_noac = 185,
234 .status_low_noac = 175,
235};
236
237static struct platform_device *spitzpm_device;
238
239static int __devinit spitzpm_init(void)
240{
241 int ret;
242
243 if (!machine_is_spitz() && !machine_is_akita()
244 && !machine_is_borzoi())
245 return -ENODEV;
246
247 spitzpm_device = platform_device_alloc("sharpsl-pm", -1);
248 if (!spitzpm_device)
249 return -ENOMEM;
250
251 spitzpm_device->dev.platform_data = &spitz_pm_machinfo;
252 ret = platform_device_add(spitzpm_device);
253
254 if (ret)
255 platform_device_put(spitzpm_device);
256
257 return ret;
258}
259
260static void spitzpm_exit(void)
261{
262 platform_device_unregister(spitzpm_device);
263}
264
265module_init(spitzpm_init);
266module_exit(spitzpm_exit);