blob: 1bb3a91b1acc2eb376d6460006218a7b1f4e1e76 [file] [log] [blame]
Heikki Krogerusec464752010-08-19 15:09:36 +03001/*
2 * ISP1704 USB Charger Detection driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
Pali Rohárf07c11e2013-09-08 10:50:37 +02005 * Copyright (C) 2012 - 2013 Pali Rohár <pali.rohar@gmail.com>
Heikki Krogerusec464752010-08-19 15:09:36 +03006 *
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 Jokiniemi2785cef2011-03-29 16:27:59 +030037#include <linux/power/isp1704_charger.h>
Heikki Krogerusec464752010-08-19 15:09:36 +030038
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
52static u16 isp170x_id[] = {
53 0x1704,
54 0x1707,
55};
56
57struct isp1704_charger {
58 struct device *dev;
59 struct power_supply psy;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +020060 struct usb_phy *phy;
Heikki Krogerusec464752010-08-19 15:09:36 +030061 struct notifier_block nb;
62 struct work_struct work;
63
Heikki Krogerusbac43b22010-11-04 16:31:47 +020064 /* properties */
Ameya Palande746d8fb2010-11-04 16:31:46 +020065 char model[8];
Heikki Krogerusec464752010-08-19 15:09:36 +030066 unsigned present:1;
Heikki Krogerusbac43b22010-11-04 16:31:47 +020067 unsigned online:1;
68 unsigned current_max;
Heikki Krogerusec464752010-08-19 15:09:36 +030069};
70
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +020071static inline int isp1704_read(struct isp1704_charger *isp, u32 reg)
72{
73 return usb_phy_io_read(isp->phy, reg);
74}
75
76static 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 Krogerusec464752010-08-19 15:09:36 +030081/*
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +030082 * Disable/enable the power from the isp1704 if a function for it
83 * has been provided with platform data.
84 */
85static void isp1704_charger_set_power(struct isp1704_charger *isp, bool on)
86{
87 struct isp1704_charger_data *board = isp->dev->platform_data;
88
Felipe Contrerasc9345022012-01-06 06:00:48 +040089 if (board && board->set_power)
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +030090 board->set_power(on);
91}
92
93/*
Heikki Krogerusbac43b22010-11-04 16:31:47 +020094 * 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 */
100static 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 Krogerusfcc8ebc2012-02-13 13:24:16 +0200107 func_ctrl = isp1704_read(isp, ULPI_FUNC_CTRL);
108 otg_ctrl = isp1704_read(isp, ULPI_OTG_CTRL);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200109
110 /* disable pulldowns */
111 reg = ULPI_OTG_CTRL_DM_PULLDOWN | ULPI_OTG_CTRL_DP_PULLDOWN;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200112 isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), reg);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200113
114 /* full speed */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200115 isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200116 ULPI_FUNC_CTRL_XCVRSEL_MASK);
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200117 isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL),
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200118 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 Krogerusfcc8ebc2012-02-13 13:24:16 +0200122 isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), reg);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200123 usleep_range(1000, 2000);
124
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200125 reg = isp1704_read(isp, ULPI_DEBUG);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200126 if ((reg & 3) != 3)
127 type = POWER_SUPPLY_TYPE_USB_CDP;
128
129 /* recover original state */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200130 isp1704_write(isp, ULPI_FUNC_CTRL, func_ctrl);
131 isp1704_write(isp, ULPI_OTG_CTRL, otg_ctrl);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200132
133 return type;
134}
135
136/*
Heikki Krogerusec464752010-08-19 15:09:36 +0300137 * 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 */
140static inline int isp1704_charger_verify(struct isp1704_charger *isp)
141{
142 int ret = 0;
143 u8 r;
144
145 /* Reset the transceiver */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200146 r = isp1704_read(isp, ULPI_FUNC_CTRL);
Heikki Krogerusec464752010-08-19 15:09:36 +0300147 r |= ULPI_FUNC_CTRL_RESET;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200148 isp1704_write(isp, ULPI_FUNC_CTRL, r);
Heikki Krogerusec464752010-08-19 15:09:36 +0300149 usleep_range(1000, 2000);
150
151 /* Set normal mode */
152 r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK);
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200153 isp1704_write(isp, ULPI_FUNC_CTRL, r);
Heikki Krogerusec464752010-08-19 15:09:36 +0300154
155 /* Clear the DP and DM pull-down bits */
156 r = ULPI_OTG_CTRL_DP_PULLDOWN | ULPI_OTG_CTRL_DM_PULLDOWN;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200157 isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), r);
Heikki Krogerusec464752010-08-19 15:09:36 +0300158
159 /* Enable strong pull-up on DP (1.5K) and reset */
160 r = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200161 isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), r);
Heikki Krogerusec464752010-08-19 15:09:36 +0300162 usleep_range(1000, 2000);
163
164 /* Read the line state */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200165 if (!isp1704_read(isp, ULPI_DEBUG)) {
Heikki Krogerusec464752010-08-19 15:09:36 +0300166 /* Disable strong pull-up on DP (1.5K) */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200167 isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300168 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 Krogerusfcc8ebc2012-02-13 13:24:16 +0200175 isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300176 ISP1704_PWR_CTRL_DP_WKPU_EN);
177
178 /* Disable strong pull-up on DP (1.5K) */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200179 isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300180 ULPI_FUNC_CTRL_TERMSELECT);
181
182 /* Enable weak pull-down resistor on DM */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200183 isp1704_write(isp, ULPI_SET(ULPI_OTG_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300184 ULPI_OTG_CTRL_DM_PULLDOWN);
185
186 /* It's a charger if the line states are clear */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200187 if (!(isp1704_read(isp, ULPI_DEBUG)))
Heikki Krogerusec464752010-08-19 15:09:36 +0300188 ret = 1;
189
190 /* Disable weak pull-up resistor on DP */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200191 isp1704_write(isp, ULPI_CLR(ISP1704_PWR_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300192 ISP1704_PWR_CTRL_DP_WKPU_EN);
193
194 return ret;
195}
196
197static inline int isp1704_charger_detect(struct isp1704_charger *isp)
198{
199 unsigned long timeout;
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200200 u8 pwr_ctrl;
Heikki Krogerusec464752010-08-19 15:09:36 +0300201 int ret = 0;
202
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200203 pwr_ctrl = isp1704_read(isp, ISP1704_PWR_CTRL);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200204
Heikki Krogerusec464752010-08-19 15:09:36 +0300205 /* set SW control bit in PWR_CTRL register */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200206 isp1704_write(isp, ISP1704_PWR_CTRL,
Heikki Krogerusec464752010-08-19 15:09:36 +0300207 ISP1704_PWR_CTRL_SWCTRL);
208
209 /* enable manual charger detection */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200210 isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200211 ISP1704_PWR_CTRL_SWCTRL
212 | ISP1704_PWR_CTRL_DPVSRC_EN);
Heikki Krogerusec464752010-08-19 15:09:36 +0300213 usleep_range(1000, 2000);
214
215 timeout = jiffies + msecs_to_jiffies(300);
216 do {
217 /* Check if there is a charger */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200218 if (isp1704_read(isp, ISP1704_PWR_CTRL)
Heikki Krogerusec464752010-08-19 15:09:36 +0300219 & ISP1704_PWR_CTRL_VDAT_DET) {
220 ret = isp1704_charger_verify(isp);
221 break;
222 }
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200223 } while (!time_after(jiffies, timeout) && isp->online);
224
225 /* recover original state */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200226 isp1704_write(isp, ISP1704_PWR_CTRL, pwr_ctrl);
Heikki Krogerusec464752010-08-19 15:09:36 +0300227
228 return ret;
229}
230
Pali Rohárf07c11e2013-09-08 10:50:37 +0200231static 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 Krogerusec464752010-08-19 15:09:36 +0300240static void isp1704_charger_work(struct work_struct *data)
241{
Heikki Krogerusec464752010-08-19 15:09:36 +0300242 struct isp1704_charger *isp =
243 container_of(data, struct isp1704_charger, work);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200244 static DEFINE_MUTEX(lock);
Heikki Krogerusec464752010-08-19 15:09:36 +0300245
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200246 mutex_lock(&lock);
Heikki Krogerusec464752010-08-19 15:09:36 +0300247
Pali Rohárf07c11e2013-09-08 10:50:37 +0200248 switch (isp->phy->last_event) {
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200249 case USB_EVENT_VBUS:
Pali Rohárf07c11e2013-09-08 10:50:37 +0200250 /* 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 Krogerusbac43b22010-11-04 16:31:47 +0200255
Pali Rohárf07c11e2013-09-08 10:50:37 +0200256 /* 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 Krogerusbac43b22010-11-04 16:31:47 +0200264
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200265 /* enable data pullups */
Heikki Krogerusb1c711d2012-02-13 13:24:17 +0200266 if (isp->phy->otg->gadget)
267 usb_gadget_connect(isp->phy->otg->gadget);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200268 }
Pali Rohárf07c11e2013-09-08 10:50:37 +0200269
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 Krogerusbac43b22010-11-04 16:31:47 +0200281 break;
282 case USB_EVENT_NONE:
283 isp->online = false;
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200284 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 Krogerusb1c711d2012-02-13 13:24:17 +0200296 if (isp->phy->otg->gadget)
297 usb_gadget_disconnect(isp->phy->otg->gadget);
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +0300298
299 isp1704_charger_set_power(isp, 0);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200300 break;
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200301 default:
302 goto out;
Heikki Krogerusec464752010-08-19 15:09:36 +0300303 }
304
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200305 power_supply_changed(&isp->psy);
306out:
307 mutex_unlock(&lock);
Heikki Krogerusec464752010-08-19 15:09:36 +0300308}
309
310static int isp1704_notifier_call(struct notifier_block *nb,
Pali Rohárf07c11e2013-09-08 10:50:37 +0200311 unsigned long val, void *v)
Heikki Krogerusec464752010-08-19 15:09:36 +0300312{
313 struct isp1704_charger *isp =
314 container_of(nb, struct isp1704_charger, nb);
315
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200316 schedule_work(&isp->work);
Heikki Krogerusec464752010-08-19 15:09:36 +0300317
318 return NOTIFY_OK;
319}
320
321static 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 Krogerusbac43b22010-11-04 16:31:47 +0200332 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 Krogerusec464752010-08-19 15:09:36 +0300338 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
350static enum power_supply_property power_props[] = {
351 POWER_SUPPLY_PROP_PRESENT,
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200352 POWER_SUPPLY_PROP_ONLINE,
353 POWER_SUPPLY_PROP_CURRENT_MAX,
Heikki Krogerusec464752010-08-19 15:09:36 +0300354 POWER_SUPPLY_PROP_MODEL_NAME,
355 POWER_SUPPLY_PROP_MANUFACTURER,
356};
357
358static 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 Krogerusfcc8ebc2012-02-13 13:24:16 +0200366 ret = isp1704_write(isp, ULPI_SCRATCH, 0xaa);
Heikki Krogerusec464752010-08-19 15:09:36 +0300367 if (ret < 0)
368 return ret;
369
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200370 ret = isp1704_read(isp, ULPI_SCRATCH);
Heikki Krogerusec464752010-08-19 15:09:36 +0300371 if (ret < 0)
372 return ret;
373
374 if (ret != 0xaa)
375 return -ENODEV;
376
377 /* Verify the product and vendor id matches */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200378 vendor = isp1704_read(isp, ULPI_VENDOR_ID_LOW);
379 vendor |= isp1704_read(isp, ULPI_VENDOR_ID_HIGH) << 8;
Heikki Krogerusec464752010-08-19 15:09:36 +0300380 if (vendor != NXP_VENDOR_ID)
381 return -ENODEV;
382
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200383 product = isp1704_read(isp, ULPI_PRODUCT_ID_LOW);
384 product |= isp1704_read(isp, ULPI_PRODUCT_ID_HIGH) << 8;
Heikki Krogerusec464752010-08-19 15:09:36 +0300385
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 Pembertonc8afa642012-11-19 13:22:23 -0500398static int isp1704_charger_probe(struct platform_device *pdev)
Heikki Krogerusec464752010-08-19 15:09:36 +0300399{
400 struct isp1704_charger *isp;
401 int ret = -ENODEV;
402
Jingoo Han2a2ce522013-03-11 15:34:15 +0900403 isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
Heikki Krogerusec464752010-08-19 15:09:36 +0300404 if (!isp)
405 return -ENOMEM;
406
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530407 isp->phy = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530408 if (IS_ERR_OR_NULL(isp->phy))
Heikki Krogerusec464752010-08-19 15:09:36 +0300409 goto fail0;
410
Heikki Krogerusa4607d92010-11-04 16:31:48 +0200411 isp->dev = &pdev->dev;
412 platform_set_drvdata(pdev, isp);
413
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +0300414 isp1704_charger_set_power(isp, 1);
415
Heikki Krogerusec464752010-08-19 15:09:36 +0300416 ret = isp1704_test_ulpi(isp);
417 if (ret < 0)
418 goto fail1;
419
Heikki Krogerusec464752010-08-19 15:09:36 +0300420 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 Krogerusfcc8ebc2012-02-13 13:24:16 +0200431 * REVISIT: using work in order to allow the usb notifications to be
Heikki Krogerusec464752010-08-19 15:09:36 +0300432 * 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 Krogerusfcc8ebc2012-02-13 13:24:16 +0200438 ret = usb_register_notifier(isp->phy, &isp->nb);
Heikki Krogerusec464752010-08-19 15:09:36 +0300439 if (ret)
440 goto fail2;
441
442 dev_info(isp->dev, "registered with product id %s\n", isp->model);
443
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200444 /*
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 Krogerusb1c711d2012-02-13 13:24:17 +0200451 if (isp->phy->otg->gadget)
452 usb_gadget_disconnect(isp->phy->otg->gadget);
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200453
Pali Rohárf07c11e2013-09-08 10:50:37 +0200454 if (isp->phy->last_event == USB_EVENT_NONE)
455 isp1704_charger_set_power(isp, 0);
456
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200457 /* Detect charger if VBUS is valid (the cable was already plugged). */
Pali Rohárf07c11e2013-09-08 10:50:37 +0200458 if (isp->phy->last_event == USB_EVENT_VBUS &&
459 !isp->phy->otg->default_a)
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200460 schedule_work(&isp->work);
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200461
Heikki Krogerusec464752010-08-19 15:09:36 +0300462 return 0;
463fail2:
464 power_supply_unregister(&isp->psy);
465fail1:
Dan Carpenter81a08382012-03-27 23:22:13 +0300466 isp1704_charger_set_power(isp, 0);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530467 usb_put_phy(isp->phy);
Heikki Krogerusec464752010-08-19 15:09:36 +0300468fail0:
Heikki Krogerusec464752010-08-19 15:09:36 +0300469 dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret);
470
471 return ret;
472}
473
Bill Pemberton415ec692012-11-19 13:26:07 -0500474static int isp1704_charger_remove(struct platform_device *pdev)
Heikki Krogerusec464752010-08-19 15:09:36 +0300475{
476 struct isp1704_charger *isp = platform_get_drvdata(pdev);
477
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200478 usb_unregister_notifier(isp->phy, &isp->nb);
Heikki Krogerusec464752010-08-19 15:09:36 +0300479 power_supply_unregister(&isp->psy);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530480 usb_put_phy(isp->phy);
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +0300481 isp1704_charger_set_power(isp, 0);
Heikki Krogerusec464752010-08-19 15:09:36 +0300482
483 return 0;
484}
485
486static struct platform_driver isp1704_charger_driver = {
487 .driver = {
488 .name = "isp1704_charger",
489 },
490 .probe = isp1704_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500491 .remove = isp1704_charger_remove,
Heikki Krogerusec464752010-08-19 15:09:36 +0300492};
493
Axel Lin300bac72011-11-26 12:01:10 +0800494module_platform_driver(isp1704_charger_driver);
Heikki Krogerusec464752010-08-19 15:09:36 +0300495
496MODULE_ALIAS("platform:isp1704_charger");
497MODULE_AUTHOR("Nokia Corporation");
498MODULE_DESCRIPTION("ISP170x USB Charger driver");
499MODULE_LICENSE("GPL");