lm83.c 14 KB

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