blob: 2e4490562c9e85d5a4891c823a9e17d83a78923a [file] [log] [blame]
Richard Purdiee8b6f7f2005-11-13 10:07:47 +00001/*
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/interrupt.h>
18#include <linux/platform_device.h>
Russell King14fca612007-02-27 12:10:07 +000019#include <linux/apm-emulation.h>
20
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000021#include <asm/irq.h>
22#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/hardware.h>
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/sharpsl.h>
26#include <mach/spitz.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/pxa2xx-regs.h>
28#include <mach/pxa2xx-gpio.h>
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000029#include "sharpsl.h"
30
Richard Purdief8703dc2006-06-19 19:58:52 +010031#define SHARPSL_CHARGE_ON_VOLT 0x99 /* 2.9V */
32#define SHARPSL_CHARGE_ON_TEMP 0xe0 /* 2.9V */
33#define SHARPSL_CHARGE_ON_ACIN_HIGH 0x9b /* 6V */
34#define SHARPSL_CHARGE_ON_ACIN_LOW 0x34 /* 2V */
35#define SHARPSL_FATAL_ACIN_VOLT 182 /* 3.45V */
36#define SHARPSL_FATAL_NOACIN_VOLT 170 /* 3.40V */
37
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000038static int spitz_last_ac_status;
39
40static void spitz_charger_init(void)
41{
42 pxa_gpio_mode(SPITZ_GPIO_KEY_INT | GPIO_IN);
43 pxa_gpio_mode(SPITZ_GPIO_SYNC | GPIO_IN);
Richard Purdieb7557de2006-01-05 20:44:55 +000044 sharpsl_pm_pxa_init();
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000045}
46
47static void spitz_measure_temp(int on)
48{
Eric Miaofff14722008-09-05 22:15:23 +080049 gpio_set_value(SPITZ_GPIO_ADC_TEMP_ON, on);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000050}
51
52static void spitz_charge(int on)
53{
54 if (on) {
55 if (sharpsl_pm.flags & SHARPSL_SUSPENDED) {
Eric Miaofff14722008-09-05 22:15:23 +080056 gpio_set_value(SPITZ_GPIO_JK_B, 1);
57 gpio_set_value(SPITZ_GPIO_CHRG_ON, 0);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000058 } else {
Eric Miaofff14722008-09-05 22:15:23 +080059 gpio_set_value(SPITZ_GPIO_JK_B, 0);
60 gpio_set_value(SPITZ_GPIO_CHRG_ON, 0);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000061 }
62 } else {
Eric Miaofff14722008-09-05 22:15:23 +080063 gpio_set_value(SPITZ_GPIO_JK_B, 0);
64 gpio_set_value(SPITZ_GPIO_CHRG_ON, 1);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000065 }
66}
67
68static void spitz_discharge(int on)
69{
Eric Miaofff14722008-09-05 22:15:23 +080070 gpio_set_value(SPITZ_GPIO_JK_A, on);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000071}
72
73/* HACK - For unknown reasons, accurate voltage readings are only made with a load
74 on the power bus which the green led on spitz provides */
75static void spitz_discharge1(int on)
76{
Eric Miaofff14722008-09-05 22:15:23 +080077 gpio_set_value(SPITZ_GPIO_LED_GREEN, on);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000078}
79
80static void spitz_presuspend(void)
81{
Richard Purdieb7557de2006-01-05 20:44:55 +000082 spitz_last_ac_status = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +000083
84 /* GPIO Sleep Register */
85 PGSR0 = 0x00144018;
86 PGSR1 = 0x00EF0000;
87 if (machine_is_akita()) {
88 PGSR2 = 0x2121C000;
89 PGSR3 = 0x00600400;
90 } else {
91 PGSR2 = 0x0121C000;
92 PGSR3 = 0x00600000;
93 }
94
95 PGSR0 &= ~SPITZ_GPIO_G0_STROBE_BIT;
96 PGSR1 &= ~SPITZ_GPIO_G1_STROBE_BIT;
97 PGSR2 &= ~SPITZ_GPIO_G2_STROBE_BIT;
98 PGSR3 &= ~SPITZ_GPIO_G3_STROBE_BIT;
99 PGSR2 |= GPIO_bit(SPITZ_GPIO_KEY_STROBE0);
100
101 pxa_gpio_mode(GPIO18_RDY|GPIO_OUT | GPIO_DFLT_HIGH);
102
103 PRER = GPIO_bit(SPITZ_GPIO_KEY_INT);
104 PFER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
105 PWER = GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET) | PWER_RTC;
106 PKWR = GPIO_bit(SPITZ_GPIO_SYNC) | GPIO_bit(SPITZ_GPIO_KEY_INT) | GPIO_bit(SPITZ_GPIO_RESET);
107 PKSR = 0xffffffff; // clear
108
109 /* nRESET_OUT Disable */
110 PSLR |= PSLR_SL_ROD;
111
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000112 /* Stop 3.6MHz and drive HIGH to PCMCIA and CS */
113 PCFR = PCFR_GPR_EN | PCFR_OPDE;
114}
115
116static void spitz_postsuspend(void)
117{
118 pxa_gpio_mode(GPIO18_RDY_MD);
119 pxa_gpio_mode(10 | GPIO_IN);
120}
121
122static int spitz_should_wakeup(unsigned int resume_on_alarm)
123{
124 int is_resume = 0;
Richard Purdieb7557de2006-01-05 20:44:55 +0000125 int acin = sharpsl_pm.machinfo->read_devdata(SHARPSL_STATUS_ACIN);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000126
127 if (spitz_last_ac_status != acin) {
128 if (acin) {
129 /* charge on */
130 sharpsl_pm.flags |= SHARPSL_DO_OFFLINE_CHRG;
131 dev_dbg(sharpsl_pm.dev, "AC Inserted\n");
132 } else {
133 /* charge off */
134 dev_dbg(sharpsl_pm.dev, "AC Removed\n");
Richard Purdieb7557de2006-01-05 20:44:55 +0000135 sharpsl_pm_led(SHARPSL_LED_OFF);
136 sharpsl_pm.machinfo->charge(0);
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000137 sharpsl_pm.charge_mode = CHRG_OFF;
138 }
139 spitz_last_ac_status = acin;
140 /* Return to suspend as this must be what we were woken for */
141 return 0;
142 }
143
144 if (PEDR & GPIO_bit(SPITZ_GPIO_KEY_INT))
145 is_resume |= GPIO_bit(SPITZ_GPIO_KEY_INT);
146
147 if (PKSR & GPIO_bit(SPITZ_GPIO_SYNC))
148 is_resume |= GPIO_bit(SPITZ_GPIO_SYNC);
149
150 if (resume_on_alarm && (PEDR & PWER_RTC))
151 is_resume |= PWER_RTC;
152
153 dev_dbg(sharpsl_pm.dev, "is_resume: %x\n",is_resume);
154 return is_resume;
155}
156
157static unsigned long spitz_charger_wakeup(void)
158{
159 return (~GPLR0 & GPIO_bit(SPITZ_GPIO_KEY_INT)) | (GPLR0 & GPIO_bit(SPITZ_GPIO_SYNC));
160}
161
Richard Purdieb7557de2006-01-05 20:44:55 +0000162unsigned long spitzpm_read_devdata(int type)
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000163{
Richard Purdieb7557de2006-01-05 20:44:55 +0000164 switch(type) {
165 case SHARPSL_STATUS_ACIN:
166 return (((~GPLR(SPITZ_GPIO_AC_IN)) & GPIO_bit(SPITZ_GPIO_AC_IN)) != 0);
167 case SHARPSL_STATUS_LOCK:
168 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batlock);
169 case SHARPSL_STATUS_CHRGFULL:
170 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_batfull);
171 case SHARPSL_STATUS_FATAL:
172 return READ_GPIO_BIT(sharpsl_pm.machinfo->gpio_fatal);
173 case SHARPSL_ACIN_VOLT:
174 return sharpsl_pm_pxa_read_max1111(MAX1111_ACIN_VOLT);
175 case SHARPSL_BATT_TEMP:
176 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_TEMP);
177 case SHARPSL_BATT_VOLT:
178 default:
179 return sharpsl_pm_pxa_read_max1111(MAX1111_BATT_VOLT);
180 }
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000181}
182
183struct sharpsl_charger_machinfo spitz_pm_machinfo = {
184 .init = spitz_charger_init,
Richard Purdieb7557de2006-01-05 20:44:55 +0000185 .exit = sharpsl_pm_pxa_remove,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000186 .gpio_batlock = SPITZ_GPIO_BAT_COVER,
187 .gpio_acin = SPITZ_GPIO_AC_IN,
188 .gpio_batfull = SPITZ_GPIO_CHRG_FULL,
Richard Purdief8703dc2006-06-19 19:58:52 +0100189 .batfull_irq = 1,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000190 .gpio_fatal = SPITZ_GPIO_FATAL_BAT,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000191 .discharge = spitz_discharge,
192 .discharge1 = spitz_discharge1,
193 .charge = spitz_charge,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000194 .measure_temp = spitz_measure_temp,
195 .presuspend = spitz_presuspend,
196 .postsuspend = spitz_postsuspend,
Richard Purdieb7557de2006-01-05 20:44:55 +0000197 .read_devdata = spitzpm_read_devdata,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000198 .charger_wakeup = spitz_charger_wakeup,
199 .should_wakeup = spitz_should_wakeup,
Dmitry Baryshkov5cbff962008-10-28 20:26:40 +0300200#if defined(CONFIG_LCD_CORGI)
201 .backlight_limit = corgi_lcd_limit_intensity,
202#elif defined(CONFIG_BACKLIGHT_CORGI)
Richard Purdief8703dc2006-06-19 19:58:52 +0100203 .backlight_limit = corgibl_limit_intensity,
Russell King02a8e762008-04-20 17:15:32 +0100204#endif
Richard Purdief8703dc2006-06-19 19:58:52 +0100205 .charge_on_volt = SHARPSL_CHARGE_ON_VOLT,
206 .charge_on_temp = SHARPSL_CHARGE_ON_TEMP,
207 .charge_acin_high = SHARPSL_CHARGE_ON_ACIN_HIGH,
208 .charge_acin_low = SHARPSL_CHARGE_ON_ACIN_LOW,
209 .fatal_acin_volt = SHARPSL_FATAL_ACIN_VOLT,
210 .fatal_noacin_volt= SHARPSL_FATAL_NOACIN_VOLT,
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000211 .bat_levels = 40,
212 .bat_levels_noac = spitz_battery_levels_noac,
213 .bat_levels_acin = spitz_battery_levels_acin,
214 .status_high_acin = 188,
215 .status_low_acin = 178,
216 .status_high_noac = 185,
217 .status_low_noac = 175,
218};
219
220static struct platform_device *spitzpm_device;
221
222static int __devinit spitzpm_init(void)
223{
224 int ret;
225
Dmitry Baryshkov5806d8a2008-05-31 16:17:32 +0100226 if (!machine_is_spitz() && !machine_is_akita()
227 && !machine_is_borzoi())
228 return -ENODEV;
229
Richard Purdiee8b6f7f2005-11-13 10:07:47 +0000230 spitzpm_device = platform_device_alloc("sharpsl-pm", -1);
231 if (!spitzpm_device)
232 return -ENOMEM;
233
234 spitzpm_device->dev.platform_data = &spitz_pm_machinfo;
235 ret = platform_device_add(spitzpm_device);
236
237 if (ret)
238 platform_device_put(spitzpm_device);
239
240 return ret;
241}
242
243static void spitzpm_exit(void)
244{
245 platform_device_unregister(spitzpm_device);
246}
247
248module_init(spitzpm_init);
249module_exit(spitzpm_exit);