tea6330t.c 10 KB

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