daca.c 7.1 KB

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