cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

ts3a227e.c (10533B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * TS3A227E Autonomous Audio Accessory Detection and Configuration Switch
      4 *
      5 * Copyright (C) 2014 Google, Inc.
      6 */
      7
      8#include <linux/gpio.h>
      9#include <linux/i2c.h>
     10#include <linux/init.h>
     11#include <linux/input.h>
     12#include <linux/module.h>
     13#include <linux/of_gpio.h>
     14#include <linux/regmap.h>
     15#include <linux/acpi.h>
     16
     17#include <sound/core.h>
     18#include <sound/jack.h>
     19#include <sound/soc.h>
     20
     21#include "ts3a227e.h"
     22
     23struct ts3a227e {
     24	struct device *dev;
     25	struct regmap *regmap;
     26	struct snd_soc_jack *jack;
     27	bool plugged;
     28	bool mic_present;
     29	unsigned int buttons_held;
     30	int irq;
     31};
     32
     33/* Button values to be reported on the jack */
     34static const int ts3a227e_buttons[] = {
     35	SND_JACK_BTN_0,
     36	SND_JACK_BTN_1,
     37	SND_JACK_BTN_2,
     38	SND_JACK_BTN_3,
     39};
     40
     41#define TS3A227E_NUM_BUTTONS 4
     42#define TS3A227E_JACK_MASK (SND_JACK_HEADPHONE | \
     43			    SND_JACK_MICROPHONE | \
     44			    SND_JACK_BTN_0 | \
     45			    SND_JACK_BTN_1 | \
     46			    SND_JACK_BTN_2 | \
     47			    SND_JACK_BTN_3)
     48
     49/* TS3A227E registers */
     50#define TS3A227E_REG_DEVICE_ID		0x00
     51#define TS3A227E_REG_INTERRUPT		0x01
     52#define TS3A227E_REG_KP_INTERRUPT	0x02
     53#define TS3A227E_REG_INTERRUPT_DISABLE	0x03
     54#define TS3A227E_REG_SETTING_1		0x04
     55#define TS3A227E_REG_SETTING_2		0x05
     56#define TS3A227E_REG_SETTING_3		0x06
     57#define TS3A227E_REG_SWITCH_CONTROL_1	0x07
     58#define TS3A227E_REG_SWITCH_CONTROL_2	0x08
     59#define TS3A227E_REG_SWITCH_STATUS_1	0x09
     60#define TS3A227E_REG_SWITCH_STATUS_2	0x0a
     61#define TS3A227E_REG_ACCESSORY_STATUS	0x0b
     62#define TS3A227E_REG_ADC_OUTPUT		0x0c
     63#define TS3A227E_REG_KP_THRESHOLD_1	0x0d
     64#define TS3A227E_REG_KP_THRESHOLD_2	0x0e
     65#define TS3A227E_REG_KP_THRESHOLD_3	0x0f
     66
     67/* TS3A227E_REG_INTERRUPT 0x01 */
     68#define INS_REM_EVENT 0x01
     69#define DETECTION_COMPLETE_EVENT 0x02
     70
     71/* TS3A227E_REG_KP_INTERRUPT 0x02 */
     72#define PRESS_MASK(idx) (0x01 << (2 * (idx)))
     73#define RELEASE_MASK(idx) (0x02 << (2 * (idx)))
     74
     75/* TS3A227E_REG_INTERRUPT_DISABLE 0x03 */
     76#define INS_REM_INT_DISABLE 0x01
     77#define DETECTION_COMPLETE_INT_DISABLE 0x02
     78#define ADC_COMPLETE_INT_DISABLE 0x04
     79#define INTB_DISABLE 0x08
     80
     81/* TS3A227E_REG_SETTING_2 0x05 */
     82#define KP_ENABLE 0x04
     83
     84/* TS3A227E_REG_SETTING_3 0x06 */
     85#define MICBIAS_SETTING_SFT (3)
     86#define MICBIAS_SETTING_MASK (0x7 << MICBIAS_SETTING_SFT)
     87
     88/* TS3A227E_REG_ACCESSORY_STATUS  0x0b */
     89#define TYPE_3_POLE 0x01
     90#define TYPE_4_POLE_OMTP 0x02
     91#define TYPE_4_POLE_STANDARD 0x04
     92#define JACK_INSERTED 0x08
     93#define EITHER_MIC_MASK (TYPE_4_POLE_OMTP | TYPE_4_POLE_STANDARD)
     94
     95static const struct reg_default ts3a227e_reg_defaults[] = {
     96	{ TS3A227E_REG_DEVICE_ID, 0x10 },
     97	{ TS3A227E_REG_INTERRUPT, 0x00 },
     98	{ TS3A227E_REG_KP_INTERRUPT, 0x00 },
     99	{ TS3A227E_REG_INTERRUPT_DISABLE, 0x08 },
    100	{ TS3A227E_REG_SETTING_1, 0x23 },
    101	{ TS3A227E_REG_SETTING_2, 0x00 },
    102	{ TS3A227E_REG_SETTING_3, 0x0e },
    103	{ TS3A227E_REG_SWITCH_CONTROL_1, 0x00 },
    104	{ TS3A227E_REG_SWITCH_CONTROL_2, 0x00 },
    105	{ TS3A227E_REG_SWITCH_STATUS_1, 0x0c },
    106	{ TS3A227E_REG_SWITCH_STATUS_2, 0x00 },
    107	{ TS3A227E_REG_ACCESSORY_STATUS, 0x00 },
    108	{ TS3A227E_REG_ADC_OUTPUT, 0x00 },
    109	{ TS3A227E_REG_KP_THRESHOLD_1, 0x20 },
    110	{ TS3A227E_REG_KP_THRESHOLD_2, 0x40 },
    111	{ TS3A227E_REG_KP_THRESHOLD_3, 0x68 },
    112};
    113
    114static bool ts3a227e_readable_reg(struct device *dev, unsigned int reg)
    115{
    116	switch (reg) {
    117	case TS3A227E_REG_DEVICE_ID ... TS3A227E_REG_KP_THRESHOLD_3:
    118		return true;
    119	default:
    120		return false;
    121	}
    122}
    123
    124static bool ts3a227e_writeable_reg(struct device *dev, unsigned int reg)
    125{
    126	switch (reg) {
    127	case TS3A227E_REG_INTERRUPT_DISABLE ... TS3A227E_REG_SWITCH_CONTROL_2:
    128	case TS3A227E_REG_KP_THRESHOLD_1 ... TS3A227E_REG_KP_THRESHOLD_3:
    129		return true;
    130	default:
    131		return false;
    132	}
    133}
    134
    135static bool ts3a227e_volatile_reg(struct device *dev, unsigned int reg)
    136{
    137	switch (reg) {
    138	case TS3A227E_REG_INTERRUPT ... TS3A227E_REG_INTERRUPT_DISABLE:
    139	case TS3A227E_REG_SETTING_2:
    140	case TS3A227E_REG_SWITCH_STATUS_1 ... TS3A227E_REG_ADC_OUTPUT:
    141		return true;
    142	default:
    143		return false;
    144	}
    145}
    146
    147static void ts3a227e_jack_report(struct ts3a227e *ts3a227e)
    148{
    149	unsigned int i;
    150	int report = 0;
    151
    152	if (!ts3a227e->jack)
    153		return;
    154
    155	if (ts3a227e->plugged)
    156		report = SND_JACK_HEADPHONE;
    157	if (ts3a227e->mic_present)
    158		report |= SND_JACK_MICROPHONE;
    159	for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
    160		if (ts3a227e->buttons_held & (1 << i))
    161			report |= ts3a227e_buttons[i];
    162	}
    163	snd_soc_jack_report(ts3a227e->jack, report, TS3A227E_JACK_MASK);
    164}
    165
    166static void ts3a227e_new_jack_state(struct ts3a227e *ts3a227e, unsigned acc_reg)
    167{
    168	bool plugged, mic_present;
    169
    170	plugged = !!(acc_reg & JACK_INSERTED);
    171	mic_present = plugged && !!(acc_reg & EITHER_MIC_MASK);
    172
    173	ts3a227e->plugged = plugged;
    174
    175	if (mic_present != ts3a227e->mic_present) {
    176		ts3a227e->mic_present = mic_present;
    177		ts3a227e->buttons_held = 0;
    178		if (mic_present) {
    179			/* Enable key press detection. */
    180			regmap_update_bits(ts3a227e->regmap,
    181					   TS3A227E_REG_SETTING_2,
    182					   KP_ENABLE, KP_ENABLE);
    183		}
    184	}
    185}
    186
    187static irqreturn_t ts3a227e_interrupt(int irq, void *data)
    188{
    189	struct ts3a227e *ts3a227e = (struct ts3a227e *)data;
    190	struct regmap *regmap = ts3a227e->regmap;
    191	unsigned int int_reg, kp_int_reg, acc_reg, i;
    192	struct device *dev = ts3a227e->dev;
    193	int ret;
    194
    195	/* Check for plug/unplug. */
    196	ret = regmap_read(regmap, TS3A227E_REG_INTERRUPT, &int_reg);
    197	if (ret) {
    198		dev_err(dev, "failed to clear interrupt ret=%d\n", ret);
    199		return IRQ_NONE;
    200	}
    201
    202	if (int_reg & (DETECTION_COMPLETE_EVENT | INS_REM_EVENT)) {
    203		regmap_read(regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
    204		ts3a227e_new_jack_state(ts3a227e, acc_reg);
    205	}
    206
    207	/* Report any key events. */
    208	ret = regmap_read(regmap, TS3A227E_REG_KP_INTERRUPT, &kp_int_reg);
    209	if (ret) {
    210		dev_err(dev, "failed to clear key interrupt ret=%d\n", ret);
    211		return IRQ_NONE;
    212	}
    213
    214	for (i = 0; i < TS3A227E_NUM_BUTTONS; i++) {
    215		if (kp_int_reg & PRESS_MASK(i))
    216			ts3a227e->buttons_held |= (1 << i);
    217		if (kp_int_reg & RELEASE_MASK(i))
    218			ts3a227e->buttons_held &= ~(1 << i);
    219	}
    220
    221	ts3a227e_jack_report(ts3a227e);
    222
    223	return IRQ_HANDLED;
    224}
    225
    226/**
    227 * ts3a227e_enable_jack_detect - Specify a jack for event reporting
    228 *
    229 * @component:  component to register the jack with
    230 * @jack: jack to use to report headset and button events on
    231 *
    232 * After this function has been called the headset insert/remove and button
    233 * events 0-3 will be routed to the given jack.  Jack can be null to stop
    234 * reporting.
    235 */
    236int ts3a227e_enable_jack_detect(struct snd_soc_component *component,
    237				struct snd_soc_jack *jack)
    238{
    239	struct ts3a227e *ts3a227e = snd_soc_component_get_drvdata(component);
    240
    241	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
    242	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
    243	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
    244	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
    245
    246	ts3a227e->jack = jack;
    247	ts3a227e_jack_report(ts3a227e);
    248
    249	return 0;
    250}
    251EXPORT_SYMBOL_GPL(ts3a227e_enable_jack_detect);
    252
    253static struct snd_soc_component_driver ts3a227e_soc_driver;
    254
    255static const struct regmap_config ts3a227e_regmap_config = {
    256	.val_bits = 8,
    257	.reg_bits = 8,
    258
    259	.max_register = TS3A227E_REG_KP_THRESHOLD_3,
    260	.readable_reg = ts3a227e_readable_reg,
    261	.writeable_reg = ts3a227e_writeable_reg,
    262	.volatile_reg = ts3a227e_volatile_reg,
    263
    264	.cache_type = REGCACHE_RBTREE,
    265	.reg_defaults = ts3a227e_reg_defaults,
    266	.num_reg_defaults = ARRAY_SIZE(ts3a227e_reg_defaults),
    267};
    268
    269static int ts3a227e_parse_device_property(struct ts3a227e *ts3a227e,
    270				struct device *dev)
    271{
    272	u32 micbias;
    273	int err;
    274
    275	err = device_property_read_u32(dev, "ti,micbias", &micbias);
    276	if (!err) {
    277		regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_SETTING_3,
    278			MICBIAS_SETTING_MASK,
    279			(micbias & 0x07) << MICBIAS_SETTING_SFT);
    280	}
    281
    282	return 0;
    283}
    284
    285static int ts3a227e_i2c_probe(struct i2c_client *i2c)
    286{
    287	struct ts3a227e *ts3a227e;
    288	struct device *dev = &i2c->dev;
    289	int ret;
    290	unsigned int acc_reg;
    291
    292	ts3a227e = devm_kzalloc(&i2c->dev, sizeof(*ts3a227e), GFP_KERNEL);
    293	if (ts3a227e == NULL)
    294		return -ENOMEM;
    295
    296	i2c_set_clientdata(i2c, ts3a227e);
    297	ts3a227e->dev = dev;
    298	ts3a227e->irq = i2c->irq;
    299
    300	ts3a227e->regmap = devm_regmap_init_i2c(i2c, &ts3a227e_regmap_config);
    301	if (IS_ERR(ts3a227e->regmap))
    302		return PTR_ERR(ts3a227e->regmap);
    303
    304	ret = ts3a227e_parse_device_property(ts3a227e, dev);
    305	if (ret) {
    306		dev_err(dev, "Failed to parse device property: %d\n", ret);
    307		return ret;
    308	}
    309
    310	ret = devm_request_threaded_irq(dev, i2c->irq, NULL, ts3a227e_interrupt,
    311					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
    312					"TS3A227E", ts3a227e);
    313	if (ret) {
    314		dev_err(dev, "Cannot request irq %d (%d)\n", i2c->irq, ret);
    315		return ret;
    316	}
    317
    318	ret = devm_snd_soc_register_component(&i2c->dev, &ts3a227e_soc_driver,
    319					      NULL, 0);
    320	if (ret)
    321		return ret;
    322
    323	/* Enable interrupts except for ADC complete. */
    324	regmap_update_bits(ts3a227e->regmap, TS3A227E_REG_INTERRUPT_DISABLE,
    325			   INTB_DISABLE | ADC_COMPLETE_INT_DISABLE,
    326			   ADC_COMPLETE_INT_DISABLE);
    327
    328	/* Read jack status because chip might not trigger interrupt at boot. */
    329	regmap_read(ts3a227e->regmap, TS3A227E_REG_ACCESSORY_STATUS, &acc_reg);
    330	ts3a227e_new_jack_state(ts3a227e, acc_reg);
    331	ts3a227e_jack_report(ts3a227e);
    332
    333	return 0;
    334}
    335
    336#ifdef CONFIG_PM_SLEEP
    337static int ts3a227e_suspend(struct device *dev)
    338{
    339	struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
    340
    341	dev_dbg(ts3a227e->dev, "suspend disable irq\n");
    342	disable_irq(ts3a227e->irq);
    343
    344	return 0;
    345}
    346
    347static int ts3a227e_resume(struct device *dev)
    348{
    349	struct ts3a227e *ts3a227e = dev_get_drvdata(dev);
    350
    351	dev_dbg(ts3a227e->dev, "resume enable irq\n");
    352	enable_irq(ts3a227e->irq);
    353
    354	return 0;
    355}
    356#endif
    357
    358static const struct dev_pm_ops ts3a227e_pm = {
    359	SET_SYSTEM_SLEEP_PM_OPS(ts3a227e_suspend, ts3a227e_resume)
    360};
    361
    362static const struct i2c_device_id ts3a227e_i2c_ids[] = {
    363	{ "ts3a227e", 0 },
    364	{ }
    365};
    366MODULE_DEVICE_TABLE(i2c, ts3a227e_i2c_ids);
    367
    368#ifdef CONFIG_OF
    369static const struct of_device_id ts3a227e_of_match[] = {
    370	{ .compatible = "ti,ts3a227e", },
    371	{ }
    372};
    373MODULE_DEVICE_TABLE(of, ts3a227e_of_match);
    374#endif
    375
    376#ifdef CONFIG_ACPI
    377static struct acpi_device_id ts3a227e_acpi_match[] = {
    378	{ "104C227E", 0 },
    379	{},
    380};
    381MODULE_DEVICE_TABLE(acpi, ts3a227e_acpi_match);
    382#endif
    383
    384static struct i2c_driver ts3a227e_driver = {
    385	.driver = {
    386		.name = "ts3a227e",
    387		.pm = &ts3a227e_pm,
    388		.of_match_table = of_match_ptr(ts3a227e_of_match),
    389		.acpi_match_table = ACPI_PTR(ts3a227e_acpi_match),
    390	},
    391	.probe_new = ts3a227e_i2c_probe,
    392	.id_table = ts3a227e_i2c_ids,
    393};
    394module_i2c_driver(ts3a227e_driver);
    395
    396MODULE_DESCRIPTION("ASoC ts3a227e driver");
    397MODULE_AUTHOR("Dylan Reid <dgreid@chromium.org>");
    398MODULE_LICENSE("GPL v2");