lm4857.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * LM4857 AMP driver
  3. *
  4. * Copyright 2007 Wolfson Microelectronics PLC.
  5. * Author: Graeme Gregory
  6. * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
  7. * Copyright 2011 Lars-Peter Clausen <lars@metafoo.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <sound/core.h>
  20. #include <sound/soc.h>
  21. #include <sound/tlv.h>
  22. struct lm4857 {
  23. struct i2c_client *i2c;
  24. uint8_t mode;
  25. };
  26. static const uint8_t lm4857_default_regs[] = {
  27. 0x00, 0x00, 0x00, 0x00,
  28. };
  29. /* The register offsets in the cache array */
  30. #define LM4857_MVOL 0
  31. #define LM4857_LVOL 1
  32. #define LM4857_RVOL 2
  33. #define LM4857_CTRL 3
  34. /* the shifts required to set these bits */
  35. #define LM4857_3D 5
  36. #define LM4857_WAKEUP 5
  37. #define LM4857_EPGAIN 4
  38. static int lm4857_write(struct snd_soc_codec *codec, unsigned int reg,
  39. unsigned int value)
  40. {
  41. uint8_t data;
  42. int ret;
  43. ret = snd_soc_cache_write(codec, reg, value);
  44. if (ret < 0)
  45. return ret;
  46. data = (reg << 6) | value;
  47. ret = i2c_master_send(codec->control_data, &data, 1);
  48. if (ret != 1) {
  49. dev_err(codec->dev, "Failed to write register: %d\n", ret);
  50. return ret;
  51. }
  52. return 0;
  53. }
  54. static unsigned int lm4857_read(struct snd_soc_codec *codec,
  55. unsigned int reg)
  56. {
  57. unsigned int val;
  58. int ret;
  59. ret = snd_soc_cache_read(codec, reg, &val);
  60. if (ret)
  61. return -1;
  62. return val;
  63. }
  64. static int lm4857_get_mode(struct snd_kcontrol *kcontrol,
  65. struct snd_ctl_elem_value *ucontrol)
  66. {
  67. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  68. struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
  69. ucontrol->value.integer.value[0] = lm4857->mode;
  70. return 0;
  71. }
  72. static int lm4857_set_mode(struct snd_kcontrol *kcontrol,
  73. struct snd_ctl_elem_value *ucontrol)
  74. {
  75. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  76. struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
  77. uint8_t value = ucontrol->value.integer.value[0];
  78. lm4857->mode = value;
  79. if (codec->dapm.bias_level == SND_SOC_BIAS_ON)
  80. snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, value + 6);
  81. return 1;
  82. }
  83. static int lm4857_set_bias_level(struct snd_soc_codec *codec,
  84. enum snd_soc_bias_level level)
  85. {
  86. struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
  87. switch (level) {
  88. case SND_SOC_BIAS_ON:
  89. snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, lm4857->mode + 6);
  90. break;
  91. case SND_SOC_BIAS_STANDBY:
  92. snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, 0);
  93. break;
  94. default:
  95. break;
  96. }
  97. codec->dapm.bias_level = level;
  98. return 0;
  99. }
  100. static const char *lm4857_mode[] = {
  101. "Earpiece",
  102. "Loudspeaker",
  103. "Loudspeaker + Headphone",
  104. "Headphone",
  105. };
  106. static const struct soc_enum lm4857_mode_enum =
  107. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode), lm4857_mode);
  108. static const struct snd_soc_dapm_widget lm4857_dapm_widgets[] = {
  109. SND_SOC_DAPM_INPUT("IN"),
  110. SND_SOC_DAPM_OUTPUT("LS"),
  111. SND_SOC_DAPM_OUTPUT("HP"),
  112. SND_SOC_DAPM_OUTPUT("EP"),
  113. };
  114. static const DECLARE_TLV_DB_SCALE(stereo_tlv, -4050, 150, 0);
  115. static const DECLARE_TLV_DB_SCALE(mono_tlv, -3450, 150, 0);
  116. static const struct snd_kcontrol_new lm4857_controls[] = {
  117. SOC_SINGLE_TLV("Left Playback Volume", LM4857_LVOL, 0, 31, 0,
  118. stereo_tlv),
  119. SOC_SINGLE_TLV("Right Playback Volume", LM4857_RVOL, 0, 31, 0,
  120. stereo_tlv),
  121. SOC_SINGLE_TLV("Mono Playback Volume", LM4857_MVOL, 0, 31, 0,
  122. mono_tlv),
  123. SOC_SINGLE("Spk 3D Playback Switch", LM4857_LVOL, LM4857_3D, 1, 0),
  124. SOC_SINGLE("HP 3D Playback Switch", LM4857_RVOL, LM4857_3D, 1, 0),
  125. SOC_SINGLE("Fast Wakeup Playback Switch", LM4857_CTRL,
  126. LM4857_WAKEUP, 1, 0),
  127. SOC_SINGLE("Earpiece 6dB Playback Switch", LM4857_CTRL,
  128. LM4857_EPGAIN, 1, 0),
  129. SOC_ENUM_EXT("Mode", lm4857_mode_enum,
  130. lm4857_get_mode, lm4857_set_mode),
  131. };
  132. /* There is a demux between the input signal and the output signals.
  133. * Currently there is no easy way to model it in ASoC and since it does not make
  134. * much of a difference in practice simply connect the input direclty to the
  135. * outputs. */
  136. static const struct snd_soc_dapm_route lm4857_routes[] = {
  137. {"LS", NULL, "IN"},
  138. {"HP", NULL, "IN"},
  139. {"EP", NULL, "IN"},
  140. };
  141. static int lm4857_probe(struct snd_soc_codec *codec)
  142. {
  143. struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec);
  144. struct snd_soc_dapm_context *dapm = &codec->dapm;
  145. int ret;
  146. codec->control_data = lm4857->i2c;
  147. ret = snd_soc_add_controls(codec, lm4857_controls,
  148. ARRAY_SIZE(lm4857_controls));
  149. if (ret)
  150. return ret;
  151. ret = snd_soc_dapm_new_controls(dapm, lm4857_dapm_widgets,
  152. ARRAY_SIZE(lm4857_dapm_widgets));
  153. if (ret)
  154. return ret;
  155. ret = snd_soc_dapm_add_routes(dapm, lm4857_routes,
  156. ARRAY_SIZE(lm4857_routes));
  157. if (ret)
  158. return ret;
  159. snd_soc_dapm_new_widgets(dapm);
  160. return 0;
  161. }
  162. static struct snd_soc_codec_driver soc_codec_dev_lm4857 = {
  163. .write = lm4857_write,
  164. .read = lm4857_read,
  165. .probe = lm4857_probe,
  166. .reg_cache_size = ARRAY_SIZE(lm4857_default_regs),
  167. .reg_word_size = sizeof(uint8_t),
  168. .reg_cache_default = lm4857_default_regs,
  169. .set_bias_level = lm4857_set_bias_level,
  170. };
  171. static int __devinit lm4857_i2c_probe(struct i2c_client *i2c,
  172. const struct i2c_device_id *id)
  173. {
  174. struct lm4857 *lm4857;
  175. int ret;
  176. lm4857 = kzalloc(sizeof(*lm4857), GFP_KERNEL);
  177. if (!lm4857)
  178. return -ENOMEM;
  179. i2c_set_clientdata(i2c, lm4857);
  180. lm4857->i2c = i2c;
  181. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_lm4857, NULL, 0);
  182. if (ret) {
  183. kfree(lm4857);
  184. return ret;
  185. }
  186. return 0;
  187. }
  188. static int __devexit lm4857_i2c_remove(struct i2c_client *i2c)
  189. {
  190. struct lm4857 *lm4857 = i2c_get_clientdata(i2c);
  191. snd_soc_unregister_codec(&i2c->dev);
  192. kfree(lm4857);
  193. return 0;
  194. }
  195. static const struct i2c_device_id lm4857_i2c_id[] = {
  196. { "lm4857", 0 },
  197. { }
  198. };
  199. MODULE_DEVICE_TABLE(i2c, lm4857_i2c_id);
  200. static struct i2c_driver lm4857_i2c_driver = {
  201. .driver = {
  202. .name = "lm4857",
  203. .owner = THIS_MODULE,
  204. },
  205. .probe = lm4857_i2c_probe,
  206. .remove = __devexit_p(lm4857_i2c_remove),
  207. .id_table = lm4857_i2c_id,
  208. };
  209. static int __init lm4857_init(void)
  210. {
  211. return i2c_add_driver(&lm4857_i2c_driver);
  212. }
  213. module_init(lm4857_init);
  214. static void __exit lm4857_exit(void)
  215. {
  216. i2c_del_driver(&lm4857_i2c_driver);
  217. }
  218. module_exit(lm4857_exit);
  219. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  220. MODULE_DESCRIPTION("LM4857 amplifier driver");
  221. MODULE_LICENSE("GPL");