ds1621.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Christian W. Zuckschwerdt <zany@triq.net> 2000-11-23
  5. based on lm75.c by Frodo Looijaard <frodol@dds.nl>
  6. Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
  7. the help of Jean Delvare <khali@linux-fr.org>
  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. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.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. #include <linux/sysfs.h>
  30. #include "lm75.h"
  31. /* Addresses to scan */
  32. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  33. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  34. /* Insmod parameters */
  35. static int polarity = -1;
  36. module_param(polarity, int, 0);
  37. MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
  38. /* Many DS1621 constants specified below */
  39. /* Config register used for detection */
  40. /* 7 6 5 4 3 2 1 0 */
  41. /* |Done|THF |TLF |NVB | X | X |POL |1SHOT| */
  42. #define DS1621_REG_CONFIG_NVB 0x10
  43. #define DS1621_REG_CONFIG_POLARITY 0x02
  44. #define DS1621_REG_CONFIG_1SHOT 0x01
  45. #define DS1621_REG_CONFIG_DONE 0x80
  46. /* The DS1621 registers */
  47. static const u8 DS1621_REG_TEMP[3] = {
  48. 0xAA, /* input, word, RO */
  49. 0xA2, /* min, word, RW */
  50. 0xA1, /* max, word, RW */
  51. };
  52. #define DS1621_REG_CONF 0xAC /* byte, RW */
  53. #define DS1621_COM_START 0xEE /* no data */
  54. #define DS1621_COM_STOP 0x22 /* no data */
  55. /* The DS1621 configuration register */
  56. #define DS1621_ALARM_TEMP_HIGH 0x40
  57. #define DS1621_ALARM_TEMP_LOW 0x20
  58. /* Conversions */
  59. #define ALARMS_FROM_REG(val) ((val) & \
  60. (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
  61. /* Each client has this additional data */
  62. struct ds1621_data {
  63. struct device *hwmon_dev;
  64. struct mutex update_lock;
  65. char valid; /* !=0 if following fields are valid */
  66. unsigned long last_updated; /* In jiffies */
  67. u16 temp[3]; /* Register values, word */
  68. u8 conf; /* Register encoding, combined */
  69. };
  70. /* Temperature registers are word-sized.
  71. DS1621 uses a high-byte first convention, which is exactly opposite to
  72. the SMBus standard. */
  73. static int ds1621_read_temp(struct i2c_client *client, u8 reg)
  74. {
  75. int ret;
  76. ret = i2c_smbus_read_word_data(client, reg);
  77. if (ret < 0)
  78. return ret;
  79. return swab16(ret);
  80. }
  81. static int ds1621_write_temp(struct i2c_client *client, u8 reg, u16 value)
  82. {
  83. return i2c_smbus_write_word_data(client, reg, swab16(value));
  84. }
  85. static void ds1621_init_client(struct i2c_client *client)
  86. {
  87. u8 conf, new_conf;
  88. new_conf = conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  89. /* switch to continuous conversion mode */
  90. new_conf &= ~DS1621_REG_CONFIG_1SHOT;
  91. /* setup output polarity */
  92. if (polarity == 0)
  93. new_conf &= ~DS1621_REG_CONFIG_POLARITY;
  94. else if (polarity == 1)
  95. new_conf |= DS1621_REG_CONFIG_POLARITY;
  96. if (conf != new_conf)
  97. i2c_smbus_write_byte_data(client, DS1621_REG_CONF, new_conf);
  98. /* start conversion */
  99. i2c_smbus_write_byte(client, DS1621_COM_START);
  100. }
  101. static struct ds1621_data *ds1621_update_client(struct device *dev)
  102. {
  103. struct i2c_client *client = to_i2c_client(dev);
  104. struct ds1621_data *data = i2c_get_clientdata(client);
  105. u8 new_conf;
  106. mutex_lock(&data->update_lock);
  107. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  108. || !data->valid) {
  109. int i;
  110. dev_dbg(&client->dev, "Starting ds1621 update\n");
  111. data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  112. for (i = 0; i < ARRAY_SIZE(data->temp); i++)
  113. data->temp[i] = ds1621_read_temp(client,
  114. DS1621_REG_TEMP[i]);
  115. /* reset alarms if necessary */
  116. new_conf = data->conf;
  117. if (data->temp[0] > data->temp[1]) /* input > min */
  118. new_conf &= ~DS1621_ALARM_TEMP_LOW;
  119. if (data->temp[0] < data->temp[2]) /* input < max */
  120. new_conf &= ~DS1621_ALARM_TEMP_HIGH;
  121. if (data->conf != new_conf)
  122. i2c_smbus_write_byte_data(client, DS1621_REG_CONF,
  123. new_conf);
  124. data->last_updated = jiffies;
  125. data->valid = 1;
  126. }
  127. mutex_unlock(&data->update_lock);
  128. return data;
  129. }
  130. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  131. char *buf)
  132. {
  133. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  134. struct ds1621_data *data = ds1621_update_client(dev);
  135. return sprintf(buf, "%d\n",
  136. LM75_TEMP_FROM_REG(data->temp[attr->index]));
  137. }
  138. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  139. const char *buf, size_t count)
  140. {
  141. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  142. struct i2c_client *client = to_i2c_client(dev);
  143. struct ds1621_data *data = i2c_get_clientdata(client);
  144. u16 val = LM75_TEMP_TO_REG(simple_strtol(buf, NULL, 10));
  145. mutex_lock(&data->update_lock);
  146. data->temp[attr->index] = val;
  147. ds1621_write_temp(client, DS1621_REG_TEMP[attr->index],
  148. data->temp[attr->index]);
  149. mutex_unlock(&data->update_lock);
  150. return count;
  151. }
  152. static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
  153. char *buf)
  154. {
  155. struct ds1621_data *data = ds1621_update_client(dev);
  156. return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
  157. }
  158. static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
  159. char *buf)
  160. {
  161. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  162. struct ds1621_data *data = ds1621_update_client(dev);
  163. return sprintf(buf, "%d\n", !!(data->conf & attr->index));
  164. }
  165. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  166. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  167. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, 1);
  168. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 2);
  169. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
  170. DS1621_ALARM_TEMP_LOW);
  171. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
  172. DS1621_ALARM_TEMP_HIGH);
  173. static struct attribute *ds1621_attributes[] = {
  174. &sensor_dev_attr_temp1_input.dev_attr.attr,
  175. &sensor_dev_attr_temp1_min.dev_attr.attr,
  176. &sensor_dev_attr_temp1_max.dev_attr.attr,
  177. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  178. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  179. &dev_attr_alarms.attr,
  180. NULL
  181. };
  182. static const struct attribute_group ds1621_group = {
  183. .attrs = ds1621_attributes,
  184. };
  185. /* Return 0 if detection is successful, -ENODEV otherwise */
  186. static int ds1621_detect(struct i2c_client *client,
  187. struct i2c_board_info *info)
  188. {
  189. struct i2c_adapter *adapter = client->adapter;
  190. int conf, temp;
  191. int i;
  192. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  193. | I2C_FUNC_SMBUS_WORD_DATA
  194. | I2C_FUNC_SMBUS_WRITE_BYTE))
  195. return -ENODEV;
  196. /* Now, we do the remaining detection. It is lousy. */
  197. /* The NVB bit should be low if no EEPROM write has been requested
  198. during the latest 10ms, which is highly improbable in our case. */
  199. conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  200. if (conf < 0 || conf & DS1621_REG_CONFIG_NVB)
  201. return -ENODEV;
  202. /* The 7 lowest bits of a temperature should always be 0. */
  203. for (i = 0; i < ARRAY_SIZE(DS1621_REG_TEMP); i++) {
  204. temp = i2c_smbus_read_word_data(client, DS1621_REG_TEMP[i]);
  205. if (temp < 0 || (temp & 0x7f00))
  206. return -ENODEV;
  207. }
  208. strlcpy(info->type, "ds1621", I2C_NAME_SIZE);
  209. return 0;
  210. }
  211. static int ds1621_probe(struct i2c_client *client,
  212. const struct i2c_device_id *id)
  213. {
  214. struct ds1621_data *data;
  215. int err;
  216. data = kzalloc(sizeof(struct ds1621_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 DS1621 chip */
  224. ds1621_init_client(client);
  225. /* Register sysfs hooks */
  226. if ((err = sysfs_create_group(&client->dev.kobj, &ds1621_group)))
  227. goto exit_free;
  228. data->hwmon_dev = hwmon_device_register(&client->dev);
  229. if (IS_ERR(data->hwmon_dev)) {
  230. err = PTR_ERR(data->hwmon_dev);
  231. goto exit_remove_files;
  232. }
  233. return 0;
  234. exit_remove_files:
  235. sysfs_remove_group(&client->dev.kobj, &ds1621_group);
  236. exit_free:
  237. kfree(data);
  238. exit:
  239. return err;
  240. }
  241. static int ds1621_remove(struct i2c_client *client)
  242. {
  243. struct ds1621_data *data = i2c_get_clientdata(client);
  244. hwmon_device_unregister(data->hwmon_dev);
  245. sysfs_remove_group(&client->dev.kobj, &ds1621_group);
  246. kfree(data);
  247. return 0;
  248. }
  249. static const struct i2c_device_id ds1621_id[] = {
  250. { "ds1621", 0 },
  251. { "ds1625", 0 },
  252. { }
  253. };
  254. MODULE_DEVICE_TABLE(i2c, ds1621_id);
  255. /* This is the driver that will be inserted */
  256. static struct i2c_driver ds1621_driver = {
  257. .class = I2C_CLASS_HWMON,
  258. .driver = {
  259. .name = "ds1621",
  260. },
  261. .probe = ds1621_probe,
  262. .remove = ds1621_remove,
  263. .id_table = ds1621_id,
  264. .detect = ds1621_detect,
  265. .address_list = normal_i2c,
  266. };
  267. static int __init ds1621_init(void)
  268. {
  269. return i2c_add_driver(&ds1621_driver);
  270. }
  271. static void __exit ds1621_exit(void)
  272. {
  273. i2c_del_driver(&ds1621_driver);
  274. }
  275. MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
  276. MODULE_DESCRIPTION("DS1621 driver");
  277. MODULE_LICENSE("GPL");
  278. module_init(ds1621_init);
  279. module_exit(ds1621_exit);