ds1621.c 11 KB

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