i2s.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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 <jassi.brar@samsung.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->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. break;
  476. default:
  477. dev_err(&i2s->pdev->dev, "%d channels not supported\n",
  478. params_channels(params));
  479. return -EINVAL;
  480. }
  481. if (is_secondary(i2s))
  482. mod &= ~MOD_BLCS_MASK;
  483. else
  484. mod &= ~MOD_BLCP_MASK;
  485. if (is_manager(i2s))
  486. mod &= ~MOD_BLC_MASK;
  487. switch (params_format(params)) {
  488. case SNDRV_PCM_FORMAT_S8:
  489. if (is_secondary(i2s))
  490. mod |= MOD_BLCS_8BIT;
  491. else
  492. mod |= MOD_BLCP_8BIT;
  493. if (is_manager(i2s))
  494. mod |= MOD_BLC_8BIT;
  495. break;
  496. case SNDRV_PCM_FORMAT_S16_LE:
  497. if (is_secondary(i2s))
  498. mod |= MOD_BLCS_16BIT;
  499. else
  500. mod |= MOD_BLCP_16BIT;
  501. if (is_manager(i2s))
  502. mod |= MOD_BLC_16BIT;
  503. break;
  504. case SNDRV_PCM_FORMAT_S24_LE:
  505. if (is_secondary(i2s))
  506. mod |= MOD_BLCS_24BIT;
  507. else
  508. mod |= MOD_BLCP_24BIT;
  509. if (is_manager(i2s))
  510. mod |= MOD_BLC_24BIT;
  511. break;
  512. default:
  513. dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
  514. params_format(params));
  515. return -EINVAL;
  516. }
  517. writel(mod, i2s->addr + I2SMOD);
  518. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  519. snd_soc_dai_set_dma_data(dai, substream,
  520. (void *)&i2s->dma_playback);
  521. else
  522. snd_soc_dai_set_dma_data(dai, substream,
  523. (void *)&i2s->dma_capture);
  524. i2s->frmclk = params_rate(params);
  525. return 0;
  526. }
  527. /* We set constraints on the substream acc to the version of I2S */
  528. static int i2s_startup(struct snd_pcm_substream *substream,
  529. struct snd_soc_dai *dai)
  530. {
  531. struct i2s_dai *i2s = to_info(dai);
  532. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  533. unsigned long flags;
  534. spin_lock_irqsave(&lock, flags);
  535. i2s->mode |= DAI_OPENED;
  536. if (is_manager(other))
  537. i2s->mode &= ~DAI_MANAGER;
  538. else
  539. i2s->mode |= DAI_MANAGER;
  540. /* Enforce set_sysclk in Master mode */
  541. i2s->rclk_srcrate = 0;
  542. spin_unlock_irqrestore(&lock, flags);
  543. return 0;
  544. }
  545. static void i2s_shutdown(struct snd_pcm_substream *substream,
  546. struct snd_soc_dai *dai)
  547. {
  548. struct i2s_dai *i2s = to_info(dai);
  549. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  550. unsigned long flags;
  551. spin_lock_irqsave(&lock, flags);
  552. i2s->mode &= ~DAI_OPENED;
  553. i2s->mode &= ~DAI_MANAGER;
  554. if (is_opened(other))
  555. other->mode |= DAI_MANAGER;
  556. /* Reset any constraint on RFS and BFS */
  557. i2s->rfs = 0;
  558. i2s->bfs = 0;
  559. spin_unlock_irqrestore(&lock, flags);
  560. /* Gate CDCLK by default */
  561. if (!is_opened(other))
  562. i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
  563. 0, SND_SOC_CLOCK_IN);
  564. }
  565. static int config_setup(struct i2s_dai *i2s)
  566. {
  567. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  568. unsigned rfs, bfs, blc;
  569. u32 psr;
  570. blc = get_blc(i2s);
  571. bfs = i2s->bfs;
  572. if (!bfs && other)
  573. bfs = other->bfs;
  574. /* Select least possible multiple(2) if no constraint set */
  575. if (!bfs)
  576. bfs = blc * 2;
  577. rfs = i2s->rfs;
  578. if (!rfs && other)
  579. rfs = other->rfs;
  580. if ((rfs == 256 || rfs == 512) && (blc == 24)) {
  581. dev_err(&i2s->pdev->dev,
  582. "%d-RFS not supported for 24-blc\n", rfs);
  583. return -EINVAL;
  584. }
  585. if (!rfs) {
  586. if (bfs == 16 || bfs == 32)
  587. rfs = 256;
  588. else
  589. rfs = 384;
  590. }
  591. /* If already setup and running */
  592. if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
  593. dev_err(&i2s->pdev->dev,
  594. "%s:%d Other DAI busy\n", __func__, __LINE__);
  595. return -EAGAIN;
  596. }
  597. /* Don't bother RFS, BFS & PSR in Slave mode */
  598. if (is_slave(i2s))
  599. return 0;
  600. set_bfs(i2s, bfs);
  601. set_rfs(i2s, rfs);
  602. if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
  603. psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
  604. writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
  605. dev_dbg(&i2s->pdev->dev,
  606. "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
  607. i2s->rclk_srcrate, psr, rfs, bfs);
  608. }
  609. return 0;
  610. }
  611. static int i2s_trigger(struct snd_pcm_substream *substream,
  612. int cmd, struct snd_soc_dai *dai)
  613. {
  614. int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  615. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  616. struct i2s_dai *i2s = to_info(rtd->cpu_dai);
  617. unsigned long flags;
  618. switch (cmd) {
  619. case SNDRV_PCM_TRIGGER_START:
  620. case SNDRV_PCM_TRIGGER_RESUME:
  621. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  622. local_irq_save(flags);
  623. if (config_setup(i2s)) {
  624. local_irq_restore(flags);
  625. return -EINVAL;
  626. }
  627. if (capture)
  628. i2s_rxctrl(i2s, 1);
  629. else
  630. i2s_txctrl(i2s, 1);
  631. local_irq_restore(flags);
  632. break;
  633. case SNDRV_PCM_TRIGGER_STOP:
  634. case SNDRV_PCM_TRIGGER_SUSPEND:
  635. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  636. local_irq_save(flags);
  637. if (capture)
  638. i2s_rxctrl(i2s, 0);
  639. else
  640. i2s_txctrl(i2s, 0);
  641. if (capture)
  642. i2s_fifo(i2s, FIC_RXFLUSH);
  643. else
  644. i2s_fifo(i2s, FIC_TXFLUSH);
  645. local_irq_restore(flags);
  646. break;
  647. }
  648. return 0;
  649. }
  650. static int i2s_set_clkdiv(struct snd_soc_dai *dai,
  651. int div_id, int div)
  652. {
  653. struct i2s_dai *i2s = to_info(dai);
  654. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  655. switch (div_id) {
  656. case SAMSUNG_I2S_DIV_BCLK:
  657. if ((any_active(i2s) && div && (get_bfs(i2s) != div))
  658. || (other && other->bfs && (other->bfs != div))) {
  659. dev_err(&i2s->pdev->dev,
  660. "%s:%d Other DAI busy\n", __func__, __LINE__);
  661. return -EAGAIN;
  662. }
  663. i2s->bfs = div;
  664. break;
  665. default:
  666. dev_err(&i2s->pdev->dev,
  667. "Invalid clock divider(%d)\n", div_id);
  668. return -EINVAL;
  669. }
  670. return 0;
  671. }
  672. static snd_pcm_sframes_t
  673. i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  674. {
  675. struct i2s_dai *i2s = to_info(dai);
  676. u32 reg = readl(i2s->addr + I2SFIC);
  677. snd_pcm_sframes_t delay;
  678. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  679. delay = FIC_RXCOUNT(reg);
  680. else if (is_secondary(i2s))
  681. delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
  682. else
  683. delay = FIC_TXCOUNT(reg);
  684. return delay;
  685. }
  686. #ifdef CONFIG_PM
  687. static int i2s_suspend(struct snd_soc_dai *dai)
  688. {
  689. struct i2s_dai *i2s = to_info(dai);
  690. if (dai->active) {
  691. i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
  692. i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
  693. i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
  694. }
  695. return 0;
  696. }
  697. static int i2s_resume(struct snd_soc_dai *dai)
  698. {
  699. struct i2s_dai *i2s = to_info(dai);
  700. if (dai->active) {
  701. writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
  702. writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
  703. writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
  704. }
  705. return 0;
  706. }
  707. #else
  708. #define i2s_suspend NULL
  709. #define i2s_resume NULL
  710. #endif
  711. static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
  712. {
  713. struct i2s_dai *i2s = to_info(dai);
  714. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  715. if (other && other->clk) /* If this is probe on secondary */
  716. goto probe_exit;
  717. i2s->addr = ioremap(i2s->base, 0x100);
  718. if (i2s->addr == NULL) {
  719. dev_err(&i2s->pdev->dev, "cannot ioremap registers\n");
  720. return -ENXIO;
  721. }
  722. i2s->clk = clk_get(&i2s->pdev->dev, "iis");
  723. if (IS_ERR(i2s->clk)) {
  724. dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n");
  725. iounmap(i2s->addr);
  726. return -ENOENT;
  727. }
  728. clk_enable(i2s->clk);
  729. if (other) {
  730. other->addr = i2s->addr;
  731. other->clk = i2s->clk;
  732. }
  733. if (i2s->quirks & QUIRK_NEED_RSTCLR)
  734. writel(CON_RSTCLR, i2s->addr + I2SCON);
  735. if (i2s->quirks & QUIRK_SEC_DAI)
  736. idma_reg_addr_init(i2s->addr,
  737. i2s->sec_dai->idma_playback.dma_addr);
  738. probe_exit:
  739. /* Reset any constraint on RFS and BFS */
  740. i2s->rfs = 0;
  741. i2s->bfs = 0;
  742. i2s_txctrl(i2s, 0);
  743. i2s_rxctrl(i2s, 0);
  744. i2s_fifo(i2s, FIC_TXFLUSH);
  745. i2s_fifo(other, FIC_TXFLUSH);
  746. i2s_fifo(i2s, FIC_RXFLUSH);
  747. /* Gate CDCLK by default */
  748. if (!is_opened(other))
  749. i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
  750. 0, SND_SOC_CLOCK_IN);
  751. return 0;
  752. }
  753. static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
  754. {
  755. struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
  756. struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
  757. if (!other || !other->clk) {
  758. if (i2s->quirks & QUIRK_NEED_RSTCLR)
  759. writel(0, i2s->addr + I2SCON);
  760. clk_disable(i2s->clk);
  761. clk_put(i2s->clk);
  762. iounmap(i2s->addr);
  763. }
  764. i2s->clk = NULL;
  765. return 0;
  766. }
  767. static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
  768. .trigger = i2s_trigger,
  769. .hw_params = i2s_hw_params,
  770. .set_fmt = i2s_set_fmt,
  771. .set_clkdiv = i2s_set_clkdiv,
  772. .set_sysclk = i2s_set_sysclk,
  773. .startup = i2s_startup,
  774. .shutdown = i2s_shutdown,
  775. .delay = i2s_delay,
  776. };
  777. #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
  778. #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
  779. SNDRV_PCM_FMTBIT_S16_LE | \
  780. SNDRV_PCM_FMTBIT_S24_LE)
  781. static __devinit
  782. struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
  783. {
  784. struct i2s_dai *i2s;
  785. i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
  786. if (i2s == NULL)
  787. return NULL;
  788. i2s->pdev = pdev;
  789. i2s->pri_dai = NULL;
  790. i2s->sec_dai = NULL;
  791. i2s->i2s_dai_drv.symmetric_rates = 1;
  792. i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
  793. i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
  794. i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
  795. i2s->i2s_dai_drv.suspend = i2s_suspend;
  796. i2s->i2s_dai_drv.resume = i2s_resume;
  797. i2s->i2s_dai_drv.playback.channels_min = 2;
  798. i2s->i2s_dai_drv.playback.channels_max = 2;
  799. i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
  800. i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
  801. if (!sec) {
  802. i2s->i2s_dai_drv.capture.channels_min = 2;
  803. i2s->i2s_dai_drv.capture.channels_max = 2;
  804. i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
  805. i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
  806. } else { /* Create a new platform_device for Secondary */
  807. i2s->pdev = platform_device_register_resndata(NULL,
  808. pdev->name, pdev->id + SAMSUNG_I2S_SECOFF,
  809. NULL, 0, NULL, 0);
  810. if (IS_ERR(i2s->pdev))
  811. return NULL;
  812. }
  813. /* Pre-assign snd_soc_dai_set_drvdata */
  814. dev_set_drvdata(&i2s->pdev->dev, i2s);
  815. return i2s;
  816. }
  817. static __devinit int samsung_i2s_probe(struct platform_device *pdev)
  818. {
  819. u32 dma_pl_chan, dma_cp_chan, dma_pl_sec_chan;
  820. struct i2s_dai *pri_dai, *sec_dai = NULL;
  821. struct s3c_audio_pdata *i2s_pdata;
  822. struct samsung_i2s *i2s_cfg;
  823. struct resource *res;
  824. u32 regs_base, quirks;
  825. int ret = 0;
  826. /* Call during Seconday interface registration */
  827. if (pdev->id >= SAMSUNG_I2S_SECOFF) {
  828. sec_dai = dev_get_drvdata(&pdev->dev);
  829. snd_soc_register_dai(&sec_dai->pdev->dev,
  830. &sec_dai->i2s_dai_drv);
  831. return 0;
  832. }
  833. i2s_pdata = pdev->dev.platform_data;
  834. if (i2s_pdata == NULL) {
  835. dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
  836. return -EINVAL;
  837. }
  838. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  839. if (!res) {
  840. dev_err(&pdev->dev, "Unable to get I2S-TX dma resource\n");
  841. return -ENXIO;
  842. }
  843. dma_pl_chan = res->start;
  844. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  845. if (!res) {
  846. dev_err(&pdev->dev, "Unable to get I2S-RX dma resource\n");
  847. return -ENXIO;
  848. }
  849. dma_cp_chan = res->start;
  850. res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
  851. if (res)
  852. dma_pl_sec_chan = res->start;
  853. else
  854. dma_pl_sec_chan = 0;
  855. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  856. if (!res) {
  857. dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
  858. return -ENXIO;
  859. }
  860. if (!request_mem_region(res->start, resource_size(res),
  861. "samsung-i2s")) {
  862. dev_err(&pdev->dev, "Unable to request SFR region\n");
  863. return -EBUSY;
  864. }
  865. regs_base = res->start;
  866. i2s_cfg = &i2s_pdata->type.i2s;
  867. quirks = i2s_cfg->quirks;
  868. pri_dai = i2s_alloc_dai(pdev, false);
  869. if (!pri_dai) {
  870. dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
  871. ret = -ENOMEM;
  872. goto err;
  873. }
  874. pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
  875. pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
  876. pri_dai->dma_playback.client =
  877. (struct s3c2410_dma_client *)&pri_dai->dma_playback;
  878. pri_dai->dma_capture.client =
  879. (struct s3c2410_dma_client *)&pri_dai->dma_capture;
  880. pri_dai->dma_playback.channel = dma_pl_chan;
  881. pri_dai->dma_capture.channel = dma_cp_chan;
  882. pri_dai->src_clk = i2s_cfg->src_clk;
  883. pri_dai->dma_playback.dma_size = 4;
  884. pri_dai->dma_capture.dma_size = 4;
  885. pri_dai->base = regs_base;
  886. pri_dai->quirks = quirks;
  887. if (quirks & QUIRK_PRI_6CHAN)
  888. pri_dai->i2s_dai_drv.playback.channels_max = 6;
  889. if (quirks & QUIRK_SEC_DAI) {
  890. sec_dai = i2s_alloc_dai(pdev, true);
  891. if (!sec_dai) {
  892. dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
  893. ret = -ENOMEM;
  894. goto err;
  895. }
  896. sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
  897. sec_dai->dma_playback.client =
  898. (struct s3c2410_dma_client *)&sec_dai->dma_playback;
  899. /* Use iDMA always if SysDMA not provided */
  900. sec_dai->dma_playback.channel = dma_pl_sec_chan ? : -1;
  901. sec_dai->src_clk = i2s_cfg->src_clk;
  902. sec_dai->dma_playback.dma_size = 4;
  903. sec_dai->base = regs_base;
  904. sec_dai->quirks = quirks;
  905. sec_dai->idma_playback.dma_addr = i2s_cfg->idma_addr;
  906. sec_dai->pri_dai = pri_dai;
  907. pri_dai->sec_dai = sec_dai;
  908. }
  909. if (i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
  910. dev_err(&pdev->dev, "Unable to configure gpio\n");
  911. ret = -EINVAL;
  912. goto err;
  913. }
  914. snd_soc_register_dai(&pri_dai->pdev->dev, &pri_dai->i2s_dai_drv);
  915. pm_runtime_enable(&pdev->dev);
  916. return 0;
  917. err:
  918. release_mem_region(regs_base, resource_size(res));
  919. return ret;
  920. }
  921. static __devexit int samsung_i2s_remove(struct platform_device *pdev)
  922. {
  923. struct i2s_dai *i2s, *other;
  924. struct resource *res;
  925. i2s = dev_get_drvdata(&pdev->dev);
  926. other = i2s->pri_dai ? : i2s->sec_dai;
  927. if (other) {
  928. other->pri_dai = NULL;
  929. other->sec_dai = NULL;
  930. } else {
  931. pm_runtime_disable(&pdev->dev);
  932. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  933. if (res)
  934. release_mem_region(res->start, resource_size(res));
  935. }
  936. i2s->pri_dai = NULL;
  937. i2s->sec_dai = NULL;
  938. snd_soc_unregister_dai(&pdev->dev);
  939. return 0;
  940. }
  941. static struct platform_driver samsung_i2s_driver = {
  942. .probe = samsung_i2s_probe,
  943. .remove = __devexit_p(samsung_i2s_remove),
  944. .driver = {
  945. .name = "samsung-i2s",
  946. .owner = THIS_MODULE,
  947. },
  948. };
  949. module_platform_driver(samsung_i2s_driver);
  950. /* Module information */
  951. MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
  952. MODULE_DESCRIPTION("Samsung I2S Interface");
  953. MODULE_ALIAS("platform:samsung-i2s");
  954. MODULE_LICENSE("GPL");