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/*
2 * drivers/usb/otg/ab8500_usb.c
3 *
4 * USB transceiver driver for AB8500 family chips
5 *
6 * Copyright (C) 2010-2013 ST-Ericsson AB
7 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
8 * Avinash Kumar <avinash.kumar@stericsson.com>
9 * Thirupathi Chippakurthy <thirupathi.chippakurthy@stericsson.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 */
26
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/usb/otg.h>
30#include <linux/slab.h>
31#include <linux/notifier.h>
32#include <linux/interrupt.h>
33#include <linux/delay.h>
34#include <linux/clk.h>
35#include <linux/err.h>
36#include <linux/mfd/abx500.h>
37#include <linux/mfd/abx500/ab8500.h>
38#include <linux/usb/musb-ux500.h>
39#include <linux/regulator/consumer.h>
40#include <linux/pinctrl/consumer.h>
41
42/* Bank AB8500_SYS_CTRL2_BLOCK */
43#define AB8500_MAIN_WD_CTRL_REG 0x01
44
45/* Bank AB8500_USB */
46#define AB8500_USB_LINE_STAT_REG 0x80
47#define AB8505_USB_LINE_STAT_REG 0x94
48#define AB8540_USB_LINK_STAT_REG 0x94
49#define AB9540_USB_LINK_STAT_REG 0x94
50#define AB8540_USB_OTG_CTL_REG 0x87
51#define AB8500_USB_PHY_CTRL_REG 0x8A
52#define AB8540_VBUS_CTRL_REG 0x82
53
54/* Bank AB8500_DEVELOPMENT */
55#define AB8500_BANK12_ACCESS 0x00
56
57/* Bank AB8500_DEBUG */
58#define AB8540_DEBUG 0x32
59#define AB8500_USB_PHY_TUNE1 0x05
60#define AB8500_USB_PHY_TUNE2 0x06
61#define AB8500_USB_PHY_TUNE3 0x07
62
63/* Bank AB8500_INTERRUPT */
64#define AB8500_IT_SOURCE2_REG 0x01
65
66#define AB8500_BIT_OTG_STAT_ID (1 << 0)
67#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
68#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
69#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
70#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
71#define AB8500_BIT_SOURCE2_VBUSDET (1 << 7)
72#define AB8540_BIT_OTG_CTL_VBUS_VALID_ENA (1 << 0)
73#define AB8540_BIT_OTG_CTL_ID_HOST_ENA (1 << 1)
74#define AB8540_BIT_OTG_CTL_ID_DEV_ENA (1 << 5)
75#define AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA (1 << 0)
76
77#define AB8500_WD_KICK_DELAY_US 100 /* usec */
78#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
79#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
80
81/* Usb line status register */
82enum ab8500_usb_link_status {
83 USB_LINK_NOT_CONFIGURED_8500 = 0,
84 USB_LINK_STD_HOST_NC_8500,
85 USB_LINK_STD_HOST_C_NS_8500,
86 USB_LINK_STD_HOST_C_S_8500,
87 USB_LINK_HOST_CHG_NM_8500,
88 USB_LINK_HOST_CHG_HS_8500,
89 USB_LINK_HOST_CHG_HS_CHIRP_8500,
90 USB_LINK_DEDICATED_CHG_8500,
91 USB_LINK_ACA_RID_A_8500,
92 USB_LINK_ACA_RID_B_8500,
93 USB_LINK_ACA_RID_C_NM_8500,
94 USB_LINK_ACA_RID_C_HS_8500,
95 USB_LINK_ACA_RID_C_HS_CHIRP_8500,
96 USB_LINK_HM_IDGND_8500,
97 USB_LINK_RESERVED_8500,
98 USB_LINK_NOT_VALID_LINK_8500,
99};
100
101enum ab8505_usb_link_status {
102 USB_LINK_NOT_CONFIGURED_8505 = 0,
103 USB_LINK_STD_HOST_NC_8505,
104 USB_LINK_STD_HOST_C_NS_8505,
105 USB_LINK_STD_HOST_C_S_8505,
106 USB_LINK_CDP_8505,
107 USB_LINK_RESERVED0_8505,
108 USB_LINK_RESERVED1_8505,
109 USB_LINK_DEDICATED_CHG_8505,
110 USB_LINK_ACA_RID_A_8505,
111 USB_LINK_ACA_RID_B_8505,
112 USB_LINK_ACA_RID_C_NM_8505,
113 USB_LINK_RESERVED2_8505,
114 USB_LINK_RESERVED3_8505,
115 USB_LINK_HM_IDGND_8505,
116 USB_LINK_CHARGERPORT_NOT_OK_8505,
117 USB_LINK_CHARGER_DM_HIGH_8505,
118 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
119 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
120 USB_LINK_STD_UPSTREAM_8505,
121 USB_LINK_CHARGER_SE1_8505,
122 USB_LINK_CARKIT_CHGR_1_8505,
123 USB_LINK_CARKIT_CHGR_2_8505,
124 USB_LINK_ACA_DOCK_CHGR_8505,
125 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
126 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
127 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
128 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
129 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
130};
131
132enum ab8540_usb_link_status {
133 USB_LINK_NOT_CONFIGURED_8540 = 0,
134 USB_LINK_STD_HOST_NC_8540,
135 USB_LINK_STD_HOST_C_NS_8540,
136 USB_LINK_STD_HOST_C_S_8540,
137 USB_LINK_CDP_8540,
138 USB_LINK_RESERVED0_8540,
139 USB_LINK_RESERVED1_8540,
140 USB_LINK_DEDICATED_CHG_8540,
141 USB_LINK_ACA_RID_A_8540,
142 USB_LINK_ACA_RID_B_8540,
143 USB_LINK_ACA_RID_C_NM_8540,
144 USB_LINK_RESERVED2_8540,
145 USB_LINK_RESERVED3_8540,
146 USB_LINK_HM_IDGND_8540,
147 USB_LINK_CHARGERPORT_NOT_OK_8540,
148 USB_LINK_CHARGER_DM_HIGH_8540,
149 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540,
150 USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_8540,
151 USB_LINK_STD_UPSTREAM_8540,
152 USB_LINK_CHARGER_SE1_8540,
153 USB_LINK_CARKIT_CHGR_1_8540,
154 USB_LINK_CARKIT_CHGR_2_8540,
155 USB_LINK_ACA_DOCK_CHGR_8540,
156 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8540,
157 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8540,
158 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8540,
159 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8540,
160 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8540
161};
162
163enum ab9540_usb_link_status {
164 USB_LINK_NOT_CONFIGURED_9540 = 0,
165 USB_LINK_STD_HOST_NC_9540,
166 USB_LINK_STD_HOST_C_NS_9540,
167 USB_LINK_STD_HOST_C_S_9540,
168 USB_LINK_CDP_9540,
169 USB_LINK_RESERVED0_9540,
170 USB_LINK_RESERVED1_9540,
171 USB_LINK_DEDICATED_CHG_9540,
172 USB_LINK_ACA_RID_A_9540,
173 USB_LINK_ACA_RID_B_9540,
174 USB_LINK_ACA_RID_C_NM_9540,
175 USB_LINK_RESERVED2_9540,
176 USB_LINK_RESERVED3_9540,
177 USB_LINK_HM_IDGND_9540,
178 USB_LINK_CHARGERPORT_NOT_OK_9540,
179 USB_LINK_CHARGER_DM_HIGH_9540,
180 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_9540,
181 USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_9540,
182 USB_LINK_STD_UPSTREAM_9540,
183 USB_LINK_CHARGER_SE1_9540,
184 USB_LINK_CARKIT_CHGR_1_9540,
185 USB_LINK_CARKIT_CHGR_2_9540,
186 USB_LINK_ACA_DOCK_CHGR_9540,
187 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_9540,
188 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_9540,
189 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_9540,
190 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_9540,
191 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_9540
192};
193
194enum ab8500_usb_mode {
195 USB_IDLE = 0,
196 USB_PERIPHERAL,
197 USB_HOST,
198 USB_DEDICATED_CHG
199};
200
201/* Register USB_LINK_STATUS interrupt */
202#define AB8500_USB_FLAG_USE_LINK_STATUS_IRQ (1 << 0)
203/* Register ID_WAKEUP_F interrupt */
204#define AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ (1 << 1)
205/* Register VBUS_DET_F interrupt */
206#define AB8500_USB_FLAG_USE_VBUS_DET_IRQ (1 << 2)
207/* Driver is using the ab-iddet driver*/
208#define AB8500_USB_FLAG_USE_AB_IDDET (1 << 3)
209/* Enable setting regulators voltage */
210#define AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE (1 << 4)
211/* Enable the check_vbus_status workaround */
212#define AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS (1 << 5)
213/* Enable the vbus host workaround */
214#define AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK (1 << 6)
215
216struct ab8500_usb {
217 struct usb_phy phy;
218 struct device *dev;
219 struct ab8500 *ab8500;
220 unsigned vbus_draw;
221 struct work_struct phy_dis_work;
222 struct work_struct vbus_event_work;
223 enum ab8500_usb_mode mode;
224 struct clk *sysclk;
225 struct regulator *v_ape;
226 struct regulator *v_musb;
227 struct regulator *v_ulpi;
228 int saved_v_ulpi;
229 int previous_link_status_state;
230 struct pinctrl *pinctrl;
231 struct pinctrl_state *pins_sleep;
232 bool enabled_charging_detection;
233 unsigned int flags;
234};
235
236static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
237{
238 return container_of(x, struct ab8500_usb, phy);
239}
240
241static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
242{
243 abx500_set_register_interruptible(ab->dev,
244 AB8500_SYS_CTRL2_BLOCK,
245 AB8500_MAIN_WD_CTRL_REG,
246 AB8500_BIT_WD_CTRL_ENABLE);
247
248 udelay(AB8500_WD_KICK_DELAY_US);
249
250 abx500_set_register_interruptible(ab->dev,
251 AB8500_SYS_CTRL2_BLOCK,
252 AB8500_MAIN_WD_CTRL_REG,
253 (AB8500_BIT_WD_CTRL_ENABLE
254 | AB8500_BIT_WD_CTRL_KICK));
255
256 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
257
258 abx500_set_register_interruptible(ab->dev,
259 AB8500_SYS_CTRL2_BLOCK,
260 AB8500_MAIN_WD_CTRL_REG,
261 0);
262}
263
264static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
265{
266 int ret, volt;
267
268 ret = regulator_enable(ab->v_ape);
269 if (ret)
270 dev_err(ab->dev, "Failed to enable v-ape\n");
271
272 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
273 ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
274 if (ab->saved_v_ulpi < 0)
275 dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
276
277 ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
278 if (ret < 0)
279 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
280 ret);
281
282 ret = regulator_set_optimum_mode(ab->v_ulpi, 28000);
283 if (ret < 0)
284 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
285 ret);
286 }
287
288 ret = regulator_enable(ab->v_ulpi);
289 if (ret)
290 dev_err(ab->dev, "Failed to enable vddulpivio18\n");
291
292 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
293 volt = regulator_get_voltage(ab->v_ulpi);
294 if ((volt != 1300000) && (volt != 1350000))
295 dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
296 volt);
297 }
298
299 ret = regulator_enable(ab->v_musb);
300 if (ret)
301 dev_err(ab->dev, "Failed to enable musb_1v8\n");
302}
303
304static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
305{
306 int ret;
307
308 regulator_disable(ab->v_musb);
309
310 regulator_disable(ab->v_ulpi);
311
312 /* USB is not the only consumer of Vintcore, restore old settings */
313 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
314 if (ab->saved_v_ulpi > 0) {
315 ret = regulator_set_voltage(ab->v_ulpi,
316 ab->saved_v_ulpi, ab->saved_v_ulpi);
317 if (ret < 0)
318 dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
319 ab->saved_v_ulpi, ret);
320 }
321
322 ret = regulator_set_optimum_mode(ab->v_ulpi, 0);
323 if (ret < 0)
324 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
325 ret);
326 }
327
328 regulator_disable(ab->v_ape);
329}
330
331static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
332{
333 /* Workaround for v2.0 bug # 31952 */
334 if (is_ab8500_2p0(ab->ab8500)) {
335 abx500_mask_and_set_register_interruptible(ab->dev,
336 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
337 bit, bit);
338 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
339 }
340}
341
342static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
343{
344 u8 bit;
345 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
346 AB8500_BIT_PHY_CTRL_DEVICE_EN;
347
348 /* mux and configure USB pins to DEFAULT state */
349 ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
350 if (IS_ERR(ab->pinctrl))
351 dev_err(ab->dev, "could not get/set default pinstate\n");
352
353 if (clk_prepare_enable(ab->sysclk))
354 dev_err(ab->dev, "can't prepare/enable clock\n");
355
356 ab8500_usb_regulator_enable(ab);
357
358 abx500_mask_and_set_register_interruptible(ab->dev,
359 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
360 bit, bit);
361
362 if (ab->flags & AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK) {
363 if (sel_host)
364 abx500_set_register_interruptible(ab->dev,
365 AB8500_USB, AB8540_USB_OTG_CTL_REG,
366 AB8540_BIT_OTG_CTL_VBUS_VALID_ENA |
367 AB8540_BIT_OTG_CTL_ID_HOST_ENA |
368 AB8540_BIT_OTG_CTL_ID_DEV_ENA);
369 }
370}
371
372static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
373{
374 u8 bit;
375 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
376 AB8500_BIT_PHY_CTRL_DEVICE_EN;
377
378 ab8500_usb_wd_linkstatus(ab, bit);
379
380 abx500_mask_and_set_register_interruptible(ab->dev,
381 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
382 bit, 0);
383
384 /* Needed to disable the phy.*/
385 ab8500_usb_wd_workaround(ab);
386
387 clk_disable_unprepare(ab->sysclk);
388
389 ab8500_usb_regulator_disable(ab);
390
391 if (!IS_ERR(ab->pinctrl)) {
392 /* configure USB pins to SLEEP state */
393 ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
394 PINCTRL_STATE_SLEEP);
395
396 if (IS_ERR(ab->pins_sleep))
397 dev_dbg(ab->dev, "could not get sleep pinstate\n");
398 else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
399 dev_err(ab->dev, "could not set pins to sleep state\n");
400
401 /*
402 * as USB pins are shared with iddet, release them to allow
403 * iddet to request them
404 */
405 pinctrl_put(ab->pinctrl);
406 }
407}
408
409#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true)
410#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
411#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false)
412#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
413
414static int ab9540_usb_link_status_update(struct ab8500_usb *ab,
415 enum ab9540_usb_link_status lsts)
416{
417 enum ux500_musb_vbus_id_status event = 0;
418
419 dev_dbg(ab->dev, "ab9540_usb_link_status_update %d\n", lsts);
420
421 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_9540 &&
422 (lsts == USB_LINK_STD_HOST_C_NS_9540 ||
423 lsts == USB_LINK_STD_HOST_NC_9540))
424 return 0;
425
426 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_9540 &&
427 (lsts == USB_LINK_STD_HOST_NC_9540))
428 return 0;
429
430 ab->previous_link_status_state = lsts;
431
432 switch (lsts) {
433 case USB_LINK_ACA_RID_B_9540:
434 event = UX500_MUSB_RIDB;
435 case USB_LINK_NOT_CONFIGURED_9540:
436 case USB_LINK_RESERVED0_9540:
437 case USB_LINK_RESERVED1_9540:
438 case USB_LINK_RESERVED2_9540:
439 case USB_LINK_RESERVED3_9540:
440 if (ab->mode == USB_PERIPHERAL)
441 atomic_notifier_call_chain(&ab->phy.notifier,
442 UX500_MUSB_CLEAN, &ab->vbus_draw);
443 ab->mode = USB_IDLE;
444 ab->phy.otg->default_a = false;
445 ab->vbus_draw = 0;
446 if (event != UX500_MUSB_RIDB)
447 event = UX500_MUSB_NONE;
448 /* Fallback to default B_IDLE as nothing is connected. */
449 ab->phy.state = OTG_STATE_B_IDLE;
450 break;
451
452 case USB_LINK_ACA_RID_C_NM_9540:
453 event = UX500_MUSB_RIDC;
454 case USB_LINK_STD_HOST_NC_9540:
455 case USB_LINK_STD_HOST_C_NS_9540:
456 case USB_LINK_STD_HOST_C_S_9540:
457 case USB_LINK_CDP_9540:
458 if (ab->mode == USB_HOST) {
459 ab->mode = USB_PERIPHERAL;
460 ab8500_usb_host_phy_dis(ab);
461 ab8500_usb_peri_phy_en(ab);
462 atomic_notifier_call_chain(&ab->phy.notifier,
463 UX500_MUSB_PREPARE, &ab->vbus_draw);
464 }
465 if (ab->mode == USB_IDLE) {
466 ab->mode = USB_PERIPHERAL;
467 ab8500_usb_peri_phy_en(ab);
468 atomic_notifier_call_chain(&ab->phy.notifier,
469 UX500_MUSB_PREPARE, &ab->vbus_draw);
470 }
471 if (event != UX500_MUSB_RIDC)
472 event = UX500_MUSB_VBUS;
473 break;
474
475 case USB_LINK_ACA_RID_A_9540:
476 event = UX500_MUSB_RIDA;
477 case USB_LINK_HM_IDGND_9540:
478 case USB_LINK_STD_UPSTREAM_9540:
479 if (ab->mode == USB_PERIPHERAL) {
480 ab->mode = USB_HOST;
481 ab8500_usb_peri_phy_dis(ab);
482 ab8500_usb_host_phy_en(ab);
483 atomic_notifier_call_chain(&ab->phy.notifier,
484 UX500_MUSB_PREPARE, &ab->vbus_draw);
485 }
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
496 atomic_notifier_call_chain(&ab->phy.notifier,
497 event, &ab->vbus_draw);
498 break;
499
500 case USB_LINK_DEDICATED_CHG_9540:
501 ab->mode = USB_DEDICATED_CHG;
502 event = UX500_MUSB_CHARGER;
503 atomic_notifier_call_chain(&ab->phy.notifier,
504 event, &ab->vbus_draw);
505 break;
506
507 case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_9540:
508 case USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_9540:
509 if (!(is_ab9540_2p0_or_earlier(ab->ab8500))) {
510 event = UX500_MUSB_NONE;
511 if (ab->mode == USB_HOST) {
512 ab->phy.otg->default_a = false;
513 ab->vbus_draw = 0;
514 atomic_notifier_call_chain(&ab->phy.notifier,
515 event, &ab->vbus_draw);
516 ab8500_usb_host_phy_dis(ab);
517 ab->mode = USB_IDLE;
518 }
519 if (ab->mode == USB_PERIPHERAL) {
520 atomic_notifier_call_chain(&ab->phy.notifier,
521 event, &ab->vbus_draw);
522 ab8500_usb_peri_phy_dis(ab);
523 atomic_notifier_call_chain(&ab->phy.notifier,
524 UX500_MUSB_CLEAN,
525 &ab->vbus_draw);
526 ab->mode = USB_IDLE;
527 ab->phy.otg->default_a = false;
528 ab->vbus_draw = 0;
529 }
530 }
531 break;
532
533 default:
534 break;
535 }
536
537 return 0;
538}
539
540static int ab8540_usb_link_status_update(struct ab8500_usb *ab,
541 enum ab8540_usb_link_status lsts)
542{
543 enum ux500_musb_vbus_id_status event = 0;
544
545 dev_dbg(ab->dev, "ab8540_usb_link_status_update %d\n", lsts);
546
547 if (ab->enabled_charging_detection) {
548 /* Disable USB Charger detection */
549 abx500_mask_and_set_register_interruptible(ab->dev,
550 AB8500_USB, AB8540_VBUS_CTRL_REG,
551 AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA, 0x00);
552 ab->enabled_charging_detection = false;
553 }
554
555 /*
556 * Spurious link_status interrupts are seen in case of a
557 * disconnection of a device in IDGND and RIDA stage
558 */
559 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8540 &&
560 (lsts == USB_LINK_STD_HOST_C_NS_8540 ||
561 lsts == USB_LINK_STD_HOST_NC_8540))
562 return 0;
563
564 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8540 &&
565 (lsts == USB_LINK_STD_HOST_NC_8540))
566 return 0;
567
568 ab->previous_link_status_state = lsts;
569
570 switch (lsts) {
571 case USB_LINK_ACA_RID_B_8540:
572 event = UX500_MUSB_RIDB;
573 case USB_LINK_NOT_CONFIGURED_8540:
574 case USB_LINK_RESERVED0_8540:
575 case USB_LINK_RESERVED1_8540:
576 case USB_LINK_RESERVED2_8540:
577 case USB_LINK_RESERVED3_8540:
578 ab->mode = USB_IDLE;
579 ab->phy.otg->default_a = false;
580 ab->vbus_draw = 0;
581 if (event != UX500_MUSB_RIDB)
582 event = UX500_MUSB_NONE;
583 /*
584 * Fallback to default B_IDLE as nothing
585 * is connected
586 */
587 ab->phy.state = OTG_STATE_B_IDLE;
588 break;
589
590 case USB_LINK_ACA_RID_C_NM_8540:
591 event = UX500_MUSB_RIDC;
592 case USB_LINK_STD_HOST_NC_8540:
593 case USB_LINK_STD_HOST_C_NS_8540:
594 case USB_LINK_STD_HOST_C_S_8540:
595 case USB_LINK_CDP_8540:
596 if (ab->mode == USB_IDLE) {
597 ab->mode = USB_PERIPHERAL;
598 ab8500_usb_peri_phy_en(ab);
599 atomic_notifier_call_chain(&ab->phy.notifier,
600 UX500_MUSB_PREPARE, &ab->vbus_draw);
601 }
602 if (event != UX500_MUSB_RIDC)
603 event = UX500_MUSB_VBUS;
604 break;
605
606 case USB_LINK_ACA_RID_A_8540:
607 case USB_LINK_ACA_DOCK_CHGR_8540:
608 event = UX500_MUSB_RIDA;
609 case USB_LINK_HM_IDGND_8540:
610 case USB_LINK_STD_UPSTREAM_8540:
611 if (ab->mode == USB_IDLE) {
612 ab->mode = USB_HOST;
613 ab8500_usb_host_phy_en(ab);
614 atomic_notifier_call_chain(&ab->phy.notifier,
615 UX500_MUSB_PREPARE, &ab->vbus_draw);
616 }
617 ab->phy.otg->default_a = true;
618 if (event != UX500_MUSB_RIDA)
619 event = UX500_MUSB_ID;
620 atomic_notifier_call_chain(&ab->phy.notifier,
621 event, &ab->vbus_draw);
622 break;
623
624 case USB_LINK_DEDICATED_CHG_8540:
625 ab->mode = USB_DEDICATED_CHG;
626 event = UX500_MUSB_CHARGER;
627 atomic_notifier_call_chain(&ab->phy.notifier,
628 event, &ab->vbus_draw);
629 break;
630
631 case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540:
632 case USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_8540:
633 event = UX500_MUSB_NONE;
634 if (ab->mode == USB_HOST) {
635 ab->phy.otg->default_a = false;
636 ab->vbus_draw = 0;
637 atomic_notifier_call_chain(&ab->phy.notifier,
638 event, &ab->vbus_draw);
639 ab8500_usb_host_phy_dis(ab);
640 ab->mode = USB_IDLE;
641 }
642 if (ab->mode == USB_PERIPHERAL) {
643 atomic_notifier_call_chain(&ab->phy.notifier,
644 event, &ab->vbus_draw);
645 ab8500_usb_peri_phy_dis(ab);
646 atomic_notifier_call_chain(&ab->phy.notifier,
647 UX500_MUSB_CLEAN, &ab->vbus_draw);
648 ab->mode = USB_IDLE;
649 ab->phy.otg->default_a = false;
650 ab->vbus_draw = 0;
651 }
652 break;
653
654 default:
655 event = UX500_MUSB_NONE;
656 break;
657 }
658
659 return 0;
660}
661
662static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
663 enum ab8505_usb_link_status lsts)
664{
665 enum ux500_musb_vbus_id_status event = 0;
666
667 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
668
669 /*
670 * Spurious link_status interrupts are seen at the time of
671 * disconnection of a device in RIDA state
672 */
673 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
674 (lsts == USB_LINK_STD_HOST_NC_8505))
675 return 0;
676
677 ab->previous_link_status_state = lsts;
678
679 switch (lsts) {
680 case USB_LINK_ACA_RID_B_8505:
681 event = UX500_MUSB_RIDB;
682 case USB_LINK_NOT_CONFIGURED_8505:
683 case USB_LINK_RESERVED0_8505:
684 case USB_LINK_RESERVED1_8505:
685 case USB_LINK_RESERVED2_8505:
686 case USB_LINK_RESERVED3_8505:
687 ab->mode = USB_IDLE;
688 ab->phy.otg->default_a = false;
689 ab->vbus_draw = 0;
690 if (event != UX500_MUSB_RIDB)
691 event = UX500_MUSB_NONE;
692 /*
693 * Fallback to default B_IDLE as nothing
694 * is connected
695 */
696 ab->phy.state = OTG_STATE_B_IDLE;
697 break;
698
699 case USB_LINK_ACA_RID_C_NM_8505:
700 event = UX500_MUSB_RIDC;
701 case USB_LINK_STD_HOST_NC_8505:
702 case USB_LINK_STD_HOST_C_NS_8505:
703 case USB_LINK_STD_HOST_C_S_8505:
704 case USB_LINK_CDP_8505:
705 if (ab->mode == USB_IDLE) {
706 ab->mode = USB_PERIPHERAL;
707 ab8500_usb_peri_phy_en(ab);
708 atomic_notifier_call_chain(&ab->phy.notifier,
709 UX500_MUSB_PREPARE, &ab->vbus_draw);
710 }
711 if (event != UX500_MUSB_RIDC)
712 event = UX500_MUSB_VBUS;
713 break;
714
715 case USB_LINK_ACA_RID_A_8505:
716 case USB_LINK_ACA_DOCK_CHGR_8505:
717 event = UX500_MUSB_RIDA;
718 case USB_LINK_HM_IDGND_8505:
719 if (ab->mode == USB_IDLE) {
720 ab->mode = USB_HOST;
721 ab8500_usb_host_phy_en(ab);
722 atomic_notifier_call_chain(&ab->phy.notifier,
723 UX500_MUSB_PREPARE, &ab->vbus_draw);
724 }
725 ab->phy.otg->default_a = true;
726 if (event != UX500_MUSB_RIDA)
727 event = UX500_MUSB_ID;
728 atomic_notifier_call_chain(&ab->phy.notifier,
729 event, &ab->vbus_draw);
730 break;
731
732 case USB_LINK_DEDICATED_CHG_8505:
733 ab->mode = USB_DEDICATED_CHG;
734 event = UX500_MUSB_CHARGER;
735 atomic_notifier_call_chain(&ab->phy.notifier,
736 event, &ab->vbus_draw);
737 break;
738
739 default:
740 break;
741 }
742
743 return 0;
744}
745
746static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
747 enum ab8500_usb_link_status lsts)
748{
749 enum ux500_musb_vbus_id_status event = 0;
750
751 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
752
753 /*
754 * Spurious link_status interrupts are seen in case of a
755 * disconnection of a device in IDGND and RIDA stage
756 */
757 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
758 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
759 lsts == USB_LINK_STD_HOST_NC_8500))
760 return 0;
761
762 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
763 lsts == USB_LINK_STD_HOST_NC_8500)
764 return 0;
765
766 ab->previous_link_status_state = lsts;
767
768 switch (lsts) {
769 case USB_LINK_ACA_RID_B_8500:
770 event = UX500_MUSB_RIDB;
771 case USB_LINK_NOT_CONFIGURED_8500:
772 case USB_LINK_NOT_VALID_LINK_8500:
773 ab->mode = USB_IDLE;
774 ab->phy.otg->default_a = false;
775 ab->vbus_draw = 0;
776 if (event != UX500_MUSB_RIDB)
777 event = UX500_MUSB_NONE;
778 /* Fallback to default B_IDLE as nothing is connected */
779 ab->phy.state = OTG_STATE_B_IDLE;
780 break;
781
782 case USB_LINK_ACA_RID_C_NM_8500:
783 case USB_LINK_ACA_RID_C_HS_8500:
784 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
785 event = UX500_MUSB_RIDC;
786 case USB_LINK_STD_HOST_NC_8500:
787 case USB_LINK_STD_HOST_C_NS_8500:
788 case USB_LINK_STD_HOST_C_S_8500:
789 case USB_LINK_HOST_CHG_NM_8500:
790 case USB_LINK_HOST_CHG_HS_8500:
791 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
792 if (ab->mode == USB_IDLE) {
793 ab->mode = USB_PERIPHERAL;
794 ab8500_usb_peri_phy_en(ab);
795 atomic_notifier_call_chain(&ab->phy.notifier,
796 UX500_MUSB_PREPARE, &ab->vbus_draw);
797 }
798 if (event != UX500_MUSB_RIDC)
799 event = UX500_MUSB_VBUS;
800 break;
801
802 case USB_LINK_ACA_RID_A_8500:
803 event = UX500_MUSB_RIDA;
804 case USB_LINK_HM_IDGND_8500:
805 if (ab->mode == USB_IDLE) {
806 ab->mode = USB_HOST;
807 ab8500_usb_host_phy_en(ab);
808 atomic_notifier_call_chain(&ab->phy.notifier,
809 UX500_MUSB_PREPARE, &ab->vbus_draw);
810 }
811 ab->phy.otg->default_a = true;
812 if (event != UX500_MUSB_RIDA)
813 event = UX500_MUSB_ID;
814 atomic_notifier_call_chain(&ab->phy.notifier,
815 event, &ab->vbus_draw);
816 break;
817
818 case USB_LINK_DEDICATED_CHG_8500:
819 ab->mode = USB_DEDICATED_CHG;
820 event = UX500_MUSB_CHARGER;
821 atomic_notifier_call_chain(&ab->phy.notifier,
822 event, &ab->vbus_draw);
823 break;
824
825 case USB_LINK_RESERVED_8500:
826 break;
827 }
828
829 return 0;
830}
831
832/*
833 * Connection Sequence:
834 * 1. Link Status Interrupt
835 * 2. Enable AB clock
836 * 3. Enable AB regulators
837 * 4. Enable USB phy
838 * 5. Reset the musb controller
839 * 6. Switch the ULPI GPIO pins to fucntion mode
840 * 7. Enable the musb Peripheral5 clock
841 * 8. Restore MUSB context
842 */
843static int abx500_usb_link_status_update(struct ab8500_usb *ab)
844{
845 u8 reg;
846 int ret = 0;
847
848 if (is_ab8500(ab->ab8500)) {
849 enum ab8500_usb_link_status lsts;
850
851 abx500_get_register_interruptible(ab->dev,
852 AB8500_USB, AB8500_USB_LINE_STAT_REG, ®);
853 lsts = (reg >> 3) & 0x0F;
854 ret = ab8500_usb_link_status_update(ab, lsts);
855 } else if (is_ab8505(ab->ab8500)) {
856 enum ab8505_usb_link_status lsts;
857
858 abx500_get_register_interruptible(ab->dev,
859 AB8500_USB, AB8505_USB_LINE_STAT_REG, ®);
860 lsts = (reg >> 3) & 0x1F;
861 ret = ab8505_usb_link_status_update(ab, lsts);
862 } else if (is_ab8540(ab->ab8500)) {
863 enum ab8540_usb_link_status lsts;
864
865 abx500_get_register_interruptible(ab->dev,
866 AB8500_USB, AB8540_USB_LINK_STAT_REG, ®);
867 lsts = (reg >> 3) & 0xFF;
868 ret = ab8540_usb_link_status_update(ab, lsts);
869 } else if (is_ab9540(ab->ab8500)) {
870 enum ab9540_usb_link_status lsts;
871
872 abx500_get_register_interruptible(ab->dev,
873 AB8500_USB, AB9540_USB_LINK_STAT_REG, ®);
874 lsts = (reg >> 3) & 0xFF;
875 ret = ab9540_usb_link_status_update(ab, lsts);
876 }
877
878 return ret;
879}
880
881/*
882 * Disconnection Sequence:
883 * 1. Disconect Interrupt
884 * 2. Disable regulators
885 * 3. Disable AB clock
886 * 4. Disable the Phy
887 * 5. Link Status Interrupt
888 * 6. Disable Musb Clock
889 */
890static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
891{
892 struct ab8500_usb *ab = (struct ab8500_usb *) data;
893 enum usb_phy_events event = UX500_MUSB_NONE;
894
895 /* Link status will not be updated till phy is disabled. */
896 if (ab->mode == USB_HOST) {
897 ab->phy.otg->default_a = false;
898 ab->vbus_draw = 0;
899 atomic_notifier_call_chain(&ab->phy.notifier,
900 event, &ab->vbus_draw);
901 ab8500_usb_host_phy_dis(ab);
902 ab->mode = USB_IDLE;
903 }
904
905 if (ab->mode == USB_PERIPHERAL) {
906 atomic_notifier_call_chain(&ab->phy.notifier,
907 event, &ab->vbus_draw);
908 ab8500_usb_peri_phy_dis(ab);
909 atomic_notifier_call_chain(&ab->phy.notifier,
910 UX500_MUSB_CLEAN, &ab->vbus_draw);
911 ab->mode = USB_IDLE;
912 ab->phy.otg->default_a = false;
913 ab->vbus_draw = 0;
914 }
915
916 if (is_ab8500_2p0(ab->ab8500)) {
917 if (ab->mode == USB_DEDICATED_CHG) {
918 ab8500_usb_wd_linkstatus(ab,
919 AB8500_BIT_PHY_CTRL_DEVICE_EN);
920 abx500_mask_and_set_register_interruptible(ab->dev,
921 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
922 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
923 }
924 }
925
926 return IRQ_HANDLED;
927}
928
929static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
930{
931 struct ab8500_usb *ab = (struct ab8500_usb *)data;
932
933 abx500_usb_link_status_update(ab);
934
935 return IRQ_HANDLED;
936}
937
938static void ab8500_usb_phy_disable_work(struct work_struct *work)
939{
940 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
941 phy_dis_work);
942
943 if (!ab->phy.otg->host)
944 ab8500_usb_host_phy_dis(ab);
945
946 if (!ab->phy.otg->gadget)
947 ab8500_usb_peri_phy_dis(ab);
948}
949
950/* Check if VBUS is set and linkstatus has not detected a cable. */
951static bool ab8500_usb_check_vbus_status(struct ab8500_usb *ab)
952{
953 u8 isource2;
954 u8 reg;
955 enum ab8540_usb_link_status lsts;
956
957 abx500_get_register_interruptible(ab->dev,
958 AB8500_INTERRUPT, AB8500_IT_SOURCE2_REG,
959 &isource2);
960
961 /* If Vbus is below 3.6V abort */
962 if (!(isource2 & AB8500_BIT_SOURCE2_VBUSDET))
963 return false;
964
965 abx500_get_register_interruptible(ab->dev,
966 AB8500_USB, AB8540_USB_LINK_STAT_REG,
967 ®);
968
969 lsts = (reg >> 3) & 0xFF;
970
971 /* Check if linkstatus has detected a cable */
972 if (lsts)
973 return false;
974
975 return true;
976}
977
978/* re-trigger charger detection again with watchdog re-kick. */
979static void ab8500_usb_vbus_turn_on_event_work(struct work_struct *work)
980{
981 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
982 vbus_event_work);
983
984 if (ab->mode != USB_IDLE)
985 return;
986
987 abx500_set_register_interruptible(ab->dev,
988 AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
989 AB8500_BIT_WD_CTRL_ENABLE);
990
991 udelay(100);
992
993 abx500_set_register_interruptible(ab->dev,
994 AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
995 AB8500_BIT_WD_CTRL_ENABLE | AB8500_BIT_WD_CTRL_KICK);
996
997 udelay(100);
998
999 /* Disable Main watchdog */
1000 abx500_set_register_interruptible(ab->dev,
1001 AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
1002 0x0);
1003
1004 /* Enable USB Charger detection */
1005 abx500_mask_and_set_register_interruptible(ab->dev,
1006 AB8500_USB, AB8540_VBUS_CTRL_REG,
1007 AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA,
1008 AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA);
1009
1010 ab->enabled_charging_detection = true;
1011}
1012
1013static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
1014{
1015 /*
1016 * AB8500 V2 has eye diagram issues when drawing more than 100mA from
1017 * VBUS. Set charging current to 100mA in case of standard host
1018 */
1019 if (is_ab8500_2p0_or_earlier(ab->ab8500))
1020 if (mA > 100)
1021 mA = 100;
1022
1023 return mA;
1024}
1025
1026static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
1027{
1028 struct ab8500_usb *ab;
1029
1030 if (!phy)
1031 return -ENODEV;
1032
1033 ab = phy_to_ab(phy);
1034
1035 mA = ab8500_eyediagram_workaroud(ab, mA);
1036
1037 ab->vbus_draw = mA;
1038
1039 atomic_notifier_call_chain(&ab->phy.notifier,
1040 UX500_MUSB_VBUS, &ab->vbus_draw);
1041
1042 return 0;
1043}
1044
1045static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
1046{
1047 /* TODO */
1048 return 0;
1049}
1050
1051static int ab8500_usb_set_peripheral(struct usb_otg *otg,
1052 struct usb_gadget *gadget)
1053{
1054 struct ab8500_usb *ab;
1055
1056 if (!otg)
1057 return -ENODEV;
1058
1059 ab = phy_to_ab(otg->phy);
1060
1061 ab->phy.otg->gadget = gadget;
1062
1063 /* Some drivers call this function in atomic context.
1064 * Do not update ab8500 registers directly till this
1065 * is fixed.
1066 */
1067
1068 if ((ab->mode != USB_IDLE) && !gadget) {
1069 ab->mode = USB_IDLE;
1070 schedule_work(&ab->phy_dis_work);
1071 }
1072
1073 return 0;
1074}
1075
1076static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
1077{
1078 struct ab8500_usb *ab;
1079
1080 if (!otg)
1081 return -ENODEV;
1082
1083 ab = phy_to_ab(otg->phy);
1084
1085 ab->phy.otg->host = host;
1086
1087 /* Some drivers call this function in atomic context.
1088 * Do not update ab8500 registers directly till this
1089 * is fixed.
1090 */
1091
1092 if ((ab->mode != USB_IDLE) && !host) {
1093 ab->mode = USB_IDLE;
1094 schedule_work(&ab->phy_dis_work);
1095 }
1096
1097 return 0;
1098}
1099
1100static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
1101{
1102 abx500_mask_and_set_register_interruptible(ab->dev,
1103 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
1104 AB8500_BIT_PHY_CTRL_DEVICE_EN,
1105 AB8500_BIT_PHY_CTRL_DEVICE_EN);
1106
1107 udelay(100);
1108
1109 abx500_mask_and_set_register_interruptible(ab->dev,
1110 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
1111 AB8500_BIT_PHY_CTRL_DEVICE_EN,
1112 0);
1113
1114 abx500_mask_and_set_register_interruptible(ab->dev,
1115 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
1116 AB8500_BIT_PHY_CTRL_HOST_EN,
1117 AB8500_BIT_PHY_CTRL_HOST_EN);
1118
1119 udelay(100);
1120
1121 abx500_mask_and_set_register_interruptible(ab->dev,
1122 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
1123 AB8500_BIT_PHY_CTRL_HOST_EN,
1124 0);
1125}
1126
1127static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
1128{
1129 int err;
1130
1131 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
1132 if (IS_ERR(ab->v_ape)) {
1133 dev_err(ab->dev, "Could not get v-ape supply\n");
1134 err = PTR_ERR(ab->v_ape);
1135 return err;
1136 }
1137
1138 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
1139 if (IS_ERR(ab->v_ulpi)) {
1140 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
1141 err = PTR_ERR(ab->v_ulpi);
1142 return err;
1143 }
1144
1145 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
1146 if (IS_ERR(ab->v_musb)) {
1147 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
1148 err = PTR_ERR(ab->v_musb);
1149 return err;
1150 }
1151
1152 return 0;
1153}
1154
1155static int ab8500_usb_irq_setup(struct platform_device *pdev,
1156 struct ab8500_usb *ab)
1157{
1158 int err;
1159 int irq;
1160
1161 if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
1162 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
1163 if (irq < 0) {
1164 dev_err(&pdev->dev, "Link status irq not found\n");
1165 return irq;
1166 }
1167 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1168 ab8500_usb_link_status_irq,
1169 IRQF_NO_SUSPEND | IRQF_SHARED,
1170 "usb-link-status", ab);
1171 if (err < 0) {
1172 dev_err(ab->dev, "request_irq failed for link status irq\n");
1173 return err;
1174 }
1175 }
1176
1177 if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
1178 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
1179 if (irq < 0) {
1180 dev_err(&pdev->dev, "ID fall irq not found\n");
1181 return irq;
1182 }
1183 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1184 ab8500_usb_disconnect_irq,
1185 IRQF_NO_SUSPEND | IRQF_SHARED,
1186 "usb-id-fall", ab);
1187 if (err < 0) {
1188 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
1189 return err;
1190 }
1191 }
1192
1193 if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
1194 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
1195 if (irq < 0) {
1196 dev_err(&pdev->dev, "VBUS fall irq not found\n");
1197 return irq;
1198 }
1199 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1200 ab8500_usb_disconnect_irq,
1201 IRQF_NO_SUSPEND | IRQF_SHARED,
1202 "usb-vbus-fall", ab);
1203 if (err < 0) {
1204 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
1205 return err;
1206 }
1207 }
1208
1209 return 0;
1210}
1211
1212static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
1213{
1214 int err;
1215
1216 /* Enable the PBT/Bank 0x12 access */
1217 err = abx500_set_register_interruptible(ab->dev,
1218 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
1219 if (err < 0)
1220 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
1221 err);
1222
1223 err = abx500_set_register_interruptible(ab->dev,
1224 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
1225 if (err < 0)
1226 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
1227 err);
1228
1229 err = abx500_set_register_interruptible(ab->dev,
1230 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
1231 if (err < 0)
1232 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
1233 err);
1234
1235 err = abx500_set_register_interruptible(ab->dev,
1236 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
1237 if (err < 0)
1238 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
1239 err);
1240
1241 /* Switch to normal mode/disable Bank 0x12 access */
1242 err = abx500_set_register_interruptible(ab->dev,
1243 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
1244 if (err < 0)
1245 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
1246 err);
1247}
1248
1249static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
1250{
1251 int err;
1252
1253 /* Enable the PBT/Bank 0x12 access */
1254 err = abx500_mask_and_set_register_interruptible(ab->dev,
1255 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
1256 0x01, 0x01);
1257 if (err < 0)
1258 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
1259 err);
1260
1261 err = abx500_mask_and_set_register_interruptible(ab->dev,
1262 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
1263 0xC8, 0xC8);
1264 if (err < 0)
1265 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
1266 err);
1267
1268 err = abx500_mask_and_set_register_interruptible(ab->dev,
1269 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
1270 0x60, 0x60);
1271 if (err < 0)
1272 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
1273 err);
1274
1275 err = abx500_mask_and_set_register_interruptible(ab->dev,
1276 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
1277 0xFC, 0x80);
1278
1279 if (err < 0)
1280 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
1281 err);
1282
1283 /* Switch to normal mode/disable Bank 0x12 access */
1284 err = abx500_mask_and_set_register_interruptible(ab->dev,
1285 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
1286 0x00, 0x00);
1287 if (err < 0)
1288 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
1289 err);
1290}
1291
1292static void ab8500_usb_set_ab8540_tuning_values(struct ab8500_usb *ab)
1293{
1294 int err;
1295
1296 err = abx500_set_register_interruptible(ab->dev,
1297 AB8540_DEBUG, AB8500_USB_PHY_TUNE1, 0xCC);
1298 if (err < 0)
1299 dev_err(ab->dev, "Failed to set PHY_TUNE1 register ret=%d\n",
1300 err);
1301
1302 err = abx500_set_register_interruptible(ab->dev,
1303 AB8540_DEBUG, AB8500_USB_PHY_TUNE2, 0x60);
1304 if (err < 0)
1305 dev_err(ab->dev, "Failed to set PHY_TUNE2 register ret=%d\n",
1306 err);
1307
1308 err = abx500_set_register_interruptible(ab->dev,
1309 AB8540_DEBUG, AB8500_USB_PHY_TUNE3, 0x90);
1310 if (err < 0)
1311 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester ret=%d\n",
1312 err);
1313}
1314
1315static void ab8500_usb_set_ab9540_tuning_values(struct ab8500_usb *ab)
1316{
1317 int err;
1318
1319 /* Enable the PBT/Bank 0x12 access */
1320 err = abx500_set_register_interruptible(ab->dev,
1321 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
1322 if (err < 0)
1323 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
1324 err);
1325
1326 err = abx500_set_register_interruptible(ab->dev,
1327 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
1328 if (err < 0)
1329 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
1330 err);
1331
1332 err = abx500_set_register_interruptible(ab->dev,
1333 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x60);
1334 if (err < 0)
1335 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
1336 err);
1337
1338 err = abx500_set_register_interruptible(ab->dev,
1339 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x80);
1340 if (err < 0)
1341 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
1342 err);
1343
1344 /* Switch to normal mode/disable Bank 0x12 access */
1345 err = abx500_set_register_interruptible(ab->dev,
1346 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
1347 if (err < 0)
1348 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
1349 err);
1350}
1351
1352static int ab8500_usb_probe(struct platform_device *pdev)
1353{
1354 struct ab8500_usb *ab;
1355 struct ab8500 *ab8500;
1356 struct usb_otg *otg;
1357 int err;
1358 int rev;
1359
1360 ab8500 = dev_get_drvdata(pdev->dev.parent);
1361 rev = abx500_get_chip_id(&pdev->dev);
1362
1363 if (is_ab8500_1p1_or_earlier(ab8500)) {
1364 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
1365 return -ENODEV;
1366 }
1367
1368 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
1369 if (!ab)
1370 return -ENOMEM;
1371
1372 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
1373 if (!otg)
1374 return -ENOMEM;
1375
1376 ab->dev = &pdev->dev;
1377 ab->ab8500 = ab8500;
1378 ab->phy.dev = ab->dev;
1379 ab->phy.otg = otg;
1380 ab->phy.label = "ab8500";
1381 ab->phy.set_suspend = ab8500_usb_set_suspend;
1382 ab->phy.set_power = ab8500_usb_set_power;
1383 ab->phy.state = OTG_STATE_UNDEFINED;
1384
1385 otg->phy = &ab->phy;
1386 otg->set_host = ab8500_usb_set_host;
1387 otg->set_peripheral = ab8500_usb_set_peripheral;
1388
1389 if (is_ab8500(ab->ab8500)) {
1390 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1391 AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
1392 AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
1393 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1394 } else if (is_ab8505(ab->ab8500)) {
1395 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1396 AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
1397 AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
1398 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1399 } else if (is_ab8540(ab->ab8500)) {
1400 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1401 AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS |
1402 AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK |
1403 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1404 } else if (is_ab9540(ab->ab8500)) {
1405 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1406 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1407 if (is_ab9540_2p0_or_earlier(ab->ab8500))
1408 ab->flags |= AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
1409 AB8500_USB_FLAG_USE_VBUS_DET_IRQ;
1410 }
1411
1412 /* Disable regulator voltage setting for AB8500 <= v2.0 */
1413 if (is_ab8500_2p0_or_earlier(ab->ab8500))
1414 ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1415
1416 platform_set_drvdata(pdev, ab);
1417
1418 /* all: Disable phy when called from set_host and set_peripheral */
1419 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
1420
1421 INIT_WORK(&ab->vbus_event_work, ab8500_usb_vbus_turn_on_event_work);
1422
1423 err = ab8500_usb_regulator_get(ab);
1424 if (err)
1425 return err;
1426
1427 ab->sysclk = devm_clk_get(ab->dev, "sysclk");
1428 if (IS_ERR(ab->sysclk)) {
1429 dev_err(ab->dev, "Could not get sysclk.\n");
1430 return PTR_ERR(ab->sysclk);
1431 }
1432
1433 err = ab8500_usb_irq_setup(pdev, ab);
1434 if (err < 0)
1435 return err;
1436
1437 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
1438 if (err) {
1439 dev_err(&pdev->dev, "Can't register transceiver\n");
1440 return err;
1441 }
1442
1443 if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
1444 /* Phy tuning values for AB8500 > v2.0 */
1445 ab8500_usb_set_ab8500_tuning_values(ab);
1446 else if (is_ab8505(ab->ab8500))
1447 /* Phy tuning values for AB8505 */
1448 ab8500_usb_set_ab8505_tuning_values(ab);
1449 else if (is_ab8540(ab->ab8500))
1450 /* Phy tuning values for AB8540 */
1451 ab8500_usb_set_ab8540_tuning_values(ab);
1452 else if (is_ab9540(ab->ab8500))
1453 /* Phy tuning values for AB9540 */
1454 ab8500_usb_set_ab9540_tuning_values(ab);
1455
1456 /* Needed to enable ID detection. */
1457 ab8500_usb_wd_workaround(ab);
1458
1459 /*
1460 * This is required for usb-link-status to work properly when a
1461 * cable is connected at boot time.
1462 */
1463 ab8500_usb_restart_phy(ab);
1464
1465 if (ab->flags & AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS) {
1466 if (ab8500_usb_check_vbus_status(ab))
1467 schedule_work(&ab->vbus_event_work);
1468 }
1469
1470 abx500_usb_link_status_update(ab);
1471
1472 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
1473
1474 return 0;
1475}
1476
1477static int ab8500_usb_remove(struct platform_device *pdev)
1478{
1479 struct ab8500_usb *ab = platform_get_drvdata(pdev);
1480
1481 cancel_work_sync(&ab->phy_dis_work);
1482 cancel_work_sync(&ab->vbus_event_work);
1483
1484 usb_remove_phy(&ab->phy);
1485
1486 if (ab->mode == USB_HOST)
1487 ab8500_usb_host_phy_dis(ab);
1488 else if (ab->mode == USB_PERIPHERAL)
1489 ab8500_usb_peri_phy_dis(ab);
1490
1491 return 0;
1492}
1493
1494static struct platform_device_id ab8500_usb_devtype[] = {
1495 { .name = "ab8500-usb", },
1496 { .name = "ab8540-usb", },
1497 { .name = "ab9540-usb", },
1498 { /* sentinel */ }
1499};
1500MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
1501
1502static struct platform_driver ab8500_usb_driver = {
1503 .probe = ab8500_usb_probe,
1504 .remove = ab8500_usb_remove,
1505 .id_table = ab8500_usb_devtype,
1506 .driver = {
1507 .name = "abx5x0-usb",
1508 .owner = THIS_MODULE,
1509 },
1510};
1511
1512static int __init ab8500_usb_init(void)
1513{
1514 return platform_driver_register(&ab8500_usb_driver);
1515}
1516subsys_initcall(ab8500_usb_init);
1517
1518static void __exit ab8500_usb_exit(void)
1519{
1520 platform_driver_unregister(&ab8500_usb_driver);
1521}
1522module_exit(ab8500_usb_exit);
1523
1524MODULE_AUTHOR("ST-Ericsson AB");
1525MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
1526MODULE_LICENSE("GPL");