/drivers/power/isp1704_charger.c
C | 512 lines | 336 code | 90 blank | 86 comment | 35 complexity | 2e13ac8b0e9c6e686126d00075141155 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
- /*
- * ISP1704 USB Charger Detection driver
- *
- * Copyright (C) 2010 Nokia Corporation
- *
- * 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.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/err.h>
- #include <linux/init.h>
- #include <linux/types.h>
- #include <linux/device.h>
- #include <linux/sysfs.h>
- #include <linux/platform_device.h>
- #include <linux/power_supply.h>
- #include <linux/delay.h>
- #include <linux/usb/otg.h>
- #include <linux/usb/ulpi.h>
- #include <linux/usb/ch9.h>
- #include <linux/usb/gadget.h>
- #include <linux/power/isp1704_charger.h>
- /* Vendor specific Power Control register */
- #define ISP1704_PWR_CTRL 0x3d
- #define ISP1704_PWR_CTRL_SWCTRL (1 << 0)
- #define ISP1704_PWR_CTRL_DET_COMP (1 << 1)
- #define ISP1704_PWR_CTRL_BVALID_RISE (1 << 2)
- #define ISP1704_PWR_CTRL_BVALID_FALL (1 << 3)
- #define ISP1704_PWR_CTRL_DP_WKPU_EN (1 << 4)
- #define ISP1704_PWR_CTRL_VDAT_DET (1 << 5)
- #define ISP1704_PWR_CTRL_DPVSRC_EN (1 << 6)
- #define ISP1704_PWR_CTRL_HWDETECT (1 << 7)
- #define NXP_VENDOR_ID 0x04cc
- static u16 isp170x_id[] = {
- 0x1704,
- 0x1707,
- };
- struct isp1704_charger {
- struct device *dev;
- struct power_supply psy;
- struct otg_transceiver *otg;
- struct notifier_block nb;
- struct work_struct work;
- /* properties */
- char model[8];
- unsigned present:1;
- unsigned online:1;
- unsigned current_max;
- /* temp storage variables */
- unsigned long event;
- unsigned max_power;
- };
- /*
- * Disable/enable the power from the isp1704 if a function for it
- * has been provided with platform data.
- */
- static void isp1704_charger_set_power(struct isp1704_charger *isp, bool on)
- {
- struct isp1704_charger_data *board = isp->dev->platform_data;
- if (board->set_power)
- board->set_power(on);
- }
- /*
- * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
- * chargers).
- *
- * REVISIT: The method is defined in Battery Charging Specification and is
- * applicable to any ULPI transceiver. Nothing isp170x specific here.
- */
- static inline int isp1704_charger_type(struct isp1704_charger *isp)
- {
- u8 reg;
- u8 func_ctrl;
- u8 otg_ctrl;
- int type = POWER_SUPPLY_TYPE_USB_DCP;
- func_ctrl = otg_io_read(isp->otg, ULPI_FUNC_CTRL);
- otg_ctrl = otg_io_read(isp->otg, ULPI_OTG_CTRL);
- /* disable pulldowns */
- reg = ULPI_OTG_CTRL_DM_PULLDOWN | ULPI_OTG_CTRL_DP_PULLDOWN;
- otg_io_write(isp->otg, ULPI_CLR(ULPI_OTG_CTRL), reg);
- /* full speed */
- otg_io_write(isp->otg, ULPI_CLR(ULPI_FUNC_CTRL),
- ULPI_FUNC_CTRL_XCVRSEL_MASK);
- otg_io_write(isp->otg, ULPI_SET(ULPI_FUNC_CTRL),
- ULPI_FUNC_CTRL_FULL_SPEED);
- /* Enable strong pull-up on DP (1.5K) and reset */
- reg = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
- otg_io_write(isp->otg, ULPI_SET(ULPI_FUNC_CTRL), reg);
- usleep_range(1000, 2000);
- reg = otg_io_read(isp->otg, ULPI_DEBUG);
- if ((reg & 3) != 3)
- type = POWER_SUPPLY_TYPE_USB_CDP;
- /* recover original state */
- otg_io_write(isp->otg, ULPI_FUNC_CTRL, func_ctrl);
- otg_io_write(isp->otg, ULPI_OTG_CTRL, otg_ctrl);
- return type;
- }
- /*
- * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
- * is actually a dedicated charger, the following steps need to be taken.
- */
- static inline int isp1704_charger_verify(struct isp1704_charger *isp)
- {
- int ret = 0;
- u8 r;
- /* Reset the transceiver */
- r = otg_io_read(isp->otg, ULPI_FUNC_CTRL);
- r |= ULPI_FUNC_CTRL_RESET;
- otg_io_write(isp->otg, ULPI_FUNC_CTRL, r);
- usleep_range(1000, 2000);
- /* Set normal mode */
- r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK);
- otg_io_write(isp->otg, ULPI_FUNC_CTRL, r);
- /* Clear the DP and DM pull-down bits */
- r = ULPI_OTG_CTRL_DP_PULLDOWN | ULPI_OTG_CTRL_DM_PULLDOWN;
- otg_io_write(isp->otg, ULPI_CLR(ULPI_OTG_CTRL), r);
- /* Enable strong pull-up on DP (1.5K) and reset */
- r = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
- otg_io_write(isp->otg, ULPI_SET(ULPI_FUNC_CTRL), r);
- usleep_range(1000, 2000);
- /* Read the line state */
- if (!otg_io_read(isp->otg, ULPI_DEBUG)) {
- /* Disable strong pull-up on DP (1.5K) */
- otg_io_write(isp->otg, ULPI_CLR(ULPI_FUNC_CTRL),
- ULPI_FUNC_CTRL_TERMSELECT);
- return 1;
- }
- /* Is it a charger or PS/2 connection */
- /* Enable weak pull-up resistor on DP */
- otg_io_write(isp->otg, ULPI_SET(ISP1704_PWR_CTRL),
- ISP1704_PWR_CTRL_DP_WKPU_EN);
- /* Disable strong pull-up on DP (1.5K) */
- otg_io_write(isp->otg, ULPI_CLR(ULPI_FUNC_CTRL),
- ULPI_FUNC_CTRL_TERMSELECT);
- /* Enable weak pull-down resistor on DM */
- otg_io_write(isp->otg, ULPI_SET(ULPI_OTG_CTRL),
- ULPI_OTG_CTRL_DM_PULLDOWN);
- /* It's a charger if the line states are clear */
- if (!(otg_io_read(isp->otg, ULPI_DEBUG)))
- ret = 1;
- /* Disable weak pull-up resistor on DP */
- otg_io_write(isp->otg, ULPI_CLR(ISP1704_PWR_CTRL),
- ISP1704_PWR_CTRL_DP_WKPU_EN);
- return ret;
- }
- static inline int isp1704_charger_detect(struct isp1704_charger *isp)
- {
- unsigned long timeout;
- u8 pwr_ctrl;
- int ret = 0;
- pwr_ctrl = otg_io_read(isp->otg, ISP1704_PWR_CTRL);
- /* set SW control bit in PWR_CTRL register */
- otg_io_write(isp->otg, ISP1704_PWR_CTRL,
- ISP1704_PWR_CTRL_SWCTRL);
- /* enable manual charger detection */
- otg_io_write(isp->otg, ULPI_SET(ISP1704_PWR_CTRL),
- ISP1704_PWR_CTRL_SWCTRL
- | ISP1704_PWR_CTRL_DPVSRC_EN);
- usleep_range(1000, 2000);
- timeout = jiffies + msecs_to_jiffies(300);
- do {
- /* Check if there is a charger */
- if (otg_io_read(isp->otg, ISP1704_PWR_CTRL)
- & ISP1704_PWR_CTRL_VDAT_DET) {
- ret = isp1704_charger_verify(isp);
- break;
- }
- } while (!time_after(jiffies, timeout) && isp->online);
- /* recover original state */
- otg_io_write(isp->otg, ISP1704_PWR_CTRL, pwr_ctrl);
- return ret;
- }
- static void isp1704_charger_work(struct work_struct *data)
- {
- int detect;
- unsigned long event;
- unsigned power;
- struct isp1704_charger *isp =
- container_of(data, struct isp1704_charger, work);
- static DEFINE_MUTEX(lock);
- event = isp->event;
- power = isp->max_power;
- mutex_lock(&lock);
- if (event != USB_EVENT_NONE)
- isp1704_charger_set_power(isp, 1);
- switch (event) {
- case USB_EVENT_VBUS:
- isp->online = true;
- /* detect charger */
- detect = isp1704_charger_detect(isp);
-