Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * USB transceiver driver for AB8500 family chips
4 *
5 * Copyright (C) 2010-2013 ST-Ericsson AB
6 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
7 * Avinash Kumar <avinash.kumar@stericsson.com>
8 * Thirupathi Chippakurthy <thirupathi.chippakurthy@stericsson.com>
9 */
10
11#include <linux/module.h>
12#include <linux/platform_device.h>
13#include <linux/usb/otg.h>
14#include <linux/slab.h>
15#include <linux/notifier.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
18#include <linux/clk.h>
19#include <linux/err.h>
20#include <linux/mfd/abx500.h>
21#include <linux/mfd/abx500/ab8500.h>
22#include <linux/usb/musb-ux500.h>
23#include <linux/regulator/consumer.h>
24#include <linux/pinctrl/consumer.h>
25
26/* Bank AB8500_SYS_CTRL2_BLOCK */
27#define AB8500_MAIN_WD_CTRL_REG 0x01
28
29/* Bank AB8500_USB */
30#define AB8500_USB_LINE_STAT_REG 0x80
31#define AB8505_USB_LINE_STAT_REG 0x94
32#define AB8500_USB_PHY_CTRL_REG 0x8A
33
34/* Bank AB8500_DEVELOPMENT */
35#define AB8500_BANK12_ACCESS 0x00
36
37/* Bank AB8500_DEBUG */
38#define AB8500_USB_PHY_TUNE1 0x05
39#define AB8500_USB_PHY_TUNE2 0x06
40#define AB8500_USB_PHY_TUNE3 0x07
41
42/* Bank AB8500_INTERRUPT */
43#define AB8500_IT_SOURCE2_REG 0x01
44
45#define AB8500_BIT_OTG_STAT_ID (1 << 0)
46#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
47#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
48#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
49#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
50#define AB8500_BIT_SOURCE2_VBUSDET (1 << 7)
51
52#define AB8500_WD_KICK_DELAY_US 100 /* usec */
53#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
54#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
55
56/* Usb line status register */
57enum ab8500_usb_link_status {
58 USB_LINK_NOT_CONFIGURED_8500 = 0,
59 USB_LINK_STD_HOST_NC_8500,
60 USB_LINK_STD_HOST_C_NS_8500,
61 USB_LINK_STD_HOST_C_S_8500,
62 USB_LINK_HOST_CHG_NM_8500,
63 USB_LINK_HOST_CHG_HS_8500,
64 USB_LINK_HOST_CHG_HS_CHIRP_8500,
65 USB_LINK_DEDICATED_CHG_8500,
66 USB_LINK_ACA_RID_A_8500,
67 USB_LINK_ACA_RID_B_8500,
68 USB_LINK_ACA_RID_C_NM_8500,
69 USB_LINK_ACA_RID_C_HS_8500,
70 USB_LINK_ACA_RID_C_HS_CHIRP_8500,
71 USB_LINK_HM_IDGND_8500,
72 USB_LINK_RESERVED_8500,
73 USB_LINK_NOT_VALID_LINK_8500,
74};
75
76enum ab8505_usb_link_status {
77 USB_LINK_NOT_CONFIGURED_8505 = 0,
78 USB_LINK_STD_HOST_NC_8505,
79 USB_LINK_STD_HOST_C_NS_8505,
80 USB_LINK_STD_HOST_C_S_8505,
81 USB_LINK_CDP_8505,
82 USB_LINK_RESERVED0_8505,
83 USB_LINK_RESERVED1_8505,
84 USB_LINK_DEDICATED_CHG_8505,
85 USB_LINK_ACA_RID_A_8505,
86 USB_LINK_ACA_RID_B_8505,
87 USB_LINK_ACA_RID_C_NM_8505,
88 USB_LINK_RESERVED2_8505,
89 USB_LINK_RESERVED3_8505,
90 USB_LINK_HM_IDGND_8505,
91 USB_LINK_CHARGERPORT_NOT_OK_8505,
92 USB_LINK_CHARGER_DM_HIGH_8505,
93 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
94 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
95 USB_LINK_STD_UPSTREAM_8505,
96 USB_LINK_CHARGER_SE1_8505,
97 USB_LINK_CARKIT_CHGR_1_8505,
98 USB_LINK_CARKIT_CHGR_2_8505,
99 USB_LINK_ACA_DOCK_CHGR_8505,
100 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
101 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
102 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
103 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
104 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
105};
106
107enum ab8500_usb_mode {
108 USB_IDLE = 0,
109 USB_PERIPHERAL,
110 USB_HOST,
111 USB_DEDICATED_CHG,
112 USB_UART
113};
114
115/* Register USB_LINK_STATUS interrupt */
116#define AB8500_USB_FLAG_USE_LINK_STATUS_IRQ (1 << 0)
117/* Register ID_WAKEUP_F interrupt */
118#define AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ (1 << 1)
119/* Register VBUS_DET_F interrupt */
120#define AB8500_USB_FLAG_USE_VBUS_DET_IRQ (1 << 2)
121/* Driver is using the ab-iddet driver*/
122#define AB8500_USB_FLAG_USE_AB_IDDET (1 << 3)
123/* Enable setting regulators voltage */
124#define AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE (1 << 4)
125
126struct ab8500_usb {
127 struct usb_phy phy;
128 struct device *dev;
129 struct ab8500 *ab8500;
130 unsigned vbus_draw;
131 struct work_struct phy_dis_work;
132 enum ab8500_usb_mode mode;
133 struct clk *sysclk;
134 struct regulator *v_ape;
135 struct regulator *v_musb;
136 struct regulator *v_ulpi;
137 int saved_v_ulpi;
138 int previous_link_status_state;
139 struct pinctrl *pinctrl;
140 struct pinctrl_state *pins_sleep;
141 bool enabled_charging_detection;
142 unsigned int flags;
143};
144
145static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
146{
147 return container_of(x, struct ab8500_usb, phy);
148}
149
150static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
151{
152 abx500_set_register_interruptible(ab->dev,
153 AB8500_SYS_CTRL2_BLOCK,
154 AB8500_MAIN_WD_CTRL_REG,
155 AB8500_BIT_WD_CTRL_ENABLE);
156
157 udelay(AB8500_WD_KICK_DELAY_US);
158
159 abx500_set_register_interruptible(ab->dev,
160 AB8500_SYS_CTRL2_BLOCK,
161 AB8500_MAIN_WD_CTRL_REG,
162 (AB8500_BIT_WD_CTRL_ENABLE
163 | AB8500_BIT_WD_CTRL_KICK));
164
165 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
166
167 abx500_set_register_interruptible(ab->dev,
168 AB8500_SYS_CTRL2_BLOCK,
169 AB8500_MAIN_WD_CTRL_REG,
170 0);
171}
172
173static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
174{
175 int ret, volt;
176
177 ret = regulator_enable(ab->v_ape);
178 if (ret)
179 dev_err(ab->dev, "Failed to enable v-ape\n");
180
181 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
182 ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
183 if (ab->saved_v_ulpi < 0)
184 dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
185
186 ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
187 if (ret < 0)
188 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
189 ret);
190
191 ret = regulator_set_load(ab->v_ulpi, 28000);
192 if (ret < 0)
193 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
194 ret);
195 }
196
197 ret = regulator_enable(ab->v_ulpi);
198 if (ret)
199 dev_err(ab->dev, "Failed to enable vddulpivio18\n");
200
201 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
202 volt = regulator_get_voltage(ab->v_ulpi);
203 if ((volt != 1300000) && (volt != 1350000))
204 dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
205 volt);
206 }
207
208 ret = regulator_enable(ab->v_musb);
209 if (ret)
210 dev_err(ab->dev, "Failed to enable musb_1v8\n");
211}
212
213static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
214{
215 int ret;
216
217 regulator_disable(ab->v_musb);
218
219 regulator_disable(ab->v_ulpi);
220
221 /* USB is not the only consumer of Vintcore, restore old settings */
222 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
223 if (ab->saved_v_ulpi > 0) {
224 ret = regulator_set_voltage(ab->v_ulpi,
225 ab->saved_v_ulpi, ab->saved_v_ulpi);
226 if (ret < 0)
227 dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
228 ab->saved_v_ulpi, ret);
229 }
230
231 ret = regulator_set_load(ab->v_ulpi, 0);
232 if (ret < 0)
233 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
234 ret);
235 }
236
237 regulator_disable(ab->v_ape);
238}
239
240static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
241{
242 /* Workaround for v2.0 bug # 31952 */
243 if (is_ab8500_2p0(ab->ab8500)) {
244 abx500_mask_and_set_register_interruptible(ab->dev,
245 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
246 bit, bit);
247 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
248 }
249}
250
251static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
252{
253 u8 bit;
254 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
255 AB8500_BIT_PHY_CTRL_DEVICE_EN;
256
257 /* mux and configure USB pins to DEFAULT state */
258 ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
259 if (IS_ERR(ab->pinctrl))
260 dev_err(ab->dev, "could not get/set default pinstate\n");
261
262 if (clk_prepare_enable(ab->sysclk))
263 dev_err(ab->dev, "can't prepare/enable clock\n");
264
265 ab8500_usb_regulator_enable(ab);
266
267 abx500_mask_and_set_register_interruptible(ab->dev,
268 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
269 bit, bit);
270}
271
272static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
273{
274 u8 bit;
275 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
276 AB8500_BIT_PHY_CTRL_DEVICE_EN;
277
278 ab8500_usb_wd_linkstatus(ab, bit);
279
280 abx500_mask_and_set_register_interruptible(ab->dev,
281 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
282 bit, 0);
283
284 /* Needed to disable the phy.*/
285 ab8500_usb_wd_workaround(ab);
286
287 clk_disable_unprepare(ab->sysclk);
288
289 ab8500_usb_regulator_disable(ab);
290
291 if (!IS_ERR(ab->pinctrl)) {
292 /* configure USB pins to SLEEP state */
293 ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
294 PINCTRL_STATE_SLEEP);
295
296 if (IS_ERR(ab->pins_sleep))
297 dev_dbg(ab->dev, "could not get sleep pinstate\n");
298 else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
299 dev_err(ab->dev, "could not set pins to sleep state\n");
300
301 /*
302 * as USB pins are shared with iddet, release them to allow
303 * iddet to request them
304 */
305 pinctrl_put(ab->pinctrl);
306 }
307}
308
309#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true)
310#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
311#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false)
312#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
313
314static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
315 enum ab8505_usb_link_status lsts)
316{
317 enum ux500_musb_vbus_id_status event = 0;
318
319 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
320
321 /*
322 * Spurious link_status interrupts are seen at the time of
323 * disconnection of a device in RIDA state
324 */
325 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
326 (lsts == USB_LINK_STD_HOST_NC_8505))
327 return 0;
328
329 ab->previous_link_status_state = lsts;
330
331 switch (lsts) {
332 case USB_LINK_ACA_RID_B_8505:
333 event = UX500_MUSB_RIDB;
334 fallthrough;
335 case USB_LINK_NOT_CONFIGURED_8505:
336 case USB_LINK_RESERVED0_8505:
337 case USB_LINK_RESERVED1_8505:
338 case USB_LINK_RESERVED2_8505:
339 case USB_LINK_RESERVED3_8505:
340 ab->mode = USB_IDLE;
341 ab->phy.otg->default_a = false;
342 ab->vbus_draw = 0;
343 if (event != UX500_MUSB_RIDB)
344 event = UX500_MUSB_NONE;
345 /*
346 * Fallback to default B_IDLE as nothing
347 * is connected
348 */
349 ab->phy.otg->state = OTG_STATE_B_IDLE;
350 usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
351 break;
352
353 case USB_LINK_ACA_RID_C_NM_8505:
354 event = UX500_MUSB_RIDC;
355 fallthrough;
356 case USB_LINK_STD_HOST_NC_8505:
357 case USB_LINK_STD_HOST_C_NS_8505:
358 case USB_LINK_STD_HOST_C_S_8505:
359 case USB_LINK_CDP_8505:
360 if (ab->mode == USB_IDLE) {
361 ab->mode = USB_PERIPHERAL;
362 ab8500_usb_peri_phy_en(ab);
363 atomic_notifier_call_chain(&ab->phy.notifier,
364 UX500_MUSB_PREPARE, &ab->vbus_draw);
365 usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
366 }
367 if (event != UX500_MUSB_RIDC)
368 event = UX500_MUSB_VBUS;
369 break;
370
371 case USB_LINK_ACA_RID_A_8505:
372 case USB_LINK_ACA_DOCK_CHGR_8505:
373 event = UX500_MUSB_RIDA;
374 fallthrough;
375 case USB_LINK_HM_IDGND_8505:
376 if (ab->mode == USB_IDLE) {
377 ab->mode = USB_HOST;
378 ab8500_usb_host_phy_en(ab);
379 atomic_notifier_call_chain(&ab->phy.notifier,
380 UX500_MUSB_PREPARE, &ab->vbus_draw);
381 }
382 ab->phy.otg->default_a = true;
383 if (event != UX500_MUSB_RIDA)
384 event = UX500_MUSB_ID;
385 atomic_notifier_call_chain(&ab->phy.notifier,
386 event, &ab->vbus_draw);
387 break;
388
389 case USB_LINK_DEDICATED_CHG_8505:
390 ab->mode = USB_DEDICATED_CHG;
391 event = UX500_MUSB_CHARGER;
392 atomic_notifier_call_chain(&ab->phy.notifier,
393 event, &ab->vbus_draw);
394 usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
395 break;
396
397 /*
398 * FIXME: For now we rely on the boot firmware to set up the necessary
399 * PHY/pin configuration for UART mode.
400 *
401 * AB8505 does not seem to report any status change for UART cables,
402 * possibly because it cannot detect them autonomously.
403 * We may need to measure the ID resistance manually to reliably
404 * detect UART cables after bootup.
405 */
406 case USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505:
407 case USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505:
408 if (ab->mode == USB_IDLE) {
409 ab->mode = USB_UART;
410 ab8500_usb_peri_phy_en(ab);
411 }
412
413 break;
414
415 default:
416 break;
417 }
418
419 return 0;
420}
421
422static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
423 enum ab8500_usb_link_status lsts)
424{
425 enum ux500_musb_vbus_id_status event = 0;
426
427 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
428
429 /*
430 * Spurious link_status interrupts are seen in case of a
431 * disconnection of a device in IDGND and RIDA stage
432 */
433 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
434 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
435 lsts == USB_LINK_STD_HOST_NC_8500))
436 return 0;
437
438 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
439 lsts == USB_LINK_STD_HOST_NC_8500)
440 return 0;
441
442 ab->previous_link_status_state = lsts;
443
444 switch (lsts) {
445 case USB_LINK_ACA_RID_B_8500:
446 event = UX500_MUSB_RIDB;
447 fallthrough;
448 case USB_LINK_NOT_CONFIGURED_8500:
449 case USB_LINK_NOT_VALID_LINK_8500:
450 ab->mode = USB_IDLE;
451 ab->phy.otg->default_a = false;
452 ab->vbus_draw = 0;
453 if (event != UX500_MUSB_RIDB)
454 event = UX500_MUSB_NONE;
455 /* Fallback to default B_IDLE as nothing is connected */
456 ab->phy.otg->state = OTG_STATE_B_IDLE;
457 usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
458 break;
459
460 case USB_LINK_ACA_RID_C_NM_8500:
461 case USB_LINK_ACA_RID_C_HS_8500:
462 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
463 event = UX500_MUSB_RIDC;
464 fallthrough;
465 case USB_LINK_STD_HOST_NC_8500:
466 case USB_LINK_STD_HOST_C_NS_8500:
467 case USB_LINK_STD_HOST_C_S_8500:
468 case USB_LINK_HOST_CHG_NM_8500:
469 case USB_LINK_HOST_CHG_HS_8500:
470 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
471 if (ab->mode == USB_IDLE) {
472 ab->mode = USB_PERIPHERAL;
473 ab8500_usb_peri_phy_en(ab);
474 atomic_notifier_call_chain(&ab->phy.notifier,
475 UX500_MUSB_PREPARE, &ab->vbus_draw);
476 usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
477 }
478 if (event != UX500_MUSB_RIDC)
479 event = UX500_MUSB_VBUS;
480 break;
481
482 case USB_LINK_ACA_RID_A_8500:
483 event = UX500_MUSB_RIDA;
484 fallthrough;
485 case USB_LINK_HM_IDGND_8500:
486 if (ab->mode == USB_IDLE) {
487 ab->mode = USB_HOST;
488 ab8500_usb_host_phy_en(ab);
489 atomic_notifier_call_chain(&ab->phy.notifier,
490 UX500_MUSB_PREPARE, &ab->vbus_draw);
491 }
492 ab->phy.otg->default_a = true;
493 if (event != UX500_MUSB_RIDA)
494 event = UX500_MUSB_ID;
495 atomic_notifier_call_chain(&ab->phy.notifier,
496 event, &ab->vbus_draw);
497 break;
498
499 case USB_LINK_DEDICATED_CHG_8500:
500 ab->mode = USB_DEDICATED_CHG;
501 event = UX500_MUSB_CHARGER;
502 atomic_notifier_call_chain(&ab->phy.notifier,
503 event, &ab->vbus_draw);
504 usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
505 break;
506
507 case USB_LINK_RESERVED_8500:
508 break;
509 }
510
511 return 0;
512}
513
514/*
515 * Connection Sequence:
516 * 1. Link Status Interrupt
517 * 2. Enable AB clock
518 * 3. Enable AB regulators
519 * 4. Enable USB phy
520 * 5. Reset the musb controller
521 * 6. Switch the ULPI GPIO pins to function mode
522 * 7. Enable the musb Peripheral5 clock
523 * 8. Restore MUSB context
524 */
525static int abx500_usb_link_status_update(struct ab8500_usb *ab)
526{
527 u8 reg;
528 int ret = 0;
529
530 if (is_ab8500(ab->ab8500)) {
531 enum ab8500_usb_link_status lsts;
532
533 ret = abx500_get_register_interruptible(ab->dev,
534 AB8500_USB, AB8500_USB_LINE_STAT_REG, ®);
535 if (ret < 0)
536 return ret;
537 lsts = (reg >> 3) & 0x0F;
538 ret = ab8500_usb_link_status_update(ab, lsts);
539 } else if (is_ab8505(ab->ab8500)) {
540 enum ab8505_usb_link_status lsts;
541
542 ret = abx500_get_register_interruptible(ab->dev,
543 AB8500_USB, AB8505_USB_LINE_STAT_REG, ®);
544 if (ret < 0)
545 return ret;
546 lsts = (reg >> 3) & 0x1F;
547 ret = ab8505_usb_link_status_update(ab, lsts);
548 }
549
550 return ret;
551}
552
553/*
554 * Disconnection Sequence:
555 * 1. Disconnect Interrupt
556 * 2. Disable regulators
557 * 3. Disable AB clock
558 * 4. Disable the Phy
559 * 5. Link Status Interrupt
560 * 6. Disable Musb Clock
561 */
562static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
563{
564 struct ab8500_usb *ab = (struct ab8500_usb *) data;
565 enum usb_phy_events event = USB_EVENT_NONE;
566
567 /* Link status will not be updated till phy is disabled. */
568 if (ab->mode == USB_HOST) {
569 ab->phy.otg->default_a = false;
570 ab->vbus_draw = 0;
571 atomic_notifier_call_chain(&ab->phy.notifier,
572 event, &ab->vbus_draw);
573 ab8500_usb_host_phy_dis(ab);
574 ab->mode = USB_IDLE;
575 }
576
577 if (ab->mode == USB_PERIPHERAL) {
578 atomic_notifier_call_chain(&ab->phy.notifier,
579 event, &ab->vbus_draw);
580 ab8500_usb_peri_phy_dis(ab);
581 atomic_notifier_call_chain(&ab->phy.notifier,
582 UX500_MUSB_CLEAN, &ab->vbus_draw);
583 ab->mode = USB_IDLE;
584 ab->phy.otg->default_a = false;
585 ab->vbus_draw = 0;
586 }
587
588 if (ab->mode == USB_UART) {
589 ab8500_usb_peri_phy_dis(ab);
590 ab->mode = USB_IDLE;
591 }
592
593 if (is_ab8500_2p0(ab->ab8500)) {
594 if (ab->mode == USB_DEDICATED_CHG) {
595 ab8500_usb_wd_linkstatus(ab,
596 AB8500_BIT_PHY_CTRL_DEVICE_EN);
597 abx500_mask_and_set_register_interruptible(ab->dev,
598 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
599 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
600 }
601 }
602
603 return IRQ_HANDLED;
604}
605
606static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
607{
608 struct ab8500_usb *ab = (struct ab8500_usb *)data;
609
610 abx500_usb_link_status_update(ab);
611
612 return IRQ_HANDLED;
613}
614
615static void ab8500_usb_phy_disable_work(struct work_struct *work)
616{
617 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
618 phy_dis_work);
619
620 if (!ab->phy.otg->host)
621 ab8500_usb_host_phy_dis(ab);
622
623 if (!ab->phy.otg->gadget)
624 ab8500_usb_peri_phy_dis(ab);
625}
626
627static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
628{
629 /* TODO */
630 return 0;
631}
632
633static int ab8500_usb_set_peripheral(struct usb_otg *otg,
634 struct usb_gadget *gadget)
635{
636 struct ab8500_usb *ab;
637
638 if (!otg)
639 return -ENODEV;
640
641 ab = phy_to_ab(otg->usb_phy);
642
643 ab->phy.otg->gadget = gadget;
644
645 /* Some drivers call this function in atomic context.
646 * Do not update ab8500 registers directly till this
647 * is fixed.
648 */
649
650 if ((ab->mode != USB_IDLE) && !gadget) {
651 ab->mode = USB_IDLE;
652 schedule_work(&ab->phy_dis_work);
653 }
654
655 return 0;
656}
657
658static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
659{
660 struct ab8500_usb *ab;
661
662 if (!otg)
663 return -ENODEV;
664
665 ab = phy_to_ab(otg->usb_phy);
666
667 ab->phy.otg->host = host;
668
669 /* Some drivers call this function in atomic context.
670 * Do not update ab8500 registers directly till this
671 * is fixed.
672 */
673
674 if ((ab->mode != USB_IDLE) && !host) {
675 ab->mode = USB_IDLE;
676 schedule_work(&ab->phy_dis_work);
677 }
678
679 return 0;
680}
681
682static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
683{
684 abx500_mask_and_set_register_interruptible(ab->dev,
685 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
686 AB8500_BIT_PHY_CTRL_DEVICE_EN,
687 AB8500_BIT_PHY_CTRL_DEVICE_EN);
688
689 udelay(100);
690
691 abx500_mask_and_set_register_interruptible(ab->dev,
692 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
693 AB8500_BIT_PHY_CTRL_DEVICE_EN,
694 0);
695
696 abx500_mask_and_set_register_interruptible(ab->dev,
697 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
698 AB8500_BIT_PHY_CTRL_HOST_EN,
699 AB8500_BIT_PHY_CTRL_HOST_EN);
700
701 udelay(100);
702
703 abx500_mask_and_set_register_interruptible(ab->dev,
704 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
705 AB8500_BIT_PHY_CTRL_HOST_EN,
706 0);
707}
708
709static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
710{
711 int err;
712
713 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
714 if (IS_ERR(ab->v_ape)) {
715 dev_err(ab->dev, "Could not get v-ape supply\n");
716 err = PTR_ERR(ab->v_ape);
717 return err;
718 }
719
720 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
721 if (IS_ERR(ab->v_ulpi)) {
722 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
723 err = PTR_ERR(ab->v_ulpi);
724 return err;
725 }
726
727 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
728 if (IS_ERR(ab->v_musb)) {
729 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
730 err = PTR_ERR(ab->v_musb);
731 return err;
732 }
733
734 return 0;
735}
736
737static int ab8500_usb_irq_setup(struct platform_device *pdev,
738 struct ab8500_usb *ab)
739{
740 int err;
741 int irq;
742
743 if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
744 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
745 if (irq < 0)
746 return irq;
747 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
748 ab8500_usb_link_status_irq,
749 IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
750 "usb-link-status", ab);
751 if (err < 0) {
752 dev_err(ab->dev, "request_irq failed for link status irq\n");
753 return err;
754 }
755 }
756
757 if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
758 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
759 if (irq < 0)
760 return irq;
761 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
762 ab8500_usb_disconnect_irq,
763 IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
764 "usb-id-fall", ab);
765 if (err < 0) {
766 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
767 return err;
768 }
769 }
770
771 if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
772 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
773 if (irq < 0)
774 return irq;
775 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
776 ab8500_usb_disconnect_irq,
777 IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
778 "usb-vbus-fall", ab);
779 if (err < 0) {
780 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
781 return err;
782 }
783 }
784
785 return 0;
786}
787
788static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
789{
790 int err;
791
792 /* Enable the PBT/Bank 0x12 access */
793 err = abx500_set_register_interruptible(ab->dev,
794 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
795 if (err < 0)
796 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
797 err);
798
799 err = abx500_set_register_interruptible(ab->dev,
800 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
801 if (err < 0)
802 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
803 err);
804
805 err = abx500_set_register_interruptible(ab->dev,
806 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
807 if (err < 0)
808 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
809 err);
810
811 err = abx500_set_register_interruptible(ab->dev,
812 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
813 if (err < 0)
814 dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
815 err);
816
817 /* Switch to normal mode/disable Bank 0x12 access */
818 err = abx500_set_register_interruptible(ab->dev,
819 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
820 if (err < 0)
821 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
822 err);
823}
824
825static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
826{
827 int err;
828
829 /* Enable the PBT/Bank 0x12 access */
830 err = abx500_mask_and_set_register_interruptible(ab->dev,
831 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
832 0x01, 0x01);
833 if (err < 0)
834 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
835 err);
836
837 err = abx500_mask_and_set_register_interruptible(ab->dev,
838 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
839 0xC8, 0xC8);
840 if (err < 0)
841 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
842 err);
843
844 err = abx500_mask_and_set_register_interruptible(ab->dev,
845 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
846 0x60, 0x60);
847 if (err < 0)
848 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
849 err);
850
851 err = abx500_mask_and_set_register_interruptible(ab->dev,
852 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
853 0xFC, 0x80);
854
855 if (err < 0)
856 dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
857 err);
858
859 /* Switch to normal mode/disable Bank 0x12 access */
860 err = abx500_mask_and_set_register_interruptible(ab->dev,
861 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
862 0x00, 0x00);
863 if (err < 0)
864 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
865 err);
866}
867
868static int ab8500_usb_probe(struct platform_device *pdev)
869{
870 struct ab8500_usb *ab;
871 struct ab8500 *ab8500;
872 struct usb_otg *otg;
873 int err;
874 int rev;
875
876 ab8500 = dev_get_drvdata(pdev->dev.parent);
877 rev = abx500_get_chip_id(&pdev->dev);
878
879 if (is_ab8500_1p1_or_earlier(ab8500)) {
880 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
881 return -ENODEV;
882 }
883
884 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
885 if (!ab)
886 return -ENOMEM;
887
888 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
889 if (!otg)
890 return -ENOMEM;
891
892 ab->dev = &pdev->dev;
893 ab->ab8500 = ab8500;
894 ab->phy.dev = ab->dev;
895 ab->phy.otg = otg;
896 ab->phy.label = "ab8500";
897 ab->phy.set_suspend = ab8500_usb_set_suspend;
898 ab->phy.otg->state = OTG_STATE_UNDEFINED;
899
900 otg->usb_phy = &ab->phy;
901 otg->set_host = ab8500_usb_set_host;
902 otg->set_peripheral = ab8500_usb_set_peripheral;
903
904 if (is_ab8500(ab->ab8500)) {
905 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
906 AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
907 AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
908 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
909 } else if (is_ab8505(ab->ab8500)) {
910 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
911 AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
912 AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
913 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
914 }
915
916 /* Disable regulator voltage setting for AB8500 <= v2.0 */
917 if (is_ab8500_2p0_or_earlier(ab->ab8500))
918 ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
919
920 platform_set_drvdata(pdev, ab);
921
922 /* all: Disable phy when called from set_host and set_peripheral */
923 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
924
925 err = ab8500_usb_regulator_get(ab);
926 if (err)
927 return err;
928
929 ab->sysclk = devm_clk_get(ab->dev, "sysclk");
930 if (IS_ERR(ab->sysclk)) {
931 dev_err(ab->dev, "Could not get sysclk.\n");
932 return PTR_ERR(ab->sysclk);
933 }
934
935 err = ab8500_usb_irq_setup(pdev, ab);
936 if (err < 0)
937 return err;
938
939 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
940 if (err) {
941 dev_err(&pdev->dev, "Can't register transceiver\n");
942 return err;
943 }
944
945 if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
946 /* Phy tuning values for AB8500 > v2.0 */
947 ab8500_usb_set_ab8500_tuning_values(ab);
948 else if (is_ab8505(ab->ab8500))
949 /* Phy tuning values for AB8505 */
950 ab8500_usb_set_ab8505_tuning_values(ab);
951
952 /* Needed to enable ID detection. */
953 ab8500_usb_wd_workaround(ab);
954
955 /*
956 * This is required for usb-link-status to work properly when a
957 * cable is connected at boot time.
958 */
959 ab8500_usb_restart_phy(ab);
960
961 abx500_usb_link_status_update(ab);
962
963 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
964
965 return 0;
966}
967
968static void ab8500_usb_remove(struct platform_device *pdev)
969{
970 struct ab8500_usb *ab = platform_get_drvdata(pdev);
971
972 cancel_work_sync(&ab->phy_dis_work);
973
974 usb_remove_phy(&ab->phy);
975
976 if (ab->mode == USB_HOST)
977 ab8500_usb_host_phy_dis(ab);
978 else if (ab->mode == USB_PERIPHERAL)
979 ab8500_usb_peri_phy_dis(ab);
980}
981
982static const struct platform_device_id ab8500_usb_devtype[] = {
983 { .name = "ab8500-usb", },
984 { /* sentinel */ }
985};
986MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
987
988static struct platform_driver ab8500_usb_driver = {
989 .probe = ab8500_usb_probe,
990 .remove_new = ab8500_usb_remove,
991 .id_table = ab8500_usb_devtype,
992 .driver = {
993 .name = "abx5x0-usb",
994 },
995};
996
997static int __init ab8500_usb_init(void)
998{
999 return platform_driver_register(&ab8500_usb_driver);
1000}
1001subsys_initcall(ab8500_usb_init);
1002
1003static void __exit ab8500_usb_exit(void)
1004{
1005 platform_driver_unregister(&ab8500_usb_driver);
1006}
1007module_exit(ab8500_usb_exit);
1008
1009MODULE_AUTHOR("ST-Ericsson AB");
1010MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
1011MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * USB transceiver driver for AB8500 family chips
4 *
5 * Copyright (C) 2010-2013 ST-Ericsson AB
6 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
7 * Avinash Kumar <avinash.kumar@stericsson.com>
8 * Thirupathi Chippakurthy <thirupathi.chippakurthy@stericsson.com>
9 */
10
11#include <linux/module.h>
12#include <linux/platform_device.h>
13#include <linux/usb/otg.h>
14#include <linux/slab.h>
15#include <linux/notifier.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
18#include <linux/clk.h>
19#include <linux/err.h>
20#include <linux/mfd/abx500.h>
21#include <linux/mfd/abx500/ab8500.h>
22#include <linux/usb/musb-ux500.h>
23#include <linux/regulator/consumer.h>
24#include <linux/pinctrl/consumer.h>
25
26/* Bank AB8500_SYS_CTRL2_BLOCK */
27#define AB8500_MAIN_WD_CTRL_REG 0x01
28
29/* Bank AB8500_USB */
30#define AB8500_USB_LINE_STAT_REG 0x80
31#define AB8505_USB_LINE_STAT_REG 0x94
32#define AB8500_USB_PHY_CTRL_REG 0x8A
33
34/* Bank AB8500_DEVELOPMENT */
35#define AB8500_BANK12_ACCESS 0x00
36
37/* Bank AB8500_DEBUG */
38#define AB8500_USB_PHY_TUNE1 0x05
39#define AB8500_USB_PHY_TUNE2 0x06
40#define AB8500_USB_PHY_TUNE3 0x07
41
42/* Bank AB8500_INTERRUPT */
43#define AB8500_IT_SOURCE2_REG 0x01
44
45#define AB8500_BIT_OTG_STAT_ID (1 << 0)
46#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
47#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
48#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
49#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
50#define AB8500_BIT_SOURCE2_VBUSDET (1 << 7)
51
52#define AB8500_WD_KICK_DELAY_US 100 /* usec */
53#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
54#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
55
56/* Usb line status register */
57enum ab8500_usb_link_status {
58 USB_LINK_NOT_CONFIGURED_8500 = 0,
59 USB_LINK_STD_HOST_NC_8500,
60 USB_LINK_STD_HOST_C_NS_8500,
61 USB_LINK_STD_HOST_C_S_8500,
62 USB_LINK_HOST_CHG_NM_8500,
63 USB_LINK_HOST_CHG_HS_8500,
64 USB_LINK_HOST_CHG_HS_CHIRP_8500,
65 USB_LINK_DEDICATED_CHG_8500,
66 USB_LINK_ACA_RID_A_8500,
67 USB_LINK_ACA_RID_B_8500,
68 USB_LINK_ACA_RID_C_NM_8500,
69 USB_LINK_ACA_RID_C_HS_8500,
70 USB_LINK_ACA_RID_C_HS_CHIRP_8500,
71 USB_LINK_HM_IDGND_8500,
72 USB_LINK_RESERVED_8500,
73 USB_LINK_NOT_VALID_LINK_8500,
74};
75
76enum ab8505_usb_link_status {
77 USB_LINK_NOT_CONFIGURED_8505 = 0,
78 USB_LINK_STD_HOST_NC_8505,
79 USB_LINK_STD_HOST_C_NS_8505,
80 USB_LINK_STD_HOST_C_S_8505,
81 USB_LINK_CDP_8505,
82 USB_LINK_RESERVED0_8505,
83 USB_LINK_RESERVED1_8505,
84 USB_LINK_DEDICATED_CHG_8505,
85 USB_LINK_ACA_RID_A_8505,
86 USB_LINK_ACA_RID_B_8505,
87 USB_LINK_ACA_RID_C_NM_8505,
88 USB_LINK_RESERVED2_8505,
89 USB_LINK_RESERVED3_8505,
90 USB_LINK_HM_IDGND_8505,
91 USB_LINK_CHARGERPORT_NOT_OK_8505,
92 USB_LINK_CHARGER_DM_HIGH_8505,
93 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
94 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
95 USB_LINK_STD_UPSTREAM_8505,
96 USB_LINK_CHARGER_SE1_8505,
97 USB_LINK_CARKIT_CHGR_1_8505,
98 USB_LINK_CARKIT_CHGR_2_8505,
99 USB_LINK_ACA_DOCK_CHGR_8505,
100 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
101 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
102 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
103 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
104 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
105};
106
107enum ab8500_usb_mode {
108 USB_IDLE = 0,
109 USB_PERIPHERAL,
110 USB_HOST,
111 USB_DEDICATED_CHG
112};
113
114/* Register USB_LINK_STATUS interrupt */
115#define AB8500_USB_FLAG_USE_LINK_STATUS_IRQ (1 << 0)
116/* Register ID_WAKEUP_F interrupt */
117#define AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ (1 << 1)
118/* Register VBUS_DET_F interrupt */
119#define AB8500_USB_FLAG_USE_VBUS_DET_IRQ (1 << 2)
120/* Driver is using the ab-iddet driver*/
121#define AB8500_USB_FLAG_USE_AB_IDDET (1 << 3)
122/* Enable setting regulators voltage */
123#define AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE (1 << 4)
124
125struct ab8500_usb {
126 struct usb_phy phy;
127 struct device *dev;
128 struct ab8500 *ab8500;
129 unsigned vbus_draw;
130 struct work_struct phy_dis_work;
131 enum ab8500_usb_mode mode;
132 struct clk *sysclk;
133 struct regulator *v_ape;
134 struct regulator *v_musb;
135 struct regulator *v_ulpi;
136 int saved_v_ulpi;
137 int previous_link_status_state;
138 struct pinctrl *pinctrl;
139 struct pinctrl_state *pins_sleep;
140 bool enabled_charging_detection;
141 unsigned int flags;
142};
143
144static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
145{
146 return container_of(x, struct ab8500_usb, phy);
147}
148
149static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
150{
151 abx500_set_register_interruptible(ab->dev,
152 AB8500_SYS_CTRL2_BLOCK,
153 AB8500_MAIN_WD_CTRL_REG,
154 AB8500_BIT_WD_CTRL_ENABLE);
155
156 udelay(AB8500_WD_KICK_DELAY_US);
157
158 abx500_set_register_interruptible(ab->dev,
159 AB8500_SYS_CTRL2_BLOCK,
160 AB8500_MAIN_WD_CTRL_REG,
161 (AB8500_BIT_WD_CTRL_ENABLE
162 | AB8500_BIT_WD_CTRL_KICK));
163
164 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
165
166 abx500_set_register_interruptible(ab->dev,
167 AB8500_SYS_CTRL2_BLOCK,
168 AB8500_MAIN_WD_CTRL_REG,
169 0);
170}
171
172static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
173{
174 int ret, volt;
175
176 ret = regulator_enable(ab->v_ape);
177 if (ret)
178 dev_err(ab->dev, "Failed to enable v-ape\n");
179
180 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
181 ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
182 if (ab->saved_v_ulpi < 0)
183 dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
184
185 ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
186 if (ret < 0)
187 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
188 ret);
189
190 ret = regulator_set_load(ab->v_ulpi, 28000);
191 if (ret < 0)
192 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
193 ret);
194 }
195
196 ret = regulator_enable(ab->v_ulpi);
197 if (ret)
198 dev_err(ab->dev, "Failed to enable vddulpivio18\n");
199
200 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
201 volt = regulator_get_voltage(ab->v_ulpi);
202 if ((volt != 1300000) && (volt != 1350000))
203 dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
204 volt);
205 }
206
207 ret = regulator_enable(ab->v_musb);
208 if (ret)
209 dev_err(ab->dev, "Failed to enable musb_1v8\n");
210}
211
212static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
213{
214 int ret;
215
216 regulator_disable(ab->v_musb);
217
218 regulator_disable(ab->v_ulpi);
219
220 /* USB is not the only consumer of Vintcore, restore old settings */
221 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
222 if (ab->saved_v_ulpi > 0) {
223 ret = regulator_set_voltage(ab->v_ulpi,
224 ab->saved_v_ulpi, ab->saved_v_ulpi);
225 if (ret < 0)
226 dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
227 ab->saved_v_ulpi, ret);
228 }
229
230 ret = regulator_set_load(ab->v_ulpi, 0);
231 if (ret < 0)
232 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
233 ret);
234 }
235
236 regulator_disable(ab->v_ape);
237}
238
239static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
240{
241 /* Workaround for v2.0 bug # 31952 */
242 if (is_ab8500_2p0(ab->ab8500)) {
243 abx500_mask_and_set_register_interruptible(ab->dev,
244 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
245 bit, bit);
246 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
247 }
248}
249
250static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
251{
252 u8 bit;
253 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
254 AB8500_BIT_PHY_CTRL_DEVICE_EN;
255
256 /* mux and configure USB pins to DEFAULT state */
257 ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
258 if (IS_ERR(ab->pinctrl))
259 dev_err(ab->dev, "could not get/set default pinstate\n");
260
261 if (clk_prepare_enable(ab->sysclk))
262 dev_err(ab->dev, "can't prepare/enable clock\n");
263
264 ab8500_usb_regulator_enable(ab);
265
266 abx500_mask_and_set_register_interruptible(ab->dev,
267 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
268 bit, bit);
269}
270
271static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
272{
273 u8 bit;
274 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
275 AB8500_BIT_PHY_CTRL_DEVICE_EN;
276
277 ab8500_usb_wd_linkstatus(ab, bit);
278
279 abx500_mask_and_set_register_interruptible(ab->dev,
280 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
281 bit, 0);
282
283 /* Needed to disable the phy.*/
284 ab8500_usb_wd_workaround(ab);
285
286 clk_disable_unprepare(ab->sysclk);
287
288 ab8500_usb_regulator_disable(ab);
289
290 if (!IS_ERR(ab->pinctrl)) {
291 /* configure USB pins to SLEEP state */
292 ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
293 PINCTRL_STATE_SLEEP);
294
295 if (IS_ERR(ab->pins_sleep))
296 dev_dbg(ab->dev, "could not get sleep pinstate\n");
297 else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
298 dev_err(ab->dev, "could not set pins to sleep state\n");
299
300 /*
301 * as USB pins are shared with iddet, release them to allow
302 * iddet to request them
303 */
304 pinctrl_put(ab->pinctrl);
305 }
306}
307
308#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true)
309#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
310#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false)
311#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
312
313static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
314 enum ab8505_usb_link_status lsts)
315{
316 enum ux500_musb_vbus_id_status event = 0;
317
318 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
319
320 /*
321 * Spurious link_status interrupts are seen at the time of
322 * disconnection of a device in RIDA state
323 */
324 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
325 (lsts == USB_LINK_STD_HOST_NC_8505))
326 return 0;
327
328 ab->previous_link_status_state = lsts;
329
330 switch (lsts) {
331 case USB_LINK_ACA_RID_B_8505:
332 event = UX500_MUSB_RIDB;
333 case USB_LINK_NOT_CONFIGURED_8505:
334 case USB_LINK_RESERVED0_8505:
335 case USB_LINK_RESERVED1_8505:
336 case USB_LINK_RESERVED2_8505:
337 case USB_LINK_RESERVED3_8505:
338 ab->mode = USB_IDLE;
339 ab->phy.otg->default_a = false;
340 ab->vbus_draw = 0;
341 if (event != UX500_MUSB_RIDB)
342 event = UX500_MUSB_NONE;
343 /*
344 * Fallback to default B_IDLE as nothing
345 * is connected
346 */
347 ab->phy.otg->state = OTG_STATE_B_IDLE;
348 usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
349 break;
350
351 case USB_LINK_ACA_RID_C_NM_8505:
352 event = UX500_MUSB_RIDC;
353 case USB_LINK_STD_HOST_NC_8505:
354 case USB_LINK_STD_HOST_C_NS_8505:
355 case USB_LINK_STD_HOST_C_S_8505:
356 case USB_LINK_CDP_8505:
357 if (ab->mode == USB_IDLE) {
358 ab->mode = USB_PERIPHERAL;
359 ab8500_usb_peri_phy_en(ab);
360 atomic_notifier_call_chain(&ab->phy.notifier,
361 UX500_MUSB_PREPARE, &ab->vbus_draw);
362 usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
363 }
364 if (event != UX500_MUSB_RIDC)
365 event = UX500_MUSB_VBUS;
366 break;
367
368 case USB_LINK_ACA_RID_A_8505:
369 case USB_LINK_ACA_DOCK_CHGR_8505:
370 event = UX500_MUSB_RIDA;
371 case USB_LINK_HM_IDGND_8505:
372 if (ab->mode == USB_IDLE) {
373 ab->mode = USB_HOST;
374 ab8500_usb_host_phy_en(ab);
375 atomic_notifier_call_chain(&ab->phy.notifier,
376 UX500_MUSB_PREPARE, &ab->vbus_draw);
377 }
378 ab->phy.otg->default_a = true;
379 if (event != UX500_MUSB_RIDA)
380 event = UX500_MUSB_ID;
381 atomic_notifier_call_chain(&ab->phy.notifier,
382 event, &ab->vbus_draw);
383 break;
384
385 case USB_LINK_DEDICATED_CHG_8505:
386 ab->mode = USB_DEDICATED_CHG;
387 event = UX500_MUSB_CHARGER;
388 atomic_notifier_call_chain(&ab->phy.notifier,
389 event, &ab->vbus_draw);
390 usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
391 break;
392
393 default:
394 break;
395 }
396
397 return 0;
398}
399
400static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
401 enum ab8500_usb_link_status lsts)
402{
403 enum ux500_musb_vbus_id_status event = 0;
404
405 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
406
407 /*
408 * Spurious link_status interrupts are seen in case of a
409 * disconnection of a device in IDGND and RIDA stage
410 */
411 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
412 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
413 lsts == USB_LINK_STD_HOST_NC_8500))
414 return 0;
415
416 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
417 lsts == USB_LINK_STD_HOST_NC_8500)
418 return 0;
419
420 ab->previous_link_status_state = lsts;
421
422 switch (lsts) {
423 case USB_LINK_ACA_RID_B_8500:
424 event = UX500_MUSB_RIDB;
425 case USB_LINK_NOT_CONFIGURED_8500:
426 case USB_LINK_NOT_VALID_LINK_8500:
427 ab->mode = USB_IDLE;
428 ab->phy.otg->default_a = false;
429 ab->vbus_draw = 0;
430 if (event != UX500_MUSB_RIDB)
431 event = UX500_MUSB_NONE;
432 /* Fallback to default B_IDLE as nothing is connected */
433 ab->phy.otg->state = OTG_STATE_B_IDLE;
434 usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
435 break;
436
437 case USB_LINK_ACA_RID_C_NM_8500:
438 case USB_LINK_ACA_RID_C_HS_8500:
439 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
440 event = UX500_MUSB_RIDC;
441 case USB_LINK_STD_HOST_NC_8500:
442 case USB_LINK_STD_HOST_C_NS_8500:
443 case USB_LINK_STD_HOST_C_S_8500:
444 case USB_LINK_HOST_CHG_NM_8500:
445 case USB_LINK_HOST_CHG_HS_8500:
446 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
447 if (ab->mode == USB_IDLE) {
448 ab->mode = USB_PERIPHERAL;
449 ab8500_usb_peri_phy_en(ab);
450 atomic_notifier_call_chain(&ab->phy.notifier,
451 UX500_MUSB_PREPARE, &ab->vbus_draw);
452 usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
453 }
454 if (event != UX500_MUSB_RIDC)
455 event = UX500_MUSB_VBUS;
456 break;
457
458 case USB_LINK_ACA_RID_A_8500:
459 event = UX500_MUSB_RIDA;
460 case USB_LINK_HM_IDGND_8500:
461 if (ab->mode == USB_IDLE) {
462 ab->mode = USB_HOST;
463 ab8500_usb_host_phy_en(ab);
464 atomic_notifier_call_chain(&ab->phy.notifier,
465 UX500_MUSB_PREPARE, &ab->vbus_draw);
466 }
467 ab->phy.otg->default_a = true;
468 if (event != UX500_MUSB_RIDA)
469 event = UX500_MUSB_ID;
470 atomic_notifier_call_chain(&ab->phy.notifier,
471 event, &ab->vbus_draw);
472 break;
473
474 case USB_LINK_DEDICATED_CHG_8500:
475 ab->mode = USB_DEDICATED_CHG;
476 event = UX500_MUSB_CHARGER;
477 atomic_notifier_call_chain(&ab->phy.notifier,
478 event, &ab->vbus_draw);
479 usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
480 break;
481
482 case USB_LINK_RESERVED_8500:
483 break;
484 }
485
486 return 0;
487}
488
489/*
490 * Connection Sequence:
491 * 1. Link Status Interrupt
492 * 2. Enable AB clock
493 * 3. Enable AB regulators
494 * 4. Enable USB phy
495 * 5. Reset the musb controller
496 * 6. Switch the ULPI GPIO pins to fucntion mode
497 * 7. Enable the musb Peripheral5 clock
498 * 8. Restore MUSB context
499 */
500static int abx500_usb_link_status_update(struct ab8500_usb *ab)
501{
502 u8 reg;
503 int ret = 0;
504
505 if (is_ab8500(ab->ab8500)) {
506 enum ab8500_usb_link_status lsts;
507
508 abx500_get_register_interruptible(ab->dev,
509 AB8500_USB, AB8500_USB_LINE_STAT_REG, ®);
510 lsts = (reg >> 3) & 0x0F;
511 ret = ab8500_usb_link_status_update(ab, lsts);
512 } else if (is_ab8505(ab->ab8500)) {
513 enum ab8505_usb_link_status lsts;
514
515 abx500_get_register_interruptible(ab->dev,
516 AB8500_USB, AB8505_USB_LINE_STAT_REG, ®);
517 lsts = (reg >> 3) & 0x1F;
518 ret = ab8505_usb_link_status_update(ab, lsts);
519 }
520
521 return ret;
522}
523
524/*
525 * Disconnection Sequence:
526 * 1. Disconnect Interrupt
527 * 2. Disable regulators
528 * 3. Disable AB clock
529 * 4. Disable the Phy
530 * 5. Link Status Interrupt
531 * 6. Disable Musb Clock
532 */
533static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
534{
535 struct ab8500_usb *ab = (struct ab8500_usb *) data;
536 enum usb_phy_events event = USB_EVENT_NONE;
537
538 /* Link status will not be updated till phy is disabled. */
539 if (ab->mode == USB_HOST) {
540 ab->phy.otg->default_a = false;
541 ab->vbus_draw = 0;
542 atomic_notifier_call_chain(&ab->phy.notifier,
543 event, &ab->vbus_draw);
544 ab8500_usb_host_phy_dis(ab);
545 ab->mode = USB_IDLE;
546 }
547
548 if (ab->mode == USB_PERIPHERAL) {
549 atomic_notifier_call_chain(&ab->phy.notifier,
550 event, &ab->vbus_draw);
551 ab8500_usb_peri_phy_dis(ab);
552 atomic_notifier_call_chain(&ab->phy.notifier,
553 UX500_MUSB_CLEAN, &ab->vbus_draw);
554 ab->mode = USB_IDLE;
555 ab->phy.otg->default_a = false;
556 ab->vbus_draw = 0;
557 }
558
559 if (is_ab8500_2p0(ab->ab8500)) {
560 if (ab->mode == USB_DEDICATED_CHG) {
561 ab8500_usb_wd_linkstatus(ab,
562 AB8500_BIT_PHY_CTRL_DEVICE_EN);
563 abx500_mask_and_set_register_interruptible(ab->dev,
564 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
565 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
566 }
567 }
568
569 return IRQ_HANDLED;
570}
571
572static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
573{
574 struct ab8500_usb *ab = (struct ab8500_usb *)data;
575
576 abx500_usb_link_status_update(ab);
577
578 return IRQ_HANDLED;
579}
580
581static void ab8500_usb_phy_disable_work(struct work_struct *work)
582{
583 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
584 phy_dis_work);
585
586 if (!ab->phy.otg->host)
587 ab8500_usb_host_phy_dis(ab);
588
589 if (!ab->phy.otg->gadget)
590 ab8500_usb_peri_phy_dis(ab);
591}
592
593static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
594{
595 /* TODO */
596 return 0;
597}
598
599static int ab8500_usb_set_peripheral(struct usb_otg *otg,
600 struct usb_gadget *gadget)
601{
602 struct ab8500_usb *ab;
603
604 if (!otg)
605 return -ENODEV;
606
607 ab = phy_to_ab(otg->usb_phy);
608
609 ab->phy.otg->gadget = gadget;
610
611 /* Some drivers call this function in atomic context.
612 * Do not update ab8500 registers directly till this
613 * is fixed.
614 */
615
616 if ((ab->mode != USB_IDLE) && !gadget) {
617 ab->mode = USB_IDLE;
618 schedule_work(&ab->phy_dis_work);
619 }
620
621 return 0;
622}
623
624static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
625{
626 struct ab8500_usb *ab;
627
628 if (!otg)
629 return -ENODEV;
630
631 ab = phy_to_ab(otg->usb_phy);
632
633 ab->phy.otg->host = host;
634
635 /* Some drivers call this function in atomic context.
636 * Do not update ab8500 registers directly till this
637 * is fixed.
638 */
639
640 if ((ab->mode != USB_IDLE) && !host) {
641 ab->mode = USB_IDLE;
642 schedule_work(&ab->phy_dis_work);
643 }
644
645 return 0;
646}
647
648static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
649{
650 abx500_mask_and_set_register_interruptible(ab->dev,
651 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
652 AB8500_BIT_PHY_CTRL_DEVICE_EN,
653 AB8500_BIT_PHY_CTRL_DEVICE_EN);
654
655 udelay(100);
656
657 abx500_mask_and_set_register_interruptible(ab->dev,
658 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
659 AB8500_BIT_PHY_CTRL_DEVICE_EN,
660 0);
661
662 abx500_mask_and_set_register_interruptible(ab->dev,
663 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
664 AB8500_BIT_PHY_CTRL_HOST_EN,
665 AB8500_BIT_PHY_CTRL_HOST_EN);
666
667 udelay(100);
668
669 abx500_mask_and_set_register_interruptible(ab->dev,
670 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
671 AB8500_BIT_PHY_CTRL_HOST_EN,
672 0);
673}
674
675static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
676{
677 int err;
678
679 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
680 if (IS_ERR(ab->v_ape)) {
681 dev_err(ab->dev, "Could not get v-ape supply\n");
682 err = PTR_ERR(ab->v_ape);
683 return err;
684 }
685
686 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
687 if (IS_ERR(ab->v_ulpi)) {
688 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
689 err = PTR_ERR(ab->v_ulpi);
690 return err;
691 }
692
693 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
694 if (IS_ERR(ab->v_musb)) {
695 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
696 err = PTR_ERR(ab->v_musb);
697 return err;
698 }
699
700 return 0;
701}
702
703static int ab8500_usb_irq_setup(struct platform_device *pdev,
704 struct ab8500_usb *ab)
705{
706 int err;
707 int irq;
708
709 if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
710 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
711 if (irq < 0) {
712 dev_err(&pdev->dev, "Link status irq not found\n");
713 return irq;
714 }
715 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
716 ab8500_usb_link_status_irq,
717 IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
718 "usb-link-status", ab);
719 if (err < 0) {
720 dev_err(ab->dev, "request_irq failed for link status irq\n");
721 return err;
722 }
723 }
724
725 if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
726 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
727 if (irq < 0) {
728 dev_err(&pdev->dev, "ID fall irq not found\n");
729 return irq;
730 }
731 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
732 ab8500_usb_disconnect_irq,
733 IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
734 "usb-id-fall", ab);
735 if (err < 0) {
736 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
737 return err;
738 }
739 }
740
741 if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
742 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
743 if (irq < 0) {
744 dev_err(&pdev->dev, "VBUS fall irq not found\n");
745 return irq;
746 }
747 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
748 ab8500_usb_disconnect_irq,
749 IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
750 "usb-vbus-fall", ab);
751 if (err < 0) {
752 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
753 return err;
754 }
755 }
756
757 return 0;
758}
759
760static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
761{
762 int err;
763
764 /* Enable the PBT/Bank 0x12 access */
765 err = abx500_set_register_interruptible(ab->dev,
766 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
767 if (err < 0)
768 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
769 err);
770
771 err = abx500_set_register_interruptible(ab->dev,
772 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
773 if (err < 0)
774 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
775 err);
776
777 err = abx500_set_register_interruptible(ab->dev,
778 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
779 if (err < 0)
780 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
781 err);
782
783 err = abx500_set_register_interruptible(ab->dev,
784 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
785 if (err < 0)
786 dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
787 err);
788
789 /* Switch to normal mode/disable Bank 0x12 access */
790 err = abx500_set_register_interruptible(ab->dev,
791 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
792 if (err < 0)
793 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
794 err);
795}
796
797static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
798{
799 int err;
800
801 /* Enable the PBT/Bank 0x12 access */
802 err = abx500_mask_and_set_register_interruptible(ab->dev,
803 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
804 0x01, 0x01);
805 if (err < 0)
806 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
807 err);
808
809 err = abx500_mask_and_set_register_interruptible(ab->dev,
810 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
811 0xC8, 0xC8);
812 if (err < 0)
813 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
814 err);
815
816 err = abx500_mask_and_set_register_interruptible(ab->dev,
817 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
818 0x60, 0x60);
819 if (err < 0)
820 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
821 err);
822
823 err = abx500_mask_and_set_register_interruptible(ab->dev,
824 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
825 0xFC, 0x80);
826
827 if (err < 0)
828 dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
829 err);
830
831 /* Switch to normal mode/disable Bank 0x12 access */
832 err = abx500_mask_and_set_register_interruptible(ab->dev,
833 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
834 0x00, 0x00);
835 if (err < 0)
836 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
837 err);
838}
839
840static int ab8500_usb_probe(struct platform_device *pdev)
841{
842 struct ab8500_usb *ab;
843 struct ab8500 *ab8500;
844 struct usb_otg *otg;
845 int err;
846 int rev;
847
848 ab8500 = dev_get_drvdata(pdev->dev.parent);
849 rev = abx500_get_chip_id(&pdev->dev);
850
851 if (is_ab8500_1p1_or_earlier(ab8500)) {
852 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
853 return -ENODEV;
854 }
855
856 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
857 if (!ab)
858 return -ENOMEM;
859
860 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
861 if (!otg)
862 return -ENOMEM;
863
864 ab->dev = &pdev->dev;
865 ab->ab8500 = ab8500;
866 ab->phy.dev = ab->dev;
867 ab->phy.otg = otg;
868 ab->phy.label = "ab8500";
869 ab->phy.set_suspend = ab8500_usb_set_suspend;
870 ab->phy.otg->state = OTG_STATE_UNDEFINED;
871
872 otg->usb_phy = &ab->phy;
873 otg->set_host = ab8500_usb_set_host;
874 otg->set_peripheral = ab8500_usb_set_peripheral;
875
876 if (is_ab8500(ab->ab8500)) {
877 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
878 AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
879 AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
880 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
881 } else if (is_ab8505(ab->ab8500)) {
882 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
883 AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
884 AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
885 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
886 }
887
888 /* Disable regulator voltage setting for AB8500 <= v2.0 */
889 if (is_ab8500_2p0_or_earlier(ab->ab8500))
890 ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
891
892 platform_set_drvdata(pdev, ab);
893
894 /* all: Disable phy when called from set_host and set_peripheral */
895 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
896
897 err = ab8500_usb_regulator_get(ab);
898 if (err)
899 return err;
900
901 ab->sysclk = devm_clk_get(ab->dev, "sysclk");
902 if (IS_ERR(ab->sysclk)) {
903 dev_err(ab->dev, "Could not get sysclk.\n");
904 return PTR_ERR(ab->sysclk);
905 }
906
907 err = ab8500_usb_irq_setup(pdev, ab);
908 if (err < 0)
909 return err;
910
911 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
912 if (err) {
913 dev_err(&pdev->dev, "Can't register transceiver\n");
914 return err;
915 }
916
917 if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
918 /* Phy tuning values for AB8500 > v2.0 */
919 ab8500_usb_set_ab8500_tuning_values(ab);
920 else if (is_ab8505(ab->ab8500))
921 /* Phy tuning values for AB8505 */
922 ab8500_usb_set_ab8505_tuning_values(ab);
923
924 /* Needed to enable ID detection. */
925 ab8500_usb_wd_workaround(ab);
926
927 /*
928 * This is required for usb-link-status to work properly when a
929 * cable is connected at boot time.
930 */
931 ab8500_usb_restart_phy(ab);
932
933 abx500_usb_link_status_update(ab);
934
935 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
936
937 return 0;
938}
939
940static int ab8500_usb_remove(struct platform_device *pdev)
941{
942 struct ab8500_usb *ab = platform_get_drvdata(pdev);
943
944 cancel_work_sync(&ab->phy_dis_work);
945
946 usb_remove_phy(&ab->phy);
947
948 if (ab->mode == USB_HOST)
949 ab8500_usb_host_phy_dis(ab);
950 else if (ab->mode == USB_PERIPHERAL)
951 ab8500_usb_peri_phy_dis(ab);
952
953 return 0;
954}
955
956static const struct platform_device_id ab8500_usb_devtype[] = {
957 { .name = "ab8500-usb", },
958 { /* sentinel */ }
959};
960MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
961
962static struct platform_driver ab8500_usb_driver = {
963 .probe = ab8500_usb_probe,
964 .remove = ab8500_usb_remove,
965 .id_table = ab8500_usb_devtype,
966 .driver = {
967 .name = "abx5x0-usb",
968 },
969};
970
971static int __init ab8500_usb_init(void)
972{
973 return platform_driver_register(&ab8500_usb_driver);
974}
975subsys_initcall(ab8500_usb_init);
976
977static void __exit ab8500_usb_exit(void)
978{
979 platform_driver_unregister(&ab8500_usb_driver);
980}
981module_exit(ab8500_usb_exit);
982
983MODULE_AUTHOR("ST-Ericsson AB");
984MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
985MODULE_LICENSE("GPL");