oxygen_io.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * C-Media CMI8788 driver - helper functions
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2.
  9. *
  10. * This driver is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this driver; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <sound/driver.h>
  20. #include <linux/delay.h>
  21. #include <linux/sched.h>
  22. #include <sound/core.h>
  23. #include <asm/io.h>
  24. #include "oxygen.h"
  25. u8 oxygen_read8(struct oxygen *chip, unsigned int reg)
  26. {
  27. return inb(chip->addr + reg);
  28. }
  29. EXPORT_SYMBOL(oxygen_read8);
  30. u16 oxygen_read16(struct oxygen *chip, unsigned int reg)
  31. {
  32. return inw(chip->addr + reg);
  33. }
  34. EXPORT_SYMBOL(oxygen_read16);
  35. u32 oxygen_read32(struct oxygen *chip, unsigned int reg)
  36. {
  37. return inl(chip->addr + reg);
  38. }
  39. EXPORT_SYMBOL(oxygen_read32);
  40. void oxygen_write8(struct oxygen *chip, unsigned int reg, u8 value)
  41. {
  42. outb(value, chip->addr + reg);
  43. }
  44. EXPORT_SYMBOL(oxygen_write8);
  45. void oxygen_write16(struct oxygen *chip, unsigned int reg, u16 value)
  46. {
  47. outw(value, chip->addr + reg);
  48. }
  49. EXPORT_SYMBOL(oxygen_write16);
  50. void oxygen_write32(struct oxygen *chip, unsigned int reg, u32 value)
  51. {
  52. outl(value, chip->addr + reg);
  53. }
  54. EXPORT_SYMBOL(oxygen_write32);
  55. void oxygen_write8_masked(struct oxygen *chip, unsigned int reg,
  56. u8 value, u8 mask)
  57. {
  58. u8 tmp = inb(chip->addr + reg);
  59. outb((tmp & ~mask) | (value & mask), chip->addr + reg);
  60. }
  61. EXPORT_SYMBOL(oxygen_write8_masked);
  62. void oxygen_write16_masked(struct oxygen *chip, unsigned int reg,
  63. u16 value, u16 mask)
  64. {
  65. u16 tmp = inw(chip->addr + reg);
  66. outw((tmp & ~mask) | (value & mask), chip->addr + reg);
  67. }
  68. EXPORT_SYMBOL(oxygen_write16_masked);
  69. void oxygen_write32_masked(struct oxygen *chip, unsigned int reg,
  70. u32 value, u32 mask)
  71. {
  72. u32 tmp = inl(chip->addr + reg);
  73. outl((tmp & ~mask) | (value & mask), chip->addr + reg);
  74. }
  75. EXPORT_SYMBOL(oxygen_write32_masked);
  76. static int oxygen_ac97_wait(struct oxygen *chip, unsigned int mask)
  77. {
  78. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  79. do {
  80. udelay(5);
  81. cond_resched();
  82. if (oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS) & mask)
  83. return 0;
  84. } while (time_after_eq(timeout, jiffies));
  85. return -EIO;
  86. }
  87. /*
  88. * About 10% of AC'97 register reads or writes fail to complete, but even those
  89. * where the controller indicates completion aren't guaranteed to have actually
  90. * happened.
  91. *
  92. * It's hard to assign blame to either the controller or the codec because both
  93. * were made by C-Media ...
  94. */
  95. void oxygen_write_ac97(struct oxygen *chip, unsigned int codec,
  96. unsigned int index, u16 data)
  97. {
  98. unsigned int count, succeeded;
  99. u32 reg;
  100. reg = data;
  101. reg |= index << OXYGEN_AC97_REG_ADDR_SHIFT;
  102. reg |= OXYGEN_AC97_REG_DIR_WRITE;
  103. reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT;
  104. succeeded = 0;
  105. for (count = 5; count > 0; --count) {
  106. udelay(5);
  107. oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
  108. /* require two "completed" writes, just to be sure */
  109. if (oxygen_ac97_wait(chip, OXYGEN_AC97_WRITE_COMPLETE) >= 0 &&
  110. ++succeeded >= 2)
  111. return;
  112. }
  113. snd_printk(KERN_ERR "AC'97 write timeout\n");
  114. }
  115. EXPORT_SYMBOL(oxygen_write_ac97);
  116. u16 oxygen_read_ac97(struct oxygen *chip, unsigned int codec,
  117. unsigned int index)
  118. {
  119. unsigned int count;
  120. unsigned int last_read = UINT_MAX;
  121. u32 reg;
  122. reg = index << OXYGEN_AC97_REG_ADDR_SHIFT;
  123. reg |= OXYGEN_AC97_REG_DIR_READ;
  124. reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT;
  125. for (count = 5; count > 0; --count) {
  126. udelay(5);
  127. oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
  128. udelay(10);
  129. if (oxygen_ac97_wait(chip, OXYGEN_AC97_READ_COMPLETE) >= 0) {
  130. u16 value = oxygen_read16(chip, OXYGEN_AC97_REGS);
  131. /* we require two consecutive reads of the same value */
  132. if (value == last_read)
  133. return value;
  134. last_read = value;
  135. /*
  136. * Invert the register value bits to make sure that two
  137. * consecutive unsuccessful reads do not return the same
  138. * value.
  139. */
  140. reg ^= 0xffff;
  141. }
  142. }
  143. snd_printk(KERN_ERR "AC'97 read timeout on codec %u\n", codec);
  144. return 0;
  145. }
  146. EXPORT_SYMBOL(oxygen_read_ac97);
  147. void oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec,
  148. unsigned int index, u16 data, u16 mask)
  149. {
  150. u16 value = oxygen_read_ac97(chip, codec, index);
  151. value &= ~mask;
  152. value |= data & mask;
  153. oxygen_write_ac97(chip, codec, index, value);
  154. }
  155. EXPORT_SYMBOL(oxygen_write_ac97_masked);
  156. void oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data)
  157. {
  158. unsigned int count;
  159. /* should not need more than 3.84 us (24 * 160 ns) */
  160. count = 10;
  161. while ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) & OXYGEN_SPI_BUSY)
  162. && count > 0) {
  163. udelay(1);
  164. --count;
  165. }
  166. spin_lock_irq(&chip->reg_lock);
  167. oxygen_write8(chip, OXYGEN_SPI_DATA1, data);
  168. oxygen_write8(chip, OXYGEN_SPI_DATA2, data >> 8);
  169. if (control & OXYGEN_SPI_DATA_LENGTH_3)
  170. oxygen_write8(chip, OXYGEN_SPI_DATA3, data >> 16);
  171. oxygen_write8(chip, OXYGEN_SPI_CONTROL, control);
  172. spin_unlock_irq(&chip->reg_lock);
  173. }
  174. EXPORT_SYMBOL(oxygen_write_spi);