max1619.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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/i2c-sensor.h>
  33. static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
  34. 0x29, 0x2a, 0x2b,
  35. 0x4c, 0x4d, 0x4e,
  36. I2C_CLIENT_END };
  37. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  38. /*
  39. * Insmod parameters
  40. */
  41. SENSORS_INSMOD_1(max1619);
  42. /*
  43. * The MAX1619 registers
  44. */
  45. #define MAX1619_REG_R_MAN_ID 0xFE
  46. #define MAX1619_REG_R_CHIP_ID 0xFF
  47. #define MAX1619_REG_R_CONFIG 0x03
  48. #define MAX1619_REG_W_CONFIG 0x09
  49. #define MAX1619_REG_R_CONVRATE 0x04
  50. #define MAX1619_REG_W_CONVRATE 0x0A
  51. #define MAX1619_REG_R_STATUS 0x02
  52. #define MAX1619_REG_R_LOCAL_TEMP 0x00
  53. #define MAX1619_REG_R_REMOTE_TEMP 0x01
  54. #define MAX1619_REG_R_REMOTE_HIGH 0x07
  55. #define MAX1619_REG_W_REMOTE_HIGH 0x0D
  56. #define MAX1619_REG_R_REMOTE_LOW 0x08
  57. #define MAX1619_REG_W_REMOTE_LOW 0x0E
  58. #define MAX1619_REG_R_REMOTE_CRIT 0x10
  59. #define MAX1619_REG_W_REMOTE_CRIT 0x12
  60. #define MAX1619_REG_R_TCRIT_HYST 0x11
  61. #define MAX1619_REG_W_TCRIT_HYST 0x13
  62. /*
  63. * Conversions and various macros
  64. */
  65. #define TEMP_FROM_REG(val) ((val & 0x80 ? val-0x100 : val) * 1000)
  66. #define TEMP_TO_REG(val) ((val < 0 ? val+0x100*1000 : val) / 1000)
  67. /*
  68. * Functions declaration
  69. */
  70. static int max1619_attach_adapter(struct i2c_adapter *adapter);
  71. static int max1619_detect(struct i2c_adapter *adapter, int address,
  72. int kind);
  73. static void max1619_init_client(struct i2c_client *client);
  74. static int max1619_detach_client(struct i2c_client *client);
  75. static struct max1619_data *max1619_update_device(struct device *dev);
  76. /*
  77. * Driver data (common to all clients)
  78. */
  79. static struct i2c_driver max1619_driver = {
  80. .owner = THIS_MODULE,
  81. .name = "max1619",
  82. .flags = I2C_DF_NOTIFY,
  83. .attach_adapter = max1619_attach_adapter,
  84. .detach_client = max1619_detach_client,
  85. };
  86. /*
  87. * Client data (each client gets its own)
  88. */
  89. struct max1619_data {
  90. struct i2c_client client;
  91. struct semaphore update_lock;
  92. char valid; /* zero until following fields are valid */
  93. unsigned long last_updated; /* in jiffies */
  94. /* registers values */
  95. u8 temp_input1; /* local */
  96. u8 temp_input2, temp_low2, temp_high2; /* remote */
  97. u8 temp_crit2;
  98. u8 temp_hyst2;
  99. u8 alarms;
  100. };
  101. /*
  102. * Sysfs stuff
  103. */
  104. #define show_temp(value) \
  105. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  106. { \
  107. struct max1619_data *data = max1619_update_device(dev); \
  108. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
  109. }
  110. show_temp(temp_input1);
  111. show_temp(temp_input2);
  112. show_temp(temp_low2);
  113. show_temp(temp_high2);
  114. show_temp(temp_crit2);
  115. show_temp(temp_hyst2);
  116. #define set_temp2(value, reg) \
  117. static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \
  118. size_t count) \
  119. { \
  120. struct i2c_client *client = to_i2c_client(dev); \
  121. struct max1619_data *data = i2c_get_clientdata(client); \
  122. long val = simple_strtol(buf, NULL, 10); \
  123. \
  124. down(&data->update_lock); \
  125. data->value = TEMP_TO_REG(val); \
  126. i2c_smbus_write_byte_data(client, reg, data->value); \
  127. up(&data->update_lock); \
  128. return count; \
  129. }
  130. set_temp2(temp_low2, MAX1619_REG_W_REMOTE_LOW);
  131. set_temp2(temp_high2, MAX1619_REG_W_REMOTE_HIGH);
  132. set_temp2(temp_crit2, MAX1619_REG_W_REMOTE_CRIT);
  133. set_temp2(temp_hyst2, MAX1619_REG_W_TCRIT_HYST);
  134. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  135. {
  136. struct max1619_data *data = max1619_update_device(dev);
  137. return sprintf(buf, "%d\n", data->alarms);
  138. }
  139. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  140. static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
  141. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
  142. set_temp_low2);
  143. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
  144. set_temp_high2);
  145. static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
  146. set_temp_crit2);
  147. static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst2,
  148. set_temp_hyst2);
  149. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  150. /*
  151. * Real code
  152. */
  153. static int max1619_attach_adapter(struct i2c_adapter *adapter)
  154. {
  155. if (!(adapter->class & I2C_CLASS_HWMON))
  156. return 0;
  157. return i2c_detect(adapter, &addr_data, max1619_detect);
  158. }
  159. /*
  160. * The following function does more than just detection. If detection
  161. * succeeds, it also registers the new chip.
  162. */
  163. static int max1619_detect(struct i2c_adapter *adapter, int address, int kind)
  164. {
  165. struct i2c_client *new_client;
  166. struct max1619_data *data;
  167. int err = 0;
  168. const char *name = "";
  169. u8 reg_config=0, reg_convrate=0, reg_status=0;
  170. u8 man_id, chip_id;
  171. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  172. goto exit;
  173. if (!(data = kmalloc(sizeof(struct max1619_data), GFP_KERNEL))) {
  174. err = -ENOMEM;
  175. goto exit;
  176. }
  177. memset(data, 0, sizeof(struct max1619_data));
  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. man_id = i2c_smbus_read_byte_data(new_client,
  213. MAX1619_REG_R_MAN_ID);
  214. chip_id = i2c_smbus_read_byte_data(new_client,
  215. MAX1619_REG_R_CHIP_ID);
  216. if ((man_id == 0x4D) && (chip_id == 0x04)){
  217. kind = max1619;
  218. }
  219. }
  220. if (kind <= 0) { /* identification failed */
  221. dev_info(&adapter->dev,
  222. "Unsupported chip (man_id=0x%02X, "
  223. "chip_id=0x%02X).\n", man_id, chip_id);
  224. goto exit_free;
  225. }
  226. if (kind == max1619){
  227. name = "max1619";
  228. }
  229. /* We can fill in the remaining client fields */
  230. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  231. data->valid = 0;
  232. init_MUTEX(&data->update_lock);
  233. /* Tell the I2C layer a new client has arrived */
  234. if ((err = i2c_attach_client(new_client)))
  235. goto exit_free;
  236. /* Initialize the MAX1619 chip */
  237. max1619_init_client(new_client);
  238. /* Register sysfs hooks */
  239. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  240. device_create_file(&new_client->dev, &dev_attr_temp2_input);
  241. device_create_file(&new_client->dev, &dev_attr_temp2_min);
  242. device_create_file(&new_client->dev, &dev_attr_temp2_max);
  243. device_create_file(&new_client->dev, &dev_attr_temp2_crit);
  244. device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
  245. device_create_file(&new_client->dev, &dev_attr_alarms);
  246. return 0;
  247. exit_free:
  248. kfree(data);
  249. exit:
  250. return err;
  251. }
  252. static void max1619_init_client(struct i2c_client *client)
  253. {
  254. u8 config;
  255. /*
  256. * Start the conversions.
  257. */
  258. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
  259. 5); /* 2 Hz */
  260. config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
  261. if (config & 0x40)
  262. i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
  263. config & 0xBF); /* run */
  264. }
  265. static int max1619_detach_client(struct i2c_client *client)
  266. {
  267. int err;
  268. if ((err = i2c_detach_client(client))) {
  269. dev_err(&client->dev, "Client deregistration failed, "
  270. "client not detached.\n");
  271. return err;
  272. }
  273. kfree(i2c_get_clientdata(client));
  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. down(&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. up(&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);