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