apm821xx: lm90 add thermal sensor interface support for device tree
[openwrt/staging/yousong.git] / target / linux / apm821xx / patches-4.4 / 912-hwmon-lm90-expose-to-thermal-fw-via-DT.patch
1 From: Wei Ni <wni@nvidia.com>
2 Subject: hwmon: lm90: expose to thermal fw via DT nodes
3
4 This patch adds to lm90 temperature sensor the possibility
5 to expose itself as thermal zone device, registered on the
6 thermal framework.
7
8 The thermal zone is built only if a device tree node
9 describing a thermal zone for this sensor is present
10 inside the lm90 DT node. Otherwise, the driver behavior
11 will be the same.
12
13 Discussed in:
14 http://www.gossamer-threads.com/lists/linux/kernel/1992853
15
16 BUG=chrome-os-partner:30834
17 TEST=Verified. Build and boot up system.
18
19 Signed-off-by: Wei Ni <wni@nvidia.com>
20 Reviewed-on: https://chromium-review.googlesource.com/181447
21 Reviewed-by: Dylan Reid <dgreid@chromium.org>
22 Tested-by: Dylan Reid <dgreid@chromium.org>
23 Commit-Queue: Dylan Reid <dgreid@chromium.org>
24 Change-Id: Id356b94d7e8f4b49ec15e46b17a1fa2ff0cbf8cf
25 Reviewed-on: https://chromium-review.googlesource.com/212414
26 Tested-by: Wei Ni <wni.nvidia@gmail.com>
27 Reviewed-by: Olof Johansson <olofj@chromium.org>
28 Commit-Queue: Olof Johansson <olofj@chromium.org>
29 ---
30 diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c
31 index fb9e224..c54d3c8 100644
32 --- a/drivers/hwmon/lm90.c
33 +++ b/drivers/hwmon/lm90.c
34 @@ -96,6 +96,8 @@
35 #include <linux/sysfs.h>
36 #include <linux/interrupt.h>
37 #include <linux/regulator/consumer.h>
38 +#include <linux/of.h>
39 +#include <linux/thermal.h>
40
41 /*
42 * Addresses to scan
43 @@ -118,6 +120,13 @@
44
45 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
46 max6646, w83l771, max6696, sa56004, g781, tmp451 };
47 +
48 +enum sensor_id {
49 + LOCAL = 0,
50 + REMOTE,
51 + REMOTE2,
52 + SENSOR_ID_END,
53 +};
54
55 /*
56 * The LM90 registers
57 @@ -368,6 +377,7 @@
58 struct i2c_client *client;
59 struct device *hwmon_dev;
60 const struct attribute_group *groups[6];
61 + struct thermal_zone_device *tz[SENSOR_ID_END];
62 struct mutex update_lock;
63 struct regulator *regulator;
64 char valid; /* zero until following fields are valid */
65 @@ -878,6 +888,24 @@
66 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
67
68 return sprintf(buf, "%d\n", read_temp11(dev, attr->index));
69 +}
70 +
71 +static int lm90_read_local_temp(void *dev, int *temp)
72 +{
73 + *temp = read_temp11(dev, 4);
74 + return 0;
75 +}
76 +
77 +static int lm90_read_remote_temp(void *dev, int *temp)
78 +{
79 + *temp = read_temp11(dev, 0);
80 + return 0;
81 +}
82 +
83 +static int lm90_read_remote2_temp(void *dev, int *temp)
84 +{
85 + *temp = read_temp11(dev, 5);
86 + return 0;
87 }
88
89 static int write_temp11(struct device *dev, int nr, int index, long val)
90 @@ -1150,6 +1238,18 @@
91 .attrs = lm90_temp3_attributes,
92 };
93
94 +static const struct thermal_zone_of_device_ops local_temp_sensor = {
95 + .get_temp = lm90_read_local_temp,
96 +};
97 +
98 +static const struct thermal_zone_of_device_ops remote_temp_sensor = {
99 + .get_temp = lm90_read_remote_temp,
100 +};
101 +
102 +static const struct thermal_zone_of_device_ops remote2_temp_sensor = {
103 + .get_temp = lm90_read_remote2_temp,
104 +};
105 +
106 /* pec used for ADM1032 only */
107 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
108 char *buf)
109 @@ -1599,6 +1699,30 @@
110 }
111 }
112
113 + data->tz[LOCAL] = thermal_zone_of_sensor_register(&client->dev,
114 + LOCAL,
115 + &client->dev,
116 + &local_temp_sensor);
117 + if (IS_ERR(data->tz[LOCAL]))
118 + data->tz[LOCAL] = NULL;
119 +
120 + data->tz[REMOTE] = thermal_zone_of_sensor_register(&client->dev,
121 + REMOTE,
122 + &client->dev,
123 + &remote_temp_sensor);
124 + if (IS_ERR(data->tz[REMOTE]))
125 + data->tz[REMOTE] = NULL;
126 +
127 + if (data->flags & LM90_HAVE_TEMP3) {
128 + data->tz[REMOTE2] = thermal_zone_of_sensor_register(
129 + &client->dev,
130 + REMOTE2,
131 + &client->dev,
132 + &remote2_temp_sensor);
133 + if (IS_ERR(data->tz[REMOTE2]))
134 + data->tz[REMOTE2] = NULL;
135 + }
136 +
137 return 0;
138
139 exit_unregister:
140 @@ -1674,8 +1726,11 @@
141
142 static int lm90_remove(struct i2c_client *client)
143 {
144 + int i;
145 struct lm90_data *data = i2c_get_clientdata(client);
146
147 + for (i = 0; i < SENSOR_ID_END; i++)
148 + thermal_zone_of_sensor_unregister(&client->dev, data->tz[i]);
149 hwmon_device_unregister(data->hwmon_dev);
150 device_remove_file(&client->dev, &dev_attr_pec);
151 lm90_restore_conf(client, data);