smsc47b397.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <jdelvare@suse.de>
  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 int superio_enter(void)
  65. {
  66. if (!request_muxed_region(REG, 2, DRVNAME))
  67. return -EBUSY;
  68. outb(0x55, REG);
  69. return 0;
  70. }
  71. static inline void superio_exit(void)
  72. {
  73. outb(0xAA, REG);
  74. release_region(REG, 2);
  75. }
  76. #define SUPERIO_REG_DEVID 0x20
  77. #define SUPERIO_REG_DEVREV 0x21
  78. #define SUPERIO_REG_BASE_MSB 0x60
  79. #define SUPERIO_REG_BASE_LSB 0x61
  80. #define SUPERIO_REG_LD8 0x08
  81. #define SMSC_EXTENT 0x02
  82. /* 0 <= nr <= 3 */
  83. static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
  84. #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
  85. /* 0 <= nr <= 3 */
  86. #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
  87. #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
  88. struct smsc47b397_data {
  89. unsigned short addr;
  90. struct mutex lock;
  91. struct mutex update_lock;
  92. unsigned long last_updated; /* in jiffies */
  93. int valid;
  94. /* register values */
  95. u16 fan[4];
  96. u8 temp[4];
  97. };
  98. static int smsc47b397_read_value(struct smsc47b397_data *data, u8 reg)
  99. {
  100. int res;
  101. mutex_lock(&data->lock);
  102. outb(reg, data->addr);
  103. res = inb_p(data->addr + 1);
  104. mutex_unlock(&data->lock);
  105. return res;
  106. }
  107. static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
  108. {
  109. struct smsc47b397_data *data = dev_get_drvdata(dev);
  110. int i;
  111. mutex_lock(&data->update_lock);
  112. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  113. dev_dbg(dev, "starting device update...\n");
  114. /* 4 temperature inputs, 4 fan inputs */
  115. for (i = 0; i < 4; i++) {
  116. data->temp[i] = smsc47b397_read_value(data,
  117. SMSC47B397_REG_TEMP(i));
  118. /* must read LSB first */
  119. data->fan[i] = smsc47b397_read_value(data,
  120. SMSC47B397_REG_FAN_LSB(i));
  121. data->fan[i] |= smsc47b397_read_value(data,
  122. SMSC47B397_REG_FAN_MSB(i)) << 8;
  123. }
  124. data->last_updated = jiffies;
  125. data->valid = 1;
  126. dev_dbg(dev, "... device update complete\n");
  127. }
  128. mutex_unlock(&data->update_lock);
  129. return data;
  130. }
  131. /*
  132. * TEMP: 0.001C/bit (-128C to +127C)
  133. * REG: 1C/bit, two's complement
  134. */
  135. static int temp_from_reg(u8 reg)
  136. {
  137. return (s8)reg * 1000;
  138. }
  139. static ssize_t show_temp(struct device *dev, struct device_attribute
  140. *devattr, char *buf)
  141. {
  142. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  143. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  144. return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
  145. }
  146. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  147. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  148. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  149. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  150. /*
  151. * FAN: 1 RPM/bit
  152. * REG: count of 90kHz pulses / revolution
  153. */
  154. static int fan_from_reg(u16 reg)
  155. {
  156. if (reg == 0 || reg == 0xffff)
  157. return 0;
  158. return 90000 * 60 / reg;
  159. }
  160. static ssize_t show_fan(struct device *dev, struct device_attribute
  161. *devattr, char *buf)
  162. {
  163. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  164. struct smsc47b397_data *data = smsc47b397_update_device(dev);
  165. return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
  166. }
  167. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  168. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  169. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  170. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
  171. static struct attribute *smsc47b397_attrs[] = {
  172. &sensor_dev_attr_temp1_input.dev_attr.attr,
  173. &sensor_dev_attr_temp2_input.dev_attr.attr,
  174. &sensor_dev_attr_temp3_input.dev_attr.attr,
  175. &sensor_dev_attr_temp4_input.dev_attr.attr,
  176. &sensor_dev_attr_fan1_input.dev_attr.attr,
  177. &sensor_dev_attr_fan2_input.dev_attr.attr,
  178. &sensor_dev_attr_fan3_input.dev_attr.attr,
  179. &sensor_dev_attr_fan4_input.dev_attr.attr,
  180. NULL
  181. };
  182. ATTRIBUTE_GROUPS(smsc47b397);
  183. static int smsc47b397_probe(struct platform_device *pdev);
  184. static struct platform_driver smsc47b397_driver = {
  185. .driver = {
  186. .name = DRVNAME,
  187. },
  188. .probe = smsc47b397_probe,
  189. };
  190. static int smsc47b397_probe(struct platform_device *pdev)
  191. {
  192. struct device *dev = &pdev->dev;
  193. struct smsc47b397_data *data;
  194. struct device *hwmon_dev;
  195. struct resource *res;
  196. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  197. if (!devm_request_region(dev, res->start, SMSC_EXTENT,
  198. smsc47b397_driver.driver.name)) {
  199. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  200. (unsigned long)res->start,
  201. (unsigned long)res->start + SMSC_EXTENT - 1);
  202. return -EBUSY;
  203. }
  204. data = devm_kzalloc(dev, sizeof(struct smsc47b397_data), GFP_KERNEL);
  205. if (!data)
  206. return -ENOMEM;
  207. data->addr = res->start;
  208. mutex_init(&data->lock);
  209. mutex_init(&data->update_lock);
  210. hwmon_dev = devm_hwmon_device_register_with_groups(dev, "smsc47b397",
  211. data,
  212. smsc47b397_groups);
  213. return PTR_ERR_OR_ZERO(hwmon_dev);
  214. }
  215. static int __init smsc47b397_device_add(unsigned short address)
  216. {
  217. struct resource res = {
  218. .start = address,
  219. .end = address + SMSC_EXTENT - 1,
  220. .name = DRVNAME,
  221. .flags = IORESOURCE_IO,
  222. };
  223. int err;
  224. err = acpi_check_resource_conflict(&res);
  225. if (err)
  226. goto exit;
  227. pdev = platform_device_alloc(DRVNAME, address);
  228. if (!pdev) {
  229. err = -ENOMEM;
  230. pr_err("Device allocation failed\n");
  231. goto exit;
  232. }
  233. err = platform_device_add_resources(pdev, &res, 1);
  234. if (err) {
  235. pr_err("Device resource addition failed (%d)\n", err);
  236. goto exit_device_put;
  237. }
  238. err = platform_device_add(pdev);
  239. if (err) {
  240. pr_err("Device addition failed (%d)\n", err);
  241. goto exit_device_put;
  242. }
  243. return 0;
  244. exit_device_put:
  245. platform_device_put(pdev);
  246. exit:
  247. return err;
  248. }
  249. static int __init smsc47b397_find(void)
  250. {
  251. u8 id, rev;
  252. char *name;
  253. unsigned short addr;
  254. int err;
  255. err = superio_enter();
  256. if (err)
  257. return err;
  258. id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
  259. switch (id) {
  260. case 0x81:
  261. name = "SCH5307-NS";
  262. break;
  263. case 0x6f:
  264. name = "LPC47B397-NC";
  265. break;
  266. case 0x85:
  267. case 0x8c:
  268. name = "SCH5317";
  269. break;
  270. default:
  271. superio_exit();
  272. return -ENODEV;
  273. }
  274. rev = superio_inb(SUPERIO_REG_DEVREV);
  275. superio_select(SUPERIO_REG_LD8);
  276. addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
  277. | superio_inb(SUPERIO_REG_BASE_LSB);
  278. pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
  279. name, addr, rev);
  280. superio_exit();
  281. return addr;
  282. }
  283. static int __init smsc47b397_init(void)
  284. {
  285. unsigned short address;
  286. int ret;
  287. ret = smsc47b397_find();
  288. if (ret < 0)
  289. return ret;
  290. address = 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);