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 --- 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.
35 -static inline void lm90_select_remote_channel(struct i2c_client *client,
36 - struct lm90_data *data,
38 +static inline int lm90_select_remote_channel(struct i2c_client *client,
39 + struct lm90_data *data,
45 if (data->kind == max6696) {
46 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
50 - i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
52 + err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
55 + dev_err(&client->dev,
56 + "Failed to select remote channel %d, err %d\n",
66 @@ -759,29 +768,34 @@ static u16 temp_to_u16_adt7461(struct lm
70 -static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
72 +static int read_temp8(struct device *dev, int index)
74 - struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
75 struct lm90_data *data = lm90_update_device(dev);
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]);
85 - temp = temp_from_s8(data->temp8[attr->index]);
86 + temp = temp_from_s8(data->temp8[index]);
88 /* +16 degrees offset for temp2 for the LM99 */
89 - if (data->kind == lm99 && attr->index == 3)
90 + if (data->kind == lm99 && index == 3)
93 - return sprintf(buf, "%d\n", temp);
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,
102 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
104 + return sprintf(buf, "%d\n", read_temp8(dev, attr->index));
107 +static int write_temp8(struct device *dev, int index, long val)
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,
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;
122 - err = kstrtol(buf, 10, &val);
126 /* +16 degrees offset for temp2 for the LM99 */
127 - if (data->kind == lm99 && attr->index == 3)
128 + if (data->kind == lm99 && index == 3)
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);
139 - data->temp8[nr] = temp_to_s8(val);
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);
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);
156 +static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
157 + const char *buf, size_t count)
159 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
160 + int index = attr->index;
164 + err = kstrtol(buf, 10, &val);
168 + err = write_temp8(dev, index, val);
175 -static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
177 +static int read_temp11(struct device *dev, int index)
179 - struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
180 struct lm90_data *data = lm90_update_device(dev);
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]);
190 - temp = temp_from_s16(data->temp11[attr->index]);
191 + temp = temp_from_s16(data->temp11[index]);
193 /* +16 degrees offset for temp2 for the LM99 */
194 - if (data->kind == lm99 && attr->index <= 2)
195 + if (data->kind == lm99 && index <= 2)
198 - return sprintf(buf, "%d\n", temp);
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,
207 + struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
209 + return sprintf(buf, "%d\n", read_temp11(dev, attr->index));
212 +static int write_temp11(struct device *dev, int nr, int index, long val)
216 @@ -861,18 +894,10 @@ static ssize_t set_temp11(struct device
217 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
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;
224 - int index = attr->index;
228 - err = kstrtol(buf, 10, &val);
232 /* +16 degrees offset for temp2 for the LM99 */
233 if (data->kind == lm99 && index <= 2)
235 @@ -887,15 +912,50 @@ static ssize_t set_temp11(struct device
237 data->temp11[index] = temp_to_s8(val) << 8;
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);
250 + err = i2c_smbus_write_byte_data(client, reg[nr].high,
251 + data->temp11[index] >> 8);
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);
262 + err = lm90_select_remote_channel(client, data, 0);
266 + dev_err(dev, "write_temp11 failed, err %d\n", err);
268 mutex_unlock(&data->update_lock);
273 +static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
274 + const char *buf, size_t count)
276 + struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
278 + int index = attr->index;
282 + err = kstrtol(buf, 10, &val);
286 + err = write_temp11(dev, nr, index, val);