sun4i-i2s.c 33 KB

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