i2s.c 37 KB

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