sn95031.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * sn95031.c - TI sn95031 Codec driver
  3. *
  4. * Copyright (C) 2010 Intel Corp
  5. * Author: Vinod Koul <vinod.koul@intel.com>
  6. * Author: Harsha Priya <priya.harsha@intel.com>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. *
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/platform_device.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/module.h>
  31. #include <asm/intel_scu_ipc.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include <sound/soc-dapm.h>
  36. #include <sound/initval.h>
  37. #include <sound/tlv.h>
  38. #include <sound/jack.h>
  39. #include "sn95031.h"
  40. #define SN95031_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100)
  41. #define SN95031_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  42. /* adc helper functions */
  43. /* enables mic bias voltage */
  44. static void sn95031_enable_mic_bias(struct snd_soc_codec *codec)
  45. {
  46. snd_soc_write(codec, SN95031_VAUD, BIT(2)|BIT(1)|BIT(0));
  47. snd_soc_update_bits(codec, SN95031_MICBIAS, BIT(2), BIT(2));
  48. }
  49. /* Enable/Disable the ADC depending on the argument */
  50. static void configure_adc(struct snd_soc_codec *sn95031_codec, int val)
  51. {
  52. int value = snd_soc_read(sn95031_codec, SN95031_ADC1CNTL1);
  53. if (val) {
  54. /* Enable and start the ADC */
  55. value |= (SN95031_ADC_ENBL | SN95031_ADC_START);
  56. value &= (~SN95031_ADC_NO_LOOP);
  57. } else {
  58. /* Just stop the ADC */
  59. value &= (~SN95031_ADC_START);
  60. }
  61. snd_soc_write(sn95031_codec, SN95031_ADC1CNTL1, value);
  62. }
  63. /*
  64. * finds an empty channel for conversion
  65. * If the ADC is not enabled then start using 0th channel
  66. * itself. Otherwise find an empty channel by looking for a
  67. * channel in which the stopbit is set to 1. returns the index
  68. * of the first free channel if succeeds or an error code.
  69. *
  70. * Context: can sleep
  71. *
  72. */
  73. static int find_free_channel(struct snd_soc_codec *sn95031_codec)
  74. {
  75. int i, value;
  76. /* check whether ADC is enabled */
  77. value = snd_soc_read(sn95031_codec, SN95031_ADC1CNTL1);
  78. if ((value & SN95031_ADC_ENBL) == 0)
  79. return 0;
  80. /* ADC is already enabled; Looking for an empty channel */
  81. for (i = 0; i < SN95031_ADC_CHANLS_MAX; i++) {
  82. value = snd_soc_read(sn95031_codec,
  83. SN95031_ADC_CHNL_START_ADDR + i);
  84. if (value & SN95031_STOPBIT_MASK)
  85. break;
  86. }
  87. return (i == SN95031_ADC_CHANLS_MAX) ? (-EINVAL) : i;
  88. }
  89. /* Initialize the ADC for reading micbias values. Can sleep. */
  90. static int sn95031_initialize_adc(struct snd_soc_codec *sn95031_codec)
  91. {
  92. int base_addr, chnl_addr;
  93. int value;
  94. int channel_index;
  95. /* Index of the first channel in which the stop bit is set */
  96. channel_index = find_free_channel(sn95031_codec);
  97. if (channel_index < 0) {
  98. pr_err("No free ADC channels");
  99. return channel_index;
  100. }
  101. base_addr = SN95031_ADC_CHNL_START_ADDR + channel_index;
  102. if (!(channel_index == 0 || channel_index == SN95031_ADC_LOOP_MAX)) {
  103. /* Reset stop bit for channels other than 0 and 12 */
  104. value = snd_soc_read(sn95031_codec, base_addr);
  105. /* Set the stop bit to zero */
  106. snd_soc_write(sn95031_codec, base_addr, value & 0xEF);
  107. /* Index of the first free channel */
  108. base_addr++;
  109. channel_index++;
  110. }
  111. /* Since this is the last channel, set the stop bit
  112. to 1 by ORing the DIE_SENSOR_CODE with 0x10 */
  113. snd_soc_write(sn95031_codec, base_addr,
  114. SN95031_AUDIO_DETECT_CODE | 0x10);
  115. chnl_addr = SN95031_ADC_DATA_START_ADDR + 2 * channel_index;
  116. pr_debug("mid_initialize : %x", chnl_addr);
  117. configure_adc(sn95031_codec, 1);
  118. return chnl_addr;
  119. }
  120. /* reads the ADC registers and gets the mic bias value in mV. */
  121. static unsigned int sn95031_get_mic_bias(struct snd_soc_codec *codec)
  122. {
  123. u16 adc_adr = sn95031_initialize_adc(codec);
  124. u16 adc_val1, adc_val2;
  125. unsigned int mic_bias;
  126. sn95031_enable_mic_bias(codec);
  127. /* Enable the sound card for conversion before reading */
  128. snd_soc_write(codec, SN95031_ADC1CNTL3, 0x05);
  129. /* Re-toggle the RRDATARD bit */
  130. snd_soc_write(codec, SN95031_ADC1CNTL3, 0x04);
  131. /* Read the higher bits of data */
  132. msleep(1000);
  133. adc_val1 = snd_soc_read(codec, adc_adr);
  134. adc_adr++;
  135. adc_val2 = snd_soc_read(codec, adc_adr);
  136. /* Adding lower two bits to the higher bits */
  137. mic_bias = (adc_val1 << 2) + (adc_val2 & 3);
  138. mic_bias = (mic_bias * SN95031_ADC_ONE_LSB_MULTIPLIER) / 1000;
  139. pr_debug("mic bias = %dmV\n", mic_bias);
  140. return mic_bias;
  141. }
  142. /*end - adc helper functions */
  143. static int sn95031_read(void *ctx, unsigned int reg, unsigned int *val)
  144. {
  145. u8 value = 0;
  146. int ret;
  147. ret = intel_scu_ipc_ioread8(reg, &value);
  148. if (ret == 0)
  149. *val = value;
  150. return ret;
  151. }
  152. static int sn95031_write(void *ctx, unsigned int reg, unsigned int value)
  153. {
  154. return intel_scu_ipc_iowrite8(reg, value);
  155. }
  156. static const struct regmap_config sn95031_regmap = {
  157. .reg_read = sn95031_read,
  158. .reg_write = sn95031_write,
  159. };
  160. static int sn95031_set_vaud_bias(struct snd_soc_codec *codec,
  161. enum snd_soc_bias_level level)
  162. {
  163. switch (level) {
  164. case SND_SOC_BIAS_ON:
  165. break;
  166. case SND_SOC_BIAS_PREPARE:
  167. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_STANDBY) {
  168. pr_debug("vaud_bias powering up pll\n");
  169. /* power up the pll */
  170. snd_soc_write(codec, SN95031_AUDPLLCTRL, BIT(5));
  171. /* enable pcm 2 */
  172. snd_soc_update_bits(codec, SN95031_PCM2C2,
  173. BIT(0), BIT(0));
  174. }
  175. break;
  176. case SND_SOC_BIAS_STANDBY:
  177. switch (snd_soc_codec_get_bias_level(codec)) {
  178. case SND_SOC_BIAS_OFF:
  179. pr_debug("vaud_bias power up rail\n");
  180. /* power up the rail */
  181. snd_soc_write(codec, SN95031_VAUD,
  182. BIT(2)|BIT(1)|BIT(0));
  183. msleep(1);
  184. break;
  185. case SND_SOC_BIAS_PREPARE:
  186. /* turn off pcm */
  187. pr_debug("vaud_bias power dn pcm\n");
  188. snd_soc_update_bits(codec, SN95031_PCM2C2, BIT(0), 0);
  189. snd_soc_write(codec, SN95031_AUDPLLCTRL, 0);
  190. break;
  191. default:
  192. break;
  193. }
  194. break;
  195. case SND_SOC_BIAS_OFF:
  196. pr_debug("vaud_bias _OFF doing rail shutdown\n");
  197. snd_soc_write(codec, SN95031_VAUD, BIT(3));
  198. break;
  199. }
  200. return 0;
  201. }
  202. static int sn95031_vhs_event(struct snd_soc_dapm_widget *w,
  203. struct snd_kcontrol *kcontrol, int event)
  204. {
  205. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  206. if (SND_SOC_DAPM_EVENT_ON(event)) {
  207. pr_debug("VHS SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
  208. /* power up the rail */
  209. snd_soc_write(codec, SN95031_VHSP, 0x3D);
  210. snd_soc_write(codec, SN95031_VHSN, 0x3F);
  211. msleep(1);
  212. } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
  213. pr_debug("VHS SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
  214. snd_soc_write(codec, SN95031_VHSP, 0xC4);
  215. snd_soc_write(codec, SN95031_VHSN, 0x04);
  216. }
  217. return 0;
  218. }
  219. static int sn95031_vihf_event(struct snd_soc_dapm_widget *w,
  220. struct snd_kcontrol *kcontrol, int event)
  221. {
  222. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  223. if (SND_SOC_DAPM_EVENT_ON(event)) {
  224. pr_debug("VIHF SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
  225. /* power up the rail */
  226. snd_soc_write(codec, SN95031_VIHF, 0x27);
  227. msleep(1);
  228. } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
  229. pr_debug("VIHF SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
  230. snd_soc_write(codec, SN95031_VIHF, 0x24);
  231. }
  232. return 0;
  233. }
  234. static int sn95031_dmic12_event(struct snd_soc_dapm_widget *w,
  235. struct snd_kcontrol *k, int event)
  236. {
  237. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  238. unsigned int ldo = 0, clk_dir = 0, data_dir = 0;
  239. if (SND_SOC_DAPM_EVENT_ON(event)) {
  240. ldo = BIT(5)|BIT(4);
  241. clk_dir = BIT(0);
  242. data_dir = BIT(7);
  243. }
  244. /* program DMIC LDO, clock and set clock */
  245. snd_soc_update_bits(codec, SN95031_MICBIAS, BIT(5)|BIT(4), ldo);
  246. snd_soc_update_bits(codec, SN95031_DMICBUF0123, BIT(0), clk_dir);
  247. snd_soc_update_bits(codec, SN95031_DMICBUF0123, BIT(7), data_dir);
  248. return 0;
  249. }
  250. static int sn95031_dmic34_event(struct snd_soc_dapm_widget *w,
  251. struct snd_kcontrol *k, int event)
  252. {
  253. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  254. unsigned int ldo = 0, clk_dir = 0, data_dir = 0;
  255. if (SND_SOC_DAPM_EVENT_ON(event)) {
  256. ldo = BIT(5)|BIT(4);
  257. clk_dir = BIT(2);
  258. data_dir = BIT(1);
  259. }
  260. /* program DMIC LDO, clock and set clock */
  261. snd_soc_update_bits(codec, SN95031_MICBIAS, BIT(5)|BIT(4), ldo);
  262. snd_soc_update_bits(codec, SN95031_DMICBUF0123, BIT(2), clk_dir);
  263. snd_soc_update_bits(codec, SN95031_DMICBUF45, BIT(1), data_dir);
  264. return 0;
  265. }
  266. static int sn95031_dmic56_event(struct snd_soc_dapm_widget *w,
  267. struct snd_kcontrol *k, int event)
  268. {
  269. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  270. unsigned int ldo = 0;
  271. if (SND_SOC_DAPM_EVENT_ON(event))
  272. ldo = BIT(7)|BIT(6);
  273. /* program DMIC LDO */
  274. snd_soc_update_bits(codec, SN95031_MICBIAS, BIT(7)|BIT(6), ldo);
  275. return 0;
  276. }
  277. /* mux controls */
  278. static const char *sn95031_mic_texts[] = { "AMIC", "LineIn" };
  279. static SOC_ENUM_SINGLE_DECL(sn95031_micl_enum,
  280. SN95031_ADCCONFIG, 1, sn95031_mic_texts);
  281. static const struct snd_kcontrol_new sn95031_micl_mux_control =
  282. SOC_DAPM_ENUM("Route", sn95031_micl_enum);
  283. static SOC_ENUM_SINGLE_DECL(sn95031_micr_enum,
  284. SN95031_ADCCONFIG, 3, sn95031_mic_texts);
  285. static const struct snd_kcontrol_new sn95031_micr_mux_control =
  286. SOC_DAPM_ENUM("Route", sn95031_micr_enum);
  287. static const char *sn95031_input_texts[] = { "DMIC1", "DMIC2", "DMIC3",
  288. "DMIC4", "DMIC5", "DMIC6",
  289. "ADC Left", "ADC Right" };
  290. static SOC_ENUM_SINGLE_DECL(sn95031_input1_enum,
  291. SN95031_AUDIOMUX12, 0, sn95031_input_texts);
  292. static const struct snd_kcontrol_new sn95031_input1_mux_control =
  293. SOC_DAPM_ENUM("Route", sn95031_input1_enum);
  294. static SOC_ENUM_SINGLE_DECL(sn95031_input2_enum,
  295. SN95031_AUDIOMUX12, 4, sn95031_input_texts);
  296. static const struct snd_kcontrol_new sn95031_input2_mux_control =
  297. SOC_DAPM_ENUM("Route", sn95031_input2_enum);
  298. static SOC_ENUM_SINGLE_DECL(sn95031_input3_enum,
  299. SN95031_AUDIOMUX34, 0, sn95031_input_texts);
  300. static const struct snd_kcontrol_new sn95031_input3_mux_control =
  301. SOC_DAPM_ENUM("Route", sn95031_input3_enum);
  302. static SOC_ENUM_SINGLE_DECL(sn95031_input4_enum,
  303. SN95031_AUDIOMUX34, 4, sn95031_input_texts);
  304. static const struct snd_kcontrol_new sn95031_input4_mux_control =
  305. SOC_DAPM_ENUM("Route", sn95031_input4_enum);
  306. /* capture path controls */
  307. static const char *sn95031_micmode_text[] = {"Single Ended", "Differential"};
  308. /* 0dB to 30dB in 10dB steps */
  309. static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 10, 0);
  310. static SOC_ENUM_SINGLE_DECL(sn95031_micmode1_enum,
  311. SN95031_MICAMP1, 1, sn95031_micmode_text);
  312. static SOC_ENUM_SINGLE_DECL(sn95031_micmode2_enum,
  313. SN95031_MICAMP2, 1, sn95031_micmode_text);
  314. static const char *sn95031_dmic_cfg_text[] = {"GPO", "DMIC"};
  315. static SOC_ENUM_SINGLE_DECL(sn95031_dmic12_cfg_enum,
  316. SN95031_DMICMUX, 0, sn95031_dmic_cfg_text);
  317. static SOC_ENUM_SINGLE_DECL(sn95031_dmic34_cfg_enum,
  318. SN95031_DMICMUX, 1, sn95031_dmic_cfg_text);
  319. static SOC_ENUM_SINGLE_DECL(sn95031_dmic56_cfg_enum,
  320. SN95031_DMICMUX, 2, sn95031_dmic_cfg_text);
  321. static const struct snd_kcontrol_new sn95031_snd_controls[] = {
  322. SOC_ENUM("Mic1Mode Capture Route", sn95031_micmode1_enum),
  323. SOC_ENUM("Mic2Mode Capture Route", sn95031_micmode2_enum),
  324. SOC_ENUM("DMIC12 Capture Route", sn95031_dmic12_cfg_enum),
  325. SOC_ENUM("DMIC34 Capture Route", sn95031_dmic34_cfg_enum),
  326. SOC_ENUM("DMIC56 Capture Route", sn95031_dmic56_cfg_enum),
  327. SOC_SINGLE_TLV("Mic1 Capture Volume", SN95031_MICAMP1,
  328. 2, 4, 0, mic_tlv),
  329. SOC_SINGLE_TLV("Mic2 Capture Volume", SN95031_MICAMP2,
  330. 2, 4, 0, mic_tlv),
  331. };
  332. /* DAPM widgets */
  333. static const struct snd_soc_dapm_widget sn95031_dapm_widgets[] = {
  334. /* all end points mic, hs etc */
  335. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  336. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  337. SND_SOC_DAPM_OUTPUT("EPOUT"),
  338. SND_SOC_DAPM_OUTPUT("IHFOUTL"),
  339. SND_SOC_DAPM_OUTPUT("IHFOUTR"),
  340. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  341. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  342. SND_SOC_DAPM_OUTPUT("VIB1OUT"),
  343. SND_SOC_DAPM_OUTPUT("VIB2OUT"),
  344. SND_SOC_DAPM_INPUT("AMIC1"), /* headset mic */
  345. SND_SOC_DAPM_INPUT("AMIC2"),
  346. SND_SOC_DAPM_INPUT("DMIC1"),
  347. SND_SOC_DAPM_INPUT("DMIC2"),
  348. SND_SOC_DAPM_INPUT("DMIC3"),
  349. SND_SOC_DAPM_INPUT("DMIC4"),
  350. SND_SOC_DAPM_INPUT("DMIC5"),
  351. SND_SOC_DAPM_INPUT("DMIC6"),
  352. SND_SOC_DAPM_INPUT("LINEINL"),
  353. SND_SOC_DAPM_INPUT("LINEINR"),
  354. SND_SOC_DAPM_MICBIAS("AMIC1Bias", SN95031_MICBIAS, 2, 0),
  355. SND_SOC_DAPM_MICBIAS("AMIC2Bias", SN95031_MICBIAS, 3, 0),
  356. SND_SOC_DAPM_MICBIAS("DMIC12Bias", SN95031_DMICMUX, 3, 0),
  357. SND_SOC_DAPM_MICBIAS("DMIC34Bias", SN95031_DMICMUX, 4, 0),
  358. SND_SOC_DAPM_MICBIAS("DMIC56Bias", SN95031_DMICMUX, 5, 0),
  359. SND_SOC_DAPM_SUPPLY("DMIC12supply", SN95031_DMICLK, 0, 0,
  360. sn95031_dmic12_event,
  361. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  362. SND_SOC_DAPM_SUPPLY("DMIC34supply", SN95031_DMICLK, 1, 0,
  363. sn95031_dmic34_event,
  364. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  365. SND_SOC_DAPM_SUPPLY("DMIC56supply", SN95031_DMICLK, 2, 0,
  366. sn95031_dmic56_event,
  367. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  368. SND_SOC_DAPM_AIF_OUT("PCM_Out", "Capture", 0,
  369. SND_SOC_NOPM, 0, 0),
  370. SND_SOC_DAPM_SUPPLY("Headset Rail", SND_SOC_NOPM, 0, 0,
  371. sn95031_vhs_event,
  372. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  373. SND_SOC_DAPM_SUPPLY("Speaker Rail", SND_SOC_NOPM, 0, 0,
  374. sn95031_vihf_event,
  375. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  376. /* playback path driver enables */
  377. SND_SOC_DAPM_PGA("Headset Left Playback",
  378. SN95031_DRIVEREN, 0, 0, NULL, 0),
  379. SND_SOC_DAPM_PGA("Headset Right Playback",
  380. SN95031_DRIVEREN, 1, 0, NULL, 0),
  381. SND_SOC_DAPM_PGA("Speaker Left Playback",
  382. SN95031_DRIVEREN, 2, 0, NULL, 0),
  383. SND_SOC_DAPM_PGA("Speaker Right Playback",
  384. SN95031_DRIVEREN, 3, 0, NULL, 0),
  385. SND_SOC_DAPM_PGA("Vibra1 Playback",
  386. SN95031_DRIVEREN, 4, 0, NULL, 0),
  387. SND_SOC_DAPM_PGA("Vibra2 Playback",
  388. SN95031_DRIVEREN, 5, 0, NULL, 0),
  389. SND_SOC_DAPM_PGA("Earpiece Playback",
  390. SN95031_DRIVEREN, 6, 0, NULL, 0),
  391. SND_SOC_DAPM_PGA("Lineout Left Playback",
  392. SN95031_LOCTL, 0, 0, NULL, 0),
  393. SND_SOC_DAPM_PGA("Lineout Right Playback",
  394. SN95031_LOCTL, 4, 0, NULL, 0),
  395. /* playback path filter enable */
  396. SND_SOC_DAPM_PGA("Headset Left Filter",
  397. SN95031_HSEPRXCTRL, 4, 0, NULL, 0),
  398. SND_SOC_DAPM_PGA("Headset Right Filter",
  399. SN95031_HSEPRXCTRL, 5, 0, NULL, 0),
  400. SND_SOC_DAPM_PGA("Speaker Left Filter",
  401. SN95031_IHFRXCTRL, 0, 0, NULL, 0),
  402. SND_SOC_DAPM_PGA("Speaker Right Filter",
  403. SN95031_IHFRXCTRL, 1, 0, NULL, 0),
  404. /* DACs */
  405. SND_SOC_DAPM_DAC("HSDAC Left", "Headset",
  406. SN95031_DACCONFIG, 0, 0),
  407. SND_SOC_DAPM_DAC("HSDAC Right", "Headset",
  408. SN95031_DACCONFIG, 1, 0),
  409. SND_SOC_DAPM_DAC("IHFDAC Left", "Speaker",
  410. SN95031_DACCONFIG, 2, 0),
  411. SND_SOC_DAPM_DAC("IHFDAC Right", "Speaker",
  412. SN95031_DACCONFIG, 3, 0),
  413. SND_SOC_DAPM_DAC("Vibra1 DAC", "Vibra1",
  414. SN95031_VIB1C5, 1, 0),
  415. SND_SOC_DAPM_DAC("Vibra2 DAC", "Vibra2",
  416. SN95031_VIB2C5, 1, 0),
  417. /* capture widgets */
  418. SND_SOC_DAPM_PGA("LineIn Enable Left", SN95031_MICAMP1,
  419. 7, 0, NULL, 0),
  420. SND_SOC_DAPM_PGA("LineIn Enable Right", SN95031_MICAMP2,
  421. 7, 0, NULL, 0),
  422. SND_SOC_DAPM_PGA("MIC1 Enable", SN95031_MICAMP1, 0, 0, NULL, 0),
  423. SND_SOC_DAPM_PGA("MIC2 Enable", SN95031_MICAMP2, 0, 0, NULL, 0),
  424. SND_SOC_DAPM_PGA("TX1 Enable", SN95031_AUDIOTXEN, 2, 0, NULL, 0),
  425. SND_SOC_DAPM_PGA("TX2 Enable", SN95031_AUDIOTXEN, 3, 0, NULL, 0),
  426. SND_SOC_DAPM_PGA("TX3 Enable", SN95031_AUDIOTXEN, 4, 0, NULL, 0),
  427. SND_SOC_DAPM_PGA("TX4 Enable", SN95031_AUDIOTXEN, 5, 0, NULL, 0),
  428. /* ADC have null stream as they will be turned ON by TX path */
  429. SND_SOC_DAPM_ADC("ADC Left", NULL,
  430. SN95031_ADCCONFIG, 0, 0),
  431. SND_SOC_DAPM_ADC("ADC Right", NULL,
  432. SN95031_ADCCONFIG, 2, 0),
  433. SND_SOC_DAPM_MUX("Mic_InputL Capture Route",
  434. SND_SOC_NOPM, 0, 0, &sn95031_micl_mux_control),
  435. SND_SOC_DAPM_MUX("Mic_InputR Capture Route",
  436. SND_SOC_NOPM, 0, 0, &sn95031_micr_mux_control),
  437. SND_SOC_DAPM_MUX("Txpath1 Capture Route",
  438. SND_SOC_NOPM, 0, 0, &sn95031_input1_mux_control),
  439. SND_SOC_DAPM_MUX("Txpath2 Capture Route",
  440. SND_SOC_NOPM, 0, 0, &sn95031_input2_mux_control),
  441. SND_SOC_DAPM_MUX("Txpath3 Capture Route",
  442. SND_SOC_NOPM, 0, 0, &sn95031_input3_mux_control),
  443. SND_SOC_DAPM_MUX("Txpath4 Capture Route",
  444. SND_SOC_NOPM, 0, 0, &sn95031_input4_mux_control),
  445. };
  446. static const struct snd_soc_dapm_route sn95031_audio_map[] = {
  447. /* headset and earpiece map */
  448. { "HPOUTL", NULL, "Headset Rail"},
  449. { "HPOUTR", NULL, "Headset Rail"},
  450. { "HPOUTL", NULL, "Headset Left Playback" },
  451. { "HPOUTR", NULL, "Headset Right Playback" },
  452. { "EPOUT", NULL, "Earpiece Playback" },
  453. { "Headset Left Playback", NULL, "Headset Left Filter"},
  454. { "Headset Right Playback", NULL, "Headset Right Filter"},
  455. { "Earpiece Playback", NULL, "Headset Left Filter"},
  456. { "Headset Left Filter", NULL, "HSDAC Left"},
  457. { "Headset Right Filter", NULL, "HSDAC Right"},
  458. /* speaker map */
  459. { "IHFOUTL", NULL, "Speaker Rail"},
  460. { "IHFOUTR", NULL, "Speaker Rail"},
  461. { "IHFOUTL", NULL, "Speaker Left Playback"},
  462. { "IHFOUTR", NULL, "Speaker Right Playback"},
  463. { "Speaker Left Playback", NULL, "Speaker Left Filter"},
  464. { "Speaker Right Playback", NULL, "Speaker Right Filter"},
  465. { "Speaker Left Filter", NULL, "IHFDAC Left"},
  466. { "Speaker Right Filter", NULL, "IHFDAC Right"},
  467. /* vibra map */
  468. { "VIB1OUT", NULL, "Vibra1 Playback"},
  469. { "Vibra1 Playback", NULL, "Vibra1 DAC"},
  470. { "VIB2OUT", NULL, "Vibra2 Playback"},
  471. { "Vibra2 Playback", NULL, "Vibra2 DAC"},
  472. /* lineout */
  473. { "LINEOUTL", NULL, "Lineout Left Playback"},
  474. { "LINEOUTR", NULL, "Lineout Right Playback"},
  475. { "Lineout Left Playback", NULL, "Headset Left Filter"},
  476. { "Lineout Left Playback", NULL, "Speaker Left Filter"},
  477. { "Lineout Left Playback", NULL, "Vibra1 DAC"},
  478. { "Lineout Right Playback", NULL, "Headset Right Filter"},
  479. { "Lineout Right Playback", NULL, "Speaker Right Filter"},
  480. { "Lineout Right Playback", NULL, "Vibra2 DAC"},
  481. /* Headset (AMIC1) mic */
  482. { "AMIC1Bias", NULL, "AMIC1"},
  483. { "MIC1 Enable", NULL, "AMIC1Bias"},
  484. { "Mic_InputL Capture Route", "AMIC", "MIC1 Enable"},
  485. /* AMIC2 */
  486. { "AMIC2Bias", NULL, "AMIC2"},
  487. { "MIC2 Enable", NULL, "AMIC2Bias"},
  488. { "Mic_InputR Capture Route", "AMIC", "MIC2 Enable"},
  489. /* Linein */
  490. { "LineIn Enable Left", NULL, "LINEINL"},
  491. { "LineIn Enable Right", NULL, "LINEINR"},
  492. { "Mic_InputL Capture Route", "LineIn", "LineIn Enable Left"},
  493. { "Mic_InputR Capture Route", "LineIn", "LineIn Enable Right"},
  494. /* ADC connection */
  495. { "ADC Left", NULL, "Mic_InputL Capture Route"},
  496. { "ADC Right", NULL, "Mic_InputR Capture Route"},
  497. /*DMIC connections */
  498. { "DMIC1", NULL, "DMIC12supply"},
  499. { "DMIC2", NULL, "DMIC12supply"},
  500. { "DMIC3", NULL, "DMIC34supply"},
  501. { "DMIC4", NULL, "DMIC34supply"},
  502. { "DMIC5", NULL, "DMIC56supply"},
  503. { "DMIC6", NULL, "DMIC56supply"},
  504. { "DMIC12Bias", NULL, "DMIC1"},
  505. { "DMIC12Bias", NULL, "DMIC2"},
  506. { "DMIC34Bias", NULL, "DMIC3"},
  507. { "DMIC34Bias", NULL, "DMIC4"},
  508. { "DMIC56Bias", NULL, "DMIC5"},
  509. { "DMIC56Bias", NULL, "DMIC6"},
  510. /*TX path inputs*/
  511. { "Txpath1 Capture Route", "ADC Left", "ADC Left"},
  512. { "Txpath2 Capture Route", "ADC Left", "ADC Left"},
  513. { "Txpath3 Capture Route", "ADC Left", "ADC Left"},
  514. { "Txpath4 Capture Route", "ADC Left", "ADC Left"},
  515. { "Txpath1 Capture Route", "ADC Right", "ADC Right"},
  516. { "Txpath2 Capture Route", "ADC Right", "ADC Right"},
  517. { "Txpath3 Capture Route", "ADC Right", "ADC Right"},
  518. { "Txpath4 Capture Route", "ADC Right", "ADC Right"},
  519. { "Txpath1 Capture Route", "DMIC1", "DMIC1"},
  520. { "Txpath2 Capture Route", "DMIC1", "DMIC1"},
  521. { "Txpath3 Capture Route", "DMIC1", "DMIC1"},
  522. { "Txpath4 Capture Route", "DMIC1", "DMIC1"},
  523. { "Txpath1 Capture Route", "DMIC2", "DMIC2"},
  524. { "Txpath2 Capture Route", "DMIC2", "DMIC2"},
  525. { "Txpath3 Capture Route", "DMIC2", "DMIC2"},
  526. { "Txpath4 Capture Route", "DMIC2", "DMIC2"},
  527. { "Txpath1 Capture Route", "DMIC3", "DMIC3"},
  528. { "Txpath2 Capture Route", "DMIC3", "DMIC3"},
  529. { "Txpath3 Capture Route", "DMIC3", "DMIC3"},
  530. { "Txpath4 Capture Route", "DMIC3", "DMIC3"},
  531. { "Txpath1 Capture Route", "DMIC4", "DMIC4"},
  532. { "Txpath2 Capture Route", "DMIC4", "DMIC4"},
  533. { "Txpath3 Capture Route", "DMIC4", "DMIC4"},
  534. { "Txpath4 Capture Route", "DMIC4", "DMIC4"},
  535. { "Txpath1 Capture Route", "DMIC5", "DMIC5"},
  536. { "Txpath2 Capture Route", "DMIC5", "DMIC5"},
  537. { "Txpath3 Capture Route", "DMIC5", "DMIC5"},
  538. { "Txpath4 Capture Route", "DMIC5", "DMIC5"},
  539. { "Txpath1 Capture Route", "DMIC6", "DMIC6"},
  540. { "Txpath2 Capture Route", "DMIC6", "DMIC6"},
  541. { "Txpath3 Capture Route", "DMIC6", "DMIC6"},
  542. { "Txpath4 Capture Route", "DMIC6", "DMIC6"},
  543. /* tx path */
  544. { "TX1 Enable", NULL, "Txpath1 Capture Route"},
  545. { "TX2 Enable", NULL, "Txpath2 Capture Route"},
  546. { "TX3 Enable", NULL, "Txpath3 Capture Route"},
  547. { "TX4 Enable", NULL, "Txpath4 Capture Route"},
  548. { "PCM_Out", NULL, "TX1 Enable"},
  549. { "PCM_Out", NULL, "TX2 Enable"},
  550. { "PCM_Out", NULL, "TX3 Enable"},
  551. { "PCM_Out", NULL, "TX4 Enable"},
  552. };
  553. /* speaker and headset mutes, for audio pops and clicks */
  554. static int sn95031_pcm_hs_mute(struct snd_soc_dai *dai, int mute)
  555. {
  556. snd_soc_update_bits(dai->codec,
  557. SN95031_HSLVOLCTRL, BIT(7), (!mute << 7));
  558. snd_soc_update_bits(dai->codec,
  559. SN95031_HSRVOLCTRL, BIT(7), (!mute << 7));
  560. return 0;
  561. }
  562. static int sn95031_pcm_spkr_mute(struct snd_soc_dai *dai, int mute)
  563. {
  564. snd_soc_update_bits(dai->codec,
  565. SN95031_IHFLVOLCTRL, BIT(7), (!mute << 7));
  566. snd_soc_update_bits(dai->codec,
  567. SN95031_IHFRVOLCTRL, BIT(7), (!mute << 7));
  568. return 0;
  569. }
  570. static int sn95031_pcm_hw_params(struct snd_pcm_substream *substream,
  571. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  572. {
  573. unsigned int format, rate;
  574. switch (params_width(params)) {
  575. case 16:
  576. format = BIT(4)|BIT(5);
  577. break;
  578. case 24:
  579. format = 0;
  580. break;
  581. default:
  582. return -EINVAL;
  583. }
  584. snd_soc_update_bits(dai->codec, SN95031_PCM2C2,
  585. BIT(4)|BIT(5), format);
  586. switch (params_rate(params)) {
  587. case 48000:
  588. pr_debug("RATE_48000\n");
  589. rate = 0;
  590. break;
  591. case 44100:
  592. pr_debug("RATE_44100\n");
  593. rate = BIT(7);
  594. break;
  595. default:
  596. pr_err("ERR rate %d\n", params_rate(params));
  597. return -EINVAL;
  598. }
  599. snd_soc_update_bits(dai->codec, SN95031_PCM1C1, BIT(7), rate);
  600. return 0;
  601. }
  602. /* Codec DAI section */
  603. static const struct snd_soc_dai_ops sn95031_headset_dai_ops = {
  604. .digital_mute = sn95031_pcm_hs_mute,
  605. .hw_params = sn95031_pcm_hw_params,
  606. };
  607. static const struct snd_soc_dai_ops sn95031_speaker_dai_ops = {
  608. .digital_mute = sn95031_pcm_spkr_mute,
  609. .hw_params = sn95031_pcm_hw_params,
  610. };
  611. static const struct snd_soc_dai_ops sn95031_vib1_dai_ops = {
  612. .hw_params = sn95031_pcm_hw_params,
  613. };
  614. static const struct snd_soc_dai_ops sn95031_vib2_dai_ops = {
  615. .hw_params = sn95031_pcm_hw_params,
  616. };
  617. static struct snd_soc_dai_driver sn95031_dais[] = {
  618. {
  619. .name = "SN95031 Headset",
  620. .playback = {
  621. .stream_name = "Headset",
  622. .channels_min = 2,
  623. .channels_max = 2,
  624. .rates = SN95031_RATES,
  625. .formats = SN95031_FORMATS,
  626. },
  627. .capture = {
  628. .stream_name = "Capture",
  629. .channels_min = 1,
  630. .channels_max = 5,
  631. .rates = SN95031_RATES,
  632. .formats = SN95031_FORMATS,
  633. },
  634. .ops = &sn95031_headset_dai_ops,
  635. },
  636. { .name = "SN95031 Speaker",
  637. .playback = {
  638. .stream_name = "Speaker",
  639. .channels_min = 2,
  640. .channels_max = 2,
  641. .rates = SN95031_RATES,
  642. .formats = SN95031_FORMATS,
  643. },
  644. .ops = &sn95031_speaker_dai_ops,
  645. },
  646. { .name = "SN95031 Vibra1",
  647. .playback = {
  648. .stream_name = "Vibra1",
  649. .channels_min = 1,
  650. .channels_max = 1,
  651. .rates = SN95031_RATES,
  652. .formats = SN95031_FORMATS,
  653. },
  654. .ops = &sn95031_vib1_dai_ops,
  655. },
  656. { .name = "SN95031 Vibra2",
  657. .playback = {
  658. .stream_name = "Vibra2",
  659. .channels_min = 1,
  660. .channels_max = 1,
  661. .rates = SN95031_RATES,
  662. .formats = SN95031_FORMATS,
  663. },
  664. .ops = &sn95031_vib2_dai_ops,
  665. },
  666. };
  667. static inline void sn95031_disable_jack_btn(struct snd_soc_codec *codec)
  668. {
  669. snd_soc_write(codec, SN95031_BTNCTRL2, 0x00);
  670. }
  671. static inline void sn95031_enable_jack_btn(struct snd_soc_codec *codec)
  672. {
  673. snd_soc_write(codec, SN95031_BTNCTRL1, 0x77);
  674. snd_soc_write(codec, SN95031_BTNCTRL2, 0x01);
  675. }
  676. static int sn95031_get_headset_state(struct snd_soc_codec *codec,
  677. struct snd_soc_jack *mfld_jack)
  678. {
  679. int micbias = sn95031_get_mic_bias(codec);
  680. int jack_type = snd_soc_jack_get_type(mfld_jack, micbias);
  681. pr_debug("jack type detected = %d\n", jack_type);
  682. if (jack_type == SND_JACK_HEADSET)
  683. sn95031_enable_jack_btn(codec);
  684. return jack_type;
  685. }
  686. void sn95031_jack_detection(struct snd_soc_codec *codec,
  687. struct mfld_jack_data *jack_data)
  688. {
  689. unsigned int status;
  690. unsigned int mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_HEADSET;
  691. pr_debug("interrupt id read in sram = 0x%x\n", jack_data->intr_id);
  692. if (jack_data->intr_id & 0x1) {
  693. pr_debug("short_push detected\n");
  694. status = SND_JACK_HEADSET | SND_JACK_BTN_0;
  695. } else if (jack_data->intr_id & 0x2) {
  696. pr_debug("long_push detected\n");
  697. status = SND_JACK_HEADSET | SND_JACK_BTN_1;
  698. } else if (jack_data->intr_id & 0x4) {
  699. pr_debug("headset or headphones inserted\n");
  700. status = sn95031_get_headset_state(codec, jack_data->mfld_jack);
  701. } else if (jack_data->intr_id & 0x8) {
  702. pr_debug("headset or headphones removed\n");
  703. status = 0;
  704. sn95031_disable_jack_btn(codec);
  705. } else {
  706. pr_err("unidentified interrupt\n");
  707. return;
  708. }
  709. snd_soc_jack_report(jack_data->mfld_jack, status, mask);
  710. /*button pressed and released so we send explicit button release */
  711. if ((status & SND_JACK_BTN_0) | (status & SND_JACK_BTN_1))
  712. snd_soc_jack_report(jack_data->mfld_jack,
  713. SND_JACK_HEADSET, mask);
  714. }
  715. EXPORT_SYMBOL_GPL(sn95031_jack_detection);
  716. /* codec registration */
  717. static int sn95031_codec_probe(struct snd_soc_codec *codec)
  718. {
  719. pr_debug("codec_probe called\n");
  720. /* PCM interface config
  721. * This sets the pcm rx slot conguration to max 6 slots
  722. * for max 4 dais (2 stereo and 2 mono)
  723. */
  724. snd_soc_write(codec, SN95031_PCM2RXSLOT01, 0x10);
  725. snd_soc_write(codec, SN95031_PCM2RXSLOT23, 0x32);
  726. snd_soc_write(codec, SN95031_PCM2RXSLOT45, 0x54);
  727. snd_soc_write(codec, SN95031_PCM2TXSLOT01, 0x10);
  728. snd_soc_write(codec, SN95031_PCM2TXSLOT23, 0x32);
  729. /* pcm port setting
  730. * This sets the pcm port to slave and clock at 19.2Mhz which
  731. * can support 6slots, sampling rate set per stream in hw-params
  732. */
  733. snd_soc_write(codec, SN95031_PCM1C1, 0x00);
  734. snd_soc_write(codec, SN95031_PCM2C1, 0x01);
  735. snd_soc_write(codec, SN95031_PCM2C2, 0x0A);
  736. snd_soc_write(codec, SN95031_HSMIXER, BIT(0)|BIT(4));
  737. /* vendor vibra workround, the vibras are muted by
  738. * custom register so unmute them
  739. */
  740. snd_soc_write(codec, SN95031_SSR5, 0x80);
  741. snd_soc_write(codec, SN95031_SSR6, 0x80);
  742. snd_soc_write(codec, SN95031_VIB1C5, 0x00);
  743. snd_soc_write(codec, SN95031_VIB2C5, 0x00);
  744. /* configure vibras for pcm port */
  745. snd_soc_write(codec, SN95031_VIB1C3, 0x00);
  746. snd_soc_write(codec, SN95031_VIB2C3, 0x00);
  747. /* soft mute ramp time */
  748. snd_soc_write(codec, SN95031_SOFTMUTE, 0x3);
  749. /* fix the initial volume at 1dB,
  750. * default in +9dB,
  751. * 1dB give optimal swing on DAC, amps
  752. */
  753. snd_soc_write(codec, SN95031_HSLVOLCTRL, 0x08);
  754. snd_soc_write(codec, SN95031_HSRVOLCTRL, 0x08);
  755. snd_soc_write(codec, SN95031_IHFLVOLCTRL, 0x08);
  756. snd_soc_write(codec, SN95031_IHFRVOLCTRL, 0x08);
  757. /* dac mode and lineout workaround */
  758. snd_soc_write(codec, SN95031_SSR2, 0x10);
  759. snd_soc_write(codec, SN95031_SSR3, 0x40);
  760. return 0;
  761. }
  762. static struct snd_soc_codec_driver sn95031_codec = {
  763. .probe = sn95031_codec_probe,
  764. .set_bias_level = sn95031_set_vaud_bias,
  765. .idle_bias_off = true,
  766. .component_driver = {
  767. .controls = sn95031_snd_controls,
  768. .num_controls = ARRAY_SIZE(sn95031_snd_controls),
  769. .dapm_widgets = sn95031_dapm_widgets,
  770. .num_dapm_widgets = ARRAY_SIZE(sn95031_dapm_widgets),
  771. .dapm_routes = sn95031_audio_map,
  772. .num_dapm_routes = ARRAY_SIZE(sn95031_audio_map),
  773. },
  774. };
  775. static int sn95031_device_probe(struct platform_device *pdev)
  776. {
  777. struct regmap *regmap;
  778. pr_debug("codec device probe called for %s\n", dev_name(&pdev->dev));
  779. regmap = devm_regmap_init(&pdev->dev, NULL, NULL, &sn95031_regmap);
  780. if (IS_ERR(regmap))
  781. return PTR_ERR(regmap);
  782. return snd_soc_register_codec(&pdev->dev, &sn95031_codec,
  783. sn95031_dais, ARRAY_SIZE(sn95031_dais));
  784. }
  785. static int sn95031_device_remove(struct platform_device *pdev)
  786. {
  787. pr_debug("codec device remove called\n");
  788. snd_soc_unregister_codec(&pdev->dev);
  789. return 0;
  790. }
  791. static struct platform_driver sn95031_codec_driver = {
  792. .driver = {
  793. .name = "sn95031",
  794. },
  795. .probe = sn95031_device_probe,
  796. .remove = sn95031_device_remove,
  797. };
  798. module_platform_driver(sn95031_codec_driver);
  799. MODULE_DESCRIPTION("ASoC TI SN95031 codec driver");
  800. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  801. MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
  802. MODULE_LICENSE("GPL v2");
  803. MODULE_ALIAS("platform:sn95031");