blob: a1c103fc26ec8e6107767dabdef8bbdeeed671cf [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>
Patrice Chotard899f0f52013-04-03 10:45:11 +020036#include <linux/pinctrl/consumer.h>
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010037
Sakethram Bommisetti71246312013-04-03 10:45:04 +020038/* Bank AB8500_SYS_CTRL2_BLOCK */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010039#define AB8500_MAIN_WD_CTRL_REG 0x01
Sakethram Bommisetti71246312013-04-03 10:45:04 +020040
41/* Bank AB8500_USB */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010042#define AB8500_USB_LINE_STAT_REG 0x80
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080043#define AB8505_USB_LINE_STAT_REG 0x94
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010044#define AB8500_USB_PHY_CTRL_REG 0x8A
45
Sakethram Bommisetti71246312013-04-03 10:45:04 +020046/* Bank AB8500_DEVELOPMENT */
47#define AB8500_BANK12_ACCESS 0x00
48
49/* Bank AB8500_DEBUG */
50#define AB8500_USB_PHY_TUNE1 0x05
51#define AB8500_USB_PHY_TUNE2 0x06
52#define AB8500_USB_PHY_TUNE3 0x07
53
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010054#define AB8500_BIT_OTG_STAT_ID (1 << 0)
55#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
56#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
57#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
58#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
59
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010060#define AB8500_WD_KICK_DELAY_US 100 /* usec */
61#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080062#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010063
64/* Usb line status register */
65enum ab8500_usb_link_status {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080066 USB_LINK_NOT_CONFIGURED_8500 = 0,
67 USB_LINK_STD_HOST_NC_8500,
68 USB_LINK_STD_HOST_C_NS_8500,
69 USB_LINK_STD_HOST_C_S_8500,
70 USB_LINK_HOST_CHG_NM_8500,
71 USB_LINK_HOST_CHG_HS_8500,
72 USB_LINK_HOST_CHG_HS_CHIRP_8500,
73 USB_LINK_DEDICATED_CHG_8500,
74 USB_LINK_ACA_RID_A_8500,
75 USB_LINK_ACA_RID_B_8500,
76 USB_LINK_ACA_RID_C_NM_8500,
77 USB_LINK_ACA_RID_C_HS_8500,
78 USB_LINK_ACA_RID_C_HS_CHIRP_8500,
79 USB_LINK_HM_IDGND_8500,
80 USB_LINK_RESERVED_8500,
81 USB_LINK_NOT_VALID_LINK_8500,
82};
83
84enum ab8505_usb_link_status {
85 USB_LINK_NOT_CONFIGURED_8505 = 0,
86 USB_LINK_STD_HOST_NC_8505,
87 USB_LINK_STD_HOST_C_NS_8505,
88 USB_LINK_STD_HOST_C_S_8505,
89 USB_LINK_CDP_8505,
90 USB_LINK_RESERVED0_8505,
91 USB_LINK_RESERVED1_8505,
92 USB_LINK_DEDICATED_CHG_8505,
93 USB_LINK_ACA_RID_A_8505,
94 USB_LINK_ACA_RID_B_8505,
95 USB_LINK_ACA_RID_C_NM_8505,
96 USB_LINK_RESERVED2_8505,
97 USB_LINK_RESERVED3_8505,
98 USB_LINK_HM_IDGND_8505,
99 USB_LINK_CHARGERPORT_NOT_OK_8505,
100 USB_LINK_CHARGER_DM_HIGH_8505,
101 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
102 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
103 USB_LINK_STD_UPSTREAM_8505,
104 USB_LINK_CHARGER_SE1_8505,
105 USB_LINK_CARKIT_CHGR_1_8505,
106 USB_LINK_CARKIT_CHGR_2_8505,
107 USB_LINK_ACA_DOCK_CHGR_8505,
108 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
109 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
110 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
111 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
112 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
113};
114
115enum ab8500_usb_mode {
116 USB_IDLE = 0,
117 USB_PERIPHERAL,
118 USB_HOST,
119 USB_DEDICATED_CHG
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100120};
121
122struct ab8500_usb {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200123 struct usb_phy phy;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100124 struct device *dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800125 struct ab8500 *ab8500;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100126 unsigned vbus_draw;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100127 struct work_struct phy_dis_work;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800128 enum ab8500_usb_mode mode;
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200129 struct regulator *v_ape;
130 struct regulator *v_musb;
131 struct regulator *v_ulpi;
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200132 int saved_v_ulpi;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800133 int previous_link_status_state;
Patrice Chotard899f0f52013-04-03 10:45:11 +0200134 struct pinctrl *pinctrl;
135 struct pinctrl_state *pins_sleep;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100136};
137
Heikki Krogerus144713f2012-02-13 13:24:05 +0200138static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100139{
Heikki Krogerus144713f2012-02-13 13:24:05 +0200140 return container_of(x, struct ab8500_usb, phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100141}
142
143static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
144{
145 abx500_set_register_interruptible(ab->dev,
146 AB8500_SYS_CTRL2_BLOCK,
147 AB8500_MAIN_WD_CTRL_REG,
148 AB8500_BIT_WD_CTRL_ENABLE);
149
150 udelay(AB8500_WD_KICK_DELAY_US);
151
152 abx500_set_register_interruptible(ab->dev,
153 AB8500_SYS_CTRL2_BLOCK,
154 AB8500_MAIN_WD_CTRL_REG,
155 (AB8500_BIT_WD_CTRL_ENABLE
156 | AB8500_BIT_WD_CTRL_KICK));
157
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800158 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100159
160 abx500_set_register_interruptible(ab->dev,
161 AB8500_SYS_CTRL2_BLOCK,
162 AB8500_MAIN_WD_CTRL_REG,
163 0);
164}
165
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200166static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
167{
168 int ret, volt;
169
170 regulator_enable(ab->v_ape);
171
172 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
173 ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
174 if (ab->saved_v_ulpi < 0)
175 dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
176
177 ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
178 if (ret < 0)
179 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
180 ret);
181
182 ret = regulator_set_optimum_mode(ab->v_ulpi, 28000);
183 if (ret < 0)
184 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
185 ret);
186 }
187
188 regulator_enable(ab->v_ulpi);
189
190 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
191 volt = regulator_get_voltage(ab->v_ulpi);
192 if ((volt != 1300000) && (volt != 1350000))
193 dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
194 volt);
195 }
196
197 regulator_enable(ab->v_musb);
198}
199
200static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
201{
202 int ret;
203
204 regulator_disable(ab->v_musb);
205
206 regulator_disable(ab->v_ulpi);
207
208 /* USB is not the only consumer of Vintcore, restore old settings */
209 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
210 if (ab->saved_v_ulpi > 0) {
211 ret = regulator_set_voltage(ab->v_ulpi,
212 ab->saved_v_ulpi, ab->saved_v_ulpi);
213 if (ret < 0)
214 dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
215 ab->saved_v_ulpi, ret);
216 }
217
218 ret = regulator_set_optimum_mode(ab->v_ulpi, 0);
219 if (ret < 0)
220 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
221 ret);
222 }
223
224 regulator_disable(ab->v_ape);
225}
226
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800227static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
228{
229 /* Workaround for v2.0 bug # 31952 */
230 if (is_ab8500_2p0(ab->ab8500)) {
231 abx500_mask_and_set_register_interruptible(ab->dev,
232 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
233 bit, bit);
234 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
235 }
236}
237
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200238static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100239{
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200240 u8 bit;
241 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
242 AB8500_BIT_PHY_CTRL_DEVICE_EN;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100243
Patrice Chotard899f0f52013-04-03 10:45:11 +0200244 /* mux and configure USB pins to DEFAULT state */
245 ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
246 if (IS_ERR(ab->pinctrl))
247 dev_err(ab->dev, "could not get/set default pinstate\n");
248
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200249 ab8500_usb_regulator_enable(ab);
250
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200251 abx500_mask_and_set_register_interruptible(ab->dev,
252 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
253 bit, bit);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100254}
255
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200256static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
257{
258 u8 bit;
259 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
260 AB8500_BIT_PHY_CTRL_DEVICE_EN;
261
262 ab8500_usb_wd_linkstatus(ab, bit);
263
264 abx500_mask_and_set_register_interruptible(ab->dev,
265 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
266 bit, 0);
267
268 /* Needed to disable the phy.*/
269 ab8500_usb_wd_workaround(ab);
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200270
271 ab8500_usb_regulator_disable(ab);
Patrice Chotard899f0f52013-04-03 10:45:11 +0200272
273 if (!IS_ERR(ab->pinctrl)) {
274 /* configure USB pins to SLEEP state */
275 ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
276 PINCTRL_STATE_SLEEP);
277
278 if (IS_ERR(ab->pins_sleep))
279 dev_dbg(ab->dev, "could not get sleep pinstate\n");
280 else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
281 dev_err(ab->dev, "could not set pins to sleep state\n");
282
283 /* as USB pins are shared with idddet, release them to allow
284 * iddet to request them
285 */
286 pinctrl_put(ab->pinctrl);
287 }
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200288}
289
290#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true)
291#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
292#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false)
293#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100294
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800295static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
296 enum ab8505_usb_link_status lsts)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100297{
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800298 enum ux500_musb_vbus_id_status event = 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100299
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800300 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100301
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800302 /*
303 * Spurious link_status interrupts are seen at the time of
304 * disconnection of a device in RIDA state
305 */
306 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
307 (lsts == USB_LINK_STD_HOST_NC_8505))
308 return 0;
309
310 ab->previous_link_status_state = lsts;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100311
312 switch (lsts) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800313 case USB_LINK_ACA_RID_B_8505:
314 event = UX500_MUSB_RIDB;
315 case USB_LINK_NOT_CONFIGURED_8505:
316 case USB_LINK_RESERVED0_8505:
317 case USB_LINK_RESERVED1_8505:
318 case USB_LINK_RESERVED2_8505:
319 case USB_LINK_RESERVED3_8505:
320 ab->mode = USB_IDLE;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200321 ab->phy.otg->default_a = false;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100322 ab->vbus_draw = 0;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800323 if (event != UX500_MUSB_RIDB)
324 event = UX500_MUSB_NONE;
325 /*
326 * Fallback to default B_IDLE as nothing
327 * is connected
328 */
329 ab->phy.state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100330 break;
331
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800332 case USB_LINK_ACA_RID_C_NM_8505:
333 event = UX500_MUSB_RIDC;
334 case USB_LINK_STD_HOST_NC_8505:
335 case USB_LINK_STD_HOST_C_NS_8505:
336 case USB_LINK_STD_HOST_C_S_8505:
337 case USB_LINK_CDP_8505:
338 if (ab->mode == USB_IDLE) {
339 ab->mode = USB_PERIPHERAL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100340 ab8500_usb_peri_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800341 atomic_notifier_call_chain(&ab->phy.notifier,
342 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100343 }
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800344 if (event != UX500_MUSB_RIDC)
345 event = UX500_MUSB_VBUS;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100346 break;
347
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800348 case USB_LINK_ACA_RID_A_8505:
349 case USB_LINK_ACA_DOCK_CHGR_8505:
350 event = UX500_MUSB_RIDA;
351 case USB_LINK_HM_IDGND_8505:
352 if (ab->mode == USB_IDLE) {
353 ab->mode = USB_HOST;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100354 ab8500_usb_host_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800355 atomic_notifier_call_chain(&ab->phy.notifier,
356 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100357 }
Heikki Krogerus144713f2012-02-13 13:24:05 +0200358 ab->phy.otg->default_a = true;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800359 if (event != UX500_MUSB_RIDA)
360 event = UX500_MUSB_ID;
361 atomic_notifier_call_chain(&ab->phy.notifier,
362 event, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100363 break;
364
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800365 case USB_LINK_DEDICATED_CHG_8505:
366 ab->mode = USB_DEDICATED_CHG;
367 event = UX500_MUSB_CHARGER;
368 atomic_notifier_call_chain(&ab->phy.notifier,
369 event, &ab->vbus_draw);
370 break;
371
372 default:
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100373 break;
374 }
375
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800376 return 0;
377}
378
379static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
380 enum ab8500_usb_link_status lsts)
381{
382 enum ux500_musb_vbus_id_status event = 0;
383
384 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
385
386 /*
387 * Spurious link_status interrupts are seen in case of a
388 * disconnection of a device in IDGND and RIDA stage
389 */
390 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
391 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
392 lsts == USB_LINK_STD_HOST_NC_8500))
393 return 0;
394
395 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
396 lsts == USB_LINK_STD_HOST_NC_8500)
397 return 0;
398
399 ab->previous_link_status_state = lsts;
400
401 switch (lsts) {
402 case USB_LINK_ACA_RID_B_8500:
403 event = UX500_MUSB_RIDB;
404 case USB_LINK_NOT_CONFIGURED_8500:
405 case USB_LINK_NOT_VALID_LINK_8500:
406 ab->mode = USB_IDLE;
407 ab->phy.otg->default_a = false;
408 ab->vbus_draw = 0;
409 if (event != UX500_MUSB_RIDB)
410 event = UX500_MUSB_NONE;
411 /* Fallback to default B_IDLE as nothing is connected */
412 ab->phy.state = OTG_STATE_B_IDLE;
413 break;
414
415 case USB_LINK_ACA_RID_C_NM_8500:
416 case USB_LINK_ACA_RID_C_HS_8500:
417 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
418 event = UX500_MUSB_RIDC;
419 case USB_LINK_STD_HOST_NC_8500:
420 case USB_LINK_STD_HOST_C_NS_8500:
421 case USB_LINK_STD_HOST_C_S_8500:
422 case USB_LINK_HOST_CHG_NM_8500:
423 case USB_LINK_HOST_CHG_HS_8500:
424 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
425 if (ab->mode == USB_IDLE) {
426 ab->mode = USB_PERIPHERAL;
427 ab8500_usb_peri_phy_en(ab);
428 atomic_notifier_call_chain(&ab->phy.notifier,
429 UX500_MUSB_PREPARE, &ab->vbus_draw);
430 }
431 if (event != UX500_MUSB_RIDC)
432 event = UX500_MUSB_VBUS;
433 break;
434
435 case USB_LINK_ACA_RID_A_8500:
436 event = UX500_MUSB_RIDA;
437 case USB_LINK_HM_IDGND_8500:
438 if (ab->mode == USB_IDLE) {
439 ab->mode = USB_HOST;
440 ab8500_usb_host_phy_en(ab);
441 atomic_notifier_call_chain(&ab->phy.notifier,
442 UX500_MUSB_PREPARE, &ab->vbus_draw);
443 }
444 ab->phy.otg->default_a = true;
445 if (event != UX500_MUSB_RIDA)
446 event = UX500_MUSB_ID;
447 atomic_notifier_call_chain(&ab->phy.notifier,
448 event, &ab->vbus_draw);
449 break;
450
451 case USB_LINK_DEDICATED_CHG_8500:
452 ab->mode = USB_DEDICATED_CHG;
453 event = UX500_MUSB_CHARGER;
454 atomic_notifier_call_chain(&ab->phy.notifier,
455 event, &ab->vbus_draw);
456 break;
457
458 case USB_LINK_RESERVED_8500:
459 break;
460 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100461
462 return 0;
463}
464
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800465/*
466 * Connection Sequence:
467 * 1. Link Status Interrupt
468 * 2. Enable AB clock
469 * 3. Enable AB regulators
470 * 4. Enable USB phy
471 * 5. Reset the musb controller
472 * 6. Switch the ULPI GPIO pins to fucntion mode
473 * 7. Enable the musb Peripheral5 clock
474 * 8. Restore MUSB context
475 */
476static int abx500_usb_link_status_update(struct ab8500_usb *ab)
477{
478 u8 reg;
479 int ret = 0;
480
481 if (is_ab8500(ab->ab8500)) {
482 enum ab8500_usb_link_status lsts;
483
484 abx500_get_register_interruptible(ab->dev,
485 AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
486 lsts = (reg >> 3) & 0x0F;
487 ret = ab8500_usb_link_status_update(ab, lsts);
488 } else if (is_ab8505(ab->ab8500)) {
489 enum ab8505_usb_link_status lsts;
490
491 abx500_get_register_interruptible(ab->dev,
492 AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
493 lsts = (reg >> 3) & 0x1F;
494 ret = ab8505_usb_link_status_update(ab, lsts);
495 }
496
497 return ret;
498}
499
500/*
501 * Disconnection Sequence:
502 * 1. Disconect Interrupt
503 * 2. Disable regulators
504 * 3. Disable AB clock
505 * 4. Disable the Phy
506 * 5. Link Status Interrupt
507 * 6. Disable Musb Clock
508 */
509static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
510{
511 struct ab8500_usb *ab = (struct ab8500_usb *) data;
512 enum usb_phy_events event = UX500_MUSB_NONE;
513
514 /* Link status will not be updated till phy is disabled. */
515 if (ab->mode == USB_HOST) {
516 ab->phy.otg->default_a = false;
517 ab->vbus_draw = 0;
518 atomic_notifier_call_chain(&ab->phy.notifier,
519 event, &ab->vbus_draw);
520 ab8500_usb_host_phy_dis(ab);
521 ab->mode = USB_IDLE;
522 }
523
524 if (ab->mode == USB_PERIPHERAL) {
525 atomic_notifier_call_chain(&ab->phy.notifier,
526 event, &ab->vbus_draw);
527 ab8500_usb_peri_phy_dis(ab);
528 atomic_notifier_call_chain(&ab->phy.notifier,
529 UX500_MUSB_CLEAN, &ab->vbus_draw);
530 ab->mode = USB_IDLE;
531 ab->phy.otg->default_a = false;
532 ab->vbus_draw = 0;
533 }
534
535 if (is_ab8500_2p0(ab->ab8500)) {
536 if (ab->mode == USB_DEDICATED_CHG) {
537 ab8500_usb_wd_linkstatus(ab,
538 AB8500_BIT_PHY_CTRL_DEVICE_EN);
539 abx500_mask_and_set_register_interruptible(ab->dev,
540 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
541 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
542 }
543 }
544
545 return IRQ_HANDLED;
546}
547
548static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
549{
550 struct ab8500_usb *ab = (struct ab8500_usb *) data;
551
552 abx500_usb_link_status_update(ab);
553
554 return IRQ_HANDLED;
555}
556
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100557static void ab8500_usb_phy_disable_work(struct work_struct *work)
558{
559 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
560 phy_dis_work);
561
Heikki Krogerus144713f2012-02-13 13:24:05 +0200562 if (!ab->phy.otg->host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100563 ab8500_usb_host_phy_dis(ab);
564
Heikki Krogerus144713f2012-02-13 13:24:05 +0200565 if (!ab->phy.otg->gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100566 ab8500_usb_peri_phy_dis(ab);
567}
568
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200569static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
570{
571 /*
572 * AB8500 V2 has eye diagram issues when drawing more than 100mA from
573 * VBUS. Set charging current to 100mA in case of standard host
574 */
575 if (is_ab8500_2p0_or_earlier(ab->ab8500))
576 if (mA > 100)
577 mA = 100;
578
579 return mA;
580}
581
Heikki Krogerus144713f2012-02-13 13:24:05 +0200582static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100583{
584 struct ab8500_usb *ab;
585
Heikki Krogerus144713f2012-02-13 13:24:05 +0200586 if (!phy)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100587 return -ENODEV;
588
Heikki Krogerus144713f2012-02-13 13:24:05 +0200589 ab = phy_to_ab(phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100590
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200591 mA = ab8500_eyediagram_workaroud(ab, mA);
592
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100593 ab->vbus_draw = mA;
594
Fabio Baltieri77f43962013-04-03 10:45:10 +0200595 atomic_notifier_call_chain(&ab->phy.notifier,
596 UX500_MUSB_VBUS, &ab->vbus_draw);
597
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100598 return 0;
599}
600
Heikki Krogerus86753812012-02-13 13:24:02 +0200601static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100602{
603 /* TODO */
604 return 0;
605}
606
Heikki Krogerus144713f2012-02-13 13:24:05 +0200607static int ab8500_usb_set_peripheral(struct usb_otg *otg,
608 struct usb_gadget *gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100609{
610 struct ab8500_usb *ab;
611
612 if (!otg)
613 return -ENODEV;
614
Heikki Krogerus144713f2012-02-13 13:24:05 +0200615 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100616
617 /* Some drivers call this function in atomic context.
618 * Do not update ab8500 registers directly till this
619 * is fixed.
620 */
621
622 if (!gadget) {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200623 otg->gadget = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100624 schedule_work(&ab->phy_dis_work);
625 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200626 otg->gadget = gadget;
627 otg->phy->state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100628 }
629
630 return 0;
631}
632
Heikki Krogerus144713f2012-02-13 13:24:05 +0200633static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100634{
635 struct ab8500_usb *ab;
636
637 if (!otg)
638 return -ENODEV;
639
Heikki Krogerus144713f2012-02-13 13:24:05 +0200640 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100641
642 /* Some drivers call this function in atomic context.
643 * Do not update ab8500 registers directly till this
644 * is fixed.
645 */
646
647 if (!host) {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200648 otg->host = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100649 schedule_work(&ab->phy_dis_work);
650 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200651 otg->host = host;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100652 }
653
654 return 0;
655}
656
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200657static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
658{
659 int err;
660
661 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
662 if (IS_ERR(ab->v_ape)) {
663 dev_err(ab->dev, "Could not get v-ape supply\n");
664 err = PTR_ERR(ab->v_ape);
665 return err;
666 }
667
668 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
669 if (IS_ERR(ab->v_ulpi)) {
670 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
671 err = PTR_ERR(ab->v_ulpi);
672 return err;
673 }
674
675 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
676 if (IS_ERR(ab->v_musb)) {
677 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
678 err = PTR_ERR(ab->v_musb);
679 return err;
680 }
681
682 return 0;
683}
684
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800685static int ab8500_usb_irq_setup(struct platform_device *pdev,
686 struct ab8500_usb *ab)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100687{
688 int err;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800689 int irq;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100690
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800691 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
692 if (irq < 0) {
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100693 dev_err(&pdev->dev, "Link status irq not found\n");
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800694 return irq;
695 }
696 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
697 ab8500_usb_link_status_irq,
698 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
699 if (err < 0) {
700 dev_err(ab->dev, "request_irq failed for link status irq\n");
701 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100702 }
703
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800704 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
705 if (irq < 0) {
706 dev_err(&pdev->dev, "ID fall irq not found\n");
707 return irq;
708 }
709 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
710 ab8500_usb_disconnect_irq,
711 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100712 if (err < 0) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800713 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
714 return err;
715 }
716
717 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
718 if (irq < 0) {
719 dev_err(&pdev->dev, "VBUS fall irq not found\n");
720 return irq;
721 }
722 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
723 ab8500_usb_disconnect_irq,
724 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
725 if (err < 0) {
726 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100727 return err;
728 }
729
730 return 0;
731}
732
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500733static int ab8500_usb_probe(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100734{
735 struct ab8500_usb *ab;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800736 struct ab8500 *ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200737 struct usb_otg *otg;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100738 int err;
739 int rev;
740
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800741 ab8500 = dev_get_drvdata(pdev->dev.parent);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100742 rev = abx500_get_chip_id(&pdev->dev);
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800743
744 if (is_ab8500_1p1_or_earlier(ab8500)) {
745 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100746 return -ENODEV;
747 }
748
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200749 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100750 if (!ab)
751 return -ENOMEM;
752
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200753 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
754 if (!otg)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200755 return -ENOMEM;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200756
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100757 ab->dev = &pdev->dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800758 ab->ab8500 = ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200759 ab->phy.dev = ab->dev;
760 ab->phy.otg = otg;
761 ab->phy.label = "ab8500";
762 ab->phy.set_suspend = ab8500_usb_set_suspend;
763 ab->phy.set_power = ab8500_usb_set_power;
764 ab->phy.state = OTG_STATE_UNDEFINED;
765
766 otg->phy = &ab->phy;
767 otg->set_host = ab8500_usb_set_host;
768 otg->set_peripheral = ab8500_usb_set_peripheral;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100769
770 platform_set_drvdata(pdev, ab);
771
Heikki Krogerus144713f2012-02-13 13:24:05 +0200772 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100773
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100774 /* all: Disable phy when called from set_host and set_peripheral */
775 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
776
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200777 err = ab8500_usb_regulator_get(ab);
778 if (err)
779 return err;
780
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800781 err = ab8500_usb_irq_setup(pdev, ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100782 if (err < 0)
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200783 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100784
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530785 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100786 if (err) {
787 dev_err(&pdev->dev, "Can't register transceiver\n");
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200788 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100789 }
790
Sakethram Bommisetti71246312013-04-03 10:45:04 +0200791 /* Phy tuning values for AB8500 */
792 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
793 /* Enable the PBT/Bank 0x12 access */
794 err = abx500_set_register_interruptible(ab->dev,
795 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
796 if (err < 0)
797 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
798 err);
799
800 err = abx500_set_register_interruptible(ab->dev,
801 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
802 if (err < 0)
803 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
804 err);
805
806 err = abx500_set_register_interruptible(ab->dev,
807 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
808 if (err < 0)
809 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
810 err);
811
812 err = abx500_set_register_interruptible(ab->dev,
813 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
814 if (err < 0)
815 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
816 err);
817
818 /* Switch to normal mode/disable Bank 0x12 access */
819 err = abx500_set_register_interruptible(ab->dev,
820 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
821 if (err < 0)
822 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
823 err);
824 }
825
826 /* Phy tuning values for AB8505 */
827 if (is_ab8505(ab->ab8500)) {
828 /* Enable the PBT/Bank 0x12 access */
829 err = abx500_mask_and_set_register_interruptible(ab->dev,
830 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
831 0x01, 0x01);
832 if (err < 0)
833 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
834 err);
835
836 err = abx500_mask_and_set_register_interruptible(ab->dev,
837 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
838 0xC8, 0xC8);
839 if (err < 0)
840 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
841 err);
842
843 err = abx500_mask_and_set_register_interruptible(ab->dev,
844 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
845 0x60, 0x60);
846 if (err < 0)
847 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
848 err);
849
850 err = abx500_mask_and_set_register_interruptible(ab->dev,
851 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
852 0xFC, 0x80);
853
854 if (err < 0)
855 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
856 err);
857
858 /* Switch to normal mode/disable Bank 0x12 access */
859 err = abx500_mask_and_set_register_interruptible(ab->dev,
860 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
861 0x00, 0x00);
862 if (err < 0)
863 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
864 err);
865 }
866
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800867 /* Needed to enable ID detection. */
868 ab8500_usb_wd_workaround(ab);
869
Sakethram Bommisetti8db12232013-04-03 10:45:12 +0200870 abx500_usb_link_status_update(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_work_sync(&ab->phy_dis_work);
882
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530883 usb_remove_phy(&ab->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100884
Mian Yousaf Kaukabf5ef7b42013-04-03 10:45:09 +0200885 if (ab->mode == USB_HOST)
886 ab8500_usb_host_phy_dis(ab);
887 else if (ab->mode == USB_PERIPHERAL)
888 ab8500_usb_peri_phy_dis(ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100889
890 platform_set_drvdata(pdev, NULL);
891
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100892 return 0;
893}
894
895static struct platform_driver ab8500_usb_driver = {
896 .probe = ab8500_usb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500897 .remove = ab8500_usb_remove,
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100898 .driver = {
899 .name = "ab8500-usb",
900 .owner = THIS_MODULE,
901 },
902};
903
904static int __init ab8500_usb_init(void)
905{
906 return platform_driver_register(&ab8500_usb_driver);
907}
908subsys_initcall(ab8500_usb_init);
909
910static void __exit ab8500_usb_exit(void)
911{
912 platform_driver_unregister(&ab8500_usb_driver);
913}
914module_exit(ab8500_usb_exit);
915
916MODULE_ALIAS("platform:ab8500_usb");
917MODULE_AUTHOR("ST-Ericsson AB");
918MODULE_DESCRIPTION("AB8500 usb transceiver driver");
919MODULE_LICENSE("GPL");