rt286.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * rt286.c -- RT286 ALSA SoC audio codec driver
  4. *
  5. * Copyright 2013 Realtek Semiconductor Corp.
  6. * Author: Bard Liao <bardliao@realtek.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/pm.h>
  13. #include <linux/i2c.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/dmi.h>
  17. #include <linux/acpi.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dapm.h>
  23. #include <sound/initval.h>
  24. #include <sound/tlv.h>
  25. #include <sound/jack.h>
  26. #include <linux/workqueue.h>
  27. #include <sound/rt286.h>
  28. #include "rl6347a.h"
  29. #include "rt286.h"
  30. #define RT286_VENDOR_ID 0x10ec0286
  31. #define RT288_VENDOR_ID 0x10ec0288
  32. struct rt286_priv {
  33. struct reg_default *index_cache;
  34. int index_cache_size;
  35. struct regmap *regmap;
  36. struct snd_soc_component *component;
  37. struct rt286_platform_data pdata;
  38. struct i2c_client *i2c;
  39. struct snd_soc_jack *jack;
  40. struct delayed_work jack_detect_work;
  41. int sys_clk;
  42. int clk_id;
  43. };
  44. static const struct reg_default rt286_index_def[] = {
  45. { 0x01, 0xaaaa },
  46. { 0x02, 0x8aaa },
  47. { 0x03, 0x0002 },
  48. { 0x04, 0xaf01 },
  49. { 0x08, 0x000d },
  50. { 0x09, 0xd810 },
  51. { 0x0a, 0x0120 },
  52. { 0x0b, 0x0000 },
  53. { 0x0d, 0x2800 },
  54. { 0x0f, 0x0000 },
  55. { 0x19, 0x0a17 },
  56. { 0x20, 0x0020 },
  57. { 0x33, 0x0208 },
  58. { 0x49, 0x0004 },
  59. { 0x4f, 0x50e9 },
  60. { 0x50, 0x2000 },
  61. { 0x63, 0x2902 },
  62. { 0x67, 0x1111 },
  63. { 0x68, 0x1016 },
  64. { 0x69, 0x273f },
  65. };
  66. #define INDEX_CACHE_SIZE ARRAY_SIZE(rt286_index_def)
  67. static const struct reg_default rt286_reg[] = {
  68. { 0x00170500, 0x00000400 },
  69. { 0x00220000, 0x00000031 },
  70. { 0x00239000, 0x0000007f },
  71. { 0x0023a000, 0x0000007f },
  72. { 0x00270500, 0x00000400 },
  73. { 0x00370500, 0x00000400 },
  74. { 0x00870500, 0x00000400 },
  75. { 0x00920000, 0x00000031 },
  76. { 0x00935000, 0x000000c3 },
  77. { 0x00936000, 0x000000c3 },
  78. { 0x00970500, 0x00000400 },
  79. { 0x00b37000, 0x00000097 },
  80. { 0x00b37200, 0x00000097 },
  81. { 0x00b37300, 0x00000097 },
  82. { 0x00c37000, 0x00000000 },
  83. { 0x00c37100, 0x00000080 },
  84. { 0x01270500, 0x00000400 },
  85. { 0x01370500, 0x00000400 },
  86. { 0x01371f00, 0x411111f0 },
  87. { 0x01439000, 0x00000080 },
  88. { 0x0143a000, 0x00000080 },
  89. { 0x01470700, 0x00000000 },
  90. { 0x01470500, 0x00000400 },
  91. { 0x01470c00, 0x00000000 },
  92. { 0x01470100, 0x00000000 },
  93. { 0x01837000, 0x00000000 },
  94. { 0x01870500, 0x00000400 },
  95. { 0x02050000, 0x00000000 },
  96. { 0x02139000, 0x00000080 },
  97. { 0x0213a000, 0x00000080 },
  98. { 0x02170100, 0x00000000 },
  99. { 0x02170500, 0x00000400 },
  100. { 0x02170700, 0x00000000 },
  101. { 0x02270100, 0x00000000 },
  102. { 0x02370100, 0x00000000 },
  103. { 0x01870700, 0x00000020 },
  104. { 0x00830000, 0x000000c3 },
  105. { 0x00930000, 0x000000c3 },
  106. { 0x01270700, 0x00000000 },
  107. };
  108. static bool rt286_volatile_register(struct device *dev, unsigned int reg)
  109. {
  110. switch (reg) {
  111. case 0 ... 0xff:
  112. case RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
  113. case RT286_GET_HP_SENSE:
  114. case RT286_GET_MIC1_SENSE:
  115. case RT286_PROC_COEF:
  116. return true;
  117. default:
  118. return false;
  119. }
  120. }
  121. static bool rt286_readable_register(struct device *dev, unsigned int reg)
  122. {
  123. switch (reg) {
  124. case 0 ... 0xff:
  125. case RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
  126. case RT286_GET_HP_SENSE:
  127. case RT286_GET_MIC1_SENSE:
  128. case RT286_SET_AUDIO_POWER:
  129. case RT286_SET_HPO_POWER:
  130. case RT286_SET_SPK_POWER:
  131. case RT286_SET_DMIC1_POWER:
  132. case RT286_SPK_MUX:
  133. case RT286_HPO_MUX:
  134. case RT286_ADC0_MUX:
  135. case RT286_ADC1_MUX:
  136. case RT286_SET_MIC1:
  137. case RT286_SET_PIN_HPO:
  138. case RT286_SET_PIN_SPK:
  139. case RT286_SET_PIN_DMIC1:
  140. case RT286_SPK_EAPD:
  141. case RT286_SET_AMP_GAIN_HPO:
  142. case RT286_SET_DMIC2_DEFAULT:
  143. case RT286_DACL_GAIN:
  144. case RT286_DACR_GAIN:
  145. case RT286_ADCL_GAIN:
  146. case RT286_ADCR_GAIN:
  147. case RT286_MIC_GAIN:
  148. case RT286_SPOL_GAIN:
  149. case RT286_SPOR_GAIN:
  150. case RT286_HPOL_GAIN:
  151. case RT286_HPOR_GAIN:
  152. case RT286_F_DAC_SWITCH:
  153. case RT286_F_RECMIX_SWITCH:
  154. case RT286_REC_MIC_SWITCH:
  155. case RT286_REC_I2S_SWITCH:
  156. case RT286_REC_LINE_SWITCH:
  157. case RT286_REC_BEEP_SWITCH:
  158. case RT286_DAC_FORMAT:
  159. case RT286_ADC_FORMAT:
  160. case RT286_COEF_INDEX:
  161. case RT286_PROC_COEF:
  162. case RT286_SET_AMP_GAIN_ADC_IN1:
  163. case RT286_SET_AMP_GAIN_ADC_IN2:
  164. case RT286_SET_GPIO_MASK:
  165. case RT286_SET_GPIO_DIRECTION:
  166. case RT286_SET_GPIO_DATA:
  167. case RT286_SET_POWER(RT286_DAC_OUT1):
  168. case RT286_SET_POWER(RT286_DAC_OUT2):
  169. case RT286_SET_POWER(RT286_ADC_IN1):
  170. case RT286_SET_POWER(RT286_ADC_IN2):
  171. case RT286_SET_POWER(RT286_DMIC2):
  172. case RT286_SET_POWER(RT286_MIC1):
  173. return true;
  174. default:
  175. return false;
  176. }
  177. }
  178. #ifdef CONFIG_PM
  179. static void rt286_index_sync(struct snd_soc_component *component)
  180. {
  181. struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
  182. int i;
  183. for (i = 0; i < INDEX_CACHE_SIZE; i++) {
  184. snd_soc_component_write(component, rt286->index_cache[i].reg,
  185. rt286->index_cache[i].def);
  186. }
  187. }
  188. #endif
  189. static int rt286_support_power_controls[] = {
  190. RT286_DAC_OUT1,
  191. RT286_DAC_OUT2,
  192. RT286_ADC_IN1,
  193. RT286_ADC_IN2,
  194. RT286_MIC1,
  195. RT286_DMIC1,
  196. RT286_DMIC2,
  197. RT286_SPK_OUT,
  198. RT286_HP_OUT,
  199. };
  200. #define RT286_POWER_REG_LEN ARRAY_SIZE(rt286_support_power_controls)
  201. static int rt286_jack_detect(struct rt286_priv *rt286, bool *hp, bool *mic)
  202. {
  203. struct snd_soc_dapm_context *dapm;
  204. unsigned int val, buf;
  205. *hp = false;
  206. *mic = false;
  207. if (!rt286->component)
  208. return -EINVAL;
  209. dapm = snd_soc_component_get_dapm(rt286->component);
  210. if (rt286->pdata.cbj_en) {
  211. regmap_read(rt286->regmap, RT286_GET_HP_SENSE, &buf);
  212. *hp = buf & 0x80000000;
  213. if (*hp) {
  214. /* power on HV,VERF */
  215. regmap_update_bits(rt286->regmap,
  216. RT286_DC_GAIN, 0x200, 0x200);
  217. snd_soc_dapm_force_enable_pin(dapm, "HV");
  218. snd_soc_dapm_force_enable_pin(dapm, "VREF");
  219. /* power LDO1 */
  220. snd_soc_dapm_force_enable_pin(dapm, "LDO1");
  221. snd_soc_dapm_sync(dapm);
  222. regmap_write(rt286->regmap, RT286_SET_MIC1, 0x24);
  223. msleep(50);
  224. regmap_update_bits(rt286->regmap,
  225. RT286_CBJ_CTRL1, 0xfcc0, 0xd400);
  226. msleep(300);
  227. regmap_read(rt286->regmap, RT286_CBJ_CTRL2, &val);
  228. if (0x0070 == (val & 0x0070)) {
  229. *mic = true;
  230. } else {
  231. regmap_update_bits(rt286->regmap,
  232. RT286_CBJ_CTRL1, 0xfcc0, 0xe400);
  233. msleep(300);
  234. regmap_read(rt286->regmap,
  235. RT286_CBJ_CTRL2, &val);
  236. if (0x0070 == (val & 0x0070))
  237. *mic = true;
  238. else
  239. *mic = false;
  240. }
  241. regmap_update_bits(rt286->regmap,
  242. RT286_DC_GAIN, 0x200, 0x0);
  243. } else {
  244. *mic = false;
  245. regmap_write(rt286->regmap, RT286_SET_MIC1, 0x20);
  246. regmap_update_bits(rt286->regmap,
  247. RT286_CBJ_CTRL1, 0x0400, 0x0000);
  248. }
  249. } else {
  250. regmap_read(rt286->regmap, RT286_GET_HP_SENSE, &buf);
  251. *hp = buf & 0x80000000;
  252. regmap_read(rt286->regmap, RT286_GET_MIC1_SENSE, &buf);
  253. *mic = buf & 0x80000000;
  254. }
  255. if (!*hp) {
  256. snd_soc_dapm_disable_pin(dapm, "HV");
  257. snd_soc_dapm_disable_pin(dapm, "VREF");
  258. snd_soc_dapm_disable_pin(dapm, "LDO1");
  259. snd_soc_dapm_sync(dapm);
  260. }
  261. return 0;
  262. }
  263. static void rt286_jack_detect_work(struct work_struct *work)
  264. {
  265. struct rt286_priv *rt286 =
  266. container_of(work, struct rt286_priv, jack_detect_work.work);
  267. int status = 0;
  268. bool hp = false;
  269. bool mic = false;
  270. rt286_jack_detect(rt286, &hp, &mic);
  271. if (hp)
  272. status |= SND_JACK_HEADPHONE;
  273. if (mic)
  274. status |= SND_JACK_MICROPHONE;
  275. snd_soc_jack_report(rt286->jack, status,
  276. SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
  277. }
  278. int rt286_mic_detect(struct snd_soc_component *component, struct snd_soc_jack *jack)
  279. {
  280. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  281. struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
  282. rt286->jack = jack;
  283. if (jack) {
  284. /* enable IRQ */
  285. if (rt286->jack->status & SND_JACK_HEADPHONE)
  286. snd_soc_dapm_force_enable_pin(dapm, "LDO1");
  287. regmap_update_bits(rt286->regmap, RT286_IRQ_CTRL, 0x2, 0x2);
  288. /* Send an initial empty report */
  289. snd_soc_jack_report(rt286->jack, rt286->jack->status,
  290. SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
  291. } else {
  292. /* disable IRQ */
  293. regmap_update_bits(rt286->regmap, RT286_IRQ_CTRL, 0x2, 0x0);
  294. snd_soc_dapm_disable_pin(dapm, "LDO1");
  295. }
  296. snd_soc_dapm_sync(dapm);
  297. return 0;
  298. }
  299. EXPORT_SYMBOL_GPL(rt286_mic_detect);
  300. static int is_mclk_mode(struct snd_soc_dapm_widget *source,
  301. struct snd_soc_dapm_widget *sink)
  302. {
  303. struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
  304. struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
  305. if (rt286->clk_id == RT286_SCLK_S_MCLK)
  306. return 1;
  307. else
  308. return 0;
  309. }
  310. static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6350, 50, 0);
  311. static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
  312. static const struct snd_kcontrol_new rt286_snd_controls[] = {
  313. SOC_DOUBLE_R_TLV("DAC0 Playback Volume", RT286_DACL_GAIN,
  314. RT286_DACR_GAIN, 0, 0x7f, 0, out_vol_tlv),
  315. SOC_DOUBLE_R("ADC0 Capture Switch", RT286_ADCL_GAIN,
  316. RT286_ADCR_GAIN, 7, 1, 1),
  317. SOC_DOUBLE_R_TLV("ADC0 Capture Volume", RT286_ADCL_GAIN,
  318. RT286_ADCR_GAIN, 0, 0x7f, 0, out_vol_tlv),
  319. SOC_SINGLE_TLV("AMIC Volume", RT286_MIC_GAIN,
  320. 0, 0x3, 0, mic_vol_tlv),
  321. SOC_DOUBLE_R("Speaker Playback Switch", RT286_SPOL_GAIN,
  322. RT286_SPOR_GAIN, RT286_MUTE_SFT, 1, 1),
  323. };
  324. /* Digital Mixer */
  325. static const struct snd_kcontrol_new rt286_front_mix[] = {
  326. SOC_DAPM_SINGLE("DAC Switch", RT286_F_DAC_SWITCH,
  327. RT286_MUTE_SFT, 1, 1),
  328. SOC_DAPM_SINGLE("RECMIX Switch", RT286_F_RECMIX_SWITCH,
  329. RT286_MUTE_SFT, 1, 1),
  330. };
  331. /* Analog Input Mixer */
  332. static const struct snd_kcontrol_new rt286_rec_mix[] = {
  333. SOC_DAPM_SINGLE("Mic1 Switch", RT286_REC_MIC_SWITCH,
  334. RT286_MUTE_SFT, 1, 1),
  335. SOC_DAPM_SINGLE("I2S Switch", RT286_REC_I2S_SWITCH,
  336. RT286_MUTE_SFT, 1, 1),
  337. SOC_DAPM_SINGLE("Line1 Switch", RT286_REC_LINE_SWITCH,
  338. RT286_MUTE_SFT, 1, 1),
  339. SOC_DAPM_SINGLE("Beep Switch", RT286_REC_BEEP_SWITCH,
  340. RT286_MUTE_SFT, 1, 1),
  341. };
  342. static const struct snd_kcontrol_new spo_enable_control =
  343. SOC_DAPM_SINGLE("Switch", RT286_SET_PIN_SPK,
  344. RT286_SET_PIN_SFT, 1, 0);
  345. static const struct snd_kcontrol_new hpol_enable_control =
  346. SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT286_HPOL_GAIN,
  347. RT286_MUTE_SFT, 1, 1);
  348. static const struct snd_kcontrol_new hpor_enable_control =
  349. SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT286_HPOR_GAIN,
  350. RT286_MUTE_SFT, 1, 1);
  351. /* ADC0 source */
  352. static const char * const rt286_adc_src[] = {
  353. "Mic", "RECMIX", "Dmic"
  354. };
  355. static const int rt286_adc_values[] = {
  356. 0, 4, 5,
  357. };
  358. static SOC_VALUE_ENUM_SINGLE_DECL(
  359. rt286_adc0_enum, RT286_ADC0_MUX, RT286_ADC_SEL_SFT,
  360. RT286_ADC_SEL_MASK, rt286_adc_src, rt286_adc_values);
  361. static const struct snd_kcontrol_new rt286_adc0_mux =
  362. SOC_DAPM_ENUM("ADC 0 source", rt286_adc0_enum);
  363. static SOC_VALUE_ENUM_SINGLE_DECL(
  364. rt286_adc1_enum, RT286_ADC1_MUX, RT286_ADC_SEL_SFT,
  365. RT286_ADC_SEL_MASK, rt286_adc_src, rt286_adc_values);
  366. static const struct snd_kcontrol_new rt286_adc1_mux =
  367. SOC_DAPM_ENUM("ADC 1 source", rt286_adc1_enum);
  368. static const char * const rt286_dac_src[] = {
  369. "Front", "Surround"
  370. };
  371. /* HP-OUT source */
  372. static SOC_ENUM_SINGLE_DECL(rt286_hpo_enum, RT286_HPO_MUX,
  373. 0, rt286_dac_src);
  374. static const struct snd_kcontrol_new rt286_hpo_mux =
  375. SOC_DAPM_ENUM("HPO source", rt286_hpo_enum);
  376. /* SPK-OUT source */
  377. static SOC_ENUM_SINGLE_DECL(rt286_spo_enum, RT286_SPK_MUX,
  378. 0, rt286_dac_src);
  379. static const struct snd_kcontrol_new rt286_spo_mux =
  380. SOC_DAPM_ENUM("SPO source", rt286_spo_enum);
  381. static int rt286_spk_event(struct snd_soc_dapm_widget *w,
  382. struct snd_kcontrol *kcontrol, int event)
  383. {
  384. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  385. switch (event) {
  386. case SND_SOC_DAPM_POST_PMU:
  387. snd_soc_component_write(component,
  388. RT286_SPK_EAPD, RT286_SET_EAPD_HIGH);
  389. break;
  390. case SND_SOC_DAPM_PRE_PMD:
  391. snd_soc_component_write(component,
  392. RT286_SPK_EAPD, RT286_SET_EAPD_LOW);
  393. break;
  394. default:
  395. return 0;
  396. }
  397. return 0;
  398. }
  399. static int rt286_set_dmic1_event(struct snd_soc_dapm_widget *w,
  400. struct snd_kcontrol *kcontrol, int event)
  401. {
  402. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  403. switch (event) {
  404. case SND_SOC_DAPM_POST_PMU:
  405. snd_soc_component_write(component, RT286_SET_PIN_DMIC1, 0x20);
  406. break;
  407. case SND_SOC_DAPM_PRE_PMD:
  408. snd_soc_component_write(component, RT286_SET_PIN_DMIC1, 0);
  409. break;
  410. default:
  411. return 0;
  412. }
  413. return 0;
  414. }
  415. static int rt286_ldo2_event(struct snd_soc_dapm_widget *w,
  416. struct snd_kcontrol *kcontrol, int event)
  417. {
  418. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  419. switch (event) {
  420. case SND_SOC_DAPM_POST_PMU:
  421. snd_soc_component_update_bits(component, RT286_POWER_CTRL2, 0x38, 0x08);
  422. break;
  423. case SND_SOC_DAPM_PRE_PMD:
  424. snd_soc_component_update_bits(component, RT286_POWER_CTRL2, 0x38, 0x30);
  425. break;
  426. default:
  427. return 0;
  428. }
  429. return 0;
  430. }
  431. static int rt286_mic1_event(struct snd_soc_dapm_widget *w,
  432. struct snd_kcontrol *kcontrol, int event)
  433. {
  434. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  435. switch (event) {
  436. case SND_SOC_DAPM_PRE_PMU:
  437. snd_soc_component_update_bits(component,
  438. RT286_A_BIAS_CTRL3, 0xc000, 0x8000);
  439. snd_soc_component_update_bits(component,
  440. RT286_A_BIAS_CTRL2, 0xc000, 0x8000);
  441. break;
  442. case SND_SOC_DAPM_POST_PMD:
  443. snd_soc_component_update_bits(component,
  444. RT286_A_BIAS_CTRL3, 0xc000, 0x0000);
  445. snd_soc_component_update_bits(component,
  446. RT286_A_BIAS_CTRL2, 0xc000, 0x0000);
  447. break;
  448. default:
  449. return 0;
  450. }
  451. return 0;
  452. }
  453. static const struct snd_soc_dapm_widget rt286_dapm_widgets[] = {
  454. SND_SOC_DAPM_SUPPLY_S("HV", 1, RT286_POWER_CTRL1,
  455. 12, 1, NULL, 0),
  456. SND_SOC_DAPM_SUPPLY("VREF", RT286_POWER_CTRL1,
  457. 0, 1, NULL, 0),
  458. SND_SOC_DAPM_SUPPLY_S("LDO1", 1, RT286_POWER_CTRL2,
  459. 2, 0, NULL, 0),
  460. SND_SOC_DAPM_SUPPLY_S("LDO2", 2, RT286_POWER_CTRL1,
  461. 13, 1, rt286_ldo2_event, SND_SOC_DAPM_PRE_PMD |
  462. SND_SOC_DAPM_POST_PMU),
  463. SND_SOC_DAPM_SUPPLY("MCLK MODE", RT286_PLL_CTRL1,
  464. 5, 0, NULL, 0),
  465. SND_SOC_DAPM_SUPPLY("MIC1 Input Buffer", SND_SOC_NOPM,
  466. 0, 0, rt286_mic1_event, SND_SOC_DAPM_PRE_PMU |
  467. SND_SOC_DAPM_POST_PMD),
  468. /* Input Lines */
  469. SND_SOC_DAPM_INPUT("DMIC1 Pin"),
  470. SND_SOC_DAPM_INPUT("DMIC2 Pin"),
  471. SND_SOC_DAPM_INPUT("MIC1"),
  472. SND_SOC_DAPM_INPUT("LINE1"),
  473. SND_SOC_DAPM_INPUT("Beep"),
  474. /* DMIC */
  475. SND_SOC_DAPM_PGA_E("DMIC1", RT286_SET_POWER(RT286_DMIC1), 0, 1,
  476. NULL, 0, rt286_set_dmic1_event,
  477. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  478. SND_SOC_DAPM_PGA("DMIC2", RT286_SET_POWER(RT286_DMIC2), 0, 1,
  479. NULL, 0),
  480. SND_SOC_DAPM_SUPPLY("DMIC Receiver", SND_SOC_NOPM,
  481. 0, 0, NULL, 0),
  482. /* REC Mixer */
  483. SND_SOC_DAPM_MIXER("RECMIX", SND_SOC_NOPM, 0, 0,
  484. rt286_rec_mix, ARRAY_SIZE(rt286_rec_mix)),
  485. /* ADCs */
  486. SND_SOC_DAPM_ADC("ADC 0", NULL, SND_SOC_NOPM, 0, 0),
  487. SND_SOC_DAPM_ADC("ADC 1", NULL, SND_SOC_NOPM, 0, 0),
  488. /* ADC Mux */
  489. SND_SOC_DAPM_MUX("ADC 0 Mux", RT286_SET_POWER(RT286_ADC_IN1), 0, 1,
  490. &rt286_adc0_mux),
  491. SND_SOC_DAPM_MUX("ADC 1 Mux", RT286_SET_POWER(RT286_ADC_IN2), 0, 1,
  492. &rt286_adc1_mux),
  493. /* Audio Interface */
  494. SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
  495. SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
  496. SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
  497. SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
  498. /* Output Side */
  499. /* DACs */
  500. SND_SOC_DAPM_DAC("DAC 0", NULL, SND_SOC_NOPM, 0, 0),
  501. SND_SOC_DAPM_DAC("DAC 1", NULL, SND_SOC_NOPM, 0, 0),
  502. /* Output Mux */
  503. SND_SOC_DAPM_MUX("SPK Mux", SND_SOC_NOPM, 0, 0, &rt286_spo_mux),
  504. SND_SOC_DAPM_MUX("HPO Mux", SND_SOC_NOPM, 0, 0, &rt286_hpo_mux),
  505. SND_SOC_DAPM_SUPPLY("HP Power", RT286_SET_PIN_HPO,
  506. RT286_SET_PIN_SFT, 0, NULL, 0),
  507. /* Output Mixer */
  508. SND_SOC_DAPM_MIXER("Front", RT286_SET_POWER(RT286_DAC_OUT1), 0, 1,
  509. rt286_front_mix, ARRAY_SIZE(rt286_front_mix)),
  510. SND_SOC_DAPM_PGA("Surround", RT286_SET_POWER(RT286_DAC_OUT2), 0, 1,
  511. NULL, 0),
  512. /* Output Pga */
  513. SND_SOC_DAPM_SWITCH_E("SPO", SND_SOC_NOPM, 0, 0,
  514. &spo_enable_control, rt286_spk_event,
  515. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  516. SND_SOC_DAPM_SWITCH("HPO L", SND_SOC_NOPM, 0, 0,
  517. &hpol_enable_control),
  518. SND_SOC_DAPM_SWITCH("HPO R", SND_SOC_NOPM, 0, 0,
  519. &hpor_enable_control),
  520. /* Output Lines */
  521. SND_SOC_DAPM_OUTPUT("SPOL"),
  522. SND_SOC_DAPM_OUTPUT("SPOR"),
  523. SND_SOC_DAPM_OUTPUT("HPO Pin"),
  524. SND_SOC_DAPM_OUTPUT("SPDIF"),
  525. };
  526. static const struct snd_soc_dapm_route rt286_dapm_routes[] = {
  527. {"ADC 0", NULL, "MCLK MODE", is_mclk_mode},
  528. {"ADC 1", NULL, "MCLK MODE", is_mclk_mode},
  529. {"Front", NULL, "MCLK MODE", is_mclk_mode},
  530. {"Surround", NULL, "MCLK MODE", is_mclk_mode},
  531. {"HP Power", NULL, "LDO1"},
  532. {"HP Power", NULL, "LDO2"},
  533. {"MIC1", NULL, "LDO1"},
  534. {"MIC1", NULL, "LDO2"},
  535. {"MIC1", NULL, "HV"},
  536. {"MIC1", NULL, "VREF"},
  537. {"MIC1", NULL, "MIC1 Input Buffer"},
  538. {"SPO", NULL, "LDO1"},
  539. {"SPO", NULL, "LDO2"},
  540. {"SPO", NULL, "HV"},
  541. {"SPO", NULL, "VREF"},
  542. {"DMIC1", NULL, "DMIC1 Pin"},
  543. {"DMIC2", NULL, "DMIC2 Pin"},
  544. {"DMIC1", NULL, "DMIC Receiver"},
  545. {"DMIC2", NULL, "DMIC Receiver"},
  546. {"RECMIX", "Beep Switch", "Beep"},
  547. {"RECMIX", "Line1 Switch", "LINE1"},
  548. {"RECMIX", "Mic1 Switch", "MIC1"},
  549. {"ADC 0 Mux", "Dmic", "DMIC1"},
  550. {"ADC 0 Mux", "RECMIX", "RECMIX"},
  551. {"ADC 0 Mux", "Mic", "MIC1"},
  552. {"ADC 1 Mux", "Dmic", "DMIC2"},
  553. {"ADC 1 Mux", "RECMIX", "RECMIX"},
  554. {"ADC 1 Mux", "Mic", "MIC1"},
  555. {"ADC 0", NULL, "ADC 0 Mux"},
  556. {"ADC 1", NULL, "ADC 1 Mux"},
  557. {"AIF1TX", NULL, "ADC 0"},
  558. {"AIF2TX", NULL, "ADC 1"},
  559. {"DAC 0", NULL, "AIF1RX"},
  560. {"DAC 1", NULL, "AIF2RX"},
  561. {"Front", "DAC Switch", "DAC 0"},
  562. {"Front", "RECMIX Switch", "RECMIX"},
  563. {"Surround", NULL, "DAC 1"},
  564. {"SPK Mux", "Front", "Front"},
  565. {"SPK Mux", "Surround", "Surround"},
  566. {"HPO Mux", "Front", "Front"},
  567. {"HPO Mux", "Surround", "Surround"},
  568. {"SPO", "Switch", "SPK Mux"},
  569. {"HPO L", "Switch", "HPO Mux"},
  570. {"HPO R", "Switch", "HPO Mux"},
  571. {"HPO L", NULL, "HP Power"},
  572. {"HPO R", NULL, "HP Power"},
  573. {"SPOL", NULL, "SPO"},
  574. {"SPOR", NULL, "SPO"},
  575. {"HPO Pin", NULL, "HPO L"},
  576. {"HPO Pin", NULL, "HPO R"},
  577. };
  578. static int rt286_hw_params(struct snd_pcm_substream *substream,
  579. struct snd_pcm_hw_params *params,
  580. struct snd_soc_dai *dai)
  581. {
  582. struct snd_soc_component *component = dai->component;
  583. struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
  584. unsigned int val = 0;
  585. int d_len_code;
  586. switch (params_rate(params)) {
  587. /* bit 14 0:48K 1:44.1K */
  588. case 44100:
  589. val |= 0x4000;
  590. break;
  591. case 48000:
  592. break;
  593. default:
  594. dev_err(component->dev, "Unsupported sample rate %d\n",
  595. params_rate(params));
  596. return -EINVAL;
  597. }
  598. switch (rt286->sys_clk) {
  599. case 12288000:
  600. case 24576000:
  601. if (params_rate(params) != 48000) {
  602. dev_err(component->dev, "Sys_clk is not matched (%d %d)\n",
  603. params_rate(params), rt286->sys_clk);
  604. return -EINVAL;
  605. }
  606. break;
  607. case 11289600:
  608. case 22579200:
  609. if (params_rate(params) != 44100) {
  610. dev_err(component->dev, "Sys_clk is not matched (%d %d)\n",
  611. params_rate(params), rt286->sys_clk);
  612. return -EINVAL;
  613. }
  614. break;
  615. }
  616. if (params_channels(params) <= 16) {
  617. /* bit 3:0 Number of Channel */
  618. val |= (params_channels(params) - 1);
  619. } else {
  620. dev_err(component->dev, "Unsupported channels %d\n",
  621. params_channels(params));
  622. return -EINVAL;
  623. }
  624. d_len_code = 0;
  625. switch (params_width(params)) {
  626. /* bit 6:4 Bits per Sample */
  627. case 16:
  628. d_len_code = 0;
  629. val |= (0x1 << 4);
  630. break;
  631. case 32:
  632. d_len_code = 2;
  633. val |= (0x4 << 4);
  634. break;
  635. case 20:
  636. d_len_code = 1;
  637. val |= (0x2 << 4);
  638. break;
  639. case 24:
  640. d_len_code = 2;
  641. val |= (0x3 << 4);
  642. break;
  643. case 8:
  644. d_len_code = 3;
  645. break;
  646. default:
  647. return -EINVAL;
  648. }
  649. snd_soc_component_update_bits(component,
  650. RT286_I2S_CTRL1, 0x0018, d_len_code << 3);
  651. dev_dbg(component->dev, "format val = 0x%x\n", val);
  652. snd_soc_component_update_bits(component, RT286_DAC_FORMAT, 0x407f, val);
  653. snd_soc_component_update_bits(component, RT286_ADC_FORMAT, 0x407f, val);
  654. return 0;
  655. }
  656. static int rt286_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  657. {
  658. struct snd_soc_component *component = dai->component;
  659. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  660. case SND_SOC_DAIFMT_CBM_CFM:
  661. snd_soc_component_update_bits(component,
  662. RT286_I2S_CTRL1, 0x800, 0x800);
  663. break;
  664. case SND_SOC_DAIFMT_CBS_CFS:
  665. snd_soc_component_update_bits(component,
  666. RT286_I2S_CTRL1, 0x800, 0x0);
  667. break;
  668. default:
  669. return -EINVAL;
  670. }
  671. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  672. case SND_SOC_DAIFMT_I2S:
  673. snd_soc_component_update_bits(component,
  674. RT286_I2S_CTRL1, 0x300, 0x0);
  675. break;
  676. case SND_SOC_DAIFMT_LEFT_J:
  677. snd_soc_component_update_bits(component,
  678. RT286_I2S_CTRL1, 0x300, 0x1 << 8);
  679. break;
  680. case SND_SOC_DAIFMT_DSP_A:
  681. snd_soc_component_update_bits(component,
  682. RT286_I2S_CTRL1, 0x300, 0x2 << 8);
  683. break;
  684. case SND_SOC_DAIFMT_DSP_B:
  685. snd_soc_component_update_bits(component,
  686. RT286_I2S_CTRL1, 0x300, 0x3 << 8);
  687. break;
  688. default:
  689. return -EINVAL;
  690. }
  691. /* bit 15 Stream Type 0:PCM 1:Non-PCM */
  692. snd_soc_component_update_bits(component, RT286_DAC_FORMAT, 0x8000, 0);
  693. snd_soc_component_update_bits(component, RT286_ADC_FORMAT, 0x8000, 0);
  694. return 0;
  695. }
  696. static int rt286_set_dai_sysclk(struct snd_soc_dai *dai,
  697. int clk_id, unsigned int freq, int dir)
  698. {
  699. struct snd_soc_component *component = dai->component;
  700. struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
  701. dev_dbg(component->dev, "%s freq=%d\n", __func__, freq);
  702. if (RT286_SCLK_S_MCLK == clk_id) {
  703. snd_soc_component_update_bits(component,
  704. RT286_I2S_CTRL2, 0x0100, 0x0);
  705. snd_soc_component_update_bits(component,
  706. RT286_PLL_CTRL1, 0x20, 0x20);
  707. } else {
  708. snd_soc_component_update_bits(component,
  709. RT286_I2S_CTRL2, 0x0100, 0x0100);
  710. snd_soc_component_update_bits(component,
  711. RT286_PLL_CTRL, 0x4, 0x4);
  712. snd_soc_component_update_bits(component,
  713. RT286_PLL_CTRL1, 0x20, 0x0);
  714. }
  715. switch (freq) {
  716. case 19200000:
  717. if (RT286_SCLK_S_MCLK == clk_id) {
  718. dev_err(component->dev, "Should not use MCLK\n");
  719. return -EINVAL;
  720. }
  721. snd_soc_component_update_bits(component,
  722. RT286_I2S_CTRL2, 0x40, 0x40);
  723. break;
  724. case 24000000:
  725. if (RT286_SCLK_S_MCLK == clk_id) {
  726. dev_err(component->dev, "Should not use MCLK\n");
  727. return -EINVAL;
  728. }
  729. snd_soc_component_update_bits(component,
  730. RT286_I2S_CTRL2, 0x40, 0x0);
  731. break;
  732. case 12288000:
  733. case 11289600:
  734. snd_soc_component_update_bits(component,
  735. RT286_I2S_CTRL2, 0x8, 0x0);
  736. snd_soc_component_update_bits(component,
  737. RT286_CLK_DIV, 0xfc1e, 0x0004);
  738. break;
  739. case 24576000:
  740. case 22579200:
  741. snd_soc_component_update_bits(component,
  742. RT286_I2S_CTRL2, 0x8, 0x8);
  743. snd_soc_component_update_bits(component,
  744. RT286_CLK_DIV, 0xfc1e, 0x5406);
  745. break;
  746. default:
  747. dev_err(component->dev, "Unsupported system clock\n");
  748. return -EINVAL;
  749. }
  750. rt286->sys_clk = freq;
  751. rt286->clk_id = clk_id;
  752. return 0;
  753. }
  754. static int rt286_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
  755. {
  756. struct snd_soc_component *component = dai->component;
  757. dev_dbg(component->dev, "%s ratio=%d\n", __func__, ratio);
  758. if (50 == ratio)
  759. snd_soc_component_update_bits(component,
  760. RT286_I2S_CTRL1, 0x1000, 0x1000);
  761. else
  762. snd_soc_component_update_bits(component,
  763. RT286_I2S_CTRL1, 0x1000, 0x0);
  764. return 0;
  765. }
  766. static int rt286_set_bias_level(struct snd_soc_component *component,
  767. enum snd_soc_bias_level level)
  768. {
  769. switch (level) {
  770. case SND_SOC_BIAS_PREPARE:
  771. if (SND_SOC_BIAS_STANDBY == snd_soc_component_get_bias_level(component)) {
  772. snd_soc_component_write(component,
  773. RT286_SET_AUDIO_POWER, AC_PWRST_D0);
  774. snd_soc_component_update_bits(component,
  775. RT286_DC_GAIN, 0x200, 0x200);
  776. }
  777. break;
  778. case SND_SOC_BIAS_ON:
  779. mdelay(10);
  780. snd_soc_component_update_bits(component,
  781. RT286_DC_GAIN, 0x200, 0x0);
  782. break;
  783. case SND_SOC_BIAS_STANDBY:
  784. snd_soc_component_write(component,
  785. RT286_SET_AUDIO_POWER, AC_PWRST_D3);
  786. break;
  787. default:
  788. break;
  789. }
  790. return 0;
  791. }
  792. static irqreturn_t rt286_irq(int irq, void *data)
  793. {
  794. struct rt286_priv *rt286 = data;
  795. bool hp = false;
  796. bool mic = false;
  797. int status = 0;
  798. rt286_jack_detect(rt286, &hp, &mic);
  799. /* Clear IRQ */
  800. regmap_update_bits(rt286->regmap, RT286_IRQ_CTRL, 0x1, 0x1);
  801. if (hp)
  802. status |= SND_JACK_HEADPHONE;
  803. if (mic)
  804. status |= SND_JACK_MICROPHONE;
  805. snd_soc_jack_report(rt286->jack, status,
  806. SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
  807. pm_wakeup_event(&rt286->i2c->dev, 300);
  808. return IRQ_HANDLED;
  809. }
  810. static int rt286_probe(struct snd_soc_component *component)
  811. {
  812. struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
  813. rt286->component = component;
  814. if (rt286->i2c->irq) {
  815. regmap_update_bits(rt286->regmap,
  816. RT286_IRQ_CTRL, 0x2, 0x2);
  817. INIT_DELAYED_WORK(&rt286->jack_detect_work,
  818. rt286_jack_detect_work);
  819. schedule_delayed_work(&rt286->jack_detect_work,
  820. msecs_to_jiffies(1250));
  821. }
  822. return 0;
  823. }
  824. static void rt286_remove(struct snd_soc_component *component)
  825. {
  826. struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
  827. cancel_delayed_work_sync(&rt286->jack_detect_work);
  828. }
  829. #ifdef CONFIG_PM
  830. static int rt286_suspend(struct snd_soc_component *component)
  831. {
  832. struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
  833. regcache_cache_only(rt286->regmap, true);
  834. regcache_mark_dirty(rt286->regmap);
  835. return 0;
  836. }
  837. static int rt286_resume(struct snd_soc_component *component)
  838. {
  839. struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
  840. regcache_cache_only(rt286->regmap, false);
  841. rt286_index_sync(component);
  842. regcache_sync(rt286->regmap);
  843. return 0;
  844. }
  845. #else
  846. #define rt286_suspend NULL
  847. #define rt286_resume NULL
  848. #endif
  849. #define RT286_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  850. #define RT286_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  851. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
  852. static const struct snd_soc_dai_ops rt286_aif_dai_ops = {
  853. .hw_params = rt286_hw_params,
  854. .set_fmt = rt286_set_dai_fmt,
  855. .set_sysclk = rt286_set_dai_sysclk,
  856. .set_bclk_ratio = rt286_set_bclk_ratio,
  857. };
  858. static struct snd_soc_dai_driver rt286_dai[] = {
  859. {
  860. .name = "rt286-aif1",
  861. .id = RT286_AIF1,
  862. .playback = {
  863. .stream_name = "AIF1 Playback",
  864. .channels_min = 1,
  865. .channels_max = 2,
  866. .rates = RT286_STEREO_RATES,
  867. .formats = RT286_FORMATS,
  868. },
  869. .capture = {
  870. .stream_name = "AIF1 Capture",
  871. .channels_min = 1,
  872. .channels_max = 2,
  873. .rates = RT286_STEREO_RATES,
  874. .formats = RT286_FORMATS,
  875. },
  876. .ops = &rt286_aif_dai_ops,
  877. .symmetric_rates = 1,
  878. },
  879. {
  880. .name = "rt286-aif2",
  881. .id = RT286_AIF2,
  882. .playback = {
  883. .stream_name = "AIF2 Playback",
  884. .channels_min = 1,
  885. .channels_max = 2,
  886. .rates = RT286_STEREO_RATES,
  887. .formats = RT286_FORMATS,
  888. },
  889. .capture = {
  890. .stream_name = "AIF2 Capture",
  891. .channels_min = 1,
  892. .channels_max = 2,
  893. .rates = RT286_STEREO_RATES,
  894. .formats = RT286_FORMATS,
  895. },
  896. .ops = &rt286_aif_dai_ops,
  897. .symmetric_rates = 1,
  898. },
  899. };
  900. static const struct snd_soc_component_driver soc_component_dev_rt286 = {
  901. .probe = rt286_probe,
  902. .remove = rt286_remove,
  903. .suspend = rt286_suspend,
  904. .resume = rt286_resume,
  905. .set_bias_level = rt286_set_bias_level,
  906. .controls = rt286_snd_controls,
  907. .num_controls = ARRAY_SIZE(rt286_snd_controls),
  908. .dapm_widgets = rt286_dapm_widgets,
  909. .num_dapm_widgets = ARRAY_SIZE(rt286_dapm_widgets),
  910. .dapm_routes = rt286_dapm_routes,
  911. .num_dapm_routes = ARRAY_SIZE(rt286_dapm_routes),
  912. .use_pmdown_time = 1,
  913. .endianness = 1,
  914. .non_legacy_dai_naming = 1,
  915. };
  916. static const struct regmap_config rt286_regmap = {
  917. .reg_bits = 32,
  918. .val_bits = 32,
  919. .max_register = 0x02370100,
  920. .volatile_reg = rt286_volatile_register,
  921. .readable_reg = rt286_readable_register,
  922. .reg_write = rl6347a_hw_write,
  923. .reg_read = rl6347a_hw_read,
  924. .cache_type = REGCACHE_RBTREE,
  925. .reg_defaults = rt286_reg,
  926. .num_reg_defaults = ARRAY_SIZE(rt286_reg),
  927. };
  928. static const struct i2c_device_id rt286_i2c_id[] = {
  929. {"rt286", 0},
  930. {"rt288", 0},
  931. {}
  932. };
  933. MODULE_DEVICE_TABLE(i2c, rt286_i2c_id);
  934. #ifdef CONFIG_ACPI
  935. static const struct acpi_device_id rt286_acpi_match[] = {
  936. { "INT343A", 0 },
  937. {},
  938. };
  939. MODULE_DEVICE_TABLE(acpi, rt286_acpi_match);
  940. #endif
  941. static const struct dmi_system_id force_combo_jack_table[] = {
  942. {
  943. .ident = "Intel Wilson Beach",
  944. .matches = {
  945. DMI_MATCH(DMI_BOARD_NAME, "Wilson Beach SDS")
  946. }
  947. },
  948. {
  949. .ident = "Intel Skylake RVP",
  950. .matches = {
  951. DMI_MATCH(DMI_PRODUCT_NAME, "Skylake Client platform")
  952. }
  953. },
  954. {
  955. .ident = "Intel Kabylake RVP",
  956. .matches = {
  957. DMI_MATCH(DMI_PRODUCT_NAME, "Kabylake Client platform")
  958. }
  959. },
  960. {
  961. .ident = "Thinkpad Helix 2nd",
  962. .matches = {
  963. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  964. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad Helix 2nd")
  965. }
  966. },
  967. { }
  968. };
  969. static const struct dmi_system_id dmi_dell[] = {
  970. {
  971. .ident = "Dell",
  972. .matches = {
  973. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  974. }
  975. },
  976. { }
  977. };
  978. static int rt286_i2c_probe(struct i2c_client *i2c,
  979. const struct i2c_device_id *id)
  980. {
  981. struct rt286_platform_data *pdata = dev_get_platdata(&i2c->dev);
  982. struct rt286_priv *rt286;
  983. int i, ret, vendor_id;
  984. rt286 = devm_kzalloc(&i2c->dev, sizeof(*rt286),
  985. GFP_KERNEL);
  986. if (NULL == rt286)
  987. return -ENOMEM;
  988. rt286->regmap = devm_regmap_init(&i2c->dev, NULL, i2c, &rt286_regmap);
  989. if (IS_ERR(rt286->regmap)) {
  990. ret = PTR_ERR(rt286->regmap);
  991. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  992. ret);
  993. return ret;
  994. }
  995. ret = regmap_read(rt286->regmap,
  996. RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID), &vendor_id);
  997. if (ret != 0) {
  998. dev_err(&i2c->dev, "I2C error %d\n", ret);
  999. return ret;
  1000. }
  1001. if (vendor_id != RT286_VENDOR_ID && vendor_id != RT288_VENDOR_ID) {
  1002. dev_err(&i2c->dev,
  1003. "Device with ID register %#x is not rt286\n",
  1004. vendor_id);
  1005. return -ENODEV;
  1006. }
  1007. rt286->index_cache = devm_kmemdup(&i2c->dev, rt286_index_def,
  1008. sizeof(rt286_index_def), GFP_KERNEL);
  1009. if (!rt286->index_cache)
  1010. return -ENOMEM;
  1011. rt286->index_cache_size = INDEX_CACHE_SIZE;
  1012. rt286->i2c = i2c;
  1013. i2c_set_clientdata(i2c, rt286);
  1014. /* restore codec default */
  1015. for (i = 0; i < INDEX_CACHE_SIZE; i++)
  1016. regmap_write(rt286->regmap, rt286->index_cache[i].reg,
  1017. rt286->index_cache[i].def);
  1018. for (i = 0; i < ARRAY_SIZE(rt286_reg); i++)
  1019. regmap_write(rt286->regmap, rt286_reg[i].reg,
  1020. rt286_reg[i].def);
  1021. if (pdata)
  1022. rt286->pdata = *pdata;
  1023. if ((vendor_id == RT288_VENDOR_ID && dmi_check_system(dmi_dell)) ||
  1024. dmi_check_system(force_combo_jack_table))
  1025. rt286->pdata.cbj_en = true;
  1026. regmap_write(rt286->regmap, RT286_SET_AUDIO_POWER, AC_PWRST_D3);
  1027. for (i = 0; i < RT286_POWER_REG_LEN; i++)
  1028. regmap_write(rt286->regmap,
  1029. RT286_SET_POWER(rt286_support_power_controls[i]),
  1030. AC_PWRST_D1);
  1031. if (!rt286->pdata.cbj_en) {
  1032. regmap_write(rt286->regmap, RT286_CBJ_CTRL2, 0x0000);
  1033. regmap_write(rt286->regmap, RT286_MIC1_DET_CTRL, 0x0816);
  1034. regmap_update_bits(rt286->regmap,
  1035. RT286_CBJ_CTRL1, 0xf000, 0xb000);
  1036. } else {
  1037. regmap_update_bits(rt286->regmap,
  1038. RT286_CBJ_CTRL1, 0xf000, 0x5000);
  1039. }
  1040. mdelay(10);
  1041. if (!rt286->pdata.gpio2_en)
  1042. regmap_write(rt286->regmap, RT286_SET_DMIC2_DEFAULT, 0x4000);
  1043. else
  1044. regmap_write(rt286->regmap, RT286_SET_DMIC2_DEFAULT, 0);
  1045. mdelay(10);
  1046. regmap_write(rt286->regmap, RT286_MISC_CTRL1, 0x0000);
  1047. /* Power down LDO, VREF */
  1048. regmap_update_bits(rt286->regmap, RT286_POWER_CTRL2, 0xc, 0x0);
  1049. regmap_update_bits(rt286->regmap, RT286_POWER_CTRL1, 0x1001, 0x1001);
  1050. /* Set depop parameter */
  1051. regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL2, 0x403a, 0x401a);
  1052. regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL3, 0xf777, 0x4737);
  1053. regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL4, 0x00ff, 0x003f);
  1054. if (vendor_id == RT288_VENDOR_ID && dmi_check_system(dmi_dell)) {
  1055. regmap_update_bits(rt286->regmap,
  1056. RT286_SET_GPIO_MASK, 0x40, 0x40);
  1057. regmap_update_bits(rt286->regmap,
  1058. RT286_SET_GPIO_DIRECTION, 0x40, 0x40);
  1059. regmap_update_bits(rt286->regmap,
  1060. RT286_SET_GPIO_DATA, 0x40, 0x40);
  1061. regmap_update_bits(rt286->regmap,
  1062. RT286_GPIO_CTRL, 0xc, 0x8);
  1063. }
  1064. if (rt286->i2c->irq) {
  1065. ret = request_threaded_irq(rt286->i2c->irq, NULL, rt286_irq,
  1066. IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "rt286", rt286);
  1067. if (ret != 0) {
  1068. dev_err(&i2c->dev,
  1069. "Failed to reguest IRQ: %d\n", ret);
  1070. return ret;
  1071. }
  1072. }
  1073. ret = devm_snd_soc_register_component(&i2c->dev,
  1074. &soc_component_dev_rt286,
  1075. rt286_dai, ARRAY_SIZE(rt286_dai));
  1076. return ret;
  1077. }
  1078. static int rt286_i2c_remove(struct i2c_client *i2c)
  1079. {
  1080. struct rt286_priv *rt286 = i2c_get_clientdata(i2c);
  1081. if (i2c->irq)
  1082. free_irq(i2c->irq, rt286);
  1083. return 0;
  1084. }
  1085. static struct i2c_driver rt286_i2c_driver = {
  1086. .driver = {
  1087. .name = "rt286",
  1088. .acpi_match_table = ACPI_PTR(rt286_acpi_match),
  1089. },
  1090. .probe = rt286_i2c_probe,
  1091. .remove = rt286_i2c_remove,
  1092. .id_table = rt286_i2c_id,
  1093. };
  1094. module_i2c_driver(rt286_i2c_driver);
  1095. MODULE_DESCRIPTION("ASoC RT286 driver");
  1096. MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>");
  1097. MODULE_LICENSE("GPL");