ds1621.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/err.h>
  27. #include <linux/mutex.h>
  28. #include <linux/sysfs.h>
  29. #include "lm75.h"
  30. /* Addresses to scan */
  31. static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  32. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  33. /* Insmod parameters */
  34. I2C_CLIENT_INSMOD_1(ds1621);
  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. #define DS1621_REG_TEMP 0xAA /* word, RO */
  48. #define DS1621_REG_TEMP_MIN 0xA1 /* word, RW */
  49. #define DS1621_REG_TEMP_MAX 0xA2 /* word, RW */
  50. #define DS1621_REG_CONF 0xAC /* byte, RW */
  51. #define DS1621_COM_START 0xEE /* no data */
  52. #define DS1621_COM_STOP 0x22 /* no data */
  53. /* The DS1621 configuration register */
  54. #define DS1621_ALARM_TEMP_HIGH 0x40
  55. #define DS1621_ALARM_TEMP_LOW 0x20
  56. /* Conversions. Rounding and limit checking is only done on the TO_REG
  57. variants. Note that you should be a bit careful with which arguments
  58. these macros are called: arguments may be evaluated more than once.
  59. Fixing this is just not worth it. */
  60. #define ALARMS_FROM_REG(val) ((val) & \
  61. (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
  62. /* Each client has this additional data */
  63. struct ds1621_data {
  64. struct i2c_client client;
  65. struct class_device *class_dev;
  66. struct mutex update_lock;
  67. char valid; /* !=0 if following fields are valid */
  68. unsigned long last_updated; /* In jiffies */
  69. u16 temp, temp_min, temp_max; /* Register values, word */
  70. u8 conf; /* Register encoding, combined */
  71. };
  72. static int ds1621_attach_adapter(struct i2c_adapter *adapter);
  73. static int ds1621_detect(struct i2c_adapter *adapter, int address,
  74. int kind);
  75. static void ds1621_init_client(struct i2c_client *client);
  76. static int ds1621_detach_client(struct i2c_client *client);
  77. static struct ds1621_data *ds1621_update_client(struct device *dev);
  78. /* This is the driver that will be inserted */
  79. static struct i2c_driver ds1621_driver = {
  80. .driver = {
  81. .name = "ds1621",
  82. },
  83. .id = I2C_DRIVERID_DS1621,
  84. .attach_adapter = ds1621_attach_adapter,
  85. .detach_client = ds1621_detach_client,
  86. };
  87. /* All registers are word-sized, except for the configuration register.
  88. DS1621 uses a high-byte first convention, which is exactly opposite to
  89. the usual practice. */
  90. static int ds1621_read_value(struct i2c_client *client, u8 reg)
  91. {
  92. if (reg == DS1621_REG_CONF)
  93. return i2c_smbus_read_byte_data(client, reg);
  94. else
  95. return swab16(i2c_smbus_read_word_data(client, reg));
  96. }
  97. /* All registers are word-sized, except for the configuration register.
  98. DS1621 uses a high-byte first convention, which is exactly opposite to
  99. the usual practice. */
  100. static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)
  101. {
  102. if (reg == DS1621_REG_CONF)
  103. return i2c_smbus_write_byte_data(client, reg, value);
  104. else
  105. return i2c_smbus_write_word_data(client, reg, swab16(value));
  106. }
  107. static void ds1621_init_client(struct i2c_client *client)
  108. {
  109. int reg = ds1621_read_value(client, DS1621_REG_CONF);
  110. /* switch to continuous conversion mode */
  111. reg &= ~ DS1621_REG_CONFIG_1SHOT;
  112. /* setup output polarity */
  113. if (polarity == 0)
  114. reg &= ~DS1621_REG_CONFIG_POLARITY;
  115. else if (polarity == 1)
  116. reg |= DS1621_REG_CONFIG_POLARITY;
  117. ds1621_write_value(client, DS1621_REG_CONF, reg);
  118. /* start conversion */
  119. i2c_smbus_write_byte(client, DS1621_COM_START);
  120. }
  121. #define show(value) \
  122. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  123. { \
  124. struct ds1621_data *data = ds1621_update_client(dev); \
  125. return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \
  126. }
  127. show(temp);
  128. show(temp_min);
  129. show(temp_max);
  130. #define set_temp(suffix, value, reg) \
  131. static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  132. size_t count) \
  133. { \
  134. struct i2c_client *client = to_i2c_client(dev); \
  135. struct ds1621_data *data = ds1621_update_client(dev); \
  136. u16 val = LM75_TEMP_TO_REG(simple_strtoul(buf, NULL, 10)); \
  137. \
  138. mutex_lock(&data->update_lock); \
  139. data->value = val; \
  140. ds1621_write_value(client, reg, data->value); \
  141. mutex_unlock(&data->update_lock); \
  142. return count; \
  143. }
  144. set_temp(min, temp_min, DS1621_REG_TEMP_MIN);
  145. set_temp(max, temp_max, DS1621_REG_TEMP_MAX);
  146. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  147. {
  148. struct ds1621_data *data = ds1621_update_client(dev);
  149. return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
  150. }
  151. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  152. static DEVICE_ATTR(temp1_input, S_IRUGO , show_temp, NULL);
  153. static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO , show_temp_min, set_temp_min);
  154. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
  155. static struct attribute *ds1621_attributes[] = {
  156. &dev_attr_temp1_input.attr,
  157. &dev_attr_temp1_min.attr,
  158. &dev_attr_temp1_max.attr,
  159. &dev_attr_alarms.attr,
  160. NULL
  161. };
  162. static const struct attribute_group ds1621_group = {
  163. .attrs = ds1621_attributes,
  164. };
  165. static int ds1621_attach_adapter(struct i2c_adapter *adapter)
  166. {
  167. if (!(adapter->class & I2C_CLASS_HWMON))
  168. return 0;
  169. return i2c_probe(adapter, &addr_data, ds1621_detect);
  170. }
  171. /* This function is called by i2c_probe */
  172. static int ds1621_detect(struct i2c_adapter *adapter, int address,
  173. int kind)
  174. {
  175. int conf, temp;
  176. struct i2c_client *new_client;
  177. struct ds1621_data *data;
  178. int err = 0;
  179. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  180. | I2C_FUNC_SMBUS_WORD_DATA
  181. | I2C_FUNC_SMBUS_WRITE_BYTE))
  182. goto exit;
  183. /* OK. For now, we presume we have a valid client. We now create the
  184. client structure, even though we cannot fill it completely yet.
  185. But it allows us to access ds1621_{read,write}_value. */
  186. if (!(data = kzalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
  187. err = -ENOMEM;
  188. goto exit;
  189. }
  190. new_client = &data->client;
  191. i2c_set_clientdata(new_client, data);
  192. new_client->addr = address;
  193. new_client->adapter = adapter;
  194. new_client->driver = &ds1621_driver;
  195. new_client->flags = 0;
  196. /* Now, we do the remaining detection. It is lousy. */
  197. if (kind < 0) {
  198. /* The NVB bit should be low if no EEPROM write has been
  199. requested during the latest 10ms, which is highly
  200. improbable in our case. */
  201. conf = ds1621_read_value(new_client, DS1621_REG_CONF);
  202. if (conf & DS1621_REG_CONFIG_NVB)
  203. goto exit_free;
  204. /* The 7 lowest bits of a temperature should always be 0. */
  205. temp = ds1621_read_value(new_client, DS1621_REG_TEMP);
  206. if (temp & 0x007f)
  207. goto exit_free;
  208. temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MIN);
  209. if (temp & 0x007f)
  210. goto exit_free;
  211. temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MAX);
  212. if (temp & 0x007f)
  213. goto exit_free;
  214. }
  215. /* Determine the chip type - only one kind supported! */
  216. if (kind <= 0)
  217. kind = ds1621;
  218. /* Fill in remaining client fields and put it into the global list */
  219. strlcpy(new_client->name, "ds1621", I2C_NAME_SIZE);
  220. data->valid = 0;
  221. mutex_init(&data->update_lock);
  222. /* Tell the I2C layer a new client has arrived */
  223. if ((err = i2c_attach_client(new_client)))
  224. goto exit_free;
  225. /* Initialize the DS1621 chip */
  226. ds1621_init_client(new_client);
  227. /* Register sysfs hooks */
  228. if ((err = sysfs_create_group(&new_client->dev.kobj, &ds1621_group)))
  229. goto exit_detach;
  230. data->class_dev = hwmon_device_register(&new_client->dev);
  231. if (IS_ERR(data->class_dev)) {
  232. err = PTR_ERR(data->class_dev);
  233. goto exit_remove_files;
  234. }
  235. return 0;
  236. exit_remove_files:
  237. sysfs_remove_group(&new_client->dev.kobj, &ds1621_group);
  238. exit_detach:
  239. i2c_detach_client(new_client);
  240. exit_free:
  241. kfree(data);
  242. exit:
  243. return err;
  244. }
  245. static int ds1621_detach_client(struct i2c_client *client)
  246. {
  247. struct ds1621_data *data = i2c_get_clientdata(client);
  248. int err;
  249. hwmon_device_unregister(data->class_dev);
  250. sysfs_remove_group(&client->dev.kobj, &ds1621_group);
  251. if ((err = i2c_detach_client(client)))
  252. return err;
  253. kfree(data);
  254. return 0;
  255. }
  256. static struct ds1621_data *ds1621_update_client(struct device *dev)
  257. {
  258. struct i2c_client *client = to_i2c_client(dev);
  259. struct ds1621_data *data = i2c_get_clientdata(client);
  260. u8 new_conf;
  261. mutex_lock(&data->update_lock);
  262. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  263. || !data->valid) {
  264. dev_dbg(&client->dev, "Starting ds1621 update\n");
  265. data->conf = ds1621_read_value(client, DS1621_REG_CONF);
  266. data->temp = ds1621_read_value(client, DS1621_REG_TEMP);
  267. data->temp_min = ds1621_read_value(client,
  268. DS1621_REG_TEMP_MIN);
  269. data->temp_max = ds1621_read_value(client,
  270. DS1621_REG_TEMP_MAX);
  271. /* reset alarms if necessary */
  272. new_conf = data->conf;
  273. if (data->temp < data->temp_min)
  274. new_conf &= ~DS1621_ALARM_TEMP_LOW;
  275. if (data->temp > data->temp_max)
  276. new_conf &= ~DS1621_ALARM_TEMP_HIGH;
  277. if (data->conf != new_conf)
  278. ds1621_write_value(client, DS1621_REG_CONF,
  279. new_conf);
  280. data->last_updated = jiffies;
  281. data->valid = 1;
  282. }
  283. mutex_unlock(&data->update_lock);
  284. return data;
  285. }
  286. static int __init ds1621_init(void)
  287. {
  288. return i2c_add_driver(&ds1621_driver);
  289. }
  290. static void __exit ds1621_exit(void)
  291. {
  292. i2c_del_driver(&ds1621_driver);
  293. }
  294. MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
  295. MODULE_DESCRIPTION("DS1621 driver");
  296. MODULE_LICENSE("GPL");
  297. module_init(ds1621_init);
  298. module_exit(ds1621_exit);