daca.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PMac DACA lowlevel functions
  4. *
  5. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/i2c.h>
  9. #include <linux/kmod.h>
  10. #include <linux/slab.h>
  11. #include <sound/core.h>
  12. #include "pmac.h"
  13. /* i2c address */
  14. #define DACA_I2C_ADDR 0x4d
  15. /* registers */
  16. #define DACA_REG_SR 0x01
  17. #define DACA_REG_AVOL 0x02
  18. #define DACA_REG_GCFG 0x03
  19. /* maximum volume value */
  20. #define DACA_VOL_MAX 0x38
  21. struct pmac_daca {
  22. struct pmac_keywest i2c;
  23. int left_vol, right_vol;
  24. unsigned int deemphasis : 1;
  25. unsigned int amp_on : 1;
  26. };
  27. /*
  28. * initialize / detect DACA
  29. */
  30. static int daca_init_client(struct pmac_keywest *i2c)
  31. {
  32. unsigned short wdata = 0x00;
  33. /* SR: no swap, 1bit delay, 32-48kHz */
  34. /* GCFG: power amp inverted, DAC on */
  35. if (i2c_smbus_write_byte_data(i2c->client, DACA_REG_SR, 0x08) < 0 ||
  36. i2c_smbus_write_byte_data(i2c->client, DACA_REG_GCFG, 0x05) < 0)
  37. return -EINVAL;
  38. return i2c_smbus_write_block_data(i2c->client, DACA_REG_AVOL,
  39. 2, (unsigned char*)&wdata);
  40. }
  41. /*
  42. * update volume
  43. */
  44. static int daca_set_volume(struct pmac_daca *mix)
  45. {
  46. unsigned char data[2];
  47. if (! mix->i2c.client)
  48. return -ENODEV;
  49. if (mix->left_vol > DACA_VOL_MAX)
  50. data[0] = DACA_VOL_MAX;
  51. else
  52. data[0] = mix->left_vol;
  53. if (mix->right_vol > DACA_VOL_MAX)
  54. data[1] = DACA_VOL_MAX;
  55. else
  56. data[1] = mix->right_vol;
  57. data[1] |= mix->deemphasis ? 0x40 : 0;
  58. if (i2c_smbus_write_block_data(mix->i2c.client, DACA_REG_AVOL,
  59. 2, data) < 0) {
  60. snd_printk(KERN_ERR "failed to set volume \n");
  61. return -EINVAL;
  62. }
  63. return 0;
  64. }
  65. /* deemphasis switch */
  66. #define daca_info_deemphasis snd_ctl_boolean_mono_info
  67. static int daca_get_deemphasis(struct snd_kcontrol *kcontrol,
  68. struct snd_ctl_elem_value *ucontrol)
  69. {
  70. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  71. struct pmac_daca *mix;
  72. if (! (mix = chip->mixer_data))
  73. return -ENODEV;
  74. ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
  75. return 0;
  76. }
  77. static int daca_put_deemphasis(struct snd_kcontrol *kcontrol,
  78. struct snd_ctl_elem_value *ucontrol)
  79. {
  80. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  81. struct pmac_daca *mix;
  82. int change;
  83. if (! (mix = chip->mixer_data))
  84. return -ENODEV;
  85. change = mix->deemphasis != ucontrol->value.integer.value[0];
  86. if (change) {
  87. mix->deemphasis = !!ucontrol->value.integer.value[0];
  88. daca_set_volume(mix);
  89. }
  90. return change;
  91. }
  92. /* output volume */
  93. static int daca_info_volume(struct snd_kcontrol *kcontrol,
  94. struct snd_ctl_elem_info *uinfo)
  95. {
  96. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  97. uinfo->count = 2;
  98. uinfo->value.integer.min = 0;
  99. uinfo->value.integer.max = DACA_VOL_MAX;
  100. return 0;
  101. }
  102. static int daca_get_volume(struct snd_kcontrol *kcontrol,
  103. struct snd_ctl_elem_value *ucontrol)
  104. {
  105. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  106. struct pmac_daca *mix;
  107. if (! (mix = chip->mixer_data))
  108. return -ENODEV;
  109. ucontrol->value.integer.value[0] = mix->left_vol;
  110. ucontrol->value.integer.value[1] = mix->right_vol;
  111. return 0;
  112. }
  113. static int daca_put_volume(struct snd_kcontrol *kcontrol,
  114. struct snd_ctl_elem_value *ucontrol)
  115. {
  116. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  117. struct pmac_daca *mix;
  118. unsigned int vol[2];
  119. int change;
  120. if (! (mix = chip->mixer_data))
  121. return -ENODEV;
  122. vol[0] = ucontrol->value.integer.value[0];
  123. vol[1] = ucontrol->value.integer.value[1];
  124. if (vol[0] > DACA_VOL_MAX || vol[1] > DACA_VOL_MAX)
  125. return -EINVAL;
  126. change = mix->left_vol != vol[0] ||
  127. mix->right_vol != vol[1];
  128. if (change) {
  129. mix->left_vol = vol[0];
  130. mix->right_vol = vol[1];
  131. daca_set_volume(mix);
  132. }
  133. return change;
  134. }
  135. /* amplifier switch */
  136. #define daca_info_amp daca_info_deemphasis
  137. static int daca_get_amp(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol)
  139. {
  140. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  141. struct pmac_daca *mix;
  142. if (! (mix = chip->mixer_data))
  143. return -ENODEV;
  144. ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
  145. return 0;
  146. }
  147. static int daca_put_amp(struct snd_kcontrol *kcontrol,
  148. struct snd_ctl_elem_value *ucontrol)
  149. {
  150. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  151. struct pmac_daca *mix;
  152. int change;
  153. if (! (mix = chip->mixer_data))
  154. return -ENODEV;
  155. change = mix->amp_on != ucontrol->value.integer.value[0];
  156. if (change) {
  157. mix->amp_on = !!ucontrol->value.integer.value[0];
  158. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
  159. mix->amp_on ? 0x05 : 0x04);
  160. }
  161. return change;
  162. }
  163. static const struct snd_kcontrol_new daca_mixers[] = {
  164. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  165. .name = "Deemphasis Switch",
  166. .info = daca_info_deemphasis,
  167. .get = daca_get_deemphasis,
  168. .put = daca_put_deemphasis
  169. },
  170. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  171. .name = "Master Playback Volume",
  172. .info = daca_info_volume,
  173. .get = daca_get_volume,
  174. .put = daca_put_volume
  175. },
  176. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  177. .name = "Power Amplifier Switch",
  178. .info = daca_info_amp,
  179. .get = daca_get_amp,
  180. .put = daca_put_amp
  181. },
  182. };
  183. #ifdef CONFIG_PM
  184. static void daca_resume(struct snd_pmac *chip)
  185. {
  186. struct pmac_daca *mix = chip->mixer_data;
  187. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_SR, 0x08);
  188. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
  189. mix->amp_on ? 0x05 : 0x04);
  190. daca_set_volume(mix);
  191. }
  192. #endif /* CONFIG_PM */
  193. static void daca_cleanup(struct snd_pmac *chip)
  194. {
  195. struct pmac_daca *mix = chip->mixer_data;
  196. if (! mix)
  197. return;
  198. snd_pmac_keywest_cleanup(&mix->i2c);
  199. kfree(mix);
  200. chip->mixer_data = NULL;
  201. }
  202. /* exported */
  203. int snd_pmac_daca_init(struct snd_pmac *chip)
  204. {
  205. int i, err;
  206. struct pmac_daca *mix;
  207. request_module("i2c-powermac");
  208. mix = kzalloc(sizeof(*mix), GFP_KERNEL);
  209. if (! mix)
  210. return -ENOMEM;
  211. chip->mixer_data = mix;
  212. chip->mixer_free = daca_cleanup;
  213. mix->amp_on = 1; /* default on */
  214. mix->i2c.addr = DACA_I2C_ADDR;
  215. mix->i2c.init_client = daca_init_client;
  216. mix->i2c.name = "DACA";
  217. if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
  218. return err;
  219. /*
  220. * build mixers
  221. */
  222. strcpy(chip->card->mixername, "PowerMac DACA");
  223. for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
  224. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip))) < 0)
  225. return err;
  226. }
  227. #ifdef CONFIG_PM
  228. chip->resume = daca_resume;
  229. #endif
  230. return 0;
  231. }