smsc47b397.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #define device_create_file_temp(client, num) \
  142. device_create_file(&client->dev, &dev_attr_temp##num##_input)
  143. /* FAN: 1 RPM/bit
  144. REG: count of 90kHz pulses / revolution */
  145. static int fan_from_reg(u16 reg)
  146. {
  147. return 90000 * 60 / reg;
  148. }
  149. /* 0 <= nr <= 3 */
  150. static ssize_t show_fan(struct device *dev, char *buf, int nr)
  151. {
  152. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  153. return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
  154. }
  155. #define sysfs_fan(num) \
  156. static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
  157. { \
  158. return show_fan(dev, buf, num-1); \
  159. } \
  160. static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
  161. sysfs_fan(1);
  162. sysfs_fan(2);
  163. sysfs_fan(3);
  164. sysfs_fan(4);
  165. #define device_create_file_fan(client, num) \
  166. device_create_file(&client->dev, &dev_attr_fan##num##_input)
  167. static int smsc47b397_detach_client(struct i2c_client *client)
  168. {
  169. struct smsc47b397_data *data = i2c_get_clientdata(client);
  170. int err;
  171. hwmon_device_unregister(data->class_dev);
  172. if ((err = i2c_detach_client(client)))
  173. return err;
  174. release_region(client->addr, SMSC_EXTENT);
  175. kfree(data);
  176. return 0;
  177. }
  178. static int smsc47b397_detect(struct i2c_adapter *adapter);
  179. static struct i2c_driver smsc47b397_driver = {
  180. .driver = {
  181. .name = "smsc47b397",
  182. },
  183. .attach_adapter = smsc47b397_detect,
  184. .detach_client = smsc47b397_detach_client,
  185. };
  186. static int smsc47b397_detect(struct i2c_adapter *adapter)
  187. {
  188. struct i2c_client *new_client;
  189. struct smsc47b397_data *data;
  190. int err = 0;
  191. if (!request_region(address, SMSC_EXTENT,
  192. smsc47b397_driver.driver.name)) {
  193. dev_err(&adapter->dev, "Region 0x%x already in use!\n",
  194. address);
  195. return -EBUSY;
  196. }
  197. if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
  198. err = -ENOMEM;
  199. goto error_release;
  200. }
  201. new_client = &data->client;
  202. i2c_set_clientdata(new_client, data);
  203. new_client->addr = address;
  204. mutex_init(&data->lock);
  205. new_client->adapter = adapter;
  206. new_client->driver = &smsc47b397_driver;
  207. new_client->flags = 0;
  208. strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
  209. mutex_init(&data->update_lock);
  210. if ((err = i2c_attach_client(new_client)))
  211. goto error_free;
  212. data->class_dev = hwmon_device_register(&new_client->dev);
  213. if (IS_ERR(data->class_dev)) {
  214. err = PTR_ERR(data->class_dev);
  215. goto error_detach;
  216. }
  217. device_create_file_temp(new_client, 1);
  218. device_create_file_temp(new_client, 2);
  219. device_create_file_temp(new_client, 3);
  220. device_create_file_temp(new_client, 4);
  221. device_create_file_fan(new_client, 1);
  222. device_create_file_fan(new_client, 2);
  223. device_create_file_fan(new_client, 3);
  224. device_create_file_fan(new_client, 4);
  225. return 0;
  226. error_detach:
  227. i2c_detach_client(new_client);
  228. error_free:
  229. kfree(data);
  230. error_release:
  231. release_region(address, SMSC_EXTENT);
  232. return err;
  233. }
  234. static int __init smsc47b397_find(unsigned short *addr)
  235. {
  236. u8 id, rev;
  237. superio_enter();
  238. id = superio_inb(SUPERIO_REG_DEVID);
  239. if ((id != 0x6f) && (id != 0x81)) {
  240. superio_exit();
  241. return -ENODEV;
  242. }
  243. rev = superio_inb(SUPERIO_REG_DEVREV);
  244. superio_select(SUPERIO_REG_LD8);
  245. *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
  246. | superio_inb(SUPERIO_REG_BASE_LSB);
  247. printk(KERN_INFO "smsc47b397: found SMSC %s "
  248. "(base address 0x%04x, revision %u)\n",
  249. id == 0x81 ? "SCH5307-NS" : "LPC47B397-NC", *addr, rev);
  250. superio_exit();
  251. return 0;
  252. }
  253. static int __init smsc47b397_init(void)
  254. {
  255. int ret;
  256. if ((ret = smsc47b397_find(&address)))
  257. return ret;
  258. return i2c_isa_add_driver(&smsc47b397_driver);
  259. }
  260. static void __exit smsc47b397_exit(void)
  261. {
  262. i2c_isa_del_driver(&smsc47b397_driver);
  263. }
  264. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  265. MODULE_DESCRIPTION("SMSC LPC47B397 driver");
  266. MODULE_LICENSE("GPL");
  267. module_init(smsc47b397_init);
  268. module_exit(smsc47b397_exit);