Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 1 | /* |
| 2 | * ISP1704 USB Charger Detection driver |
| 3 | * |
| 4 | * Copyright (C) 2010 Nokia Corporation |
Pali Rohár | f07c11e | 2013-09-08 10:50:37 +0200 | [diff] [blame] | 5 | * Copyright (C) 2012 - 2013 Pali Rohár <pali.rohar@gmail.com> |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/device.h> |
| 28 | #include <linux/sysfs.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/power_supply.h> |
| 31 | #include <linux/delay.h> |
Sebastian Reichel | 34a1096 | 2013-12-01 02:00:10 +0100 | [diff] [blame] | 32 | #include <linux/of.h> |
| 33 | #include <linux/of_gpio.h> |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 34 | |
| 35 | #include <linux/usb/otg.h> |
| 36 | #include <linux/usb/ulpi.h> |
| 37 | #include <linux/usb/ch9.h> |
| 38 | #include <linux/usb/gadget.h> |
Kalle Jokiniemi | 2785cef | 2011-03-29 16:27:59 +0300 | [diff] [blame] | 39 | #include <linux/power/isp1704_charger.h> |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 40 | |
| 41 | /* Vendor specific Power Control register */ |
| 42 | #define ISP1704_PWR_CTRL 0x3d |
| 43 | #define ISP1704_PWR_CTRL_SWCTRL (1 << 0) |
| 44 | #define ISP1704_PWR_CTRL_DET_COMP (1 << 1) |
| 45 | #define ISP1704_PWR_CTRL_BVALID_RISE (1 << 2) |
| 46 | #define ISP1704_PWR_CTRL_BVALID_FALL (1 << 3) |
| 47 | #define ISP1704_PWR_CTRL_DP_WKPU_EN (1 << 4) |
| 48 | #define ISP1704_PWR_CTRL_VDAT_DET (1 << 5) |
| 49 | #define ISP1704_PWR_CTRL_DPVSRC_EN (1 << 6) |
| 50 | #define ISP1704_PWR_CTRL_HWDETECT (1 << 7) |
| 51 | |
| 52 | #define NXP_VENDOR_ID 0x04cc |
| 53 | |
| 54 | static u16 isp170x_id[] = { |
| 55 | 0x1704, |
| 56 | 0x1707, |
| 57 | }; |
| 58 | |
| 59 | struct isp1704_charger { |
| 60 | struct device *dev; |
| 61 | struct power_supply psy; |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 62 | struct usb_phy *phy; |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 63 | struct notifier_block nb; |
| 64 | struct work_struct work; |
| 65 | |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 66 | /* properties */ |
Ameya Palande | 746d8fb | 2010-11-04 16:31:46 +0200 | [diff] [blame] | 67 | char model[8]; |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 68 | unsigned present:1; |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 69 | unsigned online:1; |
| 70 | unsigned current_max; |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 71 | }; |
| 72 | |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 73 | static inline int isp1704_read(struct isp1704_charger *isp, u32 reg) |
| 74 | { |
| 75 | return usb_phy_io_read(isp->phy, reg); |
| 76 | } |
| 77 | |
| 78 | static inline int isp1704_write(struct isp1704_charger *isp, u32 val, u32 reg) |
| 79 | { |
| 80 | return usb_phy_io_write(isp->phy, val, reg); |
| 81 | } |
| 82 | |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 83 | /* |
Kalle Jokiniemi | 2785cef | 2011-03-29 16:27:59 +0300 | [diff] [blame] | 84 | * Disable/enable the power from the isp1704 if a function for it |
| 85 | * has been provided with platform data. |
| 86 | */ |
| 87 | static void isp1704_charger_set_power(struct isp1704_charger *isp, bool on) |
| 88 | { |
| 89 | struct isp1704_charger_data *board = isp->dev->platform_data; |
| 90 | |
Felipe Contreras | c934502 | 2012-01-06 06:00:48 +0400 | [diff] [blame] | 91 | if (board && board->set_power) |
Kalle Jokiniemi | 2785cef | 2011-03-29 16:27:59 +0300 | [diff] [blame] | 92 | board->set_power(on); |
Sebastian Reichel | 34a1096 | 2013-12-01 02:00:10 +0100 | [diff] [blame] | 93 | else if (board) |
| 94 | gpio_set_value(board->enable_gpio, on); |
Kalle Jokiniemi | 2785cef | 2011-03-29 16:27:59 +0300 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 98 | * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB |
| 99 | * chargers). |
| 100 | * |
| 101 | * REVISIT: The method is defined in Battery Charging Specification and is |
| 102 | * applicable to any ULPI transceiver. Nothing isp170x specific here. |
| 103 | */ |
| 104 | static inline int isp1704_charger_type(struct isp1704_charger *isp) |
| 105 | { |
| 106 | u8 reg; |
| 107 | u8 func_ctrl; |
| 108 | u8 otg_ctrl; |
| 109 | int type = POWER_SUPPLY_TYPE_USB_DCP; |
| 110 | |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 111 | func_ctrl = isp1704_read(isp, ULPI_FUNC_CTRL); |
| 112 | otg_ctrl = isp1704_read(isp, ULPI_OTG_CTRL); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 113 | |
| 114 | /* disable pulldowns */ |
| 115 | reg = ULPI_OTG_CTRL_DM_PULLDOWN | ULPI_OTG_CTRL_DP_PULLDOWN; |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 116 | isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), reg); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 117 | |
| 118 | /* full speed */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 119 | isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL), |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 120 | ULPI_FUNC_CTRL_XCVRSEL_MASK); |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 121 | isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 122 | ULPI_FUNC_CTRL_FULL_SPEED); |
| 123 | |
| 124 | /* Enable strong pull-up on DP (1.5K) and reset */ |
| 125 | reg = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET; |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 126 | isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), reg); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 127 | usleep_range(1000, 2000); |
| 128 | |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 129 | reg = isp1704_read(isp, ULPI_DEBUG); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 130 | if ((reg & 3) != 3) |
| 131 | type = POWER_SUPPLY_TYPE_USB_CDP; |
| 132 | |
| 133 | /* recover original state */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 134 | isp1704_write(isp, ULPI_FUNC_CTRL, func_ctrl); |
| 135 | isp1704_write(isp, ULPI_OTG_CTRL, otg_ctrl); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 136 | |
| 137 | return type; |
| 138 | } |
| 139 | |
| 140 | /* |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 141 | * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger |
| 142 | * is actually a dedicated charger, the following steps need to be taken. |
| 143 | */ |
| 144 | static inline int isp1704_charger_verify(struct isp1704_charger *isp) |
| 145 | { |
| 146 | int ret = 0; |
| 147 | u8 r; |
| 148 | |
| 149 | /* Reset the transceiver */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 150 | r = isp1704_read(isp, ULPI_FUNC_CTRL); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 151 | r |= ULPI_FUNC_CTRL_RESET; |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 152 | isp1704_write(isp, ULPI_FUNC_CTRL, r); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 153 | usleep_range(1000, 2000); |
| 154 | |
| 155 | /* Set normal mode */ |
| 156 | r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK); |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 157 | isp1704_write(isp, ULPI_FUNC_CTRL, r); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 158 | |
| 159 | /* Clear the DP and DM pull-down bits */ |
| 160 | r = ULPI_OTG_CTRL_DP_PULLDOWN | ULPI_OTG_CTRL_DM_PULLDOWN; |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 161 | isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), r); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 162 | |
| 163 | /* Enable strong pull-up on DP (1.5K) and reset */ |
| 164 | r = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET; |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 165 | isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), r); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 166 | usleep_range(1000, 2000); |
| 167 | |
| 168 | /* Read the line state */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 169 | if (!isp1704_read(isp, ULPI_DEBUG)) { |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 170 | /* Disable strong pull-up on DP (1.5K) */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 171 | isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL), |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 172 | ULPI_FUNC_CTRL_TERMSELECT); |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | /* Is it a charger or PS/2 connection */ |
| 177 | |
| 178 | /* Enable weak pull-up resistor on DP */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 179 | isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL), |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 180 | ISP1704_PWR_CTRL_DP_WKPU_EN); |
| 181 | |
| 182 | /* Disable strong pull-up on DP (1.5K) */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 183 | isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL), |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 184 | ULPI_FUNC_CTRL_TERMSELECT); |
| 185 | |
| 186 | /* Enable weak pull-down resistor on DM */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 187 | isp1704_write(isp, ULPI_SET(ULPI_OTG_CTRL), |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 188 | ULPI_OTG_CTRL_DM_PULLDOWN); |
| 189 | |
| 190 | /* It's a charger if the line states are clear */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 191 | if (!(isp1704_read(isp, ULPI_DEBUG))) |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 192 | ret = 1; |
| 193 | |
| 194 | /* Disable weak pull-up resistor on DP */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 195 | isp1704_write(isp, ULPI_CLR(ISP1704_PWR_CTRL), |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 196 | ISP1704_PWR_CTRL_DP_WKPU_EN); |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static inline int isp1704_charger_detect(struct isp1704_charger *isp) |
| 202 | { |
| 203 | unsigned long timeout; |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 204 | u8 pwr_ctrl; |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 205 | int ret = 0; |
| 206 | |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 207 | pwr_ctrl = isp1704_read(isp, ISP1704_PWR_CTRL); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 208 | |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 209 | /* set SW control bit in PWR_CTRL register */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 210 | isp1704_write(isp, ISP1704_PWR_CTRL, |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 211 | ISP1704_PWR_CTRL_SWCTRL); |
| 212 | |
| 213 | /* enable manual charger detection */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 214 | isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL), |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 215 | ISP1704_PWR_CTRL_SWCTRL |
| 216 | | ISP1704_PWR_CTRL_DPVSRC_EN); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 217 | usleep_range(1000, 2000); |
| 218 | |
| 219 | timeout = jiffies + msecs_to_jiffies(300); |
| 220 | do { |
| 221 | /* Check if there is a charger */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 222 | if (isp1704_read(isp, ISP1704_PWR_CTRL) |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 223 | & ISP1704_PWR_CTRL_VDAT_DET) { |
| 224 | ret = isp1704_charger_verify(isp); |
| 225 | break; |
| 226 | } |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 227 | } while (!time_after(jiffies, timeout) && isp->online); |
| 228 | |
| 229 | /* recover original state */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 230 | isp1704_write(isp, ISP1704_PWR_CTRL, pwr_ctrl); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
Pali Rohár | f07c11e | 2013-09-08 10:50:37 +0200 | [diff] [blame] | 235 | static inline int isp1704_charger_detect_dcp(struct isp1704_charger *isp) |
| 236 | { |
| 237 | if (isp1704_charger_detect(isp) && |
| 238 | isp1704_charger_type(isp) == POWER_SUPPLY_TYPE_USB_DCP) |
| 239 | return true; |
| 240 | else |
| 241 | return false; |
| 242 | } |
| 243 | |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 244 | static void isp1704_charger_work(struct work_struct *data) |
| 245 | { |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 246 | struct isp1704_charger *isp = |
| 247 | container_of(data, struct isp1704_charger, work); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 248 | static DEFINE_MUTEX(lock); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 249 | |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 250 | mutex_lock(&lock); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 251 | |
Pali Rohár | f07c11e | 2013-09-08 10:50:37 +0200 | [diff] [blame] | 252 | switch (isp->phy->last_event) { |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 253 | case USB_EVENT_VBUS: |
Pali Rohár | f07c11e | 2013-09-08 10:50:37 +0200 | [diff] [blame] | 254 | /* do not call wall charger detection more times */ |
| 255 | if (!isp->present) { |
| 256 | isp->online = true; |
| 257 | isp->present = 1; |
| 258 | isp1704_charger_set_power(isp, 1); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 259 | |
Pali Rohár | f07c11e | 2013-09-08 10:50:37 +0200 | [diff] [blame] | 260 | /* detect wall charger */ |
| 261 | if (isp1704_charger_detect_dcp(isp)) { |
| 262 | isp->psy.type = POWER_SUPPLY_TYPE_USB_DCP; |
| 263 | isp->current_max = 1800; |
| 264 | } else { |
| 265 | isp->psy.type = POWER_SUPPLY_TYPE_USB; |
| 266 | isp->current_max = 500; |
| 267 | } |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 268 | |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 269 | /* enable data pullups */ |
Heikki Krogerus | b1c711d | 2012-02-13 13:24:17 +0200 | [diff] [blame] | 270 | if (isp->phy->otg->gadget) |
| 271 | usb_gadget_connect(isp->phy->otg->gadget); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 272 | } |
Pali Rohár | f07c11e | 2013-09-08 10:50:37 +0200 | [diff] [blame] | 273 | |
| 274 | if (isp->psy.type != POWER_SUPPLY_TYPE_USB_DCP) { |
| 275 | /* |
| 276 | * Only 500mA here or high speed chirp |
| 277 | * handshaking may break |
| 278 | */ |
| 279 | if (isp->current_max > 500) |
| 280 | isp->current_max = 500; |
| 281 | |
| 282 | if (isp->current_max > 100) |
| 283 | isp->psy.type = POWER_SUPPLY_TYPE_USB_CDP; |
| 284 | } |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 285 | break; |
| 286 | case USB_EVENT_NONE: |
| 287 | isp->online = false; |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 288 | isp->present = 0; |
| 289 | isp->current_max = 0; |
| 290 | isp->psy.type = POWER_SUPPLY_TYPE_USB; |
| 291 | |
| 292 | /* |
| 293 | * Disable data pullups. We need to prevent the controller from |
| 294 | * enumerating. |
| 295 | * |
| 296 | * FIXME: This is here to allow charger detection with Host/HUB |
| 297 | * chargers. The pullups may be enabled elsewhere, so this can |
| 298 | * not be the final solution. |
| 299 | */ |
Heikki Krogerus | b1c711d | 2012-02-13 13:24:17 +0200 | [diff] [blame] | 300 | if (isp->phy->otg->gadget) |
| 301 | usb_gadget_disconnect(isp->phy->otg->gadget); |
Kalle Jokiniemi | 2785cef | 2011-03-29 16:27:59 +0300 | [diff] [blame] | 302 | |
| 303 | isp1704_charger_set_power(isp, 0); |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 304 | break; |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 305 | default: |
| 306 | goto out; |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 307 | } |
| 308 | |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 309 | power_supply_changed(&isp->psy); |
| 310 | out: |
| 311 | mutex_unlock(&lock); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int isp1704_notifier_call(struct notifier_block *nb, |
Pali Rohár | f07c11e | 2013-09-08 10:50:37 +0200 | [diff] [blame] | 315 | unsigned long val, void *v) |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 316 | { |
| 317 | struct isp1704_charger *isp = |
| 318 | container_of(nb, struct isp1704_charger, nb); |
| 319 | |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 320 | schedule_work(&isp->work); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 321 | |
| 322 | return NOTIFY_OK; |
| 323 | } |
| 324 | |
| 325 | static int isp1704_charger_get_property(struct power_supply *psy, |
| 326 | enum power_supply_property psp, |
| 327 | union power_supply_propval *val) |
| 328 | { |
| 329 | struct isp1704_charger *isp = |
| 330 | container_of(psy, struct isp1704_charger, psy); |
| 331 | |
| 332 | switch (psp) { |
| 333 | case POWER_SUPPLY_PROP_PRESENT: |
| 334 | val->intval = isp->present; |
| 335 | break; |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 336 | case POWER_SUPPLY_PROP_ONLINE: |
| 337 | val->intval = isp->online; |
| 338 | break; |
| 339 | case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 340 | val->intval = isp->current_max; |
| 341 | break; |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 342 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 343 | val->strval = isp->model; |
| 344 | break; |
| 345 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 346 | val->strval = "NXP"; |
| 347 | break; |
| 348 | default: |
| 349 | return -EINVAL; |
| 350 | } |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static enum power_supply_property power_props[] = { |
| 355 | POWER_SUPPLY_PROP_PRESENT, |
Heikki Krogerus | bac43b2 | 2010-11-04 16:31:47 +0200 | [diff] [blame] | 356 | POWER_SUPPLY_PROP_ONLINE, |
| 357 | POWER_SUPPLY_PROP_CURRENT_MAX, |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 358 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 359 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 360 | }; |
| 361 | |
| 362 | static inline int isp1704_test_ulpi(struct isp1704_charger *isp) |
| 363 | { |
| 364 | int vendor; |
| 365 | int product; |
| 366 | int i; |
| 367 | int ret = -ENODEV; |
| 368 | |
| 369 | /* Test ULPI interface */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 370 | ret = isp1704_write(isp, ULPI_SCRATCH, 0xaa); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 371 | if (ret < 0) |
| 372 | return ret; |
| 373 | |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 374 | ret = isp1704_read(isp, ULPI_SCRATCH); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 375 | if (ret < 0) |
| 376 | return ret; |
| 377 | |
| 378 | if (ret != 0xaa) |
| 379 | return -ENODEV; |
| 380 | |
| 381 | /* Verify the product and vendor id matches */ |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 382 | vendor = isp1704_read(isp, ULPI_VENDOR_ID_LOW); |
| 383 | vendor |= isp1704_read(isp, ULPI_VENDOR_ID_HIGH) << 8; |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 384 | if (vendor != NXP_VENDOR_ID) |
| 385 | return -ENODEV; |
| 386 | |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 387 | product = isp1704_read(isp, ULPI_PRODUCT_ID_LOW); |
| 388 | product |= isp1704_read(isp, ULPI_PRODUCT_ID_HIGH) << 8; |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 389 | |
| 390 | for (i = 0; i < ARRAY_SIZE(isp170x_id); i++) { |
| 391 | if (product == isp170x_id[i]) { |
| 392 | sprintf(isp->model, "isp%x", product); |
| 393 | return product; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | dev_err(isp->dev, "product id %x not matching known ids", product); |
| 398 | |
| 399 | return -ENODEV; |
| 400 | } |
| 401 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 402 | static int isp1704_charger_probe(struct platform_device *pdev) |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 403 | { |
| 404 | struct isp1704_charger *isp; |
| 405 | int ret = -ENODEV; |
| 406 | |
Sebastian Reichel | 34a1096 | 2013-12-01 02:00:10 +0100 | [diff] [blame] | 407 | struct isp1704_charger_data *pdata = dev_get_platdata(&pdev->dev); |
| 408 | struct device_node *np = pdev->dev.of_node; |
| 409 | |
| 410 | if (np) { |
| 411 | int gpio = of_get_named_gpio(np, "nxp,enable-gpio", 0); |
| 412 | |
| 413 | if (gpio < 0) |
| 414 | return gpio; |
| 415 | |
| 416 | pdata = devm_kzalloc(&pdev->dev, |
| 417 | sizeof(struct isp1704_charger_data), GFP_KERNEL); |
| 418 | pdata->enable_gpio = gpio; |
| 419 | |
| 420 | dev_info(&pdev->dev, "init gpio %d\n", pdata->enable_gpio); |
| 421 | |
| 422 | ret = devm_gpio_request_one(&pdev->dev, pdata->enable_gpio, |
| 423 | GPIOF_OUT_INIT_HIGH, "isp1704_reset"); |
| 424 | if (ret) |
| 425 | goto fail0; |
| 426 | } |
| 427 | |
| 428 | if (!pdata) { |
| 429 | dev_err(&pdev->dev, "missing platform data!\n"); |
| 430 | return -ENODEV; |
| 431 | } |
| 432 | |
| 433 | |
Jingoo Han | 2a2ce52 | 2013-03-11 15:34:15 +0900 | [diff] [blame] | 434 | isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 435 | if (!isp) |
| 436 | return -ENOMEM; |
| 437 | |
Sebastian Reichel | 34a1096 | 2013-12-01 02:00:10 +0100 | [diff] [blame] | 438 | if (np) |
| 439 | isp->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0); |
| 440 | else |
| 441 | isp->phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2); |
| 442 | |
| 443 | if (IS_ERR(isp->phy)) { |
| 444 | ret = PTR_ERR(isp->phy); |
| 445 | goto fail0; |
| 446 | } |
| 447 | if (!isp->phy) |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 448 | goto fail0; |
| 449 | |
Heikki Krogerus | a4607d9 | 2010-11-04 16:31:48 +0200 | [diff] [blame] | 450 | isp->dev = &pdev->dev; |
| 451 | platform_set_drvdata(pdev, isp); |
| 452 | |
Kalle Jokiniemi | 2785cef | 2011-03-29 16:27:59 +0300 | [diff] [blame] | 453 | isp1704_charger_set_power(isp, 1); |
| 454 | |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 455 | ret = isp1704_test_ulpi(isp); |
| 456 | if (ret < 0) |
| 457 | goto fail1; |
| 458 | |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 459 | isp->psy.name = "isp1704"; |
| 460 | isp->psy.type = POWER_SUPPLY_TYPE_USB; |
| 461 | isp->psy.properties = power_props; |
| 462 | isp->psy.num_properties = ARRAY_SIZE(power_props); |
| 463 | isp->psy.get_property = isp1704_charger_get_property; |
| 464 | |
| 465 | ret = power_supply_register(isp->dev, &isp->psy); |
| 466 | if (ret) |
| 467 | goto fail1; |
| 468 | |
| 469 | /* |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 470 | * REVISIT: using work in order to allow the usb notifications to be |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 471 | * made atomically in the future. |
| 472 | */ |
| 473 | INIT_WORK(&isp->work, isp1704_charger_work); |
| 474 | |
| 475 | isp->nb.notifier_call = isp1704_notifier_call; |
| 476 | |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 477 | ret = usb_register_notifier(isp->phy, &isp->nb); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 478 | if (ret) |
| 479 | goto fail2; |
| 480 | |
| 481 | dev_info(isp->dev, "registered with product id %s\n", isp->model); |
| 482 | |
Heikki Krogerus | e1a85694e | 2010-11-08 12:22:40 +0200 | [diff] [blame] | 483 | /* |
| 484 | * Taking over the D+ pullup. |
| 485 | * |
| 486 | * FIXME: The device will be disconnected if it was already |
| 487 | * enumerated. The charger driver should be always loaded before any |
| 488 | * gadget is loaded. |
| 489 | */ |
Heikki Krogerus | b1c711d | 2012-02-13 13:24:17 +0200 | [diff] [blame] | 490 | if (isp->phy->otg->gadget) |
| 491 | usb_gadget_disconnect(isp->phy->otg->gadget); |
Heikki Krogerus | e1a85694e | 2010-11-08 12:22:40 +0200 | [diff] [blame] | 492 | |
Pali Rohár | f07c11e | 2013-09-08 10:50:37 +0200 | [diff] [blame] | 493 | if (isp->phy->last_event == USB_EVENT_NONE) |
| 494 | isp1704_charger_set_power(isp, 0); |
| 495 | |
Heikki Krogerus | e1a85694e | 2010-11-08 12:22:40 +0200 | [diff] [blame] | 496 | /* Detect charger if VBUS is valid (the cable was already plugged). */ |
Pali Rohár | f07c11e | 2013-09-08 10:50:37 +0200 | [diff] [blame] | 497 | if (isp->phy->last_event == USB_EVENT_VBUS && |
| 498 | !isp->phy->otg->default_a) |
Heikki Krogerus | e1a85694e | 2010-11-08 12:22:40 +0200 | [diff] [blame] | 499 | schedule_work(&isp->work); |
Heikki Krogerus | e1a85694e | 2010-11-08 12:22:40 +0200 | [diff] [blame] | 500 | |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 501 | return 0; |
| 502 | fail2: |
| 503 | power_supply_unregister(&isp->psy); |
| 504 | fail1: |
Dan Carpenter | 81a0838 | 2012-03-27 23:22:13 +0300 | [diff] [blame] | 505 | isp1704_charger_set_power(isp, 0); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 506 | fail0: |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 507 | dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret); |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 512 | static int isp1704_charger_remove(struct platform_device *pdev) |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 513 | { |
| 514 | struct isp1704_charger *isp = platform_get_drvdata(pdev); |
| 515 | |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 516 | usb_unregister_notifier(isp->phy, &isp->nb); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 517 | power_supply_unregister(&isp->psy); |
Kalle Jokiniemi | 2785cef | 2011-03-29 16:27:59 +0300 | [diff] [blame] | 518 | isp1704_charger_set_power(isp, 0); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
Sebastian Reichel | 34a1096 | 2013-12-01 02:00:10 +0100 | [diff] [blame] | 523 | #ifdef CONFIG_OF |
| 524 | static const struct of_device_id omap_isp1704_of_match[] = { |
| 525 | { .compatible = "nxp,isp1704", }, |
| 526 | {}, |
| 527 | }; |
| 528 | MODULE_DEVICE_TABLE(of, omap_isp1704_of_match); |
| 529 | #endif |
| 530 | |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 531 | static struct platform_driver isp1704_charger_driver = { |
| 532 | .driver = { |
| 533 | .name = "isp1704_charger", |
Sebastian Reichel | 34a1096 | 2013-12-01 02:00:10 +0100 | [diff] [blame] | 534 | .of_match_table = of_match_ptr(omap_isp1704_of_match), |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 535 | }, |
| 536 | .probe = isp1704_charger_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 537 | .remove = isp1704_charger_remove, |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 538 | }; |
| 539 | |
Axel Lin | 300bac7 | 2011-11-26 12:01:10 +0800 | [diff] [blame] | 540 | module_platform_driver(isp1704_charger_driver); |
Heikki Krogerus | ec46475 | 2010-08-19 15:09:36 +0300 | [diff] [blame] | 541 | |
| 542 | MODULE_ALIAS("platform:isp1704_charger"); |
| 543 | MODULE_AUTHOR("Nokia Corporation"); |
| 544 | MODULE_DESCRIPTION("ISP170x USB Charger driver"); |
| 545 | MODULE_LICENSE("GPL"); |