es8156.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * es8156.c -- es8156 ALSA SoC audio driver
  3. * Copyright Everest Semiconductor Co.,Ltd
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/pm.h>
  15. #include <linux/i2c.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/of_gpio.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/tlv.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/initval.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/gpio.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/irq.h>
  31. #include <linux/regmap.h>
  32. #include "es8156.h"
  33. #define INVALID_GPIO -1
  34. #define GPIO_LOW 0
  35. #define GPIO_HIGH 1
  36. #define es8156_DEF_VOL 0xBF
  37. /*
  38. * If your system doesn't have MCLK, define this to 0 or the
  39. * driver will crash.
  40. */
  41. #define MCLK 0
  42. static struct snd_soc_component *es8156_codec;
  43. static const struct reg_default es8156_reg_defaults[] = {
  44. {0x00, 0x1c}, {0x01, 0x20}, {0x02, 0x00}, {0x03, 0x01},
  45. {0x04, 0x00}, {0x05, 0x04}, {0x06, 0x11}, {0x07, 0x00},
  46. {0x08, 0x06}, {0x09, 0x00}, {0x0a, 0x50}, {0x0b, 0x50},
  47. {0x0c, 0x00}, {0x0d, 0x10}, {0x10, 0x40}, {0x10, 0x40},
  48. {0x11, 0x00}, {0x12, 0x04}, {0x13, 0x11}, {0x14, 0xbf},
  49. {0x15, 0x00}, {0x16, 0x00}, {0x17, 0xf7}, {0x18, 0x00},
  50. {0x19, 0x20}, {0x1a, 0x00}, {0x20, 0x16}, {0x21, 0x7f},
  51. {0x22, 0x00}, {0x23, 0x86}, {0x24, 0x00}, {0x25, 0x07},
  52. {0xfc, 0x00}, {0xfd, 0x81}, {0xfe, 0x55}, {0xff, 0x10},
  53. };
  54. /* codec private data */
  55. struct es8156_priv {
  56. struct regmap *regmap;
  57. unsigned int dmic_amic;
  58. unsigned int sysclk;
  59. struct snd_pcm_hw_constraint_list *sysclk_constraints;
  60. struct clk *mclk;
  61. int debounce_time;
  62. int hp_det_invert;
  63. struct delayed_work work;
  64. int spk_ctl_gpio;
  65. int hp_det_gpio;
  66. bool muted;
  67. bool hp_inserted;
  68. bool spk_active_level;
  69. int pwr_count;
  70. };
  71. /*
  72. * es8156_reset
  73. */
  74. static int es8156_reset(struct snd_soc_component *codec)
  75. {
  76. snd_soc_component_write(codec, ES8156_RESET_REG00, 0x1c);
  77. usleep_range(5000, 5500);
  78. return snd_soc_component_write(codec, ES8156_RESET_REG00, 0x03);
  79. }
  80. static void es8156_enable_spk(struct es8156_priv *es8156, bool enable)
  81. {
  82. bool level;
  83. level = enable ? es8156->spk_active_level : !es8156->spk_active_level;
  84. gpio_set_value(es8156->spk_ctl_gpio, level);
  85. }
  86. static const char *es8156_DAC_SRC[] = { "Left to Left, Right to Right",
  87. "Right to both Left and Right","Left to both Left & Right", "Left to Right, Right to Left" };
  88. static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
  89. static const DECLARE_TLV_DB_SCALE(alc_gain_tlv,-2800,400,1);
  90. static SOC_ENUM_SINGLE_DECL(es8165_dac_enum, ES8156_MISC_CONTROL3_REG18, 4, es8156_DAC_SRC);
  91. static const struct snd_kcontrol_new es8156_DAC_MUX =SOC_DAPM_ENUM("Route", es8165_dac_enum);
  92. static const struct snd_kcontrol_new es8156_snd_controls[] = {
  93. SOC_SINGLE("Timer 1",ES8156_TIME_CONTROL1_REG0A,0,63,0),
  94. SOC_SINGLE("Timer 2",ES8156_TIME_CONTROL2_REG0B,0,63,0),
  95. SOC_SINGLE("DAC Automute Gate",ES8156_AUTOMUTE_SET_REG12,4,15,0),
  96. SOC_SINGLE_TLV("ALC Gain",ES8156_ALC_CONFIG1_REG15,1,7,1,alc_gain_tlv),
  97. SOC_SINGLE("ALC Ramp Rate",ES8156_ALC_CONFIG2_REG16,4,15,0),
  98. SOC_SINGLE("ALC Window Size",ES8156_ALC_CONFIG2_REG16,0,15,0),
  99. SOC_DOUBLE("ALC Maximum Minimum Volume",ES8156_ALC_CONFIG3_REG17,
  100. 4,0,15,0),
  101. /* DAC Digital controls */
  102. SOC_SINGLE_TLV("DAC Playback Volume", ES8156_VOLUME_CONTROL_REG14,
  103. 0, 0xff, 0, dac_vol_tlv),
  104. SOC_SINGLE("HP Switch",ES8156_ANALOG_SYS3_REG22,3,1,0),
  105. };
  106. static const struct snd_soc_dapm_widget es8156_dapm_widgets[] = {
  107. SND_SOC_DAPM_AIF_OUT("SDOUT", "I2S Capture", 0,
  108. ES8156_P2S_CONTROL_REG0D, 2, 0),
  109. SND_SOC_DAPM_AIF_IN("SDIN", "I2S Playback", 0,
  110. SND_SOC_NOPM, 0, 0),
  111. SND_SOC_DAPM_MUX("Channel Select Mux", SND_SOC_NOPM, 0, 0,
  112. &es8156_DAC_MUX),
  113. SND_SOC_DAPM_DAC("DACL", "Left Playback", ES8156_DAC_MUTE_REG13, 1, 1),
  114. SND_SOC_DAPM_DAC("DACR", "Right Playback", ES8156_DAC_MUTE_REG13, 2, 1),
  115. SND_SOC_DAPM_PGA("SDOUT TRISTATE", ES8156_P2S_CONTROL_REG0D,
  116. 0, 1, NULL, 0),
  117. SND_SOC_DAPM_OUTPUT("SDOUT"),
  118. SND_SOC_DAPM_OUTPUT("LOUT"),
  119. SND_SOC_DAPM_OUTPUT("ROUT"),
  120. };
  121. static const struct snd_soc_dapm_route es8156_dapm_routes[] = {
  122. {"SDOUT TRISTATE",NULL,"SDIN"},
  123. {"SDOUT",NULL,"SDOUT TRISTATE"},
  124. {"Channel Select Mux","Left to Left, Right to Right","SDIN"},
  125. {"Channel Select Mux","Right to both Left and Right","SDIN"},
  126. {"Channel Select Mux","Left to both Left & Right","SDIN"},
  127. {"Channel Select Mux","Left to Right, Right to Left","SDIN"},
  128. {"DACL",NULL,"Channel Select Mux"},
  129. {"DACR",NULL,"Channel Select Mux"},
  130. { "LOUT", NULL, "DACL" },
  131. { "ROUT", NULL, "DACR" },
  132. };
  133. static int es8156_set_dai_fmt(struct snd_soc_dai *codec_dai,
  134. unsigned int fmt)
  135. {
  136. struct snd_soc_component *codec = codec_dai->component;
  137. /* set master/slave audio interface */
  138. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  139. case SND_SOC_DAIFMT_CBM_CFM://es8156 master
  140. snd_soc_component_update_bits(codec, ES8156_SCLK_MODE_REG02, 0x01,0x01);
  141. break;
  142. case SND_SOC_DAIFMT_CBS_CFS://es8156 slave
  143. snd_soc_component_update_bits(codec, ES8156_SCLK_MODE_REG02, 0x01,0x00);
  144. break;
  145. default:
  146. return -EINVAL;
  147. }
  148. /* interface format */
  149. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  150. case SND_SOC_DAIFMT_I2S:
  151. snd_soc_component_update_bits(codec, ES8156_DAC_SDP_REG11, 0x07,0x00);
  152. break;
  153. case SND_SOC_DAIFMT_RIGHT_J:
  154. return -EINVAL;
  155. case SND_SOC_DAIFMT_LEFT_J:
  156. snd_soc_component_update_bits(codec, ES8156_DAC_SDP_REG11, 0x07,0x01);
  157. break;
  158. case SND_SOC_DAIFMT_DSP_A:
  159. snd_soc_component_update_bits(codec, ES8156_DAC_SDP_REG11, 0x07,0x03);
  160. break;
  161. case SND_SOC_DAIFMT_DSP_B:
  162. snd_soc_component_update_bits(codec, ES8156_DAC_SDP_REG11, 0x07,0x07);
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. /* clock inversion */
  168. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  169. case SND_SOC_DAIFMT_NB_NF:
  170. snd_soc_component_update_bits(codec, ES8156_SCLK_MODE_REG02, 0x01,0x00);
  171. break;
  172. case SND_SOC_DAIFMT_IB_IF:
  173. snd_soc_component_update_bits(codec, ES8156_SCLK_MODE_REG02, 0x01,0x01);
  174. break;
  175. case SND_SOC_DAIFMT_IB_NF:
  176. snd_soc_component_update_bits(codec, ES8156_SCLK_MODE_REG02, 0x01,0x01);
  177. break;
  178. case SND_SOC_DAIFMT_NB_IF:
  179. snd_soc_component_update_bits(codec, ES8156_SCLK_MODE_REG02, 0x01,0x00);
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. return 0;
  185. }
  186. static int es8156_pcm_hw_params(struct snd_pcm_substream *substream,
  187. struct snd_pcm_hw_params *params,
  188. struct snd_soc_dai *dai)
  189. {
  190. struct snd_soc_component *codec = dai->component;
  191. struct snd_pcm_runtime *runtime = substream->runtime;
  192. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  193. 4096, UINT_MAX);
  194. switch (params_format(params)) {
  195. case SNDRV_PCM_FORMAT_S16_LE:
  196. snd_soc_component_update_bits(codec, ES8156_DAC_SDP_REG11, 0x70,0x30);
  197. break;
  198. case SNDRV_PCM_FORMAT_S20_3LE:
  199. snd_soc_component_update_bits(codec, ES8156_DAC_SDP_REG11, 0x70,0x10);
  200. break;
  201. case SNDRV_PCM_FORMAT_S24_LE:
  202. snd_soc_component_update_bits(codec, ES8156_DAC_SDP_REG11, 0x70,0x00);
  203. break;
  204. case SNDRV_PCM_FORMAT_S32_LE:
  205. snd_soc_component_update_bits(codec, ES8156_DAC_SDP_REG11, 0x70,0x40);
  206. break;
  207. }
  208. return 0;
  209. }
  210. static int es8156_set_bias_level(struct snd_soc_component *codec,
  211. enum snd_soc_bias_level level)
  212. {
  213. int ret;
  214. struct es8156_priv *priv = snd_soc_component_get_drvdata(codec);
  215. switch (level)
  216. {
  217. case SND_SOC_BIAS_ON:
  218. break;
  219. case SND_SOC_BIAS_PREPARE:
  220. break;
  221. case SND_SOC_BIAS_STANDBY:
  222. /*
  223. *open i2s clock
  224. */
  225. if (snd_soc_component_get_bias_level(codec) == SND_SOC_BIAS_OFF)
  226. {
  227. if (!IS_ERR(priv->mclk))
  228. {
  229. ret = clk_prepare_enable(priv->mclk);
  230. snd_soc_component_write(codec, ES8156_SCLK_MODE_REG02,0x04);
  231. snd_soc_component_write(codec, ES8156_ANALOG_SYS1_REG20,0x2A);
  232. snd_soc_component_write(codec, ES8156_ANALOG_SYS2_REG21,0x3C);
  233. snd_soc_component_write(codec, ES8156_ANALOG_SYS3_REG22,0x08);
  234. snd_soc_component_write(codec, ES8156_ANALOG_LP_REG24,0x07);
  235. if(ES8156_DVDD==ES8156_1V8)
  236. snd_soc_component_write(codec, ES8156_ANALOG_SYS4_REG23,VMIDLEVEL3<<4);
  237. else
  238. snd_soc_component_write(codec, ES8156_ANALOG_SYS4_REG23,0x00);
  239. snd_soc_component_write(codec, ES8156_TIME_CONTROL1_REG0A,0x01);
  240. snd_soc_component_write(codec, ES8156_TIME_CONTROL2_REG0B,0x01);
  241. snd_soc_component_write(codec, ES8156_VOLUME_CONTROL_REG14,0xBF);
  242. snd_soc_component_write(codec, ES8156_MAINCLOCK_CTL_REG01,0x21);
  243. snd_soc_component_write(codec, ES8156_P2S_CONTROL_REG0D,0x14);
  244. snd_soc_component_write(codec, ES8156_MISC_CONTROL3_REG18,0x00);
  245. snd_soc_component_write(codec, ES8156_CLOCK_ON_OFF_REG08,0x3F);
  246. snd_soc_component_write(codec, ES8156_RESET_REG00,0x02);
  247. snd_soc_component_write(codec, ES8156_RESET_REG00,0x03);
  248. snd_soc_component_write(codec, ES8156_ANALOG_SYS5_REG25,0x20);
  249. if (ret)
  250. {
  251. dev_err(codec->dev,
  252. "Failed to enable master clock: %d\n",
  253. ret);
  254. return ret;
  255. }
  256. }
  257. }
  258. break;
  259. case SND_SOC_BIAS_OFF:
  260. snd_soc_component_write(codec, ES8156_VOLUME_CONTROL_REG14, 0x00);
  261. snd_soc_component_write(codec, ES8156_EQ_CONTROL1_REG19, 0x02);
  262. snd_soc_component_write(codec, ES8156_ANALOG_SYS2_REG21, 0x1F);
  263. snd_soc_component_write(codec, ES8156_ANALOG_SYS3_REG22, 0x02);
  264. snd_soc_component_write(codec, ES8156_ANALOG_SYS5_REG25, 0x21);
  265. snd_soc_component_write(codec, ES8156_ANALOG_SYS5_REG25, 0x01);
  266. snd_soc_component_write(codec, ES8156_ANALOG_SYS5_REG25, 0x87);
  267. snd_soc_component_write(codec, ES8156_MISC_CONTROL3_REG18, 0x01);
  268. snd_soc_component_write(codec, ES8156_MISC_CONTROL2_REG09, 0x02);
  269. snd_soc_component_write(codec, ES8156_MISC_CONTROL2_REG09, 0x01);
  270. snd_soc_component_write(codec, ES8156_CLOCK_ON_OFF_REG08, 0x00);
  271. /*
  272. *close i2s clock
  273. */
  274. if (!IS_ERR(priv->mclk))
  275. clk_disable_unprepare(priv->mclk);
  276. break;
  277. }
  278. return 0;
  279. }
  280. #define es8156_RATES SNDRV_PCM_RATE_8000_96000
  281. #define es8156_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  282. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FORMAT_S32_LE)
  283. static struct snd_soc_dai_ops es8156_ops = {
  284. .startup = NULL,
  285. .hw_params = es8156_pcm_hw_params,
  286. .set_fmt = es8156_set_dai_fmt,
  287. .set_sysclk = NULL,
  288. .shutdown = NULL,
  289. };
  290. static struct snd_soc_dai_driver es8156_dai = {
  291. .name = "ES8156 HiFi",
  292. .playback = {
  293. .stream_name = "Playback",
  294. .channels_min = 1,
  295. .channels_max = 2,
  296. .rates = es8156_RATES,
  297. .formats = es8156_FORMATS,
  298. },
  299. .ops = &es8156_ops,
  300. .symmetric_rates = 1,
  301. };
  302. static int es8156_init_regs(struct snd_soc_component *codec)
  303. {
  304. /*
  305. *set clock and analog power
  306. */
  307. snd_soc_component_write(codec, ES8156_SCLK_MODE_REG02,0x04);
  308. snd_soc_component_write(codec, ES8156_ANALOG_SYS1_REG20,0x2A);
  309. snd_soc_component_write(codec, ES8156_ANALOG_SYS2_REG21,0x3C);
  310. snd_soc_component_write(codec, ES8156_ANALOG_SYS3_REG22,0x08);
  311. snd_soc_component_write(codec, ES8156_ANALOG_LP_REG24,0x07);
  312. if(ES8156_DVDD==ES8156_1V8)
  313. snd_soc_component_write(codec, ES8156_ANALOG_SYS4_REG23,VMIDLEVEL3<<4);
  314. else
  315. snd_soc_component_write(codec, ES8156_ANALOG_SYS4_REG23,0x00);
  316. /*
  317. *set powerup time
  318. */
  319. snd_soc_component_write(codec, ES8156_TIME_CONTROL1_REG0A,0x01);
  320. snd_soc_component_write(codec, ES8156_TIME_CONTROL2_REG0B,0x01);
  321. /*
  322. *set digtal volume
  323. */
  324. snd_soc_component_write(codec, ES8156_VOLUME_CONTROL_REG14,0xBF);
  325. /*
  326. *set MCLK
  327. */
  328. snd_soc_component_write(codec, ES8156_MAINCLOCK_CTL_REG01,0x21);
  329. snd_soc_component_write(codec, ES8156_P2S_CONTROL_REG0D,0x14);
  330. snd_soc_component_write(codec, ES8156_MISC_CONTROL3_REG18,0x00);
  331. snd_soc_component_write(codec, ES8156_CLOCK_ON_OFF_REG08,0x3F);
  332. snd_soc_component_write(codec, ES8156_RESET_REG00,0x02);
  333. snd_soc_component_write(codec, ES8156_RESET_REG00,0x03);
  334. snd_soc_component_write(codec, ES8156_ANALOG_SYS5_REG25,0x20);
  335. return 0;
  336. }
  337. static int es8156_suspend(struct snd_soc_component *codec)
  338. {
  339. es8156_set_bias_level(codec, SND_SOC_BIAS_OFF);
  340. printk("es8156_suspend\n");
  341. return 0;
  342. }
  343. static int es8156_resume(struct snd_soc_component *codec)
  344. {
  345. return 0;
  346. }
  347. #ifdef HP_DET_FUNTION
  348. static irqreturn_t es8156_irq_handler(int irq, void *data)
  349. {
  350. struct es8156_priv *es8156 = data;
  351. queue_delayed_work(system_power_efficient_wq, &es8156->work,
  352. msecs_to_jiffies(es8156->debounce_time));
  353. return IRQ_HANDLED;
  354. }
  355. #endif
  356. /*
  357. * Call from rk_headset_irq_hook_adc.c
  358. *
  359. * Enable micbias for HOOK detection and disable external Amplifier
  360. * when jack insertion.
  361. */
  362. int es8156_headset_detect(int jack_insert)
  363. {
  364. struct es8156_priv *es8156;
  365. if (!es8156_codec)
  366. return -1;
  367. es8156 = snd_soc_component_get_drvdata(es8156_codec);
  368. es8156->hp_inserted = jack_insert;
  369. /*enable micbias and disable PA*/
  370. if (jack_insert) {
  371. es8156_enable_spk(es8156, false);
  372. }
  373. return 0;
  374. }
  375. EXPORT_SYMBOL(es8156_headset_detect);
  376. #ifdef HP_DET_FUNTION
  377. static void hp_work(struct work_struct *work)
  378. {
  379. struct es8156_priv *es8156;
  380. int enable;
  381. es8156 = container_of(work, struct es8156_priv, work.work);
  382. enable = gpio_get_value(es8156->hp_det_gpio);
  383. if (es8156->hp_det_invert)
  384. enable = !enable;
  385. es8156->hp_inserted = enable ? true : false;
  386. if (!es8156->muted) {
  387. if (es8156->hp_inserted)
  388. es8156_enable_spk(es8156, false);
  389. else
  390. es8156_enable_spk(es8156, true);
  391. }
  392. }
  393. #endif
  394. static int es8156_probe(struct snd_soc_component *codec)
  395. {
  396. #if MCLK
  397. struct es8156_priv *es8156 = snd_soc_component_get_drvdata(codec);
  398. #endif
  399. int ret = 0;
  400. es8156_codec = codec;
  401. #if MCLK
  402. es8156->mclk = devm_clk_get(codec->dev, "mclk");
  403. if (PTR_ERR(es8156->mclk) == -EPROBE_DEFER)
  404. return -EPROBE_DEFER;
  405. ret = clk_prepare_enable(es8156->mclk);
  406. #endif
  407. es8156_reset(codec);
  408. es8156_init_regs(codec);
  409. return ret;
  410. }
  411. static void es8156_remove(struct snd_soc_component *codec)
  412. {
  413. es8156_set_bias_level(codec, SND_SOC_BIAS_OFF);
  414. }
  415. const struct regmap_config es8156_regmap_config = {
  416. .reg_bits = 8,
  417. .val_bits = 8,
  418. .max_register = 0xff,
  419. .cache_type = REGCACHE_RBTREE,
  420. .reg_defaults = es8156_reg_defaults,
  421. .num_reg_defaults = ARRAY_SIZE(es8156_reg_defaults),
  422. };
  423. static struct snd_soc_component_driver soc_codec_dev_es8156 = {
  424. .probe = es8156_probe,
  425. .remove = es8156_remove,
  426. .suspend = es8156_suspend,
  427. .resume = es8156_resume,
  428. .set_bias_level = es8156_set_bias_level,
  429. .controls = es8156_snd_controls,
  430. .num_controls = ARRAY_SIZE(es8156_snd_controls),
  431. .dapm_widgets = es8156_dapm_widgets,
  432. .num_dapm_widgets = ARRAY_SIZE(es8156_dapm_widgets),
  433. .dapm_routes = es8156_dapm_routes,
  434. .num_dapm_routes = ARRAY_SIZE(es8156_dapm_routes),
  435. };
  436. /*
  437. *es8156 7bit i2c address:CE pin:0 0x08 / CE pin:1 0x09
  438. */
  439. static int es8156_i2c_probe(struct i2c_client *i2c,
  440. const struct i2c_device_id *id)
  441. {
  442. struct es8156_priv *es8156;
  443. int ret = -1;
  444. #ifdef HP_DET_FUNTION
  445. int hp_irq;
  446. enum of_gpio_flags flags;
  447. struct device_node *np = i2c->dev.of_node;
  448. #endif
  449. es8156 = devm_kzalloc(&i2c->dev, sizeof(*es8156), GFP_KERNEL);
  450. if (!es8156)
  451. return -ENOMEM;
  452. es8156->debounce_time = 200;
  453. es8156->hp_det_invert = 0;
  454. es8156->pwr_count = 0;
  455. es8156->hp_inserted = false;
  456. es8156->muted = true;
  457. es8156->regmap = devm_regmap_init_i2c(i2c, &es8156_regmap_config);
  458. if (IS_ERR(es8156->regmap)) {
  459. ret = PTR_ERR(es8156->regmap);
  460. dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
  461. return ret;
  462. }
  463. i2c_set_clientdata(i2c, es8156);
  464. #ifdef HP_DET_FUNTION
  465. es8156->spk_ctl_gpio = of_get_named_gpio_flags(np,
  466. "spk-con-gpio",
  467. 0,
  468. &flags);
  469. if (es8156->spk_ctl_gpio < 0) {
  470. dev_info(&i2c->dev, "Can not read property spk_ctl_gpio\n");
  471. es8156->spk_ctl_gpio = INVALID_GPIO;
  472. } else {
  473. es8156->spk_active_level = !(flags & OF_GPIO_ACTIVE_LOW);
  474. ret = devm_gpio_request_one(&i2c->dev, es8156->spk_ctl_gpio,
  475. GPIOF_DIR_OUT, NULL);
  476. if (ret) {
  477. dev_err(&i2c->dev, "Failed to request spk_ctl_gpio\n");
  478. return ret;
  479. }
  480. es8156_enable_spk(es8156, false);
  481. }
  482. es8156->hp_det_gpio = of_get_named_gpio_flags(np,
  483. "hp-det-gpio",
  484. 0,
  485. &flags);
  486. if (es8156->hp_det_gpio < 0) {
  487. dev_info(&i2c->dev, "Can not read property hp_det_gpio\n");
  488. es8156->hp_det_gpio = INVALID_GPIO;
  489. } else {
  490. INIT_DELAYED_WORK(&es8156->work, hp_work);
  491. es8156->hp_det_invert = !!(flags & OF_GPIO_ACTIVE_LOW);
  492. ret = devm_gpio_request_one(&i2c->dev, es8156->hp_det_gpio,
  493. GPIOF_IN, "hp det");
  494. if (ret < 0)
  495. return ret;
  496. hp_irq = gpio_to_irq(es8156->hp_det_gpio);
  497. ret = devm_request_threaded_irq(&i2c->dev, hp_irq, NULL,
  498. es8156_irq_handler,
  499. IRQF_TRIGGER_FALLING |
  500. IRQF_TRIGGER_RISING |
  501. IRQF_ONESHOT,
  502. "es8156_interrupt", es8156);
  503. if (ret < 0) {
  504. dev_err(&i2c->dev, "request_irq failed: %d\n", ret);
  505. return ret;
  506. }
  507. schedule_delayed_work(&es8156->work,
  508. msecs_to_jiffies(es8156->debounce_time));
  509. }
  510. #endif
  511. ret = snd_soc_register_component(&i2c->dev,
  512. &soc_codec_dev_es8156,
  513. &es8156_dai, 1);
  514. return ret;
  515. }
  516. static int es8156_i2c_remove(struct i2c_client *client)
  517. {
  518. snd_soc_unregister_component(&client->dev);
  519. return 0;
  520. }
  521. static void es8156_i2c_shutdown(struct i2c_client *client)
  522. {
  523. struct es8156_priv *es8156 = i2c_get_clientdata(client);
  524. if (es8156_codec != NULL) {
  525. es8156_enable_spk(es8156, false);
  526. msleep(20);
  527. es8156_set_bias_level(es8156_codec, SND_SOC_BIAS_OFF);
  528. }
  529. }
  530. static const struct i2c_device_id es8156_i2c_id[] = {
  531. {"es8156", 0},
  532. { }
  533. };
  534. MODULE_DEVICE_TABLE(i2c, es8156_i2c_id);
  535. static const struct of_device_id es8156_of_match[] = {
  536. { .compatible = "everest,es8156", },
  537. { }
  538. };
  539. MODULE_DEVICE_TABLE(of, es8156_of_match);
  540. static struct i2c_driver es8156_i2c_driver = {
  541. .driver = {
  542. .name = "es8156",
  543. .of_match_table = es8156_of_match,
  544. },
  545. .probe = es8156_i2c_probe,
  546. .remove = es8156_i2c_remove,
  547. .shutdown = es8156_i2c_shutdown,
  548. .id_table = es8156_i2c_id,
  549. };
  550. module_i2c_driver(es8156_i2c_driver);
  551. MODULE_DESCRIPTION("ASoC es8156 driver");
  552. MODULE_LICENSE("GPL");