1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (C) 2023 Hewlett-Packard Enterprise Development Company, L.P. */ 3 4 #include <linux/err.h> 5 #include <linux/hwmon.h> 6 #include <linux/io.h> 7 #include <linux/module.h> 8 #include <linux/platform_device.h> 9 10 struct gxp_fan_ctrl_drvdata { 11 void __iomem *base; 12 }; 13 14 static int gxp_pwm_write(struct device *dev, u32 attr, int channel, long val) 15 { 16 struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev); 17 18 switch (attr) { 19 case hwmon_pwm_input: 20 if (val > 255 || val < 0) 21 return -EINVAL; 22 writeb(val, drvdata->base + channel); 23 return 0; 24 default: 25 return -EOPNOTSUPP; 26 } 27 } 28 29 static int gxp_fan_ctrl_write(struct device *dev, enum hwmon_sensor_types type, 30 u32 attr, int channel, long val) 31 { 32 switch (type) { 33 case hwmon_pwm: 34 return gxp_pwm_write(dev, attr, channel, val); 35 default: 36 return -EOPNOTSUPP; 37 } 38 } 39 40 static int gxp_pwm_read(struct device *dev, u32 attr, int channel, long *val) 41 { 42 struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev); 43 44 *val = readb(drvdata->base + channel); 45 46 return 0; 47 } 48 49 static int gxp_fan_ctrl_read(struct device *dev, enum hwmon_sensor_types type, 50 u32 attr, int channel, long *val) 51 { 52 switch (type) { 53 case hwmon_pwm: 54 return gxp_pwm_read(dev, attr, channel, val); 55 default: 56 return -EOPNOTSUPP; 57 } 58 } 59 60 static umode_t gxp_fan_ctrl_is_visible(const void *_data, 61 enum hwmon_sensor_types type, 62 u32 attr, int channel) 63 { 64 umode_t mode = 0; 65 66 switch (type) { 67 case hwmon_pwm: 68 switch (attr) { 69 case hwmon_pwm_input: 70 mode = 0644; 71 break; 72 default: 73 break; 74 } 75 break; 76 default: 77 break; 78 } 79 80 return mode; 81 } 82 83 static const struct hwmon_ops gxp_fan_ctrl_ops = { 84 .is_visible = gxp_fan_ctrl_is_visible, 85 .read = gxp_fan_ctrl_read, 86 .write = gxp_fan_ctrl_write, 87 }; 88 89 static const struct hwmon_channel_info * const gxp_fan_ctrl_info[] = { 90 HWMON_CHANNEL_INFO(pwm, 91 HWMON_PWM_INPUT, 92 HWMON_PWM_INPUT, 93 HWMON_PWM_INPUT, 94 HWMON_PWM_INPUT, 95 HWMON_PWM_INPUT, 96 HWMON_PWM_INPUT, 97 HWMON_PWM_INPUT, 98 HWMON_PWM_INPUT), 99 NULL 100 }; 101 102 static const struct hwmon_chip_info gxp_fan_ctrl_chip_info = { 103 .ops = &gxp_fan_ctrl_ops, 104 .info = gxp_fan_ctrl_info, 105 106 }; 107 108 static int gxp_fan_ctrl_probe(struct platform_device *pdev) 109 { 110 struct gxp_fan_ctrl_drvdata *drvdata; 111 struct device *dev = &pdev->dev; 112 struct device *hwmon_dev; 113 114 drvdata = devm_kzalloc(dev, sizeof(struct gxp_fan_ctrl_drvdata), 115 GFP_KERNEL); 116 if (!drvdata) 117 return -ENOMEM; 118 119 drvdata->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL); 120 if (IS_ERR(drvdata->base)) 121 return dev_err_probe(dev, PTR_ERR(drvdata->base), 122 "failed to map base\n"); 123 124 hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, 125 "hpe_gxp_fan_ctrl", 126 drvdata, 127 &gxp_fan_ctrl_chip_info, 128 NULL); 129 130 return PTR_ERR_OR_ZERO(hwmon_dev); 131 } 132 133 static const struct of_device_id gxp_fan_ctrl_of_match[] = { 134 { .compatible = "hpe,gxp-fan-ctrl", }, 135 {}, 136 }; 137 MODULE_DEVICE_TABLE(of, gxp_fan_ctrl_of_match); 138 139 static struct platform_driver gxp_fan_ctrl_driver = { 140 .probe = gxp_fan_ctrl_probe, 141 .driver = { 142 .name = "gxp-fan-ctrl", 143 .of_match_table = gxp_fan_ctrl_of_match, 144 }, 145 }; 146 module_platform_driver(gxp_fan_ctrl_driver); 147 148 MODULE_AUTHOR("Nick Hawkins <nick.hawkins@hpe.com>"); 149 MODULE_DESCRIPTION("HPE GXP fan controller"); 150 MODULE_LICENSE("GPL"); 151