i2s.c 36 KB

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