smsc47b397.c 8.9 KB

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