max98504.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * MAX98504 ALSA SoC Audio driver
  3. *
  4. * Copyright 2013 - 2014 Maxim Integrated Products
  5. * Copyright 2016 Samsung Electronics Co., Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/i2c.h>
  13. #include <linux/module.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <sound/soc.h>
  18. #include "max98504.h"
  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. struct max98504_priv {
  26. struct regmap *regmap;
  27. struct regulator_bulk_data supplies[MAX98504_NUM_SUPPLIES];
  28. unsigned int pcm_rx_channels;
  29. bool brownout_enable;
  30. unsigned int brownout_threshold;
  31. unsigned int brownout_attenuation;
  32. unsigned int brownout_attack_hold;
  33. unsigned int brownout_timed_hold;
  34. unsigned int brownout_release_rate;
  35. };
  36. static struct reg_default max98504_reg_defaults[] = {
  37. { 0x01, 0},
  38. { 0x02, 0},
  39. { 0x03, 0},
  40. { 0x04, 0},
  41. { 0x10, 0},
  42. { 0x11, 0},
  43. { 0x12, 0},
  44. { 0x13, 0},
  45. { 0x14, 0},
  46. { 0x15, 0},
  47. { 0x16, 0},
  48. { 0x17, 0},
  49. { 0x18, 0},
  50. { 0x19, 0},
  51. { 0x1A, 0},
  52. { 0x20, 0},
  53. { 0x21, 0},
  54. { 0x22, 0},
  55. { 0x23, 0},
  56. { 0x24, 0},
  57. { 0x25, 0},
  58. { 0x26, 0},
  59. { 0x27, 0},
  60. { 0x28, 0},
  61. { 0x30, 0},
  62. { 0x31, 0},
  63. { 0x32, 0},
  64. { 0x33, 0},
  65. { 0x34, 0},
  66. { 0x35, 0},
  67. { 0x36, 0},
  68. { 0x37, 0},
  69. { 0x38, 0},
  70. { 0x39, 0},
  71. { 0x40, 0},
  72. { 0x41, 0},
  73. };
  74. static bool max98504_volatile_register(struct device *dev, unsigned int reg)
  75. {
  76. switch (reg) {
  77. case MAX98504_INTERRUPT_STATUS:
  78. case MAX98504_INTERRUPT_FLAGS:
  79. case MAX98504_INTERRUPT_FLAG_CLEARS:
  80. case MAX98504_WATCHDOG_CLEAR:
  81. case MAX98504_GLOBAL_ENABLE:
  82. case MAX98504_SOFTWARE_RESET:
  83. return true;
  84. default:
  85. return false;
  86. }
  87. }
  88. static bool max98504_readable_register(struct device *dev, unsigned int reg)
  89. {
  90. switch (reg) {
  91. case MAX98504_SOFTWARE_RESET:
  92. case MAX98504_WATCHDOG_CLEAR:
  93. case MAX98504_INTERRUPT_FLAG_CLEARS:
  94. return false;
  95. default:
  96. return true;
  97. }
  98. }
  99. static int max98504_pcm_rx_ev(struct snd_soc_dapm_widget *w,
  100. struct snd_kcontrol *kcontrol, int event)
  101. {
  102. struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
  103. struct max98504_priv *max98504 = snd_soc_component_get_drvdata(c);
  104. switch (event) {
  105. case SND_SOC_DAPM_PRE_PMU:
  106. regmap_write(max98504->regmap, MAX98504_PCM_RX_ENABLE,
  107. max98504->pcm_rx_channels);
  108. break;
  109. case SND_SOC_DAPM_POST_PMD:
  110. regmap_write(max98504->regmap, MAX98504_PCM_RX_ENABLE, 0);
  111. break;
  112. }
  113. return 0;
  114. }
  115. static int max98504_component_probe(struct snd_soc_component *c)
  116. {
  117. struct max98504_priv *max98504 = snd_soc_component_get_drvdata(c);
  118. struct regmap *map = max98504->regmap;
  119. int ret;
  120. ret = regulator_bulk_enable(MAX98504_NUM_SUPPLIES, max98504->supplies);
  121. if (ret < 0)
  122. return ret;
  123. regmap_write(map, MAX98504_SOFTWARE_RESET, 0x1);
  124. msleep(20);
  125. if (!max98504->brownout_enable)
  126. return 0;
  127. regmap_write(map, MAX98504_PVDD_BROWNOUT_ENABLE, 0x1);
  128. regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_1,
  129. (max98504->brownout_threshold & 0x1f) << 3 |
  130. (max98504->brownout_attenuation & 0x3));
  131. regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_2,
  132. max98504->brownout_attack_hold & 0xff);
  133. regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_3,
  134. max98504->brownout_timed_hold & 0xff);
  135. regmap_write(map, MAX98504_PVDD_BROWNOUT_CONFIG_4,
  136. max98504->brownout_release_rate & 0xff);
  137. return 0;
  138. }
  139. static void max98504_component_remove(struct snd_soc_component *c)
  140. {
  141. struct max98504_priv *max98504 = snd_soc_component_get_drvdata(c);
  142. regulator_bulk_disable(MAX98504_NUM_SUPPLIES, max98504->supplies);
  143. }
  144. static const char *spk_source_mux_text[] = {
  145. "PCM Monomix", "Analog In", "PDM Left", "PDM Right"
  146. };
  147. static const struct soc_enum spk_source_mux_enum =
  148. SOC_ENUM_SINGLE(MAX98504_SPEAKER_SOURCE_SELECT,
  149. 0, ARRAY_SIZE(spk_source_mux_text),
  150. spk_source_mux_text);
  151. static const struct snd_kcontrol_new spk_source_mux =
  152. SOC_DAPM_ENUM("SPK Source", spk_source_mux_enum);
  153. static const struct snd_soc_dapm_route max98504_dapm_routes[] = {
  154. { "SPKOUT", NULL, "Global Enable" },
  155. { "SPK Source", "PCM Monomix", "DAC PCM" },
  156. { "SPK Source", "Analog In", "AIN" },
  157. { "SPK Source", "PDM Left", "DAC PDM" },
  158. { "SPK Source", "PDM Right", "DAC PDM" },
  159. };
  160. static const struct snd_soc_dapm_widget max98504_dapm_widgets[] = {
  161. SND_SOC_DAPM_SUPPLY("Global Enable", MAX98504_GLOBAL_ENABLE,
  162. 0, 0, NULL, 0),
  163. SND_SOC_DAPM_INPUT("AIN"),
  164. SND_SOC_DAPM_AIF_OUT("AIF2OUTL", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
  165. SND_SOC_DAPM_AIF_OUT("AIF2OUTR", "AIF2 Capture", 1, SND_SOC_NOPM, 0, 0),
  166. SND_SOC_DAPM_DAC_E("DAC PCM", NULL, SND_SOC_NOPM, 0, 0,
  167. max98504_pcm_rx_ev,
  168. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  169. SND_SOC_DAPM_DAC("DAC PDM", NULL, MAX98504_PDM_RX_ENABLE, 0, 0),
  170. SND_SOC_DAPM_MUX("SPK Source", SND_SOC_NOPM, 0, 0, &spk_source_mux),
  171. SND_SOC_DAPM_REG(snd_soc_dapm_spk, "SPKOUT",
  172. MAX98504_SPEAKER_ENABLE, 0, 1, 1, 0),
  173. };
  174. static int max98504_set_tdm_slot(struct snd_soc_dai *dai,
  175. unsigned int tx_mask, unsigned int rx_mask,
  176. int slots, int slot_width)
  177. {
  178. struct max98504_priv *max98504 = snd_soc_dai_get_drvdata(dai);
  179. struct regmap *map = max98504->regmap;
  180. switch (dai->id) {
  181. case MAX98504_DAI_ID_PCM:
  182. regmap_write(map, MAX98504_PCM_TX_ENABLE, tx_mask);
  183. max98504->pcm_rx_channels = rx_mask;
  184. break;
  185. case MAX98504_DAI_ID_PDM:
  186. regmap_write(map, MAX98504_PDM_TX_ENABLE, tx_mask);
  187. break;
  188. default:
  189. WARN_ON(1);
  190. }
  191. return 0;
  192. }
  193. static int max98504_set_channel_map(struct snd_soc_dai *dai,
  194. unsigned int tx_num, unsigned int *tx_slot,
  195. unsigned int rx_num, unsigned int *rx_slot)
  196. {
  197. struct max98504_priv *max98504 = snd_soc_dai_get_drvdata(dai);
  198. struct regmap *map = max98504->regmap;
  199. unsigned int i, sources = 0;
  200. for (i = 0; i < tx_num; i++)
  201. if (tx_slot[i])
  202. sources |= (1 << i);
  203. switch (dai->id) {
  204. case MAX98504_DAI_ID_PCM:
  205. regmap_write(map, MAX98504_PCM_TX_CHANNEL_SOURCES,
  206. sources);
  207. break;
  208. case MAX98504_DAI_ID_PDM:
  209. regmap_write(map, MAX98504_PDM_TX_CONTROL, sources);
  210. break;
  211. default:
  212. WARN_ON(1);
  213. }
  214. regmap_write(map, MAX98504_MEASUREMENT_ENABLE, sources ? 0x3 : 0x01);
  215. return 0;
  216. }
  217. static const struct snd_soc_dai_ops max98504_dai_ops = {
  218. .set_tdm_slot = max98504_set_tdm_slot,
  219. .set_channel_map = max98504_set_channel_map,
  220. };
  221. #define MAX98504_FORMATS (SNDRV_PCM_FMTBIT_S8|SNDRV_PCM_FMTBIT_S16_LE|\
  222. SNDRV_PCM_FMTBIT_S24_LE|SNDRV_PCM_FMTBIT_S32_LE)
  223. #define MAX98504_PDM_RATES (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|\
  224. SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|\
  225. SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|\
  226. SNDRV_PCM_RATE_96000)
  227. static struct snd_soc_dai_driver max98504_dai[] = {
  228. /* TODO: Add the PCM interface definitions */
  229. {
  230. .name = "max98504-aif2",
  231. .id = MAX98504_DAI_ID_PDM,
  232. .playback = {
  233. .stream_name = "AIF2 Playback",
  234. .channels_min = 1,
  235. .channels_max = 2,
  236. .rates = MAX98504_PDM_RATES,
  237. .formats = MAX98504_FORMATS,
  238. },
  239. .capture = {
  240. .stream_name = "AIF2 Capture",
  241. .channels_min = 1,
  242. .channels_max = 2,
  243. .rates = MAX98504_PDM_RATES,
  244. .formats = MAX98504_FORMATS,
  245. },
  246. .ops = &max98504_dai_ops,
  247. },
  248. };
  249. static const struct snd_soc_component_driver max98504_component_driver = {
  250. .probe = max98504_component_probe,
  251. .remove = max98504_component_remove,
  252. .dapm_widgets = max98504_dapm_widgets,
  253. .num_dapm_widgets = ARRAY_SIZE(max98504_dapm_widgets),
  254. .dapm_routes = max98504_dapm_routes,
  255. .num_dapm_routes = ARRAY_SIZE(max98504_dapm_routes),
  256. };
  257. static const struct regmap_config max98504_regmap = {
  258. .reg_bits = 16,
  259. .val_bits = 8,
  260. .max_register = MAX98504_MAX_REGISTER,
  261. .reg_defaults = max98504_reg_defaults,
  262. .num_reg_defaults = ARRAY_SIZE(max98504_reg_defaults),
  263. .volatile_reg = max98504_volatile_register,
  264. .readable_reg = max98504_readable_register,
  265. .cache_type = REGCACHE_RBTREE,
  266. };
  267. static int max98504_i2c_probe(struct i2c_client *client,
  268. const struct i2c_device_id *id)
  269. {
  270. struct device *dev = &client->dev;
  271. struct device_node *node = dev->of_node;
  272. struct max98504_priv *max98504;
  273. int i, ret;
  274. max98504 = devm_kzalloc(dev, sizeof(*max98504), GFP_KERNEL);
  275. if (!max98504)
  276. return -ENOMEM;
  277. if (node) {
  278. if (!of_property_read_u32(node, "maxim,brownout-threshold",
  279. &max98504->brownout_threshold))
  280. max98504->brownout_enable = true;
  281. of_property_read_u32(node, "maxim,brownout-attenuation",
  282. &max98504->brownout_attenuation);
  283. of_property_read_u32(node, "maxim,brownout-attack-hold-ms",
  284. &max98504->brownout_attack_hold);
  285. of_property_read_u32(node, "maxim,brownout-timed-hold-ms",
  286. &max98504->brownout_timed_hold);
  287. of_property_read_u32(node, "maxim,brownout-release-rate-ms",
  288. &max98504->brownout_release_rate);
  289. }
  290. max98504->regmap = devm_regmap_init_i2c(client, &max98504_regmap);
  291. if (IS_ERR(max98504->regmap)) {
  292. ret = PTR_ERR(max98504->regmap);
  293. dev_err(&client->dev, "regmap initialization failed: %d\n", ret);
  294. return ret;
  295. }
  296. for (i = 0; i < MAX98504_NUM_SUPPLIES; i++)
  297. max98504->supplies[i].supply = max98504_supply_names[i];
  298. ret = devm_regulator_bulk_get(dev, MAX98504_NUM_SUPPLIES,
  299. max98504->supplies);
  300. if (ret < 0)
  301. return ret;
  302. i2c_set_clientdata(client, max98504);
  303. return devm_snd_soc_register_component(dev, &max98504_component_driver,
  304. max98504_dai, ARRAY_SIZE(max98504_dai));
  305. }
  306. #ifdef CONFIG_OF
  307. static const struct of_device_id max98504_of_match[] = {
  308. { .compatible = "maxim,max98504" },
  309. { },
  310. };
  311. MODULE_DEVICE_TABLE(of, max98504_of_match);
  312. #endif
  313. static const struct i2c_device_id max98504_i2c_id[] = {
  314. { "max98504" },
  315. { }
  316. };
  317. MODULE_DEVICE_TABLE(i2c, max98504_i2c_id);
  318. static struct i2c_driver max98504_i2c_driver = {
  319. .driver = {
  320. .name = "max98504",
  321. .of_match_table = of_match_ptr(max98504_of_match),
  322. },
  323. .probe = max98504_i2c_probe,
  324. .id_table = max98504_i2c_id,
  325. };
  326. module_i2c_driver(max98504_i2c_driver);
  327. MODULE_DESCRIPTION("ASoC MAX98504 driver");
  328. MODULE_LICENSE("GPL");