linux/drivers/usb/typec/tcpm/tcpci_rt1711h.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Copyright (C) 2018, Richtek Technology Corporation
   4 *
   5 * Richtek RT1711H Type-C Chip Driver
   6 */
   7
   8#include <linux/kernel.h>
   9#include <linux/module.h>
  10#include <linux/i2c.h>
  11#include <linux/interrupt.h>
  12#include <linux/gpio/consumer.h>
  13#include <linux/usb/tcpm.h>
  14#include <linux/regmap.h>
  15#include "tcpci.h"
  16
  17#define RT1711H_VID             0x29CF
  18#define RT1711H_PID             0x1711
  19
  20#define RT1711H_RTCTRL8         0x9B
  21
  22/* Autoidle timeout = (tout * 2 + 1) * 6.4ms */
  23#define RT1711H_RTCTRL8_SET(ck300, ship_off, auto_idle, tout) \
  24                            (((ck300) << 7) | ((ship_off) << 5) | \
  25                            ((auto_idle) << 3) | ((tout) & 0x07))
  26
  27#define RT1711H_RTCTRL11        0x9E
  28
  29/* I2C timeout = (tout + 1) * 12.5ms */
  30#define RT1711H_RTCTRL11_SET(en, tout) \
  31                             (((en) << 7) | ((tout) & 0x0F))
  32
  33#define RT1711H_RTCTRL13        0xA0
  34#define RT1711H_RTCTRL14        0xA1
  35#define RT1711H_RTCTRL15        0xA2
  36#define RT1711H_RTCTRL16        0xA3
  37
  38struct rt1711h_chip {
  39        struct tcpci_data data;
  40        struct tcpci *tcpci;
  41        struct device *dev;
  42};
  43
  44static int rt1711h_read16(struct rt1711h_chip *chip, unsigned int reg, u16 *val)
  45{
  46        return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u16));
  47}
  48
  49static int rt1711h_write16(struct rt1711h_chip *chip, unsigned int reg, u16 val)
  50{
  51        return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u16));
  52}
  53
  54static int rt1711h_read8(struct rt1711h_chip *chip, unsigned int reg, u8 *val)
  55{
  56        return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u8));
  57}
  58
  59static int rt1711h_write8(struct rt1711h_chip *chip, unsigned int reg, u8 val)
  60{
  61        return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u8));
  62}
  63
  64static const struct regmap_config rt1711h_regmap_config = {
  65        .reg_bits = 8,
  66        .val_bits = 8,
  67
  68        .max_register = 0xFF, /* 0x80 .. 0xFF are vendor defined */
  69};
  70
  71static struct rt1711h_chip *tdata_to_rt1711h(struct tcpci_data *tdata)
  72{
  73        return container_of(tdata, struct rt1711h_chip, data);
  74}
  75
  76static int rt1711h_init(struct tcpci *tcpci, struct tcpci_data *tdata)
  77{
  78        int ret;
  79        struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
  80
  81        /* CK 300K from 320K, shipping off, auto_idle enable, tout = 32ms */
  82        ret = rt1711h_write8(chip, RT1711H_RTCTRL8,
  83                             RT1711H_RTCTRL8_SET(0, 1, 1, 2));
  84        if (ret < 0)
  85                return ret;
  86
  87        /* I2C reset : (val + 1) * 12.5ms */
  88        ret = rt1711h_write8(chip, RT1711H_RTCTRL11,
  89                             RT1711H_RTCTRL11_SET(1, 0x0F));
  90        if (ret < 0)
  91                return ret;
  92
  93        /* tTCPCfilter : (26.7 * val) us */
  94        ret = rt1711h_write8(chip, RT1711H_RTCTRL14, 0x0F);
  95        if (ret < 0)
  96                return ret;
  97
  98        /*  tDRP : (51.2 + 6.4 * val) ms */
  99        ret = rt1711h_write8(chip, RT1711H_RTCTRL15, 0x04);
 100        if (ret < 0)
 101                return ret;
 102
 103        /* dcSRC.DRP : 33% */
 104        return rt1711h_write16(chip, RT1711H_RTCTRL16, 330);
 105}
 106
 107static int rt1711h_set_vconn(struct tcpci *tcpci, struct tcpci_data *tdata,
 108                             bool enable)
 109{
 110        struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
 111
 112        return rt1711h_write8(chip, RT1711H_RTCTRL8,
 113                              RT1711H_RTCTRL8_SET(0, 1, !enable, 2));
 114}
 115
 116static int rt1711h_start_drp_toggling(struct tcpci *tcpci,
 117                                      struct tcpci_data *tdata,
 118                                      enum typec_cc_status cc)
 119{
 120        struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
 121        int ret;
 122        unsigned int reg = 0;
 123
 124        switch (cc) {
 125        default:
 126        case TYPEC_CC_RP_DEF:
 127                reg |= (TCPC_ROLE_CTRL_RP_VAL_DEF <<
 128                        TCPC_ROLE_CTRL_RP_VAL_SHIFT);
 129                break;
 130        case TYPEC_CC_RP_1_5:
 131                reg |= (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
 132                        TCPC_ROLE_CTRL_RP_VAL_SHIFT);
 133                break;
 134        case TYPEC_CC_RP_3_0:
 135                reg |= (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
 136                        TCPC_ROLE_CTRL_RP_VAL_SHIFT);
 137                break;
 138        }
 139
 140        if (cc == TYPEC_CC_RD)
 141                reg |= (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
 142                           (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
 143        else
 144                reg |= (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
 145                           (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT);
 146
 147        ret = rt1711h_write8(chip, TCPC_ROLE_CTRL, reg);
 148        if (ret < 0)
 149                return ret;
 150        usleep_range(500, 1000);
 151
 152        return 0;
 153}
 154
 155static irqreturn_t rt1711h_irq(int irq, void *dev_id)
 156{
 157        int ret;
 158        u16 alert;
 159        u8 status;
 160        struct rt1711h_chip *chip = dev_id;
 161
 162        if (!chip->tcpci)
 163                return IRQ_HANDLED;
 164
 165        ret = rt1711h_read16(chip, TCPC_ALERT, &alert);
 166        if (ret < 0)
 167                goto out;
 168
 169        if (alert & TCPC_ALERT_CC_STATUS) {
 170                ret = rt1711h_read8(chip, TCPC_CC_STATUS, &status);
 171                if (ret < 0)
 172                        goto out;
 173                /* Clear cc change event triggered by starting toggling */
 174                if (status & TCPC_CC_STATUS_TOGGLING)
 175                        rt1711h_write8(chip, TCPC_ALERT, TCPC_ALERT_CC_STATUS);
 176        }
 177
 178out:
 179        return tcpci_irq(chip->tcpci);
 180}
 181
 182static int rt1711h_sw_reset(struct rt1711h_chip *chip)
 183{
 184        int ret;
 185
 186        ret = rt1711h_write8(chip, RT1711H_RTCTRL13, 0x01);
 187        if (ret < 0)
 188                return ret;
 189
 190        usleep_range(1000, 2000);
 191        return 0;
 192}
 193
 194static int rt1711h_check_revision(struct i2c_client *i2c)
 195{
 196        int ret;
 197
 198        ret = i2c_smbus_read_word_data(i2c, TCPC_VENDOR_ID);
 199        if (ret < 0)
 200                return ret;
 201        if (ret != RT1711H_VID) {
 202                dev_err(&i2c->dev, "vid is not correct, 0x%04x\n", ret);
 203                return -ENODEV;
 204        }
 205        ret = i2c_smbus_read_word_data(i2c, TCPC_PRODUCT_ID);
 206        if (ret < 0)
 207                return ret;
 208        if (ret != RT1711H_PID) {
 209                dev_err(&i2c->dev, "pid is not correct, 0x%04x\n", ret);
 210                return -ENODEV;
 211        }
 212        return 0;
 213}
 214
 215static int rt1711h_probe(struct i2c_client *client,
 216                         const struct i2c_device_id *i2c_id)
 217{
 218        int ret;
 219        struct rt1711h_chip *chip;
 220
 221        ret = rt1711h_check_revision(client);
 222        if (ret < 0) {
 223                dev_err(&client->dev, "check vid/pid fail\n");
 224                return ret;
 225        }
 226
 227        chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
 228        if (!chip)
 229                return -ENOMEM;
 230
 231        chip->data.regmap = devm_regmap_init_i2c(client,
 232                                                 &rt1711h_regmap_config);
 233        if (IS_ERR(chip->data.regmap))
 234                return PTR_ERR(chip->data.regmap);
 235
 236        chip->dev = &client->dev;
 237        i2c_set_clientdata(client, chip);
 238
 239        ret = rt1711h_sw_reset(chip);
 240        if (ret < 0)
 241                return ret;
 242
 243        /* Disable chip interrupts before requesting irq */
 244        ret = rt1711h_write16(chip, TCPC_ALERT_MASK, 0);
 245        if (ret < 0)
 246                return ret;
 247
 248        chip->data.init = rt1711h_init;
 249        chip->data.set_vconn = rt1711h_set_vconn;
 250        chip->data.start_drp_toggling = rt1711h_start_drp_toggling;
 251        chip->tcpci = tcpci_register_port(chip->dev, &chip->data);
 252        if (IS_ERR_OR_NULL(chip->tcpci))
 253                return PTR_ERR(chip->tcpci);
 254
 255        ret = devm_request_threaded_irq(chip->dev, client->irq, NULL,
 256                                        rt1711h_irq,
 257                                        IRQF_ONESHOT | IRQF_TRIGGER_LOW,
 258                                        dev_name(chip->dev), chip);
 259        if (ret < 0)
 260                return ret;
 261        enable_irq_wake(client->irq);
 262
 263        return 0;
 264}
 265
 266static int rt1711h_remove(struct i2c_client *client)
 267{
 268        struct rt1711h_chip *chip = i2c_get_clientdata(client);
 269
 270        tcpci_unregister_port(chip->tcpci);
 271        return 0;
 272}
 273
 274static const struct i2c_device_id rt1711h_id[] = {
 275        { "rt1711h", 0 },
 276        { }
 277};
 278MODULE_DEVICE_TABLE(i2c, rt1711h_id);
 279
 280#ifdef CONFIG_OF
 281static const struct of_device_id rt1711h_of_match[] = {
 282        { .compatible = "richtek,rt1711h", },
 283        {},
 284};
 285MODULE_DEVICE_TABLE(of, rt1711h_of_match);
 286#endif
 287
 288static struct i2c_driver rt1711h_i2c_driver = {
 289        .driver = {
 290                .name = "rt1711h",
 291                .of_match_table = of_match_ptr(rt1711h_of_match),
 292        },
 293        .probe = rt1711h_probe,
 294        .remove = rt1711h_remove,
 295        .id_table = rt1711h_id,
 296};
 297module_i2c_driver(rt1711h_i2c_driver);
 298
 299MODULE_AUTHOR("ShuFan Lee <shufan_lee@richtek.com>");
 300MODULE_DESCRIPTION("RT1711H USB Type-C Port Controller Interface Driver");
 301MODULE_LICENSE("GPL");
 302