911-hwmon-lm90-split-set-and-show-temp-as-common-codes.patch 8.5 KB

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