2 * MAX98504 ALSA SoC Audio driver
4 * Copyright 2013 - 2014 Maxim Integrated Products
5 * Copyright 2016 Samsung Electronics Co., Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <sound/soc.h>
22 static const char * const max98504_supply_names
[] = {
27 #define MAX98504_NUM_SUPPLIES ARRAY_SIZE(max98504_supply_names)
29 struct max98504_priv
{
30 struct regmap
*regmap
;
31 struct regulator_bulk_data supplies
[MAX98504_NUM_SUPPLIES
];
32 unsigned int pcm_rx_channels
;
34 unsigned int brownout_threshold
;
35 unsigned int brownout_attenuation
;
36 unsigned int brownout_attack_hold
;
37 unsigned int brownout_timed_hold
;
38 unsigned int brownout_release_rate
;
41 static struct reg_default max98504_reg_defaults
[] = {
80 static bool max98504_volatile_register(struct device
*dev
, unsigned int reg
)
83 case MAX98504_INTERRUPT_STATUS
:
84 case MAX98504_INTERRUPT_FLAGS
:
85 case MAX98504_INTERRUPT_FLAG_CLEARS
:
86 case MAX98504_WATCHDOG_CLEAR
:
87 case MAX98504_GLOBAL_ENABLE
:
88 case MAX98504_SOFTWARE_RESET
:
95 static bool max98504_readable_register(struct device
*dev
, unsigned int reg
)
98 case MAX98504_SOFTWARE_RESET
:
99 case MAX98504_WATCHDOG_CLEAR
:
100 case MAX98504_INTERRUPT_FLAG_CLEARS
:
107 static int max98504_pcm_rx_ev(struct snd_soc_dapm_widget
*w
,
108 struct snd_kcontrol
*kcontrol
, int event
)
110 struct snd_soc_component
*c
= snd_soc_dapm_to_component(w
->dapm
);
111 struct max98504_priv
*max98504
= snd_soc_component_get_drvdata(c
);
114 case SND_SOC_DAPM_PRE_PMU
:
115 regmap_write(max98504
->regmap
, MAX98504_PCM_RX_ENABLE
,
116 max98504
->pcm_rx_channels
);
118 case SND_SOC_DAPM_POST_PMD
:
119 regmap_write(max98504
->regmap
, MAX98504_PCM_RX_ENABLE
, 0);
126 static int max98504_component_probe(struct snd_soc_component
*c
)
128 struct max98504_priv
*max98504
= snd_soc_component_get_drvdata(c
);
129 struct regmap
*map
= max98504
->regmap
;
132 ret
= regulator_bulk_enable(MAX98504_NUM_SUPPLIES
, max98504
->supplies
);
136 regmap_write(map
, MAX98504_SOFTWARE_RESET
, 0x1);
139 if (!max98504
->brownout_enable
)
142 regmap_write(map
, MAX98504_PVDD_BROWNOUT_ENABLE
, 0x1);
144 regmap_write(map
, MAX98504_PVDD_BROWNOUT_CONFIG_1
,
145 (max98504
->brownout_threshold
& 0x1f) << 3 |
146 (max98504
->brownout_attenuation
& 0x3));
148 regmap_write(map
, MAX98504_PVDD_BROWNOUT_CONFIG_2
,
149 max98504
->brownout_attack_hold
& 0xff);
151 regmap_write(map
, MAX98504_PVDD_BROWNOUT_CONFIG_3
,
152 max98504
->brownout_timed_hold
& 0xff);
154 regmap_write(map
, MAX98504_PVDD_BROWNOUT_CONFIG_4
,
155 max98504
->brownout_release_rate
& 0xff);
160 static void max98504_component_remove(struct snd_soc_component
*c
)
162 struct max98504_priv
*max98504
= snd_soc_component_get_drvdata(c
);
164 regulator_bulk_disable(MAX98504_NUM_SUPPLIES
, max98504
->supplies
);
167 static const char *spk_source_mux_text
[] = {
168 "PCM Monomix", "Analog In", "PDM Left", "PDM Right"
171 static const struct soc_enum spk_source_mux_enum
=
172 SOC_ENUM_SINGLE(MAX98504_SPEAKER_SOURCE_SELECT
,
173 0, ARRAY_SIZE(spk_source_mux_text
),
174 spk_source_mux_text
);
176 static const struct snd_kcontrol_new spk_source_mux
=
177 SOC_DAPM_ENUM("SPK Source", spk_source_mux_enum
);
179 static const struct snd_soc_dapm_route max98504_dapm_routes
[] = {
180 { "SPKOUT", NULL
, "Global Enable" },
181 { "SPK Source", "PCM Monomix", "DAC PCM" },
182 { "SPK Source", "Analog In", "AIN" },
183 { "SPK Source", "PDM Left", "DAC PDM" },
184 { "SPK Source", "PDM Right", "DAC PDM" },
187 static const struct snd_soc_dapm_widget max98504_dapm_widgets
[] = {
188 SND_SOC_DAPM_SUPPLY("Global Enable", MAX98504_GLOBAL_ENABLE
,
190 SND_SOC_DAPM_INPUT("AIN"),
191 SND_SOC_DAPM_AIF_OUT("AIF2OUTL", "AIF2 Capture", 0, SND_SOC_NOPM
, 0, 0),
192 SND_SOC_DAPM_AIF_OUT("AIF2OUTR", "AIF2 Capture", 1, SND_SOC_NOPM
, 0, 0),
193 SND_SOC_DAPM_DAC_E("DAC PCM", NULL
, SND_SOC_NOPM
, 0, 0,
195 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
196 SND_SOC_DAPM_DAC("DAC PDM", NULL
, MAX98504_PDM_RX_ENABLE
, 0, 0),
197 SND_SOC_DAPM_MUX("SPK Source", SND_SOC_NOPM
, 0, 0, &spk_source_mux
),
198 SND_SOC_DAPM_REG(snd_soc_dapm_spk
, "SPKOUT",
199 MAX98504_SPEAKER_ENABLE
, 0, 1, 1, 0),
202 static int max98504_set_tdm_slot(struct snd_soc_dai
*dai
,
203 unsigned int tx_mask
, unsigned int rx_mask
,
204 int slots
, int slot_width
)
206 struct max98504_priv
*max98504
= snd_soc_dai_get_drvdata(dai
);
207 struct regmap
*map
= max98504
->regmap
;
211 case MAX98504_DAI_ID_PCM
:
212 regmap_write(map
, MAX98504_PCM_TX_ENABLE
, tx_mask
);
213 max98504
->pcm_rx_channels
= rx_mask
;
216 case MAX98504_DAI_ID_PDM
:
217 regmap_write(map
, MAX98504_PDM_TX_ENABLE
, tx_mask
);
225 static int max98504_set_channel_map(struct snd_soc_dai
*dai
,
226 unsigned int tx_num
, unsigned int *tx_slot
,
227 unsigned int rx_num
, unsigned int *rx_slot
)
229 struct max98504_priv
*max98504
= snd_soc_dai_get_drvdata(dai
);
230 struct regmap
*map
= max98504
->regmap
;
231 unsigned int i
, sources
= 0;
233 for (i
= 0; i
< tx_num
; i
++)
238 case MAX98504_DAI_ID_PCM
:
239 regmap_write(map
, MAX98504_PCM_TX_CHANNEL_SOURCES
,
243 case MAX98504_DAI_ID_PDM
:
244 regmap_write(map
, MAX98504_PDM_TX_CONTROL
, sources
);
250 regmap_write(map
, MAX98504_MEASUREMENT_ENABLE
, sources
? 0x3 : 0x01);
255 static const struct snd_soc_dai_ops max98504_dai_ops
= {
256 .set_tdm_slot
= max98504_set_tdm_slot
,
257 .set_channel_map
= max98504_set_channel_map
,
260 #define MAX98504_FORMATS (SNDRV_PCM_FMTBIT_S8|SNDRV_PCM_FMTBIT_S16_LE|\
261 SNDRV_PCM_FMTBIT_S24_LE|SNDRV_PCM_FMTBIT_S32_LE)
262 #define MAX98504_PDM_RATES (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|\
263 SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|\
264 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|\
265 SNDRV_PCM_RATE_96000)
267 static struct snd_soc_dai_driver max98504_dai
[] = {
268 /* TODO: Add the PCM interface definitions */
270 .name
= "max98504-aif2",
271 .id
= MAX98504_DAI_ID_PDM
,
273 .stream_name
= "AIF2 Playback",
276 .rates
= MAX98504_PDM_RATES
,
277 .formats
= MAX98504_FORMATS
,
280 .stream_name
= "AIF2 Capture",
283 .rates
= MAX98504_PDM_RATES
,
284 .formats
= MAX98504_FORMATS
,
286 .ops
= &max98504_dai_ops
,
290 static const struct snd_soc_component_driver max98504_component_driver
= {
291 .probe
= max98504_component_probe
,
292 .remove
= max98504_component_remove
,
293 .dapm_widgets
= max98504_dapm_widgets
,
294 .num_dapm_widgets
= ARRAY_SIZE(max98504_dapm_widgets
),
295 .dapm_routes
= max98504_dapm_routes
,
296 .num_dapm_routes
= ARRAY_SIZE(max98504_dapm_routes
),
299 static const struct regmap_config max98504_regmap
= {
302 .max_register
= MAX98504_MAX_REGISTER
,
303 .reg_defaults
= max98504_reg_defaults
,
304 .num_reg_defaults
= ARRAY_SIZE(max98504_reg_defaults
),
305 .volatile_reg
= max98504_volatile_register
,
306 .readable_reg
= max98504_readable_register
,
307 .cache_type
= REGCACHE_RBTREE
,
310 static int max98504_i2c_probe(struct i2c_client
*client
,
311 const struct i2c_device_id
*id
)
313 struct device
*dev
= &client
->dev
;
314 struct device_node
*node
= dev
->of_node
;
315 struct max98504_priv
*max98504
;
318 max98504
= devm_kzalloc(dev
, sizeof(*max98504
), GFP_KERNEL
);
323 if (!of_property_read_u32(node
, "maxim,brownout-threshold",
324 &max98504
->brownout_threshold
))
325 max98504
->brownout_enable
= true;
327 of_property_read_u32(node
, "maxim,brownout-attenuation",
328 &max98504
->brownout_attenuation
);
329 of_property_read_u32(node
, "maxim,brownout-attack-hold-ms",
330 &max98504
->brownout_attack_hold
);
331 of_property_read_u32(node
, "maxim,brownout-timed-hold-ms",
332 &max98504
->brownout_timed_hold
);
333 of_property_read_u32(node
, "maxim,brownout-release-rate-ms",
334 &max98504
->brownout_release_rate
);
337 max98504
->regmap
= devm_regmap_init_i2c(client
, &max98504_regmap
);
338 if (IS_ERR(max98504
->regmap
)) {
339 ret
= PTR_ERR(max98504
->regmap
);
340 dev_err(&client
->dev
, "regmap initialization failed: %d\n", ret
);
344 for (i
= 0; i
< MAX98504_NUM_SUPPLIES
; i
++)
345 max98504
->supplies
[i
].supply
= max98504_supply_names
[i
];
347 ret
= devm_regulator_bulk_get(dev
, MAX98504_NUM_SUPPLIES
,
352 i2c_set_clientdata(client
, max98504
);
354 return devm_snd_soc_register_component(dev
, &max98504_component_driver
,
355 max98504_dai
, ARRAY_SIZE(max98504_dai
));
359 static const struct of_device_id max98504_of_match
[] = {
360 { .compatible
= "maxim,max98504" },
363 MODULE_DEVICE_TABLE(of
, max98504_of_match
);
366 static const struct i2c_device_id max98504_i2c_id
[] = {
370 MODULE_DEVICE_TABLE(i2c
, max98504_i2c_id
);
372 static struct i2c_driver max98504_i2c_driver
= {
375 .of_match_table
= of_match_ptr(max98504_of_match
),
377 .probe
= max98504_i2c_probe
,
378 .id_table
= max98504_i2c_id
,
380 module_i2c_driver(max98504_i2c_driver
);
382 MODULE_DESCRIPTION("ASoC MAX98504 driver");
383 MODULE_LICENSE("GPL");