daca.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. unsigned int vol[2];
  133. int change;
  134. if (! (mix = chip->mixer_data))
  135. return -ENODEV;
  136. vol[0] = ucontrol->value.integer.value[0];
  137. vol[1] = ucontrol->value.integer.value[1];
  138. if (vol[0] > DACA_VOL_MAX || vol[1] > DACA_VOL_MAX)
  139. return -EINVAL;
  140. change = mix->left_vol != vol[0] ||
  141. mix->right_vol != vol[1];
  142. if (change) {
  143. mix->left_vol = vol[0];
  144. mix->right_vol = vol[1];
  145. daca_set_volume(mix);
  146. }
  147. return change;
  148. }
  149. /* amplifier switch */
  150. #define daca_info_amp daca_info_deemphasis
  151. static int daca_get_amp(struct snd_kcontrol *kcontrol,
  152. struct snd_ctl_elem_value *ucontrol)
  153. {
  154. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  155. struct pmac_daca *mix;
  156. if (! (mix = chip->mixer_data))
  157. return -ENODEV;
  158. ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
  159. return 0;
  160. }
  161. static int daca_put_amp(struct snd_kcontrol *kcontrol,
  162. struct snd_ctl_elem_value *ucontrol)
  163. {
  164. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  165. struct pmac_daca *mix;
  166. int change;
  167. if (! (mix = chip->mixer_data))
  168. return -ENODEV;
  169. change = mix->amp_on != ucontrol->value.integer.value[0];
  170. if (change) {
  171. mix->amp_on = !!ucontrol->value.integer.value[0];
  172. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
  173. mix->amp_on ? 0x05 : 0x04);
  174. }
  175. return change;
  176. }
  177. static struct snd_kcontrol_new daca_mixers[] = {
  178. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  179. .name = "Deemphasis Switch",
  180. .info = daca_info_deemphasis,
  181. .get = daca_get_deemphasis,
  182. .put = daca_put_deemphasis
  183. },
  184. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  185. .name = "Master Playback Volume",
  186. .info = daca_info_volume,
  187. .get = daca_get_volume,
  188. .put = daca_put_volume
  189. },
  190. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  191. .name = "Power Amplifier Switch",
  192. .info = daca_info_amp,
  193. .get = daca_get_amp,
  194. .put = daca_put_amp
  195. },
  196. };
  197. #ifdef CONFIG_PM
  198. static void daca_resume(struct snd_pmac *chip)
  199. {
  200. struct pmac_daca *mix = chip->mixer_data;
  201. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_SR, 0x08);
  202. i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
  203. mix->amp_on ? 0x05 : 0x04);
  204. daca_set_volume(mix);
  205. }
  206. #endif /* CONFIG_PM */
  207. static void daca_cleanup(struct snd_pmac *chip)
  208. {
  209. struct pmac_daca *mix = chip->mixer_data;
  210. if (! mix)
  211. return;
  212. snd_pmac_keywest_cleanup(&mix->i2c);
  213. kfree(mix);
  214. chip->mixer_data = NULL;
  215. }
  216. /* exported */
  217. int __init snd_pmac_daca_init(struct snd_pmac *chip)
  218. {
  219. int i, err;
  220. struct pmac_daca *mix;
  221. #ifdef CONFIG_KMOD
  222. if (current->fs->root)
  223. request_module("i2c-powermac");
  224. #endif /* CONFIG_KMOD */
  225. mix = kzalloc(sizeof(*mix), GFP_KERNEL);
  226. if (! mix)
  227. return -ENOMEM;
  228. chip->mixer_data = mix;
  229. chip->mixer_free = daca_cleanup;
  230. mix->amp_on = 1; /* default on */
  231. mix->i2c.addr = DACA_I2C_ADDR;
  232. mix->i2c.init_client = daca_init_client;
  233. mix->i2c.name = "DACA";
  234. if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
  235. return err;
  236. /*
  237. * build mixers
  238. */
  239. strcpy(chip->card->mixername, "PowerMac DACA");
  240. for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
  241. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip))) < 0)
  242. return err;
  243. }
  244. #ifdef CONFIG_PM
  245. chip->resume = daca_resume;
  246. #endif
  247. return 0;
  248. }