ds1621.c 11 KB

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