smsc47b397.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. smsc47b397.c - Part of lm_sensors, Linux kernel modules
  3. for hardware monitoring
  4. Supports the SMSC LPC47B397-NC Super-I/O chip.
  5. Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
  6. Copyright (C) 2004 Utilitek Systems, Inc.
  7. derived in part from smsc47m1.c:
  8. Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
  9. Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/ioport.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/i2c.h>
  27. #include <linux/i2c-isa.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/err.h>
  30. #include <linux/init.h>
  31. #include <asm/io.h>
  32. /* Address is autodetected, there is no default value */
  33. static unsigned short address;
  34. /* Super-I/0 registers and commands */
  35. #define REG 0x2e /* The register to read/write */
  36. #define VAL 0x2f /* The value to read/write */
  37. static inline void superio_outb(int reg, int val)
  38. {
  39. outb(reg, REG);
  40. outb(val, VAL);
  41. }
  42. static inline int superio_inb(int reg)
  43. {
  44. outb(reg, REG);
  45. return inb(VAL);
  46. }
  47. /* select superio logical device */
  48. static inline void superio_select(int ld)
  49. {
  50. superio_outb(0x07, ld);
  51. }
  52. static inline void superio_enter(void)
  53. {
  54. outb(0x55, REG);
  55. }
  56. static inline void superio_exit(void)
  57. {
  58. outb(0xAA, REG);
  59. }
  60. #define SUPERIO_REG_DEVID 0x20
  61. #define SUPERIO_REG_DEVREV 0x21
  62. #define SUPERIO_REG_BASE_MSB 0x60
  63. #define SUPERIO_REG_BASE_LSB 0x61
  64. #define SUPERIO_REG_LD8 0x08
  65. #define SMSC_EXTENT 0x02
  66. /* 0 <= nr <= 3 */
  67. static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
  68. #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
  69. /* 0 <= nr <= 3 */
  70. #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
  71. #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
  72. struct smsc47b397_data {
  73. struct i2c_client client;
  74. struct class_device *class_dev;
  75. struct semaphore lock;
  76. struct semaphore update_lock;
  77. unsigned long last_updated; /* in jiffies */
  78. int valid;
  79. /* register values */
  80. u16 fan[4];
  81. u8 temp[4];
  82. };
  83. static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
  84. {
  85. struct smsc47b397_data *data = i2c_get_clientdata(client);
  86. int res;
  87. down(&data->lock);
  88. outb(reg, client->addr);
  89. res = inb_p(client->addr + 1);
  90. up(&data->lock);
  91. return res;
  92. }
  93. static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
  94. {
  95. struct i2c_client *client = to_i2c_client(dev);
  96. struct smsc47b397_data *data = i2c_get_clientdata(client);
  97. int i;
  98. down(&data->update_lock);
  99. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  100. dev_dbg(&client->dev, "starting device update...\n");
  101. /* 4 temperature inputs, 4 fan inputs */
  102. for (i = 0; i < 4; i++) {
  103. data->temp[i] = smsc47b397_read_value(client,
  104. SMSC47B397_REG_TEMP(i));
  105. /* must read LSB first */
  106. data->fan[i] = smsc47b397_read_value(client,
  107. SMSC47B397_REG_FAN_LSB(i));
  108. data->fan[i] |= smsc47b397_read_value(client,
  109. SMSC47B397_REG_FAN_MSB(i)) << 8;
  110. }
  111. data->last_updated = jiffies;
  112. data->valid = 1;
  113. dev_dbg(&client->dev, "... device update complete\n");
  114. }
  115. up(&data->update_lock);
  116. return data;
  117. }
  118. /* TEMP: 0.001C/bit (-128C to +127C)
  119. REG: 1C/bit, two's complement */
  120. static int temp_from_reg(u8 reg)
  121. {
  122. return (s8)reg * 1000;
  123. }
  124. /* 0 <= nr <= 3 */
  125. static ssize_t show_temp(struct device *dev, char *buf, int nr)
  126. {
  127. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  128. return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
  129. }
  130. #define sysfs_temp(num) \
  131. static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
  132. { \
  133. return show_temp(dev, buf, num-1); \
  134. } \
  135. static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
  136. sysfs_temp(1);
  137. sysfs_temp(2);
  138. sysfs_temp(3);
  139. sysfs_temp(4);
  140. #define device_create_file_temp(client, num) \
  141. device_create_file(&client->dev, &dev_attr_temp##num##_input)
  142. /* FAN: 1 RPM/bit
  143. REG: count of 90kHz pulses / revolution */
  144. static int fan_from_reg(u16 reg)
  145. {
  146. return 90000 * 60 / reg;
  147. }
  148. /* 0 <= nr <= 3 */
  149. static ssize_t show_fan(struct device *dev, char *buf, int nr)
  150. {
  151. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  152. return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
  153. }
  154. #define sysfs_fan(num) \
  155. static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
  156. { \
  157. return show_fan(dev, buf, num-1); \
  158. } \
  159. static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
  160. sysfs_fan(1);
  161. sysfs_fan(2);
  162. sysfs_fan(3);
  163. sysfs_fan(4);
  164. #define device_create_file_fan(client, num) \
  165. device_create_file(&client->dev, &dev_attr_fan##num##_input)
  166. static int smsc47b397_detach_client(struct i2c_client *client)
  167. {
  168. struct smsc47b397_data *data = i2c_get_clientdata(client);
  169. int err;
  170. hwmon_device_unregister(data->class_dev);
  171. if ((err = i2c_detach_client(client)))
  172. return err;
  173. release_region(client->addr, SMSC_EXTENT);
  174. kfree(data);
  175. return 0;
  176. }
  177. static int smsc47b397_detect(struct i2c_adapter *adapter);
  178. static struct i2c_driver smsc47b397_driver = {
  179. .driver = {
  180. .name = "smsc47b397",
  181. },
  182. .attach_adapter = smsc47b397_detect,
  183. .detach_client = smsc47b397_detach_client,
  184. };
  185. static int smsc47b397_detect(struct i2c_adapter *adapter)
  186. {
  187. struct i2c_client *new_client;
  188. struct smsc47b397_data *data;
  189. int err = 0;
  190. if (!request_region(address, SMSC_EXTENT,
  191. smsc47b397_driver.driver.name)) {
  192. dev_err(&adapter->dev, "Region 0x%x already in use!\n",
  193. address);
  194. return -EBUSY;
  195. }
  196. if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
  197. err = -ENOMEM;
  198. goto error_release;
  199. }
  200. new_client = &data->client;
  201. i2c_set_clientdata(new_client, data);
  202. new_client->addr = address;
  203. init_MUTEX(&data->lock);
  204. new_client->adapter = adapter;
  205. new_client->driver = &smsc47b397_driver;
  206. new_client->flags = 0;
  207. strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
  208. init_MUTEX(&data->update_lock);
  209. if ((err = i2c_attach_client(new_client)))
  210. goto error_free;
  211. data->class_dev = hwmon_device_register(&new_client->dev);
  212. if (IS_ERR(data->class_dev)) {
  213. err = PTR_ERR(data->class_dev);
  214. goto error_detach;
  215. }
  216. device_create_file_temp(new_client, 1);
  217. device_create_file_temp(new_client, 2);
  218. device_create_file_temp(new_client, 3);
  219. device_create_file_temp(new_client, 4);
  220. device_create_file_fan(new_client, 1);
  221. device_create_file_fan(new_client, 2);
  222. device_create_file_fan(new_client, 3);
  223. device_create_file_fan(new_client, 4);
  224. return 0;
  225. error_detach:
  226. i2c_detach_client(new_client);
  227. error_free:
  228. kfree(data);
  229. error_release:
  230. release_region(address, SMSC_EXTENT);
  231. return err;
  232. }
  233. static int __init smsc47b397_find(unsigned short *addr)
  234. {
  235. u8 id, rev;
  236. superio_enter();
  237. id = superio_inb(SUPERIO_REG_DEVID);
  238. if ((id != 0x6f) && (id != 0x81)) {
  239. superio_exit();
  240. return -ENODEV;
  241. }
  242. rev = superio_inb(SUPERIO_REG_DEVREV);
  243. superio_select(SUPERIO_REG_LD8);
  244. *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
  245. | superio_inb(SUPERIO_REG_BASE_LSB);
  246. printk(KERN_INFO "smsc47b397: found SMSC %s "
  247. "(base address 0x%04x, revision %u)\n",
  248. id == 0x81 ? "SCH5307-NS" : "LPC47B397-NC", *addr, rev);
  249. superio_exit();
  250. return 0;
  251. }
  252. static int __init smsc47b397_init(void)
  253. {
  254. int ret;
  255. if ((ret = smsc47b397_find(&address)))
  256. return ret;
  257. return i2c_isa_add_driver(&smsc47b397_driver);
  258. }
  259. static void __exit smsc47b397_exit(void)
  260. {
  261. i2c_isa_del_driver(&smsc47b397_driver);
  262. }
  263. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  264. MODULE_DESCRIPTION("SMSC LPC47B397 driver");
  265. MODULE_LICENSE("GPL");
  266. module_init(smsc47b397_init);
  267. module_exit(smsc47b397_exit);