tea6330t.c 11 KB

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