max1619.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * max1619.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2004 Alexey Fisher <fishor@mail.ru>
  5. * Jean Delvare <khali@linux-fr.org>
  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. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/i2c.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-sysfs.h>
  34. #include <linux/err.h>
  35. #include <linux/mutex.h>
  36. #include <linux/sysfs.h>
  37. static const unsigned short normal_i2c[] = {
  38. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  39. /*
  40. * The MAX1619 registers
  41. */
  42. #define MAX1619_REG_R_MAN_ID 0xFE
  43. #define MAX1619_REG_R_CHIP_ID 0xFF
  44. #define MAX1619_REG_R_CONFIG 0x03
  45. #define MAX1619_REG_W_CONFIG 0x09
  46. #define MAX1619_REG_R_CONVRATE 0x04
  47. #define MAX1619_REG_W_CONVRATE 0x0A
  48. #define MAX1619_REG_R_STATUS 0x02
  49. #define MAX1619_REG_R_LOCAL_TEMP 0x00
  50. #define MAX1619_REG_R_REMOTE_TEMP 0x01
  51. #define MAX1619_REG_R_REMOTE_HIGH 0x07
  52. #define MAX1619_REG_W_REMOTE_HIGH 0x0D
  53. #define MAX1619_REG_R_REMOTE_LOW 0x08
  54. #define MAX1619_REG_W_REMOTE_LOW 0x0E
  55. #define MAX1619_REG_R_REMOTE_CRIT 0x10
  56. #define MAX1619_REG_W_REMOTE_CRIT 0x12
  57. #define MAX1619_REG_R_TCRIT_HYST 0x11
  58. #define MAX1619_REG_W_TCRIT_HYST 0x13
  59. /*
  60. * Conversions
  61. */
  62. static int temp_from_reg(int val)
  63. {
  64. return (val & 0x80 ? val-0x100 : val) * 1000;
  65. }
  66. static int temp_to_reg(int val)
  67. {
  68. return (val < 0 ? val+0x100*1000 : val) / 1000;
  69. }
  70. /*
  71. * Functions declaration
  72. */
  73. static int max1619_probe(struct i2c_client *client,
  74. const struct i2c_device_id *id);
  75. static int max1619_detect(struct i2c_client *client,
  76. struct i2c_board_info *info);
  77. static void max1619_init_client(struct i2c_client *client);
  78. static int max1619_remove(struct i2c_client *client);
  79. static struct max1619_data *max1619_update_device(struct device *dev);
  80. /*
  81. * Driver data (common to all clients)
  82. */
  83. static const struct i2c_device_id max1619_id[] = {
  84. { "max1619", 0 },
  85. { }
  86. };
  87. MODULE_DEVICE_TABLE(i2c, max1619_id);
  88. static struct i2c_driver max1619_driver = {
  89. .class = I2C_CLASS_HWMON,
  90. .driver = {
  91. .name = "max1619",
  92. },
  93. .probe = max1619_probe,
  94. .remove = max1619_remove,
  95. .id_table = max1619_id,
  96. .detect = max1619_detect,
  97. .address_list = normal_i2c,
  98. };
  99. /*
  100. * Client data (each client gets its own)
  101. */
  102. struct max1619_data {
  103. struct device *hwmon_dev;
  104. struct mutex update_lock;
  105. char valid; /* zero until following fields are valid */
  106. unsigned long last_updated; /* in jiffies */
  107. /* registers values */
  108. u8 temp_input1; /* local */
  109. u8 temp_input2, temp_low2, temp_high2; /* remote */
  110. u8 temp_crit2;
  111. u8 temp_hyst2;
  112. u8 alarms;
  113. };
  114. /*
  115. * Sysfs stuff
  116. */
  117. #define show_temp(value) \
  118. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  119. { \
  120. struct max1619_data *data = max1619_update_device(dev); \
  121. return sprintf(buf, "%d\n", temp_from_reg(data->value)); \
  122. }
  123. show_temp(temp_input1);
  124. show_temp(temp_input2);
  125. show_temp(temp_low2);
  126. show_temp(temp_high2);
  127. show_temp(temp_crit2);
  128. show_temp(temp_hyst2);
  129. #define set_temp2(value, reg) \
  130. static ssize_t set_##value(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 max1619_data *data = i2c_get_clientdata(client); \
  135. long val = simple_strtol(buf, NULL, 10); \
  136. \
  137. mutex_lock(&data->update_lock); \
  138. data->value = temp_to_reg(val); \
  139. i2c_smbus_write_byte_data(client, reg, data->value); \
  140. mutex_unlock(&data->update_lock); \
  141. return count; \
  142. }
  143. set_temp2(temp_low2, MAX1619_REG_W_REMOTE_LOW);
  144. set_temp2(temp_high2, MAX1619_REG_W_REMOTE_HIGH);
  145. set_temp2(temp_crit2, MAX1619_REG_W_REMOTE_CRIT);
  146. set_temp2(temp_hyst2, MAX1619_REG_W_TCRIT_HYST);
  147. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  148. {
  149. struct max1619_data *data = max1619_update_device(dev);
  150. return sprintf(buf, "%d\n", data->alarms);
  151. }
  152. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  153. char *buf)
  154. {
  155. int bitnr = to_sensor_dev_attr(attr)->index;
  156. struct max1619_data *data = max1619_update_device(dev);
  157. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  158. }
  159. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  160. static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
  161. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
  162. set_temp_low2);
  163. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
  164. set_temp_high2);
  165. static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
  166. set_temp_crit2);
  167. static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst2,
  168. set_temp_hyst2);
  169. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  170. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  171. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  172. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  173. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  174. static struct attribute *max1619_attributes[] = {
  175. &dev_attr_temp1_input.attr,
  176. &dev_attr_temp2_input.attr,
  177. &dev_attr_temp2_min.attr,
  178. &dev_attr_temp2_max.attr,
  179. &dev_attr_temp2_crit.attr,
  180. &dev_attr_temp2_crit_hyst.attr,
  181. &dev_attr_alarms.attr,
  182. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  183. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  184. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  185. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  186. NULL
  187. };
  188. static const struct attribute_group max1619_group = {
  189. .attrs = max1619_attributes,
  190. };
  191. /*
  192. * Real code
  193. */
  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 int max1619_probe(struct i2c_client *new_client,
  225. const struct i2c_device_id *id)
  226. {
  227. struct max1619_data *data;
  228. int err;
  229. data = kzalloc(sizeof(struct max1619_data), GFP_KERNEL);
  230. if (!data) {
  231. err = -ENOMEM;
  232. goto exit;
  233. }
  234. i2c_set_clientdata(new_client, data);
  235. data->valid = 0;
  236. mutex_init(&data->update_lock);
  237. /* Initialize the MAX1619 chip */
  238. max1619_init_client(new_client);
  239. /* Register sysfs hooks */
  240. if ((err = sysfs_create_group(&new_client->dev.kobj, &max1619_group)))
  241. goto exit_free;
  242. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  243. if (IS_ERR(data->hwmon_dev)) {
  244. err = PTR_ERR(data->hwmon_dev);
  245. goto exit_remove_files;
  246. }
  247. return 0;
  248. exit_remove_files:
  249. sysfs_remove_group(&new_client->dev.kobj, &max1619_group);
  250. exit_free:
  251. kfree(data);
  252. exit:
  253. return err;
  254. }
  255. static void max1619_init_client(struct i2c_client *client)
  256. {
  257. u8 config;
  258. /*
  259. * Start the conversions.
  260. */
  261. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
  262. 5); /* 2 Hz */
  263. config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
  264. if (config & 0x40)
  265. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
  266. config & 0xBF); /* run */
  267. }
  268. static int max1619_remove(struct i2c_client *client)
  269. {
  270. struct max1619_data *data = i2c_get_clientdata(client);
  271. hwmon_device_unregister(data->hwmon_dev);
  272. sysfs_remove_group(&client->dev.kobj, &max1619_group);
  273. kfree(data);
  274. return 0;
  275. }
  276. static struct max1619_data *max1619_update_device(struct device *dev)
  277. {
  278. struct i2c_client *client = to_i2c_client(dev);
  279. struct max1619_data *data = i2c_get_clientdata(client);
  280. mutex_lock(&data->update_lock);
  281. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  282. dev_dbg(&client->dev, "Updating max1619 data.\n");
  283. data->temp_input1 = i2c_smbus_read_byte_data(client,
  284. MAX1619_REG_R_LOCAL_TEMP);
  285. data->temp_input2 = i2c_smbus_read_byte_data(client,
  286. MAX1619_REG_R_REMOTE_TEMP);
  287. data->temp_high2 = i2c_smbus_read_byte_data(client,
  288. MAX1619_REG_R_REMOTE_HIGH);
  289. data->temp_low2 = i2c_smbus_read_byte_data(client,
  290. MAX1619_REG_R_REMOTE_LOW);
  291. data->temp_crit2 = i2c_smbus_read_byte_data(client,
  292. MAX1619_REG_R_REMOTE_CRIT);
  293. data->temp_hyst2 = i2c_smbus_read_byte_data(client,
  294. MAX1619_REG_R_TCRIT_HYST);
  295. data->alarms = i2c_smbus_read_byte_data(client,
  296. MAX1619_REG_R_STATUS);
  297. data->last_updated = jiffies;
  298. data->valid = 1;
  299. }
  300. mutex_unlock(&data->update_lock);
  301. return data;
  302. }
  303. static int __init sensors_max1619_init(void)
  304. {
  305. return i2c_add_driver(&max1619_driver);
  306. }
  307. static void __exit sensors_max1619_exit(void)
  308. {
  309. i2c_del_driver(&max1619_driver);
  310. }
  311. MODULE_AUTHOR("Alexey Fisher <fishor@mail.ru> and "
  312. "Jean Delvare <khali@linux-fr.org>");
  313. MODULE_DESCRIPTION("MAX1619 sensor driver");
  314. MODULE_LICENSE("GPL");
  315. module_init(sensors_max1619_init);
  316. module_exit(sensors_max1619_exit);