w83773g.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (C) 2017 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * Driver for the Nuvoton W83773G SMBus temperature sensor IC.
  10. * Supported models: W83773G
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/i2c.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/hwmon-sysfs.h>
  17. #include <linux/err.h>
  18. #include <linux/of_device.h>
  19. #include <linux/regmap.h>
  20. /* W83773 has 3 channels */
  21. #define W83773_CHANNELS 3
  22. /* The W83773 registers */
  23. #define W83773_CONVERSION_RATE_REG_READ 0x04
  24. #define W83773_CONVERSION_RATE_REG_WRITE 0x0A
  25. #define W83773_MANUFACTURER_ID_REG 0xFE
  26. #define W83773_LOCAL_TEMP 0x00
  27. static const u8 W83773_STATUS[2] = { 0x02, 0x17 };
  28. static const u8 W83773_TEMP_LSB[2] = { 0x10, 0x25 };
  29. static const u8 W83773_TEMP_MSB[2] = { 0x01, 0x24 };
  30. static const u8 W83773_OFFSET_LSB[2] = { 0x12, 0x16 };
  31. static const u8 W83773_OFFSET_MSB[2] = { 0x11, 0x15 };
  32. /* this is the number of sensors in the device */
  33. static const struct i2c_device_id w83773_id[] = {
  34. { "w83773g" },
  35. { }
  36. };
  37. MODULE_DEVICE_TABLE(i2c, w83773_id);
  38. static const struct of_device_id w83773_of_match[] = {
  39. {
  40. .compatible = "nuvoton,w83773g"
  41. },
  42. { },
  43. };
  44. MODULE_DEVICE_TABLE(of, w83773_of_match);
  45. static inline long temp_of_local(s8 reg)
  46. {
  47. return reg * 1000;
  48. }
  49. static inline long temp_of_remote(s8 hb, u8 lb)
  50. {
  51. return (hb << 3 | lb >> 5) * 125;
  52. }
  53. static int get_local_temp(struct regmap *regmap, long *val)
  54. {
  55. unsigned int regval;
  56. int ret;
  57. ret = regmap_read(regmap, W83773_LOCAL_TEMP, &regval);
  58. if (ret < 0)
  59. return ret;
  60. *val = temp_of_local(regval);
  61. return 0;
  62. }
  63. static int get_remote_temp(struct regmap *regmap, int index, long *val)
  64. {
  65. unsigned int regval_high;
  66. unsigned int regval_low;
  67. int ret;
  68. ret = regmap_read(regmap, W83773_TEMP_MSB[index], &regval_high);
  69. if (ret < 0)
  70. return ret;
  71. ret = regmap_read(regmap, W83773_TEMP_LSB[index], &regval_low);
  72. if (ret < 0)
  73. return ret;
  74. *val = temp_of_remote(regval_high, regval_low);
  75. return 0;
  76. }
  77. static int get_fault(struct regmap *regmap, int index, long *val)
  78. {
  79. unsigned int regval;
  80. int ret;
  81. ret = regmap_read(regmap, W83773_STATUS[index], &regval);
  82. if (ret < 0)
  83. return ret;
  84. *val = (regval & 0x04) >> 2;
  85. return 0;
  86. }
  87. static int get_offset(struct regmap *regmap, int index, long *val)
  88. {
  89. unsigned int regval_high;
  90. unsigned int regval_low;
  91. int ret;
  92. ret = regmap_read(regmap, W83773_OFFSET_MSB[index], &regval_high);
  93. if (ret < 0)
  94. return ret;
  95. ret = regmap_read(regmap, W83773_OFFSET_LSB[index], &regval_low);
  96. if (ret < 0)
  97. return ret;
  98. *val = temp_of_remote(regval_high, regval_low);
  99. return 0;
  100. }
  101. static int set_offset(struct regmap *regmap, int index, long val)
  102. {
  103. int ret;
  104. u8 high_byte;
  105. u8 low_byte;
  106. val = clamp_val(val, -127825, 127825);
  107. /* offset value equals to (high_byte << 3 | low_byte >> 5) * 125 */
  108. val /= 125;
  109. high_byte = val >> 3;
  110. low_byte = (val & 0x07) << 5;
  111. ret = regmap_write(regmap, W83773_OFFSET_MSB[index], high_byte);
  112. if (ret < 0)
  113. return ret;
  114. return regmap_write(regmap, W83773_OFFSET_LSB[index], low_byte);
  115. }
  116. static int get_update_interval(struct regmap *regmap, long *val)
  117. {
  118. unsigned int regval;
  119. int ret;
  120. ret = regmap_read(regmap, W83773_CONVERSION_RATE_REG_READ, &regval);
  121. if (ret < 0)
  122. return ret;
  123. *val = 16000 >> regval;
  124. return 0;
  125. }
  126. static int set_update_interval(struct regmap *regmap, long val)
  127. {
  128. int rate;
  129. /*
  130. * For valid rates, interval can be calculated as
  131. * interval = (1 << (8 - rate)) * 62.5;
  132. * Rounded rate is therefore
  133. * rate = 8 - __fls(interval * 8 / (62.5 * 7));
  134. * Use clamp_val() to avoid overflows, and to ensure valid input
  135. * for __fls.
  136. */
  137. val = clamp_val(val, 62, 16000) * 10;
  138. rate = 8 - __fls((val * 8 / (625 * 7)));
  139. return regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, rate);
  140. }
  141. static int w83773_read(struct device *dev, enum hwmon_sensor_types type,
  142. u32 attr, int channel, long *val)
  143. {
  144. struct regmap *regmap = dev_get_drvdata(dev);
  145. if (type == hwmon_chip) {
  146. if (attr == hwmon_chip_update_interval)
  147. return get_update_interval(regmap, val);
  148. return -EOPNOTSUPP;
  149. }
  150. switch (attr) {
  151. case hwmon_temp_input:
  152. if (channel == 0)
  153. return get_local_temp(regmap, val);
  154. return get_remote_temp(regmap, channel - 1, val);
  155. case hwmon_temp_fault:
  156. return get_fault(regmap, channel - 1, val);
  157. case hwmon_temp_offset:
  158. return get_offset(regmap, channel - 1, val);
  159. default:
  160. return -EOPNOTSUPP;
  161. }
  162. }
  163. static int w83773_write(struct device *dev, enum hwmon_sensor_types type,
  164. u32 attr, int channel, long val)
  165. {
  166. struct regmap *regmap = dev_get_drvdata(dev);
  167. if (type == hwmon_chip && attr == hwmon_chip_update_interval)
  168. return set_update_interval(regmap, val);
  169. if (type == hwmon_temp && attr == hwmon_temp_offset)
  170. return set_offset(regmap, channel - 1, val);
  171. return -EOPNOTSUPP;
  172. }
  173. static umode_t w83773_is_visible(const void *data, enum hwmon_sensor_types type,
  174. u32 attr, int channel)
  175. {
  176. switch (type) {
  177. case hwmon_chip:
  178. switch (attr) {
  179. case hwmon_chip_update_interval:
  180. return 0644;
  181. }
  182. break;
  183. case hwmon_temp:
  184. switch (attr) {
  185. case hwmon_temp_input:
  186. case hwmon_temp_fault:
  187. return 0444;
  188. case hwmon_temp_offset:
  189. return 0644;
  190. }
  191. break;
  192. default:
  193. break;
  194. }
  195. return 0;
  196. }
  197. static const u32 w83773_chip_config[] = {
  198. HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
  199. 0
  200. };
  201. static const struct hwmon_channel_info w83773_chip = {
  202. .type = hwmon_chip,
  203. .config = w83773_chip_config,
  204. };
  205. static const u32 w83773_temp_config[] = {
  206. HWMON_T_INPUT,
  207. HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_OFFSET,
  208. HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_OFFSET,
  209. 0
  210. };
  211. static const struct hwmon_channel_info w83773_temp = {
  212. .type = hwmon_temp,
  213. .config = w83773_temp_config,
  214. };
  215. static const struct hwmon_channel_info *w83773_info[] = {
  216. &w83773_chip,
  217. &w83773_temp,
  218. NULL
  219. };
  220. static const struct hwmon_ops w83773_ops = {
  221. .is_visible = w83773_is_visible,
  222. .read = w83773_read,
  223. .write = w83773_write,
  224. };
  225. static const struct hwmon_chip_info w83773_chip_info = {
  226. .ops = &w83773_ops,
  227. .info = w83773_info,
  228. };
  229. static const struct regmap_config w83773_regmap_config = {
  230. .reg_bits = 8,
  231. .val_bits = 8,
  232. };
  233. static int w83773_probe(struct i2c_client *client,
  234. const struct i2c_device_id *id)
  235. {
  236. struct device *dev = &client->dev;
  237. struct device *hwmon_dev;
  238. struct regmap *regmap;
  239. int ret;
  240. regmap = devm_regmap_init_i2c(client, &w83773_regmap_config);
  241. if (IS_ERR(regmap)) {
  242. dev_err(dev, "failed to allocate register map\n");
  243. return PTR_ERR(regmap);
  244. }
  245. /* Set the conversion rate to 2 Hz */
  246. ret = regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, 0x05);
  247. if (ret < 0) {
  248. dev_err(&client->dev, "error writing config rate register\n");
  249. return ret;
  250. }
  251. i2c_set_clientdata(client, regmap);
  252. hwmon_dev = devm_hwmon_device_register_with_info(dev,
  253. client->name,
  254. regmap,
  255. &w83773_chip_info,
  256. NULL);
  257. return PTR_ERR_OR_ZERO(hwmon_dev);
  258. }
  259. static struct i2c_driver w83773_driver = {
  260. .class = I2C_CLASS_HWMON,
  261. .driver = {
  262. .name = "w83773g",
  263. .of_match_table = of_match_ptr(w83773_of_match),
  264. },
  265. .probe = w83773_probe,
  266. .id_table = w83773_id,
  267. };
  268. module_i2c_driver(w83773_driver);
  269. MODULE_AUTHOR("Lei YU <mine260309@gmail.com>");
  270. MODULE_DESCRIPTION("W83773G temperature sensor driver");
  271. MODULE_LICENSE("GPL");