aio-cpu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Socionext UniPhier AIO ALSA CPU DAI driver.
  4. //
  5. // Copyright (c) 2016-2018 Socionext Inc.
  6. #include <linux/clk.h>
  7. #include <linux/errno.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reset.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/soc.h>
  19. #include "aio.h"
  20. static bool is_valid_pll(struct uniphier_aio_chip *chip, int pll_id)
  21. {
  22. struct device *dev = &chip->pdev->dev;
  23. if (pll_id < 0 || chip->num_plls <= pll_id) {
  24. dev_err(dev, "PLL(%d) is not supported\n", pll_id);
  25. return false;
  26. }
  27. return chip->plls[pll_id].enable;
  28. }
  29. /**
  30. * find_volume - find volume supported HW port by HW port number
  31. * @chip: the AIO chip pointer
  32. * @oport_hw: HW port number, one of AUD_HW_XXXX
  33. *
  34. * Find AIO device from device list by HW port number. Volume feature is
  35. * available only in Output and PCM ports, this limitation comes from HW
  36. * specifications.
  37. *
  38. * Return: The pointer of AIO substream if successful, otherwise NULL on error.
  39. */
  40. static struct uniphier_aio_sub *find_volume(struct uniphier_aio_chip *chip,
  41. int oport_hw)
  42. {
  43. int i;
  44. for (i = 0; i < chip->num_aios; i++) {
  45. struct uniphier_aio_sub *sub = &chip->aios[i].sub[0];
  46. if (!sub->swm)
  47. continue;
  48. if (sub->swm->oport.hw == oport_hw)
  49. return sub;
  50. }
  51. return NULL;
  52. }
  53. static bool match_spec(const struct uniphier_aio_spec *spec,
  54. const char *name, int dir)
  55. {
  56. if (dir == SNDRV_PCM_STREAM_PLAYBACK &&
  57. spec->swm.dir != PORT_DIR_OUTPUT) {
  58. return false;
  59. }
  60. if (dir == SNDRV_PCM_STREAM_CAPTURE &&
  61. spec->swm.dir != PORT_DIR_INPUT) {
  62. return false;
  63. }
  64. if (spec->name && strcmp(spec->name, name) == 0)
  65. return true;
  66. if (spec->gname && strcmp(spec->gname, name) == 0)
  67. return true;
  68. return false;
  69. }
  70. /**
  71. * find_spec - find HW specification info by name
  72. * @aio: the AIO device pointer
  73. * @name: name of device
  74. * @direction: the direction of substream, SNDRV_PCM_STREAM_*
  75. *
  76. * Find hardware specification information from list by device name. This
  77. * information is used for telling the difference of SoCs to driver.
  78. *
  79. * Specification list is array of 'struct uniphier_aio_spec' which is defined
  80. * in each drivers (see: aio-i2s.c).
  81. *
  82. * Return: The pointer of hardware specification of AIO if successful,
  83. * otherwise NULL on error.
  84. */
  85. static const struct uniphier_aio_spec *find_spec(struct uniphier_aio *aio,
  86. const char *name,
  87. int direction)
  88. {
  89. const struct uniphier_aio_chip_spec *chip_spec = aio->chip->chip_spec;
  90. int i;
  91. for (i = 0; i < chip_spec->num_specs; i++) {
  92. const struct uniphier_aio_spec *spec = &chip_spec->specs[i];
  93. if (match_spec(spec, name, direction))
  94. return spec;
  95. }
  96. return NULL;
  97. }
  98. /**
  99. * find_divider - find clock divider by frequency
  100. * @aio: the AIO device pointer
  101. * @pll_id: PLL ID, should be AUD_PLL_XX
  102. * @freq: required frequency
  103. *
  104. * Find suitable clock divider by frequency.
  105. *
  106. * Return: The ID of PLL if successful, otherwise negative error value.
  107. */
  108. static int find_divider(struct uniphier_aio *aio, int pll_id, unsigned int freq)
  109. {
  110. struct uniphier_aio_pll *pll;
  111. static const int mul[] = { 1, 1, 1, 2, };
  112. static const int div[] = { 2, 3, 1, 3, };
  113. int i;
  114. if (!is_valid_pll(aio->chip, pll_id))
  115. return -EINVAL;
  116. pll = &aio->chip->plls[pll_id];
  117. for (i = 0; i < ARRAY_SIZE(mul); i++)
  118. if (pll->freq * mul[i] / div[i] == freq)
  119. return i;
  120. return -ENOTSUPP;
  121. }
  122. static int uniphier_aio_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  123. unsigned int freq, int dir)
  124. {
  125. struct uniphier_aio *aio = uniphier_priv(dai);
  126. struct device *dev = &aio->chip->pdev->dev;
  127. bool pll_auto = false;
  128. int pll_id, div_id;
  129. switch (clk_id) {
  130. case AUD_CLK_IO:
  131. return -ENOTSUPP;
  132. case AUD_CLK_A1:
  133. pll_id = AUD_PLL_A1;
  134. break;
  135. case AUD_CLK_F1:
  136. pll_id = AUD_PLL_F1;
  137. break;
  138. case AUD_CLK_A2:
  139. pll_id = AUD_PLL_A2;
  140. break;
  141. case AUD_CLK_F2:
  142. pll_id = AUD_PLL_F2;
  143. break;
  144. case AUD_CLK_A:
  145. pll_id = AUD_PLL_A1;
  146. pll_auto = true;
  147. break;
  148. case AUD_CLK_F:
  149. pll_id = AUD_PLL_F1;
  150. pll_auto = true;
  151. break;
  152. case AUD_CLK_APLL:
  153. pll_id = AUD_PLL_APLL;
  154. break;
  155. case AUD_CLK_RX0:
  156. pll_id = AUD_PLL_RX0;
  157. break;
  158. case AUD_CLK_USB0:
  159. pll_id = AUD_PLL_USB0;
  160. break;
  161. case AUD_CLK_HSC0:
  162. pll_id = AUD_PLL_HSC0;
  163. break;
  164. default:
  165. dev_err(dev, "Sysclk(%d) is not supported\n", clk_id);
  166. return -EINVAL;
  167. }
  168. if (pll_auto) {
  169. for (pll_id = 0; pll_id < aio->chip->num_plls; pll_id++) {
  170. div_id = find_divider(aio, pll_id, freq);
  171. if (div_id >= 0) {
  172. aio->plldiv = div_id;
  173. break;
  174. }
  175. }
  176. if (pll_id == aio->chip->num_plls) {
  177. dev_err(dev, "Sysclk frequency is not supported(%d)\n",
  178. freq);
  179. return -EINVAL;
  180. }
  181. }
  182. if (dir == SND_SOC_CLOCK_OUT)
  183. aio->pll_out = pll_id;
  184. else
  185. aio->pll_in = pll_id;
  186. return 0;
  187. }
  188. static int uniphier_aio_set_pll(struct snd_soc_dai *dai, int pll_id,
  189. int source, unsigned int freq_in,
  190. unsigned int freq_out)
  191. {
  192. struct uniphier_aio *aio = uniphier_priv(dai);
  193. int ret;
  194. if (!is_valid_pll(aio->chip, pll_id))
  195. return -EINVAL;
  196. ret = aio_chip_set_pll(aio->chip, pll_id, freq_out);
  197. if (ret < 0)
  198. return ret;
  199. return 0;
  200. }
  201. static int uniphier_aio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  202. {
  203. struct uniphier_aio *aio = uniphier_priv(dai);
  204. struct device *dev = &aio->chip->pdev->dev;
  205. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  206. case SND_SOC_DAIFMT_LEFT_J:
  207. case SND_SOC_DAIFMT_RIGHT_J:
  208. case SND_SOC_DAIFMT_I2S:
  209. aio->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  210. break;
  211. default:
  212. dev_err(dev, "Format is not supported(%d)\n",
  213. fmt & SND_SOC_DAIFMT_FORMAT_MASK);
  214. return -EINVAL;
  215. }
  216. return 0;
  217. }
  218. static int uniphier_aio_startup(struct snd_pcm_substream *substream,
  219. struct snd_soc_dai *dai)
  220. {
  221. struct uniphier_aio *aio = uniphier_priv(dai);
  222. struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
  223. sub->substream = substream;
  224. sub->pass_through = 0;
  225. sub->use_mmap = true;
  226. return aio_init(sub);
  227. }
  228. static void uniphier_aio_shutdown(struct snd_pcm_substream *substream,
  229. struct snd_soc_dai *dai)
  230. {
  231. struct uniphier_aio *aio = uniphier_priv(dai);
  232. struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
  233. sub->substream = NULL;
  234. }
  235. static int uniphier_aio_hw_params(struct snd_pcm_substream *substream,
  236. struct snd_pcm_hw_params *params,
  237. struct snd_soc_dai *dai)
  238. {
  239. struct uniphier_aio *aio = uniphier_priv(dai);
  240. struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
  241. struct device *dev = &aio->chip->pdev->dev;
  242. int freq, ret;
  243. switch (params_rate(params)) {
  244. case 48000:
  245. case 32000:
  246. case 24000:
  247. freq = 12288000;
  248. break;
  249. case 44100:
  250. case 22050:
  251. freq = 11289600;
  252. break;
  253. default:
  254. dev_err(dev, "Rate is not supported(%d)\n",
  255. params_rate(params));
  256. return -EINVAL;
  257. }
  258. ret = snd_soc_dai_set_sysclk(dai, AUD_CLK_A,
  259. freq, SND_SOC_CLOCK_OUT);
  260. if (ret)
  261. return ret;
  262. sub->params = *params;
  263. sub->setting = 1;
  264. aio_port_reset(sub);
  265. aio_port_set_volume(sub, sub->vol);
  266. aio_src_reset(sub);
  267. return 0;
  268. }
  269. static int uniphier_aio_hw_free(struct snd_pcm_substream *substream,
  270. struct snd_soc_dai *dai)
  271. {
  272. struct uniphier_aio *aio = uniphier_priv(dai);
  273. struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
  274. sub->setting = 0;
  275. return 0;
  276. }
  277. static int uniphier_aio_prepare(struct snd_pcm_substream *substream,
  278. struct snd_soc_dai *dai)
  279. {
  280. struct uniphier_aio *aio = uniphier_priv(dai);
  281. struct uniphier_aio_sub *sub = &aio->sub[substream->stream];
  282. int ret;
  283. ret = aio_port_set_param(sub, sub->pass_through, &sub->params);
  284. if (ret)
  285. return ret;
  286. ret = aio_src_set_param(sub, &sub->params);
  287. if (ret)
  288. return ret;
  289. aio_port_set_enable(sub, 1);
  290. ret = aio_if_set_param(sub, sub->pass_through);
  291. if (ret)
  292. return ret;
  293. if (sub->swm->type == PORT_TYPE_CONV) {
  294. ret = aio_srcif_set_param(sub);
  295. if (ret)
  296. return ret;
  297. ret = aio_srcch_set_param(sub);
  298. if (ret)
  299. return ret;
  300. aio_srcch_set_enable(sub, 1);
  301. }
  302. return 0;
  303. }
  304. static int uniphier_aio_dai_probe(struct snd_soc_dai *dai)
  305. {
  306. struct uniphier_aio *aio = uniphier_priv(dai);
  307. int i;
  308. for (i = 0; i < ARRAY_SIZE(aio->sub); i++) {
  309. struct uniphier_aio_sub *sub = &aio->sub[i];
  310. const struct uniphier_aio_spec *spec;
  311. spec = find_spec(aio, dai->name, i);
  312. if (!spec)
  313. continue;
  314. sub->swm = &spec->swm;
  315. sub->spec = spec;
  316. sub->vol = AUD_VOL_INIT;
  317. }
  318. aio_iecout_set_enable(aio->chip, true);
  319. aio_chip_init(aio->chip);
  320. aio->chip->active = 1;
  321. return 0;
  322. }
  323. static int uniphier_aio_dai_remove(struct snd_soc_dai *dai)
  324. {
  325. struct uniphier_aio *aio = uniphier_priv(dai);
  326. aio->chip->active = 0;
  327. return 0;
  328. }
  329. static int uniphier_aio_ld11_probe(struct snd_soc_dai *dai)
  330. {
  331. int ret;
  332. ret = uniphier_aio_dai_probe(dai);
  333. if (ret < 0)
  334. return ret;
  335. ret = snd_soc_dai_set_pll(dai, AUD_PLL_A1, 0, 0, 36864000);
  336. if (ret < 0)
  337. return ret;
  338. ret = snd_soc_dai_set_pll(dai, AUD_PLL_F1, 0, 0, 36864000);
  339. if (ret < 0)
  340. return ret;
  341. ret = snd_soc_dai_set_pll(dai, AUD_PLL_A2, 0, 0, 33868800);
  342. if (ret < 0)
  343. return ret;
  344. ret = snd_soc_dai_set_pll(dai, AUD_PLL_F2, 0, 0, 33868800);
  345. if (ret < 0)
  346. return ret;
  347. return 0;
  348. }
  349. static int uniphier_aio_pxs2_probe(struct snd_soc_dai *dai)
  350. {
  351. int ret;
  352. ret = uniphier_aio_dai_probe(dai);
  353. if (ret < 0)
  354. return ret;
  355. ret = snd_soc_dai_set_pll(dai, AUD_PLL_A1, 0, 0, 36864000);
  356. if (ret < 0)
  357. return ret;
  358. ret = snd_soc_dai_set_pll(dai, AUD_PLL_F1, 0, 0, 36864000);
  359. if (ret < 0)
  360. return ret;
  361. ret = snd_soc_dai_set_pll(dai, AUD_PLL_A2, 0, 0, 33868800);
  362. if (ret < 0)
  363. return ret;
  364. ret = snd_soc_dai_set_pll(dai, AUD_PLL_F2, 0, 0, 33868800);
  365. if (ret < 0)
  366. return ret;
  367. return 0;
  368. }
  369. const struct snd_soc_dai_ops uniphier_aio_i2s_ld11_ops = {
  370. .probe = uniphier_aio_ld11_probe,
  371. .remove = uniphier_aio_dai_remove,
  372. .set_sysclk = uniphier_aio_set_sysclk,
  373. .set_pll = uniphier_aio_set_pll,
  374. .set_fmt = uniphier_aio_set_fmt,
  375. .startup = uniphier_aio_startup,
  376. .shutdown = uniphier_aio_shutdown,
  377. .hw_params = uniphier_aio_hw_params,
  378. .hw_free = uniphier_aio_hw_free,
  379. .prepare = uniphier_aio_prepare,
  380. };
  381. EXPORT_SYMBOL_GPL(uniphier_aio_i2s_ld11_ops);
  382. const struct snd_soc_dai_ops uniphier_aio_spdif_ld11_ops = {
  383. .probe = uniphier_aio_ld11_probe,
  384. .remove = uniphier_aio_dai_remove,
  385. .set_sysclk = uniphier_aio_set_sysclk,
  386. .set_pll = uniphier_aio_set_pll,
  387. .startup = uniphier_aio_startup,
  388. .shutdown = uniphier_aio_shutdown,
  389. .hw_params = uniphier_aio_hw_params,
  390. .hw_free = uniphier_aio_hw_free,
  391. .prepare = uniphier_aio_prepare,
  392. };
  393. EXPORT_SYMBOL_GPL(uniphier_aio_spdif_ld11_ops);
  394. const struct snd_soc_dai_ops uniphier_aio_spdif_ld11_ops2 = {
  395. .probe = uniphier_aio_ld11_probe,
  396. .remove = uniphier_aio_dai_remove,
  397. .set_sysclk = uniphier_aio_set_sysclk,
  398. .set_pll = uniphier_aio_set_pll,
  399. .startup = uniphier_aio_startup,
  400. .shutdown = uniphier_aio_shutdown,
  401. .hw_params = uniphier_aio_hw_params,
  402. .hw_free = uniphier_aio_hw_free,
  403. .prepare = uniphier_aio_prepare,
  404. .compress_new = snd_soc_new_compress,
  405. };
  406. EXPORT_SYMBOL_GPL(uniphier_aio_spdif_ld11_ops2);
  407. const struct snd_soc_dai_ops uniphier_aio_i2s_pxs2_ops = {
  408. .probe = uniphier_aio_pxs2_probe,
  409. .remove = uniphier_aio_dai_remove,
  410. .set_sysclk = uniphier_aio_set_sysclk,
  411. .set_pll = uniphier_aio_set_pll,
  412. .set_fmt = uniphier_aio_set_fmt,
  413. .startup = uniphier_aio_startup,
  414. .shutdown = uniphier_aio_shutdown,
  415. .hw_params = uniphier_aio_hw_params,
  416. .hw_free = uniphier_aio_hw_free,
  417. .prepare = uniphier_aio_prepare,
  418. };
  419. EXPORT_SYMBOL_GPL(uniphier_aio_i2s_pxs2_ops);
  420. const struct snd_soc_dai_ops uniphier_aio_spdif_pxs2_ops = {
  421. .probe = uniphier_aio_pxs2_probe,
  422. .remove = uniphier_aio_dai_remove,
  423. .set_sysclk = uniphier_aio_set_sysclk,
  424. .set_pll = uniphier_aio_set_pll,
  425. .startup = uniphier_aio_startup,
  426. .shutdown = uniphier_aio_shutdown,
  427. .hw_params = uniphier_aio_hw_params,
  428. .hw_free = uniphier_aio_hw_free,
  429. .prepare = uniphier_aio_prepare,
  430. };
  431. EXPORT_SYMBOL_GPL(uniphier_aio_spdif_pxs2_ops);
  432. const struct snd_soc_dai_ops uniphier_aio_spdif_pxs2_ops2 = {
  433. .probe = uniphier_aio_pxs2_probe,
  434. .remove = uniphier_aio_dai_remove,
  435. .set_sysclk = uniphier_aio_set_sysclk,
  436. .set_pll = uniphier_aio_set_pll,
  437. .startup = uniphier_aio_startup,
  438. .shutdown = uniphier_aio_shutdown,
  439. .hw_params = uniphier_aio_hw_params,
  440. .hw_free = uniphier_aio_hw_free,
  441. .prepare = uniphier_aio_prepare,
  442. .compress_new = snd_soc_new_compress,
  443. };
  444. EXPORT_SYMBOL_GPL(uniphier_aio_spdif_pxs2_ops2);
  445. static void uniphier_aio_dai_suspend(struct snd_soc_dai *dai)
  446. {
  447. struct uniphier_aio *aio = uniphier_priv(dai);
  448. if (!snd_soc_dai_active(dai))
  449. return;
  450. aio->chip->num_wup_aios--;
  451. if (!aio->chip->num_wup_aios) {
  452. reset_control_assert(aio->chip->rst);
  453. clk_disable_unprepare(aio->chip->clk);
  454. }
  455. }
  456. static int uniphier_aio_suspend(struct snd_soc_component *component)
  457. {
  458. struct snd_soc_dai *dai;
  459. for_each_component_dais(component, dai)
  460. uniphier_aio_dai_suspend(dai);
  461. return 0;
  462. }
  463. static int uniphier_aio_dai_resume(struct snd_soc_dai *dai)
  464. {
  465. struct uniphier_aio *aio = uniphier_priv(dai);
  466. int ret, i;
  467. if (!snd_soc_dai_active(dai))
  468. return 0;
  469. if (!aio->chip->active)
  470. return 0;
  471. if (!aio->chip->num_wup_aios) {
  472. ret = clk_prepare_enable(aio->chip->clk);
  473. if (ret)
  474. return ret;
  475. ret = reset_control_deassert(aio->chip->rst);
  476. if (ret)
  477. goto err_out_clock;
  478. }
  479. aio_iecout_set_enable(aio->chip, true);
  480. aio_chip_init(aio->chip);
  481. for (i = 0; i < ARRAY_SIZE(aio->sub); i++) {
  482. struct uniphier_aio_sub *sub = &aio->sub[i];
  483. if (!sub->spec || !sub->substream)
  484. continue;
  485. ret = aio_init(sub);
  486. if (ret)
  487. goto err_out_reset;
  488. if (!sub->setting)
  489. continue;
  490. aio_port_reset(sub);
  491. aio_src_reset(sub);
  492. }
  493. aio->chip->num_wup_aios++;
  494. return 0;
  495. err_out_reset:
  496. if (!aio->chip->num_wup_aios)
  497. reset_control_assert(aio->chip->rst);
  498. err_out_clock:
  499. if (!aio->chip->num_wup_aios)
  500. clk_disable_unprepare(aio->chip->clk);
  501. return ret;
  502. }
  503. static int uniphier_aio_resume(struct snd_soc_component *component)
  504. {
  505. struct snd_soc_dai *dai;
  506. int ret = 0;
  507. for_each_component_dais(component, dai)
  508. ret |= uniphier_aio_dai_resume(dai);
  509. return ret;
  510. }
  511. static int uniphier_aio_vol_info(struct snd_kcontrol *kcontrol,
  512. struct snd_ctl_elem_info *uinfo)
  513. {
  514. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  515. uinfo->count = 1;
  516. uinfo->value.integer.min = 0;
  517. uinfo->value.integer.max = AUD_VOL_MAX;
  518. return 0;
  519. }
  520. static int uniphier_aio_vol_get(struct snd_kcontrol *kcontrol,
  521. struct snd_ctl_elem_value *ucontrol)
  522. {
  523. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  524. struct uniphier_aio_chip *chip = snd_soc_component_get_drvdata(comp);
  525. struct uniphier_aio_sub *sub;
  526. int oport_hw = kcontrol->private_value;
  527. sub = find_volume(chip, oport_hw);
  528. if (!sub)
  529. return 0;
  530. ucontrol->value.integer.value[0] = sub->vol;
  531. return 0;
  532. }
  533. static int uniphier_aio_vol_put(struct snd_kcontrol *kcontrol,
  534. struct snd_ctl_elem_value *ucontrol)
  535. {
  536. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  537. struct uniphier_aio_chip *chip = snd_soc_component_get_drvdata(comp);
  538. struct uniphier_aio_sub *sub;
  539. int oport_hw = kcontrol->private_value;
  540. sub = find_volume(chip, oport_hw);
  541. if (!sub)
  542. return 0;
  543. if (sub->vol == ucontrol->value.integer.value[0])
  544. return 0;
  545. sub->vol = ucontrol->value.integer.value[0];
  546. aio_port_set_volume(sub, sub->vol);
  547. return 0;
  548. }
  549. static const struct snd_kcontrol_new uniphier_aio_controls[] = {
  550. {
  551. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  552. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  553. .name = "HPCMOUT1 Volume",
  554. .info = uniphier_aio_vol_info,
  555. .get = uniphier_aio_vol_get,
  556. .put = uniphier_aio_vol_put,
  557. .private_value = AUD_HW_HPCMOUT1,
  558. },
  559. {
  560. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  561. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  562. .name = "PCMOUT1 Volume",
  563. .info = uniphier_aio_vol_info,
  564. .get = uniphier_aio_vol_get,
  565. .put = uniphier_aio_vol_put,
  566. .private_value = AUD_HW_PCMOUT1,
  567. },
  568. {
  569. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  570. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  571. .name = "PCMOUT2 Volume",
  572. .info = uniphier_aio_vol_info,
  573. .get = uniphier_aio_vol_get,
  574. .put = uniphier_aio_vol_put,
  575. .private_value = AUD_HW_PCMOUT2,
  576. },
  577. {
  578. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  579. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  580. .name = "PCMOUT3 Volume",
  581. .info = uniphier_aio_vol_info,
  582. .get = uniphier_aio_vol_get,
  583. .put = uniphier_aio_vol_put,
  584. .private_value = AUD_HW_PCMOUT3,
  585. },
  586. {
  587. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  588. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  589. .name = "HIECOUT1 Volume",
  590. .info = uniphier_aio_vol_info,
  591. .get = uniphier_aio_vol_get,
  592. .put = uniphier_aio_vol_put,
  593. .private_value = AUD_HW_HIECOUT1,
  594. },
  595. {
  596. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  597. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  598. .name = "IECOUT1 Volume",
  599. .info = uniphier_aio_vol_info,
  600. .get = uniphier_aio_vol_get,
  601. .put = uniphier_aio_vol_put,
  602. .private_value = AUD_HW_IECOUT1,
  603. },
  604. };
  605. static const struct snd_soc_component_driver uniphier_aio_component = {
  606. .name = "uniphier-aio",
  607. .controls = uniphier_aio_controls,
  608. .num_controls = ARRAY_SIZE(uniphier_aio_controls),
  609. .suspend = uniphier_aio_suspend,
  610. .resume = uniphier_aio_resume,
  611. };
  612. int uniphier_aio_probe(struct platform_device *pdev)
  613. {
  614. struct uniphier_aio_chip *chip;
  615. struct device *dev = &pdev->dev;
  616. int ret, i, j;
  617. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  618. if (!chip)
  619. return -ENOMEM;
  620. chip->chip_spec = of_device_get_match_data(dev);
  621. if (!chip->chip_spec)
  622. return -EINVAL;
  623. chip->regmap_sg = syscon_regmap_lookup_by_phandle(dev->of_node,
  624. "socionext,syscon");
  625. if (IS_ERR(chip->regmap_sg)) {
  626. if (PTR_ERR(chip->regmap_sg) == -EPROBE_DEFER)
  627. return -EPROBE_DEFER;
  628. chip->regmap_sg = NULL;
  629. }
  630. chip->clk = devm_clk_get(dev, "aio");
  631. if (IS_ERR(chip->clk))
  632. return PTR_ERR(chip->clk);
  633. chip->rst = devm_reset_control_get_shared(dev, "aio");
  634. if (IS_ERR(chip->rst))
  635. return PTR_ERR(chip->rst);
  636. chip->num_aios = chip->chip_spec->num_dais;
  637. chip->num_wup_aios = chip->num_aios;
  638. chip->aios = devm_kcalloc(dev,
  639. chip->num_aios, sizeof(struct uniphier_aio),
  640. GFP_KERNEL);
  641. if (!chip->aios)
  642. return -ENOMEM;
  643. chip->num_plls = chip->chip_spec->num_plls;
  644. chip->plls = devm_kmemdup_array(dev, chip->chip_spec->plls, chip->num_plls,
  645. sizeof(*chip->chip_spec->plls), GFP_KERNEL);
  646. if (!chip->plls)
  647. return -ENOMEM;
  648. for (i = 0; i < chip->num_aios; i++) {
  649. struct uniphier_aio *aio = &chip->aios[i];
  650. aio->chip = chip;
  651. aio->fmt = SND_SOC_DAIFMT_I2S;
  652. for (j = 0; j < ARRAY_SIZE(aio->sub); j++) {
  653. struct uniphier_aio_sub *sub = &aio->sub[j];
  654. sub->aio = aio;
  655. spin_lock_init(&sub->lock);
  656. }
  657. }
  658. chip->pdev = pdev;
  659. platform_set_drvdata(pdev, chip);
  660. ret = clk_prepare_enable(chip->clk);
  661. if (ret)
  662. return ret;
  663. ret = reset_control_deassert(chip->rst);
  664. if (ret)
  665. goto err_out_clock;
  666. ret = devm_snd_soc_register_component(dev, &uniphier_aio_component,
  667. chip->chip_spec->dais,
  668. chip->chip_spec->num_dais);
  669. if (ret) {
  670. dev_err(dev, "Register component failed.\n");
  671. goto err_out_reset;
  672. }
  673. ret = uniphier_aiodma_soc_register_platform(pdev);
  674. if (ret) {
  675. dev_err(dev, "Register platform failed.\n");
  676. goto err_out_reset;
  677. }
  678. return 0;
  679. err_out_reset:
  680. reset_control_assert(chip->rst);
  681. err_out_clock:
  682. clk_disable_unprepare(chip->clk);
  683. return ret;
  684. }
  685. EXPORT_SYMBOL_GPL(uniphier_aio_probe);
  686. void uniphier_aio_remove(struct platform_device *pdev)
  687. {
  688. struct uniphier_aio_chip *chip = platform_get_drvdata(pdev);
  689. reset_control_assert(chip->rst);
  690. clk_disable_unprepare(chip->clk);
  691. }
  692. EXPORT_SYMBOL_GPL(uniphier_aio_remove);
  693. MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
  694. MODULE_DESCRIPTION("UniPhier AIO CPU DAI driver.");
  695. MODULE_LICENSE("GPL v2");