i945-pwm.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (C) 2014 Michał Masłowski <mtjm@mtjm.eu>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /* Try several backlight PWM frequencies and duty cycles to see which
  15. expose the noise on X60 with coreboot.
  16. */
  17. #include <assert.h>
  18. #include <fcntl.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <sys/mman.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <pci/pci.h>
  27. static struct pci_access *pacc;
  28. static uint32_t *map;
  29. static int fd;
  30. #define REGISTER 0x61254
  31. static void
  32. init (void)
  33. {
  34. fd = -1;
  35. /* Find the device. */
  36. struct pci_dev *dev;
  37. pacc = pci_alloc ();
  38. pacc->writeable = 1;
  39. pci_init (pacc);
  40. pci_scan_bus (pacc);
  41. for (dev = pacc->devices; dev != NULL; dev = dev->next)
  42. {
  43. pci_fill_info (dev, PCI_FILL_IDENT | PCI_FILL_BASES);
  44. if (dev->vendor_id != 0x8086 || dev->device_id != 0x27a2)
  45. continue;
  46. break;
  47. }
  48. if (dev == NULL)
  49. {
  50. fprintf (stderr, "Device not found.\n");
  51. goto fail;
  52. }
  53. /* Map a part of its MMIO. */
  54. fd = open ("/dev/mem", O_RDWR);
  55. if (fd == -1)
  56. {
  57. fprintf (stderr, "Cannot open memory. Are you root?\n");
  58. goto fail;
  59. }
  60. map = mmap (NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, dev->base_addr[0] + (REGISTER & (~4095)));
  61. if (map != MAP_FAILED)
  62. return;
  63. else
  64. perror ("mmap failed");
  65. fail:
  66. if (pacc != NULL)
  67. pci_cleanup (pacc);
  68. if (fd != -1)
  69. close (fd);
  70. exit (1);
  71. }
  72. static void
  73. set_pwm (uint16_t freq, uint16_t duty)
  74. {
  75. assert (duty <= freq);
  76. map[((REGISTER - 4) & 4095) >> 2] = 0x80000000;
  77. map[(REGISTER & 4095) >> 2] = ((freq | 1) << 16) | duty;
  78. }
  79. int
  80. main (void)
  81. {
  82. init ();
  83. int exponent = 8;
  84. int diff = 0;
  85. int divisor = 2;
  86. uint16_t freq = 0x61;
  87. uint16_t duties[] = {
  88. #if 1
  89. // i945
  90. 0xf,
  91. 0x13,
  92. 0x19,
  93. 0x1f,
  94. 0x23,
  95. 0x29,
  96. 0x2f,
  97. 0x35,
  98. 0x39,
  99. 0x3f,
  100. 0x45,
  101. 0x4b,
  102. 0x4f,
  103. 0x55,
  104. 0x5b,
  105. 0x61
  106. #else
  107. // gm45
  108. 0x2,
  109. 0x4,
  110. 0x5,
  111. 0x7,
  112. 0x9,
  113. 0xb,
  114. 0xd,
  115. 0x11,
  116. 0x14,
  117. 0x17,
  118. 0x1c,
  119. 0x20,
  120. 0x27,
  121. 0x31,
  122. 0x41,
  123. 0x61,
  124. #endif
  125. };
  126. #if 1
  127. for (unsigned int di = 0; di < sizeof (duties) / sizeof (duties[0]); di++)
  128. {
  129. uint16_t duty = duties[di];
  130. #else
  131. for (uint16_t duty = 0; duty < 0x61; duty++)
  132. {
  133. #endif
  134. #if 0
  135. }
  136. #endif
  137. uint16_t pwm_freq = (freq + diff) << exponent;
  138. pwm_freq += (pwm_freq >> divisor);
  139. //pwm_freq |= 0x33;
  140. uint16_t pwm_duty = (duty + diff) << exponent;
  141. pwm_duty += (pwm_duty >> divisor);
  142. //pwm_duty |= 0x32;
  143. set_pwm (pwm_freq, pwm_duty);
  144. sleep (1);
  145. }
  146. if (pacc != NULL)
  147. pci_cleanup (pacc);
  148. if (map != NULL)
  149. munmap (map, 4096);
  150. if (fd != -1)
  151. close (fd);
  152. return 0;
  153. }