siu_dai.c 21 KB

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