tea6330t.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/control.h>
  27. #include <sound/tea6330t.h>
  28. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.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. static int snd_tea6330t_info_master_switch(struct snd_kcontrol *kcontrol,
  128. struct snd_ctl_elem_info *uinfo)
  129. {
  130. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  131. uinfo->count = 2;
  132. uinfo->value.integer.min = 0;
  133. uinfo->value.integer.max = 1;
  134. return 0;
  135. }
  136. static int snd_tea6330t_get_master_switch(struct snd_kcontrol *kcontrol,
  137. struct snd_ctl_elem_value *ucontrol)
  138. {
  139. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  140. snd_i2c_lock(tea->bus);
  141. ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
  142. ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
  143. snd_i2c_unlock(tea->bus);
  144. return 0;
  145. }
  146. static int snd_tea6330t_put_master_switch(struct snd_kcontrol *kcontrol,
  147. struct snd_ctl_elem_value *ucontrol)
  148. {
  149. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  150. int change, err;
  151. unsigned char bytes[3];
  152. unsigned char oval1, oval2, val1, val2;
  153. val1 = ucontrol->value.integer.value[0] & 1;
  154. val2 = ucontrol->value.integer.value[1] & 1;
  155. snd_i2c_lock(tea->bus);
  156. oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
  157. oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
  158. change = val1 != oval1 || val2 != oval2;
  159. tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
  160. tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
  161. bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
  162. bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
  163. bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
  164. if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0)
  165. change = err;
  166. snd_i2c_unlock(tea->bus);
  167. return change;
  168. }
  169. #define TEA6330T_BASS(xname, xindex) \
  170. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  171. .info = snd_tea6330t_info_bass, \
  172. .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
  173. static int snd_tea6330t_info_bass(struct snd_kcontrol *kcontrol,
  174. struct snd_ctl_elem_info *uinfo)
  175. {
  176. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  177. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  178. uinfo->count = 1;
  179. uinfo->value.integer.min = 0;
  180. uinfo->value.integer.max = tea->max_bass;
  181. return 0;
  182. }
  183. static int snd_tea6330t_get_bass(struct snd_kcontrol *kcontrol,
  184. struct snd_ctl_elem_value *ucontrol)
  185. {
  186. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  187. ucontrol->value.integer.value[0] = tea->bass;
  188. return 0;
  189. }
  190. static int snd_tea6330t_put_bass(struct snd_kcontrol *kcontrol,
  191. struct snd_ctl_elem_value *ucontrol)
  192. {
  193. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  194. int change, err;
  195. unsigned char bytes[2];
  196. unsigned char val1;
  197. val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1);
  198. snd_i2c_lock(tea->bus);
  199. tea->bass = val1;
  200. val1 += tea->equalizer ? 7 : 3;
  201. change = tea->regs[TEA6330T_SADDR_BASS] != val1;
  202. bytes[0] = TEA6330T_SADDR_BASS;
  203. bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1;
  204. if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
  205. change = err;
  206. snd_i2c_unlock(tea->bus);
  207. return change;
  208. }
  209. #define TEA6330T_TREBLE(xname, xindex) \
  210. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  211. .info = snd_tea6330t_info_treble, \
  212. .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble }
  213. static int snd_tea6330t_info_treble(struct snd_kcontrol *kcontrol,
  214. struct snd_ctl_elem_info *uinfo)
  215. {
  216. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  217. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  218. uinfo->count = 1;
  219. uinfo->value.integer.min = 0;
  220. uinfo->value.integer.max = tea->max_treble;
  221. return 0;
  222. }
  223. static int snd_tea6330t_get_treble(struct snd_kcontrol *kcontrol,
  224. struct snd_ctl_elem_value *ucontrol)
  225. {
  226. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  227. ucontrol->value.integer.value[0] = tea->treble;
  228. return 0;
  229. }
  230. static int snd_tea6330t_put_treble(struct snd_kcontrol *kcontrol,
  231. struct snd_ctl_elem_value *ucontrol)
  232. {
  233. struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  234. int change, err;
  235. unsigned char bytes[2];
  236. unsigned char val1;
  237. val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
  238. snd_i2c_lock(tea->bus);
  239. tea->treble = val1;
  240. val1 += 3;
  241. change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
  242. bytes[0] = TEA6330T_SADDR_TREBLE;
  243. bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
  244. if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
  245. change = err;
  246. snd_i2c_unlock(tea->bus);
  247. return change;
  248. }
  249. static struct snd_kcontrol_new snd_tea6330t_controls[] = {
  250. TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
  251. TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
  252. TEA6330T_BASS("Tone Control - Bass", 0),
  253. TEA6330T_TREBLE("Tone Control - Treble", 0)
  254. };
  255. static void snd_tea6330_free(struct snd_i2c_device *device)
  256. {
  257. kfree(device->private_data);
  258. }
  259. int snd_tea6330t_update_mixer(struct snd_card *card,
  260. struct snd_i2c_bus *bus,
  261. int equalizer, int fader)
  262. {
  263. struct snd_i2c_device *device;
  264. struct tea6330t *tea;
  265. struct snd_kcontrol_new *knew;
  266. unsigned int idx;
  267. int err = -ENOMEM;
  268. u8 default_treble, default_bass;
  269. unsigned char bytes[7];
  270. tea = kzalloc(sizeof(*tea), GFP_KERNEL);
  271. if (tea == NULL)
  272. return -ENOMEM;
  273. if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) {
  274. kfree(tea);
  275. return err;
  276. }
  277. tea->device = device;
  278. tea->bus = bus;
  279. tea->equalizer = equalizer;
  280. tea->fader = fader;
  281. device->private_data = tea;
  282. device->private_free = snd_tea6330_free;
  283. snd_i2c_lock(bus);
  284. /* turn fader off and handle equalizer */
  285. tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
  286. tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
  287. /* initialize mixer */
  288. if (!tea->equalizer) {
  289. tea->max_bass = 9;
  290. tea->max_treble = 8;
  291. default_bass = 3 + 4;
  292. tea->bass = 4;
  293. default_treble = 3 + 4;
  294. tea->treble = 4;
  295. } else {
  296. tea->max_bass = 5;
  297. tea->max_treble = 0;
  298. default_bass = 7 + 4;
  299. tea->bass = 4;
  300. default_treble = 3;
  301. tea->treble = 0;
  302. }
  303. tea->mleft = tea->mright = 0x14;
  304. tea->regs[TEA6330T_SADDR_BASS] = default_bass;
  305. tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
  306. /* compose I2C message and put the hardware to initial state */
  307. bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
  308. for (idx = 0; idx < 6; idx++)
  309. bytes[idx+1] = tea->regs[idx];
  310. if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0)
  311. goto __error;
  312. strcat(card->mixername, ",TEA6330T");
  313. if ((err = snd_component_add(card, "TEA6330T")) < 0)
  314. goto __error;
  315. for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) {
  316. knew = &snd_tea6330t_controls[idx];
  317. if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
  318. continue;
  319. if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0)
  320. goto __error;
  321. }
  322. snd_i2c_unlock(bus);
  323. return 0;
  324. __error:
  325. snd_i2c_unlock(bus);
  326. snd_i2c_device_free(device);
  327. return err;
  328. }
  329. EXPORT_SYMBOL(snd_tea6330t_detect);
  330. EXPORT_SYMBOL(snd_tea6330t_update_mixer);
  331. /*
  332. * INIT part
  333. */
  334. static int __init alsa_tea6330t_init(void)
  335. {
  336. return 0;
  337. }
  338. static void __exit alsa_tea6330t_exit(void)
  339. {
  340. }
  341. module_init(alsa_tea6330t_init)
  342. module_exit(alsa_tea6330t_exit)