simple-card-utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // simple-card-utils.c
  4. //
  5. // Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. #include <linux/clk.h>
  7. #include <linux/gpio.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_gpio.h>
  12. #include <linux/of_graph.h>
  13. #include <sound/jack.h>
  14. #include <sound/simple_card_utils.h>
  15. void asoc_simple_card_convert_fixup(struct asoc_simple_card_data *data,
  16. struct snd_pcm_hw_params *params)
  17. {
  18. struct snd_interval *rate = hw_param_interval(params,
  19. SNDRV_PCM_HW_PARAM_RATE);
  20. struct snd_interval *channels = hw_param_interval(params,
  21. SNDRV_PCM_HW_PARAM_CHANNELS);
  22. if (data->convert_rate)
  23. rate->min =
  24. rate->max = data->convert_rate;
  25. if (data->convert_channels)
  26. channels->min =
  27. channels->max = data->convert_channels;
  28. }
  29. EXPORT_SYMBOL_GPL(asoc_simple_card_convert_fixup);
  30. void asoc_simple_card_parse_convert(struct device *dev, char *prefix,
  31. struct asoc_simple_card_data *data)
  32. {
  33. struct device_node *np = dev->of_node;
  34. char prop[128];
  35. if (!prefix)
  36. prefix = "";
  37. /* sampling rate convert */
  38. snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate");
  39. of_property_read_u32(np, prop, &data->convert_rate);
  40. /* channels transfer */
  41. snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels");
  42. of_property_read_u32(np, prop, &data->convert_channels);
  43. dev_dbg(dev, "convert_rate %d\n", data->convert_rate);
  44. dev_dbg(dev, "convert_channels %d\n", data->convert_channels);
  45. }
  46. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_convert);
  47. int asoc_simple_card_parse_daifmt(struct device *dev,
  48. struct device_node *node,
  49. struct device_node *codec,
  50. char *prefix,
  51. unsigned int *retfmt)
  52. {
  53. struct device_node *bitclkmaster = NULL;
  54. struct device_node *framemaster = NULL;
  55. unsigned int daifmt;
  56. daifmt = snd_soc_of_parse_daifmt(node, prefix,
  57. &bitclkmaster, &framemaster);
  58. daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  59. if (!bitclkmaster && !framemaster) {
  60. /*
  61. * No dai-link level and master setting was not found from
  62. * sound node level, revert back to legacy DT parsing and
  63. * take the settings from codec node.
  64. */
  65. dev_dbg(dev, "Revert to legacy daifmt parsing\n");
  66. daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
  67. (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
  68. } else {
  69. if (codec == bitclkmaster)
  70. daifmt |= (codec == framemaster) ?
  71. SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
  72. else
  73. daifmt |= (codec == framemaster) ?
  74. SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
  75. }
  76. of_node_put(bitclkmaster);
  77. of_node_put(framemaster);
  78. *retfmt = daifmt;
  79. dev_dbg(dev, "format : %04x\n", daifmt);
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt);
  83. int asoc_simple_card_set_dailink_name(struct device *dev,
  84. struct snd_soc_dai_link *dai_link,
  85. const char *fmt, ...)
  86. {
  87. va_list ap;
  88. char *name = NULL;
  89. int ret = -ENOMEM;
  90. va_start(ap, fmt);
  91. name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
  92. va_end(ap);
  93. if (name) {
  94. ret = 0;
  95. dai_link->name = name;
  96. dai_link->stream_name = name;
  97. dev_dbg(dev, "name : %s\n", name);
  98. }
  99. return ret;
  100. }
  101. EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name);
  102. int asoc_simple_card_parse_card_name(struct snd_soc_card *card,
  103. char *prefix)
  104. {
  105. int ret;
  106. if (!prefix)
  107. prefix = "";
  108. /* Parse the card name from DT */
  109. ret = snd_soc_of_parse_card_name(card, "label");
  110. if (ret < 0 || !card->name) {
  111. char prop[128];
  112. snprintf(prop, sizeof(prop), "%sname", prefix);
  113. ret = snd_soc_of_parse_card_name(card, prop);
  114. if (ret < 0)
  115. return ret;
  116. }
  117. if (!card->name && card->dai_link)
  118. card->name = card->dai_link->name;
  119. dev_dbg(card->dev, "Card Name: %s\n", card->name ? card->name : "");
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
  123. static void asoc_simple_card_clk_register(struct asoc_simple_dai *dai,
  124. struct clk *clk)
  125. {
  126. dai->clk = clk;
  127. }
  128. int asoc_simple_card_clk_enable(struct asoc_simple_dai *dai)
  129. {
  130. return clk_prepare_enable(dai->clk);
  131. }
  132. EXPORT_SYMBOL_GPL(asoc_simple_card_clk_enable);
  133. void asoc_simple_card_clk_disable(struct asoc_simple_dai *dai)
  134. {
  135. clk_disable_unprepare(dai->clk);
  136. }
  137. EXPORT_SYMBOL_GPL(asoc_simple_card_clk_disable);
  138. int asoc_simple_card_parse_clk(struct device *dev,
  139. struct device_node *node,
  140. struct device_node *dai_of_node,
  141. struct asoc_simple_dai *simple_dai,
  142. const char *dai_name,
  143. struct snd_soc_dai_link_component *dlc)
  144. {
  145. struct clk *clk;
  146. u32 val;
  147. /*
  148. * Use snd_soc_dai_link_component instead of legacy style.
  149. * It is only for codec, but cpu will be supported in the future.
  150. * see
  151. * soc-core.c :: snd_soc_init_multicodec()
  152. */
  153. if (dlc) {
  154. dai_of_node = dlc->of_node;
  155. dai_name = dlc->dai_name;
  156. }
  157. /*
  158. * Parse dai->sysclk come from "clocks = <&xxx>"
  159. * (if system has common clock)
  160. * or "system-clock-frequency = <xxx>"
  161. * or device's module clock.
  162. */
  163. clk = devm_get_clk_from_child(dev, node, NULL);
  164. if (!IS_ERR(clk)) {
  165. simple_dai->sysclk = clk_get_rate(clk);
  166. asoc_simple_card_clk_register(simple_dai, clk);
  167. } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
  168. simple_dai->sysclk = val;
  169. } else {
  170. clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
  171. if (!IS_ERR(clk))
  172. simple_dai->sysclk = clk_get_rate(clk);
  173. }
  174. if (of_property_read_bool(node, "system-clock-direction-out"))
  175. simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
  176. dev_dbg(dev, "%s : sysclk = %d, direction %d\n", dai_name,
  177. simple_dai->sysclk, simple_dai->clk_direction);
  178. return 0;
  179. }
  180. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
  181. int asoc_simple_card_parse_dai(struct device_node *node,
  182. struct snd_soc_dai_link_component *dlc,
  183. struct device_node **dai_of_node,
  184. const char **dai_name,
  185. const char *list_name,
  186. const char *cells_name,
  187. int *is_single_link)
  188. {
  189. struct of_phandle_args args;
  190. int ret;
  191. if (!node)
  192. return 0;
  193. /*
  194. * Use snd_soc_dai_link_component instead of legacy style.
  195. * It is only for codec, but cpu will be supported in the future.
  196. * see
  197. * soc-core.c :: snd_soc_init_multicodec()
  198. */
  199. if (dlc) {
  200. dai_name = &dlc->dai_name;
  201. dai_of_node = &dlc->of_node;
  202. }
  203. /*
  204. * Get node via "sound-dai = <&phandle port>"
  205. * it will be used as xxx_of_node on soc_bind_dai_link()
  206. */
  207. ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
  208. if (ret)
  209. return ret;
  210. /* Get dai->name */
  211. if (dai_name) {
  212. ret = snd_soc_of_get_dai_name(node, dai_name);
  213. if (ret < 0)
  214. return ret;
  215. }
  216. *dai_of_node = args.np;
  217. if (is_single_link)
  218. *is_single_link = !args.args_count;
  219. return 0;
  220. }
  221. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
  222. static int asoc_simple_card_get_dai_id(struct device_node *ep)
  223. {
  224. struct device_node *node;
  225. struct device_node *endpoint;
  226. int i, id;
  227. int ret;
  228. ret = snd_soc_get_dai_id(ep);
  229. if (ret != -ENOTSUPP)
  230. return ret;
  231. node = of_graph_get_port_parent(ep);
  232. /*
  233. * Non HDMI sound case, counting port/endpoint on its DT
  234. * is enough. Let's count it.
  235. */
  236. i = 0;
  237. id = -1;
  238. for_each_endpoint_of_node(node, endpoint) {
  239. if (endpoint == ep)
  240. id = i;
  241. i++;
  242. }
  243. of_node_put(node);
  244. if (id < 0)
  245. return -ENODEV;
  246. return id;
  247. }
  248. int asoc_simple_card_parse_graph_dai(struct device_node *ep,
  249. struct snd_soc_dai_link_component *dlc,
  250. struct device_node **dai_of_node,
  251. const char **dai_name)
  252. {
  253. struct device_node *node;
  254. struct of_phandle_args args;
  255. int ret;
  256. /*
  257. * Use snd_soc_dai_link_component instead of legacy style.
  258. * It is only for codec, but cpu will be supported in the future.
  259. * see
  260. * soc-core.c :: snd_soc_init_multicodec()
  261. */
  262. if (dlc) {
  263. dai_name = &dlc->dai_name;
  264. dai_of_node = &dlc->of_node;
  265. }
  266. if (!ep)
  267. return 0;
  268. if (!dai_name)
  269. return 0;
  270. node = of_graph_get_port_parent(ep);
  271. /* Get dai->name */
  272. args.np = node;
  273. args.args[0] = asoc_simple_card_get_dai_id(ep);
  274. args.args_count = (of_graph_get_endpoint_count(node) > 1);
  275. ret = snd_soc_get_dai_name(&args, dai_name);
  276. if (ret < 0)
  277. return ret;
  278. *dai_of_node = node;
  279. return 0;
  280. }
  281. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
  282. int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
  283. struct asoc_simple_dai *simple_dai)
  284. {
  285. int ret;
  286. if (simple_dai->sysclk) {
  287. ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
  288. simple_dai->clk_direction);
  289. if (ret && ret != -ENOTSUPP) {
  290. dev_err(dai->dev, "simple-card: set_sysclk error\n");
  291. return ret;
  292. }
  293. }
  294. if (simple_dai->slots) {
  295. ret = snd_soc_dai_set_tdm_slot(dai,
  296. simple_dai->tx_slot_mask,
  297. simple_dai->rx_slot_mask,
  298. simple_dai->slots,
  299. simple_dai->slot_width);
  300. if (ret && ret != -ENOTSUPP) {
  301. dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
  302. return ret;
  303. }
  304. }
  305. return 0;
  306. }
  307. EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
  308. int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
  309. {
  310. /* Assumes platform == cpu */
  311. if (!dai_link->platform->of_node)
  312. dai_link->platform->of_node = dai_link->cpu_of_node;
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
  316. void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
  317. int is_single_links)
  318. {
  319. /*
  320. * In soc_bind_dai_link() will check cpu name after
  321. * of_node matching if dai_link has cpu_dai_name.
  322. * but, it will never match if name was created by
  323. * fmt_single_name() remove cpu_dai_name if cpu_args
  324. * was 0. See:
  325. * fmt_single_name()
  326. * fmt_multiple_name()
  327. */
  328. if (is_single_links)
  329. dai_link->cpu_dai_name = NULL;
  330. }
  331. EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
  332. int asoc_simple_card_clean_reference(struct snd_soc_card *card)
  333. {
  334. struct snd_soc_dai_link *dai_link;
  335. int i;
  336. for_each_card_prelinks(card, i, dai_link) {
  337. of_node_put(dai_link->cpu_of_node);
  338. of_node_put(dai_link->codecs->of_node);
  339. }
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
  343. int asoc_simple_card_of_parse_routing(struct snd_soc_card *card,
  344. char *prefix,
  345. int optional)
  346. {
  347. struct device_node *node = card->dev->of_node;
  348. char prop[128];
  349. if (!prefix)
  350. prefix = "";
  351. snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
  352. if (!of_property_read_bool(node, prop)) {
  353. if (optional)
  354. return 0;
  355. return -EINVAL;
  356. }
  357. return snd_soc_of_parse_audio_routing(card, prop);
  358. }
  359. EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_routing);
  360. int asoc_simple_card_of_parse_widgets(struct snd_soc_card *card,
  361. char *prefix)
  362. {
  363. struct device_node *node = card->dev->of_node;
  364. char prop[128];
  365. if (!prefix)
  366. prefix = "";
  367. snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
  368. if (of_property_read_bool(node, prop))
  369. return snd_soc_of_parse_audio_simple_widgets(card, prop);
  370. /* no widgets is not error */
  371. return 0;
  372. }
  373. EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_widgets);
  374. int asoc_simple_card_init_jack(struct snd_soc_card *card,
  375. struct asoc_simple_jack *sjack,
  376. int is_hp, char *prefix)
  377. {
  378. struct device *dev = card->dev;
  379. enum of_gpio_flags flags;
  380. char prop[128];
  381. char *pin_name;
  382. char *gpio_name;
  383. int mask;
  384. int det;
  385. if (!prefix)
  386. prefix = "";
  387. sjack->gpio.gpio = -ENOENT;
  388. if (is_hp) {
  389. snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix);
  390. pin_name = "Headphones";
  391. gpio_name = "Headphone detection";
  392. mask = SND_JACK_HEADPHONE;
  393. } else {
  394. snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix);
  395. pin_name = "Mic Jack";
  396. gpio_name = "Mic detection";
  397. mask = SND_JACK_MICROPHONE;
  398. }
  399. det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags);
  400. if (det == -EPROBE_DEFER)
  401. return -EPROBE_DEFER;
  402. if (gpio_is_valid(det)) {
  403. sjack->pin.pin = pin_name;
  404. sjack->pin.mask = mask;
  405. sjack->gpio.name = gpio_name;
  406. sjack->gpio.report = mask;
  407. sjack->gpio.gpio = det;
  408. sjack->gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW);
  409. sjack->gpio.debounce_time = 150;
  410. snd_soc_card_jack_new(card, pin_name, mask,
  411. &sjack->jack,
  412. &sjack->pin, 1);
  413. snd_soc_jack_add_gpios(&sjack->jack, 1,
  414. &sjack->gpio);
  415. }
  416. return 0;
  417. }
  418. EXPORT_SYMBOL_GPL(asoc_simple_card_init_jack);
  419. /* Module information */
  420. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  421. MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
  422. MODULE_LICENSE("GPL v2");