lm83.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2005 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. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  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/i2c-sensor.h>
  35. #include <linux/hwmon-sysfs.h>
  36. #include <linux/hwmon.h>
  37. #include <linux/err.h>
  38. /*
  39. * Addresses to scan
  40. * Address is selected using 2 three-level pins, resulting in 9 possible
  41. * addresses.
  42. */
  43. static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
  44. 0x29, 0x2a, 0x2b,
  45. 0x4c, 0x4d, 0x4e,
  46. I2C_CLIENT_END };
  47. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  48. /*
  49. * Insmod parameters
  50. */
  51. SENSORS_INSMOD_1(lm83);
  52. /*
  53. * The LM83 registers
  54. * Manufacturer ID is 0x01 for National Semiconductor.
  55. */
  56. #define LM83_REG_R_MAN_ID 0xFE
  57. #define LM83_REG_R_CHIP_ID 0xFF
  58. #define LM83_REG_R_CONFIG 0x03
  59. #define LM83_REG_W_CONFIG 0x09
  60. #define LM83_REG_R_STATUS1 0x02
  61. #define LM83_REG_R_STATUS2 0x35
  62. #define LM83_REG_R_LOCAL_TEMP 0x00
  63. #define LM83_REG_R_LOCAL_HIGH 0x05
  64. #define LM83_REG_W_LOCAL_HIGH 0x0B
  65. #define LM83_REG_R_REMOTE1_TEMP 0x30
  66. #define LM83_REG_R_REMOTE1_HIGH 0x38
  67. #define LM83_REG_W_REMOTE1_HIGH 0x50
  68. #define LM83_REG_R_REMOTE2_TEMP 0x01
  69. #define LM83_REG_R_REMOTE2_HIGH 0x07
  70. #define LM83_REG_W_REMOTE2_HIGH 0x0D
  71. #define LM83_REG_R_REMOTE3_TEMP 0x31
  72. #define LM83_REG_R_REMOTE3_HIGH 0x3A
  73. #define LM83_REG_W_REMOTE3_HIGH 0x52
  74. #define LM83_REG_R_TCRIT 0x42
  75. #define LM83_REG_W_TCRIT 0x5A
  76. /*
  77. * Conversions and various macros
  78. * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
  79. */
  80. #define TEMP_FROM_REG(val) ((val) * 1000)
  81. #define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
  82. (val) >= 127000 ? 127 : \
  83. (val) < 0 ? ((val) - 500) / 1000 : \
  84. ((val) + 500) / 1000)
  85. static const u8 LM83_REG_R_TEMP[] = {
  86. LM83_REG_R_LOCAL_TEMP,
  87. LM83_REG_R_REMOTE1_TEMP,
  88. LM83_REG_R_REMOTE2_TEMP,
  89. LM83_REG_R_REMOTE3_TEMP,
  90. LM83_REG_R_LOCAL_HIGH,
  91. LM83_REG_R_REMOTE1_HIGH,
  92. LM83_REG_R_REMOTE2_HIGH,
  93. LM83_REG_R_REMOTE3_HIGH,
  94. LM83_REG_R_TCRIT,
  95. };
  96. static const u8 LM83_REG_W_HIGH[] = {
  97. LM83_REG_W_LOCAL_HIGH,
  98. LM83_REG_W_REMOTE1_HIGH,
  99. LM83_REG_W_REMOTE2_HIGH,
  100. LM83_REG_W_REMOTE3_HIGH,
  101. LM83_REG_W_TCRIT,
  102. };
  103. /*
  104. * Functions declaration
  105. */
  106. static int lm83_attach_adapter(struct i2c_adapter *adapter);
  107. static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
  108. static int lm83_detach_client(struct i2c_client *client);
  109. static struct lm83_data *lm83_update_device(struct device *dev);
  110. /*
  111. * Driver data (common to all clients)
  112. */
  113. static struct i2c_driver lm83_driver = {
  114. .owner = THIS_MODULE,
  115. .name = "lm83",
  116. .id = I2C_DRIVERID_LM83,
  117. .flags = I2C_DF_NOTIFY,
  118. .attach_adapter = lm83_attach_adapter,
  119. .detach_client = lm83_detach_client,
  120. };
  121. /*
  122. * Client data (each client gets its own)
  123. */
  124. struct lm83_data {
  125. struct i2c_client client;
  126. struct class_device *class_dev;
  127. struct semaphore update_lock;
  128. char valid; /* zero until following fields are valid */
  129. unsigned long last_updated; /* in jiffies */
  130. /* registers values */
  131. s8 temp[9]; /* 0..3: input 1-4,
  132. 4..7: high limit 1-4,
  133. 8 : critical limit */
  134. u16 alarms; /* bitvector, combined */
  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 i2c_client *client = to_i2c_client(dev);
  151. struct lm83_data *data = i2c_get_clientdata(client);
  152. long val = simple_strtol(buf, NULL, 10);
  153. int nr = attr->index;
  154. down(&data->update_lock);
  155. data->temp[nr] = TEMP_TO_REG(val);
  156. i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
  157. data->temp[nr]);
  158. up(&data->update_lock);
  159. return count;
  160. }
  161. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  162. char *buf)
  163. {
  164. struct lm83_data *data = lm83_update_device(dev);
  165. return sprintf(buf, "%d\n", data->alarms);
  166. }
  167. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  168. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  169. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  170. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  171. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
  172. set_temp, 4);
  173. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
  174. set_temp, 5);
  175. static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
  176. set_temp, 6);
  177. static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
  178. set_temp, 7);
  179. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
  180. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
  181. static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
  182. set_temp, 8);
  183. static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
  184. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  185. /*
  186. * Real code
  187. */
  188. static int lm83_attach_adapter(struct i2c_adapter *adapter)
  189. {
  190. if (!(adapter->class & I2C_CLASS_HWMON))
  191. return 0;
  192. return i2c_detect(adapter, &addr_data, lm83_detect);
  193. }
  194. /*
  195. * The following function does more than just detection. If detection
  196. * succeeds, it also registers the new chip.
  197. */
  198. static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
  199. {
  200. struct i2c_client *new_client;
  201. struct lm83_data *data;
  202. int err = 0;
  203. const char *name = "";
  204. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  205. goto exit;
  206. if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
  207. err = -ENOMEM;
  208. goto exit;
  209. }
  210. memset(data, 0, sizeof(struct lm83_data));
  211. /* The common I2C client data is placed right after the
  212. * LM83-specific data. */
  213. new_client = &data->client;
  214. i2c_set_clientdata(new_client, data);
  215. new_client->addr = address;
  216. new_client->adapter = adapter;
  217. new_client->driver = &lm83_driver;
  218. new_client->flags = 0;
  219. /* Now we do the detection and identification. A negative kind
  220. * means that the driver was loaded with no force parameter
  221. * (default), so we must both detect and identify the chip
  222. * (actually there is only one possible kind of chip for now, LM83).
  223. * A zero kind means that the driver was loaded with the force
  224. * parameter, the detection step shall be skipped. A positive kind
  225. * means that the driver was loaded with the force parameter and a
  226. * given kind of chip is requested, so both the detection and the
  227. * identification steps are skipped. */
  228. /* Default to an LM83 if forced */
  229. if (kind == 0)
  230. kind = lm83;
  231. if (kind < 0) { /* detection */
  232. if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
  233. & 0xA8) != 0x00) ||
  234. ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
  235. & 0x48) != 0x00) ||
  236. ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
  237. & 0x41) != 0x00)) {
  238. dev_dbg(&adapter->dev,
  239. "LM83 detection failed at 0x%02x.\n", address);
  240. goto exit_free;
  241. }
  242. }
  243. if (kind <= 0) { /* identification */
  244. u8 man_id, chip_id;
  245. man_id = i2c_smbus_read_byte_data(new_client,
  246. LM83_REG_R_MAN_ID);
  247. chip_id = i2c_smbus_read_byte_data(new_client,
  248. LM83_REG_R_CHIP_ID);
  249. if (man_id == 0x01) { /* National Semiconductor */
  250. if (chip_id == 0x03) {
  251. kind = lm83;
  252. }
  253. }
  254. if (kind <= 0) { /* identification failed */
  255. dev_info(&adapter->dev,
  256. "Unsupported chip (man_id=0x%02X, "
  257. "chip_id=0x%02X).\n", man_id, chip_id);
  258. goto exit_free;
  259. }
  260. }
  261. if (kind == lm83) {
  262. name = "lm83";
  263. }
  264. /* We can fill in the remaining client fields */
  265. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  266. data->valid = 0;
  267. init_MUTEX(&data->update_lock);
  268. /* Tell the I2C layer a new client has arrived */
  269. if ((err = i2c_attach_client(new_client)))
  270. goto exit_free;
  271. /*
  272. * Initialize the LM83 chip
  273. * (Nothing to do for this one.)
  274. */
  275. /* Register sysfs hooks */
  276. data->class_dev = hwmon_device_register(&new_client->dev);
  277. if (IS_ERR(data->class_dev)) {
  278. err = PTR_ERR(data->class_dev);
  279. goto exit_detach;
  280. }
  281. device_create_file(&new_client->dev,
  282. &sensor_dev_attr_temp1_input.dev_attr);
  283. device_create_file(&new_client->dev,
  284. &sensor_dev_attr_temp2_input.dev_attr);
  285. device_create_file(&new_client->dev,
  286. &sensor_dev_attr_temp3_input.dev_attr);
  287. device_create_file(&new_client->dev,
  288. &sensor_dev_attr_temp4_input.dev_attr);
  289. device_create_file(&new_client->dev,
  290. &sensor_dev_attr_temp1_max.dev_attr);
  291. device_create_file(&new_client->dev,
  292. &sensor_dev_attr_temp2_max.dev_attr);
  293. device_create_file(&new_client->dev,
  294. &sensor_dev_attr_temp3_max.dev_attr);
  295. device_create_file(&new_client->dev,
  296. &sensor_dev_attr_temp4_max.dev_attr);
  297. device_create_file(&new_client->dev,
  298. &sensor_dev_attr_temp1_crit.dev_attr);
  299. device_create_file(&new_client->dev,
  300. &sensor_dev_attr_temp2_crit.dev_attr);
  301. device_create_file(&new_client->dev,
  302. &sensor_dev_attr_temp3_crit.dev_attr);
  303. device_create_file(&new_client->dev,
  304. &sensor_dev_attr_temp4_crit.dev_attr);
  305. device_create_file(&new_client->dev, &dev_attr_alarms);
  306. return 0;
  307. exit_detach:
  308. i2c_detach_client(new_client);
  309. exit_free:
  310. kfree(data);
  311. exit:
  312. return err;
  313. }
  314. static int lm83_detach_client(struct i2c_client *client)
  315. {
  316. struct lm83_data *data = i2c_get_clientdata(client);
  317. int err;
  318. hwmon_device_unregister(data->class_dev);
  319. if ((err = i2c_detach_client(client))) {
  320. dev_err(&client->dev,
  321. "Client deregistration failed, client not detached.\n");
  322. return err;
  323. }
  324. kfree(data);
  325. return 0;
  326. }
  327. static struct lm83_data *lm83_update_device(struct device *dev)
  328. {
  329. struct i2c_client *client = to_i2c_client(dev);
  330. struct lm83_data *data = i2c_get_clientdata(client);
  331. down(&data->update_lock);
  332. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  333. int nr;
  334. dev_dbg(&client->dev, "Updating lm83 data.\n");
  335. for (nr = 0; nr < 9; nr++) {
  336. data->temp[nr] =
  337. i2c_smbus_read_byte_data(client,
  338. LM83_REG_R_TEMP[nr]);
  339. }
  340. data->alarms =
  341. i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
  342. + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
  343. << 8);
  344. data->last_updated = jiffies;
  345. data->valid = 1;
  346. }
  347. up(&data->update_lock);
  348. return data;
  349. }
  350. static int __init sensors_lm83_init(void)
  351. {
  352. return i2c_add_driver(&lm83_driver);
  353. }
  354. static void __exit sensors_lm83_exit(void)
  355. {
  356. i2c_del_driver(&lm83_driver);
  357. }
  358. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  359. MODULE_DESCRIPTION("LM83 driver");
  360. MODULE_LICENSE("GPL");
  361. module_init(sensors_lm83_init);
  362. module_exit(sensors_lm83_exit);