sun4i-i2s.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * Copyright (C) 2015 Andrea Venturi
  3. * Andrea Venturi <be17068@iperbole.bo.it>
  4. *
  5. * Copyright (C) 2016 Maxime Ripard
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/regmap.h>
  20. #include <linux/reset.h>
  21. #include <sound/dmaengine_pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dai.h>
  25. #define SUN4I_I2S_CTRL_REG 0x00
  26. #define SUN4I_I2S_CTRL_SDO_EN_MASK GENMASK(11, 8)
  27. #define SUN4I_I2S_CTRL_SDO_EN(sdo) BIT(8 + (sdo))
  28. #define SUN4I_I2S_CTRL_MODE_MASK BIT(5)
  29. #define SUN4I_I2S_CTRL_MODE_SLAVE (1 << 5)
  30. #define SUN4I_I2S_CTRL_MODE_MASTER (0 << 5)
  31. #define SUN4I_I2S_CTRL_TX_EN BIT(2)
  32. #define SUN4I_I2S_CTRL_RX_EN BIT(1)
  33. #define SUN4I_I2S_CTRL_GL_EN BIT(0)
  34. #define SUN4I_I2S_FMT0_REG 0x04
  35. #define SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK BIT(7)
  36. #define SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED (1 << 7)
  37. #define SUN4I_I2S_FMT0_LRCLK_POLARITY_NORMAL (0 << 7)
  38. #define SUN4I_I2S_FMT0_BCLK_POLARITY_MASK BIT(6)
  39. #define SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED (1 << 6)
  40. #define SUN4I_I2S_FMT0_BCLK_POLARITY_NORMAL (0 << 6)
  41. #define SUN4I_I2S_FMT0_SR_MASK GENMASK(5, 4)
  42. #define SUN4I_I2S_FMT0_SR(sr) ((sr) << 4)
  43. #define SUN4I_I2S_FMT0_WSS_MASK GENMASK(3, 2)
  44. #define SUN4I_I2S_FMT0_WSS(wss) ((wss) << 2)
  45. #define SUN4I_I2S_FMT0_FMT_MASK GENMASK(1, 0)
  46. #define SUN4I_I2S_FMT0_FMT_RIGHT_J (2 << 0)
  47. #define SUN4I_I2S_FMT0_FMT_LEFT_J (1 << 0)
  48. #define SUN4I_I2S_FMT0_FMT_I2S (0 << 0)
  49. #define SUN4I_I2S_FMT0_POLARITY_INVERTED (1)
  50. #define SUN4I_I2S_FMT0_POLARITY_NORMAL (0)
  51. #define SUN4I_I2S_FMT1_REG 0x08
  52. #define SUN4I_I2S_FIFO_TX_REG 0x0c
  53. #define SUN4I_I2S_FIFO_RX_REG 0x10
  54. #define SUN4I_I2S_FIFO_CTRL_REG 0x14
  55. #define SUN4I_I2S_FIFO_CTRL_FLUSH_TX BIT(25)
  56. #define SUN4I_I2S_FIFO_CTRL_FLUSH_RX BIT(24)
  57. #define SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK BIT(2)
  58. #define SUN4I_I2S_FIFO_CTRL_TX_MODE(mode) ((mode) << 2)
  59. #define SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK GENMASK(1, 0)
  60. #define SUN4I_I2S_FIFO_CTRL_RX_MODE(mode) (mode)
  61. #define SUN4I_I2S_FIFO_STA_REG 0x18
  62. #define SUN4I_I2S_DMA_INT_CTRL_REG 0x1c
  63. #define SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN BIT(7)
  64. #define SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN BIT(3)
  65. #define SUN4I_I2S_INT_STA_REG 0x20
  66. #define SUN4I_I2S_CLK_DIV_REG 0x24
  67. #define SUN4I_I2S_CLK_DIV_MCLK_EN BIT(7)
  68. #define SUN4I_I2S_CLK_DIV_BCLK_MASK GENMASK(6, 4)
  69. #define SUN4I_I2S_CLK_DIV_BCLK(bclk) ((bclk) << 4)
  70. #define SUN4I_I2S_CLK_DIV_MCLK_MASK GENMASK(3, 0)
  71. #define SUN4I_I2S_CLK_DIV_MCLK(mclk) ((mclk) << 0)
  72. #define SUN4I_I2S_RX_CNT_REG 0x28
  73. #define SUN4I_I2S_TX_CNT_REG 0x2c
  74. #define SUN4I_I2S_TX_CHAN_SEL_REG 0x30
  75. #define SUN4I_I2S_CHAN_SEL(num_chan) (((num_chan) - 1) << 0)
  76. #define SUN4I_I2S_TX_CHAN_MAP_REG 0x34
  77. #define SUN4I_I2S_TX_CHAN_MAP(chan, sample) ((sample) << (chan << 2))
  78. #define SUN4I_I2S_RX_CHAN_SEL_REG 0x38
  79. #define SUN4I_I2S_RX_CHAN_MAP_REG 0x3c
  80. /* Defines required for sun8i-h3 support */
  81. #define SUN8I_I2S_CTRL_BCLK_OUT BIT(18)
  82. #define SUN8I_I2S_CTRL_LRCK_OUT BIT(17)
  83. #define SUN8I_I2S_FMT0_LRCK_PERIOD_MASK GENMASK(17, 8)
  84. #define SUN8I_I2S_FMT0_LRCK_PERIOD(period) ((period - 1) << 8)
  85. #define SUN8I_I2S_INT_STA_REG 0x0c
  86. #define SUN8I_I2S_FIFO_TX_REG 0x20
  87. #define SUN8I_I2S_CHAN_CFG_REG 0x30
  88. #define SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK GENMASK(6, 4)
  89. #define SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(chan) ((chan - 1) << 4)
  90. #define SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK GENMASK(2, 0)
  91. #define SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(chan) (chan - 1)
  92. #define SUN8I_I2S_TX_CHAN_MAP_REG 0x44
  93. #define SUN8I_I2S_TX_CHAN_SEL_REG 0x34
  94. #define SUN8I_I2S_TX_CHAN_OFFSET_MASK GENMASK(13, 11)
  95. #define SUN8I_I2S_TX_CHAN_OFFSET(offset) (offset << 12)
  96. #define SUN8I_I2S_TX_CHAN_EN_MASK GENMASK(11, 4)
  97. #define SUN8I_I2S_TX_CHAN_EN(num_chan) (((1 << num_chan) - 1) << 4)
  98. #define SUN8I_I2S_RX_CHAN_SEL_REG 0x54
  99. #define SUN8I_I2S_RX_CHAN_MAP_REG 0x58
  100. /**
  101. * struct sun4i_i2s_quirks - Differences between SoC variants.
  102. *
  103. * @has_reset: SoC needs reset deasserted.
  104. * @has_slave_select_bit: SoC has a bit to enable slave mode.
  105. * @has_fmt_set_lrck_period: SoC requires lrclk period to be set.
  106. * @has_chcfg: tx and rx slot number need to be set.
  107. * @has_chsel_tx_chen: SoC requires that the tx channels are enabled.
  108. * @has_chsel_offset: SoC uses offset for selecting dai operational mode.
  109. * @reg_offset_txdata: offset of the tx fifo.
  110. * @sun4i_i2s_regmap: regmap config to use.
  111. * @mclk_offset: Value by which mclkdiv needs to be adjusted.
  112. * @bclk_offset: Value by which bclkdiv needs to be adjusted.
  113. * @fmt_offset: Value by which wss and sr needs to be adjusted.
  114. * @field_clkdiv_mclk_en: regmap field to enable mclk output.
  115. * @field_fmt_wss: regmap field to set word select size.
  116. * @field_fmt_sr: regmap field to set sample resolution.
  117. * @field_fmt_bclk: regmap field to set clk polarity.
  118. * @field_fmt_lrclk: regmap field to set frame polarity.
  119. * @field_fmt_mode: regmap field to set the operational mode.
  120. * @field_txchanmap: location of the tx channel mapping register.
  121. * @field_rxchanmap: location of the rx channel mapping register.
  122. * @field_txchansel: location of the tx channel select bit fields.
  123. * @field_rxchansel: location of the rx channel select bit fields.
  124. */
  125. struct sun4i_i2s_quirks {
  126. bool has_reset;
  127. bool has_slave_select_bit;
  128. bool has_fmt_set_lrck_period;
  129. bool has_chcfg;
  130. bool has_chsel_tx_chen;
  131. bool has_chsel_offset;
  132. unsigned int reg_offset_txdata; /* TX FIFO */
  133. const struct regmap_config *sun4i_i2s_regmap;
  134. unsigned int mclk_offset;
  135. unsigned int bclk_offset;
  136. unsigned int fmt_offset;
  137. /* Register fields for i2s */
  138. struct reg_field field_clkdiv_mclk_en;
  139. struct reg_field field_fmt_wss;
  140. struct reg_field field_fmt_sr;
  141. struct reg_field field_fmt_bclk;
  142. struct reg_field field_fmt_lrclk;
  143. struct reg_field field_fmt_mode;
  144. struct reg_field field_txchanmap;
  145. struct reg_field field_rxchanmap;
  146. struct reg_field field_txchansel;
  147. struct reg_field field_rxchansel;
  148. };
  149. struct sun4i_i2s {
  150. struct clk *bus_clk;
  151. struct clk *mod_clk;
  152. struct regmap *regmap;
  153. struct reset_control *rst;
  154. unsigned int mclk_freq;
  155. struct snd_dmaengine_dai_dma_data capture_dma_data;
  156. struct snd_dmaengine_dai_dma_data playback_dma_data;
  157. /* Register fields for i2s */
  158. struct regmap_field *field_clkdiv_mclk_en;
  159. struct regmap_field *field_fmt_wss;
  160. struct regmap_field *field_fmt_sr;
  161. struct regmap_field *field_fmt_bclk;
  162. struct regmap_field *field_fmt_lrclk;
  163. struct regmap_field *field_fmt_mode;
  164. struct regmap_field *field_txchanmap;
  165. struct regmap_field *field_rxchanmap;
  166. struct regmap_field *field_txchansel;
  167. struct regmap_field *field_rxchansel;
  168. const struct sun4i_i2s_quirks *variant;
  169. };
  170. struct sun4i_i2s_clk_div {
  171. u8 div;
  172. u8 val;
  173. };
  174. static const struct sun4i_i2s_clk_div sun4i_i2s_bclk_div[] = {
  175. { .div = 2, .val = 0 },
  176. { .div = 4, .val = 1 },
  177. { .div = 6, .val = 2 },
  178. { .div = 8, .val = 3 },
  179. { .div = 12, .val = 4 },
  180. { .div = 16, .val = 5 },
  181. /* TODO - extend divide ratio supported by newer SoCs */
  182. };
  183. static const struct sun4i_i2s_clk_div sun4i_i2s_mclk_div[] = {
  184. { .div = 1, .val = 0 },
  185. { .div = 2, .val = 1 },
  186. { .div = 4, .val = 2 },
  187. { .div = 6, .val = 3 },
  188. { .div = 8, .val = 4 },
  189. { .div = 12, .val = 5 },
  190. { .div = 16, .val = 6 },
  191. { .div = 24, .val = 7 },
  192. /* TODO - extend divide ratio supported by newer SoCs */
  193. };
  194. static int sun4i_i2s_get_bclk_div(struct sun4i_i2s *i2s,
  195. unsigned int oversample_rate,
  196. unsigned int word_size)
  197. {
  198. int div = oversample_rate / word_size / 2;
  199. int i;
  200. for (i = 0; i < ARRAY_SIZE(sun4i_i2s_bclk_div); i++) {
  201. const struct sun4i_i2s_clk_div *bdiv = &sun4i_i2s_bclk_div[i];
  202. if (bdiv->div == div)
  203. return bdiv->val;
  204. }
  205. return -EINVAL;
  206. }
  207. static int sun4i_i2s_get_mclk_div(struct sun4i_i2s *i2s,
  208. unsigned int oversample_rate,
  209. unsigned int module_rate,
  210. unsigned int sampling_rate)
  211. {
  212. int div = module_rate / sampling_rate / oversample_rate;
  213. int i;
  214. for (i = 0; i < ARRAY_SIZE(sun4i_i2s_mclk_div); i++) {
  215. const struct sun4i_i2s_clk_div *mdiv = &sun4i_i2s_mclk_div[i];
  216. if (mdiv->div == div)
  217. return mdiv->val;
  218. }
  219. return -EINVAL;
  220. }
  221. static int sun4i_i2s_oversample_rates[] = { 128, 192, 256, 384, 512, 768 };
  222. static bool sun4i_i2s_oversample_is_valid(unsigned int oversample)
  223. {
  224. int i;
  225. for (i = 0; i < ARRAY_SIZE(sun4i_i2s_oversample_rates); i++)
  226. if (sun4i_i2s_oversample_rates[i] == oversample)
  227. return true;
  228. return false;
  229. }
  230. static int sun4i_i2s_set_clk_rate(struct snd_soc_dai *dai,
  231. unsigned int rate,
  232. unsigned int word_size)
  233. {
  234. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  235. unsigned int oversample_rate, clk_rate;
  236. int bclk_div, mclk_div;
  237. int ret;
  238. switch (rate) {
  239. case 176400:
  240. case 88200:
  241. case 44100:
  242. case 22050:
  243. case 11025:
  244. clk_rate = 22579200;
  245. break;
  246. case 192000:
  247. case 128000:
  248. case 96000:
  249. case 64000:
  250. case 48000:
  251. case 32000:
  252. case 24000:
  253. case 16000:
  254. case 12000:
  255. case 8000:
  256. clk_rate = 24576000;
  257. break;
  258. default:
  259. dev_err(dai->dev, "Unsupported sample rate: %u\n", rate);
  260. return -EINVAL;
  261. }
  262. ret = clk_set_rate(i2s->mod_clk, clk_rate);
  263. if (ret)
  264. return ret;
  265. oversample_rate = i2s->mclk_freq / rate;
  266. if (!sun4i_i2s_oversample_is_valid(oversample_rate)) {
  267. dev_err(dai->dev, "Unsupported oversample rate: %d\n",
  268. oversample_rate);
  269. return -EINVAL;
  270. }
  271. bclk_div = sun4i_i2s_get_bclk_div(i2s, oversample_rate,
  272. word_size);
  273. if (bclk_div < 0) {
  274. dev_err(dai->dev, "Unsupported BCLK divider: %d\n", bclk_div);
  275. return -EINVAL;
  276. }
  277. mclk_div = sun4i_i2s_get_mclk_div(i2s, oversample_rate,
  278. clk_rate, rate);
  279. if (mclk_div < 0) {
  280. dev_err(dai->dev, "Unsupported MCLK divider: %d\n", mclk_div);
  281. return -EINVAL;
  282. }
  283. /* Adjust the clock division values if needed */
  284. bclk_div += i2s->variant->bclk_offset;
  285. mclk_div += i2s->variant->mclk_offset;
  286. regmap_write(i2s->regmap, SUN4I_I2S_CLK_DIV_REG,
  287. SUN4I_I2S_CLK_DIV_BCLK(bclk_div) |
  288. SUN4I_I2S_CLK_DIV_MCLK(mclk_div));
  289. regmap_field_write(i2s->field_clkdiv_mclk_en, 1);
  290. /* Set sync period */
  291. if (i2s->variant->has_fmt_set_lrck_period)
  292. regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
  293. SUN8I_I2S_FMT0_LRCK_PERIOD_MASK,
  294. SUN8I_I2S_FMT0_LRCK_PERIOD(32));
  295. return 0;
  296. }
  297. static int sun4i_i2s_hw_params(struct snd_pcm_substream *substream,
  298. struct snd_pcm_hw_params *params,
  299. struct snd_soc_dai *dai)
  300. {
  301. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  302. int sr, wss, channels;
  303. u32 width;
  304. channels = params_channels(params);
  305. if (channels != 2) {
  306. dev_err(dai->dev, "Unsupported number of channels: %d\n",
  307. channels);
  308. return -EINVAL;
  309. }
  310. if (i2s->variant->has_chcfg) {
  311. regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
  312. SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK,
  313. SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(channels));
  314. regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
  315. SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK,
  316. SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(channels));
  317. }
  318. /* Map the channels for playback and capture */
  319. regmap_field_write(i2s->field_txchanmap, 0x76543210);
  320. regmap_field_write(i2s->field_rxchanmap, 0x00003210);
  321. /* Configure the channels */
  322. regmap_field_write(i2s->field_txchansel,
  323. SUN4I_I2S_CHAN_SEL(params_channels(params)));
  324. regmap_field_write(i2s->field_rxchansel,
  325. SUN4I_I2S_CHAN_SEL(params_channels(params)));
  326. if (i2s->variant->has_chsel_tx_chen)
  327. regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
  328. SUN8I_I2S_TX_CHAN_EN_MASK,
  329. SUN8I_I2S_TX_CHAN_EN(channels));
  330. switch (params_physical_width(params)) {
  331. case 16:
  332. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  333. break;
  334. default:
  335. dev_err(dai->dev, "Unsupported physical sample width: %d\n",
  336. params_physical_width(params));
  337. return -EINVAL;
  338. }
  339. i2s->playback_dma_data.addr_width = width;
  340. switch (params_width(params)) {
  341. case 16:
  342. sr = 0;
  343. wss = 0;
  344. break;
  345. default:
  346. dev_err(dai->dev, "Unsupported sample width: %d\n",
  347. params_width(params));
  348. return -EINVAL;
  349. }
  350. regmap_field_write(i2s->field_fmt_wss,
  351. wss + i2s->variant->fmt_offset);
  352. regmap_field_write(i2s->field_fmt_sr,
  353. sr + i2s->variant->fmt_offset);
  354. return sun4i_i2s_set_clk_rate(dai, params_rate(params),
  355. params_width(params));
  356. }
  357. static int sun4i_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  358. {
  359. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  360. u32 val;
  361. u32 offset = 0;
  362. u32 bclk_polarity = SUN4I_I2S_FMT0_POLARITY_NORMAL;
  363. u32 lrclk_polarity = SUN4I_I2S_FMT0_POLARITY_NORMAL;
  364. /* DAI Mode */
  365. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  366. case SND_SOC_DAIFMT_I2S:
  367. val = SUN4I_I2S_FMT0_FMT_I2S;
  368. offset = 1;
  369. break;
  370. case SND_SOC_DAIFMT_LEFT_J:
  371. val = SUN4I_I2S_FMT0_FMT_LEFT_J;
  372. break;
  373. case SND_SOC_DAIFMT_RIGHT_J:
  374. val = SUN4I_I2S_FMT0_FMT_RIGHT_J;
  375. break;
  376. default:
  377. dev_err(dai->dev, "Unsupported format: %d\n",
  378. fmt & SND_SOC_DAIFMT_FORMAT_MASK);
  379. return -EINVAL;
  380. }
  381. if (i2s->variant->has_chsel_offset) {
  382. /*
  383. * offset being set indicates that we're connected to an i2s
  384. * device, however offset is only used on the sun8i block and
  385. * i2s shares the same setting with the LJ format. Increment
  386. * val so that the bit to value to write is correct.
  387. */
  388. if (offset > 0)
  389. val++;
  390. /* blck offset determines whether i2s or LJ */
  391. regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
  392. SUN8I_I2S_TX_CHAN_OFFSET_MASK,
  393. SUN8I_I2S_TX_CHAN_OFFSET(offset));
  394. }
  395. regmap_field_write(i2s->field_fmt_mode, val);
  396. /* DAI clock polarity */
  397. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  398. case SND_SOC_DAIFMT_IB_IF:
  399. /* Invert both clocks */
  400. bclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED;
  401. lrclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED;
  402. break;
  403. case SND_SOC_DAIFMT_IB_NF:
  404. /* Invert bit clock */
  405. bclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED;
  406. break;
  407. case SND_SOC_DAIFMT_NB_IF:
  408. /* Invert frame clock */
  409. lrclk_polarity = SUN4I_I2S_FMT0_POLARITY_INVERTED;
  410. break;
  411. case SND_SOC_DAIFMT_NB_NF:
  412. break;
  413. default:
  414. dev_err(dai->dev, "Unsupported clock polarity: %d\n",
  415. fmt & SND_SOC_DAIFMT_INV_MASK);
  416. return -EINVAL;
  417. }
  418. regmap_field_write(i2s->field_fmt_bclk, bclk_polarity);
  419. regmap_field_write(i2s->field_fmt_lrclk, lrclk_polarity);
  420. if (i2s->variant->has_slave_select_bit) {
  421. /* DAI clock master masks */
  422. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  423. case SND_SOC_DAIFMT_CBS_CFS:
  424. /* BCLK and LRCLK master */
  425. val = SUN4I_I2S_CTRL_MODE_MASTER;
  426. break;
  427. case SND_SOC_DAIFMT_CBM_CFM:
  428. /* BCLK and LRCLK slave */
  429. val = SUN4I_I2S_CTRL_MODE_SLAVE;
  430. break;
  431. default:
  432. dev_err(dai->dev, "Unsupported slave setting: %d\n",
  433. fmt & SND_SOC_DAIFMT_MASTER_MASK);
  434. return -EINVAL;
  435. }
  436. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  437. SUN4I_I2S_CTRL_MODE_MASK,
  438. val);
  439. } else {
  440. /*
  441. * The newer i2s block does not have a slave select bit,
  442. * instead the clk pins are configured as inputs.
  443. */
  444. /* DAI clock master masks */
  445. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  446. case SND_SOC_DAIFMT_CBS_CFS:
  447. /* BCLK and LRCLK master */
  448. val = SUN8I_I2S_CTRL_BCLK_OUT |
  449. SUN8I_I2S_CTRL_LRCK_OUT;
  450. break;
  451. case SND_SOC_DAIFMT_CBM_CFM:
  452. /* BCLK and LRCLK slave */
  453. val = 0;
  454. break;
  455. default:
  456. dev_err(dai->dev, "Unsupported slave setting: %d\n",
  457. fmt & SND_SOC_DAIFMT_MASTER_MASK);
  458. return -EINVAL;
  459. }
  460. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  461. SUN8I_I2S_CTRL_BCLK_OUT |
  462. SUN8I_I2S_CTRL_LRCK_OUT,
  463. val);
  464. }
  465. /* Set significant bits in our FIFOs */
  466. regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
  467. SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK |
  468. SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK,
  469. SUN4I_I2S_FIFO_CTRL_TX_MODE(1) |
  470. SUN4I_I2S_FIFO_CTRL_RX_MODE(1));
  471. return 0;
  472. }
  473. static void sun4i_i2s_start_capture(struct sun4i_i2s *i2s)
  474. {
  475. /* Flush RX FIFO */
  476. regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
  477. SUN4I_I2S_FIFO_CTRL_FLUSH_RX,
  478. SUN4I_I2S_FIFO_CTRL_FLUSH_RX);
  479. /* Clear RX counter */
  480. regmap_write(i2s->regmap, SUN4I_I2S_RX_CNT_REG, 0);
  481. /* Enable RX Block */
  482. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  483. SUN4I_I2S_CTRL_RX_EN,
  484. SUN4I_I2S_CTRL_RX_EN);
  485. /* Enable RX DRQ */
  486. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  487. SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
  488. SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN);
  489. }
  490. static void sun4i_i2s_start_playback(struct sun4i_i2s *i2s)
  491. {
  492. /* Flush TX FIFO */
  493. regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
  494. SUN4I_I2S_FIFO_CTRL_FLUSH_TX,
  495. SUN4I_I2S_FIFO_CTRL_FLUSH_TX);
  496. /* Clear TX counter */
  497. regmap_write(i2s->regmap, SUN4I_I2S_TX_CNT_REG, 0);
  498. /* Enable TX Block */
  499. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  500. SUN4I_I2S_CTRL_TX_EN,
  501. SUN4I_I2S_CTRL_TX_EN);
  502. /* Enable TX DRQ */
  503. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  504. SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
  505. SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN);
  506. }
  507. static void sun4i_i2s_stop_capture(struct sun4i_i2s *i2s)
  508. {
  509. /* Disable RX Block */
  510. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  511. SUN4I_I2S_CTRL_RX_EN,
  512. 0);
  513. /* Disable RX DRQ */
  514. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  515. SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
  516. 0);
  517. }
  518. static void sun4i_i2s_stop_playback(struct sun4i_i2s *i2s)
  519. {
  520. /* Disable TX Block */
  521. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  522. SUN4I_I2S_CTRL_TX_EN,
  523. 0);
  524. /* Disable TX DRQ */
  525. regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
  526. SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
  527. 0);
  528. }
  529. static int sun4i_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  530. struct snd_soc_dai *dai)
  531. {
  532. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  533. switch (cmd) {
  534. case SNDRV_PCM_TRIGGER_START:
  535. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  536. case SNDRV_PCM_TRIGGER_RESUME:
  537. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  538. sun4i_i2s_start_playback(i2s);
  539. else
  540. sun4i_i2s_start_capture(i2s);
  541. break;
  542. case SNDRV_PCM_TRIGGER_STOP:
  543. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  544. case SNDRV_PCM_TRIGGER_SUSPEND:
  545. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  546. sun4i_i2s_stop_playback(i2s);
  547. else
  548. sun4i_i2s_stop_capture(i2s);
  549. break;
  550. default:
  551. return -EINVAL;
  552. }
  553. return 0;
  554. }
  555. static int sun4i_i2s_startup(struct snd_pcm_substream *substream,
  556. struct snd_soc_dai *dai)
  557. {
  558. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  559. /* Enable the whole hardware block */
  560. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  561. SUN4I_I2S_CTRL_GL_EN, SUN4I_I2S_CTRL_GL_EN);
  562. /* Enable the first output line */
  563. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  564. SUN4I_I2S_CTRL_SDO_EN_MASK,
  565. SUN4I_I2S_CTRL_SDO_EN(0));
  566. return clk_prepare_enable(i2s->mod_clk);
  567. }
  568. static void sun4i_i2s_shutdown(struct snd_pcm_substream *substream,
  569. struct snd_soc_dai *dai)
  570. {
  571. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  572. clk_disable_unprepare(i2s->mod_clk);
  573. /* Disable our output lines */
  574. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  575. SUN4I_I2S_CTRL_SDO_EN_MASK, 0);
  576. /* Disable the whole hardware block */
  577. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  578. SUN4I_I2S_CTRL_GL_EN, 0);
  579. }
  580. static int sun4i_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  581. unsigned int freq, int dir)
  582. {
  583. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  584. if (clk_id != 0)
  585. return -EINVAL;
  586. i2s->mclk_freq = freq;
  587. return 0;
  588. }
  589. static const struct snd_soc_dai_ops sun4i_i2s_dai_ops = {
  590. .hw_params = sun4i_i2s_hw_params,
  591. .set_fmt = sun4i_i2s_set_fmt,
  592. .set_sysclk = sun4i_i2s_set_sysclk,
  593. .shutdown = sun4i_i2s_shutdown,
  594. .startup = sun4i_i2s_startup,
  595. .trigger = sun4i_i2s_trigger,
  596. };
  597. static int sun4i_i2s_dai_probe(struct snd_soc_dai *dai)
  598. {
  599. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  600. snd_soc_dai_init_dma_data(dai,
  601. &i2s->playback_dma_data,
  602. &i2s->capture_dma_data);
  603. snd_soc_dai_set_drvdata(dai, i2s);
  604. return 0;
  605. }
  606. static struct snd_soc_dai_driver sun4i_i2s_dai = {
  607. .probe = sun4i_i2s_dai_probe,
  608. .capture = {
  609. .stream_name = "Capture",
  610. .channels_min = 2,
  611. .channels_max = 2,
  612. .rates = SNDRV_PCM_RATE_8000_192000,
  613. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  614. },
  615. .playback = {
  616. .stream_name = "Playback",
  617. .channels_min = 2,
  618. .channels_max = 2,
  619. .rates = SNDRV_PCM_RATE_8000_192000,
  620. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  621. },
  622. .ops = &sun4i_i2s_dai_ops,
  623. .symmetric_rates = 1,
  624. };
  625. static const struct snd_soc_component_driver sun4i_i2s_component = {
  626. .name = "sun4i-dai",
  627. };
  628. static bool sun4i_i2s_rd_reg(struct device *dev, unsigned int reg)
  629. {
  630. switch (reg) {
  631. case SUN4I_I2S_FIFO_TX_REG:
  632. return false;
  633. default:
  634. return true;
  635. }
  636. }
  637. static bool sun4i_i2s_wr_reg(struct device *dev, unsigned int reg)
  638. {
  639. switch (reg) {
  640. case SUN4I_I2S_FIFO_RX_REG:
  641. case SUN4I_I2S_FIFO_STA_REG:
  642. return false;
  643. default:
  644. return true;
  645. }
  646. }
  647. static bool sun4i_i2s_volatile_reg(struct device *dev, unsigned int reg)
  648. {
  649. switch (reg) {
  650. case SUN4I_I2S_FIFO_RX_REG:
  651. case SUN4I_I2S_INT_STA_REG:
  652. case SUN4I_I2S_RX_CNT_REG:
  653. case SUN4I_I2S_TX_CNT_REG:
  654. return true;
  655. default:
  656. return false;
  657. }
  658. }
  659. static bool sun8i_i2s_rd_reg(struct device *dev, unsigned int reg)
  660. {
  661. switch (reg) {
  662. case SUN8I_I2S_FIFO_TX_REG:
  663. return false;
  664. default:
  665. return true;
  666. }
  667. }
  668. static bool sun8i_i2s_volatile_reg(struct device *dev, unsigned int reg)
  669. {
  670. if (reg == SUN8I_I2S_INT_STA_REG)
  671. return true;
  672. if (reg == SUN8I_I2S_FIFO_TX_REG)
  673. return false;
  674. return sun4i_i2s_volatile_reg(dev, reg);
  675. }
  676. static const struct reg_default sun4i_i2s_reg_defaults[] = {
  677. { SUN4I_I2S_CTRL_REG, 0x00000000 },
  678. { SUN4I_I2S_FMT0_REG, 0x0000000c },
  679. { SUN4I_I2S_FMT1_REG, 0x00004020 },
  680. { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
  681. { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
  682. { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
  683. { SUN4I_I2S_TX_CHAN_SEL_REG, 0x00000001 },
  684. { SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210 },
  685. { SUN4I_I2S_RX_CHAN_SEL_REG, 0x00000001 },
  686. { SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210 },
  687. };
  688. static const struct reg_default sun8i_i2s_reg_defaults[] = {
  689. { SUN4I_I2S_CTRL_REG, 0x00060000 },
  690. { SUN4I_I2S_FMT0_REG, 0x00000033 },
  691. { SUN4I_I2S_FMT1_REG, 0x00000030 },
  692. { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
  693. { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
  694. { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
  695. { SUN8I_I2S_CHAN_CFG_REG, 0x00000000 },
  696. { SUN8I_I2S_TX_CHAN_SEL_REG, 0x00000000 },
  697. { SUN8I_I2S_TX_CHAN_MAP_REG, 0x00000000 },
  698. { SUN8I_I2S_RX_CHAN_SEL_REG, 0x00000000 },
  699. { SUN8I_I2S_RX_CHAN_MAP_REG, 0x00000000 },
  700. };
  701. static const struct regmap_config sun4i_i2s_regmap_config = {
  702. .reg_bits = 32,
  703. .reg_stride = 4,
  704. .val_bits = 32,
  705. .max_register = SUN4I_I2S_RX_CHAN_MAP_REG,
  706. .cache_type = REGCACHE_FLAT,
  707. .reg_defaults = sun4i_i2s_reg_defaults,
  708. .num_reg_defaults = ARRAY_SIZE(sun4i_i2s_reg_defaults),
  709. .writeable_reg = sun4i_i2s_wr_reg,
  710. .readable_reg = sun4i_i2s_rd_reg,
  711. .volatile_reg = sun4i_i2s_volatile_reg,
  712. };
  713. static const struct regmap_config sun8i_i2s_regmap_config = {
  714. .reg_bits = 32,
  715. .reg_stride = 4,
  716. .val_bits = 32,
  717. .max_register = SUN8I_I2S_RX_CHAN_MAP_REG,
  718. .cache_type = REGCACHE_FLAT,
  719. .reg_defaults = sun8i_i2s_reg_defaults,
  720. .num_reg_defaults = ARRAY_SIZE(sun8i_i2s_reg_defaults),
  721. .writeable_reg = sun4i_i2s_wr_reg,
  722. .readable_reg = sun8i_i2s_rd_reg,
  723. .volatile_reg = sun8i_i2s_volatile_reg,
  724. };
  725. static int sun4i_i2s_runtime_resume(struct device *dev)
  726. {
  727. struct sun4i_i2s *i2s = dev_get_drvdata(dev);
  728. int ret;
  729. ret = clk_prepare_enable(i2s->bus_clk);
  730. if (ret) {
  731. dev_err(dev, "Failed to enable bus clock\n");
  732. return ret;
  733. }
  734. regcache_cache_only(i2s->regmap, false);
  735. regcache_mark_dirty(i2s->regmap);
  736. ret = regcache_sync(i2s->regmap);
  737. if (ret) {
  738. dev_err(dev, "Failed to sync regmap cache\n");
  739. goto err_disable_clk;
  740. }
  741. return 0;
  742. err_disable_clk:
  743. clk_disable_unprepare(i2s->bus_clk);
  744. return ret;
  745. }
  746. static int sun4i_i2s_runtime_suspend(struct device *dev)
  747. {
  748. struct sun4i_i2s *i2s = dev_get_drvdata(dev);
  749. regcache_cache_only(i2s->regmap, true);
  750. clk_disable_unprepare(i2s->bus_clk);
  751. return 0;
  752. }
  753. static const struct sun4i_i2s_quirks sun4i_a10_i2s_quirks = {
  754. .has_reset = false,
  755. .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
  756. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  757. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  758. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  759. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  760. .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6),
  761. .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
  762. .has_slave_select_bit = true,
  763. .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1),
  764. .field_txchanmap = REG_FIELD(SUN4I_I2S_TX_CHAN_MAP_REG, 0, 31),
  765. .field_rxchanmap = REG_FIELD(SUN4I_I2S_RX_CHAN_MAP_REG, 0, 31),
  766. .field_txchansel = REG_FIELD(SUN4I_I2S_TX_CHAN_SEL_REG, 0, 2),
  767. .field_rxchansel = REG_FIELD(SUN4I_I2S_RX_CHAN_SEL_REG, 0, 2),
  768. };
  769. static const struct sun4i_i2s_quirks sun6i_a31_i2s_quirks = {
  770. .has_reset = true,
  771. .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
  772. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  773. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  774. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  775. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  776. .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6),
  777. .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
  778. .has_slave_select_bit = true,
  779. .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1),
  780. .field_txchanmap = REG_FIELD(SUN4I_I2S_TX_CHAN_MAP_REG, 0, 31),
  781. .field_rxchanmap = REG_FIELD(SUN4I_I2S_RX_CHAN_MAP_REG, 0, 31),
  782. .field_txchansel = REG_FIELD(SUN4I_I2S_TX_CHAN_SEL_REG, 0, 2),
  783. .field_rxchansel = REG_FIELD(SUN4I_I2S_RX_CHAN_SEL_REG, 0, 2),
  784. };
  785. static const struct sun4i_i2s_quirks sun8i_a83t_i2s_quirks = {
  786. .has_reset = true,
  787. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  788. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  789. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  790. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  791. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  792. .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6),
  793. .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
  794. .has_slave_select_bit = true,
  795. .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1),
  796. .field_txchanmap = REG_FIELD(SUN4I_I2S_TX_CHAN_MAP_REG, 0, 31),
  797. .field_rxchanmap = REG_FIELD(SUN4I_I2S_RX_CHAN_MAP_REG, 0, 31),
  798. .field_txchansel = REG_FIELD(SUN4I_I2S_TX_CHAN_SEL_REG, 0, 2),
  799. .field_rxchansel = REG_FIELD(SUN4I_I2S_RX_CHAN_SEL_REG, 0, 2),
  800. };
  801. static const struct sun4i_i2s_quirks sun8i_h3_i2s_quirks = {
  802. .has_reset = true,
  803. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  804. .sun4i_i2s_regmap = &sun8i_i2s_regmap_config,
  805. .mclk_offset = 1,
  806. .bclk_offset = 2,
  807. .fmt_offset = 3,
  808. .has_fmt_set_lrck_period = true,
  809. .has_chcfg = true,
  810. .has_chsel_tx_chen = true,
  811. .has_chsel_offset = true,
  812. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 8, 8),
  813. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 2),
  814. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 6),
  815. .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
  816. .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 19, 19),
  817. .field_fmt_mode = REG_FIELD(SUN4I_I2S_CTRL_REG, 4, 5),
  818. .field_txchanmap = REG_FIELD(SUN8I_I2S_TX_CHAN_MAP_REG, 0, 31),
  819. .field_rxchanmap = REG_FIELD(SUN8I_I2S_RX_CHAN_MAP_REG, 0, 31),
  820. .field_txchansel = REG_FIELD(SUN8I_I2S_TX_CHAN_SEL_REG, 0, 2),
  821. .field_rxchansel = REG_FIELD(SUN8I_I2S_RX_CHAN_SEL_REG, 0, 2),
  822. };
  823. static int sun4i_i2s_init_regmap_fields(struct device *dev,
  824. struct sun4i_i2s *i2s)
  825. {
  826. i2s->field_clkdiv_mclk_en =
  827. devm_regmap_field_alloc(dev, i2s->regmap,
  828. i2s->variant->field_clkdiv_mclk_en);
  829. if (IS_ERR(i2s->field_clkdiv_mclk_en))
  830. return PTR_ERR(i2s->field_clkdiv_mclk_en);
  831. i2s->field_fmt_wss =
  832. devm_regmap_field_alloc(dev, i2s->regmap,
  833. i2s->variant->field_fmt_wss);
  834. if (IS_ERR(i2s->field_fmt_wss))
  835. return PTR_ERR(i2s->field_fmt_wss);
  836. i2s->field_fmt_sr =
  837. devm_regmap_field_alloc(dev, i2s->regmap,
  838. i2s->variant->field_fmt_sr);
  839. if (IS_ERR(i2s->field_fmt_sr))
  840. return PTR_ERR(i2s->field_fmt_sr);
  841. i2s->field_fmt_bclk =
  842. devm_regmap_field_alloc(dev, i2s->regmap,
  843. i2s->variant->field_fmt_bclk);
  844. if (IS_ERR(i2s->field_fmt_bclk))
  845. return PTR_ERR(i2s->field_fmt_bclk);
  846. i2s->field_fmt_lrclk =
  847. devm_regmap_field_alloc(dev, i2s->regmap,
  848. i2s->variant->field_fmt_lrclk);
  849. if (IS_ERR(i2s->field_fmt_lrclk))
  850. return PTR_ERR(i2s->field_fmt_lrclk);
  851. i2s->field_fmt_mode =
  852. devm_regmap_field_alloc(dev, i2s->regmap,
  853. i2s->variant->field_fmt_mode);
  854. if (IS_ERR(i2s->field_fmt_mode))
  855. return PTR_ERR(i2s->field_fmt_mode);
  856. i2s->field_txchanmap =
  857. devm_regmap_field_alloc(dev, i2s->regmap,
  858. i2s->variant->field_txchanmap);
  859. if (IS_ERR(i2s->field_txchanmap))
  860. return PTR_ERR(i2s->field_txchanmap);
  861. i2s->field_rxchanmap =
  862. devm_regmap_field_alloc(dev, i2s->regmap,
  863. i2s->variant->field_rxchanmap);
  864. if (IS_ERR(i2s->field_rxchanmap))
  865. return PTR_ERR(i2s->field_rxchanmap);
  866. i2s->field_txchansel =
  867. devm_regmap_field_alloc(dev, i2s->regmap,
  868. i2s->variant->field_txchansel);
  869. if (IS_ERR(i2s->field_txchansel))
  870. return PTR_ERR(i2s->field_txchansel);
  871. i2s->field_rxchansel =
  872. devm_regmap_field_alloc(dev, i2s->regmap,
  873. i2s->variant->field_rxchansel);
  874. return PTR_ERR_OR_ZERO(i2s->field_rxchansel);
  875. }
  876. static int sun4i_i2s_probe(struct platform_device *pdev)
  877. {
  878. struct sun4i_i2s *i2s;
  879. struct resource *res;
  880. void __iomem *regs;
  881. int irq, ret;
  882. i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
  883. if (!i2s)
  884. return -ENOMEM;
  885. platform_set_drvdata(pdev, i2s);
  886. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  887. regs = devm_ioremap_resource(&pdev->dev, res);
  888. if (IS_ERR(regs))
  889. return PTR_ERR(regs);
  890. irq = platform_get_irq(pdev, 0);
  891. if (irq < 0) {
  892. dev_err(&pdev->dev, "Can't retrieve our interrupt\n");
  893. return irq;
  894. }
  895. i2s->variant = of_device_get_match_data(&pdev->dev);
  896. if (!i2s->variant) {
  897. dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
  898. return -ENODEV;
  899. }
  900. i2s->bus_clk = devm_clk_get(&pdev->dev, "apb");
  901. if (IS_ERR(i2s->bus_clk)) {
  902. dev_err(&pdev->dev, "Can't get our bus clock\n");
  903. return PTR_ERR(i2s->bus_clk);
  904. }
  905. i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
  906. i2s->variant->sun4i_i2s_regmap);
  907. if (IS_ERR(i2s->regmap)) {
  908. dev_err(&pdev->dev, "Regmap initialisation failed\n");
  909. return PTR_ERR(i2s->regmap);
  910. }
  911. i2s->mod_clk = devm_clk_get(&pdev->dev, "mod");
  912. if (IS_ERR(i2s->mod_clk)) {
  913. dev_err(&pdev->dev, "Can't get our mod clock\n");
  914. return PTR_ERR(i2s->mod_clk);
  915. }
  916. if (i2s->variant->has_reset) {
  917. i2s->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  918. if (IS_ERR(i2s->rst)) {
  919. dev_err(&pdev->dev, "Failed to get reset control\n");
  920. return PTR_ERR(i2s->rst);
  921. }
  922. }
  923. if (!IS_ERR(i2s->rst)) {
  924. ret = reset_control_deassert(i2s->rst);
  925. if (ret) {
  926. dev_err(&pdev->dev,
  927. "Failed to deassert the reset control\n");
  928. return -EINVAL;
  929. }
  930. }
  931. i2s->playback_dma_data.addr = res->start +
  932. i2s->variant->reg_offset_txdata;
  933. i2s->playback_dma_data.maxburst = 8;
  934. i2s->capture_dma_data.addr = res->start + SUN4I_I2S_FIFO_RX_REG;
  935. i2s->capture_dma_data.maxburst = 8;
  936. pm_runtime_enable(&pdev->dev);
  937. if (!pm_runtime_enabled(&pdev->dev)) {
  938. ret = sun4i_i2s_runtime_resume(&pdev->dev);
  939. if (ret)
  940. goto err_pm_disable;
  941. }
  942. ret = devm_snd_soc_register_component(&pdev->dev,
  943. &sun4i_i2s_component,
  944. &sun4i_i2s_dai, 1);
  945. if (ret) {
  946. dev_err(&pdev->dev, "Could not register DAI\n");
  947. goto err_suspend;
  948. }
  949. ret = snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  950. if (ret) {
  951. dev_err(&pdev->dev, "Could not register PCM\n");
  952. goto err_suspend;
  953. }
  954. ret = sun4i_i2s_init_regmap_fields(&pdev->dev, i2s);
  955. if (ret) {
  956. dev_err(&pdev->dev, "Could not initialise regmap fields\n");
  957. goto err_suspend;
  958. }
  959. return 0;
  960. err_suspend:
  961. if (!pm_runtime_status_suspended(&pdev->dev))
  962. sun4i_i2s_runtime_suspend(&pdev->dev);
  963. err_pm_disable:
  964. pm_runtime_disable(&pdev->dev);
  965. if (!IS_ERR(i2s->rst))
  966. reset_control_assert(i2s->rst);
  967. return ret;
  968. }
  969. static int sun4i_i2s_remove(struct platform_device *pdev)
  970. {
  971. struct sun4i_i2s *i2s = dev_get_drvdata(&pdev->dev);
  972. snd_dmaengine_pcm_unregister(&pdev->dev);
  973. pm_runtime_disable(&pdev->dev);
  974. if (!pm_runtime_status_suspended(&pdev->dev))
  975. sun4i_i2s_runtime_suspend(&pdev->dev);
  976. if (!IS_ERR(i2s->rst))
  977. reset_control_assert(i2s->rst);
  978. return 0;
  979. }
  980. static const struct of_device_id sun4i_i2s_match[] = {
  981. {
  982. .compatible = "allwinner,sun4i-a10-i2s",
  983. .data = &sun4i_a10_i2s_quirks,
  984. },
  985. {
  986. .compatible = "allwinner,sun6i-a31-i2s",
  987. .data = &sun6i_a31_i2s_quirks,
  988. },
  989. {
  990. .compatible = "allwinner,sun8i-a83t-i2s",
  991. .data = &sun8i_a83t_i2s_quirks,
  992. },
  993. {
  994. .compatible = "allwinner,sun8i-h3-i2s",
  995. .data = &sun8i_h3_i2s_quirks,
  996. },
  997. {}
  998. };
  999. MODULE_DEVICE_TABLE(of, sun4i_i2s_match);
  1000. static const struct dev_pm_ops sun4i_i2s_pm_ops = {
  1001. .runtime_resume = sun4i_i2s_runtime_resume,
  1002. .runtime_suspend = sun4i_i2s_runtime_suspend,
  1003. };
  1004. static struct platform_driver sun4i_i2s_driver = {
  1005. .probe = sun4i_i2s_probe,
  1006. .remove = sun4i_i2s_remove,
  1007. .driver = {
  1008. .name = "sun4i-i2s",
  1009. .of_match_table = sun4i_i2s_match,
  1010. .pm = &sun4i_i2s_pm_ops,
  1011. },
  1012. };
  1013. module_platform_driver(sun4i_i2s_driver);
  1014. MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
  1015. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  1016. MODULE_DESCRIPTION("Allwinner A10 I2S driver");
  1017. MODULE_LICENSE("GPL");