Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Copyright (C) 2012 ARM Limited
0005  */
0006 
0007 #define DRVNAME "vexpress-hwmon"
0008 #define pr_fmt(fmt) DRVNAME ": " fmt
0009 
0010 #include <linux/device.h>
0011 #include <linux/err.h>
0012 #include <linux/hwmon.h>
0013 #include <linux/hwmon-sysfs.h>
0014 #include <linux/module.h>
0015 #include <linux/of.h>
0016 #include <linux/of_device.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/vexpress.h>
0019 
0020 struct vexpress_hwmon_data {
0021     struct device *hwmon_dev;
0022     struct regmap *reg;
0023 };
0024 
0025 static ssize_t vexpress_hwmon_label_show(struct device *dev,
0026         struct device_attribute *dev_attr, char *buffer)
0027 {
0028     const char *label = of_get_property(dev->of_node, "label", NULL);
0029 
0030     return sysfs_emit(buffer, "%s\n", label);
0031 }
0032 
0033 static ssize_t vexpress_hwmon_u32_show(struct device *dev,
0034         struct device_attribute *dev_attr, char *buffer)
0035 {
0036     struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
0037     int err;
0038     u32 value;
0039 
0040     err = regmap_read(data->reg, 0, &value);
0041     if (err)
0042         return err;
0043 
0044     return sysfs_emit(buffer, "%u\n", value /
0045               to_sensor_dev_attr(dev_attr)->index);
0046 }
0047 
0048 static ssize_t vexpress_hwmon_u64_show(struct device *dev,
0049         struct device_attribute *dev_attr, char *buffer)
0050 {
0051     struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
0052     int err;
0053     u32 value_hi, value_lo;
0054 
0055     err = regmap_read(data->reg, 0, &value_lo);
0056     if (err)
0057         return err;
0058 
0059     err = regmap_read(data->reg, 1, &value_hi);
0060     if (err)
0061         return err;
0062 
0063     return sysfs_emit(buffer, "%llu\n",
0064               div_u64(((u64)value_hi << 32) | value_lo,
0065                   to_sensor_dev_attr(dev_attr)->index));
0066 }
0067 
0068 static umode_t vexpress_hwmon_attr_is_visible(struct kobject *kobj,
0069         struct attribute *attr, int index)
0070 {
0071     struct device *dev = kobj_to_dev(kobj);
0072     struct device_attribute *dev_attr = container_of(attr,
0073                 struct device_attribute, attr);
0074 
0075     if (dev_attr->show == vexpress_hwmon_label_show &&
0076             !of_get_property(dev->of_node, "label", NULL))
0077         return 0;
0078 
0079     return attr->mode;
0080 }
0081 
0082 struct vexpress_hwmon_type {
0083     const char *name;
0084     const struct attribute_group **attr_groups;
0085 };
0086 
0087 #if !defined(CONFIG_REGULATOR_VEXPRESS)
0088 static DEVICE_ATTR(in1_label, 0444, vexpress_hwmon_label_show, NULL);
0089 static SENSOR_DEVICE_ATTR_RO(in1_input, vexpress_hwmon_u32, 1000);
0090 static struct attribute *vexpress_hwmon_attrs_volt[] = {
0091     &dev_attr_in1_label.attr,
0092     &sensor_dev_attr_in1_input.dev_attr.attr,
0093     NULL
0094 };
0095 static struct attribute_group vexpress_hwmon_group_volt = {
0096     .is_visible = vexpress_hwmon_attr_is_visible,
0097     .attrs = vexpress_hwmon_attrs_volt,
0098 };
0099 static struct vexpress_hwmon_type vexpress_hwmon_volt = {
0100     .name = "vexpress_volt",
0101     .attr_groups = (const struct attribute_group *[]) {
0102         &vexpress_hwmon_group_volt,
0103         NULL,
0104     },
0105 };
0106 #endif
0107 
0108 static DEVICE_ATTR(curr1_label, 0444, vexpress_hwmon_label_show, NULL);
0109 static SENSOR_DEVICE_ATTR_RO(curr1_input, vexpress_hwmon_u32, 1000);
0110 static struct attribute *vexpress_hwmon_attrs_amp[] = {
0111     &dev_attr_curr1_label.attr,
0112     &sensor_dev_attr_curr1_input.dev_attr.attr,
0113     NULL
0114 };
0115 static struct attribute_group vexpress_hwmon_group_amp = {
0116     .is_visible = vexpress_hwmon_attr_is_visible,
0117     .attrs = vexpress_hwmon_attrs_amp,
0118 };
0119 static struct vexpress_hwmon_type vexpress_hwmon_amp = {
0120     .name = "vexpress_amp",
0121     .attr_groups = (const struct attribute_group *[]) {
0122         &vexpress_hwmon_group_amp,
0123         NULL
0124     },
0125 };
0126 
0127 static DEVICE_ATTR(temp1_label, 0444, vexpress_hwmon_label_show, NULL);
0128 static SENSOR_DEVICE_ATTR_RO(temp1_input, vexpress_hwmon_u32, 1000);
0129 static struct attribute *vexpress_hwmon_attrs_temp[] = {
0130     &dev_attr_temp1_label.attr,
0131     &sensor_dev_attr_temp1_input.dev_attr.attr,
0132     NULL
0133 };
0134 static struct attribute_group vexpress_hwmon_group_temp = {
0135     .is_visible = vexpress_hwmon_attr_is_visible,
0136     .attrs = vexpress_hwmon_attrs_temp,
0137 };
0138 static struct vexpress_hwmon_type vexpress_hwmon_temp = {
0139     .name = "vexpress_temp",
0140     .attr_groups = (const struct attribute_group *[]) {
0141         &vexpress_hwmon_group_temp,
0142         NULL
0143     },
0144 };
0145 
0146 static DEVICE_ATTR(power1_label, 0444, vexpress_hwmon_label_show, NULL);
0147 static SENSOR_DEVICE_ATTR_RO(power1_input, vexpress_hwmon_u32, 1);
0148 static struct attribute *vexpress_hwmon_attrs_power[] = {
0149     &dev_attr_power1_label.attr,
0150     &sensor_dev_attr_power1_input.dev_attr.attr,
0151     NULL
0152 };
0153 static struct attribute_group vexpress_hwmon_group_power = {
0154     .is_visible = vexpress_hwmon_attr_is_visible,
0155     .attrs = vexpress_hwmon_attrs_power,
0156 };
0157 static struct vexpress_hwmon_type vexpress_hwmon_power = {
0158     .name = "vexpress_power",
0159     .attr_groups = (const struct attribute_group *[]) {
0160         &vexpress_hwmon_group_power,
0161         NULL
0162     },
0163 };
0164 
0165 static DEVICE_ATTR(energy1_label, 0444, vexpress_hwmon_label_show, NULL);
0166 static SENSOR_DEVICE_ATTR_RO(energy1_input, vexpress_hwmon_u64, 1);
0167 static struct attribute *vexpress_hwmon_attrs_energy[] = {
0168     &dev_attr_energy1_label.attr,
0169     &sensor_dev_attr_energy1_input.dev_attr.attr,
0170     NULL
0171 };
0172 static struct attribute_group vexpress_hwmon_group_energy = {
0173     .is_visible = vexpress_hwmon_attr_is_visible,
0174     .attrs = vexpress_hwmon_attrs_energy,
0175 };
0176 static struct vexpress_hwmon_type vexpress_hwmon_energy = {
0177     .name = "vexpress_energy",
0178     .attr_groups = (const struct attribute_group *[]) {
0179         &vexpress_hwmon_group_energy,
0180         NULL
0181     },
0182 };
0183 
0184 static const struct of_device_id vexpress_hwmon_of_match[] = {
0185 #if !defined(CONFIG_REGULATOR_VEXPRESS)
0186     {
0187         .compatible = "arm,vexpress-volt",
0188         .data = &vexpress_hwmon_volt,
0189     },
0190 #endif
0191     {
0192         .compatible = "arm,vexpress-amp",
0193         .data = &vexpress_hwmon_amp,
0194     }, {
0195         .compatible = "arm,vexpress-temp",
0196         .data = &vexpress_hwmon_temp,
0197     }, {
0198         .compatible = "arm,vexpress-power",
0199         .data = &vexpress_hwmon_power,
0200     }, {
0201         .compatible = "arm,vexpress-energy",
0202         .data = &vexpress_hwmon_energy,
0203     },
0204     {}
0205 };
0206 MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match);
0207 
0208 static int vexpress_hwmon_probe(struct platform_device *pdev)
0209 {
0210     struct vexpress_hwmon_data *data;
0211     const struct vexpress_hwmon_type *type;
0212 
0213     data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
0214     if (!data)
0215         return -ENOMEM;
0216     platform_set_drvdata(pdev, data);
0217 
0218     type = of_device_get_match_data(&pdev->dev);
0219     if (!type)
0220         return -ENODEV;
0221 
0222     data->reg = devm_regmap_init_vexpress_config(&pdev->dev);
0223     if (IS_ERR(data->reg))
0224         return PTR_ERR(data->reg);
0225 
0226     data->hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
0227             type->name, data, type->attr_groups);
0228 
0229     return PTR_ERR_OR_ZERO(data->hwmon_dev);
0230 }
0231 
0232 static struct platform_driver vexpress_hwmon_driver = {
0233     .probe = vexpress_hwmon_probe,
0234     .driver = {
0235         .name = DRVNAME,
0236         .of_match_table = vexpress_hwmon_of_match,
0237     },
0238 };
0239 
0240 module_platform_driver(vexpress_hwmon_driver);
0241 
0242 MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
0243 MODULE_DESCRIPTION("Versatile Express hwmon sensors driver");
0244 MODULE_LICENSE("GPL");
0245 MODULE_ALIAS("platform:vexpress-hwmon");