Merge branch 'for-3.13-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / power / isp1704_charger.c
1 /*
2  * ISP1704 USB Charger Detection driver
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  * Copyright (C) 2012 - 2013 Pali Rohár <pali.rohar@gmail.com>
6  *
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.
11  *
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.
16  *
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
20  */
21
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
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>
38
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)
49
50 #define NXP_VENDOR_ID                   0x04cc
51
52 static u16 isp170x_id[] = {
53         0x1704,
54         0x1707,
55 };
56
57 struct isp1704_charger {
58         struct device           *dev;
59         struct power_supply     psy;
60         struct usb_phy          *phy;
61         struct notifier_block   nb;
62         struct work_struct      work;
63
64         /* properties */
65         char                    model[8];
66         unsigned                present:1;
67         unsigned                online:1;
68         unsigned                current_max;
69 };
70
71 static inline int isp1704_read(struct isp1704_charger *isp, u32 reg)
72 {
73         return usb_phy_io_read(isp->phy, reg);
74 }
75
76 static inline int isp1704_write(struct isp1704_charger *isp, u32 val, u32 reg)
77 {
78         return usb_phy_io_write(isp->phy, val, reg);
79 }
80
81 /*
82  * Disable/enable the power from the isp1704 if a function for it
83  * has been provided with platform data.
84  */
85 static void isp1704_charger_set_power(struct isp1704_charger *isp, bool on)
86 {
87         struct isp1704_charger_data     *board = isp->dev->platform_data;
88
89         if (board && board->set_power)
90                 board->set_power(on);
91 }
92
93 /*
94  * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
95  * chargers).
96  *
97  * REVISIT: The method is defined in Battery Charging Specification and is
98  * applicable to any ULPI transceiver. Nothing isp170x specific here.
99  */
100 static inline int isp1704_charger_type(struct isp1704_charger *isp)
101 {
102         u8 reg;
103         u8 func_ctrl;
104         u8 otg_ctrl;
105         int type = POWER_SUPPLY_TYPE_USB_DCP;
106
107         func_ctrl = isp1704_read(isp, ULPI_FUNC_CTRL);
108         otg_ctrl = isp1704_read(isp, ULPI_OTG_CTRL);
109
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);
113
114         /* full speed */
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);
119
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);
124
125         reg = isp1704_read(isp, ULPI_DEBUG);
126         if ((reg & 3) != 3)
127                 type = POWER_SUPPLY_TYPE_USB_CDP;
128
129         /* recover original state */
130         isp1704_write(isp, ULPI_FUNC_CTRL, func_ctrl);
131         isp1704_write(isp, ULPI_OTG_CTRL, otg_ctrl);
132
133         return type;
134 }
135
136 /*
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.
139  */
140 static inline int isp1704_charger_verify(struct isp1704_charger *isp)
141 {
142         int     ret = 0;
143         u8      r;
144
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);
150
151         /* Set normal mode */
152         r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK);
153         isp1704_write(isp, ULPI_FUNC_CTRL, r);
154
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);
158
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);
163
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);
169                 return 1;
170         }
171
172         /* Is it a charger or PS/2 connection */
173
174         /* Enable weak pull-up resistor on DP */
175         isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
176                         ISP1704_PWR_CTRL_DP_WKPU_EN);
177
178         /* Disable strong pull-up on DP (1.5K) */
179         isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
180                         ULPI_FUNC_CTRL_TERMSELECT);
181
182         /* Enable weak pull-down resistor on DM */
183         isp1704_write(isp, ULPI_SET(ULPI_OTG_CTRL),
184                         ULPI_OTG_CTRL_DM_PULLDOWN);
185
186         /* It's a charger if the line states are clear */
187         if (!(isp1704_read(isp, ULPI_DEBUG)))
188                 ret = 1;
189
190         /* Disable weak pull-up resistor on DP */
191         isp1704_write(isp, ULPI_CLR(ISP1704_PWR_CTRL),
192                         ISP1704_PWR_CTRL_DP_WKPU_EN);
193
194         return ret;
195 }
196
197 static inline int isp1704_charger_detect(struct isp1704_charger *isp)
198 {
199         unsigned long   timeout;
200         u8              pwr_ctrl;
201         int             ret = 0;
202
203         pwr_ctrl = isp1704_read(isp, ISP1704_PWR_CTRL);
204
205         /* set SW control bit in PWR_CTRL register */
206         isp1704_write(isp, ISP1704_PWR_CTRL,
207                         ISP1704_PWR_CTRL_SWCTRL);
208
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);
214
215         timeout = jiffies + msecs_to_jiffies(300);
216         do {
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);
221                         break;
222                 }
223         } while (!time_after(jiffies, timeout) && isp->online);
224
225         /* recover original state */
226         isp1704_write(isp, ISP1704_PWR_CTRL, pwr_ctrl);
227
228         return ret;
229 }
230
231 static inline int isp1704_charger_detect_dcp(struct isp1704_charger *isp)
232 {
233         if (isp1704_charger_detect(isp) &&
234                         isp1704_charger_type(isp) == POWER_SUPPLY_TYPE_USB_DCP)
235                 return true;
236         else
237                 return false;
238 }
239
240 static void isp1704_charger_work(struct work_struct *data)
241 {
242         struct isp1704_charger  *isp =
243                 container_of(data, struct isp1704_charger, work);
244         static DEFINE_MUTEX(lock);
245
246         mutex_lock(&lock);
247
248         switch (isp->phy->last_event) {
249         case USB_EVENT_VBUS:
250                 /* do not call wall charger detection more times */
251                 if (!isp->present) {
252                         isp->online = true;
253                         isp->present = 1;
254                         isp1704_charger_set_power(isp, 1);
255
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;
260                         } else {
261                                 isp->psy.type = POWER_SUPPLY_TYPE_USB;
262                                 isp->current_max = 500;
263                         }
264
265                         /* enable data pullups */
266                         if (isp->phy->otg->gadget)
267                                 usb_gadget_connect(isp->phy->otg->gadget);
268                 }
269
270                 if (isp->psy.type != POWER_SUPPLY_TYPE_USB_DCP) {
271                         /*
272                          * Only 500mA here or high speed chirp
273                          * handshaking may break
274                          */
275                         if (isp->current_max > 500)
276                                 isp->current_max = 500;
277
278                         if (isp->current_max > 100)
279                                 isp->psy.type = POWER_SUPPLY_TYPE_USB_CDP;
280                 }
281                 break;
282         case USB_EVENT_NONE:
283                 isp->online = false;
284                 isp->present = 0;
285                 isp->current_max = 0;
286                 isp->psy.type = POWER_SUPPLY_TYPE_USB;
287
288                 /*
289                  * Disable data pullups. We need to prevent the controller from
290                  * enumerating.
291                  *
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.
295                  */
296                 if (isp->phy->otg->gadget)
297                         usb_gadget_disconnect(isp->phy->otg->gadget);
298
299                 isp1704_charger_set_power(isp, 0);
300                 break;
301         default:
302                 goto out;
303         }
304
305         power_supply_changed(&isp->psy);
306 out:
307         mutex_unlock(&lock);
308 }
309
310 static int isp1704_notifier_call(struct notifier_block *nb,
311                 unsigned long val, void *v)
312 {
313         struct isp1704_charger *isp =
314                 container_of(nb, struct isp1704_charger, nb);
315
316         schedule_work(&isp->work);
317
318         return NOTIFY_OK;
319 }
320
321 static int isp1704_charger_get_property(struct power_supply *psy,
322                                 enum power_supply_property psp,
323                                 union power_supply_propval *val)
324 {
325         struct isp1704_charger *isp =
326                 container_of(psy, struct isp1704_charger, psy);
327
328         switch (psp) {
329         case POWER_SUPPLY_PROP_PRESENT:
330                 val->intval = isp->present;
331                 break;
332         case POWER_SUPPLY_PROP_ONLINE:
333                 val->intval = isp->online;
334                 break;
335         case POWER_SUPPLY_PROP_CURRENT_MAX:
336                 val->intval = isp->current_max;
337                 break;
338         case POWER_SUPPLY_PROP_MODEL_NAME:
339                 val->strval = isp->model;
340                 break;
341         case POWER_SUPPLY_PROP_MANUFACTURER:
342                 val->strval = "NXP";
343                 break;
344         default:
345                 return -EINVAL;
346         }
347         return 0;
348 }
349
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,
356 };
357
358 static inline int isp1704_test_ulpi(struct isp1704_charger *isp)
359 {
360         int vendor;
361         int product;
362         int i;
363         int ret = -ENODEV;
364
365         /* Test ULPI interface */
366         ret = isp1704_write(isp, ULPI_SCRATCH, 0xaa);
367         if (ret < 0)
368                 return ret;
369
370         ret = isp1704_read(isp, ULPI_SCRATCH);
371         if (ret < 0)
372                 return ret;
373
374         if (ret != 0xaa)
375                 return -ENODEV;
376
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)
381                 return -ENODEV;
382
383         product = isp1704_read(isp, ULPI_PRODUCT_ID_LOW);
384         product |= isp1704_read(isp, ULPI_PRODUCT_ID_HIGH) << 8;
385
386         for (i = 0; i < ARRAY_SIZE(isp170x_id); i++) {
387                 if (product == isp170x_id[i]) {
388                         sprintf(isp->model, "isp%x", product);
389                         return product;
390                 }
391         }
392
393         dev_err(isp->dev, "product id %x not matching known ids", product);
394
395         return -ENODEV;
396 }
397
398 static int isp1704_charger_probe(struct platform_device *pdev)
399 {
400         struct isp1704_charger  *isp;
401         int                     ret = -ENODEV;
402
403         isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
404         if (!isp)
405                 return -ENOMEM;
406
407         isp->phy = usb_get_phy(USB_PHY_TYPE_USB2);
408         if (IS_ERR_OR_NULL(isp->phy))
409                 goto fail0;
410
411         isp->dev = &pdev->dev;
412         platform_set_drvdata(pdev, isp);
413
414         isp1704_charger_set_power(isp, 1);
415
416         ret = isp1704_test_ulpi(isp);
417         if (ret < 0)
418                 goto fail1;
419
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;
425
426         ret = power_supply_register(isp->dev, &isp->psy);
427         if (ret)
428                 goto fail1;
429
430         /*
431          * REVISIT: using work in order to allow the usb notifications to be
432          * made atomically in the future.
433          */
434         INIT_WORK(&isp->work, isp1704_charger_work);
435
436         isp->nb.notifier_call = isp1704_notifier_call;
437
438         ret = usb_register_notifier(isp->phy, &isp->nb);
439         if (ret)
440                 goto fail2;
441
442         dev_info(isp->dev, "registered with product id %s\n", isp->model);
443
444         /*
445          * Taking over the D+ pullup.
446          *
447          * FIXME: The device will be disconnected if it was already
448          * enumerated. The charger driver should be always loaded before any
449          * gadget is loaded.
450          */
451         if (isp->phy->otg->gadget)
452                 usb_gadget_disconnect(isp->phy->otg->gadget);
453
454         if (isp->phy->last_event == USB_EVENT_NONE)
455                 isp1704_charger_set_power(isp, 0);
456
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);
461
462         return 0;
463 fail2:
464         power_supply_unregister(&isp->psy);
465 fail1:
466         isp1704_charger_set_power(isp, 0);
467         usb_put_phy(isp->phy);
468 fail0:
469         dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret);
470
471         return ret;
472 }
473
474 static int isp1704_charger_remove(struct platform_device *pdev)
475 {
476         struct isp1704_charger *isp = platform_get_drvdata(pdev);
477
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);
482
483         return 0;
484 }
485
486 static struct platform_driver isp1704_charger_driver = {
487         .driver = {
488                 .name = "isp1704_charger",
489         },
490         .probe = isp1704_charger_probe,
491         .remove = isp1704_charger_remove,
492 };
493
494 module_platform_driver(isp1704_charger_driver);
495
496 MODULE_ALIAS("platform:isp1704_charger");
497 MODULE_AUTHOR("Nokia Corporation");
498 MODULE_DESCRIPTION("ISP170x USB Charger driver");
499 MODULE_LICENSE("GPL");