usb: phy: ab8500-usb: add regulator support
[deliverable/linux.git] / drivers / usb / phy / phy-ab8500-usb.c
CommitLineData
96915234
MYK
1/*
2 * drivers/usb/otg/ab8500_usb.c
3 *
4 * USB transceiver driver for AB8500 chip
5 *
6 * Copyright (C) 2010 ST-Ericsson AB
7 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include <linux/module.h>
26#include <linux/platform_device.h>
27#include <linux/usb/otg.h>
28#include <linux/slab.h>
29#include <linux/notifier.h>
30#include <linux/interrupt.h>
31#include <linux/delay.h>
32#include <linux/mfd/abx500.h>
ee66e653 33#include <linux/mfd/abx500/ab8500.h>
af6882be 34#include <linux/usb/musb-ux500.h>
e65b36c0 35#include <linux/regulator/consumer.h>
96915234 36
7124631a 37/* Bank AB8500_SYS_CTRL2_BLOCK */
96915234 38#define AB8500_MAIN_WD_CTRL_REG 0x01
7124631a
SB
39
40/* Bank AB8500_USB */
96915234 41#define AB8500_USB_LINE_STAT_REG 0x80
af6882be 42#define AB8505_USB_LINE_STAT_REG 0x94
96915234
MYK
43#define AB8500_USB_PHY_CTRL_REG 0x8A
44
7124631a
SB
45/* Bank AB8500_DEVELOPMENT */
46#define AB8500_BANK12_ACCESS 0x00
47
48/* Bank AB8500_DEBUG */
49#define AB8500_USB_PHY_TUNE1 0x05
50#define AB8500_USB_PHY_TUNE2 0x06
51#define AB8500_USB_PHY_TUNE3 0x07
52
96915234
MYK
53#define AB8500_BIT_OTG_STAT_ID (1 << 0)
54#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
55#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
56#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
57#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
58
96915234
MYK
59#define AB8500_WD_KICK_DELAY_US 100 /* usec */
60#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
af6882be 61#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
96915234
MYK
62
63/* Usb line status register */
64enum ab8500_usb_link_status {
af6882be
FB
65 USB_LINK_NOT_CONFIGURED_8500 = 0,
66 USB_LINK_STD_HOST_NC_8500,
67 USB_LINK_STD_HOST_C_NS_8500,
68 USB_LINK_STD_HOST_C_S_8500,
69 USB_LINK_HOST_CHG_NM_8500,
70 USB_LINK_HOST_CHG_HS_8500,
71 USB_LINK_HOST_CHG_HS_CHIRP_8500,
72 USB_LINK_DEDICATED_CHG_8500,
73 USB_LINK_ACA_RID_A_8500,
74 USB_LINK_ACA_RID_B_8500,
75 USB_LINK_ACA_RID_C_NM_8500,
76 USB_LINK_ACA_RID_C_HS_8500,
77 USB_LINK_ACA_RID_C_HS_CHIRP_8500,
78 USB_LINK_HM_IDGND_8500,
79 USB_LINK_RESERVED_8500,
80 USB_LINK_NOT_VALID_LINK_8500,
81};
82
83enum ab8505_usb_link_status {
84 USB_LINK_NOT_CONFIGURED_8505 = 0,
85 USB_LINK_STD_HOST_NC_8505,
86 USB_LINK_STD_HOST_C_NS_8505,
87 USB_LINK_STD_HOST_C_S_8505,
88 USB_LINK_CDP_8505,
89 USB_LINK_RESERVED0_8505,
90 USB_LINK_RESERVED1_8505,
91 USB_LINK_DEDICATED_CHG_8505,
92 USB_LINK_ACA_RID_A_8505,
93 USB_LINK_ACA_RID_B_8505,
94 USB_LINK_ACA_RID_C_NM_8505,
95 USB_LINK_RESERVED2_8505,
96 USB_LINK_RESERVED3_8505,
97 USB_LINK_HM_IDGND_8505,
98 USB_LINK_CHARGERPORT_NOT_OK_8505,
99 USB_LINK_CHARGER_DM_HIGH_8505,
100 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
101 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
102 USB_LINK_STD_UPSTREAM_8505,
103 USB_LINK_CHARGER_SE1_8505,
104 USB_LINK_CARKIT_CHGR_1_8505,
105 USB_LINK_CARKIT_CHGR_2_8505,
106 USB_LINK_ACA_DOCK_CHGR_8505,
107 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
108 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
109 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
110 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
111 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
112};
113
114enum ab8500_usb_mode {
115 USB_IDLE = 0,
116 USB_PERIPHERAL,
117 USB_HOST,
118 USB_DEDICATED_CHG
96915234
MYK
119};
120
121struct ab8500_usb {
144713f3 122 struct usb_phy phy;
96915234 123 struct device *dev;
73f226cb 124 struct ab8500 *ab8500;
96915234
MYK
125 unsigned vbus_draw;
126 struct delayed_work dwork;
127 struct work_struct phy_dis_work;
128 unsigned long link_status_wait;
af6882be 129 enum ab8500_usb_mode mode;
e65b36c0
FB
130 struct regulator *v_ape;
131 struct regulator *v_musb;
132 struct regulator *v_ulpi;
af6882be 133 int previous_link_status_state;
96915234
MYK
134};
135
144713f3 136static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
96915234 137{
144713f3 138 return container_of(x, struct ab8500_usb, phy);
96915234
MYK
139}
140
141static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
142{
143 abx500_set_register_interruptible(ab->dev,
144 AB8500_SYS_CTRL2_BLOCK,
145 AB8500_MAIN_WD_CTRL_REG,
146 AB8500_BIT_WD_CTRL_ENABLE);
147
148 udelay(AB8500_WD_KICK_DELAY_US);
149
150 abx500_set_register_interruptible(ab->dev,
151 AB8500_SYS_CTRL2_BLOCK,
152 AB8500_MAIN_WD_CTRL_REG,
153 (AB8500_BIT_WD_CTRL_ENABLE
154 | AB8500_BIT_WD_CTRL_KICK));
155
73f226cb 156 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
96915234
MYK
157
158 abx500_set_register_interruptible(ab->dev,
159 AB8500_SYS_CTRL2_BLOCK,
160 AB8500_MAIN_WD_CTRL_REG,
161 0);
162}
163
af6882be
FB
164static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
165{
166 /* Workaround for v2.0 bug # 31952 */
167 if (is_ab8500_2p0(ab->ab8500)) {
168 abx500_mask_and_set_register_interruptible(ab->dev,
169 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
170 bit, bit);
171 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
172 }
173}
174
96915234
MYK
175static void ab8500_usb_phy_ctrl(struct ab8500_usb *ab, bool sel_host,
176 bool enable)
177{
178 u8 ctrl_reg;
179 abx500_get_register_interruptible(ab->dev,
180 AB8500_USB,
181 AB8500_USB_PHY_CTRL_REG,
182 &ctrl_reg);
183 if (sel_host) {
184 if (enable)
185 ctrl_reg |= AB8500_BIT_PHY_CTRL_HOST_EN;
186 else
187 ctrl_reg &= ~AB8500_BIT_PHY_CTRL_HOST_EN;
188 } else {
189 if (enable)
190 ctrl_reg |= AB8500_BIT_PHY_CTRL_DEVICE_EN;
191 else
192 ctrl_reg &= ~AB8500_BIT_PHY_CTRL_DEVICE_EN;
193 }
194
195 abx500_set_register_interruptible(ab->dev,
196 AB8500_USB,
197 AB8500_USB_PHY_CTRL_REG,
198 ctrl_reg);
199
200 /* Needed to enable the phy.*/
201 if (enable)
202 ab8500_usb_wd_workaround(ab);
203}
204
205#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_ctrl(ab, true, true)
206#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_ctrl(ab, true, false)
207#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_ctrl(ab, false, true)
208#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_ctrl(ab, false, false)
209
af6882be
FB
210static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
211 enum ab8505_usb_link_status lsts)
96915234 212{
af6882be 213 enum ux500_musb_vbus_id_status event = 0;
96915234 214
af6882be 215 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
96915234 216
af6882be
FB
217 /*
218 * Spurious link_status interrupts are seen at the time of
219 * disconnection of a device in RIDA state
220 */
221 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
222 (lsts == USB_LINK_STD_HOST_NC_8505))
223 return 0;
224
225 ab->previous_link_status_state = lsts;
96915234
MYK
226
227 switch (lsts) {
af6882be
FB
228 case USB_LINK_ACA_RID_B_8505:
229 event = UX500_MUSB_RIDB;
230 case USB_LINK_NOT_CONFIGURED_8505:
231 case USB_LINK_RESERVED0_8505:
232 case USB_LINK_RESERVED1_8505:
233 case USB_LINK_RESERVED2_8505:
234 case USB_LINK_RESERVED3_8505:
235 ab->mode = USB_IDLE;
144713f3 236 ab->phy.otg->default_a = false;
96915234 237 ab->vbus_draw = 0;
af6882be
FB
238 if (event != UX500_MUSB_RIDB)
239 event = UX500_MUSB_NONE;
240 /*
241 * Fallback to default B_IDLE as nothing
242 * is connected
243 */
244 ab->phy.state = OTG_STATE_B_IDLE;
96915234
MYK
245 break;
246
af6882be
FB
247 case USB_LINK_ACA_RID_C_NM_8505:
248 event = UX500_MUSB_RIDC;
249 case USB_LINK_STD_HOST_NC_8505:
250 case USB_LINK_STD_HOST_C_NS_8505:
251 case USB_LINK_STD_HOST_C_S_8505:
252 case USB_LINK_CDP_8505:
253 if (ab->mode == USB_IDLE) {
254 ab->mode = USB_PERIPHERAL;
96915234 255 ab8500_usb_peri_phy_en(ab);
af6882be
FB
256 atomic_notifier_call_chain(&ab->phy.notifier,
257 UX500_MUSB_PREPARE, &ab->vbus_draw);
96915234 258 }
af6882be
FB
259 if (event != UX500_MUSB_RIDC)
260 event = UX500_MUSB_VBUS;
96915234
MYK
261 break;
262
af6882be
FB
263 case USB_LINK_ACA_RID_A_8505:
264 case USB_LINK_ACA_DOCK_CHGR_8505:
265 event = UX500_MUSB_RIDA;
266 case USB_LINK_HM_IDGND_8505:
267 if (ab->mode == USB_IDLE) {
268 ab->mode = USB_HOST;
96915234 269 ab8500_usb_host_phy_en(ab);
af6882be
FB
270 atomic_notifier_call_chain(&ab->phy.notifier,
271 UX500_MUSB_PREPARE, &ab->vbus_draw);
96915234 272 }
144713f3 273 ab->phy.otg->default_a = true;
af6882be
FB
274 if (event != UX500_MUSB_RIDA)
275 event = UX500_MUSB_ID;
276 atomic_notifier_call_chain(&ab->phy.notifier,
277 event, &ab->vbus_draw);
96915234
MYK
278 break;
279
af6882be
FB
280 case USB_LINK_DEDICATED_CHG_8505:
281 ab->mode = USB_DEDICATED_CHG;
282 event = UX500_MUSB_CHARGER;
283 atomic_notifier_call_chain(&ab->phy.notifier,
284 event, &ab->vbus_draw);
285 break;
286
287 default:
96915234
MYK
288 break;
289 }
290
af6882be
FB
291 return 0;
292}
293
294static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
295 enum ab8500_usb_link_status lsts)
296{
297 enum ux500_musb_vbus_id_status event = 0;
298
299 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
300
301 /*
302 * Spurious link_status interrupts are seen in case of a
303 * disconnection of a device in IDGND and RIDA stage
304 */
305 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
306 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
307 lsts == USB_LINK_STD_HOST_NC_8500))
308 return 0;
309
310 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
311 lsts == USB_LINK_STD_HOST_NC_8500)
312 return 0;
313
314 ab->previous_link_status_state = lsts;
315
316 switch (lsts) {
317 case USB_LINK_ACA_RID_B_8500:
318 event = UX500_MUSB_RIDB;
319 case USB_LINK_NOT_CONFIGURED_8500:
320 case USB_LINK_NOT_VALID_LINK_8500:
321 ab->mode = USB_IDLE;
322 ab->phy.otg->default_a = false;
323 ab->vbus_draw = 0;
324 if (event != UX500_MUSB_RIDB)
325 event = UX500_MUSB_NONE;
326 /* Fallback to default B_IDLE as nothing is connected */
327 ab->phy.state = OTG_STATE_B_IDLE;
328 break;
329
330 case USB_LINK_ACA_RID_C_NM_8500:
331 case USB_LINK_ACA_RID_C_HS_8500:
332 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
333 event = UX500_MUSB_RIDC;
334 case USB_LINK_STD_HOST_NC_8500:
335 case USB_LINK_STD_HOST_C_NS_8500:
336 case USB_LINK_STD_HOST_C_S_8500:
337 case USB_LINK_HOST_CHG_NM_8500:
338 case USB_LINK_HOST_CHG_HS_8500:
339 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
340 if (ab->mode == USB_IDLE) {
341 ab->mode = USB_PERIPHERAL;
342 ab8500_usb_peri_phy_en(ab);
343 atomic_notifier_call_chain(&ab->phy.notifier,
344 UX500_MUSB_PREPARE, &ab->vbus_draw);
345 }
346 if (event != UX500_MUSB_RIDC)
347 event = UX500_MUSB_VBUS;
348 break;
349
350 case USB_LINK_ACA_RID_A_8500:
351 event = UX500_MUSB_RIDA;
352 case USB_LINK_HM_IDGND_8500:
353 if (ab->mode == USB_IDLE) {
354 ab->mode = USB_HOST;
355 ab8500_usb_host_phy_en(ab);
356 atomic_notifier_call_chain(&ab->phy.notifier,
357 UX500_MUSB_PREPARE, &ab->vbus_draw);
358 }
359 ab->phy.otg->default_a = true;
360 if (event != UX500_MUSB_RIDA)
361 event = UX500_MUSB_ID;
362 atomic_notifier_call_chain(&ab->phy.notifier,
363 event, &ab->vbus_draw);
364 break;
365
366 case USB_LINK_DEDICATED_CHG_8500:
367 ab->mode = USB_DEDICATED_CHG;
368 event = UX500_MUSB_CHARGER;
369 atomic_notifier_call_chain(&ab->phy.notifier,
370 event, &ab->vbus_draw);
371 break;
372
373 case USB_LINK_RESERVED_8500:
374 break;
375 }
96915234
MYK
376
377 return 0;
378}
379
af6882be
FB
380/*
381 * Connection Sequence:
382 * 1. Link Status Interrupt
383 * 2. Enable AB clock
384 * 3. Enable AB regulators
385 * 4. Enable USB phy
386 * 5. Reset the musb controller
387 * 6. Switch the ULPI GPIO pins to fucntion mode
388 * 7. Enable the musb Peripheral5 clock
389 * 8. Restore MUSB context
390 */
391static int abx500_usb_link_status_update(struct ab8500_usb *ab)
96915234 392{
af6882be
FB
393 u8 reg;
394 int ret = 0;
395
396 if (is_ab8500(ab->ab8500)) {
397 enum ab8500_usb_link_status lsts;
398
399 abx500_get_register_interruptible(ab->dev,
400 AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
401 lsts = (reg >> 3) & 0x0F;
402 ret = ab8500_usb_link_status_update(ab, lsts);
403 } else if (is_ab8505(ab->ab8500)) {
404 enum ab8505_usb_link_status lsts;
405
406 abx500_get_register_interruptible(ab->dev,
407 AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
408 lsts = (reg >> 3) & 0x1F;
409 ret = ab8505_usb_link_status_update(ab, lsts);
410 }
411
412 return ret;
413}
414
415/*
416 * Disconnection Sequence:
417 * 1. Disconect Interrupt
418 * 2. Disable regulators
419 * 3. Disable AB clock
420 * 4. Disable the Phy
421 * 5. Link Status Interrupt
422 * 6. Disable Musb Clock
423 */
424static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
425{
426 struct ab8500_usb *ab = (struct ab8500_usb *) data;
427 enum usb_phy_events event = UX500_MUSB_NONE;
428
429 /* Link status will not be updated till phy is disabled. */
430 if (ab->mode == USB_HOST) {
431 ab->phy.otg->default_a = false;
432 ab->vbus_draw = 0;
433 atomic_notifier_call_chain(&ab->phy.notifier,
434 event, &ab->vbus_draw);
435 ab8500_usb_host_phy_dis(ab);
436 ab->mode = USB_IDLE;
437 }
438
439 if (ab->mode == USB_PERIPHERAL) {
440 atomic_notifier_call_chain(&ab->phy.notifier,
441 event, &ab->vbus_draw);
442 ab8500_usb_peri_phy_dis(ab);
443 atomic_notifier_call_chain(&ab->phy.notifier,
444 UX500_MUSB_CLEAN, &ab->vbus_draw);
445 ab->mode = USB_IDLE;
446 ab->phy.otg->default_a = false;
447 ab->vbus_draw = 0;
448 }
449
450 if (is_ab8500_2p0(ab->ab8500)) {
451 if (ab->mode == USB_DEDICATED_CHG) {
452 ab8500_usb_wd_linkstatus(ab,
453 AB8500_BIT_PHY_CTRL_DEVICE_EN);
454 abx500_mask_and_set_register_interruptible(ab->dev,
455 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
456 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
457 }
458 }
96915234 459
af6882be 460 return IRQ_HANDLED;
96915234
MYK
461}
462
af6882be 463static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
96915234
MYK
464{
465 struct ab8500_usb *ab = (struct ab8500_usb *) data;
466
af6882be 467 abx500_usb_link_status_update(ab);
96915234
MYK
468
469 return IRQ_HANDLED;
470}
471
af6882be
FB
472static void ab8500_usb_delayed_work(struct work_struct *work)
473{
474 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
475 dwork.work);
476
477 abx500_usb_link_status_update(ab);
478}
479
96915234
MYK
480static void ab8500_usb_phy_disable_work(struct work_struct *work)
481{
482 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
483 phy_dis_work);
484
144713f3 485 if (!ab->phy.otg->host)
96915234
MYK
486 ab8500_usb_host_phy_dis(ab);
487
144713f3 488 if (!ab->phy.otg->gadget)
96915234
MYK
489 ab8500_usb_peri_phy_dis(ab);
490}
491
c2a0ab6b
SB
492static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
493{
494 /*
495 * AB8500 V2 has eye diagram issues when drawing more than 100mA from
496 * VBUS. Set charging current to 100mA in case of standard host
497 */
498 if (is_ab8500_2p0_or_earlier(ab->ab8500))
499 if (mA > 100)
500 mA = 100;
501
502 return mA;
503}
504
144713f3 505static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
96915234
MYK
506{
507 struct ab8500_usb *ab;
508
144713f3 509 if (!phy)
96915234
MYK
510 return -ENODEV;
511
144713f3 512 ab = phy_to_ab(phy);
96915234 513
c2a0ab6b
SB
514 mA = ab8500_eyediagram_workaroud(ab, mA);
515
96915234
MYK
516 ab->vbus_draw = mA;
517
518 if (mA)
144713f3 519 atomic_notifier_call_chain(&ab->phy.notifier,
af6882be 520 UX500_MUSB_ENUMERATED, ab->phy.otg->gadget);
96915234
MYK
521 return 0;
522}
523
524/* TODO: Implement some way for charging or other drivers to read
525 * ab->vbus_draw.
526 */
527
86753811 528static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
96915234
MYK
529{
530 /* TODO */
531 return 0;
532}
533
144713f3
HK
534static int ab8500_usb_set_peripheral(struct usb_otg *otg,
535 struct usb_gadget *gadget)
96915234
MYK
536{
537 struct ab8500_usb *ab;
538
539 if (!otg)
540 return -ENODEV;
541
144713f3 542 ab = phy_to_ab(otg->phy);
96915234
MYK
543
544 /* Some drivers call this function in atomic context.
545 * Do not update ab8500 registers directly till this
546 * is fixed.
547 */
548
549 if (!gadget) {
550 /* TODO: Disable regulators. */
144713f3 551 otg->gadget = NULL;
96915234
MYK
552 schedule_work(&ab->phy_dis_work);
553 } else {
144713f3
HK
554 otg->gadget = gadget;
555 otg->phy->state = OTG_STATE_B_IDLE;
96915234
MYK
556
557 /* Phy will not be enabled if cable is already
558 * plugged-in. Schedule to enable phy.
559 * Use same delay to avoid any race condition.
560 */
561 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
562 }
563
564 return 0;
565}
566
144713f3 567static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
96915234
MYK
568{
569 struct ab8500_usb *ab;
570
571 if (!otg)
572 return -ENODEV;
573
144713f3 574 ab = phy_to_ab(otg->phy);
96915234
MYK
575
576 /* Some drivers call this function in atomic context.
577 * Do not update ab8500 registers directly till this
578 * is fixed.
579 */
580
581 if (!host) {
582 /* TODO: Disable regulators. */
144713f3 583 otg->host = NULL;
96915234
MYK
584 schedule_work(&ab->phy_dis_work);
585 } else {
144713f3 586 otg->host = host;
96915234
MYK
587 /* Phy will not be enabled if cable is already
588 * plugged-in. Schedule to enable phy.
589 * Use same delay to avoid any race condition.
590 */
591 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
592 }
593
594 return 0;
595}
596
e65b36c0
FB
597static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
598{
599 int err;
600
601 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
602 if (IS_ERR(ab->v_ape)) {
603 dev_err(ab->dev, "Could not get v-ape supply\n");
604 err = PTR_ERR(ab->v_ape);
605 return err;
606 }
607
608 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
609 if (IS_ERR(ab->v_ulpi)) {
610 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
611 err = PTR_ERR(ab->v_ulpi);
612 return err;
613 }
614
615 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
616 if (IS_ERR(ab->v_musb)) {
617 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
618 err = PTR_ERR(ab->v_musb);
619 return err;
620 }
621
622 return 0;
623}
624
af6882be
FB
625static int ab8500_usb_irq_setup(struct platform_device *pdev,
626 struct ab8500_usb *ab)
96915234
MYK
627{
628 int err;
af6882be 629 int irq;
96915234 630
af6882be
FB
631 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
632 if (irq < 0) {
96915234 633 dev_err(&pdev->dev, "Link status irq not found\n");
af6882be
FB
634 return irq;
635 }
636 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
637 ab8500_usb_link_status_irq,
638 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
639 if (err < 0) {
640 dev_err(ab->dev, "request_irq failed for link status irq\n");
641 return err;
96915234
MYK
642 }
643
af6882be
FB
644 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
645 if (irq < 0) {
646 dev_err(&pdev->dev, "ID fall irq not found\n");
647 return irq;
648 }
649 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
650 ab8500_usb_disconnect_irq,
651 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
96915234 652 if (err < 0) {
af6882be
FB
653 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
654 return err;
655 }
656
657 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
658 if (irq < 0) {
659 dev_err(&pdev->dev, "VBUS fall irq not found\n");
660 return irq;
661 }
662 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
663 ab8500_usb_disconnect_irq,
664 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
665 if (err < 0) {
666 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
96915234
MYK
667 return err;
668 }
669
670 return 0;
671}
672
41ac7b3a 673static int ab8500_usb_probe(struct platform_device *pdev)
96915234
MYK
674{
675 struct ab8500_usb *ab;
73f226cb 676 struct ab8500 *ab8500;
144713f3 677 struct usb_otg *otg;
96915234
MYK
678 int err;
679 int rev;
680
73f226cb 681 ab8500 = dev_get_drvdata(pdev->dev.parent);
96915234 682 rev = abx500_get_chip_id(&pdev->dev);
73f226cb
FB
683
684 if (is_ab8500_1p1_or_earlier(ab8500)) {
685 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
96915234
MYK
686 return -ENODEV;
687 }
688
81ef6724 689 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
96915234
MYK
690 if (!ab)
691 return -ENOMEM;
692
81ef6724
FB
693 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
694 if (!otg)
144713f3 695 return -ENOMEM;
144713f3 696
96915234 697 ab->dev = &pdev->dev;
73f226cb 698 ab->ab8500 = ab8500;
144713f3
HK
699 ab->phy.dev = ab->dev;
700 ab->phy.otg = otg;
701 ab->phy.label = "ab8500";
702 ab->phy.set_suspend = ab8500_usb_set_suspend;
703 ab->phy.set_power = ab8500_usb_set_power;
704 ab->phy.state = OTG_STATE_UNDEFINED;
705
706 otg->phy = &ab->phy;
707 otg->set_host = ab8500_usb_set_host;
708 otg->set_peripheral = ab8500_usb_set_peripheral;
96915234
MYK
709
710 platform_set_drvdata(pdev, ab);
711
144713f3 712 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
96915234
MYK
713
714 /* v1: Wait for link status to become stable.
715 * all: Updates form set_host and set_peripheral as they are atomic.
716 */
717 INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
718
719 /* all: Disable phy when called from set_host and set_peripheral */
720 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
721
e65b36c0
FB
722 err = ab8500_usb_regulator_get(ab);
723 if (err)
724 return err;
725
af6882be 726 err = ab8500_usb_irq_setup(pdev, ab);
96915234 727 if (err < 0)
81ef6724 728 return err;
96915234 729
662dca54 730 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
96915234
MYK
731 if (err) {
732 dev_err(&pdev->dev, "Can't register transceiver\n");
81ef6724 733 return err;
96915234
MYK
734 }
735
7124631a
SB
736 /* Phy tuning values for AB8500 */
737 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
738 /* Enable the PBT/Bank 0x12 access */
739 err = abx500_set_register_interruptible(ab->dev,
740 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
741 if (err < 0)
742 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
743 err);
744
745 err = abx500_set_register_interruptible(ab->dev,
746 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
747 if (err < 0)
748 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
749 err);
750
751 err = abx500_set_register_interruptible(ab->dev,
752 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
753 if (err < 0)
754 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
755 err);
756
757 err = abx500_set_register_interruptible(ab->dev,
758 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
759 if (err < 0)
760 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
761 err);
762
763 /* Switch to normal mode/disable Bank 0x12 access */
764 err = abx500_set_register_interruptible(ab->dev,
765 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
766 if (err < 0)
767 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
768 err);
769 }
770
771 /* Phy tuning values for AB8505 */
772 if (is_ab8505(ab->ab8500)) {
773 /* Enable the PBT/Bank 0x12 access */
774 err = abx500_mask_and_set_register_interruptible(ab->dev,
775 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
776 0x01, 0x01);
777 if (err < 0)
778 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
779 err);
780
781 err = abx500_mask_and_set_register_interruptible(ab->dev,
782 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
783 0xC8, 0xC8);
784 if (err < 0)
785 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
786 err);
787
788 err = abx500_mask_and_set_register_interruptible(ab->dev,
789 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
790 0x60, 0x60);
791 if (err < 0)
792 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
793 err);
794
795 err = abx500_mask_and_set_register_interruptible(ab->dev,
796 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
797 0xFC, 0x80);
798
799 if (err < 0)
800 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
801 err);
802
803 /* Switch to normal mode/disable Bank 0x12 access */
804 err = abx500_mask_and_set_register_interruptible(ab->dev,
805 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
806 0x00, 0x00);
807 if (err < 0)
808 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
809 err);
810 }
811
af6882be
FB
812 /* Needed to enable ID detection. */
813 ab8500_usb_wd_workaround(ab);
814
73f226cb 815 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
96915234
MYK
816
817 return 0;
96915234
MYK
818}
819
fb4e98ab 820static int ab8500_usb_remove(struct platform_device *pdev)
96915234
MYK
821{
822 struct ab8500_usb *ab = platform_get_drvdata(pdev);
823
96915234
MYK
824 cancel_delayed_work_sync(&ab->dwork);
825
826 cancel_work_sync(&ab->phy_dis_work);
827
662dca54 828 usb_remove_phy(&ab->phy);
96915234
MYK
829
830 ab8500_usb_host_phy_dis(ab);
831 ab8500_usb_peri_phy_dis(ab);
832
833 platform_set_drvdata(pdev, NULL);
834
96915234
MYK
835 return 0;
836}
837
838static struct platform_driver ab8500_usb_driver = {
839 .probe = ab8500_usb_probe,
7690417d 840 .remove = ab8500_usb_remove,
96915234
MYK
841 .driver = {
842 .name = "ab8500-usb",
843 .owner = THIS_MODULE,
844 },
845};
846
847static int __init ab8500_usb_init(void)
848{
849 return platform_driver_register(&ab8500_usb_driver);
850}
851subsys_initcall(ab8500_usb_init);
852
853static void __exit ab8500_usb_exit(void)
854{
855 platform_driver_unregister(&ab8500_usb_driver);
856}
857module_exit(ab8500_usb_exit);
858
859MODULE_ALIAS("platform:ab8500_usb");
860MODULE_AUTHOR("ST-Ericsson AB");
861MODULE_DESCRIPTION("AB8500 usb transceiver driver");
862MODULE_LICENSE("GPL");
This page took 0.191537 seconds and 5 git commands to generate.