pt2258.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/core.h>
  22. #include <sound/control.h>
  23. #include <sound/tlv.h>
  24. #include <sound/i2c.h>
  25. #include <sound/pt2258.h>
  26. MODULE_AUTHOR("Jochen Voss <voss@seehuhn.de>");
  27. MODULE_DESCRIPTION("PT2258 volume controller (Princeton Technology Corp.)");
  28. MODULE_LICENSE("GPL");
  29. #define PT2258_CMD_RESET 0xc0
  30. #define PT2258_CMD_UNMUTE 0xf8
  31. #define PT2258_CMD_MUTE 0xf9
  32. static const unsigned char pt2258_channel_code[12] = {
  33. 0x80, 0x90, /* channel 1: -10dB, -1dB */
  34. 0x40, 0x50, /* channel 2: -10dB, -1dB */
  35. 0x00, 0x10, /* channel 3: -10dB, -1dB */
  36. 0x20, 0x30, /* channel 4: -10dB, -1dB */
  37. 0x60, 0x70, /* channel 5: -10dB, -1dB */
  38. 0xa0, 0xb0 /* channel 6: -10dB, -1dB */
  39. };
  40. int snd_pt2258_reset(struct snd_pt2258 *pt)
  41. {
  42. unsigned char bytes[2];
  43. int i;
  44. /* reset chip */
  45. bytes[0] = PT2258_CMD_RESET;
  46. snd_i2c_lock(pt->i2c_bus);
  47. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
  48. goto __error;
  49. snd_i2c_unlock(pt->i2c_bus);
  50. /* mute all channels */
  51. pt->mute = 1;
  52. bytes[0] = PT2258_CMD_MUTE;
  53. snd_i2c_lock(pt->i2c_bus);
  54. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
  55. goto __error;
  56. snd_i2c_unlock(pt->i2c_bus);
  57. /* set all channels to 0dB */
  58. for (i = 0; i < 6; ++i)
  59. pt->volume[i] = 0;
  60. bytes[0] = 0xd0;
  61. bytes[1] = 0xe0;
  62. snd_i2c_lock(pt->i2c_bus);
  63. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
  64. goto __error;
  65. snd_i2c_unlock(pt->i2c_bus);
  66. return 0;
  67. __error:
  68. snd_i2c_unlock(pt->i2c_bus);
  69. snd_printk(KERN_ERR "PT2258 reset failed\n");
  70. return -EIO;
  71. }
  72. static int pt2258_stereo_volume_info(struct snd_kcontrol *kcontrol,
  73. struct snd_ctl_elem_info *uinfo)
  74. {
  75. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  76. uinfo->count = 2;
  77. uinfo->value.integer.min = 0;
  78. uinfo->value.integer.max = 79;
  79. return 0;
  80. }
  81. static int pt2258_stereo_volume_get(struct snd_kcontrol *kcontrol,
  82. struct snd_ctl_elem_value *ucontrol)
  83. {
  84. struct snd_pt2258 *pt = kcontrol->private_data;
  85. int base = kcontrol->private_value;
  86. /* chip does not support register reads */
  87. ucontrol->value.integer.value[0] = 79 - pt->volume[base];
  88. ucontrol->value.integer.value[1] = 79 - pt->volume[base + 1];
  89. return 0;
  90. }
  91. static int pt2258_stereo_volume_put(struct snd_kcontrol *kcontrol,
  92. struct snd_ctl_elem_value *ucontrol)
  93. {
  94. struct snd_pt2258 *pt = kcontrol->private_data;
  95. int base = kcontrol->private_value;
  96. unsigned char bytes[2];
  97. int val0, val1;
  98. val0 = 79 - ucontrol->value.integer.value[0];
  99. val1 = 79 - ucontrol->value.integer.value[1];
  100. if (val0 < 0 || val0 > 79 || val1 < 0 || val1 > 79)
  101. return -EINVAL;
  102. if (val0 == pt->volume[base] && val1 == pt->volume[base + 1])
  103. return 0;
  104. pt->volume[base] = val0;
  105. bytes[0] = pt2258_channel_code[2 * base] | (val0 / 10);
  106. bytes[1] = pt2258_channel_code[2 * base + 1] | (val0 % 10);
  107. snd_i2c_lock(pt->i2c_bus);
  108. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
  109. goto __error;
  110. snd_i2c_unlock(pt->i2c_bus);
  111. pt->volume[base + 1] = val1;
  112. bytes[0] = pt2258_channel_code[2 * base + 2] | (val1 / 10);
  113. bytes[1] = pt2258_channel_code[2 * base + 3] | (val1 % 10);
  114. snd_i2c_lock(pt->i2c_bus);
  115. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
  116. goto __error;
  117. snd_i2c_unlock(pt->i2c_bus);
  118. return 1;
  119. __error:
  120. snd_i2c_unlock(pt->i2c_bus);
  121. snd_printk(KERN_ERR "PT2258 access failed\n");
  122. return -EIO;
  123. }
  124. #define pt2258_switch_info snd_ctl_boolean_mono_info
  125. static int pt2258_switch_get(struct snd_kcontrol *kcontrol,
  126. struct snd_ctl_elem_value *ucontrol)
  127. {
  128. struct snd_pt2258 *pt = kcontrol->private_data;
  129. ucontrol->value.integer.value[0] = !pt->mute;
  130. return 0;
  131. }
  132. static int pt2258_switch_put(struct snd_kcontrol *kcontrol,
  133. struct snd_ctl_elem_value *ucontrol)
  134. {
  135. struct snd_pt2258 *pt = kcontrol->private_data;
  136. unsigned char bytes[2];
  137. int val;
  138. val = !ucontrol->value.integer.value[0];
  139. if (pt->mute == val)
  140. return 0;
  141. pt->mute = val;
  142. bytes[0] = val ? PT2258_CMD_MUTE : PT2258_CMD_UNMUTE;
  143. snd_i2c_lock(pt->i2c_bus);
  144. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
  145. goto __error;
  146. snd_i2c_unlock(pt->i2c_bus);
  147. return 1;
  148. __error:
  149. snd_i2c_unlock(pt->i2c_bus);
  150. snd_printk(KERN_ERR "PT2258 access failed 2\n");
  151. return -EIO;
  152. }
  153. static const DECLARE_TLV_DB_SCALE(pt2258_db_scale, -7900, 100, 0);
  154. int snd_pt2258_build_controls(struct snd_pt2258 *pt)
  155. {
  156. struct snd_kcontrol_new knew;
  157. char *names[3] = {
  158. "Mic Loopback Playback Volume",
  159. "Line Loopback Playback Volume",
  160. "CD Loopback Playback Volume"
  161. };
  162. int i, err;
  163. for (i = 0; i < 3; ++i) {
  164. memset(&knew, 0, sizeof(knew));
  165. knew.name = names[i];
  166. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  167. knew.count = 1;
  168. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  169. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  170. knew.private_value = 2 * i;
  171. knew.info = pt2258_stereo_volume_info;
  172. knew.get = pt2258_stereo_volume_get;
  173. knew.put = pt2258_stereo_volume_put;
  174. knew.tlv.p = pt2258_db_scale;
  175. err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
  176. if (err < 0)
  177. return err;
  178. }
  179. memset(&knew, 0, sizeof(knew));
  180. knew.name = "Loopback Switch";
  181. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  182. knew.info = pt2258_switch_info;
  183. knew.get = pt2258_switch_get;
  184. knew.put = pt2258_switch_put;
  185. knew.access = 0;
  186. err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
  187. if (err < 0)
  188. return err;
  189. return 0;
  190. }
  191. EXPORT_SYMBOL(snd_pt2258_reset);
  192. EXPORT_SYMBOL(snd_pt2258_build_controls);