Commit | Line | Data |
---|---|---|
ec46475f HK |
1 | /* |
2 | * ISP1704 USB Charger Detection driver | |
3 | * | |
4 | * Copyright (C) 2010 Nokia Corporation | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
20 | ||
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> | |
31 | ||
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 | ||
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) | |
47 | ||
48 | #define NXP_VENDOR_ID 0x04cc | |
49 | ||
50 | static u16 isp170x_id[] = { | |
51 | 0x1704, | |
52 | 0x1707, | |
53 | }; | |
54 | ||
55 | struct isp1704_charger { | |
56 | struct device *dev; | |
57 | struct power_supply psy; | |
58 | struct otg_transceiver *otg; | |
59 | struct notifier_block nb; | |
60 | struct work_struct work; | |
61 | ||
bac43b20 | 62 | /* properties */ |
746d8fb8 | 63 | char model[8]; |
ec46475f | 64 | unsigned present:1; |
bac43b20 HK |
65 | unsigned online:1; |
66 | unsigned current_max; | |
67 | ||
68 | /* temp storage variables */ | |
69 | unsigned long event; | |
70 | unsigned max_power; | |
ec46475f HK |
71 | }; |
72 | ||
bac43b20 HK |
73 | /* |
74 | * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB | |
75 | * chargers). | |
76 | * | |
77 | * REVISIT: The method is defined in Battery Charging Specification and is | |
78 | * applicable to any ULPI transceiver. Nothing isp170x specific here. | |
79 | */ | |
80 | static inline int isp1704_charger_type(struct isp1704_charger *isp) | |
81 | { | |
82 | u8 reg; | |
83 | u8 func_ctrl; | |
84 | u8 otg_ctrl; | |
85 | int type = POWER_SUPPLY_TYPE_USB_DCP; | |
86 | ||
87 | func_ctrl = otg_io_read(isp->otg, ULPI_FUNC_CTRL); | |
88 | otg_ctrl = otg_io_read(isp->otg, ULPI_OTG_CTRL); | |
89 | ||
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); | |
93 | ||
94 | /* full speed */ | |
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); | |
99 | ||
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); | |
104 | ||
105 | reg = otg_io_read(isp->otg, ULPI_DEBUG); | |
106 | if ((reg & 3) != 3) | |
107 | type = POWER_SUPPLY_TYPE_USB_CDP; | |
108 | ||
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); | |
112 | ||
113 | return type; | |
114 | } | |
115 | ||
ec46475f HK |
116 | /* |
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. | |
119 | */ | |
120 | static inline int isp1704_charger_verify(struct isp1704_charger *isp) | |
121 | { | |
122 | int ret = 0; | |
123 | u8 r; | |
124 | ||
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); | |
130 | ||
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); | |
134 | ||
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); | |
138 | ||
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); | |
143 | ||
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); | |
149 | return 1; | |
150 | } | |
151 | ||
152 | /* Is it a charger or PS/2 connection */ | |
153 | ||
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); | |
157 | ||
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); | |
161 | ||
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); | |
165 | ||
166 | /* It's a charger if the line states are clear */ | |
167 | if (!(otg_io_read(isp->otg, ULPI_DEBUG))) | |
168 | ret = 1; | |
169 | ||
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); | |
173 | ||
174 | return ret; | |
175 | } | |
176 | ||
177 | static inline int isp1704_charger_detect(struct isp1704_charger *isp) | |
178 | { | |
179 | unsigned long timeout; | |
bac43b20 | 180 | u8 pwr_ctrl; |
ec46475f HK |
181 | int ret = 0; |
182 | ||
bac43b20 HK |
183 | pwr_ctrl = otg_io_read(isp->otg, ISP1704_PWR_CTRL); |
184 | ||
ec46475f HK |
185 | /* set SW control bit in PWR_CTRL register */ |
186 | otg_io_write(isp->otg, ISP1704_PWR_CTRL, | |
187 | ISP1704_PWR_CTRL_SWCTRL); | |
188 | ||
189 | /* enable manual charger detection */ | |
bac43b20 HK |
190 | otg_io_write(isp->otg, ULPI_SET(ISP1704_PWR_CTRL), |
191 | ISP1704_PWR_CTRL_SWCTRL | |
192 | | ISP1704_PWR_CTRL_DPVSRC_EN); | |
ec46475f HK |
193 | usleep_range(1000, 2000); |
194 | ||
195 | timeout = jiffies + msecs_to_jiffies(300); | |
196 | do { | |
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); | |
201 | break; | |
202 | } | |
bac43b20 HK |
203 | } while (!time_after(jiffies, timeout) && isp->online); |
204 | ||
205 | /* recover original state */ | |
206 | otg_io_write(isp->otg, ISP1704_PWR_CTRL, pwr_ctrl); | |
ec46475f HK |
207 | |
208 | return ret; | |
209 | } | |
210 | ||
211 | static void isp1704_charger_work(struct work_struct *data) | |
212 | { | |
213 | int detect; | |
bac43b20 HK |
214 | unsigned long event; |
215 | unsigned power; | |
ec46475f HK |
216 | struct isp1704_charger *isp = |
217 | container_of(data, struct isp1704_charger, work); | |
bac43b20 | 218 | static DEFINE_MUTEX(lock); |
ec46475f | 219 | |
bac43b20 HK |
220 | event = isp->event; |
221 | power = isp->max_power; | |
ec46475f | 222 | |
bac43b20 | 223 | mutex_lock(&lock); |
ec46475f HK |
224 | |
225 | switch (event) { | |
226 | case USB_EVENT_VBUS: | |
bac43b20 HK |
227 | isp->online = true; |
228 | ||
229 | /* detect charger */ | |
230 | detect = isp1704_charger_detect(isp); | |
231 | ||
232 | if (detect) { | |
233 | isp->present = detect; | |
234 | isp->psy.type = isp1704_charger_type(isp); | |
235 | } | |
236 | ||
237 | switch (isp->psy.type) { | |
238 | case POWER_SUPPLY_TYPE_USB_DCP: | |
239 | isp->current_max = 1800; | |
240 | break; | |
241 | case POWER_SUPPLY_TYPE_USB_CDP: | |
242 | /* | |
243 | * Only 500mA here or high speed chirp | |
244 | * handshaking may break | |
245 | */ | |
246 | isp->current_max = 500; | |
247 | /* FALLTHROUGH */ | |
248 | case POWER_SUPPLY_TYPE_USB: | |
249 | default: | |
250 | /* enable data pullups */ | |
251 | if (isp->otg->gadget) | |
252 | usb_gadget_connect(isp->otg->gadget); | |
253 | } | |
ec46475f HK |
254 | break; |
255 | case USB_EVENT_NONE: | |
bac43b20 HK |
256 | isp->online = false; |
257 | isp->current_max = 0; | |
258 | isp->present = 0; | |
259 | isp->current_max = 0; | |
260 | isp->psy.type = POWER_SUPPLY_TYPE_USB; | |
261 | ||
262 | /* | |
263 | * Disable data pullups. We need to prevent the controller from | |
264 | * enumerating. | |
265 | * | |
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. | |
269 | */ | |
270 | if (isp->otg->gadget) | |
271 | usb_gadget_disconnect(isp->otg->gadget); | |
272 | break; | |
273 | case USB_EVENT_ENUMERATED: | |
274 | if (isp->present) | |
275 | isp->current_max = 1800; | |
276 | else | |
277 | isp->current_max = power; | |
ec46475f HK |
278 | break; |
279 | default: | |
bac43b20 | 280 | goto out; |
ec46475f HK |
281 | } |
282 | ||
bac43b20 HK |
283 | power_supply_changed(&isp->psy); |
284 | out: | |
285 | mutex_unlock(&lock); | |
286 | } | |
287 | ||
288 | static int isp1704_notifier_call(struct notifier_block *nb, | |
289 | unsigned long event, void *power) | |
290 | { | |
291 | struct isp1704_charger *isp = | |
292 | container_of(nb, struct isp1704_charger, nb); | |
293 | ||
294 | isp->event = event; | |
295 | ||
296 | if (power) | |
297 | isp->max_power = *((unsigned *)power); | |
298 | ||
299 | schedule_work(&isp->work); | |
300 | ||
ec46475f HK |
301 | return NOTIFY_OK; |
302 | } | |
303 | ||
304 | static int isp1704_charger_get_property(struct power_supply *psy, | |
305 | enum power_supply_property psp, | |
306 | union power_supply_propval *val) | |
307 | { | |
308 | struct isp1704_charger *isp = | |
309 | container_of(psy, struct isp1704_charger, psy); | |
310 | ||
311 | switch (psp) { | |
312 | case POWER_SUPPLY_PROP_PRESENT: | |
313 | val->intval = isp->present; | |
314 | break; | |
bac43b20 HK |
315 | case POWER_SUPPLY_PROP_ONLINE: |
316 | val->intval = isp->online; | |
317 | break; | |
318 | case POWER_SUPPLY_PROP_CURRENT_MAX: | |
319 | val->intval = isp->current_max; | |
320 | break; | |
ec46475f HK |
321 | case POWER_SUPPLY_PROP_MODEL_NAME: |
322 | val->strval = isp->model; | |
323 | break; | |
324 | case POWER_SUPPLY_PROP_MANUFACTURER: | |
325 | val->strval = "NXP"; | |
326 | break; | |
327 | default: | |
328 | return -EINVAL; | |
329 | } | |
330 | return 0; | |
331 | } | |
332 | ||
333 | static enum power_supply_property power_props[] = { | |
334 | POWER_SUPPLY_PROP_PRESENT, | |
bac43b20 HK |
335 | POWER_SUPPLY_PROP_ONLINE, |
336 | POWER_SUPPLY_PROP_CURRENT_MAX, | |
ec46475f HK |
337 | POWER_SUPPLY_PROP_MODEL_NAME, |
338 | POWER_SUPPLY_PROP_MANUFACTURER, | |
339 | }; | |
340 | ||
341 | static inline int isp1704_test_ulpi(struct isp1704_charger *isp) | |
342 | { | |
343 | int vendor; | |
344 | int product; | |
345 | int i; | |
346 | int ret = -ENODEV; | |
347 | ||
348 | /* Test ULPI interface */ | |
349 | ret = otg_io_write(isp->otg, ULPI_SCRATCH, 0xaa); | |
350 | if (ret < 0) | |
351 | return ret; | |
352 | ||
353 | ret = otg_io_read(isp->otg, ULPI_SCRATCH); | |
354 | if (ret < 0) | |
355 | return ret; | |
356 | ||
357 | if (ret != 0xaa) | |
358 | return -ENODEV; | |
359 | ||
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) | |
364 | return -ENODEV; | |
365 | ||
366 | product = otg_io_read(isp->otg, ULPI_PRODUCT_ID_LOW); | |
367 | product |= otg_io_read(isp->otg, ULPI_PRODUCT_ID_HIGH) << 8; | |
368 | ||
369 | for (i = 0; i < ARRAY_SIZE(isp170x_id); i++) { | |
370 | if (product == isp170x_id[i]) { | |
371 | sprintf(isp->model, "isp%x", product); | |
372 | return product; | |
373 | } | |
374 | } | |
375 | ||
376 | dev_err(isp->dev, "product id %x not matching known ids", product); | |
377 | ||
378 | return -ENODEV; | |
379 | } | |
380 | ||
381 | static int __devinit isp1704_charger_probe(struct platform_device *pdev) | |
382 | { | |
383 | struct isp1704_charger *isp; | |
384 | int ret = -ENODEV; | |
385 | ||
386 | isp = kzalloc(sizeof *isp, GFP_KERNEL); | |
387 | if (!isp) | |
388 | return -ENOMEM; | |
389 | ||
390 | isp->otg = otg_get_transceiver(); | |
391 | if (!isp->otg) | |
392 | goto fail0; | |
393 | ||
a4607d9f HK |
394 | isp->dev = &pdev->dev; |
395 | platform_set_drvdata(pdev, isp); | |
396 | ||
ec46475f HK |
397 | ret = isp1704_test_ulpi(isp); |
398 | if (ret < 0) | |
399 | goto fail1; | |
400 | ||
ec46475f HK |
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; | |
406 | ||
407 | ret = power_supply_register(isp->dev, &isp->psy); | |
408 | if (ret) | |
409 | goto fail1; | |
410 | ||
411 | /* | |
412 | * REVISIT: using work in order to allow the otg notifications to be | |
413 | * made atomically in the future. | |
414 | */ | |
415 | INIT_WORK(&isp->work, isp1704_charger_work); | |
416 | ||
417 | isp->nb.notifier_call = isp1704_notifier_call; | |
418 | ||
419 | ret = otg_register_notifier(isp->otg, &isp->nb); | |
420 | if (ret) | |
421 | goto fail2; | |
422 | ||
423 | dev_info(isp->dev, "registered with product id %s\n", isp->model); | |
424 | ||
e1a85694 HK |
425 | /* |
426 | * Taking over the D+ pullup. | |
427 | * | |
428 | * FIXME: The device will be disconnected if it was already | |
429 | * enumerated. The charger driver should be always loaded before any | |
430 | * gadget is loaded. | |
431 | */ | |
432 | if (isp->otg->gadget) | |
433 | usb_gadget_disconnect(isp->otg->gadget); | |
434 | ||
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); | |
440 | } | |
441 | ||
ec46475f HK |
442 | return 0; |
443 | fail2: | |
444 | power_supply_unregister(&isp->psy); | |
445 | fail1: | |
446 | otg_put_transceiver(isp->otg); | |
447 | fail0: | |
448 | kfree(isp); | |
449 | ||
450 | dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret); | |
451 | ||
452 | return ret; | |
453 | } | |
454 | ||
455 | static int __devexit isp1704_charger_remove(struct platform_device *pdev) | |
456 | { | |
457 | struct isp1704_charger *isp = platform_get_drvdata(pdev); | |
458 | ||
459 | otg_unregister_notifier(isp->otg, &isp->nb); | |
460 | power_supply_unregister(&isp->psy); | |
461 | otg_put_transceiver(isp->otg); | |
462 | kfree(isp); | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | static struct platform_driver isp1704_charger_driver = { | |
468 | .driver = { | |
469 | .name = "isp1704_charger", | |
470 | }, | |
471 | .probe = isp1704_charger_probe, | |
472 | .remove = __devexit_p(isp1704_charger_remove), | |
473 | }; | |
474 | ||
475 | static int __init isp1704_charger_init(void) | |
476 | { | |
477 | return platform_driver_register(&isp1704_charger_driver); | |
478 | } | |
479 | module_init(isp1704_charger_init); | |
480 | ||
481 | static void __exit isp1704_charger_exit(void) | |
482 | { | |
483 | platform_driver_unregister(&isp1704_charger_driver); | |
484 | } | |
485 | module_exit(isp1704_charger_exit); | |
486 | ||
487 | MODULE_ALIAS("platform:isp1704_charger"); | |
488 | MODULE_AUTHOR("Nokia Corporation"); | |
489 | MODULE_DESCRIPTION("ISP170x USB Charger driver"); | |
490 | MODULE_LICENSE("GPL"); |