]>
Commit | Line | Data |
---|---|---|
6d6ce40f TL |
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, | |
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 | ||
044e1a93 TL |
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 | ||
6d6ce40f TL |
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 | ||
044e1a93 | 242 | cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND); |
6d6ce40f TL |
243 | |
244 | error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, | |
245 | CPCAP_BIT_VBUSSTBY_EN, | |
246 | CPCAP_BIT_VBUSSTBY_EN); | |
247 | if (error) | |
248 | goto out_err; | |
249 | ||
250 | return; | |
251 | } | |
252 | ||
253 | error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, | |
254 | CPCAP_BIT_VBUSSTBY_EN, 0); | |
255 | if (error) | |
256 | goto out_err; | |
257 | ||
258 | vbus = cpcap_usb_vbus_valid(ddata); | |
259 | ||
260 | if (vbus) { | |
261 | /* Are we connected to a docking station with vbus? */ | |
262 | if (s.id_ground) { | |
263 | dev_dbg(ddata->dev, "connected to a dock\n"); | |
264 | ||
265 | /* No VBUS needed with docks */ | |
266 | error = cpcap_usb_set_usb_mode(ddata); | |
267 | if (error) | |
268 | goto out_err; | |
044e1a93 | 269 | cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND); |
6d6ce40f TL |
270 | |
271 | return; | |
272 | } | |
273 | ||
274 | /* Otherwise assume we're connected to a USB host */ | |
275 | dev_dbg(ddata->dev, "connected to USB host\n"); | |
276 | error = cpcap_usb_set_usb_mode(ddata); | |
277 | if (error) | |
278 | goto out_err; | |
044e1a93 | 279 | cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_VALID); |
6d6ce40f TL |
280 | |
281 | return; | |
282 | } | |
283 | ||
c1fe9829 TL |
284 | cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF); |
285 | ||
6d6ce40f TL |
286 | /* Default to debug UART mode */ |
287 | error = cpcap_usb_set_uart_mode(ddata); | |
288 | if (error) | |
289 | goto out_err; | |
290 | ||
6d6ce40f TL |
291 | dev_dbg(ddata->dev, "set UART mode\n"); |
292 | ||
293 | return; | |
294 | ||
295 | out_err: | |
296 | dev_err(ddata->dev, "error setting cable state: %i\n", error); | |
297 | } | |
298 | ||
299 | static irqreturn_t cpcap_phy_irq_thread(int irq, void *data) | |
300 | { | |
301 | struct cpcap_phy_ddata *ddata = data; | |
302 | ||
303 | if (!atomic_read(&ddata->active)) | |
304 | return IRQ_NONE; | |
305 | ||
306 | schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1)); | |
307 | ||
308 | return IRQ_HANDLED; | |
309 | } | |
310 | ||
311 | static int cpcap_usb_init_irq(struct platform_device *pdev, | |
312 | struct cpcap_phy_ddata *ddata, | |
313 | const char *name) | |
314 | { | |
315 | int irq, error; | |
316 | ||
317 | irq = platform_get_irq_byname(pdev, name); | |
e796cc6a | 318 | if (irq < 0) |
6d6ce40f TL |
319 | return -ENODEV; |
320 | ||
321 | error = devm_request_threaded_irq(ddata->dev, irq, NULL, | |
322 | cpcap_phy_irq_thread, | |
323 | IRQF_SHARED, | |
324 | name, ddata); | |
325 | if (error) { | |
326 | dev_err(ddata->dev, "could not get irq %s: %i\n", | |
327 | name, error); | |
328 | ||
329 | return error; | |
330 | } | |
331 | ||
332 | return 0; | |
333 | } | |
334 | ||
335 | static const char * const cpcap_phy_irqs[] = { | |
336 | /* REG_INT_0 */ | |
337 | "id_ground", "id_float", | |
338 | ||
339 | /* REG_INT1 */ | |
340 | "se0conn", "vbusvld", "sessvld", "sessend", "se1", | |
341 | ||
342 | /* REG_INT_3 */ | |
343 | "dm", "dp", | |
344 | }; | |
345 | ||
346 | static int cpcap_usb_init_interrupts(struct platform_device *pdev, | |
347 | struct cpcap_phy_ddata *ddata) | |
348 | { | |
349 | int i, error; | |
350 | ||
351 | for (i = 0; i < ARRAY_SIZE(cpcap_phy_irqs); i++) { | |
352 | error = cpcap_usb_init_irq(pdev, ddata, cpcap_phy_irqs[i]); | |
353 | if (error) | |
354 | return error; | |
355 | } | |
356 | ||
357 | return 0; | |
358 | } | |
359 | ||
360 | /* | |
361 | * Optional pins and modes. At least Motorola mapphone devices | |
362 | * are using two GPIOs and dynamic pinctrl to multiplex PHY pins | |
363 | * to UART, ULPI or UTMI mode. | |
364 | */ | |
365 | ||
366 | static int cpcap_usb_gpio_set_mode(struct cpcap_phy_ddata *ddata, | |
367 | enum cpcap_gpio_mode mode) | |
368 | { | |
369 | if (!ddata->gpio[0] || !ddata->gpio[1]) | |
370 | return 0; | |
371 | ||
372 | gpiod_set_value(ddata->gpio[0], mode & 1); | |
373 | gpiod_set_value(ddata->gpio[1], mode >> 1); | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
378 | static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata) | |
379 | { | |
380 | int error; | |
381 | ||
382 | error = cpcap_usb_gpio_set_mode(ddata, CPCAP_DM_DP); | |
383 | if (error) | |
384 | goto out_err; | |
385 | ||
386 | if (ddata->pins_uart) { | |
387 | error = pinctrl_select_state(ddata->pins, ddata->pins_uart); | |
388 | if (error) | |
389 | goto out_err; | |
390 | } | |
391 | ||
392 | error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1, | |
393 | CPCAP_BIT_VBUSPD, | |
394 | CPCAP_BIT_VBUSPD); | |
395 | if (error) | |
396 | goto out_err; | |
397 | ||
398 | error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2, | |
399 | 0xffff, CPCAP_BIT_UARTMUX0 | | |
400 | CPCAP_BIT_EMUMODE0); | |
401 | if (error) | |
402 | goto out_err; | |
403 | ||
404 | error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 0x7fff, | |
405 | CPCAP_BIT_IDPU_SPI); | |
406 | if (error) | |
407 | goto out_err; | |
408 | ||
409 | return 0; | |
410 | ||
411 | out_err: | |
412 | dev_err(ddata->dev, "%s failed with %i\n", __func__, error); | |
413 | ||
414 | return error; | |
415 | } | |
416 | ||
417 | static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata) | |
418 | { | |
419 | int error; | |
420 | ||
421 | error = cpcap_usb_gpio_set_mode(ddata, CPCAP_OTG_DM_DP); | |
422 | if (error) | |
423 | return error; | |
424 | ||
425 | if (ddata->pins_utmi) { | |
426 | error = pinctrl_select_state(ddata->pins, ddata->pins_utmi); | |
427 | if (error) { | |
428 | dev_err(ddata->dev, "could not set usb mode: %i\n", | |
429 | error); | |
430 | ||
431 | return error; | |
432 | } | |
433 | } | |
434 | ||
435 | error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1, | |
436 | CPCAP_BIT_VBUSPD, 0); | |
437 | if (error) | |
438 | goto out_err; | |
439 | ||
440 | error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2, | |
441 | CPCAP_BIT_USBXCVREN, | |
442 | CPCAP_BIT_USBXCVREN); | |
443 | if (error) | |
444 | goto out_err; | |
445 | ||
446 | error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, | |
447 | CPCAP_BIT_PU_SPI | | |
448 | CPCAP_BIT_DMPD_SPI | | |
449 | CPCAP_BIT_DPPD_SPI | | |
450 | CPCAP_BIT_SUSPEND_SPI | | |
451 | CPCAP_BIT_ULPI_SPI_SEL, 0); | |
452 | if (error) | |
453 | goto out_err; | |
454 | ||
455 | error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2, | |
456 | CPCAP_BIT_USBXCVREN, | |
457 | CPCAP_BIT_USBXCVREN); | |
458 | if (error) | |
459 | goto out_err; | |
460 | ||
461 | return 0; | |
462 | ||
463 | out_err: | |
464 | dev_err(ddata->dev, "%s failed with %i\n", __func__, error); | |
465 | ||
466 | return error; | |
467 | } | |
468 | ||
469 | static int cpcap_usb_init_optional_pins(struct cpcap_phy_ddata *ddata) | |
470 | { | |
471 | ddata->pins = devm_pinctrl_get(ddata->dev); | |
472 | if (IS_ERR(ddata->pins)) { | |
473 | dev_info(ddata->dev, "default pins not configured: %ld\n", | |
474 | PTR_ERR(ddata->pins)); | |
475 | ddata->pins = NULL; | |
259a2400 TL |
476 | |
477 | return 0; | |
6d6ce40f TL |
478 | } |
479 | ||
480 | ddata->pins_ulpi = pinctrl_lookup_state(ddata->pins, "ulpi"); | |
481 | if (IS_ERR(ddata->pins_ulpi)) { | |
482 | dev_info(ddata->dev, "ulpi pins not configured\n"); | |
483 | ddata->pins_ulpi = NULL; | |
484 | } | |
485 | ||
486 | ddata->pins_utmi = pinctrl_lookup_state(ddata->pins, "utmi"); | |
487 | if (IS_ERR(ddata->pins_utmi)) { | |
488 | dev_info(ddata->dev, "utmi pins not configured\n"); | |
489 | ddata->pins_utmi = NULL; | |
490 | } | |
491 | ||
492 | ddata->pins_uart = pinctrl_lookup_state(ddata->pins, "uart"); | |
493 | if (IS_ERR(ddata->pins_uart)) { | |
494 | dev_info(ddata->dev, "uart pins not configured\n"); | |
495 | ddata->pins_uart = NULL; | |
496 | } | |
497 | ||
498 | if (ddata->pins_uart) | |
499 | return pinctrl_select_state(ddata->pins, ddata->pins_uart); | |
500 | ||
501 | return 0; | |
502 | } | |
503 | ||
504 | static void cpcap_usb_init_optional_gpios(struct cpcap_phy_ddata *ddata) | |
505 | { | |
506 | int i; | |
507 | ||
508 | for (i = 0; i < 2; i++) { | |
509 | ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode", | |
510 | i, GPIOD_OUT_HIGH); | |
511 | if (IS_ERR(ddata->gpio[i])) { | |
512 | dev_info(ddata->dev, "no mode change GPIO%i: %li\n", | |
513 | i, PTR_ERR(ddata->gpio[i])); | |
aea430ee | 514 | ddata->gpio[i] = NULL; |
6d6ce40f TL |
515 | } |
516 | } | |
517 | } | |
518 | ||
519 | static int cpcap_usb_init_iio(struct cpcap_phy_ddata *ddata) | |
520 | { | |
521 | enum iio_chan_type type; | |
522 | int error; | |
523 | ||
524 | ddata->vbus = devm_iio_channel_get(ddata->dev, "vbus"); | |
525 | if (IS_ERR(ddata->vbus)) { | |
526 | error = PTR_ERR(ddata->vbus); | |
527 | goto out_err; | |
528 | } | |
529 | ||
530 | if (!ddata->vbus->indio_dev) { | |
531 | error = -ENXIO; | |
532 | goto out_err; | |
533 | } | |
534 | ||
535 | error = iio_get_channel_type(ddata->vbus, &type); | |
536 | if (error < 0) | |
537 | goto out_err; | |
538 | ||
539 | if (type != IIO_VOLTAGE) { | |
540 | error = -EINVAL; | |
541 | goto out_err; | |
542 | } | |
543 | ||
544 | return 0; | |
545 | ||
546 | out_err: | |
547 | dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n", | |
548 | error); | |
549 | ||
550 | return error; | |
551 | } | |
552 | ||
553 | #ifdef CONFIG_OF | |
554 | static const struct of_device_id cpcap_usb_phy_id_table[] = { | |
555 | { | |
556 | .compatible = "motorola,cpcap-usb-phy", | |
557 | }, | |
558 | { | |
559 | .compatible = "motorola,mapphone-cpcap-usb-phy", | |
560 | }, | |
561 | {}, | |
562 | }; | |
563 | MODULE_DEVICE_TABLE(of, cpcap_usb_phy_id_table); | |
564 | #endif | |
565 | ||
566 | static int cpcap_usb_phy_probe(struct platform_device *pdev) | |
567 | { | |
568 | struct cpcap_phy_ddata *ddata; | |
569 | struct phy *generic_phy; | |
570 | struct phy_provider *phy_provider; | |
571 | struct usb_otg *otg; | |
572 | const struct of_device_id *of_id; | |
573 | int error; | |
574 | ||
575 | of_id = of_match_device(of_match_ptr(cpcap_usb_phy_id_table), | |
576 | &pdev->dev); | |
577 | if (!of_id) | |
578 | return -EINVAL; | |
579 | ||
580 | ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); | |
581 | if (!ddata) | |
582 | return -ENOMEM; | |
583 | ||
584 | ddata->reg = dev_get_regmap(pdev->dev.parent, NULL); | |
585 | if (!ddata->reg) | |
586 | return -ENODEV; | |
587 | ||
588 | otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL); | |
589 | if (!otg) | |
590 | return -ENOMEM; | |
591 | ||
592 | ddata->dev = &pdev->dev; | |
593 | ddata->phy.dev = ddata->dev; | |
594 | ddata->phy.label = "cpcap_usb_phy"; | |
595 | ddata->phy.otg = otg; | |
596 | ddata->phy.type = USB_PHY_TYPE_USB2; | |
597 | otg->set_host = cpcap_usb_phy_set_host; | |
598 | otg->set_peripheral = cpcap_usb_phy_set_peripheral; | |
599 | otg->usb_phy = &ddata->phy; | |
600 | INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect); | |
601 | platform_set_drvdata(pdev, ddata); | |
602 | ||
603 | ddata->vusb = devm_regulator_get(&pdev->dev, "vusb"); | |
604 | if (IS_ERR(ddata->vusb)) | |
605 | return PTR_ERR(ddata->vusb); | |
606 | ||
607 | error = regulator_enable(ddata->vusb); | |
608 | if (error) | |
609 | return error; | |
610 | ||
611 | generic_phy = devm_phy_create(ddata->dev, NULL, &ops); | |
612 | if (IS_ERR(generic_phy)) { | |
613 | error = PTR_ERR(generic_phy); | |
614 | return PTR_ERR(generic_phy); | |
615 | } | |
616 | ||
617 | phy_set_drvdata(generic_phy, ddata); | |
618 | ||
619 | phy_provider = devm_of_phy_provider_register(ddata->dev, | |
620 | of_phy_simple_xlate); | |
621 | if (IS_ERR(phy_provider)) | |
622 | return PTR_ERR(phy_provider); | |
623 | ||
624 | error = cpcap_usb_init_optional_pins(ddata); | |
625 | if (error) | |
626 | return error; | |
627 | ||
628 | cpcap_usb_init_optional_gpios(ddata); | |
629 | ||
630 | error = cpcap_usb_init_iio(ddata); | |
631 | if (error) | |
632 | return error; | |
633 | ||
634 | error = cpcap_usb_init_interrupts(pdev, ddata); | |
635 | if (error) | |
636 | return error; | |
637 | ||
638 | usb_add_phy_dev(&ddata->phy); | |
639 | atomic_set(&ddata->active, 1); | |
640 | schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1)); | |
641 | ||
642 | return 0; | |
643 | } | |
644 | ||
645 | static int cpcap_usb_phy_remove(struct platform_device *pdev) | |
646 | { | |
647 | struct cpcap_phy_ddata *ddata = platform_get_drvdata(pdev); | |
648 | int error; | |
649 | ||
650 | atomic_set(&ddata->active, 0); | |
651 | error = cpcap_usb_set_uart_mode(ddata); | |
652 | if (error) | |
653 | dev_err(ddata->dev, "could not set UART mode\n"); | |
654 | ||
044e1a93 | 655 | cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF); |
6d6ce40f TL |
656 | |
657 | usb_remove_phy(&ddata->phy); | |
658 | cancel_delayed_work_sync(&ddata->detect_work); | |
659 | clk_unprepare(ddata->refclk); | |
660 | regulator_disable(ddata->vusb); | |
661 | ||
662 | return 0; | |
663 | } | |
664 | ||
665 | static struct platform_driver cpcap_usb_phy_driver = { | |
666 | .probe = cpcap_usb_phy_probe, | |
667 | .remove = cpcap_usb_phy_remove, | |
668 | .driver = { | |
669 | .name = "cpcap-usb-phy", | |
670 | .of_match_table = of_match_ptr(cpcap_usb_phy_id_table), | |
671 | }, | |
672 | }; | |
673 | ||
674 | module_platform_driver(cpcap_usb_phy_driver); | |
675 | ||
676 | MODULE_ALIAS("platform:cpcap_usb"); | |
677 | MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); | |
678 | MODULE_DESCRIPTION("CPCAP usb phy driver"); | |
679 | MODULE_LICENSE("GPL v2"); |