adau1977.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 Substraction 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 (codec->dapm.bias_level == 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. if (ret)
  411. return ret;
  412. codec->dapm.bias_level = level;
  413. return 0;
  414. }
  415. static int adau1977_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  416. unsigned int rx_mask, int slots, int width)
  417. {
  418. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  419. unsigned int ctrl0, ctrl1, drv;
  420. unsigned int slot[4];
  421. unsigned int i;
  422. int ret;
  423. if (slots == 0) {
  424. /* 0 = No fixed slot width */
  425. adau1977->slot_width = 0;
  426. adau1977->max_master_fs = 192000;
  427. return regmap_update_bits(adau1977->regmap,
  428. ADAU1977_REG_SAI_CTRL0, ADAU1977_SAI_CTRL0_SAI_MASK,
  429. ADAU1977_SAI_CTRL0_SAI_I2S);
  430. }
  431. if (rx_mask == 0 || tx_mask != 0)
  432. return -EINVAL;
  433. drv = 0;
  434. for (i = 0; i < 4; i++) {
  435. slot[i] = __ffs(rx_mask);
  436. drv |= ADAU1977_SAI_OVERTEMP_DRV_C(i);
  437. rx_mask &= ~(1 << slot[i]);
  438. if (slot[i] >= slots)
  439. return -EINVAL;
  440. if (rx_mask == 0)
  441. break;
  442. }
  443. if (rx_mask != 0)
  444. return -EINVAL;
  445. switch (width) {
  446. case 16:
  447. ctrl1 = ADAU1977_SAI_CTRL1_SLOT_WIDTH_16;
  448. break;
  449. case 24:
  450. /* We can only generate 16 bit or 32 bit wide slots */
  451. if (adau1977->master)
  452. return -EINVAL;
  453. ctrl1 = ADAU1977_SAI_CTRL1_SLOT_WIDTH_24;
  454. break;
  455. case 32:
  456. ctrl1 = ADAU1977_SAI_CTRL1_SLOT_WIDTH_32;
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. switch (slots) {
  462. case 2:
  463. ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_2;
  464. break;
  465. case 4:
  466. ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_4;
  467. break;
  468. case 8:
  469. ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_8;
  470. break;
  471. case 16:
  472. ctrl0 = ADAU1977_SAI_CTRL0_SAI_TDM_16;
  473. break;
  474. default:
  475. return -EINVAL;
  476. }
  477. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_OVERTEMP,
  478. ADAU1977_SAI_OVERTEMP_DRV_C(0) |
  479. ADAU1977_SAI_OVERTEMP_DRV_C(1) |
  480. ADAU1977_SAI_OVERTEMP_DRV_C(2) |
  481. ADAU1977_SAI_OVERTEMP_DRV_C(3), drv);
  482. if (ret)
  483. return ret;
  484. ret = regmap_write(adau1977->regmap, ADAU1977_REG_CMAP12,
  485. (slot[1] << ADAU1977_CHAN_MAP_SECOND_SLOT_OFFSET) |
  486. (slot[0] << ADAU1977_CHAN_MAP_FIRST_SLOT_OFFSET));
  487. if (ret)
  488. return ret;
  489. ret = regmap_write(adau1977->regmap, ADAU1977_REG_CMAP34,
  490. (slot[3] << ADAU1977_CHAN_MAP_SECOND_SLOT_OFFSET) |
  491. (slot[2] << ADAU1977_CHAN_MAP_FIRST_SLOT_OFFSET));
  492. if (ret)
  493. return ret;
  494. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL0,
  495. ADAU1977_SAI_CTRL0_SAI_MASK, ctrl0);
  496. if (ret)
  497. return ret;
  498. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL1,
  499. ADAU1977_SAI_CTRL1_SLOT_WIDTH_MASK, ctrl1);
  500. if (ret)
  501. return ret;
  502. adau1977->slot_width = width;
  503. /* In master mode the maximum bitclock is 24.576 MHz */
  504. adau1977->max_master_fs = min(192000, 24576000 / width / slots);
  505. return 0;
  506. }
  507. static int adau1977_mute(struct snd_soc_dai *dai, int mute, int stream)
  508. {
  509. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  510. unsigned int val;
  511. if (mute)
  512. val = ADAU1977_MISC_CONTROL_MMUTE;
  513. else
  514. val = 0;
  515. return regmap_update_bits(adau1977->regmap, ADAU1977_REG_MISC_CONTROL,
  516. ADAU1977_MISC_CONTROL_MMUTE, val);
  517. }
  518. static int adau1977_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  519. {
  520. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  521. unsigned int ctrl0 = 0, ctrl1 = 0, block_power = 0;
  522. bool invert_lrclk;
  523. int ret;
  524. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  525. case SND_SOC_DAIFMT_CBS_CFS:
  526. adau1977->master = false;
  527. break;
  528. case SND_SOC_DAIFMT_CBM_CFM:
  529. ctrl1 |= ADAU1977_SAI_CTRL1_MASTER;
  530. adau1977->master = true;
  531. break;
  532. default:
  533. return -EINVAL;
  534. }
  535. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  536. case SND_SOC_DAIFMT_NB_NF:
  537. invert_lrclk = false;
  538. break;
  539. case SND_SOC_DAIFMT_IB_NF:
  540. block_power |= ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE;
  541. invert_lrclk = false;
  542. break;
  543. case SND_SOC_DAIFMT_NB_IF:
  544. invert_lrclk = true;
  545. break;
  546. case SND_SOC_DAIFMT_IB_IF:
  547. block_power |= ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE;
  548. invert_lrclk = true;
  549. break;
  550. default:
  551. return -EINVAL;
  552. }
  553. adau1977->right_j = false;
  554. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  555. case SND_SOC_DAIFMT_I2S:
  556. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_I2S;
  557. break;
  558. case SND_SOC_DAIFMT_LEFT_J:
  559. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_LJ;
  560. invert_lrclk = !invert_lrclk;
  561. break;
  562. case SND_SOC_DAIFMT_RIGHT_J:
  563. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_RJ_24BIT;
  564. adau1977->right_j = true;
  565. invert_lrclk = !invert_lrclk;
  566. break;
  567. case SND_SOC_DAIFMT_DSP_A:
  568. ctrl1 |= ADAU1977_SAI_CTRL1_LRCLK_PULSE;
  569. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_I2S;
  570. invert_lrclk = false;
  571. break;
  572. case SND_SOC_DAIFMT_DSP_B:
  573. ctrl1 |= ADAU1977_SAI_CTRL1_LRCLK_PULSE;
  574. ctrl0 |= ADAU1977_SAI_CTRL0_FMT_LJ;
  575. invert_lrclk = false;
  576. break;
  577. default:
  578. return -EINVAL;
  579. }
  580. if (invert_lrclk)
  581. block_power |= ADAU1977_BLOCK_POWER_SAI_LR_POL;
  582. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_BLOCK_POWER_SAI,
  583. ADAU1977_BLOCK_POWER_SAI_LR_POL |
  584. ADAU1977_BLOCK_POWER_SAI_BCLK_EDGE, block_power);
  585. if (ret)
  586. return ret;
  587. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL0,
  588. ADAU1977_SAI_CTRL0_FMT_MASK,
  589. ctrl0);
  590. if (ret)
  591. return ret;
  592. return regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_CTRL1,
  593. ADAU1977_SAI_CTRL1_MASTER | ADAU1977_SAI_CTRL1_LRCLK_PULSE,
  594. ctrl1);
  595. }
  596. static int adau1977_startup(struct snd_pcm_substream *substream,
  597. struct snd_soc_dai *dai)
  598. {
  599. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  600. u64 formats = 0;
  601. if (adau1977->slot_width == 16)
  602. formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE;
  603. else if (adau1977->right_j || adau1977->slot_width == 24)
  604. formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  605. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE;
  606. snd_pcm_hw_constraint_list(substream->runtime, 0,
  607. SNDRV_PCM_HW_PARAM_RATE, &adau1977->constraints);
  608. if (adau1977->master)
  609. snd_pcm_hw_constraint_minmax(substream->runtime,
  610. SNDRV_PCM_HW_PARAM_RATE, 8000, adau1977->max_master_fs);
  611. if (formats != 0)
  612. snd_pcm_hw_constraint_mask64(substream->runtime,
  613. SNDRV_PCM_HW_PARAM_FORMAT, formats);
  614. return 0;
  615. }
  616. static int adau1977_set_tristate(struct snd_soc_dai *dai, int tristate)
  617. {
  618. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(dai->codec);
  619. unsigned int val;
  620. if (tristate)
  621. val = ADAU1977_SAI_OVERTEMP_DRV_HIZ;
  622. else
  623. val = 0;
  624. return regmap_update_bits(adau1977->regmap, ADAU1977_REG_SAI_OVERTEMP,
  625. ADAU1977_SAI_OVERTEMP_DRV_HIZ, val);
  626. }
  627. static const struct snd_soc_dai_ops adau1977_dai_ops = {
  628. .startup = adau1977_startup,
  629. .hw_params = adau1977_hw_params,
  630. .mute_stream = adau1977_mute,
  631. .set_fmt = adau1977_set_dai_fmt,
  632. .set_tdm_slot = adau1977_set_tdm_slot,
  633. .set_tristate = adau1977_set_tristate,
  634. };
  635. static struct snd_soc_dai_driver adau1977_dai = {
  636. .name = "adau1977-hifi",
  637. .capture = {
  638. .stream_name = "Capture",
  639. .channels_min = 1,
  640. .channels_max = 4,
  641. .rates = SNDRV_PCM_RATE_KNOT,
  642. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  643. SNDRV_PCM_FMTBIT_S32_LE,
  644. .sig_bits = 24,
  645. },
  646. .ops = &adau1977_dai_ops,
  647. };
  648. static const unsigned int adau1977_rates[] = {
  649. 8000, 16000, 32000, 64000, 128000,
  650. 11025, 22050, 44100, 88200, 172400,
  651. 12000, 24000, 48000, 96000, 192000,
  652. };
  653. #define ADAU1977_RATE_CONSTRAINT_MASK_32000 0x001f
  654. #define ADAU1977_RATE_CONSTRAINT_MASK_44100 0x03e0
  655. #define ADAU1977_RATE_CONSTRAINT_MASK_48000 0x7c00
  656. /* All rates >= 32000 */
  657. #define ADAU1977_RATE_CONSTRAINT_MASK_LRCLK 0x739c
  658. static bool adau1977_check_sysclk(unsigned int mclk, unsigned int base_freq)
  659. {
  660. unsigned int mcs;
  661. if (mclk % (base_freq * 128) != 0)
  662. return false;
  663. mcs = mclk / (128 * base_freq);
  664. if (mcs < 1 || mcs > 6 || mcs == 5)
  665. return false;
  666. return true;
  667. }
  668. static int adau1977_set_sysclk(struct snd_soc_codec *codec,
  669. int clk_id, int source, unsigned int freq, int dir)
  670. {
  671. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
  672. unsigned int mask = 0;
  673. unsigned int clk_src;
  674. unsigned int ret;
  675. if (dir != SND_SOC_CLOCK_IN)
  676. return -EINVAL;
  677. if (clk_id != ADAU1977_SYSCLK)
  678. return -EINVAL;
  679. switch (source) {
  680. case ADAU1977_SYSCLK_SRC_MCLK:
  681. clk_src = 0;
  682. break;
  683. case ADAU1977_SYSCLK_SRC_LRCLK:
  684. clk_src = ADAU1977_PLL_CLK_S;
  685. break;
  686. default:
  687. return -EINVAL;
  688. }
  689. if (freq != 0 && source == ADAU1977_SYSCLK_SRC_MCLK) {
  690. if (freq < 4000000 || freq > 36864000)
  691. return -EINVAL;
  692. if (adau1977_check_sysclk(freq, 32000))
  693. mask |= ADAU1977_RATE_CONSTRAINT_MASK_32000;
  694. if (adau1977_check_sysclk(freq, 44100))
  695. mask |= ADAU1977_RATE_CONSTRAINT_MASK_44100;
  696. if (adau1977_check_sysclk(freq, 48000))
  697. mask |= ADAU1977_RATE_CONSTRAINT_MASK_48000;
  698. if (mask == 0)
  699. return -EINVAL;
  700. } else if (source == ADAU1977_SYSCLK_SRC_LRCLK) {
  701. mask = ADAU1977_RATE_CONSTRAINT_MASK_LRCLK;
  702. }
  703. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_PLL,
  704. ADAU1977_PLL_CLK_S, clk_src);
  705. if (ret)
  706. return ret;
  707. adau1977->constraints.mask = mask;
  708. adau1977->sysclk_src = source;
  709. adau1977->sysclk = freq;
  710. return 0;
  711. }
  712. static int adau1977_codec_probe(struct snd_soc_codec *codec)
  713. {
  714. struct adau1977 *adau1977 = snd_soc_codec_get_drvdata(codec);
  715. int ret;
  716. switch (adau1977->type) {
  717. case ADAU1977:
  718. ret = snd_soc_dapm_new_controls(&codec->dapm,
  719. adau1977_micbias_dapm_widgets,
  720. ARRAY_SIZE(adau1977_micbias_dapm_widgets));
  721. if (ret < 0)
  722. return ret;
  723. break;
  724. default:
  725. break;
  726. }
  727. return 0;
  728. }
  729. static struct snd_soc_codec_driver adau1977_codec_driver = {
  730. .probe = adau1977_codec_probe,
  731. .set_bias_level = adau1977_set_bias_level,
  732. .set_sysclk = adau1977_set_sysclk,
  733. .idle_bias_off = true,
  734. .controls = adau1977_snd_controls,
  735. .num_controls = ARRAY_SIZE(adau1977_snd_controls),
  736. .dapm_widgets = adau1977_dapm_widgets,
  737. .num_dapm_widgets = ARRAY_SIZE(adau1977_dapm_widgets),
  738. .dapm_routes = adau1977_dapm_routes,
  739. .num_dapm_routes = ARRAY_SIZE(adau1977_dapm_routes),
  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(dev, "reset");
  784. if (IS_ERR(adau1977->reset_gpio)) {
  785. ret = PTR_ERR(adau1977->reset_gpio);
  786. if (ret != -ENOENT && ret != -ENOSYS)
  787. return PTR_ERR(adau1977->reset_gpio);
  788. adau1977->reset_gpio = NULL;
  789. }
  790. dev_set_drvdata(dev, adau1977);
  791. if (adau1977->reset_gpio) {
  792. ret = gpiod_direction_output(adau1977->reset_gpio, 0);
  793. if (ret)
  794. return ret;
  795. ndelay(100);
  796. }
  797. ret = adau1977_power_enable(adau1977);
  798. if (ret)
  799. return ret;
  800. if (type == ADAU1977) {
  801. ret = adau1977_setup_micbias(adau1977);
  802. if (ret)
  803. goto err_poweroff;
  804. }
  805. if (adau1977->dvdd_reg)
  806. power_off_mask = ~0;
  807. else
  808. power_off_mask = (unsigned int)~ADAU1977_BLOCK_POWER_SAI_LDO_EN;
  809. ret = regmap_update_bits(adau1977->regmap, ADAU1977_REG_BLOCK_POWER_SAI,
  810. power_off_mask, 0x00);
  811. if (ret)
  812. goto err_poweroff;
  813. ret = adau1977_power_disable(adau1977);
  814. if (ret)
  815. return ret;
  816. return snd_soc_register_codec(dev, &adau1977_codec_driver,
  817. &adau1977_dai, 1);
  818. err_poweroff:
  819. adau1977_power_disable(adau1977);
  820. return ret;
  821. }
  822. EXPORT_SYMBOL_GPL(adau1977_probe);
  823. static bool adau1977_register_volatile(struct device *dev, unsigned int reg)
  824. {
  825. switch (reg) {
  826. case ADAU1977_REG_STATUS(0):
  827. case ADAU1977_REG_STATUS(1):
  828. case ADAU1977_REG_STATUS(2):
  829. case ADAU1977_REG_STATUS(3):
  830. case ADAU1977_REG_ADC_CLIP:
  831. return true;
  832. }
  833. return false;
  834. }
  835. const struct regmap_config adau1977_regmap_config = {
  836. .max_register = ADAU1977_REG_DC_HPF_CAL,
  837. .volatile_reg = adau1977_register_volatile,
  838. .cache_type = REGCACHE_RBTREE,
  839. .reg_defaults = adau1977_reg_defaults,
  840. .num_reg_defaults = ARRAY_SIZE(adau1977_reg_defaults),
  841. };
  842. EXPORT_SYMBOL_GPL(adau1977_regmap_config);
  843. MODULE_DESCRIPTION("ASoC ADAU1977/ADAU1978/ADAU1979 driver");
  844. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  845. MODULE_LICENSE("GPL");