1
2
3
4
5
6
7
8#include <linux/delay.h>
9#include <linux/gpio/consumer.h>
10#include <linux/i2c.h>
11#include <linux/module.h>
12#include <linux/pm_runtime.h>
13#include <linux/regmap.h>
14#include <linux/slab.h>
15
16#include <sound/initval.h>
17#include <sound/pcm.h>
18#include <sound/pcm_params.h>
19#include <sound/soc.h>
20#include <sound/soc-dapm.h>
21#include <sound/tlv.h>
22
23#include "ak5558.h"
24
25
26struct ak5558_priv {
27 struct snd_soc_component component;
28 struct regmap *regmap;
29 struct i2c_client *i2c;
30 struct gpio_desc *reset_gpiod;
31 int slots;
32 int slot_width;
33};
34
35
36static const struct reg_default ak5558_reg[] = {
37 { 0x0, 0xFF },
38 { 0x1, 0x01 },
39 { 0x2, 0x01 },
40 { 0x3, 0x00 },
41 { 0x4, 0x00 },
42 { 0x5, 0x00 }
43};
44
45static const char * const mono_texts[] = {
46 "8 Slot", "2 Slot", "4 Slot", "1 Slot",
47};
48
49static const struct soc_enum ak5558_mono_enum[] = {
50 SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1,
51 ARRAY_SIZE(mono_texts), mono_texts),
52};
53
54static const char * const digfil_texts[] = {
55 "Sharp Roll-Off", "Show Roll-Off",
56 "Short Delay Sharp Roll-Off", "Short Delay Show Roll-Off",
57};
58
59static const struct soc_enum ak5558_adcset_enum[] = {
60 SOC_ENUM_SINGLE(AK5558_04_CONTROL3, 0,
61 ARRAY_SIZE(digfil_texts), digfil_texts),
62};
63
64static const struct snd_kcontrol_new ak5558_snd_controls[] = {
65 SOC_ENUM("AK5558 Monaural Mode", ak5558_mono_enum[0]),
66 SOC_ENUM("AK5558 Digital Filter", ak5558_adcset_enum[0]),
67};
68
69static const struct snd_soc_dapm_widget ak5558_dapm_widgets[] = {
70
71 SND_SOC_DAPM_INPUT("AIN1"),
72 SND_SOC_DAPM_INPUT("AIN2"),
73 SND_SOC_DAPM_INPUT("AIN3"),
74 SND_SOC_DAPM_INPUT("AIN4"),
75 SND_SOC_DAPM_INPUT("AIN5"),
76 SND_SOC_DAPM_INPUT("AIN6"),
77 SND_SOC_DAPM_INPUT("AIN7"),
78 SND_SOC_DAPM_INPUT("AIN8"),
79
80 SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0),
81 SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0),
82 SND_SOC_DAPM_ADC("ADC Ch3", NULL, AK5558_00_POWER_MANAGEMENT1, 2, 0),
83 SND_SOC_DAPM_ADC("ADC Ch4", NULL, AK5558_00_POWER_MANAGEMENT1, 3, 0),
84 SND_SOC_DAPM_ADC("ADC Ch5", NULL, AK5558_00_POWER_MANAGEMENT1, 4, 0),
85 SND_SOC_DAPM_ADC("ADC Ch6", NULL, AK5558_00_POWER_MANAGEMENT1, 5, 0),
86 SND_SOC_DAPM_ADC("ADC Ch7", NULL, AK5558_00_POWER_MANAGEMENT1, 6, 0),
87 SND_SOC_DAPM_ADC("ADC Ch8", NULL, AK5558_00_POWER_MANAGEMENT1, 7, 0),
88
89 SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0),
90};
91
92static const struct snd_soc_dapm_route ak5558_intercon[] = {
93 {"ADC Ch1", NULL, "AIN1"},
94 {"SDTO", NULL, "ADC Ch1"},
95
96 {"ADC Ch2", NULL, "AIN2"},
97 {"SDTO", NULL, "ADC Ch2"},
98
99 {"ADC Ch3", NULL, "AIN3"},
100 {"SDTO", NULL, "ADC Ch3"},
101
102 {"ADC Ch4", NULL, "AIN4"},
103 {"SDTO", NULL, "ADC Ch4"},
104
105 {"ADC Ch5", NULL, "AIN5"},
106 {"SDTO", NULL, "ADC Ch5"},
107
108 {"ADC Ch6", NULL, "AIN6"},
109 {"SDTO", NULL, "ADC Ch6"},
110
111 {"ADC Ch7", NULL, "AIN7"},
112 {"SDTO", NULL, "ADC Ch7"},
113
114 {"ADC Ch8", NULL, "AIN8"},
115 {"SDTO", NULL, "ADC Ch8"},
116};
117
118static int ak5558_set_mcki(struct snd_soc_component *component)
119{
120 return snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_CKS,
121 AK5558_CKS_AUTO);
122}
123
124static int ak5558_hw_params(struct snd_pcm_substream *substream,
125 struct snd_pcm_hw_params *params,
126 struct snd_soc_dai *dai)
127{
128 struct snd_soc_component *component = dai->component;
129 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
130 u8 bits;
131 int pcm_width = max(params_physical_width(params), ak5558->slot_width);
132
133
134 bits = snd_soc_component_read32(component, AK5558_02_CONTROL1);
135 bits &= ~AK5558_BITS;
136
137 switch (pcm_width) {
138 case 16:
139 bits |= AK5558_DIF_24BIT_MODE;
140 break;
141 case 32:
142 bits |= AK5558_DIF_32BIT_MODE;
143 break;
144 default:
145 return -EINVAL;
146 }
147
148 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_BITS, bits);
149
150 return 0;
151}
152
153static int ak5558_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
154{
155 struct snd_soc_component *component = dai->component;
156 u8 format;
157
158 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
159 case SND_SOC_DAIFMT_CBS_CFS:
160 break;
161 case SND_SOC_DAIFMT_CBM_CFM:
162 break;
163 case SND_SOC_DAIFMT_CBS_CFM:
164 case SND_SOC_DAIFMT_CBM_CFS:
165 default:
166 dev_err(dai->dev, "Clock mode unsupported");
167 return -EINVAL;
168 }
169
170
171 format = snd_soc_component_read32(component, AK5558_02_CONTROL1);
172 format &= ~AK5558_DIF;
173
174 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
175 case SND_SOC_DAIFMT_I2S:
176 format |= AK5558_DIF_I2S_MODE;
177 break;
178 case SND_SOC_DAIFMT_LEFT_J:
179 format |= AK5558_DIF_MSB_MODE;
180 break;
181 case SND_SOC_DAIFMT_DSP_B:
182 format |= AK5558_DIF_MSB_MODE;
183 break;
184 default:
185 return -EINVAL;
186 }
187
188 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_DIF, format);
189
190 return 0;
191}
192
193static int ak5558_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
194 unsigned int rx_mask, int slots,
195 int slot_width)
196{
197 struct snd_soc_component *component = dai->component;
198 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
199 int tdm_mode;
200
201 ak5558->slots = slots;
202 ak5558->slot_width = slot_width;
203
204 switch (slots * slot_width) {
205 case 128:
206 tdm_mode = AK5558_MODE_TDM128;
207 break;
208 case 256:
209 tdm_mode = AK5558_MODE_TDM256;
210 break;
211 case 512:
212 tdm_mode = AK5558_MODE_TDM512;
213 break;
214 default:
215 tdm_mode = AK5558_MODE_NORMAL;
216 break;
217 }
218
219 snd_soc_component_update_bits(component, AK5558_03_CONTROL2, AK5558_MODE_BITS,
220 tdm_mode);
221 return 0;
222}
223
224#define AK5558_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
225 SNDRV_PCM_FMTBIT_S24_LE |\
226 SNDRV_PCM_FMTBIT_S32_LE)
227
228static const unsigned int ak5558_rates[] = {
229 8000, 11025, 16000, 22050,
230 32000, 44100, 48000, 88200,
231 96000, 176400, 192000, 352800,
232 384000, 705600, 768000, 1411200,
233 2822400,
234};
235
236static const struct snd_pcm_hw_constraint_list ak5558_rate_constraints = {
237 .count = ARRAY_SIZE(ak5558_rates),
238 .list = ak5558_rates,
239};
240
241static int ak5558_startup(struct snd_pcm_substream *substream,
242 struct snd_soc_dai *dai)
243{
244 return snd_pcm_hw_constraint_list(substream->runtime, 0,
245 SNDRV_PCM_HW_PARAM_RATE,
246 &ak5558_rate_constraints);
247}
248
249static struct snd_soc_dai_ops ak5558_dai_ops = {
250 .startup = ak5558_startup,
251 .hw_params = ak5558_hw_params,
252
253 .set_fmt = ak5558_set_dai_fmt,
254 .set_tdm_slot = ak5558_set_tdm_slot,
255};
256
257static struct snd_soc_dai_driver ak5558_dai = {
258 .name = "ak5558-aif",
259 .capture = {
260 .stream_name = "Capture",
261 .channels_min = 1,
262 .channels_max = 8,
263 .rates = SNDRV_PCM_RATE_KNOT,
264 .formats = AK5558_FORMATS,
265 },
266 .ops = &ak5558_dai_ops,
267};
268
269static void ak5558_power_off(struct ak5558_priv *ak5558)
270{
271 if (!ak5558->reset_gpiod)
272 return;
273
274 gpiod_set_value_cansleep(ak5558->reset_gpiod, 0);
275 usleep_range(1000, 2000);
276}
277
278static void ak5558_power_on(struct ak5558_priv *ak5558)
279{
280 if (!ak5558->reset_gpiod)
281 return;
282
283 gpiod_set_value_cansleep(ak5558->reset_gpiod, 1);
284 usleep_range(1000, 2000);
285}
286
287static int ak5558_probe(struct snd_soc_component *component)
288{
289 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
290
291 ak5558_power_on(ak5558);
292 return ak5558_set_mcki(component);
293}
294
295static void ak5558_remove(struct snd_soc_component *component)
296{
297 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
298
299 ak5558_power_off(ak5558);
300}
301
302static int __maybe_unused ak5558_runtime_suspend(struct device *dev)
303{
304 struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
305
306 regcache_cache_only(ak5558->regmap, true);
307 ak5558_power_off(ak5558);
308
309 return 0;
310}
311
312static int __maybe_unused ak5558_runtime_resume(struct device *dev)
313{
314 struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
315
316 ak5558_power_off(ak5558);
317 ak5558_power_on(ak5558);
318
319 regcache_cache_only(ak5558->regmap, false);
320 regcache_mark_dirty(ak5558->regmap);
321
322 return regcache_sync(ak5558->regmap);
323}
324
325const struct dev_pm_ops ak5558_pm = {
326 SET_RUNTIME_PM_OPS(ak5558_runtime_suspend, ak5558_runtime_resume, NULL)
327 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
328 pm_runtime_force_resume)
329};
330
331struct snd_soc_component_driver soc_codec_dev_ak5558 = {
332 .probe = ak5558_probe,
333 .remove = ak5558_remove,
334 .controls = ak5558_snd_controls,
335 .num_controls = ARRAY_SIZE(ak5558_snd_controls),
336 .dapm_widgets = ak5558_dapm_widgets,
337 .num_dapm_widgets = ARRAY_SIZE(ak5558_dapm_widgets),
338 .dapm_routes = ak5558_intercon,
339 .num_dapm_routes = ARRAY_SIZE(ak5558_intercon),
340 .idle_bias_on = 1,
341 .use_pmdown_time = 1,
342 .endianness = 1,
343 .non_legacy_dai_naming = 1,
344};
345
346static const struct regmap_config ak5558_regmap = {
347 .reg_bits = 8,
348 .val_bits = 8,
349
350 .max_register = AK5558_05_DSD,
351 .reg_defaults = ak5558_reg,
352 .num_reg_defaults = ARRAY_SIZE(ak5558_reg),
353 .cache_type = REGCACHE_RBTREE,
354};
355
356static int ak5558_i2c_probe(struct i2c_client *i2c)
357{
358 struct ak5558_priv *ak5558;
359 int ret = 0;
360
361 ak5558 = devm_kzalloc(&i2c->dev, sizeof(*ak5558), GFP_KERNEL);
362 if (!ak5558)
363 return -ENOMEM;
364
365 ak5558->regmap = devm_regmap_init_i2c(i2c, &ak5558_regmap);
366 if (IS_ERR(ak5558->regmap))
367 return PTR_ERR(ak5558->regmap);
368
369 i2c_set_clientdata(i2c, ak5558);
370 ak5558->i2c = i2c;
371
372 ak5558->reset_gpiod = devm_gpiod_get_optional(&i2c->dev, "reset",
373 GPIOD_OUT_LOW);
374 if (IS_ERR(ak5558->reset_gpiod))
375 return PTR_ERR(ak5558->reset_gpiod);
376
377 ret = devm_snd_soc_register_component(&i2c->dev,
378 &soc_codec_dev_ak5558,
379 &ak5558_dai, 1);
380 if (ret)
381 return ret;
382
383 pm_runtime_enable(&i2c->dev);
384
385 return 0;
386}
387
388static int ak5558_i2c_remove(struct i2c_client *i2c)
389{
390 pm_runtime_disable(&i2c->dev);
391
392 return 0;
393}
394
395static const struct of_device_id ak5558_i2c_dt_ids[] = {
396 { .compatible = "asahi-kasei,ak5558"},
397 { }
398};
399
400static struct i2c_driver ak5558_i2c_driver = {
401 .driver = {
402 .name = "ak5558",
403 .of_match_table = of_match_ptr(ak5558_i2c_dt_ids),
404 .pm = &ak5558_pm,
405 },
406 .probe_new = ak5558_i2c_probe,
407 .remove = ak5558_i2c_remove,
408};
409
410module_i2c_driver(ak5558_i2c_driver);
411
412MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
413MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
414MODULE_DESCRIPTION("ASoC AK5558 ADC driver");
415MODULE_LICENSE("GPL v2");
416