1 /*
2  * Motorola CPCAP PMIC USB PHY driver
3  * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
4  *
5  * Some parts based on earlier Motorola Linux kernel tree code in
6  * board-mapphone-usb.c and cpcap-usb-det.c:
7  * Copyright (C) 2007 - 2011 Motorola, Inc.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation version 2.
12  *
13  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
14  * kind, whether express or implied; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
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 /* CPCAP_REG_USBC1 register bits */
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 /* CPCAP_REG_USBC2 register bits */
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 /* CPCAP_REG_USBC3 register bits */
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,	/* Seems to disable USB lines */
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 	unsigned int vbus_provider:1;
138 	unsigned int docked:1;
139 };
140 
141 static bool cpcap_usb_vbus_valid(struct cpcap_phy_ddata *ddata)
142 {
143 	int error, value = 0;
144 
145 	error = iio_read_channel_processed(ddata->vbus, &value);
146 	if (error >= 0)
147 		return value > 3900 ? true : false;
148 
149 	dev_err(ddata->dev, "error reading VBUS: %i\n", error);
150 
151 	return false;
152 }
153 
154 static int cpcap_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
155 {
156 	otg->host = host;
157 	if (!host)
158 		otg->state = OTG_STATE_UNDEFINED;
159 
160 	return 0;
161 }
162 
163 static int cpcap_usb_phy_set_peripheral(struct usb_otg *otg,
164 					struct usb_gadget *gadget)
165 {
166 	otg->gadget = gadget;
167 	if (!gadget)
168 		otg->state = OTG_STATE_UNDEFINED;
169 
170 	return 0;
171 }
172 
173 static const struct phy_ops ops = {
174 	.owner		= THIS_MODULE,
175 };
176 
177 static int cpcap_phy_get_ints_state(struct cpcap_phy_ddata *ddata,
178 				    struct cpcap_usb_ints_state *s)
179 {
180 	int val, error;
181 
182 	error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
183 	if (error)
184 		return error;
185 
186 	s->id_ground = val & BIT(15);
187 	s->id_float = val & BIT(14);
188 	s->vbusov = val & BIT(11);
189 
190 	error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
191 	if (error)
192 		return error;
193 
194 	s->vbusvld = val & BIT(3);
195 	s->sessvld = val & BIT(2);
196 	s->sessend = val & BIT(1);
197 	s->se1 = val & BIT(0);
198 
199 	error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
200 	if (error)
201 		return error;
202 
203 	s->dm = val & BIT(1);
204 	s->dp = val & BIT(0);
205 
206 	return 0;
207 }
208 
209 static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata);
210 static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata);
211 
212 static void cpcap_usb_try_musb_mailbox(struct cpcap_phy_ddata *ddata,
213 				       enum musb_vbus_id_status status)
214 {
215 	int error;
216 
217 	error = musb_mailbox(status);
218 	if (!error)
219 		return;
220 
221 	dev_dbg(ddata->dev, "%s: musb_mailbox failed: %i\n",
222 		__func__, error);
223 }
224 
225 static void cpcap_usb_detect(struct work_struct *work)
226 {
227 	struct cpcap_phy_ddata *ddata;
228 	struct cpcap_usb_ints_state s;
229 	bool vbus = false;
230 	int error;
231 
232 	ddata = container_of(work, struct cpcap_phy_ddata, detect_work.work);
233 
234 	error = cpcap_phy_get_ints_state(ddata, &s);
235 	if (error)
236 		return;
237 
238 	vbus = cpcap_usb_vbus_valid(ddata);
239 
240 	/* We need to kick the VBUS as USB A-host */
241 	if (s.id_ground && ddata->vbus_provider) {
242 		dev_dbg(ddata->dev, "still in USB A-host mode, kicking VBUS\n");
243 
244 		cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
245 
246 		error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
247 					   CPCAP_BIT_VBUSSTBY_EN |
248 					   CPCAP_BIT_VBUSEN_SPI,
249 					   CPCAP_BIT_VBUSEN_SPI);
250 		if (error)
251 			goto out_err;
252 
253 		return;
254 	}
255 
256 	if (vbus && s.id_ground && ddata->docked) {
257 		dev_dbg(ddata->dev, "still docked as A-host, signal ID down\n");
258 
259 		cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
260 
261 		return;
262 	}
263 
264 	/* No VBUS needed with docks */
265 	if (vbus && s.id_ground && !ddata->vbus_provider) {
266 		dev_dbg(ddata->dev, "connected to a dock\n");
267 
268 		ddata->docked = true;
269 
270 		error = cpcap_usb_set_usb_mode(ddata);
271 		if (error)
272 			goto out_err;
273 
274 		cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
275 
276 		/*
277 		 * Force check state again after musb has reoriented,
278 		 * otherwise devices won't enumerate after loading PHY
279 		 * driver.
280 		 */
281 		schedule_delayed_work(&ddata->detect_work,
282 				      msecs_to_jiffies(1000));
283 
284 		return;
285 	}
286 
287 	if (s.id_ground && !ddata->docked) {
288 		dev_dbg(ddata->dev, "id ground, USB host mode\n");
289 
290 		ddata->vbus_provider = true;
291 
292 		error = cpcap_usb_set_usb_mode(ddata);
293 		if (error)
294 			goto out_err;
295 
296 		cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
297 
298 		error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
299 					   CPCAP_BIT_VBUSSTBY_EN |
300 					   CPCAP_BIT_VBUSEN_SPI,
301 					   CPCAP_BIT_VBUSEN_SPI);
302 		if (error)
303 			goto out_err;
304 
305 		return;
306 	}
307 
308 	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
309 				   CPCAP_BIT_VBUSSTBY_EN |
310 				   CPCAP_BIT_VBUSEN_SPI, 0);
311 	if (error)
312 		goto out_err;
313 
314 	vbus = cpcap_usb_vbus_valid(ddata);
315 
316 	/* Otherwise assume we're connected to a USB host */
317 	if (vbus) {
318 		dev_dbg(ddata->dev, "connected to USB host\n");
319 		error = cpcap_usb_set_usb_mode(ddata);
320 		if (error)
321 			goto out_err;
322 		cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_VALID);
323 
324 		return;
325 	}
326 
327 	ddata->vbus_provider = false;
328 	ddata->docked = false;
329 	cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);
330 
331 	/* Default to debug UART mode */
332 	error = cpcap_usb_set_uart_mode(ddata);
333 	if (error)
334 		goto out_err;
335 
336 	dev_dbg(ddata->dev, "set UART mode\n");
337 
338 	return;
339 
340 out_err:
341 	dev_err(ddata->dev, "error setting cable state: %i\n", error);
342 }
343 
344 static irqreturn_t cpcap_phy_irq_thread(int irq, void *data)
345 {
346 	struct cpcap_phy_ddata *ddata = data;
347 
348 	if (!atomic_read(&ddata->active))
349 		return IRQ_NONE;
350 
351 	schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));
352 
353 	return IRQ_HANDLED;
354 }
355 
356 static int cpcap_usb_init_irq(struct platform_device *pdev,
357 			      struct cpcap_phy_ddata *ddata,
358 			      const char *name)
359 {
360 	int irq, error;
361 
362 	irq = platform_get_irq_byname(pdev, name);
363 	if (irq < 0)
364 		return -ENODEV;
365 
366 	error = devm_request_threaded_irq(ddata->dev, irq, NULL,
367 					  cpcap_phy_irq_thread,
368 					  IRQF_SHARED,
369 					  name, ddata);
370 	if (error) {
371 		dev_err(ddata->dev, "could not get irq %s: %i\n",
372 			name, error);
373 
374 		return error;
375 	}
376 
377 	return 0;
378 }
379 
380 static const char * const cpcap_phy_irqs[] = {
381 	/* REG_INT_0 */
382 	"id_ground", "id_float",
383 
384 	/* REG_INT1 */
385 	"se0conn", "vbusvld", "sessvld", "sessend", "se1",
386 
387 	/* REG_INT_3 */
388 	"dm", "dp",
389 };
390 
391 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
392 				     struct cpcap_phy_ddata *ddata)
393 {
394 	int i, error;
395 
396 	for (i = 0; i < ARRAY_SIZE(cpcap_phy_irqs); i++) {
397 		error = cpcap_usb_init_irq(pdev, ddata, cpcap_phy_irqs[i]);
398 		if (error)
399 			return error;
400 	}
401 
402 	return 0;
403 }
404 
405 /*
406  * Optional pins and modes. At least Motorola mapphone devices
407  * are using two GPIOs and dynamic pinctrl to multiplex PHY pins
408  * to UART, ULPI or UTMI mode.
409  */
410 
411 static int cpcap_usb_gpio_set_mode(struct cpcap_phy_ddata *ddata,
412 				   enum cpcap_gpio_mode mode)
413 {
414 	if (!ddata->gpio[0] || !ddata->gpio[1])
415 		return 0;
416 
417 	gpiod_set_value(ddata->gpio[0], mode & 1);
418 	gpiod_set_value(ddata->gpio[1], mode >> 1);
419 
420 	return 0;
421 }
422 
423 static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata)
424 {
425 	int error;
426 
427 	/* Disable lines to prevent glitches from waking up mdm6600 */
428 	error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
429 	if (error)
430 		goto out_err;
431 
432 	if (ddata->pins_uart) {
433 		error = pinctrl_select_state(ddata->pins, ddata->pins_uart);
434 		if (error)
435 			goto out_err;
436 	}
437 
438 	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
439 				   CPCAP_BIT_VBUSPD,
440 				   CPCAP_BIT_VBUSPD);
441 	if (error)
442 		goto out_err;
443 
444 	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
445 				   0xffff, CPCAP_BIT_UARTMUX0 |
446 				   CPCAP_BIT_EMUMODE0);
447 	if (error)
448 		goto out_err;
449 
450 	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 0x7fff,
451 				   CPCAP_BIT_IDPU_SPI);
452 	if (error)
453 		goto out_err;
454 
455 	/* Enable UART mode */
456 	error = cpcap_usb_gpio_set_mode(ddata, CPCAP_DM_DP);
457 	if (error)
458 		goto out_err;
459 
460 	return 0;
461 
462 out_err:
463 	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
464 
465 	return error;
466 }
467 
468 static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata)
469 {
470 	int error;
471 
472 	/* Disable lines to prevent glitches from waking up mdm6600 */
473 	error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
474 	if (error)
475 		return error;
476 
477 	if (ddata->pins_utmi) {
478 		error = pinctrl_select_state(ddata->pins, ddata->pins_utmi);
479 		if (error) {
480 			dev_err(ddata->dev, "could not set usb mode: %i\n",
481 				error);
482 
483 			return error;
484 		}
485 	}
486 
487 	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
488 				   CPCAP_BIT_VBUSPD, 0);
489 	if (error)
490 		goto out_err;
491 
492 	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
493 				   CPCAP_BIT_PU_SPI |
494 				   CPCAP_BIT_DMPD_SPI |
495 				   CPCAP_BIT_DPPD_SPI |
496 				   CPCAP_BIT_SUSPEND_SPI |
497 				   CPCAP_BIT_ULPI_SPI_SEL, 0);
498 	if (error)
499 		goto out_err;
500 
501 	error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
502 				   CPCAP_BIT_USBXCVREN,
503 				   CPCAP_BIT_USBXCVREN);
504 	if (error)
505 		goto out_err;
506 
507 	/* Enable USB mode */
508 	error = cpcap_usb_gpio_set_mode(ddata, CPCAP_OTG_DM_DP);
509 	if (error)
510 		goto out_err;
511 
512 	return 0;
513 
514 out_err:
515 	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
516 
517 	return error;
518 }
519 
520 static int cpcap_usb_init_optional_pins(struct cpcap_phy_ddata *ddata)
521 {
522 	ddata->pins = devm_pinctrl_get(ddata->dev);
523 	if (IS_ERR(ddata->pins)) {
524 		dev_info(ddata->dev, "default pins not configured: %ld\n",
525 			 PTR_ERR(ddata->pins));
526 		ddata->pins = NULL;
527 
528 		return 0;
529 	}
530 
531 	ddata->pins_ulpi = pinctrl_lookup_state(ddata->pins, "ulpi");
532 	if (IS_ERR(ddata->pins_ulpi)) {
533 		dev_info(ddata->dev, "ulpi pins not configured\n");
534 		ddata->pins_ulpi = NULL;
535 	}
536 
537 	ddata->pins_utmi = pinctrl_lookup_state(ddata->pins, "utmi");
538 	if (IS_ERR(ddata->pins_utmi)) {
539 		dev_info(ddata->dev, "utmi pins not configured\n");
540 		ddata->pins_utmi = NULL;
541 	}
542 
543 	ddata->pins_uart = pinctrl_lookup_state(ddata->pins, "uart");
544 	if (IS_ERR(ddata->pins_uart)) {
545 		dev_info(ddata->dev, "uart pins not configured\n");
546 		ddata->pins_uart = NULL;
547 	}
548 
549 	if (ddata->pins_uart)
550 		return pinctrl_select_state(ddata->pins, ddata->pins_uart);
551 
552 	return 0;
553 }
554 
555 static void cpcap_usb_init_optional_gpios(struct cpcap_phy_ddata *ddata)
556 {
557 	int i;
558 
559 	for (i = 0; i < 2; i++) {
560 		ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
561 						      i, GPIOD_OUT_HIGH);
562 		if (IS_ERR(ddata->gpio[i])) {
563 			dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
564 				 i, PTR_ERR(ddata->gpio[i]));
565 			ddata->gpio[i] = NULL;
566 		}
567 	}
568 }
569 
570 static int cpcap_usb_init_iio(struct cpcap_phy_ddata *ddata)
571 {
572 	enum iio_chan_type type;
573 	int error;
574 
575 	ddata->vbus = devm_iio_channel_get(ddata->dev, "vbus");
576 	if (IS_ERR(ddata->vbus)) {
577 		error = PTR_ERR(ddata->vbus);
578 		goto out_err;
579 	}
580 
581 	if (!ddata->vbus->indio_dev) {
582 		error = -ENXIO;
583 		goto out_err;
584 	}
585 
586 	error = iio_get_channel_type(ddata->vbus, &type);
587 	if (error < 0)
588 		goto out_err;
589 
590 	if (type != IIO_VOLTAGE) {
591 		error = -EINVAL;
592 		goto out_err;
593 	}
594 
595 	return 0;
596 
597 out_err:
598 	dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
599 		error);
600 
601 	return error;
602 }
603 
604 #ifdef CONFIG_OF
605 static const struct of_device_id cpcap_usb_phy_id_table[] = {
606 	{
607 		.compatible = "motorola,cpcap-usb-phy",
608 	},
609 	{
610 		.compatible = "motorola,mapphone-cpcap-usb-phy",
611 	},
612 	{},
613 };
614 MODULE_DEVICE_TABLE(of, cpcap_usb_phy_id_table);
615 #endif
616 
617 static int cpcap_usb_phy_probe(struct platform_device *pdev)
618 {
619 	struct cpcap_phy_ddata *ddata;
620 	struct phy *generic_phy;
621 	struct phy_provider *phy_provider;
622 	struct usb_otg *otg;
623 	const struct of_device_id *of_id;
624 	int error;
625 
626 	of_id = of_match_device(of_match_ptr(cpcap_usb_phy_id_table),
627 				&pdev->dev);
628 	if (!of_id)
629 		return -EINVAL;
630 
631 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
632 	if (!ddata)
633 		return -ENOMEM;
634 
635 	ddata->reg = dev_get_regmap(pdev->dev.parent, NULL);
636 	if (!ddata->reg)
637 		return -ENODEV;
638 
639 	otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
640 	if (!otg)
641 		return -ENOMEM;
642 
643 	ddata->dev = &pdev->dev;
644 	ddata->phy.dev = ddata->dev;
645 	ddata->phy.label = "cpcap_usb_phy";
646 	ddata->phy.otg = otg;
647 	ddata->phy.type = USB_PHY_TYPE_USB2;
648 	otg->set_host = cpcap_usb_phy_set_host;
649 	otg->set_peripheral = cpcap_usb_phy_set_peripheral;
650 	otg->usb_phy = &ddata->phy;
651 	INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
652 	platform_set_drvdata(pdev, ddata);
653 
654 	ddata->vusb = devm_regulator_get(&pdev->dev, "vusb");
655 	if (IS_ERR(ddata->vusb))
656 		return PTR_ERR(ddata->vusb);
657 
658 	error = regulator_enable(ddata->vusb);
659 	if (error)
660 		return error;
661 
662 	generic_phy = devm_phy_create(ddata->dev, NULL, &ops);
663 	if (IS_ERR(generic_phy)) {
664 		error = PTR_ERR(generic_phy);
665 		return PTR_ERR(generic_phy);
666 	}
667 
668 	phy_set_drvdata(generic_phy, ddata);
669 
670 	phy_provider = devm_of_phy_provider_register(ddata->dev,
671 						     of_phy_simple_xlate);
672 	if (IS_ERR(phy_provider))
673 		return PTR_ERR(phy_provider);
674 
675 	error = cpcap_usb_init_optional_pins(ddata);
676 	if (error)
677 		return error;
678 
679 	cpcap_usb_init_optional_gpios(ddata);
680 
681 	error = cpcap_usb_init_iio(ddata);
682 	if (error)
683 		return error;
684 
685 	error = cpcap_usb_init_interrupts(pdev, ddata);
686 	if (error)
687 		return error;
688 
689 	usb_add_phy_dev(&ddata->phy);
690 	atomic_set(&ddata->active, 1);
691 	schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));
692 
693 	return 0;
694 }
695 
696 static int cpcap_usb_phy_remove(struct platform_device *pdev)
697 {
698 	struct cpcap_phy_ddata *ddata = platform_get_drvdata(pdev);
699 	int error;
700 
701 	atomic_set(&ddata->active, 0);
702 	error = cpcap_usb_set_uart_mode(ddata);
703 	if (error)
704 		dev_err(ddata->dev, "could not set UART mode\n");
705 
706 	cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);
707 
708 	usb_remove_phy(&ddata->phy);
709 	cancel_delayed_work_sync(&ddata->detect_work);
710 	clk_unprepare(ddata->refclk);
711 	regulator_disable(ddata->vusb);
712 
713 	return 0;
714 }
715 
716 static struct platform_driver cpcap_usb_phy_driver = {
717 	.probe		= cpcap_usb_phy_probe,
718 	.remove		= cpcap_usb_phy_remove,
719 	.driver		= {
720 		.name	= "cpcap-usb-phy",
721 		.of_match_table = of_match_ptr(cpcap_usb_phy_id_table),
722 	},
723 };
724 
725 module_platform_driver(cpcap_usb_phy_driver);
726 
727 MODULE_ALIAS("platform:cpcap_usb");
728 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
729 MODULE_DESCRIPTION("CPCAP usb phy driver");
730 MODULE_LICENSE("GPL v2");
731