max98390.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * max98390.c -- MAX98390 ALSA Soc Audio driver
  4. *
  5. * Copyright (C) 2020 Maxim Integrated Products
  6. *
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/cdev.h>
  10. #include <linux/dmi.h>
  11. #include <linux/firmware.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/regmap.h>
  17. #include <linux/slab.h>
  18. #include <linux/time.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <sound/tlv.h>
  23. #include "max98390.h"
  24. static struct reg_default max98390_reg_defaults[] = {
  25. {MAX98390_INT_EN1, 0xf0},
  26. {MAX98390_INT_EN2, 0x00},
  27. {MAX98390_INT_EN3, 0x00},
  28. {MAX98390_INT_FLAG_CLR1, 0x00},
  29. {MAX98390_INT_FLAG_CLR2, 0x00},
  30. {MAX98390_INT_FLAG_CLR3, 0x00},
  31. {MAX98390_IRQ_CTRL, 0x01},
  32. {MAX98390_CLK_MON, 0x6d},
  33. {MAX98390_DAT_MON, 0x03},
  34. {MAX98390_WDOG_CTRL, 0x00},
  35. {MAX98390_WDOG_RST, 0x00},
  36. {MAX98390_MEAS_ADC_THERM_WARN_THRESH, 0x75},
  37. {MAX98390_MEAS_ADC_THERM_SHDN_THRESH, 0x8c},
  38. {MAX98390_MEAS_ADC_THERM_HYSTERESIS, 0x08},
  39. {MAX98390_PIN_CFG, 0x55},
  40. {MAX98390_PCM_RX_EN_A, 0x00},
  41. {MAX98390_PCM_RX_EN_B, 0x00},
  42. {MAX98390_PCM_TX_EN_A, 0x00},
  43. {MAX98390_PCM_TX_EN_B, 0x00},
  44. {MAX98390_PCM_TX_HIZ_CTRL_A, 0xff},
  45. {MAX98390_PCM_TX_HIZ_CTRL_B, 0xff},
  46. {MAX98390_PCM_CH_SRC_1, 0x00},
  47. {MAX98390_PCM_CH_SRC_2, 0x00},
  48. {MAX98390_PCM_CH_SRC_3, 0x00},
  49. {MAX98390_PCM_MODE_CFG, 0xc0},
  50. {MAX98390_PCM_MASTER_MODE, 0x1c},
  51. {MAX98390_PCM_CLK_SETUP, 0x44},
  52. {MAX98390_PCM_SR_SETUP, 0x08},
  53. {MAX98390_ICC_RX_EN_A, 0x00},
  54. {MAX98390_ICC_RX_EN_B, 0x00},
  55. {MAX98390_ICC_TX_EN_A, 0x00},
  56. {MAX98390_ICC_TX_EN_B, 0x00},
  57. {MAX98390_ICC_HIZ_MANUAL_MODE, 0x00},
  58. {MAX98390_ICC_TX_HIZ_EN_A, 0x00},
  59. {MAX98390_ICC_TX_HIZ_EN_B, 0x00},
  60. {MAX98390_ICC_LNK_EN, 0x00},
  61. {MAX98390_R2039_AMP_DSP_CFG, 0x0f},
  62. {MAX98390_R203A_AMP_EN, 0x81},
  63. {MAX98390_TONE_GEN_DC_CFG, 0x00},
  64. {MAX98390_SPK_SRC_SEL, 0x00},
  65. {MAX98390_SSM_CFG, 0x85},
  66. {MAX98390_MEAS_EN, 0x03},
  67. {MAX98390_MEAS_DSP_CFG, 0x0f},
  68. {MAX98390_BOOST_CTRL0, 0x1c},
  69. {MAX98390_BOOST_CTRL3, 0x01},
  70. {MAX98390_BOOST_CTRL1, 0x40},
  71. {MAX98390_MEAS_ADC_CFG, 0x07},
  72. {MAX98390_MEAS_ADC_BASE_MSB, 0x00},
  73. {MAX98390_MEAS_ADC_BASE_LSB, 0x23},
  74. {MAX98390_ADC_CH0_DIVIDE, 0x00},
  75. {MAX98390_ADC_CH1_DIVIDE, 0x00},
  76. {MAX98390_ADC_CH2_DIVIDE, 0x00},
  77. {MAX98390_ADC_CH0_FILT_CFG, 0x00},
  78. {MAX98390_ADC_CH1_FILT_CFG, 0x00},
  79. {MAX98390_ADC_CH2_FILT_CFG, 0x00},
  80. {MAX98390_PWR_GATE_CTL, 0x2c},
  81. {MAX98390_BROWNOUT_EN, 0x00},
  82. {MAX98390_BROWNOUT_INFINITE_HOLD, 0x00},
  83. {MAX98390_BROWNOUT_INFINITE_HOLD_CLR, 0x00},
  84. {MAX98390_BROWNOUT_LVL_HOLD, 0x00},
  85. {MAX98390_BROWNOUT_LVL1_THRESH, 0x00},
  86. {MAX98390_BROWNOUT_LVL2_THRESH, 0x00},
  87. {MAX98390_BROWNOUT_LVL3_THRESH, 0x00},
  88. {MAX98390_BROWNOUT_LVL4_THRESH, 0x00},
  89. {MAX98390_BROWNOUT_THRESH_HYSTERYSIS, 0x00},
  90. {MAX98390_BROWNOUT_AMP_LIMITER_ATK_REL, 0x1f},
  91. {MAX98390_BROWNOUT_AMP_GAIN_ATK_REL, 0x00},
  92. {MAX98390_BROWNOUT_AMP1_CLIP_MODE, 0x00},
  93. {MAX98390_BROWNOUT_LVL1_CUR_LIMIT, 0x00},
  94. {MAX98390_BROWNOUT_LVL1_AMP1_CTRL1, 0x00},
  95. {MAX98390_BROWNOUT_LVL1_AMP1_CTRL2, 0x00},
  96. {MAX98390_BROWNOUT_LVL1_AMP1_CTRL3, 0x00},
  97. {MAX98390_BROWNOUT_LVL2_CUR_LIMIT, 0x00},
  98. {MAX98390_BROWNOUT_LVL2_AMP1_CTRL1, 0x00},
  99. {MAX98390_BROWNOUT_LVL2_AMP1_CTRL2, 0x00},
  100. {MAX98390_BROWNOUT_LVL2_AMP1_CTRL3, 0x00},
  101. {MAX98390_BROWNOUT_LVL3_CUR_LIMIT, 0x00},
  102. {MAX98390_BROWNOUT_LVL3_AMP1_CTRL1, 0x00},
  103. {MAX98390_BROWNOUT_LVL3_AMP1_CTRL2, 0x00},
  104. {MAX98390_BROWNOUT_LVL3_AMP1_CTRL3, 0x00},
  105. {MAX98390_BROWNOUT_LVL4_CUR_LIMIT, 0x00},
  106. {MAX98390_BROWNOUT_LVL4_AMP1_CTRL1, 0x00},
  107. {MAX98390_BROWNOUT_LVL4_AMP1_CTRL2, 0x00},
  108. {MAX98390_BROWNOUT_LVL4_AMP1_CTRL3, 0x00},
  109. {MAX98390_BROWNOUT_ILIM_HLD, 0x00},
  110. {MAX98390_BROWNOUT_LIM_HLD, 0x00},
  111. {MAX98390_BROWNOUT_CLIP_HLD, 0x00},
  112. {MAX98390_BROWNOUT_GAIN_HLD, 0x00},
  113. {MAX98390_ENV_TRACK_VOUT_HEADROOM, 0x0f},
  114. {MAX98390_ENV_TRACK_BOOST_VOUT_DELAY, 0x80},
  115. {MAX98390_ENV_TRACK_REL_RATE, 0x07},
  116. {MAX98390_ENV_TRACK_HOLD_RATE, 0x07},
  117. {MAX98390_ENV_TRACK_CTRL, 0x01},
  118. {MAX98390_BOOST_BYPASS1, 0x49},
  119. {MAX98390_BOOST_BYPASS2, 0x2b},
  120. {MAX98390_BOOST_BYPASS3, 0x08},
  121. {MAX98390_FET_SCALING1, 0x00},
  122. {MAX98390_FET_SCALING2, 0x03},
  123. {MAX98390_FET_SCALING3, 0x00},
  124. {MAX98390_FET_SCALING4, 0x07},
  125. {MAX98390_SPK_SPEEDUP, 0x00},
  126. {DSMIG_WB_DRC_RELEASE_TIME_1, 0x00},
  127. {DSMIG_WB_DRC_RELEASE_TIME_2, 0x00},
  128. {DSMIG_WB_DRC_ATTACK_TIME_1, 0x00},
  129. {DSMIG_WB_DRC_ATTACK_TIME_2, 0x00},
  130. {DSMIG_WB_DRC_COMPRESSION_RATIO, 0x00},
  131. {DSMIG_WB_DRC_COMPRESSION_THRESHOLD, 0x00},
  132. {DSMIG_WB_DRC_MAKEUPGAIN, 0x00},
  133. {DSMIG_WB_DRC_NOISE_GATE_THRESHOLD, 0x00},
  134. {DSMIG_WBDRC_HPF_ENABLE, 0x00},
  135. {DSMIG_WB_DRC_TEST_SMOOTHER_OUT_EN, 0x00},
  136. {DSMIG_PPR_THRESHOLD, 0x00},
  137. {DSM_STEREO_BASS_CHANNEL_SELECT, 0x00},
  138. {DSM_TPROT_THRESHOLD_BYTE0, 0x00},
  139. {DSM_TPROT_THRESHOLD_BYTE1, 0x00},
  140. {DSM_TPROT_ROOM_TEMPERATURE_BYTE0, 0x00},
  141. {DSM_TPROT_ROOM_TEMPERATURE_BYTE1, 0x00},
  142. {DSM_TPROT_RECIP_RDC_ROOM_BYTE0, 0x00},
  143. {DSM_TPROT_RECIP_RDC_ROOM_BYTE1, 0x00},
  144. {DSM_TPROT_RECIP_RDC_ROOM_BYTE2, 0x00},
  145. {DSM_TPROT_RECIP_TCONST_BYTE0, 0x00},
  146. {DSM_TPROT_RECIP_TCONST_BYTE1, 0x00},
  147. {DSM_TPROT_RECIP_TCONST_BYTE2, 0x00},
  148. {DSM_THERMAL_ATTENUATION_SETTINGS, 0x00},
  149. {DSM_THERMAL_PILOT_TONE_ATTENUATION, 0x00},
  150. {DSM_TPROT_PG_TEMP_THRESH_BYTE0, 0x00},
  151. {DSM_TPROT_PG_TEMP_THRESH_BYTE1, 0x00},
  152. {DSMIG_DEBUZZER_THRESHOLD, 0x00},
  153. {DSMIG_DEBUZZER_ALPHA_COEF_TEST_ONLY, 0x08},
  154. {DSM_VOL_ENA, 0x20},
  155. {DSM_VOL_CTRL, 0xa0},
  156. {DSMIG_EN, 0x00},
  157. {MAX98390_R23E1_DSP_GLOBAL_EN, 0x00},
  158. {MAX98390_R23FF_GLOBAL_EN, 0x00},
  159. };
  160. static int max98390_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  161. {
  162. struct snd_soc_component *component = codec_dai->component;
  163. struct max98390_priv *max98390 =
  164. snd_soc_component_get_drvdata(component);
  165. unsigned int mode;
  166. unsigned int format;
  167. unsigned int invert = 0;
  168. dev_dbg(component->dev, "%s: fmt 0x%08X\n", __func__, fmt);
  169. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  170. case SND_SOC_DAIFMT_CBC_CFC:
  171. mode = MAX98390_PCM_MASTER_MODE_SLAVE;
  172. break;
  173. case SND_SOC_DAIFMT_CBP_CFP:
  174. max98390->provider = true;
  175. mode = MAX98390_PCM_MASTER_MODE_MASTER;
  176. break;
  177. default:
  178. dev_err(component->dev, "DAI clock mode unsupported\n");
  179. return -EINVAL;
  180. }
  181. regmap_update_bits(max98390->regmap,
  182. MAX98390_PCM_MASTER_MODE,
  183. MAX98390_PCM_MASTER_MODE_MASK,
  184. mode);
  185. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  186. case SND_SOC_DAIFMT_NB_NF:
  187. break;
  188. case SND_SOC_DAIFMT_IB_NF:
  189. invert = MAX98390_PCM_MODE_CFG_PCM_BCLKEDGE;
  190. break;
  191. default:
  192. dev_err(component->dev, "DAI invert mode unsupported\n");
  193. return -EINVAL;
  194. }
  195. regmap_update_bits(max98390->regmap,
  196. MAX98390_PCM_MODE_CFG,
  197. MAX98390_PCM_MODE_CFG_PCM_BCLKEDGE,
  198. invert);
  199. /* interface format */
  200. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  201. case SND_SOC_DAIFMT_I2S:
  202. format = MAX98390_PCM_FORMAT_I2S;
  203. break;
  204. case SND_SOC_DAIFMT_LEFT_J:
  205. format = MAX98390_PCM_FORMAT_LJ;
  206. break;
  207. case SND_SOC_DAIFMT_DSP_A:
  208. format = MAX98390_PCM_FORMAT_TDM_MODE1;
  209. break;
  210. case SND_SOC_DAIFMT_DSP_B:
  211. format = MAX98390_PCM_FORMAT_TDM_MODE0;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. regmap_update_bits(max98390->regmap,
  217. MAX98390_PCM_MODE_CFG,
  218. MAX98390_PCM_MODE_CFG_FORMAT_MASK,
  219. format << MAX98390_PCM_MODE_CFG_FORMAT_SHIFT);
  220. return 0;
  221. }
  222. static int max98390_get_bclk_sel(int bclk)
  223. {
  224. int i;
  225. /* BCLKs per LRCLK */
  226. static int bclk_sel_table[] = {
  227. 32, 48, 64, 96, 128, 192, 256, 320, 384, 512,
  228. };
  229. /* match BCLKs per LRCLK */
  230. for (i = 0; i < ARRAY_SIZE(bclk_sel_table); i++) {
  231. if (bclk_sel_table[i] == bclk)
  232. return i + 2;
  233. }
  234. return 0;
  235. }
  236. static int max98390_set_clock(struct snd_soc_component *component,
  237. struct snd_pcm_hw_params *params)
  238. {
  239. struct max98390_priv *max98390 =
  240. snd_soc_component_get_drvdata(component);
  241. /* codec MCLK rate in master mode */
  242. static int rate_table[] = {
  243. 5644800, 6000000, 6144000, 6500000,
  244. 9600000, 11289600, 12000000, 12288000,
  245. 13000000, 19200000,
  246. };
  247. /* BCLK/LRCLK ratio calculation */
  248. int blr_clk_ratio = params_channels(params)
  249. * snd_pcm_format_width(params_format(params));
  250. int value;
  251. if (max98390->provider) {
  252. int i;
  253. /* match rate to closest value */
  254. for (i = 0; i < ARRAY_SIZE(rate_table); i++) {
  255. if (rate_table[i] >= max98390->sysclk)
  256. break;
  257. }
  258. if (i == ARRAY_SIZE(rate_table)) {
  259. dev_err(component->dev, "failed to find proper clock rate.\n");
  260. return -EINVAL;
  261. }
  262. regmap_update_bits(max98390->regmap,
  263. MAX98390_PCM_MASTER_MODE,
  264. MAX98390_PCM_MASTER_MODE_MCLK_MASK,
  265. i << MAX98390_PCM_MASTER_MODE_MCLK_RATE_SHIFT);
  266. }
  267. if (!max98390->tdm_mode) {
  268. /* BCLK configuration */
  269. value = max98390_get_bclk_sel(blr_clk_ratio);
  270. if (!value) {
  271. dev_err(component->dev, "format unsupported %d\n",
  272. params_format(params));
  273. return -EINVAL;
  274. }
  275. regmap_update_bits(max98390->regmap,
  276. MAX98390_PCM_CLK_SETUP,
  277. MAX98390_PCM_CLK_SETUP_BSEL_MASK,
  278. value);
  279. }
  280. return 0;
  281. }
  282. static int max98390_dai_hw_params(struct snd_pcm_substream *substream,
  283. struct snd_pcm_hw_params *params,
  284. struct snd_soc_dai *dai)
  285. {
  286. struct snd_soc_component *component =
  287. dai->component;
  288. struct max98390_priv *max98390 =
  289. snd_soc_component_get_drvdata(component);
  290. unsigned int sampling_rate;
  291. unsigned int chan_sz;
  292. /* pcm mode configuration */
  293. switch (snd_pcm_format_width(params_format(params))) {
  294. case 16:
  295. chan_sz = MAX98390_PCM_MODE_CFG_CHANSZ_16;
  296. break;
  297. case 24:
  298. chan_sz = MAX98390_PCM_MODE_CFG_CHANSZ_24;
  299. break;
  300. case 32:
  301. chan_sz = MAX98390_PCM_MODE_CFG_CHANSZ_32;
  302. break;
  303. default:
  304. dev_err(component->dev, "format unsupported %d\n",
  305. params_format(params));
  306. goto err;
  307. }
  308. regmap_update_bits(max98390->regmap,
  309. MAX98390_PCM_MODE_CFG,
  310. MAX98390_PCM_MODE_CFG_CHANSZ_MASK, chan_sz);
  311. dev_dbg(component->dev, "format supported %d",
  312. params_format(params));
  313. /* sampling rate configuration */
  314. switch (params_rate(params)) {
  315. case 8000:
  316. sampling_rate = MAX98390_PCM_SR_SET1_SR_8000;
  317. break;
  318. case 11025:
  319. sampling_rate = MAX98390_PCM_SR_SET1_SR_11025;
  320. break;
  321. case 12000:
  322. sampling_rate = MAX98390_PCM_SR_SET1_SR_12000;
  323. break;
  324. case 16000:
  325. sampling_rate = MAX98390_PCM_SR_SET1_SR_16000;
  326. break;
  327. case 22050:
  328. sampling_rate = MAX98390_PCM_SR_SET1_SR_22050;
  329. break;
  330. case 24000:
  331. sampling_rate = MAX98390_PCM_SR_SET1_SR_24000;
  332. break;
  333. case 32000:
  334. sampling_rate = MAX98390_PCM_SR_SET1_SR_32000;
  335. break;
  336. case 44100:
  337. sampling_rate = MAX98390_PCM_SR_SET1_SR_44100;
  338. break;
  339. case 48000:
  340. sampling_rate = MAX98390_PCM_SR_SET1_SR_48000;
  341. break;
  342. default:
  343. dev_err(component->dev, "rate %d not supported\n",
  344. params_rate(params));
  345. goto err;
  346. }
  347. /* set DAI_SR to correct LRCLK frequency */
  348. regmap_update_bits(max98390->regmap,
  349. MAX98390_PCM_SR_SETUP,
  350. MAX98390_PCM_SR_SET1_SR_MASK,
  351. sampling_rate);
  352. return max98390_set_clock(component, params);
  353. err:
  354. return -EINVAL;
  355. }
  356. static int max98390_dai_tdm_slot(struct snd_soc_dai *dai,
  357. unsigned int tx_mask, unsigned int rx_mask,
  358. int slots, int slot_width)
  359. {
  360. struct snd_soc_component *component = dai->component;
  361. struct max98390_priv *max98390 =
  362. snd_soc_component_get_drvdata(component);
  363. int bsel;
  364. unsigned int chan_sz;
  365. if (!tx_mask && !rx_mask && !slots && !slot_width)
  366. max98390->tdm_mode = false;
  367. else
  368. max98390->tdm_mode = true;
  369. dev_dbg(component->dev,
  370. "Tdm mode : %d\n", max98390->tdm_mode);
  371. /* BCLK configuration */
  372. bsel = max98390_get_bclk_sel(slots * slot_width);
  373. if (!bsel) {
  374. dev_err(component->dev, "BCLK %d not supported\n",
  375. slots * slot_width);
  376. return -EINVAL;
  377. }
  378. regmap_update_bits(max98390->regmap,
  379. MAX98390_PCM_CLK_SETUP,
  380. MAX98390_PCM_CLK_SETUP_BSEL_MASK,
  381. bsel);
  382. /* Channel size configuration */
  383. switch (slot_width) {
  384. case 16:
  385. chan_sz = MAX98390_PCM_MODE_CFG_CHANSZ_16;
  386. break;
  387. case 24:
  388. chan_sz = MAX98390_PCM_MODE_CFG_CHANSZ_24;
  389. break;
  390. case 32:
  391. chan_sz = MAX98390_PCM_MODE_CFG_CHANSZ_32;
  392. break;
  393. default:
  394. dev_err(component->dev, "format unsupported %d\n",
  395. slot_width);
  396. return -EINVAL;
  397. }
  398. regmap_update_bits(max98390->regmap,
  399. MAX98390_PCM_MODE_CFG,
  400. MAX98390_PCM_MODE_CFG_CHANSZ_MASK, chan_sz);
  401. /* Rx slot configuration */
  402. regmap_write(max98390->regmap,
  403. MAX98390_PCM_RX_EN_A,
  404. rx_mask & 0xFF);
  405. regmap_write(max98390->regmap,
  406. MAX98390_PCM_RX_EN_B,
  407. (rx_mask & 0xFF00) >> 8);
  408. /* Tx slot Hi-Z configuration */
  409. regmap_write(max98390->regmap,
  410. MAX98390_PCM_TX_HIZ_CTRL_A,
  411. ~tx_mask & 0xFF);
  412. regmap_write(max98390->regmap,
  413. MAX98390_PCM_TX_HIZ_CTRL_B,
  414. (~tx_mask & 0xFF00) >> 8);
  415. return 0;
  416. }
  417. static int max98390_dai_set_sysclk(struct snd_soc_dai *dai,
  418. int clk_id, unsigned int freq, int dir)
  419. {
  420. struct snd_soc_component *component = dai->component;
  421. struct max98390_priv *max98390 =
  422. snd_soc_component_get_drvdata(component);
  423. max98390->sysclk = freq;
  424. return 0;
  425. }
  426. static const struct snd_soc_dai_ops max98390_dai_ops = {
  427. .set_sysclk = max98390_dai_set_sysclk,
  428. .set_fmt = max98390_dai_set_fmt,
  429. .hw_params = max98390_dai_hw_params,
  430. .set_tdm_slot = max98390_dai_tdm_slot,
  431. };
  432. static int max98390_dac_event(struct snd_soc_dapm_widget *w,
  433. struct snd_kcontrol *kcontrol, int event)
  434. {
  435. struct snd_soc_component *component =
  436. snd_soc_dapm_to_component(w->dapm);
  437. struct max98390_priv *max98390 =
  438. snd_soc_component_get_drvdata(component);
  439. switch (event) {
  440. case SND_SOC_DAPM_POST_PMU:
  441. regmap_update_bits(max98390->regmap,
  442. MAX98390_R203A_AMP_EN,
  443. MAX98390_AMP_EN_MASK, 1);
  444. regmap_update_bits(max98390->regmap,
  445. MAX98390_R23FF_GLOBAL_EN,
  446. MAX98390_GLOBAL_EN_MASK, 1);
  447. break;
  448. case SND_SOC_DAPM_POST_PMD:
  449. regmap_update_bits(max98390->regmap,
  450. MAX98390_R23FF_GLOBAL_EN,
  451. MAX98390_GLOBAL_EN_MASK, 0);
  452. regmap_update_bits(max98390->regmap,
  453. MAX98390_R203A_AMP_EN,
  454. MAX98390_AMP_EN_MASK, 0);
  455. break;
  456. }
  457. return 0;
  458. }
  459. static const char * const max98390_switch_text[] = {
  460. "Left", "Right", "LeftRight"};
  461. static const char * const max98390_boost_voltage_text[] = {
  462. "6.5V", "6.625V", "6.75V", "6.875V", "7V", "7.125V", "7.25V", "7.375V",
  463. "7.5V", "7.625V", "7.75V", "7.875V", "8V", "8.125V", "8.25V", "8.375V",
  464. "8.5V", "8.625V", "8.75V", "8.875V", "9V", "9.125V", "9.25V", "9.375V",
  465. "9.5V", "9.625V", "9.75V", "9.875V", "10V"
  466. };
  467. static SOC_ENUM_SINGLE_DECL(max98390_boost_voltage,
  468. MAX98390_BOOST_CTRL0, 0,
  469. max98390_boost_voltage_text);
  470. static DECLARE_TLV_DB_SCALE(max98390_spk_tlv, 300, 300, 0);
  471. static DECLARE_TLV_DB_SCALE(max98390_digital_tlv, -8000, 50, 0);
  472. static const char * const max98390_current_limit_text[] = {
  473. "0.00A", "0.50A", "1.00A", "1.05A", "1.10A", "1.15A", "1.20A", "1.25A",
  474. "1.30A", "1.35A", "1.40A", "1.45A", "1.50A", "1.55A", "1.60A", "1.65A",
  475. "1.70A", "1.75A", "1.80A", "1.85A", "1.90A", "1.95A", "2.00A", "2.05A",
  476. "2.10A", "2.15A", "2.20A", "2.25A", "2.30A", "2.35A", "2.40A", "2.45A",
  477. "2.50A", "2.55A", "2.60A", "2.65A", "2.70A", "2.75A", "2.80A", "2.85A",
  478. "2.90A", "2.95A", "3.00A", "3.05A", "3.10A", "3.15A", "3.20A", "3.25A",
  479. "3.30A", "3.35A", "3.40A", "3.45A", "3.50A", "3.55A", "3.60A", "3.65A",
  480. "3.70A", "3.75A", "3.80A", "3.85A", "3.90A", "3.95A", "4.00A", "4.05A",
  481. "4.10A"
  482. };
  483. static SOC_ENUM_SINGLE_DECL(max98390_current_limit,
  484. MAX98390_BOOST_CTRL1, 0,
  485. max98390_current_limit_text);
  486. static int max98390_ref_rdc_put(struct snd_kcontrol *kcontrol,
  487. struct snd_ctl_elem_value *ucontrol)
  488. {
  489. struct snd_soc_component *component =
  490. snd_soc_kcontrol_component(kcontrol);
  491. struct max98390_priv *max98390 =
  492. snd_soc_component_get_drvdata(component);
  493. max98390->ref_rdc_value = ucontrol->value.integer.value[0];
  494. regmap_write(max98390->regmap, DSM_TPROT_RECIP_RDC_ROOM_BYTE0,
  495. max98390->ref_rdc_value & 0x000000ff);
  496. regmap_write(max98390->regmap, DSM_TPROT_RECIP_RDC_ROOM_BYTE1,
  497. (max98390->ref_rdc_value >> 8) & 0x000000ff);
  498. regmap_write(max98390->regmap, DSM_TPROT_RECIP_RDC_ROOM_BYTE2,
  499. (max98390->ref_rdc_value >> 16) & 0x000000ff);
  500. return 0;
  501. }
  502. static int max98390_ref_rdc_get(struct snd_kcontrol *kcontrol,
  503. struct snd_ctl_elem_value *ucontrol)
  504. {
  505. struct snd_soc_component *component =
  506. snd_soc_kcontrol_component(kcontrol);
  507. struct max98390_priv *max98390 =
  508. snd_soc_component_get_drvdata(component);
  509. ucontrol->value.integer.value[0] = max98390->ref_rdc_value;
  510. return 0;
  511. }
  512. static int max98390_ambient_temp_put(struct snd_kcontrol *kcontrol,
  513. struct snd_ctl_elem_value *ucontrol)
  514. {
  515. struct snd_soc_component *component =
  516. snd_soc_kcontrol_component(kcontrol);
  517. struct max98390_priv *max98390 =
  518. snd_soc_component_get_drvdata(component);
  519. max98390->ambient_temp_value = ucontrol->value.integer.value[0];
  520. regmap_write(max98390->regmap, DSM_TPROT_ROOM_TEMPERATURE_BYTE1,
  521. (max98390->ambient_temp_value >> 8) & 0x000000ff);
  522. regmap_write(max98390->regmap, DSM_TPROT_ROOM_TEMPERATURE_BYTE0,
  523. (max98390->ambient_temp_value) & 0x000000ff);
  524. return 0;
  525. }
  526. static int max98390_ambient_temp_get(struct snd_kcontrol *kcontrol,
  527. struct snd_ctl_elem_value *ucontrol)
  528. {
  529. struct snd_soc_component *component =
  530. snd_soc_kcontrol_component(kcontrol);
  531. struct max98390_priv *max98390 =
  532. snd_soc_component_get_drvdata(component);
  533. ucontrol->value.integer.value[0] = max98390->ambient_temp_value;
  534. return 0;
  535. }
  536. static int max98390_adaptive_rdc_put(struct snd_kcontrol *kcontrol,
  537. struct snd_ctl_elem_value *ucontrol)
  538. {
  539. struct snd_soc_component *component =
  540. snd_soc_kcontrol_component(kcontrol);
  541. dev_warn(component->dev, "Put adaptive rdc not supported\n");
  542. return 0;
  543. }
  544. static int max98390_adaptive_rdc_get(struct snd_kcontrol *kcontrol,
  545. struct snd_ctl_elem_value *ucontrol)
  546. {
  547. int rdc, rdc0;
  548. struct snd_soc_component *component =
  549. snd_soc_kcontrol_component(kcontrol);
  550. struct max98390_priv *max98390 =
  551. snd_soc_component_get_drvdata(component);
  552. regmap_read(max98390->regmap, THERMAL_RDC_RD_BACK_BYTE1, &rdc);
  553. regmap_read(max98390->regmap, THERMAL_RDC_RD_BACK_BYTE0, &rdc0);
  554. ucontrol->value.integer.value[0] = rdc0 | rdc << 8;
  555. return 0;
  556. }
  557. static int max98390_dsm_calib_get(struct snd_kcontrol *kcontrol,
  558. struct snd_ctl_elem_value *ucontrol)
  559. {
  560. /* Do nothing */
  561. return 0;
  562. }
  563. static int max98390_dsm_calib_put(struct snd_kcontrol *kcontrol,
  564. struct snd_ctl_elem_value *ucontrol)
  565. {
  566. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  567. struct max98390_priv *max98390 = snd_soc_component_get_drvdata(component);
  568. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  569. unsigned int rdc, rdc_cal_result, rdc_integer, rdc_factor, temp, val;
  570. snd_soc_dapm_mutex_lock(dapm);
  571. regmap_read(max98390->regmap, MAX98390_R23FF_GLOBAL_EN, &val);
  572. if (!val) {
  573. /* Enable the codec for the duration of calibration readout */
  574. regmap_update_bits(max98390->regmap, MAX98390_R203A_AMP_EN,
  575. MAX98390_AMP_EN_MASK, 1);
  576. regmap_update_bits(max98390->regmap, MAX98390_R23FF_GLOBAL_EN,
  577. MAX98390_GLOBAL_EN_MASK, 1);
  578. }
  579. regmap_read(max98390->regmap, THERMAL_RDC_RD_BACK_BYTE1, &rdc);
  580. regmap_read(max98390->regmap, THERMAL_RDC_RD_BACK_BYTE0, &rdc_cal_result);
  581. regmap_read(max98390->regmap, MAX98390_MEAS_ADC_CH2_READ, &temp);
  582. if (!val) {
  583. /* Disable the codec if it was disabled */
  584. regmap_update_bits(max98390->regmap, MAX98390_R23FF_GLOBAL_EN,
  585. MAX98390_GLOBAL_EN_MASK, 0);
  586. regmap_update_bits(max98390->regmap, MAX98390_R203A_AMP_EN,
  587. MAX98390_AMP_EN_MASK, 0);
  588. }
  589. snd_soc_dapm_mutex_unlock(dapm);
  590. rdc_cal_result |= (rdc << 8) & 0x0000FFFF;
  591. if (rdc_cal_result)
  592. max98390->ref_rdc_value = 268435456U / rdc_cal_result;
  593. max98390->ambient_temp_value = temp * 52 - 1188;
  594. rdc_integer = rdc_cal_result * 937 / 65536;
  595. rdc_factor = ((rdc_cal_result * 937 * 100) / 65536) - (rdc_integer * 100);
  596. dev_info(component->dev,
  597. "rdc resistance about %d.%02d ohm, reg=0x%X temp reg=0x%X\n",
  598. rdc_integer, rdc_factor, rdc_cal_result, temp);
  599. return 0;
  600. }
  601. static const struct snd_kcontrol_new max98390_snd_controls[] = {
  602. SOC_SINGLE_TLV("Digital Volume", DSM_VOL_CTRL,
  603. 0, 184, 0,
  604. max98390_digital_tlv),
  605. SOC_SINGLE_TLV("Speaker Volume", MAX98390_R203D_SPK_GAIN,
  606. 0, 6, 0,
  607. max98390_spk_tlv),
  608. SOC_SINGLE("Ramp Up Bypass Switch", MAX98390_R2039_AMP_DSP_CFG,
  609. MAX98390_AMP_DSP_CFG_RMP_UP_SHIFT, 1, 0),
  610. SOC_SINGLE("Ramp Down Bypass Switch", MAX98390_R2039_AMP_DSP_CFG,
  611. MAX98390_AMP_DSP_CFG_RMP_DN_SHIFT, 1, 0),
  612. SOC_SINGLE("Boost Clock Phase", MAX98390_BOOST_CTRL3,
  613. MAX98390_BOOST_CLK_PHASE_CFG_SHIFT, 3, 0),
  614. SOC_ENUM("Boost Output Voltage", max98390_boost_voltage),
  615. SOC_ENUM("Current Limit", max98390_current_limit),
  616. SOC_SINGLE_EXT("DSM Rdc", SND_SOC_NOPM, 0, 0xffffff, 0,
  617. max98390_ref_rdc_get, max98390_ref_rdc_put),
  618. SOC_SINGLE_EXT("DSM Ambient Temp", SND_SOC_NOPM, 0, 0xffff, 0,
  619. max98390_ambient_temp_get, max98390_ambient_temp_put),
  620. SOC_SINGLE_EXT("DSM Adaptive Rdc", SND_SOC_NOPM, 0, 0xffff, 0,
  621. max98390_adaptive_rdc_get, max98390_adaptive_rdc_put),
  622. SOC_SINGLE_EXT("DSM Calibration", SND_SOC_NOPM, 0, 1, 0,
  623. max98390_dsm_calib_get, max98390_dsm_calib_put),
  624. };
  625. static const struct soc_enum dai_sel_enum =
  626. SOC_ENUM_SINGLE(MAX98390_PCM_CH_SRC_1,
  627. MAX98390_PCM_RX_CH_SRC_SHIFT,
  628. 3, max98390_switch_text);
  629. static const struct snd_kcontrol_new max98390_dai_controls =
  630. SOC_DAPM_ENUM("DAI Sel", dai_sel_enum);
  631. static const struct snd_soc_dapm_widget max98390_dapm_widgets[] = {
  632. SND_SOC_DAPM_DAC_E("Amp Enable", "HiFi Playback",
  633. SND_SOC_NOPM, 0, 0, max98390_dac_event,
  634. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  635. SND_SOC_DAPM_MUX("DAI Sel Mux", SND_SOC_NOPM, 0, 0,
  636. &max98390_dai_controls),
  637. SND_SOC_DAPM_OUTPUT("BE_OUT"),
  638. };
  639. static const struct snd_soc_dapm_route max98390_audio_map[] = {
  640. /* Plabyack */
  641. {"DAI Sel Mux", "Left", "Amp Enable"},
  642. {"DAI Sel Mux", "Right", "Amp Enable"},
  643. {"DAI Sel Mux", "LeftRight", "Amp Enable"},
  644. {"BE_OUT", NULL, "DAI Sel Mux"},
  645. };
  646. static bool max98390_readable_register(struct device *dev, unsigned int reg)
  647. {
  648. switch (reg) {
  649. case MAX98390_SOFTWARE_RESET ... MAX98390_INT_EN3:
  650. case MAX98390_IRQ_CTRL ... MAX98390_WDOG_CTRL:
  651. case MAX98390_MEAS_ADC_THERM_WARN_THRESH
  652. ... MAX98390_BROWNOUT_INFINITE_HOLD:
  653. case MAX98390_BROWNOUT_LVL_HOLD ... DSMIG_DEBUZZER_THRESHOLD:
  654. case DSM_VOL_ENA ... MAX98390_R24FF_REV_ID:
  655. return true;
  656. default:
  657. return false;
  658. }
  659. };
  660. static bool max98390_volatile_reg(struct device *dev, unsigned int reg)
  661. {
  662. switch (reg) {
  663. case MAX98390_SOFTWARE_RESET ... MAX98390_INT_EN3:
  664. case MAX98390_MEAS_ADC_CH0_READ ... MAX98390_MEAS_ADC_CH2_READ:
  665. case MAX98390_PWR_GATE_STATUS ... MAX98390_BROWNOUT_STATUS:
  666. case MAX98390_BROWNOUT_LOWEST_STATUS:
  667. case MAX98390_ENV_TRACK_BOOST_VOUT_READ:
  668. case DSM_STBASS_HPF_B0_BYTE0 ... DSM_DEBUZZER_ATTACK_TIME_BYTE2:
  669. case THERMAL_RDC_RD_BACK_BYTE1 ... DSMIG_DEBUZZER_THRESHOLD:
  670. case DSM_THERMAL_GAIN ... DSM_WBDRC_GAIN:
  671. return true;
  672. default:
  673. return false;
  674. }
  675. }
  676. #define MAX98390_RATES SNDRV_PCM_RATE_8000_48000
  677. #define MAX98390_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  678. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  679. static struct snd_soc_dai_driver max98390_dai[] = {
  680. {
  681. .name = "max98390-aif1",
  682. .playback = {
  683. .stream_name = "HiFi Playback",
  684. .channels_min = 1,
  685. .channels_max = 2,
  686. .rates = MAX98390_RATES,
  687. .formats = MAX98390_FORMATS,
  688. },
  689. .capture = {
  690. .stream_name = "HiFi Capture",
  691. .channels_min = 1,
  692. .channels_max = 2,
  693. .rates = MAX98390_RATES,
  694. .formats = MAX98390_FORMATS,
  695. },
  696. .ops = &max98390_dai_ops,
  697. }
  698. };
  699. static int max98390_dsm_init(struct snd_soc_component *component)
  700. {
  701. int ret;
  702. int param_size, param_start_addr;
  703. char filename[128];
  704. const char *vendor, *product;
  705. struct max98390_priv *max98390 =
  706. snd_soc_component_get_drvdata(component);
  707. const struct firmware *fw;
  708. char *dsm_param;
  709. vendor = dmi_get_system_info(DMI_SYS_VENDOR);
  710. product = dmi_get_system_info(DMI_PRODUCT_NAME);
  711. if (!strcmp(max98390->dsm_param_name, "default")) {
  712. if (vendor && product) {
  713. snprintf(filename, sizeof(filename),
  714. "dsm_param_%s_%s.bin", vendor, product);
  715. } else {
  716. sprintf(filename, "dsm_param.bin");
  717. }
  718. } else {
  719. snprintf(filename, sizeof(filename), "%s",
  720. max98390->dsm_param_name);
  721. }
  722. ret = request_firmware(&fw, filename, component->dev);
  723. if (ret) {
  724. ret = request_firmware(&fw, "dsm_param.bin", component->dev);
  725. if (ret) {
  726. ret = request_firmware(&fw, "dsmparam.bin",
  727. component->dev);
  728. if (ret)
  729. goto err;
  730. }
  731. }
  732. dev_dbg(component->dev,
  733. "max98390: param fw size %zd\n",
  734. fw->size);
  735. if (fw->size < MAX98390_DSM_PARAM_MIN_SIZE) {
  736. dev_err(component->dev,
  737. "param fw is invalid.\n");
  738. ret = -EINVAL;
  739. goto err_alloc;
  740. }
  741. dsm_param = (char *)fw->data;
  742. param_start_addr = (dsm_param[0] & 0xff) | (dsm_param[1] & 0xff) << 8;
  743. param_size = (dsm_param[2] & 0xff) | (dsm_param[3] & 0xff) << 8;
  744. if (param_size > MAX98390_DSM_PARAM_MAX_SIZE ||
  745. param_start_addr < MAX98390_IRQ_CTRL ||
  746. fw->size < param_size + MAX98390_DSM_PAYLOAD_OFFSET) {
  747. dev_err(component->dev,
  748. "param fw is invalid.\n");
  749. ret = -EINVAL;
  750. goto err_alloc;
  751. }
  752. regmap_write(max98390->regmap, MAX98390_R203A_AMP_EN, 0x80);
  753. dsm_param += MAX98390_DSM_PAYLOAD_OFFSET;
  754. regmap_bulk_write(max98390->regmap, param_start_addr,
  755. dsm_param, param_size);
  756. regmap_write(max98390->regmap, MAX98390_R23E1_DSP_GLOBAL_EN, 0x01);
  757. err_alloc:
  758. release_firmware(fw);
  759. err:
  760. return ret;
  761. }
  762. static void max98390_init_regs(struct snd_soc_component *component)
  763. {
  764. struct max98390_priv *max98390 =
  765. snd_soc_component_get_drvdata(component);
  766. regmap_write(max98390->regmap, MAX98390_CLK_MON, 0x6f);
  767. regmap_write(max98390->regmap, MAX98390_DAT_MON, 0x00);
  768. regmap_write(max98390->regmap, MAX98390_PWR_GATE_CTL, 0x00);
  769. regmap_write(max98390->regmap, MAX98390_PCM_RX_EN_A, 0x03);
  770. regmap_write(max98390->regmap, MAX98390_ENV_TRACK_VOUT_HEADROOM, 0x0e);
  771. regmap_write(max98390->regmap, MAX98390_BOOST_BYPASS1, 0x46);
  772. regmap_write(max98390->regmap, MAX98390_FET_SCALING3, 0x03);
  773. /* voltage, current slot configuration */
  774. regmap_write(max98390->regmap,
  775. MAX98390_PCM_CH_SRC_2,
  776. (max98390->i_l_slot << 4 |
  777. max98390->v_l_slot)&0xFF);
  778. if (max98390->v_l_slot < 8) {
  779. regmap_update_bits(max98390->regmap,
  780. MAX98390_PCM_TX_HIZ_CTRL_A,
  781. 1 << max98390->v_l_slot, 0);
  782. regmap_update_bits(max98390->regmap,
  783. MAX98390_PCM_TX_EN_A,
  784. 1 << max98390->v_l_slot,
  785. 1 << max98390->v_l_slot);
  786. } else {
  787. regmap_update_bits(max98390->regmap,
  788. MAX98390_PCM_TX_HIZ_CTRL_B,
  789. 1 << (max98390->v_l_slot - 8), 0);
  790. regmap_update_bits(max98390->regmap,
  791. MAX98390_PCM_TX_EN_B,
  792. 1 << (max98390->v_l_slot - 8),
  793. 1 << (max98390->v_l_slot - 8));
  794. }
  795. if (max98390->i_l_slot < 8) {
  796. regmap_update_bits(max98390->regmap,
  797. MAX98390_PCM_TX_HIZ_CTRL_A,
  798. 1 << max98390->i_l_slot, 0);
  799. regmap_update_bits(max98390->regmap,
  800. MAX98390_PCM_TX_EN_A,
  801. 1 << max98390->i_l_slot,
  802. 1 << max98390->i_l_slot);
  803. } else {
  804. regmap_update_bits(max98390->regmap,
  805. MAX98390_PCM_TX_HIZ_CTRL_B,
  806. 1 << (max98390->i_l_slot - 8), 0);
  807. regmap_update_bits(max98390->regmap,
  808. MAX98390_PCM_TX_EN_B,
  809. 1 << (max98390->i_l_slot - 8),
  810. 1 << (max98390->i_l_slot - 8));
  811. }
  812. }
  813. static int max98390_probe(struct snd_soc_component *component)
  814. {
  815. struct max98390_priv *max98390 =
  816. snd_soc_component_get_drvdata(component);
  817. regmap_write(max98390->regmap, MAX98390_SOFTWARE_RESET, 0x01);
  818. /* Sleep reset settle time */
  819. msleep(20);
  820. /* Amp init setting */
  821. max98390_init_regs(component);
  822. /* Update dsm bin param */
  823. max98390_dsm_init(component);
  824. /* Dsm Setting */
  825. if (max98390->ref_rdc_value) {
  826. regmap_write(max98390->regmap, DSM_TPROT_RECIP_RDC_ROOM_BYTE0,
  827. max98390->ref_rdc_value & 0x000000ff);
  828. regmap_write(max98390->regmap, DSM_TPROT_RECIP_RDC_ROOM_BYTE1,
  829. (max98390->ref_rdc_value >> 8) & 0x000000ff);
  830. regmap_write(max98390->regmap, DSM_TPROT_RECIP_RDC_ROOM_BYTE2,
  831. (max98390->ref_rdc_value >> 16) & 0x000000ff);
  832. }
  833. if (max98390->ambient_temp_value) {
  834. regmap_write(max98390->regmap, DSM_TPROT_ROOM_TEMPERATURE_BYTE1,
  835. (max98390->ambient_temp_value >> 8) & 0x000000ff);
  836. regmap_write(max98390->regmap, DSM_TPROT_ROOM_TEMPERATURE_BYTE0,
  837. (max98390->ambient_temp_value) & 0x000000ff);
  838. }
  839. return 0;
  840. }
  841. #ifdef CONFIG_PM_SLEEP
  842. static int max98390_suspend(struct device *dev)
  843. {
  844. struct max98390_priv *max98390 = dev_get_drvdata(dev);
  845. dev_dbg(dev, "%s:Enter\n", __func__);
  846. regcache_cache_only(max98390->regmap, true);
  847. regcache_mark_dirty(max98390->regmap);
  848. return 0;
  849. }
  850. static int max98390_resume(struct device *dev)
  851. {
  852. struct max98390_priv *max98390 = dev_get_drvdata(dev);
  853. dev_dbg(dev, "%s:Enter\n", __func__);
  854. regcache_cache_only(max98390->regmap, false);
  855. regcache_sync(max98390->regmap);
  856. return 0;
  857. }
  858. #endif
  859. static const struct dev_pm_ops max98390_pm = {
  860. SET_SYSTEM_SLEEP_PM_OPS(max98390_suspend, max98390_resume)
  861. };
  862. static const struct snd_soc_component_driver soc_codec_dev_max98390 = {
  863. .probe = max98390_probe,
  864. .controls = max98390_snd_controls,
  865. .num_controls = ARRAY_SIZE(max98390_snd_controls),
  866. .dapm_widgets = max98390_dapm_widgets,
  867. .num_dapm_widgets = ARRAY_SIZE(max98390_dapm_widgets),
  868. .dapm_routes = max98390_audio_map,
  869. .num_dapm_routes = ARRAY_SIZE(max98390_audio_map),
  870. .idle_bias_on = 1,
  871. .use_pmdown_time = 1,
  872. .endianness = 1,
  873. };
  874. static const struct regmap_config max98390_regmap = {
  875. .reg_bits = 16,
  876. .val_bits = 8,
  877. .max_register = MAX98390_R24FF_REV_ID,
  878. .reg_defaults = max98390_reg_defaults,
  879. .num_reg_defaults = ARRAY_SIZE(max98390_reg_defaults),
  880. .readable_reg = max98390_readable_register,
  881. .volatile_reg = max98390_volatile_reg,
  882. .cache_type = REGCACHE_RBTREE,
  883. };
  884. static void max98390_slot_config(struct i2c_client *i2c,
  885. struct max98390_priv *max98390)
  886. {
  887. int value;
  888. struct device *dev = &i2c->dev;
  889. if (!device_property_read_u32(dev, "maxim,vmon-slot-no", &value))
  890. max98390->v_l_slot = value & 0xF;
  891. else
  892. max98390->v_l_slot = 0;
  893. if (!device_property_read_u32(dev, "maxim,imon-slot-no", &value))
  894. max98390->i_l_slot = value & 0xF;
  895. else
  896. max98390->i_l_slot = 1;
  897. }
  898. static int max98390_i2c_probe(struct i2c_client *i2c)
  899. {
  900. int ret = 0;
  901. int reg = 0;
  902. struct max98390_priv *max98390 = NULL;
  903. struct i2c_adapter *adapter = i2c->adapter;
  904. struct gpio_desc *reset_gpio;
  905. ret = i2c_check_functionality(adapter,
  906. I2C_FUNC_SMBUS_BYTE
  907. | I2C_FUNC_SMBUS_BYTE_DATA);
  908. if (!ret) {
  909. dev_err(&i2c->dev, "I2C check functionality failed\n");
  910. return -ENXIO;
  911. }
  912. max98390 = devm_kzalloc(&i2c->dev, sizeof(*max98390), GFP_KERNEL);
  913. if (!max98390) {
  914. ret = -ENOMEM;
  915. return ret;
  916. }
  917. i2c_set_clientdata(i2c, max98390);
  918. ret = device_property_read_u32(&i2c->dev, "maxim,temperature_calib",
  919. &max98390->ambient_temp_value);
  920. if (ret) {
  921. dev_info(&i2c->dev,
  922. "no optional property 'temperature_calib' found, default:\n");
  923. }
  924. ret = device_property_read_u32(&i2c->dev, "maxim,r0_calib",
  925. &max98390->ref_rdc_value);
  926. if (ret) {
  927. dev_info(&i2c->dev,
  928. "no optional property 'r0_calib' found, default:\n");
  929. }
  930. dev_info(&i2c->dev,
  931. "%s: r0_calib: 0x%x,temperature_calib: 0x%x",
  932. __func__, max98390->ref_rdc_value,
  933. max98390->ambient_temp_value);
  934. ret = device_property_read_string(&i2c->dev, "maxim,dsm_param_name",
  935. &max98390->dsm_param_name);
  936. if (ret)
  937. max98390->dsm_param_name = "default";
  938. /* voltage/current slot configuration */
  939. max98390_slot_config(i2c, max98390);
  940. /* regmap initialization */
  941. max98390->regmap = devm_regmap_init_i2c(i2c, &max98390_regmap);
  942. if (IS_ERR(max98390->regmap)) {
  943. ret = PTR_ERR(max98390->regmap);
  944. dev_err(&i2c->dev,
  945. "Failed to allocate regmap: %d\n", ret);
  946. return ret;
  947. }
  948. reset_gpio = devm_gpiod_get_optional(&i2c->dev,
  949. "reset", GPIOD_OUT_HIGH);
  950. /* Power on device */
  951. if (reset_gpio) {
  952. usleep_range(1000, 2000);
  953. /* bring out of reset */
  954. gpiod_set_value_cansleep(reset_gpio, 0);
  955. usleep_range(1000, 2000);
  956. }
  957. /* Check Revision ID */
  958. ret = regmap_read(max98390->regmap,
  959. MAX98390_R24FF_REV_ID, &reg);
  960. if (ret) {
  961. dev_err(&i2c->dev,
  962. "ret=%d, Failed to read: 0x%02X\n",
  963. ret, MAX98390_R24FF_REV_ID);
  964. return ret;
  965. }
  966. dev_info(&i2c->dev, "MAX98390 revisionID: 0x%02X\n", reg);
  967. ret = devm_snd_soc_register_component(&i2c->dev,
  968. &soc_codec_dev_max98390,
  969. max98390_dai, ARRAY_SIZE(max98390_dai));
  970. return ret;
  971. }
  972. static const struct i2c_device_id max98390_i2c_id[] = {
  973. { "max98390", 0},
  974. {},
  975. };
  976. MODULE_DEVICE_TABLE(i2c, max98390_i2c_id);
  977. #if defined(CONFIG_OF)
  978. static const struct of_device_id max98390_of_match[] = {
  979. { .compatible = "maxim,max98390", },
  980. {}
  981. };
  982. MODULE_DEVICE_TABLE(of, max98390_of_match);
  983. #endif
  984. #ifdef CONFIG_ACPI
  985. static const struct acpi_device_id max98390_acpi_match[] = {
  986. { "MX98390", 0 },
  987. {},
  988. };
  989. MODULE_DEVICE_TABLE(acpi, max98390_acpi_match);
  990. #endif
  991. static struct i2c_driver max98390_i2c_driver = {
  992. .driver = {
  993. .name = "max98390",
  994. .of_match_table = of_match_ptr(max98390_of_match),
  995. .acpi_match_table = ACPI_PTR(max98390_acpi_match),
  996. .pm = &max98390_pm,
  997. },
  998. .probe_new = max98390_i2c_probe,
  999. .id_table = max98390_i2c_id,
  1000. };
  1001. module_i2c_driver(max98390_i2c_driver)
  1002. MODULE_DESCRIPTION("ALSA SoC MAX98390 driver");
  1003. MODULE_AUTHOR("Steve Lee <[email protected]>");
  1004. MODULE_LICENSE("GPL");