c7d1d142342709839159f7b05e5d3e588927e18d
[linux-2.6-microblaze.git] / drivers / hwmon / scpi-hwmon.c
1 /*
2  * System Control and Power Interface(SCPI) based hwmon sensor driver
3  *
4  * Copyright (C) 2015 ARM Ltd.
5  * Punit Agrawal <punit.agrawal@arm.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12  * kind, whether express or implied; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/hwmon.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/scpi_protocol.h>
21 #include <linux/slab.h>
22 #include <linux/sysfs.h>
23
24 struct sensor_data {
25         struct scpi_sensor_info info;
26         struct device_attribute dev_attr_input;
27         struct device_attribute dev_attr_label;
28         char input[20];
29         char label[20];
30 };
31
32 struct scpi_sensors {
33         struct scpi_ops *scpi_ops;
34         struct sensor_data *data;
35         struct attribute **attrs;
36         struct attribute_group group;
37         const struct attribute_group *groups[2];
38 };
39
40 /* hwmon callback functions */
41 static ssize_t
42 scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf)
43 {
44         struct scpi_sensors *scpi_sensors = dev_get_drvdata(dev);
45         struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
46         struct sensor_data *sensor;
47         u32 value;
48         int ret;
49
50         sensor = container_of(attr, struct sensor_data, dev_attr_input);
51
52         ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
53         if (ret)
54                 return ret;
55
56         return sprintf(buf, "%u\n", value);
57 }
58
59 static ssize_t
60 scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf)
61 {
62         struct sensor_data *sensor;
63
64         sensor = container_of(attr, struct sensor_data, dev_attr_label);
65
66         return sprintf(buf, "%s\n", sensor->info.name);
67 }
68
69 static int scpi_hwmon_probe(struct platform_device *pdev)
70 {
71         u16 nr_sensors, i;
72         int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0;
73         struct scpi_ops *scpi_ops;
74         struct device *hwdev, *dev = &pdev->dev;
75         struct scpi_sensors *scpi_sensors;
76         int ret;
77
78         scpi_ops = get_scpi_ops();
79         if (!scpi_ops)
80                 return -EPROBE_DEFER;
81
82         ret = scpi_ops->sensor_get_capability(&nr_sensors);
83         if (ret)
84                 return ret;
85
86         if (!nr_sensors)
87                 return -ENODEV;
88
89         scpi_sensors = devm_kzalloc(dev, sizeof(*scpi_sensors), GFP_KERNEL);
90         if (!scpi_sensors)
91                 return -ENOMEM;
92
93         scpi_sensors->data = devm_kcalloc(dev, nr_sensors,
94                                    sizeof(*scpi_sensors->data), GFP_KERNEL);
95         if (!scpi_sensors->data)
96                 return -ENOMEM;
97
98         scpi_sensors->attrs = devm_kcalloc(dev, (nr_sensors * 2) + 1,
99                                    sizeof(*scpi_sensors->attrs), GFP_KERNEL);
100         if (!scpi_sensors->attrs)
101                 return -ENOMEM;
102
103         scpi_sensors->scpi_ops = scpi_ops;
104
105         for (i = 0; i < nr_sensors; i++) {
106                 struct sensor_data *sensor = &scpi_sensors->data[i];
107
108                 ret = scpi_ops->sensor_get_info(i, &sensor->info);
109                 if (ret)
110                         return ret;
111
112                 switch (sensor->info.class) {
113                 case TEMPERATURE:
114                         snprintf(sensor->input, sizeof(sensor->input),
115                                  "temp%d_input", num_temp + 1);
116                         snprintf(sensor->label, sizeof(sensor->input),
117                                  "temp%d_label", num_temp + 1);
118                         num_temp++;
119                         break;
120                 case VOLTAGE:
121                         snprintf(sensor->input, sizeof(sensor->input),
122                                  "in%d_input", num_volt);
123                         snprintf(sensor->label, sizeof(sensor->input),
124                                  "in%d_label", num_volt);
125                         num_volt++;
126                         break;
127                 case CURRENT:
128                         snprintf(sensor->input, sizeof(sensor->input),
129                                  "curr%d_input", num_current + 1);
130                         snprintf(sensor->label, sizeof(sensor->input),
131                                  "curr%d_label", num_current + 1);
132                         num_current++;
133                         break;
134                 case POWER:
135                         snprintf(sensor->input, sizeof(sensor->input),
136                                  "power%d_input", num_power + 1);
137                         snprintf(sensor->label, sizeof(sensor->input),
138                                  "power%d_label", num_power + 1);
139                         num_power++;
140                         break;
141                 default:
142                         break;
143                 }
144
145                 sensor->dev_attr_input.attr.mode = S_IRUGO;
146                 sensor->dev_attr_input.show = scpi_show_sensor;
147                 sensor->dev_attr_input.attr.name = sensor->input;
148
149                 sensor->dev_attr_label.attr.mode = S_IRUGO;
150                 sensor->dev_attr_label.show = scpi_show_label;
151                 sensor->dev_attr_label.attr.name = sensor->label;
152
153                 scpi_sensors->attrs[i << 1] = &sensor->dev_attr_input.attr;
154                 scpi_sensors->attrs[(i << 1) + 1] = &sensor->dev_attr_label.attr;
155
156                 sysfs_attr_init(scpi_sensors->attrs[i << 1]);
157                 sysfs_attr_init(scpi_sensors->attrs[(i << 1) + 1]);
158         }
159
160         scpi_sensors->group.attrs = scpi_sensors->attrs;
161         scpi_sensors->groups[0] = &scpi_sensors->group;
162
163         hwdev = devm_hwmon_device_register_with_groups(dev,
164                         "scpi_sensors", scpi_sensors, scpi_sensors->groups);
165
166         return PTR_ERR_OR_ZERO(hwdev);
167 }
168
169 static const struct of_device_id scpi_of_match[] = {
170         {.compatible = "arm,scpi-sensors"},
171         {},
172 };
173
174 static struct platform_driver scpi_hwmon_platdrv = {
175         .driver = {
176                 .name   = "scpi-hwmon",
177                 .owner  = THIS_MODULE,
178                 .of_match_table = scpi_of_match,
179         },
180         .probe          = scpi_hwmon_probe,
181 };
182 module_platform_driver(scpi_hwmon_platdrv);
183
184 MODULE_AUTHOR("Punit Agrawal <punit.agrawal@arm.com>");
185 MODULE_DESCRIPTION("ARM SCPI HWMON interface driver");
186 MODULE_LICENSE("GPL v2");