kernel: update kernel 4.4 to version 4.4.20
[oweals/openwrt.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 --- a/drivers/hwmon/lm90.c
31 +++ b/drivers/hwmon/lm90.c
32 @@ -96,6 +96,8 @@
33  #include <linux/sysfs.h>
34  #include <linux/interrupt.h>
35  #include <linux/regulator/consumer.h>
36 +#include <linux/of.h>
37 +#include <linux/thermal.h>
38  
39  /*
40   * Addresses to scan
41 @@ -119,6 +121,13 @@ static const unsigned short normal_i2c[]
42  enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
43         max6646, w83l771, max6696, sa56004, g781, tmp451 };
44  
45 +enum sensor_id {
46 +       LOCAL = 0,
47 +       REMOTE,
48 +       REMOTE2,
49 +       SENSOR_ID_END,
50 +};
51 +
52  /*
53   * The LM90 registers
54   */
55 @@ -368,6 +377,7 @@ struct lm90_data {
56         struct i2c_client *client;
57         struct device *hwmon_dev;
58         const struct attribute_group *groups[6];
59 +       struct thermal_zone_device *tz[SENSOR_ID_END];
60         struct mutex update_lock;
61         struct regulator *regulator;
62         char valid; /* zero until following fields are valid */
63 @@ -880,6 +890,24 @@ static ssize_t show_temp11(struct device
64         return sprintf(buf, "%d\n", read_temp11(dev, attr->index));
65  }
66  
67 +static int lm90_read_local_temp(void *dev, int *temp)
68 +{
69 +       *temp = read_temp11(dev, 4);
70 +       return 0;
71 +}
72 +
73 +static int lm90_read_remote_temp(void *dev, int *temp)
74 +{
75 +       *temp = read_temp11(dev, 0);
76 +       return 0;
77 +}
78 +
79 +static int lm90_read_remote2_temp(void *dev, int *temp)
80 +{
81 +       *temp = read_temp11(dev, 5);
82 +       return 0;
83 +}
84 +
85  static int write_temp11(struct device *dev, int nr, int index, long val)
86  {
87         struct {
88 @@ -1210,6 +1238,18 @@ static const struct attribute_group lm90
89         .attrs = lm90_temp3_attributes,
90  };
91  
92 +static const struct thermal_zone_of_device_ops local_temp_sensor = {
93 +       .get_temp = lm90_read_local_temp,
94 +};
95 +
96 +static const struct thermal_zone_of_device_ops remote_temp_sensor = {
97 +       .get_temp = lm90_read_remote_temp,
98 +};
99 +
100 +static const struct thermal_zone_of_device_ops remote2_temp_sensor = {
101 +       .get_temp = lm90_read_remote2_temp,
102 +};
103 +
104  /* pec used for ADM1032 only */
105  static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
106                         char *buf)
107 @@ -1659,6 +1699,30 @@ static int lm90_probe(struct i2c_client
108                 }
109         }
110  
111 +       data->tz[LOCAL] = thermal_zone_of_sensor_register(&client->dev,
112 +                                                       LOCAL,
113 +                                                       &client->dev,
114 +                                                       &local_temp_sensor);
115 +       if (IS_ERR(data->tz[LOCAL]))
116 +               data->tz[LOCAL] = NULL;
117 +
118 +       data->tz[REMOTE] = thermal_zone_of_sensor_register(&client->dev,
119 +                                                       REMOTE,
120 +                                                       &client->dev,
121 +                                                       &remote_temp_sensor);
122 +       if (IS_ERR(data->tz[REMOTE]))
123 +               data->tz[REMOTE] = NULL;
124 +
125 +       if (data->flags & LM90_HAVE_TEMP3) {
126 +               data->tz[REMOTE2] = thermal_zone_of_sensor_register(
127 +                                                       &client->dev,
128 +                                                       REMOTE2,
129 +                                                       &client->dev,
130 +                                                       &remote2_temp_sensor);
131 +               if (IS_ERR(data->tz[REMOTE2]))
132 +                       data->tz[REMOTE2] = NULL;
133 +       }
134 +
135         return 0;
136  
137  exit_unregister:
138 @@ -1674,8 +1738,11 @@ exit_restore:
139  
140  static int lm90_remove(struct i2c_client *client)
141  {
142 +       int i;
143         struct lm90_data *data = i2c_get_clientdata(client);
144  
145 +       for (i = 0; i < SENSOR_ID_END; i++)
146 +               thermal_zone_of_sensor_unregister(&client->dev, data->tz[i]);
147         hwmon_device_unregister(data->hwmon_dev);
148         device_remove_file(&client->dev, &dev_attr_pec);
149         lm90_restore_conf(client, data);