max1619.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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, \
  119. char *buf) \
  120. { \
  121. struct max1619_data *data = max1619_update_device(dev); \
  122. return sprintf(buf, "%d\n", temp_from_reg(data->value)); \
  123. }
  124. show_temp(temp_input1);
  125. show_temp(temp_input2);
  126. show_temp(temp_low2);
  127. show_temp(temp_high2);
  128. show_temp(temp_crit2);
  129. show_temp(temp_hyst2);
  130. #define set_temp2(value, reg) \
  131. static ssize_t set_##value(struct device *dev, struct device_attribute *attr, \
  132. const char *buf, \
  133. size_t count) \
  134. { \
  135. struct i2c_client *client = to_i2c_client(dev); \
  136. struct max1619_data *data = i2c_get_clientdata(client); \
  137. long val; \
  138. int err = kstrtol(buf, 10, &val); \
  139. if (err) \
  140. return err; \
  141. \
  142. mutex_lock(&data->update_lock); \
  143. data->value = temp_to_reg(val); \
  144. i2c_smbus_write_byte_data(client, reg, data->value); \
  145. mutex_unlock(&data->update_lock); \
  146. return count; \
  147. }
  148. set_temp2(temp_low2, MAX1619_REG_W_REMOTE_LOW);
  149. set_temp2(temp_high2, MAX1619_REG_W_REMOTE_HIGH);
  150. set_temp2(temp_crit2, MAX1619_REG_W_REMOTE_CRIT);
  151. set_temp2(temp_hyst2, MAX1619_REG_W_TCRIT_HYST);
  152. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  153. char *buf)
  154. {
  155. struct max1619_data *data = max1619_update_device(dev);
  156. return sprintf(buf, "%d\n", data->alarms);
  157. }
  158. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  159. char *buf)
  160. {
  161. int bitnr = to_sensor_dev_attr(attr)->index;
  162. struct max1619_data *data = max1619_update_device(dev);
  163. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  164. }
  165. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  166. static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
  167. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
  168. set_temp_low2);
  169. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
  170. set_temp_high2);
  171. static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
  172. set_temp_crit2);
  173. static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst2,
  174. set_temp_hyst2);
  175. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  176. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  177. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  178. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  179. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  180. static struct attribute *max1619_attributes[] = {
  181. &dev_attr_temp1_input.attr,
  182. &dev_attr_temp2_input.attr,
  183. &dev_attr_temp2_min.attr,
  184. &dev_attr_temp2_max.attr,
  185. &dev_attr_temp2_crit.attr,
  186. &dev_attr_temp2_crit_hyst.attr,
  187. &dev_attr_alarms.attr,
  188. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  189. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  190. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  191. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  192. NULL
  193. };
  194. static const struct attribute_group max1619_group = {
  195. .attrs = max1619_attributes,
  196. };
  197. /*
  198. * Real code
  199. */
  200. /* Return 0 if detection is successful, -ENODEV otherwise */
  201. static int max1619_detect(struct i2c_client *client,
  202. struct i2c_board_info *info)
  203. {
  204. struct i2c_adapter *adapter = client->adapter;
  205. u8 reg_config, reg_convrate, reg_status, man_id, chip_id;
  206. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  207. return -ENODEV;
  208. /* detection */
  209. reg_config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
  210. reg_convrate = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONVRATE);
  211. reg_status = i2c_smbus_read_byte_data(client, MAX1619_REG_R_STATUS);
  212. if ((reg_config & 0x03) != 0x00
  213. || reg_convrate > 0x07 || (reg_status & 0x61) != 0x00) {
  214. dev_dbg(&adapter->dev, "MAX1619 detection failed at 0x%02x\n",
  215. client->addr);
  216. return -ENODEV;
  217. }
  218. /* identification */
  219. man_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_MAN_ID);
  220. chip_id = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CHIP_ID);
  221. if (man_id != 0x4D || chip_id != 0x04) {
  222. dev_info(&adapter->dev,
  223. "Unsupported chip (man_id=0x%02X, chip_id=0x%02X).\n",
  224. man_id, chip_id);
  225. return -ENODEV;
  226. }
  227. strlcpy(info->type, "max1619", I2C_NAME_SIZE);
  228. return 0;
  229. }
  230. static int max1619_probe(struct i2c_client *new_client,
  231. const struct i2c_device_id *id)
  232. {
  233. struct max1619_data *data;
  234. int err;
  235. data = kzalloc(sizeof(struct max1619_data), GFP_KERNEL);
  236. if (!data) {
  237. err = -ENOMEM;
  238. goto exit;
  239. }
  240. i2c_set_clientdata(new_client, data);
  241. data->valid = 0;
  242. mutex_init(&data->update_lock);
  243. /* Initialize the MAX1619 chip */
  244. max1619_init_client(new_client);
  245. /* Register sysfs hooks */
  246. err = sysfs_create_group(&new_client->dev.kobj, &max1619_group);
  247. if (err)
  248. goto exit_free;
  249. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  250. if (IS_ERR(data->hwmon_dev)) {
  251. err = PTR_ERR(data->hwmon_dev);
  252. goto exit_remove_files;
  253. }
  254. return 0;
  255. exit_remove_files:
  256. sysfs_remove_group(&new_client->dev.kobj, &max1619_group);
  257. exit_free:
  258. kfree(data);
  259. exit:
  260. return err;
  261. }
  262. static void max1619_init_client(struct i2c_client *client)
  263. {
  264. u8 config;
  265. /*
  266. * Start the conversions.
  267. */
  268. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
  269. 5); /* 2 Hz */
  270. config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
  271. if (config & 0x40)
  272. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
  273. config & 0xBF); /* run */
  274. }
  275. static int max1619_remove(struct i2c_client *client)
  276. {
  277. struct max1619_data *data = i2c_get_clientdata(client);
  278. hwmon_device_unregister(data->hwmon_dev);
  279. sysfs_remove_group(&client->dev.kobj, &max1619_group);
  280. kfree(data);
  281. return 0;
  282. }
  283. static struct max1619_data *max1619_update_device(struct device *dev)
  284. {
  285. struct i2c_client *client = to_i2c_client(dev);
  286. struct max1619_data *data = i2c_get_clientdata(client);
  287. mutex_lock(&data->update_lock);
  288. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  289. dev_dbg(&client->dev, "Updating max1619 data.\n");
  290. data->temp_input1 = i2c_smbus_read_byte_data(client,
  291. MAX1619_REG_R_LOCAL_TEMP);
  292. data->temp_input2 = i2c_smbus_read_byte_data(client,
  293. MAX1619_REG_R_REMOTE_TEMP);
  294. data->temp_high2 = i2c_smbus_read_byte_data(client,
  295. MAX1619_REG_R_REMOTE_HIGH);
  296. data->temp_low2 = i2c_smbus_read_byte_data(client,
  297. MAX1619_REG_R_REMOTE_LOW);
  298. data->temp_crit2 = i2c_smbus_read_byte_data(client,
  299. MAX1619_REG_R_REMOTE_CRIT);
  300. data->temp_hyst2 = i2c_smbus_read_byte_data(client,
  301. MAX1619_REG_R_TCRIT_HYST);
  302. data->alarms = i2c_smbus_read_byte_data(client,
  303. MAX1619_REG_R_STATUS);
  304. data->last_updated = jiffies;
  305. data->valid = 1;
  306. }
  307. mutex_unlock(&data->update_lock);
  308. return data;
  309. }
  310. module_i2c_driver(max1619_driver);
  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");