ds1621.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 <jdelvare@suse.de>
  8. *
  9. * The DS1621 device is a digital temperature/thermometer with 9-bit
  10. * resolution, a thermal alarm output (Tout), and user-defined minimum
  11. * and maximum temperature thresholds (TH and TL).
  12. *
  13. * The DS1625, DS1631, DS1721, and DS1731 are pin compatible with the DS1621
  14. * and similar in operation, with slight variations as noted in the device
  15. * datasheets (please refer to www.maximintegrated.com for specific
  16. * device information).
  17. *
  18. * Since the DS1621 was the first chipset supported by this driver,
  19. * most comments will refer to this chipset, but are actually general
  20. * and concern all supported chipsets, unless mentioned otherwise.
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation; either version 2 of the License, or
  25. * (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to the Free Software
  34. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35. */
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/slab.h>
  39. #include <linux/jiffies.h>
  40. #include <linux/i2c.h>
  41. #include <linux/hwmon.h>
  42. #include <linux/hwmon-sysfs.h>
  43. #include <linux/err.h>
  44. #include <linux/mutex.h>
  45. #include <linux/sysfs.h>
  46. #include <linux/kernel.h>
  47. /* Supported devices */
  48. enum chips { ds1621, ds1625, ds1631, ds1721, ds1731 };
  49. /* Insmod parameters */
  50. static int polarity = -1;
  51. module_param(polarity, int, 0);
  52. MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
  53. /*
  54. * The Configuration/Status register
  55. *
  56. * - DS1621:
  57. * 7 6 5 4 3 2 1 0
  58. * |Done|THF |TLF |NVB | X | X |POL |1SHOT|
  59. *
  60. * - DS1625:
  61. * 7 6 5 4 3 2 1 0
  62. * |Done|THF |TLF |NVB | 1 | 0 |POL |1SHOT|
  63. *
  64. * - DS1631, DS1731:
  65. * 7 6 5 4 3 2 1 0
  66. * |Done|THF |TLF |NVB | R1 | R0 |POL |1SHOT|
  67. *
  68. * - DS1721:
  69. * 7 6 5 4 3 2 1 0
  70. * |Done| X | X | U | R1 | R0 |POL |1SHOT|
  71. *
  72. * Where:
  73. * - 'X' is Reserved
  74. * - 'U' is Undefined
  75. */
  76. #define DS1621_REG_CONFIG_NVB 0x10
  77. #define DS1621_REG_CONFIG_RESOL 0x0C
  78. #define DS1621_REG_CONFIG_POLARITY 0x02
  79. #define DS1621_REG_CONFIG_1SHOT 0x01
  80. #define DS1621_REG_CONFIG_DONE 0x80
  81. #define DS1621_REG_CONFIG_RESOL_SHIFT 2
  82. /* ds1721 conversion rates: {C/LSB, time(ms), resolution bit setting} */
  83. static const unsigned short ds1721_convrates[] = {
  84. 94, /* 9-bits (0.5, 93.75, RES[0..1] = 0 */
  85. 188, /* 10-bits (0.25, 187.5, RES[0..1] = 1 */
  86. 375, /* 11-bits (0.125, 375, RES[0..1] = 2 */
  87. 750, /* 12-bits (0.0625, 750, RES[0..1] = 3 */
  88. };
  89. #define DS1621_CONVERSION_MAX 750
  90. #define DS1625_CONVERSION_MAX 500
  91. #define DS1621_TEMP_MAX 125000
  92. #define DS1621_TEMP_MIN (-55000)
  93. /* The DS1621 temperature registers */
  94. static const u8 DS1621_REG_TEMP[3] = {
  95. 0xAA, /* input, word, RO */
  96. 0xA2, /* min, word, RW */
  97. 0xA1, /* max, word, RW */
  98. };
  99. #define DS1621_REG_CONF 0xAC /* byte, RW */
  100. #define DS1621_COM_START 0xEE /* no data */
  101. #define DS1721_COM_START 0x51 /* no data */
  102. #define DS1621_COM_STOP 0x22 /* no data */
  103. /* The DS1621 configuration register */
  104. #define DS1621_ALARM_TEMP_HIGH 0x40
  105. #define DS1621_ALARM_TEMP_LOW 0x20
  106. /* Conversions */
  107. #define ALARMS_FROM_REG(val) ((val) & \
  108. (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
  109. /* Each client has this additional data */
  110. struct ds1621_data {
  111. struct i2c_client *client;
  112. struct mutex update_lock;
  113. char valid; /* !=0 if following fields are valid */
  114. unsigned long last_updated; /* In jiffies */
  115. enum chips kind; /* device type */
  116. u16 temp[3]; /* Register values, word */
  117. u8 conf; /* Register encoding, combined */
  118. u8 zbits; /* Resolution encoded as number of
  119. * zero bits */
  120. u16 update_interval; /* Conversion rate in milliseconds */
  121. };
  122. static inline int DS1621_TEMP_FROM_REG(u16 reg)
  123. {
  124. return DIV_ROUND_CLOSEST(((s16)reg / 16) * 625, 10);
  125. }
  126. /*
  127. * TEMP: 0.001C/bit (-55C to +125C)
  128. * REG:
  129. * - 1621, 1625: 0.5C/bit, 7 zero-bits
  130. * - 1631, 1721, 1731: 0.0625C/bit, 4 zero-bits
  131. */
  132. static inline u16 DS1621_TEMP_TO_REG(long temp, u8 zbits)
  133. {
  134. temp = clamp_val(temp, DS1621_TEMP_MIN, DS1621_TEMP_MAX);
  135. temp = DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
  136. return temp;
  137. }
  138. static void ds1621_init_client(struct ds1621_data *data,
  139. struct i2c_client *client)
  140. {
  141. u8 conf, new_conf, sreg, resol;
  142. new_conf = conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  143. /* switch to continuous conversion mode */
  144. new_conf &= ~DS1621_REG_CONFIG_1SHOT;
  145. /* setup output polarity */
  146. if (polarity == 0)
  147. new_conf &= ~DS1621_REG_CONFIG_POLARITY;
  148. else if (polarity == 1)
  149. new_conf |= DS1621_REG_CONFIG_POLARITY;
  150. if (conf != new_conf)
  151. i2c_smbus_write_byte_data(client, DS1621_REG_CONF, new_conf);
  152. switch (data->kind) {
  153. case ds1625:
  154. data->update_interval = DS1625_CONVERSION_MAX;
  155. data->zbits = 7;
  156. sreg = DS1621_COM_START;
  157. break;
  158. case ds1631:
  159. case ds1721:
  160. case ds1731:
  161. resol = (new_conf & DS1621_REG_CONFIG_RESOL) >>
  162. DS1621_REG_CONFIG_RESOL_SHIFT;
  163. data->update_interval = ds1721_convrates[resol];
  164. data->zbits = 7 - resol;
  165. sreg = DS1721_COM_START;
  166. break;
  167. default:
  168. data->update_interval = DS1621_CONVERSION_MAX;
  169. data->zbits = 7;
  170. sreg = DS1621_COM_START;
  171. break;
  172. }
  173. /* start conversion */
  174. i2c_smbus_write_byte(client, sreg);
  175. }
  176. static struct ds1621_data *ds1621_update_client(struct device *dev)
  177. {
  178. struct ds1621_data *data = dev_get_drvdata(dev);
  179. struct i2c_client *client = data->client;
  180. u8 new_conf;
  181. mutex_lock(&data->update_lock);
  182. if (time_after(jiffies, data->last_updated + data->update_interval) ||
  183. !data->valid) {
  184. int i;
  185. dev_dbg(&client->dev, "Starting ds1621 update\n");
  186. data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  187. for (i = 0; i < ARRAY_SIZE(data->temp); i++)
  188. data->temp[i] = i2c_smbus_read_word_swapped(client,
  189. DS1621_REG_TEMP[i]);
  190. /* reset alarms if necessary */
  191. new_conf = data->conf;
  192. if (data->temp[0] > data->temp[1]) /* input > min */
  193. new_conf &= ~DS1621_ALARM_TEMP_LOW;
  194. if (data->temp[0] < data->temp[2]) /* input < max */
  195. new_conf &= ~DS1621_ALARM_TEMP_HIGH;
  196. if (data->conf != new_conf)
  197. i2c_smbus_write_byte_data(client, DS1621_REG_CONF,
  198. new_conf);
  199. data->last_updated = jiffies;
  200. data->valid = 1;
  201. }
  202. mutex_unlock(&data->update_lock);
  203. return data;
  204. }
  205. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  206. char *buf)
  207. {
  208. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  209. struct ds1621_data *data = ds1621_update_client(dev);
  210. return sprintf(buf, "%d\n",
  211. DS1621_TEMP_FROM_REG(data->temp[attr->index]));
  212. }
  213. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  214. const char *buf, size_t count)
  215. {
  216. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  217. struct ds1621_data *data = dev_get_drvdata(dev);
  218. long val;
  219. int err;
  220. err = kstrtol(buf, 10, &val);
  221. if (err)
  222. return err;
  223. mutex_lock(&data->update_lock);
  224. data->temp[attr->index] = DS1621_TEMP_TO_REG(val, data->zbits);
  225. i2c_smbus_write_word_swapped(data->client, DS1621_REG_TEMP[attr->index],
  226. data->temp[attr->index]);
  227. mutex_unlock(&data->update_lock);
  228. return count;
  229. }
  230. static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
  231. char *buf)
  232. {
  233. struct ds1621_data *data = ds1621_update_client(dev);
  234. return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
  235. }
  236. static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
  237. char *buf)
  238. {
  239. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  240. struct ds1621_data *data = ds1621_update_client(dev);
  241. return sprintf(buf, "%d\n", !!(data->conf & attr->index));
  242. }
  243. static ssize_t show_convrate(struct device *dev, struct device_attribute *da,
  244. char *buf)
  245. {
  246. struct ds1621_data *data = dev_get_drvdata(dev);
  247. return scnprintf(buf, PAGE_SIZE, "%hu\n", data->update_interval);
  248. }
  249. static ssize_t set_convrate(struct device *dev, struct device_attribute *da,
  250. const char *buf, size_t count)
  251. {
  252. struct ds1621_data *data = dev_get_drvdata(dev);
  253. struct i2c_client *client = data->client;
  254. unsigned long convrate;
  255. s32 err;
  256. int resol = 0;
  257. err = kstrtoul(buf, 10, &convrate);
  258. if (err)
  259. return err;
  260. /* Convert rate into resolution bits */
  261. while (resol < (ARRAY_SIZE(ds1721_convrates) - 1) &&
  262. convrate > ds1721_convrates[resol])
  263. resol++;
  264. mutex_lock(&data->update_lock);
  265. data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  266. data->conf &= ~DS1621_REG_CONFIG_RESOL;
  267. data->conf |= (resol << DS1621_REG_CONFIG_RESOL_SHIFT);
  268. i2c_smbus_write_byte_data(client, DS1621_REG_CONF, data->conf);
  269. data->update_interval = ds1721_convrates[resol];
  270. data->zbits = 7 - resol;
  271. mutex_unlock(&data->update_lock);
  272. return count;
  273. }
  274. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  275. static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_convrate,
  276. set_convrate);
  277. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  278. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, 1);
  279. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 2);
  280. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
  281. DS1621_ALARM_TEMP_LOW);
  282. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
  283. DS1621_ALARM_TEMP_HIGH);
  284. static struct attribute *ds1621_attributes[] = {
  285. &sensor_dev_attr_temp1_input.dev_attr.attr,
  286. &sensor_dev_attr_temp1_min.dev_attr.attr,
  287. &sensor_dev_attr_temp1_max.dev_attr.attr,
  288. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  289. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  290. &dev_attr_alarms.attr,
  291. &dev_attr_update_interval.attr,
  292. NULL
  293. };
  294. static umode_t ds1621_attribute_visible(struct kobject *kobj,
  295. struct attribute *attr, int index)
  296. {
  297. struct device *dev = container_of(kobj, struct device, kobj);
  298. struct ds1621_data *data = dev_get_drvdata(dev);
  299. if (attr == &dev_attr_update_interval.attr)
  300. if (data->kind == ds1621 || data->kind == ds1625)
  301. /* shhh, we're hiding update_interval */
  302. return 0;
  303. return attr->mode;
  304. }
  305. static const struct attribute_group ds1621_group = {
  306. .attrs = ds1621_attributes,
  307. .is_visible = ds1621_attribute_visible
  308. };
  309. __ATTRIBUTE_GROUPS(ds1621);
  310. static int ds1621_probe(struct i2c_client *client,
  311. const struct i2c_device_id *id)
  312. {
  313. struct ds1621_data *data;
  314. struct device *hwmon_dev;
  315. data = devm_kzalloc(&client->dev, sizeof(struct ds1621_data),
  316. GFP_KERNEL);
  317. if (!data)
  318. return -ENOMEM;
  319. mutex_init(&data->update_lock);
  320. data->kind = id->driver_data;
  321. data->client = client;
  322. /* Initialize the DS1621 chip */
  323. ds1621_init_client(data, client);
  324. hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
  325. client->name, data,
  326. ds1621_groups);
  327. return PTR_ERR_OR_ZERO(hwmon_dev);
  328. }
  329. static const struct i2c_device_id ds1621_id[] = {
  330. { "ds1621", ds1621 },
  331. { "ds1625", ds1625 },
  332. { "ds1631", ds1631 },
  333. { "ds1721", ds1721 },
  334. { "ds1731", ds1731 },
  335. { }
  336. };
  337. MODULE_DEVICE_TABLE(i2c, ds1621_id);
  338. /* This is the driver that will be inserted */
  339. static struct i2c_driver ds1621_driver = {
  340. .class = I2C_CLASS_HWMON,
  341. .driver = {
  342. .name = "ds1621",
  343. },
  344. .probe = ds1621_probe,
  345. .id_table = ds1621_id,
  346. };
  347. module_i2c_driver(ds1621_driver);
  348. MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
  349. MODULE_DESCRIPTION("DS1621 driver");
  350. MODULE_LICENSE("GPL");