rt5514.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /*
  2. * rt5514.c -- RT5514 ALSA SoC audio codec driver
  3. *
  4. * Copyright 2015 Realtek Semiconductor Corp.
  5. * Author: Oder Chiou <oder_chiou@realtek.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/regmap.h>
  18. #include <linux/i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/firmware.h>
  21. #include <linux/gpio.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <sound/initval.h>
  28. #include <sound/tlv.h>
  29. #include "rl6231.h"
  30. #include "rt5514.h"
  31. #if defined(CONFIG_SND_SOC_RT5514_SPI)
  32. #include "rt5514-spi.h"
  33. #endif
  34. static const struct reg_sequence rt5514_i2c_patch[] = {
  35. {0x1800101c, 0x00000000},
  36. {0x18001100, 0x0000031f},
  37. {0x18001104, 0x00000007},
  38. {0x18001108, 0x00000000},
  39. {0x1800110c, 0x00000000},
  40. {0x18001110, 0x00000000},
  41. {0x18001114, 0x00000001},
  42. {0x18001118, 0x00000000},
  43. {0x18002f08, 0x00000006},
  44. {0x18002f00, 0x00055149},
  45. {0x18002f00, 0x0005514b},
  46. {0x18002f00, 0x00055149},
  47. {0xfafafafa, 0x00000001},
  48. {0x18002f10, 0x00000001},
  49. {0x18002f10, 0x00000000},
  50. {0x18002f10, 0x00000001},
  51. {0xfafafafa, 0x00000001},
  52. {0x18002000, 0x000010ec},
  53. {0xfafafafa, 0x00000000},
  54. };
  55. static const struct reg_sequence rt5514_patch[] = {
  56. {RT5514_DIG_IO_CTRL, 0x00000040},
  57. {RT5514_CLK_CTRL1, 0x38020041},
  58. {RT5514_SRC_CTRL, 0x44000eee},
  59. {RT5514_ANA_CTRL_LDO10, 0x00028604},
  60. {RT5514_ANA_CTRL_ADCFED, 0x00000800},
  61. };
  62. static const struct reg_default rt5514_reg[] = {
  63. {RT5514_RESET, 0x00000000},
  64. {RT5514_PWR_ANA1, 0x00808880},
  65. {RT5514_PWR_ANA2, 0x00220000},
  66. {RT5514_I2S_CTRL1, 0x00000330},
  67. {RT5514_I2S_CTRL2, 0x20000000},
  68. {RT5514_VAD_CTRL6, 0xc00007d2},
  69. {RT5514_EXT_VAD_CTRL, 0x80000080},
  70. {RT5514_DIG_IO_CTRL, 0x00000040},
  71. {RT5514_PAD_CTRL1, 0x00804000},
  72. {RT5514_DMIC_DATA_CTRL, 0x00000005},
  73. {RT5514_DIG_SOURCE_CTRL, 0x00000002},
  74. {RT5514_SRC_CTRL, 0x44000eee},
  75. {RT5514_DOWNFILTER2_CTRL1, 0x0000882f},
  76. {RT5514_PLL_SOURCE_CTRL, 0x00000004},
  77. {RT5514_CLK_CTRL1, 0x38020041},
  78. {RT5514_CLK_CTRL2, 0x00000000},
  79. {RT5514_PLL3_CALIB_CTRL1, 0x00400200},
  80. {RT5514_PLL3_CALIB_CTRL5, 0x40220012},
  81. {RT5514_DELAY_BUF_CTRL1, 0x7fff006a},
  82. {RT5514_DELAY_BUF_CTRL3, 0x00000000},
  83. {RT5514_DOWNFILTER0_CTRL1, 0x00020c2f},
  84. {RT5514_DOWNFILTER0_CTRL2, 0x00020c2f},
  85. {RT5514_DOWNFILTER0_CTRL3, 0x00000362},
  86. {RT5514_DOWNFILTER1_CTRL1, 0x00020c2f},
  87. {RT5514_DOWNFILTER1_CTRL2, 0x00020c2f},
  88. {RT5514_DOWNFILTER1_CTRL3, 0x00000362},
  89. {RT5514_ANA_CTRL_LDO10, 0x00028604},
  90. {RT5514_ANA_CTRL_LDO18_16, 0x02000345},
  91. {RT5514_ANA_CTRL_ADC12, 0x0000a2a8},
  92. {RT5514_ANA_CTRL_ADC21, 0x00001180},
  93. {RT5514_ANA_CTRL_ADC22, 0x0000aaa8},
  94. {RT5514_ANA_CTRL_ADC23, 0x00151427},
  95. {RT5514_ANA_CTRL_MICBST, 0x00002000},
  96. {RT5514_ANA_CTRL_ADCFED, 0x00000800},
  97. {RT5514_ANA_CTRL_INBUF, 0x00000143},
  98. {RT5514_ANA_CTRL_VREF, 0x00008d50},
  99. {RT5514_ANA_CTRL_PLL3, 0x0000000e},
  100. {RT5514_ANA_CTRL_PLL1_1, 0x00000000},
  101. {RT5514_ANA_CTRL_PLL1_2, 0x00030220},
  102. {RT5514_DMIC_LP_CTRL, 0x00000000},
  103. {RT5514_MISC_CTRL_DSP, 0x00000000},
  104. {RT5514_DSP_CTRL1, 0x00055149},
  105. {RT5514_DSP_CTRL3, 0x00000006},
  106. {RT5514_DSP_CTRL4, 0x00000001},
  107. {RT5514_VENDOR_ID1, 0x00000001},
  108. {RT5514_VENDOR_ID2, 0x10ec5514},
  109. };
  110. static void rt5514_enable_dsp_prepare(struct rt5514_priv *rt5514)
  111. {
  112. /* Reset */
  113. regmap_write(rt5514->i2c_regmap, 0x18002000, 0x000010ec);
  114. /* LDO_I_limit */
  115. regmap_write(rt5514->i2c_regmap, 0x18002200, 0x00028604);
  116. /* I2C bypass enable */
  117. regmap_write(rt5514->i2c_regmap, 0xfafafafa, 0x00000001);
  118. /* mini-core reset */
  119. regmap_write(rt5514->i2c_regmap, 0x18002f00, 0x0005514b);
  120. regmap_write(rt5514->i2c_regmap, 0x18002f00, 0x00055149);
  121. /* I2C bypass disable */
  122. regmap_write(rt5514->i2c_regmap, 0xfafafafa, 0x00000000);
  123. /* PIN config */
  124. regmap_write(rt5514->i2c_regmap, 0x18002070, 0x00000040);
  125. /* PLL3(QN)=RCOSC*(10+2) */
  126. regmap_write(rt5514->i2c_regmap, 0x18002240, 0x0000000a);
  127. /* PLL3 source=RCOSC, fsi=rt_clk */
  128. regmap_write(rt5514->i2c_regmap, 0x18002100, 0x0000000b);
  129. /* Power on RCOSC, pll3 */
  130. regmap_write(rt5514->i2c_regmap, 0x18002004, 0x00808b81);
  131. /* DSP clk source = pll3, ENABLE DSP clk */
  132. regmap_write(rt5514->i2c_regmap, 0x18002f08, 0x00000005);
  133. /* Enable DSP clk auto switch */
  134. regmap_write(rt5514->i2c_regmap, 0x18001114, 0x00000001);
  135. /* Reduce DSP power */
  136. regmap_write(rt5514->i2c_regmap, 0x18001118, 0x00000001);
  137. }
  138. static bool rt5514_volatile_register(struct device *dev, unsigned int reg)
  139. {
  140. switch (reg) {
  141. case RT5514_VENDOR_ID1:
  142. case RT5514_VENDOR_ID2:
  143. return true;
  144. default:
  145. return false;
  146. }
  147. }
  148. static bool rt5514_readable_register(struct device *dev, unsigned int reg)
  149. {
  150. switch (reg) {
  151. case RT5514_RESET:
  152. case RT5514_PWR_ANA1:
  153. case RT5514_PWR_ANA2:
  154. case RT5514_I2S_CTRL1:
  155. case RT5514_I2S_CTRL2:
  156. case RT5514_VAD_CTRL6:
  157. case RT5514_EXT_VAD_CTRL:
  158. case RT5514_DIG_IO_CTRL:
  159. case RT5514_PAD_CTRL1:
  160. case RT5514_DMIC_DATA_CTRL:
  161. case RT5514_DIG_SOURCE_CTRL:
  162. case RT5514_SRC_CTRL:
  163. case RT5514_DOWNFILTER2_CTRL1:
  164. case RT5514_PLL_SOURCE_CTRL:
  165. case RT5514_CLK_CTRL1:
  166. case RT5514_CLK_CTRL2:
  167. case RT5514_PLL3_CALIB_CTRL1:
  168. case RT5514_PLL3_CALIB_CTRL5:
  169. case RT5514_DELAY_BUF_CTRL1:
  170. case RT5514_DELAY_BUF_CTRL3:
  171. case RT5514_DOWNFILTER0_CTRL1:
  172. case RT5514_DOWNFILTER0_CTRL2:
  173. case RT5514_DOWNFILTER0_CTRL3:
  174. case RT5514_DOWNFILTER1_CTRL1:
  175. case RT5514_DOWNFILTER1_CTRL2:
  176. case RT5514_DOWNFILTER1_CTRL3:
  177. case RT5514_ANA_CTRL_LDO10:
  178. case RT5514_ANA_CTRL_LDO18_16:
  179. case RT5514_ANA_CTRL_ADC12:
  180. case RT5514_ANA_CTRL_ADC21:
  181. case RT5514_ANA_CTRL_ADC22:
  182. case RT5514_ANA_CTRL_ADC23:
  183. case RT5514_ANA_CTRL_MICBST:
  184. case RT5514_ANA_CTRL_ADCFED:
  185. case RT5514_ANA_CTRL_INBUF:
  186. case RT5514_ANA_CTRL_VREF:
  187. case RT5514_ANA_CTRL_PLL3:
  188. case RT5514_ANA_CTRL_PLL1_1:
  189. case RT5514_ANA_CTRL_PLL1_2:
  190. case RT5514_DMIC_LP_CTRL:
  191. case RT5514_MISC_CTRL_DSP:
  192. case RT5514_DSP_CTRL1:
  193. case RT5514_DSP_CTRL3:
  194. case RT5514_DSP_CTRL4:
  195. case RT5514_VENDOR_ID1:
  196. case RT5514_VENDOR_ID2:
  197. return true;
  198. default:
  199. return false;
  200. }
  201. }
  202. static bool rt5514_i2c_readable_register(struct device *dev,
  203. unsigned int reg)
  204. {
  205. switch (reg) {
  206. case RT5514_DSP_MAPPING | RT5514_RESET:
  207. case RT5514_DSP_MAPPING | RT5514_PWR_ANA1:
  208. case RT5514_DSP_MAPPING | RT5514_PWR_ANA2:
  209. case RT5514_DSP_MAPPING | RT5514_I2S_CTRL1:
  210. case RT5514_DSP_MAPPING | RT5514_I2S_CTRL2:
  211. case RT5514_DSP_MAPPING | RT5514_VAD_CTRL6:
  212. case RT5514_DSP_MAPPING | RT5514_EXT_VAD_CTRL:
  213. case RT5514_DSP_MAPPING | RT5514_DIG_IO_CTRL:
  214. case RT5514_DSP_MAPPING | RT5514_PAD_CTRL1:
  215. case RT5514_DSP_MAPPING | RT5514_DMIC_DATA_CTRL:
  216. case RT5514_DSP_MAPPING | RT5514_DIG_SOURCE_CTRL:
  217. case RT5514_DSP_MAPPING | RT5514_SRC_CTRL:
  218. case RT5514_DSP_MAPPING | RT5514_DOWNFILTER2_CTRL1:
  219. case RT5514_DSP_MAPPING | RT5514_PLL_SOURCE_CTRL:
  220. case RT5514_DSP_MAPPING | RT5514_CLK_CTRL1:
  221. case RT5514_DSP_MAPPING | RT5514_CLK_CTRL2:
  222. case RT5514_DSP_MAPPING | RT5514_PLL3_CALIB_CTRL1:
  223. case RT5514_DSP_MAPPING | RT5514_PLL3_CALIB_CTRL5:
  224. case RT5514_DSP_MAPPING | RT5514_DELAY_BUF_CTRL1:
  225. case RT5514_DSP_MAPPING | RT5514_DELAY_BUF_CTRL3:
  226. case RT5514_DSP_MAPPING | RT5514_DOWNFILTER0_CTRL1:
  227. case RT5514_DSP_MAPPING | RT5514_DOWNFILTER0_CTRL2:
  228. case RT5514_DSP_MAPPING | RT5514_DOWNFILTER0_CTRL3:
  229. case RT5514_DSP_MAPPING | RT5514_DOWNFILTER1_CTRL1:
  230. case RT5514_DSP_MAPPING | RT5514_DOWNFILTER1_CTRL2:
  231. case RT5514_DSP_MAPPING | RT5514_DOWNFILTER1_CTRL3:
  232. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_LDO10:
  233. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_LDO18_16:
  234. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC12:
  235. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC21:
  236. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC22:
  237. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADC23:
  238. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_MICBST:
  239. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_ADCFED:
  240. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_INBUF:
  241. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_VREF:
  242. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_PLL3:
  243. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_PLL1_1:
  244. case RT5514_DSP_MAPPING | RT5514_ANA_CTRL_PLL1_2:
  245. case RT5514_DSP_MAPPING | RT5514_DMIC_LP_CTRL:
  246. case RT5514_DSP_MAPPING | RT5514_MISC_CTRL_DSP:
  247. case RT5514_DSP_MAPPING | RT5514_DSP_CTRL1:
  248. case RT5514_DSP_MAPPING | RT5514_DSP_CTRL3:
  249. case RT5514_DSP_MAPPING | RT5514_DSP_CTRL4:
  250. case RT5514_DSP_MAPPING | RT5514_VENDOR_ID1:
  251. case RT5514_DSP_MAPPING | RT5514_VENDOR_ID2:
  252. return true;
  253. default:
  254. return false;
  255. }
  256. }
  257. /* {-3, 0, +3, +4.5, +7.5, +9.5, +12, +14, +17} dB */
  258. static const DECLARE_TLV_DB_RANGE(bst_tlv,
  259. 0, 2, TLV_DB_SCALE_ITEM(-300, 300, 0),
  260. 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
  261. 4, 4, TLV_DB_SCALE_ITEM(750, 0, 0),
  262. 5, 5, TLV_DB_SCALE_ITEM(950, 0, 0),
  263. 6, 6, TLV_DB_SCALE_ITEM(1200, 0, 0),
  264. 7, 7, TLV_DB_SCALE_ITEM(1400, 0, 0),
  265. 8, 8, TLV_DB_SCALE_ITEM(1700, 0, 0)
  266. );
  267. static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -1725, 75, 0);
  268. static int rt5514_dsp_voice_wake_up_get(struct snd_kcontrol *kcontrol,
  269. struct snd_ctl_elem_value *ucontrol)
  270. {
  271. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  272. struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
  273. ucontrol->value.integer.value[0] = rt5514->dsp_enabled;
  274. return 0;
  275. }
  276. static int rt5514_dsp_voice_wake_up_put(struct snd_kcontrol *kcontrol,
  277. struct snd_ctl_elem_value *ucontrol)
  278. {
  279. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  280. struct rt5514_priv *rt5514 = snd_soc_component_get_drvdata(component);
  281. struct snd_soc_codec *codec = rt5514->codec;
  282. const struct firmware *fw = NULL;
  283. if (ucontrol->value.integer.value[0] == rt5514->dsp_enabled)
  284. return 0;
  285. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  286. rt5514->dsp_enabled = ucontrol->value.integer.value[0];
  287. if (rt5514->dsp_enabled) {
  288. rt5514_enable_dsp_prepare(rt5514);
  289. reject_firmware(&fw, RT5514_FIRMWARE1, codec->dev);
  290. if (fw) {
  291. #if defined(CONFIG_SND_SOC_RT5514_SPI)
  292. rt5514_spi_burst_write(0x4ff60000, fw->data,
  293. ((fw->size/8)+1)*8);
  294. #else
  295. dev_err(codec->dev, "There is no SPI driver for"
  296. " loading the firmware\n");
  297. #endif
  298. release_firmware(fw);
  299. fw = NULL;
  300. }
  301. reject_firmware(&fw, RT5514_FIRMWARE2, codec->dev);
  302. if (fw) {
  303. #if defined(CONFIG_SND_SOC_RT5514_SPI)
  304. rt5514_spi_burst_write(0x4ffc0000, fw->data,
  305. ((fw->size/8)+1)*8);
  306. #else
  307. dev_err(codec->dev, "There is no SPI driver for"
  308. " loading the firmware\n");
  309. #endif
  310. release_firmware(fw);
  311. fw = NULL;
  312. }
  313. /* DSP run */
  314. regmap_write(rt5514->i2c_regmap, 0x18002f00,
  315. 0x00055148);
  316. } else {
  317. regmap_multi_reg_write(rt5514->i2c_regmap,
  318. rt5514_i2c_patch, ARRAY_SIZE(rt5514_i2c_patch));
  319. regcache_mark_dirty(rt5514->regmap);
  320. regcache_sync(rt5514->regmap);
  321. }
  322. }
  323. return 0;
  324. }
  325. static const struct snd_kcontrol_new rt5514_snd_controls[] = {
  326. SOC_DOUBLE_TLV("MIC Boost Volume", RT5514_ANA_CTRL_MICBST,
  327. RT5514_SEL_BSTL_SFT, RT5514_SEL_BSTR_SFT, 8, 0, bst_tlv),
  328. SOC_DOUBLE_R_TLV("ADC1 Capture Volume", RT5514_DOWNFILTER0_CTRL1,
  329. RT5514_DOWNFILTER0_CTRL2, RT5514_AD_GAIN_SFT, 63, 0,
  330. adc_vol_tlv),
  331. SOC_DOUBLE_R_TLV("ADC2 Capture Volume", RT5514_DOWNFILTER1_CTRL1,
  332. RT5514_DOWNFILTER1_CTRL2, RT5514_AD_GAIN_SFT, 63, 0,
  333. adc_vol_tlv),
  334. SOC_SINGLE_EXT("DSP Voice Wake Up", SND_SOC_NOPM, 0, 1, 0,
  335. rt5514_dsp_voice_wake_up_get, rt5514_dsp_voice_wake_up_put),
  336. };
  337. /* ADC Mixer*/
  338. static const struct snd_kcontrol_new rt5514_sto1_adc_l_mix[] = {
  339. SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER0_CTRL1,
  340. RT5514_AD_DMIC_MIX_BIT, 1, 1),
  341. SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER0_CTRL1,
  342. RT5514_AD_AD_MIX_BIT, 1, 1),
  343. };
  344. static const struct snd_kcontrol_new rt5514_sto1_adc_r_mix[] = {
  345. SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER0_CTRL2,
  346. RT5514_AD_DMIC_MIX_BIT, 1, 1),
  347. SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER0_CTRL2,
  348. RT5514_AD_AD_MIX_BIT, 1, 1),
  349. };
  350. static const struct snd_kcontrol_new rt5514_sto2_adc_l_mix[] = {
  351. SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER1_CTRL1,
  352. RT5514_AD_DMIC_MIX_BIT, 1, 1),
  353. SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER1_CTRL1,
  354. RT5514_AD_AD_MIX_BIT, 1, 1),
  355. };
  356. static const struct snd_kcontrol_new rt5514_sto2_adc_r_mix[] = {
  357. SOC_DAPM_SINGLE("DMIC Switch", RT5514_DOWNFILTER1_CTRL2,
  358. RT5514_AD_DMIC_MIX_BIT, 1, 1),
  359. SOC_DAPM_SINGLE("ADC Switch", RT5514_DOWNFILTER1_CTRL2,
  360. RT5514_AD_AD_MIX_BIT, 1, 1),
  361. };
  362. /* DMIC Source */
  363. static const char * const rt5514_dmic_src[] = {
  364. "DMIC1", "DMIC2"
  365. };
  366. static SOC_ENUM_SINGLE_DECL(
  367. rt5514_stereo1_dmic_enum, RT5514_DIG_SOURCE_CTRL,
  368. RT5514_AD0_DMIC_INPUT_SEL_SFT, rt5514_dmic_src);
  369. static const struct snd_kcontrol_new rt5514_sto1_dmic_mux =
  370. SOC_DAPM_ENUM("Stereo1 DMIC Source", rt5514_stereo1_dmic_enum);
  371. static SOC_ENUM_SINGLE_DECL(
  372. rt5514_stereo2_dmic_enum, RT5514_DIG_SOURCE_CTRL,
  373. RT5514_AD1_DMIC_INPUT_SEL_SFT, rt5514_dmic_src);
  374. static const struct snd_kcontrol_new rt5514_sto2_dmic_mux =
  375. SOC_DAPM_ENUM("Stereo2 DMIC Source", rt5514_stereo2_dmic_enum);
  376. /**
  377. * rt5514_calc_dmic_clk - Calculate the frequency divider parameter of dmic.
  378. *
  379. * @rate: base clock rate.
  380. *
  381. * Choose divider parameter that gives the highest possible DMIC frequency in
  382. * 1MHz - 3MHz range.
  383. */
  384. static int rt5514_calc_dmic_clk(struct snd_soc_codec *codec, int rate)
  385. {
  386. int div[] = {2, 3, 4, 8, 12, 16, 24, 32};
  387. int i;
  388. if (rate < 1000000 * div[0]) {
  389. pr_warn("Base clock rate %d is too low\n", rate);
  390. return -EINVAL;
  391. }
  392. for (i = 0; i < ARRAY_SIZE(div); i++) {
  393. /* find divider that gives DMIC frequency below 3.072MHz */
  394. if (3072000 * div[i] >= rate)
  395. return i;
  396. }
  397. dev_warn(codec->dev, "Base clock rate %d is too high\n", rate);
  398. return -EINVAL;
  399. }
  400. static int rt5514_set_dmic_clk(struct snd_soc_dapm_widget *w,
  401. struct snd_kcontrol *kcontrol, int event)
  402. {
  403. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  404. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  405. int idx;
  406. idx = rt5514_calc_dmic_clk(codec, rt5514->sysclk);
  407. if (idx < 0)
  408. dev_err(codec->dev, "Failed to set DMIC clock\n");
  409. else
  410. regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL1,
  411. RT5514_CLK_DMIC_OUT_SEL_MASK,
  412. idx << RT5514_CLK_DMIC_OUT_SEL_SFT);
  413. return idx;
  414. }
  415. static int rt5514_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
  416. struct snd_soc_dapm_widget *sink)
  417. {
  418. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  419. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  420. if (rt5514->sysclk_src == RT5514_SCLK_S_PLL1)
  421. return 1;
  422. else
  423. return 0;
  424. }
  425. static int rt5514_pre_event(struct snd_soc_dapm_widget *w,
  426. struct snd_kcontrol *kcontrol, int event)
  427. {
  428. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  429. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  430. switch (event) {
  431. case SND_SOC_DAPM_PRE_PMU:
  432. /**
  433. * If the DSP is enabled in start of recording, the DSP
  434. * should be disabled, and sync back to normal recording
  435. * settings to make sure recording properly.
  436. */
  437. if (rt5514->dsp_enabled) {
  438. rt5514->dsp_enabled = 0;
  439. regmap_multi_reg_write(rt5514->i2c_regmap,
  440. rt5514_i2c_patch, ARRAY_SIZE(rt5514_i2c_patch));
  441. regcache_mark_dirty(rt5514->regmap);
  442. regcache_sync(rt5514->regmap);
  443. }
  444. break;
  445. default:
  446. return 0;
  447. }
  448. return 0;
  449. }
  450. static const struct snd_soc_dapm_widget rt5514_dapm_widgets[] = {
  451. /* Input Lines */
  452. SND_SOC_DAPM_INPUT("DMIC1L"),
  453. SND_SOC_DAPM_INPUT("DMIC1R"),
  454. SND_SOC_DAPM_INPUT("DMIC2L"),
  455. SND_SOC_DAPM_INPUT("DMIC2R"),
  456. SND_SOC_DAPM_INPUT("AMICL"),
  457. SND_SOC_DAPM_INPUT("AMICR"),
  458. SND_SOC_DAPM_PGA("DMIC1", SND_SOC_NOPM, 0, 0, NULL, 0),
  459. SND_SOC_DAPM_PGA("DMIC2", SND_SOC_NOPM, 0, 0, NULL, 0),
  460. SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
  461. rt5514_set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
  462. SND_SOC_DAPM_SUPPLY("ADC CLK", RT5514_CLK_CTRL1,
  463. RT5514_CLK_AD_ANA1_EN_BIT, 0, NULL, 0),
  464. SND_SOC_DAPM_SUPPLY("LDO18 IN", RT5514_PWR_ANA1,
  465. RT5514_POW_LDO18_IN_BIT, 0, NULL, 0),
  466. SND_SOC_DAPM_SUPPLY("LDO18 ADC", RT5514_PWR_ANA1,
  467. RT5514_POW_LDO18_ADC_BIT, 0, NULL, 0),
  468. SND_SOC_DAPM_SUPPLY("LDO21", RT5514_PWR_ANA1, RT5514_POW_LDO21_BIT, 0,
  469. NULL, 0),
  470. SND_SOC_DAPM_SUPPLY("BG LDO18 IN", RT5514_PWR_ANA1,
  471. RT5514_POW_BG_LDO18_IN_BIT, 0, NULL, 0),
  472. SND_SOC_DAPM_SUPPLY("BG LDO21", RT5514_PWR_ANA1,
  473. RT5514_POW_BG_LDO21_BIT, 0, NULL, 0),
  474. SND_SOC_DAPM_SUPPLY("BG MBIAS", RT5514_PWR_ANA2,
  475. RT5514_POW_BG_MBIAS_BIT, 0, NULL, 0),
  476. SND_SOC_DAPM_SUPPLY("MBIAS", RT5514_PWR_ANA2, RT5514_POW_MBIAS_BIT, 0,
  477. NULL, 0),
  478. SND_SOC_DAPM_SUPPLY("VREF2", RT5514_PWR_ANA2, RT5514_POW_VREF2_BIT, 0,
  479. NULL, 0),
  480. SND_SOC_DAPM_SUPPLY("VREF1", RT5514_PWR_ANA2, RT5514_POW_VREF1_BIT, 0,
  481. NULL, 0),
  482. SND_SOC_DAPM_SUPPLY("ADC Power", SND_SOC_NOPM, 0, 0, NULL, 0),
  483. SND_SOC_DAPM_SUPPLY("LDO16L", RT5514_PWR_ANA2, RT5514_POWL_LDO16_BIT, 0,
  484. NULL, 0),
  485. SND_SOC_DAPM_SUPPLY("ADC1L", RT5514_PWR_ANA2, RT5514_POW_ADC1_L_BIT, 0,
  486. NULL, 0),
  487. SND_SOC_DAPM_SUPPLY("BSTL2", RT5514_PWR_ANA2, RT5514_POW2_BSTL_BIT, 0,
  488. NULL, 0),
  489. SND_SOC_DAPM_SUPPLY("BSTL", RT5514_PWR_ANA2, RT5514_POW_BSTL_BIT, 0,
  490. NULL, 0),
  491. SND_SOC_DAPM_SUPPLY("ADCFEDL", RT5514_PWR_ANA2, RT5514_POW_ADCFEDL_BIT,
  492. 0, NULL, 0),
  493. SND_SOC_DAPM_SUPPLY("ADCL Power", SND_SOC_NOPM, 0, 0, NULL, 0),
  494. SND_SOC_DAPM_SUPPLY("LDO16R", RT5514_PWR_ANA2, RT5514_POWR_LDO16_BIT, 0,
  495. NULL, 0),
  496. SND_SOC_DAPM_SUPPLY("ADC1R", RT5514_PWR_ANA2, RT5514_POW_ADC1_R_BIT, 0,
  497. NULL, 0),
  498. SND_SOC_DAPM_SUPPLY("BSTR2", RT5514_PWR_ANA2, RT5514_POW2_BSTR_BIT, 0,
  499. NULL, 0),
  500. SND_SOC_DAPM_SUPPLY("BSTR", RT5514_PWR_ANA2, RT5514_POW_BSTR_BIT, 0,
  501. NULL, 0),
  502. SND_SOC_DAPM_SUPPLY("ADCFEDR", RT5514_PWR_ANA2, RT5514_POW_ADCFEDR_BIT,
  503. 0, NULL, 0),
  504. SND_SOC_DAPM_SUPPLY("ADCR Power", SND_SOC_NOPM, 0, 0, NULL, 0),
  505. SND_SOC_DAPM_SUPPLY("PLL1 LDO ENABLE", RT5514_ANA_CTRL_PLL1_2,
  506. RT5514_EN_LDO_PLL1_BIT, 0, NULL, 0),
  507. SND_SOC_DAPM_SUPPLY("PLL1 LDO", RT5514_PWR_ANA2,
  508. RT5514_POW_PLL1_LDO_BIT, 0, NULL, 0),
  509. SND_SOC_DAPM_SUPPLY("PLL1", RT5514_PWR_ANA2, RT5514_POW_PLL1_BIT, 0,
  510. NULL, 0),
  511. /* ADC Mux */
  512. SND_SOC_DAPM_MUX("Stereo1 DMIC Mux", SND_SOC_NOPM, 0, 0,
  513. &rt5514_sto1_dmic_mux),
  514. SND_SOC_DAPM_MUX("Stereo2 DMIC Mux", SND_SOC_NOPM, 0, 0,
  515. &rt5514_sto2_dmic_mux),
  516. /* ADC Mixer */
  517. SND_SOC_DAPM_SUPPLY("adc stereo1 filter", RT5514_CLK_CTRL1,
  518. RT5514_CLK_AD0_EN_BIT, 0, NULL, 0),
  519. SND_SOC_DAPM_SUPPLY("adc stereo2 filter", RT5514_CLK_CTRL1,
  520. RT5514_CLK_AD1_EN_BIT, 0, NULL, 0),
  521. SND_SOC_DAPM_MIXER("Sto1 ADC MIXL", SND_SOC_NOPM, 0, 0,
  522. rt5514_sto1_adc_l_mix, ARRAY_SIZE(rt5514_sto1_adc_l_mix)),
  523. SND_SOC_DAPM_MIXER("Sto1 ADC MIXR", SND_SOC_NOPM, 0, 0,
  524. rt5514_sto1_adc_r_mix, ARRAY_SIZE(rt5514_sto1_adc_r_mix)),
  525. SND_SOC_DAPM_MIXER("Sto2 ADC MIXL", SND_SOC_NOPM, 0, 0,
  526. rt5514_sto2_adc_l_mix, ARRAY_SIZE(rt5514_sto2_adc_l_mix)),
  527. SND_SOC_DAPM_MIXER("Sto2 ADC MIXR", SND_SOC_NOPM, 0, 0,
  528. rt5514_sto2_adc_r_mix, ARRAY_SIZE(rt5514_sto2_adc_r_mix)),
  529. SND_SOC_DAPM_ADC("Stereo1 ADC MIXL", NULL, RT5514_DOWNFILTER0_CTRL1,
  530. RT5514_AD_AD_MUTE_BIT, 1),
  531. SND_SOC_DAPM_ADC("Stereo1 ADC MIXR", NULL, RT5514_DOWNFILTER0_CTRL2,
  532. RT5514_AD_AD_MUTE_BIT, 1),
  533. SND_SOC_DAPM_ADC("Stereo2 ADC MIXL", NULL, RT5514_DOWNFILTER1_CTRL1,
  534. RT5514_AD_AD_MUTE_BIT, 1),
  535. SND_SOC_DAPM_ADC("Stereo2 ADC MIXR", NULL, RT5514_DOWNFILTER1_CTRL2,
  536. RT5514_AD_AD_MUTE_BIT, 1),
  537. /* ADC PGA */
  538. SND_SOC_DAPM_PGA("Stereo1 ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
  539. SND_SOC_DAPM_PGA("Stereo2 ADC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
  540. /* Audio Interface */
  541. SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
  542. SND_SOC_DAPM_PRE("DAPM Pre", rt5514_pre_event),
  543. };
  544. static const struct snd_soc_dapm_route rt5514_dapm_routes[] = {
  545. { "DMIC1", NULL, "DMIC1L" },
  546. { "DMIC1", NULL, "DMIC1R" },
  547. { "DMIC2", NULL, "DMIC2L" },
  548. { "DMIC2", NULL, "DMIC2R" },
  549. { "DMIC1L", NULL, "DMIC CLK" },
  550. { "DMIC1R", NULL, "DMIC CLK" },
  551. { "DMIC2L", NULL, "DMIC CLK" },
  552. { "DMIC2R", NULL, "DMIC CLK" },
  553. { "Stereo1 DMIC Mux", "DMIC1", "DMIC1" },
  554. { "Stereo1 DMIC Mux", "DMIC2", "DMIC2" },
  555. { "Sto1 ADC MIXL", "DMIC Switch", "Stereo1 DMIC Mux" },
  556. { "Sto1 ADC MIXL", "ADC Switch", "AMICL" },
  557. { "Sto1 ADC MIXR", "DMIC Switch", "Stereo1 DMIC Mux" },
  558. { "Sto1 ADC MIXR", "ADC Switch", "AMICR" },
  559. { "ADC Power", NULL, "LDO18 IN" },
  560. { "ADC Power", NULL, "LDO18 ADC" },
  561. { "ADC Power", NULL, "LDO21" },
  562. { "ADC Power", NULL, "BG LDO18 IN" },
  563. { "ADC Power", NULL, "BG LDO21" },
  564. { "ADC Power", NULL, "BG MBIAS" },
  565. { "ADC Power", NULL, "MBIAS" },
  566. { "ADC Power", NULL, "VREF2" },
  567. { "ADC Power", NULL, "VREF1" },
  568. { "ADCL Power", NULL, "LDO16L" },
  569. { "ADCL Power", NULL, "ADC1L" },
  570. { "ADCL Power", NULL, "BSTL2" },
  571. { "ADCL Power", NULL, "BSTL" },
  572. { "ADCL Power", NULL, "ADCFEDL" },
  573. { "ADCR Power", NULL, "LDO16R" },
  574. { "ADCR Power", NULL, "ADC1R" },
  575. { "ADCR Power", NULL, "BSTR2" },
  576. { "ADCR Power", NULL, "BSTR" },
  577. { "ADCR Power", NULL, "ADCFEDR" },
  578. { "AMICL", NULL, "ADC CLK" },
  579. { "AMICL", NULL, "ADC Power" },
  580. { "AMICL", NULL, "ADCL Power" },
  581. { "AMICR", NULL, "ADC CLK" },
  582. { "AMICR", NULL, "ADC Power" },
  583. { "AMICR", NULL, "ADCR Power" },
  584. { "PLL1 LDO", NULL, "PLL1 LDO ENABLE" },
  585. { "PLL1", NULL, "PLL1 LDO" },
  586. { "Stereo1 ADC MIXL", NULL, "Sto1 ADC MIXL" },
  587. { "Stereo1 ADC MIXR", NULL, "Sto1 ADC MIXR" },
  588. { "Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXL" },
  589. { "Stereo1 ADC MIX", NULL, "Stereo1 ADC MIXR" },
  590. { "Stereo1 ADC MIX", NULL, "adc stereo1 filter" },
  591. { "adc stereo1 filter", NULL, "PLL1", rt5514_is_sys_clk_from_pll },
  592. { "Stereo2 DMIC Mux", "DMIC1", "DMIC1" },
  593. { "Stereo2 DMIC Mux", "DMIC2", "DMIC2" },
  594. { "Sto2 ADC MIXL", "DMIC Switch", "Stereo2 DMIC Mux" },
  595. { "Sto2 ADC MIXL", "ADC Switch", "AMICL" },
  596. { "Sto2 ADC MIXR", "DMIC Switch", "Stereo2 DMIC Mux" },
  597. { "Sto2 ADC MIXR", "ADC Switch", "AMICR" },
  598. { "Stereo2 ADC MIXL", NULL, "Sto2 ADC MIXL" },
  599. { "Stereo2 ADC MIXR", NULL, "Sto2 ADC MIXR" },
  600. { "Stereo2 ADC MIX", NULL, "Stereo2 ADC MIXL" },
  601. { "Stereo2 ADC MIX", NULL, "Stereo2 ADC MIXR" },
  602. { "Stereo2 ADC MIX", NULL, "adc stereo2 filter" },
  603. { "adc stereo2 filter", NULL, "PLL1", rt5514_is_sys_clk_from_pll },
  604. { "AIF1TX", NULL, "Stereo1 ADC MIX"},
  605. { "AIF1TX", NULL, "Stereo2 ADC MIX"},
  606. };
  607. static int rt5514_hw_params(struct snd_pcm_substream *substream,
  608. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  609. {
  610. struct snd_soc_codec *codec = dai->codec;
  611. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  612. int pre_div, bclk_ms, frame_size;
  613. unsigned int val_len = 0;
  614. rt5514->lrck = params_rate(params);
  615. pre_div = rl6231_get_clk_info(rt5514->sysclk, rt5514->lrck);
  616. if (pre_div < 0) {
  617. dev_err(codec->dev, "Unsupported clock setting\n");
  618. return -EINVAL;
  619. }
  620. frame_size = snd_soc_params_to_frame_size(params);
  621. if (frame_size < 0) {
  622. dev_err(codec->dev, "Unsupported frame size: %d\n", frame_size);
  623. return -EINVAL;
  624. }
  625. bclk_ms = frame_size > 32;
  626. rt5514->bclk = rt5514->lrck * (32 << bclk_ms);
  627. dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
  628. rt5514->bclk, rt5514->lrck);
  629. dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
  630. bclk_ms, pre_div, dai->id);
  631. switch (params_format(params)) {
  632. case SNDRV_PCM_FORMAT_S16_LE:
  633. break;
  634. case SNDRV_PCM_FORMAT_S20_3LE:
  635. val_len = RT5514_I2S_DL_20;
  636. break;
  637. case SNDRV_PCM_FORMAT_S24_LE:
  638. val_len = RT5514_I2S_DL_24;
  639. break;
  640. case SNDRV_PCM_FORMAT_S8:
  641. val_len = RT5514_I2S_DL_8;
  642. break;
  643. default:
  644. return -EINVAL;
  645. }
  646. regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL1, RT5514_I2S_DL_MASK,
  647. val_len);
  648. regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL2,
  649. RT5514_CLK_SYS_DIV_OUT_MASK | RT5514_SEL_ADC_OSR_MASK,
  650. pre_div << RT5514_CLK_SYS_DIV_OUT_SFT |
  651. pre_div << RT5514_SEL_ADC_OSR_SFT);
  652. return 0;
  653. }
  654. static int rt5514_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  655. {
  656. struct snd_soc_codec *codec = dai->codec;
  657. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  658. unsigned int reg_val = 0;
  659. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  660. case SND_SOC_DAIFMT_NB_NF:
  661. break;
  662. case SND_SOC_DAIFMT_NB_IF:
  663. reg_val |= RT5514_I2S_LR_INV;
  664. break;
  665. case SND_SOC_DAIFMT_IB_NF:
  666. reg_val |= RT5514_I2S_BP_INV;
  667. break;
  668. case SND_SOC_DAIFMT_IB_IF:
  669. reg_val |= RT5514_I2S_BP_INV | RT5514_I2S_LR_INV;
  670. break;
  671. default:
  672. return -EINVAL;
  673. }
  674. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  675. case SND_SOC_DAIFMT_I2S:
  676. break;
  677. case SND_SOC_DAIFMT_LEFT_J:
  678. reg_val |= RT5514_I2S_DF_LEFT;
  679. break;
  680. case SND_SOC_DAIFMT_DSP_A:
  681. reg_val |= RT5514_I2S_DF_PCM_A;
  682. break;
  683. case SND_SOC_DAIFMT_DSP_B:
  684. reg_val |= RT5514_I2S_DF_PCM_B;
  685. break;
  686. default:
  687. return -EINVAL;
  688. }
  689. regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL1,
  690. RT5514_I2S_DF_MASK | RT5514_I2S_BP_MASK | RT5514_I2S_LR_MASK,
  691. reg_val);
  692. return 0;
  693. }
  694. static int rt5514_set_dai_sysclk(struct snd_soc_dai *dai,
  695. int clk_id, unsigned int freq, int dir)
  696. {
  697. struct snd_soc_codec *codec = dai->codec;
  698. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  699. unsigned int reg_val = 0;
  700. if (freq == rt5514->sysclk && clk_id == rt5514->sysclk_src)
  701. return 0;
  702. switch (clk_id) {
  703. case RT5514_SCLK_S_MCLK:
  704. reg_val |= RT5514_CLK_SYS_PRE_SEL_MCLK;
  705. break;
  706. case RT5514_SCLK_S_PLL1:
  707. reg_val |= RT5514_CLK_SYS_PRE_SEL_PLL;
  708. break;
  709. default:
  710. dev_err(codec->dev, "Invalid clock id (%d)\n", clk_id);
  711. return -EINVAL;
  712. }
  713. regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL2,
  714. RT5514_CLK_SYS_PRE_SEL_MASK, reg_val);
  715. rt5514->sysclk = freq;
  716. rt5514->sysclk_src = clk_id;
  717. dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
  718. return 0;
  719. }
  720. static int rt5514_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
  721. unsigned int freq_in, unsigned int freq_out)
  722. {
  723. struct snd_soc_codec *codec = dai->codec;
  724. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  725. struct rl6231_pll_code pll_code;
  726. int ret;
  727. if (!freq_in || !freq_out) {
  728. dev_dbg(codec->dev, "PLL disabled\n");
  729. rt5514->pll_in = 0;
  730. rt5514->pll_out = 0;
  731. regmap_update_bits(rt5514->regmap, RT5514_CLK_CTRL2,
  732. RT5514_CLK_SYS_PRE_SEL_MASK,
  733. RT5514_CLK_SYS_PRE_SEL_MCLK);
  734. return 0;
  735. }
  736. if (source == rt5514->pll_src && freq_in == rt5514->pll_in &&
  737. freq_out == rt5514->pll_out)
  738. return 0;
  739. switch (source) {
  740. case RT5514_PLL1_S_MCLK:
  741. regmap_update_bits(rt5514->regmap, RT5514_PLL_SOURCE_CTRL,
  742. RT5514_PLL_1_SEL_MASK, RT5514_PLL_1_SEL_MCLK);
  743. break;
  744. case RT5514_PLL1_S_BCLK:
  745. regmap_update_bits(rt5514->regmap, RT5514_PLL_SOURCE_CTRL,
  746. RT5514_PLL_1_SEL_MASK, RT5514_PLL_1_SEL_SCLK);
  747. break;
  748. default:
  749. dev_err(codec->dev, "Unknown PLL source %d\n", source);
  750. return -EINVAL;
  751. }
  752. ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
  753. if (ret < 0) {
  754. dev_err(codec->dev, "Unsupport input clock %d\n", freq_in);
  755. return ret;
  756. }
  757. dev_dbg(codec->dev, "bypass=%d m=%d n=%d k=%d\n",
  758. pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
  759. pll_code.n_code, pll_code.k_code);
  760. regmap_write(rt5514->regmap, RT5514_ANA_CTRL_PLL1_1,
  761. pll_code.k_code << RT5514_PLL_K_SFT |
  762. pll_code.n_code << RT5514_PLL_N_SFT |
  763. (pll_code.m_bp ? 0 : pll_code.m_code) << RT5514_PLL_M_SFT);
  764. regmap_update_bits(rt5514->regmap, RT5514_ANA_CTRL_PLL1_2,
  765. RT5514_PLL_M_BP, pll_code.m_bp << RT5514_PLL_M_BP_SFT);
  766. rt5514->pll_in = freq_in;
  767. rt5514->pll_out = freq_out;
  768. rt5514->pll_src = source;
  769. return 0;
  770. }
  771. static int rt5514_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  772. unsigned int rx_mask, int slots, int slot_width)
  773. {
  774. struct snd_soc_codec *codec = dai->codec;
  775. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  776. unsigned int val = 0;
  777. if (rx_mask || tx_mask)
  778. val |= RT5514_TDM_MODE;
  779. if (slots == 4)
  780. val |= RT5514_TDMSLOT_SEL_RX_4CH | RT5514_TDMSLOT_SEL_TX_4CH;
  781. switch (slot_width) {
  782. case 20:
  783. val |= RT5514_CH_LEN_RX_20 | RT5514_CH_LEN_TX_20;
  784. break;
  785. case 24:
  786. val |= RT5514_CH_LEN_RX_24 | RT5514_CH_LEN_TX_24;
  787. break;
  788. case 32:
  789. val |= RT5514_CH_LEN_RX_32 | RT5514_CH_LEN_TX_32;
  790. break;
  791. case 16:
  792. default:
  793. break;
  794. }
  795. regmap_update_bits(rt5514->regmap, RT5514_I2S_CTRL1, RT5514_TDM_MODE |
  796. RT5514_TDMSLOT_SEL_RX_MASK | RT5514_TDMSLOT_SEL_TX_MASK |
  797. RT5514_CH_LEN_RX_MASK | RT5514_CH_LEN_TX_MASK, val);
  798. return 0;
  799. }
  800. static int rt5514_set_bias_level(struct snd_soc_codec *codec,
  801. enum snd_soc_bias_level level)
  802. {
  803. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  804. int ret;
  805. switch (level) {
  806. case SND_SOC_BIAS_PREPARE:
  807. if (IS_ERR(rt5514->mclk))
  808. break;
  809. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_ON) {
  810. clk_disable_unprepare(rt5514->mclk);
  811. } else {
  812. ret = clk_prepare_enable(rt5514->mclk);
  813. if (ret)
  814. return ret;
  815. }
  816. break;
  817. default:
  818. break;
  819. }
  820. return 0;
  821. }
  822. static int rt5514_probe(struct snd_soc_codec *codec)
  823. {
  824. struct rt5514_priv *rt5514 = snd_soc_codec_get_drvdata(codec);
  825. rt5514->mclk = devm_clk_get(codec->dev, "mclk");
  826. if (PTR_ERR(rt5514->mclk) == -EPROBE_DEFER)
  827. return -EPROBE_DEFER;
  828. rt5514->codec = codec;
  829. return 0;
  830. }
  831. static int rt5514_i2c_read(void *context, unsigned int reg, unsigned int *val)
  832. {
  833. struct i2c_client *client = context;
  834. struct rt5514_priv *rt5514 = i2c_get_clientdata(client);
  835. regmap_read(rt5514->i2c_regmap, reg | RT5514_DSP_MAPPING, val);
  836. return 0;
  837. }
  838. static int rt5514_i2c_write(void *context, unsigned int reg, unsigned int val)
  839. {
  840. struct i2c_client *client = context;
  841. struct rt5514_priv *rt5514 = i2c_get_clientdata(client);
  842. regmap_write(rt5514->i2c_regmap, reg | RT5514_DSP_MAPPING, val);
  843. return 0;
  844. }
  845. #define RT5514_STEREO_RATES SNDRV_PCM_RATE_8000_192000
  846. #define RT5514_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  847. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
  848. struct snd_soc_dai_ops rt5514_aif_dai_ops = {
  849. .hw_params = rt5514_hw_params,
  850. .set_fmt = rt5514_set_dai_fmt,
  851. .set_sysclk = rt5514_set_dai_sysclk,
  852. .set_pll = rt5514_set_dai_pll,
  853. .set_tdm_slot = rt5514_set_tdm_slot,
  854. };
  855. struct snd_soc_dai_driver rt5514_dai[] = {
  856. {
  857. .name = "rt5514-aif1",
  858. .id = 0,
  859. .capture = {
  860. .stream_name = "AIF1 Capture",
  861. .channels_min = 1,
  862. .channels_max = 4,
  863. .rates = RT5514_STEREO_RATES,
  864. .formats = RT5514_FORMATS,
  865. },
  866. .ops = &rt5514_aif_dai_ops,
  867. }
  868. };
  869. static struct snd_soc_codec_driver soc_codec_dev_rt5514 = {
  870. .probe = rt5514_probe,
  871. .idle_bias_off = true,
  872. .set_bias_level = rt5514_set_bias_level,
  873. .component_driver = {
  874. .controls = rt5514_snd_controls,
  875. .num_controls = ARRAY_SIZE(rt5514_snd_controls),
  876. .dapm_widgets = rt5514_dapm_widgets,
  877. .num_dapm_widgets = ARRAY_SIZE(rt5514_dapm_widgets),
  878. .dapm_routes = rt5514_dapm_routes,
  879. .num_dapm_routes = ARRAY_SIZE(rt5514_dapm_routes),
  880. },
  881. };
  882. static const struct regmap_config rt5514_i2c_regmap = {
  883. .name = "i2c",
  884. .reg_bits = 32,
  885. .val_bits = 32,
  886. .readable_reg = rt5514_i2c_readable_register,
  887. .cache_type = REGCACHE_NONE,
  888. };
  889. static const struct regmap_config rt5514_regmap = {
  890. .reg_bits = 16,
  891. .val_bits = 32,
  892. .max_register = RT5514_VENDOR_ID2,
  893. .volatile_reg = rt5514_volatile_register,
  894. .readable_reg = rt5514_readable_register,
  895. .reg_read = rt5514_i2c_read,
  896. .reg_write = rt5514_i2c_write,
  897. .cache_type = REGCACHE_RBTREE,
  898. .reg_defaults = rt5514_reg,
  899. .num_reg_defaults = ARRAY_SIZE(rt5514_reg),
  900. .use_single_rw = true,
  901. };
  902. static const struct i2c_device_id rt5514_i2c_id[] = {
  903. { "rt5514", 0 },
  904. { }
  905. };
  906. MODULE_DEVICE_TABLE(i2c, rt5514_i2c_id);
  907. #if defined(CONFIG_OF)
  908. static const struct of_device_id rt5514_of_match[] = {
  909. { .compatible = "realtek,rt5514", },
  910. {},
  911. };
  912. MODULE_DEVICE_TABLE(of, rt5514_of_match);
  913. #endif
  914. static int rt5514_i2c_probe(struct i2c_client *i2c,
  915. const struct i2c_device_id *id)
  916. {
  917. struct rt5514_priv *rt5514;
  918. int ret;
  919. unsigned int val;
  920. rt5514 = devm_kzalloc(&i2c->dev, sizeof(struct rt5514_priv),
  921. GFP_KERNEL);
  922. if (rt5514 == NULL)
  923. return -ENOMEM;
  924. i2c_set_clientdata(i2c, rt5514);
  925. rt5514->i2c_regmap = devm_regmap_init_i2c(i2c, &rt5514_i2c_regmap);
  926. if (IS_ERR(rt5514->i2c_regmap)) {
  927. ret = PTR_ERR(rt5514->i2c_regmap);
  928. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  929. ret);
  930. return ret;
  931. }
  932. rt5514->regmap = devm_regmap_init(&i2c->dev, NULL, i2c, &rt5514_regmap);
  933. if (IS_ERR(rt5514->regmap)) {
  934. ret = PTR_ERR(rt5514->regmap);
  935. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  936. ret);
  937. return ret;
  938. }
  939. regmap_read(rt5514->regmap, RT5514_VENDOR_ID2, &val);
  940. if (val != RT5514_DEVICE_ID) {
  941. dev_err(&i2c->dev,
  942. "Device with ID register %x is not rt5514\n", val);
  943. return -ENODEV;
  944. }
  945. ret = regmap_multi_reg_write(rt5514->i2c_regmap, rt5514_i2c_patch,
  946. ARRAY_SIZE(rt5514_i2c_patch));
  947. if (ret != 0)
  948. dev_warn(&i2c->dev, "Failed to apply i2c_regmap patch: %d\n",
  949. ret);
  950. ret = regmap_register_patch(rt5514->regmap, rt5514_patch,
  951. ARRAY_SIZE(rt5514_patch));
  952. if (ret != 0)
  953. dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
  954. return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5514,
  955. rt5514_dai, ARRAY_SIZE(rt5514_dai));
  956. }
  957. static int rt5514_i2c_remove(struct i2c_client *i2c)
  958. {
  959. snd_soc_unregister_codec(&i2c->dev);
  960. return 0;
  961. }
  962. struct i2c_driver rt5514_i2c_driver = {
  963. .driver = {
  964. .name = "rt5514",
  965. .of_match_table = of_match_ptr(rt5514_of_match),
  966. },
  967. .probe = rt5514_i2c_probe,
  968. .remove = rt5514_i2c_remove,
  969. .id_table = rt5514_i2c_id,
  970. };
  971. module_i2c_driver(rt5514_i2c_driver);
  972. MODULE_DESCRIPTION("ASoC RT5514 driver");
  973. MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
  974. MODULE_LICENSE("GPL v2");