ds620.c 7.4 KB

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