Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License version 2 as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * Copyright (C) 2012 ARM Limited |
| 12 | */ |
| 13 | |
| 14 | #define DRVNAME "vexpress-hwmon" |
| 15 | #define pr_fmt(fmt) DRVNAME ": " fmt |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/hwmon.h> |
| 20 | #include <linux/hwmon-sysfs.h> |
| 21 | #include <linux/module.h> |
Guenter Roeck | 08245ad | 2012-12-31 00:04:59 -0800 | [diff] [blame] | 22 | #include <linux/of.h> |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 23 | #include <linux/of_device.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/vexpress.h> |
| 26 | |
| 27 | struct vexpress_hwmon_data { |
| 28 | struct device *hwmon_dev; |
| 29 | struct vexpress_config_func *func; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 30 | const char *name; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static ssize_t vexpress_hwmon_name_show(struct device *dev, |
| 34 | struct device_attribute *dev_attr, char *buffer) |
| 35 | { |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 36 | struct vexpress_hwmon_data *data = dev_get_drvdata(dev); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 37 | |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 38 | return sprintf(buffer, "%s\n", data->name); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static ssize_t vexpress_hwmon_label_show(struct device *dev, |
| 42 | struct device_attribute *dev_attr, char *buffer) |
| 43 | { |
| 44 | const char *label = of_get_property(dev->of_node, "label", NULL); |
| 45 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 46 | return snprintf(buffer, PAGE_SIZE, "%s\n", label); |
| 47 | } |
| 48 | |
| 49 | static ssize_t vexpress_hwmon_u32_show(struct device *dev, |
| 50 | struct device_attribute *dev_attr, char *buffer) |
| 51 | { |
| 52 | struct vexpress_hwmon_data *data = dev_get_drvdata(dev); |
| 53 | int err; |
| 54 | u32 value; |
| 55 | |
| 56 | err = vexpress_config_read(data->func, 0, &value); |
| 57 | if (err) |
| 58 | return err; |
| 59 | |
| 60 | return snprintf(buffer, PAGE_SIZE, "%u\n", value / |
| 61 | to_sensor_dev_attr(dev_attr)->index); |
| 62 | } |
| 63 | |
| 64 | static ssize_t vexpress_hwmon_u64_show(struct device *dev, |
| 65 | struct device_attribute *dev_attr, char *buffer) |
| 66 | { |
| 67 | struct vexpress_hwmon_data *data = dev_get_drvdata(dev); |
| 68 | int err; |
| 69 | u32 value_hi, value_lo; |
| 70 | |
| 71 | err = vexpress_config_read(data->func, 0, &value_lo); |
| 72 | if (err) |
| 73 | return err; |
| 74 | |
| 75 | err = vexpress_config_read(data->func, 1, &value_hi); |
| 76 | if (err) |
| 77 | return err; |
| 78 | |
| 79 | return snprintf(buffer, PAGE_SIZE, "%llu\n", |
| 80 | div_u64(((u64)value_hi << 32) | value_lo, |
| 81 | to_sensor_dev_attr(dev_attr)->index)); |
| 82 | } |
| 83 | |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 84 | static umode_t vexpress_hwmon_attr_is_visible(struct kobject *kobj, |
| 85 | struct attribute *attr, int index) |
| 86 | { |
| 87 | struct device *dev = kobj_to_dev(kobj); |
| 88 | struct device_attribute *dev_attr = container_of(attr, |
| 89 | struct device_attribute, attr); |
| 90 | |
| 91 | if (dev_attr->show == vexpress_hwmon_label_show && |
| 92 | !of_get_property(dev->of_node, "label", NULL)) |
| 93 | return 0; |
| 94 | |
| 95 | return attr->mode; |
| 96 | } |
| 97 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 98 | static DEVICE_ATTR(name, S_IRUGO, vexpress_hwmon_name_show, NULL); |
| 99 | |
| 100 | #define VEXPRESS_HWMON_ATTRS(_name, _label_attr, _input_attr) \ |
| 101 | struct attribute *vexpress_hwmon_attrs_##_name[] = { \ |
| 102 | &dev_attr_name.attr, \ |
| 103 | &dev_attr_##_label_attr.attr, \ |
| 104 | &sensor_dev_attr_##_input_attr.dev_attr.attr, \ |
| 105 | NULL \ |
| 106 | } |
| 107 | |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 108 | struct vexpress_hwmon_type { |
| 109 | const char *name; |
| 110 | const struct attribute_group **attr_groups; |
| 111 | }; |
| 112 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 113 | #if !defined(CONFIG_REGULATOR_VEXPRESS) |
| 114 | static DEVICE_ATTR(in1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 115 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 116 | NULL, 1000); |
| 117 | static VEXPRESS_HWMON_ATTRS(volt, in1_label, in1_input); |
| 118 | static struct attribute_group vexpress_hwmon_group_volt = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 119 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 120 | .attrs = vexpress_hwmon_attrs_volt, |
| 121 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 122 | static struct vexpress_hwmon_type vexpress_hwmon_volt = { |
| 123 | .name = "vexpress_volt", |
| 124 | .attr_groups = (const struct attribute_group *[]) { |
| 125 | &vexpress_hwmon_group_volt, |
| 126 | NULL, |
| 127 | }, |
| 128 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 129 | #endif |
| 130 | |
| 131 | static DEVICE_ATTR(curr1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 132 | static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 133 | NULL, 1000); |
| 134 | static VEXPRESS_HWMON_ATTRS(amp, curr1_label, curr1_input); |
| 135 | static struct attribute_group vexpress_hwmon_group_amp = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 136 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 137 | .attrs = vexpress_hwmon_attrs_amp, |
| 138 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 139 | static struct vexpress_hwmon_type vexpress_hwmon_amp = { |
| 140 | .name = "vexpress_amp", |
| 141 | .attr_groups = (const struct attribute_group *[]) { |
| 142 | &vexpress_hwmon_group_amp, |
| 143 | NULL |
| 144 | }, |
| 145 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 146 | |
| 147 | static DEVICE_ATTR(temp1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 148 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 149 | NULL, 1000); |
| 150 | static VEXPRESS_HWMON_ATTRS(temp, temp1_label, temp1_input); |
| 151 | static struct attribute_group vexpress_hwmon_group_temp = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 152 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 153 | .attrs = vexpress_hwmon_attrs_temp, |
| 154 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 155 | static struct vexpress_hwmon_type vexpress_hwmon_temp = { |
| 156 | .name = "vexpress_temp", |
| 157 | .attr_groups = (const struct attribute_group *[]) { |
| 158 | &vexpress_hwmon_group_temp, |
| 159 | NULL |
| 160 | }, |
| 161 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 162 | |
| 163 | static DEVICE_ATTR(power1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 164 | static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 165 | NULL, 1); |
| 166 | static VEXPRESS_HWMON_ATTRS(power, power1_label, power1_input); |
| 167 | static struct attribute_group vexpress_hwmon_group_power = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 168 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 169 | .attrs = vexpress_hwmon_attrs_power, |
| 170 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 171 | static struct vexpress_hwmon_type vexpress_hwmon_power = { |
| 172 | .name = "vexpress_power", |
| 173 | .attr_groups = (const struct attribute_group *[]) { |
| 174 | &vexpress_hwmon_group_power, |
| 175 | NULL |
| 176 | }, |
| 177 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 178 | |
| 179 | static DEVICE_ATTR(energy1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 180 | static SENSOR_DEVICE_ATTR(energy1_input, S_IRUGO, vexpress_hwmon_u64_show, |
| 181 | NULL, 1); |
| 182 | static VEXPRESS_HWMON_ATTRS(energy, energy1_label, energy1_input); |
| 183 | static struct attribute_group vexpress_hwmon_group_energy = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 184 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 185 | .attrs = vexpress_hwmon_attrs_energy, |
| 186 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 187 | static struct vexpress_hwmon_type vexpress_hwmon_energy = { |
| 188 | .name = "vexpress_energy", |
| 189 | .attr_groups = (const struct attribute_group *[]) { |
| 190 | &vexpress_hwmon_group_energy, |
| 191 | NULL |
| 192 | }, |
| 193 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 194 | |
| 195 | static struct of_device_id vexpress_hwmon_of_match[] = { |
| 196 | #if !defined(CONFIG_REGULATOR_VEXPRESS) |
| 197 | { |
| 198 | .compatible = "arm,vexpress-volt", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 199 | .data = &vexpress_hwmon_volt, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 200 | }, |
| 201 | #endif |
| 202 | { |
| 203 | .compatible = "arm,vexpress-amp", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 204 | .data = &vexpress_hwmon_amp, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 205 | }, { |
| 206 | .compatible = "arm,vexpress-temp", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 207 | .data = &vexpress_hwmon_temp, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 208 | }, { |
| 209 | .compatible = "arm,vexpress-power", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 210 | .data = &vexpress_hwmon_power, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 211 | }, { |
| 212 | .compatible = "arm,vexpress-energy", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 213 | .data = &vexpress_hwmon_energy, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 214 | }, |
| 215 | {} |
| 216 | }; |
| 217 | MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match); |
| 218 | |
| 219 | static int vexpress_hwmon_probe(struct platform_device *pdev) |
| 220 | { |
| 221 | int err; |
| 222 | const struct of_device_id *match; |
| 223 | struct vexpress_hwmon_data *data; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 224 | const struct vexpress_hwmon_type *type; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 225 | |
| 226 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 227 | if (!data) |
| 228 | return -ENOMEM; |
| 229 | platform_set_drvdata(pdev, data); |
| 230 | |
| 231 | match = of_match_device(vexpress_hwmon_of_match, &pdev->dev); |
| 232 | if (!match) |
| 233 | return -ENODEV; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 234 | type = match->data; |
| 235 | data->name = type->name; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 236 | |
| 237 | data->func = vexpress_config_func_get_by_dev(&pdev->dev); |
| 238 | if (!data->func) |
| 239 | return -ENODEV; |
| 240 | |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 241 | err = sysfs_create_groups(&pdev->dev.kobj, type->attr_groups); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 242 | if (err) |
| 243 | goto error; |
| 244 | |
| 245 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
| 246 | if (IS_ERR(data->hwmon_dev)) { |
| 247 | err = PTR_ERR(data->hwmon_dev); |
| 248 | goto error; |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | |
| 253 | error: |
| 254 | sysfs_remove_group(&pdev->dev.kobj, match->data); |
| 255 | vexpress_config_func_put(data->func); |
| 256 | return err; |
| 257 | } |
| 258 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 259 | static int vexpress_hwmon_remove(struct platform_device *pdev) |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 260 | { |
| 261 | struct vexpress_hwmon_data *data = platform_get_drvdata(pdev); |
| 262 | const struct of_device_id *match; |
| 263 | |
| 264 | hwmon_device_unregister(data->hwmon_dev); |
| 265 | |
| 266 | match = of_match_device(vexpress_hwmon_of_match, &pdev->dev); |
| 267 | sysfs_remove_group(&pdev->dev.kobj, match->data); |
| 268 | |
| 269 | vexpress_config_func_put(data->func); |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static struct platform_driver vexpress_hwmon_driver = { |
| 275 | .probe = vexpress_hwmon_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 276 | .remove = vexpress_hwmon_remove, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 277 | .driver = { |
| 278 | .name = DRVNAME, |
| 279 | .owner = THIS_MODULE, |
| 280 | .of_match_table = vexpress_hwmon_of_match, |
| 281 | }, |
| 282 | }; |
| 283 | |
| 284 | module_platform_driver(vexpress_hwmon_driver); |
| 285 | |
| 286 | MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); |
| 287 | MODULE_DESCRIPTION("Versatile Express hwmon sensors driver"); |
| 288 | MODULE_LICENSE("GPL"); |
| 289 | MODULE_ALIAS("platform:vexpress-hwmon"); |