lm83.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2009 Jean Delvare <jdelvare@suse.de>
  5. *
  6. * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
  7. * a sensor chip made by National Semiconductor. It reports up to four
  8. * temperatures (its own plus up to three external ones) with a 1 deg
  9. * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
  10. * from National's website at:
  11. * http://www.national.com/pf/LM/LM83.html
  12. * Since the datasheet omits to give the chip stepping code, I give it
  13. * here: 0x03 (at register 0xff).
  14. *
  15. * Also supports the LM82 temp sensor, which is basically a stripped down
  16. * model of the LM83. Datasheet is here:
  17. * http://www.national.com/pf/LM/LM82.html
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/slab.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/i2c.h>
  34. #include <linux/hwmon-sysfs.h>
  35. #include <linux/hwmon.h>
  36. #include <linux/err.h>
  37. #include <linux/mutex.h>
  38. #include <linux/sysfs.h>
  39. /*
  40. * Addresses to scan
  41. * Address is selected using 2 three-level pins, resulting in 9 possible
  42. * addresses.
  43. */
  44. static const unsigned short normal_i2c[] = {
  45. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  46. enum chips { lm83, lm82 };
  47. /*
  48. * The LM83 registers
  49. * Manufacturer ID is 0x01 for National Semiconductor.
  50. */
  51. #define LM83_REG_R_MAN_ID 0xFE
  52. #define LM83_REG_R_CHIP_ID 0xFF
  53. #define LM83_REG_R_CONFIG 0x03
  54. #define LM83_REG_W_CONFIG 0x09
  55. #define LM83_REG_R_STATUS1 0x02
  56. #define LM83_REG_R_STATUS2 0x35
  57. #define LM83_REG_R_LOCAL_TEMP 0x00
  58. #define LM83_REG_R_LOCAL_HIGH 0x05
  59. #define LM83_REG_W_LOCAL_HIGH 0x0B
  60. #define LM83_REG_R_REMOTE1_TEMP 0x30
  61. #define LM83_REG_R_REMOTE1_HIGH 0x38
  62. #define LM83_REG_W_REMOTE1_HIGH 0x50
  63. #define LM83_REG_R_REMOTE2_TEMP 0x01
  64. #define LM83_REG_R_REMOTE2_HIGH 0x07
  65. #define LM83_REG_W_REMOTE2_HIGH 0x0D
  66. #define LM83_REG_R_REMOTE3_TEMP 0x31
  67. #define LM83_REG_R_REMOTE3_HIGH 0x3A
  68. #define LM83_REG_W_REMOTE3_HIGH 0x52
  69. #define LM83_REG_R_TCRIT 0x42
  70. #define LM83_REG_W_TCRIT 0x5A
  71. /*
  72. * Conversions and various macros
  73. * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
  74. */
  75. #define TEMP_FROM_REG(val) ((val) * 1000)
  76. #define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
  77. (val) >= 127000 ? 127 : \
  78. (val) < 0 ? ((val) - 500) / 1000 : \
  79. ((val) + 500) / 1000)
  80. static const u8 LM83_REG_R_TEMP[] = {
  81. LM83_REG_R_LOCAL_TEMP,
  82. LM83_REG_R_REMOTE1_TEMP,
  83. LM83_REG_R_REMOTE2_TEMP,
  84. LM83_REG_R_REMOTE3_TEMP,
  85. LM83_REG_R_LOCAL_HIGH,
  86. LM83_REG_R_REMOTE1_HIGH,
  87. LM83_REG_R_REMOTE2_HIGH,
  88. LM83_REG_R_REMOTE3_HIGH,
  89. LM83_REG_R_TCRIT,
  90. };
  91. static const u8 LM83_REG_W_HIGH[] = {
  92. LM83_REG_W_LOCAL_HIGH,
  93. LM83_REG_W_REMOTE1_HIGH,
  94. LM83_REG_W_REMOTE2_HIGH,
  95. LM83_REG_W_REMOTE3_HIGH,
  96. LM83_REG_W_TCRIT,
  97. };
  98. /*
  99. * Client data (each client gets its own)
  100. */
  101. struct lm83_data {
  102. struct i2c_client *client;
  103. const struct attribute_group *groups[3];
  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. s8 temp[9]; /* 0..3: input 1-4,
  109. 4..7: high limit 1-4,
  110. 8 : critical limit */
  111. u16 alarms; /* bitvector, combined */
  112. };
  113. static struct lm83_data *lm83_update_device(struct device *dev)
  114. {
  115. struct lm83_data *data = dev_get_drvdata(dev);
  116. struct i2c_client *client = data->client;
  117. mutex_lock(&data->update_lock);
  118. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  119. int nr;
  120. dev_dbg(&client->dev, "Updating lm83 data.\n");
  121. for (nr = 0; nr < 9; nr++) {
  122. data->temp[nr] =
  123. i2c_smbus_read_byte_data(client,
  124. LM83_REG_R_TEMP[nr]);
  125. }
  126. data->alarms =
  127. i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
  128. + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
  129. << 8);
  130. data->last_updated = jiffies;
  131. data->valid = 1;
  132. }
  133. mutex_unlock(&data->update_lock);
  134. return data;
  135. }
  136. /*
  137. * Sysfs stuff
  138. */
  139. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  140. char *buf)
  141. {
  142. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  143. struct lm83_data *data = lm83_update_device(dev);
  144. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  145. }
  146. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  147. const char *buf, size_t count)
  148. {
  149. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  150. struct lm83_data *data = dev_get_drvdata(dev);
  151. struct i2c_client *client = data->client;
  152. long val;
  153. int nr = attr->index;
  154. int err;
  155. err = kstrtol(buf, 10, &val);
  156. if (err < 0)
  157. return err;
  158. mutex_lock(&data->update_lock);
  159. data->temp[nr] = TEMP_TO_REG(val);
  160. i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
  161. data->temp[nr]);
  162. mutex_unlock(&data->update_lock);
  163. return count;
  164. }
  165. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  166. char *buf)
  167. {
  168. struct lm83_data *data = lm83_update_device(dev);
  169. return sprintf(buf, "%d\n", data->alarms);
  170. }
  171. static ssize_t show_alarm(struct device *dev, struct device_attribute
  172. *devattr, char *buf)
  173. {
  174. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  175. struct lm83_data *data = lm83_update_device(dev);
  176. int bitnr = attr->index;
  177. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  178. }
  179. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  180. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  181. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  182. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  183. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
  184. set_temp, 4);
  185. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
  186. set_temp, 5);
  187. static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
  188. set_temp, 6);
  189. static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
  190. set_temp, 7);
  191. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
  192. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
  193. static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
  194. set_temp, 8);
  195. static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
  196. /* Individual alarm files */
  197. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
  198. static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  199. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
  200. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  201. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  202. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
  203. static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
  204. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
  205. static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
  206. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
  207. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
  208. /* Raw alarm file for compatibility */
  209. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  210. static struct attribute *lm83_attributes[] = {
  211. &sensor_dev_attr_temp1_input.dev_attr.attr,
  212. &sensor_dev_attr_temp3_input.dev_attr.attr,
  213. &sensor_dev_attr_temp1_max.dev_attr.attr,
  214. &sensor_dev_attr_temp3_max.dev_attr.attr,
  215. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  216. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  217. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  218. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  219. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  220. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  221. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  222. &dev_attr_alarms.attr,
  223. NULL
  224. };
  225. static const struct attribute_group lm83_group = {
  226. .attrs = lm83_attributes,
  227. };
  228. static struct attribute *lm83_attributes_opt[] = {
  229. &sensor_dev_attr_temp2_input.dev_attr.attr,
  230. &sensor_dev_attr_temp4_input.dev_attr.attr,
  231. &sensor_dev_attr_temp2_max.dev_attr.attr,
  232. &sensor_dev_attr_temp4_max.dev_attr.attr,
  233. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  234. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  235. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  236. &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
  237. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  238. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  239. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  240. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  241. NULL
  242. };
  243. static const struct attribute_group lm83_group_opt = {
  244. .attrs = lm83_attributes_opt,
  245. };
  246. /*
  247. * Real code
  248. */
  249. /* Return 0 if detection is successful, -ENODEV otherwise */
  250. static int lm83_detect(struct i2c_client *new_client,
  251. struct i2c_board_info *info)
  252. {
  253. struct i2c_adapter *adapter = new_client->adapter;
  254. const char *name;
  255. u8 man_id, chip_id;
  256. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  257. return -ENODEV;
  258. /* Detection */
  259. if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) ||
  260. (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) ||
  261. (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) {
  262. dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
  263. new_client->addr);
  264. return -ENODEV;
  265. }
  266. /* Identification */
  267. man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID);
  268. if (man_id != 0x01) /* National Semiconductor */
  269. return -ENODEV;
  270. chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID);
  271. switch (chip_id) {
  272. case 0x03:
  273. name = "lm83";
  274. break;
  275. case 0x01:
  276. name = "lm82";
  277. break;
  278. default:
  279. /* identification failed */
  280. dev_info(&adapter->dev,
  281. "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
  282. man_id, chip_id);
  283. return -ENODEV;
  284. }
  285. strlcpy(info->type, name, I2C_NAME_SIZE);
  286. return 0;
  287. }
  288. static int lm83_probe(struct i2c_client *new_client,
  289. const struct i2c_device_id *id)
  290. {
  291. struct device *hwmon_dev;
  292. struct lm83_data *data;
  293. data = devm_kzalloc(&new_client->dev, sizeof(struct lm83_data),
  294. GFP_KERNEL);
  295. if (!data)
  296. return -ENOMEM;
  297. data->client = new_client;
  298. mutex_init(&data->update_lock);
  299. /*
  300. * Register sysfs hooks
  301. * The LM82 can only monitor one external diode which is
  302. * at the same register as the LM83 temp3 entry - so we
  303. * declare 1 and 3 common, and then 2 and 4 only for the LM83.
  304. */
  305. data->groups[0] = &lm83_group;
  306. if (id->driver_data == lm83)
  307. data->groups[1] = &lm83_group_opt;
  308. hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
  309. new_client->name,
  310. data, data->groups);
  311. return PTR_ERR_OR_ZERO(hwmon_dev);
  312. }
  313. /*
  314. * Driver data (common to all clients)
  315. */
  316. static const struct i2c_device_id lm83_id[] = {
  317. { "lm83", lm83 },
  318. { "lm82", lm82 },
  319. { }
  320. };
  321. MODULE_DEVICE_TABLE(i2c, lm83_id);
  322. static struct i2c_driver lm83_driver = {
  323. .class = I2C_CLASS_HWMON,
  324. .driver = {
  325. .name = "lm83",
  326. },
  327. .probe = lm83_probe,
  328. .id_table = lm83_id,
  329. .detect = lm83_detect,
  330. .address_list = normal_i2c,
  331. };
  332. module_i2c_driver(lm83_driver);
  333. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  334. MODULE_DESCRIPTION("LM83 driver");
  335. MODULE_LICENSE("GPL");