ds620.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ds620.c - Support for temperature sensor and thermostat DS620
  4. *
  5. * Copyright (C) 2010, 2011 Roland Stigge <[email protected]>
  6. *
  7. * based on ds1621.c by Christian W. Zuckschwerdt <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/i2c.h>
  14. #include <linux/hwmon.h>
  15. #include <linux/hwmon-sysfs.h>
  16. #include <linux/err.h>
  17. #include <linux/mutex.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/platform_data/ds620.h>
  20. /*
  21. * Many DS620 constants specified below
  22. * 15 14 13 12 11 10 09 08
  23. * |Done|NVB |THF |TLF |R1 |R0 |AUTOC|1SHOT|
  24. *
  25. * 07 06 05 04 03 02 01 00
  26. * |PO2 |PO1 |A2 |A1 |A0 | | | |
  27. */
  28. #define DS620_REG_CONFIG_DONE 0x8000
  29. #define DS620_REG_CONFIG_NVB 0x4000
  30. #define DS620_REG_CONFIG_THF 0x2000
  31. #define DS620_REG_CONFIG_TLF 0x1000
  32. #define DS620_REG_CONFIG_R1 0x0800
  33. #define DS620_REG_CONFIG_R0 0x0400
  34. #define DS620_REG_CONFIG_AUTOC 0x0200
  35. #define DS620_REG_CONFIG_1SHOT 0x0100
  36. #define DS620_REG_CONFIG_PO2 0x0080
  37. #define DS620_REG_CONFIG_PO1 0x0040
  38. #define DS620_REG_CONFIG_A2 0x0020
  39. #define DS620_REG_CONFIG_A1 0x0010
  40. #define DS620_REG_CONFIG_A0 0x0008
  41. /* The DS620 registers */
  42. static const u8 DS620_REG_TEMP[3] = {
  43. 0xAA, /* input, word, RO */
  44. 0xA2, /* min, word, RW */
  45. 0xA0, /* max, word, RW */
  46. };
  47. #define DS620_REG_CONF 0xAC /* word, RW */
  48. #define DS620_COM_START 0x51 /* no data */
  49. #define DS620_COM_STOP 0x22 /* no data */
  50. /* Each client has this additional data */
  51. struct ds620_data {
  52. struct i2c_client *client;
  53. struct mutex update_lock;
  54. bool valid; /* true if following fields are valid */
  55. unsigned long last_updated; /* In jiffies */
  56. s16 temp[3]; /* Register values, word */
  57. };
  58. static void ds620_init_client(struct i2c_client *client)
  59. {
  60. struct ds620_platform_data *ds620_info = dev_get_platdata(&client->dev);
  61. u16 conf, new_conf;
  62. new_conf = conf =
  63. i2c_smbus_read_word_swapped(client, DS620_REG_CONF);
  64. /* switch to continuous conversion mode */
  65. new_conf &= ~DS620_REG_CONFIG_1SHOT;
  66. /* already high at power-on, but don't trust the BIOS! */
  67. new_conf |= DS620_REG_CONFIG_PO2;
  68. /* thermostat mode according to platform data */
  69. if (ds620_info && ds620_info->pomode == 1)
  70. new_conf &= ~DS620_REG_CONFIG_PO1; /* PO_LOW */
  71. else if (ds620_info && ds620_info->pomode == 2)
  72. new_conf |= DS620_REG_CONFIG_PO1; /* PO_HIGH */
  73. else
  74. new_conf &= ~DS620_REG_CONFIG_PO2; /* always low */
  75. /* with highest precision */
  76. new_conf |= DS620_REG_CONFIG_R1 | DS620_REG_CONFIG_R0;
  77. if (conf != new_conf)
  78. i2c_smbus_write_word_swapped(client, DS620_REG_CONF, new_conf);
  79. /* start conversion */
  80. i2c_smbus_write_byte(client, DS620_COM_START);
  81. }
  82. static struct ds620_data *ds620_update_client(struct device *dev)
  83. {
  84. struct ds620_data *data = dev_get_drvdata(dev);
  85. struct i2c_client *client = data->client;
  86. struct ds620_data *ret = data;
  87. mutex_lock(&data->update_lock);
  88. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  89. || !data->valid) {
  90. int i;
  91. int res;
  92. dev_dbg(&client->dev, "Starting ds620 update\n");
  93. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  94. res = i2c_smbus_read_word_swapped(client,
  95. DS620_REG_TEMP[i]);
  96. if (res < 0) {
  97. ret = ERR_PTR(res);
  98. goto abort;
  99. }
  100. data->temp[i] = res;
  101. }
  102. data->last_updated = jiffies;
  103. data->valid = true;
  104. }
  105. abort:
  106. mutex_unlock(&data->update_lock);
  107. return ret;
  108. }
  109. static ssize_t temp_show(struct device *dev, struct device_attribute *da,
  110. char *buf)
  111. {
  112. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  113. struct ds620_data *data = ds620_update_client(dev);
  114. if (IS_ERR(data))
  115. return PTR_ERR(data);
  116. return sprintf(buf, "%d\n", ((data->temp[attr->index] / 8) * 625) / 10);
  117. }
  118. static ssize_t temp_store(struct device *dev, struct device_attribute *da,
  119. const char *buf, size_t count)
  120. {
  121. int res;
  122. long val;
  123. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  124. struct ds620_data *data = dev_get_drvdata(dev);
  125. struct i2c_client *client = data->client;
  126. res = kstrtol(buf, 10, &val);
  127. if (res)
  128. return res;
  129. val = (clamp_val(val, -128000, 128000) * 10 / 625) * 8;
  130. mutex_lock(&data->update_lock);
  131. data->temp[attr->index] = val;
  132. i2c_smbus_write_word_swapped(client, DS620_REG_TEMP[attr->index],
  133. data->temp[attr->index]);
  134. mutex_unlock(&data->update_lock);
  135. return count;
  136. }
  137. static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
  138. char *buf)
  139. {
  140. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  141. struct ds620_data *data = ds620_update_client(dev);
  142. struct i2c_client *client;
  143. u16 conf, new_conf;
  144. int res;
  145. if (IS_ERR(data))
  146. return PTR_ERR(data);
  147. client = data->client;
  148. /* reset alarms if necessary */
  149. res = i2c_smbus_read_word_swapped(client, DS620_REG_CONF);
  150. if (res < 0)
  151. return res;
  152. new_conf = conf = res;
  153. new_conf &= ~attr->index;
  154. if (conf != new_conf) {
  155. res = i2c_smbus_write_word_swapped(client, DS620_REG_CONF,
  156. new_conf);
  157. if (res < 0)
  158. return res;
  159. }
  160. return sprintf(buf, "%d\n", !!(conf & attr->index));
  161. }
  162. static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
  163. static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 1);
  164. static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
  165. static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, DS620_REG_CONFIG_TLF);
  166. static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, DS620_REG_CONFIG_THF);
  167. static struct attribute *ds620_attrs[] = {
  168. &sensor_dev_attr_temp1_input.dev_attr.attr,
  169. &sensor_dev_attr_temp1_min.dev_attr.attr,
  170. &sensor_dev_attr_temp1_max.dev_attr.attr,
  171. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  172. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  173. NULL
  174. };
  175. ATTRIBUTE_GROUPS(ds620);
  176. static int ds620_probe(struct i2c_client *client)
  177. {
  178. struct device *dev = &client->dev;
  179. struct device *hwmon_dev;
  180. struct ds620_data *data;
  181. data = devm_kzalloc(dev, sizeof(struct ds620_data), GFP_KERNEL);
  182. if (!data)
  183. return -ENOMEM;
  184. data->client = client;
  185. mutex_init(&data->update_lock);
  186. /* Initialize the DS620 chip */
  187. ds620_init_client(client);
  188. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  189. data, ds620_groups);
  190. return PTR_ERR_OR_ZERO(hwmon_dev);
  191. }
  192. static const struct i2c_device_id ds620_id[] = {
  193. {"ds620", 0},
  194. {}
  195. };
  196. MODULE_DEVICE_TABLE(i2c, ds620_id);
  197. /* This is the driver that will be inserted */
  198. static struct i2c_driver ds620_driver = {
  199. .class = I2C_CLASS_HWMON,
  200. .driver = {
  201. .name = "ds620",
  202. },
  203. .probe_new = ds620_probe,
  204. .id_table = ds620_id,
  205. };
  206. module_i2c_driver(ds620_driver);
  207. MODULE_AUTHOR("Roland Stigge <[email protected]>");
  208. MODULE_DESCRIPTION("DS620 driver");
  209. MODULE_LICENSE("GPL");