apm821xx: lm90 add thermal sensor interface support for device tree
[openwrt/staging/yousong.git] / target / linux / apm821xx / patches-4.4 / 911-hwmon-lm90-split-set-and-show-temp-as-common-codes.patch
1 From: Wei Ni <wni@nvidia.com>
2 Subject: hwmon: lm90: split set and show temp as common codes
3
4 Split set and show temp codes as common functions, so we can use
5 it directly when implement linux thermal framework.
6 And handle error return value for the lm90_select_remote_channel
7 and write_tempx, then set_temp8 and set_temp11 could return it
8 to user-space.
9
10 Discussed in:
11 http://www.spinics.net/lists/linux-tegra/msg14020.html .
12 Applied with Jean.
13
14 BUG=chrome-os-partner:30834
15 TEST=None
16
17 Signed-off-by: Wei Ni <wni@nvidia.com>
18 Signed-off-by: Jean Delvare <khali@linux-fr.org>
19 Reviewed-on: https://chromium-review.googlesource.com/175114
20 Tested-by: Wei Ni <wni.nvidia@gmail.com>
21 Commit-Queue: Dylan Reid <dgreid@chromium.org>
22 Reviewed-by: Dylan Reid <dgreid@chromium.org>
23 (cherry picked from commit 614a96decdc7a3784128c9f21c5471367e2c627d)
24 Change-Id: Idbe3948812c6737cba94810cd147c29cc527c3cf
25 Reviewed-on: https://chromium-review.googlesource.com/212413
26 Reviewed-by: Olof Johansson <olofj@chromium.org>
27 Commit-Queue: Olof Johansson <olofj@chromium.org>
28 ---
29 diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c
30 index c9ff08d..fb9e224 100644
31 --- a/drivers/hwmon/lm90.c
32 +++ b/drivers/hwmon/lm90.c
33 @@ -473,20 +473,29 @@
34 * various registers have different meanings as a result of selecting a
35 * non-default remote channel.
36 */
37 -static inline void lm90_select_remote_channel(struct i2c_client *client,
38 - struct lm90_data *data,
39 - int channel)
40 +static inline int lm90_select_remote_channel(struct i2c_client *client,
41 + struct lm90_data *data,
42 + int channel)
43 {
44 u8 config;
45 + int err;
46
47 if (data->kind == max6696) {
48 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
49 config &= ~0x08;
50 if (channel)
51 config |= 0x08;
52 - i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
53 - config);
54 + err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
55 + config);
56 + if (err < 0) {
57 + dev_err(&client->dev,
58 + "Failed to select remote channel %d, err %d\n",
59 + channel, err);
60 + return err;
61 + }
62 }
63 +
64 + return 0;
65 }
66
67 /*
68 @@ -759,29 +768,34 @@
69 * Sysfs stuff
70 */
71
72 -static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
73 - char *buf)
74 +static int read_temp8(struct device *dev, int index)
75 {
76 - struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
77 struct lm90_data *data = lm90_update_device(dev);
78 int temp;
79
80 if (data->kind == adt7461 || data->kind == tmp451)
81 - temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
82 + temp = temp_from_u8_adt7461(data, data->temp8[index]);
83 else if (data->kind == max6646)
84 - temp = temp_from_u8(data->temp8[attr->index]);
85 + temp = temp_from_u8(data->temp8[index]);
86 else
87 - temp = temp_from_s8(data->temp8[attr->index]);
88 + temp = temp_from_s8(data->temp8[index]);
89
90 /* +16 degrees offset for temp2 for the LM99 */
91 - if (data->kind == lm99 && attr->index == 3)
92 + if (data->kind == lm99 && index == 3)
93 temp += 16000;
94
95 - return sprintf(buf, "%d\n", temp);
96 + return temp;
97 }
98
99 -static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
100 - const char *buf, size_t count)
101 +static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
102 + char *buf)
103 +{
104 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
105 +
106 + return sprintf(buf, "%d\n", read_temp8(dev, attr->index));
107 +}
108 +
109 +static int write_temp8(struct device *dev, int index, long val)
110 {
111 static const u8 reg[TEMP8_REG_NUM] = {
112 LM90_REG_W_LOCAL_LOW,
113 @@ -794,10 +808,37 @@
114 MAX6659_REG_W_REMOTE_EMERG,
115 };
116
117 - struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
118 struct lm90_data *data = dev_get_drvdata(dev);
119 struct i2c_client *client = data->client;
120 - int nr = attr->index;
121 + int err;
122 +
123 + /* +16 degrees offset for temp2 for the LM99 */
124 + if (data->kind == lm99 && index == 3)
125 + val -= 16000;
126 +
127 + mutex_lock(&data->update_lock);
128 + if (data->kind == adt7461 || data->kind == tmp451)
129 + data->temp8[index] = temp_to_u8_adt7461(data, val);
130 + else if (data->kind == max6646)
131 + data->temp8[index] = temp_to_u8(val);
132 + else
133 + data->temp8[index] = temp_to_s8(val);
134 +
135 + if ((err = lm90_select_remote_channel(client, data, index >= 6)) ||
136 + (err = i2c_smbus_write_byte_data(client, reg[index],
137 + data->temp8[index])) ||
138 + (err = lm90_select_remote_channel(client, data, 0)))
139 + dev_err(dev, "write_temp8 failed, err %d\n", err);
140 + mutex_unlock(&data->update_lock);
141 +
142 + return err;
143 +}
144 +
145 +static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
146 + const char *buf, size_t count)
147 +{
148 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
149 + int index = attr->index;
150 long val;
151 int err;
152
153 @@ -805,49 +846,41 @@
154 if (err < 0)
155 return err;
156
157 - /* +16 degrees offset for temp2 for the LM99 */
158 - if (data->kind == lm99 && attr->index == 3)
159 - val -= 16000;
160 + err = write_temp8(dev, index, val);
161 + if (err < 0)
162 + return err;
163
164 - mutex_lock(&data->update_lock);
165 - if (data->kind == adt7461 || data->kind == tmp451)
166 - data->temp8[nr] = temp_to_u8_adt7461(data, val);
167 - else if (data->kind == max6646)
168 - data->temp8[nr] = temp_to_u8(val);
169 - else
170 - data->temp8[nr] = temp_to_s8(val);
171 -
172 - lm90_select_remote_channel(client, data, nr >= 6);
173 - i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
174 - lm90_select_remote_channel(client, data, 0);
175 -
176 - mutex_unlock(&data->update_lock);
177 return count;
178 +}
179 +
180 +static int read_temp11(struct device *dev, int index)
181 +{
182 + struct lm90_data *data = lm90_update_device(dev);
183 + int temp;
184 +
185 + if (data->kind == adt7461 || data->kind == tmp451)
186 + temp = temp_from_u16_adt7461(data, data->temp11[index]);
187 + else if (data->kind == max6646)
188 + temp = temp_from_u16(data->temp11[index]);
189 + else
190 + temp = temp_from_s16(data->temp11[index]);
191 +
192 + /* +16 degrees offset for temp2 for the LM99 */
193 + if (data->kind == lm99 && index <= 2)
194 + temp += 16000;
195 +
196 + return temp;
197 }
198
199 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
200 char *buf)
201 {
202 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
203 - struct lm90_data *data = lm90_update_device(dev);
204 - int temp;
205
206 - if (data->kind == adt7461 || data->kind == tmp451)
207 - temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
208 - else if (data->kind == max6646)
209 - temp = temp_from_u16(data->temp11[attr->index]);
210 - else
211 - temp = temp_from_s16(data->temp11[attr->index]);
212 -
213 - /* +16 degrees offset for temp2 for the LM99 */
214 - if (data->kind == lm99 && attr->index <= 2)
215 - temp += 16000;
216 -
217 - return sprintf(buf, "%d\n", temp);
218 + return sprintf(buf, "%d\n", read_temp11(dev, attr->index));
219 }
220
221 -static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
222 - const char *buf, size_t count)
223 +static int write_temp11(struct device *dev, int nr, int index, long val)
224 {
225 struct {
226 u8 high;
227 @@ -861,17 +894,9 @@
228 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
229 };
230
231 - struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
232 struct lm90_data *data = dev_get_drvdata(dev);
233 struct i2c_client *client = data->client;
234 - int nr = attr->nr;
235 - int index = attr->index;
236 - long val;
237 int err;
238 -
239 - err = kstrtol(buf, 10, &val);
240 - if (err < 0)
241 - return err;
242
243 /* +16 degrees offset for temp2 for the LM99 */
244 if (data->kind == lm99 && index <= 2)
245 @@ -887,15 +912,50 @@
246 else
247 data->temp11[index] = temp_to_s8(val) << 8;
248
249 - lm90_select_remote_channel(client, data, reg[nr].channel);
250 - i2c_smbus_write_byte_data(client, reg[nr].high,
251 - data->temp11[index] >> 8);
252 - if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
253 - i2c_smbus_write_byte_data(client, reg[nr].low,
254 - data->temp11[index] & 0xff);
255 - lm90_select_remote_channel(client, data, 0);
256 + err = lm90_select_remote_channel(client, data, reg[nr].channel);
257 + if (err)
258 + goto error;
259 +
260 + err = i2c_smbus_write_byte_data(client, reg[nr].high,
261 + data->temp11[index] >> 8);
262 + if (err)
263 + goto error;
264 +
265 + if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
266 + err = i2c_smbus_write_byte_data(client, reg[nr].low,
267 + data->temp11[index] & 0xff);
268 + if (err)
269 + goto error;
270 + }
271 +
272 + err = lm90_select_remote_channel(client, data, 0);
273 +
274 +error:
275 + if (err)
276 + dev_err(dev, "write_temp11 failed, err %d\n", err);
277
278 mutex_unlock(&data->update_lock);
279 +
280 + return err;
281 +}
282 +
283 +static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
284 + const char *buf, size_t count)
285 +{
286 + struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
287 + int nr = attr->nr;
288 + int index = attr->index;
289 + long val;
290 + int err;
291 +
292 + err = kstrtol(buf, 10, &val);
293 + if (err < 0)
294 + return err;
295 +
296 + err = write_temp11(dev, nr, index, val);
297 + if (err < 0)
298 + return err;
299 +
300 return count;
301 }
302