smsc47b397.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/ioport.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/init.h>
  32. #include <linux/mutex.h>
  33. #include <linux/acpi.h>
  34. #include <linux/io.h>
  35. static unsigned short force_id;
  36. module_param(force_id, ushort, 0);
  37. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  38. static struct platform_device *pdev;
  39. #define DRVNAME "smsc47b397"
  40. /* Super-I/0 registers and commands */
  41. #define REG 0x2e /* The register to read/write */
  42. #define VAL 0x2f /* The value to read/write */
  43. static inline void superio_outb(int reg, int val)
  44. {
  45. outb(reg, REG);
  46. outb(val, VAL);
  47. }
  48. static inline int superio_inb(int reg)
  49. {
  50. outb(reg, REG);
  51. return inb(VAL);
  52. }
  53. /* select superio logical device */
  54. static inline void superio_select(int ld)
  55. {
  56. superio_outb(0x07, ld);
  57. }
  58. static inline void superio_enter(void)
  59. {
  60. outb(0x55, REG);
  61. }
  62. static inline void superio_exit(void)
  63. {
  64. outb(0xAA, REG);
  65. }
  66. #define SUPERIO_REG_DEVID 0x20
  67. #define SUPERIO_REG_DEVREV 0x21
  68. #define SUPERIO_REG_BASE_MSB 0x60
  69. #define SUPERIO_REG_BASE_LSB 0x61
  70. #define SUPERIO_REG_LD8 0x08
  71. #define SMSC_EXTENT 0x02
  72. /* 0 <= nr <= 3 */
  73. static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
  74. #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
  75. /* 0 <= nr <= 3 */
  76. #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
  77. #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
  78. struct smsc47b397_data {
  79. unsigned short addr;
  80. const char *name;
  81. struct device *hwmon_dev;
  82. struct mutex lock;
  83. struct mutex 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 smsc47b397_data* data, u8 reg)
  91. {
  92. int res;
  93. mutex_lock(&data->lock);
  94. outb(reg, data->addr);
  95. res = inb_p(data->addr + 1);
  96. mutex_unlock(&data->lock);
  97. return res;
  98. }
  99. static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
  100. {
  101. struct smsc47b397_data *data = dev_get_drvdata(dev);
  102. int i;
  103. mutex_lock(&data->update_lock);
  104. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  105. dev_dbg(dev, "starting device update...\n");
  106. /* 4 temperature inputs, 4 fan inputs */
  107. for (i = 0; i < 4; i++) {
  108. data->temp[i] = smsc47b397_read_value(data,
  109. SMSC47B397_REG_TEMP(i));
  110. /* must read LSB first */
  111. data->fan[i] = smsc47b397_read_value(data,
  112. SMSC47B397_REG_FAN_LSB(i));
  113. data->fan[i] |= smsc47b397_read_value(data,
  114. SMSC47B397_REG_FAN_MSB(i)) << 8;
  115. }
  116. data->last_updated = jiffies;
  117. data->valid = 1;
  118. dev_dbg(dev, "... device update complete\n");
  119. }
  120. mutex_unlock(&data->update_lock);
  121. return data;
  122. }
  123. /* TEMP: 0.001C/bit (-128C to +127C)
  124. REG: 1C/bit, two's complement */
  125. static int temp_from_reg(u8 reg)
  126. {
  127. return (s8)reg * 1000;
  128. }
  129. static ssize_t show_temp(struct device *dev, struct device_attribute
  130. *devattr, char *buf)
  131. {
  132. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  133. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  134. return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
  135. }
  136. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  137. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  138. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  139. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  140. /* FAN: 1 RPM/bit
  141. REG: count of 90kHz pulses / revolution */
  142. static int fan_from_reg(u16 reg)
  143. {
  144. if (reg == 0 || reg == 0xffff)
  145. return 0;
  146. return 90000 * 60 / reg;
  147. }
  148. static ssize_t show_fan(struct device *dev, struct device_attribute
  149. *devattr, char *buf)
  150. {
  151. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  152. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  153. return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
  154. }
  155. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  156. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  157. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  158. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
  159. static ssize_t show_name(struct device *dev, struct device_attribute
  160. *devattr, char *buf)
  161. {
  162. struct smsc47b397_data *data = dev_get_drvdata(dev);
  163. return sprintf(buf, "%s\n", data->name);
  164. }
  165. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  166. static struct attribute *smsc47b397_attributes[] = {
  167. &sensor_dev_attr_temp1_input.dev_attr.attr,
  168. &sensor_dev_attr_temp2_input.dev_attr.attr,
  169. &sensor_dev_attr_temp3_input.dev_attr.attr,
  170. &sensor_dev_attr_temp4_input.dev_attr.attr,
  171. &sensor_dev_attr_fan1_input.dev_attr.attr,
  172. &sensor_dev_attr_fan2_input.dev_attr.attr,
  173. &sensor_dev_attr_fan3_input.dev_attr.attr,
  174. &sensor_dev_attr_fan4_input.dev_attr.attr,
  175. &dev_attr_name.attr,
  176. NULL
  177. };
  178. static const struct attribute_group smsc47b397_group = {
  179. .attrs = smsc47b397_attributes,
  180. };
  181. static int __devexit smsc47b397_remove(struct platform_device *pdev)
  182. {
  183. struct smsc47b397_data *data = platform_get_drvdata(pdev);
  184. struct resource *res;
  185. hwmon_device_unregister(data->hwmon_dev);
  186. sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
  187. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  188. release_region(res->start, SMSC_EXTENT);
  189. kfree(data);
  190. return 0;
  191. }
  192. static int smsc47b397_probe(struct platform_device *pdev);
  193. static struct platform_driver smsc47b397_driver = {
  194. .driver = {
  195. .owner = THIS_MODULE,
  196. .name = DRVNAME,
  197. },
  198. .probe = smsc47b397_probe,
  199. .remove = __devexit_p(smsc47b397_remove),
  200. };
  201. static int __devinit smsc47b397_probe(struct platform_device *pdev)
  202. {
  203. struct device *dev = &pdev->dev;
  204. struct smsc47b397_data *data;
  205. struct resource *res;
  206. int err = 0;
  207. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  208. if (!request_region(res->start, SMSC_EXTENT,
  209. smsc47b397_driver.driver.name)) {
  210. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  211. (unsigned long)res->start,
  212. (unsigned long)res->start + SMSC_EXTENT - 1);
  213. return -EBUSY;
  214. }
  215. if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
  216. err = -ENOMEM;
  217. goto error_release;
  218. }
  219. data->addr = res->start;
  220. data->name = "smsc47b397";
  221. mutex_init(&data->lock);
  222. mutex_init(&data->update_lock);
  223. platform_set_drvdata(pdev, data);
  224. if ((err = sysfs_create_group(&dev->kobj, &smsc47b397_group)))
  225. goto error_free;
  226. data->hwmon_dev = hwmon_device_register(dev);
  227. if (IS_ERR(data->hwmon_dev)) {
  228. err = PTR_ERR(data->hwmon_dev);
  229. goto error_remove;
  230. }
  231. return 0;
  232. error_remove:
  233. sysfs_remove_group(&dev->kobj, &smsc47b397_group);
  234. error_free:
  235. kfree(data);
  236. error_release:
  237. release_region(res->start, SMSC_EXTENT);
  238. return err;
  239. }
  240. static int __init smsc47b397_device_add(unsigned short address)
  241. {
  242. struct resource res = {
  243. .start = address,
  244. .end = address + SMSC_EXTENT - 1,
  245. .name = DRVNAME,
  246. .flags = IORESOURCE_IO,
  247. };
  248. int err;
  249. err = acpi_check_resource_conflict(&res);
  250. if (err)
  251. goto exit;
  252. pdev = platform_device_alloc(DRVNAME, address);
  253. if (!pdev) {
  254. err = -ENOMEM;
  255. pr_err("Device allocation failed\n");
  256. goto exit;
  257. }
  258. err = platform_device_add_resources(pdev, &res, 1);
  259. if (err) {
  260. pr_err("Device resource addition failed (%d)\n", err);
  261. goto exit_device_put;
  262. }
  263. err = platform_device_add(pdev);
  264. if (err) {
  265. pr_err("Device addition failed (%d)\n", err);
  266. goto exit_device_put;
  267. }
  268. return 0;
  269. exit_device_put:
  270. platform_device_put(pdev);
  271. exit:
  272. return err;
  273. }
  274. static int __init smsc47b397_find(unsigned short *addr)
  275. {
  276. u8 id, rev;
  277. char *name;
  278. superio_enter();
  279. id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
  280. switch(id) {
  281. case 0x81:
  282. name = "SCH5307-NS";
  283. break;
  284. case 0x6f:
  285. name = "LPC47B397-NC";
  286. break;
  287. case 0x85:
  288. case 0x8c:
  289. name = "SCH5317";
  290. break;
  291. default:
  292. superio_exit();
  293. return -ENODEV;
  294. }
  295. rev = superio_inb(SUPERIO_REG_DEVREV);
  296. superio_select(SUPERIO_REG_LD8);
  297. *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
  298. | superio_inb(SUPERIO_REG_BASE_LSB);
  299. pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
  300. name, *addr, rev);
  301. superio_exit();
  302. return 0;
  303. }
  304. static int __init smsc47b397_init(void)
  305. {
  306. unsigned short address;
  307. int ret;
  308. if ((ret = smsc47b397_find(&address)))
  309. return ret;
  310. ret = platform_driver_register(&smsc47b397_driver);
  311. if (ret)
  312. goto exit;
  313. /* Sets global pdev as a side effect */
  314. ret = smsc47b397_device_add(address);
  315. if (ret)
  316. goto exit_driver;
  317. return 0;
  318. exit_driver:
  319. platform_driver_unregister(&smsc47b397_driver);
  320. exit:
  321. return ret;
  322. }
  323. static void __exit smsc47b397_exit(void)
  324. {
  325. platform_device_unregister(pdev);
  326. platform_driver_unregister(&smsc47b397_driver);
  327. }
  328. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  329. MODULE_DESCRIPTION("SMSC LPC47B397 driver");
  330. MODULE_LICENSE("GPL");
  331. module_init(smsc47b397_init);
  332. module_exit(smsc47b397_exit);