ark1668e_audio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * ark1668e audio driver -- ALSA SoC audio for ark1668e
  3. *
  4. */
  5. #include <linux/module.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/io.h>
  8. #include <sound/core.h>
  9. #include <sound/pcm.h>
  10. #include <sound/soc.h>
  11. #include <sound/soc-dapm.h>
  12. #include <linux/clk.h>
  13. #define ARK_OUT_HP 0
  14. #define ARK_OUT_SPK 1
  15. #define ARK_STREAM_AVIN1 0
  16. #define ARK_STREAM_AVIN2 1
  17. #define ARK_STREAM_PLATFORM 2
  18. #define ARK_STREAM_BLUETOOTH 3
  19. static int ark_ir_ch_sel = 0;
  20. static int ark_ir_en_sel = 0;
  21. static int ark_out_sel = ARK_OUT_SPK;
  22. static int ark_stream_sel = ARK_STREAM_PLATFORM;
  23. static int ark_amp_volume = 80;
  24. static int ark_fm_freq = 765;
  25. static int ark_pa_mute = 0;
  26. enum {
  27. DAI_LINK_I2S0_OUT,
  28. DAI_LINK_I2S0_IN,
  29. DAI_LINK_I2S1,
  30. DAI_LINK_I2S2,
  31. };
  32. static int ark_startup(struct snd_pcm_substream *substream)
  33. {
  34. int ret = 0;
  35. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  36. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  37. /* variable MCLK */
  38. ret = snd_soc_dai_set_sysclk(codec_dai, 0, 0, SND_SOC_CLOCK_IN);
  39. return ret;
  40. }
  41. #define SYS_BASE 0x40408000
  42. #define DAC_I2S_NCO_CFG 0x16c
  43. #define ADC_I2S_NCO_CFG 0x15c
  44. #define I2S_CLK_FREQ 240000000
  45. static int ark_hw_params(
  46. struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
  47. {
  48. unsigned int val;
  49. void *sysreg;
  50. u32 rate = params_rate(params);
  51. u32 step, modulo = 18750;
  52. step = rate / (I2S_CLK_FREQ / 256 / 2 / modulo);
  53. val = (step << 16) | modulo;
  54. sysreg = ioremap(SYS_BASE, 0x1000);
  55. if (!sysreg) {
  56. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  57. writel(val, sysreg + DAC_I2S_NCO_CFG);
  58. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  59. writel(val, sysreg + ADC_I2S_NCO_CFG);
  60. }
  61. iounmap(sysreg);
  62. }
  63. return 0;
  64. }
  65. static struct snd_soc_ops ark_ops = {
  66. .startup = ark_startup,
  67. .hw_params = ark_hw_params,
  68. };
  69. static int ark_get_output (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  70. {
  71. ucontrol->value.integer.value[0] = ark_out_sel;
  72. return 0;
  73. }
  74. static int ark_set_output (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  75. {
  76. //struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  77. if (ark_out_sel == ucontrol->value.integer.value[0])
  78. return 0;
  79. ark_out_sel = ucontrol->value.integer.value[0];
  80. return 0;
  81. }
  82. static int ark_get_stream (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  83. {
  84. ucontrol->value.integer.value[0] = ark_stream_sel;
  85. return 0;
  86. }
  87. static int ark_set_stream (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  88. {
  89. //struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  90. if (ark_stream_sel == ucontrol->value.integer.value[0])
  91. return 0;
  92. ark_stream_sel = ucontrol->value.integer.value[0];
  93. return 0;
  94. }
  95. static int ark_get_ir_status (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  96. {
  97. ucontrol->value.integer.value[0] = ark_ir_en_sel;
  98. return 0;
  99. }
  100. static int ark_set_ir_status (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  101. {
  102. //struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  103. if (ark_ir_en_sel == ucontrol->value.integer.value[0])
  104. return 0;
  105. ark_ir_en_sel = ucontrol->value.integer.value[0];
  106. return 0;
  107. }
  108. static int ark_get_ir_channel (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  109. {
  110. ucontrol->value.integer.value[0] = ark_ir_ch_sel;
  111. return 0;
  112. }
  113. static int ark_set_ir_channel (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  114. {
  115. //struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  116. if (ark_ir_en_sel == 0 || ark_ir_ch_sel == ucontrol->value.integer.value[0])
  117. return 0;
  118. ark_ir_ch_sel = ucontrol->value.integer.value[0];
  119. return 0;
  120. }
  121. static int ark_get_pa_mute_status (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  122. {
  123. ucontrol->value.integer.value[0] = ark_pa_mute;
  124. return 0;
  125. }
  126. static int ark_set_pa_mute_status (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  127. {
  128. //struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  129. if (ark_pa_mute == ucontrol->value.integer.value[0])
  130. return 0;
  131. ark_pa_mute = ucontrol->value.integer.value[0];
  132. return 0;
  133. }
  134. static int ark_get_amp_volume (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  135. {
  136. ucontrol->value.integer.value[0] = ark_amp_volume;
  137. return 0;
  138. }
  139. static int ark_set_amp_volume (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  140. {
  141. //struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  142. if (ark_amp_volume == ucontrol->value.integer.value[0])
  143. return 0;
  144. ark_amp_volume = ucontrol->value.integer.value[0];
  145. return 0;
  146. }
  147. static int ark_get_fm_freq (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  148. {
  149. ucontrol->value.integer.value[0] = ark_fm_freq;
  150. return 0;
  151. }
  152. extern void fm_set_transfer_rate(int freq);
  153. static int ark_set_fm_freq (struct snd_kcontrol * kcontrol, struct snd_ctl_elem_value * ucontrol)
  154. {
  155. //struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  156. if (ark_fm_freq == ucontrol->value.integer.value[0])
  157. return 0;
  158. ark_fm_freq = ucontrol->value.integer.value[0];
  159. return 0;
  160. }
  161. static const char *stream_select[] = {
  162. "AVIN1",
  163. "AVIN2",
  164. "Platform",
  165. "Bluetooth"
  166. };
  167. static const char *out_select[] = {
  168. "Headphone Jack",
  169. "Ext Spk",
  170. };
  171. static const char *ir_status_select[] = {
  172. "IR_Off",
  173. "IR_On",
  174. };
  175. static const char *ir_ch_select[] = {
  176. "IR_ch0",
  177. "IR_ch1",
  178. };
  179. static const char *pa_mute_switch[] = {
  180. "PA_Mute_Off",
  181. "PA_Mute_On",
  182. };
  183. static const struct soc_enum ark_stream_sel_enum =
  184. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(stream_select), stream_select);
  185. static const struct soc_enum ark_out_sel_enum =
  186. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(out_select), out_select);
  187. static const struct soc_enum ark_ir_status_sel_enum =
  188. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ir_status_select), ir_status_select);
  189. static const struct soc_enum ark_ir_ch_sel_enum =
  190. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ir_ch_select), ir_ch_select);
  191. static const struct soc_enum ark_pa_mute_switch_enum =
  192. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(pa_mute_switch), pa_mute_switch);
  193. static const struct snd_soc_dapm_widget ark_dapm_widgets[] = {
  194. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  195. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  196. };
  197. static const struct snd_soc_dapm_route ark_audio_map[] = {
  198. {"Headphone Jack", NULL, "LOUT"},
  199. {"Headphone Jack", NULL, "ROUT"},
  200. {"Ext Spk", NULL, "ROUT"},
  201. {"Ext Spk", NULL, "LOUT"},
  202. };
  203. static const struct snd_kcontrol_new ark_controls[] = {
  204. SOC_ENUM_EXT("Stream Select", ark_stream_sel_enum,
  205. ark_get_stream, ark_set_stream),
  206. SOC_ENUM_EXT("Output Select", ark_out_sel_enum,
  207. ark_get_output, ark_set_output),
  208. SOC_SINGLE_EXT("AMP Volume", 0, 0, 99, 0,
  209. ark_get_amp_volume, ark_set_amp_volume),
  210. SOC_SINGLE_EXT("FM Freq", 0, 0, 1272, 0,
  211. ark_get_fm_freq, ark_set_fm_freq),
  212. SOC_ENUM_EXT("IR Status", ark_ir_status_sel_enum,
  213. ark_get_ir_status, ark_set_ir_status),
  214. SOC_ENUM_EXT("IR Channel", ark_ir_ch_sel_enum,
  215. ark_get_ir_channel, ark_set_ir_channel),
  216. SOC_ENUM_EXT("PA Mute", ark_pa_mute_switch_enum,
  217. ark_get_pa_mute_status, ark_set_pa_mute_status),
  218. };
  219. static struct snd_soc_dai_link ark_dai[] = {
  220. /* cpu dai
  221. {
  222. .name = "sddac",
  223. .stream_name = "sddac",
  224. .codec_dai_name = "ark-sddac-codec",
  225. .cpu_dai_name = "ark1668e-i2s",//ark-i2s-dac
  226. .codec_name = "ark-sddac",
  227. .ops = &ark_ops,
  228. .dai_fmt = SND_SOC_DAIFMT_I2S,
  229. },
  230. {
  231. .name = "sdadc",
  232. .stream_name = "sdadc",
  233. .codec_dai_name = "ark-sdadc-codec",
  234. .cpu_dai_name = "ark1668e-i2s",//ark-i2s-adc
  235. .codec_name = "ark-sdadc",
  236. .ops = &ark_ops,
  237. .dai_fmt = SND_SOC_DAIFMT_I2S,
  238. },
  239. */
  240. /* codec dai */
  241. {
  242. .name = "ES8316",
  243. .stream_name = "ES8316",
  244. .codec_dai_name = "es8316-hifi", //es8316 dai_driver
  245. .cpu_dai_name = "ark1668e-i2s", //cpu dai_driver
  246. .codec_name = "es8316_codec", //Ö¸¶¨codec codec:device_id
  247. .ops = &ark_ops,
  248. .dai_fmt = SND_SOC_DAIFMT_I2S,
  249. },
  250. };
  251. #if 0
  252. static struct snd_soc_dai_link ark_dai_es8316[] = {
  253. {
  254. .name = "ES8316",
  255. .stream_name = "ES8316",
  256. .codec_dai_name = "ES8316-HiFi", //es8316 dai_driver
  257. .cpu_dai_name = "ark1668e-i2s", //cpu dai_driver
  258. .codec_name = "es8316_codec", //Ö¸¶¨codec codec:device_id
  259. .ops = &ark_ops,
  260. .dai_fmt = SND_SOC_DAIFMT_I2S,
  261. },
  262. };
  263. #endif
  264. static struct snd_soc_card ark1668e_audio = {
  265. .name = "arkn1668e-audio",/* proc/asound/cards */
  266. .owner = THIS_MODULE,
  267. .dai_link = ark_dai,
  268. .num_links = ARRAY_SIZE(ark_dai),
  269. .controls = ark_controls,
  270. .num_controls = ARRAY_SIZE(ark_controls),
  271. .dapm_widgets = ark_dapm_widgets,
  272. .num_dapm_widgets = ARRAY_SIZE(ark_dapm_widgets),
  273. .dapm_routes = ark_audio_map,
  274. .num_dapm_routes = ARRAY_SIZE(ark_audio_map),
  275. #if 0
  276. {
  277. .name = "es8316",/* proc/asound/cards */
  278. .owner = THIS_MODULE,
  279. .dai_link = ark_dai_es8316,
  280. .num_links = ARRAY_SIZE(ark_dai_es8316),
  281. .controls = ark_controls,
  282. .num_controls = ARRAY_SIZE(ark_controls),
  283. .dapm_widgets = ark_dapm_widgets,
  284. .num_dapm_widgets = ARRAY_SIZE(ark_dapm_widgets),
  285. .dapm_routes = ark_audio_map,
  286. .num_dapm_routes = ARRAY_SIZE(ark_audio_map),
  287. },
  288. #endif
  289. };
  290. static struct platform_device *ark1668e_snd_device;
  291. static int ark1668e_audio_probe(struct platform_device *pdev)
  292. {printk("==============>[%s]\n",__func__);
  293. #if 1
  294. struct snd_soc_card *card = &ark1668e_audio;
  295. int ret;
  296. card->dev = &pdev->dev;
  297. ret = devm_snd_soc_register_card(&pdev->dev, card);
  298. if (ret)
  299. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  300. ret);
  301. // ark1668e_audio[pdev->id].dev = &pdev->dev;
  302. // ret = devm_snd_soc_register_card(&pdev->dev, &ark1668e_audio[pdev->id]);//×¢²áÉù¿¨Çý¶¯
  303. // if (ret)
  304. // dev_err(&pdev->dev,
  305. // "snd_soc_register_card(%s) failed: %d\n",
  306. // ark1668e_audio[pdev->id].name, ret);
  307. #else
  308. #endif
  309. return ret;
  310. }
  311. static const struct of_device_id ark1668e_audio_of_match[] = {
  312. {
  313. .compatible = "simple-audio-card",
  314. },
  315. {
  316. /* sentinel */
  317. }
  318. };
  319. MODULE_DEVICE_TABLE(of, ark1668e_audio_of_match);
  320. static struct platform_driver ark1668e_audio_driver = {
  321. .probe = ark1668e_audio_probe,
  322. .driver = {
  323. .name = "ark1668e-audio",
  324. .of_match_table = of_match_ptr(ark1668e_audio_of_match),
  325. },
  326. };
  327. module_platform_driver(ark1668e_audio_driver);
  328. MODULE_DESCRIPTION("Arkmicro audio driver under ALSA SoC architecture");
  329. MODULE_AUTHOR("Sim");
  330. MODULE_LICENSE("GPL v2");