1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2022 Richtek Technology Corp.
4 *
5 * Author: ChiYuan Huang <cy_huang@richtek.com>
6 */
7
8#include <linux/bits.h>
9#include <linux/interrupt.h>
10#include <linux/kernel.h>
11#include <linux/mod_devicetable.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <linux/pm_wakeup.h>
15#include <linux/pm_wakeirq.h>
16#include <linux/regmap.h>
17#include <linux/regulator/consumer.h>
18#include <linux/usb/tcpci.h>
19#include <linux/usb/tcpm.h>
20
21#define MT6370_REG_SYSCTRL8 0x9B
22
23#define MT6370_AUTOIDLE_MASK BIT(3)
24
25#define MT6370_VENDOR_ID 0x29CF
26#define MT6370_TCPC_DID_A 0x2170
27
28struct mt6370_priv {
29 struct device *dev;
30 struct regulator *vbus;
31 struct tcpci *tcpci;
32 struct tcpci_data tcpci_data;
33};
34
35static const struct reg_sequence mt6370_reg_init[] = {
36 REG_SEQ(0xA0, 0x1, 1000),
37 REG_SEQ(0x81, 0x38, 0),
38 REG_SEQ(0x82, 0x82, 0),
39 REG_SEQ(0xBA, 0xFC, 0),
40 REG_SEQ(0xBB, 0x50, 0),
41 REG_SEQ(0x9E, 0x8F, 0),
42 REG_SEQ(0xA1, 0x5, 0),
43 REG_SEQ(0xA2, 0x4, 0),
44 REG_SEQ(0xA3, 0x4A, 0),
45 REG_SEQ(0xA4, 0x01, 0),
46 REG_SEQ(0x95, 0x01, 0),
47 REG_SEQ(0x80, 0x71, 0),
48 REG_SEQ(0x9B, 0x3A, 1000),
49};
50
51static int mt6370_tcpc_init(struct tcpci *tcpci, struct tcpci_data *data)
52{
53 u16 did;
54 int ret;
55
56 ret = regmap_register_patch(map: data->regmap, regs: mt6370_reg_init,
57 ARRAY_SIZE(mt6370_reg_init));
58 if (ret)
59 return ret;
60
61 ret = regmap_raw_read(map: data->regmap, TCPC_BCD_DEV, val: &did, val_len: sizeof(u16));
62 if (ret)
63 return ret;
64
65 if (did == MT6370_TCPC_DID_A)
66 return regmap_write(map: data->regmap, TCPC_FAULT_CTRL, val: 0x80);
67
68 return 0;
69}
70
71static int mt6370_tcpc_set_vconn(struct tcpci *tcpci, struct tcpci_data *data,
72 bool enable)
73{
74 return regmap_update_bits(map: data->regmap, MT6370_REG_SYSCTRL8,
75 MT6370_AUTOIDLE_MASK,
76 val: enable ? 0 : MT6370_AUTOIDLE_MASK);
77}
78
79static int mt6370_tcpc_set_vbus(struct tcpci *tcpci, struct tcpci_data *data,
80 bool source, bool sink)
81{
82 struct mt6370_priv *priv = container_of(data, struct mt6370_priv,
83 tcpci_data);
84 int ret;
85
86 ret = regulator_is_enabled(regulator: priv->vbus);
87 if (ret < 0)
88 return ret;
89
90 if (ret && !source)
91 return regulator_disable(regulator: priv->vbus);
92
93 if (!ret && source)
94 return regulator_enable(regulator: priv->vbus);
95
96 return 0;
97}
98
99static irqreturn_t mt6370_irq_handler(int irq, void *dev_id)
100{
101 struct mt6370_priv *priv = dev_id;
102
103 return tcpci_irq(tcpci: priv->tcpci);
104}
105
106static int mt6370_check_vendor_info(struct mt6370_priv *priv)
107{
108 struct regmap *regmap = priv->tcpci_data.regmap;
109 u16 vid;
110 int ret;
111
112 ret = regmap_raw_read(map: regmap, TCPC_VENDOR_ID, val: &vid, val_len: sizeof(u16));
113 if (ret)
114 return ret;
115
116 if (vid != MT6370_VENDOR_ID)
117 return dev_err_probe(dev: priv->dev, err: -ENODEV,
118 fmt: "Vendor ID not correct 0x%02x\n", vid);
119
120 return 0;
121}
122
123static void mt6370_unregister_tcpci_port(void *tcpci)
124{
125 tcpci_unregister_port(tcpci);
126}
127
128static int mt6370_tcpc_probe(struct platform_device *pdev)
129{
130 struct mt6370_priv *priv;
131 struct device *dev = &pdev->dev;
132 int irq, ret;
133
134 priv = devm_kzalloc(dev, size: sizeof(*priv), GFP_KERNEL);
135 if (!priv)
136 return -ENOMEM;
137
138 priv->dev = dev;
139
140 priv->tcpci_data.regmap = dev_get_regmap(dev: dev->parent, NULL);
141 if (!priv->tcpci_data.regmap)
142 return dev_err_probe(dev, err: -ENODEV, fmt: "Failed to init regmap\n");
143
144 ret = mt6370_check_vendor_info(priv);
145 if (ret)
146 return ret;
147
148 irq = platform_get_irq(pdev, 0);
149 if (irq < 0)
150 return irq;
151
152 /* Assign TCPCI feature and ops */
153 priv->tcpci_data.auto_discharge_disconnect = 1;
154 priv->tcpci_data.init = mt6370_tcpc_init;
155 priv->tcpci_data.set_vconn = mt6370_tcpc_set_vconn;
156
157 priv->vbus = devm_regulator_get_optional(dev, id: "vbus");
158 if (!IS_ERR(ptr: priv->vbus))
159 priv->tcpci_data.set_vbus = mt6370_tcpc_set_vbus;
160
161 priv->tcpci = tcpci_register_port(dev, data: &priv->tcpci_data);
162 if (IS_ERR(ptr: priv->tcpci))
163 return dev_err_probe(dev, err: PTR_ERR(ptr: priv->tcpci),
164 fmt: "Failed to register tcpci port\n");
165
166 ret = devm_add_action_or_reset(dev, mt6370_unregister_tcpci_port, priv->tcpci);
167 if (ret)
168 return ret;
169
170 ret = devm_request_threaded_irq(dev, irq, NULL, thread_fn: mt6370_irq_handler,
171 IRQF_ONESHOT, devname: dev_name(dev), dev_id: priv);
172 if (ret)
173 return dev_err_probe(dev, err: ret, fmt: "Failed to allocate irq\n");
174
175 device_init_wakeup(dev, enable: true);
176 dev_pm_set_wake_irq(dev, irq);
177
178 return 0;
179}
180
181static void mt6370_tcpc_remove(struct platform_device *pdev)
182{
183 dev_pm_clear_wake_irq(dev: &pdev->dev);
184 device_init_wakeup(dev: &pdev->dev, enable: false);
185}
186
187static const struct of_device_id mt6370_tcpc_devid_table[] = {
188 { .compatible = "mediatek,mt6370-tcpc" },
189 {}
190};
191MODULE_DEVICE_TABLE(of, mt6370_tcpc_devid_table);
192
193static struct platform_driver mt6370_tcpc_driver = {
194 .driver = {
195 .name = "mt6370-tcpc",
196 .of_match_table = mt6370_tcpc_devid_table,
197 },
198 .probe = mt6370_tcpc_probe,
199 .remove_new = mt6370_tcpc_remove,
200};
201module_platform_driver(mt6370_tcpc_driver);
202
203MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
204MODULE_DESCRIPTION("MT6370 USB Type-C Port Controller Interface Driver");
205MODULE_LICENSE("GPL v2");
206

source code of linux/drivers/usb/typec/tcpm/tcpci_mt6370.c