max1619.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. * Insmod parameters
  41. */
  42. I2C_CLIENT_INSMOD_1(max1619);
  43. /*
  44. * The MAX1619 registers
  45. */
  46. #define MAX1619_REG_R_MAN_ID 0xFE
  47. #define MAX1619_REG_R_CHIP_ID 0xFF
  48. #define MAX1619_REG_R_CONFIG 0x03
  49. #define MAX1619_REG_W_CONFIG 0x09
  50. #define MAX1619_REG_R_CONVRATE 0x04
  51. #define MAX1619_REG_W_CONVRATE 0x0A
  52. #define MAX1619_REG_R_STATUS 0x02
  53. #define MAX1619_REG_R_LOCAL_TEMP 0x00
  54. #define MAX1619_REG_R_REMOTE_TEMP 0x01
  55. #define MAX1619_REG_R_REMOTE_HIGH 0x07
  56. #define MAX1619_REG_W_REMOTE_HIGH 0x0D
  57. #define MAX1619_REG_R_REMOTE_LOW 0x08
  58. #define MAX1619_REG_W_REMOTE_LOW 0x0E
  59. #define MAX1619_REG_R_REMOTE_CRIT 0x10
  60. #define MAX1619_REG_W_REMOTE_CRIT 0x12
  61. #define MAX1619_REG_R_TCRIT_HYST 0x11
  62. #define MAX1619_REG_W_TCRIT_HYST 0x13
  63. /*
  64. * Conversions
  65. */
  66. static int temp_from_reg(int val)
  67. {
  68. return (val & 0x80 ? val-0x100 : val) * 1000;
  69. }
  70. static int temp_to_reg(int val)
  71. {
  72. return (val < 0 ? val+0x100*1000 : val) / 1000;
  73. }
  74. /*
  75. * Functions declaration
  76. */
  77. static int max1619_probe(struct i2c_client *client,
  78. const struct i2c_device_id *id);
  79. static int max1619_detect(struct i2c_client *client, int kind,
  80. struct i2c_board_info *info);
  81. static void max1619_init_client(struct i2c_client *client);
  82. static int max1619_remove(struct i2c_client *client);
  83. static struct max1619_data *max1619_update_device(struct device *dev);
  84. /*
  85. * Driver data (common to all clients)
  86. */
  87. static const struct i2c_device_id max1619_id[] = {
  88. { "max1619", max1619 },
  89. { }
  90. };
  91. MODULE_DEVICE_TABLE(i2c, max1619_id);
  92. static struct i2c_driver max1619_driver = {
  93. .class = I2C_CLASS_HWMON,
  94. .driver = {
  95. .name = "max1619",
  96. },
  97. .probe = max1619_probe,
  98. .remove = max1619_remove,
  99. .id_table = max1619_id,
  100. .detect = max1619_detect,
  101. .address_data = &addr_data,
  102. };
  103. /*
  104. * Client data (each client gets its own)
  105. */
  106. struct max1619_data {
  107. struct device *hwmon_dev;
  108. struct mutex update_lock;
  109. char valid; /* zero until following fields are valid */
  110. unsigned long last_updated; /* in jiffies */
  111. /* registers values */
  112. u8 temp_input1; /* local */
  113. u8 temp_input2, temp_low2, temp_high2; /* remote */
  114. u8 temp_crit2;
  115. u8 temp_hyst2;
  116. u8 alarms;
  117. };
  118. /*
  119. * Sysfs stuff
  120. */
  121. #define show_temp(value) \
  122. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  123. { \
  124. struct max1619_data *data = max1619_update_device(dev); \
  125. return sprintf(buf, "%d\n", temp_from_reg(data->value)); \
  126. }
  127. show_temp(temp_input1);
  128. show_temp(temp_input2);
  129. show_temp(temp_low2);
  130. show_temp(temp_high2);
  131. show_temp(temp_crit2);
  132. show_temp(temp_hyst2);
  133. #define set_temp2(value, reg) \
  134. static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \
  135. size_t count) \
  136. { \
  137. struct i2c_client *client = to_i2c_client(dev); \
  138. struct max1619_data *data = i2c_get_clientdata(client); \
  139. long val = simple_strtol(buf, NULL, 10); \
  140. \
  141. mutex_lock(&data->update_lock); \
  142. data->value = temp_to_reg(val); \
  143. i2c_smbus_write_byte_data(client, reg, data->value); \
  144. mutex_unlock(&data->update_lock); \
  145. return count; \
  146. }
  147. set_temp2(temp_low2, MAX1619_REG_W_REMOTE_LOW);
  148. set_temp2(temp_high2, MAX1619_REG_W_REMOTE_HIGH);
  149. set_temp2(temp_crit2, MAX1619_REG_W_REMOTE_CRIT);
  150. set_temp2(temp_hyst2, MAX1619_REG_W_TCRIT_HYST);
  151. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  152. {
  153. struct max1619_data *data = max1619_update_device(dev);
  154. return sprintf(buf, "%d\n", data->alarms);
  155. }
  156. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  157. char *buf)
  158. {
  159. int bitnr = to_sensor_dev_attr(attr)->index;
  160. struct max1619_data *data = max1619_update_device(dev);
  161. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  162. }
  163. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  164. static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
  165. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
  166. set_temp_low2);
  167. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
  168. set_temp_high2);
  169. static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
  170. set_temp_crit2);
  171. static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst2,
  172. set_temp_hyst2);
  173. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  174. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  175. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  176. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  177. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  178. static struct attribute *max1619_attributes[] = {
  179. &dev_attr_temp1_input.attr,
  180. &dev_attr_temp2_input.attr,
  181. &dev_attr_temp2_min.attr,
  182. &dev_attr_temp2_max.attr,
  183. &dev_attr_temp2_crit.attr,
  184. &dev_attr_temp2_crit_hyst.attr,
  185. &dev_attr_alarms.attr,
  186. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  187. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  188. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  189. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  190. NULL
  191. };
  192. static const struct attribute_group max1619_group = {
  193. .attrs = max1619_attributes,
  194. };
  195. /*
  196. * Real code
  197. */
  198. /* Return 0 if detection is successful, -ENODEV otherwise */
  199. static int max1619_detect(struct i2c_client *client, int kind,
  200. struct i2c_board_info *info)
  201. {
  202. struct i2c_adapter *adapter = client->adapter;
  203. u8 reg_config, reg_convrate, reg_status, man_id, chip_id;
  204. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  205. return -ENODEV;
  206. /* detection */
  207. reg_config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
  208. reg_convrate = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONVRATE);
  209. reg_status = i2c_smbus_read_byte_data(client, MAX1619_REG_R_STATUS);
  210. if ((reg_config & 0x03) != 0x00
  211. || reg_convrate > 0x07 || (reg_status & 0x61) != 0x00) {
  212. dev_dbg(&adapter->dev, "MAX1619 detection failed at 0x%02x\n",
  213. client->addr);
  214. return -ENODEV;
  215. }
  216. /* identification */
  217. man_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_MAN_ID);
  218. chip_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CHIP_ID);
  219. if (man_id != 0x4D || chip_id != 0x04) {
  220. dev_info(&adapter->dev,
  221. "Unsupported chip (man_id=0x%02X, chip_id=0x%02X).\n",
  222. man_id, chip_id);
  223. return -ENODEV;
  224. }
  225. strlcpy(info->type, "max1619", I2C_NAME_SIZE);
  226. return 0;
  227. }
  228. static int max1619_probe(struct i2c_client *new_client,
  229. const struct i2c_device_id *id)
  230. {
  231. struct max1619_data *data;
  232. int err;
  233. data = kzalloc(sizeof(struct max1619_data), GFP_KERNEL);
  234. if (!data) {
  235. err = -ENOMEM;
  236. goto exit;
  237. }
  238. i2c_set_clientdata(new_client, data);
  239. data->valid = 0;
  240. mutex_init(&data->update_lock);
  241. /* Initialize the MAX1619 chip */
  242. max1619_init_client(new_client);
  243. /* Register sysfs hooks */
  244. if ((err = sysfs_create_group(&new_client->dev.kobj, &max1619_group)))
  245. goto exit_free;
  246. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  247. if (IS_ERR(data->hwmon_dev)) {
  248. err = PTR_ERR(data->hwmon_dev);
  249. goto exit_remove_files;
  250. }
  251. return 0;
  252. exit_remove_files:
  253. sysfs_remove_group(&new_client->dev.kobj, &max1619_group);
  254. exit_free:
  255. kfree(data);
  256. exit:
  257. return err;
  258. }
  259. static void max1619_init_client(struct i2c_client *client)
  260. {
  261. u8 config;
  262. /*
  263. * Start the conversions.
  264. */
  265. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
  266. 5); /* 2 Hz */
  267. config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
  268. if (config & 0x40)
  269. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
  270. config & 0xBF); /* run */
  271. }
  272. static int max1619_remove(struct i2c_client *client)
  273. {
  274. struct max1619_data *data = i2c_get_clientdata(client);
  275. hwmon_device_unregister(data->hwmon_dev);
  276. sysfs_remove_group(&client->dev.kobj, &max1619_group);
  277. kfree(data);
  278. return 0;
  279. }
  280. static struct max1619_data *max1619_update_device(struct device *dev)
  281. {
  282. struct i2c_client *client = to_i2c_client(dev);
  283. struct max1619_data *data = i2c_get_clientdata(client);
  284. mutex_lock(&data->update_lock);
  285. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  286. dev_dbg(&client->dev, "Updating max1619 data.\n");
  287. data->temp_input1 = i2c_smbus_read_byte_data(client,
  288. MAX1619_REG_R_LOCAL_TEMP);
  289. data->temp_input2 = i2c_smbus_read_byte_data(client,
  290. MAX1619_REG_R_REMOTE_TEMP);
  291. data->temp_high2 = i2c_smbus_read_byte_data(client,
  292. MAX1619_REG_R_REMOTE_HIGH);
  293. data->temp_low2 = i2c_smbus_read_byte_data(client,
  294. MAX1619_REG_R_REMOTE_LOW);
  295. data->temp_crit2 = i2c_smbus_read_byte_data(client,
  296. MAX1619_REG_R_REMOTE_CRIT);
  297. data->temp_hyst2 = i2c_smbus_read_byte_data(client,
  298. MAX1619_REG_R_TCRIT_HYST);
  299. data->alarms = i2c_smbus_read_byte_data(client,
  300. MAX1619_REG_R_STATUS);
  301. data->last_updated = jiffies;
  302. data->valid = 1;
  303. }
  304. mutex_unlock(&data->update_lock);
  305. return data;
  306. }
  307. static int __init sensors_max1619_init(void)
  308. {
  309. return i2c_add_driver(&max1619_driver);
  310. }
  311. static void __exit sensors_max1619_exit(void)
  312. {
  313. i2c_del_driver(&max1619_driver);
  314. }
  315. MODULE_AUTHOR("Alexey Fisher <fishor@mail.ru> and "
  316. "Jean Delvare <khali@linux-fr.org>");
  317. MODULE_DESCRIPTION("MAX1619 sensor driver");
  318. MODULE_LICENSE("GPL");
  319. module_init(sensors_max1619_init);
  320. module_exit(sensors_max1619_exit);