ds620.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. /*
  72. * Temperature registers are word-sized.
  73. * DS620 uses a high-byte first convention, which is exactly opposite to
  74. * the SMBus standard.
  75. */
  76. static int ds620_read_temp(struct i2c_client *client, u8 reg)
  77. {
  78. int ret;
  79. ret = i2c_smbus_read_word_data(client, reg);
  80. if (ret < 0)
  81. return ret;
  82. return swab16(ret);
  83. }
  84. static int ds620_write_temp(struct i2c_client *client, u8 reg, u16 value)
  85. {
  86. return i2c_smbus_write_word_data(client, reg, swab16(value));
  87. }
  88. static void ds620_init_client(struct i2c_client *client)
  89. {
  90. struct ds620_platform_data *ds620_info = client->dev.platform_data;
  91. u16 conf, new_conf;
  92. new_conf = conf =
  93. swab16(i2c_smbus_read_word_data(client, DS620_REG_CONF));
  94. /* switch to continuous conversion mode */
  95. new_conf &= ~DS620_REG_CONFIG_1SHOT;
  96. /* already high at power-on, but don't trust the BIOS! */
  97. new_conf |= DS620_REG_CONFIG_PO2;
  98. /* thermostat mode according to platform data */
  99. if (ds620_info && ds620_info->pomode == 1)
  100. new_conf &= ~DS620_REG_CONFIG_PO1; /* PO_LOW */
  101. else if (ds620_info && ds620_info->pomode == 2)
  102. new_conf |= DS620_REG_CONFIG_PO1; /* PO_HIGH */
  103. else
  104. new_conf &= ~DS620_REG_CONFIG_PO2; /* always low */
  105. /* with highest precision */
  106. new_conf |= DS620_REG_CONFIG_R1 | DS620_REG_CONFIG_R0;
  107. if (conf != new_conf)
  108. i2c_smbus_write_word_data(client, DS620_REG_CONF,
  109. swab16(new_conf));
  110. /* start conversion */
  111. i2c_smbus_write_byte(client, DS620_COM_START);
  112. }
  113. static struct ds620_data *ds620_update_client(struct device *dev)
  114. {
  115. struct i2c_client *client = to_i2c_client(dev);
  116. struct ds620_data *data = i2c_get_clientdata(client);
  117. struct ds620_data *ret = data;
  118. mutex_lock(&data->update_lock);
  119. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  120. || !data->valid) {
  121. int i;
  122. int res;
  123. dev_dbg(&client->dev, "Starting ds620 update\n");
  124. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  125. res = ds620_read_temp(client,
  126. DS620_REG_TEMP[i]);
  127. if (res < 0) {
  128. ret = ERR_PTR(res);
  129. goto abort;
  130. }
  131. data->temp[i] = res;
  132. }
  133. data->last_updated = jiffies;
  134. data->valid = 1;
  135. }
  136. abort:
  137. mutex_unlock(&data->update_lock);
  138. return ret;
  139. }
  140. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  141. char *buf)
  142. {
  143. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  144. struct ds620_data *data = ds620_update_client(dev);
  145. if (IS_ERR(data))
  146. return PTR_ERR(data);
  147. return sprintf(buf, "%d\n", ((data->temp[attr->index] / 8) * 625) / 10);
  148. }
  149. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  150. const char *buf, size_t count)
  151. {
  152. int res;
  153. long val;
  154. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  155. struct i2c_client *client = to_i2c_client(dev);
  156. struct ds620_data *data = i2c_get_clientdata(client);
  157. res = strict_strtol(buf, 10, &val);
  158. if (res)
  159. return res;
  160. val = (val * 10 / 625) * 8;
  161. mutex_lock(&data->update_lock);
  162. data->temp[attr->index] = val;
  163. ds620_write_temp(client, DS620_REG_TEMP[attr->index],
  164. data->temp[attr->index]);
  165. mutex_unlock(&data->update_lock);
  166. return count;
  167. }
  168. static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
  169. char *buf)
  170. {
  171. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  172. struct ds620_data *data = ds620_update_client(dev);
  173. struct i2c_client *client = to_i2c_client(dev);
  174. u16 conf, new_conf;
  175. int res;
  176. if (IS_ERR(data))
  177. return PTR_ERR(data);
  178. /* reset alarms if necessary */
  179. res = i2c_smbus_read_word_data(client, DS620_REG_CONF);
  180. if (res < 0)
  181. return res;
  182. conf = swab16(res);
  183. new_conf = conf;
  184. new_conf &= ~attr->index;
  185. if (conf != new_conf) {
  186. res = i2c_smbus_write_word_data(client, DS620_REG_CONF,
  187. swab16(new_conf));
  188. if (res < 0)
  189. return res;
  190. }
  191. return sprintf(buf, "%d\n", !!(conf & attr->index));
  192. }
  193. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  194. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, 1);
  195. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 2);
  196. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
  197. DS620_REG_CONFIG_TLF);
  198. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
  199. DS620_REG_CONFIG_THF);
  200. static struct attribute *ds620_attributes[] = {
  201. &sensor_dev_attr_temp1_input.dev_attr.attr,
  202. &sensor_dev_attr_temp1_min.dev_attr.attr,
  203. &sensor_dev_attr_temp1_max.dev_attr.attr,
  204. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  205. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  206. NULL
  207. };
  208. static const struct attribute_group ds620_group = {
  209. .attrs = ds620_attributes,
  210. };
  211. static int ds620_probe(struct i2c_client *client,
  212. const struct i2c_device_id *id)
  213. {
  214. struct ds620_data *data;
  215. int err;
  216. data = kzalloc(sizeof(struct ds620_data), GFP_KERNEL);
  217. if (!data) {
  218. err = -ENOMEM;
  219. goto exit;
  220. }
  221. i2c_set_clientdata(client, data);
  222. mutex_init(&data->update_lock);
  223. /* Initialize the DS620 chip */
  224. ds620_init_client(client);
  225. /* Register sysfs hooks */
  226. err = sysfs_create_group(&client->dev.kobj, &ds620_group);
  227. if (err)
  228. goto exit_free;
  229. data->hwmon_dev = hwmon_device_register(&client->dev);
  230. if (IS_ERR(data->hwmon_dev)) {
  231. err = PTR_ERR(data->hwmon_dev);
  232. goto exit_remove_files;
  233. }
  234. dev_info(&client->dev, "temperature sensor found\n");
  235. return 0;
  236. exit_remove_files:
  237. sysfs_remove_group(&client->dev.kobj, &ds620_group);
  238. exit_free:
  239. kfree(data);
  240. exit:
  241. return err;
  242. }
  243. static int ds620_remove(struct i2c_client *client)
  244. {
  245. struct ds620_data *data = i2c_get_clientdata(client);
  246. hwmon_device_unregister(data->hwmon_dev);
  247. sysfs_remove_group(&client->dev.kobj, &ds620_group);
  248. kfree(data);
  249. return 0;
  250. }
  251. static const struct i2c_device_id ds620_id[] = {
  252. {"ds620", 0},
  253. {}
  254. };
  255. MODULE_DEVICE_TABLE(i2c, ds620_id);
  256. /* This is the driver that will be inserted */
  257. static struct i2c_driver ds620_driver = {
  258. .class = I2C_CLASS_HWMON,
  259. .driver = {
  260. .name = "ds620",
  261. },
  262. .probe = ds620_probe,
  263. .remove = ds620_remove,
  264. .id_table = ds620_id,
  265. };
  266. static int __init ds620_init(void)
  267. {
  268. return i2c_add_driver(&ds620_driver);
  269. }
  270. static void __exit ds620_exit(void)
  271. {
  272. i2c_del_driver(&ds620_driver);
  273. }
  274. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  275. MODULE_DESCRIPTION("DS620 driver");
  276. MODULE_LICENSE("GPL");
  277. module_init(ds620_init);
  278. module_exit(ds620_exit);