summaryrefslogtreecommitdiff
path: root/drivers/phy/motorola/phy-cpcap-usb.c
blob: 7cb020dd3423cbed1bcd7eb4f7402405a9d87d76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Motorola CPCAP PMIC USB PHY driver
 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
 *
 * Some parts based on earlier Motorola Linux kernel tree code in
 * board-mapphone-usb.c and cpcap-usb-det.c:
 * Copyright (C) 2007 - 2011 Motorola, Inc.
 */

#include <linux/atomic.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/iio/consumer.h>
#include <linux/pinctrl/consumer.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/slab.h>

#include <linux/gpio/consumer.h>
#include <linux/mfd/motorola-cpcap.h>
#include <linux/phy/omap_usb.h>
#include <linux/phy/phy.h>
#include <linux/regulator/consumer.h>
#include <linux/usb/musb.h>

/* CPCAP_REG_USBC1 register bits */
#define CPCAP_BIT_IDPULSE		BIT(15)
#define CPCAP_BIT_ID100KPU		BIT(14)
#define CPCAP_BIT_IDPUCNTRL		BIT(13)
#define CPCAP_BIT_IDPU			BIT(12)
#define CPCAP_BIT_IDPD			BIT(11)
#define CPCAP_BIT_VBUSCHRGTMR3		BIT(10)
#define CPCAP_BIT_VBUSCHRGTMR2		BIT(9)
#define CPCAP_BIT_VBUSCHRGTMR1		BIT(8)
#define CPCAP_BIT_VBUSCHRGTMR0		BIT(7)
#define CPCAP_BIT_VBUSPU		BIT(6)
#define CPCAP_BIT_VBUSPD		BIT(5)
#define CPCAP_BIT_DMPD			BIT(4)
#define CPCAP_BIT_DPPD			BIT(3)
#define CPCAP_BIT_DM1K5PU		BIT(2)
#define CPCAP_BIT_DP1K5PU		BIT(1)
#define CPCAP_BIT_DP150KPU		BIT(0)

/* CPCAP_REG_USBC2 register bits */
#define CPCAP_BIT_ZHSDRV1		BIT(15)
#define CPCAP_BIT_ZHSDRV0		BIT(14)
#define CPCAP_BIT_DPLLCLKREQ		BIT(13)
#define CPCAP_BIT_SE0CONN		BIT(12)
#define CPCAP_BIT_UARTTXTRI		BIT(11)
#define CPCAP_BIT_UARTSWAP		BIT(10)
#define CPCAP_BIT_UARTMUX1		BIT(9)
#define CPCAP_BIT_UARTMUX0		BIT(8)
#define CPCAP_BIT_ULPISTPLOW		BIT(7)
#define CPCAP_BIT_TXENPOL		BIT(6)
#define CPCAP_BIT_USBXCVREN		BIT(5)
#define CPCAP_BIT_USBCNTRL		BIT(4)
#define CPCAP_BIT_USBSUSPEND		BIT(3)
#define CPCAP_BIT_EMUMODE2		BIT(2)
#define CPCAP_BIT_EMUMODE1		BIT(1)
#define CPCAP_BIT_EMUMODE0		BIT(0)

/* CPCAP_REG_USBC3 register bits */
#define CPCAP_BIT_SPARE_898_15		BIT(15)
#define CPCAP_BIT_IHSTX03		BIT(14)
#define CPCAP_BIT_IHSTX02		BIT(13)
#define CPCAP_BIT_IHSTX01		BIT(12)
#define CPCAP_BIT_IHSTX0		BIT(11)
#define CPCAP_BIT_IDPU_SPI		BIT(10)
#define CPCAP_BIT_UNUSED_898_9		BIT(9)
#define CPCAP_BIT_VBUSSTBY_EN		BIT(8)
#define CPCAP_BIT_VBUSEN_SPI		BIT(7)
#define CPCAP_BIT_VBUSPU_SPI		BIT(6)
#define CPCAP_BIT_VBUSPD_SPI		BIT(5)
#define CPCAP_BIT_DMPD_SPI		BIT(4)
#define CPCAP_BIT_DPPD_SPI		BIT(3)
#define CPCAP_BIT_SUSPEND_SPI		BIT(2)
#define CPCAP_BIT_PU_SPI		BIT(1)
#define CPCAP_BIT_ULPI_SPI_SEL		BIT(0)

struct cpcap_usb_ints_state {
	bool id_ground;
	bool id_float;
	bool chrg_det;
	bool rvrs_chrg;
	bool vbusov;

	bool chrg_se1b;
	bool se0conn;
	bool rvrs_mode;
	bool chrgcurr1;
	bool vbusvld;
	bool sessvld;
	bool sessend;
	bool se1;

	bool battdetb;
	bool dm;
	bool dp;
};

enum cpcap_gpio_mode {
	CPCAP_DM_DP,
	CPCAP_MDM_RX_TX,
	CPCAP_UNKNOWN_DISABLED,	/* Seems to disable USB lines */
	CPCAP_OTG_DM_DP,
};

struct cpcap_phy_ddata {
	struct regmap *reg;
	struct device *dev;
	struct usb_phy phy;
	struct delayed_work detect_work;
	struct pinctrl *pins;
	struct pinctrl_state *pins_ulpi;
	struct pinctrl_state *pins_utmi;
	struct pinctrl_state *pins_uart;
	struct gpio_desc *gpio[2];
	struct iio_channel *vbus;
	struct iio_channel *id;
	struct regulator *vusb;
	atomic_t active;
	unsigned int vbus_provider:1;
	unsigned int docked:1;
};

static bool cpcap_usb_vbus_valid(struct cpcap_phy_ddata *ddata)
{
	int error, value = 0;

	error = iio_read_channel_processed(ddata->vbus, &value);
	if (error >= 0)
		return value > 3900;

	dev_err(ddata->dev, "error reading VBUS: %i\n", error);

	return false;
}

static int cpcap_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
{
	otg->host = host;
	if (!host)
		otg->state = OTG_STATE_UNDEFINED;

	return 0;
}

static int cpcap_usb_phy_set_peripheral(struct usb_otg *otg,
					struct usb_gadget *gadget)
{
	otg->gadget = gadget;
	if (!gadget)
		otg->state = OTG_STATE_UNDEFINED;

	return 0;
}

static const struct phy_ops ops = {
	.owner		= THIS_MODULE,
};

static int cpcap_phy_get_ints_state(struct cpcap_phy_ddata *ddata,
				    struct cpcap_usb_ints_state *s)
{
	int val, error;

	error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
	if (error)
		return error;

	s->id_ground = val & BIT(15);
	s->id_float = val & BIT(14);
	s->vbusov = val & BIT(11);

	error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
	if (error)
		return error;

	s->vbusvld = val & BIT(3);
	s->sessvld = val & BIT(2);
	s->sessend = val & BIT(1);
	s->se1 = val & BIT(0);

	error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
	if (error)
		return error;

	s->dm = val & BIT(1);
	s->dp = val & BIT(0);

	return 0;
}

static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata);
static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata);

static void cpcap_usb_try_musb_mailbox(struct cpcap_phy_ddata *ddata,
				       enum musb_vbus_id_status status)
{
	int error;

	error = musb_mailbox(status);
	if (!error)
		return;

	dev_dbg(ddata->dev, "%s: musb_mailbox failed: %i\n",
		__func__, error);
}

static void cpcap_usb_detect(struct work_struct *work)
{
	struct cpcap_phy_ddata *ddata;
	struct cpcap_usb_ints_state s;
	bool vbus = false;
	int error;

	ddata = container_of(work, struct cpcap_phy_ddata, detect_work.work);

	error = cpcap_phy_get_ints_state(ddata, &s);
	if (error)
		return;

	vbus = cpcap_usb_vbus_valid(ddata);

	/* We need to kick the VBUS as USB A-host */
	if (s.id_ground && ddata->vbus_provider) {
		dev_dbg(ddata->dev, "still in USB A-host mode, kicking VBUS\n");

		cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);

		error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
					   CPCAP_BIT_VBUSSTBY_EN |
					   CPCAP_BIT_VBUSEN_SPI,
					   CPCAP_BIT_VBUSEN_SPI);
		if (error)
			goto out_err;

		return;
	}

	if (vbus && s.id_ground && ddata->docked) {
		dev_dbg(ddata->dev, "still docked as A-host, signal ID down\n");

		cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);

		return;
	}

	/* No VBUS needed with docks */
	if (vbus && s.id_ground && !ddata->vbus_provider) {
		dev_dbg(ddata->dev, "connected to a dock\n");

		ddata->docked = true;

		error = cpcap_usb_set_usb_mode(ddata);
		if (error)
			goto out_err;

		cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);

		/*
		 * Force check state again after musb has reoriented,
		 * otherwise devices won't enumerate after loading PHY
		 * driver.
		 */
		schedule_delayed_work(&ddata->detect_work,
				      msecs_to_jiffies(1000));

		return;
	}

	if (s.id_ground && !ddata->docked) {
		dev_dbg(ddata->dev, "id ground, USB host mode\n");

		ddata->vbus_provider = true;

		error = cpcap_usb_set_usb_mode(ddata);
		if (error)
			goto out_err;

		cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);

		error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
					   CPCAP_BIT_VBUSSTBY_EN |
					   CPCAP_BIT_VBUSEN_SPI,
					   CPCAP_BIT_VBUSEN_SPI);
		if (error)
			goto out_err;

		return;
	}

	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
				   CPCAP_BIT_VBUSSTBY_EN |
				   CPCAP_BIT_VBUSEN_SPI, 0);
	if (error)
		goto out_err;

	vbus = cpcap_usb_vbus_valid(ddata);

	/* Otherwise assume we're connected to a USB host */
	if (vbus) {
		dev_dbg(ddata->dev, "connected to USB host\n");
		error = cpcap_usb_set_usb_mode(ddata);
		if (error)
			goto out_err;
		cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_VALID);

		return;
	}

	ddata->vbus_provider = false;
	ddata->docked = false;
	cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);

	/* Default to debug UART mode */
	error = cpcap_usb_set_uart_mode(ddata);
	if (error)
		goto out_err;

	dev_dbg(ddata->dev, "set UART mode\n");

	return;

out_err:
	dev_err(ddata->dev, "error setting cable state: %i\n", error);
}

static irqreturn_t cpcap_phy_irq_thread(int irq, void *data)
{
	struct cpcap_phy_ddata *ddata = data;

	if (!atomic_read(&ddata->active))
		return IRQ_NONE;

	schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));

	return IRQ_HANDLED;
}

static int cpcap_usb_init_irq(struct platform_device *pdev,
			      struct cpcap_phy_ddata *ddata,
			      const char *name)
{
	int irq, error;

	irq = platform_get_irq_byname(pdev, name);
	if (irq < 0)
		return -ENODEV;

	error = devm_request_threaded_irq(ddata->dev, irq, NULL,
					  cpcap_phy_irq_thread,
					  IRQF_SHARED |
					  IRQF_ONESHOT,
					  name, ddata);
	if (error) {
		dev_err(ddata->dev, "could not get irq %s: %i\n",
			name, error);

		return error;
	}

	return 0;
}

static const char * const cpcap_phy_irqs[] = {
	/* REG_INT_0 */
	"id_ground", "id_float",

	/* REG_INT1 */
	"se0conn", "vbusvld", "sessvld", "sessend", "se1",

	/* REG_INT_3 */
	"dm", "dp",
};

static int cpcap_usb_init_interrupts(struct platform_device *pdev,
				     struct cpcap_phy_ddata *ddata)
{
	int i, error;

	for (i = 0; i < ARRAY_SIZE(cpcap_phy_irqs); i++) {
		error = cpcap_usb_init_irq(pdev, ddata, cpcap_phy_irqs[i]);
		if (error)
			return error;
	}

	return 0;
}

/*
 * Optional pins and modes. At least Motorola mapphone devices
 * are using two GPIOs and dynamic pinctrl to multiplex PHY pins
 * to UART, ULPI or UTMI mode.
 */

static int cpcap_usb_gpio_set_mode(struct cpcap_phy_ddata *ddata,
				   enum cpcap_gpio_mode mode)
{
	if (!ddata->gpio[0] || !ddata->gpio[1])
		return 0;

	gpiod_set_value(ddata->gpio[0], mode & 1);
	gpiod_set_value(ddata->gpio[1], mode >> 1);

	return 0;
}

static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata)
{
	int error;

	/* Disable lines to prevent glitches from waking up mdm6600 */
	error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
	if (error)
		goto out_err;

	if (ddata->pins_uart) {
		error = pinctrl_select_state(ddata->pins, ddata->pins_uart);
		if (error)
			goto out_err;
	}

	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
				   CPCAP_BIT_VBUSPD,
				   CPCAP_BIT_VBUSPD);
	if (error)
		goto out_err;

	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
				   0xffff, CPCAP_BIT_UARTMUX0 |
				   CPCAP_BIT_EMUMODE0);
	if (error)
		goto out_err;

	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 0x7fff,
				   CPCAP_BIT_IDPU_SPI);
	if (error)
		goto out_err;

	/* Enable UART mode */
	error = cpcap_usb_gpio_set_mode(ddata, CPCAP_DM_DP);
	if (error)
		goto out_err;

	return 0;

out_err:
	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);

	return error;
}

static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata)
{
	int error;

	/* Disable lines to prevent glitches from waking up mdm6600 */
	error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
	if (error)
		return error;

	if (ddata->pins_utmi) {
		error = pinctrl_select_state(ddata->pins, ddata->pins_utmi);
		if (error) {
			dev_err(ddata->dev, "could not set usb mode: %i\n",
				error);

			return error;
		}
	}

	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
				   CPCAP_BIT_VBUSPD, 0);
	if (error)
		goto out_err;

	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
				   CPCAP_BIT_PU_SPI |
				   CPCAP_BIT_DMPD_SPI |
				   CPCAP_BIT_DPPD_SPI |
				   CPCAP_BIT_SUSPEND_SPI |
				   CPCAP_BIT_ULPI_SPI_SEL, 0);
	if (error)
		goto out_err;

	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
				   CPCAP_BIT_USBXCVREN,
				   CPCAP_BIT_USBXCVREN);
	if (error)
		goto out_err;

	/* Enable USB mode */
	error = cpcap_usb_gpio_set_mode(ddata, CPCAP_OTG_DM_DP);
	if (error)
		goto out_err;

	return 0;

out_err:
	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);

	return error;
}

static int cpcap_usb_init_optional_pins(struct cpcap_phy_ddata *ddata)
{
	ddata->pins = devm_pinctrl_get(ddata->dev);
	if (IS_ERR(ddata->pins)) {
		dev_info(ddata->dev, "default pins not configured: %ld\n",
			 PTR_ERR(ddata->pins));
		ddata->pins = NULL;

		return 0;
	}

	ddata->pins_ulpi = pinctrl_lookup_state(ddata->pins, "ulpi");
	if (IS_ERR(ddata->pins_ulpi)) {
		dev_info(ddata->dev, "ulpi pins not configured\n");
		ddata->pins_ulpi = NULL;
	}

	ddata->pins_utmi = pinctrl_lookup_state(ddata->pins, "utmi");
	if (IS_ERR(ddata->pins_utmi)) {
		dev_info(ddata->dev, "utmi pins not configured\n");
		ddata->pins_utmi = NULL;
	}

	ddata->pins_uart = pinctrl_lookup_state(ddata->pins, "uart");
	if (IS_ERR(ddata->pins_uart)) {
		dev_info(ddata->dev, "uart pins not configured\n");
		ddata->pins_uart = NULL;
	}

	if (ddata->pins_uart)
		return pinctrl_select_state(ddata->pins, ddata->pins_uart);

	return 0;
}

static void cpcap_usb_init_optional_gpios(struct cpcap_phy_ddata *ddata)
{
	int i;

	for (i = 0; i < 2; i++) {
		ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
						      i, GPIOD_OUT_HIGH);
		if (IS_ERR(ddata->gpio[i])) {
			dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
				 i, PTR_ERR(ddata->gpio[i]));
			ddata->gpio[i] = NULL;
		}
	}
}

static int cpcap_usb_init_iio(struct cpcap_phy_ddata *ddata)
{
	enum iio_chan_type type;
	int error;

	ddata->vbus = devm_iio_channel_get(ddata->dev, "vbus");
	if (IS_ERR(ddata->vbus)) {
		error = PTR_ERR(ddata->vbus);
		goto out_err;
	}

	if (!ddata->vbus->indio_dev) {
		error = -ENXIO;
		goto out_err;
	}

	error = iio_get_channel_type(ddata->vbus, &type);
	if (error < 0)
		goto out_err;

	if (type != IIO_VOLTAGE) {
		error = -EINVAL;
		goto out_err;
	}

	return 0;

out_err:
	dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
		error);

	return error;
}

#ifdef CONFIG_OF
static const struct of_device_id cpcap_usb_phy_id_table[] = {
	{
		.compatible = "motorola,cpcap-usb-phy",
	},
	{
		.compatible = "motorola,mapphone-cpcap-usb-phy",
	},
	{},
};
MODULE_DEVICE_TABLE(of, cpcap_usb_phy_id_table);
#endif

static int cpcap_usb_phy_probe(struct platform_device *pdev)
{
	struct cpcap_phy_ddata *ddata;
	struct phy *generic_phy;
	struct phy_provider *phy_provider;
	struct usb_otg *otg;
	int error;

	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
	if (!ddata)
		return -ENOMEM;

	ddata->reg = dev_get_regmap(pdev->dev.parent, NULL);
	if (!ddata->reg)
		return -ENODEV;

	otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
	if (!otg)
		return -ENOMEM;

	ddata->dev = &pdev->dev;
	ddata->phy.dev = ddata->dev;
	ddata->phy.label = "cpcap_usb_phy";
	ddata->phy.otg = otg;
	ddata->phy.type = USB_PHY_TYPE_USB2;
	otg->set_host = cpcap_usb_phy_set_host;
	otg->set_peripheral = cpcap_usb_phy_set_peripheral;
	otg->usb_phy = &ddata->phy;
	INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
	platform_set_drvdata(pdev, ddata);

	ddata->vusb = devm_regulator_get(&pdev->dev, "vusb");
	if (IS_ERR(ddata->vusb))
		return PTR_ERR(ddata->vusb);

	error = regulator_enable(ddata->vusb);
	if (error)
		return error;

	generic_phy = devm_phy_create(ddata->dev, NULL, &ops);
	if (IS_ERR(generic_phy)) {
		error = PTR_ERR(generic_phy);
		goto out_reg_disable;
	}

	phy_set_drvdata(generic_phy, ddata);

	phy_provider = devm_of_phy_provider_register(ddata->dev,
						     of_phy_simple_xlate);
	if (IS_ERR(phy_provider)) {
		error = PTR_ERR(phy_provider);
		goto out_reg_disable;
	}

	error = cpcap_usb_init_optional_pins(ddata);
	if (error)
		goto out_reg_disable;

	cpcap_usb_init_optional_gpios(ddata);

	error = cpcap_usb_init_iio(ddata);
	if (error)
		goto out_reg_disable;

	error = cpcap_usb_init_interrupts(pdev, ddata);
	if (error)
		goto out_reg_disable;

	usb_add_phy_dev(&ddata->phy);
	atomic_set(&ddata->active, 1);
	schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));

	return 0;

out_reg_disable:
	regulator_disable(ddata->vusb);

	return error;
}

static void cpcap_usb_phy_remove(struct platform_device *pdev)
{
	struct cpcap_phy_ddata *ddata = platform_get_drvdata(pdev);
	int error;

	atomic_set(&ddata->active, 0);
	error = cpcap_usb_set_uart_mode(ddata);
	if (error)
		dev_err(ddata->dev, "could not set UART mode\n");

	cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);

	usb_remove_phy(&ddata->phy);
	cancel_delayed_work_sync(&ddata->detect_work);
	regulator_disable(ddata->vusb);
}

static struct platform_driver cpcap_usb_phy_driver = {
	.probe		= cpcap_usb_phy_probe,
	.remove		= cpcap_usb_phy_remove,
	.driver		= {
		.name	= "cpcap-usb-phy",
		.of_match_table = of_match_ptr(cpcap_usb_phy_id_table),
	},
};

module_platform_driver(cpcap_usb_phy_driver);

MODULE_ALIAS("platform:cpcap_usb");
MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
MODULE_DESCRIPTION("CPCAP usb phy driver");
MODULE_LICENSE("GPL v2");