max1619.c 11 KB

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