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