smsc47m1.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. smsc47m1.c - Part of lm_sensors, Linux kernel modules
  3. for hardware monitoring
  4. Supports the SMSC LPC47B27x, LPC47M10x, LPC47M112, LPC47M13x,
  5. LPC47M14x, LPC47M15x, LPC47M192, LPC47M292 and LPC47M997
  6. Super-I/O chips.
  7. Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
  8. Copyright (C) 2004-2007 Jean Delvare <khali@linux-fr.org>
  9. Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
  10. and Jean Delvare
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/ioport.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/i2c.h>
  28. #include <linux/i2c-isa.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/err.h>
  31. #include <linux/init.h>
  32. #include <linux/mutex.h>
  33. #include <linux/sysfs.h>
  34. #include <asm/io.h>
  35. /* Address is autodetected, there is no default value */
  36. static unsigned short address;
  37. static u8 devid;
  38. enum chips { smsc47m1, smsc47m2 };
  39. /* Super-I/0 registers and commands */
  40. #define REG 0x2e /* The register to read/write */
  41. #define VAL 0x2f /* The value to read/write */
  42. static inline void
  43. superio_outb(int reg, int val)
  44. {
  45. outb(reg, REG);
  46. outb(val, VAL);
  47. }
  48. static inline int
  49. superio_inb(int reg)
  50. {
  51. outb(reg, REG);
  52. return inb(VAL);
  53. }
  54. /* logical device for fans is 0x0A */
  55. #define superio_select() superio_outb(0x07, 0x0A)
  56. static inline void
  57. superio_enter(void)
  58. {
  59. outb(0x55, REG);
  60. }
  61. static inline void
  62. superio_exit(void)
  63. {
  64. outb(0xAA, REG);
  65. }
  66. #define SUPERIO_REG_ACT 0x30
  67. #define SUPERIO_REG_BASE 0x60
  68. #define SUPERIO_REG_DEVID 0x20
  69. /* Logical device registers */
  70. #define SMSC_EXTENT 0x80
  71. /* nr is 0 or 1 in the macros below */
  72. #define SMSC47M1_REG_ALARM 0x04
  73. #define SMSC47M1_REG_TPIN(nr) (0x34 - (nr))
  74. #define SMSC47M1_REG_PPIN(nr) (0x36 - (nr))
  75. #define SMSC47M1_REG_FANDIV 0x58
  76. static const u8 SMSC47M1_REG_FAN[3] = { 0x59, 0x5a, 0x6b };
  77. static const u8 SMSC47M1_REG_FAN_PRELOAD[3] = { 0x5b, 0x5c, 0x6c };
  78. static const u8 SMSC47M1_REG_PWM[3] = { 0x56, 0x57, 0x69 };
  79. #define SMSC47M2_REG_ALARM6 0x09
  80. #define SMSC47M2_REG_TPIN1 0x38
  81. #define SMSC47M2_REG_TPIN2 0x37
  82. #define SMSC47M2_REG_TPIN3 0x2d
  83. #define SMSC47M2_REG_PPIN3 0x2c
  84. #define SMSC47M2_REG_FANDIV3 0x6a
  85. #define MIN_FROM_REG(reg,div) ((reg)>=192 ? 0 : \
  86. 983040/((192-(reg))*(div)))
  87. #define FAN_FROM_REG(reg,div,preload) ((reg)<=(preload) || (reg)==255 ? 0 : \
  88. 983040/(((reg)-(preload))*(div)))
  89. #define DIV_FROM_REG(reg) (1 << (reg))
  90. #define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1)
  91. #define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01)
  92. #define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E)
  93. struct smsc47m1_data {
  94. struct i2c_client client;
  95. enum chips type;
  96. struct class_device *class_dev;
  97. struct mutex update_lock;
  98. unsigned long last_updated; /* In jiffies */
  99. u8 fan[3]; /* Register value */
  100. u8 fan_preload[3]; /* Register value */
  101. u8 fan_div[3]; /* Register encoding, shifted right */
  102. u8 alarms; /* Register encoding */
  103. u8 pwm[3]; /* Register value (bit 0 is disable) */
  104. };
  105. static int smsc47m1_detect(struct i2c_adapter *adapter);
  106. static int smsc47m1_detach_client(struct i2c_client *client);
  107. static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
  108. int init);
  109. static inline int smsc47m1_read_value(struct i2c_client *client, u8 reg)
  110. {
  111. return inb_p(client->addr + reg);
  112. }
  113. static inline void smsc47m1_write_value(struct i2c_client *client, u8 reg,
  114. u8 value)
  115. {
  116. outb_p(value, client->addr + reg);
  117. }
  118. static struct i2c_driver smsc47m1_driver = {
  119. .driver = {
  120. .owner = THIS_MODULE,
  121. .name = "smsc47m1",
  122. },
  123. .attach_adapter = smsc47m1_detect,
  124. .detach_client = smsc47m1_detach_client,
  125. };
  126. /* nr is 0 or 1 in the callback functions below */
  127. static ssize_t get_fan(struct device *dev, char *buf, int nr)
  128. {
  129. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  130. /* This chip (stupidly) stops monitoring fan speed if PWM is
  131. enabled and duty cycle is 0%. This is fine if the monitoring
  132. and control concern the same fan, but troublesome if they are
  133. not (which could as well happen). */
  134. int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 :
  135. FAN_FROM_REG(data->fan[nr],
  136. DIV_FROM_REG(data->fan_div[nr]),
  137. data->fan_preload[nr]);
  138. return sprintf(buf, "%d\n", rpm);
  139. }
  140. static ssize_t get_fan_min(struct device *dev, char *buf, int nr)
  141. {
  142. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  143. int rpm = MIN_FROM_REG(data->fan_preload[nr],
  144. DIV_FROM_REG(data->fan_div[nr]));
  145. return sprintf(buf, "%d\n", rpm);
  146. }
  147. static ssize_t get_fan_div(struct device *dev, char *buf, int nr)
  148. {
  149. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  150. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  151. }
  152. static ssize_t get_pwm(struct device *dev, char *buf, int nr)
  153. {
  154. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  155. return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
  156. }
  157. static ssize_t get_pwm_en(struct device *dev, char *buf, int nr)
  158. {
  159. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  160. return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[nr]));
  161. }
  162. static ssize_t get_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  163. {
  164. struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
  165. return sprintf(buf, "%d\n", data->alarms);
  166. }
  167. static ssize_t set_fan_min(struct device *dev, const char *buf,
  168. size_t count, int nr)
  169. {
  170. struct i2c_client *client = to_i2c_client(dev);
  171. struct smsc47m1_data *data = i2c_get_clientdata(client);
  172. long rpmdiv, val = simple_strtol(buf, NULL, 10);
  173. mutex_lock(&data->update_lock);
  174. rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
  175. if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
  176. mutex_unlock(&data->update_lock);
  177. return -EINVAL;
  178. }
  179. data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
  180. smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD[nr],
  181. data->fan_preload[nr]);
  182. mutex_unlock(&data->update_lock);
  183. return count;
  184. }
  185. /* Note: we save and restore the fan minimum here, because its value is
  186. determined in part by the fan clock divider. This follows the principle
  187. of least surprise; the user doesn't expect the fan minimum to change just
  188. because the divider changed. */
  189. static ssize_t set_fan_div(struct device *dev, const char *buf,
  190. size_t count, int nr)
  191. {
  192. struct i2c_client *client = to_i2c_client(dev);
  193. struct smsc47m1_data *data = i2c_get_clientdata(client);
  194. long new_div = simple_strtol(buf, NULL, 10), tmp;
  195. u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
  196. if (new_div == old_div) /* No change */
  197. return count;
  198. mutex_lock(&data->update_lock);
  199. switch (new_div) {
  200. case 1: data->fan_div[nr] = 0; break;
  201. case 2: data->fan_div[nr] = 1; break;
  202. case 4: data->fan_div[nr] = 2; break;
  203. case 8: data->fan_div[nr] = 3; break;
  204. default:
  205. mutex_unlock(&data->update_lock);
  206. return -EINVAL;
  207. }
  208. switch (nr) {
  209. case 0:
  210. case 1:
  211. tmp = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV)
  212. & ~(0x03 << (4 + 2 * nr));
  213. tmp |= data->fan_div[nr] << (4 + 2 * nr);
  214. smsc47m1_write_value(client, SMSC47M1_REG_FANDIV, tmp);
  215. break;
  216. case 2:
  217. tmp = smsc47m1_read_value(client, SMSC47M2_REG_FANDIV3) & 0xCF;
  218. tmp |= data->fan_div[2] << 4;
  219. smsc47m1_write_value(client, SMSC47M2_REG_FANDIV3, tmp);
  220. break;
  221. }
  222. /* Preserve fan min */
  223. tmp = 192 - (old_div * (192 - data->fan_preload[nr])
  224. + new_div / 2) / new_div;
  225. data->fan_preload[nr] = SENSORS_LIMIT(tmp, 0, 191);
  226. smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD[nr],
  227. data->fan_preload[nr]);
  228. mutex_unlock(&data->update_lock);
  229. return count;
  230. }
  231. static ssize_t set_pwm(struct device *dev, const char *buf,
  232. size_t count, int nr)
  233. {
  234. struct i2c_client *client = to_i2c_client(dev);
  235. struct smsc47m1_data *data = i2c_get_clientdata(client);
  236. long val = simple_strtol(buf, NULL, 10);
  237. if (val < 0 || val > 255)
  238. return -EINVAL;
  239. mutex_lock(&data->update_lock);
  240. data->pwm[nr] &= 0x81; /* Preserve additional bits */
  241. data->pwm[nr] |= PWM_TO_REG(val);
  242. smsc47m1_write_value(client, SMSC47M1_REG_PWM[nr],
  243. data->pwm[nr]);
  244. mutex_unlock(&data->update_lock);
  245. return count;
  246. }
  247. static ssize_t set_pwm_en(struct device *dev, const char *buf,
  248. size_t count, int nr)
  249. {
  250. struct i2c_client *client = to_i2c_client(dev);
  251. struct smsc47m1_data *data = i2c_get_clientdata(client);
  252. long val = simple_strtol(buf, NULL, 10);
  253. if (val != 0 && val != 1)
  254. return -EINVAL;
  255. mutex_lock(&data->update_lock);
  256. data->pwm[nr] &= 0xFE; /* preserve the other bits */
  257. data->pwm[nr] |= !val;
  258. smsc47m1_write_value(client, SMSC47M1_REG_PWM[nr],
  259. data->pwm[nr]);
  260. mutex_unlock(&data->update_lock);
  261. return count;
  262. }
  263. #define fan_present(offset) \
  264. static ssize_t get_fan##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  265. { \
  266. return get_fan(dev, buf, offset - 1); \
  267. } \
  268. static ssize_t get_fan##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
  269. { \
  270. return get_fan_min(dev, buf, offset - 1); \
  271. } \
  272. static ssize_t set_fan##offset##_min (struct device *dev, struct device_attribute *attr, \
  273. const char *buf, size_t count) \
  274. { \
  275. return set_fan_min(dev, buf, count, offset - 1); \
  276. } \
  277. static ssize_t get_fan##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
  278. { \
  279. return get_fan_div(dev, buf, offset - 1); \
  280. } \
  281. static ssize_t set_fan##offset##_div (struct device *dev, struct device_attribute *attr, \
  282. const char *buf, size_t count) \
  283. { \
  284. return set_fan_div(dev, buf, count, offset - 1); \
  285. } \
  286. static ssize_t get_pwm##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  287. { \
  288. return get_pwm(dev, buf, offset - 1); \
  289. } \
  290. static ssize_t set_pwm##offset (struct device *dev, struct device_attribute *attr, \
  291. const char *buf, size_t count) \
  292. { \
  293. return set_pwm(dev, buf, count, offset - 1); \
  294. } \
  295. static ssize_t get_pwm##offset##_en (struct device *dev, struct device_attribute *attr, char *buf) \
  296. { \
  297. return get_pwm_en(dev, buf, offset - 1); \
  298. } \
  299. static ssize_t set_pwm##offset##_en (struct device *dev, struct device_attribute *attr, \
  300. const char *buf, size_t count) \
  301. { \
  302. return set_pwm_en(dev, buf, count, offset - 1); \
  303. } \
  304. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan##offset, \
  305. NULL); \
  306. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  307. get_fan##offset##_min, set_fan##offset##_min); \
  308. static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  309. get_fan##offset##_div, set_fan##offset##_div); \
  310. static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
  311. get_pwm##offset, set_pwm##offset); \
  312. static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
  313. get_pwm##offset##_en, set_pwm##offset##_en);
  314. fan_present(1);
  315. fan_present(2);
  316. fan_present(3);
  317. static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
  318. /* Almost all sysfs files may or may not be created depending on the chip
  319. setup so we create them individually. It is still convenient to define a
  320. group to remove them all at once. */
  321. static struct attribute *smsc47m1_attributes[] = {
  322. &dev_attr_fan1_input.attr,
  323. &dev_attr_fan1_min.attr,
  324. &dev_attr_fan1_div.attr,
  325. &dev_attr_fan2_input.attr,
  326. &dev_attr_fan2_min.attr,
  327. &dev_attr_fan2_div.attr,
  328. &dev_attr_fan3_input.attr,
  329. &dev_attr_fan3_min.attr,
  330. &dev_attr_fan3_div.attr,
  331. &dev_attr_pwm1.attr,
  332. &dev_attr_pwm1_enable.attr,
  333. &dev_attr_pwm2.attr,
  334. &dev_attr_pwm2_enable.attr,
  335. &dev_attr_pwm3.attr,
  336. &dev_attr_pwm3_enable.attr,
  337. &dev_attr_alarms.attr,
  338. NULL
  339. };
  340. static const struct attribute_group smsc47m1_group = {
  341. .attrs = smsc47m1_attributes,
  342. };
  343. static int __init smsc47m1_find(unsigned short *addr)
  344. {
  345. u8 val;
  346. superio_enter();
  347. devid = superio_inb(SUPERIO_REG_DEVID);
  348. /*
  349. * SMSC LPC47M10x/LPC47M112/LPC47M13x (device id 0x59), LPC47M14x
  350. * (device id 0x5F) and LPC47B27x (device id 0x51) have fan control.
  351. * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
  352. * can do much more besides (device id 0x60).
  353. * The LPC47M997 is undocumented, but seems to be compatible with
  354. * the LPC47M192, and has the same device id.
  355. * The LPC47M292 (device id 0x6B) is somewhat compatible, but it
  356. * supports a 3rd fan, and the pin configuration registers are
  357. * unfortunately different.
  358. */
  359. switch (devid) {
  360. case 0x51:
  361. printk(KERN_INFO "smsc47m1: Found SMSC LPC47B27x\n");
  362. break;
  363. case 0x59:
  364. printk(KERN_INFO "smsc47m1: Found SMSC "
  365. "LPC47M10x/LPC47M112/LPC47M13x\n");
  366. break;
  367. case 0x5F:
  368. printk(KERN_INFO "smsc47m1: Found SMSC LPC47M14x\n");
  369. break;
  370. case 0x60:
  371. printk(KERN_INFO "smsc47m1: Found SMSC "
  372. "LPC47M15x/LPC47M192/LPC47M997\n");
  373. break;
  374. case 0x6B:
  375. printk(KERN_INFO "smsc47m1: Found SMSC LPC47M292\n");
  376. break;
  377. default:
  378. superio_exit();
  379. return -ENODEV;
  380. }
  381. superio_select();
  382. *addr = (superio_inb(SUPERIO_REG_BASE) << 8)
  383. | superio_inb(SUPERIO_REG_BASE + 1);
  384. val = superio_inb(SUPERIO_REG_ACT);
  385. if (*addr == 0 || (val & 0x01) == 0) {
  386. printk(KERN_INFO "smsc47m1: Device is disabled, will not use\n");
  387. superio_exit();
  388. return -ENODEV;
  389. }
  390. superio_exit();
  391. return 0;
  392. }
  393. static int smsc47m1_detect(struct i2c_adapter *adapter)
  394. {
  395. struct i2c_client *new_client;
  396. struct smsc47m1_data *data;
  397. int err = 0;
  398. int fan1, fan2, fan3, pwm1, pwm2, pwm3;
  399. if (!request_region(address, SMSC_EXTENT, smsc47m1_driver.driver.name)) {
  400. dev_err(&adapter->dev, "Region 0x%x already in use!\n", address);
  401. return -EBUSY;
  402. }
  403. if (!(data = kzalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) {
  404. err = -ENOMEM;
  405. goto error_release;
  406. }
  407. data->type = devid == 0x6B ? smsc47m2 : smsc47m1;
  408. new_client = &data->client;
  409. i2c_set_clientdata(new_client, data);
  410. new_client->addr = address;
  411. new_client->adapter = adapter;
  412. new_client->driver = &smsc47m1_driver;
  413. new_client->flags = 0;
  414. strlcpy(new_client->name,
  415. data->type == smsc47m2 ? "smsc47m2" : "smsc47m1",
  416. I2C_NAME_SIZE);
  417. mutex_init(&data->update_lock);
  418. /* If no function is properly configured, there's no point in
  419. actually registering the chip. */
  420. pwm1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(0)) & 0x05)
  421. == 0x04;
  422. pwm2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(1)) & 0x05)
  423. == 0x04;
  424. if (data->type == smsc47m2) {
  425. fan1 = (smsc47m1_read_value(new_client, SMSC47M2_REG_TPIN1)
  426. & 0x0d) == 0x09;
  427. fan2 = (smsc47m1_read_value(new_client, SMSC47M2_REG_TPIN2)
  428. & 0x0d) == 0x09;
  429. fan3 = (smsc47m1_read_value(new_client, SMSC47M2_REG_TPIN3)
  430. & 0x0d) == 0x0d;
  431. pwm3 = (smsc47m1_read_value(new_client, SMSC47M2_REG_PPIN3)
  432. & 0x0d) == 0x08;
  433. } else {
  434. fan1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(0))
  435. & 0x05) == 0x05;
  436. fan2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(1))
  437. & 0x05) == 0x05;
  438. fan3 = 0;
  439. pwm3 = 0;
  440. }
  441. if (!(fan1 || fan2 || fan3 || pwm1 || pwm2 || pwm3)) {
  442. dev_warn(&adapter->dev, "Device at 0x%x is not configured, "
  443. "will not use\n", new_client->addr);
  444. err = -ENODEV;
  445. goto error_free;
  446. }
  447. if ((err = i2c_attach_client(new_client)))
  448. goto error_free;
  449. /* Some values (fan min, clock dividers, pwm registers) may be
  450. needed before any update is triggered, so we better read them
  451. at least once here. We don't usually do it that way, but in
  452. this particular case, manually reading 5 registers out of 8
  453. doesn't make much sense and we're better using the existing
  454. function. */
  455. smsc47m1_update_device(&new_client->dev, 1);
  456. /* Register sysfs hooks */
  457. if (fan1) {
  458. if ((err = device_create_file(&new_client->dev,
  459. &dev_attr_fan1_input))
  460. || (err = device_create_file(&new_client->dev,
  461. &dev_attr_fan1_min))
  462. || (err = device_create_file(&new_client->dev,
  463. &dev_attr_fan1_div)))
  464. goto error_remove_files;
  465. } else
  466. dev_dbg(&new_client->dev, "Fan 1 not enabled by hardware, "
  467. "skipping\n");
  468. if (fan2) {
  469. if ((err = device_create_file(&new_client->dev,
  470. &dev_attr_fan2_input))
  471. || (err = device_create_file(&new_client->dev,
  472. &dev_attr_fan2_min))
  473. || (err = device_create_file(&new_client->dev,
  474. &dev_attr_fan2_div)))
  475. goto error_remove_files;
  476. } else
  477. dev_dbg(&new_client->dev, "Fan 2 not enabled by hardware, "
  478. "skipping\n");
  479. if (fan3) {
  480. if ((err = device_create_file(&new_client->dev,
  481. &dev_attr_fan3_input))
  482. || (err = device_create_file(&new_client->dev,
  483. &dev_attr_fan3_min))
  484. || (err = device_create_file(&new_client->dev,
  485. &dev_attr_fan3_div)))
  486. goto error_remove_files;
  487. } else
  488. dev_dbg(&new_client->dev, "Fan 3 not enabled by hardware, "
  489. "skipping\n");
  490. if (pwm1) {
  491. if ((err = device_create_file(&new_client->dev,
  492. &dev_attr_pwm1))
  493. || (err = device_create_file(&new_client->dev,
  494. &dev_attr_pwm1_enable)))
  495. goto error_remove_files;
  496. } else
  497. dev_dbg(&new_client->dev, "PWM 1 not enabled by hardware, "
  498. "skipping\n");
  499. if (pwm2) {
  500. if ((err = device_create_file(&new_client->dev,
  501. &dev_attr_pwm2))
  502. || (err = device_create_file(&new_client->dev,
  503. &dev_attr_pwm2_enable)))
  504. goto error_remove_files;
  505. } else
  506. dev_dbg(&new_client->dev, "PWM 2 not enabled by hardware, "
  507. "skipping\n");
  508. if (pwm3) {
  509. if ((err = device_create_file(&new_client->dev,
  510. &dev_attr_pwm3))
  511. || (err = device_create_file(&new_client->dev,
  512. &dev_attr_pwm3_enable)))
  513. goto error_remove_files;
  514. } else
  515. dev_dbg(&new_client->dev, "PWM 3 not enabled by hardware, "
  516. "skipping\n");
  517. if ((err = device_create_file(&new_client->dev, &dev_attr_alarms)))
  518. goto error_remove_files;
  519. data->class_dev = hwmon_device_register(&new_client->dev);
  520. if (IS_ERR(data->class_dev)) {
  521. err = PTR_ERR(data->class_dev);
  522. goto error_remove_files;
  523. }
  524. return 0;
  525. error_remove_files:
  526. sysfs_remove_group(&new_client->dev.kobj, &smsc47m1_group);
  527. i2c_detach_client(new_client);
  528. error_free:
  529. kfree(data);
  530. error_release:
  531. release_region(address, SMSC_EXTENT);
  532. return err;
  533. }
  534. static int smsc47m1_detach_client(struct i2c_client *client)
  535. {
  536. struct smsc47m1_data *data = i2c_get_clientdata(client);
  537. int err;
  538. hwmon_device_unregister(data->class_dev);
  539. sysfs_remove_group(&client->dev.kobj, &smsc47m1_group);
  540. if ((err = i2c_detach_client(client)))
  541. return err;
  542. release_region(client->addr, SMSC_EXTENT);
  543. kfree(data);
  544. return 0;
  545. }
  546. static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
  547. int init)
  548. {
  549. struct i2c_client *client = to_i2c_client(dev);
  550. struct smsc47m1_data *data = i2c_get_clientdata(client);
  551. mutex_lock(&data->update_lock);
  552. if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) {
  553. int i, fan_nr;
  554. fan_nr = data->type == smsc47m2 ? 3 : 2;
  555. for (i = 0; i < fan_nr; i++) {
  556. data->fan[i] = smsc47m1_read_value(client,
  557. SMSC47M1_REG_FAN[i]);
  558. data->fan_preload[i] = smsc47m1_read_value(client,
  559. SMSC47M1_REG_FAN_PRELOAD[i]);
  560. data->pwm[i] = smsc47m1_read_value(client,
  561. SMSC47M1_REG_PWM[i]);
  562. }
  563. i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
  564. data->fan_div[0] = (i >> 4) & 0x03;
  565. data->fan_div[1] = i >> 6;
  566. data->alarms = smsc47m1_read_value(client,
  567. SMSC47M1_REG_ALARM) >> 6;
  568. /* Clear alarms if needed */
  569. if (data->alarms)
  570. smsc47m1_write_value(client, SMSC47M1_REG_ALARM, 0xC0);
  571. if (fan_nr >= 3) {
  572. data->fan_div[2] = (smsc47m1_read_value(client,
  573. SMSC47M2_REG_FANDIV3) >> 4) & 0x03;
  574. data->alarms |= (smsc47m1_read_value(client,
  575. SMSC47M2_REG_ALARM6) & 0x40) >> 4;
  576. /* Clear alarm if needed */
  577. if (data->alarms & 0x04)
  578. smsc47m1_write_value(client,
  579. SMSC47M2_REG_ALARM6,
  580. 0x40);
  581. }
  582. data->last_updated = jiffies;
  583. }
  584. mutex_unlock(&data->update_lock);
  585. return data;
  586. }
  587. static int __init sm_smsc47m1_init(void)
  588. {
  589. if (smsc47m1_find(&address)) {
  590. return -ENODEV;
  591. }
  592. return i2c_isa_add_driver(&smsc47m1_driver);
  593. }
  594. static void __exit sm_smsc47m1_exit(void)
  595. {
  596. i2c_isa_del_driver(&smsc47m1_driver);
  597. }
  598. MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
  599. MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
  600. MODULE_LICENSE("GPL");
  601. module_init(sm_smsc47m1_init);
  602. module_exit(sm_smsc47m1_exit);