1 // SPDX-License-Identifier: GPL-2.0-only
3 * MAX98504 ALSA SoC Audio driver
5 * Copyright 2013 - 2014 Maxim Integrated Products
6 * Copyright 2016 Samsung Electronics Co., Ltd.
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <sound/soc.h>
19 static const char * const max98504_supply_names
[] = {
24 #define MAX98504_NUM_SUPPLIES ARRAY_SIZE(max98504_supply_names)
26 struct max98504_priv
{
27 struct regmap
*regmap
;
28 struct regulator_bulk_data supplies
[MAX98504_NUM_SUPPLIES
];
29 unsigned int pcm_rx_channels
;
31 unsigned int brownout_threshold
;
32 unsigned int brownout_attenuation
;
33 unsigned int brownout_attack_hold
;
34 unsigned int brownout_timed_hold
;
35 unsigned int brownout_release_rate
;
38 static struct reg_default max98504_reg_defaults
[] = {
77 static bool max98504_volatile_register(struct device
*dev
, unsigned int reg
)
80 case MAX98504_INTERRUPT_STATUS
:
81 case MAX98504_INTERRUPT_FLAGS
:
82 case MAX98504_INTERRUPT_FLAG_CLEARS
:
83 case MAX98504_WATCHDOG_CLEAR
:
84 case MAX98504_GLOBAL_ENABLE
:
85 case MAX98504_SOFTWARE_RESET
:
92 static bool max98504_readable_register(struct device
*dev
, unsigned int reg
)
95 case MAX98504_SOFTWARE_RESET
:
96 case MAX98504_WATCHDOG_CLEAR
:
97 case MAX98504_INTERRUPT_FLAG_CLEARS
:
104 static int max98504_pcm_rx_ev(struct snd_soc_dapm_widget
*w
,
105 struct snd_kcontrol
*kcontrol
, int event
)
107 struct snd_soc_component
*c
= snd_soc_dapm_to_component(w
->dapm
);
108 struct max98504_priv
*max98504
= snd_soc_component_get_drvdata(c
);
111 case SND_SOC_DAPM_PRE_PMU
:
112 regmap_write(max98504
->regmap
, MAX98504_PCM_RX_ENABLE
,
113 max98504
->pcm_rx_channels
);
115 case SND_SOC_DAPM_POST_PMD
:
116 regmap_write(max98504
->regmap
, MAX98504_PCM_RX_ENABLE
, 0);
123 static int max98504_component_probe(struct snd_soc_component
*c
)
125 struct max98504_priv
*max98504
= snd_soc_component_get_drvdata(c
);
126 struct regmap
*map
= max98504
->regmap
;
129 ret
= regulator_bulk_enable(MAX98504_NUM_SUPPLIES
, max98504
->supplies
);
133 regmap_write(map
, MAX98504_SOFTWARE_RESET
, 0x1);
136 if (!max98504
->brownout_enable
)
139 regmap_write(map
, MAX98504_PVDD_BROWNOUT_ENABLE
, 0x1);
141 regmap_write(map
, MAX98504_PVDD_BROWNOUT_CONFIG_1
,
142 (max98504
->brownout_threshold
& 0x1f) << 3 |
143 (max98504
->brownout_attenuation
& 0x3));
145 regmap_write(map
, MAX98504_PVDD_BROWNOUT_CONFIG_2
,
146 max98504
->brownout_attack_hold
& 0xff);
148 regmap_write(map
, MAX98504_PVDD_BROWNOUT_CONFIG_3
,
149 max98504
->brownout_timed_hold
& 0xff);
151 regmap_write(map
, MAX98504_PVDD_BROWNOUT_CONFIG_4
,
152 max98504
->brownout_release_rate
& 0xff);
157 static void max98504_component_remove(struct snd_soc_component
*c
)
159 struct max98504_priv
*max98504
= snd_soc_component_get_drvdata(c
);
161 regulator_bulk_disable(MAX98504_NUM_SUPPLIES
, max98504
->supplies
);
164 static const char *spk_source_mux_text
[] = {
165 "PCM Monomix", "Analog In", "PDM Left", "PDM Right"
168 static const struct soc_enum spk_source_mux_enum
=
169 SOC_ENUM_SINGLE(MAX98504_SPEAKER_SOURCE_SELECT
,
170 0, ARRAY_SIZE(spk_source_mux_text
),
171 spk_source_mux_text
);
173 static const struct snd_kcontrol_new spk_source_mux
=
174 SOC_DAPM_ENUM("SPK Source", spk_source_mux_enum
);
176 static const struct snd_soc_dapm_route max98504_dapm_routes
[] = {
177 { "SPKOUT", NULL
, "Global Enable" },
178 { "SPK Source", "PCM Monomix", "DAC PCM" },
179 { "SPK Source", "Analog In", "AIN" },
180 { "SPK Source", "PDM Left", "DAC PDM" },
181 { "SPK Source", "PDM Right", "DAC PDM" },
184 static const struct snd_soc_dapm_widget max98504_dapm_widgets
[] = {
185 SND_SOC_DAPM_SUPPLY("Global Enable", MAX98504_GLOBAL_ENABLE
,
187 SND_SOC_DAPM_INPUT("AIN"),
188 SND_SOC_DAPM_AIF_OUT("AIF2OUTL", "AIF2 Capture", 0, SND_SOC_NOPM
, 0, 0),
189 SND_SOC_DAPM_AIF_OUT("AIF2OUTR", "AIF2 Capture", 1, SND_SOC_NOPM
, 0, 0),
190 SND_SOC_DAPM_DAC_E("DAC PCM", NULL
, SND_SOC_NOPM
, 0, 0,
192 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
193 SND_SOC_DAPM_DAC("DAC PDM", NULL
, MAX98504_PDM_RX_ENABLE
, 0, 0),
194 SND_SOC_DAPM_MUX("SPK Source", SND_SOC_NOPM
, 0, 0, &spk_source_mux
),
195 SND_SOC_DAPM_REG(snd_soc_dapm_spk
, "SPKOUT",
196 MAX98504_SPEAKER_ENABLE
, 0, 1, 1, 0),
199 static int max98504_set_tdm_slot(struct snd_soc_dai
*dai
,
200 unsigned int tx_mask
, unsigned int rx_mask
,
201 int slots
, int slot_width
)
203 struct max98504_priv
*max98504
= snd_soc_dai_get_drvdata(dai
);
204 struct regmap
*map
= max98504
->regmap
;
208 case MAX98504_DAI_ID_PCM
:
209 regmap_write(map
, MAX98504_PCM_TX_ENABLE
, tx_mask
);
210 max98504
->pcm_rx_channels
= rx_mask
;
213 case MAX98504_DAI_ID_PDM
:
214 regmap_write(map
, MAX98504_PDM_TX_ENABLE
, tx_mask
);
222 static int max98504_set_channel_map(struct snd_soc_dai
*dai
,
223 unsigned int tx_num
, unsigned int *tx_slot
,
224 unsigned int rx_num
, unsigned int *rx_slot
)
226 struct max98504_priv
*max98504
= snd_soc_dai_get_drvdata(dai
);
227 struct regmap
*map
= max98504
->regmap
;
228 unsigned int i
, sources
= 0;
230 for (i
= 0; i
< tx_num
; i
++)
235 case MAX98504_DAI_ID_PCM
:
236 regmap_write(map
, MAX98504_PCM_TX_CHANNEL_SOURCES
,
240 case MAX98504_DAI_ID_PDM
:
241 regmap_write(map
, MAX98504_PDM_TX_CONTROL
, sources
);
247 regmap_write(map
, MAX98504_MEASUREMENT_ENABLE
, sources
? 0x3 : 0x01);
252 static const struct snd_soc_dai_ops max98504_dai_ops
= {
253 .set_tdm_slot
= max98504_set_tdm_slot
,
254 .set_channel_map
= max98504_set_channel_map
,
257 #define MAX98504_FORMATS (SNDRV_PCM_FMTBIT_S8|SNDRV_PCM_FMTBIT_S16_LE|\
258 SNDRV_PCM_FMTBIT_S24_LE|SNDRV_PCM_FMTBIT_S32_LE)
259 #define MAX98504_PDM_RATES (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|\
260 SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|\
261 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|\
262 SNDRV_PCM_RATE_96000)
264 static struct snd_soc_dai_driver max98504_dai
[] = {
265 /* TODO: Add the PCM interface definitions */
267 .name
= "max98504-aif2",
268 .id
= MAX98504_DAI_ID_PDM
,
270 .stream_name
= "AIF2 Playback",
273 .rates
= MAX98504_PDM_RATES
,
274 .formats
= MAX98504_FORMATS
,
277 .stream_name
= "AIF2 Capture",
280 .rates
= MAX98504_PDM_RATES
,
281 .formats
= MAX98504_FORMATS
,
283 .ops
= &max98504_dai_ops
,
287 static const struct snd_soc_component_driver max98504_component_driver
= {
288 .probe
= max98504_component_probe
,
289 .remove
= max98504_component_remove
,
290 .dapm_widgets
= max98504_dapm_widgets
,
291 .num_dapm_widgets
= ARRAY_SIZE(max98504_dapm_widgets
),
292 .dapm_routes
= max98504_dapm_routes
,
293 .num_dapm_routes
= ARRAY_SIZE(max98504_dapm_routes
),
296 static const struct regmap_config max98504_regmap
= {
299 .max_register
= MAX98504_MAX_REGISTER
,
300 .reg_defaults
= max98504_reg_defaults
,
301 .num_reg_defaults
= ARRAY_SIZE(max98504_reg_defaults
),
302 .volatile_reg
= max98504_volatile_register
,
303 .readable_reg
= max98504_readable_register
,
304 .cache_type
= REGCACHE_RBTREE
,
307 static int max98504_i2c_probe(struct i2c_client
*client
,
308 const struct i2c_device_id
*id
)
310 struct device
*dev
= &client
->dev
;
311 struct device_node
*node
= dev
->of_node
;
312 struct max98504_priv
*max98504
;
315 max98504
= devm_kzalloc(dev
, sizeof(*max98504
), GFP_KERNEL
);
320 if (!of_property_read_u32(node
, "maxim,brownout-threshold",
321 &max98504
->brownout_threshold
))
322 max98504
->brownout_enable
= true;
324 of_property_read_u32(node
, "maxim,brownout-attenuation",
325 &max98504
->brownout_attenuation
);
326 of_property_read_u32(node
, "maxim,brownout-attack-hold-ms",
327 &max98504
->brownout_attack_hold
);
328 of_property_read_u32(node
, "maxim,brownout-timed-hold-ms",
329 &max98504
->brownout_timed_hold
);
330 of_property_read_u32(node
, "maxim,brownout-release-rate-ms",
331 &max98504
->brownout_release_rate
);
334 max98504
->regmap
= devm_regmap_init_i2c(client
, &max98504_regmap
);
335 if (IS_ERR(max98504
->regmap
)) {
336 ret
= PTR_ERR(max98504
->regmap
);
337 dev_err(&client
->dev
, "regmap initialization failed: %d\n", ret
);
341 for (i
= 0; i
< MAX98504_NUM_SUPPLIES
; i
++)
342 max98504
->supplies
[i
].supply
= max98504_supply_names
[i
];
344 ret
= devm_regulator_bulk_get(dev
, MAX98504_NUM_SUPPLIES
,
349 i2c_set_clientdata(client
, max98504
);
351 return devm_snd_soc_register_component(dev
, &max98504_component_driver
,
352 max98504_dai
, ARRAY_SIZE(max98504_dai
));
356 static const struct of_device_id max98504_of_match
[] = {
357 { .compatible
= "maxim,max98504" },
360 MODULE_DEVICE_TABLE(of
, max98504_of_match
);
363 static const struct i2c_device_id max98504_i2c_id
[] = {
367 MODULE_DEVICE_TABLE(i2c
, max98504_i2c_id
);
369 static struct i2c_driver max98504_i2c_driver
= {
372 .of_match_table
= of_match_ptr(max98504_of_match
),
374 .probe
= max98504_i2c_probe
,
375 .id_table
= max98504_i2c_id
,
377 module_i2c_driver(max98504_i2c_driver
);
379 MODULE_DESCRIPTION("ASoC MAX98504 driver");
380 MODULE_LICENSE("GPL");