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>
36 #include <linux/power/isp1704_charger.h>
38 /* Vendor specific Power Control register */
39 #define ISP1704_PWR_CTRL 0x3d
40 #define ISP1704_PWR_CTRL_SWCTRL (1 << 0)
41 #define ISP1704_PWR_CTRL_DET_COMP (1 << 1)
42 #define ISP1704_PWR_CTRL_BVALID_RISE (1 << 2)
43 #define ISP1704_PWR_CTRL_BVALID_FALL (1 << 3)
44 #define ISP1704_PWR_CTRL_DP_WKPU_EN (1 << 4)
45 #define ISP1704_PWR_CTRL_VDAT_DET (1 << 5)
46 #define ISP1704_PWR_CTRL_DPVSRC_EN (1 << 6)
47 #define ISP1704_PWR_CTRL_HWDETECT (1 << 7)
49 #define NXP_VENDOR_ID 0x04cc
51 static u16 isp170x_id
[] = {
56 struct isp1704_charger
{
58 struct power_supply psy
;
59 struct otg_transceiver
*otg
;
60 struct notifier_block nb
;
61 struct work_struct work
;
69 /* temp storage variables */
75 * Disable/enable the power from the isp1704 if a function for it
76 * has been provided with platform data.
78 static void isp1704_charger_set_power(struct isp1704_charger
*isp
, bool on
)
80 struct isp1704_charger_data
*board
= isp
->dev
->platform_data
;
82 if (board
&& board
->set_power
)
87 * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
90 * REVISIT: The method is defined in Battery Charging Specification and is
91 * applicable to any ULPI transceiver. Nothing isp170x specific here.
93 static inline int isp1704_charger_type(struct isp1704_charger
*isp
)
98 int type
= POWER_SUPPLY_TYPE_USB_DCP
;
100 func_ctrl
= otg_io_read(isp
->otg
, ULPI_FUNC_CTRL
);
101 otg_ctrl
= otg_io_read(isp
->otg
, ULPI_OTG_CTRL
);
103 /* disable pulldowns */
104 reg
= ULPI_OTG_CTRL_DM_PULLDOWN
| ULPI_OTG_CTRL_DP_PULLDOWN
;
105 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_OTG_CTRL
), reg
);
108 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_FUNC_CTRL
),
109 ULPI_FUNC_CTRL_XCVRSEL_MASK
);
110 otg_io_write(isp
->otg
, ULPI_SET(ULPI_FUNC_CTRL
),
111 ULPI_FUNC_CTRL_FULL_SPEED
);
113 /* Enable strong pull-up on DP (1.5K) and reset */
114 reg
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
115 otg_io_write(isp
->otg
, ULPI_SET(ULPI_FUNC_CTRL
), reg
);
116 usleep_range(1000, 2000);
118 reg
= otg_io_read(isp
->otg
, ULPI_DEBUG
);
120 type
= POWER_SUPPLY_TYPE_USB_CDP
;
122 /* recover original state */
123 otg_io_write(isp
->otg
, ULPI_FUNC_CTRL
, func_ctrl
);
124 otg_io_write(isp
->otg
, ULPI_OTG_CTRL
, otg_ctrl
);
130 * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
131 * is actually a dedicated charger, the following steps need to be taken.
133 static inline int isp1704_charger_verify(struct isp1704_charger
*isp
)
138 /* Reset the transceiver */
139 r
= otg_io_read(isp
->otg
, ULPI_FUNC_CTRL
);
140 r
|= ULPI_FUNC_CTRL_RESET
;
141 otg_io_write(isp
->otg
, ULPI_FUNC_CTRL
, r
);
142 usleep_range(1000, 2000);
144 /* Set normal mode */
145 r
&= ~(ULPI_FUNC_CTRL_RESET
| ULPI_FUNC_CTRL_OPMODE_MASK
);
146 otg_io_write(isp
->otg
, ULPI_FUNC_CTRL
, r
);
148 /* Clear the DP and DM pull-down bits */
149 r
= ULPI_OTG_CTRL_DP_PULLDOWN
| ULPI_OTG_CTRL_DM_PULLDOWN
;
150 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_OTG_CTRL
), r
);
152 /* Enable strong pull-up on DP (1.5K) and reset */
153 r
= ULPI_FUNC_CTRL_TERMSELECT
| ULPI_FUNC_CTRL_RESET
;
154 otg_io_write(isp
->otg
, ULPI_SET(ULPI_FUNC_CTRL
), r
);
155 usleep_range(1000, 2000);
157 /* Read the line state */
158 if (!otg_io_read(isp
->otg
, ULPI_DEBUG
)) {
159 /* Disable strong pull-up on DP (1.5K) */
160 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_FUNC_CTRL
),
161 ULPI_FUNC_CTRL_TERMSELECT
);
165 /* Is it a charger or PS/2 connection */
167 /* Enable weak pull-up resistor on DP */
168 otg_io_write(isp
->otg
, ULPI_SET(ISP1704_PWR_CTRL
),
169 ISP1704_PWR_CTRL_DP_WKPU_EN
);
171 /* Disable strong pull-up on DP (1.5K) */
172 otg_io_write(isp
->otg
, ULPI_CLR(ULPI_FUNC_CTRL
),
173 ULPI_FUNC_CTRL_TERMSELECT
);
175 /* Enable weak pull-down resistor on DM */
176 otg_io_write(isp
->otg
, ULPI_SET(ULPI_OTG_CTRL
),
177 ULPI_OTG_CTRL_DM_PULLDOWN
);
179 /* It's a charger if the line states are clear */
180 if (!(otg_io_read(isp
->otg
, ULPI_DEBUG
)))
183 /* Disable weak pull-up resistor on DP */
184 otg_io_write(isp
->otg
, ULPI_CLR(ISP1704_PWR_CTRL
),
185 ISP1704_PWR_CTRL_DP_WKPU_EN
);
190 static inline int isp1704_charger_detect(struct isp1704_charger
*isp
)
192 unsigned long timeout
;
196 pwr_ctrl
= otg_io_read(isp
->otg
, ISP1704_PWR_CTRL
);
198 /* set SW control bit in PWR_CTRL register */
199 otg_io_write(isp
->otg
, ISP1704_PWR_CTRL
,
200 ISP1704_PWR_CTRL_SWCTRL
);
202 /* enable manual charger detection */
203 otg_io_write(isp
->otg
, ULPI_SET(ISP1704_PWR_CTRL
),
204 ISP1704_PWR_CTRL_SWCTRL
205 | ISP1704_PWR_CTRL_DPVSRC_EN
);
206 usleep_range(1000, 2000);
208 timeout
= jiffies
+ msecs_to_jiffies(300);
210 /* Check if there is a charger */
211 if (otg_io_read(isp
->otg
, ISP1704_PWR_CTRL
)
212 & ISP1704_PWR_CTRL_VDAT_DET
) {
213 ret
= isp1704_charger_verify(isp
);
216 } while (!time_after(jiffies
, timeout
) && isp
->online
);
218 /* recover original state */
219 otg_io_write(isp
->otg
, ISP1704_PWR_CTRL
, pwr_ctrl
);
224 static void isp1704_charger_work(struct work_struct
*data
)
229 struct isp1704_charger
*isp
=
230 container_of(data
, struct isp1704_charger
, work
);
231 static DEFINE_MUTEX(lock
);
234 power
= isp
->max_power
;
238 if (event
!= USB_EVENT_NONE
)
239 isp1704_charger_set_power(isp
, 1);
246 detect
= isp1704_charger_detect(isp
);
249 isp
->present
= detect
;
250 isp
->psy
.type
= isp1704_charger_type(isp
);
253 switch (isp
->psy
.type
) {
254 case POWER_SUPPLY_TYPE_USB_DCP
:
255 isp
->current_max
= 1800;
257 case POWER_SUPPLY_TYPE_USB_CDP
:
259 * Only 500mA here or high speed chirp
260 * handshaking may break
262 isp
->current_max
= 500;
264 case POWER_SUPPLY_TYPE_USB
:
266 /* enable data pullups */
267 if (isp
->otg
->gadget
)
268 usb_gadget_connect(isp
->otg
->gadget
);
273 isp
->current_max
= 0;
275 isp
->current_max
= 0;
276 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
279 * Disable data pullups. We need to prevent the controller from
282 * FIXME: This is here to allow charger detection with Host/HUB
283 * chargers. The pullups may be enabled elsewhere, so this can
284 * not be the final solution.
286 if (isp
->otg
->gadget
)
287 usb_gadget_disconnect(isp
->otg
->gadget
);
289 isp1704_charger_set_power(isp
, 0);
291 case USB_EVENT_ENUMERATED
:
293 isp
->current_max
= 1800;
295 isp
->current_max
= power
;
301 power_supply_changed(&isp
->psy
);
306 static int isp1704_notifier_call(struct notifier_block
*nb
,
307 unsigned long event
, void *power
)
309 struct isp1704_charger
*isp
=
310 container_of(nb
, struct isp1704_charger
, nb
);
315 isp
->max_power
= *((unsigned *)power
);
317 schedule_work(&isp
->work
);
322 static int isp1704_charger_get_property(struct power_supply
*psy
,
323 enum power_supply_property psp
,
324 union power_supply_propval
*val
)
326 struct isp1704_charger
*isp
=
327 container_of(psy
, struct isp1704_charger
, psy
);
330 case POWER_SUPPLY_PROP_PRESENT
:
331 val
->intval
= isp
->present
;
333 case POWER_SUPPLY_PROP_ONLINE
:
334 val
->intval
= isp
->online
;
336 case POWER_SUPPLY_PROP_CURRENT_MAX
:
337 val
->intval
= isp
->current_max
;
339 case POWER_SUPPLY_PROP_MODEL_NAME
:
340 val
->strval
= isp
->model
;
342 case POWER_SUPPLY_PROP_MANUFACTURER
:
351 static enum power_supply_property power_props
[] = {
352 POWER_SUPPLY_PROP_PRESENT
,
353 POWER_SUPPLY_PROP_ONLINE
,
354 POWER_SUPPLY_PROP_CURRENT_MAX
,
355 POWER_SUPPLY_PROP_MODEL_NAME
,
356 POWER_SUPPLY_PROP_MANUFACTURER
,
359 static inline int isp1704_test_ulpi(struct isp1704_charger
*isp
)
366 /* Test ULPI interface */
367 ret
= otg_io_write(isp
->otg
, ULPI_SCRATCH
, 0xaa);
371 ret
= otg_io_read(isp
->otg
, ULPI_SCRATCH
);
378 /* Verify the product and vendor id matches */
379 vendor
= otg_io_read(isp
->otg
, ULPI_VENDOR_ID_LOW
);
380 vendor
|= otg_io_read(isp
->otg
, ULPI_VENDOR_ID_HIGH
) << 8;
381 if (vendor
!= NXP_VENDOR_ID
)
384 product
= otg_io_read(isp
->otg
, ULPI_PRODUCT_ID_LOW
);
385 product
|= otg_io_read(isp
->otg
, ULPI_PRODUCT_ID_HIGH
) << 8;
387 for (i
= 0; i
< ARRAY_SIZE(isp170x_id
); i
++) {
388 if (product
== isp170x_id
[i
]) {
389 sprintf(isp
->model
, "isp%x", product
);
394 dev_err(isp
->dev
, "product id %x not matching known ids", product
);
399 static int __devinit
isp1704_charger_probe(struct platform_device
*pdev
)
401 struct isp1704_charger
*isp
;
404 isp
= kzalloc(sizeof *isp
, GFP_KERNEL
);
408 isp
->otg
= otg_get_transceiver();
412 isp
->dev
= &pdev
->dev
;
413 platform_set_drvdata(pdev
, isp
);
415 isp1704_charger_set_power(isp
, 1);
417 ret
= isp1704_test_ulpi(isp
);
421 isp
->psy
.name
= "isp1704";
422 isp
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
423 isp
->psy
.properties
= power_props
;
424 isp
->psy
.num_properties
= ARRAY_SIZE(power_props
);
425 isp
->psy
.get_property
= isp1704_charger_get_property
;
427 ret
= power_supply_register(isp
->dev
, &isp
->psy
);
432 * REVISIT: using work in order to allow the otg notifications to be
433 * made atomically in the future.
435 INIT_WORK(&isp
->work
, isp1704_charger_work
);
437 isp
->nb
.notifier_call
= isp1704_notifier_call
;
439 ret
= otg_register_notifier(isp
->otg
, &isp
->nb
);
443 dev_info(isp
->dev
, "registered with product id %s\n", isp
->model
);
446 * Taking over the D+ pullup.
448 * FIXME: The device will be disconnected if it was already
449 * enumerated. The charger driver should be always loaded before any
452 if (isp
->otg
->gadget
)
453 usb_gadget_disconnect(isp
->otg
->gadget
);
455 /* Detect charger if VBUS is valid (the cable was already plugged). */
456 ret
= otg_io_read(isp
->otg
, ULPI_USB_INT_STS
);
457 isp1704_charger_set_power(isp
, 0);
458 if ((ret
& ULPI_INT_VBUS_VALID
) && !isp
->otg
->default_a
) {
459 isp
->event
= USB_EVENT_VBUS
;
460 schedule_work(&isp
->work
);
465 power_supply_unregister(&isp
->psy
);
467 otg_put_transceiver(isp
->otg
);
471 dev_err(&pdev
->dev
, "failed to register isp1704 with error %d\n", ret
);
476 static int __devexit
isp1704_charger_remove(struct platform_device
*pdev
)
478 struct isp1704_charger
*isp
= platform_get_drvdata(pdev
);
480 otg_unregister_notifier(isp
->otg
, &isp
->nb
);
481 power_supply_unregister(&isp
->psy
);
482 otg_put_transceiver(isp
->otg
);
483 isp1704_charger_set_power(isp
, 0);
489 static struct platform_driver isp1704_charger_driver
= {
491 .name
= "isp1704_charger",
493 .probe
= isp1704_charger_probe
,
494 .remove
= __devexit_p(isp1704_charger_remove
),
497 module_platform_driver(isp1704_charger_driver
);
499 MODULE_ALIAS("platform:isp1704_charger");
500 MODULE_AUTHOR("Nokia Corporation");
501 MODULE_DESCRIPTION("ISP170x USB Charger driver");
502 MODULE_LICENSE("GPL");