1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, Richtek Technology Corporation
5 * Richtek RT1711H Type-C Chip Driver
8 #include <linux/bits.h>
9 #include <linux/kernel.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/usb/tcpci.h>
16 #include <linux/usb/tcpm.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
20 #define RT1711H_VID 0x29CF
21 #define RT1711H_PID 0x1711
22 #define RT1711H_DID 0x2171
23 #define RT1715_DID 0x2173
25 #define RT1711H_PHYCTRL1 0x80
26 #define RT1711H_PHYCTRL2 0x81
28 #define RT1711H_RTCTRL4 0x93
29 /* rx threshold of rd/rp: 1b0 for level 0.4V/0.7V, 1b1 for 0.35V/0.75V */
30 #define RT1711H_BMCIO_RXDZSEL BIT(0)
32 #define RT1711H_RTCTRL8 0x9B
33 /* Autoidle timeout = (tout * 2 + 1) * 6.4ms */
34 #define RT1711H_RTCTRL8_SET(ck300, ship_off, auto_idle, tout) \
35 (((ck300) << 7) | ((ship_off) << 5) | \
36 ((auto_idle) << 3) | ((tout) & 0x07))
37 #define RT1711H_AUTOIDLEEN BIT(3)
38 #define RT1711H_ENEXTMSG BIT(4)
40 #define RT1711H_RTCTRL11 0x9E
42 /* I2C timeout = (tout + 1) * 12.5ms */
43 #define RT1711H_RTCTRL11_SET(en, tout) \
44 (((en) << 7) | ((tout) & 0x0F))
46 #define RT1711H_RTCTRL13 0xA0
47 #define RT1711H_RTCTRL14 0xA1
48 #define RT1711H_RTCTRL15 0xA2
49 #define RT1711H_RTCTRL16 0xA3
51 #define RT1711H_RTCTRL18 0xAF
52 /* 1b0 as fixed rx threshold of rd/rp 0.55V, 1b1 depends on RTCRTL4[0] */
53 #define BMCIO_RXDZEN BIT(0)
55 struct rt1711h_chip_info {
58 bool enable_pd30_extended_message;
62 struct tcpci_data data;
65 struct regulator *vbus;
66 const struct rt1711h_chip_info *info;
70 static int rt1711h_read16(struct rt1711h_chip *chip, unsigned int reg, u16 *val)
72 return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u16));
75 static int rt1711h_write16(struct rt1711h_chip *chip, unsigned int reg, u16 val)
77 return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u16));
80 static int rt1711h_read8(struct rt1711h_chip *chip, unsigned int reg, u8 *val)
82 return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u8));
85 static int rt1711h_write8(struct rt1711h_chip *chip, unsigned int reg, u8 val)
87 return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u8));
90 static const struct regmap_config rt1711h_regmap_config = {
94 .max_register = 0xFF, /* 0x80 .. 0xFF are vendor defined */
97 static struct rt1711h_chip *tdata_to_rt1711h(struct tcpci_data *tdata)
99 return container_of(tdata, struct rt1711h_chip, data);
102 static int rt1711h_init(struct tcpci *tcpci, struct tcpci_data *tdata)
104 struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
105 struct regmap *regmap = chip->data.regmap;
108 /* CK 300K from 320K, shipping off, auto_idle enable, tout = 32ms */
109 ret = rt1711h_write8(chip, RT1711H_RTCTRL8,
110 RT1711H_RTCTRL8_SET(0, 1, 1, 2));
114 /* Enable PD30 extended message for RT1715 */
115 if (chip->info->enable_pd30_extended_message) {
116 ret = regmap_update_bits(regmap, RT1711H_RTCTRL8,
117 RT1711H_ENEXTMSG, RT1711H_ENEXTMSG);
122 /* I2C reset : (val + 1) * 12.5ms */
123 ret = rt1711h_write8(chip, RT1711H_RTCTRL11,
124 RT1711H_RTCTRL11_SET(1, 0x0F));
128 /* tTCPCfilter : (26.7 * val) us */
129 ret = rt1711h_write8(chip, RT1711H_RTCTRL14, 0x0F);
133 /* tDRP : (51.2 + 6.4 * val) ms */
134 ret = rt1711h_write8(chip, RT1711H_RTCTRL15, 0x04);
138 /* dcSRC.DRP : 33% */
139 ret = rt1711h_write16(chip, RT1711H_RTCTRL16, 330);
143 /* Enable phy discard retry, retry count 7, rx filter deglitch 100 us */
144 ret = rt1711h_write8(chip, RT1711H_PHYCTRL1, 0xF1);
148 /* Decrease wait time of BMC-encoded 1 bit from 2.67us to 2.55us */
149 /* wait time : (val * .4167) us */
150 return rt1711h_write8(chip, RT1711H_PHYCTRL2, 62);
153 static int rt1711h_set_vbus(struct tcpci *tcpci, struct tcpci_data *tdata,
156 struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
159 if (chip->src_en == src)
163 ret = regulator_enable(chip->vbus);
165 ret = regulator_disable(chip->vbus);
172 static int rt1711h_set_vconn(struct tcpci *tcpci, struct tcpci_data *tdata,
175 struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
177 return regmap_update_bits(chip->data.regmap, RT1711H_RTCTRL8,
178 RT1711H_AUTOIDLEEN, enable ? 0 : RT1711H_AUTOIDLEEN);
182 * Selects the CC PHY noise filter voltage level according to the remote current
185 * @status: The port's current cc status read from IC
186 * Return 0 if writes succeed; failure code otherwise
188 static inline int rt1711h_init_cc_params(struct rt1711h_chip *chip, u8 status)
192 u32 rxdz_en, rxdz_sel;
194 ret = rt1711h_read8(chip, TCPC_ROLE_CTRL, &role);
198 cc1 = tcpci_to_typec_cc((status >> TCPC_CC_STATUS_CC1_SHIFT) &
199 TCPC_CC_STATUS_CC1_MASK,
200 status & TCPC_CC_STATUS_TERM ||
201 tcpc_presenting_rd(role, CC1));
202 cc2 = tcpci_to_typec_cc((status >> TCPC_CC_STATUS_CC2_SHIFT) &
203 TCPC_CC_STATUS_CC2_MASK,
204 status & TCPC_CC_STATUS_TERM ||
205 tcpc_presenting_rd(role, CC2));
207 if ((cc1 >= TYPEC_CC_RP_1_5 && cc2 < TYPEC_CC_RP_DEF) ||
208 (cc2 >= TYPEC_CC_RP_1_5 && cc1 < TYPEC_CC_RP_DEF)) {
209 rxdz_en = BMCIO_RXDZEN;
210 rxdz_sel = chip->info->rxdz_sel;
213 rxdz_sel = RT1711H_BMCIO_RXDZSEL;
216 ret = regmap_update_bits(chip->data.regmap, RT1711H_RTCTRL18,
217 BMCIO_RXDZEN, rxdz_en);
221 return regmap_update_bits(chip->data.regmap, RT1711H_RTCTRL4,
222 RT1711H_BMCIO_RXDZSEL, rxdz_sel);
225 static int rt1711h_start_drp_toggling(struct tcpci *tcpci,
226 struct tcpci_data *tdata,
227 enum typec_cc_status cc)
229 struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
231 unsigned int reg = 0;
235 case TYPEC_CC_RP_DEF:
236 reg |= (TCPC_ROLE_CTRL_RP_VAL_DEF <<
237 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
239 case TYPEC_CC_RP_1_5:
240 reg |= (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
241 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
243 case TYPEC_CC_RP_3_0:
244 reg |= (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
245 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
249 if (cc == TYPEC_CC_RD)
250 reg |= (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
251 (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
253 reg |= (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
254 (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT);
256 ret = rt1711h_write8(chip, TCPC_ROLE_CTRL, reg);
259 usleep_range(500, 1000);
264 static irqreturn_t rt1711h_irq(int irq, void *dev_id)
269 struct rt1711h_chip *chip = dev_id;
274 ret = rt1711h_read16(chip, TCPC_ALERT, &alert);
278 if (alert & TCPC_ALERT_CC_STATUS) {
279 ret = rt1711h_read8(chip, TCPC_CC_STATUS, &status);
282 /* Clear cc change event triggered by starting toggling */
283 if (status & TCPC_CC_STATUS_TOGGLING)
284 rt1711h_write8(chip, TCPC_ALERT, TCPC_ALERT_CC_STATUS);
286 rt1711h_init_cc_params(chip, status);
290 return tcpci_irq(chip->tcpci);
293 static int rt1711h_sw_reset(struct rt1711h_chip *chip)
297 ret = rt1711h_write8(chip, RT1711H_RTCTRL13, 0x01);
301 usleep_range(1000, 2000);
305 static int rt1711h_check_revision(struct i2c_client *i2c, struct rt1711h_chip *chip)
309 ret = i2c_smbus_read_word_data(i2c, TCPC_VENDOR_ID);
312 if (ret != RT1711H_VID) {
313 dev_err(&i2c->dev, "vid is not correct, 0x%04x\n", ret);
316 ret = i2c_smbus_read_word_data(i2c, TCPC_PRODUCT_ID);
319 if (ret != RT1711H_PID) {
320 dev_err(&i2c->dev, "pid is not correct, 0x%04x\n", ret);
323 ret = i2c_smbus_read_word_data(i2c, TCPC_BCD_DEV);
326 if (ret != chip->info->did) {
327 dev_err(&i2c->dev, "did is not correct, 0x%04x\n", ret);
330 dev_dbg(&i2c->dev, "did is 0x%04x\n", ret);
334 static int rt1711h_probe(struct i2c_client *client)
337 struct rt1711h_chip *chip;
339 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
343 chip->info = i2c_get_match_data(client);
345 ret = rt1711h_check_revision(client, chip);
347 dev_err(&client->dev, "check vid/pid fail\n");
351 chip->data.regmap = devm_regmap_init_i2c(client,
352 &rt1711h_regmap_config);
353 if (IS_ERR(chip->data.regmap))
354 return PTR_ERR(chip->data.regmap);
356 chip->dev = &client->dev;
357 i2c_set_clientdata(client, chip);
359 ret = rt1711h_sw_reset(chip);
363 /* Disable chip interrupts before requesting irq */
364 ret = rt1711h_write16(chip, TCPC_ALERT_MASK, 0);
368 chip->vbus = devm_regulator_get(&client->dev, "vbus");
369 if (IS_ERR(chip->vbus))
370 return PTR_ERR(chip->vbus);
372 chip->data.init = rt1711h_init;
373 chip->data.set_vbus = rt1711h_set_vbus;
374 chip->data.set_vconn = rt1711h_set_vconn;
375 chip->data.start_drp_toggling = rt1711h_start_drp_toggling;
376 chip->tcpci = tcpci_register_port(chip->dev, &chip->data);
377 if (IS_ERR_OR_NULL(chip->tcpci))
378 return PTR_ERR(chip->tcpci);
380 ret = devm_request_threaded_irq(chip->dev, client->irq, NULL,
382 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
383 dev_name(chip->dev), chip);
386 enable_irq_wake(client->irq);
391 static void rt1711h_remove(struct i2c_client *client)
393 struct rt1711h_chip *chip = i2c_get_clientdata(client);
395 tcpci_unregister_port(chip->tcpci);
398 static const struct rt1711h_chip_info rt1711h = {
402 static const struct rt1711h_chip_info rt1715 = {
403 .rxdz_sel = RT1711H_BMCIO_RXDZSEL,
405 .enable_pd30_extended_message = true,
408 static const struct i2c_device_id rt1711h_id[] = {
409 { "rt1711h", (kernel_ulong_t)&rt1711h },
410 { "rt1715", (kernel_ulong_t)&rt1715 },
413 MODULE_DEVICE_TABLE(i2c, rt1711h_id);
415 static const struct of_device_id rt1711h_of_match[] = {
416 { .compatible = "richtek,rt1711h", .data = &rt1711h },
417 { .compatible = "richtek,rt1715", .data = &rt1715 },
420 MODULE_DEVICE_TABLE(of, rt1711h_of_match);
422 static struct i2c_driver rt1711h_i2c_driver = {
425 .of_match_table = rt1711h_of_match,
427 .probe = rt1711h_probe,
428 .remove = rt1711h_remove,
429 .id_table = rt1711h_id,
431 module_i2c_driver(rt1711h_i2c_driver);
433 MODULE_AUTHOR("ShuFan Lee <shufan_lee@richtek.com>");
434 MODULE_DESCRIPTION("RT1711H USB Type-C Port Controller Interface Driver");
435 MODULE_LICENSE("GPL");