ds1621.c 9.0 KB

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