This source file includes following definitions.
- cpcap_usb_vbus_valid
- cpcap_usb_phy_set_host
- cpcap_usb_phy_set_peripheral
- cpcap_phy_get_ints_state
- cpcap_usb_try_musb_mailbox
- cpcap_usb_detect
- cpcap_phy_irq_thread
- cpcap_usb_init_irq
- cpcap_usb_init_interrupts
- cpcap_usb_gpio_set_mode
- cpcap_usb_set_uart_mode
- cpcap_usb_set_usb_mode
- cpcap_usb_init_optional_pins
- cpcap_usb_init_optional_gpios
- cpcap_usb_init_iio
- cpcap_usb_phy_probe
- cpcap_usb_phy_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <linux/atomic.h>
20 #include <linux/clk.h>
21 #include <linux/delay.h>
22 #include <linux/err.h>
23 #include <linux/io.h>
24 #include <linux/module.h>
25 #include <linux/of.h>
26 #include <linux/of_platform.h>
27 #include <linux/iio/consumer.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/platform_device.h>
30 #include <linux/regmap.h>
31 #include <linux/slab.h>
32
33 #include <linux/gpio/consumer.h>
34 #include <linux/mfd/motorola-cpcap.h>
35 #include <linux/phy/omap_usb.h>
36 #include <linux/phy/phy.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/usb/musb.h>
39
40
41 #define CPCAP_BIT_IDPULSE BIT(15)
42 #define CPCAP_BIT_ID100KPU BIT(14)
43 #define CPCAP_BIT_IDPUCNTRL BIT(13)
44 #define CPCAP_BIT_IDPU BIT(12)
45 #define CPCAP_BIT_IDPD BIT(11)
46 #define CPCAP_BIT_VBUSCHRGTMR3 BIT(10)
47 #define CPCAP_BIT_VBUSCHRGTMR2 BIT(9)
48 #define CPCAP_BIT_VBUSCHRGTMR1 BIT(8)
49 #define CPCAP_BIT_VBUSCHRGTMR0 BIT(7)
50 #define CPCAP_BIT_VBUSPU BIT(6)
51 #define CPCAP_BIT_VBUSPD BIT(5)
52 #define CPCAP_BIT_DMPD BIT(4)
53 #define CPCAP_BIT_DPPD BIT(3)
54 #define CPCAP_BIT_DM1K5PU BIT(2)
55 #define CPCAP_BIT_DP1K5PU BIT(1)
56 #define CPCAP_BIT_DP150KPU BIT(0)
57
58
59 #define CPCAP_BIT_ZHSDRV1 BIT(15)
60 #define CPCAP_BIT_ZHSDRV0 BIT(14)
61 #define CPCAP_BIT_DPLLCLKREQ BIT(13)
62 #define CPCAP_BIT_SE0CONN BIT(12)
63 #define CPCAP_BIT_UARTTXTRI BIT(11)
64 #define CPCAP_BIT_UARTSWAP BIT(10)
65 #define CPCAP_BIT_UARTMUX1 BIT(9)
66 #define CPCAP_BIT_UARTMUX0 BIT(8)
67 #define CPCAP_BIT_ULPISTPLOW BIT(7)
68 #define CPCAP_BIT_TXENPOL BIT(6)
69 #define CPCAP_BIT_USBXCVREN BIT(5)
70 #define CPCAP_BIT_USBCNTRL BIT(4)
71 #define CPCAP_BIT_USBSUSPEND BIT(3)
72 #define CPCAP_BIT_EMUMODE2 BIT(2)
73 #define CPCAP_BIT_EMUMODE1 BIT(1)
74 #define CPCAP_BIT_EMUMODE0 BIT(0)
75
76
77 #define CPCAP_BIT_SPARE_898_15 BIT(15)
78 #define CPCAP_BIT_IHSTX03 BIT(14)
79 #define CPCAP_BIT_IHSTX02 BIT(13)
80 #define CPCAP_BIT_IHSTX01 BIT(12)
81 #define CPCAP_BIT_IHSTX0 BIT(11)
82 #define CPCAP_BIT_IDPU_SPI BIT(10)
83 #define CPCAP_BIT_UNUSED_898_9 BIT(9)
84 #define CPCAP_BIT_VBUSSTBY_EN BIT(8)
85 #define CPCAP_BIT_VBUSEN_SPI BIT(7)
86 #define CPCAP_BIT_VBUSPU_SPI BIT(6)
87 #define CPCAP_BIT_VBUSPD_SPI BIT(5)
88 #define CPCAP_BIT_DMPD_SPI BIT(4)
89 #define CPCAP_BIT_DPPD_SPI BIT(3)
90 #define CPCAP_BIT_SUSPEND_SPI BIT(2)
91 #define CPCAP_BIT_PU_SPI BIT(1)
92 #define CPCAP_BIT_ULPI_SPI_SEL BIT(0)
93
94 struct cpcap_usb_ints_state {
95 bool id_ground;
96 bool id_float;
97 bool chrg_det;
98 bool rvrs_chrg;
99 bool vbusov;
100
101 bool chrg_se1b;
102 bool se0conn;
103 bool rvrs_mode;
104 bool chrgcurr1;
105 bool vbusvld;
106 bool sessvld;
107 bool sessend;
108 bool se1;
109
110 bool battdetb;
111 bool dm;
112 bool dp;
113 };
114
115 enum cpcap_gpio_mode {
116 CPCAP_DM_DP,
117 CPCAP_MDM_RX_TX,
118 CPCAP_UNKNOWN_DISABLED,
119 CPCAP_OTG_DM_DP,
120 };
121
122 struct cpcap_phy_ddata {
123 struct regmap *reg;
124 struct device *dev;
125 struct clk *refclk;
126 struct usb_phy phy;
127 struct delayed_work detect_work;
128 struct pinctrl *pins;
129 struct pinctrl_state *pins_ulpi;
130 struct pinctrl_state *pins_utmi;
131 struct pinctrl_state *pins_uart;
132 struct gpio_desc *gpio[2];
133 struct iio_channel *vbus;
134 struct iio_channel *id;
135 struct regulator *vusb;
136 atomic_t active;
137 };
138
139 static bool cpcap_usb_vbus_valid(struct cpcap_phy_ddata *ddata)
140 {
141 int error, value = 0;
142
143 error = iio_read_channel_processed(ddata->vbus, &value);
144 if (error >= 0)
145 return value > 3900 ? true : false;
146
147 dev_err(ddata->dev, "error reading VBUS: %i\n", error);
148
149 return false;
150 }
151
152 static int cpcap_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
153 {
154 otg->host = host;
155 if (!host)
156 otg->state = OTG_STATE_UNDEFINED;
157
158 return 0;
159 }
160
161 static int cpcap_usb_phy_set_peripheral(struct usb_otg *otg,
162 struct usb_gadget *gadget)
163 {
164 otg->gadget = gadget;
165 if (!gadget)
166 otg->state = OTG_STATE_UNDEFINED;
167
168 return 0;
169 }
170
171 static const struct phy_ops ops = {
172 .owner = THIS_MODULE,
173 };
174
175 static int cpcap_phy_get_ints_state(struct cpcap_phy_ddata *ddata,
176 struct cpcap_usb_ints_state *s)
177 {
178 int val, error;
179
180 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
181 if (error)
182 return error;
183
184 s->id_ground = val & BIT(15);
185 s->id_float = val & BIT(14);
186 s->vbusov = val & BIT(11);
187
188 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
189 if (error)
190 return error;
191
192 s->vbusvld = val & BIT(3);
193 s->sessvld = val & BIT(2);
194 s->sessend = val & BIT(1);
195 s->se1 = val & BIT(0);
196
197 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
198 if (error)
199 return error;
200
201 s->dm = val & BIT(1);
202 s->dp = val & BIT(0);
203
204 return 0;
205 }
206
207 static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata);
208 static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata);
209
210 static void cpcap_usb_try_musb_mailbox(struct cpcap_phy_ddata *ddata,
211 enum musb_vbus_id_status status)
212 {
213 int error;
214
215 error = musb_mailbox(status);
216 if (!error)
217 return;
218
219 dev_dbg(ddata->dev, "%s: musb_mailbox failed: %i\n",
220 __func__, error);
221 }
222
223 static void cpcap_usb_detect(struct work_struct *work)
224 {
225 struct cpcap_phy_ddata *ddata;
226 struct cpcap_usb_ints_state s;
227 bool vbus = false;
228 int error;
229
230 ddata = container_of(work, struct cpcap_phy_ddata, detect_work.work);
231
232 error = cpcap_phy_get_ints_state(ddata, &s);
233 if (error)
234 return;
235
236 if (s.id_ground) {
237 dev_dbg(ddata->dev, "id ground, USB host mode\n");
238 error = cpcap_usb_set_usb_mode(ddata);
239 if (error)
240 goto out_err;
241
242 cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
243
244 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
245 CPCAP_BIT_VBUSSTBY_EN |
246 CPCAP_BIT_VBUSEN_SPI,
247 CPCAP_BIT_VBUSEN_SPI);
248 if (error)
249 goto out_err;
250
251 return;
252 }
253
254 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
255 CPCAP_BIT_VBUSSTBY_EN |
256 CPCAP_BIT_VBUSEN_SPI, 0);
257 if (error)
258 goto out_err;
259
260 vbus = cpcap_usb_vbus_valid(ddata);
261
262 if (vbus) {
263
264 if (s.id_ground) {
265 dev_dbg(ddata->dev, "connected to a dock\n");
266
267
268 error = cpcap_usb_set_usb_mode(ddata);
269 if (error)
270 goto out_err;
271 cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
272
273 return;
274 }
275
276
277 dev_dbg(ddata->dev, "connected to USB host\n");
278 error = cpcap_usb_set_usb_mode(ddata);
279 if (error)
280 goto out_err;
281 cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_VALID);
282
283 return;
284 }
285
286 cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);
287
288
289 error = cpcap_usb_set_uart_mode(ddata);
290 if (error)
291 goto out_err;
292
293 dev_dbg(ddata->dev, "set UART mode\n");
294
295 return;
296
297 out_err:
298 dev_err(ddata->dev, "error setting cable state: %i\n", error);
299 }
300
301 static irqreturn_t cpcap_phy_irq_thread(int irq, void *data)
302 {
303 struct cpcap_phy_ddata *ddata = data;
304
305 if (!atomic_read(&ddata->active))
306 return IRQ_NONE;
307
308 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));
309
310 return IRQ_HANDLED;
311 }
312
313 static int cpcap_usb_init_irq(struct platform_device *pdev,
314 struct cpcap_phy_ddata *ddata,
315 const char *name)
316 {
317 int irq, error;
318
319 irq = platform_get_irq_byname(pdev, name);
320 if (irq < 0)
321 return -ENODEV;
322
323 error = devm_request_threaded_irq(ddata->dev, irq, NULL,
324 cpcap_phy_irq_thread,
325 IRQF_SHARED,
326 name, ddata);
327 if (error) {
328 dev_err(ddata->dev, "could not get irq %s: %i\n",
329 name, error);
330
331 return error;
332 }
333
334 return 0;
335 }
336
337 static const char * const cpcap_phy_irqs[] = {
338
339 "id_ground", "id_float",
340
341
342 "se0conn", "vbusvld", "sessvld", "sessend", "se1",
343
344
345 "dm", "dp",
346 };
347
348 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
349 struct cpcap_phy_ddata *ddata)
350 {
351 int i, error;
352
353 for (i = 0; i < ARRAY_SIZE(cpcap_phy_irqs); i++) {
354 error = cpcap_usb_init_irq(pdev, ddata, cpcap_phy_irqs[i]);
355 if (error)
356 return error;
357 }
358
359 return 0;
360 }
361
362
363
364
365
366
367
368 static int cpcap_usb_gpio_set_mode(struct cpcap_phy_ddata *ddata,
369 enum cpcap_gpio_mode mode)
370 {
371 if (!ddata->gpio[0] || !ddata->gpio[1])
372 return 0;
373
374 gpiod_set_value(ddata->gpio[0], mode & 1);
375 gpiod_set_value(ddata->gpio[1], mode >> 1);
376
377 return 0;
378 }
379
380 static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata)
381 {
382 int error;
383
384
385 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
386 if (error)
387 goto out_err;
388
389 if (ddata->pins_uart) {
390 error = pinctrl_select_state(ddata->pins, ddata->pins_uart);
391 if (error)
392 goto out_err;
393 }
394
395 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
396 CPCAP_BIT_VBUSPD,
397 CPCAP_BIT_VBUSPD);
398 if (error)
399 goto out_err;
400
401 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
402 0xffff, CPCAP_BIT_UARTMUX0 |
403 CPCAP_BIT_EMUMODE0);
404 if (error)
405 goto out_err;
406
407 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 0x7fff,
408 CPCAP_BIT_IDPU_SPI);
409 if (error)
410 goto out_err;
411
412
413 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_DM_DP);
414 if (error)
415 goto out_err;
416
417 return 0;
418
419 out_err:
420 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
421
422 return error;
423 }
424
425 static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata)
426 {
427 int error;
428
429
430 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
431 if (error)
432 return error;
433
434 if (ddata->pins_utmi) {
435 error = pinctrl_select_state(ddata->pins, ddata->pins_utmi);
436 if (error) {
437 dev_err(ddata->dev, "could not set usb mode: %i\n",
438 error);
439
440 return error;
441 }
442 }
443
444 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
445 CPCAP_BIT_VBUSPD, 0);
446 if (error)
447 goto out_err;
448
449 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
450 CPCAP_BIT_USBXCVREN,
451 CPCAP_BIT_USBXCVREN);
452 if (error)
453 goto out_err;
454
455 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
456 CPCAP_BIT_PU_SPI |
457 CPCAP_BIT_DMPD_SPI |
458 CPCAP_BIT_DPPD_SPI |
459 CPCAP_BIT_SUSPEND_SPI |
460 CPCAP_BIT_ULPI_SPI_SEL, 0);
461 if (error)
462 goto out_err;
463
464 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
465 CPCAP_BIT_USBXCVREN,
466 CPCAP_BIT_USBXCVREN);
467 if (error)
468 goto out_err;
469
470
471 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_OTG_DM_DP);
472 if (error)
473 goto out_err;
474
475 return 0;
476
477 out_err:
478 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
479
480 return error;
481 }
482
483 static int cpcap_usb_init_optional_pins(struct cpcap_phy_ddata *ddata)
484 {
485 ddata->pins = devm_pinctrl_get(ddata->dev);
486 if (IS_ERR(ddata->pins)) {
487 dev_info(ddata->dev, "default pins not configured: %ld\n",
488 PTR_ERR(ddata->pins));
489 ddata->pins = NULL;
490
491 return 0;
492 }
493
494 ddata->pins_ulpi = pinctrl_lookup_state(ddata->pins, "ulpi");
495 if (IS_ERR(ddata->pins_ulpi)) {
496 dev_info(ddata->dev, "ulpi pins not configured\n");
497 ddata->pins_ulpi = NULL;
498 }
499
500 ddata->pins_utmi = pinctrl_lookup_state(ddata->pins, "utmi");
501 if (IS_ERR(ddata->pins_utmi)) {
502 dev_info(ddata->dev, "utmi pins not configured\n");
503 ddata->pins_utmi = NULL;
504 }
505
506 ddata->pins_uart = pinctrl_lookup_state(ddata->pins, "uart");
507 if (IS_ERR(ddata->pins_uart)) {
508 dev_info(ddata->dev, "uart pins not configured\n");
509 ddata->pins_uart = NULL;
510 }
511
512 if (ddata->pins_uart)
513 return pinctrl_select_state(ddata->pins, ddata->pins_uart);
514
515 return 0;
516 }
517
518 static void cpcap_usb_init_optional_gpios(struct cpcap_phy_ddata *ddata)
519 {
520 int i;
521
522 for (i = 0; i < 2; i++) {
523 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
524 i, GPIOD_OUT_HIGH);
525 if (IS_ERR(ddata->gpio[i])) {
526 dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
527 i, PTR_ERR(ddata->gpio[i]));
528 ddata->gpio[i] = NULL;
529 }
530 }
531 }
532
533 static int cpcap_usb_init_iio(struct cpcap_phy_ddata *ddata)
534 {
535 enum iio_chan_type type;
536 int error;
537
538 ddata->vbus = devm_iio_channel_get(ddata->dev, "vbus");
539 if (IS_ERR(ddata->vbus)) {
540 error = PTR_ERR(ddata->vbus);
541 goto out_err;
542 }
543
544 if (!ddata->vbus->indio_dev) {
545 error = -ENXIO;
546 goto out_err;
547 }
548
549 error = iio_get_channel_type(ddata->vbus, &type);
550 if (error < 0)
551 goto out_err;
552
553 if (type != IIO_VOLTAGE) {
554 error = -EINVAL;
555 goto out_err;
556 }
557
558 return 0;
559
560 out_err:
561 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
562 error);
563
564 return error;
565 }
566
567 #ifdef CONFIG_OF
568 static const struct of_device_id cpcap_usb_phy_id_table[] = {
569 {
570 .compatible = "motorola,cpcap-usb-phy",
571 },
572 {
573 .compatible = "motorola,mapphone-cpcap-usb-phy",
574 },
575 {},
576 };
577 MODULE_DEVICE_TABLE(of, cpcap_usb_phy_id_table);
578 #endif
579
580 static int cpcap_usb_phy_probe(struct platform_device *pdev)
581 {
582 struct cpcap_phy_ddata *ddata;
583 struct phy *generic_phy;
584 struct phy_provider *phy_provider;
585 struct usb_otg *otg;
586 const struct of_device_id *of_id;
587 int error;
588
589 of_id = of_match_device(of_match_ptr(cpcap_usb_phy_id_table),
590 &pdev->dev);
591 if (!of_id)
592 return -EINVAL;
593
594 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
595 if (!ddata)
596 return -ENOMEM;
597
598 ddata->reg = dev_get_regmap(pdev->dev.parent, NULL);
599 if (!ddata->reg)
600 return -ENODEV;
601
602 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
603 if (!otg)
604 return -ENOMEM;
605
606 ddata->dev = &pdev->dev;
607 ddata->phy.dev = ddata->dev;
608 ddata->phy.label = "cpcap_usb_phy";
609 ddata->phy.otg = otg;
610 ddata->phy.type = USB_PHY_TYPE_USB2;
611 otg->set_host = cpcap_usb_phy_set_host;
612 otg->set_peripheral = cpcap_usb_phy_set_peripheral;
613 otg->usb_phy = &ddata->phy;
614 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
615 platform_set_drvdata(pdev, ddata);
616
617 ddata->vusb = devm_regulator_get(&pdev->dev, "vusb");
618 if (IS_ERR(ddata->vusb))
619 return PTR_ERR(ddata->vusb);
620
621 error = regulator_enable(ddata->vusb);
622 if (error)
623 return error;
624
625 generic_phy = devm_phy_create(ddata->dev, NULL, &ops);
626 if (IS_ERR(generic_phy)) {
627 error = PTR_ERR(generic_phy);
628 return PTR_ERR(generic_phy);
629 }
630
631 phy_set_drvdata(generic_phy, ddata);
632
633 phy_provider = devm_of_phy_provider_register(ddata->dev,
634 of_phy_simple_xlate);
635 if (IS_ERR(phy_provider))
636 return PTR_ERR(phy_provider);
637
638 error = cpcap_usb_init_optional_pins(ddata);
639 if (error)
640 return error;
641
642 cpcap_usb_init_optional_gpios(ddata);
643
644 error = cpcap_usb_init_iio(ddata);
645 if (error)
646 return error;
647
648 error = cpcap_usb_init_interrupts(pdev, ddata);
649 if (error)
650 return error;
651
652 usb_add_phy_dev(&ddata->phy);
653 atomic_set(&ddata->active, 1);
654 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));
655
656 return 0;
657 }
658
659 static int cpcap_usb_phy_remove(struct platform_device *pdev)
660 {
661 struct cpcap_phy_ddata *ddata = platform_get_drvdata(pdev);
662 int error;
663
664 atomic_set(&ddata->active, 0);
665 error = cpcap_usb_set_uart_mode(ddata);
666 if (error)
667 dev_err(ddata->dev, "could not set UART mode\n");
668
669 cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);
670
671 usb_remove_phy(&ddata->phy);
672 cancel_delayed_work_sync(&ddata->detect_work);
673 clk_unprepare(ddata->refclk);
674 regulator_disable(ddata->vusb);
675
676 return 0;
677 }
678
679 static struct platform_driver cpcap_usb_phy_driver = {
680 .probe = cpcap_usb_phy_probe,
681 .remove = cpcap_usb_phy_remove,
682 .driver = {
683 .name = "cpcap-usb-phy",
684 .of_match_table = of_match_ptr(cpcap_usb_phy_id_table),
685 },
686 };
687
688 module_platform_driver(cpcap_usb_phy_driver);
689
690 MODULE_ALIAS("platform:cpcap_usb");
691 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
692 MODULE_DESCRIPTION("CPCAP usb phy driver");
693 MODULE_LICENSE("GPL v2");