sis5595.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. * sis5595.c - Part of lm_sensors, Linux kernel modules
  3. * for hardware monitoring
  4. *
  5. * Copyright (C) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
  6. * Kyösti Mälkki <kmalkki@cc.hut.fi>, and
  7. * Mark D. Studebaker <mdsxyz123@yahoo.com>
  8. * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
  9. * the help of Jean Delvare <khali@linux-fr.org>
  10. *
  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. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*
  26. * SiS southbridge has a LM78-like chip integrated on the same IC.
  27. * This driver is a customized copy of lm78.c
  28. *
  29. * Supports following revisions:
  30. * Version PCI ID PCI Revision
  31. * 1 1039/0008 AF or less
  32. * 2 1039/0008 B0 or greater
  33. *
  34. * Note: these chips contain a 0008 device which is incompatible with the
  35. * 5595. We recognize these by the presence of the listed
  36. * "blacklist" PCI ID and refuse to load.
  37. *
  38. * NOT SUPPORTED PCI ID BLACKLIST PCI ID
  39. * 540 0008 0540
  40. * 550 0008 0550
  41. * 5513 0008 5511
  42. * 5581 0008 5597
  43. * 5582 0008 5597
  44. * 5597 0008 5597
  45. * 5598 0008 5597/5598
  46. * 630 0008 0630
  47. * 645 0008 0645
  48. * 730 0008 0730
  49. * 735 0008 0735
  50. */
  51. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  52. #include <linux/module.h>
  53. #include <linux/slab.h>
  54. #include <linux/ioport.h>
  55. #include <linux/pci.h>
  56. #include <linux/platform_device.h>
  57. #include <linux/hwmon.h>
  58. #include <linux/hwmon-sysfs.h>
  59. #include <linux/err.h>
  60. #include <linux/init.h>
  61. #include <linux/jiffies.h>
  62. #include <linux/mutex.h>
  63. #include <linux/sysfs.h>
  64. #include <linux/acpi.h>
  65. #include <linux/io.h>
  66. /*
  67. * If force_addr is set to anything different from 0, we forcibly enable
  68. * the device at the given address.
  69. */
  70. static u16 force_addr;
  71. module_param(force_addr, ushort, 0);
  72. MODULE_PARM_DESC(force_addr,
  73. "Initialize the base address of the sensors");
  74. static struct platform_device *pdev;
  75. /* Many SIS5595 constants specified below */
  76. /* Length of ISA address segment */
  77. #define SIS5595_EXTENT 8
  78. /* PCI Config Registers */
  79. #define SIS5595_BASE_REG 0x68
  80. #define SIS5595_PIN_REG 0x7A
  81. #define SIS5595_ENABLE_REG 0x7B
  82. /* Where are the ISA address/data registers relative to the base address */
  83. #define SIS5595_ADDR_REG_OFFSET 5
  84. #define SIS5595_DATA_REG_OFFSET 6
  85. /* The SIS5595 registers */
  86. #define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
  87. #define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
  88. #define SIS5595_REG_IN(nr) (0x20 + (nr))
  89. #define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
  90. #define SIS5595_REG_FAN(nr) (0x28 + (nr))
  91. /*
  92. * On the first version of the chip, the temp registers are separate.
  93. * On the second version,
  94. * TEMP pin is shared with IN4, configured in PCI register 0x7A.
  95. * The registers are the same as well.
  96. * OVER and HYST are really MAX and MIN.
  97. */
  98. #define REV2MIN 0xb0
  99. #define SIS5595_REG_TEMP (((data->revision) >= REV2MIN) ? \
  100. SIS5595_REG_IN(4) : 0x27)
  101. #define SIS5595_REG_TEMP_OVER (((data->revision) >= REV2MIN) ? \
  102. SIS5595_REG_IN_MAX(4) : 0x39)
  103. #define SIS5595_REG_TEMP_HYST (((data->revision) >= REV2MIN) ? \
  104. SIS5595_REG_IN_MIN(4) : 0x3a)
  105. #define SIS5595_REG_CONFIG 0x40
  106. #define SIS5595_REG_ALARM1 0x41
  107. #define SIS5595_REG_ALARM2 0x42
  108. #define SIS5595_REG_FANDIV 0x47
  109. /*
  110. * Conversions. Limit checking is only done on the TO_REG
  111. * variants.
  112. */
  113. /*
  114. * IN: mV, (0V to 4.08V)
  115. * REG: 16mV/bit
  116. */
  117. static inline u8 IN_TO_REG(unsigned long val)
  118. {
  119. unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
  120. return (nval + 8) / 16;
  121. }
  122. #define IN_FROM_REG(val) ((val) * 16)
  123. static inline u8 FAN_TO_REG(long rpm, int div)
  124. {
  125. if (rpm <= 0)
  126. return 255;
  127. if (rpm > 1350000)
  128. return 1;
  129. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  130. }
  131. static inline int FAN_FROM_REG(u8 val, int div)
  132. {
  133. return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
  134. }
  135. /*
  136. * TEMP: mC (-54.12C to +157.53C)
  137. * REG: 0.83C/bit + 52.12, two's complement
  138. */
  139. static inline int TEMP_FROM_REG(s8 val)
  140. {
  141. return val * 830 + 52120;
  142. }
  143. static inline s8 TEMP_TO_REG(int val)
  144. {
  145. int nval = SENSORS_LIMIT(val, -54120, 157530) ;
  146. return nval < 0 ? (nval - 5212 - 415) / 830 : (nval - 5212 + 415) / 830;
  147. }
  148. /*
  149. * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
  150. * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
  151. */
  152. static inline u8 DIV_TO_REG(int val)
  153. {
  154. return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
  155. }
  156. #define DIV_FROM_REG(val) (1 << (val))
  157. /*
  158. * For each registered chip, we need to keep some data in memory.
  159. * The structure is dynamically allocated.
  160. */
  161. struct sis5595_data {
  162. unsigned short addr;
  163. const char *name;
  164. struct device *hwmon_dev;
  165. struct mutex lock;
  166. struct mutex update_lock;
  167. char valid; /* !=0 if following fields are valid */
  168. unsigned long last_updated; /* In jiffies */
  169. char maxins; /* == 3 if temp enabled, otherwise == 4 */
  170. u8 revision; /* Reg. value */
  171. u8 in[5]; /* Register value */
  172. u8 in_max[5]; /* Register value */
  173. u8 in_min[5]; /* Register value */
  174. u8 fan[2]; /* Register value */
  175. u8 fan_min[2]; /* Register value */
  176. s8 temp; /* Register value */
  177. s8 temp_over; /* Register value */
  178. s8 temp_hyst; /* Register value */
  179. u8 fan_div[2]; /* Register encoding, shifted right */
  180. u16 alarms; /* Register encoding, combined */
  181. };
  182. static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
  183. static int sis5595_probe(struct platform_device *pdev);
  184. static int __devexit sis5595_remove(struct platform_device *pdev);
  185. static int sis5595_read_value(struct sis5595_data *data, u8 reg);
  186. static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value);
  187. static struct sis5595_data *sis5595_update_device(struct device *dev);
  188. static void sis5595_init_device(struct sis5595_data *data);
  189. static struct platform_driver sis5595_driver = {
  190. .driver = {
  191. .owner = THIS_MODULE,
  192. .name = "sis5595",
  193. },
  194. .probe = sis5595_probe,
  195. .remove = __devexit_p(sis5595_remove),
  196. };
  197. /* 4 Voltages */
  198. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  199. char *buf)
  200. {
  201. struct sis5595_data *data = sis5595_update_device(dev);
  202. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  203. int nr = attr->index;
  204. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
  205. }
  206. static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
  207. char *buf)
  208. {
  209. struct sis5595_data *data = sis5595_update_device(dev);
  210. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  211. int nr = attr->index;
  212. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
  213. }
  214. static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
  215. char *buf)
  216. {
  217. struct sis5595_data *data = sis5595_update_device(dev);
  218. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  219. int nr = attr->index;
  220. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
  221. }
  222. static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
  223. const char *buf, size_t count)
  224. {
  225. struct sis5595_data *data = dev_get_drvdata(dev);
  226. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  227. int nr = attr->index;
  228. unsigned long val;
  229. int err;
  230. err = kstrtoul(buf, 10, &val);
  231. if (err)
  232. return err;
  233. mutex_lock(&data->update_lock);
  234. data->in_min[nr] = IN_TO_REG(val);
  235. sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
  236. mutex_unlock(&data->update_lock);
  237. return count;
  238. }
  239. static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
  240. const char *buf, size_t count)
  241. {
  242. struct sis5595_data *data = dev_get_drvdata(dev);
  243. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  244. int nr = attr->index;
  245. unsigned long val;
  246. int err;
  247. err = kstrtoul(buf, 10, &val);
  248. if (err)
  249. return err;
  250. mutex_lock(&data->update_lock);
  251. data->in_max[nr] = IN_TO_REG(val);
  252. sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
  253. mutex_unlock(&data->update_lock);
  254. return count;
  255. }
  256. #define show_in_offset(offset) \
  257. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  258. show_in, NULL, offset); \
  259. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  260. show_in_min, set_in_min, offset); \
  261. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  262. show_in_max, set_in_max, offset);
  263. show_in_offset(0);
  264. show_in_offset(1);
  265. show_in_offset(2);
  266. show_in_offset(3);
  267. show_in_offset(4);
  268. /* Temperature */
  269. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  270. char *buf)
  271. {
  272. struct sis5595_data *data = sis5595_update_device(dev);
  273. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
  274. }
  275. static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr,
  276. char *buf)
  277. {
  278. struct sis5595_data *data = sis5595_update_device(dev);
  279. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
  280. }
  281. static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr,
  282. const char *buf, size_t count)
  283. {
  284. struct sis5595_data *data = dev_get_drvdata(dev);
  285. long val;
  286. int err;
  287. err = kstrtol(buf, 10, &val);
  288. if (err)
  289. return err;
  290. mutex_lock(&data->update_lock);
  291. data->temp_over = TEMP_TO_REG(val);
  292. sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
  293. mutex_unlock(&data->update_lock);
  294. return count;
  295. }
  296. static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr,
  297. char *buf)
  298. {
  299. struct sis5595_data *data = sis5595_update_device(dev);
  300. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
  301. }
  302. static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr,
  303. const char *buf, size_t count)
  304. {
  305. struct sis5595_data *data = dev_get_drvdata(dev);
  306. long val;
  307. int err;
  308. err = kstrtol(buf, 10, &val);
  309. if (err)
  310. return err;
  311. mutex_lock(&data->update_lock);
  312. data->temp_hyst = TEMP_TO_REG(val);
  313. sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
  314. mutex_unlock(&data->update_lock);
  315. return count;
  316. }
  317. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  318. static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  319. show_temp_over, set_temp_over);
  320. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  321. show_temp_hyst, set_temp_hyst);
  322. /* 2 Fans */
  323. static ssize_t show_fan(struct device *dev, struct device_attribute *da,
  324. char *buf)
  325. {
  326. struct sis5595_data *data = sis5595_update_device(dev);
  327. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  328. int nr = attr->index;
  329. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  330. DIV_FROM_REG(data->fan_div[nr])));
  331. }
  332. static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
  333. char *buf)
  334. {
  335. struct sis5595_data *data = sis5595_update_device(dev);
  336. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  337. int nr = attr->index;
  338. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  339. DIV_FROM_REG(data->fan_div[nr])));
  340. }
  341. static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
  342. const char *buf, size_t count)
  343. {
  344. struct sis5595_data *data = dev_get_drvdata(dev);
  345. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  346. int nr = attr->index;
  347. unsigned long val;
  348. int err;
  349. err = kstrtoul(buf, 10, &val);
  350. if (err)
  351. return err;
  352. mutex_lock(&data->update_lock);
  353. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  354. sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
  355. mutex_unlock(&data->update_lock);
  356. return count;
  357. }
  358. static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
  359. char *buf)
  360. {
  361. struct sis5595_data *data = sis5595_update_device(dev);
  362. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  363. int nr = attr->index;
  364. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  365. }
  366. /*
  367. * Note: we save and restore the fan minimum here, because its value is
  368. * determined in part by the fan divisor. This follows the principle of
  369. * least surprise; the user doesn't expect the fan minimum to change just
  370. * because the divisor changed.
  371. */
  372. static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
  373. const char *buf, size_t count)
  374. {
  375. struct sis5595_data *data = dev_get_drvdata(dev);
  376. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  377. int nr = attr->index;
  378. unsigned long min;
  379. int reg;
  380. unsigned long val;
  381. int err;
  382. err = kstrtoul(buf, 10, &val);
  383. if (err)
  384. return err;
  385. mutex_lock(&data->update_lock);
  386. min = FAN_FROM_REG(data->fan_min[nr],
  387. DIV_FROM_REG(data->fan_div[nr]));
  388. reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
  389. switch (val) {
  390. case 1:
  391. data->fan_div[nr] = 0;
  392. break;
  393. case 2:
  394. data->fan_div[nr] = 1;
  395. break;
  396. case 4:
  397. data->fan_div[nr] = 2;
  398. break;
  399. case 8:
  400. data->fan_div[nr] = 3;
  401. break;
  402. default:
  403. dev_err(dev, "fan_div value %ld not "
  404. "supported. Choose one of 1, 2, 4 or 8!\n", val);
  405. mutex_unlock(&data->update_lock);
  406. return -EINVAL;
  407. }
  408. switch (nr) {
  409. case 0:
  410. reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
  411. break;
  412. case 1:
  413. reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
  414. break;
  415. }
  416. sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
  417. data->fan_min[nr] =
  418. FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  419. sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
  420. mutex_unlock(&data->update_lock);
  421. return count;
  422. }
  423. #define show_fan_offset(offset) \
  424. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  425. show_fan, NULL, offset - 1); \
  426. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  427. show_fan_min, set_fan_min, offset - 1); \
  428. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  429. show_fan_div, set_fan_div, offset - 1);
  430. show_fan_offset(1);
  431. show_fan_offset(2);
  432. /* Alarms */
  433. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  434. char *buf)
  435. {
  436. struct sis5595_data *data = sis5595_update_device(dev);
  437. return sprintf(buf, "%d\n", data->alarms);
  438. }
  439. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  440. static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
  441. char *buf)
  442. {
  443. struct sis5595_data *data = sis5595_update_device(dev);
  444. int nr = to_sensor_dev_attr(da)->index;
  445. return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
  446. }
  447. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  448. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  449. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  450. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  451. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 15);
  452. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  453. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  454. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 15);
  455. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  456. char *buf)
  457. {
  458. struct sis5595_data *data = dev_get_drvdata(dev);
  459. return sprintf(buf, "%s\n", data->name);
  460. }
  461. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  462. static struct attribute *sis5595_attributes[] = {
  463. &sensor_dev_attr_in0_input.dev_attr.attr,
  464. &sensor_dev_attr_in0_min.dev_attr.attr,
  465. &sensor_dev_attr_in0_max.dev_attr.attr,
  466. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  467. &sensor_dev_attr_in1_input.dev_attr.attr,
  468. &sensor_dev_attr_in1_min.dev_attr.attr,
  469. &sensor_dev_attr_in1_max.dev_attr.attr,
  470. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  471. &sensor_dev_attr_in2_input.dev_attr.attr,
  472. &sensor_dev_attr_in2_min.dev_attr.attr,
  473. &sensor_dev_attr_in2_max.dev_attr.attr,
  474. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  475. &sensor_dev_attr_in3_input.dev_attr.attr,
  476. &sensor_dev_attr_in3_min.dev_attr.attr,
  477. &sensor_dev_attr_in3_max.dev_attr.attr,
  478. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  479. &sensor_dev_attr_fan1_input.dev_attr.attr,
  480. &sensor_dev_attr_fan1_min.dev_attr.attr,
  481. &sensor_dev_attr_fan1_div.dev_attr.attr,
  482. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  483. &sensor_dev_attr_fan2_input.dev_attr.attr,
  484. &sensor_dev_attr_fan2_min.dev_attr.attr,
  485. &sensor_dev_attr_fan2_div.dev_attr.attr,
  486. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  487. &dev_attr_alarms.attr,
  488. &dev_attr_name.attr,
  489. NULL
  490. };
  491. static const struct attribute_group sis5595_group = {
  492. .attrs = sis5595_attributes,
  493. };
  494. static struct attribute *sis5595_attributes_in4[] = {
  495. &sensor_dev_attr_in4_input.dev_attr.attr,
  496. &sensor_dev_attr_in4_min.dev_attr.attr,
  497. &sensor_dev_attr_in4_max.dev_attr.attr,
  498. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  499. NULL
  500. };
  501. static const struct attribute_group sis5595_group_in4 = {
  502. .attrs = sis5595_attributes_in4,
  503. };
  504. static struct attribute *sis5595_attributes_temp1[] = {
  505. &dev_attr_temp1_input.attr,
  506. &dev_attr_temp1_max.attr,
  507. &dev_attr_temp1_max_hyst.attr,
  508. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  509. NULL
  510. };
  511. static const struct attribute_group sis5595_group_temp1 = {
  512. .attrs = sis5595_attributes_temp1,
  513. };
  514. /* This is called when the module is loaded */
  515. static int __devinit sis5595_probe(struct platform_device *pdev)
  516. {
  517. int err = 0;
  518. int i;
  519. struct sis5595_data *data;
  520. struct resource *res;
  521. char val;
  522. /* Reserve the ISA region */
  523. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  524. if (!request_region(res->start, SIS5595_EXTENT,
  525. sis5595_driver.driver.name)) {
  526. err = -EBUSY;
  527. goto exit;
  528. }
  529. data = kzalloc(sizeof(struct sis5595_data), GFP_KERNEL);
  530. if (!data) {
  531. err = -ENOMEM;
  532. goto exit_release;
  533. }
  534. mutex_init(&data->lock);
  535. mutex_init(&data->update_lock);
  536. data->addr = res->start;
  537. data->name = "sis5595";
  538. platform_set_drvdata(pdev, data);
  539. /*
  540. * Check revision and pin registers to determine whether 4 or 5 voltages
  541. */
  542. data->revision = s_bridge->revision;
  543. /* 4 voltages, 1 temp */
  544. data->maxins = 3;
  545. if (data->revision >= REV2MIN) {
  546. pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
  547. if (!(val & 0x80))
  548. /* 5 voltages, no temps */
  549. data->maxins = 4;
  550. }
  551. /* Initialize the SIS5595 chip */
  552. sis5595_init_device(data);
  553. /* A few vars need to be filled upon startup */
  554. for (i = 0; i < 2; i++) {
  555. data->fan_min[i] = sis5595_read_value(data,
  556. SIS5595_REG_FAN_MIN(i));
  557. }
  558. /* Register sysfs hooks */
  559. err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group);
  560. if (err)
  561. goto exit_free;
  562. if (data->maxins == 4) {
  563. err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_in4);
  564. if (err)
  565. goto exit_remove_files;
  566. } else {
  567. err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_temp1);
  568. if (err)
  569. goto exit_remove_files;
  570. }
  571. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  572. if (IS_ERR(data->hwmon_dev)) {
  573. err = PTR_ERR(data->hwmon_dev);
  574. goto exit_remove_files;
  575. }
  576. return 0;
  577. exit_remove_files:
  578. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
  579. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
  580. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
  581. exit_free:
  582. kfree(data);
  583. exit_release:
  584. release_region(res->start, SIS5595_EXTENT);
  585. exit:
  586. return err;
  587. }
  588. static int __devexit sis5595_remove(struct platform_device *pdev)
  589. {
  590. struct sis5595_data *data = platform_get_drvdata(pdev);
  591. hwmon_device_unregister(data->hwmon_dev);
  592. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
  593. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
  594. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
  595. release_region(data->addr, SIS5595_EXTENT);
  596. platform_set_drvdata(pdev, NULL);
  597. kfree(data);
  598. return 0;
  599. }
  600. /* ISA access must be locked explicitly. */
  601. static int sis5595_read_value(struct sis5595_data *data, u8 reg)
  602. {
  603. int res;
  604. mutex_lock(&data->lock);
  605. outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  606. res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
  607. mutex_unlock(&data->lock);
  608. return res;
  609. }
  610. static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
  611. {
  612. mutex_lock(&data->lock);
  613. outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  614. outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
  615. mutex_unlock(&data->lock);
  616. }
  617. /* Called when we have found a new SIS5595. */
  618. static void __devinit sis5595_init_device(struct sis5595_data *data)
  619. {
  620. u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
  621. if (!(config & 0x01))
  622. sis5595_write_value(data, SIS5595_REG_CONFIG,
  623. (config & 0xf7) | 0x01);
  624. }
  625. static struct sis5595_data *sis5595_update_device(struct device *dev)
  626. {
  627. struct sis5595_data *data = dev_get_drvdata(dev);
  628. int i;
  629. mutex_lock(&data->update_lock);
  630. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  631. || !data->valid) {
  632. for (i = 0; i <= data->maxins; i++) {
  633. data->in[i] =
  634. sis5595_read_value(data, SIS5595_REG_IN(i));
  635. data->in_min[i] =
  636. sis5595_read_value(data,
  637. SIS5595_REG_IN_MIN(i));
  638. data->in_max[i] =
  639. sis5595_read_value(data,
  640. SIS5595_REG_IN_MAX(i));
  641. }
  642. for (i = 0; i < 2; i++) {
  643. data->fan[i] =
  644. sis5595_read_value(data, SIS5595_REG_FAN(i));
  645. data->fan_min[i] =
  646. sis5595_read_value(data,
  647. SIS5595_REG_FAN_MIN(i));
  648. }
  649. if (data->maxins == 3) {
  650. data->temp =
  651. sis5595_read_value(data, SIS5595_REG_TEMP);
  652. data->temp_over =
  653. sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
  654. data->temp_hyst =
  655. sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
  656. }
  657. i = sis5595_read_value(data, SIS5595_REG_FANDIV);
  658. data->fan_div[0] = (i >> 4) & 0x03;
  659. data->fan_div[1] = i >> 6;
  660. data->alarms =
  661. sis5595_read_value(data, SIS5595_REG_ALARM1) |
  662. (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
  663. data->last_updated = jiffies;
  664. data->valid = 1;
  665. }
  666. mutex_unlock(&data->update_lock);
  667. return data;
  668. }
  669. static DEFINE_PCI_DEVICE_TABLE(sis5595_pci_ids) = {
  670. { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
  671. { 0, }
  672. };
  673. MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
  674. static int blacklist[] __devinitdata = {
  675. PCI_DEVICE_ID_SI_540,
  676. PCI_DEVICE_ID_SI_550,
  677. PCI_DEVICE_ID_SI_630,
  678. PCI_DEVICE_ID_SI_645,
  679. PCI_DEVICE_ID_SI_730,
  680. PCI_DEVICE_ID_SI_735,
  681. PCI_DEVICE_ID_SI_5511, /*
  682. * 5513 chip has the 0008 device but
  683. * that ID shows up in other chips so we
  684. * use the 5511 ID for recognition
  685. */
  686. PCI_DEVICE_ID_SI_5597,
  687. PCI_DEVICE_ID_SI_5598,
  688. 0 };
  689. static int __devinit sis5595_device_add(unsigned short address)
  690. {
  691. struct resource res = {
  692. .start = address,
  693. .end = address + SIS5595_EXTENT - 1,
  694. .name = "sis5595",
  695. .flags = IORESOURCE_IO,
  696. };
  697. int err;
  698. err = acpi_check_resource_conflict(&res);
  699. if (err)
  700. goto exit;
  701. pdev = platform_device_alloc("sis5595", address);
  702. if (!pdev) {
  703. err = -ENOMEM;
  704. pr_err("Device allocation failed\n");
  705. goto exit;
  706. }
  707. err = platform_device_add_resources(pdev, &res, 1);
  708. if (err) {
  709. pr_err("Device resource addition failed (%d)\n", err);
  710. goto exit_device_put;
  711. }
  712. err = platform_device_add(pdev);
  713. if (err) {
  714. pr_err("Device addition failed (%d)\n", err);
  715. goto exit_device_put;
  716. }
  717. return 0;
  718. exit_device_put:
  719. platform_device_put(pdev);
  720. exit:
  721. return err;
  722. }
  723. static int __devinit sis5595_pci_probe(struct pci_dev *dev,
  724. const struct pci_device_id *id)
  725. {
  726. u16 address;
  727. u8 enable;
  728. int *i;
  729. for (i = blacklist; *i != 0; i++) {
  730. struct pci_dev *d;
  731. d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
  732. if (d) {
  733. dev_err(&d->dev,
  734. "Looked for SIS5595 but found unsupported device %.4x\n",
  735. *i);
  736. pci_dev_put(d);
  737. return -ENODEV;
  738. }
  739. }
  740. force_addr &= ~(SIS5595_EXTENT - 1);
  741. if (force_addr) {
  742. dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", force_addr);
  743. pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
  744. }
  745. if (PCIBIOS_SUCCESSFUL !=
  746. pci_read_config_word(dev, SIS5595_BASE_REG, &address)) {
  747. dev_err(&dev->dev, "Failed to read ISA address\n");
  748. return -ENODEV;
  749. }
  750. address &= ~(SIS5595_EXTENT - 1);
  751. if (!address) {
  752. dev_err(&dev->dev,
  753. "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
  754. return -ENODEV;
  755. }
  756. if (force_addr && address != force_addr) {
  757. /* doesn't work for some chips? */
  758. dev_err(&dev->dev, "Failed to force ISA address\n");
  759. return -ENODEV;
  760. }
  761. if (PCIBIOS_SUCCESSFUL !=
  762. pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable)) {
  763. dev_err(&dev->dev, "Failed to read enable register\n");
  764. return -ENODEV;
  765. }
  766. if (!(enable & 0x80)) {
  767. if ((PCIBIOS_SUCCESSFUL !=
  768. pci_write_config_byte(dev, SIS5595_ENABLE_REG,
  769. enable | 0x80))
  770. || (PCIBIOS_SUCCESSFUL !=
  771. pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable))
  772. || (!(enable & 0x80))) {
  773. /* doesn't work for some chips! */
  774. dev_err(&dev->dev, "Failed to enable HWM device\n");
  775. return -ENODEV;
  776. }
  777. }
  778. if (platform_driver_register(&sis5595_driver)) {
  779. dev_dbg(&dev->dev, "Failed to register sis5595 driver\n");
  780. goto exit;
  781. }
  782. s_bridge = pci_dev_get(dev);
  783. /* Sets global pdev as a side effect */
  784. if (sis5595_device_add(address))
  785. goto exit_unregister;
  786. /*
  787. * Always return failure here. This is to allow other drivers to bind
  788. * to this pci device. We don't really want to have control over the
  789. * pci device, we only wanted to read as few register values from it.
  790. */
  791. return -ENODEV;
  792. exit_unregister:
  793. pci_dev_put(dev);
  794. platform_driver_unregister(&sis5595_driver);
  795. exit:
  796. return -ENODEV;
  797. }
  798. static struct pci_driver sis5595_pci_driver = {
  799. .name = "sis5595",
  800. .id_table = sis5595_pci_ids,
  801. .probe = sis5595_pci_probe,
  802. };
  803. static int __init sm_sis5595_init(void)
  804. {
  805. return pci_register_driver(&sis5595_pci_driver);
  806. }
  807. static void __exit sm_sis5595_exit(void)
  808. {
  809. pci_unregister_driver(&sis5595_pci_driver);
  810. if (s_bridge != NULL) {
  811. platform_device_unregister(pdev);
  812. platform_driver_unregister(&sis5595_driver);
  813. pci_dev_put(s_bridge);
  814. s_bridge = NULL;
  815. }
  816. }
  817. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  818. MODULE_DESCRIPTION("SiS 5595 Sensor device");
  819. MODULE_LICENSE("GPL");
  820. module_init(sm_sis5595_init);
  821. module_exit(sm_sis5595_exit);