Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 2 | /* |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2012 ARM Limited |
| 5 | */ |
| 6 | |
| 7 | #define DRVNAME "vexpress-hwmon" |
| 8 | #define pr_fmt(fmt) DRVNAME ": " fmt |
| 9 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/hwmon.h> |
| 13 | #include <linux/hwmon-sysfs.h> |
| 14 | #include <linux/module.h> |
Guenter Roeck | 08245ad | 2012-12-31 00:04:59 -0800 | [diff] [blame] | 15 | #include <linux/of.h> |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 16 | #include <linux/of_device.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/vexpress.h> |
| 19 | |
| 20 | struct vexpress_hwmon_data { |
| 21 | struct device *hwmon_dev; |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 22 | struct regmap *reg; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 23 | }; |
| 24 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 25 | static ssize_t vexpress_hwmon_label_show(struct device *dev, |
| 26 | struct device_attribute *dev_attr, char *buffer) |
| 27 | { |
| 28 | const char *label = of_get_property(dev->of_node, "label", NULL); |
| 29 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 30 | return snprintf(buffer, PAGE_SIZE, "%s\n", label); |
| 31 | } |
| 32 | |
| 33 | static ssize_t vexpress_hwmon_u32_show(struct device *dev, |
| 34 | struct device_attribute *dev_attr, char *buffer) |
| 35 | { |
| 36 | struct vexpress_hwmon_data *data = dev_get_drvdata(dev); |
| 37 | int err; |
| 38 | u32 value; |
| 39 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 40 | err = regmap_read(data->reg, 0, &value); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 41 | if (err) |
| 42 | return err; |
| 43 | |
| 44 | return snprintf(buffer, PAGE_SIZE, "%u\n", value / |
| 45 | to_sensor_dev_attr(dev_attr)->index); |
| 46 | } |
| 47 | |
| 48 | static ssize_t vexpress_hwmon_u64_show(struct device *dev, |
| 49 | struct device_attribute *dev_attr, char *buffer) |
| 50 | { |
| 51 | struct vexpress_hwmon_data *data = dev_get_drvdata(dev); |
| 52 | int err; |
| 53 | u32 value_hi, value_lo; |
| 54 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 55 | err = regmap_read(data->reg, 0, &value_lo); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 56 | if (err) |
| 57 | return err; |
| 58 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 59 | err = regmap_read(data->reg, 1, &value_hi); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 60 | if (err) |
| 61 | return err; |
| 62 | |
| 63 | return snprintf(buffer, PAGE_SIZE, "%llu\n", |
| 64 | div_u64(((u64)value_hi << 32) | value_lo, |
| 65 | to_sensor_dev_attr(dev_attr)->index)); |
| 66 | } |
| 67 | |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 68 | static umode_t vexpress_hwmon_attr_is_visible(struct kobject *kobj, |
| 69 | struct attribute *attr, int index) |
| 70 | { |
| 71 | struct device *dev = kobj_to_dev(kobj); |
| 72 | struct device_attribute *dev_attr = container_of(attr, |
| 73 | struct device_attribute, attr); |
| 74 | |
| 75 | if (dev_attr->show == vexpress_hwmon_label_show && |
| 76 | !of_get_property(dev->of_node, "label", NULL)) |
| 77 | return 0; |
| 78 | |
| 79 | return attr->mode; |
| 80 | } |
| 81 | |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 82 | struct vexpress_hwmon_type { |
| 83 | const char *name; |
| 84 | const struct attribute_group **attr_groups; |
| 85 | }; |
| 86 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 87 | #if !defined(CONFIG_REGULATOR_VEXPRESS) |
Guenter Roeck | fa75f74 | 2018-12-10 14:02:23 -0800 | [diff] [blame] | 88 | static DEVICE_ATTR(in1_label, 0444, vexpress_hwmon_label_show, NULL); |
| 89 | static SENSOR_DEVICE_ATTR_RO(in1_input, vexpress_hwmon_u32, 1000); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 90 | static struct attribute *vexpress_hwmon_attrs_volt[] = { |
| 91 | &dev_attr_in1_label.attr, |
| 92 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 93 | NULL |
| 94 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 95 | static struct attribute_group vexpress_hwmon_group_volt = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 96 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 97 | .attrs = vexpress_hwmon_attrs_volt, |
| 98 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 99 | static struct vexpress_hwmon_type vexpress_hwmon_volt = { |
| 100 | .name = "vexpress_volt", |
| 101 | .attr_groups = (const struct attribute_group *[]) { |
| 102 | &vexpress_hwmon_group_volt, |
| 103 | NULL, |
| 104 | }, |
| 105 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 106 | #endif |
| 107 | |
Guenter Roeck | fa75f74 | 2018-12-10 14:02:23 -0800 | [diff] [blame] | 108 | static DEVICE_ATTR(curr1_label, 0444, vexpress_hwmon_label_show, NULL); |
| 109 | static SENSOR_DEVICE_ATTR_RO(curr1_input, vexpress_hwmon_u32, 1000); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 110 | static struct attribute *vexpress_hwmon_attrs_amp[] = { |
| 111 | &dev_attr_curr1_label.attr, |
| 112 | &sensor_dev_attr_curr1_input.dev_attr.attr, |
| 113 | NULL |
| 114 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 115 | static struct attribute_group vexpress_hwmon_group_amp = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 116 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 117 | .attrs = vexpress_hwmon_attrs_amp, |
| 118 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 119 | static struct vexpress_hwmon_type vexpress_hwmon_amp = { |
| 120 | .name = "vexpress_amp", |
| 121 | .attr_groups = (const struct attribute_group *[]) { |
| 122 | &vexpress_hwmon_group_amp, |
| 123 | NULL |
| 124 | }, |
| 125 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 126 | |
Guenter Roeck | fa75f74 | 2018-12-10 14:02:23 -0800 | [diff] [blame] | 127 | static DEVICE_ATTR(temp1_label, 0444, vexpress_hwmon_label_show, NULL); |
| 128 | static SENSOR_DEVICE_ATTR_RO(temp1_input, vexpress_hwmon_u32, 1000); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 129 | static struct attribute *vexpress_hwmon_attrs_temp[] = { |
| 130 | &dev_attr_temp1_label.attr, |
| 131 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 132 | NULL |
| 133 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 134 | static struct attribute_group vexpress_hwmon_group_temp = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 135 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 136 | .attrs = vexpress_hwmon_attrs_temp, |
| 137 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 138 | static struct vexpress_hwmon_type vexpress_hwmon_temp = { |
| 139 | .name = "vexpress_temp", |
| 140 | .attr_groups = (const struct attribute_group *[]) { |
| 141 | &vexpress_hwmon_group_temp, |
| 142 | NULL |
| 143 | }, |
| 144 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 145 | |
Guenter Roeck | fa75f74 | 2018-12-10 14:02:23 -0800 | [diff] [blame] | 146 | static DEVICE_ATTR(power1_label, 0444, vexpress_hwmon_label_show, NULL); |
| 147 | static SENSOR_DEVICE_ATTR_RO(power1_input, vexpress_hwmon_u32, 1); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 148 | static struct attribute *vexpress_hwmon_attrs_power[] = { |
| 149 | &dev_attr_power1_label.attr, |
| 150 | &sensor_dev_attr_power1_input.dev_attr.attr, |
| 151 | NULL |
| 152 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 153 | static struct attribute_group vexpress_hwmon_group_power = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 154 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 155 | .attrs = vexpress_hwmon_attrs_power, |
| 156 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 157 | static struct vexpress_hwmon_type vexpress_hwmon_power = { |
| 158 | .name = "vexpress_power", |
| 159 | .attr_groups = (const struct attribute_group *[]) { |
| 160 | &vexpress_hwmon_group_power, |
| 161 | NULL |
| 162 | }, |
| 163 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 164 | |
Guenter Roeck | fa75f74 | 2018-12-10 14:02:23 -0800 | [diff] [blame] | 165 | static DEVICE_ATTR(energy1_label, 0444, vexpress_hwmon_label_show, NULL); |
| 166 | static SENSOR_DEVICE_ATTR_RO(energy1_input, vexpress_hwmon_u64, 1); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 167 | static struct attribute *vexpress_hwmon_attrs_energy[] = { |
| 168 | &dev_attr_energy1_label.attr, |
| 169 | &sensor_dev_attr_energy1_input.dev_attr.attr, |
| 170 | NULL |
| 171 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 172 | static struct attribute_group vexpress_hwmon_group_energy = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 173 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 174 | .attrs = vexpress_hwmon_attrs_energy, |
| 175 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 176 | static struct vexpress_hwmon_type vexpress_hwmon_energy = { |
| 177 | .name = "vexpress_energy", |
| 178 | .attr_groups = (const struct attribute_group *[]) { |
| 179 | &vexpress_hwmon_group_energy, |
| 180 | NULL |
| 181 | }, |
| 182 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 183 | |
Fabian Frederick | d720aca | 2015-03-16 20:54:36 +0100 | [diff] [blame] | 184 | static const struct of_device_id vexpress_hwmon_of_match[] = { |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 185 | #if !defined(CONFIG_REGULATOR_VEXPRESS) |
| 186 | { |
| 187 | .compatible = "arm,vexpress-volt", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 188 | .data = &vexpress_hwmon_volt, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 189 | }, |
| 190 | #endif |
| 191 | { |
| 192 | .compatible = "arm,vexpress-amp", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 193 | .data = &vexpress_hwmon_amp, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 194 | }, { |
| 195 | .compatible = "arm,vexpress-temp", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 196 | .data = &vexpress_hwmon_temp, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 197 | }, { |
| 198 | .compatible = "arm,vexpress-power", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 199 | .data = &vexpress_hwmon_power, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 200 | }, { |
| 201 | .compatible = "arm,vexpress-energy", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 202 | .data = &vexpress_hwmon_energy, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 203 | }, |
| 204 | {} |
| 205 | }; |
| 206 | MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match); |
| 207 | |
| 208 | static int vexpress_hwmon_probe(struct platform_device *pdev) |
| 209 | { |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 210 | const struct of_device_id *match; |
| 211 | struct vexpress_hwmon_data *data; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 212 | const struct vexpress_hwmon_type *type; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 213 | |
| 214 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 215 | if (!data) |
| 216 | return -ENOMEM; |
| 217 | platform_set_drvdata(pdev, data); |
| 218 | |
| 219 | match = of_match_device(vexpress_hwmon_of_match, &pdev->dev); |
| 220 | if (!match) |
| 221 | return -ENODEV; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 222 | type = match->data; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 223 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 224 | data->reg = devm_regmap_init_vexpress_config(&pdev->dev); |
| 225 | if (IS_ERR(data->reg)) |
| 226 | return PTR_ERR(data->reg); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 227 | |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 228 | data->hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, |
| 229 | type->name, data, type->attr_groups); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 230 | |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 231 | return PTR_ERR_OR_ZERO(data->hwmon_dev); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static struct platform_driver vexpress_hwmon_driver = { |
| 235 | .probe = vexpress_hwmon_probe, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 236 | .driver = { |
| 237 | .name = DRVNAME, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 238 | .of_match_table = vexpress_hwmon_of_match, |
| 239 | }, |
| 240 | }; |
| 241 | |
| 242 | module_platform_driver(vexpress_hwmon_driver); |
| 243 | |
| 244 | MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); |
| 245 | MODULE_DESCRIPTION("Versatile Express hwmon sensors driver"); |
| 246 | MODULE_LICENSE("GPL"); |
| 247 | MODULE_ALIAS("platform:vexpress-hwmon"); |