linux/drivers/usb/otg/ab8500-usb.c
<<
>>
Prefs
   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 <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>
  33#include <linux/mfd/abx500/ab8500.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 */
  51enum ab8500_usb_link_status {
  52        USB_LINK_NOT_CONFIGURED = 0,
  53        USB_LINK_STD_HOST_NC,
  54        USB_LINK_STD_HOST_C_NS,
  55        USB_LINK_STD_HOST_C_S,
  56        USB_LINK_HOST_CHG_NM,
  57        USB_LINK_HOST_CHG_HS,
  58        USB_LINK_HOST_CHG_HS_CHIRP,
  59        USB_LINK_DEDICATED_CHG,
  60        USB_LINK_ACA_RID_A,
  61        USB_LINK_ACA_RID_B,
  62        USB_LINK_ACA_RID_C_NM,
  63        USB_LINK_ACA_RID_C_HS,
  64        USB_LINK_ACA_RID_C_HS_CHIRP,
  65        USB_LINK_HM_IDGND,
  66        USB_LINK_RESERVED,
  67        USB_LINK_NOT_VALID_LINK
  68};
  69
  70struct ab8500_usb {
  71        struct usb_phy phy;
  72        struct device *dev;
  73        int irq_num_id_rise;
  74        int irq_num_id_fall;
  75        int irq_num_vbus_rise;
  76        int irq_num_vbus_fall;
  77        int irq_num_link_status;
  78        unsigned vbus_draw;
  79        struct delayed_work dwork;
  80        struct work_struct phy_dis_work;
  81        unsigned long link_status_wait;
  82        int rev;
  83};
  84
  85static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
  86{
  87        return container_of(x, struct ab8500_usb, phy);
  88}
  89
  90static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
  91{
  92        abx500_set_register_interruptible(ab->dev,
  93                AB8500_SYS_CTRL2_BLOCK,
  94                AB8500_MAIN_WD_CTRL_REG,
  95                AB8500_BIT_WD_CTRL_ENABLE);
  96
  97        udelay(AB8500_WD_KICK_DELAY_US);
  98
  99        abx500_set_register_interruptible(ab->dev,
 100                AB8500_SYS_CTRL2_BLOCK,
 101                AB8500_MAIN_WD_CTRL_REG,
 102                (AB8500_BIT_WD_CTRL_ENABLE
 103                | AB8500_BIT_WD_CTRL_KICK));
 104
 105        if (ab->rev > 0x10) /* v1.1 v2.0 */
 106                udelay(AB8500_WD_V11_DISABLE_DELAY_US);
 107        else /* v1.0 */
 108                msleep(AB8500_WD_V10_DISABLE_DELAY_MS);
 109
 110        abx500_set_register_interruptible(ab->dev,
 111                AB8500_SYS_CTRL2_BLOCK,
 112                AB8500_MAIN_WD_CTRL_REG,
 113                0);
 114}
 115
 116static void ab8500_usb_phy_ctrl(struct ab8500_usb *ab, bool sel_host,
 117                                        bool enable)
 118{
 119        u8 ctrl_reg;
 120        abx500_get_register_interruptible(ab->dev,
 121                                AB8500_USB,
 122                                AB8500_USB_PHY_CTRL_REG,
 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
 136        abx500_set_register_interruptible(ab->dev,
 137                                AB8500_USB,
 138                                AB8500_USB_PHY_CTRL_REG,
 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
 151static 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
 158        abx500_get_register_interruptible(ab->dev,
 159                        AB8500_USB,
 160                        AB8500_USB_LINE_STAT_REG,
 161                        &reg);
 162
 163        lsts = (reg >> 3) & 0x0F;
 164
 165        switch (lsts) {
 166        case USB_LINK_NOT_CONFIGURED:
 167        case USB_LINK_RESERVED:
 168        case USB_LINK_NOT_VALID_LINK:
 169                /* TODO: Disable regulators. */
 170                ab8500_usb_host_phy_dis(ab);
 171                ab8500_usb_peri_phy_dis(ab);
 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
 178        case USB_LINK_STD_HOST_NC:
 179        case USB_LINK_STD_HOST_C_NS:
 180        case USB_LINK_STD_HOST_C_S:
 181        case USB_LINK_HOST_CHG_NM:
 182        case USB_LINK_HOST_CHG_HS:
 183        case USB_LINK_HOST_CHG_HS_CHIRP:
 184                if (ab->phy.otg->gadget) {
 185                        /* TODO: Enable regulators. */
 186                        ab8500_usb_peri_phy_en(ab);
 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. */
 195                        ab8500_usb_host_phy_en(ab);
 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 */
 206        case USB_LINK_ACA_RID_C_NM:
 207        case USB_LINK_ACA_RID_C_HS:
 208        case USB_LINK_ACA_RID_C_HS_CHIRP:
 209        case USB_LINK_DEDICATED_CHG:
 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
 220static 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
 228static 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. */
 233        schedule_delayed_work(&ab->dwork, ab->link_status_wait);
 234
 235        return IRQ_HANDLED;
 236}
 237
 238static 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. */
 243        ab8500_usb_peri_phy_dis(ab);
 244
 245        /* Wait for link status to become stable. */
 246        schedule_delayed_work(&ab->dwork, ab->link_status_wait);
 247
 248        return IRQ_HANDLED;
 249}
 250
 251static 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
 260static 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)
 266                ab8500_usb_host_phy_dis(ab);
 267
 268        if (!ab->phy.otg->gadget)
 269                ab8500_usb_peri_phy_dis(ab);
 270}
 271
 272static 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
 293static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
 294{
 295        /* TODO */
 296        return 0;
 297}
 298
 299static 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;
 317                schedule_work(&ab->phy_dis_work);
 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                 */
 326                schedule_delayed_work(&ab->dwork, ab->link_status_wait);
 327        }
 328
 329        return 0;
 330}
 331
 332static 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;
 349                schedule_work(&ab->phy_dis_work);
 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                 */
 356                schedule_delayed_work(&ab->dwork, ab->link_status_wait);
 357        }
 358
 359        return 0;
 360}
 361
 362static 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
 374static 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        }
 384        err = request_threaded_irq(ab->irq_num_id_rise, NULL,
 385                ab8500_usb_v1x_common_irq,
 386                IRQF_NO_SUSPEND | IRQF_SHARED,
 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        }
 398        err = request_threaded_irq(ab->irq_num_id_fall, NULL,
 399                ab8500_usb_v1x_common_irq,
 400                IRQF_NO_SUSPEND | IRQF_SHARED,
 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        }
 412        err = request_threaded_irq(ab->irq_num_vbus_rise, NULL,
 413                ab8500_usb_v1x_common_irq,
 414                IRQF_NO_SUSPEND | IRQF_SHARED,
 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        }
 426        err = request_threaded_irq(ab->irq_num_vbus_fall, NULL,
 427                ab8500_usb_v1x_vbus_fall_irq,
 428                IRQF_NO_SUSPEND | IRQF_SHARED,
 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;
 436fail3:
 437        free_irq(ab->irq_num_vbus_rise, ab);
 438fail2:
 439        free_irq(ab->irq_num_id_fall, ab);
 440fail1:
 441        free_irq(ab->irq_num_id_rise, ab);
 442fail0:
 443        return err;
 444}
 445
 446static int ab8500_usb_v2_res_setup(struct platform_device *pdev,
 447                                struct ab8500_usb *ab)
 448{
 449        int err;
 450
 451        ab->irq_num_link_status = platform_get_irq_byname(pdev,
 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
 458        err = request_threaded_irq(ab->irq_num_link_status, NULL,
 459                ab8500_usb_v20_irq,
 460                IRQF_NO_SUSPEND | IRQF_SHARED,
 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
 471static 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);
 524                ab->link_status_wait = AB8500_V1x_LINK_STAT_WAIT;
 525        } else {
 526                err = ab8500_usb_v2_res_setup(pdev, ab);
 527        }
 528
 529        if (err < 0)
 530                goto fail0;
 531
 532        err = usb_set_transceiver(&ab->phy);
 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;
 541fail1:
 542        ab8500_usb_irq_free(ab);
 543fail0:
 544        kfree(otg);
 545        kfree(ab);
 546        return err;
 547}
 548
 549static 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
 555        cancel_delayed_work_sync(&ab->dwork);
 556
 557        cancel_work_sync(&ab->phy_dis_work);
 558
 559        usb_set_transceiver(NULL);
 560
 561        ab8500_usb_host_phy_dis(ab);
 562        ab8500_usb_peri_phy_dis(ab);
 563
 564        platform_set_drvdata(pdev, NULL);
 565
 566        kfree(ab->phy.otg);
 567        kfree(ab);
 568
 569        return 0;
 570}
 571
 572static 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
 581static int __init ab8500_usb_init(void)
 582{
 583        return platform_driver_register(&ab8500_usb_driver);
 584}
 585subsys_initcall(ab8500_usb_init);
 586
 587static void __exit ab8500_usb_exit(void)
 588{
 589        platform_driver_unregister(&ab8500_usb_driver);
 590}
 591module_exit(ab8500_usb_exit);
 592
 593MODULE_ALIAS("platform:ab8500_usb");
 594MODULE_AUTHOR("ST-Ericsson AB");
 595MODULE_DESCRIPTION("AB8500 usb transceiver driver");
 596MODULE_LICENSE("GPL");
 597