sun4i-i2s.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Andrea Venturi
  4. * Andrea Venturi <[email protected]>
  5. *
  6. * Copyright (C) 2016 Maxime Ripard
  7. * Maxime Ripard <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/dmaengine.h>
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regmap.h>
  16. #include <linux/reset.h>
  17. #include <sound/dmaengine_pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/soc.h>
  20. #include <sound/soc-dai.h>
  21. #define SUN4I_I2S_CTRL_REG 0x00
  22. #define SUN4I_I2S_CTRL_SDO_EN_MASK GENMASK(11, 8)
  23. #define SUN4I_I2S_CTRL_SDO_EN(sdo) BIT(8 + (sdo))
  24. #define SUN4I_I2S_CTRL_MODE_MASK BIT(5)
  25. #define SUN4I_I2S_CTRL_MODE_SLAVE (1 << 5)
  26. #define SUN4I_I2S_CTRL_MODE_MASTER (0 << 5)
  27. #define SUN4I_I2S_CTRL_TX_EN BIT(2)
  28. #define SUN4I_I2S_CTRL_RX_EN BIT(1)
  29. #define SUN4I_I2S_CTRL_GL_EN BIT(0)
  30. #define SUN4I_I2S_FMT0_REG 0x04
  31. #define SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK BIT(7)
  32. #define SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED (1 << 7)
  33. #define SUN4I_I2S_FMT0_LRCLK_POLARITY_NORMAL (0 << 7)
  34. #define SUN4I_I2S_FMT0_BCLK_POLARITY_MASK BIT(6)
  35. #define SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED (1 << 6)
  36. #define SUN4I_I2S_FMT0_BCLK_POLARITY_NORMAL (0 << 6)
  37. #define SUN4I_I2S_FMT0_SR_MASK GENMASK(5, 4)
  38. #define SUN4I_I2S_FMT0_SR(sr) ((sr) << 4)
  39. #define SUN4I_I2S_FMT0_WSS_MASK GENMASK(3, 2)
  40. #define SUN4I_I2S_FMT0_WSS(wss) ((wss) << 2)
  41. #define SUN4I_I2S_FMT0_FMT_MASK GENMASK(1, 0)
  42. #define SUN4I_I2S_FMT0_FMT_RIGHT_J (2 << 0)
  43. #define SUN4I_I2S_FMT0_FMT_LEFT_J (1 << 0)
  44. #define SUN4I_I2S_FMT0_FMT_I2S (0 << 0)
  45. #define SUN4I_I2S_FMT1_REG 0x08
  46. #define SUN4I_I2S_FMT1_REG_SEXT_MASK BIT(8)
  47. #define SUN4I_I2S_FMT1_REG_SEXT(sext) ((sext) << 8)
  48. #define SUN4I_I2S_FIFO_TX_REG 0x0c
  49. #define SUN4I_I2S_FIFO_RX_REG 0x10
  50. #define SUN4I_I2S_FIFO_CTRL_REG 0x14
  51. #define SUN4I_I2S_FIFO_CTRL_FLUSH_TX BIT(25)
  52. #define SUN4I_I2S_FIFO_CTRL_FLUSH_RX BIT(24)
  53. #define SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK BIT(2)
  54. #define SUN4I_I2S_FIFO_CTRL_TX_MODE(mode) ((mode) << 2)
  55. #define SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK GENMASK(1, 0)
  56. #define SUN4I_I2S_FIFO_CTRL_RX_MODE(mode) (mode)
  57. #define SUN4I_I2S_FIFO_STA_REG 0x18
  58. #define SUN4I_I2S_DMA_INT_CTRL_REG 0x1c
  59. #define SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN BIT(7)
  60. #define SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN BIT(3)
  61. #define SUN4I_I2S_INT_STA_REG 0x20
  62. #define SUN4I_I2S_CLK_DIV_REG 0x24
  63. #define SUN4I_I2S_CLK_DIV_MCLK_EN BIT(7)
  64. #define SUN4I_I2S_CLK_DIV_BCLK_MASK GENMASK(6, 4)
  65. #define SUN4I_I2S_CLK_DIV_BCLK(bclk) ((bclk) << 4)
  66. #define SUN4I_I2S_CLK_DIV_MCLK_MASK GENMASK(3, 0)
  67. #define SUN4I_I2S_CLK_DIV_MCLK(mclk) ((mclk) << 0)
  68. #define SUN4I_I2S_TX_CNT_REG 0x28
  69. #define SUN4I_I2S_RX_CNT_REG 0x2c
  70. #define SUN4I_I2S_TX_CHAN_SEL_REG 0x30
  71. #define SUN4I_I2S_CHAN_SEL_MASK GENMASK(2, 0)
  72. #define SUN4I_I2S_CHAN_SEL(num_chan) (((num_chan) - 1) << 0)
  73. #define SUN4I_I2S_TX_CHAN_MAP_REG 0x34
  74. #define SUN4I_I2S_TX_CHAN_MAP(chan, sample) ((sample) << (chan << 2))
  75. #define SUN4I_I2S_RX_CHAN_SEL_REG 0x38
  76. #define SUN4I_I2S_RX_CHAN_MAP_REG 0x3c
  77. /* Defines required for sun8i-h3 support */
  78. #define SUN8I_I2S_CTRL_BCLK_OUT BIT(18)
  79. #define SUN8I_I2S_CTRL_LRCK_OUT BIT(17)
  80. #define SUN8I_I2S_CTRL_MODE_MASK GENMASK(5, 4)
  81. #define SUN8I_I2S_CTRL_MODE_RIGHT (2 << 4)
  82. #define SUN8I_I2S_CTRL_MODE_LEFT (1 << 4)
  83. #define SUN8I_I2S_CTRL_MODE_PCM (0 << 4)
  84. #define SUN8I_I2S_FMT0_LRCLK_POLARITY_MASK BIT(19)
  85. #define SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED (1 << 19)
  86. #define SUN8I_I2S_FMT0_LRCLK_POLARITY_NORMAL (0 << 19)
  87. #define SUN8I_I2S_FMT0_LRCK_PERIOD_MASK GENMASK(17, 8)
  88. #define SUN8I_I2S_FMT0_LRCK_PERIOD(period) ((period - 1) << 8)
  89. #define SUN8I_I2S_FMT0_BCLK_POLARITY_MASK BIT(7)
  90. #define SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED (1 << 7)
  91. #define SUN8I_I2S_FMT0_BCLK_POLARITY_NORMAL (0 << 7)
  92. #define SUN8I_I2S_FMT1_REG_SEXT_MASK GENMASK(5, 4)
  93. #define SUN8I_I2S_FMT1_REG_SEXT(sext) ((sext) << 4)
  94. #define SUN8I_I2S_INT_STA_REG 0x0c
  95. #define SUN8I_I2S_FIFO_TX_REG 0x20
  96. #define SUN8I_I2S_CHAN_CFG_REG 0x30
  97. #define SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK GENMASK(7, 4)
  98. #define SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(chan) ((chan - 1) << 4)
  99. #define SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK GENMASK(3, 0)
  100. #define SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(chan) (chan - 1)
  101. #define SUN8I_I2S_TX_CHAN_MAP_REG 0x44
  102. #define SUN8I_I2S_TX_CHAN_SEL_REG 0x34
  103. #define SUN8I_I2S_TX_CHAN_OFFSET_MASK GENMASK(13, 12)
  104. #define SUN8I_I2S_TX_CHAN_OFFSET(offset) (offset << 12)
  105. #define SUN8I_I2S_TX_CHAN_EN_MASK GENMASK(11, 4)
  106. #define SUN8I_I2S_TX_CHAN_EN(num_chan) (((1 << num_chan) - 1) << 4)
  107. #define SUN8I_I2S_RX_CHAN_SEL_REG 0x54
  108. #define SUN8I_I2S_RX_CHAN_MAP_REG 0x58
  109. /* Defines required for sun50i-h6 support */
  110. #define SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET_MASK GENMASK(21, 20)
  111. #define SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET(offset) ((offset) << 20)
  112. #define SUN50I_H6_I2S_TX_CHAN_SEL_MASK GENMASK(19, 16)
  113. #define SUN50I_H6_I2S_TX_CHAN_SEL(chan) ((chan - 1) << 16)
  114. #define SUN50I_H6_I2S_TX_CHAN_EN_MASK GENMASK(15, 0)
  115. #define SUN50I_H6_I2S_TX_CHAN_EN(num_chan) (((1 << num_chan) - 1))
  116. #define SUN50I_H6_I2S_TX_CHAN_SEL_REG(pin) (0x34 + 4 * (pin))
  117. #define SUN50I_H6_I2S_TX_CHAN_MAP0_REG(pin) (0x44 + 8 * (pin))
  118. #define SUN50I_H6_I2S_TX_CHAN_MAP1_REG(pin) (0x48 + 8 * (pin))
  119. #define SUN50I_H6_I2S_RX_CHAN_SEL_REG 0x64
  120. #define SUN50I_H6_I2S_RX_CHAN_MAP0_REG 0x68
  121. #define SUN50I_H6_I2S_RX_CHAN_MAP1_REG 0x6C
  122. #define SUN50I_R329_I2S_RX_CHAN_MAP0_REG 0x68
  123. #define SUN50I_R329_I2S_RX_CHAN_MAP1_REG 0x6c
  124. #define SUN50I_R329_I2S_RX_CHAN_MAP2_REG 0x70
  125. #define SUN50I_R329_I2S_RX_CHAN_MAP3_REG 0x74
  126. struct sun4i_i2s;
  127. /**
  128. * struct sun4i_i2s_quirks - Differences between SoC variants.
  129. * @has_reset: SoC needs reset deasserted.
  130. * @reg_offset_txdata: offset of the tx fifo.
  131. * @sun4i_i2s_regmap: regmap config to use.
  132. * @field_clkdiv_mclk_en: regmap field to enable mclk output.
  133. * @field_fmt_wss: regmap field to set word select size.
  134. * @field_fmt_sr: regmap field to set sample resolution.
  135. * @num_din_pins: input pins
  136. * @num_dout_pins: output pins (currently set but unused)
  137. * @bclk_dividers: bit clock dividers array
  138. * @num_bclk_dividers: number of bit clock dividers
  139. * @mclk_dividers: mclk dividers array
  140. * @num_mclk_dividers: number of mclk dividers
  141. * @get_bclk_parent_rate: callback to get bclk parent rate
  142. * @get_sr: callback to get sample resolution
  143. * @get_wss: callback to get word select size
  144. * @set_chan_cfg: callback to set channel configuration
  145. * @set_fmt: callback to set format
  146. */
  147. struct sun4i_i2s_quirks {
  148. bool has_reset;
  149. unsigned int reg_offset_txdata; /* TX FIFO */
  150. const struct regmap_config *sun4i_i2s_regmap;
  151. /* Register fields for i2s */
  152. struct reg_field field_clkdiv_mclk_en;
  153. struct reg_field field_fmt_wss;
  154. struct reg_field field_fmt_sr;
  155. unsigned int num_din_pins;
  156. unsigned int num_dout_pins;
  157. const struct sun4i_i2s_clk_div *bclk_dividers;
  158. unsigned int num_bclk_dividers;
  159. const struct sun4i_i2s_clk_div *mclk_dividers;
  160. unsigned int num_mclk_dividers;
  161. unsigned long (*get_bclk_parent_rate)(const struct sun4i_i2s *i2s);
  162. int (*get_sr)(unsigned int width);
  163. int (*get_wss)(unsigned int width);
  164. /*
  165. * In the set_chan_cfg() function pointer:
  166. * @slots: channels per frame + padding slots, regardless of format
  167. * @slot_width: bits per sample + padding bits, regardless of format
  168. */
  169. int (*set_chan_cfg)(const struct sun4i_i2s *i2s,
  170. unsigned int channels, unsigned int slots,
  171. unsigned int slot_width);
  172. int (*set_fmt)(const struct sun4i_i2s *i2s, unsigned int fmt);
  173. };
  174. struct sun4i_i2s {
  175. struct clk *bus_clk;
  176. struct clk *mod_clk;
  177. struct regmap *regmap;
  178. struct reset_control *rst;
  179. unsigned int format;
  180. unsigned int mclk_freq;
  181. unsigned int slots;
  182. unsigned int slot_width;
  183. struct snd_dmaengine_dai_dma_data capture_dma_data;
  184. struct snd_dmaengine_dai_dma_data playback_dma_data;
  185. /* Register fields for i2s */
  186. struct regmap_field *field_clkdiv_mclk_en;
  187. struct regmap_field *field_fmt_wss;
  188. struct regmap_field *field_fmt_sr;
  189. const struct sun4i_i2s_quirks *variant;
  190. };
  191. struct sun4i_i2s_clk_div {
  192. u8 div;
  193. u8 val;
  194. };
  195. static const struct sun4i_i2s_clk_div sun4i_i2s_bclk_div[] = {
  196. { .div = 2, .val = 0 },
  197. { .div = 4, .val = 1 },
  198. { .div = 6, .val = 2 },
  199. { .div = 8, .val = 3 },
  200. { .div = 12, .val = 4 },
  201. { .div = 16, .val = 5 },
  202. /* TODO - extend divide ratio supported by newer SoCs */
  203. };
  204. static const struct sun4i_i2s_clk_div sun4i_i2s_mclk_div[] = {
  205. { .div = 1, .val = 0 },
  206. { .div = 2, .val = 1 },
  207. { .div = 4, .val = 2 },
  208. { .div = 6, .val = 3 },
  209. { .div = 8, .val = 4 },
  210. { .div = 12, .val = 5 },
  211. { .div = 16, .val = 6 },
  212. { .div = 24, .val = 7 },
  213. /* TODO - extend divide ratio supported by newer SoCs */
  214. };
  215. static const struct sun4i_i2s_clk_div sun8i_i2s_clk_div[] = {
  216. { .div = 1, .val = 1 },
  217. { .div = 2, .val = 2 },
  218. { .div = 4, .val = 3 },
  219. { .div = 6, .val = 4 },
  220. { .div = 8, .val = 5 },
  221. { .div = 12, .val = 6 },
  222. { .div = 16, .val = 7 },
  223. { .div = 24, .val = 8 },
  224. { .div = 32, .val = 9 },
  225. { .div = 48, .val = 10 },
  226. { .div = 64, .val = 11 },
  227. { .div = 96, .val = 12 },
  228. { .div = 128, .val = 13 },
  229. { .div = 176, .val = 14 },
  230. { .div = 192, .val = 15 },
  231. };
  232. static unsigned long sun4i_i2s_get_bclk_parent_rate(const struct sun4i_i2s *i2s)
  233. {
  234. return i2s->mclk_freq;
  235. }
  236. static unsigned long sun8i_i2s_get_bclk_parent_rate(const struct sun4i_i2s *i2s)
  237. {
  238. return clk_get_rate(i2s->mod_clk);
  239. }
  240. static int sun4i_i2s_get_bclk_div(struct sun4i_i2s *i2s,
  241. unsigned long parent_rate,
  242. unsigned int sampling_rate,
  243. unsigned int channels,
  244. unsigned int word_size)
  245. {
  246. const struct sun4i_i2s_clk_div *dividers = i2s->variant->bclk_dividers;
  247. int div = parent_rate / sampling_rate / word_size / channels;
  248. int i;
  249. for (i = 0; i < i2s->variant->num_bclk_dividers; i++) {
  250. const struct sun4i_i2s_clk_div *bdiv = &dividers[i];
  251. if (bdiv->div == div)
  252. return bdiv->val;
  253. }
  254. return -EINVAL;
  255. }
  256. static int sun4i_i2s_get_mclk_div(struct sun4i_i2s *i2s,
  257. unsigned long parent_rate,
  258. unsigned long mclk_rate)
  259. {
  260. const struct sun4i_i2s_clk_div *dividers = i2s->variant->mclk_dividers;
  261. int div = parent_rate / mclk_rate;
  262. int i;
  263. for (i = 0; i < i2s->variant->num_mclk_dividers; i++) {
  264. const struct sun4i_i2s_clk_div *mdiv = &dividers[i];
  265. if (mdiv->div == div)
  266. return mdiv->val;
  267. }
  268. return -EINVAL;
  269. }
  270. static int sun4i_i2s_oversample_rates[] = { 128, 192, 256, 384, 512, 768 };
  271. static bool sun4i_i2s_oversample_is_valid(unsigned int oversample)
  272. {
  273. int i;
  274. for (i = 0; i < ARRAY_SIZE(sun4i_i2s_oversample_rates); i++)
  275. if (sun4i_i2s_oversample_rates[i] == oversample)
  276. return true;
  277. return false;
  278. }
  279. static int sun4i_i2s_set_clk_rate(struct snd_soc_dai *dai,
  280. unsigned int rate,
  281. unsigned int slots,
  282. unsigned int slot_width)
  283. {
  284. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  285. unsigned int oversample_rate, clk_rate, bclk_parent_rate;
  286. int bclk_div, mclk_div;
  287. int ret;
  288. switch (rate) {
  289. case 176400:
  290. case 88200:
  291. case 44100:
  292. case 22050:
  293. case 11025:
  294. clk_rate = 22579200;
  295. break;
  296. case 192000:
  297. case 128000:
  298. case 96000:
  299. case 64000:
  300. case 48000:
  301. case 32000:
  302. case 24000:
  303. case 16000:
  304. case 12000:
  305. case 8000:
  306. clk_rate = 24576000;
  307. break;
  308. default:
  309. dev_err(dai->dev, "Unsupported sample rate: %u\n", rate);
  310. return -EINVAL;
  311. }
  312. ret = clk_set_rate(i2s->mod_clk, clk_rate);
  313. if (ret)
  314. return ret;
  315. oversample_rate = i2s->mclk_freq / rate;
  316. if (!sun4i_i2s_oversample_is_valid(oversample_rate)) {
  317. dev_err(dai->dev, "Unsupported oversample rate: %d\n",
  318. oversample_rate);
  319. return -EINVAL;
  320. }
  321. bclk_parent_rate = i2s->variant->get_bclk_parent_rate(i2s);
  322. bclk_div = sun4i_i2s_get_bclk_div(i2s, bclk_parent_rate,
  323. rate, slots, slot_width);
  324. if (bclk_div < 0) {
  325. dev_err(dai->dev, "Unsupported BCLK divider: %d\n", bclk_div);
  326. return -EINVAL;
  327. }
  328. mclk_div = sun4i_i2s_get_mclk_div(i2s, clk_rate, i2s->mclk_freq);
  329. if (mclk_div < 0) {
  330. dev_err(dai->dev, "Unsupported MCLK divider: %d\n", mclk_div);
  331. return -EINVAL;
  332. }
  333. regmap_write(i2s->regmap, SUN4I_I2S_CLK_DIV_REG,
  334. SUN4I_I2S_CLK_DIV_BCLK(bclk_div) |
  335. SUN4I_I2S_CLK_DIV_MCLK(mclk_div));
  336. regmap_field_write(i2s->field_clkdiv_mclk_en, 1);
  337. return 0;
  338. }
  339. static int sun4i_i2s_get_sr(unsigned int width)
  340. {
  341. switch (width) {
  342. case 16:
  343. return 0;
  344. case 20:
  345. return 1;
  346. case 24:
  347. return 2;
  348. }
  349. return -EINVAL;
  350. }
  351. static int sun4i_i2s_get_wss(unsigned int width)
  352. {
  353. switch (width) {
  354. case 16:
  355. return 0;
  356. case 20:
  357. return 1;
  358. case 24:
  359. return 2;
  360. case 32:
  361. return 3;
  362. }
  363. return -EINVAL;
  364. }
  365. static int sun8i_i2s_get_sr_wss(unsigned int width)
  366. {
  367. switch (width) {
  368. case 8:
  369. return 1;
  370. case 12:
  371. return 2;
  372. case 16:
  373. return 3;
  374. case 20:
  375. return 4;
  376. case 24:
  377. return 5;
  378. case 28:
  379. return 6;
  380. case 32:
  381. return 7;
  382. }
  383. return -EINVAL;
  384. }
  385. static int sun4i_i2s_set_chan_cfg(const struct sun4i_i2s *i2s,
  386. unsigned int channels, unsigned int slots,
  387. unsigned int slot_width)
  388. {
  389. /* Map the channels for playback and capture */
  390. regmap_write(i2s->regmap, SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210);
  391. regmap_write(i2s->regmap, SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210);
  392. /* Configure the channels */
  393. regmap_update_bits(i2s->regmap, SUN4I_I2S_TX_CHAN_SEL_REG,
  394. SUN4I_I2S_CHAN_SEL_MASK,
  395. SUN4I_I2S_CHAN_SEL(channels));
  396. regmap_update_bits(i2s->regmap, SUN4I_I2S_RX_CHAN_SEL_REG,
  397. SUN4I_I2S_CHAN_SEL_MASK,
  398. SUN4I_I2S_CHAN_SEL(channels));
  399. return 0;
  400. }
  401. static int sun8i_i2s_set_chan_cfg(const struct sun4i_i2s *i2s,
  402. unsigned int channels, unsigned int slots,
  403. unsigned int slot_width)
  404. {
  405. unsigned int lrck_period;
  406. /* Map the channels for playback and capture */
  407. regmap_write(i2s->regmap, SUN8I_I2S_TX_CHAN_MAP_REG, 0x76543210);
  408. regmap_write(i2s->regmap, SUN8I_I2S_RX_CHAN_MAP_REG, 0x76543210);
  409. /* Configure the channels */
  410. regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
  411. SUN4I_I2S_CHAN_SEL_MASK,
  412. SUN4I_I2S_CHAN_SEL(channels));
  413. regmap_update_bits(i2s->regmap, SUN8I_I2S_RX_CHAN_SEL_REG,
  414. SUN4I_I2S_CHAN_SEL_MASK,
  415. SUN4I_I2S_CHAN_SEL(channels));
  416. regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
  417. SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK,
  418. SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(channels));
  419. regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
  420. SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK,
  421. SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(channels));
  422. switch (i2s->format & SND_SOC_DAIFMT_FORMAT_MASK) {
  423. case SND_SOC_DAIFMT_DSP_A:
  424. case SND_SOC_DAIFMT_DSP_B:
  425. lrck_period = slot_width * slots;
  426. break;
  427. case SND_SOC_DAIFMT_LEFT_J:
  428. case SND_SOC_DAIFMT_RIGHT_J:
  429. case SND_SOC_DAIFMT_I2S:
  430. lrck_period = slot_width;
  431. break;
  432. default:
  433. return -EINVAL;
  434. }
  435. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  436. SUN8I_I2S_FMT0_LRCK_PERIOD_MASK,
  437. SUN8I_I2S_FMT0_LRCK_PERIOD(lrck_period));
  438. regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
  439. SUN8I_I2S_TX_CHAN_EN_MASK,
  440. SUN8I_I2S_TX_CHAN_EN(channels));
  441. return 0;
  442. }
  443. static int sun50i_h6_i2s_set_chan_cfg(const struct sun4i_i2s *i2s,
  444. unsigned int channels, unsigned int slots,
  445. unsigned int slot_width)
  446. {
  447. unsigned int lrck_period;
  448. /* Map the channels for playback and capture */
  449. regmap_write(i2s->regmap, SUN50I_H6_I2S_TX_CHAN_MAP0_REG(0), 0xFEDCBA98);
  450. regmap_write(i2s->regmap, SUN50I_H6_I2S_TX_CHAN_MAP1_REG(0), 0x76543210);
  451. if (i2s->variant->num_din_pins > 1) {
  452. regmap_write(i2s->regmap, SUN50I_R329_I2S_RX_CHAN_MAP0_REG, 0x0F0E0D0C);
  453. regmap_write(i2s->regmap, SUN50I_R329_I2S_RX_CHAN_MAP1_REG, 0x0B0A0908);
  454. regmap_write(i2s->regmap, SUN50I_R329_I2S_RX_CHAN_MAP2_REG, 0x07060504);
  455. regmap_write(i2s->regmap, SUN50I_R329_I2S_RX_CHAN_MAP3_REG, 0x03020100);
  456. } else {
  457. regmap_write(i2s->regmap, SUN50I_H6_I2S_RX_CHAN_MAP0_REG, 0xFEDCBA98);
  458. regmap_write(i2s->regmap, SUN50I_H6_I2S_RX_CHAN_MAP1_REG, 0x76543210);
  459. }
  460. /* Configure the channels */
  461. regmap_update_bits(i2s->regmap, SUN50I_H6_I2S_TX_CHAN_SEL_REG(0),
  462. SUN50I_H6_I2S_TX_CHAN_SEL_MASK,
  463. SUN50I_H6_I2S_TX_CHAN_SEL(channels));
  464. regmap_update_bits(i2s->regmap, SUN50I_H6_I2S_RX_CHAN_SEL_REG,
  465. SUN50I_H6_I2S_TX_CHAN_SEL_MASK,
  466. SUN50I_H6_I2S_TX_CHAN_SEL(channels));
  467. regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
  468. SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK,
  469. SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(channels));
  470. regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
  471. SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK,
  472. SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(channels));
  473. switch (i2s->format & SND_SOC_DAIFMT_FORMAT_MASK) {
  474. case SND_SOC_DAIFMT_DSP_A:
  475. case SND_SOC_DAIFMT_DSP_B:
  476. lrck_period = slot_width * slots;
  477. break;
  478. case SND_SOC_DAIFMT_LEFT_J:
  479. case SND_SOC_DAIFMT_RIGHT_J:
  480. case SND_SOC_DAIFMT_I2S:
  481. lrck_period = slot_width;
  482. break;
  483. default:
  484. return -EINVAL;
  485. }
  486. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  487. SUN8I_I2S_FMT0_LRCK_PERIOD_MASK,
  488. SUN8I_I2S_FMT0_LRCK_PERIOD(lrck_period));
  489. regmap_update_bits(i2s->regmap, SUN50I_H6_I2S_TX_CHAN_SEL_REG(0),
  490. SUN50I_H6_I2S_TX_CHAN_EN_MASK,
  491. SUN50I_H6_I2S_TX_CHAN_EN(channels));
  492. return 0;
  493. }
  494. static int sun4i_i2s_hw_params(struct snd_pcm_substream *substream,
  495. struct snd_pcm_hw_params *params,
  496. struct snd_soc_dai *dai)
  497. {
  498. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  499. unsigned int word_size = params_width(params);
  500. unsigned int slot_width = params_physical_width(params);
  501. unsigned int channels = params_channels(params);
  502. unsigned int slots = channels;
  503. int ret, sr, wss;
  504. u32 width;
  505. if (i2s->slots)
  506. slots = i2s->slots;
  507. if (i2s->slot_width)
  508. slot_width = i2s->slot_width;
  509. ret = i2s->variant->set_chan_cfg(i2s, channels, slots, slot_width);
  510. if (ret < 0) {
  511. dev_err(dai->dev, "Invalid channel configuration\n");
  512. return ret;
  513. }
  514. /* Set significant bits in our FIFOs */
  515. regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
  516. SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK |
  517. SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK,
  518. SUN4I_I2S_FIFO_CTRL_TX_MODE(1) |
  519. SUN4I_I2S_FIFO_CTRL_RX_MODE(1));
  520. switch (params_physical_width(params)) {
  521. case 16:
  522. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  523. break;
  524. case 32:
  525. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  526. break;
  527. default:
  528. dev_err(dai->dev, "Unsupported physical sample width: %d\n",
  529. params_physical_width(params));
  530. return -EINVAL;
  531. }
  532. i2s->playback_dma_data.addr_width = width;
  533. sr = i2s->variant->get_sr(word_size);
  534. if (sr < 0)
  535. return -EINVAL;
  536. wss = i2s->variant->get_wss(slot_width);
  537. if (wss < 0)
  538. return -EINVAL;
  539. regmap_field_write(i2s->field_fmt_wss, wss);
  540. regmap_field_write(i2s->field_fmt_sr, sr);
  541. return sun4i_i2s_set_clk_rate(dai, params_rate(params),
  542. slots, slot_width);
  543. }
  544. static int sun4i_i2s_set_soc_fmt(const struct sun4i_i2s *i2s,
  545. unsigned int fmt)
  546. {
  547. u32 val;
  548. /* DAI clock polarity */
  549. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  550. case SND_SOC_DAIFMT_IB_IF:
  551. /* Invert both clocks */
  552. val = SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED |
  553. SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
  554. break;
  555. case SND_SOC_DAIFMT_IB_NF:
  556. /* Invert bit clock */
  557. val = SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED;
  558. break;
  559. case SND_SOC_DAIFMT_NB_IF:
  560. /* Invert frame clock */
  561. val = SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
  562. break;
  563. case SND_SOC_DAIFMT_NB_NF:
  564. val = 0;
  565. break;
  566. default:
  567. return -EINVAL;
  568. }
  569. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  570. SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK |
  571. SUN4I_I2S_FMT0_BCLK_POLARITY_MASK,
  572. val);
  573. /* DAI Mode */
  574. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  575. case SND_SOC_DAIFMT_I2S:
  576. val = SUN4I_I2S_FMT0_FMT_I2S;
  577. break;
  578. case SND_SOC_DAIFMT_LEFT_J:
  579. val = SUN4I_I2S_FMT0_FMT_LEFT_J;
  580. break;
  581. case SND_SOC_DAIFMT_RIGHT_J:
  582. val = SUN4I_I2S_FMT0_FMT_RIGHT_J;
  583. break;
  584. default:
  585. return -EINVAL;
  586. }
  587. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  588. SUN4I_I2S_FMT0_FMT_MASK, val);
  589. /* DAI clock master masks */
  590. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  591. case SND_SOC_DAIFMT_BP_FP:
  592. /* BCLK and LRCLK master */
  593. val = SUN4I_I2S_CTRL_MODE_MASTER;
  594. break;
  595. case SND_SOC_DAIFMT_BC_FC:
  596. /* BCLK and LRCLK slave */
  597. val = SUN4I_I2S_CTRL_MODE_SLAVE;
  598. break;
  599. default:
  600. return -EINVAL;
  601. }
  602. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  603. SUN4I_I2S_CTRL_MODE_MASK, val);
  604. return 0;
  605. }
  606. static int sun8i_i2s_set_soc_fmt(const struct sun4i_i2s *i2s,
  607. unsigned int fmt)
  608. {
  609. u32 mode, val;
  610. u8 offset;
  611. /*
  612. * DAI clock polarity
  613. *
  614. * The setup for LRCK contradicts the datasheet, but under a
  615. * scope it's clear that the LRCK polarity is reversed
  616. * compared to the expected polarity on the bus.
  617. */
  618. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  619. case SND_SOC_DAIFMT_IB_IF:
  620. /* Invert both clocks */
  621. val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED;
  622. break;
  623. case SND_SOC_DAIFMT_IB_NF:
  624. /* Invert bit clock */
  625. val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED |
  626. SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
  627. break;
  628. case SND_SOC_DAIFMT_NB_IF:
  629. /* Invert frame clock */
  630. val = 0;
  631. break;
  632. case SND_SOC_DAIFMT_NB_NF:
  633. val = SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
  634. break;
  635. default:
  636. return -EINVAL;
  637. }
  638. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  639. SUN8I_I2S_FMT0_LRCLK_POLARITY_MASK |
  640. SUN8I_I2S_FMT0_BCLK_POLARITY_MASK,
  641. val);
  642. /* DAI Mode */
  643. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  644. case SND_SOC_DAIFMT_DSP_A:
  645. mode = SUN8I_I2S_CTRL_MODE_PCM;
  646. offset = 1;
  647. break;
  648. case SND_SOC_DAIFMT_DSP_B:
  649. mode = SUN8I_I2S_CTRL_MODE_PCM;
  650. offset = 0;
  651. break;
  652. case SND_SOC_DAIFMT_I2S:
  653. mode = SUN8I_I2S_CTRL_MODE_LEFT;
  654. offset = 1;
  655. break;
  656. case SND_SOC_DAIFMT_LEFT_J:
  657. mode = SUN8I_I2S_CTRL_MODE_LEFT;
  658. offset = 0;
  659. break;
  660. case SND_SOC_DAIFMT_RIGHT_J:
  661. mode = SUN8I_I2S_CTRL_MODE_RIGHT;
  662. offset = 0;
  663. break;
  664. default:
  665. return -EINVAL;
  666. }
  667. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  668. SUN8I_I2S_CTRL_MODE_MASK, mode);
  669. regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
  670. SUN8I_I2S_TX_CHAN_OFFSET_MASK,
  671. SUN8I_I2S_TX_CHAN_OFFSET(offset));
  672. regmap_update_bits(i2s->regmap, SUN8I_I2S_RX_CHAN_SEL_REG,
  673. SUN8I_I2S_TX_CHAN_OFFSET_MASK,
  674. SUN8I_I2S_TX_CHAN_OFFSET(offset));
  675. /* DAI clock master masks */
  676. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  677. case SND_SOC_DAIFMT_BP_FP:
  678. /* BCLK and LRCLK master */
  679. val = SUN8I_I2S_CTRL_BCLK_OUT | SUN8I_I2S_CTRL_LRCK_OUT;
  680. break;
  681. case SND_SOC_DAIFMT_BC_FC:
  682. /* BCLK and LRCLK slave */
  683. val = 0;
  684. break;
  685. default:
  686. return -EINVAL;
  687. }
  688. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  689. SUN8I_I2S_CTRL_BCLK_OUT | SUN8I_I2S_CTRL_LRCK_OUT,
  690. val);
  691. /* Set sign extension to pad out LSB with 0 */
  692. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT1_REG,
  693. SUN8I_I2S_FMT1_REG_SEXT_MASK,
  694. SUN8I_I2S_FMT1_REG_SEXT(0));
  695. return 0;
  696. }
  697. static int sun50i_h6_i2s_set_soc_fmt(const struct sun4i_i2s *i2s,
  698. unsigned int fmt)
  699. {
  700. u32 mode, val;
  701. u8 offset;
  702. /*
  703. * DAI clock polarity
  704. *
  705. * The setup for LRCK contradicts the datasheet, but under a
  706. * scope it's clear that the LRCK polarity is reversed
  707. * compared to the expected polarity on the bus.
  708. */
  709. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  710. case SND_SOC_DAIFMT_IB_IF:
  711. /* Invert both clocks */
  712. val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED;
  713. break;
  714. case SND_SOC_DAIFMT_IB_NF:
  715. /* Invert bit clock */
  716. val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED |
  717. SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
  718. break;
  719. case SND_SOC_DAIFMT_NB_IF:
  720. /* Invert frame clock */
  721. val = 0;
  722. break;
  723. case SND_SOC_DAIFMT_NB_NF:
  724. val = SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
  725. break;
  726. default:
  727. return -EINVAL;
  728. }
  729. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  730. SUN8I_I2S_FMT0_LRCLK_POLARITY_MASK |
  731. SUN8I_I2S_FMT0_BCLK_POLARITY_MASK,
  732. val);
  733. /* DAI Mode */
  734. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  735. case SND_SOC_DAIFMT_DSP_A:
  736. mode = SUN8I_I2S_CTRL_MODE_PCM;
  737. offset = 1;
  738. break;
  739. case SND_SOC_DAIFMT_DSP_B:
  740. mode = SUN8I_I2S_CTRL_MODE_PCM;
  741. offset = 0;
  742. break;
  743. case SND_SOC_DAIFMT_I2S:
  744. mode = SUN8I_I2S_CTRL_MODE_LEFT;
  745. offset = 1;
  746. break;
  747. case SND_SOC_DAIFMT_LEFT_J:
  748. mode = SUN8I_I2S_CTRL_MODE_LEFT;
  749. offset = 0;
  750. break;
  751. case SND_SOC_DAIFMT_RIGHT_J:
  752. mode = SUN8I_I2S_CTRL_MODE_RIGHT;
  753. offset = 0;
  754. break;
  755. default:
  756. return -EINVAL;
  757. }
  758. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  759. SUN8I_I2S_CTRL_MODE_MASK, mode);
  760. regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
  761. SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET_MASK,
  762. SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET(offset));
  763. regmap_update_bits(i2s->regmap, SUN50I_H6_I2S_RX_CHAN_SEL_REG,
  764. SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET_MASK,
  765. SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET(offset));
  766. /* DAI clock master masks */
  767. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  768. case SND_SOC_DAIFMT_BP_FP:
  769. /* BCLK and LRCLK master */
  770. val = SUN8I_I2S_CTRL_BCLK_OUT | SUN8I_I2S_CTRL_LRCK_OUT;
  771. break;
  772. case SND_SOC_DAIFMT_BC_FC:
  773. /* BCLK and LRCLK slave */
  774. val = 0;
  775. break;
  776. default:
  777. return -EINVAL;
  778. }
  779. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  780. SUN8I_I2S_CTRL_BCLK_OUT | SUN8I_I2S_CTRL_LRCK_OUT,
  781. val);
  782. /* Set sign extension to pad out LSB with 0 */
  783. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT1_REG,
  784. SUN8I_I2S_FMT1_REG_SEXT_MASK,
  785. SUN8I_I2S_FMT1_REG_SEXT(0));
  786. return 0;
  787. }
  788. static int sun4i_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  789. {
  790. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  791. int ret;
  792. ret = i2s->variant->set_fmt(i2s, fmt);
  793. if (ret) {
  794. dev_err(dai->dev, "Unsupported format configuration\n");
  795. return ret;
  796. }
  797. i2s->format = fmt;
  798. return 0;
  799. }
  800. static void sun4i_i2s_start_capture(struct sun4i_i2s *i2s)
  801. {
  802. /* Flush RX FIFO */
  803. regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
  804. SUN4I_I2S_FIFO_CTRL_FLUSH_RX,
  805. SUN4I_I2S_FIFO_CTRL_FLUSH_RX);
  806. /* Clear RX counter */
  807. regmap_write(i2s->regmap, SUN4I_I2S_RX_CNT_REG, 0);
  808. /* Enable RX Block */
  809. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  810. SUN4I_I2S_CTRL_RX_EN,
  811. SUN4I_I2S_CTRL_RX_EN);
  812. /* Enable RX DRQ */
  813. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  814. SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
  815. SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN);
  816. }
  817. static void sun4i_i2s_start_playback(struct sun4i_i2s *i2s)
  818. {
  819. /* Flush TX FIFO */
  820. regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
  821. SUN4I_I2S_FIFO_CTRL_FLUSH_TX,
  822. SUN4I_I2S_FIFO_CTRL_FLUSH_TX);
  823. /* Clear TX counter */
  824. regmap_write(i2s->regmap, SUN4I_I2S_TX_CNT_REG, 0);
  825. /* Enable TX Block */
  826. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  827. SUN4I_I2S_CTRL_TX_EN,
  828. SUN4I_I2S_CTRL_TX_EN);
  829. /* Enable TX DRQ */
  830. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  831. SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
  832. SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN);
  833. }
  834. static void sun4i_i2s_stop_capture(struct sun4i_i2s *i2s)
  835. {
  836. /* Disable RX Block */
  837. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  838. SUN4I_I2S_CTRL_RX_EN,
  839. 0);
  840. /* Disable RX DRQ */
  841. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  842. SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
  843. 0);
  844. }
  845. static void sun4i_i2s_stop_playback(struct sun4i_i2s *i2s)
  846. {
  847. /* Disable TX Block */
  848. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  849. SUN4I_I2S_CTRL_TX_EN,
  850. 0);
  851. /* Disable TX DRQ */
  852. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  853. SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
  854. 0);
  855. }
  856. static int sun4i_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  857. struct snd_soc_dai *dai)
  858. {
  859. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  860. switch (cmd) {
  861. case SNDRV_PCM_TRIGGER_START:
  862. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  863. case SNDRV_PCM_TRIGGER_RESUME:
  864. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  865. sun4i_i2s_start_playback(i2s);
  866. else
  867. sun4i_i2s_start_capture(i2s);
  868. break;
  869. case SNDRV_PCM_TRIGGER_STOP:
  870. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  871. case SNDRV_PCM_TRIGGER_SUSPEND:
  872. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  873. sun4i_i2s_stop_playback(i2s);
  874. else
  875. sun4i_i2s_stop_capture(i2s);
  876. break;
  877. default:
  878. return -EINVAL;
  879. }
  880. return 0;
  881. }
  882. static int sun4i_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  883. unsigned int freq, int dir)
  884. {
  885. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  886. if (clk_id != 0)
  887. return -EINVAL;
  888. i2s->mclk_freq = freq;
  889. return 0;
  890. }
  891. static int sun4i_i2s_set_tdm_slot(struct snd_soc_dai *dai,
  892. unsigned int tx_mask, unsigned int rx_mask,
  893. int slots, int slot_width)
  894. {
  895. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  896. if (slots > 8)
  897. return -EINVAL;
  898. i2s->slots = slots;
  899. i2s->slot_width = slot_width;
  900. return 0;
  901. }
  902. static const struct snd_soc_dai_ops sun4i_i2s_dai_ops = {
  903. .hw_params = sun4i_i2s_hw_params,
  904. .set_fmt = sun4i_i2s_set_fmt,
  905. .set_sysclk = sun4i_i2s_set_sysclk,
  906. .set_tdm_slot = sun4i_i2s_set_tdm_slot,
  907. .trigger = sun4i_i2s_trigger,
  908. };
  909. static int sun4i_i2s_dai_probe(struct snd_soc_dai *dai)
  910. {
  911. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  912. snd_soc_dai_init_dma_data(dai,
  913. &i2s->playback_dma_data,
  914. &i2s->capture_dma_data);
  915. return 0;
  916. }
  917. #define SUN4I_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  918. SNDRV_PCM_FMTBIT_S20_LE | \
  919. SNDRV_PCM_FMTBIT_S24_LE)
  920. static struct snd_soc_dai_driver sun4i_i2s_dai = {
  921. .probe = sun4i_i2s_dai_probe,
  922. .capture = {
  923. .stream_name = "Capture",
  924. .channels_min = 1,
  925. .channels_max = 8,
  926. .rates = SNDRV_PCM_RATE_8000_192000,
  927. .formats = SUN4I_FORMATS,
  928. },
  929. .playback = {
  930. .stream_name = "Playback",
  931. .channels_min = 1,
  932. .channels_max = 8,
  933. .rates = SNDRV_PCM_RATE_8000_192000,
  934. .formats = SUN4I_FORMATS,
  935. },
  936. .ops = &sun4i_i2s_dai_ops,
  937. .symmetric_rate = 1,
  938. };
  939. static const struct snd_soc_component_driver sun4i_i2s_component = {
  940. .name = "sun4i-dai",
  941. .legacy_dai_naming = 1,
  942. };
  943. static bool sun4i_i2s_rd_reg(struct device *dev, unsigned int reg)
  944. {
  945. switch (reg) {
  946. case SUN4I_I2S_FIFO_TX_REG:
  947. return false;
  948. default:
  949. return true;
  950. }
  951. }
  952. static bool sun4i_i2s_wr_reg(struct device *dev, unsigned int reg)
  953. {
  954. switch (reg) {
  955. case SUN4I_I2S_FIFO_RX_REG:
  956. case SUN4I_I2S_FIFO_STA_REG:
  957. return false;
  958. default:
  959. return true;
  960. }
  961. }
  962. static bool sun4i_i2s_volatile_reg(struct device *dev, unsigned int reg)
  963. {
  964. switch (reg) {
  965. case SUN4I_I2S_FIFO_RX_REG:
  966. case SUN4I_I2S_INT_STA_REG:
  967. case SUN4I_I2S_RX_CNT_REG:
  968. case SUN4I_I2S_TX_CNT_REG:
  969. return true;
  970. default:
  971. return false;
  972. }
  973. }
  974. static bool sun8i_i2s_rd_reg(struct device *dev, unsigned int reg)
  975. {
  976. switch (reg) {
  977. case SUN8I_I2S_FIFO_TX_REG:
  978. return false;
  979. default:
  980. return true;
  981. }
  982. }
  983. static bool sun8i_i2s_volatile_reg(struct device *dev, unsigned int reg)
  984. {
  985. switch (reg) {
  986. case SUN4I_I2S_FIFO_CTRL_REG:
  987. case SUN4I_I2S_FIFO_RX_REG:
  988. case SUN4I_I2S_FIFO_STA_REG:
  989. case SUN4I_I2S_RX_CNT_REG:
  990. case SUN4I_I2S_TX_CNT_REG:
  991. case SUN8I_I2S_FIFO_TX_REG:
  992. case SUN8I_I2S_INT_STA_REG:
  993. return true;
  994. default:
  995. return false;
  996. }
  997. }
  998. static const struct reg_default sun4i_i2s_reg_defaults[] = {
  999. { SUN4I_I2S_CTRL_REG, 0x00000000 },
  1000. { SUN4I_I2S_FMT0_REG, 0x0000000c },
  1001. { SUN4I_I2S_FMT1_REG, 0x00004020 },
  1002. { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
  1003. { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
  1004. { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
  1005. { SUN4I_I2S_TX_CHAN_SEL_REG, 0x00000001 },
  1006. { SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210 },
  1007. { SUN4I_I2S_RX_CHAN_SEL_REG, 0x00000001 },
  1008. { SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210 },
  1009. };
  1010. static const struct reg_default sun8i_i2s_reg_defaults[] = {
  1011. { SUN4I_I2S_CTRL_REG, 0x00060000 },
  1012. { SUN4I_I2S_FMT0_REG, 0x00000033 },
  1013. { SUN4I_I2S_FMT1_REG, 0x00000030 },
  1014. { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
  1015. { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
  1016. { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
  1017. { SUN8I_I2S_CHAN_CFG_REG, 0x00000000 },
  1018. { SUN8I_I2S_TX_CHAN_SEL_REG, 0x00000000 },
  1019. { SUN8I_I2S_TX_CHAN_MAP_REG, 0x00000000 },
  1020. { SUN8I_I2S_RX_CHAN_SEL_REG, 0x00000000 },
  1021. { SUN8I_I2S_RX_CHAN_MAP_REG, 0x00000000 },
  1022. };
  1023. static const struct reg_default sun50i_h6_i2s_reg_defaults[] = {
  1024. { SUN4I_I2S_CTRL_REG, 0x00060000 },
  1025. { SUN4I_I2S_FMT0_REG, 0x00000033 },
  1026. { SUN4I_I2S_FMT1_REG, 0x00000030 },
  1027. { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
  1028. { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
  1029. { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
  1030. { SUN8I_I2S_CHAN_CFG_REG, 0x00000000 },
  1031. { SUN50I_H6_I2S_TX_CHAN_SEL_REG(0), 0x00000000 },
  1032. { SUN50I_H6_I2S_TX_CHAN_MAP0_REG(0), 0x00000000 },
  1033. { SUN50I_H6_I2S_TX_CHAN_MAP1_REG(0), 0x00000000 },
  1034. { SUN50I_H6_I2S_RX_CHAN_SEL_REG, 0x00000000 },
  1035. { SUN50I_H6_I2S_RX_CHAN_MAP0_REG, 0x00000000 },
  1036. { SUN50I_H6_I2S_RX_CHAN_MAP1_REG, 0x00000000 },
  1037. };
  1038. static const struct regmap_config sun4i_i2s_regmap_config = {
  1039. .reg_bits = 32,
  1040. .reg_stride = 4,
  1041. .val_bits = 32,
  1042. .max_register = SUN4I_I2S_RX_CHAN_MAP_REG,
  1043. .cache_type = REGCACHE_FLAT,
  1044. .reg_defaults = sun4i_i2s_reg_defaults,
  1045. .num_reg_defaults = ARRAY_SIZE(sun4i_i2s_reg_defaults),
  1046. .writeable_reg = sun4i_i2s_wr_reg,
  1047. .readable_reg = sun4i_i2s_rd_reg,
  1048. .volatile_reg = sun4i_i2s_volatile_reg,
  1049. };
  1050. static const struct regmap_config sun8i_i2s_regmap_config = {
  1051. .reg_bits = 32,
  1052. .reg_stride = 4,
  1053. .val_bits = 32,
  1054. .max_register = SUN8I_I2S_RX_CHAN_MAP_REG,
  1055. .cache_type = REGCACHE_FLAT,
  1056. .reg_defaults = sun8i_i2s_reg_defaults,
  1057. .num_reg_defaults = ARRAY_SIZE(sun8i_i2s_reg_defaults),
  1058. .writeable_reg = sun4i_i2s_wr_reg,
  1059. .readable_reg = sun8i_i2s_rd_reg,
  1060. .volatile_reg = sun8i_i2s_volatile_reg,
  1061. };
  1062. static const struct regmap_config sun50i_h6_i2s_regmap_config = {
  1063. .reg_bits = 32,
  1064. .reg_stride = 4,
  1065. .val_bits = 32,
  1066. .max_register = SUN50I_R329_I2S_RX_CHAN_MAP3_REG,
  1067. .cache_type = REGCACHE_FLAT,
  1068. .reg_defaults = sun50i_h6_i2s_reg_defaults,
  1069. .num_reg_defaults = ARRAY_SIZE(sun50i_h6_i2s_reg_defaults),
  1070. .writeable_reg = sun4i_i2s_wr_reg,
  1071. .readable_reg = sun8i_i2s_rd_reg,
  1072. .volatile_reg = sun8i_i2s_volatile_reg,
  1073. };
  1074. static int sun4i_i2s_runtime_resume(struct device *dev)
  1075. {
  1076. struct sun4i_i2s *i2s = dev_get_drvdata(dev);
  1077. int ret;
  1078. ret = clk_prepare_enable(i2s->bus_clk);
  1079. if (ret) {
  1080. dev_err(dev, "Failed to enable bus clock\n");
  1081. return ret;
  1082. }
  1083. regcache_cache_only(i2s->regmap, false);
  1084. regcache_mark_dirty(i2s->regmap);
  1085. ret = regcache_sync(i2s->regmap);
  1086. if (ret) {
  1087. dev_err(dev, "Failed to sync regmap cache\n");
  1088. goto err_disable_clk;
  1089. }
  1090. /* Enable the whole hardware block */
  1091. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  1092. SUN4I_I2S_CTRL_GL_EN, SUN4I_I2S_CTRL_GL_EN);
  1093. /* Enable the first output line */
  1094. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  1095. SUN4I_I2S_CTRL_SDO_EN_MASK,
  1096. SUN4I_I2S_CTRL_SDO_EN(0));
  1097. ret = clk_prepare_enable(i2s->mod_clk);
  1098. if (ret) {
  1099. dev_err(dev, "Failed to enable module clock\n");
  1100. goto err_disable_clk;
  1101. }
  1102. return 0;
  1103. err_disable_clk:
  1104. clk_disable_unprepare(i2s->bus_clk);
  1105. return ret;
  1106. }
  1107. static int sun4i_i2s_runtime_suspend(struct device *dev)
  1108. {
  1109. struct sun4i_i2s *i2s = dev_get_drvdata(dev);
  1110. clk_disable_unprepare(i2s->mod_clk);
  1111. /* Disable our output lines */
  1112. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  1113. SUN4I_I2S_CTRL_SDO_EN_MASK, 0);
  1114. /* Disable the whole hardware block */
  1115. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  1116. SUN4I_I2S_CTRL_GL_EN, 0);
  1117. regcache_cache_only(i2s->regmap, true);
  1118. clk_disable_unprepare(i2s->bus_clk);
  1119. return 0;
  1120. }
  1121. static const struct sun4i_i2s_quirks sun4i_a10_i2s_quirks = {
  1122. .has_reset = false,
  1123. .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
  1124. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  1125. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  1126. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  1127. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  1128. .bclk_dividers = sun4i_i2s_bclk_div,
  1129. .num_bclk_dividers = ARRAY_SIZE(sun4i_i2s_bclk_div),
  1130. .mclk_dividers = sun4i_i2s_mclk_div,
  1131. .num_mclk_dividers = ARRAY_SIZE(sun4i_i2s_mclk_div),
  1132. .get_bclk_parent_rate = sun4i_i2s_get_bclk_parent_rate,
  1133. .get_sr = sun4i_i2s_get_sr,
  1134. .get_wss = sun4i_i2s_get_wss,
  1135. .set_chan_cfg = sun4i_i2s_set_chan_cfg,
  1136. .set_fmt = sun4i_i2s_set_soc_fmt,
  1137. };
  1138. static const struct sun4i_i2s_quirks sun6i_a31_i2s_quirks = {
  1139. .has_reset = true,
  1140. .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
  1141. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  1142. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  1143. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  1144. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  1145. .bclk_dividers = sun4i_i2s_bclk_div,
  1146. .num_bclk_dividers = ARRAY_SIZE(sun4i_i2s_bclk_div),
  1147. .mclk_dividers = sun4i_i2s_mclk_div,
  1148. .num_mclk_dividers = ARRAY_SIZE(sun4i_i2s_mclk_div),
  1149. .get_bclk_parent_rate = sun4i_i2s_get_bclk_parent_rate,
  1150. .get_sr = sun4i_i2s_get_sr,
  1151. .get_wss = sun4i_i2s_get_wss,
  1152. .set_chan_cfg = sun4i_i2s_set_chan_cfg,
  1153. .set_fmt = sun4i_i2s_set_soc_fmt,
  1154. };
  1155. /*
  1156. * This doesn't describe the TDM controller documented in the A83t
  1157. * datasheet, but the three undocumented I2S controller that use the
  1158. * older design.
  1159. */
  1160. static const struct sun4i_i2s_quirks sun8i_a83t_i2s_quirks = {
  1161. .has_reset = true,
  1162. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  1163. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  1164. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  1165. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  1166. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  1167. .bclk_dividers = sun4i_i2s_bclk_div,
  1168. .num_bclk_dividers = ARRAY_SIZE(sun4i_i2s_bclk_div),
  1169. .mclk_dividers = sun4i_i2s_mclk_div,
  1170. .num_mclk_dividers = ARRAY_SIZE(sun4i_i2s_mclk_div),
  1171. .get_bclk_parent_rate = sun4i_i2s_get_bclk_parent_rate,
  1172. .get_sr = sun4i_i2s_get_sr,
  1173. .get_wss = sun4i_i2s_get_wss,
  1174. .set_chan_cfg = sun4i_i2s_set_chan_cfg,
  1175. .set_fmt = sun4i_i2s_set_soc_fmt,
  1176. };
  1177. static const struct sun4i_i2s_quirks sun8i_h3_i2s_quirks = {
  1178. .has_reset = true,
  1179. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  1180. .sun4i_i2s_regmap = &sun8i_i2s_regmap_config,
  1181. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 8, 8),
  1182. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 2),
  1183. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 6),
  1184. .bclk_dividers = sun8i_i2s_clk_div,
  1185. .num_bclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
  1186. .mclk_dividers = sun8i_i2s_clk_div,
  1187. .num_mclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
  1188. .get_bclk_parent_rate = sun8i_i2s_get_bclk_parent_rate,
  1189. .get_sr = sun8i_i2s_get_sr_wss,
  1190. .get_wss = sun8i_i2s_get_sr_wss,
  1191. .set_chan_cfg = sun8i_i2s_set_chan_cfg,
  1192. .set_fmt = sun8i_i2s_set_soc_fmt,
  1193. };
  1194. static const struct sun4i_i2s_quirks sun50i_a64_codec_i2s_quirks = {
  1195. .has_reset = true,
  1196. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  1197. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  1198. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  1199. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  1200. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  1201. .bclk_dividers = sun4i_i2s_bclk_div,
  1202. .num_bclk_dividers = ARRAY_SIZE(sun4i_i2s_bclk_div),
  1203. .mclk_dividers = sun4i_i2s_mclk_div,
  1204. .num_mclk_dividers = ARRAY_SIZE(sun4i_i2s_mclk_div),
  1205. .get_bclk_parent_rate = sun4i_i2s_get_bclk_parent_rate,
  1206. .get_sr = sun4i_i2s_get_sr,
  1207. .get_wss = sun4i_i2s_get_wss,
  1208. .set_chan_cfg = sun4i_i2s_set_chan_cfg,
  1209. .set_fmt = sun4i_i2s_set_soc_fmt,
  1210. };
  1211. static const struct sun4i_i2s_quirks sun50i_h6_i2s_quirks = {
  1212. .has_reset = true,
  1213. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  1214. .sun4i_i2s_regmap = &sun50i_h6_i2s_regmap_config,
  1215. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 8, 8),
  1216. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 2),
  1217. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 6),
  1218. .bclk_dividers = sun8i_i2s_clk_div,
  1219. .num_bclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
  1220. .mclk_dividers = sun8i_i2s_clk_div,
  1221. .num_mclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
  1222. .get_bclk_parent_rate = sun8i_i2s_get_bclk_parent_rate,
  1223. .get_sr = sun8i_i2s_get_sr_wss,
  1224. .get_wss = sun8i_i2s_get_sr_wss,
  1225. .set_chan_cfg = sun50i_h6_i2s_set_chan_cfg,
  1226. .set_fmt = sun50i_h6_i2s_set_soc_fmt,
  1227. };
  1228. static const struct sun4i_i2s_quirks sun50i_r329_i2s_quirks = {
  1229. .has_reset = true,
  1230. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  1231. .sun4i_i2s_regmap = &sun50i_h6_i2s_regmap_config,
  1232. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 8, 8),
  1233. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 2),
  1234. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 6),
  1235. .num_din_pins = 4,
  1236. .num_dout_pins = 4,
  1237. .bclk_dividers = sun8i_i2s_clk_div,
  1238. .num_bclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
  1239. .mclk_dividers = sun8i_i2s_clk_div,
  1240. .num_mclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
  1241. .get_bclk_parent_rate = sun8i_i2s_get_bclk_parent_rate,
  1242. .get_sr = sun8i_i2s_get_sr_wss,
  1243. .get_wss = sun8i_i2s_get_sr_wss,
  1244. .set_chan_cfg = sun50i_h6_i2s_set_chan_cfg,
  1245. .set_fmt = sun50i_h6_i2s_set_soc_fmt,
  1246. };
  1247. static int sun4i_i2s_init_regmap_fields(struct device *dev,
  1248. struct sun4i_i2s *i2s)
  1249. {
  1250. i2s->field_clkdiv_mclk_en =
  1251. devm_regmap_field_alloc(dev, i2s->regmap,
  1252. i2s->variant->field_clkdiv_mclk_en);
  1253. if (IS_ERR(i2s->field_clkdiv_mclk_en))
  1254. return PTR_ERR(i2s->field_clkdiv_mclk_en);
  1255. i2s->field_fmt_wss =
  1256. devm_regmap_field_alloc(dev, i2s->regmap,
  1257. i2s->variant->field_fmt_wss);
  1258. if (IS_ERR(i2s->field_fmt_wss))
  1259. return PTR_ERR(i2s->field_fmt_wss);
  1260. i2s->field_fmt_sr =
  1261. devm_regmap_field_alloc(dev, i2s->regmap,
  1262. i2s->variant->field_fmt_sr);
  1263. if (IS_ERR(i2s->field_fmt_sr))
  1264. return PTR_ERR(i2s->field_fmt_sr);
  1265. return 0;
  1266. }
  1267. static int sun4i_i2s_probe(struct platform_device *pdev)
  1268. {
  1269. struct sun4i_i2s *i2s;
  1270. struct resource *res;
  1271. void __iomem *regs;
  1272. int irq, ret;
  1273. i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
  1274. if (!i2s)
  1275. return -ENOMEM;
  1276. platform_set_drvdata(pdev, i2s);
  1277. regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1278. if (IS_ERR(regs))
  1279. return PTR_ERR(regs);
  1280. irq = platform_get_irq(pdev, 0);
  1281. if (irq < 0)
  1282. return irq;
  1283. i2s->variant = of_device_get_match_data(&pdev->dev);
  1284. if (!i2s->variant) {
  1285. dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
  1286. return -ENODEV;
  1287. }
  1288. i2s->bus_clk = devm_clk_get(&pdev->dev, "apb");
  1289. if (IS_ERR(i2s->bus_clk)) {
  1290. dev_err(&pdev->dev, "Can't get our bus clock\n");
  1291. return PTR_ERR(i2s->bus_clk);
  1292. }
  1293. i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
  1294. i2s->variant->sun4i_i2s_regmap);
  1295. if (IS_ERR(i2s->regmap)) {
  1296. dev_err(&pdev->dev, "Regmap initialisation failed\n");
  1297. return PTR_ERR(i2s->regmap);
  1298. }
  1299. i2s->mod_clk = devm_clk_get(&pdev->dev, "mod");
  1300. if (IS_ERR(i2s->mod_clk)) {
  1301. dev_err(&pdev->dev, "Can't get our mod clock\n");
  1302. return PTR_ERR(i2s->mod_clk);
  1303. }
  1304. if (i2s->variant->has_reset) {
  1305. i2s->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  1306. if (IS_ERR(i2s->rst)) {
  1307. dev_err(&pdev->dev, "Failed to get reset control\n");
  1308. return PTR_ERR(i2s->rst);
  1309. }
  1310. }
  1311. if (!IS_ERR(i2s->rst)) {
  1312. ret = reset_control_deassert(i2s->rst);
  1313. if (ret) {
  1314. dev_err(&pdev->dev,
  1315. "Failed to deassert the reset control\n");
  1316. return -EINVAL;
  1317. }
  1318. }
  1319. i2s->playback_dma_data.addr = res->start +
  1320. i2s->variant->reg_offset_txdata;
  1321. i2s->playback_dma_data.maxburst = 8;
  1322. i2s->capture_dma_data.addr = res->start + SUN4I_I2S_FIFO_RX_REG;
  1323. i2s->capture_dma_data.maxburst = 8;
  1324. pm_runtime_enable(&pdev->dev);
  1325. if (!pm_runtime_enabled(&pdev->dev)) {
  1326. ret = sun4i_i2s_runtime_resume(&pdev->dev);
  1327. if (ret)
  1328. goto err_pm_disable;
  1329. }
  1330. ret = sun4i_i2s_init_regmap_fields(&pdev->dev, i2s);
  1331. if (ret) {
  1332. dev_err(&pdev->dev, "Could not initialise regmap fields\n");
  1333. goto err_suspend;
  1334. }
  1335. ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  1336. if (ret) {
  1337. dev_err(&pdev->dev, "Could not register PCM\n");
  1338. goto err_suspend;
  1339. }
  1340. ret = devm_snd_soc_register_component(&pdev->dev,
  1341. &sun4i_i2s_component,
  1342. &sun4i_i2s_dai, 1);
  1343. if (ret) {
  1344. dev_err(&pdev->dev, "Could not register DAI\n");
  1345. goto err_suspend;
  1346. }
  1347. return 0;
  1348. err_suspend:
  1349. if (!pm_runtime_status_suspended(&pdev->dev))
  1350. sun4i_i2s_runtime_suspend(&pdev->dev);
  1351. err_pm_disable:
  1352. pm_runtime_disable(&pdev->dev);
  1353. if (!IS_ERR(i2s->rst))
  1354. reset_control_assert(i2s->rst);
  1355. return ret;
  1356. }
  1357. static int sun4i_i2s_remove(struct platform_device *pdev)
  1358. {
  1359. struct sun4i_i2s *i2s = dev_get_drvdata(&pdev->dev);
  1360. pm_runtime_disable(&pdev->dev);
  1361. if (!pm_runtime_status_suspended(&pdev->dev))
  1362. sun4i_i2s_runtime_suspend(&pdev->dev);
  1363. if (!IS_ERR(i2s->rst))
  1364. reset_control_assert(i2s->rst);
  1365. return 0;
  1366. }
  1367. static const struct of_device_id sun4i_i2s_match[] = {
  1368. {
  1369. .compatible = "allwinner,sun4i-a10-i2s",
  1370. .data = &sun4i_a10_i2s_quirks,
  1371. },
  1372. {
  1373. .compatible = "allwinner,sun6i-a31-i2s",
  1374. .data = &sun6i_a31_i2s_quirks,
  1375. },
  1376. {
  1377. .compatible = "allwinner,sun8i-a83t-i2s",
  1378. .data = &sun8i_a83t_i2s_quirks,
  1379. },
  1380. {
  1381. .compatible = "allwinner,sun8i-h3-i2s",
  1382. .data = &sun8i_h3_i2s_quirks,
  1383. },
  1384. {
  1385. .compatible = "allwinner,sun50i-a64-codec-i2s",
  1386. .data = &sun50i_a64_codec_i2s_quirks,
  1387. },
  1388. {
  1389. .compatible = "allwinner,sun50i-h6-i2s",
  1390. .data = &sun50i_h6_i2s_quirks,
  1391. },
  1392. {
  1393. .compatible = "allwinner,sun50i-r329-i2s",
  1394. .data = &sun50i_r329_i2s_quirks,
  1395. },
  1396. {}
  1397. };
  1398. MODULE_DEVICE_TABLE(of, sun4i_i2s_match);
  1399. static const struct dev_pm_ops sun4i_i2s_pm_ops = {
  1400. .runtime_resume = sun4i_i2s_runtime_resume,
  1401. .runtime_suspend = sun4i_i2s_runtime_suspend,
  1402. };
  1403. static struct platform_driver sun4i_i2s_driver = {
  1404. .probe = sun4i_i2s_probe,
  1405. .remove = sun4i_i2s_remove,
  1406. .driver = {
  1407. .name = "sun4i-i2s",
  1408. .of_match_table = sun4i_i2s_match,
  1409. .pm = &sun4i_i2s_pm_ops,
  1410. },
  1411. };
  1412. module_platform_driver(sun4i_i2s_driver);
  1413. MODULE_AUTHOR("Andrea Venturi <[email protected]>");
  1414. MODULE_AUTHOR("Maxime Ripard <[email protected]>");
  1415. MODULE_DESCRIPTION("Allwinner A10 I2S driver");
  1416. MODULE_LICENSE("GPL");