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