i2s.c 36 KB

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