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/usb/otg.h>
34 #include <linux/usb/ulpi.h>
35 #include <linux/usb/ch9.h>
36 #include <linux/usb/gadget.h>
37 #include <linux/power/isp1704_charger.h>
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)
50 #define NXP_VENDOR_ID 0x04cc
52 static u16 isp170x_id
[] = {
57 struct isp1704_charger
{
59 struct power_supply psy
;
61 struct notifier_block nb
;
62 struct work_struct work
;
71 static inline int isp1704_read(struct isp1704_charger
*isp
, u32 reg
)
73 return usb_phy_io_read(isp
->phy
, reg
);
76 static inline int isp1704_write(struct isp1704_charger
*isp
, u32 val
, u32 reg
)
78 return usb_phy_io_write(isp
->phy
, val
, reg
);
82 * Disable/enable the power from the isp1704 if a function for it
83 * has been provided with platform data.
85 static void isp1704_charger_set_power(struct isp1704_charger
*isp
, bool on
)
87 struct isp1704_charger_data
*board
= isp
->dev
->platform_data
;
89 if (board
&& board
->set_power
)
94 * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
97 * REVISIT: The method is defined in Battery Charging Specification and is
98 * applicable to any ULPI transceiver. Nothing isp170x specific here.
100 static inline int isp1704_charger_type(struct isp1704_charger
*isp
)
105 int type
= POWER_SUPPLY_TYPE_USB_DCP
;
107 func_ctrl
= isp1704_read(isp
, ULPI_FUNC_CTRL
);
108 otg_ctrl
= isp1704_read(isp
, ULPI_OTG_CTRL
);
110 /* disable pulldowns */
111 reg
= ULPI_OTG_CTRL_DM_PULLDOWN
| ULPI_OTG_CTRL_DP_PULLDOWN
;
112 isp1704_write(isp
, ULPI_CLR(ULPI_OTG_CTRL
), reg
);
115 isp1704_write(isp
, ULPI_CLR(ULPI_FUNC_CTRL
),
116 ULPI_FUNC_CTRL_XCVRSEL_MASK
);
117 isp1704_write(isp
, ULPI_SET(ULPI_FUNC_CTRL
),
118 ULPI_FUNC_CTRL_FULL_SPEED
);
120 /* Enable strong pull-up on DP (1.5K) and reset */
121 reg
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
122 isp1704_write(isp
, ULPI_SET(ULPI_FUNC_CTRL
), reg
);
123 usleep_range(1000, 2000);
125 reg
= isp1704_read(isp
, ULPI_DEBUG
);
127 type
= POWER_SUPPLY_TYPE_USB_CDP
;
129 /* recover original state */
130 isp1704_write(isp
, ULPI_FUNC_CTRL
, func_ctrl
);
131 isp1704_write(isp
, ULPI_OTG_CTRL
, otg_ctrl
);
137 * 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.
140 static inline int isp1704_charger_verify(struct isp1704_charger
*isp
)
145 /* Reset the transceiver */
146 r
= isp1704_read(isp
, ULPI_FUNC_CTRL
);
147 r
|= ULPI_FUNC_CTRL_RESET
;
148 isp1704_write(isp
, ULPI_FUNC_CTRL
, r
);
149 usleep_range(1000, 2000);
151 /* Set normal mode */
152 r
&= ~(ULPI_FUNC_CTRL_RESET
| ULPI_FUNC_CTRL_OPMODE_MASK
);
153 isp1704_write(isp
, ULPI_FUNC_CTRL
, r
);
155 /* Clear the DP and DM pull-down bits */
156 r
= ULPI_OTG_CTRL_DP_PULLDOWN
| ULPI_OTG_CTRL_DM_PULLDOWN
;
157 isp1704_write(isp
, ULPI_CLR(ULPI_OTG_CTRL
), r
);
159 /* Enable strong pull-up on DP (1.5K) and reset */
160 r
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
161 isp1704_write(isp
, ULPI_SET(ULPI_FUNC_CTRL
), r
);
162 usleep_range(1000, 2000);
164 /* Read the line state */
165 if (!isp1704_read(isp
, ULPI_DEBUG
)) {
166 /* Disable strong pull-up on DP (1.5K) */
167 isp1704_write(isp
, ULPI_CLR(ULPI_FUNC_CTRL
),
168 ULPI_FUNC_CTRL_TERMSELECT
);
172 /* Is it a charger or PS/2 connection */
174 /* Enable weak pull-up resistor on DP */
175 isp1704_write(isp
, ULPI_SET(ISP1704_PWR_CTRL
),
176 ISP1704_PWR_CTRL_DP_WKPU_EN
);
178 /* Disable strong pull-up on DP (1.5K) */
179 isp1704_write(isp
, ULPI_CLR(ULPI_FUNC_CTRL
),
180 ULPI_FUNC_CTRL_TERMSELECT
);
182 /* Enable weak pull-down resistor on DM */
183 isp1704_write(isp
, ULPI_SET(ULPI_OTG_CTRL
),
184 ULPI_OTG_CTRL_DM_PULLDOWN
);
186 /* It's a charger if the line states are clear */
187 if (!(isp1704_read(isp
, ULPI_DEBUG
)))
190 /* Disable weak pull-up resistor on DP */
191 isp1704_write(isp
, ULPI_CLR(ISP1704_PWR_CTRL
),
192 ISP1704_PWR_CTRL_DP_WKPU_EN
);
197 static inline int isp1704_charger_detect(struct isp1704_charger
*isp
)
199 unsigned long timeout
;
203 pwr_ctrl
= isp1704_read(isp
, ISP1704_PWR_CTRL
);
205 /* set SW control bit in PWR_CTRL register */
206 isp1704_write(isp
, ISP1704_PWR_CTRL
,
207 ISP1704_PWR_CTRL_SWCTRL
);
209 /* enable manual charger detection */
210 isp1704_write(isp
, ULPI_SET(ISP1704_PWR_CTRL
),
211 ISP1704_PWR_CTRL_SWCTRL
212 | ISP1704_PWR_CTRL_DPVSRC_EN
);
213 usleep_range(1000, 2000);
215 timeout
= jiffies
+ msecs_to_jiffies(300);
217 /* Check if there is a charger */
218 if (isp1704_read(isp
, ISP1704_PWR_CTRL
)
219 & ISP1704_PWR_CTRL_VDAT_DET
) {
220 ret
= isp1704_charger_verify(isp
);
223 } while (!time_after(jiffies
, timeout
) && isp
->online
);
225 /* recover original state */
226 isp1704_write(isp
, ISP1704_PWR_CTRL
, pwr_ctrl
);
231 static inline int isp1704_charger_detect_dcp(struct isp1704_charger
*isp
)
233 if (isp1704_charger_detect(isp
) &&
234 isp1704_charger_type(isp
) == POWER_SUPPLY_TYPE_USB_DCP
)
240 static void isp1704_charger_work(struct work_struct
*data
)
242 struct isp1704_charger
*isp
=
243 container_of(data
, struct isp1704_charger
, work
);
244 static DEFINE_MUTEX(lock
);
248 switch (isp
->phy
->last_event
) {
250 /* do not call wall charger detection more times */
254 isp1704_charger_set_power(isp
, 1);
256 /* detect wall charger */
257 if (isp1704_charger_detect_dcp(isp
)) {
258 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB_DCP
;
259 isp
->current_max
= 1800;
261 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
262 isp
->current_max
= 500;
265 /* enable data pullups */
266 if (isp
->phy
->otg
->gadget
)
267 usb_gadget_connect(isp
->phy
->otg
->gadget
);
270 if (isp
->psy
.type
!= POWER_SUPPLY_TYPE_USB_DCP
) {
272 * Only 500mA here or high speed chirp
273 * handshaking may break
275 if (isp
->current_max
> 500)
276 isp
->current_max
= 500;
278 if (isp
->current_max
> 100)
279 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB_CDP
;
285 isp
->current_max
= 0;
286 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
289 * Disable data pullups. We need to prevent the controller from
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.
296 if (isp
->phy
->otg
->gadget
)
297 usb_gadget_disconnect(isp
->phy
->otg
->gadget
);
299 isp1704_charger_set_power(isp
, 0);
305 power_supply_changed(&isp
->psy
);
310 static int isp1704_notifier_call(struct notifier_block
*nb
,
311 unsigned long val
, void *v
)
313 struct isp1704_charger
*isp
=
314 container_of(nb
, struct isp1704_charger
, nb
);
316 schedule_work(&isp
->work
);
321 static int isp1704_charger_get_property(struct power_supply
*psy
,
322 enum power_supply_property psp
,
323 union power_supply_propval
*val
)
325 struct isp1704_charger
*isp
=
326 container_of(psy
, struct isp1704_charger
, psy
);
329 case POWER_SUPPLY_PROP_PRESENT
:
330 val
->intval
= isp
->present
;
332 case POWER_SUPPLY_PROP_ONLINE
:
333 val
->intval
= isp
->online
;
335 case POWER_SUPPLY_PROP_CURRENT_MAX
:
336 val
->intval
= isp
->current_max
;
338 case POWER_SUPPLY_PROP_MODEL_NAME
:
339 val
->strval
= isp
->model
;
341 case POWER_SUPPLY_PROP_MANUFACTURER
:
350 static enum power_supply_property power_props
[] = {
351 POWER_SUPPLY_PROP_PRESENT
,
352 POWER_SUPPLY_PROP_ONLINE
,
353 POWER_SUPPLY_PROP_CURRENT_MAX
,
354 POWER_SUPPLY_PROP_MODEL_NAME
,
355 POWER_SUPPLY_PROP_MANUFACTURER
,
358 static inline int isp1704_test_ulpi(struct isp1704_charger
*isp
)
365 /* Test ULPI interface */
366 ret
= isp1704_write(isp
, ULPI_SCRATCH
, 0xaa);
370 ret
= isp1704_read(isp
, ULPI_SCRATCH
);
377 /* Verify the product and vendor id matches */
378 vendor
= isp1704_read(isp
, ULPI_VENDOR_ID_LOW
);
379 vendor
|= isp1704_read(isp
, ULPI_VENDOR_ID_HIGH
) << 8;
380 if (vendor
!= NXP_VENDOR_ID
)
383 product
= isp1704_read(isp
, ULPI_PRODUCT_ID_LOW
);
384 product
|= isp1704_read(isp
, ULPI_PRODUCT_ID_HIGH
) << 8;
386 for (i
= 0; i
< ARRAY_SIZE(isp170x_id
); i
++) {
387 if (product
== isp170x_id
[i
]) {
388 sprintf(isp
->model
, "isp%x", product
);
393 dev_err(isp
->dev
, "product id %x not matching known ids", product
);
398 static int isp1704_charger_probe(struct platform_device
*pdev
)
400 struct isp1704_charger
*isp
;
403 isp
= devm_kzalloc(&pdev
->dev
, sizeof(*isp
), GFP_KERNEL
);
407 isp
->phy
= usb_get_phy(USB_PHY_TYPE_USB2
);
408 if (IS_ERR_OR_NULL(isp
->phy
))
411 isp
->dev
= &pdev
->dev
;
412 platform_set_drvdata(pdev
, isp
);
414 isp1704_charger_set_power(isp
, 1);
416 ret
= isp1704_test_ulpi(isp
);
420 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
;
426 ret
= power_supply_register(isp
->dev
, &isp
->psy
);
431 * REVISIT: using work in order to allow the usb notifications to be
432 * made atomically in the future.
434 INIT_WORK(&isp
->work
, isp1704_charger_work
);
436 isp
->nb
.notifier_call
= isp1704_notifier_call
;
438 ret
= usb_register_notifier(isp
->phy
, &isp
->nb
);
442 dev_info(isp
->dev
, "registered with product id %s\n", isp
->model
);
445 * Taking over the D+ pullup.
447 * FIXME: The device will be disconnected if it was already
448 * enumerated. The charger driver should be always loaded before any
451 if (isp
->phy
->otg
->gadget
)
452 usb_gadget_disconnect(isp
->phy
->otg
->gadget
);
454 if (isp
->phy
->last_event
== USB_EVENT_NONE
)
455 isp1704_charger_set_power(isp
, 0);
457 /* Detect charger if VBUS is valid (the cable was already plugged). */
458 if (isp
->phy
->last_event
== USB_EVENT_VBUS
&&
459 !isp
->phy
->otg
->default_a
)
460 schedule_work(&isp
->work
);
464 power_supply_unregister(&isp
->psy
);
466 isp1704_charger_set_power(isp
, 0);
467 usb_put_phy(isp
->phy
);
469 dev_err(&pdev
->dev
, "failed to register isp1704 with error %d\n", ret
);
474 static int isp1704_charger_remove(struct platform_device
*pdev
)
476 struct isp1704_charger
*isp
= platform_get_drvdata(pdev
);
478 usb_unregister_notifier(isp
->phy
, &isp
->nb
);
479 power_supply_unregister(&isp
->psy
);
480 usb_put_phy(isp
->phy
);
481 isp1704_charger_set_power(isp
, 0);
486 static struct platform_driver isp1704_charger_driver
= {
488 .name
= "isp1704_charger",
490 .probe
= isp1704_charger_probe
,
491 .remove
= isp1704_charger_remove
,
494 module_platform_driver(isp1704_charger_driver
);
496 MODULE_ALIAS("platform:isp1704_charger");
497 MODULE_AUTHOR("Nokia Corporation");
498 MODULE_DESCRIPTION("ISP170x USB Charger driver");
499 MODULE_LICENSE("GPL");