summaryrefslogtreecommitdiffstats
path: root/resources/utilities/i945-pwm/i945-pwm.c
blob: e3dc5742dae1e945374896b5a594b3cbb894db1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
  Copyright (C) 2014  Michał Masłowski  <mtjm@mtjm.eu>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Try several backlight PWM frequencies and duty cycles to see which
   expose the noise on X60 with coreboot.
*/

#include <assert.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pci/pci.h>

static struct pci_access *pacc;
static uint32_t *map;
static int fd;

#define REGISTER 0x61254

static void
init (void)
{
  fd = -1;
  /* Find the device. */
  struct pci_dev *dev;
  pacc = pci_alloc ();
  pacc->writeable = 1;
  pci_init (pacc);
  pci_scan_bus (pacc);
  for (dev = pacc->devices; dev != NULL; dev = dev->next)
    {
      pci_fill_info (dev, PCI_FILL_IDENT | PCI_FILL_BASES);
      if (dev->vendor_id != 0x8086 || dev->device_id != 0x27a2)
        continue;
      break;
    }
  if (dev == NULL)
    {
      fprintf (stderr, "Device not found.\n");
      goto fail;
    }
  /* Map a part of its MMIO. */
  fd = open ("/dev/mem", O_RDWR);
  if (fd == -1)
    {
      fprintf (stderr, "Cannot open memory.  Are you root?\n");
      goto fail;
    }
  map = mmap (NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, dev->base_addr[0] + (REGISTER & (~4095)));
  if (map != MAP_FAILED)
    return;
  else
    perror ("mmap failed");
 fail:
  if (pacc != NULL)
    pci_cleanup (pacc);
  if (fd != -1)
    close (fd);
  exit (1);
}

static void
set_pwm (uint16_t freq, uint16_t duty)
{
  assert (duty <= freq);
  map[((REGISTER - 4) & 4095) >> 2] = 0x80000000;
  map[(REGISTER & 4095) >> 2] = ((freq | 1) << 16) | duty;
}

int
main (void)
{
  init ();
  int exponent = 8;
  int diff = 0;
  int divisor = 2;
  uint16_t freq = 0x61;
  uint16_t duties[] = {
#if 1
    // i945
    0xf,
    0x13,
    0x19,
    0x1f,
    0x23,
    0x29,
    0x2f,
    0x35,
    0x39,
    0x3f,
    0x45,
    0x4b,
    0x4f,
    0x55,
    0x5b,
    0x61
#else
    // gm45
    0x2,
    0x4,
    0x5,
    0x7,
    0x9,
    0xb,
    0xd,
    0x11,
    0x14,
    0x17,
    0x1c,
    0x20,
    0x27,
    0x31,
    0x41,
    0x61,
#endif
  };
#if 1
  for (unsigned int di = 0; di < sizeof (duties) / sizeof (duties[0]); di++)
    {
      uint16_t duty = duties[di];
#else
      for (uint16_t duty = 0; duty < 0x61; duty++)
        {
#endif
#if 0
        }
#endif
      uint16_t pwm_freq = (freq + diff) << exponent;
      pwm_freq += (pwm_freq >> divisor);
      //pwm_freq |= 0x33;
      uint16_t pwm_duty = (duty + diff) << exponent;
      pwm_duty += (pwm_duty >> divisor);
      //pwm_duty |= 0x32;
      set_pwm (pwm_freq, pwm_duty);
      sleep (1);
    }
  if (pacc != NULL)
    pci_cleanup (pacc);
  if (map != NULL)
    munmap (map, 4096);
  if (fd != -1)
    close (fd);
  return 0;
}