smsc47b397.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/platform_device.h>
  27. #include <linux/hwmon.h>
  28. #include <linux/hwmon-sysfs.h>
  29. #include <linux/err.h>
  30. #include <linux/init.h>
  31. #include <linux/mutex.h>
  32. #include <asm/io.h>
  33. static struct platform_device *pdev;
  34. #define DRVNAME "smsc47b397"
  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. unsigned short addr;
  75. const char *name;
  76. struct class_device *class_dev;
  77. struct mutex lock;
  78. struct mutex update_lock;
  79. unsigned long last_updated; /* in jiffies */
  80. int valid;
  81. /* register values */
  82. u16 fan[4];
  83. u8 temp[4];
  84. };
  85. static int smsc47b397_read_value(struct smsc47b397_data* data, u8 reg)
  86. {
  87. int res;
  88. mutex_lock(&data->lock);
  89. outb(reg, data->addr);
  90. res = inb_p(data->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 smsc47b397_data *data = dev_get_drvdata(dev);
  97. int i;
  98. mutex_lock(&data->update_lock);
  99. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  100. dev_dbg(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(data,
  104. SMSC47B397_REG_TEMP(i));
  105. /* must read LSB first */
  106. data->fan[i] = smsc47b397_read_value(data,
  107. SMSC47B397_REG_FAN_LSB(i));
  108. data->fan[i] |= smsc47b397_read_value(data,
  109. SMSC47B397_REG_FAN_MSB(i)) << 8;
  110. }
  111. data->last_updated = jiffies;
  112. data->valid = 1;
  113. dev_dbg(dev, "... device update complete\n");
  114. }
  115. mutex_unlock(&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. static ssize_t show_temp(struct device *dev, struct device_attribute
  125. *devattr, char *buf)
  126. {
  127. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  128. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  129. return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
  130. }
  131. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  132. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  133. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  134. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  135. /* FAN: 1 RPM/bit
  136. REG: count of 90kHz pulses / revolution */
  137. static int fan_from_reg(u16 reg)
  138. {
  139. return 90000 * 60 / reg;
  140. }
  141. static ssize_t show_fan(struct device *dev, struct device_attribute
  142. *devattr, char *buf)
  143. {
  144. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  145. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  146. return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
  147. }
  148. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  149. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  150. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  151. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
  152. static ssize_t show_name(struct device *dev, struct device_attribute
  153. *devattr, char *buf)
  154. {
  155. struct smsc47b397_data *data = dev_get_drvdata(dev);
  156. return sprintf(buf, "%s\n", data->name);
  157. }
  158. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  159. static struct attribute *smsc47b397_attributes[] = {
  160. &sensor_dev_attr_temp1_input.dev_attr.attr,
  161. &sensor_dev_attr_temp2_input.dev_attr.attr,
  162. &sensor_dev_attr_temp3_input.dev_attr.attr,
  163. &sensor_dev_attr_temp4_input.dev_attr.attr,
  164. &sensor_dev_attr_fan1_input.dev_attr.attr,
  165. &sensor_dev_attr_fan2_input.dev_attr.attr,
  166. &sensor_dev_attr_fan3_input.dev_attr.attr,
  167. &sensor_dev_attr_fan4_input.dev_attr.attr,
  168. &dev_attr_name.attr,
  169. NULL
  170. };
  171. static const struct attribute_group smsc47b397_group = {
  172. .attrs = smsc47b397_attributes,
  173. };
  174. static int __devexit smsc47b397_remove(struct platform_device *pdev)
  175. {
  176. struct smsc47b397_data *data = platform_get_drvdata(pdev);
  177. struct resource *res;
  178. hwmon_device_unregister(data->class_dev);
  179. sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
  180. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  181. release_region(res->start, SMSC_EXTENT);
  182. kfree(data);
  183. return 0;
  184. }
  185. static int smsc47b397_probe(struct platform_device *pdev);
  186. static struct platform_driver smsc47b397_driver = {
  187. .driver = {
  188. .owner = THIS_MODULE,
  189. .name = DRVNAME,
  190. },
  191. .probe = smsc47b397_probe,
  192. .remove = __devexit_p(smsc47b397_remove),
  193. };
  194. static int __devinit smsc47b397_probe(struct platform_device *pdev)
  195. {
  196. struct device *dev = &pdev->dev;
  197. struct smsc47b397_data *data;
  198. struct resource *res;
  199. int err = 0;
  200. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  201. if (!request_region(res->start, SMSC_EXTENT,
  202. smsc47b397_driver.driver.name)) {
  203. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  204. (unsigned long)res->start,
  205. (unsigned long)res->start + SMSC_EXTENT - 1);
  206. return -EBUSY;
  207. }
  208. if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
  209. err = -ENOMEM;
  210. goto error_release;
  211. }
  212. data->addr = res->start;
  213. data->name = "smsc47b397";
  214. mutex_init(&data->lock);
  215. mutex_init(&data->update_lock);
  216. platform_set_drvdata(pdev, data);
  217. if ((err = sysfs_create_group(&dev->kobj, &smsc47b397_group)))
  218. goto error_free;
  219. data->class_dev = hwmon_device_register(dev);
  220. if (IS_ERR(data->class_dev)) {
  221. err = PTR_ERR(data->class_dev);
  222. goto error_remove;
  223. }
  224. return 0;
  225. error_remove:
  226. sysfs_remove_group(&dev->kobj, &smsc47b397_group);
  227. error_free:
  228. kfree(data);
  229. error_release:
  230. release_region(res->start, SMSC_EXTENT);
  231. return err;
  232. }
  233. static int __init smsc47b397_device_add(unsigned short address)
  234. {
  235. struct resource res = {
  236. .start = address,
  237. .end = address + SMSC_EXTENT - 1,
  238. .name = DRVNAME,
  239. .flags = IORESOURCE_IO,
  240. };
  241. int err;
  242. pdev = platform_device_alloc(DRVNAME, address);
  243. if (!pdev) {
  244. err = -ENOMEM;
  245. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  246. goto exit;
  247. }
  248. err = platform_device_add_resources(pdev, &res, 1);
  249. if (err) {
  250. printk(KERN_ERR DRVNAME ": Device resource addition failed "
  251. "(%d)\n", err);
  252. goto exit_device_put;
  253. }
  254. err = platform_device_add(pdev);
  255. if (err) {
  256. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  257. err);
  258. goto exit_device_put;
  259. }
  260. return 0;
  261. exit_device_put:
  262. platform_device_put(pdev);
  263. exit:
  264. return err;
  265. }
  266. static int __init smsc47b397_find(unsigned short *addr)
  267. {
  268. u8 id, rev;
  269. superio_enter();
  270. id = superio_inb(SUPERIO_REG_DEVID);
  271. if ((id != 0x6f) && (id != 0x81)) {
  272. superio_exit();
  273. return -ENODEV;
  274. }
  275. rev = superio_inb(SUPERIO_REG_DEVREV);
  276. superio_select(SUPERIO_REG_LD8);
  277. *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
  278. | superio_inb(SUPERIO_REG_BASE_LSB);
  279. printk(KERN_INFO DRVNAME ": found SMSC %s "
  280. "(base address 0x%04x, revision %u)\n",
  281. id == 0x81 ? "SCH5307-NS" : "LPC47B397-NC", *addr, rev);
  282. superio_exit();
  283. return 0;
  284. }
  285. static int __init smsc47b397_init(void)
  286. {
  287. unsigned short address;
  288. int ret;
  289. if ((ret = smsc47b397_find(&address)))
  290. return ret;
  291. ret = platform_driver_register(&smsc47b397_driver);
  292. if (ret)
  293. goto exit;
  294. /* Sets global pdev as a side effect */
  295. ret = smsc47b397_device_add(address);
  296. if (ret)
  297. goto exit_driver;
  298. return 0;
  299. exit_driver:
  300. platform_driver_unregister(&smsc47b397_driver);
  301. exit:
  302. return ret;
  303. }
  304. static void __exit smsc47b397_exit(void)
  305. {
  306. platform_device_unregister(pdev);
  307. platform_driver_unregister(&smsc47b397_driver);
  308. }
  309. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  310. MODULE_DESCRIPTION("SMSC LPC47B397 driver");
  311. MODULE_LICENSE("GPL");
  312. module_init(smsc47b397_init);
  313. module_exit(smsc47b397_exit);