Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / power / isp1704_charger.c
blob4cd6899b961e780e0623e81951ecc12e1fcd3557
1 /*
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>
32 #include <linux/of.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[] = {
55 0x1704,
56 0x1707,
59 struct isp1704_charger {
60 struct device *dev;
61 struct power_supply *psy;
62 struct power_supply_desc psy_desc;
63 struct usb_phy *phy;
64 struct notifier_block nb;
65 struct work_struct work;
67 /* properties */
68 char model[8];
69 unsigned present:1;
70 unsigned online:1;
71 unsigned current_max;
74 static inline int isp1704_read(struct isp1704_charger *isp, u32 reg)
76 return usb_phy_io_read(isp->phy, reg);
79 static inline int isp1704_write(struct isp1704_charger *isp, u32 reg, u32 val)
81 return usb_phy_io_write(isp->phy, val, reg);
85 * Disable/enable the power from the isp1704 if a function for it
86 * has been provided with platform data.
88 static void isp1704_charger_set_power(struct isp1704_charger *isp, bool on)
90 struct isp1704_charger_data *board = isp->dev->platform_data;
92 if (board && board->set_power)
93 board->set_power(on);
94 else if (board)
95 gpio_set_value(board->enable_gpio, on);
99 * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
100 * chargers).
102 * REVISIT: The method is defined in Battery Charging Specification and is
103 * applicable to any ULPI transceiver. Nothing isp170x specific here.
105 static inline int isp1704_charger_type(struct isp1704_charger *isp)
107 u8 reg;
108 u8 func_ctrl;
109 u8 otg_ctrl;
110 int type = POWER_SUPPLY_TYPE_USB_DCP;
112 func_ctrl = isp1704_read(isp, ULPI_FUNC_CTRL);
113 otg_ctrl = isp1704_read(isp, ULPI_OTG_CTRL);
115 /* disable pulldowns */
116 reg = ULPI_OTG_CTRL_DM_PULLDOWN | ULPI_OTG_CTRL_DP_PULLDOWN;
117 isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), reg);
119 /* full speed */
120 isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
121 ULPI_FUNC_CTRL_XCVRSEL_MASK);
122 isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL),
123 ULPI_FUNC_CTRL_FULL_SPEED);
125 /* Enable strong pull-up on DP (1.5K) and reset */
126 reg = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
127 isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), reg);
128 usleep_range(1000, 2000);
130 reg = isp1704_read(isp, ULPI_DEBUG);
131 if ((reg & 3) != 3)
132 type = POWER_SUPPLY_TYPE_USB_CDP;
134 /* recover original state */
135 isp1704_write(isp, ULPI_FUNC_CTRL, func_ctrl);
136 isp1704_write(isp, ULPI_OTG_CTRL, otg_ctrl);
138 return type;
142 * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
143 * is actually a dedicated charger, the following steps need to be taken.
145 static inline int isp1704_charger_verify(struct isp1704_charger *isp)
147 int ret = 0;
148 u8 r;
150 /* Reset the transceiver */
151 r = isp1704_read(isp, ULPI_FUNC_CTRL);
152 r |= ULPI_FUNC_CTRL_RESET;
153 isp1704_write(isp, ULPI_FUNC_CTRL, r);
154 usleep_range(1000, 2000);
156 /* Set normal mode */
157 r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK);
158 isp1704_write(isp, ULPI_FUNC_CTRL, r);
160 /* Clear the DP and DM pull-down bits */
161 r = ULPI_OTG_CTRL_DP_PULLDOWN | ULPI_OTG_CTRL_DM_PULLDOWN;
162 isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), r);
164 /* Enable strong pull-up on DP (1.5K) and reset */
165 r = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
166 isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), r);
167 usleep_range(1000, 2000);
169 /* Read the line state */
170 if (!isp1704_read(isp, ULPI_DEBUG)) {
171 /* Disable strong pull-up on DP (1.5K) */
172 isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
173 ULPI_FUNC_CTRL_TERMSELECT);
174 return 1;
177 /* Is it a charger or PS/2 connection */
179 /* Enable weak pull-up resistor on DP */
180 isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
181 ISP1704_PWR_CTRL_DP_WKPU_EN);
183 /* Disable strong pull-up on DP (1.5K) */
184 isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
185 ULPI_FUNC_CTRL_TERMSELECT);
187 /* Enable weak pull-down resistor on DM */
188 isp1704_write(isp, ULPI_SET(ULPI_OTG_CTRL),
189 ULPI_OTG_CTRL_DM_PULLDOWN);
191 /* It's a charger if the line states are clear */
192 if (!(isp1704_read(isp, ULPI_DEBUG)))
193 ret = 1;
195 /* Disable weak pull-up resistor on DP */
196 isp1704_write(isp, ULPI_CLR(ISP1704_PWR_CTRL),
197 ISP1704_PWR_CTRL_DP_WKPU_EN);
199 return ret;
202 static inline int isp1704_charger_detect(struct isp1704_charger *isp)
204 unsigned long timeout;
205 u8 pwr_ctrl;
206 int ret = 0;
208 pwr_ctrl = isp1704_read(isp, ISP1704_PWR_CTRL);
210 /* set SW control bit in PWR_CTRL register */
211 isp1704_write(isp, ISP1704_PWR_CTRL,
212 ISP1704_PWR_CTRL_SWCTRL);
214 /* enable manual charger detection */
215 isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
216 ISP1704_PWR_CTRL_SWCTRL
217 | ISP1704_PWR_CTRL_DPVSRC_EN);
218 usleep_range(1000, 2000);
220 timeout = jiffies + msecs_to_jiffies(300);
221 do {
222 /* Check if there is a charger */
223 if (isp1704_read(isp, ISP1704_PWR_CTRL)
224 & ISP1704_PWR_CTRL_VDAT_DET) {
225 ret = isp1704_charger_verify(isp);
226 break;
228 } while (!time_after(jiffies, timeout) && isp->online);
230 /* recover original state */
231 isp1704_write(isp, ISP1704_PWR_CTRL, pwr_ctrl);
233 return ret;
236 static inline int isp1704_charger_detect_dcp(struct isp1704_charger *isp)
238 if (isp1704_charger_detect(isp) &&
239 isp1704_charger_type(isp) == POWER_SUPPLY_TYPE_USB_DCP)
240 return true;
241 else
242 return false;
245 static void isp1704_charger_work(struct work_struct *data)
247 struct isp1704_charger *isp =
248 container_of(data, struct isp1704_charger, work);
249 static DEFINE_MUTEX(lock);
251 mutex_lock(&lock);
253 switch (isp->phy->last_event) {
254 case USB_EVENT_VBUS:
255 /* do not call wall charger detection more times */
256 if (!isp->present) {
257 isp->online = true;
258 isp->present = 1;
259 isp1704_charger_set_power(isp, 1);
261 /* detect wall charger */
262 if (isp1704_charger_detect_dcp(isp)) {
263 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB_DCP;
264 isp->current_max = 1800;
265 } else {
266 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
267 isp->current_max = 500;
270 /* enable data pullups */
271 if (isp->phy->otg->gadget)
272 usb_gadget_connect(isp->phy->otg->gadget);
275 if (isp->psy_desc.type != POWER_SUPPLY_TYPE_USB_DCP) {
277 * Only 500mA here or high speed chirp
278 * handshaking may break
280 if (isp->current_max > 500)
281 isp->current_max = 500;
283 if (isp->current_max > 100)
284 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB_CDP;
286 break;
287 case USB_EVENT_NONE:
288 isp->online = false;
289 isp->present = 0;
290 isp->current_max = 0;
291 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
294 * Disable data pullups. We need to prevent the controller from
295 * enumerating.
297 * FIXME: This is here to allow charger detection with Host/HUB
298 * chargers. The pullups may be enabled elsewhere, so this can
299 * not be the final solution.
301 if (isp->phy->otg->gadget)
302 usb_gadget_disconnect(isp->phy->otg->gadget);
304 isp1704_charger_set_power(isp, 0);
305 break;
306 default:
307 goto out;
310 power_supply_changed(isp->psy);
311 out:
312 mutex_unlock(&lock);
315 static int isp1704_notifier_call(struct notifier_block *nb,
316 unsigned long val, void *v)
318 struct isp1704_charger *isp =
319 container_of(nb, struct isp1704_charger, nb);
321 schedule_work(&isp->work);
323 return NOTIFY_OK;
326 static int isp1704_charger_get_property(struct power_supply *psy,
327 enum power_supply_property psp,
328 union power_supply_propval *val)
330 struct isp1704_charger *isp = power_supply_get_drvdata(psy);
332 switch (psp) {
333 case POWER_SUPPLY_PROP_PRESENT:
334 val->intval = isp->present;
335 break;
336 case POWER_SUPPLY_PROP_ONLINE:
337 val->intval = isp->online;
338 break;
339 case POWER_SUPPLY_PROP_CURRENT_MAX:
340 val->intval = isp->current_max;
341 break;
342 case POWER_SUPPLY_PROP_MODEL_NAME:
343 val->strval = isp->model;
344 break;
345 case POWER_SUPPLY_PROP_MANUFACTURER:
346 val->strval = "NXP";
347 break;
348 default:
349 return -EINVAL;
351 return 0;
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)
364 int vendor;
365 int product;
366 int i;
367 int ret = -ENODEV;
369 /* Test ULPI interface */
370 ret = isp1704_write(isp, ULPI_SCRATCH, 0xaa);
371 if (ret < 0)
372 return ret;
374 ret = isp1704_read(isp, ULPI_SCRATCH);
375 if (ret < 0)
376 return ret;
378 if (ret != 0xaa)
379 return -ENODEV;
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)
385 return -ENODEV;
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);
393 return product;
397 dev_err(isp->dev, "product id %x not matching known ids", product);
399 return -ENODEV;
402 static int isp1704_charger_probe(struct platform_device *pdev)
404 struct isp1704_charger *isp;
405 int ret = -ENODEV;
406 struct power_supply_config psy_cfg = {};
408 struct isp1704_charger_data *pdata = dev_get_platdata(&pdev->dev);
409 struct device_node *np = pdev->dev.of_node;
411 if (np) {
412 int gpio = of_get_named_gpio(np, "nxp,enable-gpio", 0);
414 if (gpio < 0) {
415 dev_err(&pdev->dev, "missing DT GPIO nxp,enable-gpio\n");
416 return gpio;
419 pdata = devm_kzalloc(&pdev->dev,
420 sizeof(struct isp1704_charger_data), GFP_KERNEL);
421 pdata->enable_gpio = gpio;
423 dev_info(&pdev->dev, "init gpio %d\n", pdata->enable_gpio);
425 ret = devm_gpio_request_one(&pdev->dev, pdata->enable_gpio,
426 GPIOF_OUT_INIT_HIGH, "isp1704_reset");
427 if (ret) {
428 dev_err(&pdev->dev, "gpio request failed\n");
429 goto fail0;
433 if (!pdata) {
434 dev_err(&pdev->dev, "missing platform data!\n");
435 return -ENODEV;
439 isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
440 if (!isp)
441 return -ENOMEM;
443 if (np)
444 isp->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
445 else
446 isp->phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
448 if (IS_ERR(isp->phy)) {
449 ret = PTR_ERR(isp->phy);
450 dev_err(&pdev->dev, "usb_get_phy failed\n");
451 goto fail0;
454 isp->dev = &pdev->dev;
455 platform_set_drvdata(pdev, isp);
457 isp1704_charger_set_power(isp, 1);
459 ret = isp1704_test_ulpi(isp);
460 if (ret < 0) {
461 dev_err(&pdev->dev, "isp1704_test_ulpi failed\n");
462 goto fail1;
465 isp->psy_desc.name = "isp1704";
466 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
467 isp->psy_desc.properties = power_props;
468 isp->psy_desc.num_properties = ARRAY_SIZE(power_props);
469 isp->psy_desc.get_property = isp1704_charger_get_property;
471 psy_cfg.drv_data = isp;
473 isp->psy = power_supply_register(isp->dev, &isp->psy_desc, &psy_cfg);
474 if (IS_ERR(isp->psy)) {
475 ret = PTR_ERR(isp->psy);
476 dev_err(&pdev->dev, "power_supply_register failed\n");
477 goto fail1;
481 * REVISIT: using work in order to allow the usb notifications to be
482 * made atomically in the future.
484 INIT_WORK(&isp->work, isp1704_charger_work);
486 isp->nb.notifier_call = isp1704_notifier_call;
488 ret = usb_register_notifier(isp->phy, &isp->nb);
489 if (ret) {
490 dev_err(&pdev->dev, "usb_register_notifier failed\n");
491 goto fail2;
494 dev_info(isp->dev, "registered with product id %s\n", isp->model);
497 * Taking over the D+ pullup.
499 * FIXME: The device will be disconnected if it was already
500 * enumerated. The charger driver should be always loaded before any
501 * gadget is loaded.
503 if (isp->phy->otg->gadget)
504 usb_gadget_disconnect(isp->phy->otg->gadget);
506 if (isp->phy->last_event == USB_EVENT_NONE)
507 isp1704_charger_set_power(isp, 0);
509 /* Detect charger if VBUS is valid (the cable was already plugged). */
510 if (isp->phy->last_event == USB_EVENT_VBUS &&
511 !isp->phy->otg->default_a)
512 schedule_work(&isp->work);
514 return 0;
515 fail2:
516 power_supply_unregister(isp->psy);
517 fail1:
518 isp1704_charger_set_power(isp, 0);
519 fail0:
520 dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret);
522 return ret;
525 static int isp1704_charger_remove(struct platform_device *pdev)
527 struct isp1704_charger *isp = platform_get_drvdata(pdev);
529 usb_unregister_notifier(isp->phy, &isp->nb);
530 power_supply_unregister(isp->psy);
531 isp1704_charger_set_power(isp, 0);
533 return 0;
536 #ifdef CONFIG_OF
537 static const struct of_device_id omap_isp1704_of_match[] = {
538 { .compatible = "nxp,isp1704", },
539 { .compatible = "nxp,isp1707", },
542 MODULE_DEVICE_TABLE(of, omap_isp1704_of_match);
543 #endif
545 static struct platform_driver isp1704_charger_driver = {
546 .driver = {
547 .name = "isp1704_charger",
548 .of_match_table = of_match_ptr(omap_isp1704_of_match),
550 .probe = isp1704_charger_probe,
551 .remove = isp1704_charger_remove,
554 module_platform_driver(isp1704_charger_driver);
556 MODULE_ALIAS("platform:isp1704_charger");
557 MODULE_AUTHOR("Nokia Corporation");
558 MODULE_DESCRIPTION("ISP170x USB Charger driver");
559 MODULE_LICENSE("GPL");