siu_dai.c 20 KB

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