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 | |
| 46 | if (!label) |
| 47 | return -ENOENT; |
| 48 | |
| 49 | return snprintf(buffer, PAGE_SIZE, "%s\n", label); |
| 50 | } |
| 51 | |
| 52 | static ssize_t vexpress_hwmon_u32_show(struct device *dev, |
| 53 | struct device_attribute *dev_attr, char *buffer) |
| 54 | { |
| 55 | struct vexpress_hwmon_data *data = dev_get_drvdata(dev); |
| 56 | int err; |
| 57 | u32 value; |
| 58 | |
| 59 | err = vexpress_config_read(data->func, 0, &value); |
| 60 | if (err) |
| 61 | return err; |
| 62 | |
| 63 | return snprintf(buffer, PAGE_SIZE, "%u\n", value / |
| 64 | to_sensor_dev_attr(dev_attr)->index); |
| 65 | } |
| 66 | |
| 67 | static ssize_t vexpress_hwmon_u64_show(struct device *dev, |
| 68 | struct device_attribute *dev_attr, char *buffer) |
| 69 | { |
| 70 | struct vexpress_hwmon_data *data = dev_get_drvdata(dev); |
| 71 | int err; |
| 72 | u32 value_hi, value_lo; |
| 73 | |
| 74 | err = vexpress_config_read(data->func, 0, &value_lo); |
| 75 | if (err) |
| 76 | return err; |
| 77 | |
| 78 | err = vexpress_config_read(data->func, 1, &value_hi); |
| 79 | if (err) |
| 80 | return err; |
| 81 | |
| 82 | return snprintf(buffer, PAGE_SIZE, "%llu\n", |
| 83 | div_u64(((u64)value_hi << 32) | value_lo, |
| 84 | to_sensor_dev_attr(dev_attr)->index)); |
| 85 | } |
| 86 | |
| 87 | static DEVICE_ATTR(name, S_IRUGO, vexpress_hwmon_name_show, NULL); |
| 88 | |
| 89 | #define VEXPRESS_HWMON_ATTRS(_name, _label_attr, _input_attr) \ |
| 90 | struct attribute *vexpress_hwmon_attrs_##_name[] = { \ |
| 91 | &dev_attr_name.attr, \ |
| 92 | &dev_attr_##_label_attr.attr, \ |
| 93 | &sensor_dev_attr_##_input_attr.dev_attr.attr, \ |
| 94 | NULL \ |
| 95 | } |
| 96 | |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 97 | struct vexpress_hwmon_type { |
| 98 | const char *name; |
| 99 | const struct attribute_group **attr_groups; |
| 100 | }; |
| 101 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 102 | #if !defined(CONFIG_REGULATOR_VEXPRESS) |
| 103 | static DEVICE_ATTR(in1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 104 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 105 | NULL, 1000); |
| 106 | static VEXPRESS_HWMON_ATTRS(volt, in1_label, in1_input); |
| 107 | static struct attribute_group vexpress_hwmon_group_volt = { |
| 108 | .attrs = vexpress_hwmon_attrs_volt, |
| 109 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 110 | static struct vexpress_hwmon_type vexpress_hwmon_volt = { |
| 111 | .name = "vexpress_volt", |
| 112 | .attr_groups = (const struct attribute_group *[]) { |
| 113 | &vexpress_hwmon_group_volt, |
| 114 | NULL, |
| 115 | }, |
| 116 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 117 | #endif |
| 118 | |
| 119 | static DEVICE_ATTR(curr1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 120 | static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 121 | NULL, 1000); |
| 122 | static VEXPRESS_HWMON_ATTRS(amp, curr1_label, curr1_input); |
| 123 | static struct attribute_group vexpress_hwmon_group_amp = { |
| 124 | .attrs = vexpress_hwmon_attrs_amp, |
| 125 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 126 | static struct vexpress_hwmon_type vexpress_hwmon_amp = { |
| 127 | .name = "vexpress_amp", |
| 128 | .attr_groups = (const struct attribute_group *[]) { |
| 129 | &vexpress_hwmon_group_amp, |
| 130 | NULL |
| 131 | }, |
| 132 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 133 | |
| 134 | static DEVICE_ATTR(temp1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 135 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 136 | NULL, 1000); |
| 137 | static VEXPRESS_HWMON_ATTRS(temp, temp1_label, temp1_input); |
| 138 | static struct attribute_group vexpress_hwmon_group_temp = { |
| 139 | .attrs = vexpress_hwmon_attrs_temp, |
| 140 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 141 | static struct vexpress_hwmon_type vexpress_hwmon_temp = { |
| 142 | .name = "vexpress_temp", |
| 143 | .attr_groups = (const struct attribute_group *[]) { |
| 144 | &vexpress_hwmon_group_temp, |
| 145 | NULL |
| 146 | }, |
| 147 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 148 | |
| 149 | static DEVICE_ATTR(power1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 150 | static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 151 | NULL, 1); |
| 152 | static VEXPRESS_HWMON_ATTRS(power, power1_label, power1_input); |
| 153 | static struct attribute_group vexpress_hwmon_group_power = { |
| 154 | .attrs = vexpress_hwmon_attrs_power, |
| 155 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 156 | static struct vexpress_hwmon_type vexpress_hwmon_power = { |
| 157 | .name = "vexpress_power", |
| 158 | .attr_groups = (const struct attribute_group *[]) { |
| 159 | &vexpress_hwmon_group_power, |
| 160 | NULL |
| 161 | }, |
| 162 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 163 | |
| 164 | static DEVICE_ATTR(energy1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 165 | static SENSOR_DEVICE_ATTR(energy1_input, S_IRUGO, vexpress_hwmon_u64_show, |
| 166 | NULL, 1); |
| 167 | static VEXPRESS_HWMON_ATTRS(energy, energy1_label, energy1_input); |
| 168 | static struct attribute_group vexpress_hwmon_group_energy = { |
| 169 | .attrs = vexpress_hwmon_attrs_energy, |
| 170 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 171 | static struct vexpress_hwmon_type vexpress_hwmon_energy = { |
| 172 | .name = "vexpress_energy", |
| 173 | .attr_groups = (const struct attribute_group *[]) { |
| 174 | &vexpress_hwmon_group_energy, |
| 175 | NULL |
| 176 | }, |
| 177 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 178 | |
| 179 | static struct of_device_id vexpress_hwmon_of_match[] = { |
| 180 | #if !defined(CONFIG_REGULATOR_VEXPRESS) |
| 181 | { |
| 182 | .compatible = "arm,vexpress-volt", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 183 | .data = &vexpress_hwmon_volt, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 184 | }, |
| 185 | #endif |
| 186 | { |
| 187 | .compatible = "arm,vexpress-amp", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 188 | .data = &vexpress_hwmon_amp, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 189 | }, { |
| 190 | .compatible = "arm,vexpress-temp", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 191 | .data = &vexpress_hwmon_temp, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 192 | }, { |
| 193 | .compatible = "arm,vexpress-power", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 194 | .data = &vexpress_hwmon_power, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 195 | }, { |
| 196 | .compatible = "arm,vexpress-energy", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 197 | .data = &vexpress_hwmon_energy, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 198 | }, |
| 199 | {} |
| 200 | }; |
| 201 | MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match); |
| 202 | |
| 203 | static int vexpress_hwmon_probe(struct platform_device *pdev) |
| 204 | { |
| 205 | int err; |
| 206 | const struct of_device_id *match; |
| 207 | struct vexpress_hwmon_data *data; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 208 | const struct vexpress_hwmon_type *type; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 209 | |
| 210 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 211 | if (!data) |
| 212 | return -ENOMEM; |
| 213 | platform_set_drvdata(pdev, data); |
| 214 | |
| 215 | match = of_match_device(vexpress_hwmon_of_match, &pdev->dev); |
| 216 | if (!match) |
| 217 | return -ENODEV; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 218 | type = match->data; |
| 219 | data->name = type->name; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 220 | |
| 221 | data->func = vexpress_config_func_get_by_dev(&pdev->dev); |
| 222 | if (!data->func) |
| 223 | return -ENODEV; |
| 224 | |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame^] | 225 | err = sysfs_create_groups(&pdev->dev.kobj, type->attr_groups); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 226 | if (err) |
| 227 | goto error; |
| 228 | |
| 229 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
| 230 | if (IS_ERR(data->hwmon_dev)) { |
| 231 | err = PTR_ERR(data->hwmon_dev); |
| 232 | goto error; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | |
| 237 | error: |
| 238 | sysfs_remove_group(&pdev->dev.kobj, match->data); |
| 239 | vexpress_config_func_put(data->func); |
| 240 | return err; |
| 241 | } |
| 242 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 243 | static int vexpress_hwmon_remove(struct platform_device *pdev) |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 244 | { |
| 245 | struct vexpress_hwmon_data *data = platform_get_drvdata(pdev); |
| 246 | const struct of_device_id *match; |
| 247 | |
| 248 | hwmon_device_unregister(data->hwmon_dev); |
| 249 | |
| 250 | match = of_match_device(vexpress_hwmon_of_match, &pdev->dev); |
| 251 | sysfs_remove_group(&pdev->dev.kobj, match->data); |
| 252 | |
| 253 | vexpress_config_func_put(data->func); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static struct platform_driver vexpress_hwmon_driver = { |
| 259 | .probe = vexpress_hwmon_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 260 | .remove = vexpress_hwmon_remove, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 261 | .driver = { |
| 262 | .name = DRVNAME, |
| 263 | .owner = THIS_MODULE, |
| 264 | .of_match_table = vexpress_hwmon_of_match, |
| 265 | }, |
| 266 | }; |
| 267 | |
| 268 | module_platform_driver(vexpress_hwmon_driver); |
| 269 | |
| 270 | MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); |
| 271 | MODULE_DESCRIPTION("Versatile Express hwmon sensors driver"); |
| 272 | MODULE_LICENSE("GPL"); |
| 273 | MODULE_ALIAS("platform:vexpress-hwmon"); |