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/module.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/usb/tcpci.h>
15 #include <linux/usb/tcpm.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
19 #define RT1711H_VID 0x29CF
20 #define RT1711H_PID 0x1711
21 #define RT1711H_DID 0x2171
22 #define RT1715_DID 0x2173
24 #define RT1711H_PHYCTRL1 0x80
25 #define RT1711H_PHYCTRL2 0x81
27 #define RT1711H_RTCTRL4 0x93
28 /* rx threshold of rd/rp: 1b0 for level 0.4V/0.7V, 1b1 for 0.35V/0.75V */
29 #define RT1711H_BMCIO_RXDZSEL BIT(0)
31 #define RT1711H_RTCTRL8 0x9B
32 /* Autoidle timeout = (tout * 2 + 1) * 6.4ms */
33 #define RT1711H_RTCTRL8_SET(ck300, ship_off, auto_idle, tout) \
34 (((ck300) << 7) | ((ship_off) << 5) | \
35 ((auto_idle) << 3) | ((tout) & 0x07))
36 #define RT1711H_AUTOIDLEEN BIT(3)
37 #define RT1711H_ENEXTMSG BIT(4)
39 #define RT1711H_RTCTRL11 0x9E
41 /* I2C timeout = (tout + 1) * 12.5ms */
42 #define RT1711H_RTCTRL11_SET(en, tout) \
43 (((en) << 7) | ((tout) & 0x0F))
45 #define RT1711H_RTCTRL13 0xA0
46 #define RT1711H_RTCTRL14 0xA1
47 #define RT1711H_RTCTRL15 0xA2
48 #define RT1711H_RTCTRL16 0xA3
50 #define RT1711H_RTCTRL18 0xAF
51 /* 1b0 as fixed rx threshold of rd/rp 0.55V, 1b1 depends on RTCRTL4[0] */
52 #define BMCIO_RXDZEN BIT(0)
55 struct tcpci_data data;
58 struct regulator *vbus;
63 static int rt1711h_read16(struct rt1711h_chip *chip, unsigned int reg, u16 *val)
65 return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u16));
68 static int rt1711h_write16(struct rt1711h_chip *chip, unsigned int reg, u16 val)
70 return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u16));
73 static int rt1711h_read8(struct rt1711h_chip *chip, unsigned int reg, u8 *val)
75 return regmap_raw_read(chip->data.regmap, reg, val, sizeof(u8));
78 static int rt1711h_write8(struct rt1711h_chip *chip, unsigned int reg, u8 val)
80 return regmap_raw_write(chip->data.regmap, reg, &val, sizeof(u8));
83 static const struct regmap_config rt1711h_regmap_config = {
87 .max_register = 0xFF, /* 0x80 .. 0xFF are vendor defined */
90 static struct rt1711h_chip *tdata_to_rt1711h(struct tcpci_data *tdata)
92 return container_of(tdata, struct rt1711h_chip, data);
95 static int rt1711h_init(struct tcpci *tcpci, struct tcpci_data *tdata)
97 struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
98 struct regmap *regmap = chip->data.regmap;
101 /* CK 300K from 320K, shipping off, auto_idle enable, tout = 32ms */
102 ret = rt1711h_write8(chip, RT1711H_RTCTRL8,
103 RT1711H_RTCTRL8_SET(0, 1, 1, 2));
107 /* Enable PD30 extended message for RT1715 */
108 if (chip->did == RT1715_DID) {
109 ret = regmap_update_bits(regmap, RT1711H_RTCTRL8,
110 RT1711H_ENEXTMSG, RT1711H_ENEXTMSG);
115 /* I2C reset : (val + 1) * 12.5ms */
116 ret = rt1711h_write8(chip, RT1711H_RTCTRL11,
117 RT1711H_RTCTRL11_SET(1, 0x0F));
121 /* tTCPCfilter : (26.7 * val) us */
122 ret = rt1711h_write8(chip, RT1711H_RTCTRL14, 0x0F);
126 /* tDRP : (51.2 + 6.4 * val) ms */
127 ret = rt1711h_write8(chip, RT1711H_RTCTRL15, 0x04);
131 /* dcSRC.DRP : 33% */
132 ret = rt1711h_write16(chip, RT1711H_RTCTRL16, 330);
136 /* Enable phy discard retry, retry count 7, rx filter deglitch 100 us */
137 ret = rt1711h_write8(chip, RT1711H_PHYCTRL1, 0xF1);
141 /* Decrease wait time of BMC-encoded 1 bit from 2.67us to 2.55us */
142 /* wait time : (val * .4167) us */
143 return rt1711h_write8(chip, RT1711H_PHYCTRL2, 62);
146 static int rt1711h_set_vbus(struct tcpci *tcpci, struct tcpci_data *tdata,
149 struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
152 if (chip->src_en == src)
156 ret = regulator_enable(chip->vbus);
158 ret = regulator_disable(chip->vbus);
165 static int rt1711h_set_vconn(struct tcpci *tcpci, struct tcpci_data *tdata,
168 struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
170 return regmap_update_bits(chip->data.regmap, RT1711H_RTCTRL8,
171 RT1711H_AUTOIDLEEN, enable ? 0 : RT1711H_AUTOIDLEEN);
175 * Selects the CC PHY noise filter voltage level according to the remote current
178 * @status: The port's current cc status read from IC
179 * Return 0 if writes succeed; failure code otherwise
181 static inline int rt1711h_init_cc_params(struct rt1711h_chip *chip, u8 status)
185 u32 rxdz_en, rxdz_sel;
187 ret = rt1711h_read8(chip, TCPC_ROLE_CTRL, &role);
191 cc1 = tcpci_to_typec_cc((status >> TCPC_CC_STATUS_CC1_SHIFT) &
192 TCPC_CC_STATUS_CC1_MASK,
193 status & TCPC_CC_STATUS_TERM ||
194 tcpc_presenting_rd(role, CC1));
195 cc2 = tcpci_to_typec_cc((status >> TCPC_CC_STATUS_CC2_SHIFT) &
196 TCPC_CC_STATUS_CC2_MASK,
197 status & TCPC_CC_STATUS_TERM ||
198 tcpc_presenting_rd(role, CC2));
200 if ((cc1 >= TYPEC_CC_RP_1_5 && cc2 < TYPEC_CC_RP_DEF) ||
201 (cc2 >= TYPEC_CC_RP_1_5 && cc1 < TYPEC_CC_RP_DEF)) {
202 rxdz_en = BMCIO_RXDZEN;
203 if (chip->did == RT1715_DID)
204 rxdz_sel = RT1711H_BMCIO_RXDZSEL;
209 rxdz_sel = RT1711H_BMCIO_RXDZSEL;
212 ret = regmap_update_bits(chip->data.regmap, RT1711H_RTCTRL18,
213 BMCIO_RXDZEN, rxdz_en);
217 return regmap_update_bits(chip->data.regmap, RT1711H_RTCTRL4,
218 RT1711H_BMCIO_RXDZSEL, rxdz_sel);
221 static int rt1711h_start_drp_toggling(struct tcpci *tcpci,
222 struct tcpci_data *tdata,
223 enum typec_cc_status cc)
225 struct rt1711h_chip *chip = tdata_to_rt1711h(tdata);
227 unsigned int reg = 0;
231 case TYPEC_CC_RP_DEF:
232 reg |= (TCPC_ROLE_CTRL_RP_VAL_DEF <<
233 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
235 case TYPEC_CC_RP_1_5:
236 reg |= (TCPC_ROLE_CTRL_RP_VAL_1_5 <<
237 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
239 case TYPEC_CC_RP_3_0:
240 reg |= (TCPC_ROLE_CTRL_RP_VAL_3_0 <<
241 TCPC_ROLE_CTRL_RP_VAL_SHIFT);
245 if (cc == TYPEC_CC_RD)
246 reg |= (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC1_SHIFT) |
247 (TCPC_ROLE_CTRL_CC_RD << TCPC_ROLE_CTRL_CC2_SHIFT);
249 reg |= (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC1_SHIFT) |
250 (TCPC_ROLE_CTRL_CC_RP << TCPC_ROLE_CTRL_CC2_SHIFT);
252 ret = rt1711h_write8(chip, TCPC_ROLE_CTRL, reg);
255 usleep_range(500, 1000);
260 static irqreturn_t rt1711h_irq(int irq, void *dev_id)
265 struct rt1711h_chip *chip = dev_id;
270 ret = rt1711h_read16(chip, TCPC_ALERT, &alert);
274 if (alert & TCPC_ALERT_CC_STATUS) {
275 ret = rt1711h_read8(chip, TCPC_CC_STATUS, &status);
278 /* Clear cc change event triggered by starting toggling */
279 if (status & TCPC_CC_STATUS_TOGGLING)
280 rt1711h_write8(chip, TCPC_ALERT, TCPC_ALERT_CC_STATUS);
282 rt1711h_init_cc_params(chip, status);
286 return tcpci_irq(chip->tcpci);
289 static int rt1711h_sw_reset(struct rt1711h_chip *chip)
293 ret = rt1711h_write8(chip, RT1711H_RTCTRL13, 0x01);
297 usleep_range(1000, 2000);
301 static int rt1711h_check_revision(struct i2c_client *i2c, struct rt1711h_chip *chip)
305 ret = i2c_smbus_read_word_data(i2c, TCPC_VENDOR_ID);
308 if (ret != RT1711H_VID) {
309 dev_err(&i2c->dev, "vid is not correct, 0x%04x\n", ret);
312 ret = i2c_smbus_read_word_data(i2c, TCPC_PRODUCT_ID);
315 if (ret != RT1711H_PID) {
316 dev_err(&i2c->dev, "pid is not correct, 0x%04x\n", ret);
319 ret = i2c_smbus_read_word_data(i2c, TCPC_BCD_DEV);
322 if (ret != chip->did) {
323 dev_err(&i2c->dev, "did is not correct, 0x%04x\n", ret);
326 dev_dbg(&i2c->dev, "did is 0x%04x\n", ret);
330 static int rt1711h_probe(struct i2c_client *client,
331 const struct i2c_device_id *i2c_id)
334 struct rt1711h_chip *chip;
336 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
340 chip->did = (size_t)device_get_match_data(&client->dev);
342 ret = rt1711h_check_revision(client, chip);
344 dev_err(&client->dev, "check vid/pid fail\n");
348 chip->data.regmap = devm_regmap_init_i2c(client,
349 &rt1711h_regmap_config);
350 if (IS_ERR(chip->data.regmap))
351 return PTR_ERR(chip->data.regmap);
353 chip->dev = &client->dev;
354 i2c_set_clientdata(client, chip);
356 ret = rt1711h_sw_reset(chip);
360 /* Disable chip interrupts before requesting irq */
361 ret = rt1711h_write16(chip, TCPC_ALERT_MASK, 0);
365 chip->vbus = devm_regulator_get(&client->dev, "vbus");
366 if (IS_ERR(chip->vbus))
367 return PTR_ERR(chip->vbus);
369 chip->data.init = rt1711h_init;
370 chip->data.set_vbus = rt1711h_set_vbus;
371 chip->data.set_vconn = rt1711h_set_vconn;
372 chip->data.start_drp_toggling = rt1711h_start_drp_toggling;
373 chip->tcpci = tcpci_register_port(chip->dev, &chip->data);
374 if (IS_ERR_OR_NULL(chip->tcpci))
375 return PTR_ERR(chip->tcpci);
377 ret = devm_request_threaded_irq(chip->dev, client->irq, NULL,
379 IRQF_ONESHOT | IRQF_TRIGGER_LOW,
380 dev_name(chip->dev), chip);
383 enable_irq_wake(client->irq);
388 static void rt1711h_remove(struct i2c_client *client)
390 struct rt1711h_chip *chip = i2c_get_clientdata(client);
392 tcpci_unregister_port(chip->tcpci);
395 static const struct i2c_device_id rt1711h_id[] = {
400 MODULE_DEVICE_TABLE(i2c, rt1711h_id);
403 static const struct of_device_id rt1711h_of_match[] = {
404 { .compatible = "richtek,rt1711h", .data = (void *)RT1711H_DID },
405 { .compatible = "richtek,rt1715", .data = (void *)RT1715_DID },
408 MODULE_DEVICE_TABLE(of, rt1711h_of_match);
411 static struct i2c_driver rt1711h_i2c_driver = {
414 .of_match_table = of_match_ptr(rt1711h_of_match),
416 .probe = rt1711h_probe,
417 .remove = rt1711h_remove,
418 .id_table = rt1711h_id,
420 module_i2c_driver(rt1711h_i2c_driver);
422 MODULE_AUTHOR("ShuFan Lee <shufan_lee@richtek.com>");
423 MODULE_DESCRIPTION("RT1711H USB Type-C Port Controller Interface Driver");
424 MODULE_LICENSE("GPL");