0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/gpio.h>
0009 #include <linux/i2c.h>
0010 #include <linux/init.h>
0011 #include <linux/input.h>
0012 #include <linux/module.h>
0013 #include <linux/of_gpio.h>
0014 #include <linux/regmap.h>
0015 #include <linux/acpi.h>
0016
0017 #include <sound/core.h>
0018 #include <sound/jack.h>
0019 #include <sound/soc.h>
0020
0021 #include "ts3a227e.h"
0022
0023 struct ts3a227e {
0024 struct device *dev;
0025 struct regmap *regmap;
0026 struct snd_soc_jack *jack;
0027 bool plugged;
0028 bool mic_present;
0029 unsigned int buttons_held;
0030 int irq;
0031 };
0032
0033
0034 static const int ts3a227e_buttons[] = {
0035 SND_JACK_BTN_0,
0036 SND_JACK_BTN_1,
0037 SND_JACK_BTN_2,
0038 SND_JACK_BTN_3,
0039 };
0040
0041 #define TS3A227E_NUM_BUTTONS 4
0042 #define TS3A227E_JACK_MASK (SND_JACK_HEADPHONE | \
0043 SND_JACK_MICROPHONE | \
0044 SND_JACK_BTN_0 | \
0045 SND_JACK_BTN_1 | \
0046 SND_JACK_BTN_2 | \
0047 SND_JACK_BTN_3)
0048
0049
0050 #define TS3A227E_REG_DEVICE_ID 0x00
0051 #define TS3A227E_REG_INTERRUPT 0x01
0052 #define TS3A227E_REG_KP_INTERRUPT 0x02
0053 #define TS3A227E_REG_INTERRUPT_DISABLE 0x03
0054 #define TS3A227E_REG_SETTING_1 0x04
0055 #define TS3A227E_REG_SETTING_2 0x05
0056 #define TS3A227E_REG_SETTING_3 0x06
0057 #define TS3A227E_REG_SWITCH_CONTROL_1 0x07
0058 #define TS3A227E_REG_SWITCH_CONTROL_2 0x08
0059 #define TS3A227E_REG_SWITCH_STATUS_1 0x09
0060 #define TS3A227E_REG_SWITCH_STATUS_2 0x0a
0061 #define TS3A227E_REG_ACCESSORY_STATUS 0x0b
0062 #define TS3A227E_REG_ADC_OUTPUT 0x0c
0063 #define TS3A227E_REG_KP_THRESHOLD_1 0x0d
0064 #define TS3A227E_REG_KP_THRESHOLD_2 0x0e
0065 #define TS3A227E_REG_KP_THRESHOLD_3 0x0f
0066
0067
0068 #define INS_REM_EVENT 0x01
0069 #define DETECTION_COMPLETE_EVENT 0x02
0070
0071
0072 #define PRESS_MASK(idx) (0x01 << (2 * (idx)))
0073 #define RELEASE_MASK(idx) (0x02 << (2 * (idx)))
0074
0075
0076 #define INS_REM_INT_DISABLE 0x01
0077 #define DETECTION_COMPLETE_INT_DISABLE 0x02
0078 #define ADC_COMPLETE_INT_DISABLE 0x04
0079 #define INTB_DISABLE 0x08
0080
0081
0082 #define KP_ENABLE 0x04
0083
0084
0085 #define MICBIAS_SETTING_SFT (3)
0086 #define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT)
0087
0088
0089 #define TYPE_3_POLE 0x01
0090 #define TYPE_4_POLE_OMTP 0x02
0091 #define TYPE_4_POLE_STANDARD 0x04
0092 #define JACK_INSERTED 0x08
0093 #define EITHER_MIC_MASK (TYPE_4_POLE_OMTP | TYPE_4_POLE_STANDARD)
0094
0095 static const struct reg_default ts3a227e_reg_defaults[] = {
0096 { TS3A227E_REG_DEVICE_ID, 0x10 },
0097 { TS3A227E_REG_INTERRUPT, 0x00 },
0098 { TS3A227E_REG_KP_INTERRUPT, 0x00 },
0099 { TS3A227E_REG_INTERRUPT_DISABLE, 0x08 },
0100 { TS3A227E_REG_SETTING_1, 0x23 },
0101 { TS3A227E_REG_SETTING_2, 0x00 },
0102 { TS3A227E_REG_SETTING_3, 0x0e },
0103 { TS3A227E_REG_SWITCH_CONTROL_1, 0x00 },
0104 { TS3A227E_REG_SWITCH_CONTROL_2, 0x00 },
0105 { TS3A227E_REG_SWITCH_STATUS_1, 0x0c },
0106 { TS3A227E_REG_SWITCH_STATUS_2, 0x00 },
0107 { TS3A227E_REG_ACCESSORY_STATUS, 0x00 },
0108 { TS3A227E_REG_ADC_OUTPUT, 0x00 },
0109 { TS3A227E_REG_KP_THRESHOLD_1, 0x20 },
0110 { TS3A227E_REG_KP_THRESHOLD_2, 0x40 },
0111 { TS3A227E_REG_KP_THRESHOLD_3, 0x68 },
0112 };
0113
0114 static bool ts3a227e_readable_reg(struct device *dev, unsigned int reg)
0115 {
0116 switch (reg) {
0117 case TS3A227E_REG_DEVICE_ID ... TS3A227E_REG_KP_THRESHOLD_3:
0118 return true;
0119 default:
0120 return false;
0121 }
0122 }
0123
0124 static bool ts3a227e_writeable_reg(struct device *dev, unsigned int reg)
0125 {
0126 switch (reg) {
0127 case TS3A227E_REG_INTERRUPT_DISABLE ... TS3A227E_REG_SWITCH_CONTROL_2:
0128 case TS3A227E_REG_KP_THRESHOLD_1 ... TS3A227E_REG_KP_THRESHOLD_3:
0129 return true;
0130 default:
0131 return false;
0132 }
0133 }
0134
0135 static bool ts3a227e_volatile_reg(struct device *dev, unsigned int reg)
0136 {
0137 switch (reg) {
0138 case TS3A227E_REG_INTERRUPT ... TS3A227E_REG_INTERRUPT_DISABLE:
0139 case TS3A227E_REG_SETTING_2:
0140 case TS3A227E_REG_SWITCH_STATUS_1 ... TS3A227E_REG_ADC_OUTPUT:
0141 return true;
0142 default:
0143 return false;
0144 }
0145 }
0146
0147 static void ts3a227e_jack_report(struct ts3a227e *ts3a227e)
0148 {
0149 unsigned int i;
0150 int report = 0;
0151
0152 if (!ts3a227e->jack)
0153 return;
0154
0155 if (ts3a227e->plugged)
0156 report = SND_JACK_HEADPHONE;
0157 if (ts3a227e->mic_present)
0158 report |= SND_JACK_MICROPHONE;
0159 for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
0160 if (ts3a227e->buttons_held & (1 << i))
0161 report |= ts3a227e_buttons[i];
0162 }
0163 snd_soc_jack_report(ts3a227e->jack, report, TS3A227E_JACK_MASK);
0164 }
0165
0166 static void ts3a227e_new_jack_state(struct ts3a227e *ts3a227e, unsigned acc_reg)
0167 {
0168 bool plugged, mic_present;
0169
0170 plugged = !!(acc_reg & JACK_INSERTED);
0171 mic_present = plugged && !!(acc_reg & EITHER_MIC_MASK);
0172
0173 ts3a227e->plugged = plugged;
0174
0175 if (mic_present != ts3a227e->mic_present) {
0176 ts3a227e->mic_present = mic_present;
0177 ts3a227e->buttons_held = 0;
0178 if (mic_present) {
0179
0180 regmap_update_bits(ts3a227e->regmap,
0181 TS3A227E_REG_SETTING_2,
0182 KP_ENABLE, KP_ENABLE);
0183 }
0184 }
0185 }
0186
0187 static irqreturn_t ts3a227e_interrupt(int irq, void *data)
0188 {
0189 struct ts3a227e *ts3a227e = (struct ts3a227e *)data;
0190 struct regmap *regmap = ts3a227e->regmap;
0191 unsigned int int_reg, kp_int_reg, acc_reg, i;
0192 struct device *dev = ts3a227e->dev;
0193 int ret;
0194
0195
0196 ret = regmap_read(regmap, TS3A227E_REG_INTERRUPT, &int_reg);
0197 if (ret) {
0198 dev_err(dev, "failed to clear interrupt ret=%d\n", ret);
0199 return IRQ_NONE;
0200 }
0201
0202 if (int_reg & (DETECTION_COMPLETE_EVENT | INS_REM_EVENT)) {
0203 regmap_read(regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
0204 ts3a227e_new_jack_state(ts3a227e, acc_reg);
0205 }
0206
0207
0208 ret = regmap_read(regmap, TS3A227E_REG_KP_INTERRUPT, &kp_int_reg);
0209 if (ret) {
0210 dev_err(dev, "failed to clear key interrupt ret=%d\n", ret);
0211 return IRQ_NONE;
0212 }
0213
0214 for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
0215 if (kp_int_reg & PRESS_MASK(i))
0216 ts3a227e->buttons_held |= (1 << i);
0217 if (kp_int_reg & RELEASE_MASK(i))
0218 ts3a227e->buttons_held &= ~(1 << i);
0219 }
0220
0221 ts3a227e_jack_report(ts3a227e);
0222
0223 return IRQ_HANDLED;
0224 }
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 int ts3a227e_enable_jack_detect(struct snd_soc_component *component,
0237 struct snd_soc_jack *jack)
0238 {
0239 struct ts3a227e *ts3a227e = snd_soc_component_get_drvdata(component);
0240
0241 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
0242 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
0243 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
0244 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
0245
0246 ts3a227e->jack = jack;
0247 ts3a227e_jack_report(ts3a227e);
0248
0249 return 0;
0250 }
0251 EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect);
0252
0253 static struct snd_soc_component_driver ts3a227e_soc_driver;
0254
0255 static const struct regmap_config ts3a227e_regmap_config = {
0256 .val_bits = 8,
0257 .reg_bits = 8,
0258
0259 .max_register = TS3A227E_REG_KP_THRESHOLD_3,
0260 .readable_reg = ts3a227e_readable_reg,
0261 .writeable_reg = ts3a227e_writeable_reg,
0262 .volatile_reg = ts3a227e_volatile_reg,
0263
0264 .cache_type = REGCACHE_RBTREE,
0265 .reg_defaults = ts3a227e_reg_defaults,
0266 .num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults),
0267 };
0268
0269 static int ts3a227e_parse_device_property(struct ts3a227e *ts3a227e,
0270 struct device *dev)
0271 {
0272 u32 micbias;
0273 int err;
0274
0275 err = device_property_read_u32(dev, "ti,micbias", &micbias);
0276 if (!err) {
0277 regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_SETTING_3,
0278 MICBIAS_SETTING_MASK,
0279 (micbias & 0x07) << MICBIAS_SETTING_SFT);
0280 }
0281
0282 return 0;
0283 }
0284
0285 static int ts3a227e_i2c_probe(struct i2c_client *i2c)
0286 {
0287 struct ts3a227e *ts3a227e;
0288 struct device *dev = &i2c->dev;
0289 int ret;
0290 unsigned int acc_reg;
0291
0292 ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL);
0293 if (ts3a227e == NULL)
0294 return -ENOMEM;
0295
0296 i2c_set_clientdata(i2c, ts3a227e);
0297 ts3a227e->dev = dev;
0298 ts3a227e->irq = i2c->irq;
0299
0300 ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config);
0301 if (IS_ERR(ts3a227e->regmap))
0302 return PTR_ERR(ts3a227e->regmap);
0303
0304 ret = ts3a227e_parse_device_property(ts3a227e, dev);
0305 if (ret) {
0306 dev_err(dev, "Failed to parse device property: %d\n", ret);
0307 return ret;
0308 }
0309
0310 ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt,
0311 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
0312 "TS3A227E", ts3a227e);
0313 if (ret) {
0314 dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret);
0315 return ret;
0316 }
0317
0318 ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver,
0319 NULL, 0);
0320 if (ret)
0321 return ret;
0322
0323
0324 regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_INTERRUPT_DISABLE,
0325 INTB_DISABLE | ADC_COMPLETE_INT_DISABLE,
0326 ADC_COMPLETE_INT_DISABLE);
0327
0328
0329 regmap_read(ts3a227e->regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
0330 ts3a227e_new_jack_state(ts3a227e, acc_reg);
0331 ts3a227e_jack_report(ts3a227e);
0332
0333 return 0;
0334 }
0335
0336 #ifdef CONFIG_PM_SLEEP
0337 static int ts3a227e_suspend(struct device *dev)
0338 {
0339 struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
0340
0341 dev_dbg(ts3a227e->dev, "suspend disable irq\n");
0342 disable_irq(ts3a227e->irq);
0343
0344 return 0;
0345 }
0346
0347 static int ts3a227e_resume(struct device *dev)
0348 {
0349 struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
0350
0351 dev_dbg(ts3a227e->dev, "resume enable irq\n");
0352 enable_irq(ts3a227e->irq);
0353
0354 return 0;
0355 }
0356 #endif
0357
0358 static const struct dev_pm_ops ts3a227e_pm = {
0359 SET_SYSTEM_SLEEP_PM_OPS(ts3a227e_suspend, ts3a227e_resume)
0360 };
0361
0362 static const struct i2c_device_id ts3a227e_i2c_ids[] = {
0363 { "ts3a227e", 0 },
0364 { }
0365 };
0366 MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids);
0367
0368 #ifdef CONFIG_OF
0369 static const struct of_device_id ts3a227e_of_match[] = {
0370 { .compatible = "ti,ts3a227e", },
0371 { }
0372 };
0373 MODULE_DEVICE_TABLE(of, ts3a227e_of_match);
0374 #endif
0375
0376 #ifdef CONFIG_ACPI
0377 static struct acpi_device_id ts3a227e_acpi_match[] = {
0378 { "104C227E", 0 },
0379 {},
0380 };
0381 MODULE_DEVICE_TABLE(acpi, ts3a227e_acpi_match);
0382 #endif
0383
0384 static struct i2c_driver ts3a227e_driver = {
0385 .driver = {
0386 .name = "ts3a227e",
0387 .pm = &ts3a227e_pm,
0388 .of_match_table = of_match_ptr(ts3a227e_of_match),
0389 .acpi_match_table = ACPI_PTR(ts3a227e_acpi_match),
0390 },
0391 .probe_new = ts3a227e_i2c_probe,
0392 .id_table = ts3a227e_i2c_ids,
0393 };
0394 module_i2c_driver(ts3a227e_driver);
0395
0396 MODULE_DESCRIPTION("ASoC ts3a227e driver");
0397 MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
0398 MODULE_LICENSE("GPL v2");