1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * MAX98504 ALSA SoC Audio driver
4 *
5 * Copyright 2013 - 2014 Maxim Integrated Products
6 * Copyright 2016 Samsung Electronics Co., Ltd.
7 */
8
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>
16
17 #include "max98504.h"
18
19 static const char * const max98504_supply_names[] = {
20 "DVDD",
21 "DIOVDD",
22 "PVDD",
23 };
24 #define MAX98504_NUM_SUPPLIES ARRAY_SIZE(max98504_supply_names)
25
26 struct max98504_priv {
27 struct regmap *regmap;
28 struct regulator_bulk_data supplies[MAX98504_NUM_SUPPLIES];
29 unsigned int pcm_rx_channels;
30 bool brownout_enable;
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;
36 };
37
38 static struct reg_default max98504_reg_defaults[] = {
39 { 0x01, 0},
40 { 0x02, 0},
41 { 0x03, 0},
42 { 0x04, 0},
43 { 0x10, 0},
44 { 0x11, 0},
45 { 0x12, 0},
46 { 0x13, 0},
47 { 0x14, 0},
48 { 0x15, 0},
49 { 0x16, 0},
50 { 0x17, 0},
51 { 0x18, 0},
52 { 0x19, 0},
53 { 0x1A, 0},
54 { 0x20, 0},
55 { 0x21, 0},
56 { 0x22, 0},
57 { 0x23, 0},
58 { 0x24, 0},
59 { 0x25, 0},
60 { 0x26, 0},
61 { 0x27, 0},
62 { 0x28, 0},
63 { 0x30, 0},
64 { 0x31, 0},
65 { 0x32, 0},
66 { 0x33, 0},
67 { 0x34, 0},
68 { 0x35, 0},
69 { 0x36, 0},
70 { 0x37, 0},
71 { 0x38, 0},
72 { 0x39, 0},
73 { 0x40, 0},
74 { 0x41, 0},
75 };
76
max98504_volatile_register(struct device * dev,unsigned int reg)77 static bool max98504_volatile_register(struct device *dev, unsigned int reg)
78 {
79 switch (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:
86 return true;
87 default:
88 return false;
89 }
90 }
91
max98504_readable_register(struct device * dev,unsigned int reg)92 static bool max98504_readable_register(struct device *dev, unsigned int reg)
93 {
94 switch (reg) {
95 case MAX98504_SOFTWARE_RESET:
96 case MAX98504_WATCHDOG_CLEAR:
97 case MAX98504_INTERRUPT_FLAG_CLEARS:
98 return false;
99 default:
100 return true;
101 }
102 }
103
max98504_pcm_rx_ev(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)104 static int max98504_pcm_rx_ev(struct snd_soc_dapm_widget *w,
105 struct snd_kcontrol *kcontrol, int event)
106 {
107 struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
108 struct max98504_priv *max98504 = snd_soc_component_get_drvdata(c);
109
110 switch (event) {
111 case SND_SOC_DAPM_PRE_PMU:
112 regmap_write(max98504->regmap, MAX98504_PCM_RX_ENABLE,
113 max98504->pcm_rx_channels);
114 break;
115 case SND_SOC_DAPM_POST_PMD:
116 regmap_write(max98504->regmap, MAX98504_PCM_RX_ENABLE, 0);
117 break;
118 }
119
120 return 0;
121 }
122
max98504_component_probe(struct snd_soc_component * c)123 static int max98504_component_probe(struct snd_soc_component *c)
124 {
125 struct max98504_priv *max98504 = snd_soc_component_get_drvdata(c);
126 struct regmap *map = max98504->regmap;
127 int ret;
128
129 ret = regulator_bulk_enable(MAX98504_NUM_SUPPLIES, max98504->supplies);
130 if (ret < 0)
131 return ret;
132
133 regmap_write(map, MAX98504_SOFTWARE_RESET, 0x1);
134 msleep(20);
135
136 if (!max98504->brownout_enable)
137 return 0;
138
139 regmap_write(map, MAX98504_PVDD_BROWNOUT_ENABLE, 0x1);
140
141 regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_1,
142 (max98504->brownout_threshold & 0x1f) << 3 |
143 (max98504->brownout_attenuation & 0x3));
144
145 regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_2,
146 max98504->brownout_attack_hold & 0xff);
147
148 regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_3,
149 max98504->brownout_timed_hold & 0xff);
150
151 regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_4,
152 max98504->brownout_release_rate & 0xff);
153
154 return 0;
155 }
156
max98504_component_remove(struct snd_soc_component * c)157 static void max98504_component_remove(struct snd_soc_component *c)
158 {
159 struct max98504_priv *max98504 = snd_soc_component_get_drvdata(c);
160
161 regulator_bulk_disable(MAX98504_NUM_SUPPLIES, max98504->supplies);
162 }
163
164 static const char *spk_source_mux_text[] = {
165 "PCM Monomix", "Analog In", "PDM Left", "PDM Right"
166 };
167
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);
172
173 static const struct snd_kcontrol_new spk_source_mux =
174 SOC_DAPM_ENUM("SPK Source", spk_source_mux_enum);
175
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" },
182 };
183
184 static const struct snd_soc_dapm_widget max98504_dapm_widgets[] = {
185 SND_SOC_DAPM_SUPPLY("Global Enable", MAX98504_GLOBAL_ENABLE,
186 0, 0, NULL, 0),
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,
191 max98504_pcm_rx_ev,
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),
197 };
198
max98504_set_tdm_slot(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)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)
202 {
203 struct max98504_priv *max98504 = snd_soc_dai_get_drvdata(dai);
204 struct regmap *map = max98504->regmap;
205
206
207 switch (dai->id) {
208 case MAX98504_DAI_ID_PCM:
209 regmap_write(map, MAX98504_PCM_TX_ENABLE, tx_mask);
210 max98504->pcm_rx_channels = rx_mask;
211 break;
212
213 case MAX98504_DAI_ID_PDM:
214 regmap_write(map, MAX98504_PDM_TX_ENABLE, tx_mask);
215 break;
216 default:
217 WARN_ON(1);
218 }
219
220 return 0;
221 }
max98504_set_channel_map(struct snd_soc_dai * dai,unsigned int tx_num,const unsigned int * tx_slot,unsigned int rx_num,const unsigned int * rx_slot)222 static int max98504_set_channel_map(struct snd_soc_dai *dai,
223 unsigned int tx_num,
224 const unsigned int *tx_slot,
225 unsigned int rx_num,
226 const unsigned int *rx_slot)
227 {
228 struct max98504_priv *max98504 = snd_soc_dai_get_drvdata(dai);
229 struct regmap *map = max98504->regmap;
230 unsigned int i, sources = 0;
231
232 for (i = 0; i < tx_num; i++)
233 if (tx_slot[i])
234 sources |= (1 << i);
235
236 switch (dai->id) {
237 case MAX98504_DAI_ID_PCM:
238 regmap_write(map, MAX98504_PCM_TX_CHANNEL_SOURCES,
239 sources);
240 break;
241
242 case MAX98504_DAI_ID_PDM:
243 regmap_write(map, MAX98504_PDM_TX_CONTROL, sources);
244 break;
245 default:
246 WARN_ON(1);
247 }
248
249 regmap_write(map, MAX98504_MEASUREMENT_ENABLE, sources ? 0x3 : 0x01);
250
251 return 0;
252 }
253
254 static const struct snd_soc_dai_ops max98504_dai_ops = {
255 .set_tdm_slot = max98504_set_tdm_slot,
256 .set_channel_map = max98504_set_channel_map,
257 };
258
259 #define MAX98504_FORMATS (SNDRV_PCM_FMTBIT_S8|SNDRV_PCM_FMTBIT_S16_LE|\
260 SNDRV_PCM_FMTBIT_S24_LE|SNDRV_PCM_FMTBIT_S32_LE)
261 #define MAX98504_PDM_RATES (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|\
262 SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|\
263 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|\
264 SNDRV_PCM_RATE_96000)
265
266 static struct snd_soc_dai_driver max98504_dai[] = {
267 /* TODO: Add the PCM interface definitions */
268 {
269 .name = "max98504-aif2",
270 .id = MAX98504_DAI_ID_PDM,
271 .playback = {
272 .stream_name = "AIF2 Playback",
273 .channels_min = 1,
274 .channels_max = 2,
275 .rates = MAX98504_PDM_RATES,
276 .formats = MAX98504_FORMATS,
277 },
278 .capture = {
279 .stream_name = "AIF2 Capture",
280 .channels_min = 1,
281 .channels_max = 2,
282 .rates = MAX98504_PDM_RATES,
283 .formats = MAX98504_FORMATS,
284 },
285 .ops = &max98504_dai_ops,
286 },
287 };
288
289 static const struct snd_soc_component_driver max98504_component_driver = {
290 .probe = max98504_component_probe,
291 .remove = max98504_component_remove,
292 .dapm_widgets = max98504_dapm_widgets,
293 .num_dapm_widgets = ARRAY_SIZE(max98504_dapm_widgets),
294 .dapm_routes = max98504_dapm_routes,
295 .num_dapm_routes = ARRAY_SIZE(max98504_dapm_routes),
296 .endianness = 1,
297 };
298
299 static const struct regmap_config max98504_regmap = {
300 .reg_bits = 16,
301 .val_bits = 8,
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,
308 };
309
max98504_i2c_probe(struct i2c_client * client)310 static int max98504_i2c_probe(struct i2c_client *client)
311 {
312 struct device *dev = &client->dev;
313 struct device_node *node = dev->of_node;
314 struct max98504_priv *max98504;
315 int i, ret;
316
317 max98504 = devm_kzalloc(dev, sizeof(*max98504), GFP_KERNEL);
318 if (!max98504)
319 return -ENOMEM;
320
321 if (node) {
322 if (!of_property_read_u32(node, "maxim,brownout-threshold",
323 &max98504->brownout_threshold))
324 max98504->brownout_enable = true;
325
326 of_property_read_u32(node, "maxim,brownout-attenuation",
327 &max98504->brownout_attenuation);
328 of_property_read_u32(node, "maxim,brownout-attack-hold-ms",
329 &max98504->brownout_attack_hold);
330 of_property_read_u32(node, "maxim,brownout-timed-hold-ms",
331 &max98504->brownout_timed_hold);
332 of_property_read_u32(node, "maxim,brownout-release-rate-ms",
333 &max98504->brownout_release_rate);
334 }
335
336 max98504->regmap = devm_regmap_init_i2c(client, &max98504_regmap);
337 if (IS_ERR(max98504->regmap)) {
338 ret = PTR_ERR(max98504->regmap);
339 dev_err(&client->dev, "regmap initialization failed: %d\n", ret);
340 return ret;
341 }
342
343 for (i = 0; i < MAX98504_NUM_SUPPLIES; i++)
344 max98504->supplies[i].supply = max98504_supply_names[i];
345
346 ret = devm_regulator_bulk_get(dev, MAX98504_NUM_SUPPLIES,
347 max98504->supplies);
348 if (ret < 0)
349 return ret;
350
351 i2c_set_clientdata(client, max98504);
352
353 return devm_snd_soc_register_component(dev, &max98504_component_driver,
354 max98504_dai, ARRAY_SIZE(max98504_dai));
355 }
356
357 #ifdef CONFIG_OF
358 static const struct of_device_id max98504_of_match[] = {
359 { .compatible = "maxim,max98504" },
360 { },
361 };
362 MODULE_DEVICE_TABLE(of, max98504_of_match);
363 #endif
364
365 static const struct i2c_device_id max98504_i2c_id[] = {
366 { "max98504" },
367 { }
368 };
369 MODULE_DEVICE_TABLE(i2c, max98504_i2c_id);
370
371 static struct i2c_driver max98504_i2c_driver = {
372 .driver = {
373 .name = "max98504",
374 .of_match_table = of_match_ptr(max98504_of_match),
375 },
376 .probe = max98504_i2c_probe,
377 .id_table = max98504_i2c_id,
378 };
379 module_i2c_driver(max98504_i2c_driver);
380
381 MODULE_DESCRIPTION("ASoC MAX98504 driver");
382 MODULE_LICENSE("GPL");
383