max9877.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * max9877.c -- amp driver for max9877
  3. *
  4. * Copyright (C) 2009 Samsung Electronics Co.Ltd
  5. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/i2c.h>
  16. #include <sound/soc.h>
  17. #include <sound/tlv.h>
  18. #include "max9877.h"
  19. static struct i2c_client *i2c;
  20. static u8 max9877_regs[5] = { 0x40, 0x00, 0x00, 0x00, 0x49 };
  21. static void max9877_write_regs(void)
  22. {
  23. unsigned int i;
  24. u8 data[6];
  25. data[0] = MAX9877_INPUT_MODE;
  26. for (i = 0; i < ARRAY_SIZE(max9877_regs); i++)
  27. data[i + 1] = max9877_regs[i];
  28. if (i2c_master_send(i2c, data, 6) != 6)
  29. dev_err(&i2c->dev, "i2c write failed\n");
  30. }
  31. static int max9877_get_reg(struct snd_kcontrol *kcontrol,
  32. struct snd_ctl_elem_value *ucontrol)
  33. {
  34. struct soc_mixer_control *mc =
  35. (struct soc_mixer_control *)kcontrol->private_value;
  36. unsigned int reg = mc->reg;
  37. unsigned int shift = mc->shift;
  38. unsigned int mask = mc->max;
  39. unsigned int invert = mc->invert;
  40. ucontrol->value.integer.value[0] = (max9877_regs[reg] >> shift) & mask;
  41. if (invert)
  42. ucontrol->value.integer.value[0] =
  43. mask - ucontrol->value.integer.value[0];
  44. return 0;
  45. }
  46. static int max9877_set_reg(struct snd_kcontrol *kcontrol,
  47. struct snd_ctl_elem_value *ucontrol)
  48. {
  49. struct soc_mixer_control *mc =
  50. (struct soc_mixer_control *)kcontrol->private_value;
  51. unsigned int reg = mc->reg;
  52. unsigned int shift = mc->shift;
  53. unsigned int mask = mc->max;
  54. unsigned int invert = mc->invert;
  55. unsigned int val = (ucontrol->value.integer.value[0] & mask);
  56. if (invert)
  57. val = mask - val;
  58. if (((max9877_regs[reg] >> shift) & mask) == val)
  59. return 0;
  60. max9877_regs[reg] &= ~(mask << shift);
  61. max9877_regs[reg] |= val << shift;
  62. max9877_write_regs();
  63. return 1;
  64. }
  65. static int max9877_get_2reg(struct snd_kcontrol *kcontrol,
  66. struct snd_ctl_elem_value *ucontrol)
  67. {
  68. struct soc_mixer_control *mc =
  69. (struct soc_mixer_control *)kcontrol->private_value;
  70. unsigned int reg = mc->reg;
  71. unsigned int reg2 = mc->rreg;
  72. unsigned int shift = mc->shift;
  73. unsigned int mask = mc->max;
  74. ucontrol->value.integer.value[0] = (max9877_regs[reg] >> shift) & mask;
  75. ucontrol->value.integer.value[1] = (max9877_regs[reg2] >> shift) & mask;
  76. return 0;
  77. }
  78. static int max9877_set_2reg(struct snd_kcontrol *kcontrol,
  79. struct snd_ctl_elem_value *ucontrol)
  80. {
  81. struct soc_mixer_control *mc =
  82. (struct soc_mixer_control *)kcontrol->private_value;
  83. unsigned int reg = mc->reg;
  84. unsigned int reg2 = mc->rreg;
  85. unsigned int shift = mc->shift;
  86. unsigned int mask = mc->max;
  87. unsigned int val = (ucontrol->value.integer.value[0] & mask);
  88. unsigned int val2 = (ucontrol->value.integer.value[1] & mask);
  89. unsigned int change = 1;
  90. if (((max9877_regs[reg] >> shift) & mask) == val)
  91. change = 0;
  92. if (((max9877_regs[reg2] >> shift) & mask) == val2)
  93. change = 0;
  94. if (change) {
  95. max9877_regs[reg] &= ~(mask << shift);
  96. max9877_regs[reg] |= val << shift;
  97. max9877_regs[reg2] &= ~(mask << shift);
  98. max9877_regs[reg2] |= val2 << shift;
  99. max9877_write_regs();
  100. }
  101. return change;
  102. }
  103. static int max9877_get_out_mode(struct snd_kcontrol *kcontrol,
  104. struct snd_ctl_elem_value *ucontrol)
  105. {
  106. u8 value = max9877_regs[MAX9877_OUTPUT_MODE] & MAX9877_OUTMODE_MASK;
  107. if (value)
  108. value -= 1;
  109. ucontrol->value.integer.value[0] = value;
  110. return 0;
  111. }
  112. static int max9877_set_out_mode(struct snd_kcontrol *kcontrol,
  113. struct snd_ctl_elem_value *ucontrol)
  114. {
  115. u8 value = ucontrol->value.integer.value[0];
  116. value += 1;
  117. if ((max9877_regs[MAX9877_OUTPUT_MODE] & MAX9877_OUTMODE_MASK) == value)
  118. return 0;
  119. max9877_regs[MAX9877_OUTPUT_MODE] &= ~MAX9877_OUTMODE_MASK;
  120. max9877_regs[MAX9877_OUTPUT_MODE] |= value;
  121. max9877_write_regs();
  122. return 1;
  123. }
  124. static int max9877_get_osc_mode(struct snd_kcontrol *kcontrol,
  125. struct snd_ctl_elem_value *ucontrol)
  126. {
  127. u8 value = (max9877_regs[MAX9877_OUTPUT_MODE] & MAX9877_OSC_MASK);
  128. value = value >> MAX9877_OSC_OFFSET;
  129. ucontrol->value.integer.value[0] = value;
  130. return 0;
  131. }
  132. static int max9877_set_osc_mode(struct snd_kcontrol *kcontrol,
  133. struct snd_ctl_elem_value *ucontrol)
  134. {
  135. u8 value = ucontrol->value.integer.value[0];
  136. value = value << MAX9877_OSC_OFFSET;
  137. if ((max9877_regs[MAX9877_OUTPUT_MODE] & MAX9877_OSC_MASK) == value)
  138. return 0;
  139. max9877_regs[MAX9877_OUTPUT_MODE] &= ~MAX9877_OSC_MASK;
  140. max9877_regs[MAX9877_OUTPUT_MODE] |= value;
  141. max9877_write_regs();
  142. return 1;
  143. }
  144. static const unsigned int max9877_pgain_tlv[] = {
  145. TLV_DB_RANGE_HEAD(2),
  146. 0, 1, TLV_DB_SCALE_ITEM(0, 900, 0),
  147. 2, 2, TLV_DB_SCALE_ITEM(2000, 0, 0),
  148. };
  149. static const unsigned int max9877_output_tlv[] = {
  150. TLV_DB_RANGE_HEAD(4),
  151. 0, 7, TLV_DB_SCALE_ITEM(-7900, 400, 1),
  152. 8, 15, TLV_DB_SCALE_ITEM(-4700, 300, 0),
  153. 16, 23, TLV_DB_SCALE_ITEM(-2300, 200, 0),
  154. 24, 31, TLV_DB_SCALE_ITEM(-700, 100, 0),
  155. };
  156. static const char *max9877_out_mode[] = {
  157. "INA -> SPK",
  158. "INA -> HP",
  159. "INA -> SPK and HP",
  160. "INB -> SPK",
  161. "INB -> HP",
  162. "INB -> SPK and HP",
  163. "INA + INB -> SPK",
  164. "INA + INB -> HP",
  165. "INA + INB -> SPK and HP",
  166. };
  167. static const char *max9877_osc_mode[] = {
  168. "1176KHz",
  169. "1100KHz",
  170. "700KHz",
  171. };
  172. static const struct soc_enum max9877_enum[] = {
  173. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(max9877_out_mode), max9877_out_mode),
  174. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(max9877_osc_mode), max9877_osc_mode),
  175. };
  176. static const struct snd_kcontrol_new max9877_controls[] = {
  177. SOC_SINGLE_EXT_TLV("MAX9877 PGAINA Playback Volume",
  178. MAX9877_INPUT_MODE, 0, 2, 0,
  179. max9877_get_reg, max9877_set_reg, max9877_pgain_tlv),
  180. SOC_SINGLE_EXT_TLV("MAX9877 PGAINB Playback Volume",
  181. MAX9877_INPUT_MODE, 2, 2, 0,
  182. max9877_get_reg, max9877_set_reg, max9877_pgain_tlv),
  183. SOC_SINGLE_EXT_TLV("MAX9877 Amp Speaker Playback Volume",
  184. MAX9877_SPK_VOLUME, 0, 31, 0,
  185. max9877_get_reg, max9877_set_reg, max9877_output_tlv),
  186. SOC_DOUBLE_R_EXT_TLV("MAX9877 Amp HP Playback Volume",
  187. MAX9877_HPL_VOLUME, MAX9877_HPR_VOLUME, 0, 31, 0,
  188. max9877_get_2reg, max9877_set_2reg, max9877_output_tlv),
  189. SOC_SINGLE_EXT("MAX9877 INB Stereo Switch",
  190. MAX9877_INPUT_MODE, 4, 1, 1,
  191. max9877_get_reg, max9877_set_reg),
  192. SOC_SINGLE_EXT("MAX9877 INA Stereo Switch",
  193. MAX9877_INPUT_MODE, 5, 1, 1,
  194. max9877_get_reg, max9877_set_reg),
  195. SOC_SINGLE_EXT("MAX9877 Zero-crossing detection Switch",
  196. MAX9877_INPUT_MODE, 6, 1, 0,
  197. max9877_get_reg, max9877_set_reg),
  198. SOC_SINGLE_EXT("MAX9877 Bypass Mode Switch",
  199. MAX9877_OUTPUT_MODE, 6, 1, 0,
  200. max9877_get_reg, max9877_set_reg),
  201. SOC_SINGLE_EXT("MAX9877 Shutdown Mode Switch",
  202. MAX9877_OUTPUT_MODE, 7, 1, 1,
  203. max9877_get_reg, max9877_set_reg),
  204. SOC_ENUM_EXT("MAX9877 Output Mode", max9877_enum[0],
  205. max9877_get_out_mode, max9877_set_out_mode),
  206. SOC_ENUM_EXT("MAX9877 Oscillator Mode", max9877_enum[1],
  207. max9877_get_osc_mode, max9877_set_osc_mode),
  208. };
  209. /* This function is called from ASoC machine driver */
  210. int max9877_add_controls(struct snd_soc_codec *codec)
  211. {
  212. return snd_soc_add_controls(codec, max9877_controls,
  213. ARRAY_SIZE(max9877_controls));
  214. }
  215. EXPORT_SYMBOL_GPL(max9877_add_controls);
  216. static int __devinit max9877_i2c_probe(struct i2c_client *client,
  217. const struct i2c_device_id *id)
  218. {
  219. i2c = client;
  220. max9877_write_regs();
  221. return 0;
  222. }
  223. static __devexit int max9877_i2c_remove(struct i2c_client *client)
  224. {
  225. i2c = NULL;
  226. return 0;
  227. }
  228. static const struct i2c_device_id max9877_i2c_id[] = {
  229. { "max9877", 0 },
  230. { }
  231. };
  232. MODULE_DEVICE_TABLE(i2c, max9877_i2c_id);
  233. static struct i2c_driver max9877_i2c_driver = {
  234. .driver = {
  235. .name = "max9877",
  236. .owner = THIS_MODULE,
  237. },
  238. .probe = max9877_i2c_probe,
  239. .remove = __devexit_p(max9877_i2c_remove),
  240. .id_table = max9877_i2c_id,
  241. };
  242. static int __init max9877_init(void)
  243. {
  244. return i2c_add_driver(&max9877_i2c_driver);
  245. }
  246. module_init(max9877_init);
  247. static void __exit max9877_exit(void)
  248. {
  249. i2c_del_driver(&max9877_i2c_driver);
  250. }
  251. module_exit(max9877_exit);
  252. MODULE_DESCRIPTION("ASoC MAX9877 amp driver");
  253. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  254. MODULE_LICENSE("GPL");