blob: 7fbb31b4e5a45f2cfc89f45b5b45f2057d33d712 [file] [log] [blame]
Nico Huberffea2372017-08-24 23:30:44 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2017 secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program 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
16#include <delay.h>
17#include <timer.h>
18#include <console/console.h>
19#include <device/device.h>
20#include <device/i2c_bus.h>
21
22#include "lm96000.h"
23#include "chip.h"
24
25static inline int lm96000_read(struct device *const dev, const u8 reg)
26{
27 return i2c_dev_readb_at(dev, reg);
28}
29
30static inline int lm96000_write(struct device *const dev,
31 const u8 reg, const u8 value)
32{
33 return i2c_dev_writeb_at(dev, reg, value);
34}
35
36static inline int lm96000_update(struct device *const dev, const u8 reg,
37 const u8 clear_mask, const u8 set_mask)
38{
39 const int val = i2c_dev_readb_at(dev, reg);
40 if (val < 0)
41 return val;
42 return i2c_dev_writeb_at(dev, reg, (val & ~clear_mask) | set_mask);
43}
44
45static const unsigned int ref_mv[] = { 2500, 2250, 3300, 5000, 12000 };
46
47static u8 lm96000_to_low_limit(const enum lm96000_vin ref, const u16 limit)
48{
49 const unsigned int reg =
50 (unsigned int)limit * 0xc0 / ref_mv[ref];
51 return reg < 0xff ? reg : 0xff;
52}
53
54static u8 lm96000_to_high_limit(const enum lm96000_vin ref, const u16 limit)
55{
56 const unsigned int reg =
57 DIV_ROUND_UP((unsigned int)limit * 0xc0, ref_mv[ref]);
58 return reg < 0xff ? reg : 0xff;
59}
60
61static void lm96000_set_vin_limits(struct device *const dev,
62 const struct drivers_i2c_lm96000_config *const config)
63{
64 unsigned int i;
65
66 for (i = 0; i < LM96000_VIN_CNT; ++i) {
67 lm96000_write(dev, LM96000_VIN_LOW_LIMIT(i),
68 lm96000_to_low_limit(i, config->vin[i].low));
69 if (config->vin[i].high > config->vin[i].low)
70 lm96000_write(dev, LM96000_VIN_HIGH_LIMIT(i),
71 lm96000_to_high_limit(i, config->vin[i].high));
72 else
73 lm96000_write(dev, LM96000_VIN_HIGH_LIMIT(i), 0xff);
74 }
75}
76
77static void lm96000_set_temp_limits(struct device *const dev,
78 const struct drivers_i2c_lm96000_config *const config)
79{
80 unsigned int i;
81
82 for (i = 0; i < LM96000_TEMP_IN_CNT; ++i) {
83 lm96000_write(dev, LM96000_TEMP_LOW_LIMIT(i),
84 config->temp_in[i].low);
85 if (config->temp_in[i].high > config->temp_in[i].low)
86 lm96000_write(dev, LM96000_TEMP_HIGH_LIMIT(i),
87 config->temp_in[i].high);
88 else
89 lm96000_write(dev, LM96000_TEMP_HIGH_LIMIT(i), 0x7f);
90 }
91}
92
93static u16 lm96000_rpm_to_tach(const u16 rpm)
94{
95 return rpm ? (60 * 90000 / rpm) & 0xfffc : 0xfffc;
96}
97
98static void lm96000_set_fan_limits(struct device *const dev,
99 const struct drivers_i2c_lm96000_config *const config)
100{
101 unsigned int i;
102
103 for (i = 0; i < LM96000_FAN_IN_CNT; ++i) {
104 const u16 tach = lm96000_rpm_to_tach(config->fan_in[i].low);
105 lm96000_write(dev, LM96000_FAN_LOW_LIMIT(i), tach & 0xff);
106 lm96000_write(dev, LM96000_FAN_LOW_LIMIT(i) + 1, tach >> 8);
107 }
108}
109
110static u8 lm96000_to_duty(const u8 duty_cycle)
111{
112 return duty_cycle * 255 / 100;
113}
114
115static void lm96000_configure_pwm(struct device *const dev,
116 const unsigned int fan,
117 const struct lm96000_fan_config *const config)
118{
119 lm96000_update(dev, LM96000_FAN_CFG(fan),
120 LM96000_FAN_CFG_MODE_MASK | LM96000_FAN_CFG_PWM_INVERT |
121 LM96000_FAN_CFG_SPINUP_MASK,
122 ((config->mode << LM96000_FAN_CFG_MODE_SHIFT)
123 & LM96000_FAN_CFG_MODE_MASK) |
124 (config->invert ? LM96000_FAN_CFG_PWM_INVERT : 0) |
125 config->spinup);
126 lm96000_update(dev, LM96000_FAN_FREQ(fan),
127 LM96000_FAN_FREQ_MASK, config->freq);
128 lm96000_update(dev, LM96000_TACH_MONITOR_MODE,
129 LM96000_TACH_MODE_FAN_MASK(fan),
130 config->freq <= LM96000_PWM_94HZ
131 ? config->tach << LM96000_TACH_MODE_FAN_SHIFT(fan) : 0);
132
133 switch (config->mode) {
134 case LM96000_FAN_ZONE_1_AUTO:
135 case LM96000_FAN_ZONE_2_AUTO:
136 case LM96000_FAN_ZONE_3_AUTO:
137 case LM96000_FAN_HOTTEST_23:
138 case LM96000_FAN_HOTTEST_123:
139 lm96000_write(dev, LM96000_FAN_MIN_PWM(fan),
140 lm96000_to_duty(config->min_duty));
141 break;
142 case LM96000_FAN_MANUAL:
143 lm96000_write(dev, LM96000_FAN_DUTY(fan),
144 lm96000_to_duty(config->duty_cycle));
145 break;
146 default:
147 break;
148 }
149}
150
151static void lm96000_configure_temp_zone(struct device *const dev,
152 const unsigned int zone,
153 const struct lm96000_temp_zone *const config)
154{
155 static const u8 temp_range[] =
156 { 2, 3, 3, 4, 5, 7, 8, 10, 13, 16, 20, 27, 32, 40, 53, 80 };
157 unsigned int i;
158
Nico Huberb56fcfe2019-08-06 18:05:50 +0200159 /* find longest range that starts from `low_temp` */
Nico Huberffea2372017-08-24 23:30:44 +0200160 for (i = ARRAY_SIZE(temp_range) - 1; i > 0; --i) {
Nico Huberb56fcfe2019-08-06 18:05:50 +0200161 if (config->low_temp + temp_range[i] <= config->target_temp)
Nico Huberffea2372017-08-24 23:30:44 +0200162 break;
163 }
164
165 lm96000_update(dev, LM96000_ZONE_RANGE(zone),
166 LM96000_ZONE_RANGE_MASK, i << LM96000_ZONE_RANGE_SHIFT);
167 lm96000_write(dev, LM96000_ZONE_TEMP_LOW(zone),
168 config->target_temp >= temp_range[i]
169 ? config->target_temp - temp_range[i]
170 : 0);
171 lm96000_write(dev, LM96000_ZONE_TEMP_PANIC(zone),
172 config->panic_temp ? config->panic_temp : 100);
Nico Huberb56fcfe2019-08-06 18:05:50 +0200173 lm96000_update(dev, LM96000_ZONE_SMOOTH(zone),
174 LM96000_ZONE_SMOOTH_MASK(zone),
175 LM96000_ZONE_SMOOTH_EN(zone) | 0); /* 0: 35s */
Nico Huberffea2372017-08-24 23:30:44 +0200176 lm96000_update(dev, LM96000_FAN_MIN_OFF,
177 LM96000_FAN_MIN(zone),
178 config->min_off ? LM96000_FAN_MIN(zone) : 0);
Nico Huberb56fcfe2019-08-06 18:05:50 +0200179 lm96000_update(dev, LM96000_ZONE_HYSTERESIS(zone),
180 LM96000_ZONE_HYST_MASK(zone),
181 config->hysteresis << LM96000_ZONE_HYST_SHIFT(zone)
182 & LM96000_ZONE_HYST_MASK(zone));
Nico Huberffea2372017-08-24 23:30:44 +0200183}
184
185static void lm96000_init(struct device *const dev)
186{
187 const struct drivers_i2c_lm96000_config *const config = dev->chip_info;
Nico Huber9a940bf2019-05-20 14:13:12 +0200188 unsigned int i;
189 int lm_config;
Nico Huberffea2372017-08-24 23:30:44 +0200190 struct stopwatch sw;
191
192 printk(BIOS_DEBUG, "lm96000: Initialization hardware monitoring.\n");
193
194 stopwatch_init_msecs_expire(&sw, 1000);
195 lm_config = lm96000_read(dev, LM96000_CONFIG);
Nico Huber9a940bf2019-05-20 14:13:12 +0200196 while ((lm_config < 0 || !((unsigned int)lm_config & LM96000_READY))) {
Nico Huberffea2372017-08-24 23:30:44 +0200197 mdelay(1);
198 lm_config = lm96000_read(dev, LM96000_CONFIG);
199 if (stopwatch_expired(&sw))
200 break;
201 }
Nico Huber9a940bf2019-05-20 14:13:12 +0200202 if (lm_config < 0 || !((unsigned int)lm_config & LM96000_READY)) {
Nico Huberffea2372017-08-24 23:30:44 +0200203 printk(BIOS_INFO, "lm96000: Not ready after 1s.\n");
204 return;
205 }
206
207 lm96000_set_vin_limits(dev, config);
208 lm96000_set_temp_limits(dev, config);
209 lm96000_set_fan_limits(dev, config);
210 for (i = 0; i < LM96000_PWM_CTL_CNT; ++i) {
211 if (config->fan[i].mode != LM96000_FAN_IGNORE)
212 lm96000_configure_pwm(dev, i, config->fan + i);
213 }
214 for (i = 0; i < LM96000_TEMP_ZONE_CNT; ++i)
215 lm96000_configure_temp_zone(dev, i, config->zone + i);
216 lm96000_update(dev, LM96000_CONFIG, 0, LM96000_START);
217}
218
219static struct device_operations lm96000_ops = {
220 .read_resources = DEVICE_NOOP,
221 .set_resources = DEVICE_NOOP,
222 .enable_resources = DEVICE_NOOP,
223 .init = lm96000_init,
224};
225
226static void lm96000_enable(struct device *const dev)
227{
228 dev->ops = &lm96000_ops;
229}
230
231struct chip_operations drivers_i2c_lm96000_ops = {
232 CHIP_NAME("LM96000")
233 .enable_dev = lm96000_enable
234};