Commit 8f1a27bb authored by Mark Brown's avatar Mark Brown

ASOC: cs42l42: Add support for system suspend

Merge series from Richard Fitzgerald <rf@opensource.cirrus.com>:

Add system suspend and resume handlers so that the cs42l42 is cleanly
put into power-off state during system suspend and the registers are
restored in resume.
parents a567abf5 f8593e88
...@@ -550,7 +550,7 @@ static int cs42l42_set_jack(struct snd_soc_component *component, struct snd_soc_ ...@@ -550,7 +550,7 @@ static int cs42l42_set_jack(struct snd_soc_component *component, struct snd_soc_
struct cs42l42_private *cs42l42 = snd_soc_component_get_drvdata(component); struct cs42l42_private *cs42l42 = snd_soc_component_get_drvdata(component);
/* Prevent race with interrupt handler */ /* Prevent race with interrupt handler */
mutex_lock(&cs42l42->jack_detect_mutex); mutex_lock(&cs42l42->irq_lock);
cs42l42->jack = jk; cs42l42->jack = jk;
if (jk) { if (jk) {
...@@ -566,7 +566,7 @@ static int cs42l42_set_jack(struct snd_soc_component *component, struct snd_soc_ ...@@ -566,7 +566,7 @@ static int cs42l42_set_jack(struct snd_soc_component *component, struct snd_soc_
break; break;
} }
} }
mutex_unlock(&cs42l42->jack_detect_mutex); mutex_unlock(&cs42l42->irq_lock);
return 0; return 0;
} }
...@@ -1613,6 +1613,11 @@ static irqreturn_t cs42l42_irq_thread(int irq, void *data) ...@@ -1613,6 +1613,11 @@ static irqreturn_t cs42l42_irq_thread(int irq, void *data)
unsigned int i; unsigned int i;
int report = 0; int report = 0;
mutex_lock(&cs42l42->irq_lock);
if (cs42l42->suspended) {
mutex_unlock(&cs42l42->irq_lock);
return IRQ_NONE;
}
/* Read sticky registers to clear interurpt */ /* Read sticky registers to clear interurpt */
for (i = 0; i < ARRAY_SIZE(stickies); i++) { for (i = 0; i < ARRAY_SIZE(stickies); i++) {
...@@ -1635,9 +1640,11 @@ static irqreturn_t cs42l42_irq_thread(int irq, void *data) ...@@ -1635,9 +1640,11 @@ static irqreturn_t cs42l42_irq_thread(int irq, void *data)
CS42L42_M_DETECT_FT_MASK | CS42L42_M_DETECT_FT_MASK |
CS42L42_M_HSBIAS_HIZ_MASK); CS42L42_M_HSBIAS_HIZ_MASK);
mutex_lock(&cs42l42->jack_detect_mutex); /*
* Check auto-detect status. Don't assume a previous unplug event has
/* Check auto-detect status */ * cleared the flags. If the jack is unplugged and plugged during
* system suspend there won't have been an unplug event.
*/
if ((~masks[5]) & irq_params_table[5].mask) { if ((~masks[5]) & irq_params_table[5].mask) {
if (stickies[5] & CS42L42_HSDET_AUTO_DONE_MASK) { if (stickies[5] & CS42L42_HSDET_AUTO_DONE_MASK) {
cs42l42_process_hs_type_detect(cs42l42); cs42l42_process_hs_type_detect(cs42l42);
...@@ -1645,11 +1652,15 @@ static irqreturn_t cs42l42_irq_thread(int irq, void *data) ...@@ -1645,11 +1652,15 @@ static irqreturn_t cs42l42_irq_thread(int irq, void *data)
case CS42L42_PLUG_CTIA: case CS42L42_PLUG_CTIA:
case CS42L42_PLUG_OMTP: case CS42L42_PLUG_OMTP:
snd_soc_jack_report(cs42l42->jack, SND_JACK_HEADSET, snd_soc_jack_report(cs42l42->jack, SND_JACK_HEADSET,
SND_JACK_HEADSET); SND_JACK_HEADSET |
SND_JACK_BTN_0 | SND_JACK_BTN_1 |
SND_JACK_BTN_2 | SND_JACK_BTN_3);
break; break;
case CS42L42_PLUG_HEADPHONE: case CS42L42_PLUG_HEADPHONE:
snd_soc_jack_report(cs42l42->jack, SND_JACK_HEADPHONE, snd_soc_jack_report(cs42l42->jack, SND_JACK_HEADPHONE,
SND_JACK_HEADPHONE); SND_JACK_HEADSET |
SND_JACK_BTN_0 | SND_JACK_BTN_1 |
SND_JACK_BTN_2 | SND_JACK_BTN_3);
break; break;
default: default:
break; break;
...@@ -1705,7 +1716,7 @@ static irqreturn_t cs42l42_irq_thread(int irq, void *data) ...@@ -1705,7 +1716,7 @@ static irqreturn_t cs42l42_irq_thread(int irq, void *data)
} }
} }
mutex_unlock(&cs42l42->jack_detect_mutex); mutex_unlock(&cs42l42->irq_lock);
return IRQ_HANDLED; return IRQ_HANDLED;
} }
...@@ -2040,6 +2051,138 @@ static int cs42l42_handle_device_data(struct device *dev, ...@@ -2040,6 +2051,138 @@ static int cs42l42_handle_device_data(struct device *dev,
return 0; return 0;
} }
/* Datasheet suspend sequence */
static const struct reg_sequence __maybe_unused cs42l42_shutdown_seq[] = {
REG_SEQ0(CS42L42_MIC_DET_CTL1, 0x9F),
REG_SEQ0(CS42L42_ADC_OVFL_INT_MASK, 0x01),
REG_SEQ0(CS42L42_MIXER_INT_MASK, 0x0F),
REG_SEQ0(CS42L42_SRC_INT_MASK, 0x0F),
REG_SEQ0(CS42L42_ASP_RX_INT_MASK, 0x1F),
REG_SEQ0(CS42L42_ASP_TX_INT_MASK, 0x0F),
REG_SEQ0(CS42L42_CODEC_INT_MASK, 0x03),
REG_SEQ0(CS42L42_SRCPL_INT_MASK, 0x7F),
REG_SEQ0(CS42L42_VPMON_INT_MASK, 0x01),
REG_SEQ0(CS42L42_PLL_LOCK_INT_MASK, 0x01),
REG_SEQ0(CS42L42_TSRS_PLUG_INT_MASK, 0x0F),
REG_SEQ0(CS42L42_WAKE_CTL, 0xE1),
REG_SEQ0(CS42L42_DET_INT1_MASK, 0xE0),
REG_SEQ0(CS42L42_DET_INT2_MASK, 0xFF),
REG_SEQ0(CS42L42_MIXER_CHA_VOL, 0x3F),
REG_SEQ0(CS42L42_MIXER_ADC_VOL, 0x3F),
REG_SEQ0(CS42L42_MIXER_CHB_VOL, 0x3F),
REG_SEQ0(CS42L42_HP_CTL, 0x0F),
REG_SEQ0(CS42L42_ASP_RX_DAI0_EN, 0x00),
REG_SEQ0(CS42L42_ASP_CLK_CFG, 0x00),
REG_SEQ0(CS42L42_HSDET_CTL2, 0x00),
REG_SEQ0(CS42L42_PWR_CTL1, 0xFE),
REG_SEQ0(CS42L42_PWR_CTL2, 0x8C),
REG_SEQ0(CS42L42_DAC_CTL2, 0x02),
REG_SEQ0(CS42L42_HS_CLAMP_DISABLE, 0x00),
REG_SEQ0(CS42L42_MISC_DET_CTL, 0x03),
REG_SEQ0(CS42L42_TIPSENSE_CTL, 0x02),
REG_SEQ0(CS42L42_HSBIAS_SC_AUTOCTL, 0x03),
REG_SEQ0(CS42L42_PWR_CTL1, 0xFF)
};
static int __maybe_unused cs42l42_suspend(struct device *dev)
{
struct cs42l42_private *cs42l42 = dev_get_drvdata(dev);
unsigned int reg;
u8 save_regs[ARRAY_SIZE(cs42l42_shutdown_seq)];
int i, ret;
/*
* Wait for threaded irq handler to be idle and stop it processing
* future interrupts. This ensures a safe disable if the interrupt
* is shared.
*/
mutex_lock(&cs42l42->irq_lock);
cs42l42->suspended = true;
/* Save register values that will be overwritten by shutdown sequence */
for (i = 0; i < ARRAY_SIZE(cs42l42_shutdown_seq); ++i) {
regmap_read(cs42l42->regmap, cs42l42_shutdown_seq[i].reg, &reg);
save_regs[i] = (u8)reg;
}
/* Shutdown codec */
regmap_multi_reg_write(cs42l42->regmap,
cs42l42_shutdown_seq,
ARRAY_SIZE(cs42l42_shutdown_seq));
/* All interrupt sources are now disabled */
mutex_unlock(&cs42l42->irq_lock);
/* Wait for power-down complete */
msleep(CS42L42_PDN_DONE_TIME_MS);
ret = regmap_read_poll_timeout(cs42l42->regmap,
CS42L42_CODEC_STATUS, reg,
(reg & CS42L42_PDN_DONE_MASK),
CS42L42_PDN_DONE_POLL_US,
CS42L42_PDN_DONE_TIMEOUT_US);
if (ret)
dev_warn(dev, "Failed to get PDN_DONE: %d\n", ret);
/* Discharge FILT+ */
regmap_update_bits(cs42l42->regmap, CS42L42_PWR_CTL2,
CS42L42_DISCHARGE_FILT_MASK, CS42L42_DISCHARGE_FILT_MASK);
msleep(CS42L42_FILT_DISCHARGE_TIME_MS);
regcache_cache_only(cs42l42->regmap, true);
gpiod_set_value_cansleep(cs42l42->reset_gpio, 0);
regulator_bulk_disable(ARRAY_SIZE(cs42l42->supplies), cs42l42->supplies);
/* Restore register values to the regmap cache */
for (i = 0; i < ARRAY_SIZE(cs42l42_shutdown_seq); ++i)
regmap_write(cs42l42->regmap, cs42l42_shutdown_seq[i].reg, save_regs[i]);
/* The cached address page register value is now stale */
regcache_drop_region(cs42l42->regmap, CS42L42_PAGE_REGISTER, CS42L42_PAGE_REGISTER);
dev_dbg(dev, "System suspended\n");
return 0;
}
static int __maybe_unused cs42l42_resume(struct device *dev)
{
struct cs42l42_private *cs42l42 = dev_get_drvdata(dev);
int ret;
/*
* If jack was unplugged and re-plugged during suspend it could
* have changed type but the tip-sense state hasn't changed.
* Force a plugged state to be re-evaluated.
*/
if (cs42l42->plug_state != CS42L42_TS_UNPLUG)
cs42l42->plug_state = CS42L42_TS_TRANS;
ret = regulator_bulk_enable(ARRAY_SIZE(cs42l42->supplies), cs42l42->supplies);
if (ret != 0) {
dev_err(dev, "Failed to enable supplies: %d\n", ret);
return ret;
}
gpiod_set_value_cansleep(cs42l42->reset_gpio, 1);
usleep_range(CS42L42_BOOT_TIME_US, CS42L42_BOOT_TIME_US * 2);
regcache_cache_only(cs42l42->regmap, false);
regcache_mark_dirty(cs42l42->regmap);
mutex_lock(&cs42l42->irq_lock);
/* Sync LATCH_TO_VP first so the VP domain registers sync correctly */
regcache_sync_region(cs42l42->regmap, CS42L42_MIC_DET_CTL1, CS42L42_MIC_DET_CTL1);
regcache_sync(cs42l42->regmap);
cs42l42->suspended = false;
mutex_unlock(&cs42l42->irq_lock);
dev_dbg(dev, "System resumed\n");
return 0;
}
static int cs42l42_i2c_probe(struct i2c_client *i2c_client, static int cs42l42_i2c_probe(struct i2c_client *i2c_client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
...@@ -2054,7 +2197,7 @@ static int cs42l42_i2c_probe(struct i2c_client *i2c_client, ...@@ -2054,7 +2197,7 @@ static int cs42l42_i2c_probe(struct i2c_client *i2c_client,
cs42l42->dev = &i2c_client->dev; cs42l42->dev = &i2c_client->dev;
i2c_set_clientdata(i2c_client, cs42l42); i2c_set_clientdata(i2c_client, cs42l42);
mutex_init(&cs42l42->jack_detect_mutex); mutex_init(&cs42l42->irq_lock);
cs42l42->regmap = devm_regmap_init_i2c(i2c_client, &cs42l42_regmap); cs42l42->regmap = devm_regmap_init_i2c(i2c_client, &cs42l42_regmap);
if (IS_ERR(cs42l42->regmap)) { if (IS_ERR(cs42l42->regmap)) {
...@@ -2210,6 +2353,10 @@ static int cs42l42_i2c_remove(struct i2c_client *i2c_client) ...@@ -2210,6 +2353,10 @@ static int cs42l42_i2c_remove(struct i2c_client *i2c_client)
return 0; return 0;
} }
static const struct dev_pm_ops cs42l42_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(cs42l42_suspend, cs42l42_resume)
};
#ifdef CONFIG_OF #ifdef CONFIG_OF
static const struct of_device_id cs42l42_of_match[] = { static const struct of_device_id cs42l42_of_match[] = {
{ .compatible = "cirrus,cs42l42", }, { .compatible = "cirrus,cs42l42", },
...@@ -2236,6 +2383,7 @@ MODULE_DEVICE_TABLE(i2c, cs42l42_id); ...@@ -2236,6 +2383,7 @@ MODULE_DEVICE_TABLE(i2c, cs42l42_id);
static struct i2c_driver cs42l42_i2c_driver = { static struct i2c_driver cs42l42_i2c_driver = {
.driver = { .driver = {
.name = "cs42l42", .name = "cs42l42",
.pm = &cs42l42_pm_ops,
.of_match_table = of_match_ptr(cs42l42_of_match), .of_match_table = of_match_ptr(cs42l42_of_match),
.acpi_match_table = ACPI_PTR(cs42l42_acpi_match), .acpi_match_table = ACPI_PTR(cs42l42_acpi_match),
}, },
......
...@@ -826,6 +826,10 @@ ...@@ -826,6 +826,10 @@
#define CS42L42_PLL_LOCK_POLL_US 250 #define CS42L42_PLL_LOCK_POLL_US 250
#define CS42L42_PLL_LOCK_TIMEOUT_US 1250 #define CS42L42_PLL_LOCK_TIMEOUT_US 1250
#define CS42L42_HP_ADC_EN_TIME_US 20000 #define CS42L42_HP_ADC_EN_TIME_US 20000
#define CS42L42_PDN_DONE_POLL_US 1000
#define CS42L42_PDN_DONE_TIMEOUT_US 200000
#define CS42L42_PDN_DONE_TIME_MS 100
#define CS42L42_FILT_DISCHARGE_TIME_MS 46
static const char *const cs42l42_supply_names[CS42L42_NUM_SUPPLIES] = { static const char *const cs42l42_supply_names[CS42L42_NUM_SUPPLIES] = {
"VA", "VA",
...@@ -842,7 +846,7 @@ struct cs42l42_private { ...@@ -842,7 +846,7 @@ struct cs42l42_private {
struct gpio_desc *reset_gpio; struct gpio_desc *reset_gpio;
struct completion pdn_done; struct completion pdn_done;
struct snd_soc_jack *jack; struct snd_soc_jack *jack;
struct mutex jack_detect_mutex; struct mutex irq_lock;
int pll_config; int pll_config;
int bclk; int bclk;
u32 sclk; u32 sclk;
...@@ -860,6 +864,7 @@ struct cs42l42_private { ...@@ -860,6 +864,7 @@ struct cs42l42_private {
u8 hs_bias_sense_en; u8 hs_bias_sense_en;
u8 stream_use; u8 stream_use;
bool hp_adc_up_pending; bool hp_adc_up_pending;
bool suspended;
}; };
#endif /* __CS42L42_H__ */ #endif /* __CS42L42_H__ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment