blob: d8c99b596357a18165b71ba2a75bbad82231a128 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * 88pm80x VBus driver for Marvell USB
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/platform_device.h>
12#include <linux/mfd/88pm80x.h>
13#include <linux/delay.h>
14#include <linux/platform_data/mv_usb.h>
15#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/mfd/pm802.h>
18
19#define USB_HANDLE_TIME_MSEC (5000)
20
21struct pm80x_usb_info {
22 struct pm80x_chip *chip;
23 struct pm80x_subchip *subchip;
24 struct work_struct meas_id_work;
25 int vbus_irq;
26 int id_irq;
27 int id_level;
28 int vbus_gpio;
29 int id_gpadc;
30 int vbus_detect;
31 int get_vbus;
32 bool id_ov_sampling;
33 int id_ov_samp_count;
34 int id_ov_samp_sleep;
35 unsigned int gpadc_meas;
36 unsigned int gpadc_upp_th;
37 unsigned int gpadc_low_th;
38 int vchg_from_exton;
39 int chg_in;
40 int id_in;
41 spinlock_t lock;
42};
43
44static struct pm80x_usb_info *usb_info;
45static int rm_flag;
46
47static int pm80x_read_usb_val(unsigned int *level)
48{
49 int ret;
50 unsigned int val;
51 unsigned long flags;
52
53 spin_lock_irqsave(&usb_info->lock, flags);
54 if (rm_flag) {
55 *level = VBUS_LOW;
56 spin_unlock_irqrestore(&usb_info->lock, flags);
57 pr_info("%s do nothing..\n", __func__);
58 return 0;
59 } else {
60 spin_unlock_irqrestore(&usb_info->lock, flags);
61 }
62
63 ret = regmap_read(usb_info->chip->regmap,
64 PM800_STATUS_1, &val);
65 if (ret)
66 return ret;
67
68 if (usb_info->vchg_from_exton)
69 *level = (val & PM800_EXTON_STS1) ? VBUS_HIGH : VBUS_LOW;
70 else
71 *level = (val & PM800_CHG_STS1) ? VBUS_HIGH : VBUS_LOW;
72
73 if (usb_info->id_in) {
74 dev_info(usb_info->chip->dev,
75 "VBUS requested with USB ID IN\n");
76 return 0;
77 }
78
79 if (*level == VBUS_HIGH) {
80 if (!usb_info->chg_in && usb_info->id_gpadc != -1) {
81 dev_dbg(usb_info->chip->dev,
82 "USB cable in, and disable OTG interrupts\n");
83 usb_info->chg_in = 1;
84 disable_irq(usb_info->id_irq);
85 }
86 } else {
87 if (usb_info->chg_in && usb_info->id_gpadc != -1) {
88 dev_dbg(usb_info->chip->dev,
89 "USB cable out, and enable OTG interrupts\n");
90 usb_info->chg_in = 0;
91 enable_irq(usb_info->id_irq);
92 }
93 }
94
95 return 0;
96}
97
98#if 0
99static int pm80x_read_id_val(unsigned int *level)
100{
101 int ret, data;
102 unsigned int val;
103 unsigned int meas1, meas2, upp_th, low_th;
104 unsigned long flags;
105
106 spin_lock_irqsave(&usb_info->lock, flags);
107 if (rm_flag) {
108 *level = VBUS_LOW;
109 spin_unlock_irqrestore(&usb_info->lock, flags);
110 pr_info("%s do nothing..\n", __func__);
111 return 0;
112 } else {
113 spin_unlock_irqrestore(&usb_info->lock, flags);
114 }
115
116
117 switch (usb_info->id_gpadc) {
118 case PM800_GPADC0:
119 meas1 = PM800_GPADC0_MEAS1;
120 meas2 = PM800_GPADC0_MEAS2;
121 low_th = PM800_GPADC0_LOW_TH;
122 upp_th = PM800_GPADC0_UPP_TH;
123 break;
124 case PM800_GPADC1:
125 meas1 = PM800_GPADC1_MEAS1;
126 meas2 = PM800_GPADC1_MEAS2;
127 low_th = PM800_GPADC1_LOW_TH;
128 upp_th = PM800_GPADC1_UPP_TH;
129 break;
130 case PM800_GPADC2:
131 meas1 = PM800_GPADC2_MEAS1;
132 meas2 = PM800_GPADC2_MEAS2;
133 low_th = PM800_GPADC2_LOW_TH;
134 upp_th = PM800_GPADC2_UPP_TH;
135 break;
136 case PM800_GPADC3:
137 meas1 = PM800_GPADC3_MEAS1;
138 meas2 = PM800_GPADC3_MEAS2;
139 low_th = PM800_GPADC3_LOW_TH;
140 upp_th = PM800_GPADC3_UPP_TH;
141 break;
142 case PM800_GPADC4:
143 meas1 = PM800_GPADC4_MEAS1;
144 meas2 = PM800_GPADC4_MEAS2;
145 low_th = PM800_GPADC4_LOW_TH;
146 upp_th = PM800_GPADC4_UPP_TH;
147 break;
148 default:
149 return -ENODEV;
150 }
151
152 ret = regmap_read(usb_info->subchip->regmap_gpadc, meas1, &val);
153 data = val << 4;
154 if (ret)
155 return ret;
156
157 ret = regmap_read(usb_info->subchip->regmap_gpadc, meas2, &val);
158 data |= val & 0x0F;
159 if (ret)
160 return ret;
161
162 if (data > 0x10) {
163 regmap_write(usb_info->subchip->regmap_gpadc, low_th, 0x10);
164 if (ret)
165 return ret;
166
167 regmap_write(usb_info->subchip->regmap_gpadc, upp_th, 0xff);
168 if (ret)
169 return ret;
170
171 *level = 1;
172 } else {
173 regmap_write(usb_info->subchip->regmap_gpadc, low_th, 0);
174 if (ret)
175 return ret;
176
177 regmap_write(usb_info->subchip->regmap_gpadc, upp_th, 0x10);
178 if (ret)
179 return ret;
180
181 *level = 0;
182 }
183
184 return 0;
185};
186#endif
187
188static int pm80x_ext_read_id_level(unsigned int *level)
189{
190 unsigned long flags;
191
192 spin_lock_irqsave(&usb_info->lock, flags);
193 if (rm_flag) {
194 *level = 0;
195 spin_unlock_irqrestore(&usb_info->lock, flags);
196 pr_info("%s do nothing..\n", __func__);
197 return 0;
198 } else {
199 spin_unlock_irqrestore(&usb_info->lock, flags);
200 }
201
202 if (usb_info->chg_in) {
203 *level = 1;
204 dev_info(usb_info->chip->dev, "idpin requested with USB IN\n");
205 return 0;
206 }
207
208 *level = usb_info->id_level;
209
210 if (usb_info->id_level) {
211 /* USB ID plug out */
212 if (usb_info->vbus_detect != -1 && usb_info->id_in == 1) {
213 dev_dbg(usb_info->chip->dev,
214 "USB ID OUT, and enable VBUS INT\n");
215 usb_info->id_in = 0;
216 enable_irq(usb_info->vbus_irq);
217 }
218 } else {
219 /* USB ID plug in */
220 if (usb_info->vbus_detect != -1 && usb_info->id_in == 0) {
221 dev_dbg(usb_info->chip->dev,
222 "USB ID IN, and disable VBUS INT\n");
223 usb_info->id_in = 1;
224 disable_irq(usb_info->vbus_irq);
225 }
226 }
227
228 return 0;
229}
230
231static int pm80x_get_id_level(unsigned int *level)
232{
233 int ret, data;
234 unsigned char buf[2];
235
236 ret = regmap_bulk_read(usb_info->subchip->regmap_gpadc,
237 usb_info->gpadc_meas, buf, 2);
238 if (ret)
239 return ret;
240
241 data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
242 if (data > 0x100)
243 *level = 1;
244 else
245 *level = 0;
246
247 dev_dbg(usb_info->chip->dev,
248 "usb id voltage = %d mV, level is %s\n",
249 (((data) & 0xfff) * 175) >> 9, (*level == 1 ? "HIGH" : "LOW"));
250
251 return 0;
252}
253
254
255static int pm80x_update_id_level(void)
256{
257 int ret;
258
259 ret = pm80x_get_id_level(&usb_info->id_level);
260 if (ret)
261 return ret;
262
263 if (usb_info->id_level) {
264 regmap_write(usb_info->subchip->regmap_gpadc,
265 usb_info->gpadc_low_th, 0x10);
266 regmap_write(usb_info->subchip->regmap_gpadc,
267 usb_info->gpadc_upp_th, 0xff);
268 } else {
269 regmap_write(usb_info->subchip->regmap_gpadc,
270 usb_info->gpadc_low_th, 0);
271 regmap_write(usb_info->subchip->regmap_gpadc,
272 usb_info->gpadc_upp_th, 0x10);
273 }
274
275 dev_info(usb_info->chip->dev, "idpin is %s\n",
276 usb_info->id_level ? "HIGH" : "LOW");
277 return 0;
278}
279
280static void pm80x_meas_id_work(struct work_struct *work)
281{
282 int i = 0;
283 unsigned int level, last_level = 1;
284
285 /*
286 * 1.loop until the line is stable
287 * 2.in every iteration do the follwing:
288 * - measure the line voltage
289 * - check if the voltage is the same as the previous value
290 * - if not, start the loop again (set loop index to 0)
291 * - if yes, continue the loop to next iteration
292 * 3.if we get x (id_meas_count) identical results, loop end
293 */
294 while (i < usb_info->id_ov_samp_count) {
295 pm80x_get_id_level(&level);
296
297 if (i == 0) {
298 last_level = level;
299 i++;
300 } else if (level != last_level) {
301 i = 0;
302 } else {
303 i++;
304 }
305
306 msleep(usb_info->id_ov_samp_sleep);
307 }
308
309 /* set the GPADC thrsholds for next insertion/removal */
310 if (last_level) {
311 regmap_write(usb_info->subchip->regmap_gpadc,
312 usb_info->gpadc_low_th, 0x10);
313 regmap_write(usb_info->subchip->regmap_gpadc,
314 usb_info->gpadc_upp_th, 0xff);
315 } else {
316 regmap_write(usb_info->subchip->regmap_gpadc,
317 usb_info->gpadc_low_th, 0);
318 regmap_write(usb_info->subchip->regmap_gpadc,
319 usb_info->gpadc_upp_th, 0x10);
320 }
321
322 /* after the line is stable, we can enable the id interrupt */
323 enable_irq(usb_info->id_irq);
324
325 /*
326 * in case we missed interrupt till we enable it
327 * we take one more measurment
328 */
329 pm80x_get_id_level(&level);
330
331 /*
332 * if the last measurment is different from the stable value,
333 * need to start the process again
334 */
335 if (level != last_level) {
336 dev_dbg(usb_info->chip->dev, "disable irq, resched id work\n");
337 disable_irq(usb_info->id_irq);
338 schedule_work(&usb_info->meas_id_work);
339 return;
340 }
341
342 /* notify to wake up the usb subsystem if ID pin value changed */
343 if (last_level != usb_info->id_level) {
344 usb_info->id_level = last_level;
345 pxa_usb_notify(PXA_USB_DEV_OTG, EVENT_ID, 0);
346
347 dev_info(usb_info->chip->dev, "idpin is %s\n",
348 usb_info->id_level ? "HIGH" : "LOW");
349 }
350}
351
352int pm80x_init_id(void)
353{
354#if 0
355 int ret;
356 unsigned int en;
357 unsigned long flags;
358
359 spin_lock_irqsave(&usb_info->lock, flags);
360 if (rm_flag) {
361 spin_unlock_irqrestore(&usb_info->lock, flags);
362 pr_info("%s do nothing..\n", __func__);
363 return 0;
364 } else {
365 spin_unlock_irqrestore(&usb_info->lock, flags);
366 }
367
368 switch (usb_info->id_gpadc) {
369 case PM800_GPADC0:
370 en = PM800_MEAS_GP0_EN;
371 break;
372 case PM800_GPADC1:
373 en = PM800_MEAS_GP1_EN;
374 break;
375 case PM800_GPADC2:
376 en = PM800_MEAS_GP2_EN;
377 break;
378 case PM800_GPADC3:
379 en = PM800_MEAS_GP3_EN;
380 break;
381 case PM800_GPADC4:
382 en = PM800_MEAS_GP4_EN;
383 break;
384 default:
385 return -ENODEV;
386 }
387
388 ret = regmap_update_bits(usb_info->subchip->regmap_gpadc,
389 PM800_GPADC_MEAS_EN2, en, en);
390 if (ret)
391 return ret;
392
393 ret = regmap_update_bits(usb_info->subchip->regmap_gpadc,
394 PM800_GPADC_MISC_CONFIG2, PM800_GPADC_MISC_GPFSM_EN,
395 PM800_GPADC_MISC_GPFSM_EN);
396 if (ret)
397 return ret;
398#endif
399 return 0;
400}
401
402static void pm802_usb_id_config(struct pm80x_usb_info *info)
403{
404 unsigned int gpadc_en, bias_en;
405
406 /* pm802 based usb id detetction is not supported now */
407 BUG();
408
409 /* set gpadc parameters */
410 switch (info->id_gpadc) {
411 case PM802_GPADC0:
412 info->gpadc_meas = PM802_GPADC0_MEAS1;
413 info->gpadc_low_th = PM802_GPADC0_LOW_TH;
414 info->gpadc_upp_th = PM802_GPADC0_HIGH_TH;
415 gpadc_en = PM802_MEAS_GP0_EN;
416 bias_en = PM802_GPADC_GP_BIAS_EN0;
417
418 /* Set bias to 36uA*/
419 regmap_update_bits(info->subchip->regmap_gpadc,
420 PM802_GPADC_BIAS1,
421 PM802_GPADC_BIAS_MASK0 | PM802_GPADC_BIAS_MASK1,
422 BIAS_GP_SET(36));
423 break;
424 case PM802_GPADC1:
425 info->gpadc_meas = PM802_GPADC1_MEAS1;
426 info->gpadc_low_th = PM802_GPADC1_LOW_TH;
427 info->gpadc_upp_th = PM802_GPADC1_HIGH_TH;
428 gpadc_en = PM802_MEAS_GP1_EN;
429 bias_en = PM802_GPADC_GP_BIAS_EN1;
430
431 /* Set bias to 36uA*/
432 regmap_update_bits(info->subchip->regmap_gpadc,
433 PM802_GPADC_BIAS2,
434 PM802_GPADC_BIAS_MASK0 | PM802_GPADC_BIAS_MASK1,
435 (BIAS_GP_SET(36) << 4));
436 break;
437 default:
438 dev_err(info->chip->dev, "no configuration for gpadc %d\n",
439 info->id_gpadc);
440 return;
441 }
442
443 /* enable GPADCx BIAS ENABLE AND OUT BIT */
444 regmap_update_bits(info->subchip->regmap_gpadc,
445 PM802_GP_BIAS_ENA1,
446 bias_en, bias_en);
447
448 /* enable GPADCx meas function */
449 regmap_update_bits(info->subchip->regmap_gpadc,
450 PM802_GPADC_MEAS_EN1, gpadc_en, gpadc_en);
451
452 regmap_update_bits(info->subchip->regmap_gpadc,
453 PM802_GPADC_MISC_CONFIG2, PM802_GPADC_MISC_GPFSM_EN,
454 PM802_GPADC_MISC_GPFSM_EN);
455
456 /* read ID level and set the thresholds for GPADC to prepare for INT */
457 pm80x_update_id_level();
458 if (usb_info->id_level == 0)
459 usb_info->id_in = 1;
460 else
461 usb_info->id_in = 0;
462}
463
464static void pm80x_usb_id_config(struct pm80x_usb_info *info)
465{
466 unsigned int gpadc_en, bias_en;
467
468 /* set gpadc parameters */
469 switch (info->id_gpadc) {
470 case PM800_GPADC0:
471 info->gpadc_meas = PM800_GPADC0_MEAS1;
472 info->gpadc_low_th = PM800_GPADC0_LOW_TH;
473 info->gpadc_upp_th = PM800_GPADC0_UPP_TH;
474 gpadc_en = PM800_MEAS_GP0_EN;
475 bias_en = PM800_GPADC_GP_BIAS_EN0 | PM800_GPADC_GP_BIAS_OUT0;
476 /* Set bias to 36uA*/
477 regmap_update_bits(info->subchip->regmap_gpadc,
478 PM800_GPADC_BIAS1,
479 PM800_GPADC_BIAS_MASK0 | PM800_GPADC_BIAS_MASK1,
480 BIAS_GP_SET(36) | GP_PREBIAS(3));
481 break;
482 case PM800_GPADC1:
483 info->gpadc_meas = PM800_GPADC1_MEAS1;
484 info->gpadc_low_th = PM800_GPADC1_LOW_TH;
485 info->gpadc_upp_th = PM800_GPADC1_UPP_TH;
486 gpadc_en = PM800_MEAS_GP1_EN;
487 bias_en = PM800_GPADC_GP_BIAS_EN1 | PM800_GPADC_GP_BIAS_OUT1;
488
489 /* Set bias to 36uA*/
490 regmap_update_bits(info->subchip->regmap_gpadc,
491 PM800_GPADC_BIAS2,
492 PM800_GPADC_BIAS_MASK0 | PM800_GPADC_BIAS_MASK1,
493 BIAS_GP_SET(36) | GP_PREBIAS(3));
494 break;
495 case PM800_GPADC2:
496 info->gpadc_meas = PM800_GPADC2_MEAS1;
497 info->gpadc_low_th = PM800_GPADC2_LOW_TH;
498 info->gpadc_upp_th = PM800_GPADC2_UPP_TH;
499 gpadc_en = PM800_MEAS_GP2_EN;
500 bias_en = PM800_GPADC_GP_BIAS_EN2 | PM800_GPADC_GP_BIAS_OUT2;
501
502 /* Set bias to 36uA*/
503 regmap_update_bits(info->subchip->regmap_gpadc,
504 PM800_GPADC_BIAS3,
505 PM800_GPADC_BIAS_MASK0 | PM800_GPADC_BIAS_MASK1,
506 BIAS_GP_SET(36) | GP_PREBIAS(3));
507 break;
508 case PM800_GPADC3:
509 info->gpadc_meas = PM800_GPADC3_MEAS1;
510 info->gpadc_low_th = PM800_GPADC3_LOW_TH;
511 info->gpadc_upp_th = PM800_GPADC3_UPP_TH;
512 gpadc_en = PM800_MEAS_GP3_EN;
513 bias_en = PM800_GPADC_GP_BIAS_EN3 | PM800_GPADC_GP_BIAS_OUT3;
514
515 /* Set bias to 36uA*/
516 regmap_update_bits(info->subchip->regmap_gpadc,
517 PM800_GPADC_BIAS4,
518 PM800_GPADC_BIAS_MASK0 | PM800_GPADC_BIAS_MASK1,
519 BIAS_GP_SET(36) | GP_PREBIAS(3));
520 break;
521 default:
522 dev_err(info->chip->dev, "no configuration for gpadc %d\n",
523 info->id_gpadc);
524 return;
525 }
526
527 /* enable GPADCx BIAS ENABLE AND OUT BIT */
528 regmap_update_bits(info->subchip->regmap_gpadc,
529 PM800_GP_BIAS_ENA1,
530 bias_en, bias_en);
531
532 /* enable GPADCx meas function */
533 regmap_update_bits(info->subchip->regmap_gpadc,
534 PM800_GPADC_MEAS_EN2, gpadc_en, gpadc_en);
535
536 regmap_update_bits(info->subchip->regmap_gpadc,
537 PM800_GPADC_MISC_CONFIG2, PM800_GPADC_MISC_GPFSM_EN,
538 PM800_GPADC_MISC_GPFSM_EN);
539 /* read ID level and set the thresholds for GPADC to prepare for INT */
540 pm80x_update_id_level();
541 if (usb_info->id_level == 0)
542 usb_info->id_in = 1;
543 else
544 usb_info->id_in = 0;
545}
546
547static int pm80x_set_vbus(unsigned int vbus)
548{
549 int ret;
550 unsigned int data = 0, mask, reg = 0;
551 unsigned long flags;
552
553 spin_lock_irqsave(&usb_info->lock, flags);
554 if (rm_flag) {
555 spin_unlock_irqrestore(&usb_info->lock, flags);
556 pr_info("%s do nothing..\n", __func__);
557 return 0;
558 } else {
559 spin_unlock_irqrestore(&usb_info->lock, flags);
560 }
561
562 switch (usb_info->vbus_gpio) {
563 case PM800_NO_GPIO:
564 /* OTG5V not supported - Do nothing */
565 return 0;
566
567 case PM800_GPIO0:
568 /* OTG5V Enable/Disable is connected to GPIO_0 */
569 mask = PM800_GPIO0_GPIO_MODE(0x01) | PM800_GPIO0_VAL;
570 reg = PM800_GPIO_0_1_CNTRL;
571 break;
572
573 case PM800_GPIO1:
574 /* OTG5V Enable/Disable is connected to GPIO_1 */
575 mask = PM800_GPIO1_GPIO_MODE(0x01) | PM800_GPIO1_VAL;
576 reg = PM800_GPIO_0_1_CNTRL;
577 break;
578
579 case PM800_GPIO2:
580 /* OTG5V Enable/Disable is connected to GPIO_2 */
581 mask = PM800_GPIO2_GPIO_MODE(0x01) | PM800_GPIO2_VAL;
582 reg = PM800_GPIO_2_3_CNTRL;
583 break;
584
585 case PM800_GPIO3:
586 /* OTG5V Enable/Disable is connected to GPIO_3 */
587 mask = PM800_GPIO3_GPIO_MODE(0x01) | PM800_GPIO3_VAL;
588 reg = PM800_GPIO_2_3_CNTRL;
589 break;
590
591 case PM800_GPIO4:
592 /* OTG5V Enable/Disable is connected to GPIO_4 */
593 mask = PM800_GPIO4_GPIO_MODE(0x01) | PM800_GPIO4_VAL;
594 reg = PM800_GPIO_4_5_CNTRL;
595 break;
596
597 default:
598 return -ENODEV;
599 }
600
601 if (vbus == VBUS_HIGH)
602 data = mask;
603
604 ret = regmap_update_bits(usb_info->chip->regmap,
605 reg, mask, data);
606 if (ret)
607 return ret;
608
609 mdelay(20);
610
611 ret = pm80x_read_usb_val(&data);
612 if (ret)
613 return ret;
614
615 if (ret != vbus)
616 pr_info("vbus set failed %x\n", vbus);
617 else
618 pr_info("vbus set done %x\n", vbus);
619
620 return 0;
621}
622
623static irqreturn_t vbus_irq(int irq, void *dev)
624{
625 if (usb_info->id_level == 0)
626 return IRQ_HANDLED;
627
628 pm_wakeup_event(usb_info->chip->dev, USB_HANDLE_TIME_MSEC);
629 pxa_usb_notify(PXA_USB_DEV_OTG, EVENT_VBUS, 0);
630 dev_info(usb_info->chip->dev, "88pm80x vbus interrupt is served..\n");
631 return IRQ_HANDLED;
632}
633
634static irqreturn_t id_handler(int irq, void *data)
635{
636 struct pm80x_usb_info *info = data;
637
638 pm_wakeup_event(info->chip->dev, USB_HANDLE_TIME_MSEC);
639 if (info->id_ov_sampling) {
640 /* disable id interrupt, and start measurment process */
641 disable_irq_nosync(info->id_irq);
642 schedule_work(&info->meas_id_work);
643 } else {
644 /* update id value */
645 pm80x_update_id_level();
646
647 /* notify to wake up usb subsystem if ID pin is pulled down */
648 pxa_usb_notify(PXA_USB_DEV_OTG, EVENT_ID, 0);
649 }
650
651 dev_info(info->chip->dev, "88pm80x id interrupt is served..\n");
652 return IRQ_HANDLED;
653}
654
655static int pm80x_usb_probe(struct platform_device *pdev)
656{
657 struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
658 struct pm80x_usb_info *usb;
659 struct device_node *np = pdev->dev.of_node;
660 int ret;
661
662 usb = kzalloc(sizeof(struct pm80x_usb_info), GFP_KERNEL);
663 if (!usb)
664 return -ENOMEM;
665
666 usb->chip = chip;
667 usb->subchip = chip->subchip;
668
669 if (!of_device_is_available(pdev->dev.of_node)) {
670 dev_err(&pdev->dev, "dts disabled!\n");
671 ret = -EINVAL;
672 goto out;
673 }
674 ret = of_property_read_u32(np, "vbus_gpio", &usb->vbus_gpio);
675 if (ret)
676 return ret;
677 if (usb->vbus_gpio == 0xff)
678 usb->vbus_gpio = -1;
679
680 ret = of_property_read_u32(np, "id-gpadc", &usb->id_gpadc);
681 if (ret)
682 return ret;
683 if (usb->id_gpadc == 0xff)
684 usb->id_gpadc = -1;
685
686 ret = of_property_read_u32(np, "vbus-detect", &usb->vbus_detect);
687 if (ret)
688 usb->vbus_detect = 0;
689
690 ret = of_property_read_u32(np, "get-vbus", &usb->get_vbus);
691 if (ret)
692 usb->get_vbus = 0;
693
694 ret = of_property_read_u32(np, "vchg-from-exton",
695 &usb->vchg_from_exton);
696 if (ret)
697 usb->vchg_from_exton = 0;
698
699 usb->id_ov_sampling = of_property_read_bool(np,
700 "id-ov-sampling");
701
702 if (usb->id_ov_sampling) {
703 ret = of_property_read_u32(np, "id-ov-samp-count",
704 &usb->id_ov_samp_count);
705 if (ret) {
706 pr_err("cannot get id measurments count.\n");
707 return -EINVAL;
708 }
709
710 ret = of_property_read_u32(np, "id-ov-samp-sleep",
711 &usb->id_ov_samp_sleep);
712 if (ret) {
713 pr_err("cannot get id sleep time.\n");
714 return -EINVAL;
715 }
716 }
717 usb->id_level = 1;
718 usb->id_in = 0;
719 usb->chg_in = 0;
720
721 spin_lock_init(&usb->lock);
722
723
724 if (usb->vbus_gpio != -1)
725 pxa_usb_set_extern_call(PXA_USB_DEV_OTG, vbus, set_vbus,
726 pm80x_set_vbus);
727
728 if (usb->get_vbus)
729 pxa_usb_set_extern_call(PXA_USB_DEV_OTG, vbus, get_vbus,
730 pm80x_read_usb_val);
731
732 if (usb->id_gpadc != -1) {
733 pxa_usb_set_extern_call(PXA_USB_DEV_OTG, idpin, get_idpin,
734 pm80x_ext_read_id_level);
735 pxa_usb_set_extern_call(PXA_USB_DEV_OTG, idpin, init,
736 pm80x_init_id);
737 }
738
739 INIT_WORK(&usb->meas_id_work, pm80x_meas_id_work);
740
741 usb_info = usb;
742
743 if (usb->id_gpadc != -1) {
744 if (usb->chip->type == CHIP_PM801 || usb->chip->type == CHIP_PM800)
745 pm80x_usb_id_config(usb);
746 else if (usb->chip->type == CHIP_PM802)
747 pm802_usb_id_config(usb);
748 else {
749 dev_err(&pdev->dev, "unknow PMIC: 0x%x\n", (u32)usb->chip->type);
750 return -EINVAL;
751 }
752 }
753
754 usb->vbus_irq = platform_get_irq(pdev, 0);
755 if (usb->vbus_irq < 0) {
756 dev_err(&pdev->dev, "failed to get vbus irq\n");
757 ret = -ENXIO;
758 goto out;
759 }
760 if (usb->id_gpadc != -1) {
761 usb->id_irq = platform_get_irq(pdev, usb->id_gpadc + 1);
762 if (usb->id_irq < 0) {
763 dev_err(&pdev->dev, "failed to get idpin irq\n");
764 ret = -ENXIO;
765 goto out;
766 }
767 }
768
769 if (usb->vbus_detect) {
770 ret = devm_request_threaded_irq(&pdev->dev, usb->vbus_irq,
771 NULL, vbus_irq,
772 IRQF_ONESHOT | IRQF_NO_SUSPEND,
773 "88pm800-usb-vbus", usb);
774
775 if (ret) {
776 dev_info(&pdev->dev,
777 "Can not request irq for VBUS, "
778 "disable clock gating\n");
779 goto out;
780 }
781 }
782 if (usb->id_gpadc != -1) {
783 ret = devm_request_threaded_irq(&pdev->dev, usb->id_irq, NULL,
784 id_handler,
785 IRQF_ONESHOT | IRQF_NO_SUSPEND,
786 "88pm800-id-irq", usb);
787 if (ret) {
788 dev_info(&pdev->dev,
789 "cannot request irq for idpin, return\n");
790 goto out;
791 }
792 }
793
794 platform_set_drvdata(pdev, usb);
795 device_init_wakeup(&pdev->dev, 1);
796
797 return 0;
798out:
799 kfree(usb);
800 return ret;
801}
802
803static int pm80x_usb_remove(struct platform_device *pdev)
804{
805 struct pm80x_usb_info *usb = platform_get_drvdata(pdev);
806 unsigned long flags;
807
808 if (!usb)
809 return 0;
810
811 if (usb->vbus_detect)
812 devm_free_irq(&pdev->dev, usb->vbus_irq, usb);
813
814 if (usb->id_gpadc != -1)
815 devm_free_irq(&pdev->dev, usb->id_irq, usb);
816
817 platform_set_drvdata(pdev, NULL);
818
819 spin_lock_irqsave(&usb->lock, flags);
820 rm_flag = 1;
821 spin_unlock_irqrestore(&usb->lock, flags);
822
823 kfree(usb);
824
825 return 0;
826}
827
828static void pm80x_usb_shutdown(struct platform_device *pdev)
829{
830 pm80x_usb_remove(pdev);
831 return;
832}
833
834#ifdef CONFIG_PM
835static int pm80x_usb_suspend(struct device *dev)
836{
837 return pm80x_dev_suspend(dev);
838}
839
840static int pm80x_usb_resume(struct device *dev)
841{
842 return pm80x_dev_resume(dev);
843}
844
845static const struct dev_pm_ops pm80x_usb_pm_ops = {
846 .suspend = pm80x_usb_suspend,
847 .resume = pm80x_usb_resume,
848};
849#endif
850
851static struct platform_driver pm80x_usb_driver = {
852 .driver = {
853 .name = "88pm80x-usb",
854 .owner = THIS_MODULE,
855#ifdef CONFIG_PM
856 .pm = &pm80x_usb_pm_ops,
857#endif
858 },
859 .probe = pm80x_usb_probe,
860 .remove = pm80x_usb_remove,
861 .shutdown = pm80x_usb_shutdown,
862};
863
864static int __init pm80x_usb_init(void)
865{
866 return platform_driver_register(&pm80x_usb_driver);
867}
868module_init(pm80x_usb_init);
869
870static void __exit pm80x_usb_exit(void)
871{
872 platform_driver_unregister(&pm80x_usb_driver);
873}
874module_exit(pm80x_usb_exit);
875
876MODULE_DESCRIPTION("VBUS driver for Marvell Semiconductor 88PM80x");
877MODULE_LICENSE("GPL");
878MODULE_ALIAS("platform:88pm80x-usb");