md/raid5: remove setting of ->queue_lock
[linux-2.6/x86.git] / drivers / power / isp1704_charger.c
blob2ad9b14a5ce37c4ec15f8cb8b319c7845e982c4f
1 /*
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>
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)
48 #define NXP_VENDOR_ID 0x04cc
50 static u16 isp170x_id[] = {
51 0x1704,
52 0x1707,
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;
62 /* properties */
63 char model[8];
64 unsigned present:1;
65 unsigned online:1;
66 unsigned current_max;
68 /* temp storage variables */
69 unsigned long event;
70 unsigned max_power;
74 * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
75 * chargers).
77 * REVISIT: The method is defined in Battery Charging Specification and is
78 * applicable to any ULPI transceiver. Nothing isp170x specific here.
80 static inline int isp1704_charger_type(struct isp1704_charger *isp)
82 u8 reg;
83 u8 func_ctrl;
84 u8 otg_ctrl;
85 int type = POWER_SUPPLY_TYPE_USB_DCP;
87 func_ctrl = otg_io_read(isp->otg, ULPI_FUNC_CTRL);
88 otg_ctrl = otg_io_read(isp->otg, ULPI_OTG_CTRL);
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);
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);
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);
105 reg = otg_io_read(isp->otg, ULPI_DEBUG);
106 if ((reg & 3) != 3)
107 type = POWER_SUPPLY_TYPE_USB_CDP;
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);
113 return type;
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.
120 static inline int isp1704_charger_verify(struct isp1704_charger *isp)
122 int ret = 0;
123 u8 r;
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);
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);
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);
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);
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;
152 /* Is it a charger or PS/2 connection */
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);
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);
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);
166 /* It's a charger if the line states are clear */
167 if (!(otg_io_read(isp->otg, ULPI_DEBUG)))
168 ret = 1;
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);
174 return ret;
177 static inline int isp1704_charger_detect(struct isp1704_charger *isp)
179 unsigned long timeout;
180 u8 pwr_ctrl;
181 int ret = 0;
183 pwr_ctrl = otg_io_read(isp->otg, ISP1704_PWR_CTRL);
185 /* set SW control bit in PWR_CTRL register */
186 otg_io_write(isp->otg, ISP1704_PWR_CTRL,
187 ISP1704_PWR_CTRL_SWCTRL);
189 /* enable manual charger detection */
190 otg_io_write(isp->otg, ULPI_SET(ISP1704_PWR_CTRL),
191 ISP1704_PWR_CTRL_SWCTRL
192 | ISP1704_PWR_CTRL_DPVSRC_EN);
193 usleep_range(1000, 2000);
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;
203 } while (!time_after(jiffies, timeout) && isp->online);
205 /* recover original state */
206 otg_io_write(isp->otg, ISP1704_PWR_CTRL, pwr_ctrl);
208 return ret;
211 static void isp1704_charger_work(struct work_struct *data)
213 int detect;
214 unsigned long event;
215 unsigned power;
216 struct isp1704_charger *isp =
217 container_of(data, struct isp1704_charger, work);
218 static DEFINE_MUTEX(lock);
220 event = isp->event;
221 power = isp->max_power;
223 mutex_lock(&lock);
225 switch (event) {
226 case USB_EVENT_VBUS:
227 isp->online = true;
229 /* detect charger */
230 detect = isp1704_charger_detect(isp);
232 if (detect) {
233 isp->present = detect;
234 isp->psy.type = isp1704_charger_type(isp);
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:
243 * Only 500mA here or high speed chirp
244 * handshaking may break
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);
254 break;
255 case USB_EVENT_NONE:
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;
263 * Disable data pullups. We need to prevent the controller from
264 * enumerating.
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.
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;
278 break;
279 default:
280 goto out;
283 power_supply_changed(&isp->psy);
284 out:
285 mutex_unlock(&lock);
288 static int isp1704_notifier_call(struct notifier_block *nb,
289 unsigned long event, void *power)
291 struct isp1704_charger *isp =
292 container_of(nb, struct isp1704_charger, nb);
294 isp->event = event;
296 if (power)
297 isp->max_power = *((unsigned *)power);
299 schedule_work(&isp->work);
301 return NOTIFY_OK;
304 static int isp1704_charger_get_property(struct power_supply *psy,
305 enum power_supply_property psp,
306 union power_supply_propval *val)
308 struct isp1704_charger *isp =
309 container_of(psy, struct isp1704_charger, psy);
311 switch (psp) {
312 case POWER_SUPPLY_PROP_PRESENT:
313 val->intval = isp->present;
314 break;
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;
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;
330 return 0;
333 static enum power_supply_property power_props[] = {
334 POWER_SUPPLY_PROP_PRESENT,
335 POWER_SUPPLY_PROP_ONLINE,
336 POWER_SUPPLY_PROP_CURRENT_MAX,
337 POWER_SUPPLY_PROP_MODEL_NAME,
338 POWER_SUPPLY_PROP_MANUFACTURER,
341 static inline int isp1704_test_ulpi(struct isp1704_charger *isp)
343 int vendor;
344 int product;
345 int i;
346 int ret = -ENODEV;
348 /* Test ULPI interface */
349 ret = otg_io_write(isp->otg, ULPI_SCRATCH, 0xaa);
350 if (ret < 0)
351 return ret;
353 ret = otg_io_read(isp->otg, ULPI_SCRATCH);
354 if (ret < 0)
355 return ret;
357 if (ret != 0xaa)
358 return -ENODEV;
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;
366 product = otg_io_read(isp->otg, ULPI_PRODUCT_ID_LOW);
367 product |= otg_io_read(isp->otg, ULPI_PRODUCT_ID_HIGH) << 8;
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;
376 dev_err(isp->dev, "product id %x not matching known ids", product);
378 return -ENODEV;
381 static int __devinit isp1704_charger_probe(struct platform_device *pdev)
383 struct isp1704_charger *isp;
384 int ret = -ENODEV;
386 isp = kzalloc(sizeof *isp, GFP_KERNEL);
387 if (!isp)
388 return -ENOMEM;
390 isp->otg = otg_get_transceiver();
391 if (!isp->otg)
392 goto fail0;
394 isp->dev = &pdev->dev;
395 platform_set_drvdata(pdev, isp);
397 ret = isp1704_test_ulpi(isp);
398 if (ret < 0)
399 goto fail1;
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;
407 ret = power_supply_register(isp->dev, &isp->psy);
408 if (ret)
409 goto fail1;
412 * REVISIT: using work in order to allow the otg notifications to be
413 * made atomically in the future.
415 INIT_WORK(&isp->work, isp1704_charger_work);
417 isp->nb.notifier_call = isp1704_notifier_call;
419 ret = otg_register_notifier(isp->otg, &isp->nb);
420 if (ret)
421 goto fail2;
423 dev_info(isp->dev, "registered with product id %s\n", isp->model);
426 * Taking over the D+ pullup.
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.
432 if (isp->otg->gadget)
433 usb_gadget_disconnect(isp->otg->gadget);
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);
442 return 0;
443 fail2:
444 power_supply_unregister(&isp->psy);
445 fail1:
446 otg_put_transceiver(isp->otg);
447 fail0:
448 kfree(isp);
450 dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret);
452 return ret;
455 static int __devexit isp1704_charger_remove(struct platform_device *pdev)
457 struct isp1704_charger *isp = platform_get_drvdata(pdev);
459 otg_unregister_notifier(isp->otg, &isp->nb);
460 power_supply_unregister(&isp->psy);
461 otg_put_transceiver(isp->otg);
462 kfree(isp);
464 return 0;
467 static struct platform_driver isp1704_charger_driver = {
468 .driver = {
469 .name = "isp1704_charger",
471 .probe = isp1704_charger_probe,
472 .remove = __devexit_p(isp1704_charger_remove),
475 static int __init isp1704_charger_init(void)
477 return platform_driver_register(&isp1704_charger_driver);
479 module_init(isp1704_charger_init);
481 static void __exit isp1704_charger_exit(void)
483 platform_driver_unregister(&isp1704_charger_driver);
485 module_exit(isp1704_charger_exit);
487 MODULE_ALIAS("platform:isp1704_charger");
488 MODULE_AUTHOR("Nokia Corporation");
489 MODULE_DESCRIPTION("ISP170x USB Charger driver");
490 MODULE_LICENSE("GPL");