pt2258.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <linux/module.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 < 0 || val0 > 79 || val1 < 0 || val1 > 79)
  102. return -EINVAL;
  103. if (val0 == pt->volume[base] && val1 == pt->volume[base + 1])
  104. return 0;
  105. pt->volume[base] = val0;
  106. bytes[0] = pt2258_channel_code[2 * base] | (val0 / 10);
  107. bytes[1] = pt2258_channel_code[2 * base + 1] | (val0 % 10);
  108. snd_i2c_lock(pt->i2c_bus);
  109. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
  110. goto __error;
  111. snd_i2c_unlock(pt->i2c_bus);
  112. pt->volume[base + 1] = val1;
  113. bytes[0] = pt2258_channel_code[2 * base + 2] | (val1 / 10);
  114. bytes[1] = pt2258_channel_code[2 * base + 3] | (val1 % 10);
  115. snd_i2c_lock(pt->i2c_bus);
  116. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
  117. goto __error;
  118. snd_i2c_unlock(pt->i2c_bus);
  119. return 1;
  120. __error:
  121. snd_i2c_unlock(pt->i2c_bus);
  122. snd_printk(KERN_ERR "PT2258 access failed\n");
  123. return -EIO;
  124. }
  125. #define pt2258_switch_info snd_ctl_boolean_mono_info
  126. static int pt2258_switch_get(struct snd_kcontrol *kcontrol,
  127. struct snd_ctl_elem_value *ucontrol)
  128. {
  129. struct snd_pt2258 *pt = kcontrol->private_data;
  130. ucontrol->value.integer.value[0] = !pt->mute;
  131. return 0;
  132. }
  133. static int pt2258_switch_put(struct snd_kcontrol *kcontrol,
  134. struct snd_ctl_elem_value *ucontrol)
  135. {
  136. struct snd_pt2258 *pt = kcontrol->private_data;
  137. unsigned char bytes[2];
  138. int val;
  139. val = !ucontrol->value.integer.value[0];
  140. if (pt->mute == val)
  141. return 0;
  142. pt->mute = val;
  143. bytes[0] = val ? PT2258_CMD_MUTE : PT2258_CMD_UNMUTE;
  144. snd_i2c_lock(pt->i2c_bus);
  145. if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
  146. goto __error;
  147. snd_i2c_unlock(pt->i2c_bus);
  148. return 1;
  149. __error:
  150. snd_i2c_unlock(pt->i2c_bus);
  151. snd_printk(KERN_ERR "PT2258 access failed 2\n");
  152. return -EIO;
  153. }
  154. static const DECLARE_TLV_DB_SCALE(pt2258_db_scale, -7900, 100, 0);
  155. int snd_pt2258_build_controls(struct snd_pt2258 *pt)
  156. {
  157. struct snd_kcontrol_new knew;
  158. char *names[3] = {
  159. "Mic Loopback Playback Volume",
  160. "Line Loopback Playback Volume",
  161. "CD Loopback Playback Volume"
  162. };
  163. int i, err;
  164. for (i = 0; i < 3; ++i) {
  165. memset(&knew, 0, sizeof(knew));
  166. knew.name = names[i];
  167. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  168. knew.count = 1;
  169. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  170. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  171. knew.private_value = 2 * i;
  172. knew.info = pt2258_stereo_volume_info;
  173. knew.get = pt2258_stereo_volume_get;
  174. knew.put = pt2258_stereo_volume_put;
  175. knew.tlv.p = pt2258_db_scale;
  176. err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
  177. if (err < 0)
  178. return err;
  179. }
  180. memset(&knew, 0, sizeof(knew));
  181. knew.name = "Loopback Switch";
  182. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  183. knew.info = pt2258_switch_info;
  184. knew.get = pt2258_switch_get;
  185. knew.put = pt2258_switch_put;
  186. knew.access = 0;
  187. err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
  188. if (err < 0)
  189. return err;
  190. return 0;
  191. }
  192. EXPORT_SYMBOL(snd_pt2258_reset);
  193. EXPORT_SYMBOL(snd_pt2258_build_controls);