ds1621.c 11 KB

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