WIP FPC-III support
[linux/fpc-iii.git] / sound / soc / codecs / ak5558.c
blob8a32b0139cb0c080b03b393d547d4fd6c68b8b60
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Audio driver for AK5558 ADC
4 //
5 // Copyright (C) 2015 Asahi Kasei Microdevices Corporation
6 // Copyright 2018 NXP
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/regulator/consumer.h>
15 #include <linux/slab.h>
17 #include <sound/initval.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
22 #include <sound/tlv.h>
24 #include "ak5558.h"
26 #define AK5558_NUM_SUPPLIES 2
27 static const char *ak5558_supply_names[AK5558_NUM_SUPPLIES] = {
28 "DVDD",
29 "AVDD",
32 /* AK5558 Codec Private Data */
33 struct ak5558_priv {
34 struct regulator_bulk_data supplies[AK5558_NUM_SUPPLIES];
35 struct snd_soc_component component;
36 struct regmap *regmap;
37 struct i2c_client *i2c;
38 struct gpio_desc *reset_gpiod; /* Reset & Power down GPIO */
39 int slots;
40 int slot_width;
43 /* ak5558 register cache & default register settings */
44 static const struct reg_default ak5558_reg[] = {
45 { 0x0, 0xFF }, /* 0x00 AK5558_00_POWER_MANAGEMENT1 */
46 { 0x1, 0x01 }, /* 0x01 AK5558_01_POWER_MANAGEMENT2 */
47 { 0x2, 0x01 }, /* 0x02 AK5558_02_CONTROL1 */
48 { 0x3, 0x00 }, /* 0x03 AK5558_03_CONTROL2 */
49 { 0x4, 0x00 }, /* 0x04 AK5558_04_CONTROL3 */
50 { 0x5, 0x00 } /* 0x05 AK5558_05_DSD */
53 static const char * const mono_texts[] = {
54 "8 Slot", "2 Slot", "4 Slot", "1 Slot",
57 static const struct soc_enum ak5558_mono_enum[] = {
58 SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1,
59 ARRAY_SIZE(mono_texts), mono_texts),
62 static const char * const digfil_texts[] = {
63 "Sharp Roll-Off", "Show Roll-Off",
64 "Short Delay Sharp Roll-Off", "Short Delay Show Roll-Off",
67 static const struct soc_enum ak5558_adcset_enum[] = {
68 SOC_ENUM_SINGLE(AK5558_04_CONTROL3, 0,
69 ARRAY_SIZE(digfil_texts), digfil_texts),
72 static const struct snd_kcontrol_new ak5558_snd_controls[] = {
73 SOC_ENUM("AK5558 Monaural Mode", ak5558_mono_enum[0]),
74 SOC_ENUM("AK5558 Digital Filter", ak5558_adcset_enum[0]),
77 static const struct snd_soc_dapm_widget ak5558_dapm_widgets[] = {
78 /* Analog Input */
79 SND_SOC_DAPM_INPUT("AIN1"),
80 SND_SOC_DAPM_INPUT("AIN2"),
81 SND_SOC_DAPM_INPUT("AIN3"),
82 SND_SOC_DAPM_INPUT("AIN4"),
83 SND_SOC_DAPM_INPUT("AIN5"),
84 SND_SOC_DAPM_INPUT("AIN6"),
85 SND_SOC_DAPM_INPUT("AIN7"),
86 SND_SOC_DAPM_INPUT("AIN8"),
88 SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0),
89 SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0),
90 SND_SOC_DAPM_ADC("ADC Ch3", NULL, AK5558_00_POWER_MANAGEMENT1, 2, 0),
91 SND_SOC_DAPM_ADC("ADC Ch4", NULL, AK5558_00_POWER_MANAGEMENT1, 3, 0),
92 SND_SOC_DAPM_ADC("ADC Ch5", NULL, AK5558_00_POWER_MANAGEMENT1, 4, 0),
93 SND_SOC_DAPM_ADC("ADC Ch6", NULL, AK5558_00_POWER_MANAGEMENT1, 5, 0),
94 SND_SOC_DAPM_ADC("ADC Ch7", NULL, AK5558_00_POWER_MANAGEMENT1, 6, 0),
95 SND_SOC_DAPM_ADC("ADC Ch8", NULL, AK5558_00_POWER_MANAGEMENT1, 7, 0),
97 SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0),
100 static const struct snd_soc_dapm_route ak5558_intercon[] = {
101 {"ADC Ch1", NULL, "AIN1"},
102 {"SDTO", NULL, "ADC Ch1"},
104 {"ADC Ch2", NULL, "AIN2"},
105 {"SDTO", NULL, "ADC Ch2"},
107 {"ADC Ch3", NULL, "AIN3"},
108 {"SDTO", NULL, "ADC Ch3"},
110 {"ADC Ch4", NULL, "AIN4"},
111 {"SDTO", NULL, "ADC Ch4"},
113 {"ADC Ch5", NULL, "AIN5"},
114 {"SDTO", NULL, "ADC Ch5"},
116 {"ADC Ch6", NULL, "AIN6"},
117 {"SDTO", NULL, "ADC Ch6"},
119 {"ADC Ch7", NULL, "AIN7"},
120 {"SDTO", NULL, "ADC Ch7"},
122 {"ADC Ch8", NULL, "AIN8"},
123 {"SDTO", NULL, "ADC Ch8"},
126 static int ak5558_set_mcki(struct snd_soc_component *component)
128 return snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_CKS,
129 AK5558_CKS_AUTO);
132 static int ak5558_hw_params(struct snd_pcm_substream *substream,
133 struct snd_pcm_hw_params *params,
134 struct snd_soc_dai *dai)
136 struct snd_soc_component *component = dai->component;
137 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
138 u8 bits;
139 int pcm_width = max(params_physical_width(params), ak5558->slot_width);
141 switch (pcm_width) {
142 case 16:
143 bits = AK5558_DIF_24BIT_MODE;
144 break;
145 case 32:
146 bits = AK5558_DIF_32BIT_MODE;
147 break;
148 default:
149 return -EINVAL;
152 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_BITS, bits);
154 return 0;
157 static int ak5558_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
159 struct snd_soc_component *component = dai->component;
160 u8 format;
162 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
163 case SND_SOC_DAIFMT_CBS_CFS:
164 break;
165 case SND_SOC_DAIFMT_CBM_CFM:
166 break;
167 case SND_SOC_DAIFMT_CBS_CFM:
168 case SND_SOC_DAIFMT_CBM_CFS:
169 default:
170 dev_err(dai->dev, "Clock mode unsupported");
171 return -EINVAL;
174 /* set master/slave audio interface */
175 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
176 case SND_SOC_DAIFMT_I2S:
177 format = AK5558_DIF_I2S_MODE;
178 break;
179 case SND_SOC_DAIFMT_LEFT_J:
180 format = AK5558_DIF_MSB_MODE;
181 break;
182 case SND_SOC_DAIFMT_DSP_B:
183 format = AK5558_DIF_MSB_MODE;
184 break;
185 default:
186 return -EINVAL;
189 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_DIF, format);
191 return 0;
194 static int ak5558_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
195 unsigned int rx_mask, int slots,
196 int slot_width)
198 struct snd_soc_component *component = dai->component;
199 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
200 int tdm_mode;
202 ak5558->slots = slots;
203 ak5558->slot_width = slot_width;
205 switch (slots * slot_width) {
206 case 128:
207 tdm_mode = AK5558_MODE_TDM128;
208 break;
209 case 256:
210 tdm_mode = AK5558_MODE_TDM256;
211 break;
212 case 512:
213 tdm_mode = AK5558_MODE_TDM512;
214 break;
215 default:
216 tdm_mode = AK5558_MODE_NORMAL;
217 break;
220 snd_soc_component_update_bits(component, AK5558_03_CONTROL2, AK5558_MODE_BITS,
221 tdm_mode);
222 return 0;
225 #define AK5558_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
226 SNDRV_PCM_FMTBIT_S24_LE |\
227 SNDRV_PCM_FMTBIT_S32_LE)
229 static const unsigned int ak5558_rates[] = {
230 8000, 11025, 16000, 22050,
231 32000, 44100, 48000, 88200,
232 96000, 176400, 192000, 352800,
233 384000, 705600, 768000, 1411200,
234 2822400,
237 static const struct snd_pcm_hw_constraint_list ak5558_rate_constraints = {
238 .count = ARRAY_SIZE(ak5558_rates),
239 .list = ak5558_rates,
242 static int ak5558_startup(struct snd_pcm_substream *substream,
243 struct snd_soc_dai *dai)
245 return snd_pcm_hw_constraint_list(substream->runtime, 0,
246 SNDRV_PCM_HW_PARAM_RATE,
247 &ak5558_rate_constraints);
250 static const struct snd_soc_dai_ops ak5558_dai_ops = {
251 .startup = ak5558_startup,
252 .hw_params = ak5558_hw_params,
254 .set_fmt = ak5558_set_dai_fmt,
255 .set_tdm_slot = ak5558_set_tdm_slot,
258 static struct snd_soc_dai_driver ak5558_dai = {
259 .name = "ak5558-aif",
260 .capture = {
261 .stream_name = "Capture",
262 .channels_min = 1,
263 .channels_max = 8,
264 .rates = SNDRV_PCM_RATE_KNOT,
265 .formats = AK5558_FORMATS,
267 .ops = &ak5558_dai_ops,
270 static void ak5558_power_off(struct ak5558_priv *ak5558)
272 if (!ak5558->reset_gpiod)
273 return;
275 gpiod_set_value_cansleep(ak5558->reset_gpiod, 0);
276 usleep_range(1000, 2000);
279 static void ak5558_power_on(struct ak5558_priv *ak5558)
281 if (!ak5558->reset_gpiod)
282 return;
284 gpiod_set_value_cansleep(ak5558->reset_gpiod, 1);
285 usleep_range(1000, 2000);
288 static int ak5558_probe(struct snd_soc_component *component)
290 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
292 ak5558_power_on(ak5558);
293 return ak5558_set_mcki(component);
296 static void ak5558_remove(struct snd_soc_component *component)
298 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
300 ak5558_power_off(ak5558);
303 static int __maybe_unused ak5558_runtime_suspend(struct device *dev)
305 struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
307 regcache_cache_only(ak5558->regmap, true);
308 ak5558_power_off(ak5558);
310 regulator_bulk_disable(ARRAY_SIZE(ak5558->supplies),
311 ak5558->supplies);
312 return 0;
315 static int __maybe_unused ak5558_runtime_resume(struct device *dev)
317 struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
318 int ret;
320 ret = regulator_bulk_enable(ARRAY_SIZE(ak5558->supplies),
321 ak5558->supplies);
322 if (ret != 0) {
323 dev_err(dev, "Failed to enable supplies: %d\n", ret);
324 return ret;
327 ak5558_power_off(ak5558);
328 ak5558_power_on(ak5558);
330 regcache_cache_only(ak5558->regmap, false);
331 regcache_mark_dirty(ak5558->regmap);
333 return regcache_sync(ak5558->regmap);
336 static const struct dev_pm_ops ak5558_pm = {
337 SET_RUNTIME_PM_OPS(ak5558_runtime_suspend, ak5558_runtime_resume, NULL)
338 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
339 pm_runtime_force_resume)
342 static const struct snd_soc_component_driver soc_codec_dev_ak5558 = {
343 .probe = ak5558_probe,
344 .remove = ak5558_remove,
345 .controls = ak5558_snd_controls,
346 .num_controls = ARRAY_SIZE(ak5558_snd_controls),
347 .dapm_widgets = ak5558_dapm_widgets,
348 .num_dapm_widgets = ARRAY_SIZE(ak5558_dapm_widgets),
349 .dapm_routes = ak5558_intercon,
350 .num_dapm_routes = ARRAY_SIZE(ak5558_intercon),
351 .idle_bias_on = 1,
352 .use_pmdown_time = 1,
353 .endianness = 1,
354 .non_legacy_dai_naming = 1,
357 static const struct regmap_config ak5558_regmap = {
358 .reg_bits = 8,
359 .val_bits = 8,
361 .max_register = AK5558_05_DSD,
362 .reg_defaults = ak5558_reg,
363 .num_reg_defaults = ARRAY_SIZE(ak5558_reg),
364 .cache_type = REGCACHE_RBTREE,
367 static int ak5558_i2c_probe(struct i2c_client *i2c)
369 struct ak5558_priv *ak5558;
370 int ret = 0;
371 int i;
373 ak5558 = devm_kzalloc(&i2c->dev, sizeof(*ak5558), GFP_KERNEL);
374 if (!ak5558)
375 return -ENOMEM;
377 ak5558->regmap = devm_regmap_init_i2c(i2c, &ak5558_regmap);
378 if (IS_ERR(ak5558->regmap))
379 return PTR_ERR(ak5558->regmap);
381 i2c_set_clientdata(i2c, ak5558);
382 ak5558->i2c = i2c;
384 ak5558->reset_gpiod = devm_gpiod_get_optional(&i2c->dev, "reset",
385 GPIOD_OUT_LOW);
386 if (IS_ERR(ak5558->reset_gpiod))
387 return PTR_ERR(ak5558->reset_gpiod);
389 for (i = 0; i < ARRAY_SIZE(ak5558->supplies); i++)
390 ak5558->supplies[i].supply = ak5558_supply_names[i];
392 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(ak5558->supplies),
393 ak5558->supplies);
394 if (ret != 0) {
395 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
396 return ret;
399 ret = devm_snd_soc_register_component(&i2c->dev,
400 &soc_codec_dev_ak5558,
401 &ak5558_dai, 1);
402 if (ret)
403 return ret;
405 pm_runtime_enable(&i2c->dev);
406 regcache_cache_only(ak5558->regmap, true);
408 return 0;
411 static int ak5558_i2c_remove(struct i2c_client *i2c)
413 pm_runtime_disable(&i2c->dev);
415 return 0;
418 static const struct of_device_id ak5558_i2c_dt_ids[] __maybe_unused = {
419 { .compatible = "asahi-kasei,ak5558"},
423 static struct i2c_driver ak5558_i2c_driver = {
424 .driver = {
425 .name = "ak5558",
426 .of_match_table = of_match_ptr(ak5558_i2c_dt_ids),
427 .pm = &ak5558_pm,
429 .probe_new = ak5558_i2c_probe,
430 .remove = ak5558_i2c_remove,
433 module_i2c_driver(ak5558_i2c_driver);
435 MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
436 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
437 MODULE_DESCRIPTION("ASoC AK5558 ADC driver");
438 MODULE_LICENSE("GPL v2");