sun4i-i2s.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  556. unsigned int freq, int dir)
  557. {
  558. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  559. if (clk_id != 0)
  560. return -EINVAL;
  561. i2s->mclk_freq = freq;
  562. return 0;
  563. }
  564. static const struct snd_soc_dai_ops sun4i_i2s_dai_ops = {
  565. .hw_params = sun4i_i2s_hw_params,
  566. .set_fmt = sun4i_i2s_set_fmt,
  567. .set_sysclk = sun4i_i2s_set_sysclk,
  568. .trigger = sun4i_i2s_trigger,
  569. };
  570. static int sun4i_i2s_dai_probe(struct snd_soc_dai *dai)
  571. {
  572. struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
  573. snd_soc_dai_init_dma_data(dai,
  574. &i2s->playback_dma_data,
  575. &i2s->capture_dma_data);
  576. snd_soc_dai_set_drvdata(dai, i2s);
  577. return 0;
  578. }
  579. static struct snd_soc_dai_driver sun4i_i2s_dai = {
  580. .probe = sun4i_i2s_dai_probe,
  581. .capture = {
  582. .stream_name = "Capture",
  583. .channels_min = 2,
  584. .channels_max = 2,
  585. .rates = SNDRV_PCM_RATE_8000_192000,
  586. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  587. },
  588. .playback = {
  589. .stream_name = "Playback",
  590. .channels_min = 2,
  591. .channels_max = 2,
  592. .rates = SNDRV_PCM_RATE_8000_192000,
  593. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  594. },
  595. .ops = &sun4i_i2s_dai_ops,
  596. .symmetric_rates = 1,
  597. };
  598. static const struct snd_soc_component_driver sun4i_i2s_component = {
  599. .name = "sun4i-dai",
  600. };
  601. static bool sun4i_i2s_rd_reg(struct device *dev, unsigned int reg)
  602. {
  603. switch (reg) {
  604. case SUN4I_I2S_FIFO_TX_REG:
  605. return false;
  606. default:
  607. return true;
  608. }
  609. }
  610. static bool sun4i_i2s_wr_reg(struct device *dev, unsigned int reg)
  611. {
  612. switch (reg) {
  613. case SUN4I_I2S_FIFO_RX_REG:
  614. case SUN4I_I2S_FIFO_STA_REG:
  615. return false;
  616. default:
  617. return true;
  618. }
  619. }
  620. static bool sun4i_i2s_volatile_reg(struct device *dev, unsigned int reg)
  621. {
  622. switch (reg) {
  623. case SUN4I_I2S_FIFO_RX_REG:
  624. case SUN4I_I2S_INT_STA_REG:
  625. case SUN4I_I2S_RX_CNT_REG:
  626. case SUN4I_I2S_TX_CNT_REG:
  627. return true;
  628. default:
  629. return false;
  630. }
  631. }
  632. static bool sun8i_i2s_rd_reg(struct device *dev, unsigned int reg)
  633. {
  634. switch (reg) {
  635. case SUN8I_I2S_FIFO_TX_REG:
  636. return false;
  637. default:
  638. return true;
  639. }
  640. }
  641. static bool sun8i_i2s_volatile_reg(struct device *dev, unsigned int reg)
  642. {
  643. if (reg == SUN8I_I2S_INT_STA_REG)
  644. return true;
  645. if (reg == SUN8I_I2S_FIFO_TX_REG)
  646. return false;
  647. return sun4i_i2s_volatile_reg(dev, reg);
  648. }
  649. static const struct reg_default sun4i_i2s_reg_defaults[] = {
  650. { SUN4I_I2S_CTRL_REG, 0x00000000 },
  651. { SUN4I_I2S_FMT0_REG, 0x0000000c },
  652. { SUN4I_I2S_FMT1_REG, 0x00004020 },
  653. { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
  654. { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
  655. { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
  656. { SUN4I_I2S_TX_CHAN_SEL_REG, 0x00000001 },
  657. { SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210 },
  658. { SUN4I_I2S_RX_CHAN_SEL_REG, 0x00000001 },
  659. { SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210 },
  660. };
  661. static const struct reg_default sun8i_i2s_reg_defaults[] = {
  662. { SUN4I_I2S_CTRL_REG, 0x00060000 },
  663. { SUN4I_I2S_FMT0_REG, 0x00000033 },
  664. { SUN4I_I2S_FMT1_REG, 0x00000030 },
  665. { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
  666. { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
  667. { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
  668. { SUN8I_I2S_CHAN_CFG_REG, 0x00000000 },
  669. { SUN8I_I2S_TX_CHAN_SEL_REG, 0x00000000 },
  670. { SUN8I_I2S_TX_CHAN_MAP_REG, 0x00000000 },
  671. { SUN8I_I2S_RX_CHAN_SEL_REG, 0x00000000 },
  672. { SUN8I_I2S_RX_CHAN_MAP_REG, 0x00000000 },
  673. };
  674. static const struct regmap_config sun4i_i2s_regmap_config = {
  675. .reg_bits = 32,
  676. .reg_stride = 4,
  677. .val_bits = 32,
  678. .max_register = SUN4I_I2S_RX_CHAN_MAP_REG,
  679. .cache_type = REGCACHE_FLAT,
  680. .reg_defaults = sun4i_i2s_reg_defaults,
  681. .num_reg_defaults = ARRAY_SIZE(sun4i_i2s_reg_defaults),
  682. .writeable_reg = sun4i_i2s_wr_reg,
  683. .readable_reg = sun4i_i2s_rd_reg,
  684. .volatile_reg = sun4i_i2s_volatile_reg,
  685. };
  686. static const struct regmap_config sun8i_i2s_regmap_config = {
  687. .reg_bits = 32,
  688. .reg_stride = 4,
  689. .val_bits = 32,
  690. .max_register = SUN8I_I2S_RX_CHAN_MAP_REG,
  691. .cache_type = REGCACHE_FLAT,
  692. .reg_defaults = sun8i_i2s_reg_defaults,
  693. .num_reg_defaults = ARRAY_SIZE(sun8i_i2s_reg_defaults),
  694. .writeable_reg = sun4i_i2s_wr_reg,
  695. .readable_reg = sun8i_i2s_rd_reg,
  696. .volatile_reg = sun8i_i2s_volatile_reg,
  697. };
  698. static int sun4i_i2s_runtime_resume(struct device *dev)
  699. {
  700. struct sun4i_i2s *i2s = dev_get_drvdata(dev);
  701. int ret;
  702. ret = clk_prepare_enable(i2s->bus_clk);
  703. if (ret) {
  704. dev_err(dev, "Failed to enable bus clock\n");
  705. return ret;
  706. }
  707. regcache_cache_only(i2s->regmap, false);
  708. regcache_mark_dirty(i2s->regmap);
  709. ret = regcache_sync(i2s->regmap);
  710. if (ret) {
  711. dev_err(dev, "Failed to sync regmap cache\n");
  712. goto err_disable_clk;
  713. }
  714. /* Enable the whole hardware block */
  715. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  716. SUN4I_I2S_CTRL_GL_EN, SUN4I_I2S_CTRL_GL_EN);
  717. /* Enable the first output line */
  718. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  719. SUN4I_I2S_CTRL_SDO_EN_MASK,
  720. SUN4I_I2S_CTRL_SDO_EN(0));
  721. ret = clk_prepare_enable(i2s->mod_clk);
  722. if (ret) {
  723. dev_err(dev, "Failed to enable module clock\n");
  724. goto err_disable_clk;
  725. }
  726. return 0;
  727. err_disable_clk:
  728. clk_disable_unprepare(i2s->bus_clk);
  729. return ret;
  730. }
  731. static int sun4i_i2s_runtime_suspend(struct device *dev)
  732. {
  733. struct sun4i_i2s *i2s = dev_get_drvdata(dev);
  734. clk_disable_unprepare(i2s->mod_clk);
  735. /* Disable our output lines */
  736. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  737. SUN4I_I2S_CTRL_SDO_EN_MASK, 0);
  738. /* Disable the whole hardware block */
  739. regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
  740. SUN4I_I2S_CTRL_GL_EN, 0);
  741. regcache_cache_only(i2s->regmap, true);
  742. clk_disable_unprepare(i2s->bus_clk);
  743. return 0;
  744. }
  745. static const struct sun4i_i2s_quirks sun4i_a10_i2s_quirks = {
  746. .has_reset = false,
  747. .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
  748. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  749. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  750. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  751. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  752. .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6),
  753. .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
  754. .has_slave_select_bit = true,
  755. .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1),
  756. .field_txchanmap = REG_FIELD(SUN4I_I2S_TX_CHAN_MAP_REG, 0, 31),
  757. .field_rxchanmap = REG_FIELD(SUN4I_I2S_RX_CHAN_MAP_REG, 0, 31),
  758. .field_txchansel = REG_FIELD(SUN4I_I2S_TX_CHAN_SEL_REG, 0, 2),
  759. .field_rxchansel = REG_FIELD(SUN4I_I2S_RX_CHAN_SEL_REG, 0, 2),
  760. };
  761. static const struct sun4i_i2s_quirks sun6i_a31_i2s_quirks = {
  762. .has_reset = true,
  763. .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
  764. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  765. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  766. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  767. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  768. .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6),
  769. .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
  770. .has_slave_select_bit = true,
  771. .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1),
  772. .field_txchanmap = REG_FIELD(SUN4I_I2S_TX_CHAN_MAP_REG, 0, 31),
  773. .field_rxchanmap = REG_FIELD(SUN4I_I2S_RX_CHAN_MAP_REG, 0, 31),
  774. .field_txchansel = REG_FIELD(SUN4I_I2S_TX_CHAN_SEL_REG, 0, 2),
  775. .field_rxchansel = REG_FIELD(SUN4I_I2S_RX_CHAN_SEL_REG, 0, 2),
  776. };
  777. static const struct sun4i_i2s_quirks sun8i_a83t_i2s_quirks = {
  778. .has_reset = true,
  779. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  780. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  781. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  782. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  783. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  784. .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6),
  785. .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
  786. .has_slave_select_bit = true,
  787. .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1),
  788. .field_txchanmap = REG_FIELD(SUN4I_I2S_TX_CHAN_MAP_REG, 0, 31),
  789. .field_rxchanmap = REG_FIELD(SUN4I_I2S_RX_CHAN_MAP_REG, 0, 31),
  790. .field_txchansel = REG_FIELD(SUN4I_I2S_TX_CHAN_SEL_REG, 0, 2),
  791. .field_rxchansel = REG_FIELD(SUN4I_I2S_RX_CHAN_SEL_REG, 0, 2),
  792. };
  793. static const struct sun4i_i2s_quirks sun8i_h3_i2s_quirks = {
  794. .has_reset = true,
  795. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  796. .sun4i_i2s_regmap = &sun8i_i2s_regmap_config,
  797. .mclk_offset = 1,
  798. .bclk_offset = 2,
  799. .fmt_offset = 3,
  800. .has_fmt_set_lrck_period = true,
  801. .has_chcfg = true,
  802. .has_chsel_tx_chen = true,
  803. .has_chsel_offset = true,
  804. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 8, 8),
  805. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 2),
  806. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 6),
  807. .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
  808. .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 19, 19),
  809. .field_fmt_mode = REG_FIELD(SUN4I_I2S_CTRL_REG, 4, 5),
  810. .field_txchanmap = REG_FIELD(SUN8I_I2S_TX_CHAN_MAP_REG, 0, 31),
  811. .field_rxchanmap = REG_FIELD(SUN8I_I2S_RX_CHAN_MAP_REG, 0, 31),
  812. .field_txchansel = REG_FIELD(SUN8I_I2S_TX_CHAN_SEL_REG, 0, 2),
  813. .field_rxchansel = REG_FIELD(SUN8I_I2S_RX_CHAN_SEL_REG, 0, 2),
  814. };
  815. static const struct sun4i_i2s_quirks sun50i_a64_codec_i2s_quirks = {
  816. .has_reset = true,
  817. .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
  818. .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
  819. .has_slave_select_bit = true,
  820. .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
  821. .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
  822. .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
  823. .field_fmt_bclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 6, 6),
  824. .field_fmt_lrclk = REG_FIELD(SUN4I_I2S_FMT0_REG, 7, 7),
  825. .field_fmt_mode = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 1),
  826. .field_txchanmap = REG_FIELD(SUN4I_I2S_TX_CHAN_MAP_REG, 0, 31),
  827. .field_rxchanmap = REG_FIELD(SUN4I_I2S_RX_CHAN_MAP_REG, 0, 31),
  828. .field_txchansel = REG_FIELD(SUN4I_I2S_TX_CHAN_SEL_REG, 0, 2),
  829. .field_rxchansel = REG_FIELD(SUN4I_I2S_RX_CHAN_SEL_REG, 0, 2),
  830. };
  831. static int sun4i_i2s_init_regmap_fields(struct device *dev,
  832. struct sun4i_i2s *i2s)
  833. {
  834. i2s->field_clkdiv_mclk_en =
  835. devm_regmap_field_alloc(dev, i2s->regmap,
  836. i2s->variant->field_clkdiv_mclk_en);
  837. if (IS_ERR(i2s->field_clkdiv_mclk_en))
  838. return PTR_ERR(i2s->field_clkdiv_mclk_en);
  839. i2s->field_fmt_wss =
  840. devm_regmap_field_alloc(dev, i2s->regmap,
  841. i2s->variant->field_fmt_wss);
  842. if (IS_ERR(i2s->field_fmt_wss))
  843. return PTR_ERR(i2s->field_fmt_wss);
  844. i2s->field_fmt_sr =
  845. devm_regmap_field_alloc(dev, i2s->regmap,
  846. i2s->variant->field_fmt_sr);
  847. if (IS_ERR(i2s->field_fmt_sr))
  848. return PTR_ERR(i2s->field_fmt_sr);
  849. i2s->field_fmt_bclk =
  850. devm_regmap_field_alloc(dev, i2s->regmap,
  851. i2s->variant->field_fmt_bclk);
  852. if (IS_ERR(i2s->field_fmt_bclk))
  853. return PTR_ERR(i2s->field_fmt_bclk);
  854. i2s->field_fmt_lrclk =
  855. devm_regmap_field_alloc(dev, i2s->regmap,
  856. i2s->variant->field_fmt_lrclk);
  857. if (IS_ERR(i2s->field_fmt_lrclk))
  858. return PTR_ERR(i2s->field_fmt_lrclk);
  859. i2s->field_fmt_mode =
  860. devm_regmap_field_alloc(dev, i2s->regmap,
  861. i2s->variant->field_fmt_mode);
  862. if (IS_ERR(i2s->field_fmt_mode))
  863. return PTR_ERR(i2s->field_fmt_mode);
  864. i2s->field_txchanmap =
  865. devm_regmap_field_alloc(dev, i2s->regmap,
  866. i2s->variant->field_txchanmap);
  867. if (IS_ERR(i2s->field_txchanmap))
  868. return PTR_ERR(i2s->field_txchanmap);
  869. i2s->field_rxchanmap =
  870. devm_regmap_field_alloc(dev, i2s->regmap,
  871. i2s->variant->field_rxchanmap);
  872. if (IS_ERR(i2s->field_rxchanmap))
  873. return PTR_ERR(i2s->field_rxchanmap);
  874. i2s->field_txchansel =
  875. devm_regmap_field_alloc(dev, i2s->regmap,
  876. i2s->variant->field_txchansel);
  877. if (IS_ERR(i2s->field_txchansel))
  878. return PTR_ERR(i2s->field_txchansel);
  879. i2s->field_rxchansel =
  880. devm_regmap_field_alloc(dev, i2s->regmap,
  881. i2s->variant->field_rxchansel);
  882. return PTR_ERR_OR_ZERO(i2s->field_rxchansel);
  883. }
  884. static int sun4i_i2s_probe(struct platform_device *pdev)
  885. {
  886. struct sun4i_i2s *i2s;
  887. struct resource *res;
  888. void __iomem *regs;
  889. int irq, ret;
  890. i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
  891. if (!i2s)
  892. return -ENOMEM;
  893. platform_set_drvdata(pdev, i2s);
  894. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  895. regs = devm_ioremap_resource(&pdev->dev, res);
  896. if (IS_ERR(regs))
  897. return PTR_ERR(regs);
  898. irq = platform_get_irq(pdev, 0);
  899. if (irq < 0) {
  900. dev_err(&pdev->dev, "Can't retrieve our interrupt\n");
  901. return irq;
  902. }
  903. i2s->variant = of_device_get_match_data(&pdev->dev);
  904. if (!i2s->variant) {
  905. dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
  906. return -ENODEV;
  907. }
  908. i2s->bus_clk = devm_clk_get(&pdev->dev, "apb");
  909. if (IS_ERR(i2s->bus_clk)) {
  910. dev_err(&pdev->dev, "Can't get our bus clock\n");
  911. return PTR_ERR(i2s->bus_clk);
  912. }
  913. i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
  914. i2s->variant->sun4i_i2s_regmap);
  915. if (IS_ERR(i2s->regmap)) {
  916. dev_err(&pdev->dev, "Regmap initialisation failed\n");
  917. return PTR_ERR(i2s->regmap);
  918. }
  919. i2s->mod_clk = devm_clk_get(&pdev->dev, "mod");
  920. if (IS_ERR(i2s->mod_clk)) {
  921. dev_err(&pdev->dev, "Can't get our mod clock\n");
  922. return PTR_ERR(i2s->mod_clk);
  923. }
  924. if (i2s->variant->has_reset) {
  925. i2s->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  926. if (IS_ERR(i2s->rst)) {
  927. dev_err(&pdev->dev, "Failed to get reset control\n");
  928. return PTR_ERR(i2s->rst);
  929. }
  930. }
  931. if (!IS_ERR(i2s->rst)) {
  932. ret = reset_control_deassert(i2s->rst);
  933. if (ret) {
  934. dev_err(&pdev->dev,
  935. "Failed to deassert the reset control\n");
  936. return -EINVAL;
  937. }
  938. }
  939. i2s->playback_dma_data.addr = res->start +
  940. i2s->variant->reg_offset_txdata;
  941. i2s->playback_dma_data.maxburst = 8;
  942. i2s->capture_dma_data.addr = res->start + SUN4I_I2S_FIFO_RX_REG;
  943. i2s->capture_dma_data.maxburst = 8;
  944. pm_runtime_enable(&pdev->dev);
  945. if (!pm_runtime_enabled(&pdev->dev)) {
  946. ret = sun4i_i2s_runtime_resume(&pdev->dev);
  947. if (ret)
  948. goto err_pm_disable;
  949. }
  950. ret = devm_snd_soc_register_component(&pdev->dev,
  951. &sun4i_i2s_component,
  952. &sun4i_i2s_dai, 1);
  953. if (ret) {
  954. dev_err(&pdev->dev, "Could not register DAI\n");
  955. goto err_suspend;
  956. }
  957. ret = snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
  958. if (ret) {
  959. dev_err(&pdev->dev, "Could not register PCM\n");
  960. goto err_suspend;
  961. }
  962. ret = sun4i_i2s_init_regmap_fields(&pdev->dev, i2s);
  963. if (ret) {
  964. dev_err(&pdev->dev, "Could not initialise regmap fields\n");
  965. goto err_suspend;
  966. }
  967. return 0;
  968. err_suspend:
  969. if (!pm_runtime_status_suspended(&pdev->dev))
  970. sun4i_i2s_runtime_suspend(&pdev->dev);
  971. err_pm_disable:
  972. pm_runtime_disable(&pdev->dev);
  973. if (!IS_ERR(i2s->rst))
  974. reset_control_assert(i2s->rst);
  975. return ret;
  976. }
  977. static int sun4i_i2s_remove(struct platform_device *pdev)
  978. {
  979. struct sun4i_i2s *i2s = dev_get_drvdata(&pdev->dev);
  980. snd_dmaengine_pcm_unregister(&pdev->dev);
  981. pm_runtime_disable(&pdev->dev);
  982. if (!pm_runtime_status_suspended(&pdev->dev))
  983. sun4i_i2s_runtime_suspend(&pdev->dev);
  984. if (!IS_ERR(i2s->rst))
  985. reset_control_assert(i2s->rst);
  986. return 0;
  987. }
  988. static const struct of_device_id sun4i_i2s_match[] = {
  989. {
  990. .compatible = "allwinner,sun4i-a10-i2s",
  991. .data = &sun4i_a10_i2s_quirks,
  992. },
  993. {
  994. .compatible = "allwinner,sun6i-a31-i2s",
  995. .data = &sun6i_a31_i2s_quirks,
  996. },
  997. {
  998. .compatible = "allwinner,sun8i-a83t-i2s",
  999. .data = &sun8i_a83t_i2s_quirks,
  1000. },
  1001. {
  1002. .compatible = "allwinner,sun8i-h3-i2s",
  1003. .data = &sun8i_h3_i2s_quirks,
  1004. },
  1005. {
  1006. .compatible = "allwinner,sun50i-a64-codec-i2s",
  1007. .data = &sun50i_a64_codec_i2s_quirks,
  1008. },
  1009. {}
  1010. };
  1011. MODULE_DEVICE_TABLE(of, sun4i_i2s_match);
  1012. static const struct dev_pm_ops sun4i_i2s_pm_ops = {
  1013. .runtime_resume = sun4i_i2s_runtime_resume,
  1014. .runtime_suspend = sun4i_i2s_runtime_suspend,
  1015. };
  1016. static struct platform_driver sun4i_i2s_driver = {
  1017. .probe = sun4i_i2s_probe,
  1018. .remove = sun4i_i2s_remove,
  1019. .driver = {
  1020. .name = "sun4i-i2s",
  1021. .of_match_table = sun4i_i2s_match,
  1022. .pm = &sun4i_i2s_pm_ops,
  1023. },
  1024. };
  1025. module_platform_driver(sun4i_i2s_driver);
  1026. MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
  1027. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  1028. MODULE_DESCRIPTION("Allwinner A10 I2S driver");
  1029. MODULE_LICENSE("GPL");