/*
* TWL4030/TPS65950 BCI (Battery Charger Interface) driver
*
* Copyright (C) 2010 Gražvydas Ignotas <notasas@gmail.com>
*
* based on twl4030_bci_battery.c by TI
* Copyright (C) 2008 Texas Instruments, Inc.
*
* 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.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/i2c/twl.h>
#include <linux/power_supply.h>
#include <linux/notifier.h>
#include <linux/usb/otg.h>
#include <linux/iio/consumer.h>
#define TWL4030_BCIMDEN 0x00
#define TWL4030_BCIMDKEY 0x01
#define TWL4030_BCIMSTATEC 0x02
#define TWL4030_BCIICHG 0x08
#define TWL4030_BCIVAC 0x0a
#define TWL4030_BCIVBUS 0x0c
#define TWL4030_BCIMFSTS3 0x0F
#define TWL4030_BCIMFSTS4 0x10
#define TWL4030_BCICTL1 0x23
#define TWL4030_BB_CFG 0x12
#define TWL4030_BCIIREF1 0x27
#define TWL4030_BCIIREF2 0x28
#define TWL4030_BCIMFKEY 0x11
#define TWL4030_BCIMFEN3 0x14
#define TWL4030_BCIMFTH8 0x1d
#define TWL4030_BCIMFTH9 0x1e
#define TWL4030_BCIWDKEY 0x21
#define TWL4030_BCIMFSTS1 0x01
#define TWL4030_BCIAUTOWEN BIT(5)
#define TWL4030_CONFIG_DONE BIT(4)
#define TWL4030_CVENAC BIT(2)
#define TWL4030_BCIAUTOUSB BIT(1)
#define TWL4030_BCIAUTOAC BIT(0)
#define TWL4030_CGAIN BIT(5)
#define TWL4030_USBFASTMCHG BIT(2)
#define TWL4030_STS_VBUS BIT(7)
#define TWL4030_STS_USB_ID BIT(2)
#define TWL4030_BBCHEN BIT(4)
#define TWL4030_BBSEL_MASK 0x0c
#define TWL4030_BBSEL_2V5 0x00
#define TWL4030_BBSEL_3V0 0x04
#define TWL4030_BBSEL_3V1 0x08
#define TWL4030_BBSEL_3V2 0x0c
#define TWL4030_BBISEL_MASK 0x03
#define TWL4030_BBISEL_25uA 0x00
#define TWL4030_BBISEL_150uA 0x01
#define TWL4030_BBISEL_500uA 0x02
#define TWL4030_BBISEL_1000uA 0x03
#define TWL4030_BATSTSPCHG BIT(2)
#define TWL4030_BATSTSMCHG BIT(6)
/* BCI interrupts */
#define TWL4030_WOVF BIT(0) /* Watchdog overflow */
#define TWL4030_TMOVF BIT(1) /* Timer overflow */
#define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
#define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
#define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
#define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
#define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
#define TWL4030_BATSTS BIT(7) /* Battery status */
#define TWL4030_VBATLVL BIT(0) /* VBAT level */
#define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
#define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
#define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
#define TWL4030_MSTATEC_USB BIT(4)
#define TWL4030_MSTATEC_AC BIT(5)
#define TWL4030_MSTATEC_MASK 0x0f
#define TWL4030_MSTATEC_QUICK1 0x02
#define TWL4030_MSTATEC_QUICK7 0x07
#define TWL4030_MSTATEC_COMPLETE1 0x0b
#define TWL4030_MSTATEC_COMPLETE4 0x0e
/*
* If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
* then AC is available.
*/
static inline int ac_available(struct iio_channel *channel_vac)
{
int val, err;
if (!channel_vac)
return 0;
err = iio_read_channel_processed(channel_vac, &val);
if (err < 0)
return 0;
return val > 4500;
}
static bool allow_usb;
module_param(allow_usb, bool, 0644);
MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current");
struct twl4030_bci {
struct device *dev;
struct power_supply *ac;
struct power_supply *usb;
struct usb_phy *transceiver;
struct notifier_block usb_nb;
struct work_struct work;
int irq_chg;
int irq_bci;
int usb_enabled;
/*
* ichg_* and *_cur values in uA. If any are 'large', we set
* CGAIN to '1' which doubles the range for half the
* precision.
*/
unsigned int ichg_eoc, ichg_lo, ichg_hi;
unsigned int usb_cur, ac_cur;
struct iio_channel *channel_vac;
bool ac_is_active;
int usb_mode, ac_mode; /* charging mode requested */
#define CHARGE_OFF 0
#define CHARGE_AUTO 1
#define CHARGE_LINEAR 2
/* When setting the USB current we slowly increase the
* requested current until target is reached or the voltage
* drops below 4.75V. In the latter case we step back one
* step.
*/
unsigned int usb_cur_target;
struct delayed_work current_worker;
#define USB_CUR_STEP 20000 /* 20mA at a time */
#define USB_MIN_VOLT 4750000 /* 4.75V */
#define USB_CUR_DELAY msecs_to_jiffies(100)
#define USB_MAX_CURRENT 1700000 /* TWL4030 caps at 1.7A */
unsigned long event;
};
/* strings for 'usb_mode' values */
static char *modes[] = { "off", "auto", "continuous" };
/*
* clear and set bits on an given register on a given module
*/
static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg)
{
u8 val = 0;
int ret;
ret = twl_i2c_read_u8(mod_no, &val, reg);
if (ret)
return ret;
val &= ~clear;
val |= set;
return twl_i2c_write_u8(mod_no, val, reg);
}
static int twl4030_bci_read(u8