linux/drivers/usb/mtu3/mtu3_dr.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * mtu3_dr.c - dual role switch and host glue layer
   4 *
   5 * Copyright (C) 2016 MediaTek Inc.
   6 *
   7 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
   8 */
   9
  10#include <linux/debugfs.h>
  11#include <linux/irq.h>
  12#include <linux/kernel.h>
  13#include <linux/of_device.h>
  14#include <linux/pinctrl/consumer.h>
  15#include <linux/seq_file.h>
  16#include <linux/uaccess.h>
  17
  18#include "mtu3.h"
  19#include "mtu3_dr.h"
  20
  21#define USB2_PORT 2
  22#define USB3_PORT 3
  23
  24enum mtu3_vbus_id_state {
  25        MTU3_ID_FLOAT = 1,
  26        MTU3_ID_GROUND,
  27        MTU3_VBUS_OFF,
  28        MTU3_VBUS_VALID,
  29};
  30
  31static void toggle_opstate(struct ssusb_mtk *ssusb)
  32{
  33        if (!ssusb->otg_switch.is_u3_drd) {
  34                mtu3_setbits(ssusb->mac_base, U3D_DEVICE_CONTROL, DC_SESSION);
  35                mtu3_setbits(ssusb->mac_base, U3D_POWER_MANAGEMENT, SOFT_CONN);
  36        }
  37}
  38
  39/* only port0 supports dual-role mode */
  40static int ssusb_port0_switch(struct ssusb_mtk *ssusb,
  41        int version, bool tohost)
  42{
  43        void __iomem *ibase = ssusb->ippc_base;
  44        u32 value;
  45
  46        dev_dbg(ssusb->dev, "%s (switch u%d port0 to %s)\n", __func__,
  47                version, tohost ? "host" : "device");
  48
  49        if (version == USB2_PORT) {
  50                /* 1. power off and disable u2 port0 */
  51                value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
  52                value |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS;
  53                mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
  54
  55                /* 2. power on, enable u2 port0 and select its mode */
  56                value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
  57                value &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
  58                value = tohost ? (value | SSUSB_U2_PORT_HOST_SEL) :
  59                        (value & (~SSUSB_U2_PORT_HOST_SEL));
  60                mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
  61        } else {
  62                /* 1. power off and disable u3 port0 */
  63                value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
  64                value |= SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS;
  65                mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
  66
  67                /* 2. power on, enable u3 port0 and select its mode */
  68                value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
  69                value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
  70                value = tohost ? (value | SSUSB_U3_PORT_HOST_SEL) :
  71                        (value & (~SSUSB_U3_PORT_HOST_SEL));
  72                mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
  73        }
  74
  75        return 0;
  76}
  77
  78static void switch_port_to_host(struct ssusb_mtk *ssusb)
  79{
  80        u32 check_clk = 0;
  81
  82        dev_dbg(ssusb->dev, "%s\n", __func__);
  83
  84        ssusb_port0_switch(ssusb, USB2_PORT, true);
  85
  86        if (ssusb->otg_switch.is_u3_drd) {
  87                ssusb_port0_switch(ssusb, USB3_PORT, true);
  88                check_clk = SSUSB_U3_MAC_RST_B_STS;
  89        }
  90
  91        ssusb_check_clocks(ssusb, check_clk);
  92
  93        /* after all clocks are stable */
  94        toggle_opstate(ssusb);
  95}
  96
  97static void switch_port_to_device(struct ssusb_mtk *ssusb)
  98{
  99        u32 check_clk = 0;
 100
 101        dev_dbg(ssusb->dev, "%s\n", __func__);
 102
 103        ssusb_port0_switch(ssusb, USB2_PORT, false);
 104
 105        if (ssusb->otg_switch.is_u3_drd) {
 106                ssusb_port0_switch(ssusb, USB3_PORT, false);
 107                check_clk = SSUSB_U3_MAC_RST_B_STS;
 108        }
 109
 110        ssusb_check_clocks(ssusb, check_clk);
 111}
 112
 113int ssusb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on)
 114{
 115        struct ssusb_mtk *ssusb =
 116                container_of(otg_sx, struct ssusb_mtk, otg_switch);
 117        struct regulator *vbus = otg_sx->vbus;
 118        int ret;
 119
 120        /* vbus is optional */
 121        if (!vbus)
 122                return 0;
 123
 124        dev_dbg(ssusb->dev, "%s: turn %s\n", __func__, is_on ? "on" : "off");
 125
 126        if (is_on) {
 127                ret = regulator_enable(vbus);
 128                if (ret) {
 129                        dev_err(ssusb->dev, "vbus regulator enable failed\n");
 130                        return ret;
 131                }
 132        } else {
 133                regulator_disable(vbus);
 134        }
 135
 136        return 0;
 137}
 138
 139/*
 140 * switch to host: -> MTU3_VBUS_OFF --> MTU3_ID_GROUND
 141 * switch to device: -> MTU3_ID_FLOAT --> MTU3_VBUS_VALID
 142 */
 143static void ssusb_set_mailbox(struct otg_switch_mtk *otg_sx,
 144        enum mtu3_vbus_id_state status)
 145{
 146        struct ssusb_mtk *ssusb =
 147                container_of(otg_sx, struct ssusb_mtk, otg_switch);
 148        struct mtu3 *mtu = ssusb->u3d;
 149
 150        dev_dbg(ssusb->dev, "mailbox state(%d)\n", status);
 151
 152        switch (status) {
 153        case MTU3_ID_GROUND:
 154                switch_port_to_host(ssusb);
 155                ssusb_set_vbus(otg_sx, 1);
 156                ssusb->is_host = true;
 157                break;
 158        case MTU3_ID_FLOAT:
 159                ssusb->is_host = false;
 160                ssusb_set_vbus(otg_sx, 0);
 161                switch_port_to_device(ssusb);
 162                break;
 163        case MTU3_VBUS_OFF:
 164                mtu3_stop(mtu);
 165                pm_relax(ssusb->dev);
 166                break;
 167        case MTU3_VBUS_VALID:
 168                /* avoid suspend when works as device */
 169                pm_stay_awake(ssusb->dev);
 170                mtu3_start(mtu);
 171                break;
 172        default:
 173                dev_err(ssusb->dev, "invalid state\n");
 174        }
 175}
 176
 177static void ssusb_id_work(struct work_struct *work)
 178{
 179        struct otg_switch_mtk *otg_sx =
 180                container_of(work, struct otg_switch_mtk, id_work);
 181
 182        if (otg_sx->id_event)
 183                ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
 184        else
 185                ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
 186}
 187
 188static void ssusb_vbus_work(struct work_struct *work)
 189{
 190        struct otg_switch_mtk *otg_sx =
 191                container_of(work, struct otg_switch_mtk, vbus_work);
 192
 193        if (otg_sx->vbus_event)
 194                ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
 195        else
 196                ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
 197}
 198
 199/*
 200 * @ssusb_id_notifier is called in atomic context, but @ssusb_set_mailbox
 201 * may sleep, so use work queue here
 202 */
 203static int ssusb_id_notifier(struct notifier_block *nb,
 204        unsigned long event, void *ptr)
 205{
 206        struct otg_switch_mtk *otg_sx =
 207                container_of(nb, struct otg_switch_mtk, id_nb);
 208
 209        otg_sx->id_event = event;
 210        schedule_work(&otg_sx->id_work);
 211
 212        return NOTIFY_DONE;
 213}
 214
 215static int ssusb_vbus_notifier(struct notifier_block *nb,
 216        unsigned long event, void *ptr)
 217{
 218        struct otg_switch_mtk *otg_sx =
 219                container_of(nb, struct otg_switch_mtk, vbus_nb);
 220
 221        otg_sx->vbus_event = event;
 222        schedule_work(&otg_sx->vbus_work);
 223
 224        return NOTIFY_DONE;
 225}
 226
 227static int ssusb_extcon_register(struct otg_switch_mtk *otg_sx)
 228{
 229        struct ssusb_mtk *ssusb =
 230                container_of(otg_sx, struct ssusb_mtk, otg_switch);
 231        struct extcon_dev *edev = otg_sx->edev;
 232        int ret;
 233
 234        /* extcon is optional */
 235        if (!edev)
 236                return 0;
 237
 238        otg_sx->vbus_nb.notifier_call = ssusb_vbus_notifier;
 239        ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB,
 240                                        &otg_sx->vbus_nb);
 241        if (ret < 0)
 242                dev_err(ssusb->dev, "failed to register notifier for USB\n");
 243
 244        otg_sx->id_nb.notifier_call = ssusb_id_notifier;
 245        ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB_HOST,
 246                                        &otg_sx->id_nb);
 247        if (ret < 0)
 248                dev_err(ssusb->dev, "failed to register notifier for USB-HOST\n");
 249
 250        dev_dbg(ssusb->dev, "EXTCON_USB: %d, EXTCON_USB_HOST: %d\n",
 251                extcon_get_state(edev, EXTCON_USB),
 252                extcon_get_state(edev, EXTCON_USB_HOST));
 253
 254        /* default as host, switch to device mode if needed */
 255        if (extcon_get_state(edev, EXTCON_USB_HOST) == false)
 256                ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
 257        if (extcon_get_state(edev, EXTCON_USB) == true)
 258                ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
 259
 260        return 0;
 261}
 262
 263/*
 264 * We provide an interface via debugfs to switch between host and device modes
 265 * depending on user input.
 266 * This is useful in special cases, such as uses TYPE-A receptacle but also
 267 * wants to support dual-role mode.
 268 */
 269static void ssusb_mode_manual_switch(struct ssusb_mtk *ssusb, int to_host)
 270{
 271        struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
 272
 273        if (to_host) {
 274                ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST);
 275                ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
 276                ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
 277        } else {
 278                ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_DEVICE);
 279                ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
 280                ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
 281        }
 282}
 283
 284static int ssusb_mode_show(struct seq_file *sf, void *unused)
 285{
 286        struct ssusb_mtk *ssusb = sf->private;
 287
 288        seq_printf(sf, "current mode: %s(%s drd)\n(echo device/host)\n",
 289                ssusb->is_host ? "host" : "device",
 290                ssusb->otg_switch.manual_drd_enabled ? "manual" : "auto");
 291
 292        return 0;
 293}
 294
 295static int ssusb_mode_open(struct inode *inode, struct file *file)
 296{
 297        return single_open(file, ssusb_mode_show, inode->i_private);
 298}
 299
 300static ssize_t ssusb_mode_write(struct file *file,
 301        const char __user *ubuf, size_t count, loff_t *ppos)
 302{
 303        struct seq_file *sf = file->private_data;
 304        struct ssusb_mtk *ssusb = sf->private;
 305        char buf[16];
 306
 307        if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
 308                return -EFAULT;
 309
 310        if (!strncmp(buf, "host", 4) && !ssusb->is_host) {
 311                ssusb_mode_manual_switch(ssusb, 1);
 312        } else if (!strncmp(buf, "device", 6) && ssusb->is_host) {
 313                ssusb_mode_manual_switch(ssusb, 0);
 314        } else {
 315                dev_err(ssusb->dev, "wrong or duplicated setting\n");
 316                return -EINVAL;
 317        }
 318
 319        return count;
 320}
 321
 322static const struct file_operations ssusb_mode_fops = {
 323        .open = ssusb_mode_open,
 324        .write = ssusb_mode_write,
 325        .read = seq_read,
 326        .llseek = seq_lseek,
 327        .release = single_release,
 328};
 329
 330static int ssusb_vbus_show(struct seq_file *sf, void *unused)
 331{
 332        struct ssusb_mtk *ssusb = sf->private;
 333        struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
 334
 335        seq_printf(sf, "vbus state: %s\n(echo on/off)\n",
 336                regulator_is_enabled(otg_sx->vbus) ? "on" : "off");
 337
 338        return 0;
 339}
 340
 341static int ssusb_vbus_open(struct inode *inode, struct file *file)
 342{
 343        return single_open(file, ssusb_vbus_show, inode->i_private);
 344}
 345
 346static ssize_t ssusb_vbus_write(struct file *file,
 347        const char __user *ubuf, size_t count, loff_t *ppos)
 348{
 349        struct seq_file *sf = file->private_data;
 350        struct ssusb_mtk *ssusb = sf->private;
 351        struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
 352        char buf[16];
 353        bool enable;
 354
 355        if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
 356                return -EFAULT;
 357
 358        if (kstrtobool(buf, &enable)) {
 359                dev_err(ssusb->dev, "wrong setting\n");
 360                return -EINVAL;
 361        }
 362
 363        ssusb_set_vbus(otg_sx, enable);
 364
 365        return count;
 366}
 367
 368static const struct file_operations ssusb_vbus_fops = {
 369        .open = ssusb_vbus_open,
 370        .write = ssusb_vbus_write,
 371        .read = seq_read,
 372        .llseek = seq_lseek,
 373        .release = single_release,
 374};
 375
 376static void ssusb_debugfs_init(struct ssusb_mtk *ssusb)
 377{
 378        struct dentry *root;
 379
 380        root = debugfs_create_dir(dev_name(ssusb->dev), usb_debug_root);
 381        ssusb->dbgfs_root = root;
 382
 383        debugfs_create_file("mode", 0644, root, ssusb, &ssusb_mode_fops);
 384        debugfs_create_file("vbus", 0644, root, ssusb, &ssusb_vbus_fops);
 385}
 386
 387static void ssusb_debugfs_exit(struct ssusb_mtk *ssusb)
 388{
 389        debugfs_remove_recursive(ssusb->dbgfs_root);
 390}
 391
 392void ssusb_set_force_mode(struct ssusb_mtk *ssusb,
 393                          enum mtu3_dr_force_mode mode)
 394{
 395        u32 value;
 396
 397        value = mtu3_readl(ssusb->ippc_base, SSUSB_U2_CTRL(0));
 398        switch (mode) {
 399        case MTU3_DR_FORCE_DEVICE:
 400                value |= SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG;
 401                break;
 402        case MTU3_DR_FORCE_HOST:
 403                value |= SSUSB_U2_PORT_FORCE_IDDIG;
 404                value &= ~SSUSB_U2_PORT_RG_IDDIG;
 405                break;
 406        case MTU3_DR_FORCE_NONE:
 407                value &= ~(SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG);
 408                break;
 409        default:
 410                return;
 411        }
 412        mtu3_writel(ssusb->ippc_base, SSUSB_U2_CTRL(0), value);
 413}
 414
 415int ssusb_otg_switch_init(struct ssusb_mtk *ssusb)
 416{
 417        struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
 418
 419        INIT_WORK(&otg_sx->id_work, ssusb_id_work);
 420        INIT_WORK(&otg_sx->vbus_work, ssusb_vbus_work);
 421
 422        if (otg_sx->manual_drd_enabled)
 423                ssusb_debugfs_init(ssusb);
 424        else
 425                ssusb_extcon_register(otg_sx);
 426
 427        return 0;
 428}
 429
 430void ssusb_otg_switch_exit(struct ssusb_mtk *ssusb)
 431{
 432        struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
 433
 434        if (otg_sx->manual_drd_enabled)
 435                ssusb_debugfs_exit(ssusb);
 436
 437        cancel_work_sync(&otg_sx->id_work);
 438        cancel_work_sync(&otg_sx->vbus_work);
 439}
 440