2 * ISP1704 USB Charger Detection driver
4 * Copyright (C) 2010 Nokia Corporation
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.
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.
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
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>
32 #include <linux/usb/otg.h>
33 #include <linux/usb/ulpi.h>
34 #include <linux/usb/ch9.h>
35 #include <linux/usb/gadget.h>
37 /* Vendor specific Power Control register */
38 #define ISP1704_PWR_CTRL 0x3d
39 #define ISP1704_PWR_CTRL_SWCTRL (1 << 0)
40 #define ISP1704_PWR_CTRL_DET_COMP (1 << 1)
41 #define ISP1704_PWR_CTRL_BVALID_RISE (1 << 2)
42 #define ISP1704_PWR_CTRL_BVALID_FALL (1 << 3)
43 #define ISP1704_PWR_CTRL_DP_WKPU_EN (1 << 4)
44 #define ISP1704_PWR_CTRL_VDAT_DET (1 << 5)
45 #define ISP1704_PWR_CTRL_DPVSRC_EN (1 << 6)
46 #define ISP1704_PWR_CTRL_HWDETECT (1 << 7)
48 #define NXP_VENDOR_ID 0x04cc
50 static u16 isp170x_id
[] = {
55 struct isp1704_charger
{
57 struct power_supply psy
;
58 struct otg_transceiver
*otg
;
59 struct notifier_block nb
;
60 struct work_struct work
;
68 /* temp storage variables */
74 * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
77 * REVISIT: The method is defined in Battery Charging Specification and is
78 * applicable to any ULPI transceiver. Nothing isp170x specific here.
80 static inline int isp1704_charger_type(struct isp1704_charger
*isp
)
85 int type
= POWER_SUPPLY_TYPE_USB_DCP
;
87 func_ctrl
= otg_io_read(isp
->otg
, ULPI_FUNC_CTRL
);
88 otg_ctrl
= otg_io_read(isp
->otg
, ULPI_OTG_CTRL
);
90 /* disable pulldowns */
91 reg
= ULPI_OTG_CTRL_DM_PULLDOWN
| ULPI_OTG_CTRL_DP_PULLDOWN
;
92 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_OTG_CTRL
), reg
);
95 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_FUNC_CTRL
),
96 ULPI_FUNC_CTRL_XCVRSEL_MASK
);
97 otg_io_write(isp
->otg
, ULPI_SET(ULPI_FUNC_CTRL
),
98 ULPI_FUNC_CTRL_FULL_SPEED
);
100 /* Enable strong pull-up on DP (1.5K) and reset */
101 reg
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
102 otg_io_write(isp
->otg
, ULPI_SET(ULPI_FUNC_CTRL
), reg
);
103 usleep_range(1000, 2000);
105 reg
= otg_io_read(isp
->otg
, ULPI_DEBUG
);
107 type
= POWER_SUPPLY_TYPE_USB_CDP
;
109 /* recover original state */
110 otg_io_write(isp
->otg
, ULPI_FUNC_CTRL
, func_ctrl
);
111 otg_io_write(isp
->otg
, ULPI_OTG_CTRL
, otg_ctrl
);
117 * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
118 * is actually a dedicated charger, the following steps need to be taken.
120 static inline int isp1704_charger_verify(struct isp1704_charger
*isp
)
125 /* Reset the transceiver */
126 r
= otg_io_read(isp
->otg
, ULPI_FUNC_CTRL
);
127 r
|= ULPI_FUNC_CTRL_RESET
;
128 otg_io_write(isp
->otg
, ULPI_FUNC_CTRL
, r
);
129 usleep_range(1000, 2000);
131 /* Set normal mode */
132 r
&= ~(ULPI_FUNC_CTRL_RESET
| ULPI_FUNC_CTRL_OPMODE_MASK
);
133 otg_io_write(isp
->otg
, ULPI_FUNC_CTRL
, r
);
135 /* Clear the DP and DM pull-down bits */
136 r
= ULPI_OTG_CTRL_DP_PULLDOWN
| ULPI_OTG_CTRL_DM_PULLDOWN
;
137 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_OTG_CTRL
), r
);
139 /* Enable strong pull-up on DP (1.5K) and reset */
140 r
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
141 otg_io_write(isp
->otg
, ULPI_SET(ULPI_FUNC_CTRL
), r
);
142 usleep_range(1000, 2000);
144 /* Read the line state */
145 if (!otg_io_read(isp
->otg
, ULPI_DEBUG
)) {
146 /* Disable strong pull-up on DP (1.5K) */
147 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_FUNC_CTRL
),
148 ULPI_FUNC_CTRL_TERMSELECT
);
152 /* Is it a charger or PS/2 connection */
154 /* Enable weak pull-up resistor on DP */
155 otg_io_write(isp
->otg
, ULPI_SET(ISP1704_PWR_CTRL
),
156 ISP1704_PWR_CTRL_DP_WKPU_EN
);
158 /* Disable strong pull-up on DP (1.5K) */
159 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_FUNC_CTRL
),
160 ULPI_FUNC_CTRL_TERMSELECT
);
162 /* Enable weak pull-down resistor on DM */
163 otg_io_write(isp
->otg
, ULPI_SET(ULPI_OTG_CTRL
),
164 ULPI_OTG_CTRL_DM_PULLDOWN
);
166 /* It's a charger if the line states are clear */
167 if (!(otg_io_read(isp
->otg
, ULPI_DEBUG
)))
170 /* Disable weak pull-up resistor on DP */
171 otg_io_write(isp
->otg
, ULPI_CLR(ISP1704_PWR_CTRL
),
172 ISP1704_PWR_CTRL_DP_WKPU_EN
);
177 static inline int isp1704_charger_detect(struct isp1704_charger
*isp
)
179 unsigned long timeout
;
183 pwr_ctrl
= otg_io_read(isp
->otg
, ISP1704_PWR_CTRL
);
185 /* set SW control bit in PWR_CTRL register */
186 otg_io_write(isp
->otg
, ISP1704_PWR_CTRL
,
187 ISP1704_PWR_CTRL_SWCTRL
);
189 /* enable manual charger detection */
190 otg_io_write(isp
->otg
, ULPI_SET(ISP1704_PWR_CTRL
),
191 ISP1704_PWR_CTRL_SWCTRL
192 | ISP1704_PWR_CTRL_DPVSRC_EN
);
193 usleep_range(1000, 2000);
195 timeout
= jiffies
+ msecs_to_jiffies(300);
197 /* Check if there is a charger */
198 if (otg_io_read(isp
->otg
, ISP1704_PWR_CTRL
)
199 & ISP1704_PWR_CTRL_VDAT_DET
) {
200 ret
= isp1704_charger_verify(isp
);
203 } while (!time_after(jiffies
, timeout
) && isp
->online
);
205 /* recover original state */
206 otg_io_write(isp
->otg
, ISP1704_PWR_CTRL
, pwr_ctrl
);
211 static void isp1704_charger_work(struct work_struct
*data
)
216 struct isp1704_charger
*isp
=
217 container_of(data
, struct isp1704_charger
, work
);
218 static DEFINE_MUTEX(lock
);
221 power
= isp
->max_power
;
230 detect
= isp1704_charger_detect(isp
);
233 isp
->present
= detect
;
234 isp
->psy
.type
= isp1704_charger_type(isp
);
237 switch (isp
->psy
.type
) {
238 case POWER_SUPPLY_TYPE_USB_DCP
:
239 isp
->current_max
= 1800;
241 case POWER_SUPPLY_TYPE_USB_CDP
:
243 * Only 500mA here or high speed chirp
244 * handshaking may break
246 isp
->current_max
= 500;
248 case POWER_SUPPLY_TYPE_USB
:
250 /* enable data pullups */
251 if (isp
->otg
->gadget
)
252 usb_gadget_connect(isp
->otg
->gadget
);
257 isp
->current_max
= 0;
259 isp
->current_max
= 0;
260 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
263 * Disable data pullups. We need to prevent the controller from
266 * FIXME: This is here to allow charger detection with Host/HUB
267 * chargers. The pullups may be enabled elsewhere, so this can
268 * not be the final solution.
270 if (isp
->otg
->gadget
)
271 usb_gadget_disconnect(isp
->otg
->gadget
);
273 case USB_EVENT_ENUMERATED
:
275 isp
->current_max
= 1800;
277 isp
->current_max
= power
;
283 power_supply_changed(&isp
->psy
);
288 static int isp1704_notifier_call(struct notifier_block
*nb
,
289 unsigned long event
, void *power
)
291 struct isp1704_charger
*isp
=
292 container_of(nb
, struct isp1704_charger
, nb
);
297 isp
->max_power
= *((unsigned *)power
);
299 schedule_work(&isp
->work
);
304 static int isp1704_charger_get_property(struct power_supply
*psy
,
305 enum power_supply_property psp
,
306 union power_supply_propval
*val
)
308 struct isp1704_charger
*isp
=
309 container_of(psy
, struct isp1704_charger
, psy
);
312 case POWER_SUPPLY_PROP_PRESENT
:
313 val
->intval
= isp
->present
;
315 case POWER_SUPPLY_PROP_ONLINE
:
316 val
->intval
= isp
->online
;
318 case POWER_SUPPLY_PROP_CURRENT_MAX
:
319 val
->intval
= isp
->current_max
;
321 case POWER_SUPPLY_PROP_MODEL_NAME
:
322 val
->strval
= isp
->model
;
324 case POWER_SUPPLY_PROP_MANUFACTURER
:
333 static enum power_supply_property power_props
[] = {
334 POWER_SUPPLY_PROP_PRESENT
,
335 POWER_SUPPLY_PROP_ONLINE
,
336 POWER_SUPPLY_PROP_CURRENT_MAX
,
337 POWER_SUPPLY_PROP_MODEL_NAME
,
338 POWER_SUPPLY_PROP_MANUFACTURER
,
341 static inline int isp1704_test_ulpi(struct isp1704_charger
*isp
)
348 /* Test ULPI interface */
349 ret
= otg_io_write(isp
->otg
, ULPI_SCRATCH
, 0xaa);
353 ret
= otg_io_read(isp
->otg
, ULPI_SCRATCH
);
360 /* Verify the product and vendor id matches */
361 vendor
= otg_io_read(isp
->otg
, ULPI_VENDOR_ID_LOW
);
362 vendor
|= otg_io_read(isp
->otg
, ULPI_VENDOR_ID_HIGH
) << 8;
363 if (vendor
!= NXP_VENDOR_ID
)
366 product
= otg_io_read(isp
->otg
, ULPI_PRODUCT_ID_LOW
);
367 product
|= otg_io_read(isp
->otg
, ULPI_PRODUCT_ID_HIGH
) << 8;
369 for (i
= 0; i
< ARRAY_SIZE(isp170x_id
); i
++) {
370 if (product
== isp170x_id
[i
]) {
371 sprintf(isp
->model
, "isp%x", product
);
376 dev_err(isp
->dev
, "product id %x not matching known ids", product
);
381 static int __devinit
isp1704_charger_probe(struct platform_device
*pdev
)
383 struct isp1704_charger
*isp
;
386 isp
= kzalloc(sizeof *isp
, GFP_KERNEL
);
390 isp
->otg
= otg_get_transceiver();
394 isp
->dev
= &pdev
->dev
;
395 platform_set_drvdata(pdev
, isp
);
397 ret
= isp1704_test_ulpi(isp
);
401 isp
->psy
.name
= "isp1704";
402 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
403 isp
->psy
.properties
= power_props
;
404 isp
->psy
.num_properties
= ARRAY_SIZE(power_props
);
405 isp
->psy
.get_property
= isp1704_charger_get_property
;
407 ret
= power_supply_register(isp
->dev
, &isp
->psy
);
412 * REVISIT: using work in order to allow the otg notifications to be
413 * made atomically in the future.
415 INIT_WORK(&isp
->work
, isp1704_charger_work
);
417 isp
->nb
.notifier_call
= isp1704_notifier_call
;
419 ret
= otg_register_notifier(isp
->otg
, &isp
->nb
);
423 dev_info(isp
->dev
, "registered with product id %s\n", isp
->model
);
426 * Taking over the D+ pullup.
428 * FIXME: The device will be disconnected if it was already
429 * enumerated. The charger driver should be always loaded before any
432 if (isp
->otg
->gadget
)
433 usb_gadget_disconnect(isp
->otg
->gadget
);
435 /* Detect charger if VBUS is valid (the cable was already plugged). */
436 ret
= otg_io_read(isp
->otg
, ULPI_USB_INT_STS
);
437 if ((ret
& ULPI_INT_VBUS_VALID
) && !isp
->otg
->default_a
) {
438 isp
->event
= USB_EVENT_VBUS
;
439 schedule_work(&isp
->work
);
444 power_supply_unregister(&isp
->psy
);
446 otg_put_transceiver(isp
->otg
);
450 dev_err(&pdev
->dev
, "failed to register isp1704 with error %d\n", ret
);
455 static int __devexit
isp1704_charger_remove(struct platform_device
*pdev
)
457 struct isp1704_charger
*isp
= platform_get_drvdata(pdev
);
459 otg_unregister_notifier(isp
->otg
, &isp
->nb
);
460 power_supply_unregister(&isp
->psy
);
461 otg_put_transceiver(isp
->otg
);
467 static struct platform_driver isp1704_charger_driver
= {
469 .name
= "isp1704_charger",
471 .probe
= isp1704_charger_probe
,
472 .remove
= __devexit_p(isp1704_charger_remove
),
475 static int __init
isp1704_charger_init(void)
477 return platform_driver_register(&isp1704_charger_driver
);
479 module_init(isp1704_charger_init
);
481 static void __exit
isp1704_charger_exit(void)
483 platform_driver_unregister(&isp1704_charger_driver
);
485 module_exit(isp1704_charger_exit
);
487 MODULE_ALIAS("platform:isp1704_charger");
488 MODULE_AUTHOR("Nokia Corporation");
489 MODULE_DESCRIPTION("ISP170x USB Charger driver");
490 MODULE_LICENSE("GPL");