Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ab8500-usb.c
Go to the documentation of this file.
1 /*
2  * drivers/usb/otg/ab8500_usb.c
3  *
4  * USB transceiver driver for AB8500 chip
5  *
6  * Copyright (C) 2010 ST-Ericsson AB
7  * Mian Yousaf Kaukab <[email protected]>
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>
34 
35 #define AB8500_MAIN_WD_CTRL_REG 0x01
36 #define AB8500_USB_LINE_STAT_REG 0x80
37 #define AB8500_USB_PHY_CTRL_REG 0x8A
38 
39 #define AB8500_BIT_OTG_STAT_ID (1 << 0)
40 #define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
41 #define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
42 #define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
43 #define AB8500_BIT_WD_CTRL_KICK (1 << 1)
44 
45 #define AB8500_V1x_LINK_STAT_WAIT (HZ/10)
46 #define AB8500_WD_KICK_DELAY_US 100 /* usec */
47 #define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
48 #define AB8500_WD_V10_DISABLE_DELAY_MS 100 /* ms */
49 
50 /* Usb line status register */
68 };
69 
70 struct ab8500_usb {
71  struct usb_phy phy;
72  struct device *dev;
78  unsigned vbus_draw;
81  unsigned long link_status_wait;
82  int rev;
83 };
84 
85 static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
86 {
87  return container_of(x, struct ab8500_usb, phy);
88 }
89 
90 static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
91 {
96 
98 
104 
105  if (ab->rev > 0x10) /* v1.1 v2.0 */
107  else /* v1.0 */
109 
113  0);
114 }
115 
116 static void ab8500_usb_phy_ctrl(struct ab8500_usb *ab, bool sel_host,
117  bool enable)
118 {
119  u8 ctrl_reg;
121  AB8500_USB,
123  &ctrl_reg);
124  if (sel_host) {
125  if (enable)
126  ctrl_reg |= AB8500_BIT_PHY_CTRL_HOST_EN;
127  else
128  ctrl_reg &= ~AB8500_BIT_PHY_CTRL_HOST_EN;
129  } else {
130  if (enable)
131  ctrl_reg |= AB8500_BIT_PHY_CTRL_DEVICE_EN;
132  else
133  ctrl_reg &= ~AB8500_BIT_PHY_CTRL_DEVICE_EN;
134  }
135 
137  AB8500_USB,
139  ctrl_reg);
140 
141  /* Needed to enable the phy.*/
142  if (enable)
143  ab8500_usb_wd_workaround(ab);
144 }
145 
146 #define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_ctrl(ab, true, true)
147 #define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_ctrl(ab, true, false)
148 #define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_ctrl(ab, false, true)
149 #define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_ctrl(ab, false, false)
150 
151 static int ab8500_usb_link_status_update(struct ab8500_usb *ab)
152 {
153  u8 reg;
154  enum ab8500_usb_link_status lsts;
155  void *v = NULL;
156  enum usb_phy_events event;
157 
159  AB8500_USB,
161  &reg);
162 
163  lsts = (reg >> 3) & 0x0F;
164 
165  switch (lsts) {
167  case USB_LINK_RESERVED:
169  /* TODO: Disable regulators. */
172  ab->phy.state = OTG_STATE_B_IDLE;
173  ab->phy.otg->default_a = false;
174  ab->vbus_draw = 0;
175  event = USB_EVENT_NONE;
176  break;
177 
184  if (ab->phy.otg->gadget) {
185  /* TODO: Enable regulators. */
187  v = ab->phy.otg->gadget;
188  }
189  event = USB_EVENT_VBUS;
190  break;
191 
192  case USB_LINK_HM_IDGND:
193  if (ab->phy.otg->host) {
194  /* TODO: Enable regulators. */
196  v = ab->phy.otg->host;
197  }
198  ab->phy.state = OTG_STATE_A_IDLE;
199  ab->phy.otg->default_a = true;
200  event = USB_EVENT_ID;
201  break;
202 
203  case USB_LINK_ACA_RID_A:
204  case USB_LINK_ACA_RID_B:
205  /* TODO */
210  /* TODO: vbus_draw */
211  event = USB_EVENT_CHARGER;
212  break;
213  }
214 
215  atomic_notifier_call_chain(&ab->phy.notifier, event, v);
216 
217  return 0;
218 }
219 
220 static void ab8500_usb_delayed_work(struct work_struct *work)
221 {
222  struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
223  dwork.work);
224 
225  ab8500_usb_link_status_update(ab);
226 }
227 
228 static irqreturn_t ab8500_usb_v1x_common_irq(int irq, void *data)
229 {
230  struct ab8500_usb *ab = (struct ab8500_usb *) data;
231 
232  /* Wait for link status to become stable. */
234 
235  return IRQ_HANDLED;
236 }
237 
238 static irqreturn_t ab8500_usb_v1x_vbus_fall_irq(int irq, void *data)
239 {
240  struct ab8500_usb *ab = (struct ab8500_usb *) data;
241 
242  /* Link status will not be updated till phy is disabled. */
244 
245  /* Wait for link status to become stable. */
247 
248  return IRQ_HANDLED;
249 }
250 
251 static irqreturn_t ab8500_usb_v20_irq(int irq, void *data)
252 {
253  struct ab8500_usb *ab = (struct ab8500_usb *) data;
254 
255  ab8500_usb_link_status_update(ab);
256 
257  return IRQ_HANDLED;
258 }
259 
260 static void ab8500_usb_phy_disable_work(struct work_struct *work)
261 {
262  struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
263  phy_dis_work);
264 
265  if (!ab->phy.otg->host)
267 
268  if (!ab->phy.otg->gadget)
270 }
271 
272 static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
273 {
274  struct ab8500_usb *ab;
275 
276  if (!phy)
277  return -ENODEV;
278 
279  ab = phy_to_ab(phy);
280 
281  ab->vbus_draw = mA;
282 
283  if (mA)
284  atomic_notifier_call_chain(&ab->phy.notifier,
285  USB_EVENT_ENUMERATED, ab->phy.otg->gadget);
286  return 0;
287 }
288 
289 /* TODO: Implement some way for charging or other drivers to read
290  * ab->vbus_draw.
291  */
292 
293 static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
294 {
295  /* TODO */
296  return 0;
297 }
298 
299 static int ab8500_usb_set_peripheral(struct usb_otg *otg,
300  struct usb_gadget *gadget)
301 {
302  struct ab8500_usb *ab;
303 
304  if (!otg)
305  return -ENODEV;
306 
307  ab = phy_to_ab(otg->phy);
308 
309  /* Some drivers call this function in atomic context.
310  * Do not update ab8500 registers directly till this
311  * is fixed.
312  */
313 
314  if (!gadget) {
315  /* TODO: Disable regulators. */
316  otg->gadget = NULL;
318  } else {
319  otg->gadget = gadget;
320  otg->phy->state = OTG_STATE_B_IDLE;
321 
322  /* Phy will not be enabled if cable is already
323  * plugged-in. Schedule to enable phy.
324  * Use same delay to avoid any race condition.
325  */
327  }
328 
329  return 0;
330 }
331 
332 static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
333 {
334  struct ab8500_usb *ab;
335 
336  if (!otg)
337  return -ENODEV;
338 
339  ab = phy_to_ab(otg->phy);
340 
341  /* Some drivers call this function in atomic context.
342  * Do not update ab8500 registers directly till this
343  * is fixed.
344  */
345 
346  if (!host) {
347  /* TODO: Disable regulators. */
348  otg->host = NULL;
350  } else {
351  otg->host = host;
352  /* Phy will not be enabled if cable is already
353  * plugged-in. Schedule to enable phy.
354  * Use same delay to avoid any race condition.
355  */
357  }
358 
359  return 0;
360 }
361 
362 static void ab8500_usb_irq_free(struct ab8500_usb *ab)
363 {
364  if (ab->rev < 0x20) {
365  free_irq(ab->irq_num_id_rise, ab);
366  free_irq(ab->irq_num_id_fall, ab);
367  free_irq(ab->irq_num_vbus_rise, ab);
368  free_irq(ab->irq_num_vbus_fall, ab);
369  } else {
370  free_irq(ab->irq_num_link_status, ab);
371  }
372 }
373 
374 static int ab8500_usb_v1x_res_setup(struct platform_device *pdev,
375  struct ab8500_usb *ab)
376 {
377  int err;
378 
379  ab->irq_num_id_rise = platform_get_irq_byname(pdev, "ID_WAKEUP_R");
380  if (ab->irq_num_id_rise < 0) {
381  dev_err(&pdev->dev, "ID rise irq not found\n");
382  return ab->irq_num_id_rise;
383  }
385  ab8500_usb_v1x_common_irq,
387  "usb-id-rise", ab);
388  if (err < 0) {
389  dev_err(ab->dev, "request_irq failed for ID rise irq\n");
390  goto fail0;
391  }
392 
393  ab->irq_num_id_fall = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
394  if (ab->irq_num_id_fall < 0) {
395  dev_err(&pdev->dev, "ID fall irq not found\n");
396  return ab->irq_num_id_fall;
397  }
399  ab8500_usb_v1x_common_irq,
401  "usb-id-fall", ab);
402  if (err < 0) {
403  dev_err(ab->dev, "request_irq failed for ID fall irq\n");
404  goto fail1;
405  }
406 
407  ab->irq_num_vbus_rise = platform_get_irq_byname(pdev, "VBUS_DET_R");
408  if (ab->irq_num_vbus_rise < 0) {
409  dev_err(&pdev->dev, "VBUS rise irq not found\n");
410  return ab->irq_num_vbus_rise;
411  }
413  ab8500_usb_v1x_common_irq,
415  "usb-vbus-rise", ab);
416  if (err < 0) {
417  dev_err(ab->dev, "request_irq failed for Vbus rise irq\n");
418  goto fail2;
419  }
420 
421  ab->irq_num_vbus_fall = platform_get_irq_byname(pdev, "VBUS_DET_F");
422  if (ab->irq_num_vbus_fall < 0) {
423  dev_err(&pdev->dev, "VBUS fall irq not found\n");
424  return ab->irq_num_vbus_fall;
425  }
427  ab8500_usb_v1x_vbus_fall_irq,
429  "usb-vbus-fall", ab);
430  if (err < 0) {
431  dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
432  goto fail3;
433  }
434 
435  return 0;
436 fail3:
437  free_irq(ab->irq_num_vbus_rise, ab);
438 fail2:
439  free_irq(ab->irq_num_id_fall, ab);
440 fail1:
441  free_irq(ab->irq_num_id_rise, ab);
442 fail0:
443  return err;
444 }
445 
446 static int ab8500_usb_v2_res_setup(struct platform_device *pdev,
447  struct ab8500_usb *ab)
448 {
449  int err;
450 
452  "USB_LINK_STATUS");
453  if (ab->irq_num_link_status < 0) {
454  dev_err(&pdev->dev, "Link status irq not found\n");
455  return ab->irq_num_link_status;
456  }
457 
459  ab8500_usb_v20_irq,
461  "usb-link-status", ab);
462  if (err < 0) {
463  dev_err(ab->dev,
464  "request_irq failed for link status irq\n");
465  return err;
466  }
467 
468  return 0;
469 }
470 
471 static int __devinit ab8500_usb_probe(struct platform_device *pdev)
472 {
473  struct ab8500_usb *ab;
474  struct usb_otg *otg;
475  int err;
476  int rev;
477 
478  rev = abx500_get_chip_id(&pdev->dev);
479  if (rev < 0) {
480  dev_err(&pdev->dev, "Chip id read failed\n");
481  return rev;
482  } else if (rev < 0x10) {
483  dev_err(&pdev->dev, "Unsupported AB8500 chip\n");
484  return -ENODEV;
485  }
486 
487  ab = kzalloc(sizeof *ab, GFP_KERNEL);
488  if (!ab)
489  return -ENOMEM;
490 
491  otg = kzalloc(sizeof *otg, GFP_KERNEL);
492  if (!otg) {
493  kfree(ab);
494  return -ENOMEM;
495  }
496 
497  ab->dev = &pdev->dev;
498  ab->rev = rev;
499  ab->phy.dev = ab->dev;
500  ab->phy.otg = otg;
501  ab->phy.label = "ab8500";
502  ab->phy.set_suspend = ab8500_usb_set_suspend;
503  ab->phy.set_power = ab8500_usb_set_power;
504  ab->phy.state = OTG_STATE_UNDEFINED;
505 
506  otg->phy = &ab->phy;
507  otg->set_host = ab8500_usb_set_host;
508  otg->set_peripheral = ab8500_usb_set_peripheral;
509 
510  platform_set_drvdata(pdev, ab);
511 
512  ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
513 
514  /* v1: Wait for link status to become stable.
515  * all: Updates form set_host and set_peripheral as they are atomic.
516  */
517  INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
518 
519  /* all: Disable phy when called from set_host and set_peripheral */
520  INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
521 
522  if (ab->rev < 0x20) {
523  err = ab8500_usb_v1x_res_setup(pdev, ab);
525  } else {
526  err = ab8500_usb_v2_res_setup(pdev, ab);
527  }
528 
529  if (err < 0)
530  goto fail0;
531 
532  err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
533  if (err) {
534  dev_err(&pdev->dev, "Can't register transceiver\n");
535  goto fail1;
536  }
537 
538  dev_info(&pdev->dev, "AB8500 usb driver initialized\n");
539 
540  return 0;
541 fail1:
542  ab8500_usb_irq_free(ab);
543 fail0:
544  kfree(otg);
545  kfree(ab);
546  return err;
547 }
548 
549 static int __devexit ab8500_usb_remove(struct platform_device *pdev)
550 {
551  struct ab8500_usb *ab = platform_get_drvdata(pdev);
552 
553  ab8500_usb_irq_free(ab);
554 
556 
558 
559  usb_remove_phy(&ab->phy);
560 
563 
564  platform_set_drvdata(pdev, NULL);
565 
566  kfree(ab->phy.otg);
567  kfree(ab);
568 
569  return 0;
570 }
571 
572 static struct platform_driver ab8500_usb_driver = {
573  .probe = ab8500_usb_probe,
574  .remove = __devexit_p(ab8500_usb_remove),
575  .driver = {
576  .name = "ab8500-usb",
577  .owner = THIS_MODULE,
578  },
579 };
580 
581 static int __init ab8500_usb_init(void)
582 {
583  return platform_driver_register(&ab8500_usb_driver);
584 }
585 subsys_initcall(ab8500_usb_init);
586 
587 static void __exit ab8500_usb_exit(void)
588 {
589  platform_driver_unregister(&ab8500_usb_driver);
590 }
591 module_exit(ab8500_usb_exit);
592 
593 MODULE_ALIAS("platform:ab8500_usb");
594 MODULE_AUTHOR("ST-Ericsson AB");
595 MODULE_DESCRIPTION("AB8500 usb transceiver driver");
596 MODULE_LICENSE("GPL");