ds620.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 device *hwmon_dev;
  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 = client->dev.platform_data;
  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 i2c_client *client = to_i2c_client(dev);
  98. struct ds620_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
  138. struct ds620_data *data = i2c_get_clientdata(client);
  139. res = kstrtol(buf, 10, &val);
  140. if (res)
  141. return res;
  142. val = (val * 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 = to_i2c_client(dev);
  156. u16 conf, new_conf;
  157. int res;
  158. if (IS_ERR(data))
  159. return PTR_ERR(data);
  160. /* reset alarms if necessary */
  161. res = i2c_smbus_read_word_swapped(client, DS620_REG_CONF);
  162. if (res < 0)
  163. return res;
  164. new_conf = conf = res;
  165. new_conf &= ~attr->index;
  166. if (conf != new_conf) {
  167. res = i2c_smbus_write_word_swapped(client, DS620_REG_CONF,
  168. new_conf);
  169. if (res < 0)
  170. return res;
  171. }
  172. return sprintf(buf, "%d\n", !!(conf & attr->index));
  173. }
  174. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  175. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, 1);
  176. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 2);
  177. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
  178. DS620_REG_CONFIG_TLF);
  179. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
  180. DS620_REG_CONFIG_THF);
  181. static struct attribute *ds620_attributes[] = {
  182. &sensor_dev_attr_temp1_input.dev_attr.attr,
  183. &sensor_dev_attr_temp1_min.dev_attr.attr,
  184. &sensor_dev_attr_temp1_max.dev_attr.attr,
  185. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  186. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  187. NULL
  188. };
  189. static const struct attribute_group ds620_group = {
  190. .attrs = ds620_attributes,
  191. };
  192. static int ds620_probe(struct i2c_client *client,
  193. const struct i2c_device_id *id)
  194. {
  195. struct ds620_data *data;
  196. int err;
  197. data = kzalloc(sizeof(struct ds620_data), GFP_KERNEL);
  198. if (!data) {
  199. err = -ENOMEM;
  200. goto exit;
  201. }
  202. i2c_set_clientdata(client, data);
  203. mutex_init(&data->update_lock);
  204. /* Initialize the DS620 chip */
  205. ds620_init_client(client);
  206. /* Register sysfs hooks */
  207. err = sysfs_create_group(&client->dev.kobj, &ds620_group);
  208. if (err)
  209. goto exit_free;
  210. data->hwmon_dev = hwmon_device_register(&client->dev);
  211. if (IS_ERR(data->hwmon_dev)) {
  212. err = PTR_ERR(data->hwmon_dev);
  213. goto exit_remove_files;
  214. }
  215. dev_info(&client->dev, "temperature sensor found\n");
  216. return 0;
  217. exit_remove_files:
  218. sysfs_remove_group(&client->dev.kobj, &ds620_group);
  219. exit_free:
  220. kfree(data);
  221. exit:
  222. return err;
  223. }
  224. static int ds620_remove(struct i2c_client *client)
  225. {
  226. struct ds620_data *data = i2c_get_clientdata(client);
  227. hwmon_device_unregister(data->hwmon_dev);
  228. sysfs_remove_group(&client->dev.kobj, &ds620_group);
  229. kfree(data);
  230. return 0;
  231. }
  232. static const struct i2c_device_id ds620_id[] = {
  233. {"ds620", 0},
  234. {}
  235. };
  236. MODULE_DEVICE_TABLE(i2c, ds620_id);
  237. /* This is the driver that will be inserted */
  238. static struct i2c_driver ds620_driver = {
  239. .class = I2C_CLASS_HWMON,
  240. .driver = {
  241. .name = "ds620",
  242. },
  243. .probe = ds620_probe,
  244. .remove = ds620_remove,
  245. .id_table = ds620_id,
  246. };
  247. module_i2c_driver(ds620_driver);
  248. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  249. MODULE_DESCRIPTION("DS620 driver");
  250. MODULE_LICENSE("GPL");