1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ISP1704 USB Charger Detection driver
5 * Copyright (C) 2010 Nokia Corporation
6 * Copyright (C) 2012 - 2013 Pali Rohár <pali.rohar@gmail.com>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/device.h>
15 #include <linux/sysfs.h>
16 #include <linux/platform_device.h>
17 #include <linux/power_supply.h>
18 #include <linux/delay.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/usb/otg.h>
23 #include <linux/usb/ulpi.h>
24 #include <linux/usb/ch9.h>
25 #include <linux/usb/gadget.h>
27 /* Vendor specific Power Control register */
28 #define ISP1704_PWR_CTRL 0x3d
29 #define ISP1704_PWR_CTRL_SWCTRL (1 << 0)
30 #define ISP1704_PWR_CTRL_DET_COMP (1 << 1)
31 #define ISP1704_PWR_CTRL_BVALID_RISE (1 << 2)
32 #define ISP1704_PWR_CTRL_BVALID_FALL (1 << 3)
33 #define ISP1704_PWR_CTRL_DP_WKPU_EN (1 << 4)
34 #define ISP1704_PWR_CTRL_VDAT_DET (1 << 5)
35 #define ISP1704_PWR_CTRL_DPVSRC_EN (1 << 6)
36 #define ISP1704_PWR_CTRL_HWDETECT (1 << 7)
38 #define NXP_VENDOR_ID 0x04cc
40 static u16 isp170x_id
[] = {
45 struct isp1704_charger
{
47 struct power_supply
*psy
;
48 struct power_supply_desc psy_desc
;
49 struct gpio_desc
*enable_gpio
;
51 struct notifier_block nb
;
52 struct work_struct work
;
61 static inline int isp1704_read(struct isp1704_charger
*isp
, u32 reg
)
63 return usb_phy_io_read(isp
->phy
, reg
);
66 static inline int isp1704_write(struct isp1704_charger
*isp
, u32 reg
, u32 val
)
68 return usb_phy_io_write(isp
->phy
, val
, reg
);
71 static void isp1704_charger_set_power(struct isp1704_charger
*isp
, bool on
)
73 gpiod_set_value(isp
->enable_gpio
, on
);
77 * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
80 * REVISIT: The method is defined in Battery Charging Specification and is
81 * applicable to any ULPI transceiver. Nothing isp170x specific here.
83 static inline int isp1704_charger_type(struct isp1704_charger
*isp
)
88 int type
= POWER_SUPPLY_TYPE_USB_DCP
;
90 func_ctrl
= isp1704_read(isp
, ULPI_FUNC_CTRL
);
91 otg_ctrl
= isp1704_read(isp
, ULPI_OTG_CTRL
);
93 /* disable pulldowns */
94 reg
= ULPI_OTG_CTRL_DM_PULLDOWN
| ULPI_OTG_CTRL_DP_PULLDOWN
;
95 isp1704_write(isp
, ULPI_CLR(ULPI_OTG_CTRL
), reg
);
98 isp1704_write(isp
, ULPI_CLR(ULPI_FUNC_CTRL
),
99 ULPI_FUNC_CTRL_XCVRSEL_MASK
);
100 isp1704_write(isp
, ULPI_SET(ULPI_FUNC_CTRL
),
101 ULPI_FUNC_CTRL_FULL_SPEED
);
103 /* Enable strong pull-up on DP (1.5K) and reset */
104 reg
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
105 isp1704_write(isp
, ULPI_SET(ULPI_FUNC_CTRL
), reg
);
106 usleep_range(1000, 2000);
108 reg
= isp1704_read(isp
, ULPI_DEBUG
);
110 type
= POWER_SUPPLY_TYPE_USB_CDP
;
112 /* recover original state */
113 isp1704_write(isp
, ULPI_FUNC_CTRL
, func_ctrl
);
114 isp1704_write(isp
, ULPI_OTG_CTRL
, otg_ctrl
);
120 * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
121 * is actually a dedicated charger, the following steps need to be taken.
123 static inline int isp1704_charger_verify(struct isp1704_charger
*isp
)
128 /* Reset the transceiver */
129 r
= isp1704_read(isp
, ULPI_FUNC_CTRL
);
130 r
|= ULPI_FUNC_CTRL_RESET
;
131 isp1704_write(isp
, ULPI_FUNC_CTRL
, r
);
132 usleep_range(1000, 2000);
134 /* Set normal mode */
135 r
&= ~(ULPI_FUNC_CTRL_RESET
| ULPI_FUNC_CTRL_OPMODE_MASK
);
136 isp1704_write(isp
, ULPI_FUNC_CTRL
, r
);
138 /* Clear the DP and DM pull-down bits */
139 r
= ULPI_OTG_CTRL_DP_PULLDOWN
| ULPI_OTG_CTRL_DM_PULLDOWN
;
140 isp1704_write(isp
, ULPI_CLR(ULPI_OTG_CTRL
), r
);
142 /* Enable strong pull-up on DP (1.5K) and reset */
143 r
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
144 isp1704_write(isp
, ULPI_SET(ULPI_FUNC_CTRL
), r
);
145 usleep_range(1000, 2000);
147 /* Read the line state */
148 if (!isp1704_read(isp
, ULPI_DEBUG
)) {
149 /* Disable strong pull-up on DP (1.5K) */
150 isp1704_write(isp
, ULPI_CLR(ULPI_FUNC_CTRL
),
151 ULPI_FUNC_CTRL_TERMSELECT
);
155 /* Is it a charger or PS/2 connection */
157 /* Enable weak pull-up resistor on DP */
158 isp1704_write(isp
, ULPI_SET(ISP1704_PWR_CTRL
),
159 ISP1704_PWR_CTRL_DP_WKPU_EN
);
161 /* Disable strong pull-up on DP (1.5K) */
162 isp1704_write(isp
, ULPI_CLR(ULPI_FUNC_CTRL
),
163 ULPI_FUNC_CTRL_TERMSELECT
);
165 /* Enable weak pull-down resistor on DM */
166 isp1704_write(isp
, ULPI_SET(ULPI_OTG_CTRL
),
167 ULPI_OTG_CTRL_DM_PULLDOWN
);
169 /* It's a charger if the line states are clear */
170 if (!(isp1704_read(isp
, ULPI_DEBUG
)))
173 /* Disable weak pull-up resistor on DP */
174 isp1704_write(isp
, ULPI_CLR(ISP1704_PWR_CTRL
),
175 ISP1704_PWR_CTRL_DP_WKPU_EN
);
180 static inline int isp1704_charger_detect(struct isp1704_charger
*isp
)
182 unsigned long timeout
;
186 pwr_ctrl
= isp1704_read(isp
, ISP1704_PWR_CTRL
);
188 /* set SW control bit in PWR_CTRL register */
189 isp1704_write(isp
, ISP1704_PWR_CTRL
,
190 ISP1704_PWR_CTRL_SWCTRL
);
192 /* enable manual charger detection */
193 isp1704_write(isp
, ULPI_SET(ISP1704_PWR_CTRL
),
194 ISP1704_PWR_CTRL_SWCTRL
195 | ISP1704_PWR_CTRL_DPVSRC_EN
);
196 usleep_range(1000, 2000);
198 timeout
= jiffies
+ msecs_to_jiffies(300);
200 /* Check if there is a charger */
201 if (isp1704_read(isp
, ISP1704_PWR_CTRL
)
202 & ISP1704_PWR_CTRL_VDAT_DET
) {
203 ret
= isp1704_charger_verify(isp
);
206 } while (!time_after(jiffies
, timeout
) && isp
->online
);
208 /* recover original state */
209 isp1704_write(isp
, ISP1704_PWR_CTRL
, pwr_ctrl
);
214 static inline int isp1704_charger_detect_dcp(struct isp1704_charger
*isp
)
216 if (isp1704_charger_detect(isp
) &&
217 isp1704_charger_type(isp
) == POWER_SUPPLY_TYPE_USB_DCP
)
223 static void isp1704_charger_work(struct work_struct
*data
)
225 struct isp1704_charger
*isp
=
226 container_of(data
, struct isp1704_charger
, work
);
227 static DEFINE_MUTEX(lock
);
231 switch (isp
->phy
->last_event
) {
233 /* do not call wall charger detection more times */
237 isp1704_charger_set_power(isp
, 1);
239 /* detect wall charger */
240 if (isp1704_charger_detect_dcp(isp
)) {
241 isp
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB_DCP
;
242 isp
->current_max
= 1800;
244 isp
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
;
245 isp
->current_max
= 500;
248 /* enable data pullups */
249 if (isp
->phy
->otg
->gadget
)
250 usb_gadget_connect(isp
->phy
->otg
->gadget
);
253 if (isp
->psy_desc
.type
!= POWER_SUPPLY_TYPE_USB_DCP
) {
255 * Only 500mA here or high speed chirp
256 * handshaking may break
258 if (isp
->current_max
> 500)
259 isp
->current_max
= 500;
261 if (isp
->current_max
> 100)
262 isp
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB_CDP
;
268 isp
->current_max
= 0;
269 isp
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
;
272 * Disable data pullups. We need to prevent the controller from
275 * FIXME: This is here to allow charger detection with Host/HUB
276 * chargers. The pullups may be enabled elsewhere, so this can
277 * not be the final solution.
279 if (isp
->phy
->otg
->gadget
)
280 usb_gadget_disconnect(isp
->phy
->otg
->gadget
);
282 isp1704_charger_set_power(isp
, 0);
288 power_supply_changed(isp
->psy
);
293 static int isp1704_notifier_call(struct notifier_block
*nb
,
294 unsigned long val
, void *v
)
296 struct isp1704_charger
*isp
=
297 container_of(nb
, struct isp1704_charger
, nb
);
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
= power_supply_get_drvdata(psy
);
311 case POWER_SUPPLY_PROP_PRESENT
:
312 val
->intval
= isp
->present
;
314 case POWER_SUPPLY_PROP_ONLINE
:
315 val
->intval
= isp
->online
;
317 case POWER_SUPPLY_PROP_CURRENT_MAX
:
318 val
->intval
= isp
->current_max
;
320 case POWER_SUPPLY_PROP_MODEL_NAME
:
321 val
->strval
= isp
->model
;
323 case POWER_SUPPLY_PROP_MANUFACTURER
:
332 static enum power_supply_property power_props
[] = {
333 POWER_SUPPLY_PROP_PRESENT
,
334 POWER_SUPPLY_PROP_ONLINE
,
335 POWER_SUPPLY_PROP_CURRENT_MAX
,
336 POWER_SUPPLY_PROP_MODEL_NAME
,
337 POWER_SUPPLY_PROP_MANUFACTURER
,
340 static inline int isp1704_test_ulpi(struct isp1704_charger
*isp
)
347 /* Test ULPI interface */
348 ret
= isp1704_write(isp
, ULPI_SCRATCH
, 0xaa);
352 ret
= isp1704_read(isp
, ULPI_SCRATCH
);
359 /* Verify the product and vendor id matches */
360 vendor
= isp1704_read(isp
, ULPI_VENDOR_ID_LOW
);
361 vendor
|= isp1704_read(isp
, ULPI_VENDOR_ID_HIGH
) << 8;
362 if (vendor
!= NXP_VENDOR_ID
)
365 product
= isp1704_read(isp
, ULPI_PRODUCT_ID_LOW
);
366 product
|= isp1704_read(isp
, ULPI_PRODUCT_ID_HIGH
) << 8;
368 for (i
= 0; i
< ARRAY_SIZE(isp170x_id
); i
++) {
369 if (product
== isp170x_id
[i
]) {
370 sprintf(isp
->model
, "isp%x", product
);
375 dev_err(isp
->dev
, "product id %x not matching known ids", product
);
380 static int isp1704_charger_probe(struct platform_device
*pdev
)
382 struct isp1704_charger
*isp
;
384 struct power_supply_config psy_cfg
= {};
386 isp
= devm_kzalloc(&pdev
->dev
, sizeof(*isp
), GFP_KERNEL
);
390 isp
->enable_gpio
= devm_gpiod_get(&pdev
->dev
, "nxp,enable",
392 if (IS_ERR(isp
->enable_gpio
)) {
393 ret
= PTR_ERR(isp
->enable_gpio
);
394 dev_err(&pdev
->dev
, "Could not get reset gpio: %d\n", ret
);
398 if (pdev
->dev
.of_node
)
399 isp
->phy
= devm_usb_get_phy_by_phandle(&pdev
->dev
, "usb-phy", 0);
401 isp
->phy
= devm_usb_get_phy(&pdev
->dev
, USB_PHY_TYPE_USB2
);
403 if (IS_ERR(isp
->phy
)) {
404 ret
= PTR_ERR(isp
->phy
);
405 dev_err(&pdev
->dev
, "usb_get_phy failed\n");
409 isp
->dev
= &pdev
->dev
;
410 platform_set_drvdata(pdev
, isp
);
412 isp1704_charger_set_power(isp
, 1);
414 ret
= isp1704_test_ulpi(isp
);
416 dev_err(&pdev
->dev
, "isp1704_test_ulpi failed\n");
420 isp
->psy_desc
.name
= "isp1704";
421 isp
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
;
422 isp
->psy_desc
.properties
= power_props
;
423 isp
->psy_desc
.num_properties
= ARRAY_SIZE(power_props
);
424 isp
->psy_desc
.get_property
= isp1704_charger_get_property
;
426 psy_cfg
.drv_data
= isp
;
428 isp
->psy
= power_supply_register(isp
->dev
, &isp
->psy_desc
, &psy_cfg
);
429 if (IS_ERR(isp
->psy
)) {
430 ret
= PTR_ERR(isp
->psy
);
431 dev_err(&pdev
->dev
, "power_supply_register failed\n");
436 * REVISIT: using work in order to allow the usb notifications to be
437 * made atomically in the future.
439 INIT_WORK(&isp
->work
, isp1704_charger_work
);
441 isp
->nb
.notifier_call
= isp1704_notifier_call
;
443 ret
= usb_register_notifier(isp
->phy
, &isp
->nb
);
445 dev_err(&pdev
->dev
, "usb_register_notifier failed\n");
449 dev_info(isp
->dev
, "registered with product id %s\n", isp
->model
);
452 * Taking over the D+ pullup.
454 * FIXME: The device will be disconnected if it was already
455 * enumerated. The charger driver should be always loaded before any
458 if (isp
->phy
->otg
->gadget
)
459 usb_gadget_disconnect(isp
->phy
->otg
->gadget
);
461 if (isp
->phy
->last_event
== USB_EVENT_NONE
)
462 isp1704_charger_set_power(isp
, 0);
464 /* Detect charger if VBUS is valid (the cable was already plugged). */
465 if (isp
->phy
->last_event
== USB_EVENT_VBUS
&&
466 !isp
->phy
->otg
->default_a
)
467 schedule_work(&isp
->work
);
471 power_supply_unregister(isp
->psy
);
473 isp1704_charger_set_power(isp
, 0);
475 dev_err(&pdev
->dev
, "failed to register isp1704 with error %d\n", ret
);
480 static int isp1704_charger_remove(struct platform_device
*pdev
)
482 struct isp1704_charger
*isp
= platform_get_drvdata(pdev
);
484 usb_unregister_notifier(isp
->phy
, &isp
->nb
);
485 power_supply_unregister(isp
->psy
);
486 isp1704_charger_set_power(isp
, 0);
492 static const struct of_device_id omap_isp1704_of_match
[] = {
493 { .compatible
= "nxp,isp1704", },
494 { .compatible
= "nxp,isp1707", },
497 MODULE_DEVICE_TABLE(of
, omap_isp1704_of_match
);
500 static struct platform_driver isp1704_charger_driver
= {
502 .name
= "isp1704_charger",
503 .of_match_table
= of_match_ptr(omap_isp1704_of_match
),
505 .probe
= isp1704_charger_probe
,
506 .remove
= isp1704_charger_remove
,
509 module_platform_driver(isp1704_charger_driver
);
511 MODULE_ALIAS("platform:isp1704_charger");
512 MODULE_AUTHOR("Nokia Corporation");
513 MODULE_DESCRIPTION("ISP170x USB Charger driver");
514 MODULE_LICENSE("GPL");