sn95031.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 "sn95031.h"
  36. #define SN95031_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100)
  37. #define SN95031_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  38. /*
  39. * todo:
  40. * capture paths
  41. * jack detection
  42. * PM functions
  43. */
  44. static inline unsigned int sn95031_read(struct snd_soc_codec *codec,
  45. unsigned int reg)
  46. {
  47. u8 value = 0;
  48. int ret;
  49. ret = intel_scu_ipc_ioread8(reg, &value);
  50. if (ret)
  51. pr_err("read of %x failed, err %d\n", reg, ret);
  52. return value;
  53. }
  54. static inline int sn95031_write(struct snd_soc_codec *codec,
  55. unsigned int reg, unsigned int value)
  56. {
  57. int ret;
  58. ret = intel_scu_ipc_iowrite8(reg, value);
  59. if (ret)
  60. pr_err("write of %x failed, err %d\n", reg, ret);
  61. return ret;
  62. }
  63. static int sn95031_set_vaud_bias(struct snd_soc_codec *codec,
  64. enum snd_soc_bias_level level)
  65. {
  66. switch (level) {
  67. case SND_SOC_BIAS_ON:
  68. break;
  69. case SND_SOC_BIAS_PREPARE:
  70. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
  71. pr_debug("vaud_bias powering up pll\n");
  72. /* power up the pll */
  73. snd_soc_write(codec, SN95031_AUDPLLCTRL, BIT(5));
  74. /* enable pcm 2 */
  75. snd_soc_update_bits(codec, SN95031_PCM2C2,
  76. BIT(0), BIT(0));
  77. }
  78. break;
  79. case SND_SOC_BIAS_STANDBY:
  80. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  81. pr_debug("vaud_bias power up rail\n");
  82. /* power up the rail */
  83. snd_soc_write(codec, SN95031_VAUD,
  84. BIT(2)|BIT(1)|BIT(0));
  85. msleep(1);
  86. } else if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
  87. /* turn off pcm */
  88. pr_debug("vaud_bias power dn pcm\n");
  89. snd_soc_update_bits(codec, SN95031_PCM2C2, BIT(0), 0);
  90. snd_soc_write(codec, SN95031_AUDPLLCTRL, 0);
  91. }
  92. break;
  93. case SND_SOC_BIAS_OFF:
  94. pr_debug("vaud_bias _OFF doing rail shutdown\n");
  95. snd_soc_write(codec, SN95031_VAUD, BIT(3));
  96. break;
  97. }
  98. codec->dapm.bias_level = level;
  99. return 0;
  100. }
  101. static int sn95031_vhs_event(struct snd_soc_dapm_widget *w,
  102. struct snd_kcontrol *kcontrol, int event)
  103. {
  104. if (SND_SOC_DAPM_EVENT_ON(event)) {
  105. pr_debug("VHS SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
  106. /* power up the rail */
  107. snd_soc_write(w->codec, SN95031_VHSP, 0x3D);
  108. snd_soc_write(w->codec, SN95031_VHSN, 0x3F);
  109. msleep(1);
  110. } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
  111. pr_debug("VHS SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
  112. snd_soc_write(w->codec, SN95031_VHSP, 0xC4);
  113. snd_soc_write(w->codec, SN95031_VHSN, 0x04);
  114. }
  115. return 0;
  116. }
  117. static int sn95031_vihf_event(struct snd_soc_dapm_widget *w,
  118. struct snd_kcontrol *kcontrol, int event)
  119. {
  120. if (SND_SOC_DAPM_EVENT_ON(event)) {
  121. pr_debug("VIHF SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
  122. /* power up the rail */
  123. snd_soc_write(w->codec, SN95031_VIHF, 0x27);
  124. msleep(1);
  125. } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
  126. pr_debug("VIHF SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
  127. snd_soc_write(w->codec, SN95031_VIHF, 0x24);
  128. }
  129. return 0;
  130. }
  131. /* DAPM widgets */
  132. static const struct snd_soc_dapm_widget sn95031_dapm_widgets[] = {
  133. /* all end points mic, hs etc */
  134. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  135. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  136. SND_SOC_DAPM_OUTPUT("EPOUT"),
  137. SND_SOC_DAPM_OUTPUT("IHFOUTL"),
  138. SND_SOC_DAPM_OUTPUT("IHFOUTR"),
  139. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  140. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  141. SND_SOC_DAPM_OUTPUT("VIB1OUT"),
  142. SND_SOC_DAPM_OUTPUT("VIB2OUT"),
  143. SND_SOC_DAPM_SUPPLY("Headset Rail", SND_SOC_NOPM, 0, 0,
  144. sn95031_vhs_event,
  145. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  146. SND_SOC_DAPM_SUPPLY("Speaker Rail", SND_SOC_NOPM, 0, 0,
  147. sn95031_vihf_event,
  148. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  149. /* playback path driver enables */
  150. SND_SOC_DAPM_PGA("Headset Left Playback",
  151. SN95031_DRIVEREN, 0, 0, NULL, 0),
  152. SND_SOC_DAPM_PGA("Headset Right Playback",
  153. SN95031_DRIVEREN, 1, 0, NULL, 0),
  154. SND_SOC_DAPM_PGA("Speaker Left Playback",
  155. SN95031_DRIVEREN, 2, 0, NULL, 0),
  156. SND_SOC_DAPM_PGA("Speaker Right Playback",
  157. SN95031_DRIVEREN, 3, 0, NULL, 0),
  158. SND_SOC_DAPM_PGA("Vibra1 Playback",
  159. SN95031_DRIVEREN, 4, 0, NULL, 0),
  160. SND_SOC_DAPM_PGA("Vibra2 Playback",
  161. SN95031_DRIVEREN, 5, 0, NULL, 0),
  162. SND_SOC_DAPM_PGA("Earpiece Playback",
  163. SN95031_DRIVEREN, 6, 0, NULL, 0),
  164. SND_SOC_DAPM_PGA("Lineout Left Playback",
  165. SN95031_LOCTL, 0, 0, NULL, 0),
  166. SND_SOC_DAPM_PGA("Lineout Right Playback",
  167. SN95031_LOCTL, 4, 0, NULL, 0),
  168. /* playback path filter enable */
  169. SND_SOC_DAPM_PGA("Headset Left Filter",
  170. SN95031_HSEPRXCTRL, 4, 0, NULL, 0),
  171. SND_SOC_DAPM_PGA("Headset Right Filter",
  172. SN95031_HSEPRXCTRL, 5, 0, NULL, 0),
  173. SND_SOC_DAPM_PGA("Speaker Left Filter",
  174. SN95031_IHFRXCTRL, 0, 0, NULL, 0),
  175. SND_SOC_DAPM_PGA("Speaker Right Filter",
  176. SN95031_IHFRXCTRL, 1, 0, NULL, 0),
  177. /* DACs */
  178. SND_SOC_DAPM_DAC("HSDAC Left", "Headset",
  179. SN95031_DACCONFIG, 0, 0),
  180. SND_SOC_DAPM_DAC("HSDAC Right", "Headset",
  181. SN95031_DACCONFIG, 1, 0),
  182. SND_SOC_DAPM_DAC("IHFDAC Left", "Speaker",
  183. SN95031_DACCONFIG, 2, 0),
  184. SND_SOC_DAPM_DAC("IHFDAC Right", "Speaker",
  185. SN95031_DACCONFIG, 3, 0),
  186. SND_SOC_DAPM_DAC("Vibra1 DAC", "Vibra1",
  187. SN95031_VIB1C5, 1, 0),
  188. SND_SOC_DAPM_DAC("Vibra2 DAC", "Vibra2",
  189. SN95031_VIB2C5, 1, 0),
  190. };
  191. static const struct snd_soc_dapm_route sn95031_audio_map[] = {
  192. /* headset and earpiece map */
  193. { "HPOUTL", NULL, "Headset Left Playback" },
  194. { "HPOUTR", NULL, "Headset Right Playback" },
  195. { "EPOUT", NULL, "Earpiece Playback" },
  196. { "Headset Left Playback", NULL, "Headset Left Filter"},
  197. { "Headset Right Playback", NULL, "Headset Right Filter"},
  198. { "Earpiece Playback", NULL, "Headset Left Filter"},
  199. { "Headset Left Filter", NULL, "HSDAC Left"},
  200. { "Headset Right Filter", NULL, "HSDAC Right"},
  201. { "HSDAC Left", NULL, "Headset Rail"},
  202. { "HSDAC Right", NULL, "Headset Rail"},
  203. /* speaker map */
  204. { "IHFOUTL", "NULL", "Speaker Left Playback"},
  205. { "IHFOUTR", "NULL", "Speaker Right Playback"},
  206. { "Speaker Left Playback", NULL, "Speaker Left Filter"},
  207. { "Speaker Right Playback", NULL, "Speaker Right Filter"},
  208. { "Speaker Left Filter", NULL, "IHFDAC Left"},
  209. { "Speaker Right Filter", NULL, "IHFDAC Right"},
  210. { "IHFDAC Left", NULL, "Speaker Rail"},
  211. { "IHFDAC Right", NULL, "Speaker Rail"},
  212. /* vibra map */
  213. { "VIB1OUT", NULL, "Vibra1 Playback"},
  214. { "Vibra1 Playback", NULL, "Vibra1 DAC"},
  215. { "VIB2OUT", NULL, "Vibra2 Playback"},
  216. { "Vibra2 Playback", NULL, "Vibra2 DAC"},
  217. /* lineout */
  218. { "LINEOUTL", NULL, "Lineout Left Playback"},
  219. { "LINEOUTR", NULL, "Lineout Right Playback"},
  220. { "Lineout Left Playback", NULL, "Headset Left Filter"},
  221. { "Lineout Left Playback", NULL, "Speaker Left Filter"},
  222. { "Lineout Left Playback", NULL, "Vibra1 DAC"},
  223. { "Lineout Right Playback", NULL, "Headset Right Filter"},
  224. { "Lineout Right Playback", NULL, "Speaker Right Filter"},
  225. { "Lineout Right Playback", NULL, "Vibra2 DAC"},
  226. };
  227. /* speaker and headset mutes, for audio pops and clicks */
  228. static int sn95031_pcm_hs_mute(struct snd_soc_dai *dai, int mute)
  229. {
  230. snd_soc_update_bits(dai->codec,
  231. SN95031_HSLVOLCTRL, BIT(7), (!mute << 7));
  232. snd_soc_update_bits(dai->codec,
  233. SN95031_HSRVOLCTRL, BIT(7), (!mute << 7));
  234. return 0;
  235. }
  236. static int sn95031_pcm_spkr_mute(struct snd_soc_dai *dai, int mute)
  237. {
  238. snd_soc_update_bits(dai->codec,
  239. SN95031_IHFLVOLCTRL, BIT(7), (!mute << 7));
  240. snd_soc_update_bits(dai->codec,
  241. SN95031_IHFRVOLCTRL, BIT(7), (!mute << 7));
  242. return 0;
  243. }
  244. int sn95031_pcm_hw_params(struct snd_pcm_substream *substream,
  245. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  246. {
  247. unsigned int format, rate;
  248. switch (params_format(params)) {
  249. case SNDRV_PCM_FORMAT_S16_LE:
  250. format = BIT(4)|BIT(5);
  251. break;
  252. case SNDRV_PCM_FORMAT_S24_LE:
  253. format = 0;
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. snd_soc_update_bits(dai->codec, SN95031_PCM2C2,
  259. BIT(4)|BIT(5), format);
  260. switch (params_rate(params)) {
  261. case 48000:
  262. pr_debug("RATE_48000\n");
  263. rate = 0;
  264. break;
  265. case 44100:
  266. pr_debug("RATE_44100\n");
  267. rate = BIT(7);
  268. break;
  269. default:
  270. pr_err("ERR rate %d\n", params_rate(params));
  271. return -EINVAL;
  272. }
  273. snd_soc_update_bits(dai->codec, SN95031_PCM1C1, BIT(7), rate);
  274. return 0;
  275. }
  276. /* Codec DAI section */
  277. static struct snd_soc_dai_ops sn95031_headset_dai_ops = {
  278. .digital_mute = sn95031_pcm_hs_mute,
  279. .hw_params = sn95031_pcm_hw_params,
  280. };
  281. static struct snd_soc_dai_ops sn95031_speaker_dai_ops = {
  282. .digital_mute = sn95031_pcm_spkr_mute,
  283. .hw_params = sn95031_pcm_hw_params,
  284. };
  285. static struct snd_soc_dai_ops sn95031_vib1_dai_ops = {
  286. .hw_params = sn95031_pcm_hw_params,
  287. };
  288. static struct snd_soc_dai_ops sn95031_vib2_dai_ops = {
  289. .hw_params = sn95031_pcm_hw_params,
  290. };
  291. struct snd_soc_dai_driver sn95031_dais[] = {
  292. {
  293. .name = "SN95031 Headset",
  294. .playback = {
  295. .stream_name = "Headset",
  296. .channels_min = 2,
  297. .channels_max = 2,
  298. .rates = SN95031_RATES,
  299. .formats = SN95031_FORMATS,
  300. },
  301. .ops = &sn95031_headset_dai_ops,
  302. },
  303. { .name = "SN95031 Speaker",
  304. .playback = {
  305. .stream_name = "Speaker",
  306. .channels_min = 2,
  307. .channels_max = 2,
  308. .rates = SN95031_RATES,
  309. .formats = SN95031_FORMATS,
  310. },
  311. .ops = &sn95031_speaker_dai_ops,
  312. },
  313. { .name = "SN95031 Vibra1",
  314. .playback = {
  315. .stream_name = "Vibra1",
  316. .channels_min = 1,
  317. .channels_max = 1,
  318. .rates = SN95031_RATES,
  319. .formats = SN95031_FORMATS,
  320. },
  321. .ops = &sn95031_vib1_dai_ops,
  322. },
  323. { .name = "SN95031 Vibra2",
  324. .playback = {
  325. .stream_name = "Vibra2",
  326. .channels_min = 1,
  327. .channels_max = 1,
  328. .rates = SN95031_RATES,
  329. .formats = SN95031_FORMATS,
  330. },
  331. .ops = &sn95031_vib2_dai_ops,
  332. },
  333. };
  334. /* codec registration */
  335. static int sn95031_codec_probe(struct snd_soc_codec *codec)
  336. {
  337. int ret;
  338. pr_debug("codec_probe called\n");
  339. codec->dapm.bias_level = SND_SOC_BIAS_OFF;
  340. codec->dapm.idle_bias_off = 1;
  341. /* PCM interface config
  342. * This sets the pcm rx slot conguration to max 6 slots
  343. * for max 4 dais (2 stereo and 2 mono)
  344. */
  345. snd_soc_write(codec, SN95031_PCM2RXSLOT01, 0x10);
  346. snd_soc_write(codec, SN95031_PCM2RXSLOT23, 0x32);
  347. snd_soc_write(codec, SN95031_PCM2RXSLOT45, 0x54);
  348. /* pcm port setting
  349. * This sets the pcm port to slave and clock at 19.2Mhz which
  350. * can support 6slots, sampling rate set per stream in hw-params
  351. */
  352. snd_soc_write(codec, SN95031_PCM1C1, 0x00);
  353. snd_soc_write(codec, SN95031_PCM2C1, 0x01);
  354. snd_soc_write(codec, SN95031_PCM2C2, 0x0A);
  355. snd_soc_write(codec, SN95031_HSMIXER, BIT(0)|BIT(4));
  356. /* vendor vibra workround, the vibras are muted by
  357. * custom register so unmute them
  358. */
  359. snd_soc_write(codec, SN95031_SSR5, 0x80);
  360. snd_soc_write(codec, SN95031_SSR6, 0x80);
  361. snd_soc_write(codec, SN95031_VIB1C5, 0x00);
  362. snd_soc_write(codec, SN95031_VIB2C5, 0x00);
  363. /* configure vibras for pcm port */
  364. snd_soc_write(codec, SN95031_VIB1C3, 0x00);
  365. snd_soc_write(codec, SN95031_VIB2C3, 0x00);
  366. /* soft mute ramp time */
  367. snd_soc_write(codec, SN95031_SOFTMUTE, 0x3);
  368. /* fix the initial volume at 1dB,
  369. * default in +9dB,
  370. * 1dB give optimal swing on DAC, amps
  371. */
  372. snd_soc_write(codec, SN95031_HSLVOLCTRL, 0x08);
  373. snd_soc_write(codec, SN95031_HSRVOLCTRL, 0x08);
  374. snd_soc_write(codec, SN95031_IHFLVOLCTRL, 0x08);
  375. snd_soc_write(codec, SN95031_IHFRVOLCTRL, 0x08);
  376. /* dac mode and lineout workaround */
  377. snd_soc_write(codec, SN95031_SSR2, 0x10);
  378. snd_soc_write(codec, SN95031_SSR3, 0x40);
  379. ret = snd_soc_dapm_new_controls(&codec->dapm, sn95031_dapm_widgets,
  380. ARRAY_SIZE(sn95031_dapm_widgets));
  381. if (ret)
  382. pr_err("soc_dapm_new_control failed %d", ret);
  383. ret = snd_soc_dapm_add_routes(&codec->dapm, sn95031_audio_map,
  384. ARRAY_SIZE(sn95031_audio_map));
  385. if (ret)
  386. pr_err("soc_dapm_add_routes failed %d", ret);
  387. return ret;
  388. }
  389. static int sn95031_codec_remove(struct snd_soc_codec *codec)
  390. {
  391. pr_debug("codec_remove called\n");
  392. sn95031_set_vaud_bias(codec, SND_SOC_BIAS_OFF);
  393. return 0;
  394. }
  395. struct snd_soc_codec_driver sn95031_codec = {
  396. .probe = sn95031_codec_probe,
  397. .remove = sn95031_codec_remove,
  398. .read = sn95031_read,
  399. .write = sn95031_write,
  400. .set_bias_level = sn95031_set_vaud_bias,
  401. };
  402. static int __devinit sn95031_device_probe(struct platform_device *pdev)
  403. {
  404. pr_debug("codec device probe called for %s\n", dev_name(&pdev->dev));
  405. return snd_soc_register_codec(&pdev->dev, &sn95031_codec,
  406. sn95031_dais, ARRAY_SIZE(sn95031_dais));
  407. }
  408. static int __devexit sn95031_device_remove(struct platform_device *pdev)
  409. {
  410. pr_debug("codec device remove called\n");
  411. snd_soc_unregister_codec(&pdev->dev);
  412. return 0;
  413. }
  414. static struct platform_driver sn95031_codec_driver = {
  415. .driver = {
  416. .name = "sn95031",
  417. .owner = THIS_MODULE,
  418. },
  419. .probe = sn95031_device_probe,
  420. .remove = sn95031_device_remove,
  421. };
  422. static int __init sn95031_init(void)
  423. {
  424. pr_debug("driver init called\n");
  425. return platform_driver_register(&sn95031_codec_driver);
  426. }
  427. module_init(sn95031_init);
  428. static void __exit sn95031_exit(void)
  429. {
  430. pr_debug("driver exit called\n");
  431. platform_driver_unregister(&sn95031_codec_driver);
  432. }
  433. module_exit(sn95031_exit);
  434. MODULE_DESCRIPTION("ASoC TI SN95031 codec driver");
  435. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  436. MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
  437. MODULE_LICENSE("GPL v2");
  438. MODULE_ALIAS("platform:sn95031");