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; |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 29 | struct regmap *reg; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 32 | static ssize_t vexpress_hwmon_label_show(struct device *dev, |
| 33 | struct device_attribute *dev_attr, char *buffer) |
| 34 | { |
| 35 | const char *label = of_get_property(dev->of_node, "label", NULL); |
| 36 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 37 | return snprintf(buffer, PAGE_SIZE, "%s\n", label); |
| 38 | } |
| 39 | |
| 40 | static ssize_t vexpress_hwmon_u32_show(struct device *dev, |
| 41 | struct device_attribute *dev_attr, char *buffer) |
| 42 | { |
| 43 | struct vexpress_hwmon_data *data = dev_get_drvdata(dev); |
| 44 | int err; |
| 45 | u32 value; |
| 46 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 47 | err = regmap_read(data->reg, 0, &value); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 48 | if (err) |
| 49 | return err; |
| 50 | |
| 51 | return snprintf(buffer, PAGE_SIZE, "%u\n", value / |
| 52 | to_sensor_dev_attr(dev_attr)->index); |
| 53 | } |
| 54 | |
| 55 | static ssize_t vexpress_hwmon_u64_show(struct device *dev, |
| 56 | struct device_attribute *dev_attr, char *buffer) |
| 57 | { |
| 58 | struct vexpress_hwmon_data *data = dev_get_drvdata(dev); |
| 59 | int err; |
| 60 | u32 value_hi, value_lo; |
| 61 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 62 | err = regmap_read(data->reg, 0, &value_lo); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 63 | if (err) |
| 64 | return err; |
| 65 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 66 | err = regmap_read(data->reg, 1, &value_hi); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 67 | if (err) |
| 68 | return err; |
| 69 | |
| 70 | return snprintf(buffer, PAGE_SIZE, "%llu\n", |
| 71 | div_u64(((u64)value_hi << 32) | value_lo, |
| 72 | to_sensor_dev_attr(dev_attr)->index)); |
| 73 | } |
| 74 | |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 75 | static umode_t vexpress_hwmon_attr_is_visible(struct kobject *kobj, |
| 76 | struct attribute *attr, int index) |
| 77 | { |
| 78 | struct device *dev = kobj_to_dev(kobj); |
| 79 | struct device_attribute *dev_attr = container_of(attr, |
| 80 | struct device_attribute, attr); |
| 81 | |
| 82 | if (dev_attr->show == vexpress_hwmon_label_show && |
| 83 | !of_get_property(dev->of_node, "label", NULL)) |
| 84 | return 0; |
| 85 | |
| 86 | return attr->mode; |
| 87 | } |
| 88 | |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 89 | struct vexpress_hwmon_type { |
| 90 | const char *name; |
| 91 | const struct attribute_group **attr_groups; |
| 92 | }; |
| 93 | |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 94 | #if !defined(CONFIG_REGULATOR_VEXPRESS) |
| 95 | static DEVICE_ATTR(in1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 96 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 97 | NULL, 1000); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 98 | static struct attribute *vexpress_hwmon_attrs_volt[] = { |
| 99 | &dev_attr_in1_label.attr, |
| 100 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 101 | NULL |
| 102 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 103 | static struct attribute_group vexpress_hwmon_group_volt = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 104 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 105 | .attrs = vexpress_hwmon_attrs_volt, |
| 106 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 107 | static struct vexpress_hwmon_type vexpress_hwmon_volt = { |
| 108 | .name = "vexpress_volt", |
| 109 | .attr_groups = (const struct attribute_group *[]) { |
| 110 | &vexpress_hwmon_group_volt, |
| 111 | NULL, |
| 112 | }, |
| 113 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 114 | #endif |
| 115 | |
| 116 | static DEVICE_ATTR(curr1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 117 | static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 118 | NULL, 1000); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 119 | static struct attribute *vexpress_hwmon_attrs_amp[] = { |
| 120 | &dev_attr_curr1_label.attr, |
| 121 | &sensor_dev_attr_curr1_input.dev_attr.attr, |
| 122 | NULL |
| 123 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 124 | static struct attribute_group vexpress_hwmon_group_amp = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 125 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 126 | .attrs = vexpress_hwmon_attrs_amp, |
| 127 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 128 | static struct vexpress_hwmon_type vexpress_hwmon_amp = { |
| 129 | .name = "vexpress_amp", |
| 130 | .attr_groups = (const struct attribute_group *[]) { |
| 131 | &vexpress_hwmon_group_amp, |
| 132 | NULL |
| 133 | }, |
| 134 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 135 | |
| 136 | static DEVICE_ATTR(temp1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 137 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 138 | NULL, 1000); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 139 | static struct attribute *vexpress_hwmon_attrs_temp[] = { |
| 140 | &dev_attr_temp1_label.attr, |
| 141 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 142 | NULL |
| 143 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 144 | static struct attribute_group vexpress_hwmon_group_temp = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 145 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 146 | .attrs = vexpress_hwmon_attrs_temp, |
| 147 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 148 | static struct vexpress_hwmon_type vexpress_hwmon_temp = { |
| 149 | .name = "vexpress_temp", |
| 150 | .attr_groups = (const struct attribute_group *[]) { |
| 151 | &vexpress_hwmon_group_temp, |
| 152 | NULL |
| 153 | }, |
| 154 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 155 | |
| 156 | static DEVICE_ATTR(power1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 157 | static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, vexpress_hwmon_u32_show, |
| 158 | NULL, 1); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 159 | static struct attribute *vexpress_hwmon_attrs_power[] = { |
| 160 | &dev_attr_power1_label.attr, |
| 161 | &sensor_dev_attr_power1_input.dev_attr.attr, |
| 162 | NULL |
| 163 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 164 | static struct attribute_group vexpress_hwmon_group_power = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 165 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 166 | .attrs = vexpress_hwmon_attrs_power, |
| 167 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 168 | static struct vexpress_hwmon_type vexpress_hwmon_power = { |
| 169 | .name = "vexpress_power", |
| 170 | .attr_groups = (const struct attribute_group *[]) { |
| 171 | &vexpress_hwmon_group_power, |
| 172 | NULL |
| 173 | }, |
| 174 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 175 | |
| 176 | static DEVICE_ATTR(energy1_label, S_IRUGO, vexpress_hwmon_label_show, NULL); |
| 177 | static SENSOR_DEVICE_ATTR(energy1_input, S_IRUGO, vexpress_hwmon_u64_show, |
| 178 | NULL, 1); |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 179 | static struct attribute *vexpress_hwmon_attrs_energy[] = { |
| 180 | &dev_attr_energy1_label.attr, |
| 181 | &sensor_dev_attr_energy1_input.dev_attr.attr, |
| 182 | NULL |
| 183 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 184 | static struct attribute_group vexpress_hwmon_group_energy = { |
Pawel Moll | b2e5411 | 2014-04-23 18:27:05 +0100 | [diff] [blame] | 185 | .is_visible = vexpress_hwmon_attr_is_visible, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 186 | .attrs = vexpress_hwmon_attrs_energy, |
| 187 | }; |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 188 | static struct vexpress_hwmon_type vexpress_hwmon_energy = { |
| 189 | .name = "vexpress_energy", |
| 190 | .attr_groups = (const struct attribute_group *[]) { |
| 191 | &vexpress_hwmon_group_energy, |
| 192 | NULL |
| 193 | }, |
| 194 | }; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 195 | |
| 196 | static struct of_device_id vexpress_hwmon_of_match[] = { |
| 197 | #if !defined(CONFIG_REGULATOR_VEXPRESS) |
| 198 | { |
| 199 | .compatible = "arm,vexpress-volt", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 200 | .data = &vexpress_hwmon_volt, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 201 | }, |
| 202 | #endif |
| 203 | { |
| 204 | .compatible = "arm,vexpress-amp", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 205 | .data = &vexpress_hwmon_amp, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 206 | }, { |
| 207 | .compatible = "arm,vexpress-temp", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 208 | .data = &vexpress_hwmon_temp, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 209 | }, { |
| 210 | .compatible = "arm,vexpress-power", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 211 | .data = &vexpress_hwmon_power, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 212 | }, { |
| 213 | .compatible = "arm,vexpress-energy", |
Pawel Moll | 52feaca | 2014-04-23 18:27:04 +0100 | [diff] [blame] | 214 | .data = &vexpress_hwmon_energy, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 215 | }, |
| 216 | {} |
| 217 | }; |
| 218 | MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match); |
| 219 | |
| 220 | static int vexpress_hwmon_probe(struct platform_device *pdev) |
| 221 | { |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 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; |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 235 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 236 | data->reg = devm_regmap_init_vexpress_config(&pdev->dev); |
| 237 | if (IS_ERR(data->reg)) |
| 238 | return PTR_ERR(data->reg); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 239 | |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 240 | data->hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, |
| 241 | type->name, data, type->attr_groups); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 242 | |
Pawel Moll | 78cebd0 | 2014-06-12 15:14:33 +0100 | [diff] [blame] | 243 | return PTR_ERR_OR_ZERO(data->hwmon_dev); |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static struct platform_driver vexpress_hwmon_driver = { |
| 247 | .probe = vexpress_hwmon_probe, |
Pawel Moll | 48ed887 | 2012-09-17 18:40:09 +0100 | [diff] [blame] | 248 | .driver = { |
| 249 | .name = DRVNAME, |
| 250 | .owner = THIS_MODULE, |
| 251 | .of_match_table = vexpress_hwmon_of_match, |
| 252 | }, |
| 253 | }; |
| 254 | |
| 255 | module_platform_driver(vexpress_hwmon_driver); |
| 256 | |
| 257 | MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>"); |
| 258 | MODULE_DESCRIPTION("Versatile Express hwmon sensors driver"); |
| 259 | MODULE_LICENSE("GPL"); |
| 260 | MODULE_ALIAS("platform:vexpress-hwmon"); |