simple-card-utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 *name)
  143. {
  144. struct clk *clk;
  145. u32 val;
  146. /*
  147. * Parse dai->sysclk come from "clocks = <&xxx>"
  148. * (if system has common clock)
  149. * or "system-clock-frequency = <xxx>"
  150. * or device's module clock.
  151. */
  152. clk = devm_get_clk_from_child(dev, node, NULL);
  153. if (!IS_ERR(clk)) {
  154. simple_dai->sysclk = clk_get_rate(clk);
  155. asoc_simple_card_clk_register(simple_dai, clk);
  156. } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
  157. simple_dai->sysclk = val;
  158. } else {
  159. clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
  160. if (!IS_ERR(clk))
  161. simple_dai->sysclk = clk_get_rate(clk);
  162. }
  163. if (of_property_read_bool(node, "system-clock-direction-out"))
  164. simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
  165. dev_dbg(dev, "%s : sysclk = %d, direction %d\n", name,
  166. simple_dai->sysclk, simple_dai->clk_direction);
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
  170. int asoc_simple_card_parse_dai(struct device_node *node,
  171. struct device_node **dai_of_node,
  172. const char **dai_name,
  173. const char *list_name,
  174. const char *cells_name,
  175. int *is_single_link)
  176. {
  177. struct of_phandle_args args;
  178. int ret;
  179. if (!node)
  180. return 0;
  181. /*
  182. * Get node via "sound-dai = <&phandle port>"
  183. * it will be used as xxx_of_node on soc_bind_dai_link()
  184. */
  185. ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
  186. if (ret)
  187. return ret;
  188. /* Get dai->name */
  189. if (dai_name) {
  190. ret = snd_soc_of_get_dai_name(node, dai_name);
  191. if (ret < 0)
  192. return ret;
  193. }
  194. *dai_of_node = args.np;
  195. if (is_single_link)
  196. *is_single_link = !args.args_count;
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
  200. static int asoc_simple_card_get_dai_id(struct device_node *ep)
  201. {
  202. struct device_node *node;
  203. struct device_node *endpoint;
  204. int i, id;
  205. int ret;
  206. ret = snd_soc_get_dai_id(ep);
  207. if (ret != -ENOTSUPP)
  208. return ret;
  209. node = of_graph_get_port_parent(ep);
  210. /*
  211. * Non HDMI sound case, counting port/endpoint on its DT
  212. * is enough. Let's count it.
  213. */
  214. i = 0;
  215. id = -1;
  216. for_each_endpoint_of_node(node, endpoint) {
  217. if (endpoint == ep)
  218. id = i;
  219. i++;
  220. }
  221. of_node_put(node);
  222. if (id < 0)
  223. return -ENODEV;
  224. return id;
  225. }
  226. int asoc_simple_card_parse_graph_dai(struct device_node *ep,
  227. struct device_node **dai_of_node,
  228. const char **dai_name)
  229. {
  230. struct device_node *node;
  231. struct of_phandle_args args;
  232. int ret;
  233. if (!ep)
  234. return 0;
  235. if (!dai_name)
  236. return 0;
  237. node = of_graph_get_port_parent(ep);
  238. /* Get dai->name */
  239. args.np = node;
  240. args.args[0] = asoc_simple_card_get_dai_id(ep);
  241. args.args_count = (of_graph_get_endpoint_count(node) > 1);
  242. ret = snd_soc_get_dai_name(&args, dai_name);
  243. if (ret < 0)
  244. return ret;
  245. *dai_of_node = node;
  246. return 0;
  247. }
  248. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
  249. int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
  250. struct asoc_simple_dai *simple_dai)
  251. {
  252. int ret;
  253. if (simple_dai->sysclk) {
  254. ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
  255. simple_dai->clk_direction);
  256. if (ret && ret != -ENOTSUPP) {
  257. dev_err(dai->dev, "simple-card: set_sysclk error\n");
  258. return ret;
  259. }
  260. }
  261. if (simple_dai->slots) {
  262. ret = snd_soc_dai_set_tdm_slot(dai,
  263. simple_dai->tx_slot_mask,
  264. simple_dai->rx_slot_mask,
  265. simple_dai->slots,
  266. simple_dai->slot_width);
  267. if (ret && ret != -ENOTSUPP) {
  268. dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
  269. return ret;
  270. }
  271. }
  272. return 0;
  273. }
  274. EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
  275. int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
  276. {
  277. /* Assumes platform == cpu */
  278. if (!dai_link->platform_of_node)
  279. dai_link->platform_of_node = dai_link->cpu_of_node;
  280. return 0;
  281. }
  282. EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
  283. void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
  284. int is_single_links)
  285. {
  286. /*
  287. * In soc_bind_dai_link() will check cpu name after
  288. * of_node matching if dai_link has cpu_dai_name.
  289. * but, it will never match if name was created by
  290. * fmt_single_name() remove cpu_dai_name if cpu_args
  291. * was 0. See:
  292. * fmt_single_name()
  293. * fmt_multiple_name()
  294. */
  295. if (is_single_links)
  296. dai_link->cpu_dai_name = NULL;
  297. }
  298. EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
  299. int asoc_simple_card_clean_reference(struct snd_soc_card *card)
  300. {
  301. struct snd_soc_dai_link *dai_link;
  302. int num_links;
  303. for (num_links = 0, dai_link = card->dai_link;
  304. num_links < card->num_links;
  305. num_links++, dai_link++) {
  306. of_node_put(dai_link->cpu_of_node);
  307. of_node_put(dai_link->codec_of_node);
  308. }
  309. return 0;
  310. }
  311. EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
  312. int asoc_simple_card_of_parse_routing(struct snd_soc_card *card,
  313. char *prefix,
  314. int optional)
  315. {
  316. struct device_node *node = card->dev->of_node;
  317. char prop[128];
  318. if (!prefix)
  319. prefix = "";
  320. snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
  321. if (!of_property_read_bool(node, prop)) {
  322. if (optional)
  323. return 0;
  324. return -EINVAL;
  325. }
  326. return snd_soc_of_parse_audio_routing(card, prop);
  327. }
  328. EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_routing);
  329. int asoc_simple_card_of_parse_widgets(struct snd_soc_card *card,
  330. char *prefix)
  331. {
  332. struct device_node *node = card->dev->of_node;
  333. char prop[128];
  334. if (!prefix)
  335. prefix = "";
  336. snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
  337. if (of_property_read_bool(node, prop))
  338. return snd_soc_of_parse_audio_simple_widgets(card, prop);
  339. /* no widgets is not error */
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_widgets);
  343. int asoc_simple_card_init_jack(struct snd_soc_card *card,
  344. struct asoc_simple_jack *sjack,
  345. int is_hp, char *prefix)
  346. {
  347. struct device *dev = card->dev;
  348. enum of_gpio_flags flags;
  349. char prop[128];
  350. char *pin_name;
  351. char *gpio_name;
  352. int mask;
  353. int det;
  354. if (!prefix)
  355. prefix = "";
  356. sjack->gpio.gpio = -ENOENT;
  357. if (is_hp) {
  358. snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix);
  359. pin_name = "Headphones";
  360. gpio_name = "Headphone detection";
  361. mask = SND_JACK_HEADPHONE;
  362. } else {
  363. snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix);
  364. pin_name = "Mic Jack";
  365. gpio_name = "Mic detection";
  366. mask = SND_JACK_MICROPHONE;
  367. }
  368. det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags);
  369. if (det == -EPROBE_DEFER)
  370. return -EPROBE_DEFER;
  371. if (gpio_is_valid(det)) {
  372. sjack->pin.pin = pin_name;
  373. sjack->pin.mask = mask;
  374. sjack->gpio.name = gpio_name;
  375. sjack->gpio.report = mask;
  376. sjack->gpio.gpio = det;
  377. sjack->gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW);
  378. sjack->gpio.debounce_time = 150;
  379. snd_soc_card_jack_new(card, pin_name, mask,
  380. &sjack->jack,
  381. &sjack->pin, 1);
  382. snd_soc_jack_add_gpios(&sjack->jack, 1,
  383. &sjack->gpio);
  384. }
  385. return 0;
  386. }
  387. EXPORT_SYMBOL_GPL(asoc_simple_card_init_jack);
  388. /* Module information */
  389. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  390. MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
  391. MODULE_LICENSE("GPL v2");