0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/delay.h>
0010 #include <linux/i2c.h>
0011 #include <linux/module.h>
0012 #include <linux/regulator/consumer.h>
0013 #include <linux/slab.h>
0014 #include <linux/types.h>
0015 #include <sound/soc.h>
0016
0017 #include "max98504.h"
0018
0019 static const char * const max98504_supply_names[] = {
0020 "DVDD",
0021 "DIOVDD",
0022 "PVDD",
0023 };
0024 #define MAX98504_NUM_SUPPLIES ARRAY_SIZE(max98504_supply_names)
0025
0026 struct max98504_priv {
0027 struct regmap *regmap;
0028 struct regulator_bulk_data supplies[MAX98504_NUM_SUPPLIES];
0029 unsigned int pcm_rx_channels;
0030 bool brownout_enable;
0031 unsigned int brownout_threshold;
0032 unsigned int brownout_attenuation;
0033 unsigned int brownout_attack_hold;
0034 unsigned int brownout_timed_hold;
0035 unsigned int brownout_release_rate;
0036 };
0037
0038 static struct reg_default max98504_reg_defaults[] = {
0039 { 0x01, 0},
0040 { 0x02, 0},
0041 { 0x03, 0},
0042 { 0x04, 0},
0043 { 0x10, 0},
0044 { 0x11, 0},
0045 { 0x12, 0},
0046 { 0x13, 0},
0047 { 0x14, 0},
0048 { 0x15, 0},
0049 { 0x16, 0},
0050 { 0x17, 0},
0051 { 0x18, 0},
0052 { 0x19, 0},
0053 { 0x1A, 0},
0054 { 0x20, 0},
0055 { 0x21, 0},
0056 { 0x22, 0},
0057 { 0x23, 0},
0058 { 0x24, 0},
0059 { 0x25, 0},
0060 { 0x26, 0},
0061 { 0x27, 0},
0062 { 0x28, 0},
0063 { 0x30, 0},
0064 { 0x31, 0},
0065 { 0x32, 0},
0066 { 0x33, 0},
0067 { 0x34, 0},
0068 { 0x35, 0},
0069 { 0x36, 0},
0070 { 0x37, 0},
0071 { 0x38, 0},
0072 { 0x39, 0},
0073 { 0x40, 0},
0074 { 0x41, 0},
0075 };
0076
0077 static bool max98504_volatile_register(struct device *dev, unsigned int reg)
0078 {
0079 switch (reg) {
0080 case MAX98504_INTERRUPT_STATUS:
0081 case MAX98504_INTERRUPT_FLAGS:
0082 case MAX98504_INTERRUPT_FLAG_CLEARS:
0083 case MAX98504_WATCHDOG_CLEAR:
0084 case MAX98504_GLOBAL_ENABLE:
0085 case MAX98504_SOFTWARE_RESET:
0086 return true;
0087 default:
0088 return false;
0089 }
0090 }
0091
0092 static bool max98504_readable_register(struct device *dev, unsigned int reg)
0093 {
0094 switch (reg) {
0095 case MAX98504_SOFTWARE_RESET:
0096 case MAX98504_WATCHDOG_CLEAR:
0097 case MAX98504_INTERRUPT_FLAG_CLEARS:
0098 return false;
0099 default:
0100 return true;
0101 }
0102 }
0103
0104 static int max98504_pcm_rx_ev(struct snd_soc_dapm_widget *w,
0105 struct snd_kcontrol *kcontrol, int event)
0106 {
0107 struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
0108 struct max98504_priv *max98504 = snd_soc_component_get_drvdata(c);
0109
0110 switch (event) {
0111 case SND_SOC_DAPM_PRE_PMU:
0112 regmap_write(max98504->regmap, MAX98504_PCM_RX_ENABLE,
0113 max98504->pcm_rx_channels);
0114 break;
0115 case SND_SOC_DAPM_POST_PMD:
0116 regmap_write(max98504->regmap, MAX98504_PCM_RX_ENABLE, 0);
0117 break;
0118 }
0119
0120 return 0;
0121 }
0122
0123 static int max98504_component_probe(struct snd_soc_component *c)
0124 {
0125 struct max98504_priv *max98504 = snd_soc_component_get_drvdata(c);
0126 struct regmap *map = max98504->regmap;
0127 int ret;
0128
0129 ret = regulator_bulk_enable(MAX98504_NUM_SUPPLIES, max98504->supplies);
0130 if (ret < 0)
0131 return ret;
0132
0133 regmap_write(map, MAX98504_SOFTWARE_RESET, 0x1);
0134 msleep(20);
0135
0136 if (!max98504->brownout_enable)
0137 return 0;
0138
0139 regmap_write(map, MAX98504_PVDD_BROWNOUT_ENABLE, 0x1);
0140
0141 regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_1,
0142 (max98504->brownout_threshold & 0x1f) << 3 |
0143 (max98504->brownout_attenuation & 0x3));
0144
0145 regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_2,
0146 max98504->brownout_attack_hold & 0xff);
0147
0148 regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_3,
0149 max98504->brownout_timed_hold & 0xff);
0150
0151 regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_4,
0152 max98504->brownout_release_rate & 0xff);
0153
0154 return 0;
0155 }
0156
0157 static void max98504_component_remove(struct snd_soc_component *c)
0158 {
0159 struct max98504_priv *max98504 = snd_soc_component_get_drvdata(c);
0160
0161 regulator_bulk_disable(MAX98504_NUM_SUPPLIES, max98504->supplies);
0162 }
0163
0164 static const char *spk_source_mux_text[] = {
0165 "PCM Monomix", "Analog In", "PDM Left", "PDM Right"
0166 };
0167
0168 static const struct soc_enum spk_source_mux_enum =
0169 SOC_ENUM_SINGLE(MAX98504_SPEAKER_SOURCE_SELECT,
0170 0, ARRAY_SIZE(spk_source_mux_text),
0171 spk_source_mux_text);
0172
0173 static const struct snd_kcontrol_new spk_source_mux =
0174 SOC_DAPM_ENUM("SPK Source", spk_source_mux_enum);
0175
0176 static const struct snd_soc_dapm_route max98504_dapm_routes[] = {
0177 { "SPKOUT", NULL, "Global Enable" },
0178 { "SPK Source", "PCM Monomix", "DAC PCM" },
0179 { "SPK Source", "Analog In", "AIN" },
0180 { "SPK Source", "PDM Left", "DAC PDM" },
0181 { "SPK Source", "PDM Right", "DAC PDM" },
0182 };
0183
0184 static const struct snd_soc_dapm_widget max98504_dapm_widgets[] = {
0185 SND_SOC_DAPM_SUPPLY("Global Enable", MAX98504_GLOBAL_ENABLE,
0186 0, 0, NULL, 0),
0187 SND_SOC_DAPM_INPUT("AIN"),
0188 SND_SOC_DAPM_AIF_OUT("AIF2OUTL", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
0189 SND_SOC_DAPM_AIF_OUT("AIF2OUTR", "AIF2 Capture", 1, SND_SOC_NOPM, 0, 0),
0190 SND_SOC_DAPM_DAC_E("DAC PCM", NULL, SND_SOC_NOPM, 0, 0,
0191 max98504_pcm_rx_ev,
0192 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
0193 SND_SOC_DAPM_DAC("DAC PDM", NULL, MAX98504_PDM_RX_ENABLE, 0, 0),
0194 SND_SOC_DAPM_MUX("SPK Source", SND_SOC_NOPM, 0, 0, &spk_source_mux),
0195 SND_SOC_DAPM_REG(snd_soc_dapm_spk, "SPKOUT",
0196 MAX98504_SPEAKER_ENABLE, 0, 1, 1, 0),
0197 };
0198
0199 static int max98504_set_tdm_slot(struct snd_soc_dai *dai,
0200 unsigned int tx_mask, unsigned int rx_mask,
0201 int slots, int slot_width)
0202 {
0203 struct max98504_priv *max98504 = snd_soc_dai_get_drvdata(dai);
0204 struct regmap *map = max98504->regmap;
0205
0206
0207 switch (dai->id) {
0208 case MAX98504_DAI_ID_PCM:
0209 regmap_write(map, MAX98504_PCM_TX_ENABLE, tx_mask);
0210 max98504->pcm_rx_channels = rx_mask;
0211 break;
0212
0213 case MAX98504_DAI_ID_PDM:
0214 regmap_write(map, MAX98504_PDM_TX_ENABLE, tx_mask);
0215 break;
0216 default:
0217 WARN_ON(1);
0218 }
0219
0220 return 0;
0221 }
0222 static int max98504_set_channel_map(struct snd_soc_dai *dai,
0223 unsigned int tx_num, unsigned int *tx_slot,
0224 unsigned int rx_num, unsigned int *rx_slot)
0225 {
0226 struct max98504_priv *max98504 = snd_soc_dai_get_drvdata(dai);
0227 struct regmap *map = max98504->regmap;
0228 unsigned int i, sources = 0;
0229
0230 for (i = 0; i < tx_num; i++)
0231 if (tx_slot[i])
0232 sources |= (1 << i);
0233
0234 switch (dai->id) {
0235 case MAX98504_DAI_ID_PCM:
0236 regmap_write(map, MAX98504_PCM_TX_CHANNEL_SOURCES,
0237 sources);
0238 break;
0239
0240 case MAX98504_DAI_ID_PDM:
0241 regmap_write(map, MAX98504_PDM_TX_CONTROL, sources);
0242 break;
0243 default:
0244 WARN_ON(1);
0245 }
0246
0247 regmap_write(map, MAX98504_MEASUREMENT_ENABLE, sources ? 0x3 : 0x01);
0248
0249 return 0;
0250 }
0251
0252 static const struct snd_soc_dai_ops max98504_dai_ops = {
0253 .set_tdm_slot = max98504_set_tdm_slot,
0254 .set_channel_map = max98504_set_channel_map,
0255 };
0256
0257 #define MAX98504_FORMATS (SNDRV_PCM_FMTBIT_S8|SNDRV_PCM_FMTBIT_S16_LE|\
0258 SNDRV_PCM_FMTBIT_S24_LE|SNDRV_PCM_FMTBIT_S32_LE)
0259 #define MAX98504_PDM_RATES (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|\
0260 SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|\
0261 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|\
0262 SNDRV_PCM_RATE_96000)
0263
0264 static struct snd_soc_dai_driver max98504_dai[] = {
0265
0266 {
0267 .name = "max98504-aif2",
0268 .id = MAX98504_DAI_ID_PDM,
0269 .playback = {
0270 .stream_name = "AIF2 Playback",
0271 .channels_min = 1,
0272 .channels_max = 2,
0273 .rates = MAX98504_PDM_RATES,
0274 .formats = MAX98504_FORMATS,
0275 },
0276 .capture = {
0277 .stream_name = "AIF2 Capture",
0278 .channels_min = 1,
0279 .channels_max = 2,
0280 .rates = MAX98504_PDM_RATES,
0281 .formats = MAX98504_FORMATS,
0282 },
0283 .ops = &max98504_dai_ops,
0284 },
0285 };
0286
0287 static const struct snd_soc_component_driver max98504_component_driver = {
0288 .probe = max98504_component_probe,
0289 .remove = max98504_component_remove,
0290 .dapm_widgets = max98504_dapm_widgets,
0291 .num_dapm_widgets = ARRAY_SIZE(max98504_dapm_widgets),
0292 .dapm_routes = max98504_dapm_routes,
0293 .num_dapm_routes = ARRAY_SIZE(max98504_dapm_routes),
0294 .endianness = 1,
0295 };
0296
0297 static const struct regmap_config max98504_regmap = {
0298 .reg_bits = 16,
0299 .val_bits = 8,
0300 .max_register = MAX98504_MAX_REGISTER,
0301 .reg_defaults = max98504_reg_defaults,
0302 .num_reg_defaults = ARRAY_SIZE(max98504_reg_defaults),
0303 .volatile_reg = max98504_volatile_register,
0304 .readable_reg = max98504_readable_register,
0305 .cache_type = REGCACHE_RBTREE,
0306 };
0307
0308 static int max98504_i2c_probe(struct i2c_client *client)
0309 {
0310 struct device *dev = &client->dev;
0311 struct device_node *node = dev->of_node;
0312 struct max98504_priv *max98504;
0313 int i, ret;
0314
0315 max98504 = devm_kzalloc(dev, sizeof(*max98504), GFP_KERNEL);
0316 if (!max98504)
0317 return -ENOMEM;
0318
0319 if (node) {
0320 if (!of_property_read_u32(node, "maxim,brownout-threshold",
0321 &max98504->brownout_threshold))
0322 max98504->brownout_enable = true;
0323
0324 of_property_read_u32(node, "maxim,brownout-attenuation",
0325 &max98504->brownout_attenuation);
0326 of_property_read_u32(node, "maxim,brownout-attack-hold-ms",
0327 &max98504->brownout_attack_hold);
0328 of_property_read_u32(node, "maxim,brownout-timed-hold-ms",
0329 &max98504->brownout_timed_hold);
0330 of_property_read_u32(node, "maxim,brownout-release-rate-ms",
0331 &max98504->brownout_release_rate);
0332 }
0333
0334 max98504->regmap = devm_regmap_init_i2c(client, &max98504_regmap);
0335 if (IS_ERR(max98504->regmap)) {
0336 ret = PTR_ERR(max98504->regmap);
0337 dev_err(&client->dev, "regmap initialization failed: %d\n", ret);
0338 return ret;
0339 }
0340
0341 for (i = 0; i < MAX98504_NUM_SUPPLIES; i++)
0342 max98504->supplies[i].supply = max98504_supply_names[i];
0343
0344 ret = devm_regulator_bulk_get(dev, MAX98504_NUM_SUPPLIES,
0345 max98504->supplies);
0346 if (ret < 0)
0347 return ret;
0348
0349 i2c_set_clientdata(client, max98504);
0350
0351 return devm_snd_soc_register_component(dev, &max98504_component_driver,
0352 max98504_dai, ARRAY_SIZE(max98504_dai));
0353 }
0354
0355 #ifdef CONFIG_OF
0356 static const struct of_device_id max98504_of_match[] = {
0357 { .compatible = "maxim,max98504" },
0358 { },
0359 };
0360 MODULE_DEVICE_TABLE(of, max98504_of_match);
0361 #endif
0362
0363 static const struct i2c_device_id max98504_i2c_id[] = {
0364 { "max98504" },
0365 { }
0366 };
0367 MODULE_DEVICE_TABLE(i2c, max98504_i2c_id);
0368
0369 static struct i2c_driver max98504_i2c_driver = {
0370 .driver = {
0371 .name = "max98504",
0372 .of_match_table = of_match_ptr(max98504_of_match),
0373 },
0374 .probe_new = max98504_i2c_probe,
0375 .id_table = max98504_i2c_id,
0376 };
0377 module_i2c_driver(max98504_i2c_driver);
0378
0379 MODULE_DESCRIPTION("ASoC MAX98504 driver");
0380 MODULE_LICENSE("GPL");