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