adau1977.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. * ADAU1977/ADAU1978/ADAU1979 driver
  3. *
  4. * Copyright 2014 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/i2c.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_data/adau1977.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/slab.h>
  19. #include <sound/core.h>
  20. #include <sound/initval.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/tlv.h>
  25. #include "adau1977.h"
  26. #define ADAU1977_REG_POWER 0x00
  27. #define ADAU1977_REG_PLL 0x01
  28. #define ADAU1977_REG_BOOST 0x02
  29. #define ADAU1977_REG_MICBIAS 0x03
  30. #define ADAU1977_REG_BLOCK_POWER_SAI 0x04
  31. #define ADAU1977_REG_SAI_CTRL0 0x05
  32. #define ADAU1977_REG_SAI_CTRL1 0x06
  33. #define ADAU1977_REG_CMAP12 0x07
  34. #define ADAU1977_REG_CMAP34 0x08
  35. #define ADAU1977_REG_SAI_OVERTEMP 0x09
  36. #define ADAU1977_REG_POST_ADC_GAIN(x) (0x0a + (x))
  37. #define ADAU1977_REG_MISC_CONTROL 0x0e
  38. #define ADAU1977_REG_DIAG_CONTROL 0x10
  39. #define ADAU1977_REG_STATUS(x) (0x11 + (x))
  40. #define ADAU1977_REG_DIAG_IRQ1 0x15
  41. #define ADAU1977_REG_DIAG_IRQ2 0x16
  42. #define ADAU1977_REG_ADJUST1 0x17
  43. #define ADAU1977_REG_ADJUST2 0x18
  44. #define ADAU1977_REG_ADC_CLIP 0x19
  45. #define ADAU1977_REG_DC_HPF_CAL 0x1a
  46. #define ADAU1977_POWER_RESET BIT(7)
  47. #define ADAU1977_POWER_PWUP BIT(0)
  48. #define ADAU1977_PLL_CLK_S BIT(4)
  49. #define ADAU1977_PLL_MCS_MASK 0x7
  50. #define ADAU1977_MICBIAS_MB_VOLTS_MASK 0xf0
  51. #define ADAU1977_MICBIAS_MB_VOLTS_OFFSET 4
  52. #define ADAU1977_BLOCK_POWER_SAI_LR_POL BIT(7)
  53. #define ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE BIT(6)
  54. #define ADAU1977_BLOCK_POWER_SAI_LDO_EN BIT(5)
  55. #define ADAU1977_SAI_CTRL0_FMT_MASK (0x3 << 6)
  56. #define ADAU1977_SAI_CTRL0_FMT_I2S (0x0 << 6)
  57. #define ADAU1977_SAI_CTRL0_FMT_LJ (0x1 << 6)
  58. #define ADAU1977_SAI_CTRL0_FMT_RJ_24BIT (0x2 << 6)
  59. #define ADAU1977_SAI_CTRL0_FMT_RJ_16BIT (0x3 << 6)
  60. #define ADAU1977_SAI_CTRL0_SAI_MASK (0x7 << 3)
  61. #define ADAU1977_SAI_CTRL0_SAI_I2S (0x0 << 3)
  62. #define ADAU1977_SAI_CTRL0_SAI_TDM_2 (0x1 << 3)
  63. #define ADAU1977_SAI_CTRL0_SAI_TDM_4 (0x2 << 3)
  64. #define ADAU1977_SAI_CTRL0_SAI_TDM_8 (0x3 << 3)
  65. #define ADAU1977_SAI_CTRL0_SAI_TDM_16 (0x4 << 3)
  66. #define ADAU1977_SAI_CTRL0_FS_MASK (0x7)
  67. #define ADAU1977_SAI_CTRL0_FS_8000_12000 (0x0)
  68. #define ADAU1977_SAI_CTRL0_FS_16000_24000 (0x1)
  69. #define ADAU1977_SAI_CTRL0_FS_32000_48000 (0x2)
  70. #define ADAU1977_SAI_CTRL0_FS_64000_96000 (0x3)
  71. #define ADAU1977_SAI_CTRL0_FS_128000_192000 (0x4)
  72. #define ADAU1977_SAI_CTRL1_SLOT_WIDTH_MASK (0x3 << 5)
  73. #define ADAU1977_SAI_CTRL1_SLOT_WIDTH_32 (0x0 << 5)
  74. #define ADAU1977_SAI_CTRL1_SLOT_WIDTH_24 (0x1 << 5)
  75. #define ADAU1977_SAI_CTRL1_SLOT_WIDTH_16 (0x2 << 5)
  76. #define ADAU1977_SAI_CTRL1_DATA_WIDTH_MASK (0x1 << 4)
  77. #define ADAU1977_SAI_CTRL1_DATA_WIDTH_16BIT (0x1 << 4)
  78. #define ADAU1977_SAI_CTRL1_DATA_WIDTH_24BIT (0x0 << 4)
  79. #define ADAU1977_SAI_CTRL1_LRCLK_PULSE BIT(3)
  80. #define ADAU1977_SAI_CTRL1_MSB BIT(2)
  81. #define ADAU1977_SAI_CTRL1_BCLKRATE_16 (0x1 << 1)
  82. #define ADAU1977_SAI_CTRL1_BCLKRATE_32 (0x0 << 1)
  83. #define ADAU1977_SAI_CTRL1_BCLKRATE_MASK (0x1 << 1)
  84. #define ADAU1977_SAI_CTRL1_MASTER BIT(0)
  85. #define ADAU1977_SAI_OVERTEMP_DRV_C(x) BIT(4 + (x))
  86. #define ADAU1977_SAI_OVERTEMP_DRV_HIZ BIT(3)
  87. #define ADAU1977_MISC_CONTROL_SUM_MODE_MASK (0x3 << 6)
  88. #define ADAU1977_MISC_CONTROL_SUM_MODE_1CH (0x2 << 6)
  89. #define ADAU1977_MISC_CONTROL_SUM_MODE_2CH (0x1 << 6)
  90. #define ADAU1977_MISC_CONTROL_SUM_MODE_4CH (0x0 << 6)
  91. #define ADAU1977_MISC_CONTROL_MMUTE BIT(4)
  92. #define ADAU1977_MISC_CONTROL_DC_CAL BIT(0)
  93. #define ADAU1977_CHAN_MAP_SECOND_SLOT_OFFSET 4
  94. #define ADAU1977_CHAN_MAP_FIRST_SLOT_OFFSET 0
  95. struct adau1977 {
  96. struct regmap *regmap;
  97. bool right_j;
  98. unsigned int sysclk;
  99. enum adau1977_sysclk_src sysclk_src;
  100. struct gpio_desc *reset_gpio;
  101. enum adau1977_type type;
  102. struct regulator *avdd_reg;
  103. struct regulator *dvdd_reg;
  104. struct snd_pcm_hw_constraint_list constraints;
  105. struct device *dev;
  106. void (*switch_mode)(struct device *dev);
  107. unsigned int max_master_fs;
  108. unsigned int slot_width;
  109. bool enabled;
  110. bool master;
  111. };
  112. static const struct reg_default adau1977_reg_defaults[] = {
  113. { 0x00, 0x00 },
  114. { 0x01, 0x41 },
  115. { 0x02, 0x4a },
  116. { 0x03, 0x7d },
  117. { 0x04, 0x3d },
  118. { 0x05, 0x02 },
  119. { 0x06, 0x00 },
  120. { 0x07, 0x10 },
  121. { 0x08, 0x32 },
  122. { 0x09, 0xf0 },
  123. { 0x0a, 0xa0 },
  124. { 0x0b, 0xa0 },
  125. { 0x0c, 0xa0 },
  126. { 0x0d, 0xa0 },
  127. { 0x0e, 0x02 },
  128. { 0x10, 0x0f },
  129. { 0x15, 0x20 },
  130. { 0x16, 0x00 },
  131. { 0x17, 0x00 },
  132. { 0x18, 0x00 },
  133. { 0x1a, 0x00 },
  134. };
  135. static const DECLARE_TLV_DB_MINMAX_MUTE(adau1977_adc_gain, -3562, 6000);
  136. static const struct snd_soc_dapm_widget adau1977_micbias_dapm_widgets[] = {
  137. SND_SOC_DAPM_SUPPLY("MICBIAS", ADAU1977_REG_MICBIAS,
  138. 3, 0, NULL, 0)
  139. };
  140. static const struct snd_soc_dapm_widget adau1977_dapm_widgets[] = {
  141. SND_SOC_DAPM_SUPPLY("Vref", ADAU1977_REG_BLOCK_POWER_SAI,
  142. 4, 0, NULL, 0),
  143. SND_SOC_DAPM_ADC("ADC1", "Capture", ADAU1977_REG_BLOCK_POWER_SAI, 0, 0),
  144. SND_SOC_DAPM_ADC("ADC2", "Capture", ADAU1977_REG_BLOCK_POWER_SAI, 1, 0),
  145. SND_SOC_DAPM_ADC("ADC3", "Capture", ADAU1977_REG_BLOCK_POWER_SAI, 2, 0),
  146. SND_SOC_DAPM_ADC("ADC4", "Capture", ADAU1977_REG_BLOCK_POWER_SAI, 3, 0),
  147. SND_SOC_DAPM_INPUT("AIN1"),
  148. SND_SOC_DAPM_INPUT("AIN2"),
  149. SND_SOC_DAPM_INPUT("AIN3"),
  150. SND_SOC_DAPM_INPUT("AIN4"),
  151. SND_SOC_DAPM_OUTPUT("VREF"),
  152. };
  153. static const struct snd_soc_dapm_route adau1977_dapm_routes[] = {
  154. { "ADC1", NULL, "AIN1" },
  155. { "ADC2", NULL, "AIN2" },
  156. { "ADC3", NULL, "AIN3" },
  157. { "ADC4", NULL, "AIN4" },
  158. { "ADC1", NULL, "Vref" },
  159. { "ADC2", NULL, "Vref" },
  160. { "ADC3", NULL, "Vref" },
  161. { "ADC4", NULL, "Vref" },
  162. { "VREF", NULL, "Vref" },
  163. };
  164. #define ADAU1977_VOLUME(x) \
  165. SOC_SINGLE_TLV("ADC" #x " Capture Volume", \
  166. ADAU1977_REG_POST_ADC_GAIN((x) - 1), \
  167. 0, 255, 1, adau1977_adc_gain)
  168. #define ADAU1977_HPF_SWITCH(x) \
  169. SOC_SINGLE("ADC" #x " Highpass-Filter Capture Switch", \
  170. ADAU1977_REG_DC_HPF_CAL, (x) - 1, 1, 0)
  171. #define ADAU1977_DC_SUB_SWITCH(x) \
  172. SOC_SINGLE("ADC" #x " DC Subtraction Capture Switch", \
  173. ADAU1977_REG_DC_HPF_CAL, (x) + 3, 1, 0)
  174. static const struct snd_kcontrol_new adau1977_snd_controls[] = {
  175. ADAU1977_VOLUME(1),
  176. ADAU1977_VOLUME(2),
  177. ADAU1977_VOLUME(3),
  178. ADAU1977_VOLUME(4),
  179. ADAU1977_HPF_SWITCH(1),
  180. ADAU1977_HPF_SWITCH(2),
  181. ADAU1977_HPF_SWITCH(3),
  182. ADAU1977_HPF_SWITCH(4),
  183. ADAU1977_DC_SUB_SWITCH(1),
  184. ADAU1977_DC_SUB_SWITCH(2),
  185. ADAU1977_DC_SUB_SWITCH(3),
  186. ADAU1977_DC_SUB_SWITCH(4),
  187. };
  188. static int adau1977_reset(struct adau1977 *adau1977)
  189. {
  190. int ret;
  191. /*
  192. * The reset bit is obviously volatile, but we need to be able to cache
  193. * the other bits in the register, so we can't just mark the whole
  194. * register as volatile. Since this is the only place where we'll ever
  195. * touch the reset bit just bypass the cache for this operation.
  196. */
  197. regcache_cache_bypass(adau1977->regmap, true);
  198. ret = regmap_write(adau1977->regmap, ADAU1977_REG_POWER,
  199. ADAU1977_POWER_RESET);
  200. regcache_cache_bypass(adau1977->regmap, false);
  201. if (ret)
  202. return ret;
  203. return ret;
  204. }
  205. /*
  206. * Returns the appropriate setting for ths FS field in the CTRL0 register
  207. * depending on the rate.
  208. */
  209. static int adau1977_lookup_fs(unsigned int rate)
  210. {
  211. if (rate >= 8000 && rate <= 12000)
  212. return ADAU1977_SAI_CTRL0_FS_8000_12000;
  213. else if (rate >= 16000 && rate <= 24000)
  214. return ADAU1977_SAI_CTRL0_FS_16000_24000;
  215. else if (rate >= 32000 && rate <= 48000)
  216. return ADAU1977_SAI_CTRL0_FS_32000_48000;
  217. else if (rate >= 64000 && rate <= 96000)
  218. return ADAU1977_SAI_CTRL0_FS_64000_96000;
  219. else if (rate >= 128000 && rate <= 192000)
  220. return ADAU1977_SAI_CTRL0_FS_128000_192000;
  221. else
  222. return -EINVAL;
  223. }
  224. static int adau1977_lookup_mcs(struct adau1977 *adau1977, unsigned int rate,
  225. unsigned int fs)
  226. {
  227. unsigned int mcs;
  228. /*
  229. * rate = sysclk / (512 * mcs_lut[mcs]) * 2**fs
  230. * => mcs_lut[mcs] = sysclk / (512 * rate) * 2**fs
  231. * => mcs_lut[mcs] = sysclk / ((512 / 2**fs) * rate)
  232. */
  233. rate *= 512 >> fs;
  234. if (adau1977->sysclk % rate != 0)
  235. return -EINVAL;
  236. mcs = adau1977->sysclk / rate;
  237. /* The factors configured by MCS are 1, 2, 3, 4, 6 */
  238. if (mcs < 1 || mcs > 6 || mcs == 5)
  239. return -EINVAL;
  240. mcs = mcs - 1;
  241. if (mcs == 5)
  242. mcs = 4;
  243. return mcs;
  244. }
  245. static int adau1977_hw_params(struct snd_pcm_substream *substream,
  246. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  247. {
  248. struct snd_soc_codec *codec = dai->codec;
  249. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
  250. unsigned int rate = params_rate(params);
  251. unsigned int slot_width;
  252. unsigned int ctrl0, ctrl0_mask;
  253. unsigned int ctrl1;
  254. int mcs, fs;
  255. int ret;
  256. fs = adau1977_lookup_fs(rate);
  257. if (fs < 0)
  258. return fs;
  259. if (adau1977->sysclk_src == ADAU1977_SYSCLK_SRC_MCLK) {
  260. mcs = adau1977_lookup_mcs(adau1977, rate, fs);
  261. if (mcs < 0)
  262. return mcs;
  263. } else {
  264. mcs = 0;
  265. }
  266. ctrl0_mask = ADAU1977_SAI_CTRL0_FS_MASK;
  267. ctrl0 = fs;
  268. if (adau1977->right_j) {
  269. switch (params_width(params)) {
  270. case 16:
  271. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_RJ_16BIT;
  272. break;
  273. case 24:
  274. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_RJ_24BIT;
  275. break;
  276. default:
  277. return -EINVAL;
  278. }
  279. ctrl0_mask |= ADAU1977_SAI_CTRL0_FMT_MASK;
  280. }
  281. if (adau1977->master) {
  282. switch (params_width(params)) {
  283. case 16:
  284. ctrl1 = ADAU1977_SAI_CTRL1_DATA_WIDTH_16BIT;
  285. slot_width = 16;
  286. break;
  287. case 24:
  288. case 32:
  289. ctrl1 = ADAU1977_SAI_CTRL1_DATA_WIDTH_24BIT;
  290. slot_width = 32;
  291. break;
  292. default:
  293. return -EINVAL;
  294. }
  295. /* In TDM mode there is a fixed slot width */
  296. if (adau1977->slot_width)
  297. slot_width = adau1977->slot_width;
  298. if (slot_width == 16)
  299. ctrl1 |= ADAU1977_SAI_CTRL1_BCLKRATE_16;
  300. else
  301. ctrl1 |= ADAU1977_SAI_CTRL1_BCLKRATE_32;
  302. ret = regmap_update_bits(adau1977->regmap,
  303. ADAU1977_REG_SAI_CTRL1,
  304. ADAU1977_SAI_CTRL1_DATA_WIDTH_MASK |
  305. ADAU1977_SAI_CTRL1_BCLKRATE_MASK,
  306. ctrl1);
  307. if (ret < 0)
  308. return ret;
  309. }
  310. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL0,
  311. ctrl0_mask, ctrl0);
  312. if (ret < 0)
  313. return ret;
  314. return regmap_update_bits(adau1977->regmap, ADAU1977_REG_PLL,
  315. ADAU1977_PLL_MCS_MASK, mcs);
  316. }
  317. static int adau1977_power_disable(struct adau1977 *adau1977)
  318. {
  319. int ret = 0;
  320. if (!adau1977->enabled)
  321. return 0;
  322. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_POWER,
  323. ADAU1977_POWER_PWUP, 0);
  324. if (ret)
  325. return ret;
  326. regcache_mark_dirty(adau1977->regmap);
  327. if (adau1977->reset_gpio)
  328. gpiod_set_value_cansleep(adau1977->reset_gpio, 0);
  329. regcache_cache_only(adau1977->regmap, true);
  330. regulator_disable(adau1977->avdd_reg);
  331. if (adau1977->dvdd_reg)
  332. regulator_disable(adau1977->dvdd_reg);
  333. adau1977->enabled = false;
  334. return 0;
  335. }
  336. static int adau1977_power_enable(struct adau1977 *adau1977)
  337. {
  338. unsigned int val;
  339. int ret = 0;
  340. if (adau1977->enabled)
  341. return 0;
  342. ret = regulator_enable(adau1977->avdd_reg);
  343. if (ret)
  344. return ret;
  345. if (adau1977->dvdd_reg) {
  346. ret = regulator_enable(adau1977->dvdd_reg);
  347. if (ret)
  348. goto err_disable_avdd;
  349. }
  350. if (adau1977->reset_gpio)
  351. gpiod_set_value_cansleep(adau1977->reset_gpio, 1);
  352. regcache_cache_only(adau1977->regmap, false);
  353. if (adau1977->switch_mode)
  354. adau1977->switch_mode(adau1977->dev);
  355. ret = adau1977_reset(adau1977);
  356. if (ret)
  357. goto err_disable_dvdd;
  358. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_POWER,
  359. ADAU1977_POWER_PWUP, ADAU1977_POWER_PWUP);
  360. if (ret)
  361. goto err_disable_dvdd;
  362. ret = regcache_sync(adau1977->regmap);
  363. if (ret)
  364. goto err_disable_dvdd;
  365. /*
  366. * The PLL register is not affected by the software reset. It is
  367. * possible that the value of the register was changed to the
  368. * default value while we were in cache only mode. In this case
  369. * regcache_sync will skip over it and we have to manually sync
  370. * it.
  371. */
  372. ret = regmap_read(adau1977->regmap, ADAU1977_REG_PLL, &val);
  373. if (ret)
  374. goto err_disable_dvdd;
  375. if (val == 0x41) {
  376. regcache_cache_bypass(adau1977->regmap, true);
  377. ret = regmap_write(adau1977->regmap, ADAU1977_REG_PLL,
  378. 0x41);
  379. if (ret)
  380. goto err_disable_dvdd;
  381. regcache_cache_bypass(adau1977->regmap, false);
  382. }
  383. adau1977->enabled = true;
  384. return ret;
  385. err_disable_dvdd:
  386. if (adau1977->dvdd_reg)
  387. regulator_disable(adau1977->dvdd_reg);
  388. err_disable_avdd:
  389. regulator_disable(adau1977->avdd_reg);
  390. return ret;
  391. }
  392. static int adau1977_set_bias_level(struct snd_soc_codec *codec,
  393. enum snd_soc_bias_level level)
  394. {
  395. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
  396. int ret = 0;
  397. switch (level) {
  398. case SND_SOC_BIAS_ON:
  399. break;
  400. case SND_SOC_BIAS_PREPARE:
  401. break;
  402. case SND_SOC_BIAS_STANDBY:
  403. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF)
  404. ret = adau1977_power_enable(adau1977);
  405. break;
  406. case SND_SOC_BIAS_OFF:
  407. ret = adau1977_power_disable(adau1977);
  408. break;
  409. }
  410. return ret;
  411. }
  412. static int adau1977_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  413. unsigned int rx_mask, int slots, int width)
  414. {
  415. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  416. unsigned int ctrl0, ctrl1, drv;
  417. unsigned int slot[4];
  418. unsigned int i;
  419. int ret;
  420. if (slots == 0) {
  421. /* 0 = No fixed slot width */
  422. adau1977->slot_width = 0;
  423. adau1977->max_master_fs = 192000;
  424. return regmap_update_bits(adau1977->regmap,
  425. ADAU1977_REG_SAI_CTRL0, ADAU1977_SAI_CTRL0_SAI_MASK,
  426. ADAU1977_SAI_CTRL0_SAI_I2S);
  427. }
  428. if (rx_mask == 0 || tx_mask != 0)
  429. return -EINVAL;
  430. drv = 0;
  431. for (i = 0; i < 4; i++) {
  432. slot[i] = __ffs(rx_mask);
  433. drv |= ADAU1977_SAI_OVERTEMP_DRV_C(i);
  434. rx_mask &= ~(1 << slot[i]);
  435. if (slot[i] >= slots)
  436. return -EINVAL;
  437. if (rx_mask == 0)
  438. break;
  439. }
  440. if (rx_mask != 0)
  441. return -EINVAL;
  442. switch (width) {
  443. case 16:
  444. ctrl1 = ADAU1977_SAI_CTRL1_SLOT_WIDTH_16;
  445. break;
  446. case 24:
  447. /* We can only generate 16 bit or 32 bit wide slots */
  448. if (adau1977->master)
  449. return -EINVAL;
  450. ctrl1 = ADAU1977_SAI_CTRL1_SLOT_WIDTH_24;
  451. break;
  452. case 32:
  453. ctrl1 = ADAU1977_SAI_CTRL1_SLOT_WIDTH_32;
  454. break;
  455. default:
  456. return -EINVAL;
  457. }
  458. switch (slots) {
  459. case 2:
  460. ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_2;
  461. break;
  462. case 4:
  463. ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_4;
  464. break;
  465. case 8:
  466. ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_8;
  467. break;
  468. case 16:
  469. ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_16;
  470. break;
  471. default:
  472. return -EINVAL;
  473. }
  474. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_OVERTEMP,
  475. ADAU1977_SAI_OVERTEMP_DRV_C(0) |
  476. ADAU1977_SAI_OVERTEMP_DRV_C(1) |
  477. ADAU1977_SAI_OVERTEMP_DRV_C(2) |
  478. ADAU1977_SAI_OVERTEMP_DRV_C(3), drv);
  479. if (ret)
  480. return ret;
  481. ret = regmap_write(adau1977->regmap, ADAU1977_REG_CMAP12,
  482. (slot[1] << ADAU1977_CHAN_MAP_SECOND_SLOT_OFFSET) |
  483. (slot[0] << ADAU1977_CHAN_MAP_FIRST_SLOT_OFFSET));
  484. if (ret)
  485. return ret;
  486. ret = regmap_write(adau1977->regmap, ADAU1977_REG_CMAP34,
  487. (slot[3] << ADAU1977_CHAN_MAP_SECOND_SLOT_OFFSET) |
  488. (slot[2] << ADAU1977_CHAN_MAP_FIRST_SLOT_OFFSET));
  489. if (ret)
  490. return ret;
  491. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL0,
  492. ADAU1977_SAI_CTRL0_SAI_MASK, ctrl0);
  493. if (ret)
  494. return ret;
  495. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL1,
  496. ADAU1977_SAI_CTRL1_SLOT_WIDTH_MASK, ctrl1);
  497. if (ret)
  498. return ret;
  499. adau1977->slot_width = width;
  500. /* In master mode the maximum bitclock is 24.576 MHz */
  501. adau1977->max_master_fs = min(192000, 24576000 / width / slots);
  502. return 0;
  503. }
  504. static int adau1977_mute(struct snd_soc_dai *dai, int mute, int stream)
  505. {
  506. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  507. unsigned int val;
  508. if (mute)
  509. val = ADAU1977_MISC_CONTROL_MMUTE;
  510. else
  511. val = 0;
  512. return regmap_update_bits(adau1977->regmap, ADAU1977_REG_MISC_CONTROL,
  513. ADAU1977_MISC_CONTROL_MMUTE, val);
  514. }
  515. static int adau1977_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  516. {
  517. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  518. unsigned int ctrl0 = 0, ctrl1 = 0, block_power = 0;
  519. bool invert_lrclk;
  520. int ret;
  521. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  522. case SND_SOC_DAIFMT_CBS_CFS:
  523. adau1977->master = false;
  524. break;
  525. case SND_SOC_DAIFMT_CBM_CFM:
  526. ctrl1 |= ADAU1977_SAI_CTRL1_MASTER;
  527. adau1977->master = true;
  528. break;
  529. default:
  530. return -EINVAL;
  531. }
  532. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  533. case SND_SOC_DAIFMT_NB_NF:
  534. invert_lrclk = false;
  535. break;
  536. case SND_SOC_DAIFMT_IB_NF:
  537. block_power |= ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE;
  538. invert_lrclk = false;
  539. break;
  540. case SND_SOC_DAIFMT_NB_IF:
  541. invert_lrclk = true;
  542. break;
  543. case SND_SOC_DAIFMT_IB_IF:
  544. block_power |= ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE;
  545. invert_lrclk = true;
  546. break;
  547. default:
  548. return -EINVAL;
  549. }
  550. adau1977->right_j = false;
  551. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  552. case SND_SOC_DAIFMT_I2S:
  553. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_I2S;
  554. break;
  555. case SND_SOC_DAIFMT_LEFT_J:
  556. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_LJ;
  557. invert_lrclk = !invert_lrclk;
  558. break;
  559. case SND_SOC_DAIFMT_RIGHT_J:
  560. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_RJ_24BIT;
  561. adau1977->right_j = true;
  562. invert_lrclk = !invert_lrclk;
  563. break;
  564. case SND_SOC_DAIFMT_DSP_A:
  565. ctrl1 |= ADAU1977_SAI_CTRL1_LRCLK_PULSE;
  566. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_I2S;
  567. invert_lrclk = false;
  568. break;
  569. case SND_SOC_DAIFMT_DSP_B:
  570. ctrl1 |= ADAU1977_SAI_CTRL1_LRCLK_PULSE;
  571. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_LJ;
  572. invert_lrclk = false;
  573. break;
  574. default:
  575. return -EINVAL;
  576. }
  577. if (invert_lrclk)
  578. block_power |= ADAU1977_BLOCK_POWER_SAI_LR_POL;
  579. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_BLOCK_POWER_SAI,
  580. ADAU1977_BLOCK_POWER_SAI_LR_POL |
  581. ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE, block_power);
  582. if (ret)
  583. return ret;
  584. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL0,
  585. ADAU1977_SAI_CTRL0_FMT_MASK,
  586. ctrl0);
  587. if (ret)
  588. return ret;
  589. return regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL1,
  590. ADAU1977_SAI_CTRL1_MASTER | ADAU1977_SAI_CTRL1_LRCLK_PULSE,
  591. ctrl1);
  592. }
  593. static int adau1977_startup(struct snd_pcm_substream *substream,
  594. struct snd_soc_dai *dai)
  595. {
  596. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  597. u64 formats = 0;
  598. if (adau1977->slot_width == 16)
  599. formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE;
  600. else if (adau1977->right_j || adau1977->slot_width == 24)
  601. formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  602. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE;
  603. snd_pcm_hw_constraint_list(substream->runtime, 0,
  604. SNDRV_PCM_HW_PARAM_RATE, &adau1977->constraints);
  605. if (adau1977->master)
  606. snd_pcm_hw_constraint_minmax(substream->runtime,
  607. SNDRV_PCM_HW_PARAM_RATE, 8000, adau1977->max_master_fs);
  608. if (formats != 0)
  609. snd_pcm_hw_constraint_mask64(substream->runtime,
  610. SNDRV_PCM_HW_PARAM_FORMAT, formats);
  611. return 0;
  612. }
  613. static int adau1977_set_tristate(struct snd_soc_dai *dai, int tristate)
  614. {
  615. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  616. unsigned int val;
  617. if (tristate)
  618. val = ADAU1977_SAI_OVERTEMP_DRV_HIZ;
  619. else
  620. val = 0;
  621. return regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_OVERTEMP,
  622. ADAU1977_SAI_OVERTEMP_DRV_HIZ, val);
  623. }
  624. static const struct snd_soc_dai_ops adau1977_dai_ops = {
  625. .startup = adau1977_startup,
  626. .hw_params = adau1977_hw_params,
  627. .mute_stream = adau1977_mute,
  628. .set_fmt = adau1977_set_dai_fmt,
  629. .set_tdm_slot = adau1977_set_tdm_slot,
  630. .set_tristate = adau1977_set_tristate,
  631. };
  632. static struct snd_soc_dai_driver adau1977_dai = {
  633. .name = "adau1977-hifi",
  634. .capture = {
  635. .stream_name = "Capture",
  636. .channels_min = 1,
  637. .channels_max = 4,
  638. .rates = SNDRV_PCM_RATE_KNOT,
  639. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  640. SNDRV_PCM_FMTBIT_S32_LE,
  641. .sig_bits = 24,
  642. },
  643. .ops = &adau1977_dai_ops,
  644. };
  645. static const unsigned int adau1977_rates[] = {
  646. 8000, 16000, 32000, 64000, 128000,
  647. 11025, 22050, 44100, 88200, 172400,
  648. 12000, 24000, 48000, 96000, 192000,
  649. };
  650. #define ADAU1977_RATE_CONSTRAINT_MASK_32000 0x001f
  651. #define ADAU1977_RATE_CONSTRAINT_MASK_44100 0x03e0
  652. #define ADAU1977_RATE_CONSTRAINT_MASK_48000 0x7c00
  653. /* All rates >= 32000 */
  654. #define ADAU1977_RATE_CONSTRAINT_MASK_LRCLK 0x739c
  655. static bool adau1977_check_sysclk(unsigned int mclk, unsigned int base_freq)
  656. {
  657. unsigned int mcs;
  658. if (mclk % (base_freq * 128) != 0)
  659. return false;
  660. mcs = mclk / (128 * base_freq);
  661. if (mcs < 1 || mcs > 6 || mcs == 5)
  662. return false;
  663. return true;
  664. }
  665. static int adau1977_set_sysclk(struct snd_soc_codec *codec,
  666. int clk_id, int source, unsigned int freq, int dir)
  667. {
  668. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
  669. unsigned int mask = 0;
  670. unsigned int clk_src;
  671. unsigned int ret;
  672. if (dir != SND_SOC_CLOCK_IN)
  673. return -EINVAL;
  674. if (clk_id != ADAU1977_SYSCLK)
  675. return -EINVAL;
  676. switch (source) {
  677. case ADAU1977_SYSCLK_SRC_MCLK:
  678. clk_src = 0;
  679. break;
  680. case ADAU1977_SYSCLK_SRC_LRCLK:
  681. clk_src = ADAU1977_PLL_CLK_S;
  682. break;
  683. default:
  684. return -EINVAL;
  685. }
  686. if (freq != 0 && source == ADAU1977_SYSCLK_SRC_MCLK) {
  687. if (freq < 4000000 || freq > 36864000)
  688. return -EINVAL;
  689. if (adau1977_check_sysclk(freq, 32000))
  690. mask |= ADAU1977_RATE_CONSTRAINT_MASK_32000;
  691. if (adau1977_check_sysclk(freq, 44100))
  692. mask |= ADAU1977_RATE_CONSTRAINT_MASK_44100;
  693. if (adau1977_check_sysclk(freq, 48000))
  694. mask |= ADAU1977_RATE_CONSTRAINT_MASK_48000;
  695. if (mask == 0)
  696. return -EINVAL;
  697. } else if (source == ADAU1977_SYSCLK_SRC_LRCLK) {
  698. mask = ADAU1977_RATE_CONSTRAINT_MASK_LRCLK;
  699. }
  700. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_PLL,
  701. ADAU1977_PLL_CLK_S, clk_src);
  702. if (ret)
  703. return ret;
  704. adau1977->constraints.mask = mask;
  705. adau1977->sysclk_src = source;
  706. adau1977->sysclk = freq;
  707. return 0;
  708. }
  709. static int adau1977_codec_probe(struct snd_soc_codec *codec)
  710. {
  711. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  712. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
  713. int ret;
  714. switch (adau1977->type) {
  715. case ADAU1977:
  716. ret = snd_soc_dapm_new_controls(dapm,
  717. adau1977_micbias_dapm_widgets,
  718. ARRAY_SIZE(adau1977_micbias_dapm_widgets));
  719. if (ret < 0)
  720. return ret;
  721. break;
  722. default:
  723. break;
  724. }
  725. return 0;
  726. }
  727. static struct snd_soc_codec_driver adau1977_codec_driver = {
  728. .probe = adau1977_codec_probe,
  729. .set_bias_level = adau1977_set_bias_level,
  730. .set_sysclk = adau1977_set_sysclk,
  731. .idle_bias_off = true,
  732. .component_driver = {
  733. .controls = adau1977_snd_controls,
  734. .num_controls = ARRAY_SIZE(adau1977_snd_controls),
  735. .dapm_widgets = adau1977_dapm_widgets,
  736. .num_dapm_widgets = ARRAY_SIZE(adau1977_dapm_widgets),
  737. .dapm_routes = adau1977_dapm_routes,
  738. .num_dapm_routes = ARRAY_SIZE(adau1977_dapm_routes),
  739. },
  740. };
  741. static int adau1977_setup_micbias(struct adau1977 *adau1977)
  742. {
  743. struct adau1977_platform_data *pdata = adau1977->dev->platform_data;
  744. unsigned int micbias;
  745. if (pdata) {
  746. micbias = pdata->micbias;
  747. if (micbias > ADAU1977_MICBIAS_9V0)
  748. return -EINVAL;
  749. } else {
  750. micbias = ADAU1977_MICBIAS_8V5;
  751. }
  752. return regmap_update_bits(adau1977->regmap, ADAU1977_REG_MICBIAS,
  753. ADAU1977_MICBIAS_MB_VOLTS_MASK,
  754. micbias << ADAU1977_MICBIAS_MB_VOLTS_OFFSET);
  755. }
  756. int adau1977_probe(struct device *dev, struct regmap *regmap,
  757. enum adau1977_type type, void (*switch_mode)(struct device *dev))
  758. {
  759. unsigned int power_off_mask;
  760. struct adau1977 *adau1977;
  761. int ret;
  762. if (IS_ERR(regmap))
  763. return PTR_ERR(regmap);
  764. adau1977 = devm_kzalloc(dev, sizeof(*adau1977), GFP_KERNEL);
  765. if (adau1977 == NULL)
  766. return -ENOMEM;
  767. adau1977->dev = dev;
  768. adau1977->type = type;
  769. adau1977->regmap = regmap;
  770. adau1977->switch_mode = switch_mode;
  771. adau1977->max_master_fs = 192000;
  772. adau1977->constraints.list = adau1977_rates;
  773. adau1977->constraints.count = ARRAY_SIZE(adau1977_rates);
  774. adau1977->avdd_reg = devm_regulator_get(dev, "AVDD");
  775. if (IS_ERR(adau1977->avdd_reg))
  776. return PTR_ERR(adau1977->avdd_reg);
  777. adau1977->dvdd_reg = devm_regulator_get_optional(dev, "DVDD");
  778. if (IS_ERR(adau1977->dvdd_reg)) {
  779. if (PTR_ERR(adau1977->dvdd_reg) != -ENODEV)
  780. return PTR_ERR(adau1977->dvdd_reg);
  781. adau1977->dvdd_reg = NULL;
  782. }
  783. adau1977->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  784. GPIOD_OUT_LOW);
  785. if (IS_ERR(adau1977->reset_gpio))
  786. return PTR_ERR(adau1977->reset_gpio);
  787. dev_set_drvdata(dev, adau1977);
  788. if (adau1977->reset_gpio)
  789. ndelay(100);
  790. ret = adau1977_power_enable(adau1977);
  791. if (ret)
  792. return ret;
  793. if (type == ADAU1977) {
  794. ret = adau1977_setup_micbias(adau1977);
  795. if (ret)
  796. goto err_poweroff;
  797. }
  798. if (adau1977->dvdd_reg)
  799. power_off_mask = ~0;
  800. else
  801. power_off_mask = (unsigned int)~ADAU1977_BLOCK_POWER_SAI_LDO_EN;
  802. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_BLOCK_POWER_SAI,
  803. power_off_mask, 0x00);
  804. if (ret)
  805. goto err_poweroff;
  806. ret = adau1977_power_disable(adau1977);
  807. if (ret)
  808. return ret;
  809. return snd_soc_register_codec(dev, &adau1977_codec_driver,
  810. &adau1977_dai, 1);
  811. err_poweroff:
  812. adau1977_power_disable(adau1977);
  813. return ret;
  814. }
  815. EXPORT_SYMBOL_GPL(adau1977_probe);
  816. static bool adau1977_register_volatile(struct device *dev, unsigned int reg)
  817. {
  818. switch (reg) {
  819. case ADAU1977_REG_STATUS(0):
  820. case ADAU1977_REG_STATUS(1):
  821. case ADAU1977_REG_STATUS(2):
  822. case ADAU1977_REG_STATUS(3):
  823. case ADAU1977_REG_ADC_CLIP:
  824. return true;
  825. }
  826. return false;
  827. }
  828. const struct regmap_config adau1977_regmap_config = {
  829. .max_register = ADAU1977_REG_DC_HPF_CAL,
  830. .volatile_reg = adau1977_register_volatile,
  831. .cache_type = REGCACHE_RBTREE,
  832. .reg_defaults = adau1977_reg_defaults,
  833. .num_reg_defaults = ARRAY_SIZE(adau1977_reg_defaults),
  834. };
  835. EXPORT_SYMBOL_GPL(adau1977_regmap_config);
  836. MODULE_DESCRIPTION("ASoC ADAU1977/ADAU1978/ADAU1979 driver");
  837. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  838. MODULE_LICENSE("GPL");