blob: fec260ee67d3fa1ef149c4f011529b1fce8f6a28 [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
581 if (mA)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200582 atomic_notifier_call_chain(&ab->phy.notifier,
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800583 UX500_MUSB_ENUMERATED, ab->phy.otg->gadget);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100584 return 0;
585}
586
587/* TODO: Implement some way for charging or other drivers to read
588 * ab->vbus_draw.
589 */
590
Heikki Krogerus86753812012-02-13 13:24:02 +0200591static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100592{
593 /* TODO */
594 return 0;
595}
596
Heikki Krogerus144713f2012-02-13 13:24:05 +0200597static int ab8500_usb_set_peripheral(struct usb_otg *otg,
598 struct usb_gadget *gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100599{
600 struct ab8500_usb *ab;
601
602 if (!otg)
603 return -ENODEV;
604
Heikki Krogerus144713f2012-02-13 13:24:05 +0200605 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100606
607 /* Some drivers call this function in atomic context.
608 * Do not update ab8500 registers directly till this
609 * is fixed.
610 */
611
612 if (!gadget) {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200613 otg->gadget = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100614 schedule_work(&ab->phy_dis_work);
615 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200616 otg->gadget = gadget;
617 otg->phy->state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100618
619 /* Phy will not be enabled if cable is already
620 * plugged-in. Schedule to enable phy.
621 * Use same delay to avoid any race condition.
622 */
623 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
624 }
625
626 return 0;
627}
628
Heikki Krogerus144713f2012-02-13 13:24:05 +0200629static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100630{
631 struct ab8500_usb *ab;
632
633 if (!otg)
634 return -ENODEV;
635
Heikki Krogerus144713f2012-02-13 13:24:05 +0200636 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100637
638 /* Some drivers call this function in atomic context.
639 * Do not update ab8500 registers directly till this
640 * is fixed.
641 */
642
643 if (!host) {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200644 otg->host = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100645 schedule_work(&ab->phy_dis_work);
646 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200647 otg->host = host;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100648 /* Phy will not be enabled if cable is already
649 * plugged-in. Schedule to enable phy.
650 * Use same delay to avoid any race condition.
651 */
652 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
653 }
654
655 return 0;
656}
657
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200658static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
659{
660 int err;
661
662 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
663 if (IS_ERR(ab->v_ape)) {
664 dev_err(ab->dev, "Could not get v-ape supply\n");
665 err = PTR_ERR(ab->v_ape);
666 return err;
667 }
668
669 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
670 if (IS_ERR(ab->v_ulpi)) {
671 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
672 err = PTR_ERR(ab->v_ulpi);
673 return err;
674 }
675
676 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
677 if (IS_ERR(ab->v_musb)) {
678 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
679 err = PTR_ERR(ab->v_musb);
680 return err;
681 }
682
683 return 0;
684}
685
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800686static int ab8500_usb_irq_setup(struct platform_device *pdev,
687 struct ab8500_usb *ab)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100688{
689 int err;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800690 int irq;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100691
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800692 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
693 if (irq < 0) {
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100694 dev_err(&pdev->dev, "Link status irq not found\n");
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800695 return irq;
696 }
697 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
698 ab8500_usb_link_status_irq,
699 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
700 if (err < 0) {
701 dev_err(ab->dev, "request_irq failed for link status irq\n");
702 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100703 }
704
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800705 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
706 if (irq < 0) {
707 dev_err(&pdev->dev, "ID fall irq not found\n");
708 return irq;
709 }
710 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
711 ab8500_usb_disconnect_irq,
712 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100713 if (err < 0) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800714 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
715 return err;
716 }
717
718 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
719 if (irq < 0) {
720 dev_err(&pdev->dev, "VBUS fall irq not found\n");
721 return irq;
722 }
723 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
724 ab8500_usb_disconnect_irq,
725 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
726 if (err < 0) {
727 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100728 return err;
729 }
730
731 return 0;
732}
733
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500734static int ab8500_usb_probe(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100735{
736 struct ab8500_usb *ab;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800737 struct ab8500 *ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200738 struct usb_otg *otg;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100739 int err;
740 int rev;
741
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800742 ab8500 = dev_get_drvdata(pdev->dev.parent);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100743 rev = abx500_get_chip_id(&pdev->dev);
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800744
745 if (is_ab8500_1p1_or_earlier(ab8500)) {
746 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100747 return -ENODEV;
748 }
749
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200750 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100751 if (!ab)
752 return -ENOMEM;
753
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200754 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
755 if (!otg)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200756 return -ENOMEM;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200757
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100758 ab->dev = &pdev->dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800759 ab->ab8500 = ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200760 ab->phy.dev = ab->dev;
761 ab->phy.otg = otg;
762 ab->phy.label = "ab8500";
763 ab->phy.set_suspend = ab8500_usb_set_suspend;
764 ab->phy.set_power = ab8500_usb_set_power;
765 ab->phy.state = OTG_STATE_UNDEFINED;
766
767 otg->phy = &ab->phy;
768 otg->set_host = ab8500_usb_set_host;
769 otg->set_peripheral = ab8500_usb_set_peripheral;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100770
771 platform_set_drvdata(pdev, ab);
772
Heikki Krogerus144713f2012-02-13 13:24:05 +0200773 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100774
775 /* v1: Wait for link status to become stable.
776 * all: Updates form set_host and set_peripheral as they are atomic.
777 */
778 INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
779
780 /* all: Disable phy when called from set_host and set_peripheral */
781 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
782
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200783 err = ab8500_usb_regulator_get(ab);
784 if (err)
785 return err;
786
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800787 err = ab8500_usb_irq_setup(pdev, ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100788 if (err < 0)
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200789 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100790
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530791 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100792 if (err) {
793 dev_err(&pdev->dev, "Can't register transceiver\n");
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200794 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100795 }
796
Sakethram Bommisetti71246312013-04-03 10:45:04 +0200797 /* Phy tuning values for AB8500 */
798 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
799 /* Enable the PBT/Bank 0x12 access */
800 err = abx500_set_register_interruptible(ab->dev,
801 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
802 if (err < 0)
803 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
804 err);
805
806 err = abx500_set_register_interruptible(ab->dev,
807 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
808 if (err < 0)
809 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
810 err);
811
812 err = abx500_set_register_interruptible(ab->dev,
813 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
814 if (err < 0)
815 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
816 err);
817
818 err = abx500_set_register_interruptible(ab->dev,
819 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
820 if (err < 0)
821 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
822 err);
823
824 /* Switch to normal mode/disable Bank 0x12 access */
825 err = abx500_set_register_interruptible(ab->dev,
826 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
827 if (err < 0)
828 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
829 err);
830 }
831
832 /* Phy tuning values for AB8505 */
833 if (is_ab8505(ab->ab8500)) {
834 /* Enable the PBT/Bank 0x12 access */
835 err = abx500_mask_and_set_register_interruptible(ab->dev,
836 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
837 0x01, 0x01);
838 if (err < 0)
839 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
840 err);
841
842 err = abx500_mask_and_set_register_interruptible(ab->dev,
843 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
844 0xC8, 0xC8);
845 if (err < 0)
846 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
847 err);
848
849 err = abx500_mask_and_set_register_interruptible(ab->dev,
850 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
851 0x60, 0x60);
852 if (err < 0)
853 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
854 err);
855
856 err = abx500_mask_and_set_register_interruptible(ab->dev,
857 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
858 0xFC, 0x80);
859
860 if (err < 0)
861 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
862 err);
863
864 /* Switch to normal mode/disable Bank 0x12 access */
865 err = abx500_mask_and_set_register_interruptible(ab->dev,
866 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
867 0x00, 0x00);
868 if (err < 0)
869 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
870 err);
871 }
872
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800873 /* Needed to enable ID detection. */
874 ab8500_usb_wd_workaround(ab);
875
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800876 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100877
878 return 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100879}
880
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500881static int ab8500_usb_remove(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100882{
883 struct ab8500_usb *ab = platform_get_drvdata(pdev);
884
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100885 cancel_delayed_work_sync(&ab->dwork);
886
887 cancel_work_sync(&ab->phy_dis_work);
888
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530889 usb_remove_phy(&ab->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100890
Mian Yousaf Kaukabf5ef7b42013-04-03 10:45:09 +0200891 if (ab->mode == USB_HOST)
892 ab8500_usb_host_phy_dis(ab);
893 else if (ab->mode == USB_PERIPHERAL)
894 ab8500_usb_peri_phy_dis(ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100895
896 platform_set_drvdata(pdev, NULL);
897
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100898 return 0;
899}
900
901static struct platform_driver ab8500_usb_driver = {
902 .probe = ab8500_usb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500903 .remove = ab8500_usb_remove,
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100904 .driver = {
905 .name = "ab8500-usb",
906 .owner = THIS_MODULE,
907 },
908};
909
910static int __init ab8500_usb_init(void)
911{
912 return platform_driver_register(&ab8500_usb_driver);
913}
914subsys_initcall(ab8500_usb_init);
915
916static void __exit ab8500_usb_exit(void)
917{
918 platform_driver_unregister(&ab8500_usb_driver);
919}
920module_exit(ab8500_usb_exit);
921
922MODULE_ALIAS("platform:ab8500_usb");
923MODULE_AUTHOR("ST-Ericsson AB");
924MODULE_DESCRIPTION("AB8500 usb transceiver driver");
925MODULE_LICENSE("GPL");