lm83.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003 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/config.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/i2c.h>
  35. #include <linux/i2c-sensor.h>
  36. /*
  37. * Addresses to scan
  38. * Address is selected using 2 three-level pins, resulting in 9 possible
  39. * addresses.
  40. */
  41. static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
  42. 0x29, 0x2a, 0x2b,
  43. 0x4c, 0x4d, 0x4e,
  44. I2C_CLIENT_END };
  45. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  46. /*
  47. * Insmod parameters
  48. */
  49. SENSORS_INSMOD_1(lm83);
  50. /*
  51. * The LM83 registers
  52. * Manufacturer ID is 0x01 for National Semiconductor.
  53. */
  54. #define LM83_REG_R_MAN_ID 0xFE
  55. #define LM83_REG_R_CHIP_ID 0xFF
  56. #define LM83_REG_R_CONFIG 0x03
  57. #define LM83_REG_W_CONFIG 0x09
  58. #define LM83_REG_R_STATUS1 0x02
  59. #define LM83_REG_R_STATUS2 0x35
  60. #define LM83_REG_R_LOCAL_TEMP 0x00
  61. #define LM83_REG_R_LOCAL_HIGH 0x05
  62. #define LM83_REG_W_LOCAL_HIGH 0x0B
  63. #define LM83_REG_R_REMOTE1_TEMP 0x30
  64. #define LM83_REG_R_REMOTE1_HIGH 0x38
  65. #define LM83_REG_W_REMOTE1_HIGH 0x50
  66. #define LM83_REG_R_REMOTE2_TEMP 0x01
  67. #define LM83_REG_R_REMOTE2_HIGH 0x07
  68. #define LM83_REG_W_REMOTE2_HIGH 0x0D
  69. #define LM83_REG_R_REMOTE3_TEMP 0x31
  70. #define LM83_REG_R_REMOTE3_HIGH 0x3A
  71. #define LM83_REG_W_REMOTE3_HIGH 0x52
  72. #define LM83_REG_R_TCRIT 0x42
  73. #define LM83_REG_W_TCRIT 0x5A
  74. /*
  75. * Conversions and various macros
  76. * The LM83 uses signed 8-bit values with LSB = 1 degree Celcius.
  77. */
  78. #define TEMP_FROM_REG(val) ((val) * 1000)
  79. #define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
  80. (val) >= 127000 ? 127 : \
  81. (val) < 0 ? ((val) - 500) / 1000 : \
  82. ((val) + 500) / 1000)
  83. static const u8 LM83_REG_R_TEMP[] = {
  84. LM83_REG_R_LOCAL_TEMP,
  85. LM83_REG_R_REMOTE1_TEMP,
  86. LM83_REG_R_REMOTE2_TEMP,
  87. LM83_REG_R_REMOTE3_TEMP
  88. };
  89. static const u8 LM83_REG_R_HIGH[] = {
  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. };
  95. static const u8 LM83_REG_W_HIGH[] = {
  96. LM83_REG_W_LOCAL_HIGH,
  97. LM83_REG_W_REMOTE1_HIGH,
  98. LM83_REG_W_REMOTE2_HIGH,
  99. LM83_REG_W_REMOTE3_HIGH
  100. };
  101. /*
  102. * Functions declaration
  103. */
  104. static int lm83_attach_adapter(struct i2c_adapter *adapter);
  105. static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
  106. static int lm83_detach_client(struct i2c_client *client);
  107. static struct lm83_data *lm83_update_device(struct device *dev);
  108. /*
  109. * Driver data (common to all clients)
  110. */
  111. static struct i2c_driver lm83_driver = {
  112. .owner = THIS_MODULE,
  113. .name = "lm83",
  114. .id = I2C_DRIVERID_LM83,
  115. .flags = I2C_DF_NOTIFY,
  116. .attach_adapter = lm83_attach_adapter,
  117. .detach_client = lm83_detach_client,
  118. };
  119. /*
  120. * Client data (each client gets its own)
  121. */
  122. struct lm83_data {
  123. struct i2c_client client;
  124. struct semaphore update_lock;
  125. char valid; /* zero until following fields are valid */
  126. unsigned long last_updated; /* in jiffies */
  127. /* registers values */
  128. s8 temp_input[4];
  129. s8 temp_high[4];
  130. s8 temp_crit;
  131. u16 alarms; /* bitvector, combined */
  132. };
  133. /*
  134. * Sysfs stuff
  135. */
  136. #define show_temp(suffix, value) \
  137. static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  138. { \
  139. struct lm83_data *data = lm83_update_device(dev); \
  140. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
  141. }
  142. show_temp(input1, temp_input[0]);
  143. show_temp(input2, temp_input[1]);
  144. show_temp(input3, temp_input[2]);
  145. show_temp(input4, temp_input[3]);
  146. show_temp(high1, temp_high[0]);
  147. show_temp(high2, temp_high[1]);
  148. show_temp(high3, temp_high[2]);
  149. show_temp(high4, temp_high[3]);
  150. show_temp(crit, temp_crit);
  151. #define set_temp(suffix, value, reg) \
  152. static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  153. size_t count) \
  154. { \
  155. struct i2c_client *client = to_i2c_client(dev); \
  156. struct lm83_data *data = i2c_get_clientdata(client); \
  157. long val = simple_strtol(buf, NULL, 10); \
  158. \
  159. down(&data->update_lock); \
  160. data->value = TEMP_TO_REG(val); \
  161. i2c_smbus_write_byte_data(client, reg, data->value); \
  162. up(&data->update_lock); \
  163. return count; \
  164. }
  165. set_temp(high1, temp_high[0], LM83_REG_W_LOCAL_HIGH);
  166. set_temp(high2, temp_high[1], LM83_REG_W_REMOTE1_HIGH);
  167. set_temp(high3, temp_high[2], LM83_REG_W_REMOTE2_HIGH);
  168. set_temp(high4, temp_high[3], LM83_REG_W_REMOTE3_HIGH);
  169. set_temp(crit, temp_crit, LM83_REG_W_TCRIT);
  170. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  171. {
  172. struct lm83_data *data = lm83_update_device(dev);
  173. return sprintf(buf, "%d\n", data->alarms);
  174. }
  175. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  176. static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
  177. static DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input3, NULL);
  178. static DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input4, NULL);
  179. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_high1,
  180. set_temp_high1);
  181. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
  182. set_temp_high2);
  183. static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_high3,
  184. set_temp_high3);
  185. static DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_high4,
  186. set_temp_high4);
  187. static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL);
  188. static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL);
  189. static DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp_crit,
  190. set_temp_crit);
  191. static DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL);
  192. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  193. /*
  194. * Real code
  195. */
  196. static int lm83_attach_adapter(struct i2c_adapter *adapter)
  197. {
  198. if (!(adapter->class & I2C_CLASS_HWMON))
  199. return 0;
  200. return i2c_detect(adapter, &addr_data, lm83_detect);
  201. }
  202. /*
  203. * The following function does more than just detection. If detection
  204. * succeeds, it also registers the new chip.
  205. */
  206. static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
  207. {
  208. struct i2c_client *new_client;
  209. struct lm83_data *data;
  210. int err = 0;
  211. const char *name = "";
  212. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  213. goto exit;
  214. if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
  215. err = -ENOMEM;
  216. goto exit;
  217. }
  218. memset(data, 0, sizeof(struct lm83_data));
  219. /* The common I2C client data is placed right after the
  220. * LM83-specific data. */
  221. new_client = &data->client;
  222. i2c_set_clientdata(new_client, data);
  223. new_client->addr = address;
  224. new_client->adapter = adapter;
  225. new_client->driver = &lm83_driver;
  226. new_client->flags = 0;
  227. /* Now we do the detection and identification. A negative kind
  228. * means that the driver was loaded with no force parameter
  229. * (default), so we must both detect and identify the chip
  230. * (actually there is only one possible kind of chip for now, LM83).
  231. * A zero kind means that the driver was loaded with the force
  232. * parameter, the detection step shall be skipped. A positive kind
  233. * means that the driver was loaded with the force parameter and a
  234. * given kind of chip is requested, so both the detection and the
  235. * identification steps are skipped. */
  236. /* Default to an LM83 if forced */
  237. if (kind == 0)
  238. kind = lm83;
  239. if (kind < 0) { /* detection */
  240. if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
  241. & 0xA8) != 0x00) ||
  242. ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
  243. & 0x48) != 0x00) ||
  244. ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
  245. & 0x41) != 0x00)) {
  246. dev_dbg(&adapter->dev,
  247. "LM83 detection failed at 0x%02x.\n", address);
  248. goto exit_free;
  249. }
  250. }
  251. if (kind <= 0) { /* identification */
  252. u8 man_id, chip_id;
  253. man_id = i2c_smbus_read_byte_data(new_client,
  254. LM83_REG_R_MAN_ID);
  255. chip_id = i2c_smbus_read_byte_data(new_client,
  256. LM83_REG_R_CHIP_ID);
  257. if (man_id == 0x01) { /* National Semiconductor */
  258. if (chip_id == 0x03) {
  259. kind = lm83;
  260. }
  261. }
  262. if (kind <= 0) { /* identification failed */
  263. dev_info(&adapter->dev,
  264. "Unsupported chip (man_id=0x%02X, "
  265. "chip_id=0x%02X).\n", man_id, chip_id);
  266. goto exit_free;
  267. }
  268. }
  269. if (kind == lm83) {
  270. name = "lm83";
  271. }
  272. /* We can fill in the remaining client fields */
  273. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  274. data->valid = 0;
  275. init_MUTEX(&data->update_lock);
  276. /* Tell the I2C layer a new client has arrived */
  277. if ((err = i2c_attach_client(new_client)))
  278. goto exit_free;
  279. /*
  280. * Initialize the LM83 chip
  281. * (Nothing to do for this one.)
  282. */
  283. /* Register sysfs hooks */
  284. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  285. device_create_file(&new_client->dev, &dev_attr_temp2_input);
  286. device_create_file(&new_client->dev, &dev_attr_temp3_input);
  287. device_create_file(&new_client->dev, &dev_attr_temp4_input);
  288. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  289. device_create_file(&new_client->dev, &dev_attr_temp2_max);
  290. device_create_file(&new_client->dev, &dev_attr_temp3_max);
  291. device_create_file(&new_client->dev, &dev_attr_temp4_max);
  292. device_create_file(&new_client->dev, &dev_attr_temp1_crit);
  293. device_create_file(&new_client->dev, &dev_attr_temp2_crit);
  294. device_create_file(&new_client->dev, &dev_attr_temp3_crit);
  295. device_create_file(&new_client->dev, &dev_attr_temp4_crit);
  296. device_create_file(&new_client->dev, &dev_attr_alarms);
  297. return 0;
  298. exit_free:
  299. kfree(data);
  300. exit:
  301. return err;
  302. }
  303. static int lm83_detach_client(struct i2c_client *client)
  304. {
  305. int err;
  306. if ((err = i2c_detach_client(client))) {
  307. dev_err(&client->dev,
  308. "Client deregistration failed, client not detached.\n");
  309. return err;
  310. }
  311. kfree(i2c_get_clientdata(client));
  312. return 0;
  313. }
  314. static struct lm83_data *lm83_update_device(struct device *dev)
  315. {
  316. struct i2c_client *client = to_i2c_client(dev);
  317. struct lm83_data *data = i2c_get_clientdata(client);
  318. down(&data->update_lock);
  319. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  320. int nr;
  321. dev_dbg(&client->dev, "Updating lm83 data.\n");
  322. for (nr = 0; nr < 4 ; nr++) {
  323. data->temp_input[nr] =
  324. i2c_smbus_read_byte_data(client,
  325. LM83_REG_R_TEMP[nr]);
  326. data->temp_high[nr] =
  327. i2c_smbus_read_byte_data(client,
  328. LM83_REG_R_HIGH[nr]);
  329. }
  330. data->temp_crit =
  331. i2c_smbus_read_byte_data(client, LM83_REG_R_TCRIT);
  332. data->alarms =
  333. i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
  334. + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
  335. << 8);
  336. data->last_updated = jiffies;
  337. data->valid = 1;
  338. }
  339. up(&data->update_lock);
  340. return data;
  341. }
  342. static int __init sensors_lm83_init(void)
  343. {
  344. return i2c_add_driver(&lm83_driver);
  345. }
  346. static void __exit sensors_lm83_exit(void)
  347. {
  348. i2c_del_driver(&lm83_driver);
  349. }
  350. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  351. MODULE_DESCRIPTION("LM83 driver");
  352. MODULE_LICENSE("GPL");
  353. module_init(sensors_lm83_init);
  354. module_exit(sensors_lm83_exit);