2021-01-15 Linux 耳机插入检测的防抖动过程

                         Linux 耳机插入检测的防抖动过程 

 

一、硬件连接,GPIO7_C5作为耳机检测gpio。

二、dts配置。

	es8316: es8316@10 {
		status = "okay";
	    #sound-dai-cells = <0>;
		compatible = "everest,es8316";
		reg = <0x10>;
		clocks = <&cru SCLK_I2S0_OUT>;
		clock-names = "mclk";
		pinctrl-names = "default";
		pinctrl-0 = <&i2s0_mclk>;
		spk-con-gpio = <&gpio7 3 GPIO_ACTIVE_HIGH>;
		hp-det-gpio = <&gpio7 21 GPIO_ACTIVE_LOW>;//GPIO7_C5 HP_DET

 

三、注册gpio中断以及延迟的工作队列INIT_DELAYED_WORK,设置抖动延时时间debounce_time = 200;。

struct es8316_priv {
	struct regmap *regmap;
	unsigned int dmic_amic;
	unsigned int sysclk;
	struct snd_pcm_hw_constraint_list *sysclk_constraints;
	struct clk *mclk;
	int debounce_time;
	int hp_det_invert;
	struct delayed_work work;

	int spk_ctl_gpio;
	int hp_det_gpio;
	bool muted;
	bool hp_inserted;
	bool spk_active_level;

	int pwr_count;
};
static int es8316_i2c_probe(struct i2c_client *i2c,
			    const struct i2c_device_id *id)
{
	struct es8316_priv *es8316;
	int ret = -1;
	int hp_irq;
	enum of_gpio_flags flags;
	struct device_node *np = i2c->dev.of_node;

	es8316 = devm_kzalloc(&i2c->dev, sizeof(*es8316), GFP_KERNEL);
	if (!es8316)
		return -ENOMEM;

	es8316->debounce_time = 200;
	es8316->hp_det_invert = 0;
	es8316->pwr_count = 0;
	es8316->hp_inserted = false;
	es8316->muted = true;

	es8316->regmap = devm_regmap_init_i2c(i2c, &es8316_regmap_config);
	if (IS_ERR(es8316->regmap)) {
		ret = PTR_ERR(es8316->regmap);
		dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
		return ret;
	}

	i2c_set_clientdata(i2c, es8316);
es8316->spk_ctl_gpio = of_get_named_gpio_flags(np,
						       "spk-con-gpio",
						       0,
						       &flags);
	if (es8316->spk_ctl_gpio < 0) {
		dev_info(&i2c->dev, "Can not read property spk_ctl_gpio\n");
		es8316->spk_ctl_gpio = INVALID_GPIO;
	} else {
		es8316->spk_active_level = !(flags & OF_GPIO_ACTIVE_LOW);
		ret = devm_gpio_request_one(&i2c->dev, es8316->spk_ctl_gpio,
					    GPIOF_DIR_OUT, NULL);
		if (ret) {
			dev_err(&i2c->dev, "Failed to request spk_ctl_gpio\n");
			return ret;
		}
		es8316_enable_spk(es8316, false);
	}

	es8316->hp_det_gpio = of_get_named_gpio_flags(np,
						      "hp-det-gpio",
						      0,
						      &flags);
	if (es8316->hp_det_gpio < 0) {
		dev_info(&i2c->dev, "Can not read property hp_det_gpio\n");
		es8316->hp_det_gpio = INVALID_GPIO;
	} else {
INIT_DELAYED_WORK(&es8316->work, hp_work);
		es8316->hp_det_invert = !!(flags & OF_GPIO_ACTIVE_LOW);
		ret = devm_gpio_request_one(&i2c->dev, es8316->hp_det_gpio,
					    GPIOF_IN, "hp det");
		if (ret < 0)
			return ret;
		hp_irq = gpio_to_irq(es8316->hp_det_gpio);
		ret = devm_request_threaded_irq(&i2c->dev, hp_irq, NULL,
						es8316_irq_handler,
						IRQF_TRIGGER_FALLING |
						IRQF_TRIGGER_RISING |
						IRQF_ONESHOT,
						"es8316_interrupt", es8316);
		if (ret < 0) {
			dev_err(&i2c->dev, "request_irq failed: %d\n", ret);
			return ret;
		}

		schedule_delayed_work(&es8316->work,
				      msecs_to_jiffies(es8316->debounce_time));
	}

	ret = snd_soc_register_codec(&i2c->dev,
				     &soc_codec_dev_es8316,
				     &es8316_dai, 1);

	return ret;
}

四、中断处理函数。

static irqreturn_t es8316_irq_handler(int irq, void *data)
{
	struct es8316_priv *es8316 = data;

	queue_delayed_work(system_power_efficient_wq, &es8316->work,
			   msecs_to_jiffies(es8316->debounce_time));

	return IRQ_HANDLED;
}

五、队列处理函数,enable 喇叭或者disable喇叭。

static void hp_work(struct work_struct *work)
{
	struct es8316_priv *es8316;
	int enable;

	es8316 = container_of(work, struct es8316_priv, work.work);
	enable = gpio_get_value(es8316->hp_det_gpio);
	if (es8316->hp_det_invert)
		enable = !enable;

	es8316->hp_inserted = enable ? true : false;
	if (!es8316->muted) {
		if (es8316->hp_inserted)
			es8316_enable_spk(es8316, false);
		else
			es8316_enable_spk(es8316, true);
	}
}

六、cat /proc/interrupts 查看gpio中断是否注册成功。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值