summaryrefslogtreecommitdiff
path: root/chip/lm4/pwm.c
blob: bcb19f49a016c8d2aceb9882de9184d6148f965b (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* PWM control module for Chrome EC */

#include "board.h"
#include "console.h"
#include "gpio.h"
#include "pwm.h"
#include "registers.h"
#include "uart.h"
#include "util.h"

/* Maximum RPM for fan controller */
#define MAX_RPM 0x1fff
/* Max PWM for fan controller */
#define MAX_PWM 0x1ff
/* Scaling factor for requested/actual RPM for CPU fan.  We need this because
 * the fan controller on Blizzard filters tach pulses that are less than 64
 * 15625Hz ticks apart, which works out to ~7000rpm on an unscaled fan.  By
 * telling the controller we actually have twice as many edges per revolution,
 * the controller can handle fans that actually go twice as fast.  See
 * crosbug.com/p/7718. */
#define CPU_FAN_SCALE 2


/* Configures the GPIOs for the fan module. */
static void configure_gpios(void)
{
#ifdef BOARD_link
	/* PK6 alternate function 1 = channel 1 PWM */
	gpio_set_alternate_function(LM4_GPIO_K, 0x40, 1);
	/* PM6:7 alternate function 1 = channel 0 PWM/tach */
	gpio_set_alternate_function(LM4_GPIO_M, 0xc0, 1);
#else
	/* PK6 alternate function 1 = channel 1 PWM */
	gpio_set_alternate_function(LM4_GPIO_K, 0x40, 1);
	/* PN6:7 alternate function 1 = channel 4 PWM/tach */
	gpio_set_alternate_function(LM4_GPIO_N, 0xc0, 1);
#endif
}


int pwm_get_fan_rpm(void)
{
	return (LM4_FAN_FANCST(FAN_CH_CPU) & MAX_RPM) * CPU_FAN_SCALE;
}


int pwm_set_fan_target_rpm(int rpm)
{
	/* Apply fan scaling */
	if (rpm > 0)
		rpm /= CPU_FAN_SCALE;

	/* Treat out-of-range requests as requests for maximum fan speed */
	if (rpm < 0 || rpm > MAX_RPM)
		rpm = MAX_RPM;

	LM4_FAN_FANCMD(FAN_CH_CPU) = rpm;
	return EC_SUCCESS;
}


int pwm_get_keyboard_backlight(void)
{
	return ((LM4_FAN_FANCMD(FAN_CH_KBLIGHT) >> 16) * 100 +
		MAX_PWM / 2) / MAX_PWM;
}


int pwm_set_keyboard_backlight(int percent)
{
	LM4_FAN_FANCMD(FAN_CH_KBLIGHT) = ((percent * MAX_PWM + 50) / 100) << 16;
	return EC_SUCCESS;
}


/*****************************************************************************/
/* Console commands */

static int command_fan_info(int argc, char **argv)
{
	uart_printf("Fan actual speed: %4d rpm\n", pwm_get_fan_rpm());
	uart_printf("    target speed: %4d rpm\n",
		    (LM4_FAN_FANCMD(FAN_CH_CPU) & MAX_RPM) * CPU_FAN_SCALE);
	uart_printf("    duty cycle:   %d%%\n",
		    ((LM4_FAN_FANCMD(FAN_CH_CPU) >> 16)) * 100 / MAX_PWM);
	uart_printf("    status:       %d\n",
		    (LM4_FAN_FANSTS >> (2 * FAN_CH_CPU)) & 0x03);
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(faninfo, command_fan_info);


static int command_fan_set(int argc, char **argv)
{
	int rpm = 0;
	char *e;
	int rv;

	if (argc < 2) {
		uart_puts("Usage: fanset <rpm>\n");
		return EC_ERROR_UNKNOWN;
	}

	rpm = strtoi(argv[1], &e, 0);
	if (*e) {
		uart_puts("Invalid speed\n");
		return EC_ERROR_UNKNOWN;
	}

	uart_printf("Setting fan speed to %d rpm...\n", rpm);

        /* Move the fan to automatic control */
        if (LM4_FAN_FANCH(FAN_CH_CPU) & 0x0001) {
		LM4_FAN_FANCTL &= ~(1 << FAN_CH_CPU);
          LM4_FAN_FANCH(FAN_CH_CPU) &= ~0x0001;
          LM4_FAN_FANCTL |= (1 << FAN_CH_CPU);
        }

	rv = pwm_set_fan_target_rpm(rpm);
	if (rv == EC_SUCCESS)
		uart_printf("Done.\n");
	return rv;
}
DECLARE_CONSOLE_COMMAND(fanset, command_fan_set);


/* TODO: this is a temporary command for debugging tach issues */
static int command_fan_duty(int argc, char **argv)
{
  int d = 0, pwm;
	char *e;

	if (argc < 2) {
		uart_puts("Usage: fanduty <percent>\n");
		return EC_ERROR_UNKNOWN;
	}

	d = strtoi(argv[1], &e, 0);
	if (*e) {
		uart_puts("Invalid duty cycle\n");
		return EC_ERROR_UNKNOWN;
	}

        pwm = (MAX_PWM * d) / 100;
	uart_printf("Setting fan duty cycle to %d%% = 0x%x...\n", d, pwm);

        /* Move the fan to manual control */
        if (!(LM4_FAN_FANCH(FAN_CH_CPU) & 0x0001)) {
		LM4_FAN_FANCTL &= ~(1 << FAN_CH_CPU);
		LM4_FAN_FANCH(FAN_CH_CPU) |= 0x0001;
		LM4_FAN_FANCTL |= (1 << FAN_CH_CPU);
        }

        /* Set the duty cycle */
	LM4_FAN_FANCMD(FAN_CH_CPU) = pwm << 16;

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(fanduty, command_fan_duty);


static int command_kblight(int argc, char **argv)
{
	char *e;
	int rv;
	int i;

	if (argc < 2) {
		uart_printf("Keyboard backlight is at %d%%\n",
			    pwm_get_keyboard_backlight());
		return EC_SUCCESS;
	}

	i = strtoi(argv[1], &e, 0);
	if (*e) {
		uart_puts("Invalid percent\n");
		return EC_ERROR_UNKNOWN;
	}

	uart_printf("Setting keyboard backlight to %d%%...\n", i);
	rv = pwm_set_keyboard_backlight(i);
	if (rv == EC_SUCCESS)
		uart_printf("Done.\n");
	return rv;
}
DECLARE_CONSOLE_COMMAND(kblight, command_kblight);

/*****************************************************************************/
/* Initialization */

int pwm_init(void)
{
	volatile uint32_t scratch  __attribute__((unused));

	/* Enable the fan module and delay a few clocks */
	LM4_SYSTEM_RCGCFAN = 1;
	scratch = LM4_SYSTEM_RCGCFAN;

	/* Configure GPIOs */
	configure_gpios();

	/* Disable all fans */
	LM4_FAN_FANCTL = 0;

	/* Configure CPU fan:
	 * 0x8000 = bit 15     = auto-restart
	 * 0x0000 = bit 14     = slow acceleration
	 * 0x0000 = bits 13:11 = no hysteresis
	 * 0x0000 = bits 10:8  = start period (2<<0) edges
	 * 0x0000 = bits 7:6   = no fast start
	 * 0x0020 = bits 5:4   = average 4 edges when calculating RPM
	 * 0x000c = bits 3:2   = 8 pulses per revolution
	 *                       (see note at top of file)
	 * 0x0000 = bit 0      = automatic control */
	LM4_FAN_FANCH(FAN_CH_CPU) = 0x802c;

	/* Configure keyboard backlight:
	 * 0x0000 = bit 15     = auto-restart
	 * 0x0000 = bit 14     = slow acceleration
	 * 0x0000 = bits 13:11 = no hysteresis
	 * 0x0000 = bits 10:8  = start period (2<<0) edges
	 * 0x0000 = bits 7:6   = no fast start
	 * 0x0000 = bits 5:4   = average 4 edges when calculating RPM
	 * 0x0000 = bits 3:2   = 4 pulses per revolution
	 * 0x0001 = bit 0      = manual control */
	LM4_FAN_FANCH(FAN_CH_KBLIGHT) = 0x0001;

	/* Set initial fan speed to maximum, backlight off */
	pwm_set_fan_target_rpm(-1);
	pwm_set_keyboard_backlight(0);

	/* Enable CPU fan and keyboard backlight */
	LM4_FAN_FANCTL |= (1 << FAN_CH_CPU) | (1 << FAN_CH_KBLIGHT);

	return EC_SUCCESS;
}