pt2258.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * ALSA Driver for the PT2258 volume controller.
  3. *
  4. * Copyright (c) 2006 Jochen Voss <voss@seehuhn.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <sound/core.h>
  23. #include <sound/control.h>
  24. #include <sound/tlv.h>
  25. #include <sound/i2c.h>
  26. #include <sound/pt2258.h>
  27. MODULE_AUTHOR("Jochen Voss <voss@seehuhn.de>");
  28. MODULE_DESCRIPTION("PT2258 volume controller (Princeton Technology Corp.)");
  29. MODULE_LICENSE("GPL");
  30. #define PT2258_CMD_RESET 0xc0
  31. #define PT2258_CMD_UNMUTE 0xf8
  32. #define PT2258_CMD_MUTE 0xf9
  33. static const unsigned char pt2258_channel_code[12] = {
  34. 0x80, 0x90, /* channel 1: -10dB, -1dB */
  35. 0x40, 0x50, /* channel 2: -10dB, -1dB */
  36. 0x00, 0x10, /* channel 3: -10dB, -1dB */
  37. 0x20, 0x30, /* channel 4: -10dB, -1dB */
  38. 0x60, 0x70, /* channel 5: -10dB, -1dB */
  39. 0xa0, 0xb0 /* channel 6: -10dB, -1dB */
  40. };
  41. int snd_pt2258_reset(struct snd_pt2258 *pt)
  42. {
  43. unsigned char bytes[2];
  44. int i;
  45. /* reset chip */
  46. bytes[0] = PT2258_CMD_RESET;
  47. snd_i2c_lock(pt->i2c_bus);
  48. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
  49. goto __error;
  50. snd_i2c_unlock(pt->i2c_bus);
  51. /* mute all channels */
  52. pt->mute = 1;
  53. bytes[0] = PT2258_CMD_MUTE;
  54. snd_i2c_lock(pt->i2c_bus);
  55. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
  56. goto __error;
  57. snd_i2c_unlock(pt->i2c_bus);
  58. /* set all channels to 0dB */
  59. for (i = 0; i < 6; ++i)
  60. pt->volume[i] = 0;
  61. bytes[0] = 0xd0;
  62. bytes[1] = 0xe0;
  63. snd_i2c_lock(pt->i2c_bus);
  64. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
  65. goto __error;
  66. snd_i2c_unlock(pt->i2c_bus);
  67. return 0;
  68. __error:
  69. snd_i2c_unlock(pt->i2c_bus);
  70. snd_printk(KERN_ERR "PT2258 reset failed\n");
  71. return -EIO;
  72. }
  73. static int pt2258_stereo_volume_info(struct snd_kcontrol *kcontrol,
  74. struct snd_ctl_elem_info *uinfo)
  75. {
  76. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  77. uinfo->count = 2;
  78. uinfo->value.integer.min = 0;
  79. uinfo->value.integer.max = 79;
  80. return 0;
  81. }
  82. static int pt2258_stereo_volume_get(struct snd_kcontrol *kcontrol,
  83. struct snd_ctl_elem_value *ucontrol)
  84. {
  85. struct snd_pt2258 *pt = kcontrol->private_data;
  86. int base = kcontrol->private_value;
  87. /* chip does not support register reads */
  88. ucontrol->value.integer.value[0] = 79 - pt->volume[base];
  89. ucontrol->value.integer.value[1] = 79 - pt->volume[base + 1];
  90. return 0;
  91. }
  92. static int pt2258_stereo_volume_put(struct snd_kcontrol *kcontrol,
  93. struct snd_ctl_elem_value *ucontrol)
  94. {
  95. struct snd_pt2258 *pt = kcontrol->private_data;
  96. int base = kcontrol->private_value;
  97. unsigned char bytes[2];
  98. int val0, val1;
  99. val0 = 79 - ucontrol->value.integer.value[0];
  100. val1 = 79 - ucontrol->value.integer.value[1];
  101. if (val0 == pt->volume[base] && val1 == pt->volume[base + 1])
  102. return 0;
  103. pt->volume[base] = val0;
  104. bytes[0] = pt2258_channel_code[2 * base] | (val0 / 10);
  105. bytes[1] = pt2258_channel_code[2 * base + 1] | (val0 % 10);
  106. snd_i2c_lock(pt->i2c_bus);
  107. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
  108. goto __error;
  109. snd_i2c_unlock(pt->i2c_bus);
  110. pt->volume[base + 1] = val1;
  111. bytes[0] = pt2258_channel_code[2 * base + 2] | (val1 / 10);
  112. bytes[1] = pt2258_channel_code[2 * base + 3] | (val1 % 10);
  113. snd_i2c_lock(pt->i2c_bus);
  114. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
  115. goto __error;
  116. snd_i2c_unlock(pt->i2c_bus);
  117. return 1;
  118. __error:
  119. snd_i2c_unlock(pt->i2c_bus);
  120. snd_printk(KERN_ERR "PT2258 access failed\n");
  121. return -EIO;
  122. }
  123. #define pt2258_switch_info snd_ctl_boolean_mono_info
  124. static int pt2258_switch_get(struct snd_kcontrol *kcontrol,
  125. struct snd_ctl_elem_value *ucontrol)
  126. {
  127. struct snd_pt2258 *pt = kcontrol->private_data;
  128. ucontrol->value.integer.value[0] = !pt->mute;
  129. return 0;
  130. }
  131. static int pt2258_switch_put(struct snd_kcontrol *kcontrol,
  132. struct snd_ctl_elem_value *ucontrol)
  133. {
  134. struct snd_pt2258 *pt = kcontrol->private_data;
  135. unsigned char bytes[2];
  136. int val;
  137. val = !ucontrol->value.integer.value[0];
  138. if (pt->mute == val)
  139. return 0;
  140. pt->mute = val;
  141. bytes[0] = val ? PT2258_CMD_MUTE : PT2258_CMD_UNMUTE;
  142. snd_i2c_lock(pt->i2c_bus);
  143. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
  144. goto __error;
  145. snd_i2c_unlock(pt->i2c_bus);
  146. return 1;
  147. __error:
  148. snd_i2c_unlock(pt->i2c_bus);
  149. snd_printk(KERN_ERR "PT2258 access failed 2\n");
  150. return -EIO;
  151. }
  152. static const DECLARE_TLV_DB_SCALE(pt2258_db_scale, -7900, 100, 0);
  153. int snd_pt2258_build_controls(struct snd_pt2258 *pt)
  154. {
  155. struct snd_kcontrol_new knew;
  156. char *names[3] = {
  157. "Mic Loopback Playback Volume",
  158. "Line Loopback Playback Volume",
  159. "CD Loopback Playback Volume"
  160. };
  161. int i, err;
  162. for (i = 0; i < 3; ++i) {
  163. memset(&knew, 0, sizeof(knew));
  164. knew.name = names[i];
  165. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  166. knew.count = 1;
  167. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  168. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  169. knew.private_value = 2 * i;
  170. knew.info = pt2258_stereo_volume_info;
  171. knew.get = pt2258_stereo_volume_get;
  172. knew.put = pt2258_stereo_volume_put;
  173. knew.tlv.p = pt2258_db_scale;
  174. err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
  175. if (err < 0)
  176. return err;
  177. }
  178. memset(&knew, 0, sizeof(knew));
  179. knew.name = "Loopback Switch";
  180. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  181. knew.info = pt2258_switch_info;
  182. knew.get = pt2258_switch_get;
  183. knew.put = pt2258_switch_put;
  184. knew.access = 0;
  185. err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
  186. if (err < 0)
  187. return err;
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(snd_pt2258_reset);
  191. EXPORT_SYMBOL(snd_pt2258_build_controls);