1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * sl28cpld hardware monitoring driver 4 * 5 * Copyright 2020 Kontron Europe GmbH 6 */ 7 8 #include <linux/bitfield.h> 9 #include <linux/hwmon.h> 10 #include <linux/kernel.h> 11 #include <linux/mod_devicetable.h> 12 #include <linux/module.h> 13 #include <linux/platform_device.h> 14 #include <linux/property.h> 15 #include <linux/regmap.h> 16 17 #define FAN_INPUT 0x00 18 #define FAN_SCALE_X8 BIT(7) 19 #define FAN_VALUE_MASK GENMASK(6, 0) 20 21 struct sl28cpld_hwmon { 22 struct regmap *regmap; 23 u32 offset; 24 }; 25 26 static int sl28cpld_hwmon_read(struct device *dev, 27 enum hwmon_sensor_types type, u32 attr, 28 int channel, long *input) 29 { 30 struct sl28cpld_hwmon *hwmon = dev_get_drvdata(dev); 31 unsigned int value; 32 int ret; 33 34 switch (attr) { 35 case hwmon_fan_input: 36 ret = regmap_read(hwmon->regmap, hwmon->offset + FAN_INPUT, 37 &value); 38 if (ret) 39 return ret; 40 /* 41 * The register has a 7 bit value and 1 bit which indicates the 42 * scale. If the MSB is set, then the lower 7 bit has to be 43 * multiplied by 8, to get the correct reading. 44 */ 45 if (value & FAN_SCALE_X8) 46 value = FIELD_GET(FAN_VALUE_MASK, value) << 3; 47 48 /* 49 * The counter period is 1000ms and the sysfs specification 50 * says we should assume 2 pulses per revolution. 51 */ 52 value *= 60 / 2; 53 54 break; 55 default: 56 return -EOPNOTSUPP; 57 } 58 59 *input = value; 60 return 0; 61 } 62 63 static const struct hwmon_channel_info * const sl28cpld_hwmon_info[] = { 64 HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT), 65 NULL 66 }; 67 68 static const struct hwmon_ops sl28cpld_hwmon_ops = { 69 .visible = 0444, 70 .read = sl28cpld_hwmon_read, 71 }; 72 73 static const struct hwmon_chip_info sl28cpld_hwmon_chip_info = { 74 .ops = &sl28cpld_hwmon_ops, 75 .info = sl28cpld_hwmon_info, 76 }; 77 78 static int sl28cpld_hwmon_probe(struct platform_device *pdev) 79 { 80 struct sl28cpld_hwmon *hwmon; 81 struct device *hwmon_dev; 82 int ret; 83 84 if (!pdev->dev.parent) 85 return -ENODEV; 86 87 hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL); 88 if (!hwmon) 89 return -ENOMEM; 90 91 hwmon->regmap = dev_get_regmap(pdev->dev.parent, NULL); 92 if (!hwmon->regmap) 93 return -ENODEV; 94 95 ret = device_property_read_u32(&pdev->dev, "reg", &hwmon->offset); 96 if (ret) 97 return -EINVAL; 98 99 hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, 100 "sl28cpld_hwmon", hwmon, 101 &sl28cpld_hwmon_chip_info, NULL); 102 if (IS_ERR(hwmon_dev)) 103 dev_err(&pdev->dev, "failed to register as hwmon device"); 104 105 return PTR_ERR_OR_ZERO(hwmon_dev); 106 } 107 108 static const struct of_device_id sl28cpld_hwmon_of_match[] = { 109 { .compatible = "kontron,sl28cpld-fan" }, 110 {} 111 }; 112 MODULE_DEVICE_TABLE(of, sl28cpld_hwmon_of_match); 113 114 static struct platform_driver sl28cpld_hwmon_driver = { 115 .probe = sl28cpld_hwmon_probe, 116 .driver = { 117 .name = "sl28cpld-fan", 118 .of_match_table = sl28cpld_hwmon_of_match, 119 }, 120 }; 121 module_platform_driver(sl28cpld_hwmon_driver); 122 123 MODULE_DESCRIPTION("sl28cpld Hardware Monitoring Driver"); 124 MODULE_AUTHOR("Michael Walle <michael@walle.cc>"); 125 MODULE_LICENSE("GPL"); 126