2010-11-05 16:59:39 +00:00
|
|
|
|
---
|
|
|
|
|
arch/arm/mach-omap2/board-n8x0.c | 13 +
|
|
|
|
|
drivers/cbus/Kconfig | 12 +
|
|
|
|
|
drivers/cbus/Makefile | 3
|
|
|
|
|
drivers/cbus/lipocharge.c | 63 ++++++
|
|
|
|
|
drivers/cbus/lipocharge.h | 50 ++++
|
|
|
|
|
drivers/cbus/n810bm_main.c | 397 +++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
drivers/cbus/retu.c | 4
|
|
|
|
|
drivers/cbus/retu.h | 3
|
|
|
|
|
drivers/cbus/tahvo.h | 6
|
|
|
|
|
9 files changed, 548 insertions(+), 3 deletions(-)
|
|
|
|
|
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/Kconfig
|
2010-11-05 16:59:39 +00:00
|
|
|
|
===================================================================
|
2011-02-20 14:28:40 +00:00
|
|
|
|
--- linux-2.6.37.1.orig/drivers/cbus/Kconfig 2011-02-19 20:26:01.282338290 +0100
|
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/Kconfig 2011-02-19 20:26:01.526318768 +0100
|
2011-01-28 22:45:37 +00:00
|
|
|
|
@@ -94,4 +94,12 @@
|
2010-11-05 16:59:39 +00:00
|
|
|
|
to Retu/Vilma. Detection state and events are exposed through
|
|
|
|
|
sysfs.
|
|
|
|
|
|
|
|
|
|
+config N810BM
|
|
|
|
|
+ depends on CBUS_RETU && CBUS_TAHVO
|
|
|
|
|
+ tristate "Nokia n810 battery management"
|
|
|
|
|
+ ---help---
|
|
|
|
|
+ Nokia n810 device battery management.
|
|
|
|
|
+
|
|
|
|
|
+ If unsure, say N.
|
|
|
|
|
+
|
|
|
|
|
endmenu
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/Makefile
|
2010-11-05 16:59:39 +00:00
|
|
|
|
===================================================================
|
2011-02-20 14:28:40 +00:00
|
|
|
|
--- linux-2.6.37.1.orig/drivers/cbus/Makefile 2011-02-19 20:26:01.250340850 +0100
|
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/Makefile 2011-02-19 20:26:01.526318768 +0100
|
2010-11-05 16:59:39 +00:00
|
|
|
|
@@ -12,3 +12,6 @@
|
|
|
|
|
obj-$(CONFIG_CBUS_TAHVO_USER) += tahvo-user.o
|
|
|
|
|
obj-$(CONFIG_CBUS_RETU_USER) += retu-user.o
|
|
|
|
|
obj-$(CONFIG_CBUS_RETU_HEADSET) += retu-headset.o
|
|
|
|
|
+n810bm-y += n810bm_main.o
|
|
|
|
|
+n810bm-y += lipocharge.o
|
|
|
|
|
+obj-$(CONFIG_N810BM) += n810bm.o
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/n810bm_main.c
|
2010-11-05 16:59:39 +00:00
|
|
|
|
===================================================================
|
|
|
|
|
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/n810bm_main.c 2011-02-20 15:58:42.058267135 +0100
|
|
|
|
|
@@ -0,0 +1,1582 @@
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+/*
|
|
|
|
|
+ * Nokia n810 battery management
|
|
|
|
|
+ *
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ * WARNING: This driver is based on unconfirmed documentation.
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ * It is possibly dangerous to use this software.
|
|
|
|
|
+ * Use this software at your own risk!
|
|
|
|
|
+ *
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ * Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ *
|
|
|
|
|
+ * This program is free software; you can redistribute it and/or
|
|
|
|
|
+ * modify it under the terms of the GNU General Public License
|
|
|
|
|
+ * as published by the Free Software Foundation; either version 2
|
|
|
|
|
+ * of the License, or (at your option) any later version.
|
|
|
|
|
+ *
|
|
|
|
|
+ * This program is distributed in the hope that it will be useful,
|
|
|
|
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
+ * GNU General Public License for more details.
|
|
|
|
|
+ */
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+#define DEBUG
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+#include <linux/module.h>
|
|
|
|
|
+#include <linux/device.h>
|
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
|
+#include <linux/slab.h>
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+#include <linux/mutex.h>
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+#include <linux/timer.h>
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+#include <linux/firmware.h>
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+#include <linux/bitops.h>
|
|
|
|
|
+#include <linux/workqueue.h>
|
|
|
|
|
+#include <linux/delay.h>
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+#include "cbus.h"
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+#include "retu.h"
|
|
|
|
|
+#include "tahvo.h"
|
|
|
|
|
+#include "lipocharge.h"
|
|
|
|
|
+
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+#define N810BM_PMM_BLOCK_FILENAME "n810-cal-bme-pmm.fw"
|
|
|
|
|
+#define N810BM_PMM_BLOCK_SIZE 0x600
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+#define N810BM_PMM_GROUP_SIZE 0x200
|
|
|
|
|
+#define N810BM_PMM_ELEM_SIZE 0x10
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
|
|
|
|
+#define N810BM_CHECK_INTERVAL (HZ * 2)
|
2011-02-09 20:09:49 +00:00
|
|
|
|
+#define N810BM_MIN_VOLTAGE_THRES 3200 /* Absolute minimum voltage threshold */
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/* RETU_ADC_BSI
|
|
|
|
|
+ * The battery size indicator ADC measures the resistance between
|
|
|
|
|
+ * the battery BSI pin and ground. This is used to detect the battery
|
|
|
|
|
+ * capacity, as the BSI resistor is related to capacity.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Manually measured lookup table.
|
|
|
|
|
+ * Hard to measure, thus not very accurate.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Resistance | ADC value
|
|
|
|
|
+ * ========================
|
|
|
|
|
+ * 120k | 0x3AC
|
|
|
|
|
+ * 110k | 0x37C
|
|
|
|
|
+ * 100k | 0x351
|
|
|
|
|
+ * 90k | 0x329
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+/* RETU_ADC_BATTVOLT
|
|
|
|
|
+ * Manually measured lookup table.
|
|
|
|
|
+ * Hard to measure, thus not very accurate.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Voltage | ADC value
|
|
|
|
|
+ * =====================
|
|
|
|
|
+ * 2.80V | 0x037
|
|
|
|
|
+ * 2.90V | 0x05E
|
|
|
|
|
+ * 3.00V | 0x090
|
|
|
|
|
+ * 3.10V | 0x0A4
|
|
|
|
|
+ * 3.20V | 0x0CC
|
|
|
|
|
+ * 3.30V | 0x0EF
|
|
|
|
|
+ * 3.40V | 0x115
|
|
|
|
|
+ * 3.50V | 0x136
|
|
|
|
|
+ * 3.60V | 0x15C
|
|
|
|
|
+ * 3.70V | 0x187
|
|
|
|
|
+ * 3.80V | 0x1A5
|
|
|
|
|
+ * 3.90V | 0x1C9
|
|
|
|
|
+ * 4.00V | 0x1ED
|
|
|
|
|
+ * 4.10V | 0x212
|
|
|
|
|
+ * 4.20V | 0x236
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+enum n810bm_pmm_adc_id {
|
|
|
|
|
+ N810BM_PMM_ADC_0x01 = 0x01,
|
|
|
|
|
+ N810BM_PMM_ADC_0x02 = 0x02,
|
|
|
|
|
+ N810BM_PMM_ADC_0x03 = 0x03,
|
|
|
|
|
+ N810BM_PMM_ADC_0x04 = 0x04,
|
|
|
|
|
+ N810BM_PMM_ADC_BATTEMP = 0x05,
|
|
|
|
|
+ N810BM_PMM_ADC_0x06 = 0x06,
|
|
|
|
|
+ N810BM_PMM_ADC_0x07 = 0x07,
|
|
|
|
|
+ N810BM_PMM_ADC_0x08 = 0x08,
|
|
|
|
|
+ N810BM_PMM_ADC_0x0E = 0x0E,
|
|
|
|
|
+ N810BM_PMM_ADC_0x13 = 0x13,
|
|
|
|
|
+ N810BM_PMM_ADC_0x14 = 0x14,
|
|
|
|
|
+ N810BM_PMM_ADC_0x15 = 0x15,
|
|
|
|
|
+ N810BM_PMM_ADC_0x16 = 0x16,
|
|
|
|
|
+ N810BM_PMM_ADC_0x17 = 0x17,
|
|
|
|
|
+ N810BM_PMM_ADC_0xFE = 0xFE,
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+struct n810bm_adc_calib {
|
|
|
|
|
+ enum n810bm_pmm_adc_id id;
|
|
|
|
|
+ u8 flags;
|
|
|
|
|
+ u8 adc_groupnr;
|
|
|
|
|
+ u32 field1;
|
|
|
|
|
+ u32 field2;
|
|
|
|
|
+ u16 field3;
|
|
|
|
|
+ u16 field4;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+struct n810bm_calib {
|
|
|
|
|
+ struct n810bm_adc_calib adc[25];
|
|
|
|
|
+};
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+enum n810bm_capacity {
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ N810BM_CAP_UNKNOWN = -1,
|
|
|
|
|
+ N810BM_CAP_NONE = 0,
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ N810BM_CAP_1500MAH = 1500, /* 1500 mAh battery */
|
|
|
|
|
+};
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+enum n810bm_notify_flags {
|
|
|
|
|
+ N810BM_NOTIFY_battery_charging,
|
|
|
|
|
+ N810BM_NOTIFY_charger_pwm,
|
|
|
|
|
+};
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+struct n810bm {
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ bool battery_present; /* A battery is inserted */
|
|
|
|
|
+ bool charger_present; /* The charger is connected */
|
|
|
|
|
+ enum n810bm_capacity capacity; /* The capacity of the inserted battery (if any) */
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ bool charger_enabled; /* Want to charge? */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ struct lipocharge charger; /* Charger subsystem */
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ unsigned int active_current_pwm; /* Active value of TAHVO_REG_CHGCURR */
|
|
|
|
|
+ int current_measure_enabled; /* Current measure enable refcount */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ struct platform_device *pdev;
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ struct n810bm_calib calib; /* Calibration data */
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ bool verbose_charge_log; /* Verbose charge logging */
|
|
|
|
|
+
|
|
|
|
|
+ unsigned long notify_flags;
|
|
|
|
|
+ struct work_struct notify_work;
|
|
|
|
|
+ struct work_struct currmeas_irq_work;
|
|
|
|
|
+ struct delayed_work periodic_check_work;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ bool initialized; /* The hardware was initialized */
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ struct mutex mutex;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+};
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static void n810bm_notify_battery_charging(struct n810bm *bm);
|
|
|
|
|
+static void n810bm_notify_charger_pwm(struct n810bm *bm);
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+static inline struct n810bm * device_to_n810bm(struct device *dev)
|
|
|
|
|
+{
|
|
|
|
|
+ struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
|
+ struct n810bm *bm = platform_get_drvdata(pdev);
|
|
|
|
|
+
|
|
|
|
|
+ return bm;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static inline bool n810bm_known_battery_present(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ return bm->battery_present &&
|
|
|
|
|
+ bm->capacity != N810BM_CAP_UNKNOWN &&
|
|
|
|
|
+ bm->capacity != N810BM_CAP_NONE;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+static NORET_TYPE void n810bm_emergency(struct n810bm *bm, const char *message) ATTRIB_NORET;
|
|
|
|
|
+static void n810bm_emergency(struct n810bm *bm, const char *message)
|
|
|
|
|
+{
|
|
|
|
|
+ printk(KERN_EMERG "n810 battery management fatal fault: %s\n", message);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ cbus_emergency();
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+static u16 tahvo_read(struct n810bm *bm, unsigned int reg)
|
|
|
|
|
+{
|
|
|
|
|
+ int ret;
|
|
|
|
|
+ unsigned long flags;
|
|
|
|
|
+
|
|
|
|
|
+ spin_lock_irqsave(&tahvo_lock, flags);
|
|
|
|
|
+ ret = tahvo_read_reg(reg);
|
|
|
|
|
+ spin_unlock_irqrestore(&tahvo_lock, flags);
|
|
|
|
|
+ if (ret < 0 || ret > 0xFFFF)
|
|
|
|
|
+ n810bm_emergency(bm, "tahvo_read");
|
|
|
|
|
+
|
|
|
|
|
+ return ret;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void tahvo_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
|
|
|
|
|
+{
|
|
|
|
|
+ int ret;
|
|
|
|
|
+ unsigned long flags;
|
|
|
|
|
+ u16 value;
|
|
|
|
|
+
|
|
|
|
|
+ spin_lock_irqsave(&tahvo_lock, flags);
|
|
|
|
|
+ if (~mask) {
|
|
|
|
|
+ ret = tahvo_read_reg(reg);
|
|
|
|
|
+ if (ret < 0 || ret > 0xFFFF)
|
|
|
|
|
+ goto fatal_unlock;
|
|
|
|
|
+ value = ret;
|
|
|
|
|
+ } else
|
|
|
|
|
+ value = 0;
|
|
|
|
|
+ value &= ~mask;
|
|
|
|
|
+ value |= set;
|
|
|
|
|
+ ret = tahvo_write_reg(reg, value);
|
|
|
|
|
+ if (ret)
|
|
|
|
|
+ goto fatal_unlock;
|
|
|
|
|
+ spin_unlock_irqrestore(&tahvo_lock, flags);
|
|
|
|
|
+
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+fatal_unlock:
|
|
|
|
|
+ spin_unlock_irqrestore(&tahvo_lock, flags);
|
|
|
|
|
+ n810bm_emergency(bm, "tahvo_maskset");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static inline void tahvo_write(struct n810bm *bm, unsigned int reg, u16 value)
|
|
|
|
|
+{
|
|
|
|
|
+ tahvo_maskset(bm, reg, 0xFFFF, value);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static inline void tahvo_set(struct n810bm *bm, unsigned int reg, u16 mask)
|
|
|
|
|
+{
|
|
|
|
|
+ tahvo_maskset(bm, reg, mask, mask);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static inline void tahvo_clear(struct n810bm *bm, unsigned int reg, u16 mask)
|
|
|
|
|
+{
|
|
|
|
|
+ tahvo_maskset(bm, reg, mask, 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+static u16 retu_read(struct n810bm *bm, unsigned int reg)
|
|
|
|
|
+{
|
|
|
|
|
+ int ret;
|
|
|
|
|
+ unsigned long flags;
|
|
|
|
|
+
|
|
|
|
|
+ spin_lock_irqsave(&retu_lock, flags);
|
|
|
|
|
+ ret = retu_read_reg(reg);
|
|
|
|
|
+ spin_unlock_irqrestore(&retu_lock, flags);
|
|
|
|
|
+ if (ret < 0 || ret > 0xFFFF)
|
|
|
|
|
+ n810bm_emergency(bm, "retu_read");
|
|
|
|
|
+
|
|
|
|
|
+ return ret;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void retu_maskset(struct n810bm *bm, unsigned int reg, u16 mask, u16 set)
|
|
|
|
|
+{
|
|
|
|
|
+ int ret;
|
|
|
|
|
+ unsigned long flags;
|
|
|
|
|
+ u16 value;
|
|
|
|
|
+
|
|
|
|
|
+ spin_lock_irqsave(&retu_lock, flags);
|
|
|
|
|
+ if (~mask) {
|
|
|
|
|
+ ret = retu_read_reg(reg);
|
|
|
|
|
+ if (ret < 0 || ret > 0xFFFF)
|
|
|
|
|
+ goto fatal_unlock;
|
|
|
|
|
+ value = ret;
|
|
|
|
|
+ } else
|
|
|
|
|
+ value = 0;
|
|
|
|
|
+ value &= ~mask;
|
|
|
|
|
+ value |= set;
|
|
|
|
|
+ ret = retu_write_reg(reg, value);
|
|
|
|
|
+ if (ret)
|
|
|
|
|
+ goto fatal_unlock;
|
|
|
|
|
+ spin_unlock_irqrestore(&retu_lock, flags);
|
|
|
|
|
+
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+fatal_unlock:
|
|
|
|
|
+ spin_unlock_irqrestore(&retu_lock, flags);
|
|
|
|
|
+ n810bm_emergency(bm, "retu_maskset");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static inline void retu_write(struct n810bm *bm, unsigned int reg, u16 value)
|
|
|
|
|
+{
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ retu_maskset(bm, reg, 0xFFFF, value);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int retu_adc_average(struct n810bm *bm, unsigned int chan,
|
|
|
|
|
+ unsigned int nr_passes)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int i, value = 0;
|
|
|
|
|
+ int ret;
|
|
|
|
|
+
|
|
|
|
|
+ if (WARN_ON(!nr_passes))
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ for (i = 0; i < nr_passes; i++) {
|
|
|
|
|
+ ret = retu_read_adc(chan);
|
|
|
|
|
+ if (ret < 0)
|
|
|
|
|
+ return ret;
|
|
|
|
|
+ value += ret;
|
|
|
|
|
+ }
|
|
|
|
|
+ value /= nr_passes;
|
|
|
|
|
+
|
|
|
|
|
+ return value;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+static struct n810bm_adc_calib * n810bm_get_adc_calib(struct n810bm *bm,
|
|
|
|
|
+ enum n810bm_pmm_adc_id id)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int index = 0;
|
|
|
|
|
+ struct n810bm_adc_calib *cal;
|
|
|
|
|
+
|
|
|
|
|
+ if (id != N810BM_PMM_ADC_0xFE)
|
|
|
|
|
+ index = (unsigned int)id + 1;
|
|
|
|
|
+ if (index >= ARRAY_SIZE(bm->calib.adc))
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+
|
|
|
|
|
+ cal = &bm->calib.adc[index];
|
|
|
|
|
+ WARN_ON(cal->id && cal->id != id);
|
|
|
|
|
+
|
|
|
|
|
+ return cal;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+static int pmm_record_get(struct n810bm *bm,
|
|
|
|
|
+ const struct firmware *pmm_block,
|
|
|
|
|
+ void *buffer, size_t length,
|
|
|
|
|
+ unsigned int group, unsigned int element, unsigned int offset)
|
|
|
|
|
+{
|
|
|
|
|
+ const u8 *pmm_area = pmm_block->data;
|
|
|
|
|
+ u8 active_group_mask;
|
|
|
|
|
+
|
|
|
|
|
+ if (pmm_block->size != N810BM_PMM_BLOCK_SIZE)
|
|
|
|
|
+ return -EINVAL;
|
|
|
|
|
+ if (group >= N810BM_PMM_BLOCK_SIZE / N810BM_PMM_GROUP_SIZE)
|
|
|
|
|
+ return -EINVAL;
|
|
|
|
|
+ if (element >= N810BM_PMM_GROUP_SIZE / N810BM_PMM_ELEM_SIZE)
|
|
|
|
|
+ return -EINVAL;
|
|
|
|
|
+ if (offset >= N810BM_PMM_ELEM_SIZE || length > N810BM_PMM_ELEM_SIZE ||
|
|
|
|
|
+ length + offset > N810BM_PMM_ELEM_SIZE)
|
|
|
|
|
+ return -EINVAL;
|
|
|
|
|
+
|
|
|
|
|
+ active_group_mask = pmm_area[16];
|
|
|
|
|
+ if (!(active_group_mask & (1 << group))) {
|
|
|
|
|
+ dev_dbg(&bm->pdev->dev, "pwm_record_get: Requested group %u, "
|
|
|
|
|
+ "but group is not active", group);
|
|
|
|
|
+ return -ENOENT;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ memcpy(buffer,
|
|
|
|
|
+ pmm_area + group * N810BM_PMM_GROUP_SIZE
|
|
|
|
|
+ + element * N810BM_PMM_ELEM_SIZE
|
|
|
|
|
+ + offset,
|
|
|
|
|
+ length);
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* PMM block group 1 element */
|
|
|
|
|
+struct group1_element {
|
|
|
|
|
+ u8 id;
|
|
|
|
|
+ u8 flags;
|
|
|
|
|
+ u8 adc_groupnr;
|
|
|
|
|
+ u8 _padding;
|
|
|
|
|
+ __le32 field1;
|
|
|
|
|
+ __le32 field2;
|
|
|
|
|
+} __packed;
|
|
|
|
|
+
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+static int extract_group1_elem(struct n810bm *bm,
|
|
|
|
|
+ const struct firmware *pmm_block,
|
|
|
|
|
+ const enum n810bm_pmm_adc_id *pmm_adc_ids, size_t nr_pmm_adc_ids,
|
|
|
|
|
+ u32 field1_mask, u32 field2_mask)
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+{
|
|
|
|
|
+ struct group1_element elem;
|
|
|
|
|
+ int err;
|
|
|
|
|
+ unsigned int i, element_nr;
|
|
|
|
|
+ struct n810bm_adc_calib *adc_calib;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < nr_pmm_adc_ids; i++) {
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ element_nr = (unsigned int)(pmm_adc_ids[i]) + 3;
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+
|
|
|
|
|
+ err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
|
|
|
|
|
+ 1, element_nr, 0);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ continue;
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ adc_calib = n810bm_get_adc_calib(bm, elem.id);
|
|
|
|
|
+ if (!adc_calib) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev, "extract_group1_elem: "
|
|
|
|
|
+ "Could not get calib element for 0x%02X",
|
|
|
|
|
+ elem.id);
|
|
|
|
|
+ return -EINVAL;
|
|
|
|
|
+ }
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+
|
|
|
|
|
+ if (adc_calib->flags == elem.flags) {
|
|
|
|
|
+ adc_calib->field1 = le32_to_cpu(elem.field1) & field1_mask;
|
|
|
|
|
+ adc_calib->field2 = le32_to_cpu(elem.field2) & field2_mask;
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ } else {
|
|
|
|
|
+ dev_dbg(&bm->pdev->dev, "extract_group1_elem: "
|
|
|
|
|
+ "Not extracting fields due to flags mismatch: "
|
|
|
|
|
+ "0x%02X vs 0x%02X",
|
|
|
|
|
+ adc_calib->flags, elem.flags);
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ }
|
|
|
|
|
+ }
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int n810bm_parse_pmm_group1(struct n810bm *bm,
|
|
|
|
|
+ const struct firmware *pmm_block)
|
|
|
|
|
+{
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ struct n810bm_adc_calib *adc_calib;
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ struct group1_element elem;
|
|
|
|
|
+ int err;
|
|
|
|
|
+
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ static const enum n810bm_pmm_adc_id pmm_adc_ids_1[] = {
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ N810BM_PMM_ADC_0x01,
|
|
|
|
|
+ N810BM_PMM_ADC_0x02,
|
|
|
|
|
+ N810BM_PMM_ADC_0x13,
|
|
|
|
|
+ N810BM_PMM_ADC_0x0E,
|
|
|
|
|
+ };
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ static const enum n810bm_pmm_adc_id pmm_adc_ids_2[] = {
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ N810BM_PMM_ADC_0x04,
|
|
|
|
|
+ };
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ static const enum n810bm_pmm_adc_id pmm_adc_ids_3[] = {
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ N810BM_PMM_ADC_BATTEMP,
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ /* Parse element 2 */
|
|
|
|
|
+ err = pmm_record_get(bm, pmm_block, &elem, sizeof(elem),
|
|
|
|
|
+ 1, 2, 0);
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev,
|
|
|
|
|
+ "PMM: Failed to get group 1 / element 2");
|
|
|
|
|
+ return err;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (elem.id == N810BM_PMM_ADC_0xFE && elem.flags == 0x05) {
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ adc_calib = n810bm_get_adc_calib(bm, elem.id);
|
|
|
|
|
+ if (!adc_calib) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev,
|
|
|
|
|
+ "calib extract: Failed to get 0xFE calib");
|
|
|
|
|
+ return -EINVAL;
|
|
|
|
|
+ }
|
|
|
|
|
+ adc_calib->id = elem.id;
|
|
|
|
|
+ adc_calib->flags = elem.flags;
|
|
|
|
|
+ adc_calib->field1 = le32_to_cpu(elem.field1);
|
|
|
|
|
+ adc_calib->field2 = le32_to_cpu(elem.field2);
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ }
|
|
|
|
|
+
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ err = extract_group1_elem(bm, pmm_block,
|
|
|
|
|
+ pmm_adc_ids_1, ARRAY_SIZE(pmm_adc_ids_1),
|
|
|
|
|
+ 0xFFFFFFFF, 0xFFFFFFFF);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ return err;
|
|
|
|
|
+ err = extract_group1_elem(bm, pmm_block,
|
|
|
|
|
+ pmm_adc_ids_2, ARRAY_SIZE(pmm_adc_ids_2),
|
|
|
|
|
+ 0xFFFFFFFF, 0);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ return err;
|
|
|
|
|
+ err = extract_group1_elem(bm, pmm_block,
|
|
|
|
|
+ pmm_adc_ids_3, ARRAY_SIZE(pmm_adc_ids_3),
|
|
|
|
|
+ 0xFFFFFFFF, 0x0000FFFF);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ return err;
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int n810bm_parse_pmm_group2(struct n810bm *bm,
|
|
|
|
|
+ const struct firmware *pmm_block)
|
|
|
|
|
+{
|
|
|
|
|
+ dev_err(&bm->pdev->dev, "TODO: CAL BME PMM group 2 parser not implemented, yet");
|
|
|
|
|
+ return -EOPNOTSUPP;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void n810bm_adc_calib_set_defaults(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ struct n810bm_adc_calib *adc_calib;
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ unsigned int i;
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+
|
|
|
|
|
+ static const struct n810bm_adc_calib defaults[] = {
|
|
|
|
|
+ /* ADC group-nr 0 */
|
|
|
|
|
+ {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x06,
|
|
|
|
|
+ .flags = 0x00,
|
|
|
|
|
+ .adc_groupnr = 0,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x07,
|
|
|
|
|
+ .flags = 0x00,
|
|
|
|
|
+ .adc_groupnr = 0,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x15,
|
|
|
|
|
+ .flags = 0x00,
|
|
|
|
|
+ .adc_groupnr = 0,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x08,
|
|
|
|
|
+ .flags = 0x00,
|
|
|
|
|
+ .adc_groupnr = 0,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x16,
|
|
|
|
|
+ .flags = 0x00,
|
|
|
|
|
+ .adc_groupnr = 0,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x17,
|
|
|
|
|
+ .flags = 0x00,
|
|
|
|
|
+ .adc_groupnr = 0,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x03,
|
|
|
|
|
+ .flags = 0x00,
|
|
|
|
|
+ .adc_groupnr = 0,
|
|
|
|
|
+ },
|
|
|
|
|
+ /* ADC group-nr 1 */
|
|
|
|
|
+ {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0xFE,
|
|
|
|
|
+ .flags = 0x05,
|
|
|
|
|
+ .adc_groupnr = 1,
|
|
|
|
|
+ .field1 = (u32)-2,
|
|
|
|
|
+ .field2 = 13189,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x01,
|
|
|
|
|
+ .flags = 0x01,
|
|
|
|
|
+ .adc_groupnr = 1,
|
|
|
|
|
+ .field1 = 2527,
|
|
|
|
|
+ .field2 = 21373,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x02,
|
|
|
|
|
+ .flags = 0x01,
|
|
|
|
|
+ .adc_groupnr = 1,
|
|
|
|
|
+ .field1 = 0,
|
|
|
|
|
+ .field2 = 129848,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x13,
|
|
|
|
|
+ .flags = 0x01,
|
|
|
|
|
+ .adc_groupnr = 1,
|
|
|
|
|
+ .field1 = 0,
|
|
|
|
|
+ .field2 = 20000,
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x0E,
|
|
|
|
|
+ .flags = 0x06,
|
|
|
|
|
+ .adc_groupnr = 1,
|
|
|
|
|
+ .field1 = 0,
|
|
|
|
|
+ .field2 = 9660,
|
|
|
|
|
+ },
|
|
|
|
|
+ /* ADC group-nr 2 */
|
|
|
|
|
+ {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x04,
|
|
|
|
|
+ .flags = 0x02,
|
|
|
|
|
+ .adc_groupnr = 2,
|
|
|
|
|
+ .field1 = 1169,
|
|
|
|
|
+ .field2 = 0,
|
|
|
|
|
+ },
|
|
|
|
|
+ /* ADC group-nr 3 */
|
|
|
|
|
+ {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_BATTEMP,
|
|
|
|
|
+ .flags = 0x03,
|
|
|
|
|
+ .adc_groupnr = 3,
|
|
|
|
|
+ .field1 = 265423000,
|
|
|
|
|
+ .field2 = 298,
|
|
|
|
|
+ },
|
|
|
|
|
+ /* ADC group-nr 4 */
|
|
|
|
|
+ {
|
|
|
|
|
+ .id = N810BM_PMM_ADC_0x14,
|
|
|
|
|
+ .flags = 0x04,
|
|
|
|
|
+ .adc_groupnr = 4,
|
|
|
|
|
+ .field1 = 19533778,
|
|
|
|
|
+ .field2 = 308019670,
|
|
|
|
|
+ .field3 = 4700,
|
|
|
|
|
+ .field4 = 2500,
|
|
|
|
|
+ },
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ /* Clear the array */
|
|
|
|
|
+ memset(&bm->calib.adc, 0, sizeof(bm->calib.adc));
|
|
|
|
|
+ for (i = 0; i < ARRAY_SIZE(bm->calib.adc); i++)
|
|
|
|
|
+ bm->calib.adc[i].flags = 0xFF;
|
|
|
|
|
+
|
|
|
|
|
+ /* Copy the defaults */
|
|
|
|
|
+ for (i = 0; i < ARRAY_SIZE(defaults); i++) {
|
2011-02-20 15:04:20 +00:00
|
|
|
|
+ adc_calib = n810bm_get_adc_calib(bm, defaults[i].id);
|
|
|
|
|
+ if (WARN_ON(!adc_calib))
|
|
|
|
|
+ continue;
|
|
|
|
|
+ *adc_calib = defaults[i];
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int n810bm_parse_pmm_block(struct n810bm *bm,
|
|
|
|
|
+ const struct firmware *pmm_block)
|
|
|
|
|
+{
|
|
|
|
|
+ u8 byte;
|
|
|
|
|
+ int err;
|
|
|
|
|
+ unsigned int i, count;
|
|
|
|
|
+ struct n810bm_adc_calib *adc_calib;
|
|
|
|
|
+
|
|
|
|
|
+ /* Initialize to defaults */
|
|
|
|
|
+ n810bm_adc_calib_set_defaults(bm);
|
|
|
|
|
+
|
|
|
|
|
+ /* Parse the PMM data */
|
|
|
|
|
+ err = pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
|
|
|
|
|
+ 1, 0, 0); /* group 1 / element 0 */
|
|
|
|
|
+ err |= (byte != 0x01);
|
|
|
|
|
+ err |= pmm_record_get(bm, pmm_block, &byte, sizeof(byte),
|
|
|
|
|
+ 1, 1, 0); /* group 1 / element 1 */
|
|
|
|
|
+ err |= (byte != 0x01);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ err = n810bm_parse_pmm_group2(bm, pmm_block);
|
|
|
|
|
+ else
|
|
|
|
|
+ err = n810bm_parse_pmm_group1(bm, pmm_block);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ return err;
|
|
|
|
|
+
|
|
|
|
|
+ /* Sanity checks */
|
|
|
|
|
+ for (i = 0, count = 0; i < ARRAY_SIZE(bm->calib.adc); i++) {
|
|
|
|
|
+ adc_calib = &bm->calib.adc[i];
|
|
|
|
|
+ if (adc_calib->flags == 0xFF)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ switch (adc_calib->id) {
|
|
|
|
|
+ case N810BM_PMM_ADC_0x01:
|
|
|
|
|
+ if (adc_calib->field1 < 2400 ||
|
|
|
|
|
+ adc_calib->field1 > 2700)
|
|
|
|
|
+ goto value_check_fail;
|
|
|
|
|
+ if (adc_calib->field2 < 20000 ||
|
|
|
|
|
+ adc_calib->field2 > 23000)
|
|
|
|
|
+ goto value_check_fail;
|
|
|
|
|
+ count++;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case N810BM_PMM_ADC_0x04:
|
|
|
|
|
+ if (adc_calib->field1 < 1100 ||
|
|
|
|
|
+ adc_calib->field1 > 1300)
|
|
|
|
|
+ goto value_check_fail;
|
|
|
|
|
+ count++;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case N810BM_PMM_ADC_0x0E:
|
|
|
|
|
+ if (adc_calib->field2 < 7000 ||
|
|
|
|
|
+ adc_calib->field2 > 12000)
|
|
|
|
|
+ goto value_check_fail;
|
|
|
|
|
+ count++;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case N810BM_PMM_ADC_0xFE:
|
|
|
|
|
+ if ((s32)adc_calib->field1 > 14 ||
|
|
|
|
|
+ (s32)adc_calib->field1 < -14)
|
|
|
|
|
+ goto value_check_fail;
|
|
|
|
|
+ if (adc_calib->field2 < 13000 ||
|
|
|
|
|
+ adc_calib->field2 > 13350)
|
|
|
|
|
+ goto value_check_fail;
|
|
|
|
|
+ count++;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case N810BM_PMM_ADC_0x02:
|
|
|
|
|
+ case N810BM_PMM_ADC_BATTEMP:
|
|
|
|
|
+ case N810BM_PMM_ADC_0x13:
|
|
|
|
|
+ count++;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case N810BM_PMM_ADC_0x03:
|
|
|
|
|
+ case N810BM_PMM_ADC_0x07:
|
|
|
|
|
+ case N810BM_PMM_ADC_0x08:
|
|
|
|
|
+ case N810BM_PMM_ADC_0x06:
|
|
|
|
|
+ case N810BM_PMM_ADC_0x14:
|
|
|
|
|
+ case N810BM_PMM_ADC_0x15:
|
|
|
|
|
+ case N810BM_PMM_ADC_0x16:
|
|
|
|
|
+ case N810BM_PMM_ADC_0x17:
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ dev_dbg(&bm->pdev->dev,
|
|
|
|
|
+ "ADC 0x%02X calib: 0x%02X 0x%02X 0x%08X 0x%08X 0x%04X 0x%04X",
|
|
|
|
|
+ adc_calib->id, adc_calib->flags, adc_calib->adc_groupnr,
|
|
|
|
|
+ adc_calib->field1, adc_calib->field2,
|
|
|
|
|
+ adc_calib->field3, adc_calib->field4);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (count != 7) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev, "PMM sanity check: Did not find "
|
|
|
|
|
+ "all required values (count=%u)", count);
|
|
|
|
|
+ goto check_fail;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+
|
|
|
|
|
+value_check_fail:
|
|
|
|
|
+ dev_err(&bm->pdev->dev, "PMM image sanity check failed "
|
|
|
|
|
+ "(id=%02X, field1=%08X, field2=%08X)",
|
|
|
|
|
+ adc_calib->id, adc_calib->field1, adc_calib->field2);
|
|
|
|
|
+check_fail:
|
|
|
|
|
+ return -EILSEQ;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+/* Set the current measure timer that triggers on Tahvo IRQ 7
|
|
|
|
|
+ * An interval of zero disables the timer. */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+static void n810bm_set_current_measure_timer(struct n810bm *bm,
|
|
|
|
|
+ u16 millisec_interval)
|
|
|
|
|
+{
|
|
|
|
|
+ u16 value = millisec_interval;
|
|
|
|
|
+
|
|
|
|
|
+ if (value <= 0xF905) {
|
|
|
|
|
+ value = ((u64)0x10624DD3 * (u64)(value + 0xF9)) >> 32;
|
|
|
|
|
+ value /= 16;
|
|
|
|
|
+ } else
|
|
|
|
|
+ value = 0xFF;
|
|
|
|
|
+
|
|
|
|
|
+ tahvo_write(bm, TAHVO_REG_BATCURRTIMER, value & 0xFF);
|
|
|
|
|
+
|
|
|
|
|
+ tahvo_set(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_CURTIMRST);
|
|
|
|
|
+ tahvo_clear(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_CURTIMRST);
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ if (millisec_interval)
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ tahvo_enable_irq(TAHVO_INT_BATCURR);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ else
|
|
|
|
|
+ tahvo_disable_irq(TAHVO_INT_BATCURR);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
|
|
|
|
+ //TODO also do a software timer for safety.
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static void n810bm_enable_current_measure(struct n810bm *bm)
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ WARN_ON(bm->current_measure_enabled < 0);
|
|
|
|
|
+ if (!bm->current_measure_enabled) {
|
|
|
|
|
+ /* Enable the current measurement circuitry */
|
|
|
|
|
+ tahvo_set(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_CURMEAS);
|
|
|
|
|
+ dev_dbg(&bm->pdev->dev,
|
|
|
|
|
+ "Current measurement circuitry enabled");
|
|
|
|
|
+ }
|
|
|
|
|
+ bm->current_measure_enabled++;
|
|
|
|
|
+}
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static void n810bm_disable_current_measure(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ bm->current_measure_enabled--;
|
|
|
|
|
+ WARN_ON(bm->current_measure_enabled < 0);
|
|
|
|
|
+ if (!bm->current_measure_enabled) {
|
|
|
|
|
+ /* Disable the current measurement circuitry */
|
|
|
|
|
+ tahvo_clear(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_CURMEAS);
|
|
|
|
|
+ dev_dbg(&bm->pdev->dev,
|
|
|
|
|
+ "Current measurement circuitry disabled");
|
|
|
|
|
+ }
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Measure the actual battery current. Returns a signed value in mA.
|
|
|
|
|
+ * Does only work, if current measurement was enabled. */
|
|
|
|
|
+static int n810bm_measure_batt_current(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ u16 retval;
|
|
|
|
|
+ int adc = 0, ma, i;
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ if (WARN_ON(bm->current_measure_enabled <= 0))
|
|
|
|
|
+ return 0;
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ for (i = 0; i < 3; i++) {
|
|
|
|
|
+ retval = tahvo_read(bm, TAHVO_REG_BATCURR);
|
|
|
|
|
+ adc += (s16)retval; /* Value is signed */
|
|
|
|
|
+ }
|
|
|
|
|
+ adc /= 3;
|
|
|
|
|
+
|
|
|
|
|
+ //TODO convert to mA
|
|
|
|
|
+ ma = adc;
|
|
|
|
|
+
|
|
|
|
|
+ return ma;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+/* Requires bm->mutex locked */
|
|
|
|
|
+static int n810bm_measure_batt_current_async(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int ma;
|
|
|
|
|
+ bool charging = lipocharge_is_charging(&bm->charger);
|
|
|
|
|
+
|
|
|
|
|
+ n810bm_enable_current_measure(bm);
|
|
|
|
|
+ if (!charging)
|
|
|
|
|
+ WARN_ON(bm->active_current_pwm != 0);
|
|
|
|
|
+ tahvo_maskset(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_EN |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVRZERO,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_EN |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR |
|
|
|
|
|
+ (charging ? 0 : TAHVO_REG_CHGCTL_PWMOVRZERO));
|
|
|
|
|
+ ma = n810bm_measure_batt_current(bm);
|
|
|
|
|
+ tahvo_maskset(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_EN |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVRZERO,
|
|
|
|
|
+ (charging ? TAHVO_REG_CHGCTL_EN : 0));
|
|
|
|
|
+ n810bm_disable_current_measure(bm);
|
|
|
|
|
+
|
|
|
|
|
+ return ma;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+static int adc_sanity_check(struct n810bm *bm, unsigned int channel)
|
|
|
|
|
+{
|
|
|
|
|
+ int value;
|
|
|
|
|
+
|
|
|
|
|
+ value = retu_read_adc(channel);
|
|
|
|
|
+ if (value < 0) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev, "Failed to read GND ADC channel %u",
|
|
|
|
|
+ channel);
|
|
|
|
|
+ return -EIO;
|
|
|
|
|
+ }
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ dev_dbg(&bm->pdev->dev,
|
|
|
|
|
+ "GND ADC channel %u sanity check got value: %d",
|
|
|
|
|
+ channel, value);
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ if (value > 5) {
|
|
|
|
|
+ n810bm_emergency(bm, "GND ADC sanity check failed");
|
|
|
|
|
+ return -EIO;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int n810bm_check_adc_sanity(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int err;
|
|
|
|
|
+
|
|
|
|
|
+ /* Discard one conversion */
|
|
|
|
|
+ retu_write(bm, RETU_REG_ADCSCR, 0);
|
|
|
|
|
+ retu_read_adc(RETU_ADC_GND2);
|
|
|
|
|
+
|
|
|
|
|
+ err = adc_sanity_check(bm, RETU_ADC_GND2);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ return err;
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+/* Measure the battery voltage. Returns the value in mV (or negative value on error). */
|
|
|
|
|
+static int n810bm_measure_batt_voltage(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int adc;
|
|
|
|
|
+ unsigned int mv;
|
|
|
|
|
+ const unsigned int scale = 1000;
|
|
|
|
|
+
|
|
|
|
|
+ adc = retu_adc_average(bm, RETU_ADC_BATTVOLT, 5);
|
|
|
|
|
+ if (adc < 0)
|
|
|
|
|
+ return adc;
|
|
|
|
|
+ if (adc <= 0x37)
|
|
|
|
|
+ return 2800;
|
|
|
|
|
+ mv = 2800 + ((adc - 0x37) * (((4200 - 2800) * scale) / (0x236 - 0x37))) / scale;
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ //TODO compensate for power consumption
|
|
|
|
|
+ //TODO honor calibration values
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ return mv;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+/* Measure the charger voltage. Returns the value in mV (or negative value on error). */
|
|
|
|
|
+static int n810bm_measure_charger_voltage(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int adc;
|
|
|
|
|
+ unsigned int mv;
|
|
|
|
|
+
|
|
|
|
|
+ adc = retu_adc_average(bm, RETU_ADC_CHGVOLT, 5);
|
|
|
|
|
+ if (adc < 0)
|
|
|
|
|
+ return adc;
|
|
|
|
|
+ //TODO convert to mV
|
|
|
|
|
+ mv = adc;
|
|
|
|
|
+
|
|
|
|
|
+ return mv;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Measure backup battery voltage. Returns the value in mV (or negative value on error). */
|
|
|
|
|
+static int n810bm_measure_backup_batt_voltage(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int adc;
|
|
|
|
|
+ unsigned int mv;
|
|
|
|
|
+
|
|
|
|
|
+ adc = retu_adc_average(bm, RETU_ADC_BKUPVOLT, 3);
|
|
|
|
|
+ if (adc < 0)
|
|
|
|
|
+ return adc;
|
|
|
|
|
+ //TODO convert to mV
|
|
|
|
|
+ mv = adc;
|
|
|
|
|
+
|
|
|
|
|
+ return mv;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Measure the battery temperature. Returns the value in K (or negative value on error). */
|
|
|
|
|
+static int n810bm_measure_batt_temp(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int adc;
|
|
|
|
|
+ unsigned int k;
|
|
|
|
|
+
|
|
|
|
|
+ adc = retu_adc_average(bm, RETU_ADC_BATTEMP, 3);
|
|
|
|
|
+ if (adc < 0)
|
|
|
|
|
+ return adc;
|
|
|
|
|
+ //TODO convert to K
|
|
|
|
|
+ k = adc;
|
|
|
|
|
+
|
|
|
|
|
+ return k;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+/* Read the battery capacity via BSI pin. */
|
|
|
|
|
+static enum n810bm_capacity n810bm_read_batt_capacity(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int adc;
|
|
|
|
|
+ const unsigned int hyst = 20;
|
|
|
|
|
+
|
|
|
|
|
+ adc = retu_adc_average(bm, RETU_ADC_BSI, 5);
|
|
|
|
|
+ if (adc < 0) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev, "Failed to read BSI ADC");
|
|
|
|
|
+ return N810BM_CAP_UNKNOWN;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (adc >= 0x3B5 - hyst && adc <= 0x3B5 + hyst)
|
|
|
|
|
+ return N810BM_CAP_1500MAH;
|
|
|
|
|
+
|
|
|
|
|
+ dev_err(&bm->pdev->dev, "Capacity indicator 0x%X unknown", adc);
|
|
|
|
|
+
|
|
|
|
|
+ return N810BM_CAP_UNKNOWN;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Convert a battery voltage (in mV) to percentage. */
|
|
|
|
|
+static unsigned int n810bm_mvolt2percent(unsigned int mv)
|
|
|
|
|
+{
|
|
|
|
|
+ const unsigned int minv = 3700;
|
|
|
|
|
+ const unsigned int maxv = 4150;
|
|
|
|
|
+ unsigned int percent;
|
|
|
|
|
+
|
|
|
|
|
+ mv = clamp(mv, minv, maxv);
|
|
|
|
|
+ percent = (mv - minv) * 100 / (maxv - minv);
|
|
|
|
|
+
|
|
|
|
|
+ return percent;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static void n810bm_start_charge(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int err;
|
|
|
|
|
+
|
|
|
|
|
+ WARN_ON(!bm->battery_present);
|
|
|
|
|
+ WARN_ON(!bm->charger_present);
|
|
|
|
|
+
|
|
|
|
|
+ /* Set PWM to zero */
|
|
|
|
|
+ bm->active_current_pwm = 0;
|
|
|
|
|
+ tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
|
|
|
|
|
+
|
|
|
|
|
+ /* Charge global enable */
|
|
|
|
|
+ tahvo_maskset(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_EN |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVRZERO,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_EN);
|
|
|
|
|
+
|
|
|
|
|
+ WARN_ON((int)bm->capacity <= 0);
|
|
|
|
|
+ bm->charger.capacity = bm->capacity;
|
|
|
|
|
+ err = lipocharge_start(&bm->charger);
|
|
|
|
|
+ WARN_ON(err);
|
|
|
|
|
+
|
|
|
|
|
+ /* Initialize current measurement circuitry */
|
|
|
|
|
+ n810bm_enable_current_measure(bm);
|
|
|
|
|
+ n810bm_set_current_measure_timer(bm, 250);
|
|
|
|
|
+
|
|
|
|
|
+ dev_info(&bm->pdev->dev, "Charging battery");
|
|
|
|
|
+ n810bm_notify_charger_pwm(bm);
|
|
|
|
|
+ n810bm_notify_battery_charging(bm);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void n810bm_stop_charge(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ if (lipocharge_is_charging(&bm->charger)) {
|
|
|
|
|
+ n810bm_set_current_measure_timer(bm, 0);
|
|
|
|
|
+ n810bm_disable_current_measure(bm);
|
|
|
|
|
+ }
|
|
|
|
|
+ lipocharge_stop(&bm->charger);
|
|
|
|
|
+
|
|
|
|
|
+ /* Set PWM to zero */
|
|
|
|
|
+ bm->active_current_pwm = 0;
|
|
|
|
|
+ tahvo_write(bm, TAHVO_REG_CHGCURR, bm->active_current_pwm);
|
|
|
|
|
+
|
|
|
|
|
+ /* Charge global disable */
|
|
|
|
|
+ tahvo_maskset(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_EN |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVRZERO,
|
|
|
|
|
+ 0);
|
|
|
|
|
+
|
|
|
|
|
+ dev_info(&bm->pdev->dev, "Not charging battery");
|
|
|
|
|
+ n810bm_notify_charger_pwm(bm);
|
|
|
|
|
+ n810bm_notify_battery_charging(bm);
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+/* Periodic check */
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static void n810bm_periodic_check_work(struct work_struct *work)
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ struct n810bm *bm = container_of(to_delayed_work(work),
|
|
|
|
|
+ struct n810bm, periodic_check_work);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ u16 status;
|
|
|
|
|
+ bool battery_was_present, charger_was_present;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ int mv;
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_lock(&bm->mutex);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ status = retu_read(bm, RETU_REG_STATUS);
|
|
|
|
|
+ battery_was_present = bm->battery_present;
|
|
|
|
|
+ charger_was_present = bm->charger_present;
|
|
|
|
|
+ bm->battery_present = !!(status & RETU_REG_STATUS_BATAVAIL);
|
|
|
|
|
+ bm->charger_present = !!(status & RETU_REG_STATUS_CHGPLUG);
|
|
|
|
|
+
|
|
|
|
|
+ if (bm->battery_present != battery_was_present) {
|
|
|
|
|
+ /* Battery state changed */
|
|
|
|
|
+ if (bm->battery_present) {
|
|
|
|
|
+ bm->capacity = n810bm_read_batt_capacity(bm);
|
|
|
|
|
+ if (bm->capacity == N810BM_CAP_UNKNOWN) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev, "Unknown battery detected");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ dev_info(&bm->pdev->dev, "Detected %u mAh battery",
|
|
|
|
|
+ (unsigned int)bm->capacity);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ bm->capacity = N810BM_CAP_NONE;
|
|
|
|
|
+ dev_info(&bm->pdev->dev, "The main battery was removed");
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ //TODO disable charging
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (bm->charger_present != charger_was_present) {
|
|
|
|
|
+ /* Charger state changed */
|
|
|
|
|
+ dev_info(&bm->pdev->dev, "The charger was %s",
|
|
|
|
|
+ bm->charger_present ? "plugged in" : "removed");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
2011-02-09 20:09:49 +00:00
|
|
|
|
+ if ((bm->battery_present && !bm->charger_present) ||
|
|
|
|
|
+ !n810bm_known_battery_present(bm)){
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ /* We're draining the battery */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ mv = n810bm_measure_batt_voltage(bm);
|
2011-02-09 20:09:49 +00:00
|
|
|
|
+ if (mv < 0) {
|
|
|
|
|
+ n810bm_emergency(bm,
|
|
|
|
|
+ "check: Failed to measure voltage");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (mv < N810BM_MIN_VOLTAGE_THRES) {
|
|
|
|
|
+ n810bm_emergency(bm,
|
|
|
|
|
+ "check: Minimum voltage threshold reached");
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ if (bm->charger_present && n810bm_known_battery_present(bm)) {
|
|
|
|
|
+ /* Known battery and charger are connected */
|
2011-02-09 20:09:49 +00:00
|
|
|
|
+ if (bm->charger_enabled) {
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ /* Charger is enabled */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ if (!lipocharge_is_charging(&bm->charger)) {
|
|
|
|
|
+ //TODO start charging, if battery is below some threshold
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ n810bm_start_charge(bm);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ if (lipocharge_is_charging(&bm->charger) && !bm->charger_present) {
|
|
|
|
|
+ /* Charger was unplugged. */
|
|
|
|
|
+ n810bm_stop_charge(bm);
|
|
|
|
|
+ }
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_unlock(&bm->mutex);
|
|
|
|
|
+ schedule_delayed_work(&bm->periodic_check_work,
|
|
|
|
|
+ round_jiffies_relative(N810BM_CHECK_INTERVAL));
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void n810bm_adc_irq_handler(unsigned long data)
|
|
|
|
|
+{
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ struct n810bm *bm = (struct n810bm *)data;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
|
|
|
|
+ retu_ack_irq(RETU_INT_ADCS);
|
|
|
|
|
+ //TODO
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+dev_info(&bm->pdev->dev, "ADC interrupt triggered\n");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void n810bm_tahvo_current_measure_work(struct work_struct *work)
|
|
|
|
|
+{
|
|
|
|
|
+ struct n810bm *bm = container_of(work, struct n810bm, currmeas_irq_work);
|
|
|
|
|
+ int res, ma, mv, temp;
|
|
|
|
|
+
|
|
|
|
|
+ mutex_lock(&bm->mutex);
|
|
|
|
|
+ if (!lipocharge_is_charging(&bm->charger))
|
|
|
|
|
+ goto out_unlock;
|
|
|
|
|
+
|
|
|
|
|
+ tahvo_maskset(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVRZERO,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR);
|
|
|
|
|
+ ma = n810bm_measure_batt_current(bm);
|
|
|
|
|
+ tahvo_maskset(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVRZERO,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVRZERO);
|
|
|
|
|
+ msleep(10);
|
|
|
|
|
+ mv = n810bm_measure_batt_voltage(bm);
|
|
|
|
|
+ tahvo_maskset(bm, TAHVO_REG_CHGCTL,
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVR |
|
|
|
|
|
+ TAHVO_REG_CHGCTL_PWMOVRZERO,
|
|
|
|
|
+ 0);
|
|
|
|
|
+ temp = n810bm_measure_batt_temp(bm);
|
|
|
|
|
+ if (WARN_ON(mv < 0))
|
|
|
|
|
+ goto out_unlock;
|
|
|
|
|
+ if (WARN_ON(temp < 0))
|
|
|
|
|
+ goto out_unlock;
|
|
|
|
|
+
|
|
|
|
|
+ if (bm->verbose_charge_log) {
|
|
|
|
|
+ dev_info(&bm->pdev->dev,
|
|
|
|
|
+ "Battery charge state: %d mV, %d mA (%s)",
|
|
|
|
|
+ mv, ma,
|
|
|
|
|
+ (ma <= 0) ? "discharging" : "charging");
|
|
|
|
|
+ }
|
|
|
|
|
+ res = lipocharge_update_state(&bm->charger, mv, ma, temp);
|
|
|
|
|
+ if (res) {
|
|
|
|
|
+ if (res > 0)
|
|
|
|
|
+ dev_info(&bm->pdev->dev, "Battery fully charged");
|
|
|
|
|
+ n810bm_stop_charge(bm);
|
|
|
|
|
+ }
|
|
|
|
|
+out_unlock:
|
|
|
|
|
+ mutex_unlock(&bm->mutex);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void n810bm_tahvo_current_measure_irq_handler(unsigned long data)
|
|
|
|
|
+{
|
|
|
|
|
+ struct n810bm *bm = (struct n810bm *)data;
|
|
|
|
|
+
|
|
|
|
|
+ tahvo_ack_irq(TAHVO_INT_BATCURR);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ schedule_work(&bm->currmeas_irq_work);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+#define DEFINE_ATTR_NOTIFY(attr_name) \
|
|
|
|
|
+ void n810bm_notify_##attr_name(struct n810bm *bm) \
|
|
|
|
|
+ { \
|
|
|
|
|
+ set_bit(N810BM_NOTIFY_##attr_name, &bm->notify_flags); \
|
|
|
|
|
+ wmb(); \
|
|
|
|
|
+ schedule_work(&bm->notify_work); \
|
|
|
|
|
+ }
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+#define DEFINE_SHOW_INT_FUNC(name, member) \
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ static ssize_t n810bm_attr_##name##_show(struct device *dev, \
|
|
|
|
|
+ struct device_attribute *attr, \
|
|
|
|
|
+ char *buf) \
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ { \
|
|
|
|
|
+ struct n810bm *bm = device_to_n810bm(dev); \
|
|
|
|
|
+ ssize_t count; \
|
|
|
|
|
+ \
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_lock(&bm->mutex); \
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ count = snprintf(buf, PAGE_SIZE, "%d\n", (int)(bm->member)); \
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_unlock(&bm->mutex); \
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ \
|
|
|
|
|
+ return count; \
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+#define DEFINE_STORE_INT_FUNC(name, member) \
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ static ssize_t n810bm_attr_##name##_store(struct device *dev, \
|
|
|
|
|
+ struct device_attribute *attr,\
|
|
|
|
|
+ const char *buf, size_t count)\
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ { \
|
|
|
|
|
+ struct n810bm *bm = device_to_n810bm(dev); \
|
|
|
|
|
+ long val; \
|
|
|
|
|
+ int err; \
|
|
|
|
|
+ \
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_lock(&bm->mutex); \
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ err = strict_strtol(buf, 0, &val); \
|
|
|
|
|
+ if (!err) \
|
|
|
|
|
+ bm->member = (typeof(bm->member))val; \
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_unlock(&bm->mutex); \
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ \
|
|
|
|
|
+ return err ? err : count; \
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+#define DEFINE_ATTR_SHOW_INT(name, member) \
|
|
|
|
|
+ DEFINE_SHOW_INT_FUNC(name, member) \
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ static DEVICE_ATTR(name, S_IRUGO, \
|
|
|
|
|
+ n810bm_attr_##name##_show, NULL);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
|
|
|
|
+#define DEFINE_ATTR_SHOW_STORE_INT(name, member) \
|
|
|
|
|
+ DEFINE_SHOW_INT_FUNC(name, member) \
|
|
|
|
|
+ DEFINE_STORE_INT_FUNC(name, member) \
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, \
|
|
|
|
|
+ n810bm_attr_##name##_show, \
|
|
|
|
|
+ n810bm_attr_##name##_store);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+DEFINE_ATTR_SHOW_INT(battery_present, battery_present);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+DEFINE_ATTR_SHOW_INT(charger_present, charger_present);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+DEFINE_ATTR_SHOW_INT(charger_pwm, active_current_pwm);
|
|
|
|
|
+static DEFINE_ATTR_NOTIFY(charger_pwm);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+DEFINE_ATTR_SHOW_STORE_INT(charger_enable, charger_enabled);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+DEFINE_ATTR_SHOW_STORE_INT(charger_verbose, verbose_charge_log);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static ssize_t n810bm_attr_battery_charging(struct device *dev,
|
|
|
|
|
+ struct device_attribute *attr,
|
|
|
|
|
+ char *buf)
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+{
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ struct n810bm *bm = device_to_n810bm(dev);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ ssize_t count;
|
|
|
|
|
+
|
|
|
|
|
+ mutex_lock(&bm->mutex);
|
|
|
|
|
+ count = snprintf(buf, PAGE_SIZE, "%d\n",
|
|
|
|
|
+ (int)lipocharge_is_charging(&bm->charger));
|
|
|
|
|
+ mutex_unlock(&bm->mutex);
|
|
|
|
|
+
|
|
|
|
|
+ return count;
|
|
|
|
|
+}
|
|
|
|
|
+static DEVICE_ATTR(battery_charging, S_IRUGO,
|
|
|
|
|
+ n810bm_attr_battery_charging, NULL);
|
|
|
|
|
+static DEFINE_ATTR_NOTIFY(battery_charging);
|
|
|
|
|
+
|
|
|
|
|
+static ssize_t n810bm_attr_battery_level_show(struct device *dev,
|
|
|
|
|
+ struct device_attribute *attr,
|
|
|
|
|
+ char *buf)
|
|
|
|
|
+{
|
|
|
|
|
+ struct n810bm *bm = device_to_n810bm(dev);
|
|
|
|
|
+ ssize_t count = -ENODEV;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ int millivolt;
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_lock(&bm->mutex);
|
|
|
|
|
+ if (!bm->battery_present || lipocharge_is_charging(&bm->charger))
|
|
|
|
|
+ millivolt = 0;
|
|
|
|
|
+ else
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ millivolt = n810bm_measure_batt_voltage(bm);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ if (millivolt >= 0) {
|
|
|
|
|
+ count = snprintf(buf, PAGE_SIZE, "%u\n",
|
|
|
|
|
+ n810bm_mvolt2percent(millivolt));
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ }
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_unlock(&bm->mutex);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ return count;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static DEVICE_ATTR(battery_level, S_IRUGO,
|
|
|
|
|
+ n810bm_attr_battery_level_show, NULL);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static ssize_t n810bm_attr_battery_capacity_show(struct device *dev,
|
|
|
|
|
+ struct device_attribute *attr,
|
|
|
|
|
+ char *buf)
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+{
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ struct n810bm *bm = device_to_n810bm(dev);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ ssize_t count;
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ int capacity = 0;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_lock(&bm->mutex);
|
|
|
|
|
+ if (n810bm_known_battery_present(bm))
|
|
|
|
|
+ capacity = (int)bm->capacity;
|
|
|
|
|
+ count = snprintf(buf, PAGE_SIZE, "%d\n", capacity);
|
|
|
|
|
+ mutex_unlock(&bm->mutex);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
|
|
|
|
+ return count;
|
|
|
|
|
+}
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static DEVICE_ATTR(battery_capacity, S_IRUGO,
|
|
|
|
|
+ n810bm_attr_battery_capacity_show, NULL);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static ssize_t n810bm_attr_battery_temp_show(struct device *dev,
|
|
|
|
|
+ struct device_attribute *attr,
|
|
|
|
|
+ char *buf)
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+{
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ struct n810bm *bm = device_to_n810bm(dev);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ ssize_t count = -ENODEV;
|
|
|
|
|
+ int k;
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_lock(&bm->mutex);
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ k = n810bm_measure_batt_temp(bm);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ if (k >= 0)
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ count = snprintf(buf, PAGE_SIZE, "%d\n", k);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_unlock(&bm->mutex);
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ return count;
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+}
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static DEVICE_ATTR(battery_temp, S_IRUGO,
|
|
|
|
|
+ n810bm_attr_battery_temp_show, NULL);
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+static ssize_t n810bm_attr_charger_voltage_show(struct device *dev,
|
|
|
|
|
+ struct device_attribute *attr,
|
|
|
|
|
+ char *buf)
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+{
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ struct n810bm *bm = device_to_n810bm(dev);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ ssize_t count = -ENODEV;
|
|
|
|
|
+ int mv = 0;
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_lock(&bm->mutex);
|
|
|
|
|
+ if (bm->charger_present)
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ mv = n810bm_measure_charger_voltage(bm);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ if (mv >= 0)
|
|
|
|
|
+ count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
|
|
|
|
|
+ mutex_unlock(&bm->mutex);
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ return count;
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+}
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static DEVICE_ATTR(charger_voltage, S_IRUGO,
|
|
|
|
|
+ n810bm_attr_charger_voltage_show, NULL);
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static ssize_t n810bm_attr_backup_battery_voltage_show(struct device *dev,
|
|
|
|
|
+ struct device_attribute *attr,
|
|
|
|
|
+ char *buf)
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+{
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ struct n810bm *bm = device_to_n810bm(dev);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ ssize_t count = -ENODEV;
|
|
|
|
|
+ int mv;
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_lock(&bm->mutex);
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ mv = n810bm_measure_backup_batt_voltage(bm);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ if (mv >= 0)
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ count = snprintf(buf, PAGE_SIZE, "%d\n", mv);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_unlock(&bm->mutex);
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ return count;
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+}
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static DEVICE_ATTR(backup_battery_voltage, S_IRUGO,
|
|
|
|
|
+ n810bm_attr_backup_battery_voltage_show, NULL);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static ssize_t n810bm_attr_battery_current_show(struct device *dev,
|
|
|
|
|
+ struct device_attribute *attr,
|
|
|
|
|
+ char *buf)
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+{
|
|
|
|
|
+ struct n810bm *bm = device_to_n810bm(dev);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ ssize_t count = -ENODEV;
|
|
|
|
|
+ int ma = 0;
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_lock(&bm->mutex);
|
|
|
|
|
+ if (bm->battery_present)
|
|
|
|
|
+ ma = n810bm_measure_batt_current_async(bm);
|
|
|
|
|
+ count = snprintf(buf, PAGE_SIZE, "%d\n", ma);
|
|
|
|
|
+ mutex_unlock(&bm->mutex);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
|
|
|
|
+ return count;
|
|
|
|
|
+}
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static DEVICE_ATTR(battery_current, S_IRUGO,
|
|
|
|
|
+ n810bm_attr_battery_current_show, NULL);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static const struct device_attribute *n810bm_attrs[] = {
|
|
|
|
|
+ &dev_attr_battery_present,
|
|
|
|
|
+ &dev_attr_battery_level,
|
|
|
|
|
+ &dev_attr_battery_charging,
|
|
|
|
|
+ &dev_attr_battery_current,
|
|
|
|
|
+ &dev_attr_battery_capacity,
|
|
|
|
|
+ &dev_attr_battery_temp,
|
|
|
|
|
+ &dev_attr_backup_battery_voltage,
|
|
|
|
|
+ &dev_attr_charger_present,
|
|
|
|
|
+ &dev_attr_charger_verbose,
|
|
|
|
|
+ &dev_attr_charger_voltage,
|
|
|
|
|
+ &dev_attr_charger_enable,
|
|
|
|
|
+ &dev_attr_charger_pwm,
|
|
|
|
|
+};
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static void n810bm_notify_work(struct work_struct *work)
|
|
|
|
|
+{
|
|
|
|
|
+ struct n810bm *bm = container_of(work, struct n810bm, notify_work);
|
|
|
|
|
+ unsigned long notify_flags;
|
|
|
|
|
+
|
|
|
|
|
+ notify_flags = xchg(&bm->notify_flags, 0);
|
|
|
|
|
+ mb();
|
|
|
|
|
+
|
|
|
|
|
+#define do_notify(attr_name) \
|
|
|
|
|
+ do { \
|
|
|
|
|
+ if (notify_flags & (1 << N810BM_NOTIFY_##attr_name)) { \
|
|
|
|
|
+ sysfs_notify(&bm->pdev->dev.kobj, NULL, \
|
|
|
|
|
+ dev_attr_##attr_name.attr.name); \
|
|
|
|
|
+ } \
|
|
|
|
|
+ } while (0)
|
|
|
|
|
+
|
|
|
|
|
+ do_notify(battery_charging);
|
|
|
|
|
+ do_notify(charger_pwm);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static int n810bm_charger_set_current_pwm(struct lipocharge *c,
|
|
|
|
|
+ unsigned int duty_cycle)
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ struct n810bm *bm = container_of(c, struct n810bm, charger);
|
|
|
|
|
+ int err = -EINVAL;
|
|
|
|
|
+
|
|
|
|
|
+ WARN_ON(!mutex_is_locked(&bm->mutex));
|
|
|
|
|
+ if (WARN_ON(duty_cycle > 0xFF))
|
|
|
|
|
+ goto out;
|
|
|
|
|
+ if (WARN_ON(!bm->charger_enabled))
|
|
|
|
|
+ goto out;
|
|
|
|
|
+ if (WARN_ON(!bm->battery_present || !bm->charger_present))
|
|
|
|
|
+ goto out;
|
|
|
|
|
+
|
|
|
|
|
+ if (duty_cycle != bm->active_current_pwm) {
|
|
|
|
|
+ bm->active_current_pwm = duty_cycle;
|
|
|
|
|
+ tahvo_write(bm, TAHVO_REG_CHGCURR, duty_cycle);
|
|
|
|
|
+ n810bm_notify_charger_pwm(bm);
|
|
|
|
|
+ }
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ err = 0;
|
|
|
|
|
+out:
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ return err;
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static void n810bm_charger_emergency(struct lipocharge *c)
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ struct n810bm *bm = container_of(c, struct n810bm, charger);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ n810bm_emergency(bm, "Battery charger fault");
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+}
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+static void n810bm_hw_exit(struct n810bm *bm)
|
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ n810bm_stop_charge(bm);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ retu_write(bm, RETU_REG_ADCSCR, 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int n810bm_hw_init(struct n810bm *bm)
|
|
|
|
|
+{
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ int err;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ err = n810bm_check_adc_sanity(bm);
|
|
|
|
|
+ if (err)
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ return err;
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ n810bm_stop_charge(bm);
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+static void n810bm_cancel_and_flush_work(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ cancel_delayed_work_sync(&bm->periodic_check_work);
|
|
|
|
|
+ cancel_work_sync(&bm->notify_work);
|
|
|
|
|
+ cancel_work_sync(&bm->currmeas_irq_work);
|
|
|
|
|
+ flush_scheduled_work();
|
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+static int n810bm_device_init(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int attr_index;
|
|
|
|
|
+ int err;
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ bm->charger.rate = LIPORATE_p6C;
|
|
|
|
|
+ bm->charger.top_voltage = 4100;
|
|
|
|
|
+ bm->charger.duty_cycle_max = 0xFF;
|
|
|
|
|
+ bm->charger.set_current_pwm = n810bm_charger_set_current_pwm;
|
|
|
|
|
+ bm->charger.emergency = n810bm_charger_emergency;
|
|
|
|
|
+ lipocharge_init(&bm->charger, &bm->pdev->dev);
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ err = n810bm_hw_init(bm);
|
|
|
|
|
+ if (err)
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ goto error;
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ for (attr_index = 0; attr_index < ARRAY_SIZE(n810bm_attrs); attr_index++) {
|
|
|
|
|
+ err = device_create_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ goto err_unwind_attrs;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ }
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ err = retu_request_irq(RETU_INT_ADCS,
|
|
|
|
|
+ n810bm_adc_irq_handler,
|
|
|
|
|
+ (unsigned long)bm, "n810bm");
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ goto err_unwind_attrs;
|
|
|
|
|
+ err = tahvo_request_irq(TAHVO_INT_BATCURR,
|
|
|
|
|
+ n810bm_tahvo_current_measure_irq_handler,
|
|
|
|
|
+ (unsigned long)bm, "n810bm");
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ goto err_free_retu_irq;
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ tahvo_disable_irq(TAHVO_INT_BATCURR);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ schedule_delayed_work(&bm->periodic_check_work,
|
|
|
|
|
+ round_jiffies_relative(N810BM_CHECK_INTERVAL));
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
|
|
|
|
+ bm->initialized = 1;
|
|
|
|
|
+ dev_info(&bm->pdev->dev, "Battery management initialized");
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+err_free_retu_irq:
|
|
|
|
|
+ retu_free_irq(RETU_INT_ADCS);
|
|
|
|
|
+err_unwind_attrs:
|
|
|
|
|
+ for (attr_index--; attr_index >= 0; attr_index--)
|
|
|
|
|
+ device_remove_file(&bm->pdev->dev, n810bm_attrs[attr_index]);
|
|
|
|
|
+/*err_exit:*/
|
|
|
|
|
+ n810bm_hw_exit(bm);
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+error:
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ n810bm_cancel_and_flush_work(bm);
|
|
|
|
|
+
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+ return err;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+static void n810bm_device_exit(struct n810bm *bm)
|
|
|
|
|
+{
|
|
|
|
|
+ int i;
|
|
|
|
|
+
|
|
|
|
|
+ if (!bm->initialized)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ lipocharge_exit(&bm->charger);
|
|
|
|
|
+ tahvo_free_irq(TAHVO_INT_BATCURR);
|
|
|
|
|
+ retu_free_irq(RETU_INT_ADCS);
|
|
|
|
|
+ for (i = 0; i < ARRAY_SIZE(n810bm_attrs); i++)
|
|
|
|
|
+ device_remove_file(&bm->pdev->dev, n810bm_attrs[i]);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+
|
|
|
|
|
+ n810bm_cancel_and_flush_work(bm);
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ n810bm_hw_exit(bm);
|
|
|
|
|
+
|
|
|
|
|
+ bm->initialized = 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void n810bm_pmm_block_found(const struct firmware *fw, void *context)
|
|
|
|
|
+{
|
|
|
|
|
+ struct n810bm *bm = context;
|
|
|
|
|
+ int err;
|
|
|
|
|
+
|
|
|
|
|
+ if (!fw) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev,
|
|
|
|
|
+ "CAL PMM block image file not found");
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ goto err_release;
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ }
|
|
|
|
|
+ if (fw->size != N810BM_PMM_BLOCK_SIZE ||
|
|
|
|
|
+ memcmp(fw->data, "BME-PMM-BLOCK01", 15) != 0) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev,
|
|
|
|
|
+ "CAL PMM block image file has an invalid format");
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ goto err_release;
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ }
|
|
|
|
|
+
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+ err = n810bm_parse_pmm_block(bm, fw);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ goto err_release;
|
|
|
|
|
+ release_firmware(fw);
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ err = n810bm_device_init(bm);
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev,
|
|
|
|
|
+ "Failed to initialized battery management (%d)", err);
|
|
|
|
|
+ goto error;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return;
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+err_release:
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ release_firmware(fw);
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+error:
|
|
|
|
|
+ return;
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+static int __devinit n810bm_probe(struct platform_device *pdev)
|
|
|
|
|
+{
|
|
|
|
|
+ struct n810bm *bm;
|
|
|
|
|
+ int err;
|
|
|
|
|
+
|
|
|
|
|
+ bm = kzalloc(sizeof(*bm), GFP_KERNEL);
|
|
|
|
|
+ if (!bm)
|
|
|
|
|
+ return -ENOMEM;
|
|
|
|
|
+ bm->pdev = pdev;
|
|
|
|
|
+ platform_set_drvdata(pdev, bm);
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ mutex_init(&bm->mutex);
|
|
|
|
|
+ INIT_DELAYED_WORK(&bm->periodic_check_work, n810bm_periodic_check_work);
|
|
|
|
|
+ INIT_WORK(&bm->notify_work, n810bm_notify_work);
|
|
|
|
|
+ INIT_WORK(&bm->currmeas_irq_work, n810bm_tahvo_current_measure_work);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ dev_info(&bm->pdev->dev, "Requesting CAL BME PMM block firmware file "
|
|
|
|
|
+ N810BM_PMM_BLOCK_FILENAME);
|
|
|
|
|
+ err = request_firmware_nowait(THIS_MODULE, 1,
|
|
|
|
|
+ N810BM_PMM_BLOCK_FILENAME,
|
|
|
|
|
+ &bm->pdev->dev, GFP_KERNEL,
|
|
|
|
|
+ bm, n810bm_pmm_block_found);
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ dev_err(&bm->pdev->dev,
|
|
|
|
|
+ "Failed to request CAL PMM block image file (%d)", err);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ goto err_free;
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ }
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+
|
|
|
|
|
+err_free:
|
|
|
|
|
+ kfree(bm);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ return err;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int __devexit n810bm_remove(struct platform_device *pdev)
|
|
|
|
|
+{
|
|
|
|
|
+ struct n810bm *bm = platform_get_drvdata(pdev);
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+ n810bm_device_exit(bm);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
|
|
|
|
+ kfree(bm);
|
|
|
|
|
+ platform_set_drvdata(pdev, NULL);
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static struct platform_driver n810bm_driver = {
|
|
|
|
|
+ .remove = __devexit_p(n810bm_remove),
|
|
|
|
|
+ .driver = {
|
|
|
|
|
+ .name = "n810bm",
|
|
|
|
|
+ }
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+static int __init n810bm_modinit(void)
|
|
|
|
|
+{
|
|
|
|
|
+ return platform_driver_probe(&n810bm_driver, n810bm_probe);
|
|
|
|
|
+}
|
|
|
|
|
+module_init(n810bm_modinit);
|
|
|
|
|
+
|
|
|
|
|
+static void __exit n810bm_modexit(void)
|
|
|
|
|
+{
|
|
|
|
|
+ platform_driver_unregister(&n810bm_driver);
|
|
|
|
|
+}
|
|
|
|
|
+module_exit(n810bm_modexit);
|
|
|
|
|
+
|
|
|
|
|
+MODULE_DESCRIPTION("Nokia n810 battery management");
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+MODULE_FIRMWARE(N810BM_PMM_BLOCK_FILENAME);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+MODULE_LICENSE("GPL");
|
|
|
|
|
+MODULE_AUTHOR("Michael Buesch");
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/retu.c
|
2010-11-05 16:59:39 +00:00
|
|
|
|
===================================================================
|
2011-02-20 14:28:40 +00:00
|
|
|
|
--- linux-2.6.37.1.orig/drivers/cbus/retu.c 2011-02-19 20:26:01.251340770 +0100
|
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/retu.c 2011-02-19 20:26:01.529318528 +0100
|
2010-11-05 16:59:39 +00:00
|
|
|
|
@@ -85,10 +85,10 @@
|
|
|
|
|
*
|
|
|
|
|
* This function writes a value to the specified register
|
|
|
|
|
*/
|
|
|
|
|
-void retu_write_reg(int reg, u16 val)
|
|
|
|
|
+int retu_write_reg(int reg, u16 val)
|
|
|
|
|
{
|
|
|
|
|
BUG_ON(!retu_initialized);
|
|
|
|
|
- cbus_write_reg(cbus_host, RETU_ID, reg, val);
|
|
|
|
|
+ return cbus_write_reg(cbus_host, RETU_ID, reg, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void retu_set_clear_reg_bits(int reg, u16 set, u16 clear)
|
2011-02-09 12:53:37 +00:00
|
|
|
|
@@ -459,6 +459,7 @@
|
|
|
|
|
EXPORT_SYMBOL(retu_ack_irq);
|
|
|
|
|
EXPORT_SYMBOL(retu_read_reg);
|
|
|
|
|
EXPORT_SYMBOL(retu_write_reg);
|
|
|
|
|
+EXPORT_SYMBOL(retu_read_adc);
|
|
|
|
|
|
|
|
|
|
subsys_initcall(retu_init);
|
|
|
|
|
module_exit(retu_exit);
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/retu.h
|
2010-11-05 16:59:39 +00:00
|
|
|
|
===================================================================
|
2011-02-20 14:28:40 +00:00
|
|
|
|
--- linux-2.6.37.1.orig/drivers/cbus/retu.h 2011-02-19 20:26:01.252340690 +0100
|
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/retu.h 2011-02-19 20:26:01.530318448 +0100
|
2011-02-06 12:44:12 +00:00
|
|
|
|
@@ -40,6 +40,8 @@
|
2010-11-05 16:59:39 +00:00
|
|
|
|
#define RETU_REG_CTRL_CLR 0x0f /* Regulator clear register */
|
|
|
|
|
#define RETU_REG_CTRL_SET 0x10 /* Regulator set register */
|
|
|
|
|
#define RETU_REG_STATUS 0x16 /* Status register */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+#define RETU_REG_STATUS_BATAVAIL 0x0100 /* Battery available */
|
|
|
|
|
+#define RETU_REG_STATUS_CHGPLUG 0x1000 /* Charger is plugged in */
|
2010-11-05 16:59:39 +00:00
|
|
|
|
#define RETU_REG_WATCHDOG 0x17 /* Watchdog register */
|
|
|
|
|
#define RETU_REG_AUDTXR 0x18 /* Audio Codec Tx register */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
#define RETU_REG_MAX 0x1f
|
|
|
|
|
@@ -57,8 +59,25 @@
|
2011-01-28 22:45:37 +00:00
|
|
|
|
|
2010-11-05 16:59:39 +00:00
|
|
|
|
#define MAX_RETU_IRQ_HANDLERS 16
|
|
|
|
|
|
2011-01-28 22:45:37 +00:00
|
|
|
|
+/* ADC channels */
|
|
|
|
|
+#define RETU_ADC_GND 0x00 /* Ground */
|
|
|
|
|
+#define RETU_ADC_BSI 0x01 /* Battery Size Indicator */
|
|
|
|
|
+#define RETU_ADC_BATTEMP 0x02 /* Battery temperature */
|
|
|
|
|
+#define RETU_ADC_CHGVOLT 0x03 /* Charger voltage */
|
|
|
|
|
+#define RETU_ADC_HEADSET 0x04 /* Headset detection */
|
|
|
|
|
+#define RETU_ADC_HOOKDET 0x05 /* Hook detection */
|
|
|
|
|
+#define RETU_ADC_RFGP 0x06 /* RF GP */
|
|
|
|
|
+#define RETU_ADC_WBTX 0x07 /* Wideband Tx detection */
|
|
|
|
|
+#define RETU_ADC_BATTVOLT 0x08 /* Battery voltage measurement */
|
|
|
|
|
+#define RETU_ADC_GND2 0x09 /* Ground */
|
|
|
|
|
+#define RETU_ADC_LIGHTSENS 0x0A /* Light sensor */
|
|
|
|
|
+#define RETU_ADC_LIGHTTEMP 0x0B /* Light sensor temperature */
|
|
|
|
|
+#define RETU_ADC_BKUPVOLT 0x0C /* Backup battery voltage */
|
|
|
|
|
+#define RETU_ADC_TEMP 0x0D /* RETU temperature */
|
|
|
|
|
+
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
int retu_read_reg(int reg);
|
|
|
|
|
-void retu_write_reg(int reg, u16 val);
|
|
|
|
|
+int retu_write_reg(int reg, u16 val);
|
|
|
|
|
void retu_set_clear_reg_bits(int reg, u16 set, u16 clear);
|
|
|
|
|
int retu_read_adc(int channel);
|
|
|
|
|
int retu_request_irq(int id, void *irq_handler, unsigned long arg, char *name);
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/arch/arm/mach-omap2/board-n8x0.c
|
2010-11-05 16:59:39 +00:00
|
|
|
|
===================================================================
|
2011-02-20 14:28:40 +00:00
|
|
|
|
--- linux-2.6.37.1.orig/arch/arm/mach-omap2/board-n8x0.c 2011-02-19 20:26:01.201344770 +0100
|
|
|
|
|
+++ linux-2.6.37.1/arch/arm/mach-omap2/board-n8x0.c 2011-02-19 20:26:01.531318368 +0100
|
2011-01-28 17:31:34 +00:00
|
|
|
|
@@ -907,6 +907,17 @@
|
2010-11-05 16:59:39 +00:00
|
|
|
|
ARRAY_SIZE(n8x0_gpio_switches));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+static struct platform_device n810_bm_device = {
|
|
|
|
|
+ .name = "n810bm",
|
|
|
|
|
+ .id = -1,
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+static void __init n810_bm_init(void)
|
|
|
|
|
+{
|
|
|
|
|
+ if (platform_device_register(&n810_bm_device))
|
|
|
|
|
+ BUG();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
static void __init n8x0_init_machine(void)
|
|
|
|
|
{
|
|
|
|
|
omap2420_mux_init(board_mux, OMAP_PACKAGE_ZAC);
|
2011-01-28 17:31:34 +00:00
|
|
|
|
@@ -933,6 +944,8 @@
|
2010-11-05 16:59:39 +00:00
|
|
|
|
n8x0_onenand_init();
|
|
|
|
|
n8x0_mmc_init();
|
|
|
|
|
n8x0_usb_init();
|
|
|
|
|
+
|
|
|
|
|
+ n810_bm_init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MACHINE_START(NOKIA_N800, "Nokia N800")
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/lipocharge.c
|
2010-11-05 16:59:39 +00:00
|
|
|
|
===================================================================
|
|
|
|
|
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/lipocharge.c 2011-02-19 20:26:01.531318368 +0100
|
2011-02-09 12:53:37 +00:00
|
|
|
|
@@ -0,0 +1,183 @@
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+/*
|
|
|
|
|
+ * Generic LIPO battery charger
|
|
|
|
|
+ *
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ * Copyright (c) 2010-2011 Michael Buesch <mb@bu3sch.de>
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ *
|
|
|
|
|
+ * This program is free software; you can redistribute it and/or
|
|
|
|
|
+ * modify it under the terms of the GNU General Public License
|
|
|
|
|
+ * as published by the Free Software Foundation; either version 2
|
|
|
|
|
+ * of the License, or (at your option) any later version.
|
|
|
|
|
+ *
|
|
|
|
|
+ * This program is distributed in the hope that it will be useful,
|
|
|
|
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
+ * GNU General Public License for more details.
|
|
|
|
|
+ */
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+#define DEBUG
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+#include "lipocharge.h"
|
|
|
|
|
+
|
|
|
|
|
+#include <linux/slab.h>
|
|
|
|
|
+
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+/* Hysteresis constants */
|
|
|
|
|
+#define CURRENT_HYST 30 /* mA */
|
|
|
|
|
+#define VOLTAGE_HYST 10 /* mV */
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+/* Threshold constants */
|
|
|
|
|
+#define FINISH_CURRENT_PERCENT 3
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/* Returns the requested first-stage charge current in mA */
|
|
|
|
|
+static inline unsigned int get_stage1_charge_current(struct lipocharge *c)
|
|
|
|
|
+{
|
|
|
|
|
+ /* current = (capacity * C) */
|
|
|
|
|
+ return c->capacity * c->rate / 1000;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+void lipocharge_init(struct lipocharge *c, struct device *dev)
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ c->dev = dev;
|
|
|
|
|
+ c->state = LIPO_IDLE;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+void lipocharge_exit(struct lipocharge *c)
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ c->state = LIPO_IDLE;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int lipocharge_start(struct lipocharge *c)
|
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ int err;
|
|
|
|
|
+
|
|
|
|
|
+ if (c->state != LIPO_IDLE)
|
|
|
|
|
+ return -EBUSY;
|
|
|
|
|
+ if (!c->set_current_pwm || !c->emergency)
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ return -EINVAL;
|
|
|
|
|
+ if (!c->top_voltage || c->top_voltage > 4200)
|
|
|
|
|
+ return -EINVAL;
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+
|
|
|
|
|
+ c->active_duty_cycle = 0;
|
|
|
|
|
+ err = c->set_current_pwm(c, c->active_duty_cycle);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ return err;
|
|
|
|
|
+ c->state = LIPO_FIRST_STAGE;
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void lipocharge_stop(struct lipocharge *c)
|
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ if (c->state == LIPO_IDLE)
|
|
|
|
|
+ return;
|
|
|
|
|
+ c->state = LIPO_IDLE;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int lipocharge_increase_current(struct lipocharge *c,
|
|
|
|
|
+ unsigned int inc_permille)
|
|
|
|
|
+{
|
|
|
|
|
+ int old_pwm, new_pwm;
|
|
|
|
|
+
|
|
|
|
|
+ if (c->active_duty_cycle >= c->duty_cycle_max)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+
|
|
|
|
|
+ old_pwm = c->active_duty_cycle;
|
|
|
|
|
+ new_pwm = old_pwm + (c->duty_cycle_max * inc_permille / 1000);
|
|
|
|
|
+ new_pwm = min(new_pwm, (int)c->duty_cycle_max);
|
|
|
|
|
+ c->active_duty_cycle = new_pwm;
|
|
|
|
|
+
|
|
|
|
|
+ dev_dbg(c->dev, "lipo: Increasing duty_cycle by "
|
|
|
|
|
+ "%u permille (0x%02X -> 0x%02X)",
|
|
|
|
|
+ inc_permille, old_pwm, new_pwm);
|
|
|
|
|
+
|
|
|
|
|
+ return c->set_current_pwm(c, c->active_duty_cycle);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int lipocharge_decrease_current(struct lipocharge *c,
|
|
|
|
|
+ unsigned int dec_permille)
|
|
|
|
|
+{
|
|
|
|
|
+ int old_pwm, new_pwm;
|
|
|
|
|
+
|
|
|
|
|
+ if (c->active_duty_cycle <= 0)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+
|
|
|
|
|
+ old_pwm = c->active_duty_cycle;
|
|
|
|
|
+ new_pwm = old_pwm - (c->duty_cycle_max * dec_permille / 1000);
|
|
|
|
|
+ new_pwm = max(0, new_pwm);
|
|
|
|
|
+ c->active_duty_cycle = new_pwm;
|
|
|
|
|
+
|
|
|
|
|
+ dev_dbg(c->dev, "lipo: Decreasing duty_cycle by "
|
|
|
|
|
+ "%u permille (0x%02X -> 0x%02X)",
|
|
|
|
|
+ dec_permille, old_pwm, new_pwm);
|
|
|
|
|
+
|
|
|
|
|
+ return c->set_current_pwm(c, c->active_duty_cycle);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** lipocharge_update_state - Update the charge state
|
|
|
|
|
+ * @c: The context.
|
|
|
|
|
+ * @voltage_mV: The measured battery voltage.
|
|
|
|
|
+ * @current_mA: The measured charge current.
|
|
|
|
|
+ * negative -> drain.
|
|
|
|
|
+ * positive -> charge.
|
|
|
|
|
+ * @temp_K: Battery temperature in K.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Returns 0 on success, -1 on error.
|
|
|
|
|
+ * Returns 1, if the charging process is finished.
|
|
|
|
|
+ */
|
|
|
|
|
+int lipocharge_update_state(struct lipocharge *c,
|
|
|
|
|
+ unsigned int voltage_mV,
|
|
|
|
|
+ int current_mA,
|
|
|
|
|
+ unsigned int temp_K)
|
|
|
|
|
+{
|
|
|
|
|
+ int requested_current, current_diff;
|
|
|
|
|
+ int err;
|
|
|
|
|
+ unsigned int permille;
|
|
|
|
|
+
|
|
|
|
|
+ //TODO temp
|
|
|
|
|
+
|
|
|
|
|
+restart:
|
|
|
|
|
+ switch (c->state) {
|
|
|
|
|
+ case LIPO_IDLE:
|
|
|
|
|
+ dev_err(c->dev, "%s: called while idle", __func__);
|
|
|
|
|
+ return -EINVAL;
|
|
|
|
|
+ case LIPO_FIRST_STAGE: /* Constant current */
|
|
|
|
|
+//printk("GOT %u %d %u\n", voltage_mV, current_mA, temp_K);
|
|
|
|
|
+ if (voltage_mV >= c->top_voltage) {
|
|
|
|
|
+ /* Float voltage reached.
|
|
|
|
|
+ * Switch charger mode to "constant current" */
|
|
|
|
|
+ c->state = LIPO_SECOND_STAGE;
|
|
|
|
|
+ dev_dbg(c->dev, "Switched to second charging stage.");
|
|
|
|
|
+ goto restart;
|
|
|
|
|
+ }
|
|
|
|
|
+ /* Float voltage not reached, yet.
|
|
|
|
|
+ * Try to get the requested constant current. */
|
|
|
|
|
+ requested_current = get_stage1_charge_current(c);
|
|
|
|
|
+ if (current_mA < 0)
|
|
|
|
|
+ current_mA = 0;
|
|
|
|
|
+ current_diff = requested_current - current_mA;
|
|
|
|
|
+ if (abs(requested_current - current_mA) > CURRENT_HYST) {
|
|
|
|
|
+ if (current_diff > 0) {
|
|
|
|
|
+ /* Increase current */
|
|
|
|
|
+ permille = current_diff * 1000 / requested_current;
|
|
|
|
|
+ permille /= 2;
|
|
|
|
|
+ err = lipocharge_increase_current(c, permille);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ return err;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ /* Decrease current */
|
|
|
|
|
+ permille = (-current_diff) * 1000 / requested_current;
|
|
|
|
|
+ permille /= 2;
|
|
|
|
|
+ err = lipocharge_decrease_current(c, permille);
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ return err;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ case LIPO_SECOND_STAGE: /* Constant voltage */
|
|
|
|
|
+ //TODO
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+}
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/lipocharge.h
|
2010-11-05 16:59:39 +00:00
|
|
|
|
===================================================================
|
|
|
|
|
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
|
2011-02-20 14:28:40 +00:00
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/lipocharge.h 2011-02-19 20:26:01.531318368 +0100
|
2011-02-09 12:53:37 +00:00
|
|
|
|
@@ -0,0 +1,60 @@
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+#ifndef LIPOCHARGE_H_
|
|
|
|
|
+#define LIPOCHARGE_H_
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+#include <linux/types.h>
|
|
|
|
|
+#include <linux/device.h>
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+#define LIPORATE(a,b) (((a) * 1000) + ((b) * 100))
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+#define LIPORATE_p6C LIPORATE(0,6) /* 0.6C */
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+enum lipocharge_state {
|
|
|
|
|
+ LIPO_IDLE, /* Not charging */
|
|
|
|
|
+ LIPO_FIRST_STAGE, /* Charging: constant current */
|
|
|
|
|
+ LIPO_SECOND_STAGE, /* Charging: constant voltage */
|
|
|
|
|
+};
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+/** struct lipocharge - A generic LIPO charger
|
|
|
|
|
+ *
|
|
|
|
|
+ * @capacity: Battery capacity in mAh.
|
|
|
|
|
+ * @rate: Charge rate.
|
|
|
|
|
+ * @top_voltage: Fully charged voltage, in mV.
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ * @duty_cycle_max: Max value for duty_cycle.
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ *
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ * @set_charge_current: Set the charge current PWM duty cycle.
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ * @emergency: Something went wrong. Force shutdown.
|
|
|
|
|
+ */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+struct lipocharge {
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ unsigned int capacity;
|
|
|
|
|
+ unsigned int rate;
|
|
|
|
|
+ unsigned int top_voltage;
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ unsigned int duty_cycle_max;
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ int (*set_current_pwm)(struct lipocharge *c, unsigned int duty_cycle);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+ void (*emergency)(struct lipocharge *c);
|
|
|
|
|
+
|
|
|
|
|
+ /* internal */
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ struct device *dev;
|
|
|
|
|
+ enum lipocharge_state state;
|
|
|
|
|
+ unsigned int active_duty_cycle;
|
|
|
|
|
+
|
|
|
|
|
+ //TODO implement timer to cut power after maximum charge time.
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+};
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+void lipocharge_init(struct lipocharge *c, struct device *dev);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+void lipocharge_exit(struct lipocharge *c);
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+
|
|
|
|
|
+int lipocharge_start(struct lipocharge *c);
|
|
|
|
|
+void lipocharge_stop(struct lipocharge *c);
|
|
|
|
|
+
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+int lipocharge_update_state(struct lipocharge *c,
|
|
|
|
|
+ unsigned int voltage_mV,
|
|
|
|
|
+ int current_mA,
|
|
|
|
|
+ unsigned int temp_K);
|
|
|
|
|
+
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+static inline bool lipocharge_is_charging(struct lipocharge *c)
|
|
|
|
|
+{
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+ return (c->state != LIPO_IDLE);
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+}
|
|
|
|
|
+
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+#endif /* LIPOCHARGE_H_ */
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/tahvo.h
|
2010-11-05 16:59:39 +00:00
|
|
|
|
===================================================================
|
2011-02-20 14:28:40 +00:00
|
|
|
|
--- linux-2.6.37.1.orig/drivers/cbus/tahvo.h 2011-02-19 20:26:01.256340370 +0100
|
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/tahvo.h 2011-02-19 20:26:01.532318288 +0100
|
2011-02-06 12:44:12 +00:00
|
|
|
|
@@ -30,17 +30,28 @@
|
2010-11-05 16:59:39 +00:00
|
|
|
|
#define TAHVO_REG_IDR 0x01 /* Interrupt ID */
|
|
|
|
|
#define TAHVO_REG_IDSR 0x02 /* Interrupt status */
|
|
|
|
|
#define TAHVO_REG_IMR 0x03 /* Interrupt mask */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+#define TAHVO_REG_CHGCURR 0x04 /* Charge current control PWM (8-bit) */
|
2010-11-05 16:59:39 +00:00
|
|
|
|
#define TAHVO_REG_LEDPWMR 0x05 /* LED PWM */
|
|
|
|
|
#define TAHVO_REG_USBR 0x06 /* USB control */
|
|
|
|
|
+#define TAHVO_REG_CHGCTL 0x08 /* Charge control register */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+#define TAHVO_REG_CHGCTL_EN 0x0001 /* Global charge enable */
|
2011-02-09 12:53:37 +00:00
|
|
|
|
+#define TAHVO_REG_CHGCTL_PWMOVR 0x0004 /* PWM override. Force charge PWM to 0%/100% duty cycle. */
|
|
|
|
|
+#define TAHVO_REG_CHGCTL_PWMOVRZERO 0x0008 /* If set, PWM override is 0% (If unset -> 100%) */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
+#define TAHVO_REG_CHGCTL_CURMEAS 0x0040 /* Enable battery current measurement. */
|
|
|
|
|
+#define TAHVO_REG_CHGCTL_CURTIMRST 0x0080 /* Current measure timer reset. */
|
|
|
|
|
+#define TAHVO_REG_BATCURRTIMER 0x0c /* Battery current measure timer (8-bit) */
|
2010-11-05 16:59:39 +00:00
|
|
|
|
+#define TAHVO_REG_BATCURR 0x0d /* Battery (dis)charge current (signed 16-bit) */
|
|
|
|
|
+
|
|
|
|
|
#define TAHVO_REG_MAX 0x0d
|
|
|
|
|
|
|
|
|
|
/* Interrupt sources */
|
2011-02-06 12:44:12 +00:00
|
|
|
|
#define TAHVO_INT_VBUSON 0
|
|
|
|
|
+#define TAHVO_INT_BATCURR 7 /* Battery current measure timer */
|
|
|
|
|
|
|
|
|
|
#define MAX_TAHVO_IRQ_HANDLERS 8
|
|
|
|
|
|
|
|
|
|
int tahvo_read_reg(int reg);
|
|
|
|
|
-void tahvo_write_reg(int reg, u16 val);
|
|
|
|
|
+int tahvo_write_reg(int reg, u16 val);
|
|
|
|
|
void tahvo_set_clear_reg_bits(int reg, u16 set, u16 clear);
|
|
|
|
|
int tahvo_request_irq(int id, void *irq_handler, unsigned long arg, char *name);
|
|
|
|
|
void tahvo_free_irq(int id);
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/tahvo.c
|
2011-02-06 12:44:12 +00:00
|
|
|
|
===================================================================
|
2011-02-20 14:28:40 +00:00
|
|
|
|
--- linux-2.6.37.1.orig/drivers/cbus/tahvo.c 2011-02-19 20:26:01.256340370 +0100
|
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/tahvo.c 2011-02-19 20:26:01.532318288 +0100
|
2011-02-06 12:44:12 +00:00
|
|
|
|
@@ -85,10 +85,10 @@
|
|
|
|
|
*
|
|
|
|
|
* This function writes a value to the specified register
|
|
|
|
|
*/
|
|
|
|
|
-void tahvo_write_reg(int reg, u16 val)
|
|
|
|
|
+int tahvo_write_reg(int reg, u16 val)
|
|
|
|
|
{
|
|
|
|
|
BUG_ON(!tahvo_initialized);
|
|
|
|
|
- cbus_write_reg(cbus_host, TAHVO_ID, reg, val);
|
|
|
|
|
+ return cbus_write_reg(cbus_host, TAHVO_ID, reg, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/cbus.c
|
2011-02-09 12:53:37 +00:00
|
|
|
|
===================================================================
|
2011-02-20 14:28:40 +00:00
|
|
|
|
--- linux-2.6.37.1.orig/drivers/cbus/cbus.c 2011-02-19 20:26:01.249340930 +0100
|
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/cbus.c 2011-02-19 20:26:01.533318208 +0100
|
2011-02-09 12:53:37 +00:00
|
|
|
|
@@ -31,6 +31,7 @@
|
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
+#include <linux/reboot.h>
|
|
|
|
|
|
|
|
|
|
#include <asm/io.h>
|
|
|
|
|
#include <asm/mach-types.h>
|
|
|
|
|
@@ -301,6 +302,13 @@
|
|
|
|
|
}
|
|
|
|
|
module_exit(cbus_bus_exit);
|
|
|
|
|
|
|
|
|
|
+void cbus_emergency(void)
|
|
|
|
|
+{
|
|
|
|
|
+ machine_power_off();
|
|
|
|
|
+ panic("cbus: Failed to halt machine in emergency state\n");
|
|
|
|
|
+}
|
|
|
|
|
+EXPORT_SYMBOL(cbus_emergency);
|
|
|
|
|
+
|
|
|
|
|
MODULE_DESCRIPTION("CBUS serial protocol");
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
MODULE_AUTHOR("Juha Yrj<72>l<EFBFBD>");
|
2011-02-20 14:28:40 +00:00
|
|
|
|
Index: linux-2.6.37.1/drivers/cbus/cbus.h
|
2011-02-09 12:53:37 +00:00
|
|
|
|
===================================================================
|
2011-02-20 14:28:40 +00:00
|
|
|
|
--- linux-2.6.37.1.orig/drivers/cbus/cbus.h 2011-02-19 20:26:01.250340850 +0100
|
|
|
|
|
+++ linux-2.6.37.1/drivers/cbus/cbus.h 2011-02-19 20:26:01.533318208 +0100
|
2011-02-09 12:53:37 +00:00
|
|
|
|
@@ -33,4 +33,6 @@
|
|
|
|
|
extern int cbus_read_reg(struct cbus_host *host, int dev, int reg);
|
|
|
|
|
extern int cbus_write_reg(struct cbus_host *host, int dev, int reg, u16 val);
|
|
|
|
|
|
|
|
|
|
+NORET_TYPE void cbus_emergency(void) ATTRIB_NORET;
|
|
|
|
|
+
|
|
|
|
|
#endif /* __DRIVERS_CBUS_CBUS_H */
|