ds1621.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. * 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, and DS1721 are pin compatible with the DS1621 and
  14. * 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. /* Addresses to scan */
  48. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  49. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  50. /* Supported devices */
  51. enum chips { ds1621, ds1625, ds1631, ds1721 };
  52. /* Insmod parameters */
  53. static int polarity = -1;
  54. module_param(polarity, int, 0);
  55. MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
  56. /*
  57. * The Configuration/Status register
  58. *
  59. * - DS1621:
  60. * 7 6 5 4 3 2 1 0
  61. * |Done|THF |TLF |NVB | X | X |POL |1SHOT|
  62. *
  63. * - DS1625:
  64. * 7 6 5 4 3 2 1 0
  65. * |Done|THF |TLF |NVB | 1 | 0 |POL |1SHOT|
  66. *
  67. * - DS1631:
  68. * 7 6 5 4 3 2 1 0
  69. * |Done|THF |TLF |NVB | R1 | R0 |POL |1SHOT|
  70. *
  71. * - DS1721:
  72. * 7 6 5 4 3 2 1 0
  73. * |Done| X | X | U | R1 | R0 |POL |1SHOT|
  74. *
  75. * Where:
  76. * - 'X' is Reserved
  77. * - 'U' is Undefined
  78. */
  79. #define DS1621_REG_CONFIG_NVB 0x10
  80. #define DS1621_REG_CONFIG_RESOL 0x0C
  81. #define DS1621_REG_CONFIG_POLARITY 0x02
  82. #define DS1621_REG_CONFIG_1SHOT 0x01
  83. #define DS1621_REG_CONFIG_DONE 0x80
  84. #define DS1621_REG_CONFIG_RESOL_SHIFT 2
  85. /* ds1721 conversion rates: {C/LSB, time(ms), resolution bit setting} */
  86. static const unsigned short ds1721_convrates[] = {
  87. 94, /* 9-bits (0.5, 93.75, RES[0..1] = 0 */
  88. 188, /* 10-bits (0.25, 187.5, RES[0..1] = 1 */
  89. 375, /* 11-bits (0.125, 375, RES[0..1] = 2 */
  90. 750, /* 12-bits (0.0625, 750, RES[0..1] = 3 */
  91. };
  92. #define DS1621_CONVERSION_MAX 750
  93. #define DS1625_CONVERSION_MAX 500
  94. #define DS1621_TEMP_MAX 125000
  95. #define DS1621_TEMP_MIN (-55000)
  96. /* The DS1621 temperature registers */
  97. static const u8 DS1621_REG_TEMP[3] = {
  98. 0xAA, /* input, word, RO */
  99. 0xA2, /* min, word, RW */
  100. 0xA1, /* max, word, RW */
  101. };
  102. #define DS1621_REG_CONF 0xAC /* byte, RW */
  103. #define DS1621_COM_START 0xEE /* no data */
  104. #define DS1721_COM_START 0x51 /* no data */
  105. #define DS1621_COM_STOP 0x22 /* no data */
  106. /* The DS1621 configuration register */
  107. #define DS1621_ALARM_TEMP_HIGH 0x40
  108. #define DS1621_ALARM_TEMP_LOW 0x20
  109. /* Conversions */
  110. #define ALARMS_FROM_REG(val) ((val) & \
  111. (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
  112. /* Each client has this additional data */
  113. struct ds1621_data {
  114. struct device *hwmon_dev;
  115. struct mutex update_lock;
  116. char valid; /* !=0 if following fields are valid */
  117. unsigned long last_updated; /* In jiffies */
  118. enum chips kind; /* device type */
  119. u16 temp[3]; /* Register values, word */
  120. u8 conf; /* Register encoding, combined */
  121. u16 update_interval; /* Conversion rate in milliseconds */
  122. };
  123. static inline int DS1621_TEMP_FROM_REG(u16 reg)
  124. {
  125. return DIV_ROUND_CLOSEST(((s16)reg / 16) * 625, 10);
  126. }
  127. /*
  128. * TEMP: 0.001C/bit (-55C to +125C)
  129. * REG:
  130. * - 1621, 1625: 0.5C/bit
  131. * - 1631, 1721: 0.0625C/bit
  132. * Assume highest resolution and let the bits fall where they may..
  133. */
  134. static inline u16 DS1621_TEMP_TO_REG(long temp)
  135. {
  136. int ntemp = clamp_val(temp, DS1621_TEMP_MIN, DS1621_TEMP_MAX);
  137. ntemp += (ntemp < 0 ? -31 : 31);
  138. ntemp = DIV_ROUND_CLOSEST(ntemp * 10, 625) << 4;
  139. return (u16)ntemp;
  140. }
  141. static void ds1621_init_client(struct i2c_client *client)
  142. {
  143. u8 conf, new_conf, sreg, resol;
  144. struct ds1621_data *data = i2c_get_clientdata(client);
  145. new_conf = conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  146. /* switch to continuous conversion mode */
  147. new_conf &= ~DS1621_REG_CONFIG_1SHOT;
  148. /* setup output polarity */
  149. if (polarity == 0)
  150. new_conf &= ~DS1621_REG_CONFIG_POLARITY;
  151. else if (polarity == 1)
  152. new_conf |= DS1621_REG_CONFIG_POLARITY;
  153. if (conf != new_conf)
  154. i2c_smbus_write_byte_data(client, DS1621_REG_CONF, new_conf);
  155. switch (data->kind) {
  156. case ds1625:
  157. data->update_interval = DS1625_CONVERSION_MAX;
  158. sreg = DS1621_COM_START;
  159. break;
  160. case ds1631:
  161. case ds1721:
  162. resol = (new_conf & DS1621_REG_CONFIG_RESOL) >>
  163. DS1621_REG_CONFIG_RESOL_SHIFT;
  164. data->update_interval = ds1721_convrates[resol];
  165. sreg = DS1721_COM_START;
  166. break;
  167. default:
  168. data->update_interval = DS1621_CONVERSION_MAX;
  169. sreg = DS1621_COM_START;
  170. break;
  171. }
  172. /* start conversion */
  173. i2c_smbus_write_byte(client, sreg);
  174. }
  175. static struct ds1621_data *ds1621_update_client(struct device *dev)
  176. {
  177. struct i2c_client *client = to_i2c_client(dev);
  178. struct ds1621_data *data = i2c_get_clientdata(client);
  179. u8 new_conf;
  180. mutex_lock(&data->update_lock);
  181. if (time_after(jiffies, data->last_updated + data->update_interval) ||
  182. !data->valid) {
  183. int i;
  184. dev_dbg(&client->dev, "Starting ds1621 update\n");
  185. data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  186. for (i = 0; i < ARRAY_SIZE(data->temp); i++)
  187. data->temp[i] = i2c_smbus_read_word_swapped(client,
  188. DS1621_REG_TEMP[i]);
  189. /* reset alarms if necessary */
  190. new_conf = data->conf;
  191. if (data->temp[0] > data->temp[1]) /* input > min */
  192. new_conf &= ~DS1621_ALARM_TEMP_LOW;
  193. if (data->temp[0] < data->temp[2]) /* input < max */
  194. new_conf &= ~DS1621_ALARM_TEMP_HIGH;
  195. if (data->conf != new_conf)
  196. i2c_smbus_write_byte_data(client, DS1621_REG_CONF,
  197. new_conf);
  198. data->last_updated = jiffies;
  199. data->valid = 1;
  200. }
  201. mutex_unlock(&data->update_lock);
  202. return data;
  203. }
  204. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  205. char *buf)
  206. {
  207. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  208. struct ds1621_data *data = ds1621_update_client(dev);
  209. return sprintf(buf, "%d\n",
  210. DS1621_TEMP_FROM_REG(data->temp[attr->index]));
  211. }
  212. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  213. const char *buf, size_t count)
  214. {
  215. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  216. struct i2c_client *client = to_i2c_client(dev);
  217. struct ds1621_data *data = i2c_get_clientdata(client);
  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);
  225. i2c_smbus_write_word_swapped(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 i2c_client *client = to_i2c_client(dev);
  247. struct ds1621_data *data = i2c_get_clientdata(client);
  248. return scnprintf(buf, PAGE_SIZE, "%hu\n", data->update_interval);
  249. }
  250. static ssize_t set_convrate(struct device *dev, struct device_attribute *da,
  251. const char *buf, size_t count)
  252. {
  253. struct i2c_client *client = to_i2c_client(dev);
  254. struct ds1621_data *data = i2c_get_clientdata(client);
  255. unsigned long convrate;
  256. s32 err;
  257. int resol = 0;
  258. err = kstrtoul(buf, 10, &convrate);
  259. if (err)
  260. return err;
  261. /* Convert rate into resolution bits */
  262. while (resol < (ARRAY_SIZE(ds1721_convrates) - 1) &&
  263. convrate > ds1721_convrates[resol])
  264. resol++;
  265. mutex_lock(&data->update_lock);
  266. data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  267. data->conf &= ~DS1621_REG_CONFIG_RESOL;
  268. data->conf |= (resol << DS1621_REG_CONFIG_RESOL_SHIFT);
  269. i2c_smbus_write_byte_data(client, DS1621_REG_CONF, data->conf);
  270. data->update_interval = ds1721_convrates[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 i2c_client *client = to_i2c_client(dev);
  299. struct ds1621_data *data = i2c_get_clientdata(client);
  300. if (attr == &dev_attr_update_interval.attr)
  301. if (data->kind == ds1621 || data->kind == ds1625)
  302. /* shhh, we're hiding update_interval */
  303. return 0;
  304. return attr->mode;
  305. }
  306. static const struct attribute_group ds1621_group = {
  307. .attrs = ds1621_attributes,
  308. .is_visible = ds1621_attribute_visible
  309. };
  310. /* Return 0 if detection is successful, -ENODEV otherwise */
  311. static int ds1621_detect(struct i2c_client *client,
  312. struct i2c_board_info *info)
  313. {
  314. struct i2c_adapter *adapter = client->adapter;
  315. int conf, temp;
  316. int i;
  317. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  318. | I2C_FUNC_SMBUS_WORD_DATA
  319. | I2C_FUNC_SMBUS_WRITE_BYTE))
  320. return -ENODEV;
  321. /*
  322. * Now, we do the remaining detection. It is lousy.
  323. *
  324. * The NVB bit should be low if no EEPROM write has been requested
  325. * during the latest 10ms, which is highly improbable in our case.
  326. */
  327. conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
  328. if (conf < 0 || conf & DS1621_REG_CONFIG_NVB)
  329. return -ENODEV;
  330. /*
  331. * The ds1621 & ds1625 use 9-bit resolution, so the 7 lowest bits
  332. * of the temperature should always be 0 (NOTE: The other chips
  333. * have multi-resolution support, so if they have 9-bit resolution
  334. * configured and the min/max temperature values set accordingly,
  335. * then if not explicitly instantiated, they *will* appear as and
  336. * emulate a ds1621 device).
  337. */
  338. for (i = 0; i < ARRAY_SIZE(DS1621_REG_TEMP); i++) {
  339. temp = i2c_smbus_read_word_data(client, DS1621_REG_TEMP[i]);
  340. if (temp < 0 || (temp & 0x7f00))
  341. return -ENODEV;
  342. }
  343. strlcpy(info->type, "ds1621", I2C_NAME_SIZE);
  344. return 0;
  345. }
  346. static int ds1621_probe(struct i2c_client *client,
  347. const struct i2c_device_id *id)
  348. {
  349. struct ds1621_data *data;
  350. int err;
  351. data = devm_kzalloc(&client->dev, sizeof(struct ds1621_data),
  352. GFP_KERNEL);
  353. if (!data)
  354. return -ENOMEM;
  355. i2c_set_clientdata(client, data);
  356. mutex_init(&data->update_lock);
  357. data->kind = id->driver_data;
  358. /* Initialize the DS1621 chip */
  359. ds1621_init_client(client);
  360. /* Register sysfs hooks */
  361. err = sysfs_create_group(&client->dev.kobj, &ds1621_group);
  362. if (err)
  363. return err;
  364. data->hwmon_dev = hwmon_device_register(&client->dev);
  365. if (IS_ERR(data->hwmon_dev)) {
  366. err = PTR_ERR(data->hwmon_dev);
  367. goto exit_remove_files;
  368. }
  369. return 0;
  370. exit_remove_files:
  371. sysfs_remove_group(&client->dev.kobj, &ds1621_group);
  372. return err;
  373. }
  374. static int ds1621_remove(struct i2c_client *client)
  375. {
  376. struct ds1621_data *data = i2c_get_clientdata(client);
  377. hwmon_device_unregister(data->hwmon_dev);
  378. sysfs_remove_group(&client->dev.kobj, &ds1621_group);
  379. return 0;
  380. }
  381. static const struct i2c_device_id ds1621_id[] = {
  382. { "ds1621", ds1621 },
  383. { "ds1625", ds1625 },
  384. { "ds1631", ds1631 },
  385. { "ds1721", ds1721 },
  386. { }
  387. };
  388. MODULE_DEVICE_TABLE(i2c, ds1621_id);
  389. /* This is the driver that will be inserted */
  390. static struct i2c_driver ds1621_driver = {
  391. .class = I2C_CLASS_HWMON,
  392. .driver = {
  393. .name = "ds1621",
  394. },
  395. .probe = ds1621_probe,
  396. .remove = ds1621_remove,
  397. .id_table = ds1621_id,
  398. .detect = ds1621_detect,
  399. .address_list = normal_i2c,
  400. };
  401. module_i2c_driver(ds1621_driver);
  402. MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
  403. MODULE_DESCRIPTION("DS1621 driver");
  404. MODULE_LICENSE("GPL");