stk1160-ac97.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * STK1160 driver
  3. *
  4. * Copyright (C) 2012 Ezequiel Garcia
  5. * <elezegarcia--a.t--gmail.com>
  6. *
  7. * Based on Easycap driver by R.M. Thomas
  8. * Copyright (C) 2010 R.M. Thomas
  9. * <rmthomas--a.t--sciolus.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <sound/core.h>
  24. #include <sound/initval.h>
  25. #include <sound/ac97_codec.h>
  26. #include "stk1160.h"
  27. #include "stk1160-reg.h"
  28. static struct snd_ac97 *stk1160_ac97;
  29. static void stk1160_write_ac97(struct snd_ac97 *ac97, u16 reg, u16 value)
  30. {
  31. struct stk1160 *dev = ac97->private_data;
  32. /* Set codec register address */
  33. stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
  34. /* Set codec command */
  35. stk1160_write_reg(dev, STK1160_AC97_CMD, value & 0xff);
  36. stk1160_write_reg(dev, STK1160_AC97_CMD + 1, (value & 0xff00) >> 8);
  37. /*
  38. * Set command write bit to initiate write operation.
  39. * The bit will be cleared when transfer is done.
  40. */
  41. stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8c);
  42. }
  43. static u16 stk1160_read_ac97(struct snd_ac97 *ac97, u16 reg)
  44. {
  45. struct stk1160 *dev = ac97->private_data;
  46. u8 vall = 0;
  47. u8 valh = 0;
  48. /* Set codec register address */
  49. stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
  50. /*
  51. * Set command read bit to initiate read operation.
  52. * The bit will be cleared when transfer is done.
  53. */
  54. stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8b);
  55. /* Retrieve register value */
  56. stk1160_read_reg(dev, STK1160_AC97_CMD, &vall);
  57. stk1160_read_reg(dev, STK1160_AC97_CMD + 1, &valh);
  58. return (valh << 8) | vall;
  59. }
  60. static void stk1160_reset_ac97(struct snd_ac97 *ac97)
  61. {
  62. struct stk1160 *dev = ac97->private_data;
  63. /* Two-step reset AC97 interface and hardware codec */
  64. stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x94);
  65. stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x88);
  66. /* Set 16-bit audio data and choose L&R channel*/
  67. stk1160_write_reg(dev, STK1160_AC97CTL_1 + 2, 0x01);
  68. }
  69. static struct snd_ac97_bus_ops stk1160_ac97_ops = {
  70. .read = stk1160_read_ac97,
  71. .write = stk1160_write_ac97,
  72. .reset = stk1160_reset_ac97,
  73. };
  74. int stk1160_ac97_register(struct stk1160 *dev)
  75. {
  76. struct snd_card *card = NULL;
  77. struct snd_ac97_bus *ac97_bus;
  78. struct snd_ac97_template ac97_template;
  79. int rc;
  80. /*
  81. * Just want a card to access ac96 controls,
  82. * the actual capture interface will be handled by snd-usb-audio
  83. */
  84. rc = snd_card_new(dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  85. THIS_MODULE, 0, &card);
  86. if (rc < 0)
  87. return rc;
  88. /* TODO: I'm not sure where should I get these names :-( */
  89. snprintf(card->shortname, sizeof(card->shortname),
  90. "stk1160-mixer");
  91. snprintf(card->longname, sizeof(card->longname),
  92. "stk1160 ac97 codec mixer control");
  93. strlcpy(card->driver, dev->dev->driver->name, sizeof(card->driver));
  94. rc = snd_ac97_bus(card, 0, &stk1160_ac97_ops, NULL, &ac97_bus);
  95. if (rc)
  96. goto err;
  97. /* We must set private_data before calling snd_ac97_mixer */
  98. memset(&ac97_template, 0, sizeof(ac97_template));
  99. ac97_template.private_data = dev;
  100. ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
  101. rc = snd_ac97_mixer(ac97_bus, &ac97_template, &stk1160_ac97);
  102. if (rc)
  103. goto err;
  104. dev->snd_card = card;
  105. rc = snd_card_register(card);
  106. if (rc)
  107. goto err;
  108. return 0;
  109. err:
  110. dev->snd_card = NULL;
  111. snd_card_free(card);
  112. return rc;
  113. }
  114. int stk1160_ac97_unregister(struct stk1160 *dev)
  115. {
  116. struct snd_card *card = dev->snd_card;
  117. /*
  118. * We need to check usb_device,
  119. * because ac97 release attempts to communicate with codec
  120. */
  121. if (card && dev->udev)
  122. snd_card_free(card);
  123. return 0;
  124. }