daca.c 6.9 KB

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