13f697027SMichael Walle // SPDX-License-Identifier: GPL-2.0-only
23f697027SMichael Walle /*
33f697027SMichael Walle * sl28cpld hardware monitoring driver
43f697027SMichael Walle *
53f697027SMichael Walle * Copyright 2020 Kontron Europe GmbH
63f697027SMichael Walle */
73f697027SMichael Walle
83f697027SMichael Walle #include <linux/bitfield.h>
93f697027SMichael Walle #include <linux/hwmon.h>
103f697027SMichael Walle #include <linux/kernel.h>
113f697027SMichael Walle #include <linux/mod_devicetable.h>
123f697027SMichael Walle #include <linux/module.h>
133f697027SMichael Walle #include <linux/platform_device.h>
143f697027SMichael Walle #include <linux/property.h>
153f697027SMichael Walle #include <linux/regmap.h>
163f697027SMichael Walle
173f697027SMichael Walle #define FAN_INPUT 0x00
183f697027SMichael Walle #define FAN_SCALE_X8 BIT(7)
193f697027SMichael Walle #define FAN_VALUE_MASK GENMASK(6, 0)
203f697027SMichael Walle
213f697027SMichael Walle struct sl28cpld_hwmon {
223f697027SMichael Walle struct regmap *regmap;
233f697027SMichael Walle u32 offset;
243f697027SMichael Walle };
253f697027SMichael Walle
sl28cpld_hwmon_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)263f697027SMichael Walle static umode_t sl28cpld_hwmon_is_visible(const void *data,
273f697027SMichael Walle enum hwmon_sensor_types type,
283f697027SMichael Walle u32 attr, int channel)
293f697027SMichael Walle {
303f697027SMichael Walle return 0444;
313f697027SMichael Walle }
323f697027SMichael Walle
sl28cpld_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * input)333f697027SMichael Walle static int sl28cpld_hwmon_read(struct device *dev,
343f697027SMichael Walle enum hwmon_sensor_types type, u32 attr,
353f697027SMichael Walle int channel, long *input)
363f697027SMichael Walle {
373f697027SMichael Walle struct sl28cpld_hwmon *hwmon = dev_get_drvdata(dev);
383f697027SMichael Walle unsigned int value;
393f697027SMichael Walle int ret;
403f697027SMichael Walle
413f697027SMichael Walle switch (attr) {
423f697027SMichael Walle case hwmon_fan_input:
433f697027SMichael Walle ret = regmap_read(hwmon->regmap, hwmon->offset + FAN_INPUT,
443f697027SMichael Walle &value);
453f697027SMichael Walle if (ret)
463f697027SMichael Walle return ret;
473f697027SMichael Walle /*
483f697027SMichael Walle * The register has a 7 bit value and 1 bit which indicates the
493f697027SMichael Walle * scale. If the MSB is set, then the lower 7 bit has to be
503f697027SMichael Walle * multiplied by 8, to get the correct reading.
513f697027SMichael Walle */
523f697027SMichael Walle if (value & FAN_SCALE_X8)
533f697027SMichael Walle value = FIELD_GET(FAN_VALUE_MASK, value) << 3;
543f697027SMichael Walle
553f697027SMichael Walle /*
563f697027SMichael Walle * The counter period is 1000ms and the sysfs specification
575ab312b3SJulia Lawall * says we should assume 2 pulses per revolution.
583f697027SMichael Walle */
593f697027SMichael Walle value *= 60 / 2;
603f697027SMichael Walle
613f697027SMichael Walle break;
623f697027SMichael Walle default:
633f697027SMichael Walle return -EOPNOTSUPP;
643f697027SMichael Walle }
653f697027SMichael Walle
663f697027SMichael Walle *input = value;
673f697027SMichael Walle return 0;
683f697027SMichael Walle }
693f697027SMichael Walle
70*7f46e988SKrzysztof Kozlowski static const struct hwmon_channel_info * const sl28cpld_hwmon_info[] = {
71ca538531SGuenter Roeck HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT),
723f697027SMichael Walle NULL
733f697027SMichael Walle };
743f697027SMichael Walle
753f697027SMichael Walle static const struct hwmon_ops sl28cpld_hwmon_ops = {
763f697027SMichael Walle .is_visible = sl28cpld_hwmon_is_visible,
773f697027SMichael Walle .read = sl28cpld_hwmon_read,
783f697027SMichael Walle };
793f697027SMichael Walle
803f697027SMichael Walle static const struct hwmon_chip_info sl28cpld_hwmon_chip_info = {
813f697027SMichael Walle .ops = &sl28cpld_hwmon_ops,
823f697027SMichael Walle .info = sl28cpld_hwmon_info,
833f697027SMichael Walle };
843f697027SMichael Walle
sl28cpld_hwmon_probe(struct platform_device * pdev)853f697027SMichael Walle static int sl28cpld_hwmon_probe(struct platform_device *pdev)
863f697027SMichael Walle {
873f697027SMichael Walle struct sl28cpld_hwmon *hwmon;
883f697027SMichael Walle struct device *hwmon_dev;
893f697027SMichael Walle int ret;
903f697027SMichael Walle
913f697027SMichael Walle if (!pdev->dev.parent)
923f697027SMichael Walle return -ENODEV;
933f697027SMichael Walle
943f697027SMichael Walle hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
953f697027SMichael Walle if (!hwmon)
963f697027SMichael Walle return -ENOMEM;
973f697027SMichael Walle
983f697027SMichael Walle hwmon->regmap = dev_get_regmap(pdev->dev.parent, NULL);
993f697027SMichael Walle if (!hwmon->regmap)
1003f697027SMichael Walle return -ENODEV;
1013f697027SMichael Walle
1023f697027SMichael Walle ret = device_property_read_u32(&pdev->dev, "reg", &hwmon->offset);
1033f697027SMichael Walle if (ret)
1043f697027SMichael Walle return -EINVAL;
1053f697027SMichael Walle
1063f697027SMichael Walle hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
1073f697027SMichael Walle "sl28cpld_hwmon", hwmon,
1083f697027SMichael Walle &sl28cpld_hwmon_chip_info, NULL);
1093f697027SMichael Walle if (IS_ERR(hwmon_dev))
1103f697027SMichael Walle dev_err(&pdev->dev, "failed to register as hwmon device");
1113f697027SMichael Walle
1123f697027SMichael Walle return PTR_ERR_OR_ZERO(hwmon_dev);
1133f697027SMichael Walle }
1143f697027SMichael Walle
1153f697027SMichael Walle static const struct of_device_id sl28cpld_hwmon_of_match[] = {
1163f697027SMichael Walle { .compatible = "kontron,sl28cpld-fan" },
1173f697027SMichael Walle {}
1183f697027SMichael Walle };
1193f697027SMichael Walle MODULE_DEVICE_TABLE(of, sl28cpld_hwmon_of_match);
1203f697027SMichael Walle
1213f697027SMichael Walle static struct platform_driver sl28cpld_hwmon_driver = {
1223f697027SMichael Walle .probe = sl28cpld_hwmon_probe,
1233f697027SMichael Walle .driver = {
1243f697027SMichael Walle .name = "sl28cpld-fan",
1253f697027SMichael Walle .of_match_table = sl28cpld_hwmon_of_match,
1263f697027SMichael Walle },
1273f697027SMichael Walle };
1283f697027SMichael Walle module_platform_driver(sl28cpld_hwmon_driver);
1293f697027SMichael Walle
1303f697027SMichael Walle MODULE_DESCRIPTION("sl28cpld Hardware Monitoring Driver");
1313f697027SMichael Walle MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
1323f697027SMichael Walle MODULE_LICENSE("GPL");
133