i2s.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /* sound/soc/samsung/i2s.c
  2. *
  3. * ALSA SoC Audio Layer - Samsung I2S Controller driver
  4. *
  5. * Copyright (c) 2010 Samsung Electronics Co. Ltd.
  6. * Jaswinder Singh <jassisinghbrar@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/slab.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/pm_runtime.h>
  18. #include <sound/soc.h>
  19. #include <sound/pcm_params.h>
  20. #include <plat/audio.h>
  21. #include "dma.h"
  22. #include "idma.h"
  23. #include "i2s.h"
  24. #include "i2s-regs.h"
  25. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  26. struct i2s_dai {
  27. /* Platform device for this DAI */
  28. struct platform_device *pdev;
  29. /* IOREMAP'd SFRs */
  30. void __iomem *addr;
  31. /* Physical base address of SFRs */
  32. u32 base;
  33. /* Rate of RCLK source clock */
  34. unsigned long rclk_srcrate;
  35. /* Frame Clock */
  36. unsigned frmclk;
  37. /*
  38. * Specifically requested RCLK,BCLK by MACHINE Driver.
  39. * 0 indicates CPU driver is free to choose any value.
  40. */
  41. unsigned rfs, bfs;
  42. /* I2S Controller's core clock */
  43. struct clk *clk;
  44. /* Clock for generating I2S signals */
  45. struct clk *op_clk;
  46. /* Array of clock names for op_clk */
  47. const char **src_clk;
  48. /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
  49. struct i2s_dai *pri_dai;
  50. /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
  51. struct i2s_dai *sec_dai;
  52. #define DAI_OPENED (1 << 0) /* Dai is opened */
  53. #define DAI_MANAGER (1 << 1) /* Dai is the manager */
  54. unsigned mode;
  55. /* Driver for this DAI */
  56. struct snd_soc_dai_driver i2s_dai_drv;
  57. /* DMA parameters */
  58. struct s3c_dma_params dma_playback;
  59. struct s3c_dma_params dma_capture;
  60. struct s3c_dma_params idma_playback;
  61. u32 quirks;
  62. u32 suspend_i2smod;
  63. u32 suspend_i2scon;
  64. u32 suspend_i2spsr;
  65. };
  66. /* Lock for cross i/f checks */
  67. static DEFINE_SPINLOCK(lock);
  68. /* If this is the 'overlay' stereo DAI */
  69. static inline bool is_secondary(struct i2s_dai *i2s)
  70. {
  71. return i2s->pri_dai ? true : false;
  72. }
  73. /* If operating in SoC-Slave mode */
  74. static inline bool is_slave(struct i2s_dai *i2s)
  75. {
  76. return (readl(i2s->addr + I2SMOD) & MOD_SLAVE) ? true : false;
  77. }
  78. /* If this interface of the controller is transmitting data */
  79. static inline bool tx_active(struct i2s_dai *i2s)
  80. {
  81. u32 active;
  82. if (!i2s)
  83. return false;
  84. active = readl(i2s->addr + I2SCON);
  85. if (is_secondary(i2s))
  86. active &= CON_TXSDMA_ACTIVE;
  87. else
  88. active &= CON_TXDMA_ACTIVE;
  89. return active ? true : false;
  90. }
  91. /* If the other interface of the controller is transmitting data */
  92. static inline bool other_tx_active(struct i2s_dai *i2s)
  93. {
  94. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  95. return tx_active(other);
  96. }
  97. /* If any interface of the controller is transmitting data */
  98. static inline bool any_tx_active(struct i2s_dai *i2s)
  99. {
  100. return tx_active(i2s) || other_tx_active(i2s);
  101. }
  102. /* If this interface of the controller is receiving data */
  103. static inline bool rx_active(struct i2s_dai *i2s)
  104. {
  105. u32 active;
  106. if (!i2s)
  107. return false;
  108. active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
  109. return active ? true : false;
  110. }
  111. /* If the other interface of the controller is receiving data */
  112. static inline bool other_rx_active(struct i2s_dai *i2s)
  113. {
  114. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  115. return rx_active(other);
  116. }
  117. /* If any interface of the controller is receiving data */
  118. static inline bool any_rx_active(struct i2s_dai *i2s)
  119. {
  120. return rx_active(i2s) || other_rx_active(i2s);
  121. }
  122. /* If the other DAI is transmitting or receiving data */
  123. static inline bool other_active(struct i2s_dai *i2s)
  124. {
  125. return other_rx_active(i2s) || other_tx_active(i2s);
  126. }
  127. /* If this DAI is transmitting or receiving data */
  128. static inline bool this_active(struct i2s_dai *i2s)
  129. {
  130. return tx_active(i2s) || rx_active(i2s);
  131. }
  132. /* If the controller is active anyway */
  133. static inline bool any_active(struct i2s_dai *i2s)
  134. {
  135. return this_active(i2s) || other_active(i2s);
  136. }
  137. static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
  138. {
  139. return snd_soc_dai_get_drvdata(dai);
  140. }
  141. static inline bool is_opened(struct i2s_dai *i2s)
  142. {
  143. if (i2s && (i2s->mode & DAI_OPENED))
  144. return true;
  145. else
  146. return false;
  147. }
  148. static inline bool is_manager(struct i2s_dai *i2s)
  149. {
  150. if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
  151. return true;
  152. else
  153. return false;
  154. }
  155. /* Read RCLK of I2S (in multiples of LRCLK) */
  156. static inline unsigned get_rfs(struct i2s_dai *i2s)
  157. {
  158. u32 rfs = (readl(i2s->addr + I2SMOD) >> 3) & 0x3;
  159. switch (rfs) {
  160. case 3: return 768;
  161. case 2: return 384;
  162. case 1: return 512;
  163. default: return 256;
  164. }
  165. }
  166. /* Write RCLK of I2S (in multiples of LRCLK) */
  167. static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
  168. {
  169. u32 mod = readl(i2s->addr + I2SMOD);
  170. mod &= ~MOD_RCLK_MASK;
  171. switch (rfs) {
  172. case 768:
  173. mod |= MOD_RCLK_768FS;
  174. break;
  175. case 512:
  176. mod |= MOD_RCLK_512FS;
  177. break;
  178. case 384:
  179. mod |= MOD_RCLK_384FS;
  180. break;
  181. default:
  182. mod |= MOD_RCLK_256FS;
  183. break;
  184. }
  185. writel(mod, i2s->addr + I2SMOD);
  186. }
  187. /* Read Bit-Clock of I2S (in multiples of LRCLK) */
  188. static inline unsigned get_bfs(struct i2s_dai *i2s)
  189. {
  190. u32 bfs = (readl(i2s->addr + I2SMOD) >> 1) & 0x3;
  191. switch (bfs) {
  192. case 3: return 24;
  193. case 2: return 16;
  194. case 1: return 48;
  195. default: return 32;
  196. }
  197. }
  198. /* Write Bit-Clock of I2S (in multiples of LRCLK) */
  199. static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
  200. {
  201. u32 mod = readl(i2s->addr + I2SMOD);
  202. mod &= ~MOD_BCLK_MASK;
  203. switch (bfs) {
  204. case 48:
  205. mod |= MOD_BCLK_48FS;
  206. break;
  207. case 32:
  208. mod |= MOD_BCLK_32FS;
  209. break;
  210. case 24:
  211. mod |= MOD_BCLK_24FS;
  212. break;
  213. case 16:
  214. mod |= MOD_BCLK_16FS;
  215. break;
  216. default:
  217. dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
  218. return;
  219. }
  220. writel(mod, i2s->addr + I2SMOD);
  221. }
  222. /* Sample-Size */
  223. static inline int get_blc(struct i2s_dai *i2s)
  224. {
  225. int blc = readl(i2s->addr + I2SMOD);
  226. blc = (blc >> 13) & 0x3;
  227. switch (blc) {
  228. case 2: return 24;
  229. case 1: return 8;
  230. default: return 16;
  231. }
  232. }
  233. /* TX Channel Control */
  234. static void i2s_txctrl(struct i2s_dai *i2s, int on)
  235. {
  236. void __iomem *addr = i2s->addr;
  237. u32 con = readl(addr + I2SCON);
  238. u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
  239. if (on) {
  240. con |= CON_ACTIVE;
  241. con &= ~CON_TXCH_PAUSE;
  242. if (is_secondary(i2s)) {
  243. con |= CON_TXSDMA_ACTIVE;
  244. con &= ~CON_TXSDMA_PAUSE;
  245. } else {
  246. con |= CON_TXDMA_ACTIVE;
  247. con &= ~CON_TXDMA_PAUSE;
  248. }
  249. if (any_rx_active(i2s))
  250. mod |= MOD_TXRX;
  251. else
  252. mod |= MOD_TXONLY;
  253. } else {
  254. if (is_secondary(i2s)) {
  255. con |= CON_TXSDMA_PAUSE;
  256. con &= ~CON_TXSDMA_ACTIVE;
  257. } else {
  258. con |= CON_TXDMA_PAUSE;
  259. con &= ~CON_TXDMA_ACTIVE;
  260. }
  261. if (other_tx_active(i2s)) {
  262. writel(con, addr + I2SCON);
  263. return;
  264. }
  265. con |= CON_TXCH_PAUSE;
  266. if (any_rx_active(i2s))
  267. mod |= MOD_RXONLY;
  268. else
  269. con &= ~CON_ACTIVE;
  270. }
  271. writel(mod, addr + I2SMOD);
  272. writel(con, addr + I2SCON);
  273. }
  274. /* RX Channel Control */
  275. static void i2s_rxctrl(struct i2s_dai *i2s, int on)
  276. {
  277. void __iomem *addr = i2s->addr;
  278. u32 con = readl(addr + I2SCON);
  279. u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
  280. if (on) {
  281. con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
  282. con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
  283. if (any_tx_active(i2s))
  284. mod |= MOD_TXRX;
  285. else
  286. mod |= MOD_RXONLY;
  287. } else {
  288. con |= CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
  289. con &= ~CON_RXDMA_ACTIVE;
  290. if (any_tx_active(i2s))
  291. mod |= MOD_TXONLY;
  292. else
  293. con &= ~CON_ACTIVE;
  294. }
  295. writel(mod, addr + I2SMOD);
  296. writel(con, addr + I2SCON);
  297. }
  298. /* Flush FIFO of an interface */
  299. static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
  300. {
  301. void __iomem *fic;
  302. u32 val;
  303. if (!i2s)
  304. return;
  305. if (is_secondary(i2s))
  306. fic = i2s->addr + I2SFICS;
  307. else
  308. fic = i2s->addr + I2SFIC;
  309. /* Flush the FIFO */
  310. writel(readl(fic) | flush, fic);
  311. /* Be patient */
  312. val = msecs_to_loops(1) / 1000; /* 1 usec */
  313. while (--val)
  314. cpu_relax();
  315. writel(readl(fic) & ~flush, fic);
  316. }
  317. static int i2s_set_sysclk(struct snd_soc_dai *dai,
  318. int clk_id, unsigned int rfs, int dir)
  319. {
  320. struct i2s_dai *i2s = to_info(dai);
  321. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  322. u32 mod = readl(i2s->addr + I2SMOD);
  323. switch (clk_id) {
  324. case SAMSUNG_I2S_CDCLK:
  325. /* Shouldn't matter in GATING(CLOCK_IN) mode */
  326. if (dir == SND_SOC_CLOCK_IN)
  327. rfs = 0;
  328. if ((rfs && other && other->rfs && (other->rfs != rfs)) ||
  329. (any_active(i2s) &&
  330. (((dir == SND_SOC_CLOCK_IN)
  331. && !(mod & MOD_CDCLKCON)) ||
  332. ((dir == SND_SOC_CLOCK_OUT)
  333. && (mod & MOD_CDCLKCON))))) {
  334. dev_err(&i2s->pdev->dev,
  335. "%s:%d Other DAI busy\n", __func__, __LINE__);
  336. return -EAGAIN;
  337. }
  338. if (dir == SND_SOC_CLOCK_IN)
  339. mod |= MOD_CDCLKCON;
  340. else
  341. mod &= ~MOD_CDCLKCON;
  342. i2s->rfs = rfs;
  343. break;
  344. case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
  345. case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
  346. if ((i2s->quirks & QUIRK_NO_MUXPSR)
  347. || (clk_id == SAMSUNG_I2S_RCLKSRC_0))
  348. clk_id = 0;
  349. else
  350. clk_id = 1;
  351. if (!any_active(i2s)) {
  352. if (i2s->op_clk) {
  353. if ((clk_id && !(mod & MOD_IMS_SYSMUX)) ||
  354. (!clk_id && (mod & MOD_IMS_SYSMUX))) {
  355. clk_disable(i2s->op_clk);
  356. clk_put(i2s->op_clk);
  357. } else {
  358. i2s->rclk_srcrate =
  359. clk_get_rate(i2s->op_clk);
  360. return 0;
  361. }
  362. }
  363. i2s->op_clk = clk_get(&i2s->pdev->dev,
  364. i2s->src_clk[clk_id]);
  365. clk_enable(i2s->op_clk);
  366. i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);
  367. /* Over-ride the other's */
  368. if (other) {
  369. other->op_clk = i2s->op_clk;
  370. other->rclk_srcrate = i2s->rclk_srcrate;
  371. }
  372. } else if ((!clk_id && (mod & MOD_IMS_SYSMUX))
  373. || (clk_id && !(mod & MOD_IMS_SYSMUX))) {
  374. dev_err(&i2s->pdev->dev,
  375. "%s:%d Other DAI busy\n", __func__, __LINE__);
  376. return -EAGAIN;
  377. } else {
  378. /* Call can't be on the active DAI */
  379. i2s->op_clk = other->op_clk;
  380. i2s->rclk_srcrate = other->rclk_srcrate;
  381. return 0;
  382. }
  383. if (clk_id == 0)
  384. mod &= ~MOD_IMS_SYSMUX;
  385. else
  386. mod |= MOD_IMS_SYSMUX;
  387. break;
  388. default:
  389. dev_err(&i2s->pdev->dev, "We don't serve that!\n");
  390. return -EINVAL;
  391. }
  392. writel(mod, i2s->addr + I2SMOD);
  393. return 0;
  394. }
  395. static int i2s_set_fmt(struct snd_soc_dai *dai,
  396. unsigned int fmt)
  397. {
  398. struct i2s_dai *i2s = to_info(dai);
  399. u32 mod = readl(i2s->addr + I2SMOD);
  400. u32 tmp = 0;
  401. /* Format is priority */
  402. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  403. case SND_SOC_DAIFMT_RIGHT_J:
  404. tmp |= MOD_LR_RLOW;
  405. tmp |= MOD_SDF_MSB;
  406. break;
  407. case SND_SOC_DAIFMT_LEFT_J:
  408. tmp |= MOD_LR_RLOW;
  409. tmp |= MOD_SDF_LSB;
  410. break;
  411. case SND_SOC_DAIFMT_I2S:
  412. tmp |= MOD_SDF_IIS;
  413. break;
  414. default:
  415. dev_err(&i2s->pdev->dev, "Format not supported\n");
  416. return -EINVAL;
  417. }
  418. /*
  419. * INV flag is relative to the FORMAT flag - if set it simply
  420. * flips the polarity specified by the Standard
  421. */
  422. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  423. case SND_SOC_DAIFMT_NB_NF:
  424. break;
  425. case SND_SOC_DAIFMT_NB_IF:
  426. if (tmp & MOD_LR_RLOW)
  427. tmp &= ~MOD_LR_RLOW;
  428. else
  429. tmp |= MOD_LR_RLOW;
  430. break;
  431. default:
  432. dev_err(&i2s->pdev->dev, "Polarity not supported\n");
  433. return -EINVAL;
  434. }
  435. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  436. case SND_SOC_DAIFMT_CBM_CFM:
  437. tmp |= MOD_SLAVE;
  438. break;
  439. case SND_SOC_DAIFMT_CBS_CFS:
  440. /* Set default source clock in Master mode */
  441. if (i2s->rclk_srcrate == 0)
  442. i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
  443. 0, SND_SOC_CLOCK_IN);
  444. break;
  445. default:
  446. dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
  447. return -EINVAL;
  448. }
  449. if (any_active(i2s) &&
  450. ((mod & (MOD_SDF_MASK | MOD_LR_RLOW
  451. | MOD_SLAVE)) != tmp)) {
  452. dev_err(&i2s->pdev->dev,
  453. "%s:%d Other DAI busy\n", __func__, __LINE__);
  454. return -EAGAIN;
  455. }
  456. mod &= ~(MOD_SDF_MASK | MOD_LR_RLOW | MOD_SLAVE);
  457. mod |= tmp;
  458. writel(mod, i2s->addr + I2SMOD);
  459. return 0;
  460. }
  461. static int i2s_hw_params(struct snd_pcm_substream *substream,
  462. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  463. {
  464. struct i2s_dai *i2s = to_info(dai);
  465. u32 mod = readl(i2s->addr + I2SMOD);
  466. if (!is_secondary(i2s))
  467. mod &= ~(MOD_DC2_EN | MOD_DC1_EN);
  468. switch (params_channels(params)) {
  469. case 6:
  470. mod |= MOD_DC2_EN;
  471. case 4:
  472. mod |= MOD_DC1_EN;
  473. break;
  474. case 2:
  475. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  476. i2s->dma_playback.dma_size = 4;
  477. else
  478. i2s->dma_capture.dma_size = 4;
  479. break;
  480. case 1:
  481. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  482. i2s->dma_playback.dma_size = 2;
  483. else
  484. i2s->dma_capture.dma_size = 2;
  485. break;
  486. default:
  487. dev_err(&i2s->pdev->dev, "%d channels not supported\n",
  488. params_channels(params));
  489. return -EINVAL;
  490. }
  491. if (is_secondary(i2s))
  492. mod &= ~MOD_BLCS_MASK;
  493. else
  494. mod &= ~MOD_BLCP_MASK;
  495. if (is_manager(i2s))
  496. mod &= ~MOD_BLC_MASK;
  497. switch (params_format(params)) {
  498. case SNDRV_PCM_FORMAT_S8:
  499. if (is_secondary(i2s))
  500. mod |= MOD_BLCS_8BIT;
  501. else
  502. mod |= MOD_BLCP_8BIT;
  503. if (is_manager(i2s))
  504. mod |= MOD_BLC_8BIT;
  505. break;
  506. case SNDRV_PCM_FORMAT_S16_LE:
  507. if (is_secondary(i2s))
  508. mod |= MOD_BLCS_16BIT;
  509. else
  510. mod |= MOD_BLCP_16BIT;
  511. if (is_manager(i2s))
  512. mod |= MOD_BLC_16BIT;
  513. break;
  514. case SNDRV_PCM_FORMAT_S24_LE:
  515. if (is_secondary(i2s))
  516. mod |= MOD_BLCS_24BIT;
  517. else
  518. mod |= MOD_BLCP_24BIT;
  519. if (is_manager(i2s))
  520. mod |= MOD_BLC_24BIT;
  521. break;
  522. default:
  523. dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
  524. params_format(params));
  525. return -EINVAL;
  526. }
  527. writel(mod, i2s->addr + I2SMOD);
  528. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  529. snd_soc_dai_set_dma_data(dai, substream,
  530. (void *)&i2s->dma_playback);
  531. else
  532. snd_soc_dai_set_dma_data(dai, substream,
  533. (void *)&i2s->dma_capture);
  534. i2s->frmclk = params_rate(params);
  535. return 0;
  536. }
  537. /* We set constraints on the substream acc to the version of I2S */
  538. static int i2s_startup(struct snd_pcm_substream *substream,
  539. struct snd_soc_dai *dai)
  540. {
  541. struct i2s_dai *i2s = to_info(dai);
  542. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  543. unsigned long flags;
  544. spin_lock_irqsave(&lock, flags);
  545. i2s->mode |= DAI_OPENED;
  546. if (is_manager(other))
  547. i2s->mode &= ~DAI_MANAGER;
  548. else
  549. i2s->mode |= DAI_MANAGER;
  550. /* Enforce set_sysclk in Master mode */
  551. i2s->rclk_srcrate = 0;
  552. spin_unlock_irqrestore(&lock, flags);
  553. return 0;
  554. }
  555. static void i2s_shutdown(struct snd_pcm_substream *substream,
  556. struct snd_soc_dai *dai)
  557. {
  558. struct i2s_dai *i2s = to_info(dai);
  559. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  560. unsigned long flags;
  561. spin_lock_irqsave(&lock, flags);
  562. i2s->mode &= ~DAI_OPENED;
  563. i2s->mode &= ~DAI_MANAGER;
  564. if (is_opened(other))
  565. other->mode |= DAI_MANAGER;
  566. /* Reset any constraint on RFS and BFS */
  567. i2s->rfs = 0;
  568. i2s->bfs = 0;
  569. spin_unlock_irqrestore(&lock, flags);
  570. /* Gate CDCLK by default */
  571. if (!is_opened(other))
  572. i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
  573. 0, SND_SOC_CLOCK_IN);
  574. }
  575. static int config_setup(struct i2s_dai *i2s)
  576. {
  577. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  578. unsigned rfs, bfs, blc;
  579. u32 psr;
  580. blc = get_blc(i2s);
  581. bfs = i2s->bfs;
  582. if (!bfs && other)
  583. bfs = other->bfs;
  584. /* Select least possible multiple(2) if no constraint set */
  585. if (!bfs)
  586. bfs = blc * 2;
  587. rfs = i2s->rfs;
  588. if (!rfs && other)
  589. rfs = other->rfs;
  590. if ((rfs == 256 || rfs == 512) && (blc == 24)) {
  591. dev_err(&i2s->pdev->dev,
  592. "%d-RFS not supported for 24-blc\n", rfs);
  593. return -EINVAL;
  594. }
  595. if (!rfs) {
  596. if (bfs == 16 || bfs == 32)
  597. rfs = 256;
  598. else
  599. rfs = 384;
  600. }
  601. /* If already setup and running */
  602. if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
  603. dev_err(&i2s->pdev->dev,
  604. "%s:%d Other DAI busy\n", __func__, __LINE__);
  605. return -EAGAIN;
  606. }
  607. /* Don't bother RFS, BFS & PSR in Slave mode */
  608. if (is_slave(i2s))
  609. return 0;
  610. set_bfs(i2s, bfs);
  611. set_rfs(i2s, rfs);
  612. if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
  613. psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
  614. writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
  615. dev_dbg(&i2s->pdev->dev,
  616. "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
  617. i2s->rclk_srcrate, psr, rfs, bfs);
  618. }
  619. return 0;
  620. }
  621. static int i2s_trigger(struct snd_pcm_substream *substream,
  622. int cmd, struct snd_soc_dai *dai)
  623. {
  624. int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  625. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  626. struct i2s_dai *i2s = to_info(rtd->cpu_dai);
  627. unsigned long flags;
  628. switch (cmd) {
  629. case SNDRV_PCM_TRIGGER_START:
  630. case SNDRV_PCM_TRIGGER_RESUME:
  631. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  632. local_irq_save(flags);
  633. if (config_setup(i2s)) {
  634. local_irq_restore(flags);
  635. return -EINVAL;
  636. }
  637. if (capture)
  638. i2s_rxctrl(i2s, 1);
  639. else
  640. i2s_txctrl(i2s, 1);
  641. local_irq_restore(flags);
  642. break;
  643. case SNDRV_PCM_TRIGGER_STOP:
  644. case SNDRV_PCM_TRIGGER_SUSPEND:
  645. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  646. local_irq_save(flags);
  647. if (capture) {
  648. i2s_rxctrl(i2s, 0);
  649. i2s_fifo(i2s, FIC_RXFLUSH);
  650. } else {
  651. i2s_txctrl(i2s, 0);
  652. i2s_fifo(i2s, FIC_TXFLUSH);
  653. }
  654. local_irq_restore(flags);
  655. break;
  656. }
  657. return 0;
  658. }
  659. static int i2s_set_clkdiv(struct snd_soc_dai *dai,
  660. int div_id, int div)
  661. {
  662. struct i2s_dai *i2s = to_info(dai);
  663. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  664. switch (div_id) {
  665. case SAMSUNG_I2S_DIV_BCLK:
  666. if ((any_active(i2s) && div && (get_bfs(i2s) != div))
  667. || (other && other->bfs && (other->bfs != div))) {
  668. dev_err(&i2s->pdev->dev,
  669. "%s:%d Other DAI busy\n", __func__, __LINE__);
  670. return -EAGAIN;
  671. }
  672. i2s->bfs = div;
  673. break;
  674. default:
  675. dev_err(&i2s->pdev->dev,
  676. "Invalid clock divider(%d)\n", div_id);
  677. return -EINVAL;
  678. }
  679. return 0;
  680. }
  681. static snd_pcm_sframes_t
  682. i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  683. {
  684. struct i2s_dai *i2s = to_info(dai);
  685. u32 reg = readl(i2s->addr + I2SFIC);
  686. snd_pcm_sframes_t delay;
  687. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  688. delay = FIC_RXCOUNT(reg);
  689. else if (is_secondary(i2s))
  690. delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
  691. else
  692. delay = FIC_TXCOUNT(reg);
  693. return delay;
  694. }
  695. #ifdef CONFIG_PM
  696. static int i2s_suspend(struct snd_soc_dai *dai)
  697. {
  698. struct i2s_dai *i2s = to_info(dai);
  699. if (dai->active) {
  700. i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
  701. i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
  702. i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
  703. }
  704. return 0;
  705. }
  706. static int i2s_resume(struct snd_soc_dai *dai)
  707. {
  708. struct i2s_dai *i2s = to_info(dai);
  709. if (dai->active) {
  710. writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
  711. writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
  712. writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
  713. }
  714. return 0;
  715. }
  716. #else
  717. #define i2s_suspend NULL
  718. #define i2s_resume NULL
  719. #endif
  720. static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
  721. {
  722. struct i2s_dai *i2s = to_info(dai);
  723. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  724. if (other && other->clk) /* If this is probe on secondary */
  725. goto probe_exit;
  726. i2s->addr = ioremap(i2s->base, 0x100);
  727. if (i2s->addr == NULL) {
  728. dev_err(&i2s->pdev->dev, "cannot ioremap registers\n");
  729. return -ENXIO;
  730. }
  731. i2s->clk = clk_get(&i2s->pdev->dev, "iis");
  732. if (IS_ERR(i2s->clk)) {
  733. dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n");
  734. iounmap(i2s->addr);
  735. return -ENOENT;
  736. }
  737. clk_enable(i2s->clk);
  738. if (other) {
  739. other->addr = i2s->addr;
  740. other->clk = i2s->clk;
  741. }
  742. if (i2s->quirks & QUIRK_NEED_RSTCLR)
  743. writel(CON_RSTCLR, i2s->addr + I2SCON);
  744. if (i2s->quirks & QUIRK_SEC_DAI)
  745. idma_reg_addr_init(i2s->addr,
  746. i2s->sec_dai->idma_playback.dma_addr);
  747. probe_exit:
  748. /* Reset any constraint on RFS and BFS */
  749. i2s->rfs = 0;
  750. i2s->bfs = 0;
  751. i2s_txctrl(i2s, 0);
  752. i2s_rxctrl(i2s, 0);
  753. i2s_fifo(i2s, FIC_TXFLUSH);
  754. i2s_fifo(other, FIC_TXFLUSH);
  755. i2s_fifo(i2s, FIC_RXFLUSH);
  756. /* Gate CDCLK by default */
  757. if (!is_opened(other))
  758. i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
  759. 0, SND_SOC_CLOCK_IN);
  760. return 0;
  761. }
  762. static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
  763. {
  764. struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
  765. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  766. if (!other || !other->clk) {
  767. if (i2s->quirks & QUIRK_NEED_RSTCLR)
  768. writel(0, i2s->addr + I2SCON);
  769. clk_disable(i2s->clk);
  770. clk_put(i2s->clk);
  771. iounmap(i2s->addr);
  772. }
  773. i2s->clk = NULL;
  774. return 0;
  775. }
  776. static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
  777. .trigger = i2s_trigger,
  778. .hw_params = i2s_hw_params,
  779. .set_fmt = i2s_set_fmt,
  780. .set_clkdiv = i2s_set_clkdiv,
  781. .set_sysclk = i2s_set_sysclk,
  782. .startup = i2s_startup,
  783. .shutdown = i2s_shutdown,
  784. .delay = i2s_delay,
  785. };
  786. #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
  787. #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
  788. SNDRV_PCM_FMTBIT_S16_LE | \
  789. SNDRV_PCM_FMTBIT_S24_LE)
  790. static __devinit
  791. struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
  792. {
  793. struct i2s_dai *i2s;
  794. i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
  795. if (i2s == NULL)
  796. return NULL;
  797. i2s->pdev = pdev;
  798. i2s->pri_dai = NULL;
  799. i2s->sec_dai = NULL;
  800. i2s->i2s_dai_drv.symmetric_rates = 1;
  801. i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
  802. i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
  803. i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
  804. i2s->i2s_dai_drv.suspend = i2s_suspend;
  805. i2s->i2s_dai_drv.resume = i2s_resume;
  806. i2s->i2s_dai_drv.playback.channels_min = 2;
  807. i2s->i2s_dai_drv.playback.channels_max = 2;
  808. i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
  809. i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
  810. if (!sec) {
  811. i2s->i2s_dai_drv.capture.channels_min = 1;
  812. i2s->i2s_dai_drv.capture.channels_max = 2;
  813. i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
  814. i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
  815. } else { /* Create a new platform_device for Secondary */
  816. i2s->pdev = platform_device_register_resndata(NULL,
  817. pdev->name, pdev->id + SAMSUNG_I2S_SECOFF,
  818. NULL, 0, NULL, 0);
  819. if (IS_ERR(i2s->pdev))
  820. return NULL;
  821. }
  822. /* Pre-assign snd_soc_dai_set_drvdata */
  823. dev_set_drvdata(&i2s->pdev->dev, i2s);
  824. return i2s;
  825. }
  826. static __devinit int samsung_i2s_probe(struct platform_device *pdev)
  827. {
  828. u32 dma_pl_chan, dma_cp_chan, dma_pl_sec_chan;
  829. struct i2s_dai *pri_dai, *sec_dai = NULL;
  830. struct s3c_audio_pdata *i2s_pdata;
  831. struct samsung_i2s *i2s_cfg;
  832. struct resource *res;
  833. u32 regs_base, quirks;
  834. int ret = 0;
  835. /* Call during Seconday interface registration */
  836. if (pdev->id >= SAMSUNG_I2S_SECOFF) {
  837. sec_dai = dev_get_drvdata(&pdev->dev);
  838. snd_soc_register_dai(&sec_dai->pdev->dev,
  839. &sec_dai->i2s_dai_drv);
  840. return 0;
  841. }
  842. i2s_pdata = pdev->dev.platform_data;
  843. if (i2s_pdata == NULL) {
  844. dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
  845. return -EINVAL;
  846. }
  847. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  848. if (!res) {
  849. dev_err(&pdev->dev, "Unable to get I2S-TX dma resource\n");
  850. return -ENXIO;
  851. }
  852. dma_pl_chan = res->start;
  853. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  854. if (!res) {
  855. dev_err(&pdev->dev, "Unable to get I2S-RX dma resource\n");
  856. return -ENXIO;
  857. }
  858. dma_cp_chan = res->start;
  859. res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
  860. if (res)
  861. dma_pl_sec_chan = res->start;
  862. else
  863. dma_pl_sec_chan = 0;
  864. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  865. if (!res) {
  866. dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
  867. return -ENXIO;
  868. }
  869. if (!request_mem_region(res->start, resource_size(res),
  870. "samsung-i2s")) {
  871. dev_err(&pdev->dev, "Unable to request SFR region\n");
  872. return -EBUSY;
  873. }
  874. regs_base = res->start;
  875. i2s_cfg = &i2s_pdata->type.i2s;
  876. quirks = i2s_cfg->quirks;
  877. pri_dai = i2s_alloc_dai(pdev, false);
  878. if (!pri_dai) {
  879. dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
  880. ret = -ENOMEM;
  881. goto err;
  882. }
  883. pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
  884. pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
  885. pri_dai->dma_playback.client =
  886. (struct s3c2410_dma_client *)&pri_dai->dma_playback;
  887. pri_dai->dma_capture.client =
  888. (struct s3c2410_dma_client *)&pri_dai->dma_capture;
  889. pri_dai->dma_playback.channel = dma_pl_chan;
  890. pri_dai->dma_capture.channel = dma_cp_chan;
  891. pri_dai->src_clk = i2s_cfg->src_clk;
  892. pri_dai->dma_playback.dma_size = 4;
  893. pri_dai->dma_capture.dma_size = 4;
  894. pri_dai->base = regs_base;
  895. pri_dai->quirks = quirks;
  896. if (quirks & QUIRK_PRI_6CHAN)
  897. pri_dai->i2s_dai_drv.playback.channels_max = 6;
  898. if (quirks & QUIRK_SEC_DAI) {
  899. sec_dai = i2s_alloc_dai(pdev, true);
  900. if (!sec_dai) {
  901. dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
  902. ret = -ENOMEM;
  903. goto err;
  904. }
  905. sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
  906. sec_dai->dma_playback.client =
  907. (struct s3c2410_dma_client *)&sec_dai->dma_playback;
  908. /* Use iDMA always if SysDMA not provided */
  909. sec_dai->dma_playback.channel = dma_pl_sec_chan ? : -1;
  910. sec_dai->src_clk = i2s_cfg->src_clk;
  911. sec_dai->dma_playback.dma_size = 4;
  912. sec_dai->base = regs_base;
  913. sec_dai->quirks = quirks;
  914. sec_dai->idma_playback.dma_addr = i2s_cfg->idma_addr;
  915. sec_dai->pri_dai = pri_dai;
  916. pri_dai->sec_dai = sec_dai;
  917. }
  918. if (i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
  919. dev_err(&pdev->dev, "Unable to configure gpio\n");
  920. ret = -EINVAL;
  921. goto err;
  922. }
  923. snd_soc_register_dai(&pri_dai->pdev->dev, &pri_dai->i2s_dai_drv);
  924. pm_runtime_enable(&pdev->dev);
  925. return 0;
  926. err:
  927. release_mem_region(regs_base, resource_size(res));
  928. return ret;
  929. }
  930. static __devexit int samsung_i2s_remove(struct platform_device *pdev)
  931. {
  932. struct i2s_dai *i2s, *other;
  933. struct resource *res;
  934. i2s = dev_get_drvdata(&pdev->dev);
  935. other = i2s->pri_dai ? : i2s->sec_dai;
  936. if (other) {
  937. other->pri_dai = NULL;
  938. other->sec_dai = NULL;
  939. } else {
  940. pm_runtime_disable(&pdev->dev);
  941. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  942. if (res)
  943. release_mem_region(res->start, resource_size(res));
  944. }
  945. i2s->pri_dai = NULL;
  946. i2s->sec_dai = NULL;
  947. snd_soc_unregister_dai(&pdev->dev);
  948. return 0;
  949. }
  950. static struct platform_driver samsung_i2s_driver = {
  951. .probe = samsung_i2s_probe,
  952. .remove = __devexit_p(samsung_i2s_remove),
  953. .driver = {
  954. .name = "samsung-i2s",
  955. .owner = THIS_MODULE,
  956. },
  957. };
  958. module_platform_driver(samsung_i2s_driver);
  959. /* Module information */
  960. MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
  961. MODULE_DESCRIPTION("Samsung I2S Interface");
  962. MODULE_ALIAS("platform:samsung-i2s");
  963. MODULE_LICENSE("GPL");