max1619.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * max1619.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2004 Oleksij Rempel <bug-track@fisher-privat.net>
  5. * Jean Delvare <jdelvare@suse.de>
  6. *
  7. * Based on the lm90 driver. The MAX1619 is a sensor chip made by Maxim.
  8. * It reports up to two temperatures (its own plus up to
  9. * one external one). Complete datasheet can be
  10. * obtained from Maxim's website at:
  11. * http://pdfserv.maxim-ic.com/en/ds/MAX1619.pdf
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  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. static const unsigned short normal_i2c[] = {
  34. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  35. /*
  36. * The MAX1619 registers
  37. */
  38. #define MAX1619_REG_R_MAN_ID 0xFE
  39. #define MAX1619_REG_R_CHIP_ID 0xFF
  40. #define MAX1619_REG_R_CONFIG 0x03
  41. #define MAX1619_REG_W_CONFIG 0x09
  42. #define MAX1619_REG_R_CONVRATE 0x04
  43. #define MAX1619_REG_W_CONVRATE 0x0A
  44. #define MAX1619_REG_R_STATUS 0x02
  45. #define MAX1619_REG_R_LOCAL_TEMP 0x00
  46. #define MAX1619_REG_R_REMOTE_TEMP 0x01
  47. #define MAX1619_REG_R_REMOTE_HIGH 0x07
  48. #define MAX1619_REG_W_REMOTE_HIGH 0x0D
  49. #define MAX1619_REG_R_REMOTE_LOW 0x08
  50. #define MAX1619_REG_W_REMOTE_LOW 0x0E
  51. #define MAX1619_REG_R_REMOTE_CRIT 0x10
  52. #define MAX1619_REG_W_REMOTE_CRIT 0x12
  53. #define MAX1619_REG_R_TCRIT_HYST 0x11
  54. #define MAX1619_REG_W_TCRIT_HYST 0x13
  55. /*
  56. * Conversions
  57. */
  58. static int temp_from_reg(int val)
  59. {
  60. return (val & 0x80 ? val-0x100 : val) * 1000;
  61. }
  62. static int temp_to_reg(int val)
  63. {
  64. return (val < 0 ? val+0x100*1000 : val) / 1000;
  65. }
  66. enum temp_index {
  67. t_input1 = 0,
  68. t_input2,
  69. t_low2,
  70. t_high2,
  71. t_crit2,
  72. t_hyst2,
  73. t_num_regs
  74. };
  75. /*
  76. * Client data (each client gets its own)
  77. */
  78. struct max1619_data {
  79. struct i2c_client *client;
  80. struct mutex update_lock;
  81. char valid; /* zero until following fields are valid */
  82. unsigned long last_updated; /* in jiffies */
  83. /* registers values */
  84. u8 temp[t_num_regs]; /* index with enum temp_index */
  85. u8 alarms;
  86. };
  87. static const u8 regs_read[t_num_regs] = {
  88. [t_input1] = MAX1619_REG_R_LOCAL_TEMP,
  89. [t_input2] = MAX1619_REG_R_REMOTE_TEMP,
  90. [t_low2] = MAX1619_REG_R_REMOTE_LOW,
  91. [t_high2] = MAX1619_REG_R_REMOTE_HIGH,
  92. [t_crit2] = MAX1619_REG_R_REMOTE_CRIT,
  93. [t_hyst2] = MAX1619_REG_R_TCRIT_HYST,
  94. };
  95. static const u8 regs_write[t_num_regs] = {
  96. [t_low2] = MAX1619_REG_W_REMOTE_LOW,
  97. [t_high2] = MAX1619_REG_W_REMOTE_HIGH,
  98. [t_crit2] = MAX1619_REG_W_REMOTE_CRIT,
  99. [t_hyst2] = MAX1619_REG_W_TCRIT_HYST,
  100. };
  101. static struct max1619_data *max1619_update_device(struct device *dev)
  102. {
  103. struct max1619_data *data = dev_get_drvdata(dev);
  104. struct i2c_client *client = data->client;
  105. int config, i;
  106. mutex_lock(&data->update_lock);
  107. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  108. dev_dbg(&client->dev, "Updating max1619 data.\n");
  109. for (i = 0; i < t_num_regs; i++)
  110. data->temp[i] = i2c_smbus_read_byte_data(client,
  111. regs_read[i]);
  112. data->alarms = i2c_smbus_read_byte_data(client,
  113. MAX1619_REG_R_STATUS);
  114. /* If OVERT polarity is low, reverse alarm bit */
  115. config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
  116. if (!(config & 0x20))
  117. data->alarms ^= 0x02;
  118. data->last_updated = jiffies;
  119. data->valid = 1;
  120. }
  121. mutex_unlock(&data->update_lock);
  122. return data;
  123. }
  124. /*
  125. * Sysfs stuff
  126. */
  127. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  128. char *buf)
  129. {
  130. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  131. struct max1619_data *data = max1619_update_device(dev);
  132. return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
  133. }
  134. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  135. const char *buf, size_t count)
  136. {
  137. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  138. struct max1619_data *data = dev_get_drvdata(dev);
  139. struct i2c_client *client = data->client;
  140. long val;
  141. int err = kstrtol(buf, 10, &val);
  142. if (err)
  143. return err;
  144. mutex_lock(&data->update_lock);
  145. data->temp[attr->index] = temp_to_reg(val);
  146. i2c_smbus_write_byte_data(client, regs_write[attr->index],
  147. data->temp[attr->index]);
  148. mutex_unlock(&data->update_lock);
  149. return count;
  150. }
  151. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  152. char *buf)
  153. {
  154. struct max1619_data *data = max1619_update_device(dev);
  155. return sprintf(buf, "%d\n", data->alarms);
  156. }
  157. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  158. char *buf)
  159. {
  160. int bitnr = to_sensor_dev_attr(attr)->index;
  161. struct max1619_data *data = max1619_update_device(dev);
  162. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  163. }
  164. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input1);
  165. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, t_input2);
  166. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp, set_temp,
  167. t_low2);
  168. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp, set_temp,
  169. t_high2);
  170. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp, set_temp,
  171. t_crit2);
  172. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp,
  173. set_temp, t_hyst2);
  174. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  175. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  176. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  177. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  178. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  179. static struct attribute *max1619_attrs[] = {
  180. &sensor_dev_attr_temp1_input.dev_attr.attr,
  181. &sensor_dev_attr_temp2_input.dev_attr.attr,
  182. &sensor_dev_attr_temp2_min.dev_attr.attr,
  183. &sensor_dev_attr_temp2_max.dev_attr.attr,
  184. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  185. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  186. &dev_attr_alarms.attr,
  187. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  188. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  189. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  190. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  191. NULL
  192. };
  193. ATTRIBUTE_GROUPS(max1619);
  194. /* Return 0 if detection is successful, -ENODEV otherwise */
  195. static int max1619_detect(struct i2c_client *client,
  196. struct i2c_board_info *info)
  197. {
  198. struct i2c_adapter *adapter = client->adapter;
  199. u8 reg_config, reg_convrate, reg_status, man_id, chip_id;
  200. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  201. return -ENODEV;
  202. /* detection */
  203. reg_config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
  204. reg_convrate = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONVRATE);
  205. reg_status = i2c_smbus_read_byte_data(client, MAX1619_REG_R_STATUS);
  206. if ((reg_config & 0x03) != 0x00
  207. || reg_convrate > 0x07 || (reg_status & 0x61) != 0x00) {
  208. dev_dbg(&adapter->dev, "MAX1619 detection failed at 0x%02x\n",
  209. client->addr);
  210. return -ENODEV;
  211. }
  212. /* identification */
  213. man_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_MAN_ID);
  214. chip_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CHIP_ID);
  215. if (man_id != 0x4D || chip_id != 0x04) {
  216. dev_info(&adapter->dev,
  217. "Unsupported chip (man_id=0x%02X, chip_id=0x%02X).\n",
  218. man_id, chip_id);
  219. return -ENODEV;
  220. }
  221. strlcpy(info->type, "max1619", I2C_NAME_SIZE);
  222. return 0;
  223. }
  224. static void max1619_init_client(struct i2c_client *client)
  225. {
  226. u8 config;
  227. /*
  228. * Start the conversions.
  229. */
  230. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
  231. 5); /* 2 Hz */
  232. config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
  233. if (config & 0x40)
  234. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
  235. config & 0xBF); /* run */
  236. }
  237. static int max1619_probe(struct i2c_client *new_client,
  238. const struct i2c_device_id *id)
  239. {
  240. struct max1619_data *data;
  241. struct device *hwmon_dev;
  242. data = devm_kzalloc(&new_client->dev, sizeof(struct max1619_data),
  243. GFP_KERNEL);
  244. if (!data)
  245. return -ENOMEM;
  246. data->client = new_client;
  247. mutex_init(&data->update_lock);
  248. /* Initialize the MAX1619 chip */
  249. max1619_init_client(new_client);
  250. hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
  251. new_client->name,
  252. data,
  253. max1619_groups);
  254. return PTR_ERR_OR_ZERO(hwmon_dev);
  255. }
  256. static const struct i2c_device_id max1619_id[] = {
  257. { "max1619", 0 },
  258. { }
  259. };
  260. MODULE_DEVICE_TABLE(i2c, max1619_id);
  261. static struct i2c_driver max1619_driver = {
  262. .class = I2C_CLASS_HWMON,
  263. .driver = {
  264. .name = "max1619",
  265. },
  266. .probe = max1619_probe,
  267. .id_table = max1619_id,
  268. .detect = max1619_detect,
  269. .address_list = normal_i2c,
  270. };
  271. module_i2c_driver(max1619_driver);
  272. MODULE_AUTHOR("Oleksij Rempel <bug-track@fisher-privat.net>, Jean Delvare <jdelvare@suse.de>");
  273. MODULE_DESCRIPTION("MAX1619 sensor driver");
  274. MODULE_LICENSE("GPL");