This source file includes following definitions.
- max98504_volatile_register
- max98504_readable_register
- max98504_pcm_rx_ev
- max98504_component_probe
- max98504_component_remove
- max98504_set_tdm_slot
- max98504_set_channel_map
- max98504_i2c_probe
1
2
3
4
5
6
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
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
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
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
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
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
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 }
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)
225 {
226 struct max98504_priv *max98504 = snd_soc_dai_get_drvdata(dai);
227 struct regmap *map = max98504->regmap;
228 unsigned int i, sources = 0;
229
230 for (i = 0; i < tx_num; i++)
231 if (tx_slot[i])
232 sources |= (1 << i);
233
234 switch (dai->id) {
235 case MAX98504_DAI_ID_PCM:
236 regmap_write(map, MAX98504_PCM_TX_CHANNEL_SOURCES,
237 sources);
238 break;
239
240 case MAX98504_DAI_ID_PDM:
241 regmap_write(map, MAX98504_PDM_TX_CONTROL, sources);
242 break;
243 default:
244 WARN_ON(1);
245 }
246
247 regmap_write(map, MAX98504_MEASUREMENT_ENABLE, sources ? 0x3 : 0x01);
248
249 return 0;
250 }
251
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,
255 };
256
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)
263
264 static struct snd_soc_dai_driver max98504_dai[] = {
265
266 {
267 .name = "max98504-aif2",
268 .id = MAX98504_DAI_ID_PDM,
269 .playback = {
270 .stream_name = "AIF2 Playback",
271 .channels_min = 1,
272 .channels_max = 2,
273 .rates = MAX98504_PDM_RATES,
274 .formats = MAX98504_FORMATS,
275 },
276 .capture = {
277 .stream_name = "AIF2 Capture",
278 .channels_min = 1,
279 .channels_max = 2,
280 .rates = MAX98504_PDM_RATES,
281 .formats = MAX98504_FORMATS,
282 },
283 .ops = &max98504_dai_ops,
284 },
285 };
286
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),
294 };
295
296 static const struct regmap_config max98504_regmap = {
297 .reg_bits = 16,
298 .val_bits = 8,
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,
305 };
306
307 static int max98504_i2c_probe(struct i2c_client *client,
308 const struct i2c_device_id *id)
309 {
310 struct device *dev = &client->dev;
311 struct device_node *node = dev->of_node;
312 struct max98504_priv *max98504;
313 int i, ret;
314
315 max98504 = devm_kzalloc(dev, sizeof(*max98504), GFP_KERNEL);
316 if (!max98504)
317 return -ENOMEM;
318
319 if (node) {
320 if (!of_property_read_u32(node, "maxim,brownout-threshold",
321 &max98504->brownout_threshold))
322 max98504->brownout_enable = true;
323
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);
332 }
333
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);
338 return ret;
339 }
340
341 for (i = 0; i < MAX98504_NUM_SUPPLIES; i++)
342 max98504->supplies[i].supply = max98504_supply_names[i];
343
344 ret = devm_regulator_bulk_get(dev, MAX98504_NUM_SUPPLIES,
345 max98504->supplies);
346 if (ret < 0)
347 return ret;
348
349 i2c_set_clientdata(client, max98504);
350
351 return devm_snd_soc_register_component(dev, &max98504_component_driver,
352 max98504_dai, ARRAY_SIZE(max98504_dai));
353 }
354
355 #ifdef CONFIG_OF
356 static const struct of_device_id max98504_of_match[] = {
357 { .compatible = "maxim,max98504" },
358 { },
359 };
360 MODULE_DEVICE_TABLE(of, max98504_of_match);
361 #endif
362
363 static const struct i2c_device_id max98504_i2c_id[] = {
364 { "max98504" },
365 { }
366 };
367 MODULE_DEVICE_TABLE(i2c, max98504_i2c_id);
368
369 static struct i2c_driver max98504_i2c_driver = {
370 .driver = {
371 .name = "max98504",
372 .of_match_table = of_match_ptr(max98504_of_match),
373 },
374 .probe = max98504_i2c_probe,
375 .id_table = max98504_i2c_id,
376 };
377 module_i2c_driver(max98504_i2c_driver);
378
379 MODULE_DESCRIPTION("ASoC MAX98504 driver");
380 MODULE_LICENSE("GPL");