smsc47b397.c 9.9 KB

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