i2s.c 31 KB

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