stk1160-ac97.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * STK1160 driver
  3. *
  4. * Copyright (C) 2012 Ezequiel Garcia
  5. * <elezegarcia--a.t--gmail.com>
  6. *
  7. * Copyright (C) 2016 Marcel Hasler
  8. * <mahasler--a.t--gmail.com>
  9. *
  10. * Based on Easycap driver by R.M. Thomas
  11. * Copyright (C) 2010 R.M. Thomas
  12. * <rmthomas--a.t--sciolus.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. */
  25. #include <linux/delay.h>
  26. #include "stk1160.h"
  27. #include "stk1160-reg.h"
  28. static int stk1160_ac97_wait_transfer_complete(struct stk1160 *dev)
  29. {
  30. unsigned long timeout = jiffies + msecs_to_jiffies(STK1160_AC97_TIMEOUT);
  31. u8 value;
  32. /* Wait for AC97 transfer to complete */
  33. while (time_is_after_jiffies(timeout)) {
  34. stk1160_read_reg(dev, STK1160_AC97CTL_0, &value);
  35. if (!(value & (STK1160_AC97CTL_0_CR | STK1160_AC97CTL_0_CW)))
  36. return 0;
  37. usleep_range(50, 100);
  38. }
  39. stk1160_err("AC97 transfer took too long, this should never happen!");
  40. return -EBUSY;
  41. }
  42. static void stk1160_write_ac97(struct stk1160 *dev, u16 reg, u16 value)
  43. {
  44. /* Set codec register address */
  45. stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
  46. /* Set codec command */
  47. stk1160_write_reg(dev, STK1160_AC97_CMD, value & 0xff);
  48. stk1160_write_reg(dev, STK1160_AC97_CMD + 1, (value & 0xff00) >> 8);
  49. /* Set command write bit to initiate write operation */
  50. stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8c);
  51. /* Wait for command write bit to be cleared */
  52. stk1160_ac97_wait_transfer_complete(dev);
  53. }
  54. #ifdef DEBUG
  55. static u16 stk1160_read_ac97(struct stk1160 *dev, u16 reg)
  56. {
  57. u8 vall = 0;
  58. u8 valh = 0;
  59. /* Set codec register address */
  60. stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
  61. /* Set command read bit to initiate read operation */
  62. stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8b);
  63. /* Wait for command read bit to be cleared */
  64. if (stk1160_ac97_wait_transfer_complete(dev) < 0)
  65. return 0;
  66. /* Retrieve register value */
  67. stk1160_read_reg(dev, STK1160_AC97_CMD, &vall);
  68. stk1160_read_reg(dev, STK1160_AC97_CMD + 1, &valh);
  69. return (valh << 8) | vall;
  70. }
  71. void stk1160_ac97_dump_regs(struct stk1160 *dev)
  72. {
  73. u16 value;
  74. value = stk1160_read_ac97(dev, 0x12); /* CD volume */
  75. stk1160_dbg("0x12 == 0x%04x", value);
  76. value = stk1160_read_ac97(dev, 0x10); /* Line-in volume */
  77. stk1160_dbg("0x10 == 0x%04x", value);
  78. value = stk1160_read_ac97(dev, 0x0e); /* MIC volume (mono) */
  79. stk1160_dbg("0x0e == 0x%04x", value);
  80. value = stk1160_read_ac97(dev, 0x16); /* Aux volume */
  81. stk1160_dbg("0x16 == 0x%04x", value);
  82. value = stk1160_read_ac97(dev, 0x1a); /* Record select */
  83. stk1160_dbg("0x1a == 0x%04x", value);
  84. value = stk1160_read_ac97(dev, 0x02); /* Master volume */
  85. stk1160_dbg("0x02 == 0x%04x", value);
  86. value = stk1160_read_ac97(dev, 0x1c); /* Record gain */
  87. stk1160_dbg("0x1c == 0x%04x", value);
  88. }
  89. #endif
  90. static int stk1160_has_audio(struct stk1160 *dev)
  91. {
  92. u8 value;
  93. stk1160_read_reg(dev, STK1160_POSV_L, &value);
  94. return !(value & STK1160_POSV_L_ACDOUT);
  95. }
  96. static int stk1160_has_ac97(struct stk1160 *dev)
  97. {
  98. u8 value;
  99. stk1160_read_reg(dev, STK1160_POSV_L, &value);
  100. return !(value & STK1160_POSV_L_ACSYNC);
  101. }
  102. void stk1160_ac97_setup(struct stk1160 *dev)
  103. {
  104. if (!stk1160_has_audio(dev)) {
  105. stk1160_info("Device doesn't support audio, skipping AC97 setup.");
  106. return;
  107. }
  108. if (!stk1160_has_ac97(dev)) {
  109. stk1160_info("Device uses internal 8-bit ADC, skipping AC97 setup.");
  110. return;
  111. }
  112. /* Two-step reset AC97 interface and hardware codec */
  113. stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x94);
  114. stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8c);
  115. /* Set 16-bit audio data and choose L&R channel*/
  116. stk1160_write_reg(dev, STK1160_AC97CTL_1 + 2, 0x01);
  117. stk1160_write_reg(dev, STK1160_AC97CTL_1 + 3, 0x00);
  118. /* Setup channels */
  119. stk1160_write_ac97(dev, 0x12, 0x8808); /* CD volume */
  120. stk1160_write_ac97(dev, 0x10, 0x0808); /* Line-in volume */
  121. stk1160_write_ac97(dev, 0x0e, 0x0008); /* MIC volume (mono) */
  122. stk1160_write_ac97(dev, 0x16, 0x0808); /* Aux volume */
  123. stk1160_write_ac97(dev, 0x1a, 0x0404); /* Record select */
  124. stk1160_write_ac97(dev, 0x02, 0x0000); /* Master volume */
  125. stk1160_write_ac97(dev, 0x1c, 0x0808); /* Record gain */
  126. #ifdef DEBUG
  127. stk1160_ac97_dump_regs(dev);
  128. #endif
  129. }