imx-card.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright 2017-2021 NXP
  3. #include <linux/module.h>
  4. #include <linux/init.h>
  5. #include <linux/slab.h>
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/of_device.h>
  8. #include <linux/i2c.h>
  9. #include <linux/of_gpio.h>
  10. #include <linux/clk.h>
  11. #include <sound/soc.h>
  12. #include <sound/pcm_params.h>
  13. #include <sound/pcm.h>
  14. #include <sound/soc-dapm.h>
  15. #include <sound/simple_card_utils.h>
  16. #include "fsl_sai.h"
  17. #define IMX_CARD_MCLK_22P5792MHZ 22579200
  18. #define IMX_CARD_MCLK_24P576MHZ 24576000
  19. enum codec_type {
  20. CODEC_DUMMY = 0,
  21. CODEC_AK5558 = 1,
  22. CODEC_AK4458,
  23. CODEC_AK4497,
  24. CODEC_AK5552,
  25. };
  26. /*
  27. * Mapping LRCK fs and frame width, table 3 & 4 in datasheet
  28. * @rmin: min rate
  29. * @rmax: max rate
  30. * @wmin: min frame ratio
  31. * @wmax: max frame ratio
  32. */
  33. struct imx_akcodec_fs_mul {
  34. unsigned int rmin;
  35. unsigned int rmax;
  36. unsigned int wmin;
  37. unsigned int wmax;
  38. };
  39. /*
  40. * Mapping TDM mode and frame width
  41. */
  42. struct imx_akcodec_tdm_fs_mul {
  43. unsigned int min;
  44. unsigned int max;
  45. unsigned int mul;
  46. };
  47. /*
  48. * struct imx_card_plat_data - specific info for codecs
  49. *
  50. * @fs_mul: ratio of mclk/fs for normal mode
  51. * @tdm_fs_mul: ratio of mclk/fs for tdm mode
  52. * @support_rates: supported sample rate
  53. * @support_tdm_rates: supported sample rate for tdm mode
  54. * @support_channels: supported channels
  55. * @support_tdm_channels: supported channels for tdm mode
  56. * @num_fs_mul: ARRAY_SIZE of fs_mul
  57. * @num_tdm_fs_mul: ARRAY_SIZE of tdm_fs_mul
  58. * @num_rates: ARRAY_SIZE of support_rates
  59. * @num_tdm_rates: ARRAY_SIZE of support_tdm_rates
  60. * @num_channels: ARRAY_SIZE of support_channels
  61. * @num_tdm_channels: ARRAY_SIZE of support_tdm_channels
  62. * @type: codec type
  63. */
  64. struct imx_card_plat_data {
  65. struct imx_akcodec_fs_mul *fs_mul;
  66. struct imx_akcodec_tdm_fs_mul *tdm_fs_mul;
  67. const u32 *support_rates;
  68. const u32 *support_tdm_rates;
  69. const u32 *support_channels;
  70. const u32 *support_tdm_channels;
  71. unsigned int num_fs_mul;
  72. unsigned int num_tdm_fs_mul;
  73. unsigned int num_rates;
  74. unsigned int num_tdm_rates;
  75. unsigned int num_channels;
  76. unsigned int num_tdm_channels;
  77. unsigned int num_codecs;
  78. enum codec_type type;
  79. };
  80. /*
  81. * struct dai_link_data - specific info for dai link
  82. *
  83. * @slots: slot number
  84. * @slot_width: slot width value
  85. * @cpu_sysclk_id: sysclk id for cpu dai
  86. * @one2one_ratio: true if mclk equal to bclk
  87. */
  88. struct dai_link_data {
  89. unsigned int slots;
  90. unsigned int slot_width;
  91. unsigned int cpu_sysclk_id;
  92. bool one2one_ratio;
  93. };
  94. /*
  95. * struct imx_card_data - platform device data
  96. *
  97. * @plat_data: pointer of imx_card_plat_data
  98. * @dapm_routes: pointer of dapm_routes
  99. * @link_data: private data for dai link
  100. * @card: card instance
  101. * @num_dapm_routes: number of dapm_routes
  102. * @asrc_rate: asrc rates
  103. * @asrc_format: asrc format
  104. */
  105. struct imx_card_data {
  106. struct imx_card_plat_data *plat_data;
  107. struct snd_soc_dapm_route *dapm_routes;
  108. struct dai_link_data *link_data;
  109. struct snd_soc_card card;
  110. int num_dapm_routes;
  111. u32 asrc_rate;
  112. snd_pcm_format_t asrc_format;
  113. };
  114. static struct imx_akcodec_fs_mul ak4458_fs_mul[] = {
  115. /* Normal, < 32kHz */
  116. { .rmin = 8000, .rmax = 24000, .wmin = 256, .wmax = 1024, },
  117. /* Normal, 32kHz */
  118. { .rmin = 32000, .rmax = 32000, .wmin = 256, .wmax = 1024, },
  119. /* Normal */
  120. { .rmin = 44100, .rmax = 48000, .wmin = 256, .wmax = 768, },
  121. /* Double */
  122. { .rmin = 88200, .rmax = 96000, .wmin = 256, .wmax = 512, },
  123. /* Quad */
  124. { .rmin = 176400, .rmax = 192000, .wmin = 128, .wmax = 256, },
  125. /* Oct */
  126. { .rmin = 352800, .rmax = 384000, .wmin = 32, .wmax = 128, },
  127. /* Hex */
  128. { .rmin = 705600, .rmax = 768000, .wmin = 16, .wmax = 64, },
  129. };
  130. static struct imx_akcodec_tdm_fs_mul ak4458_tdm_fs_mul[] = {
  131. /*
  132. * Table 13 - Audio Interface Format
  133. * For TDM mode, MCLK should is set to
  134. * obtained from 2 * slots * slot_width
  135. */
  136. { .min = 128, .max = 128, .mul = 256 }, /* TDM128 */
  137. { .min = 256, .max = 256, .mul = 512 }, /* TDM256 */
  138. { .min = 512, .max = 512, .mul = 1024 }, /* TDM512 */
  139. };
  140. static struct imx_akcodec_fs_mul ak4497_fs_mul[] = {
  141. /**
  142. * Table 7 - mapping multiplier and speed mode
  143. * Tables 8 & 9 - mapping speed mode and LRCK fs
  144. */
  145. { .rmin = 8000, .rmax = 32000, .wmin = 256, .wmax = 1024, }, /* Normal, <= 32kHz */
  146. { .rmin = 44100, .rmax = 48000, .wmin = 256, .wmax = 512, }, /* Normal */
  147. { .rmin = 88200, .rmax = 96000, .wmin = 256, .wmax = 256, }, /* Double */
  148. { .rmin = 176400, .rmax = 192000, .wmin = 128, .wmax = 128, }, /* Quad */
  149. { .rmin = 352800, .rmax = 384000, .wmin = 128, .wmax = 128, }, /* Oct */
  150. { .rmin = 705600, .rmax = 768000, .wmin = 64, .wmax = 64, }, /* Hex */
  151. };
  152. /*
  153. * Auto MCLK selection based on LRCK for Normal Mode
  154. * (Table 4 from datasheet)
  155. */
  156. static struct imx_akcodec_fs_mul ak5558_fs_mul[] = {
  157. { .rmin = 8000, .rmax = 32000, .wmin = 512, .wmax = 1024, },
  158. { .rmin = 44100, .rmax = 48000, .wmin = 512, .wmax = 512, },
  159. { .rmin = 88200, .rmax = 96000, .wmin = 256, .wmax = 256, },
  160. { .rmin = 176400, .rmax = 192000, .wmin = 128, .wmax = 128, },
  161. { .rmin = 352800, .rmax = 384000, .wmin = 64, .wmax = 64, },
  162. { .rmin = 705600, .rmax = 768000, .wmin = 32, .wmax = 32, },
  163. };
  164. /*
  165. * MCLK and BCLK selection based on TDM mode
  166. * because of SAI we also add the restriction: MCLK >= 2 * BCLK
  167. * (Table 9 from datasheet)
  168. */
  169. static struct imx_akcodec_tdm_fs_mul ak5558_tdm_fs_mul[] = {
  170. { .min = 128, .max = 128, .mul = 256 },
  171. { .min = 256, .max = 256, .mul = 512 },
  172. { .min = 512, .max = 512, .mul = 1024 },
  173. };
  174. static const u32 akcodec_rates[] = {
  175. 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
  176. 96000, 176400, 192000, 352800, 384000, 705600, 768000,
  177. };
  178. static const u32 akcodec_tdm_rates[] = {
  179. 8000, 16000, 32000, 48000, 96000,
  180. };
  181. static const u32 ak4458_channels[] = {
  182. 1, 2, 4, 6, 8, 10, 12, 14, 16,
  183. };
  184. static const u32 ak4458_tdm_channels[] = {
  185. 1, 2, 3, 4, 5, 6, 7, 8, 16,
  186. };
  187. static const u32 ak5558_channels[] = {
  188. 1, 2, 4, 6, 8,
  189. };
  190. static const u32 ak5558_tdm_channels[] = {
  191. 1, 2, 3, 4, 5, 6, 7, 8,
  192. };
  193. static bool format_is_dsd(struct snd_pcm_hw_params *params)
  194. {
  195. snd_pcm_format_t format = params_format(params);
  196. switch (format) {
  197. case SNDRV_PCM_FORMAT_DSD_U8:
  198. case SNDRV_PCM_FORMAT_DSD_U16_LE:
  199. case SNDRV_PCM_FORMAT_DSD_U16_BE:
  200. case SNDRV_PCM_FORMAT_DSD_U32_LE:
  201. case SNDRV_PCM_FORMAT_DSD_U32_BE:
  202. return true;
  203. default:
  204. return false;
  205. }
  206. }
  207. static bool format_is_tdm(struct dai_link_data *link_data)
  208. {
  209. if (link_data->slots > 2)
  210. return true;
  211. else
  212. return false;
  213. }
  214. static bool codec_is_akcodec(unsigned int type)
  215. {
  216. switch (type) {
  217. case CODEC_AK4458:
  218. case CODEC_AK4497:
  219. case CODEC_AK5558:
  220. case CODEC_AK5552:
  221. return true;
  222. default:
  223. break;
  224. }
  225. return false;
  226. }
  227. static unsigned long akcodec_get_mclk_rate(struct snd_pcm_substream *substream,
  228. struct snd_pcm_hw_params *params,
  229. int slots, int slot_width)
  230. {
  231. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  232. struct imx_card_data *data = snd_soc_card_get_drvdata(rtd->card);
  233. const struct imx_card_plat_data *plat_data = data->plat_data;
  234. struct dai_link_data *link_data = &data->link_data[rtd->num];
  235. unsigned int width = slots * slot_width;
  236. unsigned int rate = params_rate(params);
  237. int i;
  238. if (format_is_tdm(link_data)) {
  239. for (i = 0; i < plat_data->num_tdm_fs_mul; i++) {
  240. /* min = max = slots * slots_width */
  241. if (width != plat_data->tdm_fs_mul[i].min)
  242. continue;
  243. return rate * plat_data->tdm_fs_mul[i].mul;
  244. }
  245. } else {
  246. for (i = 0; i < plat_data->num_fs_mul; i++) {
  247. if (rate >= plat_data->fs_mul[i].rmin &&
  248. rate <= plat_data->fs_mul[i].rmax) {
  249. width = max(width, plat_data->fs_mul[i].wmin);
  250. width = min(width, plat_data->fs_mul[i].wmax);
  251. /* Adjust SAI bclk:mclk ratio */
  252. width *= link_data->one2one_ratio ? 1 : 2;
  253. return rate * width;
  254. }
  255. }
  256. }
  257. /* Let DAI manage clk frequency by default */
  258. return 0;
  259. }
  260. static int imx_aif_hw_params(struct snd_pcm_substream *substream,
  261. struct snd_pcm_hw_params *params)
  262. {
  263. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  264. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  265. struct snd_soc_card *card = rtd->card;
  266. struct imx_card_data *data = snd_soc_card_get_drvdata(card);
  267. struct dai_link_data *link_data = &data->link_data[rtd->num];
  268. struct imx_card_plat_data *plat_data = data->plat_data;
  269. struct device *dev = card->dev;
  270. struct snd_soc_dai *codec_dai;
  271. unsigned long mclk_freq;
  272. unsigned int fmt = rtd->dai_link->dai_fmt;
  273. unsigned int slots, slot_width;
  274. int ret, i;
  275. slots = link_data->slots;
  276. slot_width = link_data->slot_width;
  277. if (!format_is_tdm(link_data)) {
  278. if (format_is_dsd(params)) {
  279. slots = 1;
  280. slot_width = params_width(params);
  281. fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) |
  282. SND_SOC_DAIFMT_PDM;
  283. } else {
  284. slots = 2;
  285. slot_width = params_physical_width(params);
  286. fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) |
  287. SND_SOC_DAIFMT_I2S;
  288. }
  289. }
  290. ret = snd_soc_dai_set_fmt(cpu_dai, snd_soc_daifmt_clock_provider_flipped(fmt));
  291. if (ret && ret != -ENOTSUPP) {
  292. dev_err(dev, "failed to set cpu dai fmt: %d\n", ret);
  293. return ret;
  294. }
  295. ret = snd_soc_dai_set_tdm_slot(cpu_dai,
  296. BIT(slots) - 1,
  297. BIT(slots) - 1,
  298. slots, slot_width);
  299. if (ret && ret != -ENOTSUPP) {
  300. dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
  301. return ret;
  302. }
  303. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  304. ret = snd_soc_dai_set_fmt(codec_dai, fmt);
  305. if (ret && ret != -ENOTSUPP) {
  306. dev_err(dev, "failed to set codec dai[%d] fmt: %d\n", i, ret);
  307. return ret;
  308. }
  309. ret = snd_soc_dai_set_tdm_slot(codec_dai,
  310. BIT(slots) - 1,
  311. BIT(slots) - 1,
  312. slots, slot_width);
  313. if (ret && ret != -ENOTSUPP) {
  314. dev_err(dev, "failed to set codec dai[%d] tdm slot: %d\n", i, ret);
  315. return ret;
  316. }
  317. }
  318. /* Set MCLK freq */
  319. if (codec_is_akcodec(plat_data->type))
  320. mclk_freq = akcodec_get_mclk_rate(substream, params, slots, slot_width);
  321. else
  322. mclk_freq = params_rate(params) * slots * slot_width;
  323. if (format_is_dsd(params)) {
  324. /* Use the maximum freq from DSD512 (512*44100 = 22579200) */
  325. if (!(params_rate(params) % 11025))
  326. mclk_freq = IMX_CARD_MCLK_22P5792MHZ;
  327. else
  328. mclk_freq = IMX_CARD_MCLK_24P576MHZ;
  329. }
  330. ret = snd_soc_dai_set_sysclk(cpu_dai, link_data->cpu_sysclk_id, mclk_freq,
  331. SND_SOC_CLOCK_OUT);
  332. if (ret && ret != -ENOTSUPP) {
  333. dev_err(dev, "failed to set cpui dai mclk1 rate (%lu): %d\n", mclk_freq, ret);
  334. return ret;
  335. }
  336. return 0;
  337. }
  338. static int ak5558_hw_rule_rate(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *r)
  339. {
  340. struct dai_link_data *link_data = r->private;
  341. struct snd_interval t = { .min = 8000, .max = 8000, };
  342. unsigned long mclk_freq;
  343. unsigned int fs;
  344. int i;
  345. fs = hw_param_interval(p, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
  346. fs *= link_data->slots;
  347. /* Identify maximum supported rate */
  348. for (i = 0; i < ARRAY_SIZE(akcodec_rates); i++) {
  349. mclk_freq = fs * akcodec_rates[i];
  350. /* Adjust SAI bclk:mclk ratio */
  351. mclk_freq *= link_data->one2one_ratio ? 1 : 2;
  352. /* Skip rates for which MCLK is beyond supported value */
  353. if (mclk_freq > 36864000)
  354. continue;
  355. if (t.max < akcodec_rates[i])
  356. t.max = akcodec_rates[i];
  357. }
  358. return snd_interval_refine(hw_param_interval(p, r->var), &t);
  359. }
  360. static int imx_aif_startup(struct snd_pcm_substream *substream)
  361. {
  362. struct snd_pcm_runtime *runtime = substream->runtime;
  363. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  364. struct snd_soc_card *card = rtd->card;
  365. struct imx_card_data *data = snd_soc_card_get_drvdata(card);
  366. struct dai_link_data *link_data = &data->link_data[rtd->num];
  367. static struct snd_pcm_hw_constraint_list constraint_rates;
  368. static struct snd_pcm_hw_constraint_list constraint_channels;
  369. int ret = 0;
  370. if (format_is_tdm(link_data)) {
  371. constraint_channels.list = data->plat_data->support_tdm_channels;
  372. constraint_channels.count = data->plat_data->num_tdm_channels;
  373. constraint_rates.list = data->plat_data->support_tdm_rates;
  374. constraint_rates.count = data->plat_data->num_tdm_rates;
  375. } else {
  376. constraint_channels.list = data->plat_data->support_channels;
  377. constraint_channels.count = data->plat_data->num_channels;
  378. constraint_rates.list = data->plat_data->support_rates;
  379. constraint_rates.count = data->plat_data->num_rates;
  380. }
  381. if (constraint_channels.count) {
  382. ret = snd_pcm_hw_constraint_list(runtime, 0,
  383. SNDRV_PCM_HW_PARAM_CHANNELS,
  384. &constraint_channels);
  385. if (ret)
  386. return ret;
  387. }
  388. if (constraint_rates.count) {
  389. ret = snd_pcm_hw_constraint_list(runtime, 0,
  390. SNDRV_PCM_HW_PARAM_RATE,
  391. &constraint_rates);
  392. if (ret)
  393. return ret;
  394. }
  395. if (data->plat_data->type == CODEC_AK5558)
  396. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  397. SNDRV_PCM_HW_PARAM_RATE,
  398. ak5558_hw_rule_rate, link_data,
  399. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  400. return ret;
  401. }
  402. static const struct snd_soc_ops imx_aif_ops = {
  403. .hw_params = imx_aif_hw_params,
  404. .startup = imx_aif_startup,
  405. };
  406. static const struct snd_soc_ops imx_aif_ops_be = {
  407. .hw_params = imx_aif_hw_params,
  408. };
  409. static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  410. struct snd_pcm_hw_params *params)
  411. {
  412. struct snd_soc_card *card = rtd->card;
  413. struct imx_card_data *data = snd_soc_card_get_drvdata(card);
  414. struct snd_interval *rate;
  415. struct snd_mask *mask;
  416. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  417. rate->max = data->asrc_rate;
  418. rate->min = data->asrc_rate;
  419. mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  420. snd_mask_none(mask);
  421. snd_mask_set(mask, (__force unsigned int)data->asrc_format);
  422. return 0;
  423. }
  424. static int imx_card_parse_of(struct imx_card_data *data)
  425. {
  426. struct imx_card_plat_data *plat_data = data->plat_data;
  427. struct snd_soc_card *card = &data->card;
  428. struct snd_soc_dai_link_component *dlc;
  429. struct device_node *platform = NULL;
  430. struct device_node *codec = NULL;
  431. struct device_node *cpu = NULL;
  432. struct device_node *np;
  433. struct device *dev = card->dev;
  434. struct snd_soc_dai_link *link;
  435. struct dai_link_data *link_data;
  436. struct of_phandle_args args;
  437. int ret, num_links;
  438. u32 asrc_fmt = 0;
  439. u32 width;
  440. ret = snd_soc_of_parse_card_name(card, "model");
  441. if (ret) {
  442. dev_err(dev, "Error parsing card name: %d\n", ret);
  443. return ret;
  444. }
  445. /* DAPM routes */
  446. if (of_property_read_bool(dev->of_node, "audio-routing")) {
  447. ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
  448. if (ret)
  449. return ret;
  450. }
  451. /* Populate links */
  452. num_links = of_get_child_count(dev->of_node);
  453. /* Allocate the DAI link array */
  454. card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
  455. if (!card->dai_link)
  456. return -ENOMEM;
  457. data->link_data = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
  458. if (!data->link_data)
  459. return -ENOMEM;
  460. card->num_links = num_links;
  461. link = card->dai_link;
  462. link_data = data->link_data;
  463. for_each_child_of_node(dev->of_node, np) {
  464. dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
  465. if (!dlc) {
  466. ret = -ENOMEM;
  467. goto err_put_np;
  468. }
  469. link->cpus = &dlc[0];
  470. link->platforms = &dlc[1];
  471. link->num_cpus = 1;
  472. link->num_platforms = 1;
  473. ret = of_property_read_string(np, "link-name", &link->name);
  474. if (ret) {
  475. dev_err(card->dev, "error getting codec dai_link name\n");
  476. goto err_put_np;
  477. }
  478. cpu = of_get_child_by_name(np, "cpu");
  479. if (!cpu) {
  480. dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
  481. ret = -EINVAL;
  482. goto err;
  483. }
  484. ret = of_parse_phandle_with_args(cpu, "sound-dai",
  485. "#sound-dai-cells", 0, &args);
  486. if (ret) {
  487. dev_err(card->dev, "%s: error getting cpu phandle\n", link->name);
  488. goto err;
  489. }
  490. if (of_node_name_eq(args.np, "sai")) {
  491. /* sai sysclk id */
  492. link_data->cpu_sysclk_id = FSL_SAI_CLK_MAST1;
  493. /* sai may support mclk/bclk = 1 */
  494. if (of_property_read_bool(np, "fsl,mclk-equal-bclk")) {
  495. link_data->one2one_ratio = true;
  496. } else {
  497. int i;
  498. /*
  499. * i.MX8MQ don't support one2one ratio, then
  500. * with ak4497 only 16bit case is supported.
  501. */
  502. for (i = 0; i < ARRAY_SIZE(ak4497_fs_mul); i++) {
  503. if (ak4497_fs_mul[i].rmin == 705600 &&
  504. ak4497_fs_mul[i].rmax == 768000) {
  505. ak4497_fs_mul[i].wmin = 32;
  506. ak4497_fs_mul[i].wmax = 32;
  507. }
  508. }
  509. }
  510. }
  511. link->cpus->of_node = args.np;
  512. link->platforms->of_node = link->cpus->of_node;
  513. link->id = args.args[0];
  514. ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
  515. if (ret) {
  516. dev_err_probe(card->dev, ret,
  517. "%s: error getting cpu dai name\n", link->name);
  518. goto err;
  519. }
  520. codec = of_get_child_by_name(np, "codec");
  521. if (codec) {
  522. ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
  523. if (ret < 0) {
  524. dev_err_probe(dev, ret, "%s: codec dai not found\n",
  525. link->name);
  526. goto err;
  527. }
  528. plat_data->num_codecs = link->num_codecs;
  529. /* Check the akcodec type */
  530. if (!strcmp(link->codecs->dai_name, "ak4458-aif"))
  531. plat_data->type = CODEC_AK4458;
  532. else if (!strcmp(link->codecs->dai_name, "ak4497-aif"))
  533. plat_data->type = CODEC_AK4497;
  534. else if (!strcmp(link->codecs->dai_name, "ak5558-aif"))
  535. plat_data->type = CODEC_AK5558;
  536. else if (!strcmp(link->codecs->dai_name, "ak5552-aif"))
  537. plat_data->type = CODEC_AK5552;
  538. } else {
  539. dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
  540. if (!dlc) {
  541. ret = -ENOMEM;
  542. goto err;
  543. }
  544. link->codecs = dlc;
  545. link->num_codecs = 1;
  546. link->codecs->dai_name = "snd-soc-dummy-dai";
  547. link->codecs->name = "snd-soc-dummy";
  548. }
  549. if (!strncmp(link->name, "HiFi-ASRC-FE", 12)) {
  550. /* DPCM frontend */
  551. link->dynamic = 1;
  552. link->dpcm_merged_chan = 1;
  553. ret = of_property_read_u32(args.np, "fsl,asrc-rate", &data->asrc_rate);
  554. if (ret) {
  555. dev_err(dev, "failed to get output rate\n");
  556. ret = -EINVAL;
  557. goto err;
  558. }
  559. ret = of_property_read_u32(args.np, "fsl,asrc-format", &asrc_fmt);
  560. data->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
  561. if (ret) {
  562. /* Fallback to old binding; translate to asrc_format */
  563. ret = of_property_read_u32(args.np, "fsl,asrc-width", &width);
  564. if (ret) {
  565. dev_err(dev,
  566. "failed to decide output format\n");
  567. goto err;
  568. }
  569. if (width == 24)
  570. data->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
  571. else
  572. data->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
  573. }
  574. } else if (!strncmp(link->name, "HiFi-ASRC-BE", 12)) {
  575. /* DPCM backend */
  576. link->no_pcm = 1;
  577. link->platforms->of_node = NULL;
  578. link->platforms->name = "snd-soc-dummy";
  579. link->be_hw_params_fixup = be_hw_params_fixup;
  580. link->ops = &imx_aif_ops_be;
  581. } else {
  582. link->ops = &imx_aif_ops;
  583. }
  584. if (link->no_pcm || link->dynamic)
  585. snd_soc_dai_link_set_capabilities(link);
  586. /* Get dai fmt */
  587. ret = asoc_simple_parse_daifmt(dev, np, codec,
  588. NULL, &link->dai_fmt);
  589. if (ret)
  590. link->dai_fmt = SND_SOC_DAIFMT_NB_NF |
  591. SND_SOC_DAIFMT_CBC_CFC |
  592. SND_SOC_DAIFMT_I2S;
  593. /* Get tdm slot */
  594. snd_soc_of_parse_tdm_slot(np, NULL, NULL,
  595. &link_data->slots,
  596. &link_data->slot_width);
  597. /* default value */
  598. if (!link_data->slots)
  599. link_data->slots = 2;
  600. if (!link_data->slot_width)
  601. link_data->slot_width = 32;
  602. link->ignore_pmdown_time = 1;
  603. link->stream_name = link->name;
  604. link++;
  605. link_data++;
  606. of_node_put(cpu);
  607. of_node_put(codec);
  608. of_node_put(platform);
  609. cpu = NULL;
  610. codec = NULL;
  611. platform = NULL;
  612. }
  613. return 0;
  614. err:
  615. of_node_put(cpu);
  616. of_node_put(codec);
  617. of_node_put(platform);
  618. err_put_np:
  619. of_node_put(np);
  620. return ret;
  621. }
  622. static int imx_card_probe(struct platform_device *pdev)
  623. {
  624. struct snd_soc_dai_link *link_be = NULL, *link;
  625. struct imx_card_plat_data *plat_data;
  626. struct imx_card_data *data;
  627. int ret, i;
  628. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  629. if (!data)
  630. return -ENOMEM;
  631. plat_data = devm_kzalloc(&pdev->dev, sizeof(*plat_data), GFP_KERNEL);
  632. if (!plat_data)
  633. return -ENOMEM;
  634. data->plat_data = plat_data;
  635. data->card.dev = &pdev->dev;
  636. dev_set_drvdata(&pdev->dev, &data->card);
  637. snd_soc_card_set_drvdata(&data->card, data);
  638. ret = imx_card_parse_of(data);
  639. if (ret)
  640. return ret;
  641. data->num_dapm_routes = plat_data->num_codecs + 1;
  642. data->dapm_routes = devm_kcalloc(&pdev->dev, data->num_dapm_routes,
  643. sizeof(struct snd_soc_dapm_route),
  644. GFP_KERNEL);
  645. if (!data->dapm_routes)
  646. return -ENOMEM;
  647. /* configure the dapm routes */
  648. switch (plat_data->type) {
  649. case CODEC_AK4458:
  650. case CODEC_AK4497:
  651. if (plat_data->num_codecs == 1) {
  652. data->dapm_routes[0].sink = "Playback";
  653. data->dapm_routes[0].source = "CPU-Playback";
  654. i = 1;
  655. } else {
  656. for (i = 0; i < plat_data->num_codecs; i++) {
  657. data->dapm_routes[i].sink =
  658. devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
  659. i + 1, "Playback");
  660. data->dapm_routes[i].source = "CPU-Playback";
  661. }
  662. }
  663. data->dapm_routes[i].sink = "CPU-Playback";
  664. data->dapm_routes[i].source = "ASRC-Playback";
  665. break;
  666. case CODEC_AK5558:
  667. case CODEC_AK5552:
  668. if (plat_data->num_codecs == 1) {
  669. data->dapm_routes[0].sink = "CPU-Capture";
  670. data->dapm_routes[0].source = "Capture";
  671. i = 1;
  672. } else {
  673. for (i = 0; i < plat_data->num_codecs; i++) {
  674. data->dapm_routes[i].source =
  675. devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
  676. i + 1, "Capture");
  677. data->dapm_routes[i].sink = "CPU-Capture";
  678. }
  679. }
  680. data->dapm_routes[i].sink = "ASRC-Capture";
  681. data->dapm_routes[i].source = "CPU-Capture";
  682. break;
  683. default:
  684. break;
  685. }
  686. /* default platform data for akcodecs */
  687. if (codec_is_akcodec(plat_data->type)) {
  688. plat_data->support_rates = akcodec_rates;
  689. plat_data->num_rates = ARRAY_SIZE(akcodec_rates);
  690. plat_data->support_tdm_rates = akcodec_tdm_rates;
  691. plat_data->num_tdm_rates = ARRAY_SIZE(akcodec_tdm_rates);
  692. switch (plat_data->type) {
  693. case CODEC_AK4458:
  694. plat_data->fs_mul = ak4458_fs_mul;
  695. plat_data->num_fs_mul = ARRAY_SIZE(ak4458_fs_mul);
  696. plat_data->tdm_fs_mul = ak4458_tdm_fs_mul;
  697. plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak4458_tdm_fs_mul);
  698. plat_data->support_channels = ak4458_channels;
  699. plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
  700. plat_data->support_tdm_channels = ak4458_tdm_channels;
  701. plat_data->num_tdm_channels = ARRAY_SIZE(ak4458_tdm_channels);
  702. break;
  703. case CODEC_AK4497:
  704. plat_data->fs_mul = ak4497_fs_mul;
  705. plat_data->num_fs_mul = ARRAY_SIZE(ak4497_fs_mul);
  706. plat_data->support_channels = ak4458_channels;
  707. plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
  708. break;
  709. case CODEC_AK5558:
  710. case CODEC_AK5552:
  711. plat_data->fs_mul = ak5558_fs_mul;
  712. plat_data->num_fs_mul = ARRAY_SIZE(ak5558_fs_mul);
  713. plat_data->tdm_fs_mul = ak5558_tdm_fs_mul;
  714. plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak5558_tdm_fs_mul);
  715. plat_data->support_channels = ak5558_channels;
  716. plat_data->num_channels = ARRAY_SIZE(ak5558_channels);
  717. plat_data->support_tdm_channels = ak5558_tdm_channels;
  718. plat_data->num_tdm_channels = ARRAY_SIZE(ak5558_tdm_channels);
  719. break;
  720. default:
  721. break;
  722. }
  723. }
  724. /* with asrc as front end */
  725. if (data->card.num_links == 3) {
  726. data->card.dapm_routes = data->dapm_routes;
  727. data->card.num_dapm_routes = data->num_dapm_routes;
  728. for_each_card_prelinks(&data->card, i, link) {
  729. if (link->no_pcm == 1)
  730. link_be = link;
  731. }
  732. for_each_card_prelinks(&data->card, i, link) {
  733. if (link->dynamic == 1 && link_be) {
  734. link->dpcm_playback = link_be->dpcm_playback;
  735. link->dpcm_capture = link_be->dpcm_capture;
  736. }
  737. }
  738. }
  739. ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
  740. if (ret)
  741. return dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
  742. return 0;
  743. }
  744. static const struct of_device_id imx_card_dt_ids[] = {
  745. { .compatible = "fsl,imx-audio-card", },
  746. { },
  747. };
  748. MODULE_DEVICE_TABLE(of, imx_card_dt_ids);
  749. static struct platform_driver imx_card_driver = {
  750. .driver = {
  751. .name = "imx-card",
  752. .pm = &snd_soc_pm_ops,
  753. .of_match_table = imx_card_dt_ids,
  754. },
  755. .probe = imx_card_probe,
  756. };
  757. module_platform_driver(imx_card_driver);
  758. MODULE_DESCRIPTION("Freescale i.MX ASoC Machine Driver");
  759. MODULE_LICENSE("GPL v2");
  760. MODULE_ALIAS("platform:imx-card");