smsc47b397.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 <linux/mutex.h>
  32. #include <asm/io.h>
  33. /* Address is autodetected, there is no default value */
  34. static unsigned short address;
  35. /* Super-I/0 registers and commands */
  36. #define REG 0x2e /* The register to read/write */
  37. #define VAL 0x2f /* The value to read/write */
  38. static inline void superio_outb(int reg, int val)
  39. {
  40. outb(reg, REG);
  41. outb(val, VAL);
  42. }
  43. static inline int superio_inb(int reg)
  44. {
  45. outb(reg, REG);
  46. return inb(VAL);
  47. }
  48. /* select superio logical device */
  49. static inline void superio_select(int ld)
  50. {
  51. superio_outb(0x07, ld);
  52. }
  53. static inline void superio_enter(void)
  54. {
  55. outb(0x55, REG);
  56. }
  57. static inline void superio_exit(void)
  58. {
  59. outb(0xAA, REG);
  60. }
  61. #define SUPERIO_REG_DEVID 0x20
  62. #define SUPERIO_REG_DEVREV 0x21
  63. #define SUPERIO_REG_BASE_MSB 0x60
  64. #define SUPERIO_REG_BASE_LSB 0x61
  65. #define SUPERIO_REG_LD8 0x08
  66. #define SMSC_EXTENT 0x02
  67. /* 0 <= nr <= 3 */
  68. static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
  69. #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
  70. /* 0 <= nr <= 3 */
  71. #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
  72. #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
  73. struct smsc47b397_data {
  74. struct i2c_client client;
  75. struct class_device *class_dev;
  76. struct mutex lock;
  77. struct mutex update_lock;
  78. unsigned long last_updated; /* in jiffies */
  79. int valid;
  80. /* register values */
  81. u16 fan[4];
  82. u8 temp[4];
  83. };
  84. static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
  85. {
  86. struct smsc47b397_data *data = i2c_get_clientdata(client);
  87. int res;
  88. mutex_lock(&data->lock);
  89. outb(reg, client->addr);
  90. res = inb_p(client->addr + 1);
  91. mutex_unlock(&data->lock);
  92. return res;
  93. }
  94. static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
  95. {
  96. struct i2c_client *client = to_i2c_client(dev);
  97. struct smsc47b397_data *data = i2c_get_clientdata(client);
  98. int i;
  99. mutex_lock(&data->update_lock);
  100. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  101. dev_dbg(&client->dev, "starting device update...\n");
  102. /* 4 temperature inputs, 4 fan inputs */
  103. for (i = 0; i < 4; i++) {
  104. data->temp[i] = smsc47b397_read_value(client,
  105. SMSC47B397_REG_TEMP(i));
  106. /* must read LSB first */
  107. data->fan[i] = smsc47b397_read_value(client,
  108. SMSC47B397_REG_FAN_LSB(i));
  109. data->fan[i] |= smsc47b397_read_value(client,
  110. SMSC47B397_REG_FAN_MSB(i)) << 8;
  111. }
  112. data->last_updated = jiffies;
  113. data->valid = 1;
  114. dev_dbg(&client->dev, "... device update complete\n");
  115. }
  116. mutex_unlock(&data->update_lock);
  117. return data;
  118. }
  119. /* TEMP: 0.001C/bit (-128C to +127C)
  120. REG: 1C/bit, two's complement */
  121. static int temp_from_reg(u8 reg)
  122. {
  123. return (s8)reg * 1000;
  124. }
  125. /* 0 <= nr <= 3 */
  126. static ssize_t show_temp(struct device *dev, char *buf, int nr)
  127. {
  128. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  129. return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
  130. }
  131. #define sysfs_temp(num) \
  132. static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
  133. { \
  134. return show_temp(dev, buf, num-1); \
  135. } \
  136. static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
  137. sysfs_temp(1);
  138. sysfs_temp(2);
  139. sysfs_temp(3);
  140. sysfs_temp(4);
  141. /* FAN: 1 RPM/bit
  142. REG: count of 90kHz pulses / revolution */
  143. static int fan_from_reg(u16 reg)
  144. {
  145. return 90000 * 60 / reg;
  146. }
  147. /* 0 <= nr <= 3 */
  148. static ssize_t show_fan(struct device *dev, char *buf, int nr)
  149. {
  150. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  151. return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
  152. }
  153. #define sysfs_fan(num) \
  154. static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
  155. { \
  156. return show_fan(dev, buf, num-1); \
  157. } \
  158. static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
  159. sysfs_fan(1);
  160. sysfs_fan(2);
  161. sysfs_fan(3);
  162. sysfs_fan(4);
  163. static struct attribute *smsc47b397_attributes[] = {
  164. &dev_attr_temp1_input.attr,
  165. &dev_attr_temp2_input.attr,
  166. &dev_attr_temp3_input.attr,
  167. &dev_attr_temp4_input.attr,
  168. &dev_attr_fan1_input.attr,
  169. &dev_attr_fan2_input.attr,
  170. &dev_attr_fan3_input.attr,
  171. &dev_attr_fan4_input.attr,
  172. NULL
  173. };
  174. static const struct attribute_group smsc47b397_group = {
  175. .attrs = smsc47b397_attributes,
  176. };
  177. static int smsc47b397_detach_client(struct i2c_client *client)
  178. {
  179. struct smsc47b397_data *data = i2c_get_clientdata(client);
  180. int err;
  181. hwmon_device_unregister(data->class_dev);
  182. sysfs_remove_group(&client->dev.kobj, &smsc47b397_group);
  183. if ((err = i2c_detach_client(client)))
  184. return err;
  185. release_region(client->addr, SMSC_EXTENT);
  186. kfree(data);
  187. return 0;
  188. }
  189. static int smsc47b397_detect(struct i2c_adapter *adapter);
  190. static struct i2c_driver smsc47b397_driver = {
  191. .driver = {
  192. .owner = THIS_MODULE,
  193. .name = "smsc47b397",
  194. },
  195. .attach_adapter = smsc47b397_detect,
  196. .detach_client = smsc47b397_detach_client,
  197. };
  198. static int smsc47b397_detect(struct i2c_adapter *adapter)
  199. {
  200. struct i2c_client *new_client;
  201. struct smsc47b397_data *data;
  202. int err = 0;
  203. if (!request_region(address, SMSC_EXTENT,
  204. smsc47b397_driver.driver.name)) {
  205. dev_err(&adapter->dev, "Region 0x%x already in use!\n",
  206. address);
  207. return -EBUSY;
  208. }
  209. if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
  210. err = -ENOMEM;
  211. goto error_release;
  212. }
  213. new_client = &data->client;
  214. i2c_set_clientdata(new_client, data);
  215. new_client->addr = address;
  216. mutex_init(&data->lock);
  217. new_client->adapter = adapter;
  218. new_client->driver = &smsc47b397_driver;
  219. new_client->flags = 0;
  220. strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
  221. mutex_init(&data->update_lock);
  222. if ((err = i2c_attach_client(new_client)))
  223. goto error_free;
  224. if ((err = sysfs_create_group(&new_client->dev.kobj, &smsc47b397_group)))
  225. goto error_detach;
  226. data->class_dev = hwmon_device_register(&new_client->dev);
  227. if (IS_ERR(data->class_dev)) {
  228. err = PTR_ERR(data->class_dev);
  229. goto error_remove;
  230. }
  231. return 0;
  232. error_remove:
  233. sysfs_remove_group(&new_client->dev.kobj, &smsc47b397_group);
  234. error_detach:
  235. i2c_detach_client(new_client);
  236. error_free:
  237. kfree(data);
  238. error_release:
  239. release_region(address, SMSC_EXTENT);
  240. return err;
  241. }
  242. static int __init smsc47b397_find(unsigned short *addr)
  243. {
  244. u8 id, rev;
  245. superio_enter();
  246. id = superio_inb(SUPERIO_REG_DEVID);
  247. if ((id != 0x6f) && (id != 0x81)) {
  248. superio_exit();
  249. return -ENODEV;
  250. }
  251. rev = superio_inb(SUPERIO_REG_DEVREV);
  252. superio_select(SUPERIO_REG_LD8);
  253. *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
  254. | superio_inb(SUPERIO_REG_BASE_LSB);
  255. printk(KERN_INFO "smsc47b397: found SMSC %s "
  256. "(base address 0x%04x, revision %u)\n",
  257. id == 0x81 ? "SCH5307-NS" : "LPC47B397-NC", *addr, rev);
  258. superio_exit();
  259. return 0;
  260. }
  261. static int __init smsc47b397_init(void)
  262. {
  263. int ret;
  264. if ((ret = smsc47b397_find(&address)))
  265. return ret;
  266. return i2c_isa_add_driver(&smsc47b397_driver);
  267. }
  268. static void __exit smsc47b397_exit(void)
  269. {
  270. i2c_isa_del_driver(&smsc47b397_driver);
  271. }
  272. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  273. MODULE_DESCRIPTION("SMSC LPC47B397 driver");
  274. MODULE_LICENSE("GPL");
  275. module_init(smsc47b397_init);
  276. module_exit(smsc47b397_exit);