1 From: Wei Ni <wni@nvidia.com>
2 Subject: hwmon: lm90: split set and show temp as common codes
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
11 http://www.spinics.net/lists/linux-tegra/msg14020.html .
14 BUG=chrome-os-partner:30834
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>
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
34 * various registers have different meanings as a result of selecting a
35 * non-default remote channel.
37 -static inline void lm90_select_remote_channel(struct i2c_client *client,
38 - struct lm90_data *data,
40 +static inline int lm90_select_remote_channel(struct i2c_client *client,
41 + struct lm90_data *data,
47 if (data->kind == max6696) {
48 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
52 - i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
54 + err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
57 + dev_err(&client->dev,
58 + "Failed to select remote channel %d, err %d\n",
72 -static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
74 +static int read_temp8(struct device *dev, int index)
76 - struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
77 struct lm90_data *data = lm90_update_device(dev);
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]);
87 - temp = temp_from_s8(data->temp8[attr->index]);
88 + temp = temp_from_s8(data->temp8[index]);
90 /* +16 degrees offset for temp2 for the LM99 */
91 - if (data->kind == lm99 && attr->index == 3)
92 + if (data->kind == lm99 && index == 3)
95 - return sprintf(buf, "%d\n", temp);
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,
104 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
106 + return sprintf(buf, "%d\n", read_temp8(dev, attr->index));
109 +static int write_temp8(struct device *dev, int index, long val)
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,
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;
123 + /* +16 degrees offset for temp2 for the LM99 */
124 + if (data->kind == lm99 && index == 3)
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);
133 + data->temp8[index] = temp_to_s8(val);
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);
145 +static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
146 + const char *buf, size_t count)
148 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
149 + int index = attr->index;
153 @@ -805,49 +846,41 @@
157 - /* +16 degrees offset for temp2 for the LM99 */
158 - if (data->kind == lm99 && attr->index == 3)
160 + err = write_temp8(dev, index, val);
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);
170 - data->temp8[nr] = temp_to_s8(val);
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);
176 - mutex_unlock(&data->update_lock);
180 +static int read_temp11(struct device *dev, int index)
182 + struct lm90_data *data = lm90_update_device(dev);
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]);
190 + temp = temp_from_s16(data->temp11[index]);
192 + /* +16 degrees offset for temp2 for the LM99 */
193 + if (data->kind == lm99 && index <= 2)
199 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
202 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
203 - struct lm90_data *data = lm90_update_device(dev);
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]);
211 - temp = temp_from_s16(data->temp11[attr->index]);
213 - /* +16 degrees offset for temp2 for the LM99 */
214 - if (data->kind == lm99 && attr->index <= 2)
217 - return sprintf(buf, "%d\n", temp);
218 + return sprintf(buf, "%d\n", read_temp11(dev, attr->index));
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)
228 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
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;
235 - int index = attr->index;
239 - err = kstrtol(buf, 10, &val);
243 /* +16 degrees offset for temp2 for the LM99 */
244 if (data->kind == lm99 && index <= 2)
245 @@ -887,15 +912,50 @@
247 data->temp11[index] = temp_to_s8(val) << 8;
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);
260 + err = i2c_smbus_write_byte_data(client, reg[nr].high,
261 + data->temp11[index] >> 8);
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);
272 + err = lm90_select_remote_channel(client, data, 0);
276 + dev_err(dev, "write_temp11 failed, err %d\n", err);
278 mutex_unlock(&data->update_lock);
283 +static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
284 + const char *buf, size_t count)
286 + struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
288 + int index = attr->index;
292 + err = kstrtol(buf, 10, &val);
296 + err = write_temp11(dev, nr, index, val);