0001
0002
0003
0004
0005
0006
0007 #include <linux/bits.h>
0008 #include <linux/i2c.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/mutex.h>
0012 #include <linux/regmap.h>
0013 #include <linux/usb/typec_dp.h>
0014 #include <linux/usb/typec_mux.h>
0015
0016 #define FSA4480_SWITCH_ENABLE 0x04
0017 #define FSA4480_SWITCH_SELECT 0x05
0018 #define FSA4480_SWITCH_STATUS1 0x07
0019 #define FSA4480_SLOW_L 0x08
0020 #define FSA4480_SLOW_R 0x09
0021 #define FSA4480_SLOW_MIC 0x0a
0022 #define FSA4480_SLOW_SENSE 0x0b
0023 #define FSA4480_SLOW_GND 0x0c
0024 #define FSA4480_DELAY_L_R 0x0d
0025 #define FSA4480_DELAY_L_MIC 0x0e
0026 #define FSA4480_DELAY_L_SENSE 0x0f
0027 #define FSA4480_DELAY_L_AGND 0x10
0028 #define FSA4480_RESET 0x1e
0029 #define FSA4480_MAX_REGISTER 0x1f
0030
0031 #define FSA4480_ENABLE_DEVICE BIT(7)
0032 #define FSA4480_ENABLE_SBU GENMASK(6, 5)
0033 #define FSA4480_ENABLE_USB GENMASK(4, 3)
0034
0035 #define FSA4480_SEL_SBU_REVERSE GENMASK(6, 5)
0036 #define FSA4480_SEL_USB GENMASK(4, 3)
0037
0038 struct fsa4480 {
0039 struct i2c_client *client;
0040
0041
0042 struct mutex lock;
0043
0044 struct typec_switch_dev *sw;
0045 struct typec_mux_dev *mux;
0046
0047 struct regmap *regmap;
0048
0049 u8 cur_enable;
0050 u8 cur_select;
0051 };
0052
0053 static const struct regmap_config fsa4480_regmap_config = {
0054 .reg_bits = 8,
0055 .val_bits = 8,
0056 .max_register = FSA4480_MAX_REGISTER,
0057
0058 .disable_locking = true,
0059 };
0060
0061 static int fsa4480_switch_set(struct typec_switch_dev *sw,
0062 enum typec_orientation orientation)
0063 {
0064 struct fsa4480 *fsa = typec_switch_get_drvdata(sw);
0065 u8 new_sel;
0066
0067 mutex_lock(&fsa->lock);
0068 new_sel = FSA4480_SEL_USB;
0069 if (orientation == TYPEC_ORIENTATION_REVERSE)
0070 new_sel |= FSA4480_SEL_SBU_REVERSE;
0071
0072 if (new_sel == fsa->cur_select)
0073 goto out_unlock;
0074
0075 if (fsa->cur_enable & FSA4480_ENABLE_SBU) {
0076
0077 regmap_write(fsa->regmap, FSA4480_SWITCH_ENABLE,
0078 fsa->cur_enable & ~FSA4480_ENABLE_SBU);
0079
0080
0081 usleep_range(35, 1000);
0082 }
0083
0084 regmap_write(fsa->regmap, FSA4480_SWITCH_SELECT, new_sel);
0085 fsa->cur_select = new_sel;
0086
0087 if (fsa->cur_enable & FSA4480_ENABLE_SBU) {
0088 regmap_write(fsa->regmap, FSA4480_SWITCH_ENABLE, fsa->cur_enable);
0089
0090
0091 usleep_range(15, 1000);
0092 }
0093
0094 out_unlock:
0095 mutex_unlock(&fsa->lock);
0096
0097 return 0;
0098 }
0099
0100 static int fsa4480_mux_set(struct typec_mux_dev *mux, struct typec_mux_state *state)
0101 {
0102 struct fsa4480 *fsa = typec_mux_get_drvdata(mux);
0103 u8 new_enable;
0104
0105 mutex_lock(&fsa->lock);
0106
0107 new_enable = FSA4480_ENABLE_DEVICE | FSA4480_ENABLE_USB;
0108 if (state->mode >= TYPEC_DP_STATE_A)
0109 new_enable |= FSA4480_ENABLE_SBU;
0110
0111 if (new_enable == fsa->cur_enable)
0112 goto out_unlock;
0113
0114 regmap_write(fsa->regmap, FSA4480_SWITCH_ENABLE, new_enable);
0115 fsa->cur_enable = new_enable;
0116
0117 if (new_enable & FSA4480_ENABLE_SBU) {
0118
0119 usleep_range(15, 1000);
0120 }
0121
0122 out_unlock:
0123 mutex_unlock(&fsa->lock);
0124
0125 return 0;
0126 }
0127
0128 static int fsa4480_probe(struct i2c_client *client)
0129 {
0130 struct device *dev = &client->dev;
0131 struct typec_switch_desc sw_desc = { };
0132 struct typec_mux_desc mux_desc = { };
0133 struct fsa4480 *fsa;
0134
0135 fsa = devm_kzalloc(dev, sizeof(*fsa), GFP_KERNEL);
0136 if (!fsa)
0137 return -ENOMEM;
0138
0139 fsa->client = client;
0140 mutex_init(&fsa->lock);
0141
0142 fsa->regmap = devm_regmap_init_i2c(client, &fsa4480_regmap_config);
0143 if (IS_ERR(fsa->regmap))
0144 return dev_err_probe(dev, PTR_ERR(fsa->regmap), "failed to initialize regmap\n");
0145
0146 fsa->cur_enable = FSA4480_ENABLE_DEVICE | FSA4480_ENABLE_USB;
0147 fsa->cur_select = FSA4480_SEL_USB;
0148
0149
0150 regmap_write(fsa->regmap, FSA4480_SLOW_L, 0x00);
0151 regmap_write(fsa->regmap, FSA4480_SLOW_R, 0x00);
0152 regmap_write(fsa->regmap, FSA4480_SLOW_MIC, 0x00);
0153 regmap_write(fsa->regmap, FSA4480_SLOW_SENSE, 0x00);
0154 regmap_write(fsa->regmap, FSA4480_SLOW_GND, 0x00);
0155 regmap_write(fsa->regmap, FSA4480_DELAY_L_R, 0x00);
0156 regmap_write(fsa->regmap, FSA4480_DELAY_L_MIC, 0x00);
0157 regmap_write(fsa->regmap, FSA4480_DELAY_L_SENSE, 0x00);
0158 regmap_write(fsa->regmap, FSA4480_DELAY_L_AGND, 0x09);
0159 regmap_write(fsa->regmap, FSA4480_SWITCH_SELECT, fsa->cur_select);
0160 regmap_write(fsa->regmap, FSA4480_SWITCH_ENABLE, fsa->cur_enable);
0161
0162 sw_desc.drvdata = fsa;
0163 sw_desc.fwnode = dev_fwnode(dev);
0164 sw_desc.set = fsa4480_switch_set;
0165
0166 fsa->sw = typec_switch_register(dev, &sw_desc);
0167 if (IS_ERR(fsa->sw))
0168 return dev_err_probe(dev, PTR_ERR(fsa->sw), "failed to register typec switch\n");
0169
0170 mux_desc.drvdata = fsa;
0171 mux_desc.fwnode = dev_fwnode(dev);
0172 mux_desc.set = fsa4480_mux_set;
0173
0174 fsa->mux = typec_mux_register(dev, &mux_desc);
0175 if (IS_ERR(fsa->mux)) {
0176 typec_switch_unregister(fsa->sw);
0177 return dev_err_probe(dev, PTR_ERR(fsa->mux), "failed to register typec mux\n");
0178 }
0179
0180 i2c_set_clientdata(client, fsa);
0181 return 0;
0182 }
0183
0184 static int fsa4480_remove(struct i2c_client *client)
0185 {
0186 struct fsa4480 *fsa = i2c_get_clientdata(client);
0187
0188 typec_mux_unregister(fsa->mux);
0189 typec_switch_unregister(fsa->sw);
0190
0191 return 0;
0192 }
0193
0194 static const struct i2c_device_id fsa4480_table[] = {
0195 { "fsa4480" },
0196 { }
0197 };
0198 MODULE_DEVICE_TABLE(i2c, fsa4480_table);
0199
0200 static const struct of_device_id fsa4480_of_table[] = {
0201 { .compatible = "fcs,fsa4480" },
0202 { }
0203 };
0204 MODULE_DEVICE_TABLE(of, fsa4480_of_table);
0205
0206 static struct i2c_driver fsa4480_driver = {
0207 .driver = {
0208 .name = "fsa4480",
0209 .of_match_table = fsa4480_of_table,
0210 },
0211 .probe_new = fsa4480_probe,
0212 .remove = fsa4480_remove,
0213 .id_table = fsa4480_table,
0214 };
0215 module_i2c_driver(fsa4480_driver);
0216
0217 MODULE_DESCRIPTION("ON Semiconductor FSA4480 driver");
0218 MODULE_LICENSE("GPL v2");