[ar71xx] remove duplicated includes
[openwrt.git] / target / linux / s3c24xx / patches-2.6.24 / 1057-bq27000-battery-driver.patch.patch
1 From 40d5d4e980e0d4a5d00759f7fa2a18488f95a0ef Mon Sep 17 00:00:00 2001
2 From: mokopatches <mokopatches@openmoko.org>
3 Date: Sun, 13 Apr 2008 07:23:52 +0100
4 Subject: [PATCH] bq27000-battery-driver.patch
5
6 ---
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
16
17 diff --git a/arch/arm/mach-s3c2440/Kconfig b/arch/arm/mach-s3c2440/Kconfig
18 index f7bea5d..71719ac 100644
19 --- a/arch/arm/mach-s3c2440/Kconfig
20 +++ b/arch/arm/mach-s3c2440/Kconfig
21 @@ -85,6 +85,8 @@ config MACH_NEO1973_GTA02
22 bool "FIC Neo1973 GSM Phone (GTA02 Hardware)"
23 select CPU_S3C2442
24 select SENSORS_PCF50633
25 + select POWER_SUPPLY
26 + select GTA02_HDQ
27 help
28 Say Y here if you are using the FIC Neo1973 GSM Phone
29
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
34 @@ -81,6 +81,8 @@
35
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>
40
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 = {
44 };
45 #endif
46
47 +/* BQ27000 Battery */
48 +
49 +struct bq27000_platform_data bq27000_pdata = {
50 + .name = "bat",
51 + .rsense_mohms = 20,
52 + .hdq_read = gta02hdq_read,
53 + .hdq_write = gta02hdq_write,
54 + .hdq_initialized = gta02hdq_initialized,
55 +};
56 +
57 +struct platform_device bq27000_battery_device = {
58 + .name = "bq27000-battery",
59 + .dev = {
60 + .platform_data = &bq27000_pdata,
61 + },
62 +};
63 +
64 +
65 /* NOR Flash */
66
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(&gta02_hdq_device);
72 + platform_device_register(&bq27000_battery_device);
73 break;
74 default:
75 break;
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
81 help
82 Say Y to enable support for the battery on the OLPC laptop.
83
84 -endif # POWER_SUPPLY
85 +config BATTERY_BQ27000_HDQ
86 + tristate "BQ27000 HDQ battery monitor driver"
87 + help
88 + Say Y to enable support for the battery on the Neo Freerunner
89
90 config GTA02_HDQ
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
96 +
97 +endif # POWER_SUPPLY
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
107
108 obj-$(CONFIG_GTA02_HDQ) += gta02_hdq.o
109 diff --git a/drivers/power/bq27000_battery.c b/drivers/power/bq27000_battery.c
110 new file mode 100644
111 index 0000000..d19186a
112 --- /dev/null
113 +++ b/drivers/power/bq27000_battery.c
114 @@ -0,0 +1,375 @@
115 +/*
116 + * Driver for batteries with bq27000 chips inside via HDQ
117 + *
118 + * Copyright 2008 Openmoko, Inc
119 + * Andy Green <andy@openmoko.com>
120 + *
121 + * based on ds2760 driver, original copyright notice for that --->
122 + *
123 + * Copyright © 2007 Anton Vorontsov
124 + * 2004-2007 Matt Reimer
125 + * 2004 Szabolcs Gyurko
126 + *
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.
130 + *
131 + * Author: Anton Vorontsov <cbou@mail.ru>
132 + * February 2007
133 + *
134 + * Matt Reimer <mreimer@vpop.net>
135 + * April 2004, 2005, 2007
136 + *
137 + * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
138 + * September 2004
139 + */
140 +
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>
149 +
150 +enum bq27000_regs {
151 + /* RAM regs */
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 */
156 + BQ27000_AR_H,
157 + /* read-only after this */
158 + BQ27000_ARTTE_L, /* At-Rate Time To Empty H L */
159 + BQ27000_ARTTE_H,
160 + BQ27000_TEMP_L, /* Reported Temperature H L */
161 + BQ27000_TEMP_H,
162 + BQ27000_VOLT_L, /* Reported Voltage H L */
163 + BQ27000_VOLT_H,
164 + BQ27000_FLAGS, /* Status Flags */
165 + BQ27000_RSOC, /* Relative State of Charge */
166 + BQ27000_NAC_L, /* Nominal Available Capacity H L */
167 + BQ27000_NAC_H,
168 + BQ27000_CACD_L, /* Discharge Compensated H L */
169 + BQ27000_CACD_H,
170 + BQ27000_CACT_L, /* Temperature Compensated H L */
171 + BQ27000_CACT_H,
172 + BQ27000_LMD_L, /* Last measured discharge H L */
173 + BQ27000_LMD_H,
174 + BQ27000_AI_L, /* Average Current H L */
175 + BQ27000_AI_H,
176 + BQ27000_TTE_L, /* Time to Empty H L */
177 + BQ27000_TTE_H,
178 + BQ27000_TTF_L, /* Time to Full H L */
179 + BQ27000_TTF_H,
180 + BQ27000_SI_L, /* Standby Current H L */
181 + BQ27000_SI_H,
182 + BQ27000_STTE_L, /* Standby Time To Empty H L */
183 + BQ27000_STTE_H,
184 + BQ27000_MLI_L, /* Max Load Current H L */
185 + BQ27000_MLI_H,
186 + BQ27000_MLTTE_L, /* Max Load Time To Empty H L */
187 + BQ27000_MLTTE_H,
188 + BQ27000_SAE_L, /* Available Energy H L */
189 + BQ27000_SAE_H,
190 + BQ27000_AP_L, /* Available Power H L */
191 + BQ27000_AP_H,
192 + BQ27000_TTECP_L, /* Time to Empty at Constant Power H L */
193 + BQ27000_TTECP_H,
194 + BQ27000_CYCL_L, /* Cycle count since learning cycle H L */
195 + BQ27000_CYCL_H,
196 + BQ27000_CYCT_L, /* Cycle Count Total H L */
197 + BQ27000_CYCT_H,
198 + BQ27000_CSOC, /* Compensated State Of Charge */
199 + /* EEPROM regs */
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 */
212 +};
213 +
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 */
223 +};
224 +
225 +#define NANOVOLTS_UNIT 3750
226 +
227 +struct bq27000_device_info {
228 + struct device *dev;
229 + struct power_supply bat;
230 +
231 + int rsense_mohms; /* from platform */
232 +
233 + int (*hdq_initialized)(void); /* from platform */
234 + int (*hdq_read)(int); /* from platform */
235 + int (*hdq_write)(int, u8); /* from platform */
236 +};
237 +
238 +/*
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
242 + * to avoid trouble
243 + */
244 +
245 +static int hdq_read16(struct bq27000_device_info *di, int address)
246 +{
247 + int acc;
248 + int high;
249 + int retries = 3;
250 +
251 + while (retries--) {
252 +
253 + high = (di->hdq_read)(address + 1); /* high part */
254 +
255 + if (high < 0)
256 + return high;
257 + acc = (di->hdq_read)(address);
258 + if (acc < 0)
259 + return acc;
260 +
261 + /* confirm high didn't change between reading it and low */
262 + if (high == (di->hdq_read)(address + 1))
263 + return (high << 8) | acc;
264 + }
265 +
266 + return -ETIME;
267 +}
268 +
269 +#define to_bq27000_device_info(x) container_of((x), \
270 + struct bq27000_device_info, \
271 + bat);
272 +
273 +static void bq27000_battery_external_power_changed(struct power_supply *psy)
274 +{
275 + struct bq27000_device_info *di = to_bq27000_device_info(psy);
276 +
277 + dev_dbg(di->dev, "%s\n", __FUNCTION__);
278 +}
279 +
280 +static int bq27000_battery_get_property(struct power_supply *psy,
281 + enum power_supply_property psp,
282 + union power_supply_propval *val)
283 +{
284 + int v, n;
285 + struct bq27000_device_info *di = to_bq27000_device_info(psy);
286 +
287 + if (!(di->hdq_initialized)())
288 + return -EINVAL;
289 +
290 + switch (psp) {
291 + case POWER_SUPPLY_PROP_STATUS:
292 + val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
293 + v = hdq_read16(di, BQ27000_AI_L);
294 + if (v < 0)
295 + return v;
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;
299 + else
300 + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
301 + break;
302 + }
303 + /* power is actually going in or out... */
304 + v = (di->hdq_read)(BQ27000_FLAGS);
305 + if (v < 0)
306 + return v;
307 + if (v & BQ27000_STATUS_CHGS)
308 + val->intval = POWER_SUPPLY_STATUS_CHARGING;
309 + else
310 + val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
311 + break;
312 + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
313 + v = hdq_read16(di, BQ27000_VOLT_L);
314 + if (v < 0)
315 + return v;
316 + val->intval = v * 1000; /* mV -> uV */
317 + break;
318 + case POWER_SUPPLY_PROP_CURRENT_NOW:
319 + v = (di->hdq_read)(BQ27000_FLAGS);
320 + if (v < 0)
321 + return v;
322 + if (v & BQ27000_STATUS_CHGS)
323 + n = -NANOVOLTS_UNIT;
324 + else
325 + n = NANOVOLTS_UNIT;
326 + v = hdq_read16(di, BQ27000_AI_L);
327 + if (v < 0)
328 + return v;
329 + val->intval = (v * n) / di->rsense_mohms;
330 + break;
331 + case POWER_SUPPLY_PROP_CHARGE_FULL:
332 + v = hdq_read16(di, BQ27000_LMD_L);
333 + if (v < 0)
334 + return v;
335 + val->intval = (v * 3570) / di->rsense_mohms;
336 + break;
337 + case POWER_SUPPLY_PROP_TEMP:
338 + v = hdq_read16(di, BQ27000_TEMP_L);
339 + if (v < 0)
340 + return v;
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;
344 + break;
345 + case POWER_SUPPLY_PROP_TECHNOLOGY:
346 + val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
347 + break;
348 + case POWER_SUPPLY_PROP_CAPACITY:
349 + val->intval = (di->hdq_read)(BQ27000_RSOC);
350 + if (val->intval < 0)
351 + return val->intval;
352 + break;
353 + case POWER_SUPPLY_PROP_PRESENT:
354 + v = (di->hdq_read)(BQ27000_RSOC);
355 + val->intval = !(v < 0);
356 + break;
357 + case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
358 + v = hdq_read16(di, BQ27000_TTE_L);
359 + if (v < 0)
360 + return v;
361 + val->intval = 60 * v;
362 + break;
363 + case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
364 + v = hdq_read16(di, BQ27000_TTF_L);
365 + if (v < 0)
366 + return v;
367 + val->intval = 60 * v;
368 + break;
369 + default:
370 + return -EINVAL;
371 + }
372 +
373 + return 0;
374 +}
375 +
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
388 +};
389 +
390 +static int bq27000_battery_probe(struct platform_device *pdev)
391 +{
392 + int retval = 0;
393 + struct bq27000_device_info *di;
394 + struct bq27000_platform_data *pdata;
395 +
396 + dev_info(&pdev->dev, "BQ27000 Battery Driver (C) 2008 Openmoko, Inc\n");
397 +
398 + di = kzalloc(sizeof(*di), GFP_KERNEL);
399 + if (!di) {
400 + retval = -ENOMEM;
401 + goto di_alloc_failed;
402 + }
403 +
404 + platform_set_drvdata(pdev, di);
405 +
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;
420 +
421 + retval = power_supply_register(&pdev->dev, &di->bat);
422 + if (retval) {
423 + dev_err(di->dev, "failed to register battery\n");
424 + goto batt_failed;
425 + }
426 +
427 + return 0;
428 +
429 +batt_failed:
430 + kfree(di);
431 +di_alloc_failed:
432 + return retval;
433 +}
434 +
435 +static int bq27000_battery_remove(struct platform_device *pdev)
436 +{
437 + struct bq27000_device_info *di = platform_get_drvdata(pdev);
438 +
439 + power_supply_unregister(&di->bat);
440 +
441 + return 0;
442 +}
443 +
444 +#ifdef CONFIG_PM
445 +
446 +static int bq27000_battery_suspend(struct platform_device *pdev,
447 + pm_message_t state)
448 +{
449 + return 0;
450 +}
451 +
452 +static int bq27000_battery_resume(struct platform_device *pdev)
453 +{
454 + return 0;
455 +}
456 +
457 +#else
458 +
459 +#define bq27000_battery_suspend NULL
460 +#define bq27000_battery_resume NULL
461 +
462 +#endif /* CONFIG_PM */
463 +
464 +static struct platform_driver bq27000_battery_driver = {
465 + .driver = {
466 + .name = "bq27000-battery",
467 + },
468 + .probe = bq27000_battery_probe,
469 + .remove = bq27000_battery_remove,
470 + .suspend = bq27000_battery_suspend,
471 + .resume = bq27000_battery_resume,
472 +};
473 +
474 +static int __init bq27000_battery_init(void)
475 +{
476 + return platform_driver_register(&bq27000_battery_driver);
477 +}
478 +
479 +static void __exit bq27000_battery_exit(void)
480 +{
481 + platform_driver_unregister(&bq27000_battery_driver);
482 +}
483 +
484 +module_init(bq27000_battery_init);
485 +module_exit(bq27000_battery_exit);
486 +
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
491 new file mode 100644
492 index 0000000..36b4f20
493 --- /dev/null
494 +++ b/include/linux/bq27000_battery.h
495 @@ -0,0 +1,12 @@
496 +#ifndef __BQ27000_BATTERY_H__
497 +#define __BQ27000_BATTERY_H__
498 +
499 +struct bq27000_platform_data {
500 + const char *name;
501 + int rsense_mohms;
502 + int (*hdq_read)(int);
503 + int (*hdq_write)(int, u8);
504 + int (*hdq_initialized)(void);
505 +};
506 +
507 +#endif
508 --
509 1.5.6.5
510
This page took 0.063686 seconds and 5 git commands to generate.