1/*
2 * Based on spitz_pm.c and sharp code.
3 *
4 * Copyright (C) 2001  SHARP
5 * Copyright 2005 Pavel Machek <pavel@suse.cz>
6 *
7 * Distribute under GPLv2.
8 *
9 * Li-ion batteries are angry beasts, and they like to explode. This driver is not finished,
10 * and sometimes charges them when it should not. If it makes angry lithium to come your way...
11 * ...well, you have been warned.
12 *
13 * Actually, this should be quite safe, it seems sharp leaves charger enabled by default,
14 * and my collie did not explode (yet).
15 */
16
17#include <linux/module.h>
18#include <linux/stat.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/delay.h>
22#include <linux/interrupt.h>
23#include <linux/device.h>
24#include <linux/platform_device.h>
25
26#include <asm/irq.h>
27#include <asm/mach-types.h>
28#include <asm/hardware.h>
29#include <asm/hardware/scoop.h>
30#include <asm/dma.h>
31#include <asm/arch/collie.h>
32#include <asm/mach/sharpsl_param.h>
33#include <asm/hardware/sharpsl_pm.h>
34
35#include "../drivers/mfd/ucb1x00.h"
36
37static struct ucb1x00 *ucb;
38static int ad_revise;
39
40#define ADCtoPower(x)	       ((330 * x * 2) / 1024)
41
42static void collie_charger_init(void)
43{
44	int err;
45
46	if (sharpsl_param.adadj != -1)
47		ad_revise = sharpsl_param.adadj;
48
49	/* Register interrupt handler. */
50	if ((err = request_irq(COLLIE_IRQ_GPIO_AC_IN, sharpsl_ac_isr, IRQF_DISABLED,
51			       "ACIN", sharpsl_ac_isr))) {
52		printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_AC_IN);
53		return;
54	}
55	if ((err = request_irq(COLLIE_IRQ_GPIO_CO, sharpsl_chrg_full_isr, IRQF_DISABLED,
56			       "CO", sharpsl_chrg_full_isr))) {
57		free_irq(COLLIE_IRQ_GPIO_AC_IN, sharpsl_ac_isr);
58		printk("Could not get irq %d.\n", COLLIE_IRQ_GPIO_CO);
59		return;
60	}
61
62	ucb1x00_io_set_dir(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON | COLLIE_TC35143_GPIO_TMP_ON |
63			           COLLIE_TC35143_GPIO_BBAT_ON);
64	return;
65}
66
67static void collie_measure_temp(int on)
68{
69	if (on)
70		ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
71	else
72		ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
73}
74
75static void collie_charge(int on)
76{
77	extern struct platform_device colliescoop_device;
78
79	/* Zaurus seems to contain LTC1731; it should know when to
80	 * stop charging itself, so setting charge on should be
81	 * relatively harmless (as long as it is not done too often).
82	 */
83	if (on) {
84		set_scoop_gpio(&colliescoop_device.dev, COLLIE_SCP_CHARGE_ON);
85	} else {
86		reset_scoop_gpio(&colliescoop_device.dev, COLLIE_SCP_CHARGE_ON);
87	}
88}
89
90static void collie_discharge(int on)
91{
92}
93
94static void collie_discharge1(int on)
95{
96}
97
98static void collie_presuspend(void)
99{
100}
101
102static void collie_postsuspend(void)
103{
104}
105
106static int collie_should_wakeup(unsigned int resume_on_alarm)
107{
108	return 0;
109}
110
111static unsigned long collie_charger_wakeup(void)
112{
113	return 0;
114}
115
116int collie_read_backup_battery(void)
117{
118	int voltage;
119
120	ucb1x00_adc_enable(ucb);
121
122	ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_BBAT_ON, 0);
123	voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
124
125	ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
126	ucb1x00_adc_disable(ucb);
127
128	printk("Backup battery = %d(%d)\n", ADCtoPower(voltage), voltage);
129
130	return ADCtoPower(voltage);
131}
132
133int collie_read_main_battery(void)
134{
135	int voltage, voltage_rev, voltage_volts;
136
137	ucb1x00_adc_enable(ucb);
138	ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_BBAT_ON);
139	ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_MBAT_ON, 0);
140
141	mdelay(1);
142	voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD1, UCB_SYNC);
143
144	ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_MBAT_ON);
145	ucb1x00_adc_disable(ucb);
146
147	voltage_rev = voltage + ((ad_revise * voltage) / 652);
148	voltage_volts = ADCtoPower(voltage_rev);
149
150	printk("Main battery = %d(%d)\n", voltage_volts, voltage);
151
152	if (voltage != -1)
153		return voltage_volts;
154	else
155		return voltage;
156}
157
158int collie_read_temp(void)
159{
160	int voltage;
161
162
163	ucb1x00_adc_enable(ucb);
164	ucb1x00_io_write(ucb, COLLIE_TC35143_GPIO_TMP_ON, 0);
165	/* >1010 = battery removed, 460 = 22C ?, higer = lower temp ? */
166	voltage = ucb1x00_adc_read(ucb, UCB_ADC_INP_AD0, UCB_SYNC);
167	ucb1x00_io_write(ucb, 0, COLLIE_TC35143_GPIO_TMP_ON);
168	ucb1x00_adc_disable(ucb);
169
170	printk("Battery temp = %d\n", voltage);
171	return voltage;
172}
173
174static unsigned long read_devdata(int which)
175{
176	switch (which) {
177	case SHARPSL_BATT_VOLT:
178		return collie_read_main_battery();
179	case SHARPSL_BATT_TEMP:
180		return collie_read_temp();
181	case SHARPSL_ACIN_VOLT:
182		return 500;
183	case SHARPSL_STATUS_ACIN: {
184		int ret = GPLR & COLLIE_GPIO_AC_IN;
185		printk("AC status = %d\n", ret);
186		return ret;
187	}
188	case SHARPSL_STATUS_FATAL: {
189		int ret = GPLR & COLLIE_GPIO_MAIN_BAT_LOW;
190		printk("Fatal bat = %d\n", ret);
191		return ret;
192	}
193	default:
194		return ~0;
195	}
196}
197
198struct battery_thresh collie_battery_levels_acin[] = {
199	{ 420, 100},
200	{ 417,  95},
201	{ 415,  90},
202	{ 413,  80},
203	{ 411,  75},
204	{ 408,  70},
205	{ 406,  60},
206	{ 403,  50},
207	{ 398,  40},
208	{ 391,  25},
209	{  10,   5},
210	{   0,   0},
211};
212
213struct battery_thresh collie_battery_levels[] = {
214	{ 394, 100},
215	{ 390,  95},
216	{ 380,  90},
217	{ 370,  80},
218	{ 368,  75},	/* From sharp code: battery high with frontlight */
219	{ 366,  70},	/* 60..90 -- fake values invented by me for testing */
220	{ 364,  60},
221	{ 362,  50},
222	{ 360,  40},
223	{ 358,  25},	/* From sharp code: battery low with frontlight */
224	{ 356,   5},	/* From sharp code: battery verylow with frontlight */
225	{   0,   0},
226};
227
228struct sharpsl_charger_machinfo collie_pm_machinfo = {
229	.init             = collie_charger_init,
230	.read_devdata	  = read_devdata,
231	.discharge        = collie_discharge,
232	.discharge1       = collie_discharge1,
233	.charge           = collie_charge,
234	.measure_temp     = collie_measure_temp,
235	.presuspend       = collie_presuspend,
236	.postsuspend      = collie_postsuspend,
237	.charger_wakeup   = collie_charger_wakeup,
238	.should_wakeup    = collie_should_wakeup,
239	.bat_levels       = 12,
240	.bat_levels_noac  = collie_battery_levels,
241	.bat_levels_acin  = collie_battery_levels_acin,
242	.status_high_acin = 368,
243	.status_low_acin  = 358,
244	.status_high_noac = 368,
245	.status_low_noac  = 358,
246	.charge_on_volt	  = 350,	/* spitz uses 2.90V, but lets play it safe. */
247	.charge_on_temp   = 550,
248	.charge_acin_high = 550,	/* collie does not seem to have sensor for this, anyway */
249	.charge_acin_low  = 450,	/* ignored, too */
250	.fatal_acin_volt  = 356,
251	.fatal_noacin_volt = 356,
252
253	.batfull_irq = 1,		/* We do not want periodical charge restarts */
254};
255
256static int __init collie_pm_ucb_add(struct ucb1x00_dev *pdev)
257{
258	sharpsl_pm.machinfo = &collie_pm_machinfo;
259	ucb = pdev->ucb;
260	return 0;
261}
262
263static struct ucb1x00_driver collie_pm_ucb_driver = {
264	.add            = collie_pm_ucb_add,
265};
266
267static struct platform_device *collie_pm_device;
268
269static int __init collie_pm_init(void)
270{
271        int ret;
272
273        collie_pm_device = platform_device_alloc("sharpsl-pm", -1);
274        if (!collie_pm_device)
275                return -ENOMEM;
276
277        collie_pm_device->dev.platform_data = &collie_pm_machinfo;
278        ret = platform_device_add(collie_pm_device);
279
280        if (ret)
281                platform_device_put(collie_pm_device);
282
283	if (!ret)
284		ret = ucb1x00_register_driver(&collie_pm_ucb_driver);
285
286	return ret;
287}
288
289static void __exit collie_pm_exit(void)
290{
291	ucb1x00_unregister_driver(&collie_pm_ucb_driver);
292        platform_device_unregister(collie_pm_device);
293}
294
295module_init(collie_pm_init);
296module_exit(collie_pm_exit);
297