// SPDX-License-Identifier: GPL-2.0-only
//
// rt722-sdca.c -- rt722 SDCA ALSA SoC audio driver
//
// Copyright(c) 2023 Realtek Semiconductor Corp.
//
//
#include <linux/bitops.h>
#include <sound/core.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <sound/initval.h>
#include <sound/jack.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <sound/pcm.h>
#include <linux/pm_runtime.h>
#include <sound/pcm_params.h>
#include <linux/soundwire/sdw_registers.h>
#include <linux/slab.h>
#include <sound/soc-dapm.h>
#include <sound/tlv.h>
#include "rt722-sdca.h"
int rt722_sdca_index_write(struct rt722_sdca_priv *rt722,
unsigned int nid, unsigned int reg, unsigned int value)
{
struct regmap *regmap = rt722->mbq_regmap;
unsigned int addr = (nid << 20) | reg;
int ret;
ret = regmap_write(regmap, addr, value);
if (ret < 0)
dev_err(&rt722->slave->dev,
"%s: Failed to set private value: %06x <= %04x ret=%d\n",
__func__, addr, value, ret);
return ret;
}
int rt722_sdca_index_read(struct rt722_sdca_priv *rt722,
unsigned int nid, unsigned int reg, unsigned int *value)
{
int ret;
struct regmap *regmap = rt722->mbq_regmap;
unsigned int addr = (nid << 20) | reg;
ret = regmap_read(regmap, addr, value);
if (ret < 0)
dev_err(&rt722->slave->dev,
"%s: Failed to get private value: %06x => %04x ret=%d\n",
__func__, addr, *value, ret);
return ret;
}
static int rt722_sdca_index_update_bits(struct rt722_sdca_priv *rt722,
unsigned int nid, unsigned int reg, unsigned int mask, unsigned int val)
{
unsigned int tmp;
int ret;
ret = rt722_sdca_index_read(rt722, nid, reg, &tmp);
if (ret < 0)
return ret;
set_mask_bits(&tmp, mask, val);
return rt722_sdca_index_write(rt722, nid, reg, tmp);
}
static int rt722_sdca_btn_type(unsigned char *buffer)
{
if ((*buffer & 0xf0) == 0x10 || (*buffer & 0x0f) == 0x01 || (*(buffer + 1) == 0x01) ||
(*(buffer + 1) == 0x10))
return SND_JACK_BTN_2;
else if ((*buffer & 0xf0) == 0x20 || (*buffer & 0x0f) == 0x02 || (*(buffer + 1) == 0x02) ||
(*(buffer + 1) == 0x20))
return SND_JACK_BTN_3;
else if ((*buffer & 0xf0) == 0x40 || (*buffer & 0x0f) == 0x04 || (*(buffer + 1) == 0x04) ||
(*(buffer + 1) == 0x40))
return SND_JACK_BTN_0;
else if ((*buffer & 0xf0) == 0x80 || (*buffer & 0x0f) == 0x08 || (*(buffer + 1) == 0x08) ||
(*(buffer + 1) == 0x80))
return SND_JACK_BTN_1;
return 0;
}
static unsigned int rt722_sdca_button_detect(struct rt722_sdca_priv *rt722)
{
unsigned int btn_type = 0, offset, idx, val, owner;
int ret;
unsigned char buf[3];
/* get current UMP message owner */
ret = regmap_read(rt722->regmap,
SDW_SDCA_CTL(FUNC_NUM_HID, RT722_SDCA_ENT_HID01,
RT722_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), &owner);
if (ret < 0)
return 0;
/* if owner is device then there is no button event from device */
if (owner == 1)
return 0;
/* read UMP message offset */
ret = regmap_read(rt722->regmap,
SDW_SDCA_CTL(FUNC_NUM_HID, RT722_SDCA_ENT_HID01,
RT722_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0), &offset);
if (ret < 0)
goto _end_btn_det_;
for (idx = 0; idx < sizeof(buf); idx++) {
ret = regmap_read(rt722->regmap,
RT722_BUF_ADDR_HID1 + offset + idx, &val);
if (ret < 0)
goto _end_btn_det_;
buf[idx] = val & 0xff;
}
if (buf[0] == 0x11)
btn_type = rt722_sdca_btn_type(&buf[1]);
_end_btn_det_:
/* Host is owner, so set back to device */
if (owner == 0)
/* set owner to device */
regmap_write(rt722->regmap,
SDW_SDCA_CTL(FUNC_NUM_HID, RT722_SDCA_ENT_HID01,
RT722_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), 0x01);
return btn_type;
}
static int rt722_sdca_headset_detect(struct rt722_sdca_priv *rt722)
{
unsigned int det_mode;
int ret;
/* get detected_mode */
ret = regmap_read(rt722->regmap,
SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_GE49,
RT722_SDCA_CTL_DETECTED_MODE, 0), &det_mode);
if (ret < 0)
goto io_error;
switch (det_mode) {
case 0x00:
rt722->jack_type = 0;
break;
case 0x03:
rt722->jack_type = SND_JACK_HEADPHONE;
break;
case 0x05:
rt722->jack_type = SND_JACK_HEADSET;
break;
}
/* write selected_mode */
if (det_mode) {
ret = regmap_write(rt722->regmap,
SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_GE49,
RT722_SDCA_CTL_SELECTED_MODE, 0), det_mode);
if (ret < 0)
goto io_error;
}
dev_dbg(&rt722->slave->dev,
"%s, detected_mode=0x%x\n", __func__, det_mode);
return 0;
io_error:
pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
return ret;
}
static void rt722_sdca_jack_detect_handler(struct work_struct *work)
{
struct rt722_sdca_priv *rt722 =
container_of(work, struct rt722_sdca_priv, jack_detect_work.work);
int btn_type = 0, ret;
if (!rt722->hs_jack)
return;
if (!rt722->component->card || !rt722->component->card->instantiated)
return;
/* SDW_SCP_SDCA_INT_SDCA_0 is used for jack detection */
if (rt722->scp_sdca_stat1 & SDW_SCP_SDCA_INT_SDCA_0) {
ret = rt722_sdca_headset_detect(rt722);
if (ret < 0)
return;
}
/* SDW_SCP_SDCA_INT_SDCA_8 is used for button detection */
if (rt722->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8)
btn_type = rt722_sdca_button_detect(rt722);
if (rt722->jack_type == 0)
btn_type = 0;
dev_dbg(&rt722->slave->dev,
"in %s, jack_type=%d\n", __func__, rt722->jack_type);
dev_dbg(&rt722->slave->dev,
"in %s, btn_type=0x%x\n", __func__, btn_type);
dev_dbg(&rt722->slave->dev,
"in %s, scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__,
rt722->scp_sdca_stat1, rt722->scp_sdca_stat2);
snd_soc_jack_report(rt722->hs_jack, rt722->jack_
|