1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/bitfield.h>
5 #include <linux/hwmon.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/mod_devicetable.h>
9 #include <linux/platform_device.h>
10 #include <linux/polynomial.h>
11 #include <linux/regmap.h>
14 * The original translation formulae of the temperature (in degrees of Celsius)
17 * T = -3.4627e-11*(N^4) + 1.1023e-7*(N^3) + -1.9165e-4*(N^2) +
18 * 3.0604e-1*(N^1) + -5.6197e1
20 * where [-56.197, 136.402]C and N = [0, 1023].
22 * They must be accordingly altered to be suitable for the integer arithmetics.
23 * The technique is called 'factor redistribution', which just makes sure the
24 * multiplications and divisions are made so to have a result of the operations
25 * within the integer numbers limit. In addition we need to translate the
26 * formulae to accept millidegrees of Celsius. Here what it looks like after
29 * T = -34627e-12*(N^4) + 110230e-9*(N^3) + -191650e-6*(N^2) +
30 * 306040e-3*(N^1) + -56197
32 * where T = [-56197, 136402]mC and N = [0, 1023].
35 static const struct polynomial poly_N_to_temp = {
39 {2, -191650, 1000, 1},
45 #define PVT_SENSOR_CTRL 0x0 /* unused */
46 #define PVT_SENSOR_CFG 0x4
47 #define SENSOR_CFG_CLK_CFG GENMASK(27, 20)
48 #define SENSOR_CFG_TRIM_VAL GENMASK(13, 9)
49 #define SENSOR_CFG_SAMPLE_ENA BIT(8)
50 #define SENSOR_CFG_START_CAPTURE BIT(7)
51 #define SENSOR_CFG_CONTINIOUS_MODE BIT(6)
52 #define SENSOR_CFG_PSAMPLE_ENA GENMASK(1, 0)
53 #define PVT_SENSOR_STAT 0x8
54 #define SENSOR_STAT_DATA_VALID BIT(10)
55 #define SENSOR_STAT_DATA GENMASK(9, 0)
58 #define FAN_CFG_DUTY_CYCLE GENMASK(23, 16)
59 #define INV_POL BIT(3)
60 #define GATE_ENA BIT(2)
61 #define PWM_OPEN_COL_ENA BIT(1)
62 #define FAN_STAT_CFG BIT(0)
63 #define FAN_PWM_FREQ 0x4
64 #define FAN_PWM_CYC_10US GENMASK(25, 15)
65 #define FAN_PWM_FREQ_FREQ GENMASK(14, 0)
67 #define FAN_CNT_DATA GENMASK(15, 0)
69 #define LAN966X_PVT_CLK 1200000 /* 1.2 MHz */
71 struct lan966x_hwmon {
72 struct regmap *regmap_pvt;
73 struct regmap *regmap_fan;
75 unsigned long clk_rate;
78 static int lan966x_hwmon_read_temp(struct device *dev, long *val)
80 struct lan966x_hwmon *hwmon = dev_get_drvdata(dev);
84 ret = regmap_read(hwmon->regmap_pvt, PVT_SENSOR_STAT, &data);
88 if (!(data & SENSOR_STAT_DATA_VALID))
91 *val = polynomial_calc(&poly_N_to_temp,
92 FIELD_GET(SENSOR_STAT_DATA, data));
97 static int lan966x_hwmon_read_fan(struct device *dev, long *val)
99 struct lan966x_hwmon *hwmon = dev_get_drvdata(dev);
103 ret = regmap_read(hwmon->regmap_fan, FAN_CNT, &data);
108 * Data is given in pulses per second. Assume two pulses
111 *val = FIELD_GET(FAN_CNT_DATA, data) * 60 / 2;
116 static int lan966x_hwmon_read_pwm(struct device *dev, long *val)
118 struct lan966x_hwmon *hwmon = dev_get_drvdata(dev);
122 ret = regmap_read(hwmon->regmap_fan, FAN_CFG, &data);
126 *val = FIELD_GET(FAN_CFG_DUTY_CYCLE, data);
131 static int lan966x_hwmon_read_pwm_freq(struct device *dev, long *val)
133 struct lan966x_hwmon *hwmon = dev_get_drvdata(dev);
138 ret = regmap_read(hwmon->regmap_fan, FAN_PWM_FREQ, &data);
143 * Datasheet says it is sys_clk / 256 / pwm_freq. But in reality
144 * it is sys_clk / 256 / (pwm_freq + 1).
146 data = FIELD_GET(FAN_PWM_FREQ_FREQ, data) + 1;
147 tmp = DIV_ROUND_CLOSEST(hwmon->clk_rate, 256);
148 *val = DIV_ROUND_CLOSEST(tmp, data);
153 static int lan966x_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
154 u32 attr, int channel, long *val)
158 return lan966x_hwmon_read_temp(dev, val);
160 return lan966x_hwmon_read_fan(dev, val);
163 case hwmon_pwm_input:
164 return lan966x_hwmon_read_pwm(dev, val);
166 return lan966x_hwmon_read_pwm_freq(dev, val);
175 static int lan966x_hwmon_write_pwm(struct device *dev, long val)
177 struct lan966x_hwmon *hwmon = dev_get_drvdata(dev);
179 if (val < 0 || val > 255)
182 return regmap_update_bits(hwmon->regmap_fan, FAN_CFG,
184 FIELD_PREP(FAN_CFG_DUTY_CYCLE, val));
187 static int lan966x_hwmon_write_pwm_freq(struct device *dev, long val)
189 struct lan966x_hwmon *hwmon = dev_get_drvdata(dev);
194 val = DIV_ROUND_CLOSEST(hwmon->clk_rate, val);
195 val = DIV_ROUND_CLOSEST(val, 256) - 1;
196 val = clamp_val(val, 0, FAN_PWM_FREQ_FREQ);
198 return regmap_update_bits(hwmon->regmap_fan, FAN_PWM_FREQ,
200 FIELD_PREP(FAN_PWM_FREQ_FREQ, val));
203 static int lan966x_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
204 u32 attr, int channel, long val)
209 case hwmon_pwm_input:
210 return lan966x_hwmon_write_pwm(dev, val);
212 return lan966x_hwmon_write_pwm_freq(dev, val);
221 static umode_t lan966x_hwmon_is_visible(const void *data,
222 enum hwmon_sensor_types type,
223 u32 attr, int channel)
230 case hwmon_temp_input:
239 case hwmon_fan_input:
248 case hwmon_pwm_input:
263 static const struct hwmon_channel_info * const lan966x_hwmon_info[] = {
264 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
265 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
266 HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT),
267 HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_FREQ),
271 static const struct hwmon_ops lan966x_hwmon_ops = {
272 .is_visible = lan966x_hwmon_is_visible,
273 .read = lan966x_hwmon_read,
274 .write = lan966x_hwmon_write,
277 static const struct hwmon_chip_info lan966x_hwmon_chip_info = {
278 .ops = &lan966x_hwmon_ops,
279 .info = lan966x_hwmon_info,
282 static void lan966x_hwmon_disable(void *data)
284 struct lan966x_hwmon *hwmon = data;
286 regmap_update_bits(hwmon->regmap_pvt, PVT_SENSOR_CFG,
287 SENSOR_CFG_SAMPLE_ENA | SENSOR_CFG_CONTINIOUS_MODE,
291 static int lan966x_hwmon_enable(struct device *dev,
292 struct lan966x_hwmon *hwmon)
294 unsigned int mask = SENSOR_CFG_CLK_CFG |
295 SENSOR_CFG_SAMPLE_ENA |
296 SENSOR_CFG_START_CAPTURE |
297 SENSOR_CFG_CONTINIOUS_MODE |
298 SENSOR_CFG_PSAMPLE_ENA;
303 /* enable continuous mode */
304 val = SENSOR_CFG_SAMPLE_ENA | SENSOR_CFG_CONTINIOUS_MODE;
306 /* set PVT clock to be between 1.15 and 1.25 MHz */
307 div = DIV_ROUND_CLOSEST(hwmon->clk_rate, LAN966X_PVT_CLK);
308 val |= FIELD_PREP(SENSOR_CFG_CLK_CFG, div);
310 ret = regmap_update_bits(hwmon->regmap_pvt, PVT_SENSOR_CFG,
315 return devm_add_action_or_reset(dev, lan966x_hwmon_disable, hwmon);
318 static struct regmap *lan966x_init_regmap(struct platform_device *pdev,
321 struct regmap_config regmap_config = {
328 base = devm_platform_ioremap_resource_byname(pdev, name);
330 return ERR_CAST(base);
332 regmap_config.name = name;
334 return devm_regmap_init_mmio(&pdev->dev, base, ®map_config);
337 static int lan966x_hwmon_probe(struct platform_device *pdev)
339 struct device *dev = &pdev->dev;
340 struct lan966x_hwmon *hwmon;
341 struct device *hwmon_dev;
344 hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
348 hwmon->clk = devm_clk_get_enabled(dev, NULL);
349 if (IS_ERR(hwmon->clk))
350 return dev_err_probe(dev, PTR_ERR(hwmon->clk),
351 "failed to get clock\n");
353 hwmon->clk_rate = clk_get_rate(hwmon->clk);
355 hwmon->regmap_pvt = lan966x_init_regmap(pdev, "pvt");
356 if (IS_ERR(hwmon->regmap_pvt))
357 return dev_err_probe(dev, PTR_ERR(hwmon->regmap_pvt),
358 "failed to get regmap for PVT registers\n");
360 hwmon->regmap_fan = lan966x_init_regmap(pdev, "fan");
361 if (IS_ERR(hwmon->regmap_fan))
362 return dev_err_probe(dev, PTR_ERR(hwmon->regmap_fan),
363 "failed to get regmap for fan registers\n");
365 ret = lan966x_hwmon_enable(dev, hwmon);
367 return dev_err_probe(dev, ret, "failed to enable sensor\n");
369 hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
370 "lan966x_hwmon", hwmon,
371 &lan966x_hwmon_chip_info, NULL);
372 if (IS_ERR(hwmon_dev))
373 return dev_err_probe(dev, PTR_ERR(hwmon_dev),
374 "failed to register hwmon device\n");
379 static const struct of_device_id lan966x_hwmon_of_match[] = {
380 { .compatible = "microchip,lan9668-hwmon" },
383 MODULE_DEVICE_TABLE(of, lan966x_hwmon_of_match);
385 static struct platform_driver lan966x_hwmon_driver = {
386 .probe = lan966x_hwmon_probe,
388 .name = "lan966x-hwmon",
389 .of_match_table = lan966x_hwmon_of_match,
392 module_platform_driver(lan966x_hwmon_driver);
394 MODULE_DESCRIPTION("LAN966x Hardware Monitoring Driver");
395 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
396 MODULE_LICENSE("GPL");