siu_dai.c 20 KB

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