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