simple-card.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * ASoC simple sound card support
  3. *
  4. * Copyright (C) 2012 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  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/clk.h>
  12. #include <linux/device.h>
  13. #include <linux/gpio.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/string.h>
  19. #include <sound/jack.h>
  20. #include <sound/simple_card.h>
  21. #include <sound/soc-dai.h>
  22. #include <sound/soc.h>
  23. struct asoc_simple_jack {
  24. struct snd_soc_jack jack;
  25. struct snd_soc_jack_pin pin;
  26. struct snd_soc_jack_gpio gpio;
  27. };
  28. struct simple_card_data {
  29. struct snd_soc_card snd_card;
  30. struct simple_dai_props {
  31. struct asoc_simple_dai cpu_dai;
  32. struct asoc_simple_dai codec_dai;
  33. unsigned int mclk_fs;
  34. } *dai_props;
  35. unsigned int mclk_fs;
  36. struct asoc_simple_jack hp_jack;
  37. struct asoc_simple_jack mic_jack;
  38. struct snd_soc_dai_link *dai_link;
  39. };
  40. #define simple_priv_to_dev(priv) ((priv)->snd_card.dev)
  41. #define simple_priv_to_link(priv, i) ((priv)->snd_card.dai_link + (i))
  42. #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i))
  43. #define DAI "sound-dai"
  44. #define CELL "#sound-dai-cells"
  45. #define PREFIX "simple-audio-card,"
  46. #define asoc_simple_card_init_hp(card, sjack, prefix)\
  47. asoc_simple_card_init_jack(card, sjack, 1, prefix)
  48. #define asoc_simple_card_init_mic(card, sjack, prefix)\
  49. asoc_simple_card_init_jack(card, sjack, 0, prefix)
  50. static int asoc_simple_card_init_jack(struct snd_soc_card *card,
  51. struct asoc_simple_jack *sjack,
  52. int is_hp, char *prefix)
  53. {
  54. struct device *dev = card->dev;
  55. enum of_gpio_flags flags;
  56. char prop[128];
  57. char *pin_name;
  58. char *gpio_name;
  59. int mask;
  60. int det;
  61. sjack->gpio.gpio = -ENOENT;
  62. if (is_hp) {
  63. snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix);
  64. pin_name = "Headphones";
  65. gpio_name = "Headphone detection";
  66. mask = SND_JACK_HEADPHONE;
  67. } else {
  68. snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix);
  69. pin_name = "Mic Jack";
  70. gpio_name = "Mic detection";
  71. mask = SND_JACK_MICROPHONE;
  72. }
  73. det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags);
  74. if (det == -EPROBE_DEFER)
  75. return -EPROBE_DEFER;
  76. if (gpio_is_valid(det)) {
  77. sjack->pin.pin = pin_name;
  78. sjack->pin.mask = mask;
  79. sjack->gpio.name = gpio_name;
  80. sjack->gpio.report = mask;
  81. sjack->gpio.gpio = det;
  82. sjack->gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW);
  83. sjack->gpio.debounce_time = 150;
  84. snd_soc_card_jack_new(card, pin_name, mask,
  85. &sjack->jack,
  86. &sjack->pin, 1);
  87. snd_soc_jack_add_gpios(&sjack->jack, 1,
  88. &sjack->gpio);
  89. }
  90. return 0;
  91. }
  92. static void asoc_simple_card_remove_jack(struct asoc_simple_jack *sjack)
  93. {
  94. if (gpio_is_valid(sjack->gpio.gpio))
  95. snd_soc_jack_free_gpios(&sjack->jack, 1, &sjack->gpio);
  96. }
  97. static int asoc_simple_card_startup(struct snd_pcm_substream *substream)
  98. {
  99. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100. struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  101. struct simple_dai_props *dai_props =
  102. simple_priv_to_props(priv, rtd->num);
  103. int ret;
  104. ret = clk_prepare_enable(dai_props->cpu_dai.clk);
  105. if (ret)
  106. return ret;
  107. ret = clk_prepare_enable(dai_props->codec_dai.clk);
  108. if (ret)
  109. clk_disable_unprepare(dai_props->cpu_dai.clk);
  110. return ret;
  111. }
  112. static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream)
  113. {
  114. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  115. struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  116. struct simple_dai_props *dai_props =
  117. simple_priv_to_props(priv, rtd->num);
  118. clk_disable_unprepare(dai_props->cpu_dai.clk);
  119. clk_disable_unprepare(dai_props->codec_dai.clk);
  120. }
  121. static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
  122. struct snd_pcm_hw_params *params)
  123. {
  124. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  125. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  126. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  127. struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  128. struct simple_dai_props *dai_props =
  129. simple_priv_to_props(priv, rtd->num);
  130. unsigned int mclk, mclk_fs = 0;
  131. int ret = 0;
  132. if (priv->mclk_fs)
  133. mclk_fs = priv->mclk_fs;
  134. else if (dai_props->mclk_fs)
  135. mclk_fs = dai_props->mclk_fs;
  136. if (mclk_fs) {
  137. mclk = params_rate(params) * mclk_fs;
  138. ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  139. SND_SOC_CLOCK_IN);
  140. if (ret && ret != -ENOTSUPP)
  141. goto err;
  142. ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
  143. SND_SOC_CLOCK_OUT);
  144. if (ret && ret != -ENOTSUPP)
  145. goto err;
  146. }
  147. return 0;
  148. err:
  149. return ret;
  150. }
  151. static struct snd_soc_ops asoc_simple_card_ops = {
  152. .startup = asoc_simple_card_startup,
  153. .shutdown = asoc_simple_card_shutdown,
  154. .hw_params = asoc_simple_card_hw_params,
  155. };
  156. static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
  157. {
  158. struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  159. struct snd_soc_dai *codec = rtd->codec_dai;
  160. struct snd_soc_dai *cpu = rtd->cpu_dai;
  161. struct simple_dai_props *dai_props =
  162. simple_priv_to_props(priv, rtd->num);
  163. int ret;
  164. ret = asoc_simple_card_init_dai(codec, &dai_props->codec_dai);
  165. if (ret < 0)
  166. return ret;
  167. ret = asoc_simple_card_init_dai(cpu, &dai_props->cpu_dai);
  168. if (ret < 0)
  169. return ret;
  170. ret = asoc_simple_card_init_hp(rtd->card, &priv->hp_jack, PREFIX);
  171. if (ret < 0)
  172. return ret;
  173. ret = asoc_simple_card_init_mic(rtd->card, &priv->mic_jack, PREFIX);
  174. if (ret < 0)
  175. return ret;
  176. return 0;
  177. }
  178. static int asoc_simple_card_dai_link_of(struct device_node *node,
  179. struct simple_card_data *priv,
  180. int idx,
  181. bool is_top_level_node)
  182. {
  183. struct device *dev = simple_priv_to_dev(priv);
  184. struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
  185. struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
  186. struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai;
  187. struct asoc_simple_dai *codec_dai = &dai_props->codec_dai;
  188. struct device_node *cpu = NULL;
  189. struct device_node *plat = NULL;
  190. struct device_node *codec = NULL;
  191. char prop[128];
  192. char *prefix = "";
  193. int ret, single_cpu;
  194. /* For single DAI link & old style of DT node */
  195. if (is_top_level_node)
  196. prefix = PREFIX;
  197. snprintf(prop, sizeof(prop), "%scpu", prefix);
  198. cpu = of_get_child_by_name(node, prop);
  199. if (!cpu) {
  200. ret = -EINVAL;
  201. dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
  202. goto dai_link_of_err;
  203. }
  204. snprintf(prop, sizeof(prop), "%splat", prefix);
  205. plat = of_get_child_by_name(node, prop);
  206. snprintf(prop, sizeof(prop), "%scodec", prefix);
  207. codec = of_get_child_by_name(node, prop);
  208. if (!codec) {
  209. ret = -EINVAL;
  210. dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
  211. goto dai_link_of_err;
  212. }
  213. ret = asoc_simple_card_parse_daifmt(dev, node, codec,
  214. prefix, &dai_link->dai_fmt);
  215. if (ret < 0)
  216. goto dai_link_of_err;
  217. of_property_read_u32(node, "mclk-fs", &dai_props->mclk_fs);
  218. ret = asoc_simple_card_parse_cpu(cpu, dai_link,
  219. DAI, CELL, &single_cpu);
  220. if (ret < 0)
  221. goto dai_link_of_err;
  222. ret = asoc_simple_card_parse_codec(codec, dai_link, DAI, CELL);
  223. if (ret < 0)
  224. goto dai_link_of_err;
  225. ret = asoc_simple_card_parse_platform(plat, dai_link, DAI, CELL);
  226. if (ret < 0)
  227. goto dai_link_of_err;
  228. ret = snd_soc_of_parse_tdm_slot(cpu, &cpu_dai->tx_slot_mask,
  229. &cpu_dai->rx_slot_mask,
  230. &cpu_dai->slots,
  231. &cpu_dai->slot_width);
  232. if (ret < 0)
  233. goto dai_link_of_err;
  234. ret = snd_soc_of_parse_tdm_slot(codec, &codec_dai->tx_slot_mask,
  235. &codec_dai->rx_slot_mask,
  236. &codec_dai->slots,
  237. &codec_dai->slot_width);
  238. if (ret < 0)
  239. goto dai_link_of_err;
  240. ret = asoc_simple_card_parse_clk_cpu(cpu, dai_link, cpu_dai);
  241. if (ret < 0)
  242. goto dai_link_of_err;
  243. ret = asoc_simple_card_parse_clk_codec(codec, dai_link, codec_dai);
  244. if (ret < 0)
  245. goto dai_link_of_err;
  246. ret = asoc_simple_card_canonicalize_dailink(dai_link);
  247. if (ret < 0)
  248. goto dai_link_of_err;
  249. ret = asoc_simple_card_set_dailink_name(dev, dai_link,
  250. "%s-%s",
  251. dai_link->cpu_dai_name,
  252. dai_link->codec_dai_name);
  253. if (ret < 0)
  254. goto dai_link_of_err;
  255. dai_link->ops = &asoc_simple_card_ops;
  256. dai_link->init = asoc_simple_card_dai_init;
  257. dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
  258. dev_dbg(dev, "\tformat : %04x\n", dai_link->dai_fmt);
  259. dev_dbg(dev, "\tcpu : %s / %d\n",
  260. dai_link->cpu_dai_name,
  261. dai_props->cpu_dai.sysclk);
  262. dev_dbg(dev, "\tcodec : %s / %d\n",
  263. dai_link->codec_dai_name,
  264. dai_props->codec_dai.sysclk);
  265. asoc_simple_card_canonicalize_cpu(dai_link, single_cpu);
  266. dai_link_of_err:
  267. of_node_put(cpu);
  268. of_node_put(codec);
  269. return ret;
  270. }
  271. static int asoc_simple_card_parse_aux_devs(struct device_node *node,
  272. struct simple_card_data *priv)
  273. {
  274. struct device *dev = simple_priv_to_dev(priv);
  275. struct device_node *aux_node;
  276. int i, n, len;
  277. if (!of_find_property(node, PREFIX "aux-devs", &len))
  278. return 0; /* Ok to have no aux-devs */
  279. n = len / sizeof(__be32);
  280. if (n <= 0)
  281. return -EINVAL;
  282. priv->snd_card.aux_dev = devm_kzalloc(dev,
  283. n * sizeof(*priv->snd_card.aux_dev), GFP_KERNEL);
  284. if (!priv->snd_card.aux_dev)
  285. return -ENOMEM;
  286. for (i = 0; i < n; i++) {
  287. aux_node = of_parse_phandle(node, PREFIX "aux-devs", i);
  288. if (!aux_node)
  289. return -EINVAL;
  290. priv->snd_card.aux_dev[i].codec_of_node = aux_node;
  291. }
  292. priv->snd_card.num_aux_devs = n;
  293. return 0;
  294. }
  295. static int asoc_simple_card_parse_of(struct device_node *node,
  296. struct simple_card_data *priv)
  297. {
  298. struct device *dev = simple_priv_to_dev(priv);
  299. struct device_node *dai_link;
  300. int ret;
  301. if (!node)
  302. return -EINVAL;
  303. dai_link = of_get_child_by_name(node, PREFIX "dai-link");
  304. /* The off-codec widgets */
  305. if (of_property_read_bool(node, PREFIX "widgets")) {
  306. ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
  307. PREFIX "widgets");
  308. if (ret)
  309. goto card_parse_end;
  310. }
  311. /* DAPM routes */
  312. if (of_property_read_bool(node, PREFIX "routing")) {
  313. ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
  314. PREFIX "routing");
  315. if (ret)
  316. goto card_parse_end;
  317. }
  318. /* Factor to mclk, used in hw_params() */
  319. of_property_read_u32(node, PREFIX "mclk-fs", &priv->mclk_fs);
  320. /* Single/Muti DAI link(s) & New style of DT node */
  321. if (dai_link) {
  322. struct device_node *np = NULL;
  323. int i = 0;
  324. for_each_child_of_node(node, np) {
  325. dev_dbg(dev, "\tlink %d:\n", i);
  326. ret = asoc_simple_card_dai_link_of(np, priv,
  327. i, false);
  328. if (ret < 0) {
  329. of_node_put(np);
  330. goto card_parse_end;
  331. }
  332. i++;
  333. }
  334. } else {
  335. /* For single DAI link & old style of DT node */
  336. ret = asoc_simple_card_dai_link_of(node, priv, 0, true);
  337. if (ret < 0)
  338. goto card_parse_end;
  339. }
  340. ret = asoc_simple_card_parse_card_name(&priv->snd_card, PREFIX);
  341. if (ret < 0)
  342. goto card_parse_end;
  343. ret = asoc_simple_card_parse_aux_devs(node, priv);
  344. card_parse_end:
  345. of_node_put(dai_link);
  346. return ret;
  347. }
  348. static int asoc_simple_card_probe(struct platform_device *pdev)
  349. {
  350. struct simple_card_data *priv;
  351. struct snd_soc_dai_link *dai_link;
  352. struct simple_dai_props *dai_props;
  353. struct device_node *np = pdev->dev.of_node;
  354. struct device *dev = &pdev->dev;
  355. int num, ret;
  356. /* Get the number of DAI links */
  357. if (np && of_get_child_by_name(np, PREFIX "dai-link"))
  358. num = of_get_child_count(np);
  359. else
  360. num = 1;
  361. /* Allocate the private data and the DAI link array */
  362. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  363. if (!priv)
  364. return -ENOMEM;
  365. dai_props = devm_kzalloc(dev, sizeof(*dai_props) * num, GFP_KERNEL);
  366. dai_link = devm_kzalloc(dev, sizeof(*dai_link) * num, GFP_KERNEL);
  367. if (!dai_props || !dai_link)
  368. return -ENOMEM;
  369. priv->dai_props = dai_props;
  370. priv->dai_link = dai_link;
  371. /* Init snd_soc_card */
  372. priv->snd_card.owner = THIS_MODULE;
  373. priv->snd_card.dev = dev;
  374. priv->snd_card.dai_link = priv->dai_link;
  375. priv->snd_card.num_links = num;
  376. if (np && of_device_is_available(np)) {
  377. ret = asoc_simple_card_parse_of(np, priv);
  378. if (ret < 0) {
  379. if (ret != -EPROBE_DEFER)
  380. dev_err(dev, "parse error %d\n", ret);
  381. goto err;
  382. }
  383. } else {
  384. struct asoc_simple_card_info *cinfo;
  385. cinfo = dev->platform_data;
  386. if (!cinfo) {
  387. dev_err(dev, "no info for asoc-simple-card\n");
  388. return -EINVAL;
  389. }
  390. if (!cinfo->name ||
  391. !cinfo->codec_dai.name ||
  392. !cinfo->codec ||
  393. !cinfo->platform ||
  394. !cinfo->cpu_dai.name) {
  395. dev_err(dev, "insufficient asoc_simple_card_info settings\n");
  396. return -EINVAL;
  397. }
  398. priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
  399. dai_link->name = cinfo->name;
  400. dai_link->stream_name = cinfo->name;
  401. dai_link->platform_name = cinfo->platform;
  402. dai_link->codec_name = cinfo->codec;
  403. dai_link->cpu_dai_name = cinfo->cpu_dai.name;
  404. dai_link->codec_dai_name = cinfo->codec_dai.name;
  405. dai_link->dai_fmt = cinfo->daifmt;
  406. dai_link->init = asoc_simple_card_dai_init;
  407. memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
  408. sizeof(priv->dai_props->cpu_dai));
  409. memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
  410. sizeof(priv->dai_props->codec_dai));
  411. }
  412. snd_soc_card_set_drvdata(&priv->snd_card, priv);
  413. ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
  414. if (ret >= 0)
  415. return ret;
  416. err:
  417. asoc_simple_card_clean_reference(&priv->snd_card);
  418. return ret;
  419. }
  420. static int asoc_simple_card_remove(struct platform_device *pdev)
  421. {
  422. struct snd_soc_card *card = platform_get_drvdata(pdev);
  423. struct simple_card_data *priv = snd_soc_card_get_drvdata(card);
  424. asoc_simple_card_remove_jack(&priv->hp_jack);
  425. asoc_simple_card_remove_jack(&priv->mic_jack);
  426. return asoc_simple_card_clean_reference(card);
  427. }
  428. static const struct of_device_id asoc_simple_of_match[] = {
  429. { .compatible = "simple-audio-card", },
  430. {},
  431. };
  432. MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
  433. static struct platform_driver asoc_simple_card = {
  434. .driver = {
  435. .name = "asoc-simple-card",
  436. .pm = &snd_soc_pm_ops,
  437. .of_match_table = asoc_simple_of_match,
  438. },
  439. .probe = asoc_simple_card_probe,
  440. .remove = asoc_simple_card_remove,
  441. };
  442. module_platform_driver(asoc_simple_card);
  443. MODULE_ALIAS("platform:asoc-simple-card");
  444. MODULE_LICENSE("GPL v2");
  445. MODULE_DESCRIPTION("ASoC Simple Sound Card");
  446. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");