1 From 2bab304b10f077cb88d5b5f5bd7e50c16aac2132 Mon Sep 17 00:00:00 2001
2 From: mokopatches <mokopatches@openmoko.org>
3 Date: Fri, 25 Jul 2008 22:21:23 +0100
4 Subject: [PATCH] bq27000-battery-driver.patch
7 arch/arm/mach-s3c2440/Kconfig | 2 +
8 arch/arm/mach-s3c2440/mach-gta02.c | 21 ++
9 drivers/power/Kconfig | 7 +-
10 drivers/power/Makefile | 1 +
11 drivers/power/bq27000_battery.c | 375 ++++++++++++++++++++++++++++++++++++
12 include/linux/bq27000_battery.h | 12 ++
13 6 files changed, 417 insertions(+), 1 deletions(-)
14 create mode 100644 drivers/power/bq27000_battery.c
15 create mode 100644 include/linux/bq27000_battery.h
17 diff --git a/arch/arm/mach-s3c2440/Kconfig b/arch/arm/mach-s3c2440/Kconfig
18 index 3015aaf..b417f8e 100644
19 --- a/arch/arm/mach-s3c2440/Kconfig
20 +++ b/arch/arm/mach-s3c2440/Kconfig
21 @@ -79,6 +79,8 @@ config MACH_NEO1973_GTA02
22 bool "FIC Neo1973 GSM Phone (GTA02 Hardware)"
24 select SENSORS_PCF50633
28 Say Y here if you are using the FIC Neo1973 GSM Phone
30 diff --git a/arch/arm/mach-s3c2440/mach-gta02.c b/arch/arm/mach-s3c2440/mach-gta02.c
31 index cea76e7..3816ea5 100644
32 --- a/arch/arm/mach-s3c2440/mach-gta02.c
33 +++ b/arch/arm/mach-s3c2440/mach-gta02.c
36 #include <asm/arch/fiq_ipc_gta02.h>
37 #include "fiq_c_isr.h"
38 +#include <linux/gta02_hdq.h>
39 +#include <linux/bq27000_battery.h>
41 /* arbitrates which sensor IRQ owns the shared SPI bus */
42 static spinlock_t motion_irq_lock;
43 @@ -583,6 +585,24 @@ struct platform_device gta02_hdq_device = {
47 +/* BQ27000 Battery */
49 +struct bq27000_platform_data bq27000_pdata = {
52 + .hdq_read = gta02hdq_read,
53 + .hdq_write = gta02hdq_write,
54 + .hdq_initialized = gta02hdq_initialized,
57 +struct platform_device bq27000_battery_device = {
58 + .name = "bq27000-battery",
60 + .platform_data = &bq27000_pdata,
67 #define GTA02_FLASH_BASE 0x18000000 /* GCS3 */
68 @@ -1271,6 +1291,7 @@ static void __init gta02_machine_init(void)
69 case GTA02v5_SYSTEM_REV:
70 case GTA02v6_SYSTEM_REV:
71 platform_device_register(>a02_hdq_device);
72 + platform_device_register(&bq27000_battery_device);
76 diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
77 index 0a4515d..feb8b0a 100644
78 --- a/drivers/power/Kconfig
79 +++ b/drivers/power/Kconfig
80 @@ -49,7 +49,10 @@ config BATTERY_OLPC
82 Say Y to enable support for the battery on the OLPC laptop.
85 +config BATTERY_BQ27000_HDQ
86 + tristate "BQ27000 HDQ battery monitor driver"
88 + Say Y to enable support for the battery on the Neo Freerunner
91 tristate "Neo Freerunner HDQ"
92 @@ -58,3 +61,5 @@ config GTA02_HDQ
93 Say Y to enable support for communicating with an HDQ battery
94 on the Neo Freerunner. You probably want to select
95 at least BATTERY_BQ27000_HDQ as well
98 diff --git a/drivers/power/Makefile b/drivers/power/Makefile
99 index 88f227f..d7e87ad 100644
100 --- a/drivers/power/Makefile
101 +++ b/drivers/power/Makefile
102 @@ -20,5 +20,6 @@ obj-$(CONFIG_APM_POWER) += apm_power.o
103 obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
104 obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o
105 obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o
106 +obj-$(CONFIG_BATTERY_BQ27000_HDQ) += bq27000_battery.o
108 obj-$(CONFIG_GTA02_HDQ) += gta02_hdq.o
109 diff --git a/drivers/power/bq27000_battery.c b/drivers/power/bq27000_battery.c
111 index 0000000..d19186a
113 +++ b/drivers/power/bq27000_battery.c
116 + * Driver for batteries with bq27000 chips inside via HDQ
118 + * Copyright 2008 Openmoko, Inc
119 + * Andy Green <andy@openmoko.com>
121 + * based on ds2760 driver, original copyright notice for that --->
123 + * Copyright © 2007 Anton Vorontsov
124 + * 2004-2007 Matt Reimer
125 + * 2004 Szabolcs Gyurko
127 + * Use consistent with the GNU GPL is permitted,
128 + * provided that this copyright notice is
129 + * preserved in its entirety in all copies and derived works.
131 + * Author: Anton Vorontsov <cbou@mail.ru>
134 + * Matt Reimer <mreimer@vpop.net>
135 + * April 2004, 2005, 2007
137 + * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
141 +#include <linux/module.h>
142 +#include <linux/param.h>
143 +#include <linux/jiffies.h>
144 +#include <linux/delay.h>
145 +#include <linux/pm.h>
146 +#include <linux/platform_device.h>
147 +#include <linux/power_supply.h>
148 +#include <linux/bq27000_battery.h>
152 + /* read-write after this */
153 + BQ27000_CTRL = 0, /* Device Control Register */
154 + BQ27000_MODE, /* Device Mode Register */
155 + BQ27000_AR_L, /* At-Rate H L */
157 + /* read-only after this */
158 + BQ27000_ARTTE_L, /* At-Rate Time To Empty H L */
160 + BQ27000_TEMP_L, /* Reported Temperature H L */
162 + BQ27000_VOLT_L, /* Reported Voltage H L */
164 + BQ27000_FLAGS, /* Status Flags */
165 + BQ27000_RSOC, /* Relative State of Charge */
166 + BQ27000_NAC_L, /* Nominal Available Capacity H L */
168 + BQ27000_CACD_L, /* Discharge Compensated H L */
170 + BQ27000_CACT_L, /* Temperature Compensated H L */
172 + BQ27000_LMD_L, /* Last measured discharge H L */
174 + BQ27000_AI_L, /* Average Current H L */
176 + BQ27000_TTE_L, /* Time to Empty H L */
178 + BQ27000_TTF_L, /* Time to Full H L */
180 + BQ27000_SI_L, /* Standby Current H L */
182 + BQ27000_STTE_L, /* Standby Time To Empty H L */
184 + BQ27000_MLI_L, /* Max Load Current H L */
186 + BQ27000_MLTTE_L, /* Max Load Time To Empty H L */
188 + BQ27000_SAE_L, /* Available Energy H L */
190 + BQ27000_AP_L, /* Available Power H L */
192 + BQ27000_TTECP_L, /* Time to Empty at Constant Power H L */
194 + BQ27000_CYCL_L, /* Cycle count since learning cycle H L */
196 + BQ27000_CYCT_L, /* Cycle Count Total H L */
198 + BQ27000_CSOC, /* Compensated State Of Charge */
200 + /* read-write after this */
201 + BQ27000_EE_EE_EN = 0x6e, /* EEPROM Program Enable */
202 + BQ27000_EE_ILMD = 0x76, /* Initial Last Measured Discharge High Byte */
203 + BQ27000_EE_SEDVF, /* Scaled EDVF Threshold */
204 + BQ27000_EE_SEDV1, /* Scaled EDV1 Threshold */
205 + BQ27000_EE_ISLC, /* Initial Standby Load Current */
206 + BQ27000_EE_DMFSD, /* Digital Magnitude Filter and Self Discharge */
207 + BQ27000_EE_TAPER, /* Aging Estimate Enable, Charge Termination Taper */
208 + BQ27000_EE_PKCFG, /* Pack Configuration Values */
209 + BQ27000_EE_IMLC, /* Initial Max Load Current or ID #3 */
210 + BQ27000_EE_DCOMP, /* Discharge rate compensation constants or ID #2 */
211 + BQ27000_EE_TCOMP, /* Temperature Compensation constants or ID #1 */
214 +enum bq27000_status_flags {
215 + BQ27000_STATUS_CHGS = 0x80, /* 1 = being charged */
216 + BQ27000_STATUS_NOACT = 0x40, /* 1 = no activity */
217 + BQ27000_STATUS_IMIN = 0x20, /* 1 = Lion taper current mode */
218 + BQ27000_STATUS_CI = 0x10, /* 1 = capacity likely innacurate */
219 + BQ27000_STATUS_CALIP = 0x08, /* 1 = calibration in progress */
220 + BQ27000_STATUS_VDQ = 0x04, /* 1 = capacity should be accurate */
221 + BQ27000_STATUS_EDV1 = 0x02, /* 1 = end of discharge.. <6% left */
222 + BQ27000_STATUS_EDVF = 0x01, /* 1 = no, it's really empty now */
225 +#define NANOVOLTS_UNIT 3750
227 +struct bq27000_device_info {
228 + struct device *dev;
229 + struct power_supply bat;
231 + int rsense_mohms; /* from platform */
233 + int (*hdq_initialized)(void); /* from platform */
234 + int (*hdq_read)(int); /* from platform */
235 + int (*hdq_write)(int, u8); /* from platform */
239 + * reading 16 bit values over HDQ has a special hazard where the
240 + * hdq device firmware can update the 16-bit register during the time we
241 + * read the two halves. TI document SLUS556D recommends the algorithm here
245 +static int hdq_read16(struct bq27000_device_info *di, int address)
251 + while (retries--) {
253 + high = (di->hdq_read)(address + 1); /* high part */
257 + acc = (di->hdq_read)(address);
261 + /* confirm high didn't change between reading it and low */
262 + if (high == (di->hdq_read)(address + 1))
263 + return (high << 8) | acc;
269 +#define to_bq27000_device_info(x) container_of((x), \
270 + struct bq27000_device_info, \
273 +static void bq27000_battery_external_power_changed(struct power_supply *psy)
275 + struct bq27000_device_info *di = to_bq27000_device_info(psy);
277 + dev_dbg(di->dev, "%s\n", __FUNCTION__);
280 +static int bq27000_battery_get_property(struct power_supply *psy,
281 + enum power_supply_property psp,
282 + union power_supply_propval *val)
285 + struct bq27000_device_info *di = to_bq27000_device_info(psy);
287 + if (!(di->hdq_initialized)())
291 + case POWER_SUPPLY_PROP_STATUS:
292 + val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
293 + v = hdq_read16(di, BQ27000_AI_L);
296 + if (v < 2) { /* no real activity on the battery */
297 + if (!hdq_read16(di, BQ27000_TTF_L))
298 + val->intval = POWER_SUPPLY_STATUS_FULL;
300 + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
303 + /* power is actually going in or out... */
304 + v = (di->hdq_read)(BQ27000_FLAGS);
307 + if (v & BQ27000_STATUS_CHGS)
308 + val->intval = POWER_SUPPLY_STATUS_CHARGING;
310 + val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
312 + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
313 + v = hdq_read16(di, BQ27000_VOLT_L);
316 + val->intval = v * 1000; /* mV -> uV */
318 + case POWER_SUPPLY_PROP_CURRENT_NOW:
319 + v = (di->hdq_read)(BQ27000_FLAGS);
322 + if (v & BQ27000_STATUS_CHGS)
323 + n = -NANOVOLTS_UNIT;
325 + n = NANOVOLTS_UNIT;
326 + v = hdq_read16(di, BQ27000_AI_L);
329 + val->intval = (v * n) / di->rsense_mohms;
331 + case POWER_SUPPLY_PROP_CHARGE_FULL:
332 + v = hdq_read16(di, BQ27000_LMD_L);
335 + val->intval = (v * 3570) / di->rsense_mohms;
337 + case POWER_SUPPLY_PROP_TEMP:
338 + v = hdq_read16(di, BQ27000_TEMP_L);
341 + /* K (in 0.25K units) is 273.15 up from C (in 0.1C)*/
342 + /* 10926 = 27315 * 4 / 10 */
343 + val->intval = (((long)v * 10l) - 10926) / 4;
345 + case POWER_SUPPLY_PROP_TECHNOLOGY:
346 + val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
348 + case POWER_SUPPLY_PROP_CAPACITY:
349 + val->intval = (di->hdq_read)(BQ27000_RSOC);
350 + if (val->intval < 0)
351 + return val->intval;
353 + case POWER_SUPPLY_PROP_PRESENT:
354 + v = (di->hdq_read)(BQ27000_RSOC);
355 + val->intval = !(v < 0);
357 + case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
358 + v = hdq_read16(di, BQ27000_TTE_L);
361 + val->intval = 60 * v;
363 + case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
364 + v = hdq_read16(di, BQ27000_TTF_L);
367 + val->intval = 60 * v;
376 +static enum power_supply_property bq27000_battery_props[] = {
377 + POWER_SUPPLY_PROP_STATUS,
378 + POWER_SUPPLY_PROP_VOLTAGE_NOW,
379 + POWER_SUPPLY_PROP_CURRENT_NOW,
380 + POWER_SUPPLY_PROP_CHARGE_FULL,
381 + POWER_SUPPLY_PROP_TEMP,
382 + POWER_SUPPLY_PROP_TECHNOLOGY,
383 + POWER_SUPPLY_PROP_PRESENT,
384 + POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
385 + POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
386 + POWER_SUPPLY_PROP_MODEL_NAME,
387 + POWER_SUPPLY_PROP_CAPACITY
390 +static int bq27000_battery_probe(struct platform_device *pdev)
393 + struct bq27000_device_info *di;
394 + struct bq27000_platform_data *pdata;
396 + dev_info(&pdev->dev, "BQ27000 Battery Driver (C) 2008 Openmoko, Inc\n");
398 + di = kzalloc(sizeof(*di), GFP_KERNEL);
401 + goto di_alloc_failed;
404 + platform_set_drvdata(pdev, di);
406 + pdata = pdev->dev.platform_data;
407 + di->dev = &pdev->dev;
408 + /* di->w1_dev = pdev->dev.parent; */
409 + di->bat.name = pdata->name;
410 + di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
411 + di->bat.properties = bq27000_battery_props;
412 + di->bat.num_properties = ARRAY_SIZE(bq27000_battery_props);
413 + di->bat.get_property = bq27000_battery_get_property;
414 + di->bat.external_power_changed =
415 + bq27000_battery_external_power_changed;
416 + di->hdq_read = pdata->hdq_read;
417 + di->hdq_write = pdata->hdq_write;
418 + di->rsense_mohms = pdata->rsense_mohms;
419 + di->hdq_initialized = pdata->hdq_initialized;
421 + retval = power_supply_register(&pdev->dev, &di->bat);
423 + dev_err(di->dev, "failed to register battery\n");
435 +static int bq27000_battery_remove(struct platform_device *pdev)
437 + struct bq27000_device_info *di = platform_get_drvdata(pdev);
439 + power_supply_unregister(&di->bat);
446 +static int bq27000_battery_suspend(struct platform_device *pdev,
447 + pm_message_t state)
452 +static int bq27000_battery_resume(struct platform_device *pdev)
459 +#define bq27000_battery_suspend NULL
460 +#define bq27000_battery_resume NULL
462 +#endif /* CONFIG_PM */
464 +static struct platform_driver bq27000_battery_driver = {
466 + .name = "bq27000-battery",
468 + .probe = bq27000_battery_probe,
469 + .remove = bq27000_battery_remove,
470 + .suspend = bq27000_battery_suspend,
471 + .resume = bq27000_battery_resume,
474 +static int __init bq27000_battery_init(void)
476 + return platform_driver_register(&bq27000_battery_driver);
479 +static void __exit bq27000_battery_exit(void)
481 + platform_driver_unregister(&bq27000_battery_driver);
484 +module_init(bq27000_battery_init);
485 +module_exit(bq27000_battery_exit);
487 +MODULE_LICENSE("GPL");
488 +MODULE_AUTHOR("Andy Green <andy@openmoko.com>");
489 +MODULE_DESCRIPTION("bq27000 battery driver");
490 diff --git a/include/linux/bq27000_battery.h b/include/linux/bq27000_battery.h
492 index 0000000..36b4f20
494 +++ b/include/linux/bq27000_battery.h
496 +#ifndef __BQ27000_BATTERY_H__
497 +#define __BQ27000_BATTERY_H__
499 +struct bq27000_platform_data {
502 + int (*hdq_read)(int);
503 + int (*hdq_write)(int, u8);
504 + int (*hdq_initialized)(void);