mvebu: remove linux 4.4 support
[openwrt/openwrt.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 --- a/drivers/hwmon/lm90.c
30 +++ b/drivers/hwmon/lm90.c
31 @@ -473,20 +473,29 @@ static int lm90_read16(struct i2c_client
32 * various registers have different meanings as a result of selecting a
33 * non-default remote channel.
34 */
35 -static inline void lm90_select_remote_channel(struct i2c_client *client,
36 - struct lm90_data *data,
37 - int channel)
38 +static inline int lm90_select_remote_channel(struct i2c_client *client,
39 + struct lm90_data *data,
40 + int channel)
41 {
42 u8 config;
43 + int err;
44
45 if (data->kind == max6696) {
46 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
47 config &= ~0x08;
48 if (channel)
49 config |= 0x08;
50 - i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
51 - config);
52 + err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
53 + config);
54 + if (err < 0) {
55 + dev_err(&client->dev,
56 + "Failed to select remote channel %d, err %d\n",
57 + channel, err);
58 + return err;
59 + }
60 }
61 +
62 + return 0;
63 }
64
65 /*
66 @@ -759,29 +768,34 @@ static u16 temp_to_u16_adt7461(struct lm
67 * Sysfs stuff
68 */
69
70 -static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
71 - char *buf)
72 +static int read_temp8(struct device *dev, int index)
73 {
74 - struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
75 struct lm90_data *data = lm90_update_device(dev);
76 int temp;
77
78 if (data->kind == adt7461 || data->kind == tmp451)
79 - temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
80 + temp = temp_from_u8_adt7461(data, data->temp8[index]);
81 else if (data->kind == max6646)
82 - temp = temp_from_u8(data->temp8[attr->index]);
83 + temp = temp_from_u8(data->temp8[index]);
84 else
85 - temp = temp_from_s8(data->temp8[attr->index]);
86 + temp = temp_from_s8(data->temp8[index]);
87
88 /* +16 degrees offset for temp2 for the LM99 */
89 - if (data->kind == lm99 && attr->index == 3)
90 + if (data->kind == lm99 && index == 3)
91 temp += 16000;
92
93 - return sprintf(buf, "%d\n", temp);
94 + return temp;
95 }
96
97 -static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
98 - const char *buf, size_t count)
99 +static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
100 + char *buf)
101 +{
102 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
103 +
104 + return sprintf(buf, "%d\n", read_temp8(dev, attr->index));
105 +}
106 +
107 +static int write_temp8(struct device *dev, int index, long val)
108 {
109 static const u8 reg[TEMP8_REG_NUM] = {
110 LM90_REG_W_LOCAL_LOW,
111 @@ -794,60 +808,79 @@ static ssize_t set_temp8(struct device *
112 MAX6659_REG_W_REMOTE_EMERG,
113 };
114
115 - struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
116 struct lm90_data *data = dev_get_drvdata(dev);
117 struct i2c_client *client = data->client;
118 - int nr = attr->index;
119 - long val;
120 int err;
121
122 - err = kstrtol(buf, 10, &val);
123 - if (err < 0)
124 - return err;
125 -
126 /* +16 degrees offset for temp2 for the LM99 */
127 - if (data->kind == lm99 && attr->index == 3)
128 + if (data->kind == lm99 && index == 3)
129 val -= 16000;
130
131 mutex_lock(&data->update_lock);
132 if (data->kind == adt7461 || data->kind == tmp451)
133 - data->temp8[nr] = temp_to_u8_adt7461(data, val);
134 + data->temp8[index] = temp_to_u8_adt7461(data, val);
135 else if (data->kind == max6646)
136 - data->temp8[nr] = temp_to_u8(val);
137 + data->temp8[index] = temp_to_u8(val);
138 else
139 - data->temp8[nr] = temp_to_s8(val);
140 -
141 - lm90_select_remote_channel(client, data, nr >= 6);
142 - i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
143 - lm90_select_remote_channel(client, data, 0);
144 + data->temp8[index] = temp_to_s8(val);
145
146 + if ((err = lm90_select_remote_channel(client, data, index >= 6)) ||
147 + (err = i2c_smbus_write_byte_data(client, reg[index],
148 + data->temp8[index])) ||
149 + (err = lm90_select_remote_channel(client, data, 0)))
150 + dev_err(dev, "write_temp8 failed, err %d\n", err);
151 mutex_unlock(&data->update_lock);
152 +
153 + return err;
154 +}
155 +
156 +static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
157 + const char *buf, size_t count)
158 +{
159 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
160 + int index = attr->index;
161 + long val;
162 + int err;
163 +
164 + err = kstrtol(buf, 10, &val);
165 + if (err < 0)
166 + return err;
167 +
168 + err = write_temp8(dev, index, val);
169 + if (err < 0)
170 + return err;
171 +
172 return count;
173 }
174
175 -static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
176 - char *buf)
177 +static int read_temp11(struct device *dev, int index)
178 {
179 - struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
180 struct lm90_data *data = lm90_update_device(dev);
181 int temp;
182
183 if (data->kind == adt7461 || data->kind == tmp451)
184 - temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
185 + temp = temp_from_u16_adt7461(data, data->temp11[index]);
186 else if (data->kind == max6646)
187 - temp = temp_from_u16(data->temp11[attr->index]);
188 + temp = temp_from_u16(data->temp11[index]);
189 else
190 - temp = temp_from_s16(data->temp11[attr->index]);
191 + temp = temp_from_s16(data->temp11[index]);
192
193 /* +16 degrees offset for temp2 for the LM99 */
194 - if (data->kind == lm99 && attr->index <= 2)
195 + if (data->kind == lm99 && index <= 2)
196 temp += 16000;
197
198 - return sprintf(buf, "%d\n", temp);
199 + return temp;
200 }
201
202 -static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
203 - const char *buf, size_t count)
204 +static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
205 + char *buf)
206 +{
207 + struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
208 +
209 + return sprintf(buf, "%d\n", read_temp11(dev, attr->index));
210 +}
211 +
212 +static int write_temp11(struct device *dev, int nr, int index, long val)
213 {
214 struct {
215 u8 high;
216 @@ -861,18 +894,10 @@ static ssize_t set_temp11(struct device
217 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
218 };
219
220 - struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
221 struct lm90_data *data = dev_get_drvdata(dev);
222 struct i2c_client *client = data->client;
223 - int nr = attr->nr;
224 - int index = attr->index;
225 - long val;
226 int err;
227
228 - err = kstrtol(buf, 10, &val);
229 - if (err < 0)
230 - return err;
231 -
232 /* +16 degrees offset for temp2 for the LM99 */
233 if (data->kind == lm99 && index <= 2)
234 val -= 16000;
235 @@ -887,15 +912,50 @@ static ssize_t set_temp11(struct device
236 else
237 data->temp11[index] = temp_to_s8(val) << 8;
238
239 - lm90_select_remote_channel(client, data, reg[nr].channel);
240 - i2c_smbus_write_byte_data(client, reg[nr].high,
241 - data->temp11[index] >> 8);
242 - if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
243 - i2c_smbus_write_byte_data(client, reg[nr].low,
244 - data->temp11[index] & 0xff);
245 - lm90_select_remote_channel(client, data, 0);
246 + err = lm90_select_remote_channel(client, data, reg[nr].channel);
247 + if (err)
248 + goto error;
249 +
250 + err = i2c_smbus_write_byte_data(client, reg[nr].high,
251 + data->temp11[index] >> 8);
252 + if (err)
253 + goto error;
254 +
255 + if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
256 + err = i2c_smbus_write_byte_data(client, reg[nr].low,
257 + data->temp11[index] & 0xff);
258 + if (err)
259 + goto error;
260 + }
261 +
262 + err = lm90_select_remote_channel(client, data, 0);
263 +
264 +error:
265 + if (err)
266 + dev_err(dev, "write_temp11 failed, err %d\n", err);
267
268 mutex_unlock(&data->update_lock);
269 +
270 + return err;
271 +}
272 +
273 +static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
274 + const char *buf, size_t count)
275 +{
276 + struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
277 + int nr = attr->nr;
278 + int index = attr->index;
279 + long val;
280 + int err;
281 +
282 + err = kstrtol(buf, 10, &val);
283 + if (err < 0)
284 + return err;
285 +
286 + err = write_temp11(dev, nr, index, val);
287 + if (err < 0)
288 + return err;
289 +
290 return count;
291 }
292