sn95031.c 29 KB

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