siu_dai.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // siu_dai.c - ALSA SoC driver for Renesas SH7343, SH7722 SIU peripheral.
  4. //
  5. // Copyright (C) 2009-2010 Guennadi Liakhovetski <[email protected]>
  6. // Copyright (C) 2006 Carlos Munoz <[email protected]>
  7. #include <linux/delay.h>
  8. #include <linux/firmware.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <asm/clock.h>
  13. #include <asm/siu.h>
  14. #include <sound/control.h>
  15. #include <sound/soc.h>
  16. #include "siu.h"
  17. /* Board specifics */
  18. #if defined(CONFIG_CPU_SUBTYPE_SH7722)
  19. # define SIU_MAX_VOLUME 0x1000
  20. #else
  21. # define SIU_MAX_VOLUME 0x7fff
  22. #endif
  23. #define PRAM_SIZE 0x2000
  24. #define XRAM_SIZE 0x800
  25. #define YRAM_SIZE 0x800
  26. #define XRAM_OFFSET 0x4000
  27. #define YRAM_OFFSET 0x6000
  28. #define REG_OFFSET 0xc000
  29. #define PLAYBACK_ENABLED 1
  30. #define CAPTURE_ENABLED 2
  31. #define VOLUME_CAPTURE 0
  32. #define VOLUME_PLAYBACK 1
  33. #define DFLT_VOLUME_LEVEL 0x08000800
  34. /*
  35. * SPDIF is only available on port A and on some SIU implementations it is only
  36. * available for input. Due to the lack of hardware to test it, SPDIF is left
  37. * disabled in this driver version
  38. */
  39. struct format_flag {
  40. u32 i2s;
  41. u32 pcm;
  42. u32 spdif;
  43. u32 mask;
  44. };
  45. struct port_flag {
  46. struct format_flag playback;
  47. struct format_flag capture;
  48. };
  49. struct siu_info *siu_i2s_data;
  50. static struct port_flag siu_flags[SIU_PORT_NUM] = {
  51. [SIU_PORT_A] = {
  52. .playback = {
  53. .i2s = 0x50000000,
  54. .pcm = 0x40000000,
  55. .spdif = 0x80000000, /* not on all SIU versions */
  56. .mask = 0xd0000000,
  57. },
  58. .capture = {
  59. .i2s = 0x05000000,
  60. .pcm = 0x04000000,
  61. .spdif = 0x08000000,
  62. .mask = 0x0d000000,
  63. },
  64. },
  65. [SIU_PORT_B] = {
  66. .playback = {
  67. .i2s = 0x00500000,
  68. .pcm = 0x00400000,
  69. .spdif = 0, /* impossible - turn off */
  70. .mask = 0x00500000,
  71. },
  72. .capture = {
  73. .i2s = 0x00050000,
  74. .pcm = 0x00040000,
  75. .spdif = 0, /* impossible - turn off */
  76. .mask = 0x00050000,
  77. },
  78. },
  79. };
  80. static void siu_dai_start(struct siu_port *port_info)
  81. {
  82. struct siu_info *info = siu_i2s_data;
  83. u32 __iomem *base = info->reg;
  84. dev_dbg(port_info->pcm->card->dev, "%s\n", __func__);
  85. /* Issue software reset to siu */
  86. siu_write32(base + SIU_SRCTL, 0);
  87. /* Wait for the reset to take effect */
  88. udelay(1);
  89. port_info->stfifo = 0;
  90. port_info->trdat = 0;
  91. /* portA, portB, SIU operate */
  92. siu_write32(base + SIU_SRCTL, 0x301);
  93. /* portA=256fs, portB=256fs */
  94. siu_write32(base + SIU_CKCTL, 0x40400000);
  95. /* portA's BRG does not divide SIUCKA */
  96. siu_write32(base + SIU_BRGASEL, 0);
  97. siu_write32(base + SIU_BRRA, 0);
  98. /* portB's BRG divides SIUCKB by half */
  99. siu_write32(base + SIU_BRGBSEL, 1);
  100. siu_write32(base + SIU_BRRB, 0);
  101. siu_write32(base + SIU_IFCTL, 0x44440000);
  102. /* portA: 32 bit/fs, master; portB: 32 bit/fs, master */
  103. siu_write32(base + SIU_SFORM, 0x0c0c0000);
  104. /*
  105. * Volume levels: looks like the DSP firmware implements volume controls
  106. * differently from what's described in the datasheet
  107. */
  108. siu_write32(base + SIU_SBDVCA, port_info->playback.volume);
  109. siu_write32(base + SIU_SBDVCB, port_info->capture.volume);
  110. }
  111. static void siu_dai_stop(struct siu_port *port_info)
  112. {
  113. struct siu_info *info = siu_i2s_data;
  114. u32 __iomem *base = info->reg;
  115. /* SIU software reset */
  116. siu_write32(base + SIU_SRCTL, 0);
  117. }
  118. static void siu_dai_spbAselect(struct siu_port *port_info)
  119. {
  120. struct siu_info *info = siu_i2s_data;
  121. struct siu_firmware *fw = &info->fw;
  122. u32 *ydef = fw->yram0;
  123. u32 idx;
  124. /* path A use */
  125. if (!info->port_id)
  126. idx = 1; /* portA */
  127. else
  128. idx = 2; /* portB */
  129. ydef[0] = (fw->spbpar[idx].ab1a << 16) |
  130. (fw->spbpar[idx].ab0a << 8) |
  131. (fw->spbpar[idx].dir << 7) | 3;
  132. ydef[1] = fw->yram0[1]; /* 0x03000300 */
  133. ydef[2] = (16 / 2) << 24;
  134. ydef[3] = fw->yram0[3]; /* 0 */
  135. ydef[4] = fw->yram0[4]; /* 0 */
  136. ydef[7] = fw->spbpar[idx].event;
  137. port_info->stfifo |= fw->spbpar[idx].stfifo;
  138. port_info->trdat |= fw->spbpar[idx].trdat;
  139. }
  140. static void siu_dai_spbBselect(struct siu_port *port_info)
  141. {
  142. struct siu_info *info = siu_i2s_data;
  143. struct siu_firmware *fw = &info->fw;
  144. u32 *ydef = fw->yram0;
  145. u32 idx;
  146. /* path B use */
  147. if (!info->port_id)
  148. idx = 7; /* portA */
  149. else
  150. idx = 8; /* portB */
  151. ydef[5] = (fw->spbpar[idx].ab1a << 16) |
  152. (fw->spbpar[idx].ab0a << 8) | 1;
  153. ydef[6] = fw->spbpar[idx].event;
  154. port_info->stfifo |= fw->spbpar[idx].stfifo;
  155. port_info->trdat |= fw->spbpar[idx].trdat;
  156. }
  157. static void siu_dai_open(struct siu_stream *siu_stream)
  158. {
  159. struct siu_info *info = siu_i2s_data;
  160. u32 __iomem *base = info->reg;
  161. u32 srctl, ifctl;
  162. srctl = siu_read32(base + SIU_SRCTL);
  163. ifctl = siu_read32(base + SIU_IFCTL);
  164. switch (info->port_id) {
  165. case SIU_PORT_A:
  166. /* portA operates */
  167. srctl |= 0x200;
  168. ifctl &= ~0xc2;
  169. break;
  170. case SIU_PORT_B:
  171. /* portB operates */
  172. srctl |= 0x100;
  173. ifctl &= ~0x31;
  174. break;
  175. }
  176. siu_write32(base + SIU_SRCTL, srctl);
  177. /* Unmute and configure portA */
  178. siu_write32(base + SIU_IFCTL, ifctl);
  179. }
  180. /*
  181. * At the moment only fixed Left-upper, Left-lower, Right-upper, Right-lower
  182. * packing is supported
  183. */
  184. static void siu_dai_pcmdatapack(struct siu_stream *siu_stream)
  185. {
  186. struct siu_info *info = siu_i2s_data;
  187. u32 __iomem *base = info->reg;
  188. u32 dpak;
  189. dpak = siu_read32(base + SIU_DPAK);
  190. switch (info->port_id) {
  191. case SIU_PORT_A:
  192. dpak &= ~0xc0000000;
  193. break;
  194. case SIU_PORT_B:
  195. dpak &= ~0x00c00000;
  196. break;
  197. }
  198. siu_write32(base + SIU_DPAK, dpak);
  199. }
  200. static int siu_dai_spbstart(struct siu_port *port_info)
  201. {
  202. struct siu_info *info = siu_i2s_data;
  203. u32 __iomem *base = info->reg;
  204. struct siu_firmware *fw = &info->fw;
  205. u32 *ydef = fw->yram0;
  206. int cnt;
  207. u32 __iomem *add;
  208. u32 *ptr;
  209. /* Load SPB Program in PRAM */
  210. ptr = fw->pram0;
  211. add = info->pram;
  212. for (cnt = 0; cnt < PRAM0_SIZE; cnt++, add++, ptr++)
  213. siu_write32(add, *ptr);
  214. ptr = fw->pram1;
  215. add = info->pram + (0x0100 / sizeof(u32));
  216. for (cnt = 0; cnt < PRAM1_SIZE; cnt++, add++, ptr++)
  217. siu_write32(add, *ptr);
  218. /* XRAM initialization */
  219. add = info->xram;
  220. for (cnt = 0; cnt < XRAM0_SIZE + XRAM1_SIZE + XRAM2_SIZE; cnt++, add++)
  221. siu_write32(add, 0);
  222. /* YRAM variable area initialization */
  223. add = info->yram;
  224. for (cnt = 0; cnt < YRAM_DEF_SIZE; cnt++, add++)
  225. siu_write32(add, ydef[cnt]);
  226. /* YRAM FIR coefficient area initialization */
  227. add = info->yram + (0x0200 / sizeof(u32));
  228. for (cnt = 0; cnt < YRAM_FIR_SIZE; cnt++, add++)
  229. siu_write32(add, fw->yram_fir_coeff[cnt]);
  230. /* YRAM IIR coefficient area initialization */
  231. add = info->yram + (0x0600 / sizeof(u32));
  232. for (cnt = 0; cnt < YRAM_IIR_SIZE; cnt++, add++)
  233. siu_write32(add, 0);
  234. siu_write32(base + SIU_TRDAT, port_info->trdat);
  235. port_info->trdat = 0x0;
  236. /* SPB start condition: software */
  237. siu_write32(base + SIU_SBACTIV, 0);
  238. /* Start SPB */
  239. siu_write32(base + SIU_SBCTL, 0xc0000000);
  240. /* Wait for program to halt */
  241. cnt = 0x10000;
  242. while (--cnt && siu_read32(base + SIU_SBCTL) != 0x80000000)
  243. cpu_relax();
  244. if (!cnt)
  245. return -EBUSY;
  246. /* SPB program start address setting */
  247. siu_write32(base + SIU_SBPSET, 0x00400000);
  248. /* SPB hardware start(FIFOCTL source) */
  249. siu_write32(base + SIU_SBACTIV, 0xc0000000);
  250. return 0;
  251. }
  252. static void siu_dai_spbstop(struct siu_port *port_info)
  253. {
  254. struct siu_info *info = siu_i2s_data;
  255. u32 __iomem *base = info->reg;
  256. siu_write32(base + SIU_SBACTIV, 0);
  257. /* SPB stop */
  258. siu_write32(base + SIU_SBCTL, 0);
  259. port_info->stfifo = 0;
  260. }
  261. /* API functions */
  262. /* Playback and capture hardware properties are identical */
  263. static const struct snd_pcm_hardware siu_dai_pcm_hw = {
  264. .info = SNDRV_PCM_INFO_INTERLEAVED,
  265. .formats = SNDRV_PCM_FMTBIT_S16,
  266. .rates = SNDRV_PCM_RATE_8000_48000,
  267. .rate_min = 8000,
  268. .rate_max = 48000,
  269. .channels_min = 2,
  270. .channels_max = 2,
  271. .buffer_bytes_max = SIU_BUFFER_BYTES_MAX,
  272. .period_bytes_min = SIU_PERIOD_BYTES_MIN,
  273. .period_bytes_max = SIU_PERIOD_BYTES_MAX,
  274. .periods_min = SIU_PERIODS_MIN,
  275. .periods_max = SIU_PERIODS_MAX,
  276. };
  277. static int siu_dai_info_volume(struct snd_kcontrol *kctrl,
  278. struct snd_ctl_elem_info *uinfo)
  279. {
  280. struct siu_port *port_info = snd_kcontrol_chip(kctrl);
  281. dev_dbg(port_info->pcm->card->dev, "%s\n", __func__);
  282. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  283. uinfo->count = 2;
  284. uinfo->value.integer.min = 0;
  285. uinfo->value.integer.max = SIU_MAX_VOLUME;
  286. return 0;
  287. }
  288. static int siu_dai_get_volume(struct snd_kcontrol *kctrl,
  289. struct snd_ctl_elem_value *ucontrol)
  290. {
  291. struct siu_port *port_info = snd_kcontrol_chip(kctrl);
  292. struct device *dev = port_info->pcm->card->dev;
  293. u32 vol;
  294. dev_dbg(dev, "%s\n", __func__);
  295. switch (kctrl->private_value) {
  296. case VOLUME_PLAYBACK:
  297. /* Playback is always on port 0 */
  298. vol = port_info->playback.volume;
  299. ucontrol->value.integer.value[0] = vol & 0xffff;
  300. ucontrol->value.integer.value[1] = vol >> 16 & 0xffff;
  301. break;
  302. case VOLUME_CAPTURE:
  303. /* Capture is always on port 1 */
  304. vol = port_info->capture.volume;
  305. ucontrol->value.integer.value[0] = vol & 0xffff;
  306. ucontrol->value.integer.value[1] = vol >> 16 & 0xffff;
  307. break;
  308. default:
  309. dev_err(dev, "%s() invalid private_value=%ld\n",
  310. __func__, kctrl->private_value);
  311. return -EINVAL;
  312. }
  313. return 0;
  314. }
  315. static int siu_dai_put_volume(struct snd_kcontrol *kctrl,
  316. struct snd_ctl_elem_value *ucontrol)
  317. {
  318. struct siu_port *port_info = snd_kcontrol_chip(kctrl);
  319. struct device *dev = port_info->pcm->card->dev;
  320. struct siu_info *info = siu_i2s_data;
  321. u32 __iomem *base = info->reg;
  322. u32 new_vol;
  323. u32 cur_vol;
  324. dev_dbg(dev, "%s\n", __func__);
  325. if (ucontrol->value.integer.value[0] < 0 ||
  326. ucontrol->value.integer.value[0] > SIU_MAX_VOLUME ||
  327. ucontrol->value.integer.value[1] < 0 ||
  328. ucontrol->value.integer.value[1] > SIU_MAX_VOLUME)
  329. return -EINVAL;
  330. new_vol = ucontrol->value.integer.value[0] |
  331. ucontrol->value.integer.value[1] << 16;
  332. /* See comment above - DSP firmware implementation */
  333. switch (kctrl->private_value) {
  334. case VOLUME_PLAYBACK:
  335. /* Playback is always on port 0 */
  336. cur_vol = port_info->playback.volume;
  337. siu_write32(base + SIU_SBDVCA, new_vol);
  338. port_info->playback.volume = new_vol;
  339. break;
  340. case VOLUME_CAPTURE:
  341. /* Capture is always on port 1 */
  342. cur_vol = port_info->capture.volume;
  343. siu_write32(base + SIU_SBDVCB, new_vol);
  344. port_info->capture.volume = new_vol;
  345. break;
  346. default:
  347. dev_err(dev, "%s() invalid private_value=%ld\n",
  348. __func__, kctrl->private_value);
  349. return -EINVAL;
  350. }
  351. if (cur_vol != new_vol)
  352. return 1;
  353. return 0;
  354. }
  355. static const struct snd_kcontrol_new playback_controls = {
  356. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  357. .name = "PCM Playback Volume",
  358. .index = 0,
  359. .info = siu_dai_info_volume,
  360. .get = siu_dai_get_volume,
  361. .put = siu_dai_put_volume,
  362. .private_value = VOLUME_PLAYBACK,
  363. };
  364. static const struct snd_kcontrol_new capture_controls = {
  365. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  366. .name = "PCM Capture Volume",
  367. .index = 0,
  368. .info = siu_dai_info_volume,
  369. .get = siu_dai_get_volume,
  370. .put = siu_dai_put_volume,
  371. .private_value = VOLUME_CAPTURE,
  372. };
  373. int siu_init_port(int port, struct siu_port **port_info, struct snd_card *card)
  374. {
  375. struct device *dev = card->dev;
  376. struct snd_kcontrol *kctrl;
  377. int ret;
  378. *port_info = kzalloc(sizeof(**port_info), GFP_KERNEL);
  379. if (!*port_info)
  380. return -ENOMEM;
  381. dev_dbg(dev, "%s: port #%d@%p\n", __func__, port, *port_info);
  382. (*port_info)->playback.volume = DFLT_VOLUME_LEVEL;
  383. (*port_info)->capture.volume = DFLT_VOLUME_LEVEL;
  384. /*
  385. * Add mixer support. The SPB is used to change the volume. Both
  386. * ports use the same SPB. Therefore, we only register one
  387. * control instance since it will be used by both channels.
  388. * In error case we continue without controls.
  389. */
  390. kctrl = snd_ctl_new1(&playback_controls, *port_info);
  391. ret = snd_ctl_add(card, kctrl);
  392. if (ret < 0)
  393. dev_err(dev,
  394. "failed to add playback controls %p port=%d err=%d\n",
  395. kctrl, port, ret);
  396. kctrl = snd_ctl_new1(&capture_controls, *port_info);
  397. ret = snd_ctl_add(card, kctrl);
  398. if (ret < 0)
  399. dev_err(dev,
  400. "failed to add capture controls %p port=%d err=%d\n",
  401. kctrl, port, ret);
  402. return 0;
  403. }
  404. void siu_free_port(struct siu_port *port_info)
  405. {
  406. kfree(port_info);
  407. }
  408. static int siu_dai_startup(struct snd_pcm_substream *substream,
  409. struct snd_soc_dai *dai)
  410. {
  411. struct siu_info *info = snd_soc_dai_get_drvdata(dai);
  412. struct snd_pcm_runtime *rt = substream->runtime;
  413. struct siu_port *port_info = siu_port_info(substream);
  414. int ret;
  415. dev_dbg(substream->pcm->card->dev, "%s: port=%d@%p\n", __func__,
  416. info->port_id, port_info);
  417. snd_soc_set_runtime_hwparams(substream, &siu_dai_pcm_hw);
  418. ret = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
  419. if (unlikely(ret < 0))
  420. return ret;
  421. siu_dai_start(port_info);
  422. return 0;
  423. }
  424. static void siu_dai_shutdown(struct snd_pcm_substream *substream,
  425. struct snd_soc_dai *dai)
  426. {
  427. struct siu_info *info = snd_soc_dai_get_drvdata(dai);
  428. struct siu_port *port_info = siu_port_info(substream);
  429. dev_dbg(substream->pcm->card->dev, "%s: port=%d@%p\n", __func__,
  430. info->port_id, port_info);
  431. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  432. port_info->play_cap &= ~PLAYBACK_ENABLED;
  433. else
  434. port_info->play_cap &= ~CAPTURE_ENABLED;
  435. /* Stop the siu if the other stream is not using it */
  436. if (!port_info->play_cap) {
  437. /* during stmread or stmwrite ? */
  438. if (WARN_ON(port_info->playback.rw_flg || port_info->capture.rw_flg))
  439. return;
  440. siu_dai_spbstop(port_info);
  441. siu_dai_stop(port_info);
  442. }
  443. }
  444. /* PCM part of siu_dai_playback_prepare() / siu_dai_capture_prepare() */
  445. static int siu_dai_prepare(struct snd_pcm_substream *substream,
  446. struct snd_soc_dai *dai)
  447. {
  448. struct siu_info *info = snd_soc_dai_get_drvdata(dai);
  449. struct snd_pcm_runtime *rt = substream->runtime;
  450. struct siu_port *port_info = siu_port_info(substream);
  451. struct siu_stream *siu_stream;
  452. int self, ret;
  453. dev_dbg(substream->pcm->card->dev,
  454. "%s: port %d, active streams %lx, %d channels\n",
  455. __func__, info->port_id, port_info->play_cap, rt->channels);
  456. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  457. self = PLAYBACK_ENABLED;
  458. siu_stream = &port_info->playback;
  459. } else {
  460. self = CAPTURE_ENABLED;
  461. siu_stream = &port_info->capture;
  462. }
  463. /* Set up the siu if not already done */
  464. if (!port_info->play_cap) {
  465. siu_stream->rw_flg = 0; /* stream-data transfer flag */
  466. siu_dai_spbAselect(port_info);
  467. siu_dai_spbBselect(port_info);
  468. siu_dai_open(siu_stream);
  469. siu_dai_pcmdatapack(siu_stream);
  470. ret = siu_dai_spbstart(port_info);
  471. if (ret < 0)
  472. goto fail;
  473. } else {
  474. ret = 0;
  475. }
  476. port_info->play_cap |= self;
  477. fail:
  478. return ret;
  479. }
  480. /*
  481. * SIU can set bus format to I2S / PCM / SPDIF independently for playback and
  482. * capture, however, the current API sets the bus format globally for a DAI.
  483. */
  484. static int siu_dai_set_fmt(struct snd_soc_dai *dai,
  485. unsigned int fmt)
  486. {
  487. struct siu_info *info = snd_soc_dai_get_drvdata(dai);
  488. u32 __iomem *base = info->reg;
  489. u32 ifctl;
  490. dev_dbg(dai->dev, "%s: fmt 0x%x on port %d\n",
  491. __func__, fmt, info->port_id);
  492. if (info->port_id < 0)
  493. return -ENODEV;
  494. /* Here select between I2S / PCM / SPDIF */
  495. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  496. case SND_SOC_DAIFMT_I2S:
  497. ifctl = siu_flags[info->port_id].playback.i2s |
  498. siu_flags[info->port_id].capture.i2s;
  499. break;
  500. case SND_SOC_DAIFMT_LEFT_J:
  501. ifctl = siu_flags[info->port_id].playback.pcm |
  502. siu_flags[info->port_id].capture.pcm;
  503. break;
  504. /* SPDIF disabled - see comment at the top */
  505. default:
  506. return -EINVAL;
  507. }
  508. ifctl |= ~(siu_flags[info->port_id].playback.mask |
  509. siu_flags[info->port_id].capture.mask) &
  510. siu_read32(base + SIU_IFCTL);
  511. siu_write32(base + SIU_IFCTL, ifctl);
  512. return 0;
  513. }
  514. static int siu_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  515. unsigned int freq, int dir)
  516. {
  517. struct clk *siu_clk, *parent_clk;
  518. char *siu_name, *parent_name;
  519. int ret;
  520. if (dir != SND_SOC_CLOCK_IN)
  521. return -EINVAL;
  522. dev_dbg(dai->dev, "%s: using clock %d\n", __func__, clk_id);
  523. switch (clk_id) {
  524. case SIU_CLKA_PLL:
  525. siu_name = "siua_clk";
  526. parent_name = "pll_clk";
  527. break;
  528. case SIU_CLKA_EXT:
  529. siu_name = "siua_clk";
  530. parent_name = "siumcka_clk";
  531. break;
  532. case SIU_CLKB_PLL:
  533. siu_name = "siub_clk";
  534. parent_name = "pll_clk";
  535. break;
  536. case SIU_CLKB_EXT:
  537. siu_name = "siub_clk";
  538. parent_name = "siumckb_clk";
  539. break;
  540. default:
  541. return -EINVAL;
  542. }
  543. siu_clk = clk_get(dai->dev, siu_name);
  544. if (IS_ERR(siu_clk)) {
  545. dev_err(dai->dev, "%s: cannot get a SIU clock: %ld\n", __func__,
  546. PTR_ERR(siu_clk));
  547. return PTR_ERR(siu_clk);
  548. }
  549. parent_clk = clk_get(dai->dev, parent_name);
  550. if (IS_ERR(parent_clk)) {
  551. ret = PTR_ERR(parent_clk);
  552. dev_err(dai->dev, "cannot get a SIU clock parent: %d\n", ret);
  553. goto epclkget;
  554. }
  555. ret = clk_set_parent(siu_clk, parent_clk);
  556. if (ret < 0) {
  557. dev_err(dai->dev, "cannot reparent the SIU clock: %d\n", ret);
  558. goto eclksetp;
  559. }
  560. ret = clk_set_rate(siu_clk, freq);
  561. if (ret < 0)
  562. dev_err(dai->dev, "cannot set SIU clock rate: %d\n", ret);
  563. /* TODO: when clkdev gets reference counting we'll move these to siu_dai_shutdown() */
  564. eclksetp:
  565. clk_put(parent_clk);
  566. epclkget:
  567. clk_put(siu_clk);
  568. return ret;
  569. }
  570. static const struct snd_soc_dai_ops siu_dai_ops = {
  571. .startup = siu_dai_startup,
  572. .shutdown = siu_dai_shutdown,
  573. .prepare = siu_dai_prepare,
  574. .set_sysclk = siu_dai_set_sysclk,
  575. .set_fmt = siu_dai_set_fmt,
  576. };
  577. static struct snd_soc_dai_driver siu_i2s_dai = {
  578. .name = "siu-i2s-dai",
  579. .playback = {
  580. .channels_min = 2,
  581. .channels_max = 2,
  582. .formats = SNDRV_PCM_FMTBIT_S16,
  583. .rates = SNDRV_PCM_RATE_8000_48000,
  584. },
  585. .capture = {
  586. .channels_min = 2,
  587. .channels_max = 2,
  588. .formats = SNDRV_PCM_FMTBIT_S16,
  589. .rates = SNDRV_PCM_RATE_8000_48000,
  590. },
  591. .ops = &siu_dai_ops,
  592. };
  593. static int siu_probe(struct platform_device *pdev)
  594. {
  595. const struct firmware *fw_entry;
  596. struct resource *res, *region;
  597. struct siu_info *info;
  598. int ret;
  599. info = devm_kmalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  600. if (!info)
  601. return -ENOMEM;
  602. siu_i2s_data = info;
  603. info->dev = &pdev->dev;
  604. ret = request_firmware(&fw_entry, "siu_spb.bin", &pdev->dev);
  605. if (ret)
  606. return ret;
  607. /*
  608. * Loaded firmware is "const" - read only, but we have to modify it in
  609. * snd_siu_sh7343_spbAselect() and snd_siu_sh7343_spbBselect()
  610. */
  611. memcpy(&info->fw, fw_entry->data, fw_entry->size);
  612. release_firmware(fw_entry);
  613. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  614. if (!res)
  615. return -ENODEV;
  616. region = devm_request_mem_region(&pdev->dev, res->start,
  617. resource_size(res), pdev->name);
  618. if (!region) {
  619. dev_err(&pdev->dev, "SIU region already claimed\n");
  620. return -EBUSY;
  621. }
  622. info->pram = devm_ioremap(&pdev->dev, res->start, PRAM_SIZE);
  623. if (!info->pram)
  624. return -ENOMEM;
  625. info->xram = devm_ioremap(&pdev->dev, res->start + XRAM_OFFSET,
  626. XRAM_SIZE);
  627. if (!info->xram)
  628. return -ENOMEM;
  629. info->yram = devm_ioremap(&pdev->dev, res->start + YRAM_OFFSET,
  630. YRAM_SIZE);
  631. if (!info->yram)
  632. return -ENOMEM;
  633. info->reg = devm_ioremap(&pdev->dev, res->start + REG_OFFSET,
  634. resource_size(res) - REG_OFFSET);
  635. if (!info->reg)
  636. return -ENOMEM;
  637. dev_set_drvdata(&pdev->dev, info);
  638. /* register using ARRAY version so we can keep dai name */
  639. ret = devm_snd_soc_register_component(&pdev->dev, &siu_component,
  640. &siu_i2s_dai, 1);
  641. if (ret < 0)
  642. return ret;
  643. pm_runtime_enable(&pdev->dev);
  644. return 0;
  645. }
  646. static int siu_remove(struct platform_device *pdev)
  647. {
  648. pm_runtime_disable(&pdev->dev);
  649. return 0;
  650. }
  651. static struct platform_driver siu_driver = {
  652. .driver = {
  653. .name = "siu-pcm-audio",
  654. },
  655. .probe = siu_probe,
  656. .remove = siu_remove,
  657. };
  658. module_platform_driver(siu_driver);
  659. MODULE_AUTHOR("Carlos Munoz <[email protected]>");
  660. MODULE_DESCRIPTION("ALSA SoC SH7722 SIU driver");
  661. MODULE_LICENSE("GPL");