w83l785ts.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * w83l785ts.c - Part of lm_sensors, Linux kernel modules for hardware
  4. * monitoring
  5. * Copyright (C) 2003-2009 Jean Delvare <jdelvare@suse.de>
  6. *
  7. * Inspired from the lm83 driver. The W83L785TS-S is a sensor chip made
  8. * by Winbond. It reports a single external temperature with a 1 deg
  9. * resolution and a 3 deg accuracy. Datasheet can be obtained from
  10. * Winbond's website at:
  11. * http://www.winbond-usa.com/products/winbond_products/pdfs/PCIC/W83L785TS-S.pdf
  12. *
  13. * Ported to Linux 2.6 by Wolfgang Ziegler <nuppla@gmx.at> and Jean Delvare
  14. * <jdelvare@suse.de>.
  15. *
  16. * Thanks to James Bolt <james@evilpenguin.com> for benchmarking the read
  17. * error handling mechanism.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/i2c.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/err.h>
  28. #include <linux/mutex.h>
  29. /* How many retries on register read error */
  30. #define MAX_RETRIES 5
  31. /*
  32. * Address to scan
  33. * Address is fully defined internally and cannot be changed.
  34. */
  35. static const unsigned short normal_i2c[] = { 0x2e, I2C_CLIENT_END };
  36. /*
  37. * The W83L785TS-S registers
  38. * Manufacturer ID is 0x5CA3 for Winbond.
  39. */
  40. #define W83L785TS_REG_MAN_ID1 0x4D
  41. #define W83L785TS_REG_MAN_ID2 0x4C
  42. #define W83L785TS_REG_CHIP_ID 0x4E
  43. #define W83L785TS_REG_CONFIG 0x40
  44. #define W83L785TS_REG_TYPE 0x52
  45. #define W83L785TS_REG_TEMP 0x27
  46. #define W83L785TS_REG_TEMP_OVER 0x53 /* not sure about this one */
  47. /*
  48. * Conversions
  49. * The W83L785TS-S uses signed 8-bit values.
  50. */
  51. #define TEMP_FROM_REG(val) ((val) * 1000)
  52. /*
  53. * Functions declaration
  54. */
  55. static int w83l785ts_probe(struct i2c_client *client);
  56. static int w83l785ts_detect(struct i2c_client *client,
  57. struct i2c_board_info *info);
  58. static int w83l785ts_remove(struct i2c_client *client);
  59. static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval);
  60. static struct w83l785ts_data *w83l785ts_update_device(struct device *dev);
  61. /*
  62. * Driver data (common to all clients)
  63. */
  64. static const struct i2c_device_id w83l785ts_id[] = {
  65. { "w83l785ts", 0 },
  66. { }
  67. };
  68. MODULE_DEVICE_TABLE(i2c, w83l785ts_id);
  69. static struct i2c_driver w83l785ts_driver = {
  70. .class = I2C_CLASS_HWMON,
  71. .driver = {
  72. .name = "w83l785ts",
  73. },
  74. .probe_new = w83l785ts_probe,
  75. .remove = w83l785ts_remove,
  76. .id_table = w83l785ts_id,
  77. .detect = w83l785ts_detect,
  78. .address_list = normal_i2c,
  79. };
  80. /*
  81. * Client data (each client gets its own)
  82. */
  83. struct w83l785ts_data {
  84. struct device *hwmon_dev;
  85. struct mutex update_lock;
  86. char valid; /* zero until following fields are valid */
  87. unsigned long last_updated; /* in jiffies */
  88. /* registers values */
  89. s8 temp[2]; /* 0: input, 1: critical limit */
  90. };
  91. /*
  92. * Sysfs stuff
  93. */
  94. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  95. char *buf)
  96. {
  97. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  98. struct w83l785ts_data *data = w83l785ts_update_device(dev);
  99. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  100. }
  101. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  102. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 1);
  103. /*
  104. * Real code
  105. */
  106. /* Return 0 if detection is successful, -ENODEV otherwise */
  107. static int w83l785ts_detect(struct i2c_client *client,
  108. struct i2c_board_info *info)
  109. {
  110. struct i2c_adapter *adapter = client->adapter;
  111. u16 man_id;
  112. u8 chip_id;
  113. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  114. return -ENODEV;
  115. /* detection */
  116. if ((w83l785ts_read_value(client, W83L785TS_REG_CONFIG, 0) & 0x80)
  117. || (w83l785ts_read_value(client, W83L785TS_REG_TYPE, 0) & 0xFC)) {
  118. dev_dbg(&adapter->dev,
  119. "W83L785TS-S detection failed at 0x%02x\n",
  120. client->addr);
  121. return -ENODEV;
  122. }
  123. /* Identification */
  124. man_id = (w83l785ts_read_value(client, W83L785TS_REG_MAN_ID1, 0) << 8)
  125. + w83l785ts_read_value(client, W83L785TS_REG_MAN_ID2, 0);
  126. chip_id = w83l785ts_read_value(client, W83L785TS_REG_CHIP_ID, 0);
  127. if (man_id != 0x5CA3 /* Winbond */
  128. || chip_id != 0x70) { /* W83L785TS-S */
  129. dev_dbg(&adapter->dev,
  130. "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
  131. man_id, chip_id);
  132. return -ENODEV;
  133. }
  134. strlcpy(info->type, "w83l785ts", I2C_NAME_SIZE);
  135. return 0;
  136. }
  137. static int w83l785ts_probe(struct i2c_client *client)
  138. {
  139. struct w83l785ts_data *data;
  140. struct device *dev = &client->dev;
  141. int err;
  142. data = devm_kzalloc(dev, sizeof(struct w83l785ts_data), GFP_KERNEL);
  143. if (!data)
  144. return -ENOMEM;
  145. i2c_set_clientdata(client, data);
  146. mutex_init(&data->update_lock);
  147. /*
  148. * Initialize the W83L785TS chip
  149. * Nothing yet, assume it is already started.
  150. */
  151. err = device_create_file(dev, &sensor_dev_attr_temp1_input.dev_attr);
  152. if (err)
  153. return err;
  154. err = device_create_file(dev, &sensor_dev_attr_temp1_max.dev_attr);
  155. if (err)
  156. goto exit_remove;
  157. /* Register sysfs hooks */
  158. data->hwmon_dev = hwmon_device_register(dev);
  159. if (IS_ERR(data->hwmon_dev)) {
  160. err = PTR_ERR(data->hwmon_dev);
  161. goto exit_remove;
  162. }
  163. return 0;
  164. exit_remove:
  165. device_remove_file(dev, &sensor_dev_attr_temp1_input.dev_attr);
  166. device_remove_file(dev, &sensor_dev_attr_temp1_max.dev_attr);
  167. return err;
  168. }
  169. static int w83l785ts_remove(struct i2c_client *client)
  170. {
  171. struct w83l785ts_data *data = i2c_get_clientdata(client);
  172. hwmon_device_unregister(data->hwmon_dev);
  173. device_remove_file(&client->dev,
  174. &sensor_dev_attr_temp1_input.dev_attr);
  175. device_remove_file(&client->dev,
  176. &sensor_dev_attr_temp1_max.dev_attr);
  177. return 0;
  178. }
  179. static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval)
  180. {
  181. int value, i;
  182. struct device *dev;
  183. const char *prefix;
  184. /*
  185. * We might be called during detection, at which point the client
  186. * isn't yet fully initialized, so we can't use dev_dbg on it
  187. */
  188. if (i2c_get_clientdata(client)) {
  189. dev = &client->dev;
  190. prefix = "";
  191. } else {
  192. dev = &client->adapter->dev;
  193. prefix = "w83l785ts: ";
  194. }
  195. /*
  196. * Frequent read errors have been reported on Asus boards, so we
  197. * retry on read errors. If it still fails (unlikely), return the
  198. * default value requested by the caller.
  199. */
  200. for (i = 1; i <= MAX_RETRIES; i++) {
  201. value = i2c_smbus_read_byte_data(client, reg);
  202. if (value >= 0) {
  203. dev_dbg(dev, "%sRead 0x%02x from register 0x%02x.\n",
  204. prefix, value, reg);
  205. return value;
  206. }
  207. dev_dbg(dev, "%sRead failed, will retry in %d.\n", prefix, i);
  208. msleep(i);
  209. }
  210. dev_err(dev, "%sCouldn't read value from register 0x%02x.\n", prefix,
  211. reg);
  212. return defval;
  213. }
  214. static struct w83l785ts_data *w83l785ts_update_device(struct device *dev)
  215. {
  216. struct i2c_client *client = to_i2c_client(dev);
  217. struct w83l785ts_data *data = i2c_get_clientdata(client);
  218. mutex_lock(&data->update_lock);
  219. if (!data->valid || time_after(jiffies, data->last_updated + HZ * 2)) {
  220. dev_dbg(&client->dev, "Updating w83l785ts data.\n");
  221. data->temp[0] = w83l785ts_read_value(client,
  222. W83L785TS_REG_TEMP, data->temp[0]);
  223. data->temp[1] = w83l785ts_read_value(client,
  224. W83L785TS_REG_TEMP_OVER, data->temp[1]);
  225. data->last_updated = jiffies;
  226. data->valid = 1;
  227. }
  228. mutex_unlock(&data->update_lock);
  229. return data;
  230. }
  231. module_i2c_driver(w83l785ts_driver);
  232. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  233. MODULE_DESCRIPTION("W83L785TS-S driver");
  234. MODULE_LICENSE("GPL");