oxygen_io.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <linux/delay.h>
  20. #include <linux/sched.h>
  21. #include <sound/core.h>
  22. #include <asm/io.h>
  23. #include "oxygen.h"
  24. u8 oxygen_read8(struct oxygen *chip, unsigned int reg)
  25. {
  26. return inb(chip->addr + reg);
  27. }
  28. EXPORT_SYMBOL(oxygen_read8);
  29. u16 oxygen_read16(struct oxygen *chip, unsigned int reg)
  30. {
  31. return inw(chip->addr + reg);
  32. }
  33. EXPORT_SYMBOL(oxygen_read16);
  34. u32 oxygen_read32(struct oxygen *chip, unsigned int reg)
  35. {
  36. return inl(chip->addr + reg);
  37. }
  38. EXPORT_SYMBOL(oxygen_read32);
  39. void oxygen_write8(struct oxygen *chip, unsigned int reg, u8 value)
  40. {
  41. outb(value, chip->addr + reg);
  42. }
  43. EXPORT_SYMBOL(oxygen_write8);
  44. void oxygen_write16(struct oxygen *chip, unsigned int reg, u16 value)
  45. {
  46. outw(value, chip->addr + reg);
  47. }
  48. EXPORT_SYMBOL(oxygen_write16);
  49. void oxygen_write32(struct oxygen *chip, unsigned int reg, u32 value)
  50. {
  51. outl(value, chip->addr + reg);
  52. }
  53. EXPORT_SYMBOL(oxygen_write32);
  54. void oxygen_write8_masked(struct oxygen *chip, unsigned int reg,
  55. u8 value, u8 mask)
  56. {
  57. u8 tmp = inb(chip->addr + reg);
  58. outb((tmp & ~mask) | (value & mask), chip->addr + reg);
  59. }
  60. EXPORT_SYMBOL(oxygen_write8_masked);
  61. void oxygen_write16_masked(struct oxygen *chip, unsigned int reg,
  62. u16 value, u16 mask)
  63. {
  64. u16 tmp = inw(chip->addr + reg);
  65. outw((tmp & ~mask) | (value & mask), chip->addr + reg);
  66. }
  67. EXPORT_SYMBOL(oxygen_write16_masked);
  68. void oxygen_write32_masked(struct oxygen *chip, unsigned int reg,
  69. u32 value, u32 mask)
  70. {
  71. u32 tmp = inl(chip->addr + reg);
  72. outl((tmp & ~mask) | (value & mask), chip->addr + reg);
  73. }
  74. EXPORT_SYMBOL(oxygen_write32_masked);
  75. static int oxygen_ac97_wait(struct oxygen *chip, unsigned int mask)
  76. {
  77. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  78. do {
  79. udelay(5);
  80. cond_resched();
  81. if (oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS) & mask)
  82. return 0;
  83. } while (time_after_eq(timeout, jiffies));
  84. return -EIO;
  85. }
  86. /*
  87. * About 10% of AC'97 register reads or writes fail to complete, but even those
  88. * where the controller indicates completion aren't guaranteed to have actually
  89. * happened.
  90. *
  91. * It's hard to assign blame to either the controller or the codec because both
  92. * were made by C-Media ...
  93. */
  94. void oxygen_write_ac97(struct oxygen *chip, unsigned int codec,
  95. unsigned int index, u16 data)
  96. {
  97. unsigned int count, succeeded;
  98. u32 reg;
  99. reg = data;
  100. reg |= index << OXYGEN_AC97_REG_ADDR_SHIFT;
  101. reg |= OXYGEN_AC97_REG_DIR_WRITE;
  102. reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT;
  103. succeeded = 0;
  104. for (count = 5; count > 0; --count) {
  105. udelay(5);
  106. oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
  107. /* require two "completed" writes, just to be sure */
  108. if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_WRITE_DONE) >= 0 &&
  109. ++succeeded >= 2)
  110. return;
  111. }
  112. snd_printk(KERN_ERR "AC'97 write timeout\n");
  113. }
  114. EXPORT_SYMBOL(oxygen_write_ac97);
  115. u16 oxygen_read_ac97(struct oxygen *chip, unsigned int codec,
  116. unsigned int index)
  117. {
  118. unsigned int count;
  119. unsigned int last_read = UINT_MAX;
  120. u32 reg;
  121. reg = index << OXYGEN_AC97_REG_ADDR_SHIFT;
  122. reg |= OXYGEN_AC97_REG_DIR_READ;
  123. reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT;
  124. for (count = 5; count > 0; --count) {
  125. udelay(5);
  126. oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
  127. udelay(10);
  128. if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_READ_DONE) >= 0) {
  129. u16 value = oxygen_read16(chip, OXYGEN_AC97_REGS);
  130. /* we require two consecutive reads of the same value */
  131. if (value == last_read)
  132. return value;
  133. last_read = value;
  134. /*
  135. * Invert the register value bits to make sure that two
  136. * consecutive unsuccessful reads do not return the same
  137. * value.
  138. */
  139. reg ^= 0xffff;
  140. }
  141. }
  142. snd_printk(KERN_ERR "AC'97 read timeout on codec %u\n", codec);
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(oxygen_read_ac97);
  146. void oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec,
  147. unsigned int index, u16 data, u16 mask)
  148. {
  149. u16 value = oxygen_read_ac97(chip, codec, index);
  150. value &= ~mask;
  151. value |= data & mask;
  152. oxygen_write_ac97(chip, codec, index, value);
  153. }
  154. EXPORT_SYMBOL(oxygen_write_ac97_masked);
  155. void oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data)
  156. {
  157. unsigned int count;
  158. /* should not need more than 7.68 us (24 * 320 ns) */
  159. count = 10;
  160. while ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) & OXYGEN_SPI_BUSY)
  161. && count > 0) {
  162. udelay(1);
  163. --count;
  164. }
  165. spin_lock_irq(&chip->reg_lock);
  166. oxygen_write8(chip, OXYGEN_SPI_DATA1, data);
  167. oxygen_write8(chip, OXYGEN_SPI_DATA2, data >> 8);
  168. if (control & OXYGEN_SPI_DATA_LENGTH_3)
  169. oxygen_write8(chip, OXYGEN_SPI_DATA3, data >> 16);
  170. oxygen_write8(chip, OXYGEN_SPI_CONTROL, control);
  171. spin_unlock_irq(&chip->reg_lock);
  172. }
  173. EXPORT_SYMBOL(oxygen_write_spi);