daca.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PMac DACA lowlevel functions
  4. *
  5. * Copyright (c) by Takashi Iwai <[email protected]>
  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. mix = chip->mixer_data;
  73. if (!mix)
  74. return -ENODEV;
  75. ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
  76. return 0;
  77. }
  78. static int daca_put_deemphasis(struct snd_kcontrol *kcontrol,
  79. struct snd_ctl_elem_value *ucontrol)
  80. {
  81. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  82. struct pmac_daca *mix;
  83. int change;
  84. mix = chip->mixer_data;
  85. if (!mix)
  86. return -ENODEV;
  87. change = mix->deemphasis != ucontrol->value.integer.value[0];
  88. if (change) {
  89. mix->deemphasis = !!ucontrol->value.integer.value[0];
  90. daca_set_volume(mix);
  91. }
  92. return change;
  93. }
  94. /* output volume */
  95. static int daca_info_volume(struct snd_kcontrol *kcontrol,
  96. struct snd_ctl_elem_info *uinfo)
  97. {
  98. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  99. uinfo->count = 2;
  100. uinfo->value.integer.min = 0;
  101. uinfo->value.integer.max = DACA_VOL_MAX;
  102. return 0;
  103. }
  104. static int daca_get_volume(struct snd_kcontrol *kcontrol,
  105. struct snd_ctl_elem_value *ucontrol)
  106. {
  107. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  108. struct pmac_daca *mix;
  109. mix = chip->mixer_data;
  110. if (!mix)
  111. return -ENODEV;
  112. ucontrol->value.integer.value[0] = mix->left_vol;
  113. ucontrol->value.integer.value[1] = mix->right_vol;
  114. return 0;
  115. }
  116. static int daca_put_volume(struct snd_kcontrol *kcontrol,
  117. struct snd_ctl_elem_value *ucontrol)
  118. {
  119. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  120. struct pmac_daca *mix;
  121. unsigned int vol[2];
  122. int change;
  123. mix = chip->mixer_data;
  124. if (!mix)
  125. return -ENODEV;
  126. vol[0] = ucontrol->value.integer.value[0];
  127. vol[1] = ucontrol->value.integer.value[1];
  128. if (vol[0] > DACA_VOL_MAX || vol[1] > DACA_VOL_MAX)
  129. return -EINVAL;
  130. change = mix->left_vol != vol[0] ||
  131. mix->right_vol != vol[1];
  132. if (change) {
  133. mix->left_vol = vol[0];
  134. mix->right_vol = vol[1];
  135. daca_set_volume(mix);
  136. }
  137. return change;
  138. }
  139. /* amplifier switch */
  140. #define daca_info_amp daca_info_deemphasis
  141. static int daca_get_amp(struct snd_kcontrol *kcontrol,
  142. struct snd_ctl_elem_value *ucontrol)
  143. {
  144. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  145. struct pmac_daca *mix;
  146. mix = chip->mixer_data;
  147. if (!mix)
  148. return -ENODEV;
  149. ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
  150. return 0;
  151. }
  152. static int daca_put_amp(struct snd_kcontrol *kcontrol,
  153. struct snd_ctl_elem_value *ucontrol)
  154. {
  155. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  156. struct pmac_daca *mix;
  157. int change;
  158. mix = chip->mixer_data;
  159. if (!mix)
  160. return -ENODEV;
  161. change = mix->amp_on != ucontrol->value.integer.value[0];
  162. if (change) {
  163. mix->amp_on = !!ucontrol->value.integer.value[0];
  164. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
  165. mix->amp_on ? 0x05 : 0x04);
  166. }
  167. return change;
  168. }
  169. static const struct snd_kcontrol_new daca_mixers[] = {
  170. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  171. .name = "Deemphasis Switch",
  172. .info = daca_info_deemphasis,
  173. .get = daca_get_deemphasis,
  174. .put = daca_put_deemphasis
  175. },
  176. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  177. .name = "Master Playback Volume",
  178. .info = daca_info_volume,
  179. .get = daca_get_volume,
  180. .put = daca_put_volume
  181. },
  182. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  183. .name = "Power Amplifier Switch",
  184. .info = daca_info_amp,
  185. .get = daca_get_amp,
  186. .put = daca_put_amp
  187. },
  188. };
  189. #ifdef CONFIG_PM
  190. static void daca_resume(struct snd_pmac *chip)
  191. {
  192. struct pmac_daca *mix = chip->mixer_data;
  193. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_SR, 0x08);
  194. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
  195. mix->amp_on ? 0x05 : 0x04);
  196. daca_set_volume(mix);
  197. }
  198. #endif /* CONFIG_PM */
  199. static void daca_cleanup(struct snd_pmac *chip)
  200. {
  201. struct pmac_daca *mix = chip->mixer_data;
  202. if (! mix)
  203. return;
  204. snd_pmac_keywest_cleanup(&mix->i2c);
  205. kfree(mix);
  206. chip->mixer_data = NULL;
  207. }
  208. /* exported */
  209. int snd_pmac_daca_init(struct snd_pmac *chip)
  210. {
  211. int i, err;
  212. struct pmac_daca *mix;
  213. request_module("i2c-powermac");
  214. mix = kzalloc(sizeof(*mix), GFP_KERNEL);
  215. if (! mix)
  216. return -ENOMEM;
  217. chip->mixer_data = mix;
  218. chip->mixer_free = daca_cleanup;
  219. mix->amp_on = 1; /* default on */
  220. mix->i2c.addr = DACA_I2C_ADDR;
  221. mix->i2c.init_client = daca_init_client;
  222. mix->i2c.name = "DACA";
  223. err = snd_pmac_keywest_init(&mix->i2c);
  224. if (err < 0)
  225. return err;
  226. /*
  227. * build mixers
  228. */
  229. strcpy(chip->card->mixername, "PowerMac DACA");
  230. for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
  231. err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip));
  232. if (err < 0)
  233. return err;
  234. }
  235. #ifdef CONFIG_PM
  236. chip->resume = daca_resume;
  237. #endif
  238. return 0;
  239. }