tea6330t.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Routines for control of the TEA6330T circuit via i2c bus
  3. * Sound fader control circuit for car radios by Philips Semiconductors
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <sound/core.h>
  25. #include <sound/control.h>
  26. #include <sound/tea6330t.h>
  27. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  28. MODULE_DESCRIPTION("Routines for control of the TEA6330T circuit via i2c bus");
  29. MODULE_LICENSE("GPL");
  30. #define TEA6330T_ADDR (0x80>>1) /* fixed address */
  31. #define TEA6330T_SADDR_VOLUME_LEFT 0x00 /* volume left */
  32. #define TEA6330T_SADDR_VOLUME_RIGHT 0x01 /* volume right */
  33. #define TEA6330T_SADDR_BASS 0x02 /* bass control */
  34. #define TEA6330T_SADDR_TREBLE 0x03 /* treble control */
  35. #define TEA6330T_SADDR_FADER 0x04 /* fader control */
  36. #define TEA6330T_MFN 0x20 /* mute control for selected channels */
  37. #define TEA6330T_FCH 0x10 /* select fader channels - front or rear */
  38. #define TEA6330T_SADDR_AUDIO_SWITCH 0x05 /* audio switch */
  39. #define TEA6330T_GMU 0x80 /* mute control, general mute */
  40. #define TEA6330T_EQN 0x40 /* equalizer switchover (0=equalizer-on) */
  41. struct tea6330t {
  42. struct snd_i2c_device *device;
  43. struct snd_i2c_bus *bus;
  44. int equalizer;
  45. int fader;
  46. unsigned char regs[8];
  47. unsigned char mleft, mright;
  48. unsigned char bass, treble;
  49. unsigned char max_bass, max_treble;
  50. };
  51. int snd_tea6330t_detect(struct snd_i2c_bus *bus, int equalizer)
  52. {
  53. int res;
  54. snd_i2c_lock(bus);
  55. res = snd_i2c_probeaddr(bus, TEA6330T_ADDR);
  56. snd_i2c_unlock(bus);
  57. return res;
  58. }
  59. #if 0
  60. static void snd_tea6330t_set(struct tea6330t *tea,
  61. unsigned char addr, unsigned char value)
  62. {
  63. #if 0
  64. printk(KERN_DEBUG "set - 0x%x/0x%x\n", addr, value);
  65. #endif
  66. snd_i2c_write(tea->bus, TEA6330T_ADDR, addr, value, 1);
  67. }
  68. #endif
  69. #define TEA6330T_MASTER_VOLUME(xname, xindex) \
  70. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  71. .info = snd_tea6330t_info_master_volume, \
  72. .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume }
  73. static int snd_tea6330t_info_master_volume(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 = 43;
  80. return 0;
  81. }
  82. static int snd_tea6330t_get_master_volume(struct snd_kcontrol *kcontrol,
  83. struct snd_ctl_elem_value *ucontrol)
  84. {
  85. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  86. snd_i2c_lock(tea->bus);
  87. ucontrol->value.integer.value[0] = tea->mleft - 0x14;
  88. ucontrol->value.integer.value[1] = tea->mright - 0x14;
  89. snd_i2c_unlock(tea->bus);
  90. return 0;
  91. }
  92. static int snd_tea6330t_put_master_volume(struct snd_kcontrol *kcontrol,
  93. struct snd_ctl_elem_value *ucontrol)
  94. {
  95. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  96. int change, count, err;
  97. unsigned char bytes[3];
  98. unsigned char val1, val2;
  99. val1 = (ucontrol->value.integer.value[0] % 44) + 0x14;
  100. val2 = (ucontrol->value.integer.value[1] % 44) + 0x14;
  101. snd_i2c_lock(tea->bus);
  102. change = val1 != tea->mleft || val2 != tea->mright;
  103. tea->mleft = val1;
  104. tea->mright = val2;
  105. count = 0;
  106. if (tea->regs[TEA6330T_SADDR_VOLUME_LEFT] != 0) {
  107. bytes[count++] = TEA6330T_SADDR_VOLUME_LEFT;
  108. bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = tea->mleft;
  109. }
  110. if (tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] != 0) {
  111. if (count == 0)
  112. bytes[count++] = TEA6330T_SADDR_VOLUME_RIGHT;
  113. bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = tea->mright;
  114. }
  115. if (count > 0) {
  116. if ((err = snd_i2c_sendbytes(tea->device, bytes, count)) < 0)
  117. change = err;
  118. }
  119. snd_i2c_unlock(tea->bus);
  120. return change;
  121. }
  122. #define TEA6330T_MASTER_SWITCH(xname, xindex) \
  123. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  124. .info = snd_tea6330t_info_master_switch, \
  125. .get = snd_tea6330t_get_master_switch, .put = snd_tea6330t_put_master_switch }
  126. #define snd_tea6330t_info_master_switch snd_ctl_boolean_stereo_info
  127. static int snd_tea6330t_get_master_switch(struct snd_kcontrol *kcontrol,
  128. struct snd_ctl_elem_value *ucontrol)
  129. {
  130. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  131. snd_i2c_lock(tea->bus);
  132. ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
  133. ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
  134. snd_i2c_unlock(tea->bus);
  135. return 0;
  136. }
  137. static int snd_tea6330t_put_master_switch(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol)
  139. {
  140. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  141. int change, err;
  142. unsigned char bytes[3];
  143. unsigned char oval1, oval2, val1, val2;
  144. val1 = ucontrol->value.integer.value[0] & 1;
  145. val2 = ucontrol->value.integer.value[1] & 1;
  146. snd_i2c_lock(tea->bus);
  147. oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
  148. oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
  149. change = val1 != oval1 || val2 != oval2;
  150. tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
  151. tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
  152. bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
  153. bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
  154. bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
  155. if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0)
  156. change = err;
  157. snd_i2c_unlock(tea->bus);
  158. return change;
  159. }
  160. #define TEA6330T_BASS(xname, xindex) \
  161. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  162. .info = snd_tea6330t_info_bass, \
  163. .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
  164. static int snd_tea6330t_info_bass(struct snd_kcontrol *kcontrol,
  165. struct snd_ctl_elem_info *uinfo)
  166. {
  167. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  168. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  169. uinfo->count = 1;
  170. uinfo->value.integer.min = 0;
  171. uinfo->value.integer.max = tea->max_bass;
  172. return 0;
  173. }
  174. static int snd_tea6330t_get_bass(struct snd_kcontrol *kcontrol,
  175. struct snd_ctl_elem_value *ucontrol)
  176. {
  177. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  178. ucontrol->value.integer.value[0] = tea->bass;
  179. return 0;
  180. }
  181. static int snd_tea6330t_put_bass(struct snd_kcontrol *kcontrol,
  182. struct snd_ctl_elem_value *ucontrol)
  183. {
  184. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  185. int change, err;
  186. unsigned char bytes[2];
  187. unsigned char val1;
  188. val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1);
  189. snd_i2c_lock(tea->bus);
  190. tea->bass = val1;
  191. val1 += tea->equalizer ? 7 : 3;
  192. change = tea->regs[TEA6330T_SADDR_BASS] != val1;
  193. bytes[0] = TEA6330T_SADDR_BASS;
  194. bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1;
  195. if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
  196. change = err;
  197. snd_i2c_unlock(tea->bus);
  198. return change;
  199. }
  200. #define TEA6330T_TREBLE(xname, xindex) \
  201. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  202. .info = snd_tea6330t_info_treble, \
  203. .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble }
  204. static int snd_tea6330t_info_treble(struct snd_kcontrol *kcontrol,
  205. struct snd_ctl_elem_info *uinfo)
  206. {
  207. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  208. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  209. uinfo->count = 1;
  210. uinfo->value.integer.min = 0;
  211. uinfo->value.integer.max = tea->max_treble;
  212. return 0;
  213. }
  214. static int snd_tea6330t_get_treble(struct snd_kcontrol *kcontrol,
  215. struct snd_ctl_elem_value *ucontrol)
  216. {
  217. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  218. ucontrol->value.integer.value[0] = tea->treble;
  219. return 0;
  220. }
  221. static int snd_tea6330t_put_treble(struct snd_kcontrol *kcontrol,
  222. struct snd_ctl_elem_value *ucontrol)
  223. {
  224. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  225. int change, err;
  226. unsigned char bytes[2];
  227. unsigned char val1;
  228. val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
  229. snd_i2c_lock(tea->bus);
  230. tea->treble = val1;
  231. val1 += 3;
  232. change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
  233. bytes[0] = TEA6330T_SADDR_TREBLE;
  234. bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
  235. if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
  236. change = err;
  237. snd_i2c_unlock(tea->bus);
  238. return change;
  239. }
  240. static struct snd_kcontrol_new snd_tea6330t_controls[] = {
  241. TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
  242. TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
  243. TEA6330T_BASS("Tone Control - Bass", 0),
  244. TEA6330T_TREBLE("Tone Control - Treble", 0)
  245. };
  246. static void snd_tea6330_free(struct snd_i2c_device *device)
  247. {
  248. kfree(device->private_data);
  249. }
  250. int snd_tea6330t_update_mixer(struct snd_card *card,
  251. struct snd_i2c_bus *bus,
  252. int equalizer, int fader)
  253. {
  254. struct snd_i2c_device *device;
  255. struct tea6330t *tea;
  256. struct snd_kcontrol_new *knew;
  257. unsigned int idx;
  258. int err = -ENOMEM;
  259. u8 default_treble, default_bass;
  260. unsigned char bytes[7];
  261. tea = kzalloc(sizeof(*tea), GFP_KERNEL);
  262. if (tea == NULL)
  263. return -ENOMEM;
  264. if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) {
  265. kfree(tea);
  266. return err;
  267. }
  268. tea->device = device;
  269. tea->bus = bus;
  270. tea->equalizer = equalizer;
  271. tea->fader = fader;
  272. device->private_data = tea;
  273. device->private_free = snd_tea6330_free;
  274. snd_i2c_lock(bus);
  275. /* turn fader off and handle equalizer */
  276. tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
  277. tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
  278. /* initialize mixer */
  279. if (!tea->equalizer) {
  280. tea->max_bass = 9;
  281. tea->max_treble = 8;
  282. default_bass = 3 + 4;
  283. tea->bass = 4;
  284. default_treble = 3 + 4;
  285. tea->treble = 4;
  286. } else {
  287. tea->max_bass = 5;
  288. tea->max_treble = 0;
  289. default_bass = 7 + 4;
  290. tea->bass = 4;
  291. default_treble = 3;
  292. tea->treble = 0;
  293. }
  294. tea->mleft = tea->mright = 0x14;
  295. tea->regs[TEA6330T_SADDR_BASS] = default_bass;
  296. tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
  297. /* compose I2C message and put the hardware to initial state */
  298. bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
  299. for (idx = 0; idx < 6; idx++)
  300. bytes[idx+1] = tea->regs[idx];
  301. if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0)
  302. goto __error;
  303. strcat(card->mixername, ",TEA6330T");
  304. if ((err = snd_component_add(card, "TEA6330T")) < 0)
  305. goto __error;
  306. for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) {
  307. knew = &snd_tea6330t_controls[idx];
  308. if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
  309. continue;
  310. if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0)
  311. goto __error;
  312. }
  313. snd_i2c_unlock(bus);
  314. return 0;
  315. __error:
  316. snd_i2c_unlock(bus);
  317. snd_i2c_device_free(device);
  318. return err;
  319. }
  320. EXPORT_SYMBOL(snd_tea6330t_detect);
  321. EXPORT_SYMBOL(snd_tea6330t_update_mixer);
  322. /*
  323. * INIT part
  324. */
  325. static int __init alsa_tea6330t_init(void)
  326. {
  327. return 0;
  328. }
  329. static void __exit alsa_tea6330t_exit(void)
  330. {
  331. }
  332. module_init(alsa_tea6330t_init)
  333. module_exit(alsa_tea6330t_exit)