1 // SPDX-License-Identifier: GPL-2.0
3 * Lochnagar hardware monitoring features
5 * Copyright (c) 2016-2019 Cirrus Logic, Inc. and
6 * Cirrus Logic International Semiconductor Ltd.
8 * Author: Lucas Tanure <tanureal@opensource.cirrus.com>
11 #include <linux/delay.h>
12 #include <linux/hwmon.h>
13 #include <linux/hwmon-sysfs.h>
14 #include <linux/math64.h>
15 #include <linux/mfd/lochnagar.h>
16 #include <linux/mfd/lochnagar2_regs.h>
17 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
23 #define LN2_MAX_NSAMPLE 1023
24 #define LN2_SAMPLE_US 1670
26 #define LN2_CURR_UNITS 1000
27 #define LN2_VOLT_UNITS 1000
28 #define LN2_TEMP_UNITS 1000
29 #define LN2_PWR_UNITS 1000000
31 static const char * const lochnagar_chan_names[] = {
42 struct lochnagar_hwmon {
43 struct regmap *regmap;
45 long power_nsamples[ARRAY_SIZE(lochnagar_chan_names)];
47 /* Lock to ensure only a single sensor is read at a time */
48 struct mutex sensor_lock;
51 enum lochnagar_measure_mode {
58 * float_to_long - Convert ieee754 reading from hardware to an integer
60 * @data: Value read from the hardware
61 * @precision: Units to multiply up to eg. 1000 = milli, 1000000 = micro
63 * Return: Converted integer reading
65 * Depending on the measurement type the hardware returns an ieee754
66 * floating point value in either volts, amps or celsius. This function
67 * will convert that into an integer in a smaller unit such as micro-amps
68 * or milli-celsius. The hardware does not return NaN, so consideration of
69 * that is not required.
71 static long float_to_long(u32 data, u32 precision)
73 u64 man = data & 0x007FFFFF;
74 int exp = ((data & 0x7F800000) >> 23) - 127 - 23;
75 bool negative = data & 0x80000000;
78 man = (man + (1 << 23)) * precision;
80 if (fls64(man) + exp > (int)sizeof(long) * 8 - 1)
83 result = (man + (1ull << (-exp - 1))) >> -exp;
87 return negative ? -result : result;
90 static int do_measurement(struct regmap *regmap, int chan,
91 enum lochnagar_measure_mode mode, int nsamples)
96 chan = 1 << (chan + LOCHNAGAR2_IMON_MEASURED_CHANNELS_SHIFT);
98 ret = regmap_write(regmap, LOCHNAGAR2_IMON_CTRL1,
99 LOCHNAGAR2_IMON_ENA_MASK | chan | mode);
103 ret = regmap_write(regmap, LOCHNAGAR2_IMON_CTRL2, nsamples);
107 ret = regmap_write(regmap, LOCHNAGAR2_IMON_CTRL3,
108 LOCHNAGAR2_IMON_CONFIGURE_MASK);
112 ret = regmap_read_poll_timeout(regmap, LOCHNAGAR2_IMON_CTRL3, val,
113 val & LOCHNAGAR2_IMON_DONE_MASK,
118 ret = regmap_write(regmap, LOCHNAGAR2_IMON_CTRL3,
119 LOCHNAGAR2_IMON_MEASURE_MASK);
124 * Actual measurement time is ~1.67mS per sample, approximate this
125 * with a 1.5mS per sample msleep and then poll for success up to
126 * ~0.17mS * 1023 (LN2_MAX_NSAMPLES). Normally for smaller values
127 * of nsamples the poll will complete on the first loop due to
128 * other latency in the system.
130 msleep((nsamples * 3) / 2);
132 ret = regmap_read_poll_timeout(regmap, LOCHNAGAR2_IMON_CTRL3, val,
133 val & LOCHNAGAR2_IMON_DONE_MASK,
138 return regmap_write(regmap, LOCHNAGAR2_IMON_CTRL3, 0);
141 static int request_data(struct regmap *regmap, int chan, u32 *data)
146 ret = regmap_write(regmap, LOCHNAGAR2_IMON_CTRL4,
147 LOCHNAGAR2_IMON_DATA_REQ_MASK |
148 chan << LOCHNAGAR2_IMON_CH_SEL_SHIFT);
152 ret = regmap_read_poll_timeout(regmap, LOCHNAGAR2_IMON_CTRL4, val,
153 val & LOCHNAGAR2_IMON_DATA_RDY_MASK,
158 ret = regmap_read(regmap, LOCHNAGAR2_IMON_DATA1, &val);
164 ret = regmap_read(regmap, LOCHNAGAR2_IMON_DATA2, &val);
170 return regmap_write(regmap, LOCHNAGAR2_IMON_CTRL4, 0);
173 static int read_sensor(struct device *dev, int chan,
174 enum lochnagar_measure_mode mode, int nsamples,
175 unsigned int precision, long *val)
177 struct lochnagar_hwmon *priv = dev_get_drvdata(dev);
178 struct regmap *regmap = priv->regmap;
182 mutex_lock(&priv->sensor_lock);
184 ret = do_measurement(regmap, chan, mode, nsamples);
186 dev_err(dev, "Failed to perform measurement: %d\n", ret);
190 ret = request_data(regmap, chan, &data);
192 dev_err(dev, "Failed to read measurement: %d\n", ret);
196 *val = float_to_long(data, precision);
199 mutex_unlock(&priv->sensor_lock);
204 static int read_power(struct device *dev, int chan, long *val)
206 struct lochnagar_hwmon *priv = dev_get_drvdata(dev);
207 int nsamples = priv->power_nsamples[chan];
211 if (!strcmp("SYSVDD", lochnagar_chan_names[chan])) {
212 power = 5 * LN2_PWR_UNITS;
214 ret = read_sensor(dev, chan, LN2_VOLT, 1, LN2_PWR_UNITS, val);
221 ret = read_sensor(dev, chan, LN2_CURR, nsamples, LN2_PWR_UNITS, val);
226 power = DIV_ROUND_CLOSEST_ULL(power, LN2_PWR_UNITS);
228 if (power > LONG_MAX)
236 static umode_t lochnagar_is_visible(const void *drvdata,
237 enum hwmon_sensor_types type,
242 if (!strcmp("SYSVDD", lochnagar_chan_names[chan]))
246 if (attr == hwmon_power_average_interval)
256 static int lochnagar_read(struct device *dev, enum hwmon_sensor_types type,
257 u32 attr, int chan, long *val)
259 struct lochnagar_hwmon *priv = dev_get_drvdata(dev);
264 return read_sensor(dev, chan, LN2_VOLT, 1, LN2_VOLT_UNITS, val);
266 return read_sensor(dev, chan, LN2_CURR, 1, LN2_CURR_UNITS, val);
268 return read_sensor(dev, chan, LN2_TEMP, 1, LN2_TEMP_UNITS, val);
271 case hwmon_power_average:
272 return read_power(dev, chan, val);
273 case hwmon_power_average_interval:
274 interval = priv->power_nsamples[chan] * LN2_SAMPLE_US;
275 *val = DIV_ROUND_CLOSEST(interval, 1000);
285 static int lochnagar_read_string(struct device *dev,
286 enum hwmon_sensor_types type, u32 attr,
287 int chan, const char **str)
293 *str = lochnagar_chan_names[chan];
300 static int lochnagar_write(struct device *dev, enum hwmon_sensor_types type,
301 u32 attr, int chan, long val)
303 struct lochnagar_hwmon *priv = dev_get_drvdata(dev);
305 if (type != hwmon_power || attr != hwmon_power_average_interval)
308 val = clamp_t(long, val, 1, (LN2_MAX_NSAMPLE * LN2_SAMPLE_US) / 1000);
309 val = DIV_ROUND_CLOSEST(val * 1000, LN2_SAMPLE_US);
311 priv->power_nsamples[chan] = val;
316 static const struct hwmon_ops lochnagar_ops = {
317 .is_visible = lochnagar_is_visible,
318 .read = lochnagar_read,
319 .read_string = lochnagar_read_string,
320 .write = lochnagar_write,
323 static const struct hwmon_channel_info * const lochnagar_info[] = {
324 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
325 HWMON_CHANNEL_INFO(in, HWMON_I_INPUT | HWMON_I_LABEL,
326 HWMON_I_INPUT | HWMON_I_LABEL,
327 HWMON_I_INPUT | HWMON_I_LABEL,
328 HWMON_I_INPUT | HWMON_I_LABEL,
329 HWMON_I_INPUT | HWMON_I_LABEL,
330 HWMON_I_INPUT | HWMON_I_LABEL,
331 HWMON_I_INPUT | HWMON_I_LABEL,
332 HWMON_I_INPUT | HWMON_I_LABEL),
333 HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT | HWMON_C_LABEL,
334 HWMON_C_INPUT | HWMON_C_LABEL,
335 HWMON_C_INPUT | HWMON_C_LABEL,
336 HWMON_C_INPUT | HWMON_C_LABEL,
337 HWMON_C_INPUT | HWMON_C_LABEL,
338 HWMON_C_INPUT | HWMON_C_LABEL,
339 HWMON_C_INPUT | HWMON_C_LABEL,
340 HWMON_C_INPUT | HWMON_C_LABEL),
341 HWMON_CHANNEL_INFO(power, HWMON_P_AVERAGE | HWMON_P_AVERAGE_INTERVAL |
343 HWMON_P_AVERAGE | HWMON_P_AVERAGE_INTERVAL |
345 HWMON_P_AVERAGE | HWMON_P_AVERAGE_INTERVAL |
347 HWMON_P_AVERAGE | HWMON_P_AVERAGE_INTERVAL |
349 HWMON_P_AVERAGE | HWMON_P_AVERAGE_INTERVAL |
351 HWMON_P_AVERAGE | HWMON_P_AVERAGE_INTERVAL |
353 HWMON_P_AVERAGE | HWMON_P_AVERAGE_INTERVAL |
355 HWMON_P_AVERAGE | HWMON_P_AVERAGE_INTERVAL |
360 static const struct hwmon_chip_info lochnagar_chip_info = {
361 .ops = &lochnagar_ops,
362 .info = lochnagar_info,
365 static const struct of_device_id lochnagar_of_match[] = {
366 { .compatible = "cirrus,lochnagar2-hwmon" },
369 MODULE_DEVICE_TABLE(of, lochnagar_of_match);
371 static int lochnagar_hwmon_probe(struct platform_device *pdev)
373 struct device *dev = &pdev->dev;
374 struct device *hwmon_dev;
375 struct lochnagar_hwmon *priv;
378 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
382 mutex_init(&priv->sensor_lock);
384 priv->regmap = dev_get_regmap(dev->parent, NULL);
386 dev_err(dev, "No register map found\n");
390 for (i = 0; i < ARRAY_SIZE(priv->power_nsamples); i++)
391 priv->power_nsamples[i] = 96;
393 hwmon_dev = devm_hwmon_device_register_with_info(dev, "Lochnagar", priv,
394 &lochnagar_chip_info,
397 return PTR_ERR_OR_ZERO(hwmon_dev);
400 static struct platform_driver lochnagar_hwmon_driver = {
402 .name = "lochnagar-hwmon",
403 .of_match_table = lochnagar_of_match,
405 .probe = lochnagar_hwmon_probe,
407 module_platform_driver(lochnagar_hwmon_driver);
409 MODULE_AUTHOR("Lucas Tanure <tanureal@opensource.cirrus.com>");
410 MODULE_DESCRIPTION("Lochnagar hardware monitoring features");
411 MODULE_LICENSE("GPL");