lm83.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2006 Jean Delvare <khali@linux-fr.org>
  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. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/i2c.h>
  38. #include <linux/hwmon-sysfs.h>
  39. #include <linux/hwmon.h>
  40. #include <linux/err.h>
  41. #include <linux/mutex.h>
  42. #include <linux/sysfs.h>
  43. /*
  44. * Addresses to scan
  45. * Address is selected using 2 three-level pins, resulting in 9 possible
  46. * addresses.
  47. */
  48. static const unsigned short normal_i2c[] = {
  49. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  50. /*
  51. * Insmod parameters
  52. */
  53. I2C_CLIENT_INSMOD_2(lm83, lm82);
  54. /*
  55. * The LM83 registers
  56. * Manufacturer ID is 0x01 for National Semiconductor.
  57. */
  58. #define LM83_REG_R_MAN_ID 0xFE
  59. #define LM83_REG_R_CHIP_ID 0xFF
  60. #define LM83_REG_R_CONFIG 0x03
  61. #define LM83_REG_W_CONFIG 0x09
  62. #define LM83_REG_R_STATUS1 0x02
  63. #define LM83_REG_R_STATUS2 0x35
  64. #define LM83_REG_R_LOCAL_TEMP 0x00
  65. #define LM83_REG_R_LOCAL_HIGH 0x05
  66. #define LM83_REG_W_LOCAL_HIGH 0x0B
  67. #define LM83_REG_R_REMOTE1_TEMP 0x30
  68. #define LM83_REG_R_REMOTE1_HIGH 0x38
  69. #define LM83_REG_W_REMOTE1_HIGH 0x50
  70. #define LM83_REG_R_REMOTE2_TEMP 0x01
  71. #define LM83_REG_R_REMOTE2_HIGH 0x07
  72. #define LM83_REG_W_REMOTE2_HIGH 0x0D
  73. #define LM83_REG_R_REMOTE3_TEMP 0x31
  74. #define LM83_REG_R_REMOTE3_HIGH 0x3A
  75. #define LM83_REG_W_REMOTE3_HIGH 0x52
  76. #define LM83_REG_R_TCRIT 0x42
  77. #define LM83_REG_W_TCRIT 0x5A
  78. /*
  79. * Conversions and various macros
  80. * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
  81. */
  82. #define TEMP_FROM_REG(val) ((val) * 1000)
  83. #define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
  84. (val) >= 127000 ? 127 : \
  85. (val) < 0 ? ((val) - 500) / 1000 : \
  86. ((val) + 500) / 1000)
  87. static const u8 LM83_REG_R_TEMP[] = {
  88. LM83_REG_R_LOCAL_TEMP,
  89. LM83_REG_R_REMOTE1_TEMP,
  90. LM83_REG_R_REMOTE2_TEMP,
  91. LM83_REG_R_REMOTE3_TEMP,
  92. LM83_REG_R_LOCAL_HIGH,
  93. LM83_REG_R_REMOTE1_HIGH,
  94. LM83_REG_R_REMOTE2_HIGH,
  95. LM83_REG_R_REMOTE3_HIGH,
  96. LM83_REG_R_TCRIT,
  97. };
  98. static const u8 LM83_REG_W_HIGH[] = {
  99. LM83_REG_W_LOCAL_HIGH,
  100. LM83_REG_W_REMOTE1_HIGH,
  101. LM83_REG_W_REMOTE2_HIGH,
  102. LM83_REG_W_REMOTE3_HIGH,
  103. LM83_REG_W_TCRIT,
  104. };
  105. /*
  106. * Functions declaration
  107. */
  108. static int lm83_attach_adapter(struct i2c_adapter *adapter);
  109. static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
  110. static int lm83_detach_client(struct i2c_client *client);
  111. static struct lm83_data *lm83_update_device(struct device *dev);
  112. /*
  113. * Driver data (common to all clients)
  114. */
  115. static struct i2c_driver lm83_driver = {
  116. .driver = {
  117. .name = "lm83",
  118. },
  119. .attach_adapter = lm83_attach_adapter,
  120. .detach_client = lm83_detach_client,
  121. };
  122. /*
  123. * Client data (each client gets its own)
  124. */
  125. struct lm83_data {
  126. struct i2c_client client;
  127. struct device *hwmon_dev;
  128. struct mutex update_lock;
  129. char valid; /* zero until following fields are valid */
  130. unsigned long last_updated; /* in jiffies */
  131. /* registers values */
  132. s8 temp[9]; /* 0..3: input 1-4,
  133. 4..7: high limit 1-4,
  134. 8 : critical limit */
  135. u16 alarms; /* bitvector, combined */
  136. };
  137. /*
  138. * Sysfs stuff
  139. */
  140. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  141. char *buf)
  142. {
  143. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  144. struct lm83_data *data = lm83_update_device(dev);
  145. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  146. }
  147. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  148. const char *buf, size_t count)
  149. {
  150. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  151. struct i2c_client *client = to_i2c_client(dev);
  152. struct lm83_data *data = i2c_get_clientdata(client);
  153. long val = simple_strtol(buf, NULL, 10);
  154. int nr = attr->index;
  155. mutex_lock(&data->update_lock);
  156. data->temp[nr] = TEMP_TO_REG(val);
  157. i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
  158. data->temp[nr]);
  159. mutex_unlock(&data->update_lock);
  160. return count;
  161. }
  162. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  163. char *buf)
  164. {
  165. struct lm83_data *data = lm83_update_device(dev);
  166. return sprintf(buf, "%d\n", data->alarms);
  167. }
  168. static ssize_t show_alarm(struct device *dev, struct device_attribute
  169. *devattr, char *buf)
  170. {
  171. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  172. struct lm83_data *data = lm83_update_device(dev);
  173. int bitnr = attr->index;
  174. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  175. }
  176. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  177. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  178. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  179. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  180. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
  181. set_temp, 4);
  182. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
  183. set_temp, 5);
  184. static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
  185. set_temp, 6);
  186. static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
  187. set_temp, 7);
  188. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
  189. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
  190. static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
  191. set_temp, 8);
  192. static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
  193. /* Individual alarm files */
  194. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
  195. static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  196. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
  197. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  198. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  199. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
  200. static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
  201. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
  202. static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
  203. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
  204. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
  205. /* Raw alarm file for compatibility */
  206. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  207. static struct attribute *lm83_attributes[] = {
  208. &sensor_dev_attr_temp1_input.dev_attr.attr,
  209. &sensor_dev_attr_temp3_input.dev_attr.attr,
  210. &sensor_dev_attr_temp1_max.dev_attr.attr,
  211. &sensor_dev_attr_temp3_max.dev_attr.attr,
  212. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  213. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  214. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  215. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  216. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  217. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  218. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  219. &dev_attr_alarms.attr,
  220. NULL
  221. };
  222. static const struct attribute_group lm83_group = {
  223. .attrs = lm83_attributes,
  224. };
  225. static struct attribute *lm83_attributes_opt[] = {
  226. &sensor_dev_attr_temp2_input.dev_attr.attr,
  227. &sensor_dev_attr_temp4_input.dev_attr.attr,
  228. &sensor_dev_attr_temp2_max.dev_attr.attr,
  229. &sensor_dev_attr_temp4_max.dev_attr.attr,
  230. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  231. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  232. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  233. &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
  234. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  235. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  236. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  237. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  238. NULL
  239. };
  240. static const struct attribute_group lm83_group_opt = {
  241. .attrs = lm83_attributes_opt,
  242. };
  243. /*
  244. * Real code
  245. */
  246. static int lm83_attach_adapter(struct i2c_adapter *adapter)
  247. {
  248. if (!(adapter->class & I2C_CLASS_HWMON))
  249. return 0;
  250. return i2c_probe(adapter, &addr_data, lm83_detect);
  251. }
  252. /*
  253. * The following function does more than just detection. If detection
  254. * succeeds, it also registers the new chip.
  255. */
  256. static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
  257. {
  258. struct i2c_client *new_client;
  259. struct lm83_data *data;
  260. int err = 0;
  261. const char *name = "";
  262. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  263. goto exit;
  264. if (!(data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
  265. err = -ENOMEM;
  266. goto exit;
  267. }
  268. /* The common I2C client data is placed right after the
  269. * LM83-specific data. */
  270. new_client = &data->client;
  271. i2c_set_clientdata(new_client, data);
  272. new_client->addr = address;
  273. new_client->adapter = adapter;
  274. new_client->driver = &lm83_driver;
  275. new_client->flags = 0;
  276. /* Now we do the detection and identification. A negative kind
  277. * means that the driver was loaded with no force parameter
  278. * (default), so we must both detect and identify the chip
  279. * (actually there is only one possible kind of chip for now, LM83).
  280. * A zero kind means that the driver was loaded with the force
  281. * parameter, the detection step shall be skipped. A positive kind
  282. * means that the driver was loaded with the force parameter and a
  283. * given kind of chip is requested, so both the detection and the
  284. * identification steps are skipped. */
  285. /* Default to an LM83 if forced */
  286. if (kind == 0)
  287. kind = lm83;
  288. if (kind < 0) { /* detection */
  289. if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
  290. & 0xA8) != 0x00) ||
  291. ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
  292. & 0x48) != 0x00) ||
  293. ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
  294. & 0x41) != 0x00)) {
  295. dev_dbg(&adapter->dev,
  296. "LM83 detection failed at 0x%02x.\n", address);
  297. goto exit_free;
  298. }
  299. }
  300. if (kind <= 0) { /* identification */
  301. u8 man_id, chip_id;
  302. man_id = i2c_smbus_read_byte_data(new_client,
  303. LM83_REG_R_MAN_ID);
  304. chip_id = i2c_smbus_read_byte_data(new_client,
  305. LM83_REG_R_CHIP_ID);
  306. if (man_id == 0x01) { /* National Semiconductor */
  307. if (chip_id == 0x03) {
  308. kind = lm83;
  309. } else
  310. if (chip_id == 0x01) {
  311. kind = lm82;
  312. }
  313. }
  314. if (kind <= 0) { /* identification failed */
  315. dev_info(&adapter->dev,
  316. "Unsupported chip (man_id=0x%02X, "
  317. "chip_id=0x%02X).\n", man_id, chip_id);
  318. goto exit_free;
  319. }
  320. }
  321. if (kind == lm83) {
  322. name = "lm83";
  323. } else
  324. if (kind == lm82) {
  325. name = "lm82";
  326. }
  327. /* We can fill in the remaining client fields */
  328. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  329. data->valid = 0;
  330. mutex_init(&data->update_lock);
  331. /* Tell the I2C layer a new client has arrived */
  332. if ((err = i2c_attach_client(new_client)))
  333. goto exit_free;
  334. /*
  335. * Register sysfs hooks
  336. * The LM82 can only monitor one external diode which is
  337. * at the same register as the LM83 temp3 entry - so we
  338. * declare 1 and 3 common, and then 2 and 4 only for the LM83.
  339. */
  340. if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
  341. goto exit_detach;
  342. if (kind == lm83) {
  343. if ((err = sysfs_create_group(&new_client->dev.kobj,
  344. &lm83_group_opt)))
  345. goto exit_remove_files;
  346. }
  347. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  348. if (IS_ERR(data->hwmon_dev)) {
  349. err = PTR_ERR(data->hwmon_dev);
  350. goto exit_remove_files;
  351. }
  352. return 0;
  353. exit_remove_files:
  354. sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
  355. sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
  356. exit_detach:
  357. i2c_detach_client(new_client);
  358. exit_free:
  359. kfree(data);
  360. exit:
  361. return err;
  362. }
  363. static int lm83_detach_client(struct i2c_client *client)
  364. {
  365. struct lm83_data *data = i2c_get_clientdata(client);
  366. int err;
  367. hwmon_device_unregister(data->hwmon_dev);
  368. sysfs_remove_group(&client->dev.kobj, &lm83_group);
  369. sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
  370. if ((err = i2c_detach_client(client)))
  371. return err;
  372. kfree(data);
  373. return 0;
  374. }
  375. static struct lm83_data *lm83_update_device(struct device *dev)
  376. {
  377. struct i2c_client *client = to_i2c_client(dev);
  378. struct lm83_data *data = i2c_get_clientdata(client);
  379. mutex_lock(&data->update_lock);
  380. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  381. int nr;
  382. dev_dbg(&client->dev, "Updating lm83 data.\n");
  383. for (nr = 0; nr < 9; nr++) {
  384. data->temp[nr] =
  385. i2c_smbus_read_byte_data(client,
  386. LM83_REG_R_TEMP[nr]);
  387. }
  388. data->alarms =
  389. i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
  390. + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
  391. << 8);
  392. data->last_updated = jiffies;
  393. data->valid = 1;
  394. }
  395. mutex_unlock(&data->update_lock);
  396. return data;
  397. }
  398. static int __init sensors_lm83_init(void)
  399. {
  400. return i2c_add_driver(&lm83_driver);
  401. }
  402. static void __exit sensors_lm83_exit(void)
  403. {
  404. i2c_del_driver(&lm83_driver);
  405. }
  406. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  407. MODULE_DESCRIPTION("LM83 driver");
  408. MODULE_LICENSE("GPL");
  409. module_init(sensors_lm83_init);
  410. module_exit(sensors_lm83_exit);