smsc47b397.c 8.5 KB

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