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