summaryrefslogtreecommitdiff
path: root/drivers/mxc/pmic/mc13892
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mxc/pmic/mc13892')
-rw-r--r--drivers/mxc/pmic/mc13892/Kconfig48
-rw-r--r--drivers/mxc/pmic/mc13892/Makefile10
-rw-r--r--drivers/mxc/pmic/mc13892/pmic_adc.c1073
-rw-r--r--drivers/mxc/pmic/mc13892/pmic_battery.c797
-rw-r--r--drivers/mxc/pmic/mc13892/pmic_light.c685
5 files changed, 2613 insertions, 0 deletions
diff --git a/drivers/mxc/pmic/mc13892/Kconfig b/drivers/mxc/pmic/mc13892/Kconfig
new file mode 100644
index 000000000000..930e06ab2282
--- /dev/null
+++ b/drivers/mxc/pmic/mc13892/Kconfig
@@ -0,0 +1,48 @@
+#
+# PMIC Modules configuration
+#
+
+config MXC_MC13892_ADC
+ tristate "MC13892 ADC support"
+ depends on MXC_PMIC_MC13892
+ ---help---
+ This is the MC13892 ADC module driver. This module provides kernel API
+ for the ADC system of MC13892.
+ It controls also the touch screen interface.
+ If you want MC13892 ADC support, you should say Y here
+
+config MXC_MC13892_RTC
+ tristate "MC13892 Real Time Clock (RTC) support"
+ depends on MXC_PMIC_MC13892
+ ---help---
+ This is the MC13892 RTC module driver. This module provides kernel API
+ for RTC part of MC13892.
+ If you want MC13892 RTC support, you should say Y here
+config MXC_MC13892_LIGHT
+ tristate "MC13892 Light and Backlight support"
+ depends on MXC_PMIC_MC13892
+ ---help---
+ This is the MC13892 Light module driver. This module provides kernel API
+ for led and backlight control part of MC13892.
+ If you want MC13892 Light support, you should say Y here
+config MXC_MC13892_BATTERY
+ tristate "MC13892 Battery API support"
+ depends on MXC_PMIC_MC13892
+ ---help---
+ This is the MC13892 battery module driver. This module provides kernel API
+ for battery control part of MC13892.
+ If you want MC13892 battery support, you should say Y here
+config MXC_MC13892_CONNECTIVITY
+ tristate "MC13892 Connectivity API support"
+ depends on MXC_PMIC_MC13892
+ ---help---
+ This is the MC13892 connectivity module driver. This module provides kernel API
+ for USB/RS232 connectivity control part of MC13892.
+ If you want MC13892 connectivity support, you should say Y here
+config MXC_MC13892_POWER
+ tristate "MC13892 Power API support"
+ depends on MXC_PMIC_MC13892
+ ---help---
+ This is the MC13892 power and supplies module driver. This module provides kernel API
+ for power and regulator control part of MC13892.
+ If you want MC13892 power support, you should say Y here
diff --git a/drivers/mxc/pmic/mc13892/Makefile b/drivers/mxc/pmic/mc13892/Makefile
new file mode 100644
index 000000000000..0ed2b7eb4c11
--- /dev/null
+++ b/drivers/mxc/pmic/mc13892/Makefile
@@ -0,0 +1,10 @@
+#
+# Makefile for the mc13783 pmic drivers.
+#
+
+obj-$(CONFIG_MXC_MC13892_ADC) += pmic_adc.o
+#obj-$(CONFIG_MXC_MC13892_RTC) += pmic_rtc.o
+obj-$(CONFIG_MXC_MC13892_LIGHT) += pmic_light.o
+obj-$(CONFIG_MXC_MC13892_BATTERY) += pmic_battery.o
+#obj-$(CONFIG_MXC_MC13892_CONNECTIVITY) += pmic_convity.o
+#obj-$(CONFIG_MXC_MC13892_POWER) += pmic_power.o
diff --git a/drivers/mxc/pmic/mc13892/pmic_adc.c b/drivers/mxc/pmic/mc13892/pmic_adc.c
new file mode 100644
index 000000000000..c0f0f069b902
--- /dev/null
+++ b/drivers/mxc/pmic/mc13892/pmic_adc.c
@@ -0,0 +1,1073 @@
+/*
+ * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+#include <linux/platform_device.h>
+#include <linux/poll.h>
+#include <linux/sched.h>
+#include <linux/time.h>
+#include <linux/delay.h>
+#include <linux/wait.h>
+#include <linux/device.h>
+
+#include <linux/pmic_adc.h>
+#include <linux/pmic_status.h>
+
+#include "../core/pmic.h"
+
+#define DEF_ADC_0 0x008000
+#define DEF_ADC_3 0x0001c0
+
+#define ADC_NB_AVAILABLE 2
+
+#define MAX_CHANNEL 7
+
+#define MC13892_ADC0_TS_M_LSH 14
+#define MC13892_ADC0_TS_M_WID 3
+
+/*
+ * Maximun allowed variation in the three X/Y co-ordinates acquired from
+ * touch-screen
+ */
+#define DELTA_Y_MAX 50
+#define DELTA_X_MAX 50
+
+/*
+ * ADC 0
+ */
+#define ADC_CHRGICON 0x000002
+#define ADC_WAIT_TSI_0 0x001400
+
+#define ADC_INC 0x030000
+#define ADC_BIS 0x800000
+#define ADC_CHRGRAW_D5 0x008000
+
+/*
+ * ADC 1
+ */
+
+#define ADC_EN 0x000001
+#define ADC_SGL_CH 0x000002
+#define ADC_ADCCAL 0x000004
+#define ADC_ADSEL 0x000008
+#define ADC_TRIGMASK 0x000010
+#define ADC_CH_0_POS 5
+#define ADC_CH_0_MASK 0x0000E0
+#define ADC_CH_1_POS 8
+#define ADC_CH_1_MASK 0x000700
+#define ADC_DELAY_POS 11
+#define ADC_DELAY_MASK 0x07F800
+#define ADC_ATO 0x080000
+#define ASC_ADC 0x100000
+#define ADC_WAIT_TSI_1 0x200001
+#define ADC_NO_ADTRIG 0x200000
+
+/*
+ * ADC 2 - 4
+ */
+#define ADD1_RESULT_MASK 0x00000FFC
+#define ADD2_RESULT_MASK 0x00FFC000
+#define ADC_TS_MASK 0x00FFCFFC
+
+#define ADC_WCOMP 0x040000
+#define ADC_WCOMP_H_POS 0
+#define ADC_WCOMP_L_POS 9
+#define ADC_WCOMP_H_MASK 0x00003F
+#define ADC_WCOMP_L_MASK 0x007E00
+
+#define ADC_MODE_MASK 0x00003F
+
+#define ADC_INT_BISDONEI 0x02
+#define ADC_TSMODE_MASK 0x007000
+
+typedef enum adc_state {
+ ADC_FREE,
+ ADC_USED,
+ ADC_MONITORING,
+} t_adc_state;
+
+typedef enum reading_mode {
+ /*!
+ * Enables lithium cell reading
+ */
+ M_LITHIUM_CELL = 0x000001,
+ /*!
+ * Enables charge current reading
+ */
+ M_CHARGE_CURRENT = 0x000002,
+ /*!
+ * Enables battery current reading
+ */
+ M_BATTERY_CURRENT = 0x000004,
+} t_reading_mode;
+
+typedef struct {
+ /*!
+ * Delay before first conversion
+ */
+ unsigned int delay;
+ /*!
+ * sets the ATX bit for delay on all conversion
+ */
+ bool conv_delay;
+ /*!
+ * Sets the single channel mode
+ */
+ bool single_channel;
+ /*!
+ * Channel selection 1
+ */
+ t_channel channel_0;
+ /*!
+ * Channel selection 2
+ */
+ t_channel channel_1;
+ /*!
+ * Used to configure ADC mode with t_reading_mode
+ */
+ t_reading_mode read_mode;
+ /*!
+ * Sets the Touch screen mode
+ */
+ bool read_ts;
+ /*!
+ * Wait TSI event before touch screen reading
+ */
+ bool wait_tsi;
+ /*!
+ * Sets CHRGRAW scaling to divide by 5
+ * Only supported on 2.0 and higher
+ */
+ bool chrgraw_devide_5;
+ /*!
+ * Return ADC values
+ */
+ unsigned int value[8];
+ /*!
+ * Return touch screen values
+ */
+ t_touch_screen ts_value;
+} t_adc_param;
+
+static int pmic_adc_filter(t_touch_screen *ts_curr);
+int mc13892_adc_request(bool read_ts);
+int mc13892_adc_release(int adc_index);
+t_reading_mode mc13892_set_read_mode(t_channel channel);
+PMIC_STATUS mc13892_adc_read_ts(t_touch_screen *touch_sample, int wait_tsi);
+
+/* internal function */
+static void callback_tsi(void *);
+static void callback_adcdone(void *);
+static void callback_adcbisdone(void *);
+
+static int swait;
+
+static int suspend_flag;
+
+static wait_queue_head_t suspendq;
+
+/* EXPORTED FUNCTIONS */
+EXPORT_SYMBOL(pmic_adc_init);
+EXPORT_SYMBOL(pmic_adc_deinit);
+EXPORT_SYMBOL(pmic_adc_convert);
+EXPORT_SYMBOL(pmic_adc_convert_8x);
+EXPORT_SYMBOL(pmic_adc_set_touch_mode);
+EXPORT_SYMBOL(pmic_adc_get_touch_mode);
+EXPORT_SYMBOL(pmic_adc_get_touch_sample);
+
+static DECLARE_COMPLETION(adcdone_it);
+static DECLARE_COMPLETION(adcbisdone_it);
+static DECLARE_COMPLETION(adc_tsi);
+static pmic_event_callback_t tsi_event;
+static pmic_event_callback_t event_adc;
+static pmic_event_callback_t event_adc_bis;
+static bool data_ready_adc_1;
+static bool data_ready_adc_2;
+static bool adc_ts;
+static bool wait_ts;
+static bool monitor_en;
+static bool monitor_adc;
+static DECLARE_MUTEX(convert_mutex);
+
+static DECLARE_WAIT_QUEUE_HEAD(queue_adc_busy);
+static t_adc_state adc_dev[2];
+
+static unsigned channel_num[] = {
+ 0,
+ 1,
+ 3,
+ 4,
+ 2,
+ 0,
+ 1,
+ 3,
+ 4,
+ -1,
+ 5,
+ 6,
+ 7,
+ -1,
+ -1,
+ -1,
+ -1,
+ -1,
+ -1,
+ -1,
+ -1,
+ -1,
+ -1,
+ -1,
+ -1
+};
+
+static bool pmic_adc_ready;
+
+int is_pmic_adc_ready()
+{
+ return pmic_adc_ready;
+}
+EXPORT_SYMBOL(is_pmic_adc_ready);
+
+
+static int pmic_adc_suspend(struct platform_device *pdev, pm_message_t state)
+{
+ suspend_flag = 1;
+ CHECK_ERROR(pmic_write_reg(REG_ADC0, DEF_ADC_0, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_ADC1, 0, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_ADC2, 0, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_ADC3, DEF_ADC_3, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_ADC4, 0, PMIC_ALL_BITS));
+
+ return 0;
+};
+
+static int pmic_adc_resume(struct platform_device *pdev)
+{
+ /* nothing for mc13892 adc */
+ unsigned int adc_0_reg, adc_1_reg, reg_mask;
+ suspend_flag = 0;
+
+ /* let interrupt of TSI again */
+ adc_0_reg = ADC_WAIT_TSI_0;
+ reg_mask = ADC_WAIT_TSI_0;
+ CHECK_ERROR(pmic_write_reg(REG_ADC0, adc_0_reg, reg_mask));
+ adc_1_reg = ADC_WAIT_TSI_1 | (ADC_BIS * adc_ts);
+ CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg, PMIC_ALL_BITS));
+
+ while (swait > 0) {
+ swait--;
+ wake_up_interruptible(&suspendq);
+ }
+
+ return 0;
+};
+
+static void callback_tsi(void *unused)
+{
+ pr_debug("*** TSI IT mc13892 PMIC_ADC_GET_TOUCH_SAMPLE ***\n");
+ if (wait_ts) {
+ complete(&adc_tsi);
+ pmic_event_mask(EVENT_TSI);
+ }
+}
+
+static void callback_adcdone(void *unused)
+{
+ if (data_ready_adc_1)
+ complete(&adcdone_it);
+}
+
+static void callback_adcbisdone(void *unused)
+{
+ pr_debug("* adcdone bis it callback *\n");
+ if (data_ready_adc_2)
+ complete(&adcbisdone_it);
+}
+
+static int pmic_adc_filter(t_touch_screen *ts_curr)
+{
+ unsigned int ydiff, xdiff;
+ unsigned int sample_sumx, sample_sumy;
+
+ if (ts_curr->contact_resistance == 0) {
+ ts_curr->x_position = 0;
+ ts_curr->y_position = 0;
+ return 0;
+ }
+
+ ydiff = abs(ts_curr->y_position1 - ts_curr->y_position2);
+ if (ydiff > DELTA_Y_MAX) {
+ pr_debug("pmic_adc_filter: Ret pos y\n");
+ return -1;
+ }
+
+ xdiff = abs(ts_curr->x_position1 - ts_curr->x_position2);
+ if (xdiff > DELTA_X_MAX) {
+ pr_debug("mc13892_adc_filter: Ret pos x\n");
+ return -1;
+ }
+
+ sample_sumx = ts_curr->x_position1 + ts_curr->x_position2;
+ sample_sumy = ts_curr->y_position1 + ts_curr->y_position2;
+
+ ts_curr->y_position = sample_sumy / 2;
+ ts_curr->x_position = sample_sumx / 2;
+
+ return 0;
+}
+
+int pmic_adc_init(void)
+{
+ unsigned int reg_value = 0, i = 0;
+
+ if (suspend_flag == 1)
+ return -EBUSY;
+
+ for (i = 0; i < ADC_NB_AVAILABLE; i++)
+ adc_dev[i] = ADC_FREE;
+
+ CHECK_ERROR(pmic_write_reg(REG_ADC0, DEF_ADC_0, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_ADC1, 0, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_ADC2, 0, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_ADC3, DEF_ADC_3, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_ADC4, 0, PMIC_ALL_BITS));
+ reg_value = 0x001000;
+
+ data_ready_adc_1 = false;
+ data_ready_adc_2 = false;
+ adc_ts = false;
+ wait_ts = false;
+ monitor_en = false;
+ monitor_adc = false;
+
+ /* sub to ADCDone IT */
+ event_adc.param = NULL;
+ event_adc.func = callback_adcdone;
+ CHECK_ERROR(pmic_event_subscribe(EVENT_ADCDONEI, event_adc));
+
+ /* sub to ADCDoneBis IT */
+ event_adc_bis.param = NULL;
+ event_adc_bis.func = callback_adcbisdone;
+ CHECK_ERROR(pmic_event_subscribe(EVENT_ADCBISDONEI, event_adc_bis));
+
+ /* sub to Touch Screen IT */
+ tsi_event.param = NULL;
+ tsi_event.func = callback_tsi;
+ CHECK_ERROR(pmic_event_subscribe(EVENT_TSI, tsi_event));
+
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS pmic_adc_deinit(void)
+{
+ CHECK_ERROR(pmic_event_unsubscribe(EVENT_ADCDONEI, event_adc));
+ CHECK_ERROR(pmic_event_unsubscribe(EVENT_ADCBISDONEI, event_adc_bis));
+ CHECK_ERROR(pmic_event_unsubscribe(EVENT_TSI, tsi_event));
+
+ return PMIC_SUCCESS;
+}
+
+int mc13892_adc_init_param(t_adc_param *adc_param)
+{
+ int i = 0;
+
+ if (suspend_flag == 1)
+ return -EBUSY;
+
+ adc_param->delay = 0;
+ adc_param->conv_delay = false;
+ adc_param->single_channel = false;
+ adc_param->channel_0 = BATTERY_VOLTAGE;
+ adc_param->channel_1 = BATTERY_VOLTAGE;
+ adc_param->read_mode = 0;
+ adc_param->wait_tsi = 0;
+ adc_param->chrgraw_devide_5 = true;
+ adc_param->read_ts = false;
+ adc_param->ts_value.x_position = 0;
+ adc_param->ts_value.y_position = 0;
+ adc_param->ts_value.contact_resistance = 0;
+ for (i = 0; i <= MAX_CHANNEL; i++)
+ adc_param->value[i] = 0;
+
+ return 0;
+}
+
+PMIC_STATUS mc13892_adc_convert(t_adc_param *adc_param)
+{
+ bool use_bis = false;
+ unsigned int adc_0_reg = 0, adc_1_reg = 0, result_reg = 0, i = 0;
+ unsigned int result = 0, temp = 0;
+ pmic_version_t mc13892_ver;
+ int ret;
+
+ pr_debug("mc13892 ADC - mc13892_adc_convert ....\n");
+ if (suspend_flag == 1)
+ return -EBUSY;
+
+ if (adc_param->wait_tsi) {
+ /* configure adc to wait tsi interrupt */
+ INIT_COMPLETION(adc_tsi);
+
+ /*for ts don't use bis */
+ /*put ts in interrupt mode */
+ /* still kep reference? */
+ adc_0_reg = 0x001400 | (ADC_BIS * 0);
+ pmic_event_unmask(EVENT_TSI);
+ CHECK_ERROR(pmic_write_reg(REG_ADC0, adc_0_reg, PMIC_ALL_BITS));
+ /*for ts don't use bis */
+ adc_1_reg = 0x200001 | (ADC_BIS * 0);
+ CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg, PMIC_ALL_BITS));
+ pr_debug("wait tsi ....\n");
+ wait_ts = true;
+ wait_for_completion_interruptible(&adc_tsi);
+ wait_ts = false;
+ }
+ down(&convert_mutex);
+ use_bis = mc13892_adc_request(adc_param->read_ts);
+ if (use_bis < 0) {
+ pr_debug("process has received a signal and got interrupted\n");
+ ret = -EINTR;
+ goto out_up_convert_mutex;
+ }
+
+ /* CONFIGURE ADC REG 0 */
+ adc_0_reg = 0;
+ adc_1_reg = 0;
+ if (adc_param->read_ts == false) {
+ adc_0_reg = adc_param->read_mode & 0x00003F;
+ /* add auto inc */
+ adc_0_reg |= ADC_INC;
+ if (use_bis) {
+ /* add adc bis */
+ adc_0_reg |= ADC_BIS;
+ }
+ mc13892_ver = pmic_get_version();
+ if (mc13892_ver.revision >= 20)
+ if (adc_param->chrgraw_devide_5)
+ adc_0_reg |= ADC_CHRGRAW_D5;
+
+ if (adc_param->single_channel)
+ adc_1_reg |= ADC_SGL_CH;
+
+ if (adc_param->conv_delay)
+ adc_1_reg |= ADC_ATO;
+
+ if (adc_param->single_channel)
+ adc_1_reg |= ADC_SGL_CH;
+
+ adc_1_reg |= (adc_param->channel_0 << ADC_CH_0_POS) &
+ ADC_CH_0_MASK;
+ adc_1_reg |= (adc_param->channel_1 << ADC_CH_1_POS) &
+ ADC_CH_1_MASK;
+ } else {
+ adc_0_reg = 0x002400 | (ADC_BIS * use_bis) | ADC_INC;
+ }
+
+ if (adc_param->channel_0 == channel_num[CHARGE_CURRENT])
+ adc_0_reg |= ADC_CHRGICON;
+
+ /*Change has been made here */
+ ret = pmic_write_reg(REG_ADC0, adc_0_reg,
+ ADC_INC | ADC_BIS | ADC_CHRGRAW_D5 | 0xfff00ff);
+ if (ret != PMIC_SUCCESS) {
+ pr_debug("pmic_write_reg");
+ goto out_mc13892_adc_release;
+ }
+
+ /* CONFIGURE ADC REG 1 */
+ if (adc_param->read_ts == false) {
+ adc_1_reg |= ADC_NO_ADTRIG;
+ adc_1_reg |= ADC_EN;
+ adc_1_reg |= (adc_param->delay << ADC_DELAY_POS) &
+ ADC_DELAY_MASK;
+ if (use_bis)
+ adc_1_reg |= ADC_BIS;
+ } else {
+ /* configure and start convert to read x and y position */
+ /* configure to read 2 value in channel selection 1 & 2 */
+ adc_1_reg = 0x100409 | (ADC_BIS * use_bis) | ADC_NO_ADTRIG;
+ /* set ATOx = 5, it could be better for ts ADC */
+ adc_1_reg |= 0x002800;
+ }
+
+ if (adc_param->channel_0 == channel_num[CHARGE_CURRENT]) {
+ adc_param->channel_1 = channel_num[CHARGE_CURRENT];
+ adc_1_reg &= ~(ADC_CH_0_MASK | ADC_CH_1_MASK | ADC_NO_ADTRIG |
+ ADC_TRIGMASK | ADC_EN | ADC_SGL_CH | ADC_ADCCAL);
+ adc_1_reg |= ((adc_param->channel_0 << ADC_CH_0_POS) |
+ (adc_param->channel_1 << ADC_CH_1_POS));
+ adc_1_reg |= (ADC_EN | ADC_SGL_CH | ADC_ADCCAL);
+
+ if (use_bis == 0) {
+ CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg,
+ 0xFFFFFF));
+ } else {
+ CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg,
+ 0xFFFFFF));
+ temp = 0x800000;
+ CHECK_ERROR(pmic_write_reg(REG_ADC3, temp, 0xFFFFFF));
+ }
+
+ adc_1_reg &= ~(ADC_NO_ADTRIG | ASC_ADC | ADC_ADCCAL);
+ adc_1_reg |= (ADC_NO_ADTRIG | ASC_ADC);
+ if (use_bis == 0) {
+ data_ready_adc_1 = true;
+ INIT_COMPLETION(adcdone_it);
+ CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg,
+ 0xFFFFFF));
+ pr_debug("wait adc done\n");
+ wait_for_completion_interruptible(&adcdone_it);
+ data_ready_adc_1 = false;
+ } else {
+ data_ready_adc_2 = true;
+ INIT_COMPLETION(adcbisdone_it);
+ CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg,
+ 0xFFFFFF));
+ temp = 0x800000;
+ CHECK_ERROR(pmic_write_reg(REG_ADC3, temp, 0xFFFFFF));
+ pr_debug("wait adc done bis\n");
+ wait_for_completion_interruptible(&adcbisdone_it);
+ data_ready_adc_2 = false;
+ }
+ } else {
+ if (use_bis == 0) {
+ data_ready_adc_1 = false;
+ adc_1_reg |= ASC_ADC;
+ data_ready_adc_1 = true;
+ pr_debug("Write Reg %i = %x\n", REG_ADC1, adc_1_reg);
+ INIT_COMPLETION(adcdone_it);
+ ret = pmic_write_reg(REG_ADC1, adc_1_reg,
+ ADC_SGL_CH | ADC_ATO |
+ ADC_ADSEL | ADC_CH_0_MASK |
+ ADC_CH_1_MASK |
+ ADC_NO_ADTRIG | ADC_EN |
+ ADC_DELAY_MASK | ASC_ADC |
+ ADC_BIS);
+ if (ret != PMIC_SUCCESS) {
+ pr_debug("pmic_write_reg");
+ goto out_mc13892_adc_release;
+ }
+
+ pr_debug("wait adc done\n");
+ wait_for_completion_interruptible(&adcdone_it);
+ data_ready_adc_1 = false;
+ } else {
+ data_ready_adc_2 = false;
+ adc_1_reg |= ASC_ADC;
+ data_ready_adc_2 = true;
+ INIT_COMPLETION(adcbisdone_it);
+ ret = pmic_write_reg(REG_ADC1, adc_1_reg, 0xFFFFFF);
+ if (ret != PMIC_SUCCESS) {
+ pr_debug("pmic_write_reg");
+ goto out_mc13892_adc_release;
+ }
+
+ temp = 0x800000;
+ ret = pmic_write_reg(REG_ADC3, temp, 0xFFFFFF);
+ if (ret != PMIC_SUCCESS) {
+ pr_info("pmic_write_reg");
+ goto out_mc13892_adc_release;
+ }
+
+ pr_debug("wait adc done bis\n");
+ wait_for_completion_interruptible(&adcbisdone_it);
+ data_ready_adc_2 = false;
+ }
+ }
+
+ /* read result and store in adc_param */
+ result = 0;
+ if (use_bis == 0)
+ result_reg = REG_ADC2;
+ else
+ result_reg = REG_ADC4;
+
+ ret = pmic_write_reg(REG_ADC1, 4 << ADC_CH_1_POS,
+ ADC_CH_0_MASK | ADC_CH_1_MASK);
+ if (ret != PMIC_SUCCESS) {
+ pr_debug("pmic_write_reg");
+ goto out_mc13892_adc_release;
+ }
+
+ for (i = 0; i <= 3; i++) {
+ ret = pmic_read_reg(result_reg, &result, PMIC_ALL_BITS);
+ if (ret != PMIC_SUCCESS) {
+ pr_debug("pmic_write_reg");
+ goto out_mc13892_adc_release;
+ }
+
+ adc_param->value[i] = ((result & ADD1_RESULT_MASK) >> 2);
+ adc_param->value[i + 4] = ((result & ADD2_RESULT_MASK) >> 14);
+ pr_debug("value[%d] = %d, value[%d] = %d\n",
+ i, adc_param->value[i],
+ i + 4, adc_param->value[i + 4]);
+ }
+ if (adc_param->read_ts) {
+ adc_param->ts_value.x_position = adc_param->value[0];
+ adc_param->ts_value.x_position1 = adc_param->value[0];
+ adc_param->ts_value.x_position2 = adc_param->value[1];
+ adc_param->ts_value.y_position = adc_param->value[3];
+ adc_param->ts_value.y_position1 = adc_param->value[3];
+ adc_param->ts_value.y_position2 = adc_param->value[4];
+ adc_param->ts_value.contact_resistance = adc_param->value[6];
+ ret = pmic_write_reg(REG_ADC0, 0x0, ADC_TSMODE_MASK);
+ if (ret != PMIC_SUCCESS) {
+ pr_debug("pmic_write_reg");
+ goto out_mc13892_adc_release;
+ }
+ }
+
+ /*if (adc_param->read_ts) {
+ adc_param->ts_value.x_position = adc_param->value[2];
+ adc_param->ts_value.y_position = adc_param->value[5];
+ adc_param->ts_value.contact_resistance = adc_param->value[6];
+ } */
+ ret = PMIC_SUCCESS;
+out_mc13892_adc_release:
+ mc13892_adc_release(use_bis);
+out_up_convert_mutex:
+ up(&convert_mutex);
+
+ return ret;
+}
+
+t_reading_mode mc13892_set_read_mode(t_channel channel)
+{
+ t_reading_mode read_mode = 0;
+
+ switch (channel) {
+ case CHARGE_CURRENT:
+ read_mode = M_CHARGE_CURRENT;
+ break;
+ case BATTERY_CURRENT:
+ read_mode = M_BATTERY_CURRENT;
+ break;
+ default:
+ read_mode = 0;
+ }
+
+ return read_mode;
+}
+
+PMIC_STATUS pmic_adc_convert(t_channel channel, unsigned short *result)
+{
+ t_adc_param adc_param;
+ PMIC_STATUS ret;
+ unsigned int i;
+
+ if (suspend_flag == 1)
+ return -EBUSY;
+
+ channel = channel_num[channel];
+ if (channel == -1) {
+ pr_debug("Wrong channel ID\n");
+ return PMIC_PARAMETER_ERROR;
+ }
+ mc13892_adc_init_param(&adc_param);
+ pr_debug("pmic_adc_convert\n");
+ adc_param.read_ts = false;
+ adc_param.single_channel = true;
+ adc_param.read_mode = mc13892_set_read_mode(channel);
+
+ /* Find the group */
+ if (channel <= 7)
+ adc_param.channel_0 = channel;
+ else
+ return PMIC_PARAMETER_ERROR;
+
+ ret = mc13892_adc_convert(&adc_param);
+ for (i = 0; i <= 7; i++)
+ result[i] = adc_param.value[i];
+
+ return ret;
+}
+
+PMIC_STATUS pmic_adc_convert_8x(t_channel channel, unsigned short *result)
+{
+ t_adc_param adc_param;
+ int i;
+ PMIC_STATUS ret;
+ if (suspend_flag == 1)
+ return -EBUSY;
+
+ channel = channel_num[channel];
+
+ if (channel == -1) {
+ pr_debug("Wrong channel ID\n");
+ return PMIC_PARAMETER_ERROR;
+ }
+ mc13892_adc_init_param(&adc_param);
+ pr_debug("pmic_adc_convert_8x\n");
+ adc_param.read_ts = false;
+ adc_param.single_channel = true;
+ adc_param.read_mode = mc13892_set_read_mode(channel);
+
+ if (channel <= 7) {
+ adc_param.channel_0 = channel;
+ adc_param.channel_1 = channel;
+ } else
+ return PMIC_PARAMETER_ERROR;
+
+ ret = mc13892_adc_convert(&adc_param);
+ for (i = 0; i <= 7; i++)
+ result[i] = adc_param.value[i];
+
+ return ret;
+}
+
+PMIC_STATUS pmic_adc_set_touch_mode(t_touch_mode touch_mode)
+{
+ if (suspend_flag == 1)
+ return -EBUSY;
+
+ CHECK_ERROR(pmic_write_reg(REG_ADC0,
+ BITFVAL(MC13892_ADC0_TS_M, touch_mode),
+ BITFMASK(MC13892_ADC0_TS_M)));
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS pmic_adc_get_touch_mode(t_touch_mode *touch_mode)
+{
+ unsigned int value;
+ if (suspend_flag == 1)
+ return -EBUSY;
+
+ CHECK_ERROR(pmic_read_reg(REG_ADC0, &value, PMIC_ALL_BITS));
+
+ *touch_mode = BITFEXT(value, MC13892_ADC0_TS_M);
+
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS pmic_adc_get_touch_sample(t_touch_screen *touch_sample, int wait)
+{
+ if (mc13892_adc_read_ts(touch_sample, wait) != 0)
+ return PMIC_ERROR;
+ if (0 == pmic_adc_filter(touch_sample))
+ return PMIC_SUCCESS;
+ else
+ return PMIC_ERROR;
+}
+
+PMIC_STATUS mc13892_adc_read_ts(t_touch_screen *ts_value, int wait_tsi)
+{
+ t_adc_param param;
+ pr_debug("mc13892_adc : mc13892_adc_read_ts\n");
+ if (suspend_flag == 1)
+ return -EBUSY;
+
+ if (wait_ts) {
+ pr_debug("mc13892_adc : error TS busy \n");
+ return PMIC_ERROR;
+ }
+ mc13892_adc_init_param(&param);
+ param.wait_tsi = wait_tsi;
+ param.read_ts = true;
+ if (mc13892_adc_convert(&param) != 0)
+ return PMIC_ERROR;
+ /* check if x-y is ok */
+ if (param.ts_value.contact_resistance < 1000) {
+ ts_value->x_position = param.ts_value.x_position;
+ ts_value->x_position1 = param.ts_value.x_position1;
+ ts_value->x_position2 = param.ts_value.x_position2;
+
+ ts_value->y_position = param.ts_value.y_position;
+ ts_value->y_position1 = param.ts_value.y_position1;
+ ts_value->y_position2 = param.ts_value.y_position2;
+
+ ts_value->contact_resistance =
+ param.ts_value.contact_resistance + 1;
+
+ } else {
+ ts_value->x_position = 0;
+ ts_value->y_position = 0;
+ ts_value->contact_resistance = 0;
+
+ }
+ return PMIC_SUCCESS;
+}
+
+int mc13892_adc_request(bool read_ts)
+{
+ int adc_index = -1;
+ if (read_ts != 0) {
+ /*for ts we use bis=0 */
+ if (adc_dev[0] == ADC_USED)
+ return -1;
+ /*no wait here */
+ adc_dev[0] = ADC_USED;
+ adc_index = 0;
+ } else {
+ /*for other adc use bis = 1 */
+ if (adc_dev[1] == ADC_USED) {
+ return -1;
+ /*no wait here */
+ }
+ adc_dev[1] = ADC_USED;
+ adc_index = 1;
+ }
+ pr_debug("mc13892_adc : request ADC %d\n", adc_index);
+ return adc_index;
+}
+
+int mc13892_adc_release(int adc_index)
+{
+ while (suspend_flag == 1) {
+ swait++;
+ /* Block if the device is suspended */
+ if (wait_event_interruptible(suspendq, (suspend_flag == 0)))
+ return -ERESTARTSYS;
+ }
+
+ pr_debug("mc13892_adc : release ADC %d\n", adc_index);
+ if ((adc_dev[adc_index] == ADC_MONITORING) ||
+ (adc_dev[adc_index] == ADC_USED)) {
+ adc_dev[adc_index] = ADC_FREE;
+ wake_up(&queue_adc_busy);
+ return 0;
+ }
+ return -1;
+}
+
+#ifdef DEBUG
+static t_adc_param adc_param_db;
+
+static ssize_t adc_info(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ int *value = adc_param_db.value;
+
+ pr_debug("adc_info\n");
+
+ pr_debug("ch0\t\t%d\n", adc_param_db.channel_0);
+ pr_debug("ch1\t\t%d\n", adc_param_db.channel_1);
+ pr_debug("d5\t\t%d\n", adc_param_db.chrgraw_devide_5);
+ pr_debug("conv delay\t%d\n", adc_param_db.conv_delay);
+ pr_debug("delay\t\t%d\n", adc_param_db.delay);
+ pr_debug("read mode\t%d\n", adc_param_db.read_mode);
+ pr_debug("read ts\t\t%d\n", adc_param_db.read_ts);
+ pr_debug("single ch\t%d\n", adc_param_db.single_channel);
+ pr_debug("wait ts int\t%d\n", adc_param_db.wait_tsi);
+ pr_debug("value0-3:\t%d\t%d\t%d\t%d\n", value[0], value[1],
+ value[2], value[3]);
+ pr_debug("value4-7:\t%d\t%d\t%d\t%d\n", value[4], value[5],
+ value[6], value[7]);
+
+ return 0;
+}
+
+enum {
+ ADC_SET_CH0 = 0,
+ ADC_SET_CH1,
+ ADC_SET_DV5,
+ ADC_SET_CON_DELAY,
+ ADC_SET_DELAY,
+ ADC_SET_RM,
+ ADC_SET_RT,
+ ADC_SET_S_CH,
+ ADC_SET_WAIT_TS,
+ ADC_INIT_P,
+ ADC_START,
+ ADC_TS,
+ ADC_TS_READ,
+ ADC_TS_CAL,
+ ADC_CMD_MAX
+};
+
+static const char *const adc_cmd[ADC_CMD_MAX] = {
+ [ADC_SET_CH0] = "ch0",
+ [ADC_SET_CH1] = "ch1",
+ [ADC_SET_DV5] = "dv5",
+ [ADC_SET_CON_DELAY] = "cd",
+ [ADC_SET_DELAY] = "dl",
+ [ADC_SET_RM] = "rm",
+ [ADC_SET_RT] = "rt",
+ [ADC_SET_S_CH] = "sch",
+ [ADC_SET_WAIT_TS] = "wt",
+ [ADC_INIT_P] = "init",
+ [ADC_START] = "start",
+ [ADC_TS] = "touch",
+ [ADC_TS_READ] = "touchr",
+ [ADC_TS_CAL] = "cal"
+};
+
+static int cmd(unsigned int index, int value)
+{
+ t_touch_screen ts;
+
+ switch (index) {
+ case ADC_SET_CH0:
+ adc_param_db.channel_0 = value;
+ break;
+ case ADC_SET_CH1:
+ adc_param_db.channel_1 = value;
+ break;
+ case ADC_SET_DV5:
+ adc_param_db.chrgraw_devide_5 = value;
+ break;
+ case ADC_SET_CON_DELAY:
+ adc_param_db.conv_delay = value;
+ break;
+ case ADC_SET_RM:
+ adc_param_db.read_mode = value;
+ break;
+ case ADC_SET_RT:
+ adc_param_db.read_ts = value;
+ break;
+ case ADC_SET_S_CH:
+ adc_param_db.single_channel = value;
+ break;
+ case ADC_SET_WAIT_TS:
+ adc_param_db.wait_tsi = value;
+ break;
+ case ADC_INIT_P:
+ mc13892_adc_init_param(&adc_param_db);
+ break;
+ case ADC_START:
+ mc13892_adc_convert(&adc_param_db);
+ break;
+ case ADC_TS:
+ pmic_adc_get_touch_sample(&ts, 1);
+ pr_debug("x = %d\n", ts.x_position);
+ pr_debug("y = %d\n", ts.y_position);
+ pr_debug("p = %d\n", ts.contact_resistance);
+ break;
+ case ADC_TS_READ:
+ pmic_adc_get_touch_sample(&ts, 0);
+ pr_debug("x = %d\n", ts.x_position);
+ pr_debug("y = %d\n", ts.y_position);
+ pr_debug("p = %d\n", ts.contact_resistance);
+ break;
+ case ADC_TS_CAL:
+ break;
+ default:
+ pr_debug("error command\n");
+ break;
+ }
+ return 0;
+}
+
+static ssize_t adc_ctl(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int state = 0;
+ const char *const *s;
+ char *p, *q;
+ int error;
+ int len, value = 0;
+
+ pr_debug("adc_ctl\n");
+
+ q = NULL;
+ q = memchr(buf, ' ', count);
+
+ if (q != NULL) {
+ len = q - buf;
+ q += 1;
+ value = simple_strtoul(q, NULL, 10);
+ } else {
+ p = memchr(buf, '\n', count);
+ len = p ? p - buf : count;
+ }
+
+ for (s = &adc_cmd[state]; state < ADC_CMD_MAX; s++, state++) {
+ if (*s && !strncmp(buf, *s, len))
+ break;
+ }
+ if (state < ADC_CMD_MAX && *s)
+ error = cmd(state, value);
+ else
+ error = -EINVAL;
+
+ return count;
+}
+
+#else
+static ssize_t adc_info(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ return 0;
+}
+
+static ssize_t adc_ctl(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ return count;
+}
+
+#endif
+
+static DEVICE_ATTR(adc, 0644, adc_info, adc_ctl);
+
+static int pmic_adc_module_probe(struct platform_device *pdev)
+{
+ int ret = 0;
+
+ pr_debug("PMIC ADC start probe\n");
+ ret = device_create_file(&(pdev->dev), &dev_attr_adc);
+ if (ret) {
+ pr_debug("Can't create device file!\n");
+ return -ENODEV;
+ }
+
+ init_waitqueue_head(&suspendq);
+
+ ret = pmic_adc_init();
+ if (ret != PMIC_SUCCESS) {
+ pr_debug("Error in pmic_adc_init.\n");
+ goto rm_dev_file;
+ }
+
+ pmic_adc_ready = 1;
+ pr_debug("PMIC ADC successfully probed\n");
+ return 0;
+
+rm_dev_file:
+ device_remove_file(&(pdev->dev), &dev_attr_adc);
+ return ret;
+}
+
+static int pmic_adc_module_remove(struct platform_device *pdev)
+{
+ pmic_adc_deinit();
+ pmic_adc_ready = 0;
+ pr_debug("PMIC ADC successfully removed\n");
+ return 0;
+}
+
+static struct platform_driver pmic_adc_driver_ldm = {
+ .driver = {
+ .name = "pmic_adc",
+ },
+ .suspend = pmic_adc_suspend,
+ .resume = pmic_adc_resume,
+ .probe = pmic_adc_module_probe,
+ .remove = pmic_adc_module_remove,
+};
+
+static int __init pmic_adc_module_init(void)
+{
+ pr_debug("PMIC ADC driver loading...\n");
+ return platform_driver_register(&pmic_adc_driver_ldm);
+}
+
+static void __exit pmic_adc_module_exit(void)
+{
+ platform_driver_unregister(&pmic_adc_driver_ldm);
+ pr_debug("PMIC ADC driver successfully unloaded\n");
+}
+
+module_init(pmic_adc_module_init);
+module_exit(pmic_adc_module_exit);
+
+MODULE_DESCRIPTION("PMIC ADC device driver");
+MODULE_AUTHOR("Freescale Semiconductor, Inc.");
+MODULE_LICENSE("GPL");
diff --git a/drivers/mxc/pmic/mc13892/pmic_battery.c b/drivers/mxc/pmic/mc13892/pmic_battery.c
new file mode 100644
index 000000000000..246bd2731ee4
--- /dev/null
+++ b/drivers/mxc/pmic/mc13892/pmic_battery.c
@@ -0,0 +1,797 @@
+/*
+ * Copyright 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+/*
+ * Includes
+ */
+#include <linux/workqueue.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/delay.h>
+#include <linux/slab.h>
+#include <asm/mach-types.h>
+#include <linux/pmic_battery.h>
+#include <linux/pmic_adc.h>
+#include <linux/pmic_status.h>
+
+#define BIT_CHG_VOL_LSH 0
+#define BIT_CHG_VOL_WID 3
+
+#define BIT_CHG_CURR_LSH 3
+#define BIT_CHG_CURR_WID 4
+
+#define BIT_CHG_PLIM_LSH 15
+#define BIT_CHG_PLIM_WID 2
+
+#define BIT_CHG_DETS_LSH 6
+#define BIT_CHG_DETS_WID 1
+#define BIT_CHG_CURRS_LSH 11
+#define BIT_CHG_CURRS_WID 1
+
+#define TRICKLE_CHG_EN_LSH 7
+#define LOW_POWER_BOOT_ACK_LSH 8
+#define BAT_TH_CHECK_DIS_LSH 9
+#define BATTFET_CTL_EN_LSH 10
+#define BATTFET_CTL_LSH 11
+#define REV_MOD_EN_LSH 13
+#define PLIM_DIS_LSH 17
+#define CHG_LED_EN_LSH 18
+#define RESTART_CHG_STAT_LSH 20
+#define AUTO_CHG_DIS_LSH 21
+#define CYCLING_DIS_LSH 22
+#define VI_PROGRAM_EN_LSH 23
+
+#define TRICKLE_CHG_EN_WID 1
+#define LOW_POWER_BOOT_ACK_WID 1
+#define BAT_TH_CHECK_DIS_WID 1
+#define BATTFET_CTL_EN_WID 1
+#define BATTFET_CTL_WID 1
+#define REV_MOD_EN_WID 1
+#define PLIM_DIS_WID 1
+#define CHG_LED_EN_WID 1
+#define RESTART_CHG_STAT_WID 1
+#define AUTO_CHG_DIS_WID 1
+#define CYCLING_DIS_WID 1
+#define VI_PROGRAM_EN_WID 1
+
+#define ACC_STARTCC_LSH 0
+#define ACC_STARTCC_WID 1
+#define ACC_RSTCC_LSH 1
+#define ACC_RSTCC_WID 1
+#define ACC_CCFAULT_LSH 7
+#define ACC_CCFAULT_WID 7
+#define ACC_CCOUT_LSH 8
+#define ACC_CCOUT_WID 16
+#define ACC1_ONEC_LSH 0
+#define ACC1_ONEC_WID 15
+
+#define ACC_CALIBRATION 0x17
+#define ACC_START_COUNTER 0x07
+#define ACC_STOP_COUNTER 0x2
+#define ACC_CONTROL_BIT_MASK 0x1f
+#define ACC_ONEC_VALUE 2621
+#define ACC_COULOMB_PER_LSB 1
+#define ACC_CALIBRATION_DURATION_MSECS 20
+
+#define BAT_VOLTAGE_UNIT_UV 4692
+#define BAT_CURRENT_UNIT_UA 5870
+#define CHG_VOLTAGE_UINT_UV 23474
+#define CHG_MIN_CURRENT_UA 3500
+
+#define COULOMB_TO_UAH(c) (10000 * c / 36)
+
+enum chg_setting {
+ TRICKLE_CHG_EN,
+ LOW_POWER_BOOT_ACK,
+ BAT_TH_CHECK_DIS,
+ BATTFET_CTL_EN,
+ BATTFET_CTL,
+ REV_MOD_EN,
+ PLIM_DIS,
+ CHG_LED_EN,
+ RESTART_CHG_STAT,
+ AUTO_CHG_DIS,
+ CYCLING_DIS,
+ VI_PROGRAM_EN
+};
+
+/* Flag used to indicate if Charger workaround is active. */
+int chg_wa_is_active;
+/* Flag used to indicate if Charger workaround timer is on. */
+int chg_wa_timer;
+int disable_chg_timer;
+struct workqueue_struct *chg_wq;
+struct delayed_work chg_work;
+
+static int pmic_set_chg_current(unsigned short curr)
+{
+ unsigned int mask;
+ unsigned int value;
+
+ value = BITFVAL(BIT_CHG_CURR, curr);
+ mask = BITFMASK(BIT_CHG_CURR);
+ CHECK_ERROR(pmic_write_reg(REG_CHARGE, value, mask));
+
+ return 0;
+}
+
+static int pmic_set_chg_misc(enum chg_setting type, unsigned short flag)
+{
+
+ unsigned int reg_value = 0;
+ unsigned int mask = 0;
+
+ switch (type) {
+ case TRICKLE_CHG_EN:
+ reg_value = BITFVAL(TRICKLE_CHG_EN, flag);
+ mask = BITFMASK(TRICKLE_CHG_EN);
+ break;
+ case LOW_POWER_BOOT_ACK:
+ reg_value = BITFVAL(LOW_POWER_BOOT_ACK, flag);
+ mask = BITFMASK(LOW_POWER_BOOT_ACK);
+ break;
+ case BAT_TH_CHECK_DIS:
+ reg_value = BITFVAL(BAT_TH_CHECK_DIS, flag);
+ mask = BITFMASK(BAT_TH_CHECK_DIS);
+ break;
+ case BATTFET_CTL_EN:
+ reg_value = BITFVAL(BATTFET_CTL_EN, flag);
+ mask = BITFMASK(BATTFET_CTL_EN);
+ break;
+ case BATTFET_CTL:
+ reg_value = BITFVAL(BATTFET_CTL, flag);
+ mask = BITFMASK(BATTFET_CTL);
+ break;
+ case REV_MOD_EN:
+ reg_value = BITFVAL(REV_MOD_EN, flag);
+ mask = BITFMASK(REV_MOD_EN);
+ break;
+ case PLIM_DIS:
+ reg_value = BITFVAL(PLIM_DIS, flag);
+ mask = BITFMASK(PLIM_DIS);
+ break;
+ case CHG_LED_EN:
+ reg_value = BITFVAL(CHG_LED_EN, flag);
+ mask = BITFMASK(CHG_LED_EN);
+ break;
+ case RESTART_CHG_STAT:
+ reg_value = BITFVAL(RESTART_CHG_STAT, flag);
+ mask = BITFMASK(RESTART_CHG_STAT);
+ break;
+ case AUTO_CHG_DIS:
+ reg_value = BITFVAL(AUTO_CHG_DIS, flag);
+ mask = BITFMASK(AUTO_CHG_DIS);
+ break;
+ case CYCLING_DIS:
+ reg_value = BITFVAL(CYCLING_DIS, flag);
+ mask = BITFMASK(CYCLING_DIS);
+ break;
+ case VI_PROGRAM_EN:
+ reg_value = BITFVAL(VI_PROGRAM_EN, flag);
+ mask = BITFMASK(VI_PROGRAM_EN);
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+
+ CHECK_ERROR(pmic_write_reg(REG_CHARGE, reg_value, mask));
+
+ return 0;
+}
+
+static int pmic_get_batt_voltage(unsigned short *voltage)
+{
+ t_channel channel;
+ unsigned short result[8];
+
+ channel = BATTERY_VOLTAGE;
+ CHECK_ERROR(pmic_adc_convert(channel, result));
+ *voltage = result[0];
+
+ return 0;
+}
+
+static int pmic_get_batt_current(unsigned short *curr)
+{
+ t_channel channel;
+ unsigned short result[8];
+
+ channel = BATTERY_CURRENT;
+ CHECK_ERROR(pmic_adc_convert(channel, result));
+ *curr = result[0];
+
+ return 0;
+}
+
+static int coulomb_counter_calibration;
+static unsigned int coulomb_counter_start_time_msecs;
+
+static int pmic_start_coulomb_counter(void)
+{
+ /* set scaler */
+ CHECK_ERROR(pmic_write_reg(REG_ACC1,
+ ACC_COULOMB_PER_LSB * ACC_ONEC_VALUE, BITFMASK(ACC1_ONEC)));
+
+ CHECK_ERROR(pmic_write_reg(
+ REG_ACC0, ACC_START_COUNTER, ACC_CONTROL_BIT_MASK));
+ coulomb_counter_start_time_msecs = jiffies_to_msecs(jiffies);
+ pr_debug("coulomb counter start time %u\n",
+ coulomb_counter_start_time_msecs);
+ return 0;
+}
+
+static int pmic_stop_coulomb_counter(void)
+{
+ CHECK_ERROR(pmic_write_reg(
+ REG_ACC0, ACC_STOP_COUNTER, ACC_CONTROL_BIT_MASK));
+ return 0;
+}
+
+static int pmic_calibrate_coulomb_counter(void)
+{
+ int ret;
+ unsigned int value;
+
+ /* set scaler */
+ CHECK_ERROR(pmic_write_reg(REG_ACC1,
+ 0x1, BITFMASK(ACC1_ONEC)));
+
+ CHECK_ERROR(pmic_write_reg(
+ REG_ACC0, ACC_CALIBRATION, ACC_CONTROL_BIT_MASK));
+ msleep(ACC_CALIBRATION_DURATION_MSECS);
+
+ ret = pmic_read_reg(REG_ACC0, &value, BITFMASK(ACC_CCOUT));
+ if (ret != 0)
+ return -1;
+ value = BITFEXT(value, ACC_CCOUT);
+ pr_debug("calibrate value = %x\n", value);
+ coulomb_counter_calibration = (int)((s16)((u16) value));
+ pr_debug("coulomb_counter_calibration = %d\n",
+ coulomb_counter_calibration);
+
+ return 0;
+
+}
+
+static int pmic_get_charger_coulomb(int *coulomb)
+{
+ int ret;
+ unsigned int value;
+ int calibration;
+ unsigned int time_diff_msec;
+
+ ret = pmic_read_reg(REG_ACC0, &value, BITFMASK(ACC_CCOUT));
+ if (ret != 0)
+ return -1;
+ value = BITFEXT(value, ACC_CCOUT);
+ pr_debug("counter value = %x\n", value);
+ *coulomb = ((s16)((u16)value)) * ACC_COULOMB_PER_LSB;
+
+ if (abs(*coulomb) >= ACC_COULOMB_PER_LSB) {
+ /* calibrate */
+ time_diff_msec = jiffies_to_msecs(jiffies);
+ time_diff_msec =
+ (time_diff_msec > coulomb_counter_start_time_msecs) ?
+ (time_diff_msec - coulomb_counter_start_time_msecs) :
+ (0xffffffff - coulomb_counter_start_time_msecs
+ + time_diff_msec);
+ calibration = coulomb_counter_calibration * (int)time_diff_msec
+ / (ACC_ONEC_VALUE * ACC_CALIBRATION_DURATION_MSECS);
+ *coulomb -= calibration;
+ }
+
+ return 0;
+}
+
+static int pmic_restart_charging(void)
+{
+ pmic_set_chg_misc(BAT_TH_CHECK_DIS, 1);
+ pmic_set_chg_misc(AUTO_CHG_DIS, 0);
+ pmic_set_chg_misc(VI_PROGRAM_EN, 1);
+ pmic_set_chg_current(0x8);
+ pmic_set_chg_misc(RESTART_CHG_STAT, 1);
+ pmic_set_chg_misc(PLIM_DIS, 3);
+ return 0;
+}
+
+struct mc13892_dev_info {
+ struct device *dev;
+
+ unsigned short voltage_raw;
+ int voltage_uV;
+ unsigned short current_raw;
+ int current_uA;
+ int battery_status;
+ int full_counter;
+ int charger_online;
+ int charger_voltage_uV;
+ int accum_current_uAh;
+
+ struct power_supply bat;
+ struct power_supply charger;
+
+ struct workqueue_struct *monitor_wqueue;
+ struct delayed_work monitor_work;
+};
+
+#define mc13892_SENSER 25
+#define to_mc13892_dev_info(x) container_of((x), struct mc13892_dev_info, \
+ bat);
+
+static enum power_supply_property mc13892_battery_props[] = {
+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
+ POWER_SUPPLY_PROP_CURRENT_NOW,
+ POWER_SUPPLY_PROP_CHARGE_NOW,
+ POWER_SUPPLY_PROP_STATUS,
+};
+
+static enum power_supply_property mc13892_charger_props[] = {
+ POWER_SUPPLY_PROP_ONLINE,
+};
+
+static int pmic_get_chg_value(unsigned int *value)
+{
+ t_channel channel;
+ unsigned short result[8], max1 = 0, min1 = 0, max2 = 0, min2 = 0, i;
+ unsigned int average = 0, average1 = 0, average2 = 0;
+
+ channel = CHARGE_CURRENT;
+ CHECK_ERROR(pmic_adc_convert(channel, result));
+
+
+ for (i = 0; i < 8; i++) {
+ if ((result[i] & 0x200) != 0) {
+ result[i] = 0x400 - result[i];
+ average2 += result[i];
+ if ((max2 == 0) || (max2 < result[i]))
+ max2 = result[i];
+ if ((min2 == 0) || (min2 > result[i]))
+ min2 = result[i];
+ } else {
+ average1 += result[i];
+ if ((max1 == 0) || (max1 < result[i]))
+ max1 = result[i];
+ if ((min1 == 0) || (min1 > result[i]))
+ min1 = result[i];
+ }
+ }
+
+ if (max1 != 0) {
+ average1 -= max1;
+ if (max2 != 0)
+ average2 -= max2;
+ else
+ average1 -= min1;
+ } else
+ average2 -= max2 + min2;
+
+ if (average1 >= average2) {
+ average = (average1 - average2) / 6;
+ *value = average;
+ } else {
+ average = (average2 - average1) / 6;
+ *value = ((~average) + 1) & 0x3FF;
+ }
+
+ return 0;
+}
+
+static void chg_thread(struct work_struct *work)
+{
+ int ret;
+ unsigned int value = 0;
+ int dets;
+
+ if (disable_chg_timer) {
+ disable_chg_timer = 0;
+ pmic_set_chg_current(0x8);
+ queue_delayed_work(chg_wq, &chg_work, 100);
+ chg_wa_timer = 1;
+ return;
+ }
+
+ ret = pmic_read_reg(REG_INT_SENSE0, &value, BITFMASK(BIT_CHG_DETS));
+
+ if (ret == 0) {
+ dets = BITFEXT(value, BIT_CHG_DETS);
+ pr_debug("dets=%d\n", dets);
+
+ if (dets == 1) {
+ pmic_get_chg_value(&value);
+ pr_debug("average value=%d\n", value);
+ if ((value <= 3) | ((value & 0x200) != 0)) {
+ pr_debug("%s: Disable the charger\n", __func__);
+ pmic_set_chg_current(0);
+ disable_chg_timer = 1;
+ }
+
+ queue_delayed_work(chg_wq, &chg_work, 100);
+ chg_wa_timer = 1;
+ }
+ }
+}
+
+static int mc13892_charger_update_status(struct mc13892_dev_info *di)
+{
+ int ret;
+ unsigned int value;
+ int online;
+
+ ret = pmic_read_reg(REG_INT_SENSE0, &value, BITFMASK(BIT_CHG_DETS));
+
+ if (ret == 0) {
+ online = BITFEXT(value, BIT_CHG_DETS);
+ if (online != di->charger_online) {
+ di->charger_online = online;
+ dev_info(di->charger.dev, "charger status: %s\n",
+ online ? "online" : "offline");
+ power_supply_changed(&di->charger);
+
+ cancel_delayed_work(&di->monitor_work);
+ queue_delayed_work(di->monitor_wqueue,
+ &di->monitor_work, HZ / 10);
+ if (online) {
+ pmic_start_coulomb_counter();
+ pmic_restart_charging();
+ queue_delayed_work(chg_wq, &chg_work, 100);
+ chg_wa_timer = 1;
+ } else {
+ cancel_delayed_work(&chg_work);
+ chg_wa_timer = 0;
+ pmic_stop_coulomb_counter();
+ }
+ }
+ }
+
+ return ret;
+}
+
+static int mc13892_charger_get_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct mc13892_dev_info *di =
+ container_of((psy), struct mc13892_dev_info, charger);
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_ONLINE:
+ val->intval = di->charger_online;
+ return 0;
+ default:
+ break;
+ }
+ return -EINVAL;
+}
+
+static int mc13892_battery_read_status(struct mc13892_dev_info *di)
+{
+ int retval;
+ int coulomb;
+ retval = pmic_get_batt_voltage(&(di->voltage_raw));
+ if (retval == 0)
+ di->voltage_uV = di->voltage_raw * BAT_VOLTAGE_UNIT_UV;
+
+ retval = pmic_get_batt_current(&(di->current_raw));
+ if (retval == 0) {
+ if (di->current_raw & 0x200)
+ di->current_uA =
+ (0x1FF - (di->current_raw & 0x1FF)) *
+ BAT_CURRENT_UNIT_UA * (-1);
+ else
+ di->current_uA =
+ (di->current_raw & 0x1FF) * BAT_CURRENT_UNIT_UA;
+ }
+ retval = pmic_get_charger_coulomb(&coulomb);
+ if (retval == 0)
+ di->accum_current_uAh = COULOMB_TO_UAH(coulomb);
+
+ return retval;
+}
+
+static void mc13892_battery_update_status(struct mc13892_dev_info *di)
+{
+ unsigned int value;
+ int retval;
+ int old_battery_status = di->battery_status;
+
+ if (di->battery_status == POWER_SUPPLY_STATUS_UNKNOWN)
+ di->full_counter = 0;
+
+ if (di->charger_online) {
+ retval = pmic_read_reg(REG_INT_SENSE0,
+ &value, BITFMASK(BIT_CHG_CURRS));
+
+ if (retval == 0) {
+ value = BITFEXT(value, BIT_CHG_CURRS);
+ if (value)
+ di->battery_status =
+ POWER_SUPPLY_STATUS_CHARGING;
+ else
+ di->battery_status =
+ POWER_SUPPLY_STATUS_NOT_CHARGING;
+ }
+
+ if (di->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING)
+ di->full_counter++;
+ else
+ di->full_counter = 0;
+ } else {
+ di->battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
+ di->full_counter = 0;
+ }
+
+ dev_dbg(di->bat.dev, "bat status: %d\n",
+ di->battery_status);
+
+ if (old_battery_status != POWER_SUPPLY_STATUS_UNKNOWN &&
+ di->battery_status != old_battery_status)
+ power_supply_changed(&di->bat);
+}
+
+static void mc13892_battery_work(struct work_struct *work)
+{
+ struct mc13892_dev_info *di = container_of(work,
+ struct mc13892_dev_info,
+ monitor_work.work);
+ const int interval = HZ * 60;
+
+ dev_dbg(di->dev, "%s\n", __func__);
+
+ mc13892_battery_update_status(di);
+ queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
+}
+
+static void charger_online_event_callback(void *para)
+{
+ struct mc13892_dev_info *di = (struct mc13892_dev_info *) para;
+ pr_info("\n\n DETECTED charger plug/unplug event\n");
+ mc13892_charger_update_status(di);
+}
+
+
+static int mc13892_battery_get_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct mc13892_dev_info *di = to_mc13892_dev_info(psy);
+ switch (psp) {
+ case POWER_SUPPLY_PROP_STATUS:
+ if (di->battery_status == POWER_SUPPLY_STATUS_UNKNOWN) {
+ mc13892_charger_update_status(di);
+ mc13892_battery_update_status(di);
+ }
+ val->intval = di->battery_status;
+ return 0;
+ default:
+ break;
+ }
+
+ mc13892_battery_read_status(di);
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+ val->intval = di->voltage_uV;
+ break;
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
+ val->intval = di->current_uA;
+ break;
+ case POWER_SUPPLY_PROP_CHARGE_NOW:
+ val->intval = di->accum_current_uAh;
+ break;
+ case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+ val->intval = 3800000;
+ break;
+ case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+ val->intval = 3300000;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static ssize_t chg_wa_enable_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ if (chg_wa_is_active & chg_wa_timer)
+ return sprintf(buf, "Charger LED workaround timer is on\n");
+ else
+ return sprintf(buf, "Charger LED workaround timer is off\n");
+}
+
+static ssize_t chg_wa_enable_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ if (strstr(buf, "1") != NULL) {
+ if (chg_wa_is_active) {
+ if (chg_wa_timer)
+ printk(KERN_INFO "Charger timer is already on\n");
+ else {
+ queue_delayed_work(chg_wq, &chg_work, 100);
+ chg_wa_timer = 1;
+ printk(KERN_INFO "Turned on the timer\n");
+ }
+ }
+ } else if (strstr(buf, "0") != NULL) {
+ if (chg_wa_is_active) {
+ if (chg_wa_timer) {
+ cancel_delayed_work(&chg_work);
+ chg_wa_timer = 0;
+ printk(KERN_INFO "Turned off charger timer\n");
+ } else {
+ printk(KERN_INFO "The Charger workaround timer is off\n");
+ }
+ }
+ }
+
+ return size;
+}
+
+static DEVICE_ATTR(enable, 0644, chg_wa_enable_show, chg_wa_enable_store);
+
+static int pmic_battery_remove(struct platform_device *pdev)
+{
+ pmic_event_callback_t bat_event_callback;
+ struct mc13892_dev_info *di = platform_get_drvdata(pdev);
+
+ bat_event_callback.func = charger_online_event_callback;
+ bat_event_callback.param = (void *) di;
+ pmic_event_unsubscribe(EVENT_CHGDETI, bat_event_callback);
+
+ cancel_rearming_delayed_workqueue(di->monitor_wqueue,
+ &di->monitor_work);
+ cancel_rearming_delayed_workqueue(chg_wq,
+ &chg_work);
+ destroy_workqueue(di->monitor_wqueue);
+ destroy_workqueue(chg_wq);
+ chg_wa_timer = 0;
+ chg_wa_is_active = 0;
+ disable_chg_timer = 0;
+ power_supply_unregister(&di->bat);
+ power_supply_unregister(&di->charger);
+
+ kfree(di);
+
+ return 0;
+}
+
+static int pmic_battery_probe(struct platform_device *pdev)
+{
+ int retval = 0;
+ struct mc13892_dev_info *di;
+ pmic_event_callback_t bat_event_callback;
+ pmic_version_t pmic_version;
+
+ /* Only apply battery driver for MC13892 V2.0 due to ENGR108085 */
+ pmic_version = pmic_get_version();
+ if (pmic_version.revision < 20) {
+ pr_debug("Battery driver is only applied for MC13892 V2.0\n");
+ return -1;
+ }
+ if (machine_is_mx50_arm2()) {
+ pr_debug("mc13892 charger is not used for this platform\n");
+ return -1;
+ }
+
+ di = kzalloc(sizeof(*di), GFP_KERNEL);
+ if (!di) {
+ retval = -ENOMEM;
+ goto di_alloc_failed;
+ }
+
+ platform_set_drvdata(pdev, di);
+
+ di->charger.name = "mc13892_charger";
+ di->charger.type = POWER_SUPPLY_TYPE_MAINS;
+ di->charger.properties = mc13892_charger_props;
+ di->charger.num_properties = ARRAY_SIZE(mc13892_charger_props);
+ di->charger.get_property = mc13892_charger_get_property;
+ retval = power_supply_register(&pdev->dev, &di->charger);
+ if (retval) {
+ dev_err(di->dev, "failed to register charger\n");
+ goto charger_failed;
+ }
+
+ INIT_DELAYED_WORK(&chg_work, chg_thread);
+ chg_wq = create_singlethread_workqueue("mxc_chg");
+ if (!chg_wq) {
+ retval = -ESRCH;
+ goto workqueue_failed;
+ }
+
+ INIT_DELAYED_WORK(&di->monitor_work, mc13892_battery_work);
+ di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
+ if (!di->monitor_wqueue) {
+ retval = -ESRCH;
+ goto workqueue_failed;
+ }
+ queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 10);
+
+ di->dev = &pdev->dev;
+ di->bat.name = "mc13892_bat";
+ di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
+ di->bat.properties = mc13892_battery_props;
+ di->bat.num_properties = ARRAY_SIZE(mc13892_battery_props);
+ di->bat.get_property = mc13892_battery_get_property;
+ di->bat.use_for_apm = 1;
+
+ di->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
+
+ retval = power_supply_register(&pdev->dev, &di->bat);
+ if (retval) {
+ dev_err(di->dev, "failed to register battery\n");
+ goto batt_failed;
+ }
+
+ bat_event_callback.func = charger_online_event_callback;
+ bat_event_callback.param = (void *) di;
+ pmic_event_subscribe(EVENT_CHGDETI, bat_event_callback);
+ retval = sysfs_create_file(&pdev->dev.kobj, &dev_attr_enable.attr);
+
+ if (retval) {
+ printk(KERN_ERR
+ "Battery: Unable to register sysdev entry for Battery");
+ goto workqueue_failed;
+ }
+ chg_wa_is_active = 1;
+ chg_wa_timer = 0;
+ disable_chg_timer = 0;
+
+ pmic_stop_coulomb_counter();
+ pmic_calibrate_coulomb_counter();
+ goto success;
+
+workqueue_failed:
+ power_supply_unregister(&di->charger);
+charger_failed:
+ power_supply_unregister(&di->bat);
+batt_failed:
+ kfree(di);
+di_alloc_failed:
+success:
+ dev_dbg(di->dev, "%s battery probed!\n", __func__);
+ return retval;
+
+
+ return 0;
+}
+
+static struct platform_driver pmic_battery_driver_ldm = {
+ .driver = {
+ .name = "pmic_battery",
+ .bus = &platform_bus_type,
+ },
+ .probe = pmic_battery_probe,
+ .remove = pmic_battery_remove,
+};
+
+static int __init pmic_battery_init(void)
+{
+ pr_debug("PMIC Battery driver loading...\n");
+ return platform_driver_register(&pmic_battery_driver_ldm);
+}
+
+static void __exit pmic_battery_exit(void)
+{
+ platform_driver_unregister(&pmic_battery_driver_ldm);
+ pr_debug("PMIC Battery driver successfully unloaded\n");
+}
+
+module_init(pmic_battery_init);
+module_exit(pmic_battery_exit);
+
+MODULE_DESCRIPTION("pmic_battery driver");
+MODULE_AUTHOR("Freescale Semiconductor, Inc.");
+MODULE_LICENSE("GPL");
diff --git a/drivers/mxc/pmic/mc13892/pmic_light.c b/drivers/mxc/pmic/mc13892/pmic_light.c
new file mode 100644
index 000000000000..caaa98d7d7d9
--- /dev/null
+++ b/drivers/mxc/pmic/mc13892/pmic_light.c
@@ -0,0 +1,685 @@
+/*
+ * Copyright 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
+ */
+
+/*
+ * The code contained herein is licensed under the GNU General Public
+ * License. You may obtain a copy of the GNU General Public License
+ * Version 2 or later at the following locations:
+ *
+ * http://www.opensource.org/licenses/gpl-license.html
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+/*!
+ * @file mc13892/pmic_light.c
+ * @brief This is the main file of PMIC(mc13783) Light and Backlight driver.
+ *
+ * @ingroup PMIC_LIGHT
+ */
+
+/*
+ * Includes
+ */
+#define DEBUG
+#include <linux/platform_device.h>
+#include <linux/delay.h>
+#include <linux/fs.h>
+#include <linux/pmic_light.h>
+#include <linux/pmic_status.h>
+
+#define BIT_CL_MAIN_LSH 9
+#define BIT_CL_AUX_LSH 21
+#define BIT_CL_KEY_LSH 9
+#define BIT_CL_RED_LSH 9
+#define BIT_CL_GREEN_LSH 21
+#define BIT_CL_BLUE_LSH 9
+
+#define BIT_CL_MAIN_WID 3
+#define BIT_CL_AUX_WID 3
+#define BIT_CL_KEY_WID 3
+#define BIT_CL_RED_WID 3
+#define BIT_CL_GREEN_WID 3
+#define BIT_CL_BLUE_WID 3
+
+#define BIT_DC_MAIN_LSH 3
+#define BIT_DC_AUX_LSH 15
+#define BIT_DC_KEY_LSH 3
+#define BIT_DC_RED_LSH 3
+#define BIT_DC_GREEN_LSH 15
+#define BIT_DC_BLUE_LSH 3
+
+#define BIT_DC_MAIN_WID 6
+#define BIT_DC_AUX_WID 6
+#define BIT_DC_KEY_WID 6
+#define BIT_DC_RED_WID 6
+#define BIT_DC_GREEN_WID 6
+#define BIT_DC_BLUE_WID 6
+
+#define BIT_RP_MAIN_LSH 2
+#define BIT_RP_AUX_LSH 14
+#define BIT_RP_KEY_LSH 2
+#define BIT_RP_RED_LSH 2
+#define BIT_RP_GREEN_LSH 14
+#define BIT_RP_BLUE_LSH 2
+
+#define BIT_RP_MAIN_WID 1
+#define BIT_RP_AUX_WID 1
+#define BIT_RP_KEY_WID 1
+#define BIT_RP_RED_WID 1
+#define BIT_RP_GREEN_WID 1
+#define BIT_RP_BLUE_WID 1
+
+#define BIT_HC_MAIN_LSH 1
+#define BIT_HC_AUX_LSH 13
+#define BIT_HC_KEY_LSH 1
+
+#define BIT_HC_MAIN_WID 1
+#define BIT_HC_AUX_WID 1
+#define BIT_HC_KEY_WID 1
+
+#define BIT_BP_RED_LSH 0
+#define BIT_BP_GREEN_LSH 12
+#define BIT_BP_BLUE_LSH 0
+
+#define BIT_BP_RED_WID 2
+#define BIT_BP_GREEN_WID 2
+#define BIT_BP_BLUE_WID 2
+
+int pmic_light_init_reg(void)
+{
+ CHECK_ERROR(pmic_write_reg(REG_LED_CTL0, 0, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_LED_CTL1, 0, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_LED_CTL2, 0, PMIC_ALL_BITS));
+ CHECK_ERROR(pmic_write_reg(REG_LED_CTL3, 0, PMIC_ALL_BITS));
+
+ return 0;
+}
+
+static int pmic_light_suspend(struct platform_device *dev, pm_message_t state)
+{
+ return 0;
+};
+
+static int pmic_light_resume(struct platform_device *pdev)
+{
+ return 0;
+};
+
+PMIC_STATUS mc13892_bklit_set_hi_current(enum lit_channel channel, int mode)
+{
+ unsigned int mask;
+ unsigned int value;
+ int reg;
+
+ switch (channel) {
+ case LIT_MAIN:
+ value = BITFVAL(BIT_HC_MAIN, mode);
+ mask = BITFMASK(BIT_HC_MAIN);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_AUX:
+ value = BITFVAL(BIT_HC_AUX, mode);
+ mask = BITFMASK(BIT_HC_AUX);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_KEY:
+ value = BITFVAL(BIT_HC_KEY, mode);
+ mask = BITFMASK(BIT_HC_KEY);
+ reg = REG_LED_CTL1;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+ CHECK_ERROR(pmic_write_reg(reg, value, mask));
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS mc13892_bklit_get_hi_current(enum lit_channel channel, int *mode)
+{
+ unsigned int mask;
+ int reg;
+
+ switch (channel) {
+ case LIT_MAIN:
+ mask = BITFMASK(BIT_HC_MAIN);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_AUX:
+ mask = BITFMASK(BIT_HC_AUX);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_KEY:
+ mask = BITFMASK(BIT_HC_KEY);
+ reg = REG_LED_CTL1;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+
+ CHECK_ERROR(pmic_read_reg(reg, mode, mask));
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS mc13892_bklit_set_current(enum lit_channel channel,
+ unsigned char level)
+{
+ unsigned int mask;
+ unsigned int value;
+ int reg;
+
+ if (level > LIT_CURR_HI_42)
+ return PMIC_PARAMETER_ERROR;
+ else if (level >= LIT_CURR_HI_0) {
+ CHECK_ERROR(mc13892_bklit_set_hi_current(channel, 1));
+ level -= LIT_CURR_HI_0;
+ }
+
+ switch (channel) {
+ case LIT_MAIN:
+ value = BITFVAL(BIT_CL_MAIN, level);
+ mask = BITFMASK(BIT_CL_MAIN);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_AUX:
+ value = BITFVAL(BIT_CL_AUX, level);
+ mask = BITFMASK(BIT_CL_AUX);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_KEY:
+ value = BITFVAL(BIT_CL_KEY, level);
+ mask = BITFMASK(BIT_CL_KEY);
+ reg = REG_LED_CTL1;
+ break;
+ case LIT_RED:
+ value = BITFVAL(BIT_CL_RED, level);
+ mask = BITFMASK(BIT_CL_RED);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_GREEN:
+ value = BITFVAL(BIT_CL_GREEN, level);
+ mask = BITFMASK(BIT_CL_GREEN);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_BLUE:
+ value = BITFVAL(BIT_CL_BLUE, level);
+ mask = BITFMASK(BIT_CL_BLUE);
+ reg = REG_LED_CTL3;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+ CHECK_ERROR(pmic_write_reg(reg, value, mask));
+
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS mc13892_bklit_get_current(enum lit_channel channel,
+ unsigned char *level)
+{
+ unsigned int reg_value = 0;
+ unsigned int mask = 0;
+ int reg, mode;
+
+ CHECK_ERROR(mc13892_bklit_get_hi_current(channel, &mode));
+
+ switch (channel) {
+ case LIT_MAIN:
+ mask = BITFMASK(BIT_CL_MAIN);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_AUX:
+ mask = BITFMASK(BIT_CL_AUX);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_KEY:
+ mask = BITFMASK(BIT_CL_KEY);
+ reg = REG_LED_CTL1;
+ break;
+ case LIT_RED:
+ mask = BITFMASK(BIT_CL_RED);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_GREEN:
+ mask = BITFMASK(BIT_CL_GREEN);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_BLUE:
+ mask = BITFMASK(BIT_CL_BLUE);
+ reg = REG_LED_CTL3;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+
+ CHECK_ERROR(pmic_read_reg(reg, &reg_value, mask));
+
+ switch (channel) {
+ case LIT_MAIN:
+ *level = BITFEXT(reg_value, BIT_CL_MAIN);
+ break;
+ case LIT_AUX:
+ *level = BITFEXT(reg_value, BIT_CL_AUX);
+ break;
+ case LIT_KEY:
+ *level = BITFEXT(reg_value, BIT_CL_KEY);
+ break;
+ case LIT_RED:
+ *level = BITFEXT(reg_value, BIT_CL_RED);
+ break;
+ case LIT_GREEN:
+ *level = BITFEXT(reg_value, BIT_CL_GREEN);
+ break;
+ case LIT_BLUE:
+ *level = BITFEXT(reg_value, BIT_CL_BLUE);
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+
+ if (mode == 1)
+ *level += LIT_CURR_HI_0;
+
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS mc13892_bklit_set_dutycycle(enum lit_channel channel,
+ unsigned char dc)
+{
+ unsigned int mask;
+ unsigned int value;
+ int reg;
+
+ switch (channel) {
+ case LIT_MAIN:
+ value = BITFVAL(BIT_DC_MAIN, dc);
+ mask = BITFMASK(BIT_DC_MAIN);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_AUX:
+ value = BITFVAL(BIT_DC_AUX, dc);
+ mask = BITFMASK(BIT_DC_AUX);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_KEY:
+ value = BITFVAL(BIT_DC_KEY, dc);
+ mask = BITFMASK(BIT_DC_KEY);
+ reg = REG_LED_CTL1;
+ break;
+ case LIT_RED:
+ value = BITFVAL(BIT_DC_RED, dc);
+ mask = BITFMASK(BIT_DC_RED);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_GREEN:
+ value = BITFVAL(BIT_DC_GREEN, dc);
+ mask = BITFMASK(BIT_DC_GREEN);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_BLUE:
+ value = BITFVAL(BIT_DC_BLUE, dc);
+ mask = BITFMASK(BIT_DC_BLUE);
+ reg = REG_LED_CTL3;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+ CHECK_ERROR(pmic_write_reg(reg, value, mask));
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS mc13892_bklit_get_dutycycle(enum lit_channel channel,
+ unsigned char *dc)
+{
+ unsigned int mask;
+ int reg;
+ unsigned int reg_value = 0;
+
+ switch (channel) {
+ case LIT_MAIN:
+ mask = BITFMASK(BIT_DC_MAIN);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_AUX:
+ mask = BITFMASK(BIT_DC_AUX);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_KEY:
+ mask = BITFMASK(BIT_DC_KEY);
+ reg = REG_LED_CTL1;
+ break;
+ case LIT_RED:
+ mask = BITFMASK(BIT_DC_RED);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_GREEN:
+ mask = BITFMASK(BIT_DC_GREEN);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_BLUE:
+ mask = BITFMASK(BIT_DC_BLUE);
+ reg = REG_LED_CTL3;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+
+ CHECK_ERROR(pmic_read_reg(reg, &reg_value, mask));
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS mc13892_bklit_set_ramp(enum lit_channel channel, int flag)
+{
+ unsigned int mask;
+ unsigned int value;
+ int reg;
+
+ switch (channel) {
+ case LIT_MAIN:
+ value = BITFVAL(BIT_RP_MAIN, flag);
+ mask = BITFMASK(BIT_RP_MAIN);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_AUX:
+ value = BITFVAL(BIT_RP_AUX, flag);
+ mask = BITFMASK(BIT_RP_AUX);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_KEY:
+ value = BITFVAL(BIT_RP_KEY, flag);
+ mask = BITFMASK(BIT_RP_KEY);
+ reg = REG_LED_CTL1;
+ break;
+ case LIT_RED:
+ value = BITFVAL(BIT_RP_RED, flag);
+ mask = BITFMASK(BIT_RP_RED);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_GREEN:
+ value = BITFVAL(BIT_RP_GREEN, flag);
+ mask = BITFMASK(BIT_RP_GREEN);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_BLUE:
+ value = BITFVAL(BIT_RP_BLUE, flag);
+ mask = BITFMASK(BIT_RP_BLUE);
+ reg = REG_LED_CTL3;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+ CHECK_ERROR(pmic_write_reg(reg, value, mask));
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS mc13892_bklit_get_ramp(enum lit_channel channel, int *flag)
+{
+ unsigned int mask;
+ int reg;
+
+ switch (channel) {
+ case LIT_MAIN:
+ mask = BITFMASK(BIT_RP_MAIN);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_AUX:
+ mask = BITFMASK(BIT_RP_AUX);
+ reg = REG_LED_CTL0;
+ break;
+ case LIT_KEY:
+ mask = BITFMASK(BIT_RP_KEY);
+ reg = REG_LED_CTL1;
+ break;
+ case LIT_RED:
+ mask = BITFMASK(BIT_RP_RED);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_GREEN:
+ mask = BITFMASK(BIT_RP_GREEN);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_BLUE:
+ mask = BITFMASK(BIT_RP_BLUE);
+ reg = REG_LED_CTL3;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+
+ CHECK_ERROR(pmic_read_reg(reg, flag, mask));
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS mc13892_bklit_set_blink_p(enum lit_channel channel, int period)
+{
+ unsigned int mask;
+ unsigned int value;
+ int reg;
+
+ switch (channel) {
+ case LIT_RED:
+ value = BITFVAL(BIT_BP_RED, period);
+ mask = BITFMASK(BIT_BP_RED);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_GREEN:
+ value = BITFVAL(BIT_BP_GREEN, period);
+ mask = BITFMASK(BIT_BP_GREEN);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_BLUE:
+ value = BITFVAL(BIT_BP_BLUE, period);
+ mask = BITFMASK(BIT_BP_BLUE);
+ reg = REG_LED_CTL3;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+ CHECK_ERROR(pmic_write_reg(reg, value, mask));
+ return PMIC_SUCCESS;
+}
+
+PMIC_STATUS mc13892_bklit_get_blink_p(enum lit_channel channel, int *period)
+{
+ unsigned int mask;
+ int reg;
+
+ switch (channel) {
+ case LIT_RED:
+ mask = BITFMASK(BIT_BP_RED);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_GREEN:
+ mask = BITFMASK(BIT_BP_GREEN);
+ reg = REG_LED_CTL2;
+ break;
+ case LIT_BLUE:
+ mask = BITFMASK(BIT_BP_BLUE);
+ reg = REG_LED_CTL3;
+ break;
+ default:
+ return PMIC_PARAMETER_ERROR;
+ }
+
+ CHECK_ERROR(pmic_read_reg(reg, period, mask));
+ return PMIC_SUCCESS;
+}
+
+EXPORT_SYMBOL(mc13892_bklit_set_current);
+EXPORT_SYMBOL(mc13892_bklit_get_current);
+EXPORT_SYMBOL(mc13892_bklit_set_dutycycle);
+EXPORT_SYMBOL(mc13892_bklit_get_dutycycle);
+EXPORT_SYMBOL(mc13892_bklit_set_ramp);
+EXPORT_SYMBOL(mc13892_bklit_get_ramp);
+EXPORT_SYMBOL(mc13892_bklit_set_blink_p);
+EXPORT_SYMBOL(mc13892_bklit_get_blink_p);
+
+static int pmic_light_remove(struct platform_device *pdev)
+{
+ return 0;
+}
+
+#ifdef DEBUG
+static ssize_t lit_info(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ return 0;
+}
+
+enum {
+ SET_CURR = 0,
+ SET_DC,
+ SET_RAMP,
+ SET_BP,
+ SET_CH,
+ LIT_CMD_MAX
+};
+
+static const char *const lit_cmd[LIT_CMD_MAX] = {
+ [SET_CURR] = "cur",
+ [SET_DC] = "dc",
+ [SET_RAMP] = "ra",
+ [SET_BP] = "bp",
+ [SET_CH] = "ch"
+};
+
+static int cmd(unsigned int index, int value)
+{
+ static int ch = LIT_MAIN;
+ int ret = 0;
+
+ switch (index) {
+ case SET_CH:
+ ch = value;
+ break;
+ case SET_CURR:
+ pr_debug("set %d cur %d\n", ch, value);
+ ret = mc13892_bklit_set_current(ch, value);
+ break;
+ case SET_DC:
+ pr_debug("set %d dc %d\n", ch, value);
+ ret = mc13892_bklit_set_dutycycle(ch, value);
+ break;
+ case SET_RAMP:
+ pr_debug("set %d ramp %d\n", ch, value);
+ ret = mc13892_bklit_set_ramp(ch, value);
+ break;
+ case SET_BP:
+ pr_debug("set %d bp %d\n", ch, value);
+ ret = mc13892_bklit_set_blink_p(ch, value);
+ break;
+ default:
+ pr_debug("error command\n");
+ break;
+ }
+
+ if (ret == PMIC_SUCCESS)
+ pr_debug("command exec successfully!\n");
+
+ return 0;
+}
+
+static ssize_t lit_ctl(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int state = 0;
+ const char *const *s;
+ char *p, *q;
+ int error;
+ int len, value = 0;
+
+ pr_debug("lit_ctl\n");
+
+ q = NULL;
+ q = memchr(buf, ' ', count);
+
+ if (q != NULL) {
+ len = q - buf;
+ q += 1;
+ value = simple_strtoul(q, NULL, 10);
+ } else {
+ p = memchr(buf, '\n', count);
+ len = p ? p - buf : count;
+ }
+
+ for (s = &lit_cmd[state]; state < LIT_CMD_MAX; s++, state++) {
+ if (*s && !strncmp(buf, *s, len))
+ break;
+ }
+ if (state < LIT_CMD_MAX && *s)
+ error = cmd(state, value);
+ else
+ error = -EINVAL;
+
+ return count;
+}
+
+#else
+static ssize_t lit_info(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ return 0;
+}
+
+static ssize_t lit_ctl(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ return count;
+}
+
+#endif
+
+static DEVICE_ATTR(lit, 0644, lit_info, lit_ctl);
+
+static int pmic_light_probe(struct platform_device *pdev)
+{
+ int ret = 0;
+
+ pr_debug("PMIC ADC start probe\n");
+ ret = device_create_file(&(pdev->dev), &dev_attr_lit);
+ if (ret) {
+ pr_debug("Can't create device file!\n");
+ return -ENODEV;
+ }
+
+ pmic_light_init_reg();
+
+ pr_debug("PMIC Light successfully loaded\n");
+ return 0;
+}
+
+static struct platform_driver pmic_light_driver_ldm = {
+ .driver = {
+ .name = "pmic_light",
+ },
+ .suspend = pmic_light_suspend,
+ .resume = pmic_light_resume,
+ .probe = pmic_light_probe,
+ .remove = pmic_light_remove,
+};
+
+/*
+ * Initialization and Exit
+ */
+
+static int __init pmic_light_init(void)
+{
+ pr_debug("PMIC Light driver loading...\n");
+ return platform_driver_register(&pmic_light_driver_ldm);
+}
+static void __exit pmic_light_exit(void)
+{
+ platform_driver_unregister(&pmic_light_driver_ldm);
+ pr_debug("PMIC Light driver successfully unloaded\n");
+}
+
+/*
+ * Module entry points
+ */
+
+subsys_initcall(pmic_light_init);
+module_exit(pmic_light_exit);
+
+MODULE_DESCRIPTION("PMIC_LIGHT");
+MODULE_AUTHOR("Freescale Semiconductor, Inc.");
+MODULE_LICENSE("GPL");