tea6330t.c 10 KB

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