siu_dai.c 20 KB

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