blob: 888dad65bf9b06fa424502c4bff7f086a4753bfc [file] [log] [blame]
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001/*
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>
Linus Walleijee66e652011-12-02 14:16:33 +010033#include <linux/mfd/abx500/ab8500.h>
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080034#include <linux/usb/musb-ux500.h>
Fabio Baltierie65b36c2013-04-03 10:45:06 +020035#include <linux/regulator/consumer.h>
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010036
Sakethram Bommisetti71246312013-04-03 10:45:04 +020037/* Bank AB8500_SYS_CTRL2_BLOCK */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010038#define AB8500_MAIN_WD_CTRL_REG 0x01
Sakethram Bommisetti71246312013-04-03 10:45:04 +020039
40/* Bank AB8500_USB */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010041#define AB8500_USB_LINE_STAT_REG 0x80
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080042#define AB8505_USB_LINE_STAT_REG 0x94
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010043#define AB8500_USB_PHY_CTRL_REG 0x8A
44
Sakethram Bommisetti71246312013-04-03 10:45:04 +020045/* 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
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010053#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
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010059#define AB8500_WD_KICK_DELAY_US 100 /* usec */
60#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080061#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010062
63/* Usb line status register */
64enum ab8500_usb_link_status {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080065 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
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100119};
120
121struct ab8500_usb {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200122 struct usb_phy phy;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100123 struct device *dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800124 struct ab8500 *ab8500;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100125 unsigned vbus_draw;
126 struct delayed_work dwork;
127 struct work_struct phy_dis_work;
128 unsigned long link_status_wait;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800129 enum ab8500_usb_mode mode;
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200130 struct regulator *v_ape;
131 struct regulator *v_musb;
132 struct regulator *v_ulpi;
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200133 int saved_v_ulpi;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800134 int previous_link_status_state;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100135};
136
Heikki Krogerus144713f2012-02-13 13:24:05 +0200137static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100138{
Heikki Krogerus144713f2012-02-13 13:24:05 +0200139 return container_of(x, struct ab8500_usb, phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100140}
141
142static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
143{
144 abx500_set_register_interruptible(ab->dev,
145 AB8500_SYS_CTRL2_BLOCK,
146 AB8500_MAIN_WD_CTRL_REG,
147 AB8500_BIT_WD_CTRL_ENABLE);
148
149 udelay(AB8500_WD_KICK_DELAY_US);
150
151 abx500_set_register_interruptible(ab->dev,
152 AB8500_SYS_CTRL2_BLOCK,
153 AB8500_MAIN_WD_CTRL_REG,
154 (AB8500_BIT_WD_CTRL_ENABLE
155 | AB8500_BIT_WD_CTRL_KICK));
156
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800157 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100158
159 abx500_set_register_interruptible(ab->dev,
160 AB8500_SYS_CTRL2_BLOCK,
161 AB8500_MAIN_WD_CTRL_REG,
162 0);
163}
164
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200165static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
166{
167 int ret, volt;
168
169 regulator_enable(ab->v_ape);
170
171 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
172 ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
173 if (ab->saved_v_ulpi < 0)
174 dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
175
176 ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
177 if (ret < 0)
178 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
179 ret);
180
181 ret = regulator_set_optimum_mode(ab->v_ulpi, 28000);
182 if (ret < 0)
183 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
184 ret);
185 }
186
187 regulator_enable(ab->v_ulpi);
188
189 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
190 volt = regulator_get_voltage(ab->v_ulpi);
191 if ((volt != 1300000) && (volt != 1350000))
192 dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
193 volt);
194 }
195
196 regulator_enable(ab->v_musb);
197}
198
199static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
200{
201 int ret;
202
203 regulator_disable(ab->v_musb);
204
205 regulator_disable(ab->v_ulpi);
206
207 /* USB is not the only consumer of Vintcore, restore old settings */
208 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
209 if (ab->saved_v_ulpi > 0) {
210 ret = regulator_set_voltage(ab->v_ulpi,
211 ab->saved_v_ulpi, ab->saved_v_ulpi);
212 if (ret < 0)
213 dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
214 ab->saved_v_ulpi, ret);
215 }
216
217 ret = regulator_set_optimum_mode(ab->v_ulpi, 0);
218 if (ret < 0)
219 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
220 ret);
221 }
222
223 regulator_disable(ab->v_ape);
224}
225
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800226static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
227{
228 /* Workaround for v2.0 bug # 31952 */
229 if (is_ab8500_2p0(ab->ab8500)) {
230 abx500_mask_and_set_register_interruptible(ab->dev,
231 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
232 bit, bit);
233 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
234 }
235}
236
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200237static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100238{
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200239 u8 bit;
240 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
241 AB8500_BIT_PHY_CTRL_DEVICE_EN;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100242
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200243 ab8500_usb_regulator_enable(ab);
244
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200245 abx500_mask_and_set_register_interruptible(ab->dev,
246 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
247 bit, bit);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100248}
249
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200250static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
251{
252 u8 bit;
253 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
254 AB8500_BIT_PHY_CTRL_DEVICE_EN;
255
256 ab8500_usb_wd_linkstatus(ab, bit);
257
258 abx500_mask_and_set_register_interruptible(ab->dev,
259 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
260 bit, 0);
261
262 /* Needed to disable the phy.*/
263 ab8500_usb_wd_workaround(ab);
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200264
265 ab8500_usb_regulator_disable(ab);
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200266}
267
268#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true)
269#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
270#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false)
271#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100272
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800273static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
274 enum ab8505_usb_link_status lsts)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100275{
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800276 enum ux500_musb_vbus_id_status event = 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100277
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800278 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100279
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800280 /*
281 * Spurious link_status interrupts are seen at the time of
282 * disconnection of a device in RIDA state
283 */
284 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
285 (lsts == USB_LINK_STD_HOST_NC_8505))
286 return 0;
287
288 ab->previous_link_status_state = lsts;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100289
290 switch (lsts) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800291 case USB_LINK_ACA_RID_B_8505:
292 event = UX500_MUSB_RIDB;
293 case USB_LINK_NOT_CONFIGURED_8505:
294 case USB_LINK_RESERVED0_8505:
295 case USB_LINK_RESERVED1_8505:
296 case USB_LINK_RESERVED2_8505:
297 case USB_LINK_RESERVED3_8505:
298 ab->mode = USB_IDLE;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200299 ab->phy.otg->default_a = false;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100300 ab->vbus_draw = 0;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800301 if (event != UX500_MUSB_RIDB)
302 event = UX500_MUSB_NONE;
303 /*
304 * Fallback to default B_IDLE as nothing
305 * is connected
306 */
307 ab->phy.state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100308 break;
309
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800310 case USB_LINK_ACA_RID_C_NM_8505:
311 event = UX500_MUSB_RIDC;
312 case USB_LINK_STD_HOST_NC_8505:
313 case USB_LINK_STD_HOST_C_NS_8505:
314 case USB_LINK_STD_HOST_C_S_8505:
315 case USB_LINK_CDP_8505:
316 if (ab->mode == USB_IDLE) {
317 ab->mode = USB_PERIPHERAL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100318 ab8500_usb_peri_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800319 atomic_notifier_call_chain(&ab->phy.notifier,
320 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100321 }
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800322 if (event != UX500_MUSB_RIDC)
323 event = UX500_MUSB_VBUS;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100324 break;
325
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800326 case USB_LINK_ACA_RID_A_8505:
327 case USB_LINK_ACA_DOCK_CHGR_8505:
328 event = UX500_MUSB_RIDA;
329 case USB_LINK_HM_IDGND_8505:
330 if (ab->mode == USB_IDLE) {
331 ab->mode = USB_HOST;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100332 ab8500_usb_host_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800333 atomic_notifier_call_chain(&ab->phy.notifier,
334 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100335 }
Heikki Krogerus144713f2012-02-13 13:24:05 +0200336 ab->phy.otg->default_a = true;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800337 if (event != UX500_MUSB_RIDA)
338 event = UX500_MUSB_ID;
339 atomic_notifier_call_chain(&ab->phy.notifier,
340 event, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100341 break;
342
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800343 case USB_LINK_DEDICATED_CHG_8505:
344 ab->mode = USB_DEDICATED_CHG;
345 event = UX500_MUSB_CHARGER;
346 atomic_notifier_call_chain(&ab->phy.notifier,
347 event, &ab->vbus_draw);
348 break;
349
350 default:
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100351 break;
352 }
353
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800354 return 0;
355}
356
357static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
358 enum ab8500_usb_link_status lsts)
359{
360 enum ux500_musb_vbus_id_status event = 0;
361
362 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
363
364 /*
365 * Spurious link_status interrupts are seen in case of a
366 * disconnection of a device in IDGND and RIDA stage
367 */
368 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
369 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
370 lsts == USB_LINK_STD_HOST_NC_8500))
371 return 0;
372
373 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
374 lsts == USB_LINK_STD_HOST_NC_8500)
375 return 0;
376
377 ab->previous_link_status_state = lsts;
378
379 switch (lsts) {
380 case USB_LINK_ACA_RID_B_8500:
381 event = UX500_MUSB_RIDB;
382 case USB_LINK_NOT_CONFIGURED_8500:
383 case USB_LINK_NOT_VALID_LINK_8500:
384 ab->mode = USB_IDLE;
385 ab->phy.otg->default_a = false;
386 ab->vbus_draw = 0;
387 if (event != UX500_MUSB_RIDB)
388 event = UX500_MUSB_NONE;
389 /* Fallback to default B_IDLE as nothing is connected */
390 ab->phy.state = OTG_STATE_B_IDLE;
391 break;
392
393 case USB_LINK_ACA_RID_C_NM_8500:
394 case USB_LINK_ACA_RID_C_HS_8500:
395 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
396 event = UX500_MUSB_RIDC;
397 case USB_LINK_STD_HOST_NC_8500:
398 case USB_LINK_STD_HOST_C_NS_8500:
399 case USB_LINK_STD_HOST_C_S_8500:
400 case USB_LINK_HOST_CHG_NM_8500:
401 case USB_LINK_HOST_CHG_HS_8500:
402 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
403 if (ab->mode == USB_IDLE) {
404 ab->mode = USB_PERIPHERAL;
405 ab8500_usb_peri_phy_en(ab);
406 atomic_notifier_call_chain(&ab->phy.notifier,
407 UX500_MUSB_PREPARE, &ab->vbus_draw);
408 }
409 if (event != UX500_MUSB_RIDC)
410 event = UX500_MUSB_VBUS;
411 break;
412
413 case USB_LINK_ACA_RID_A_8500:
414 event = UX500_MUSB_RIDA;
415 case USB_LINK_HM_IDGND_8500:
416 if (ab->mode == USB_IDLE) {
417 ab->mode = USB_HOST;
418 ab8500_usb_host_phy_en(ab);
419 atomic_notifier_call_chain(&ab->phy.notifier,
420 UX500_MUSB_PREPARE, &ab->vbus_draw);
421 }
422 ab->phy.otg->default_a = true;
423 if (event != UX500_MUSB_RIDA)
424 event = UX500_MUSB_ID;
425 atomic_notifier_call_chain(&ab->phy.notifier,
426 event, &ab->vbus_draw);
427 break;
428
429 case USB_LINK_DEDICATED_CHG_8500:
430 ab->mode = USB_DEDICATED_CHG;
431 event = UX500_MUSB_CHARGER;
432 atomic_notifier_call_chain(&ab->phy.notifier,
433 event, &ab->vbus_draw);
434 break;
435
436 case USB_LINK_RESERVED_8500:
437 break;
438 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100439
440 return 0;
441}
442
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800443/*
444 * Connection Sequence:
445 * 1. Link Status Interrupt
446 * 2. Enable AB clock
447 * 3. Enable AB regulators
448 * 4. Enable USB phy
449 * 5. Reset the musb controller
450 * 6. Switch the ULPI GPIO pins to fucntion mode
451 * 7. Enable the musb Peripheral5 clock
452 * 8. Restore MUSB context
453 */
454static int abx500_usb_link_status_update(struct ab8500_usb *ab)
455{
456 u8 reg;
457 int ret = 0;
458
459 if (is_ab8500(ab->ab8500)) {
460 enum ab8500_usb_link_status lsts;
461
462 abx500_get_register_interruptible(ab->dev,
463 AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
464 lsts = (reg >> 3) & 0x0F;
465 ret = ab8500_usb_link_status_update(ab, lsts);
466 } else if (is_ab8505(ab->ab8500)) {
467 enum ab8505_usb_link_status lsts;
468
469 abx500_get_register_interruptible(ab->dev,
470 AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
471 lsts = (reg >> 3) & 0x1F;
472 ret = ab8505_usb_link_status_update(ab, lsts);
473 }
474
475 return ret;
476}
477
478/*
479 * Disconnection Sequence:
480 * 1. Disconect Interrupt
481 * 2. Disable regulators
482 * 3. Disable AB clock
483 * 4. Disable the Phy
484 * 5. Link Status Interrupt
485 * 6. Disable Musb Clock
486 */
487static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
488{
489 struct ab8500_usb *ab = (struct ab8500_usb *) data;
490 enum usb_phy_events event = UX500_MUSB_NONE;
491
492 /* Link status will not be updated till phy is disabled. */
493 if (ab->mode == USB_HOST) {
494 ab->phy.otg->default_a = false;
495 ab->vbus_draw = 0;
496 atomic_notifier_call_chain(&ab->phy.notifier,
497 event, &ab->vbus_draw);
498 ab8500_usb_host_phy_dis(ab);
499 ab->mode = USB_IDLE;
500 }
501
502 if (ab->mode == USB_PERIPHERAL) {
503 atomic_notifier_call_chain(&ab->phy.notifier,
504 event, &ab->vbus_draw);
505 ab8500_usb_peri_phy_dis(ab);
506 atomic_notifier_call_chain(&ab->phy.notifier,
507 UX500_MUSB_CLEAN, &ab->vbus_draw);
508 ab->mode = USB_IDLE;
509 ab->phy.otg->default_a = false;
510 ab->vbus_draw = 0;
511 }
512
513 if (is_ab8500_2p0(ab->ab8500)) {
514 if (ab->mode == USB_DEDICATED_CHG) {
515 ab8500_usb_wd_linkstatus(ab,
516 AB8500_BIT_PHY_CTRL_DEVICE_EN);
517 abx500_mask_and_set_register_interruptible(ab->dev,
518 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
519 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
520 }
521 }
522
523 return IRQ_HANDLED;
524}
525
526static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
527{
528 struct ab8500_usb *ab = (struct ab8500_usb *) data;
529
530 abx500_usb_link_status_update(ab);
531
532 return IRQ_HANDLED;
533}
534
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100535static void ab8500_usb_delayed_work(struct work_struct *work)
536{
537 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
538 dwork.work);
539
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800540 abx500_usb_link_status_update(ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100541}
542
543static void ab8500_usb_phy_disable_work(struct work_struct *work)
544{
545 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
546 phy_dis_work);
547
Heikki Krogerus144713f2012-02-13 13:24:05 +0200548 if (!ab->phy.otg->host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100549 ab8500_usb_host_phy_dis(ab);
550
Heikki Krogerus144713f2012-02-13 13:24:05 +0200551 if (!ab->phy.otg->gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100552 ab8500_usb_peri_phy_dis(ab);
553}
554
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200555static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
556{
557 /*
558 * AB8500 V2 has eye diagram issues when drawing more than 100mA from
559 * VBUS. Set charging current to 100mA in case of standard host
560 */
561 if (is_ab8500_2p0_or_earlier(ab->ab8500))
562 if (mA > 100)
563 mA = 100;
564
565 return mA;
566}
567
Heikki Krogerus144713f2012-02-13 13:24:05 +0200568static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100569{
570 struct ab8500_usb *ab;
571
Heikki Krogerus144713f2012-02-13 13:24:05 +0200572 if (!phy)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100573 return -ENODEV;
574
Heikki Krogerus144713f2012-02-13 13:24:05 +0200575 ab = phy_to_ab(phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100576
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200577 mA = ab8500_eyediagram_workaroud(ab, mA);
578
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100579 ab->vbus_draw = mA;
580
Fabio Baltieri77f43962013-04-03 10:45:10 +0200581 atomic_notifier_call_chain(&ab->phy.notifier,
582 UX500_MUSB_VBUS, &ab->vbus_draw);
583
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100584 return 0;
585}
586
Heikki Krogerus86753812012-02-13 13:24:02 +0200587static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100588{
589 /* TODO */
590 return 0;
591}
592
Heikki Krogerus144713f2012-02-13 13:24:05 +0200593static int ab8500_usb_set_peripheral(struct usb_otg *otg,
594 struct usb_gadget *gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100595{
596 struct ab8500_usb *ab;
597
598 if (!otg)
599 return -ENODEV;
600
Heikki Krogerus144713f2012-02-13 13:24:05 +0200601 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100602
603 /* Some drivers call this function in atomic context.
604 * Do not update ab8500 registers directly till this
605 * is fixed.
606 */
607
608 if (!gadget) {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200609 otg->gadget = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100610 schedule_work(&ab->phy_dis_work);
611 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200612 otg->gadget = gadget;
613 otg->phy->state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100614
615 /* Phy will not be enabled if cable is already
616 * plugged-in. Schedule to enable phy.
617 * Use same delay to avoid any race condition.
618 */
619 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
620 }
621
622 return 0;
623}
624
Heikki Krogerus144713f2012-02-13 13:24:05 +0200625static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100626{
627 struct ab8500_usb *ab;
628
629 if (!otg)
630 return -ENODEV;
631
Heikki Krogerus144713f2012-02-13 13:24:05 +0200632 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100633
634 /* Some drivers call this function in atomic context.
635 * Do not update ab8500 registers directly till this
636 * is fixed.
637 */
638
639 if (!host) {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200640 otg->host = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100641 schedule_work(&ab->phy_dis_work);
642 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200643 otg->host = host;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100644 /* Phy will not be enabled if cable is already
645 * plugged-in. Schedule to enable phy.
646 * Use same delay to avoid any race condition.
647 */
648 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
649 }
650
651 return 0;
652}
653
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200654static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
655{
656 int err;
657
658 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
659 if (IS_ERR(ab->v_ape)) {
660 dev_err(ab->dev, "Could not get v-ape supply\n");
661 err = PTR_ERR(ab->v_ape);
662 return err;
663 }
664
665 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
666 if (IS_ERR(ab->v_ulpi)) {
667 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
668 err = PTR_ERR(ab->v_ulpi);
669 return err;
670 }
671
672 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
673 if (IS_ERR(ab->v_musb)) {
674 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
675 err = PTR_ERR(ab->v_musb);
676 return err;
677 }
678
679 return 0;
680}
681
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800682static int ab8500_usb_irq_setup(struct platform_device *pdev,
683 struct ab8500_usb *ab)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100684{
685 int err;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800686 int irq;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100687
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800688 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
689 if (irq < 0) {
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100690 dev_err(&pdev->dev, "Link status irq not found\n");
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800691 return irq;
692 }
693 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
694 ab8500_usb_link_status_irq,
695 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
696 if (err < 0) {
697 dev_err(ab->dev, "request_irq failed for link status irq\n");
698 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100699 }
700
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800701 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
702 if (irq < 0) {
703 dev_err(&pdev->dev, "ID fall irq not found\n");
704 return irq;
705 }
706 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
707 ab8500_usb_disconnect_irq,
708 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100709 if (err < 0) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800710 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
711 return err;
712 }
713
714 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
715 if (irq < 0) {
716 dev_err(&pdev->dev, "VBUS fall irq not found\n");
717 return irq;
718 }
719 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
720 ab8500_usb_disconnect_irq,
721 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
722 if (err < 0) {
723 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100724 return err;
725 }
726
727 return 0;
728}
729
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500730static int ab8500_usb_probe(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100731{
732 struct ab8500_usb *ab;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800733 struct ab8500 *ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200734 struct usb_otg *otg;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100735 int err;
736 int rev;
737
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800738 ab8500 = dev_get_drvdata(pdev->dev.parent);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100739 rev = abx500_get_chip_id(&pdev->dev);
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800740
741 if (is_ab8500_1p1_or_earlier(ab8500)) {
742 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100743 return -ENODEV;
744 }
745
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200746 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100747 if (!ab)
748 return -ENOMEM;
749
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200750 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
751 if (!otg)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200752 return -ENOMEM;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200753
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100754 ab->dev = &pdev->dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800755 ab->ab8500 = ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200756 ab->phy.dev = ab->dev;
757 ab->phy.otg = otg;
758 ab->phy.label = "ab8500";
759 ab->phy.set_suspend = ab8500_usb_set_suspend;
760 ab->phy.set_power = ab8500_usb_set_power;
761 ab->phy.state = OTG_STATE_UNDEFINED;
762
763 otg->phy = &ab->phy;
764 otg->set_host = ab8500_usb_set_host;
765 otg->set_peripheral = ab8500_usb_set_peripheral;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100766
767 platform_set_drvdata(pdev, ab);
768
Heikki Krogerus144713f2012-02-13 13:24:05 +0200769 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100770
771 /* v1: Wait for link status to become stable.
772 * all: Updates form set_host and set_peripheral as they are atomic.
773 */
774 INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
775
776 /* all: Disable phy when called from set_host and set_peripheral */
777 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
778
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200779 err = ab8500_usb_regulator_get(ab);
780 if (err)
781 return err;
782
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800783 err = ab8500_usb_irq_setup(pdev, ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100784 if (err < 0)
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200785 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100786
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530787 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100788 if (err) {
789 dev_err(&pdev->dev, "Can't register transceiver\n");
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200790 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100791 }
792
Sakethram Bommisetti71246312013-04-03 10:45:04 +0200793 /* Phy tuning values for AB8500 */
794 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
795 /* Enable the PBT/Bank 0x12 access */
796 err = abx500_set_register_interruptible(ab->dev,
797 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
798 if (err < 0)
799 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
800 err);
801
802 err = abx500_set_register_interruptible(ab->dev,
803 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
804 if (err < 0)
805 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
806 err);
807
808 err = abx500_set_register_interruptible(ab->dev,
809 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
810 if (err < 0)
811 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
812 err);
813
814 err = abx500_set_register_interruptible(ab->dev,
815 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
816 if (err < 0)
817 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
818 err);
819
820 /* Switch to normal mode/disable Bank 0x12 access */
821 err = abx500_set_register_interruptible(ab->dev,
822 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
823 if (err < 0)
824 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
825 err);
826 }
827
828 /* Phy tuning values for AB8505 */
829 if (is_ab8505(ab->ab8500)) {
830 /* Enable the PBT/Bank 0x12 access */
831 err = abx500_mask_and_set_register_interruptible(ab->dev,
832 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
833 0x01, 0x01);
834 if (err < 0)
835 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
836 err);
837
838 err = abx500_mask_and_set_register_interruptible(ab->dev,
839 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
840 0xC8, 0xC8);
841 if (err < 0)
842 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
843 err);
844
845 err = abx500_mask_and_set_register_interruptible(ab->dev,
846 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
847 0x60, 0x60);
848 if (err < 0)
849 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
850 err);
851
852 err = abx500_mask_and_set_register_interruptible(ab->dev,
853 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
854 0xFC, 0x80);
855
856 if (err < 0)
857 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
858 err);
859
860 /* Switch to normal mode/disable Bank 0x12 access */
861 err = abx500_mask_and_set_register_interruptible(ab->dev,
862 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
863 0x00, 0x00);
864 if (err < 0)
865 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
866 err);
867 }
868
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800869 /* Needed to enable ID detection. */
870 ab8500_usb_wd_workaround(ab);
871
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800872 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100873
874 return 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100875}
876
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500877static int ab8500_usb_remove(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100878{
879 struct ab8500_usb *ab = platform_get_drvdata(pdev);
880
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100881 cancel_delayed_work_sync(&ab->dwork);
882
883 cancel_work_sync(&ab->phy_dis_work);
884
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530885 usb_remove_phy(&ab->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100886
Mian Yousaf Kaukabf5ef7b42013-04-03 10:45:09 +0200887 if (ab->mode == USB_HOST)
888 ab8500_usb_host_phy_dis(ab);
889 else if (ab->mode == USB_PERIPHERAL)
890 ab8500_usb_peri_phy_dis(ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100891
892 platform_set_drvdata(pdev, NULL);
893
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100894 return 0;
895}
896
897static struct platform_driver ab8500_usb_driver = {
898 .probe = ab8500_usb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500899 .remove = ab8500_usb_remove,
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100900 .driver = {
901 .name = "ab8500-usb",
902 .owner = THIS_MODULE,
903 },
904};
905
906static int __init ab8500_usb_init(void)
907{
908 return platform_driver_register(&ab8500_usb_driver);
909}
910subsys_initcall(ab8500_usb_init);
911
912static void __exit ab8500_usb_exit(void)
913{
914 platform_driver_unregister(&ab8500_usb_driver);
915}
916module_exit(ab8500_usb_exit);
917
918MODULE_ALIAS("platform:ab8500_usb");
919MODULE_AUTHOR("ST-Ericsson AB");
920MODULE_DESCRIPTION("AB8500 usb transceiver driver");
921MODULE_LICENSE("GPL");