smsc47m1.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * smsc47m1.c - Part of lm_sensors, Linux kernel modules
  3. * for hardware monitoring
  4. *
  5. * Supports the SMSC LPC47B27x, LPC47M10x, LPC47M112, LPC47M13x,
  6. * LPC47M14x, LPC47M15x, LPC47M192, LPC47M292 and LPC47M997
  7. * Super-I/O chips.
  8. *
  9. * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
  10. * Copyright (C) 2004-2007 Jean Delvare <jdelvare@suse.de>
  11. * Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
  12. * and Jean Delvare
  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/sysfs.h>
  40. #include <linux/acpi.h>
  41. #include <linux/io.h>
  42. static unsigned short force_id;
  43. module_param(force_id, ushort, 0);
  44. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  45. static struct platform_device *pdev;
  46. #define DRVNAME "smsc47m1"
  47. enum chips { smsc47m1, smsc47m2 };
  48. /* Super-I/0 registers and commands */
  49. #define REG 0x2e /* The register to read/write */
  50. #define VAL 0x2f /* The value to read/write */
  51. static inline void
  52. superio_outb(int reg, int val)
  53. {
  54. outb(reg, REG);
  55. outb(val, VAL);
  56. }
  57. static inline int
  58. superio_inb(int reg)
  59. {
  60. outb(reg, REG);
  61. return inb(VAL);
  62. }
  63. /* logical device for fans is 0x0A */
  64. #define superio_select() superio_outb(0x07, 0x0A)
  65. static inline void
  66. superio_enter(void)
  67. {
  68. outb(0x55, REG);
  69. }
  70. static inline void
  71. superio_exit(void)
  72. {
  73. outb(0xAA, REG);
  74. }
  75. #define SUPERIO_REG_ACT 0x30
  76. #define SUPERIO_REG_BASE 0x60
  77. #define SUPERIO_REG_DEVID 0x20
  78. #define SUPERIO_REG_DEVREV 0x21
  79. /* Logical device registers */
  80. #define SMSC_EXTENT 0x80
  81. /* nr is 0 or 1 in the macros below */
  82. #define SMSC47M1_REG_ALARM 0x04
  83. #define SMSC47M1_REG_TPIN(nr) (0x34 - (nr))
  84. #define SMSC47M1_REG_PPIN(nr) (0x36 - (nr))
  85. #define SMSC47M1_REG_FANDIV 0x58
  86. static const u8 SMSC47M1_REG_FAN[3] = { 0x59, 0x5a, 0x6b };
  87. static const u8 SMSC47M1_REG_FAN_PRELOAD[3] = { 0x5b, 0x5c, 0x6c };
  88. static const u8 SMSC47M1_REG_PWM[3] = { 0x56, 0x57, 0x69 };
  89. #define SMSC47M2_REG_ALARM6 0x09
  90. #define SMSC47M2_REG_TPIN1 0x38
  91. #define SMSC47M2_REG_TPIN2 0x37
  92. #define SMSC47M2_REG_TPIN3 0x2d
  93. #define SMSC47M2_REG_PPIN3 0x2c
  94. #define SMSC47M2_REG_FANDIV3 0x6a
  95. #define MIN_FROM_REG(reg, div) ((reg) >= 192 ? 0 : \
  96. 983040 / ((192 - (reg)) * (div)))
  97. #define FAN_FROM_REG(reg, div, preload) ((reg) <= (preload) || (reg) == 255 ? \
  98. 0 : \
  99. 983040 / (((reg) - (preload)) * (div)))
  100. #define DIV_FROM_REG(reg) (1 << (reg))
  101. #define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1)
  102. #define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01)
  103. #define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E)
  104. struct smsc47m1_data {
  105. unsigned short addr;
  106. const char *name;
  107. enum chips type;
  108. struct device *hwmon_dev;
  109. struct mutex update_lock;
  110. unsigned long last_updated; /* In jiffies */
  111. u8 fan[3]; /* Register value */
  112. u8 fan_preload[3]; /* Register value */
  113. u8 fan_div[3]; /* Register encoding, shifted right */
  114. u8 alarms; /* Register encoding */
  115. u8 pwm[3]; /* Register value (bit 0 is disable) */
  116. };
  117. struct smsc47m1_sio_data {
  118. enum chips type;
  119. u8 activate; /* Remember initial device state */
  120. };
  121. static inline int smsc47m1_read_value(struct smsc47m1_data *data, u8 reg)
  122. {
  123. return inb_p(data->addr + reg);
  124. }
  125. static inline void smsc47m1_write_value(struct smsc47m1_data *data, u8 reg,
  126. u8 value)
  127. {
  128. outb_p(value, data->addr + reg);
  129. }
  130. static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
  131. int init)
  132. {
  133. struct smsc47m1_data *data = dev_get_drvdata(dev);
  134. mutex_lock(&data->update_lock);
  135. if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) {
  136. int i, fan_nr;
  137. fan_nr = data->type == smsc47m2 ? 3 : 2;
  138. for (i = 0; i < fan_nr; i++) {
  139. data->fan[i] = smsc47m1_read_value(data,
  140. SMSC47M1_REG_FAN[i]);
  141. data->fan_preload[i] = smsc47m1_read_value(data,
  142. SMSC47M1_REG_FAN_PRELOAD[i]);
  143. data->pwm[i] = smsc47m1_read_value(data,
  144. SMSC47M1_REG_PWM[i]);
  145. }
  146. i = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV);
  147. data->fan_div[0] = (i >> 4) & 0x03;
  148. data->fan_div[1] = i >> 6;
  149. data->alarms = smsc47m1_read_value(data,
  150. SMSC47M1_REG_ALARM) >> 6;
  151. /* Clear alarms if needed */
  152. if (data->alarms)
  153. smsc47m1_write_value(data, SMSC47M1_REG_ALARM, 0xC0);
  154. if (fan_nr >= 3) {
  155. data->fan_div[2] = (smsc47m1_read_value(data,
  156. SMSC47M2_REG_FANDIV3) >> 4) & 0x03;
  157. data->alarms |= (smsc47m1_read_value(data,
  158. SMSC47M2_REG_ALARM6) & 0x40) >> 4;
  159. /* Clear alarm if needed */
  160. if (data->alarms & 0x04)
  161. smsc47m1_write_value(data,
  162. SMSC47M2_REG_ALARM6,
  163. 0x40);
  164. }
  165. data->last_updated = jiffies;
  166. }
  167. mutex_unlock(&data->update_lock);
  168. return data;
  169. }
  170. static ssize_t get_fan(struct device *dev, struct device_attribute
  171. *devattr, char *buf)
  172. {
  173. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  174. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  175. int nr = attr->index;
  176. /*
  177. * This chip (stupidly) stops monitoring fan speed if PWM is
  178. * enabled and duty cycle is 0%. This is fine if the monitoring
  179. * and control concern the same fan, but troublesome if they are
  180. * not (which could as well happen).
  181. */
  182. int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 :
  183. FAN_FROM_REG(data->fan[nr],
  184. DIV_FROM_REG(data->fan_div[nr]),
  185. data->fan_preload[nr]);
  186. return sprintf(buf, "%d\n", rpm);
  187. }
  188. static ssize_t get_fan_min(struct device *dev, struct device_attribute
  189. *devattr, char *buf)
  190. {
  191. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  192. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  193. int nr = attr->index;
  194. int rpm = MIN_FROM_REG(data->fan_preload[nr],
  195. DIV_FROM_REG(data->fan_div[nr]));
  196. return sprintf(buf, "%d\n", rpm);
  197. }
  198. static ssize_t get_fan_div(struct device *dev, struct device_attribute
  199. *devattr, char *buf)
  200. {
  201. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  202. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  203. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
  204. }
  205. static ssize_t get_fan_alarm(struct device *dev, struct device_attribute
  206. *devattr, char *buf)
  207. {
  208. int bitnr = to_sensor_dev_attr(devattr)->index;
  209. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  210. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  211. }
  212. static ssize_t get_pwm(struct device *dev, struct device_attribute
  213. *devattr, char *buf)
  214. {
  215. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  216. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  217. return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[attr->index]));
  218. }
  219. static ssize_t get_pwm_en(struct device *dev, struct device_attribute
  220. *devattr, char *buf)
  221. {
  222. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  223. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  224. return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[attr->index]));
  225. }
  226. static ssize_t get_alarms(struct device *dev, struct device_attribute
  227. *devattr, char *buf)
  228. {
  229. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  230. return sprintf(buf, "%d\n", data->alarms);
  231. }
  232. static ssize_t set_fan_min(struct device *dev, struct device_attribute
  233. *devattr, const char *buf, size_t count)
  234. {
  235. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  236. struct smsc47m1_data *data = dev_get_drvdata(dev);
  237. int nr = attr->index;
  238. long rpmdiv;
  239. long val;
  240. int err;
  241. err = kstrtol(buf, 10, &val);
  242. if (err)
  243. return err;
  244. mutex_lock(&data->update_lock);
  245. rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
  246. if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
  247. mutex_unlock(&data->update_lock);
  248. return -EINVAL;
  249. }
  250. data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
  251. smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
  252. data->fan_preload[nr]);
  253. mutex_unlock(&data->update_lock);
  254. return count;
  255. }
  256. /*
  257. * Note: we save and restore the fan minimum here, because its value is
  258. * determined in part by the fan clock divider. This follows the principle
  259. * of least surprise; the user doesn't expect the fan minimum to change just
  260. * because the divider changed.
  261. */
  262. static ssize_t set_fan_div(struct device *dev, struct device_attribute
  263. *devattr, const char *buf, size_t count)
  264. {
  265. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  266. struct smsc47m1_data *data = dev_get_drvdata(dev);
  267. int nr = attr->index;
  268. long new_div;
  269. int err;
  270. long tmp;
  271. u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
  272. err = kstrtol(buf, 10, &new_div);
  273. if (err)
  274. return err;
  275. if (new_div == old_div) /* No change */
  276. return count;
  277. mutex_lock(&data->update_lock);
  278. switch (new_div) {
  279. case 1:
  280. data->fan_div[nr] = 0;
  281. break;
  282. case 2:
  283. data->fan_div[nr] = 1;
  284. break;
  285. case 4:
  286. data->fan_div[nr] = 2;
  287. break;
  288. case 8:
  289. data->fan_div[nr] = 3;
  290. break;
  291. default:
  292. mutex_unlock(&data->update_lock);
  293. return -EINVAL;
  294. }
  295. switch (nr) {
  296. case 0:
  297. case 1:
  298. tmp = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV)
  299. & ~(0x03 << (4 + 2 * nr));
  300. tmp |= data->fan_div[nr] << (4 + 2 * nr);
  301. smsc47m1_write_value(data, SMSC47M1_REG_FANDIV, tmp);
  302. break;
  303. case 2:
  304. tmp = smsc47m1_read_value(data, SMSC47M2_REG_FANDIV3) & 0xCF;
  305. tmp |= data->fan_div[2] << 4;
  306. smsc47m1_write_value(data, SMSC47M2_REG_FANDIV3, tmp);
  307. break;
  308. }
  309. /* Preserve fan min */
  310. tmp = 192 - (old_div * (192 - data->fan_preload[nr])
  311. + new_div / 2) / new_div;
  312. data->fan_preload[nr] = clamp_val(tmp, 0, 191);
  313. smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
  314. data->fan_preload[nr]);
  315. mutex_unlock(&data->update_lock);
  316. return count;
  317. }
  318. static ssize_t set_pwm(struct device *dev, struct device_attribute
  319. *devattr, const char *buf, size_t count)
  320. {
  321. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  322. struct smsc47m1_data *data = dev_get_drvdata(dev);
  323. int nr = attr->index;
  324. long val;
  325. int err;
  326. err = kstrtol(buf, 10, &val);
  327. if (err)
  328. return err;
  329. if (val < 0 || val > 255)
  330. return -EINVAL;
  331. mutex_lock(&data->update_lock);
  332. data->pwm[nr] &= 0x81; /* Preserve additional bits */
  333. data->pwm[nr] |= PWM_TO_REG(val);
  334. smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
  335. data->pwm[nr]);
  336. mutex_unlock(&data->update_lock);
  337. return count;
  338. }
  339. static ssize_t set_pwm_en(struct device *dev, struct device_attribute
  340. *devattr, const char *buf, size_t count)
  341. {
  342. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  343. struct smsc47m1_data *data = dev_get_drvdata(dev);
  344. int nr = attr->index;
  345. unsigned long val;
  346. int err;
  347. err = kstrtoul(buf, 10, &val);
  348. if (err)
  349. return err;
  350. if (val > 1)
  351. return -EINVAL;
  352. mutex_lock(&data->update_lock);
  353. data->pwm[nr] &= 0xFE; /* preserve the other bits */
  354. data->pwm[nr] |= !val;
  355. smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
  356. data->pwm[nr]);
  357. mutex_unlock(&data->update_lock);
  358. return count;
  359. }
  360. #define fan_present(offset) \
  361. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan, \
  362. NULL, offset - 1); \
  363. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  364. get_fan_min, set_fan_min, offset - 1); \
  365. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  366. get_fan_div, set_fan_div, offset - 1); \
  367. static SENSOR_DEVICE_ATTR(fan##offset##_alarm, S_IRUGO, get_fan_alarm, \
  368. NULL, offset - 1); \
  369. static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
  370. get_pwm, set_pwm, offset - 1); \
  371. static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
  372. get_pwm_en, set_pwm_en, offset - 1)
  373. fan_present(1);
  374. fan_present(2);
  375. fan_present(3);
  376. static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
  377. static ssize_t show_name(struct device *dev, struct device_attribute
  378. *devattr, char *buf)
  379. {
  380. struct smsc47m1_data *data = dev_get_drvdata(dev);
  381. return sprintf(buf, "%s\n", data->name);
  382. }
  383. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  384. static struct attribute *smsc47m1_attributes_fan1[] = {
  385. &sensor_dev_attr_fan1_input.dev_attr.attr,
  386. &sensor_dev_attr_fan1_min.dev_attr.attr,
  387. &sensor_dev_attr_fan1_div.dev_attr.attr,
  388. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  389. NULL
  390. };
  391. static const struct attribute_group smsc47m1_group_fan1 = {
  392. .attrs = smsc47m1_attributes_fan1,
  393. };
  394. static struct attribute *smsc47m1_attributes_fan2[] = {
  395. &sensor_dev_attr_fan2_input.dev_attr.attr,
  396. &sensor_dev_attr_fan2_min.dev_attr.attr,
  397. &sensor_dev_attr_fan2_div.dev_attr.attr,
  398. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  399. NULL
  400. };
  401. static const struct attribute_group smsc47m1_group_fan2 = {
  402. .attrs = smsc47m1_attributes_fan2,
  403. };
  404. static struct attribute *smsc47m1_attributes_fan3[] = {
  405. &sensor_dev_attr_fan3_input.dev_attr.attr,
  406. &sensor_dev_attr_fan3_min.dev_attr.attr,
  407. &sensor_dev_attr_fan3_div.dev_attr.attr,
  408. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  409. NULL
  410. };
  411. static const struct attribute_group smsc47m1_group_fan3 = {
  412. .attrs = smsc47m1_attributes_fan3,
  413. };
  414. static struct attribute *smsc47m1_attributes_pwm1[] = {
  415. &sensor_dev_attr_pwm1.dev_attr.attr,
  416. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  417. NULL
  418. };
  419. static const struct attribute_group smsc47m1_group_pwm1 = {
  420. .attrs = smsc47m1_attributes_pwm1,
  421. };
  422. static struct attribute *smsc47m1_attributes_pwm2[] = {
  423. &sensor_dev_attr_pwm2.dev_attr.attr,
  424. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  425. NULL
  426. };
  427. static const struct attribute_group smsc47m1_group_pwm2 = {
  428. .attrs = smsc47m1_attributes_pwm2,
  429. };
  430. static struct attribute *smsc47m1_attributes_pwm3[] = {
  431. &sensor_dev_attr_pwm3.dev_attr.attr,
  432. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  433. NULL
  434. };
  435. static const struct attribute_group smsc47m1_group_pwm3 = {
  436. .attrs = smsc47m1_attributes_pwm3,
  437. };
  438. static struct attribute *smsc47m1_attributes[] = {
  439. &dev_attr_alarms.attr,
  440. &dev_attr_name.attr,
  441. NULL
  442. };
  443. static const struct attribute_group smsc47m1_group = {
  444. .attrs = smsc47m1_attributes,
  445. };
  446. static int __init smsc47m1_find(struct smsc47m1_sio_data *sio_data)
  447. {
  448. u8 val;
  449. unsigned short addr;
  450. superio_enter();
  451. val = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
  452. /*
  453. * SMSC LPC47M10x/LPC47M112/LPC47M13x (device id 0x59), LPC47M14x
  454. * (device id 0x5F) and LPC47B27x (device id 0x51) have fan control.
  455. * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
  456. * can do much more besides (device id 0x60).
  457. * The LPC47M997 is undocumented, but seems to be compatible with
  458. * the LPC47M192, and has the same device id.
  459. * The LPC47M292 (device id 0x6B) is somewhat compatible, but it
  460. * supports a 3rd fan, and the pin configuration registers are
  461. * unfortunately different.
  462. * The LPC47M233 has the same device id (0x6B) but is not compatible.
  463. * We check the high bit of the device revision register to
  464. * differentiate them.
  465. */
  466. switch (val) {
  467. case 0x51:
  468. pr_info("Found SMSC LPC47B27x\n");
  469. sio_data->type = smsc47m1;
  470. break;
  471. case 0x59:
  472. pr_info("Found SMSC LPC47M10x/LPC47M112/LPC47M13x\n");
  473. sio_data->type = smsc47m1;
  474. break;
  475. case 0x5F:
  476. pr_info("Found SMSC LPC47M14x\n");
  477. sio_data->type = smsc47m1;
  478. break;
  479. case 0x60:
  480. pr_info("Found SMSC LPC47M15x/LPC47M192/LPC47M997\n");
  481. sio_data->type = smsc47m1;
  482. break;
  483. case 0x6B:
  484. if (superio_inb(SUPERIO_REG_DEVREV) & 0x80) {
  485. pr_debug("Found SMSC LPC47M233, unsupported\n");
  486. superio_exit();
  487. return -ENODEV;
  488. }
  489. pr_info("Found SMSC LPC47M292\n");
  490. sio_data->type = smsc47m2;
  491. break;
  492. default:
  493. superio_exit();
  494. return -ENODEV;
  495. }
  496. superio_select();
  497. addr = (superio_inb(SUPERIO_REG_BASE) << 8)
  498. | superio_inb(SUPERIO_REG_BASE + 1);
  499. if (addr == 0) {
  500. pr_info("Device address not set, will not use\n");
  501. superio_exit();
  502. return -ENODEV;
  503. }
  504. /*
  505. * Enable only if address is set (needed at least on the
  506. * Compaq Presario S4000NX)
  507. */
  508. sio_data->activate = superio_inb(SUPERIO_REG_ACT);
  509. if ((sio_data->activate & 0x01) == 0) {
  510. pr_info("Enabling device\n");
  511. superio_outb(SUPERIO_REG_ACT, sio_data->activate | 0x01);
  512. }
  513. superio_exit();
  514. return addr;
  515. }
  516. /* Restore device to its initial state */
  517. static void smsc47m1_restore(const struct smsc47m1_sio_data *sio_data)
  518. {
  519. if ((sio_data->activate & 0x01) == 0) {
  520. superio_enter();
  521. superio_select();
  522. pr_info("Disabling device\n");
  523. superio_outb(SUPERIO_REG_ACT, sio_data->activate);
  524. superio_exit();
  525. }
  526. }
  527. #define CHECK 1
  528. #define REQUEST 2
  529. /*
  530. * This function can be used to:
  531. * - test for resource conflicts with ACPI
  532. * - request the resources
  533. * We only allocate the I/O ports we really need, to minimize the risk of
  534. * conflicts with ACPI or with other drivers.
  535. */
  536. static int __init smsc47m1_handle_resources(unsigned short address,
  537. enum chips type, int action,
  538. struct device *dev)
  539. {
  540. static const u8 ports_m1[] = {
  541. /* register, region length */
  542. 0x04, 1,
  543. 0x33, 4,
  544. 0x56, 7,
  545. };
  546. static const u8 ports_m2[] = {
  547. /* register, region length */
  548. 0x04, 1,
  549. 0x09, 1,
  550. 0x2c, 2,
  551. 0x35, 4,
  552. 0x56, 7,
  553. 0x69, 4,
  554. };
  555. int i, ports_size, err;
  556. const u8 *ports;
  557. switch (type) {
  558. case smsc47m1:
  559. default:
  560. ports = ports_m1;
  561. ports_size = ARRAY_SIZE(ports_m1);
  562. break;
  563. case smsc47m2:
  564. ports = ports_m2;
  565. ports_size = ARRAY_SIZE(ports_m2);
  566. break;
  567. }
  568. for (i = 0; i + 1 < ports_size; i += 2) {
  569. unsigned short start = address + ports[i];
  570. unsigned short len = ports[i + 1];
  571. switch (action) {
  572. case CHECK:
  573. /* Only check for conflicts */
  574. err = acpi_check_region(start, len, DRVNAME);
  575. if (err)
  576. return err;
  577. break;
  578. case REQUEST:
  579. /* Request the resources */
  580. if (!devm_request_region(dev, start, len, DRVNAME)) {
  581. dev_err(dev,
  582. "Region 0x%hx-0x%hx already in use!\n",
  583. start, start + len);
  584. return -EBUSY;
  585. }
  586. break;
  587. }
  588. }
  589. return 0;
  590. }
  591. static void smsc47m1_remove_files(struct device *dev)
  592. {
  593. sysfs_remove_group(&dev->kobj, &smsc47m1_group);
  594. sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan1);
  595. sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan2);
  596. sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan3);
  597. sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm1);
  598. sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm2);
  599. sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm3);
  600. }
  601. static int __init smsc47m1_probe(struct platform_device *pdev)
  602. {
  603. struct device *dev = &pdev->dev;
  604. struct smsc47m1_sio_data *sio_data = dev_get_platdata(dev);
  605. struct smsc47m1_data *data;
  606. struct resource *res;
  607. int err;
  608. int fan1, fan2, fan3, pwm1, pwm2, pwm3;
  609. static const char * const names[] = {
  610. "smsc47m1",
  611. "smsc47m2",
  612. };
  613. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  614. err = smsc47m1_handle_resources(res->start, sio_data->type,
  615. REQUEST, dev);
  616. if (err < 0)
  617. return err;
  618. data = devm_kzalloc(dev, sizeof(struct smsc47m1_data), GFP_KERNEL);
  619. if (!data)
  620. return -ENOMEM;
  621. data->addr = res->start;
  622. data->type = sio_data->type;
  623. data->name = names[sio_data->type];
  624. mutex_init(&data->update_lock);
  625. platform_set_drvdata(pdev, data);
  626. /*
  627. * If no function is properly configured, there's no point in
  628. * actually registering the chip.
  629. */
  630. pwm1 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(0)) & 0x05)
  631. == 0x04;
  632. pwm2 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(1)) & 0x05)
  633. == 0x04;
  634. if (data->type == smsc47m2) {
  635. fan1 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN1)
  636. & 0x0d) == 0x09;
  637. fan2 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN2)
  638. & 0x0d) == 0x09;
  639. fan3 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN3)
  640. & 0x0d) == 0x0d;
  641. pwm3 = (smsc47m1_read_value(data, SMSC47M2_REG_PPIN3)
  642. & 0x0d) == 0x08;
  643. } else {
  644. fan1 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(0))
  645. & 0x05) == 0x05;
  646. fan2 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(1))
  647. & 0x05) == 0x05;
  648. fan3 = 0;
  649. pwm3 = 0;
  650. }
  651. if (!(fan1 || fan2 || fan3 || pwm1 || pwm2 || pwm3)) {
  652. dev_warn(dev, "Device not configured, will not use\n");
  653. return -ENODEV;
  654. }
  655. /*
  656. * Some values (fan min, clock dividers, pwm registers) may be
  657. * needed before any update is triggered, so we better read them
  658. * at least once here. We don't usually do it that way, but in
  659. * this particular case, manually reading 5 registers out of 8
  660. * doesn't make much sense and we're better using the existing
  661. * function.
  662. */
  663. smsc47m1_update_device(dev, 1);
  664. /* Register sysfs hooks */
  665. if (fan1) {
  666. err = sysfs_create_group(&dev->kobj,
  667. &smsc47m1_group_fan1);
  668. if (err)
  669. goto error_remove_files;
  670. } else
  671. dev_dbg(dev, "Fan 1 not enabled by hardware, skipping\n");
  672. if (fan2) {
  673. err = sysfs_create_group(&dev->kobj,
  674. &smsc47m1_group_fan2);
  675. if (err)
  676. goto error_remove_files;
  677. } else
  678. dev_dbg(dev, "Fan 2 not enabled by hardware, skipping\n");
  679. if (fan3) {
  680. err = sysfs_create_group(&dev->kobj,
  681. &smsc47m1_group_fan3);
  682. if (err)
  683. goto error_remove_files;
  684. } else if (data->type == smsc47m2)
  685. dev_dbg(dev, "Fan 3 not enabled by hardware, skipping\n");
  686. if (pwm1) {
  687. err = sysfs_create_group(&dev->kobj,
  688. &smsc47m1_group_pwm1);
  689. if (err)
  690. goto error_remove_files;
  691. } else
  692. dev_dbg(dev, "PWM 1 not enabled by hardware, skipping\n");
  693. if (pwm2) {
  694. err = sysfs_create_group(&dev->kobj,
  695. &smsc47m1_group_pwm2);
  696. if (err)
  697. goto error_remove_files;
  698. } else
  699. dev_dbg(dev, "PWM 2 not enabled by hardware, skipping\n");
  700. if (pwm3) {
  701. err = sysfs_create_group(&dev->kobj,
  702. &smsc47m1_group_pwm3);
  703. if (err)
  704. goto error_remove_files;
  705. } else if (data->type == smsc47m2)
  706. dev_dbg(dev, "PWM 3 not enabled by hardware, skipping\n");
  707. err = sysfs_create_group(&dev->kobj, &smsc47m1_group);
  708. if (err)
  709. goto error_remove_files;
  710. data->hwmon_dev = hwmon_device_register(dev);
  711. if (IS_ERR(data->hwmon_dev)) {
  712. err = PTR_ERR(data->hwmon_dev);
  713. goto error_remove_files;
  714. }
  715. return 0;
  716. error_remove_files:
  717. smsc47m1_remove_files(dev);
  718. return err;
  719. }
  720. static int __exit smsc47m1_remove(struct platform_device *pdev)
  721. {
  722. struct smsc47m1_data *data = platform_get_drvdata(pdev);
  723. hwmon_device_unregister(data->hwmon_dev);
  724. smsc47m1_remove_files(&pdev->dev);
  725. return 0;
  726. }
  727. static struct platform_driver smsc47m1_driver = {
  728. .driver = {
  729. .name = DRVNAME,
  730. },
  731. .remove = __exit_p(smsc47m1_remove),
  732. };
  733. static int __init smsc47m1_device_add(unsigned short address,
  734. const struct smsc47m1_sio_data *sio_data)
  735. {
  736. struct resource res = {
  737. .start = address,
  738. .end = address + SMSC_EXTENT - 1,
  739. .name = DRVNAME,
  740. .flags = IORESOURCE_IO,
  741. };
  742. int err;
  743. err = smsc47m1_handle_resources(address, sio_data->type, CHECK, NULL);
  744. if (err)
  745. goto exit;
  746. pdev = platform_device_alloc(DRVNAME, address);
  747. if (!pdev) {
  748. err = -ENOMEM;
  749. pr_err("Device allocation failed\n");
  750. goto exit;
  751. }
  752. err = platform_device_add_resources(pdev, &res, 1);
  753. if (err) {
  754. pr_err("Device resource addition failed (%d)\n", err);
  755. goto exit_device_put;
  756. }
  757. err = platform_device_add_data(pdev, sio_data,
  758. sizeof(struct smsc47m1_sio_data));
  759. if (err) {
  760. pr_err("Platform data allocation failed\n");
  761. goto exit_device_put;
  762. }
  763. err = platform_device_add(pdev);
  764. if (err) {
  765. pr_err("Device addition failed (%d)\n", err);
  766. goto exit_device_put;
  767. }
  768. return 0;
  769. exit_device_put:
  770. platform_device_put(pdev);
  771. exit:
  772. return err;
  773. }
  774. static int __init sm_smsc47m1_init(void)
  775. {
  776. int err;
  777. unsigned short address;
  778. struct smsc47m1_sio_data sio_data;
  779. err = smsc47m1_find(&sio_data);
  780. if (err < 0)
  781. return err;
  782. address = err;
  783. /* Sets global pdev as a side effect */
  784. err = smsc47m1_device_add(address, &sio_data);
  785. if (err)
  786. return err;
  787. err = platform_driver_probe(&smsc47m1_driver, smsc47m1_probe);
  788. if (err)
  789. goto exit_device;
  790. return 0;
  791. exit_device:
  792. platform_device_unregister(pdev);
  793. smsc47m1_restore(&sio_data);
  794. return err;
  795. }
  796. static void __exit sm_smsc47m1_exit(void)
  797. {
  798. platform_driver_unregister(&smsc47m1_driver);
  799. smsc47m1_restore(dev_get_platdata(&pdev->dev));
  800. platform_device_unregister(pdev);
  801. }
  802. MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
  803. MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
  804. MODULE_LICENSE("GPL");
  805. module_init(sm_smsc47m1_init);
  806. module_exit(sm_smsc47m1_exit);