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/i2c.h>
15 #include <linux/math64.h>
16 #include <linux/mfd/lochnagar.h>
17 #include <linux/mfd/lochnagar2_regs.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
24 #define LN2_MAX_NSAMPLE 1023
25 #define LN2_SAMPLE_US 1670
27 #define LN2_CURR_UNITS 1000
28 #define LN2_VOLT_UNITS 1000
29 #define LN2_TEMP_UNITS 1000
30 #define LN2_PWR_UNITS 1000000
32 static const char * const lochnagar_chan_names
[] = {
43 struct lochnagar_hwmon
{
44 struct regmap
*regmap
;
46 long power_nsamples
[ARRAY_SIZE(lochnagar_chan_names
)];
48 /* Lock to ensure only a single sensor is read at a time */
49 struct mutex sensor_lock
;
52 enum lochnagar_measure_mode
{
59 * float_to_long - Convert ieee754 reading from hardware to an integer
61 * @data: Value read from the hardware
62 * @precision: Units to multiply up to eg. 1000 = milli, 1000000 = micro
64 * Return: Converted integer reading
66 * Depending on the measurement type the hardware returns an ieee754
67 * floating point value in either volts, amps or celsius. This function
68 * will convert that into an integer in a smaller unit such as micro-amps
69 * or milli-celsius. The hardware does not return NaN, so consideration of
70 * that is not required.
72 static long float_to_long(u32 data
, u32 precision
)
74 u64 man
= data
& 0x007FFFFF;
75 int exp
= ((data
& 0x7F800000) >> 23) - 127 - 23;
76 bool negative
= data
& 0x80000000;
79 man
= (man
+ (1 << 23)) * precision
;
81 if (fls64(man
) + exp
> (int)sizeof(long) * 8 - 1)
84 result
= (man
+ (1ull << (-exp
- 1))) >> -exp
;
88 return negative
? -result
: result
;
91 static int do_measurement(struct regmap
*regmap
, int chan
,
92 enum lochnagar_measure_mode mode
, int nsamples
)
97 chan
= 1 << (chan
+ LOCHNAGAR2_IMON_MEASURED_CHANNELS_SHIFT
);
99 ret
= regmap_write(regmap
, LOCHNAGAR2_IMON_CTRL1
,
100 LOCHNAGAR2_IMON_ENA_MASK
| chan
| mode
);
104 ret
= regmap_write(regmap
, LOCHNAGAR2_IMON_CTRL2
, nsamples
);
108 ret
= regmap_write(regmap
, LOCHNAGAR2_IMON_CTRL3
,
109 LOCHNAGAR2_IMON_CONFIGURE_MASK
);
113 ret
= regmap_read_poll_timeout(regmap
, LOCHNAGAR2_IMON_CTRL3
, val
,
114 val
& LOCHNAGAR2_IMON_DONE_MASK
,
119 ret
= regmap_write(regmap
, LOCHNAGAR2_IMON_CTRL3
,
120 LOCHNAGAR2_IMON_MEASURE_MASK
);
125 * Actual measurement time is ~1.67mS per sample, approximate this
126 * with a 1.5mS per sample msleep and then poll for success up to
127 * ~0.17mS * 1023 (LN2_MAX_NSAMPLES). Normally for smaller values
128 * of nsamples the poll will complete on the first loop due to
129 * other latency in the system.
131 msleep((nsamples
* 3) / 2);
133 ret
= regmap_read_poll_timeout(regmap
, LOCHNAGAR2_IMON_CTRL3
, val
,
134 val
& LOCHNAGAR2_IMON_DONE_MASK
,
139 return regmap_write(regmap
, LOCHNAGAR2_IMON_CTRL3
, 0);
142 static int request_data(struct regmap
*regmap
, int chan
, u32
*data
)
147 ret
= regmap_write(regmap
, LOCHNAGAR2_IMON_CTRL4
,
148 LOCHNAGAR2_IMON_DATA_REQ_MASK
|
149 chan
<< LOCHNAGAR2_IMON_CH_SEL_SHIFT
);
153 ret
= regmap_read_poll_timeout(regmap
, LOCHNAGAR2_IMON_CTRL4
, val
,
154 val
& LOCHNAGAR2_IMON_DATA_RDY_MASK
,
159 ret
= regmap_read(regmap
, LOCHNAGAR2_IMON_DATA1
, &val
);
165 ret
= regmap_read(regmap
, LOCHNAGAR2_IMON_DATA2
, &val
);
171 return regmap_write(regmap
, LOCHNAGAR2_IMON_CTRL4
, 0);
174 static int read_sensor(struct device
*dev
, int chan
,
175 enum lochnagar_measure_mode mode
, int nsamples
,
176 unsigned int precision
, long *val
)
178 struct lochnagar_hwmon
*priv
= dev_get_drvdata(dev
);
179 struct regmap
*regmap
= priv
->regmap
;
183 mutex_lock(&priv
->sensor_lock
);
185 ret
= do_measurement(regmap
, chan
, mode
, nsamples
);
187 dev_err(dev
, "Failed to perform measurement: %d\n", ret
);
191 ret
= request_data(regmap
, chan
, &data
);
193 dev_err(dev
, "Failed to read measurement: %d\n", ret
);
197 *val
= float_to_long(data
, precision
);
200 mutex_unlock(&priv
->sensor_lock
);
205 static int read_power(struct device
*dev
, int chan
, long *val
)
207 struct lochnagar_hwmon
*priv
= dev_get_drvdata(dev
);
208 int nsamples
= priv
->power_nsamples
[chan
];
212 if (!strcmp("SYSVDD", lochnagar_chan_names
[chan
])) {
213 power
= 5 * LN2_PWR_UNITS
;
215 ret
= read_sensor(dev
, chan
, LN2_VOLT
, 1, LN2_PWR_UNITS
, val
);
222 ret
= read_sensor(dev
, chan
, LN2_CURR
, nsamples
, LN2_PWR_UNITS
, val
);
227 power
= DIV_ROUND_CLOSEST_ULL(power
, LN2_PWR_UNITS
);
229 if (power
> LONG_MAX
)
237 static umode_t
lochnagar_is_visible(const void *drvdata
,
238 enum hwmon_sensor_types type
,
243 if (!strcmp("SYSVDD", lochnagar_chan_names
[chan
]))
247 if (attr
== hwmon_power_average_interval
)
257 static int lochnagar_read(struct device
*dev
, enum hwmon_sensor_types type
,
258 u32 attr
, int chan
, long *val
)
260 struct lochnagar_hwmon
*priv
= dev_get_drvdata(dev
);
265 return read_sensor(dev
, chan
, LN2_VOLT
, 1, LN2_VOLT_UNITS
, val
);
267 return read_sensor(dev
, chan
, LN2_CURR
, 1, LN2_CURR_UNITS
, val
);
269 return read_sensor(dev
, chan
, LN2_TEMP
, 1, LN2_TEMP_UNITS
, val
);
272 case hwmon_power_average
:
273 return read_power(dev
, chan
, val
);
274 case hwmon_power_average_interval
:
275 interval
= priv
->power_nsamples
[chan
] * LN2_SAMPLE_US
;
276 *val
= DIV_ROUND_CLOSEST(interval
, 1000);
286 static int lochnagar_read_string(struct device
*dev
,
287 enum hwmon_sensor_types type
, u32 attr
,
288 int chan
, const char **str
)
294 *str
= lochnagar_chan_names
[chan
];
301 static int lochnagar_write(struct device
*dev
, enum hwmon_sensor_types type
,
302 u32 attr
, int chan
, long val
)
304 struct lochnagar_hwmon
*priv
= dev_get_drvdata(dev
);
306 if (type
!= hwmon_power
|| attr
!= hwmon_power_average_interval
)
309 val
= clamp_t(long, val
, 1, (LN2_MAX_NSAMPLE
* LN2_SAMPLE_US
) / 1000);
310 val
= DIV_ROUND_CLOSEST(val
* 1000, LN2_SAMPLE_US
);
312 priv
->power_nsamples
[chan
] = val
;
317 static const struct hwmon_ops lochnagar_ops
= {
318 .is_visible
= lochnagar_is_visible
,
319 .read
= lochnagar_read
,
320 .read_string
= lochnagar_read_string
,
321 .write
= lochnagar_write
,
324 static const struct hwmon_channel_info
*lochnagar_info
[] = {
325 HWMON_CHANNEL_INFO(temp
, HWMON_T_INPUT
),
326 HWMON_CHANNEL_INFO(in
, 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_I_INPUT
| HWMON_I_LABEL
),
334 HWMON_CHANNEL_INFO(curr
, 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_C_INPUT
| HWMON_C_LABEL
),
342 HWMON_CHANNEL_INFO(power
, HWMON_P_AVERAGE
| HWMON_P_AVERAGE_INTERVAL
|
344 HWMON_P_AVERAGE
| HWMON_P_AVERAGE_INTERVAL
|
346 HWMON_P_AVERAGE
| HWMON_P_AVERAGE_INTERVAL
|
348 HWMON_P_AVERAGE
| HWMON_P_AVERAGE_INTERVAL
|
350 HWMON_P_AVERAGE
| HWMON_P_AVERAGE_INTERVAL
|
352 HWMON_P_AVERAGE
| HWMON_P_AVERAGE_INTERVAL
|
354 HWMON_P_AVERAGE
| HWMON_P_AVERAGE_INTERVAL
|
356 HWMON_P_AVERAGE
| HWMON_P_AVERAGE_INTERVAL
|
361 static const struct hwmon_chip_info lochnagar_chip_info
= {
362 .ops
= &lochnagar_ops
,
363 .info
= lochnagar_info
,
366 static const struct of_device_id lochnagar_of_match
[] = {
367 { .compatible
= "cirrus,lochnagar2-hwmon" },
370 MODULE_DEVICE_TABLE(of
, lochnagar_of_match
);
372 static int lochnagar_hwmon_probe(struct platform_device
*pdev
)
374 struct device
*dev
= &pdev
->dev
;
375 struct device
*hwmon_dev
;
376 struct lochnagar_hwmon
*priv
;
379 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
383 mutex_init(&priv
->sensor_lock
);
385 priv
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
387 dev_err(dev
, "No register map found\n");
391 for (i
= 0; i
< ARRAY_SIZE(priv
->power_nsamples
); i
++)
392 priv
->power_nsamples
[i
] = 96;
394 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, "Lochnagar", priv
,
395 &lochnagar_chip_info
,
398 return PTR_ERR_OR_ZERO(hwmon_dev
);
401 static struct platform_driver lochnagar_hwmon_driver
= {
403 .name
= "lochnagar-hwmon",
404 .of_match_table
= lochnagar_of_match
,
406 .probe
= lochnagar_hwmon_probe
,
408 module_platform_driver(lochnagar_hwmon_driver
);
410 MODULE_AUTHOR("Lucas Tanure <tanureal@opensource.cirrus.com>");
411 MODULE_DESCRIPTION("Lochnagar hardware monitoring features");
412 MODULE_LICENSE("GPL");