oxygen_io.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. u8 status = 0;
  78. /*
  79. * Reading the status register also clears the bits, so we have to save
  80. * the read bits in status.
  81. */
  82. wait_event_timeout(chip->ac97_waitqueue,
  83. ({ status |= oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS);
  84. status & mask; }),
  85. msecs_to_jiffies(1) + 1);
  86. /*
  87. * Check even after a timeout because this function should not require
  88. * the AC'97 interrupt to be enabled.
  89. */
  90. status |= oxygen_read8(chip, OXYGEN_AC97_INTERRUPT_STATUS);
  91. return status & mask ? 0 : -EIO;
  92. }
  93. /*
  94. * About 10% of AC'97 register reads or writes fail to complete, but even those
  95. * where the controller indicates completion aren't guaranteed to have actually
  96. * happened.
  97. *
  98. * It's hard to assign blame to either the controller or the codec because both
  99. * were made by C-Media ...
  100. */
  101. void oxygen_write_ac97(struct oxygen *chip, unsigned int codec,
  102. unsigned int index, u16 data)
  103. {
  104. unsigned int count, succeeded;
  105. u32 reg;
  106. reg = data;
  107. reg |= index << OXYGEN_AC97_REG_ADDR_SHIFT;
  108. reg |= OXYGEN_AC97_REG_DIR_WRITE;
  109. reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT;
  110. succeeded = 0;
  111. for (count = 5; count > 0; --count) {
  112. udelay(5);
  113. oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
  114. /* require two "completed" writes, just to be sure */
  115. if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_WRITE_DONE) >= 0 &&
  116. ++succeeded >= 2)
  117. return;
  118. }
  119. snd_printk(KERN_ERR "AC'97 write timeout\n");
  120. }
  121. EXPORT_SYMBOL(oxygen_write_ac97);
  122. u16 oxygen_read_ac97(struct oxygen *chip, unsigned int codec,
  123. unsigned int index)
  124. {
  125. unsigned int count;
  126. unsigned int last_read = UINT_MAX;
  127. u32 reg;
  128. reg = index << OXYGEN_AC97_REG_ADDR_SHIFT;
  129. reg |= OXYGEN_AC97_REG_DIR_READ;
  130. reg |= codec << OXYGEN_AC97_REG_CODEC_SHIFT;
  131. for (count = 5; count > 0; --count) {
  132. udelay(5);
  133. oxygen_write32(chip, OXYGEN_AC97_REGS, reg);
  134. udelay(10);
  135. if (oxygen_ac97_wait(chip, OXYGEN_AC97_INT_READ_DONE) >= 0) {
  136. u16 value = oxygen_read16(chip, OXYGEN_AC97_REGS);
  137. /* we require two consecutive reads of the same value */
  138. if (value == last_read)
  139. return value;
  140. last_read = value;
  141. /*
  142. * Invert the register value bits to make sure that two
  143. * consecutive unsuccessful reads do not return the same
  144. * value.
  145. */
  146. reg ^= 0xffff;
  147. }
  148. }
  149. snd_printk(KERN_ERR "AC'97 read timeout on codec %u\n", codec);
  150. return 0;
  151. }
  152. EXPORT_SYMBOL(oxygen_read_ac97);
  153. void oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec,
  154. unsigned int index, u16 data, u16 mask)
  155. {
  156. u16 value = oxygen_read_ac97(chip, codec, index);
  157. value &= ~mask;
  158. value |= data & mask;
  159. oxygen_write_ac97(chip, codec, index, value);
  160. }
  161. EXPORT_SYMBOL(oxygen_write_ac97_masked);
  162. void oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data)
  163. {
  164. unsigned int count;
  165. /* should not need more than 7.68 us (24 * 320 ns) */
  166. count = 10;
  167. while ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) & OXYGEN_SPI_BUSY)
  168. && count > 0) {
  169. udelay(1);
  170. --count;
  171. }
  172. oxygen_write8(chip, OXYGEN_SPI_DATA1, data);
  173. oxygen_write8(chip, OXYGEN_SPI_DATA2, data >> 8);
  174. if (control & OXYGEN_SPI_DATA_LENGTH_3)
  175. oxygen_write8(chip, OXYGEN_SPI_DATA3, data >> 16);
  176. oxygen_write8(chip, OXYGEN_SPI_CONTROL, control);
  177. }
  178. EXPORT_SYMBOL(oxygen_write_spi);
  179. void oxygen_write_i2c(struct oxygen *chip, u8 device, u8 map, u8 data)
  180. {
  181. unsigned long timeout;
  182. /* should not need more than about 300 us */
  183. timeout = jiffies + msecs_to_jiffies(1);
  184. do {
  185. if (!(oxygen_read16(chip, OXYGEN_2WIRE_BUS_STATUS)
  186. & OXYGEN_2WIRE_BUSY))
  187. break;
  188. udelay(1);
  189. cond_resched();
  190. } while (time_after_eq(timeout, jiffies));
  191. oxygen_write8(chip, OXYGEN_2WIRE_MAP, map);
  192. oxygen_write8(chip, OXYGEN_2WIRE_DATA, data);
  193. oxygen_write8(chip, OXYGEN_2WIRE_CONTROL,
  194. device | OXYGEN_2WIRE_DIR_WRITE);
  195. }
  196. EXPORT_SYMBOL(oxygen_write_i2c);