daca.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * PMac DACA lowlevel functions
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <sound/driver.h>
  21. #include <linux/init.h>
  22. #include <linux/i2c.h>
  23. #include <linux/kmod.h>
  24. #include <linux/slab.h>
  25. #include <sound/core.h>
  26. #include "pmac.h"
  27. /* i2c address */
  28. #define DACA_I2C_ADDR 0x4d
  29. /* registers */
  30. #define DACA_REG_SR 0x01
  31. #define DACA_REG_AVOL 0x02
  32. #define DACA_REG_GCFG 0x03
  33. /* maximum volume value */
  34. #define DACA_VOL_MAX 0x38
  35. struct pmac_daca {
  36. struct pmac_keywest i2c;
  37. int left_vol, right_vol;
  38. unsigned int deemphasis : 1;
  39. unsigned int amp_on : 1;
  40. };
  41. /*
  42. * initialize / detect DACA
  43. */
  44. static int daca_init_client(struct pmac_keywest *i2c)
  45. {
  46. unsigned short wdata = 0x00;
  47. /* SR: no swap, 1bit delay, 32-48kHz */
  48. /* GCFG: power amp inverted, DAC on */
  49. if (i2c_smbus_write_byte_data(i2c->client, DACA_REG_SR, 0x08) < 0 ||
  50. i2c_smbus_write_byte_data(i2c->client, DACA_REG_GCFG, 0x05) < 0)
  51. return -EINVAL;
  52. return i2c_smbus_write_block_data(i2c->client, DACA_REG_AVOL,
  53. 2, (unsigned char*)&wdata);
  54. }
  55. /*
  56. * update volume
  57. */
  58. static int daca_set_volume(struct pmac_daca *mix)
  59. {
  60. unsigned char data[2];
  61. if (! mix->i2c.client)
  62. return -ENODEV;
  63. if (mix->left_vol > DACA_VOL_MAX)
  64. data[0] = DACA_VOL_MAX;
  65. else
  66. data[0] = mix->left_vol;
  67. if (mix->right_vol > DACA_VOL_MAX)
  68. data[1] = DACA_VOL_MAX;
  69. else
  70. data[1] = mix->right_vol;
  71. data[1] |= mix->deemphasis ? 0x40 : 0;
  72. if (i2c_smbus_write_block_data(mix->i2c.client, DACA_REG_AVOL,
  73. 2, data) < 0) {
  74. snd_printk("failed to set volume \n");
  75. return -EINVAL;
  76. }
  77. return 0;
  78. }
  79. /* deemphasis switch */
  80. #define daca_info_deemphasis snd_ctl_boolean_mono_info
  81. static int daca_get_deemphasis(struct snd_kcontrol *kcontrol,
  82. struct snd_ctl_elem_value *ucontrol)
  83. {
  84. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  85. struct pmac_daca *mix;
  86. if (! (mix = chip->mixer_data))
  87. return -ENODEV;
  88. ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
  89. return 0;
  90. }
  91. static int daca_put_deemphasis(struct snd_kcontrol *kcontrol,
  92. struct snd_ctl_elem_value *ucontrol)
  93. {
  94. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  95. struct pmac_daca *mix;
  96. int change;
  97. if (! (mix = chip->mixer_data))
  98. return -ENODEV;
  99. change = mix->deemphasis != ucontrol->value.integer.value[0];
  100. if (change) {
  101. mix->deemphasis = ucontrol->value.integer.value[0];
  102. daca_set_volume(mix);
  103. }
  104. return change;
  105. }
  106. /* output volume */
  107. static int daca_info_volume(struct snd_kcontrol *kcontrol,
  108. struct snd_ctl_elem_info *uinfo)
  109. {
  110. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  111. uinfo->count = 2;
  112. uinfo->value.integer.min = 0;
  113. uinfo->value.integer.max = DACA_VOL_MAX;
  114. return 0;
  115. }
  116. static int daca_get_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. if (! (mix = chip->mixer_data))
  122. return -ENODEV;
  123. ucontrol->value.integer.value[0] = mix->left_vol;
  124. ucontrol->value.integer.value[1] = mix->right_vol;
  125. return 0;
  126. }
  127. static int daca_put_volume(struct snd_kcontrol *kcontrol,
  128. struct snd_ctl_elem_value *ucontrol)
  129. {
  130. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  131. struct pmac_daca *mix;
  132. int change;
  133. if (! (mix = chip->mixer_data))
  134. return -ENODEV;
  135. change = mix->left_vol != ucontrol->value.integer.value[0] ||
  136. mix->right_vol != ucontrol->value.integer.value[1];
  137. if (change) {
  138. mix->left_vol = ucontrol->value.integer.value[0];
  139. mix->right_vol = ucontrol->value.integer.value[1];
  140. daca_set_volume(mix);
  141. }
  142. return change;
  143. }
  144. /* amplifier switch */
  145. #define daca_info_amp daca_info_deemphasis
  146. static int daca_get_amp(struct snd_kcontrol *kcontrol,
  147. struct snd_ctl_elem_value *ucontrol)
  148. {
  149. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  150. struct pmac_daca *mix;
  151. if (! (mix = chip->mixer_data))
  152. return -ENODEV;
  153. ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
  154. return 0;
  155. }
  156. static int daca_put_amp(struct snd_kcontrol *kcontrol,
  157. struct snd_ctl_elem_value *ucontrol)
  158. {
  159. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  160. struct pmac_daca *mix;
  161. int change;
  162. if (! (mix = chip->mixer_data))
  163. return -ENODEV;
  164. change = mix->amp_on != ucontrol->value.integer.value[0];
  165. if (change) {
  166. mix->amp_on = ucontrol->value.integer.value[0];
  167. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
  168. mix->amp_on ? 0x05 : 0x04);
  169. }
  170. return change;
  171. }
  172. static struct snd_kcontrol_new daca_mixers[] = {
  173. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  174. .name = "Deemphasis Switch",
  175. .info = daca_info_deemphasis,
  176. .get = daca_get_deemphasis,
  177. .put = daca_put_deemphasis
  178. },
  179. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  180. .name = "Master Playback Volume",
  181. .info = daca_info_volume,
  182. .get = daca_get_volume,
  183. .put = daca_put_volume
  184. },
  185. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  186. .name = "Power Amplifier Switch",
  187. .info = daca_info_amp,
  188. .get = daca_get_amp,
  189. .put = daca_put_amp
  190. },
  191. };
  192. #ifdef CONFIG_PM
  193. static void daca_resume(struct snd_pmac *chip)
  194. {
  195. struct pmac_daca *mix = chip->mixer_data;
  196. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_SR, 0x08);
  197. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
  198. mix->amp_on ? 0x05 : 0x04);
  199. daca_set_volume(mix);
  200. }
  201. #endif /* CONFIG_PM */
  202. static void daca_cleanup(struct snd_pmac *chip)
  203. {
  204. struct pmac_daca *mix = chip->mixer_data;
  205. if (! mix)
  206. return;
  207. snd_pmac_keywest_cleanup(&mix->i2c);
  208. kfree(mix);
  209. chip->mixer_data = NULL;
  210. }
  211. /* exported */
  212. int __init snd_pmac_daca_init(struct snd_pmac *chip)
  213. {
  214. int i, err;
  215. struct pmac_daca *mix;
  216. #ifdef CONFIG_KMOD
  217. if (current->fs->root)
  218. request_module("i2c-powermac");
  219. #endif /* CONFIG_KMOD */
  220. mix = kzalloc(sizeof(*mix), GFP_KERNEL);
  221. if (! mix)
  222. return -ENOMEM;
  223. chip->mixer_data = mix;
  224. chip->mixer_free = daca_cleanup;
  225. mix->amp_on = 1; /* default on */
  226. mix->i2c.addr = DACA_I2C_ADDR;
  227. mix->i2c.init_client = daca_init_client;
  228. mix->i2c.name = "DACA";
  229. if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
  230. return err;
  231. /*
  232. * build mixers
  233. */
  234. strcpy(chip->card->mixername, "PowerMac DACA");
  235. for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
  236. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip))) < 0)
  237. return err;
  238. }
  239. #ifdef CONFIG_PM
  240. chip->resume = daca_resume;
  241. #endif
  242. return 0;
  243. }