2 * ISP1704 USB Charger Detection driver
4 * Copyright (C) 2010 Nokia Corporation
5 * Copyright (C) 2012 - 2013 Pali Rohár <pali.rohar@gmail.com>
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.
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.
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
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>
33 #include <linux/of_gpio.h>
35 #include <linux/usb/otg.h>
36 #include <linux/usb/ulpi.h>
37 #include <linux/usb/ch9.h>
38 #include <linux/usb/gadget.h>
39 #include <linux/power/isp1704_charger.h>
41 /* Vendor specific Power Control register */
42 #define ISP1704_PWR_CTRL 0x3d
43 #define ISP1704_PWR_CTRL_SWCTRL (1 << 0)
44 #define ISP1704_PWR_CTRL_DET_COMP (1 << 1)
45 #define ISP1704_PWR_CTRL_BVALID_RISE (1 << 2)
46 #define ISP1704_PWR_CTRL_BVALID_FALL (1 << 3)
47 #define ISP1704_PWR_CTRL_DP_WKPU_EN (1 << 4)
48 #define ISP1704_PWR_CTRL_VDAT_DET (1 << 5)
49 #define ISP1704_PWR_CTRL_DPVSRC_EN (1 << 6)
50 #define ISP1704_PWR_CTRL_HWDETECT (1 << 7)
52 #define NXP_VENDOR_ID 0x04cc
54 static u16 isp170x_id
[] = {
59 struct isp1704_charger
{
61 struct power_supply psy
;
63 struct notifier_block nb
;
64 struct work_struct work
;
73 static inline int isp1704_read(struct isp1704_charger
*isp
, u32 reg
)
75 return usb_phy_io_read(isp
->phy
, reg
);
78 static inline int isp1704_write(struct isp1704_charger
*isp
, u32 val
, u32 reg
)
80 return usb_phy_io_write(isp
->phy
, val
, reg
);
84 * Disable/enable the power from the isp1704 if a function for it
85 * has been provided with platform data.
87 static void isp1704_charger_set_power(struct isp1704_charger
*isp
, bool on
)
89 struct isp1704_charger_data
*board
= isp
->dev
->platform_data
;
91 if (board
&& board
->set_power
)
94 gpio_set_value(board
->enable_gpio
, on
);
98 * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
101 * REVISIT: The method is defined in Battery Charging Specification and is
102 * applicable to any ULPI transceiver. Nothing isp170x specific here.
104 static inline int isp1704_charger_type(struct isp1704_charger
*isp
)
109 int type
= POWER_SUPPLY_TYPE_USB_DCP
;
111 func_ctrl
= isp1704_read(isp
, ULPI_FUNC_CTRL
);
112 otg_ctrl
= isp1704_read(isp
, ULPI_OTG_CTRL
);
114 /* disable pulldowns */
115 reg
= ULPI_OTG_CTRL_DM_PULLDOWN
| ULPI_OTG_CTRL_DP_PULLDOWN
;
116 isp1704_write(isp
, ULPI_CLR(ULPI_OTG_CTRL
), reg
);
119 isp1704_write(isp
, ULPI_CLR(ULPI_FUNC_CTRL
),
120 ULPI_FUNC_CTRL_XCVRSEL_MASK
);
121 isp1704_write(isp
, ULPI_SET(ULPI_FUNC_CTRL
),
122 ULPI_FUNC_CTRL_FULL_SPEED
);
124 /* Enable strong pull-up on DP (1.5K) and reset */
125 reg
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
126 isp1704_write(isp
, ULPI_SET(ULPI_FUNC_CTRL
), reg
);
127 usleep_range(1000, 2000);
129 reg
= isp1704_read(isp
, ULPI_DEBUG
);
131 type
= POWER_SUPPLY_TYPE_USB_CDP
;
133 /* recover original state */
134 isp1704_write(isp
, ULPI_FUNC_CTRL
, func_ctrl
);
135 isp1704_write(isp
, ULPI_OTG_CTRL
, otg_ctrl
);
141 * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
142 * is actually a dedicated charger, the following steps need to be taken.
144 static inline int isp1704_charger_verify(struct isp1704_charger
*isp
)
149 /* Reset the transceiver */
150 r
= isp1704_read(isp
, ULPI_FUNC_CTRL
);
151 r
|= ULPI_FUNC_CTRL_RESET
;
152 isp1704_write(isp
, ULPI_FUNC_CTRL
, r
);
153 usleep_range(1000, 2000);
155 /* Set normal mode */
156 r
&= ~(ULPI_FUNC_CTRL_RESET
| ULPI_FUNC_CTRL_OPMODE_MASK
);
157 isp1704_write(isp
, ULPI_FUNC_CTRL
, r
);
159 /* Clear the DP and DM pull-down bits */
160 r
= ULPI_OTG_CTRL_DP_PULLDOWN
| ULPI_OTG_CTRL_DM_PULLDOWN
;
161 isp1704_write(isp
, ULPI_CLR(ULPI_OTG_CTRL
), r
);
163 /* Enable strong pull-up on DP (1.5K) and reset */
164 r
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
165 isp1704_write(isp
, ULPI_SET(ULPI_FUNC_CTRL
), r
);
166 usleep_range(1000, 2000);
168 /* Read the line state */
169 if (!isp1704_read(isp
, ULPI_DEBUG
)) {
170 /* Disable strong pull-up on DP (1.5K) */
171 isp1704_write(isp
, ULPI_CLR(ULPI_FUNC_CTRL
),
172 ULPI_FUNC_CTRL_TERMSELECT
);
176 /* Is it a charger or PS/2 connection */
178 /* Enable weak pull-up resistor on DP */
179 isp1704_write(isp
, ULPI_SET(ISP1704_PWR_CTRL
),
180 ISP1704_PWR_CTRL_DP_WKPU_EN
);
182 /* Disable strong pull-up on DP (1.5K) */
183 isp1704_write(isp
, ULPI_CLR(ULPI_FUNC_CTRL
),
184 ULPI_FUNC_CTRL_TERMSELECT
);
186 /* Enable weak pull-down resistor on DM */
187 isp1704_write(isp
, ULPI_SET(ULPI_OTG_CTRL
),
188 ULPI_OTG_CTRL_DM_PULLDOWN
);
190 /* It's a charger if the line states are clear */
191 if (!(isp1704_read(isp
, ULPI_DEBUG
)))
194 /* Disable weak pull-up resistor on DP */
195 isp1704_write(isp
, ULPI_CLR(ISP1704_PWR_CTRL
),
196 ISP1704_PWR_CTRL_DP_WKPU_EN
);
201 static inline int isp1704_charger_detect(struct isp1704_charger
*isp
)
203 unsigned long timeout
;
207 pwr_ctrl
= isp1704_read(isp
, ISP1704_PWR_CTRL
);
209 /* set SW control bit in PWR_CTRL register */
210 isp1704_write(isp
, ISP1704_PWR_CTRL
,
211 ISP1704_PWR_CTRL_SWCTRL
);
213 /* enable manual charger detection */
214 isp1704_write(isp
, ULPI_SET(ISP1704_PWR_CTRL
),
215 ISP1704_PWR_CTRL_SWCTRL
216 | ISP1704_PWR_CTRL_DPVSRC_EN
);
217 usleep_range(1000, 2000);
219 timeout
= jiffies
+ msecs_to_jiffies(300);
221 /* Check if there is a charger */
222 if (isp1704_read(isp
, ISP1704_PWR_CTRL
)
223 & ISP1704_PWR_CTRL_VDAT_DET
) {
224 ret
= isp1704_charger_verify(isp
);
227 } while (!time_after(jiffies
, timeout
) && isp
->online
);
229 /* recover original state */
230 isp1704_write(isp
, ISP1704_PWR_CTRL
, pwr_ctrl
);
235 static inline int isp1704_charger_detect_dcp(struct isp1704_charger
*isp
)
237 if (isp1704_charger_detect(isp
) &&
238 isp1704_charger_type(isp
) == POWER_SUPPLY_TYPE_USB_DCP
)
244 static void isp1704_charger_work(struct work_struct
*data
)
246 struct isp1704_charger
*isp
=
247 container_of(data
, struct isp1704_charger
, work
);
248 static DEFINE_MUTEX(lock
);
252 switch (isp
->phy
->last_event
) {
254 /* do not call wall charger detection more times */
258 isp1704_charger_set_power(isp
, 1);
260 /* detect wall charger */
261 if (isp1704_charger_detect_dcp(isp
)) {
262 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB_DCP
;
263 isp
->current_max
= 1800;
265 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
266 isp
->current_max
= 500;
269 /* enable data pullups */
270 if (isp
->phy
->otg
->gadget
)
271 usb_gadget_connect(isp
->phy
->otg
->gadget
);
274 if (isp
->psy
.type
!= POWER_SUPPLY_TYPE_USB_DCP
) {
276 * Only 500mA here or high speed chirp
277 * handshaking may break
279 if (isp
->current_max
> 500)
280 isp
->current_max
= 500;
282 if (isp
->current_max
> 100)
283 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB_CDP
;
289 isp
->current_max
= 0;
290 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
293 * Disable data pullups. We need to prevent the controller from
296 * FIXME: This is here to allow charger detection with Host/HUB
297 * chargers. The pullups may be enabled elsewhere, so this can
298 * not be the final solution.
300 if (isp
->phy
->otg
->gadget
)
301 usb_gadget_disconnect(isp
->phy
->otg
->gadget
);
303 isp1704_charger_set_power(isp
, 0);
309 power_supply_changed(&isp
->psy
);
314 static int isp1704_notifier_call(struct notifier_block
*nb
,
315 unsigned long val
, void *v
)
317 struct isp1704_charger
*isp
=
318 container_of(nb
, struct isp1704_charger
, nb
);
320 schedule_work(&isp
->work
);
325 static int isp1704_charger_get_property(struct power_supply
*psy
,
326 enum power_supply_property psp
,
327 union power_supply_propval
*val
)
329 struct isp1704_charger
*isp
=
330 container_of(psy
, struct isp1704_charger
, psy
);
333 case POWER_SUPPLY_PROP_PRESENT
:
334 val
->intval
= isp
->present
;
336 case POWER_SUPPLY_PROP_ONLINE
:
337 val
->intval
= isp
->online
;
339 case POWER_SUPPLY_PROP_CURRENT_MAX
:
340 val
->intval
= isp
->current_max
;
342 case POWER_SUPPLY_PROP_MODEL_NAME
:
343 val
->strval
= isp
->model
;
345 case POWER_SUPPLY_PROP_MANUFACTURER
:
354 static enum power_supply_property power_props
[] = {
355 POWER_SUPPLY_PROP_PRESENT
,
356 POWER_SUPPLY_PROP_ONLINE
,
357 POWER_SUPPLY_PROP_CURRENT_MAX
,
358 POWER_SUPPLY_PROP_MODEL_NAME
,
359 POWER_SUPPLY_PROP_MANUFACTURER
,
362 static inline int isp1704_test_ulpi(struct isp1704_charger
*isp
)
369 /* Test ULPI interface */
370 ret
= isp1704_write(isp
, ULPI_SCRATCH
, 0xaa);
374 ret
= isp1704_read(isp
, ULPI_SCRATCH
);
381 /* Verify the product and vendor id matches */
382 vendor
= isp1704_read(isp
, ULPI_VENDOR_ID_LOW
);
383 vendor
|= isp1704_read(isp
, ULPI_VENDOR_ID_HIGH
) << 8;
384 if (vendor
!= NXP_VENDOR_ID
)
387 product
= isp1704_read(isp
, ULPI_PRODUCT_ID_LOW
);
388 product
|= isp1704_read(isp
, ULPI_PRODUCT_ID_HIGH
) << 8;
390 for (i
= 0; i
< ARRAY_SIZE(isp170x_id
); i
++) {
391 if (product
== isp170x_id
[i
]) {
392 sprintf(isp
->model
, "isp%x", product
);
397 dev_err(isp
->dev
, "product id %x not matching known ids", product
);
402 static int isp1704_charger_probe(struct platform_device
*pdev
)
404 struct isp1704_charger
*isp
;
407 struct isp1704_charger_data
*pdata
= dev_get_platdata(&pdev
->dev
);
408 struct device_node
*np
= pdev
->dev
.of_node
;
411 int gpio
= of_get_named_gpio(np
, "nxp,enable-gpio", 0);
416 pdata
= devm_kzalloc(&pdev
->dev
,
417 sizeof(struct isp1704_charger_data
), GFP_KERNEL
);
418 pdata
->enable_gpio
= gpio
;
420 dev_info(&pdev
->dev
, "init gpio %d\n", pdata
->enable_gpio
);
422 ret
= devm_gpio_request_one(&pdev
->dev
, pdata
->enable_gpio
,
423 GPIOF_OUT_INIT_HIGH
, "isp1704_reset");
429 dev_err(&pdev
->dev
, "missing platform data!\n");
434 isp
= devm_kzalloc(&pdev
->dev
, sizeof(*isp
), GFP_KERNEL
);
439 isp
->phy
= devm_usb_get_phy_by_phandle(&pdev
->dev
, "usb-phy", 0);
441 isp
->phy
= devm_usb_get_phy(&pdev
->dev
, USB_PHY_TYPE_USB2
);
443 if (IS_ERR(isp
->phy
)) {
444 ret
= PTR_ERR(isp
->phy
);
448 isp
->dev
= &pdev
->dev
;
449 platform_set_drvdata(pdev
, isp
);
451 isp1704_charger_set_power(isp
, 1);
453 ret
= isp1704_test_ulpi(isp
);
457 isp
->psy
.name
= "isp1704";
458 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
459 isp
->psy
.properties
= power_props
;
460 isp
->psy
.num_properties
= ARRAY_SIZE(power_props
);
461 isp
->psy
.get_property
= isp1704_charger_get_property
;
463 ret
= power_supply_register(isp
->dev
, &isp
->psy
);
468 * REVISIT: using work in order to allow the usb notifications to be
469 * made atomically in the future.
471 INIT_WORK(&isp
->work
, isp1704_charger_work
);
473 isp
->nb
.notifier_call
= isp1704_notifier_call
;
475 ret
= usb_register_notifier(isp
->phy
, &isp
->nb
);
479 dev_info(isp
->dev
, "registered with product id %s\n", isp
->model
);
482 * Taking over the D+ pullup.
484 * FIXME: The device will be disconnected if it was already
485 * enumerated. The charger driver should be always loaded before any
488 if (isp
->phy
->otg
->gadget
)
489 usb_gadget_disconnect(isp
->phy
->otg
->gadget
);
491 if (isp
->phy
->last_event
== USB_EVENT_NONE
)
492 isp1704_charger_set_power(isp
, 0);
494 /* Detect charger if VBUS is valid (the cable was already plugged). */
495 if (isp
->phy
->last_event
== USB_EVENT_VBUS
&&
496 !isp
->phy
->otg
->default_a
)
497 schedule_work(&isp
->work
);
501 power_supply_unregister(&isp
->psy
);
503 isp1704_charger_set_power(isp
, 0);
505 dev_err(&pdev
->dev
, "failed to register isp1704 with error %d\n", ret
);
510 static int isp1704_charger_remove(struct platform_device
*pdev
)
512 struct isp1704_charger
*isp
= platform_get_drvdata(pdev
);
514 usb_unregister_notifier(isp
->phy
, &isp
->nb
);
515 power_supply_unregister(&isp
->psy
);
516 isp1704_charger_set_power(isp
, 0);
522 static const struct of_device_id omap_isp1704_of_match
[] = {
523 { .compatible
= "nxp,isp1704", },
526 MODULE_DEVICE_TABLE(of
, omap_isp1704_of_match
);
529 static struct platform_driver isp1704_charger_driver
= {
531 .name
= "isp1704_charger",
532 .of_match_table
= of_match_ptr(omap_isp1704_of_match
),
534 .probe
= isp1704_charger_probe
,
535 .remove
= isp1704_charger_remove
,
538 module_platform_driver(isp1704_charger_driver
);
540 MODULE_ALIAS("platform:isp1704_charger");
541 MODULE_AUTHOR("Nokia Corporation");
542 MODULE_DESCRIPTION("ISP170x USB Charger driver");
543 MODULE_LICENSE("GPL");