smsc47b397.c 8.7 KB

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