daca.c 7.1 KB

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