sis5595.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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 = clamp_val(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. return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  128. }
  129. static inline int FAN_FROM_REG(u8 val, int div)
  130. {
  131. return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
  132. }
  133. /*
  134. * TEMP: mC (-54.12C to +157.53C)
  135. * REG: 0.83C/bit + 52.12, two's complement
  136. */
  137. static inline int TEMP_FROM_REG(s8 val)
  138. {
  139. return val * 830 + 52120;
  140. }
  141. static inline s8 TEMP_TO_REG(int val)
  142. {
  143. int nval = clamp_val(val, -54120, 157530) ;
  144. return nval < 0 ? (nval - 5212 - 415) / 830 : (nval - 5212 + 415) / 830;
  145. }
  146. /*
  147. * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
  148. * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
  149. */
  150. static inline u8 DIV_TO_REG(int val)
  151. {
  152. return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
  153. }
  154. #define DIV_FROM_REG(val) (1 << (val))
  155. /*
  156. * For each registered chip, we need to keep some data in memory.
  157. * The structure is dynamically allocated.
  158. */
  159. struct sis5595_data {
  160. unsigned short addr;
  161. const char *name;
  162. struct device *hwmon_dev;
  163. struct mutex lock;
  164. struct mutex update_lock;
  165. char valid; /* !=0 if following fields are valid */
  166. unsigned long last_updated; /* In jiffies */
  167. char maxins; /* == 3 if temp enabled, otherwise == 4 */
  168. u8 revision; /* Reg. value */
  169. u8 in[5]; /* Register value */
  170. u8 in_max[5]; /* Register value */
  171. u8 in_min[5]; /* Register value */
  172. u8 fan[2]; /* Register value */
  173. u8 fan_min[2]; /* Register value */
  174. s8 temp; /* Register value */
  175. s8 temp_over; /* Register value */
  176. s8 temp_hyst; /* Register value */
  177. u8 fan_div[2]; /* Register encoding, shifted right */
  178. u16 alarms; /* Register encoding, combined */
  179. };
  180. static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
  181. static int sis5595_probe(struct platform_device *pdev);
  182. static int sis5595_remove(struct platform_device *pdev);
  183. static int sis5595_read_value(struct sis5595_data *data, u8 reg);
  184. static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value);
  185. static struct sis5595_data *sis5595_update_device(struct device *dev);
  186. static void sis5595_init_device(struct sis5595_data *data);
  187. static struct platform_driver sis5595_driver = {
  188. .driver = {
  189. .owner = THIS_MODULE,
  190. .name = "sis5595",
  191. },
  192. .probe = sis5595_probe,
  193. .remove = sis5595_remove,
  194. };
  195. /* 4 Voltages */
  196. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  197. char *buf)
  198. {
  199. struct sis5595_data *data = sis5595_update_device(dev);
  200. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  201. int nr = attr->index;
  202. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
  203. }
  204. static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
  205. char *buf)
  206. {
  207. struct sis5595_data *data = sis5595_update_device(dev);
  208. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  209. int nr = attr->index;
  210. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
  211. }
  212. static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
  213. char *buf)
  214. {
  215. struct sis5595_data *data = sis5595_update_device(dev);
  216. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  217. int nr = attr->index;
  218. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
  219. }
  220. static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
  221. const char *buf, size_t count)
  222. {
  223. struct sis5595_data *data = dev_get_drvdata(dev);
  224. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  225. int nr = attr->index;
  226. unsigned long val;
  227. int err;
  228. err = kstrtoul(buf, 10, &val);
  229. if (err)
  230. return err;
  231. mutex_lock(&data->update_lock);
  232. data->in_min[nr] = IN_TO_REG(val);
  233. sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
  234. mutex_unlock(&data->update_lock);
  235. return count;
  236. }
  237. static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
  238. const char *buf, size_t count)
  239. {
  240. struct sis5595_data *data = dev_get_drvdata(dev);
  241. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  242. int nr = attr->index;
  243. unsigned long val;
  244. int err;
  245. err = kstrtoul(buf, 10, &val);
  246. if (err)
  247. return err;
  248. mutex_lock(&data->update_lock);
  249. data->in_max[nr] = IN_TO_REG(val);
  250. sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
  251. mutex_unlock(&data->update_lock);
  252. return count;
  253. }
  254. #define show_in_offset(offset) \
  255. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  256. show_in, NULL, offset); \
  257. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  258. show_in_min, set_in_min, offset); \
  259. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  260. show_in_max, set_in_max, offset);
  261. show_in_offset(0);
  262. show_in_offset(1);
  263. show_in_offset(2);
  264. show_in_offset(3);
  265. show_in_offset(4);
  266. /* Temperature */
  267. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct sis5595_data *data = sis5595_update_device(dev);
  271. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
  272. }
  273. static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr,
  274. char *buf)
  275. {
  276. struct sis5595_data *data = sis5595_update_device(dev);
  277. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
  278. }
  279. static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr,
  280. const char *buf, size_t count)
  281. {
  282. struct sis5595_data *data = dev_get_drvdata(dev);
  283. long val;
  284. int err;
  285. err = kstrtol(buf, 10, &val);
  286. if (err)
  287. return err;
  288. mutex_lock(&data->update_lock);
  289. data->temp_over = TEMP_TO_REG(val);
  290. sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
  291. mutex_unlock(&data->update_lock);
  292. return count;
  293. }
  294. static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr,
  295. char *buf)
  296. {
  297. struct sis5595_data *data = sis5595_update_device(dev);
  298. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
  299. }
  300. static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr,
  301. const char *buf, size_t count)
  302. {
  303. struct sis5595_data *data = dev_get_drvdata(dev);
  304. long val;
  305. int err;
  306. err = kstrtol(buf, 10, &val);
  307. if (err)
  308. return err;
  309. mutex_lock(&data->update_lock);
  310. data->temp_hyst = TEMP_TO_REG(val);
  311. sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
  312. mutex_unlock(&data->update_lock);
  313. return count;
  314. }
  315. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  316. static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  317. show_temp_over, set_temp_over);
  318. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  319. show_temp_hyst, set_temp_hyst);
  320. /* 2 Fans */
  321. static ssize_t show_fan(struct device *dev, struct device_attribute *da,
  322. char *buf)
  323. {
  324. struct sis5595_data *data = sis5595_update_device(dev);
  325. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  326. int nr = attr->index;
  327. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  328. DIV_FROM_REG(data->fan_div[nr])));
  329. }
  330. static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
  331. char *buf)
  332. {
  333. struct sis5595_data *data = sis5595_update_device(dev);
  334. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  335. int nr = attr->index;
  336. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  337. DIV_FROM_REG(data->fan_div[nr])));
  338. }
  339. static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
  340. const char *buf, size_t count)
  341. {
  342. struct sis5595_data *data = dev_get_drvdata(dev);
  343. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  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. mutex_lock(&data->update_lock);
  351. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  352. sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
  353. mutex_unlock(&data->update_lock);
  354. return count;
  355. }
  356. static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
  357. char *buf)
  358. {
  359. struct sis5595_data *data = sis5595_update_device(dev);
  360. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  361. int nr = attr->index;
  362. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  363. }
  364. /*
  365. * Note: we save and restore the fan minimum here, because its value is
  366. * determined in part by the fan divisor. This follows the principle of
  367. * least surprise; the user doesn't expect the fan minimum to change just
  368. * because the divisor changed.
  369. */
  370. static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
  371. const char *buf, size_t count)
  372. {
  373. struct sis5595_data *data = dev_get_drvdata(dev);
  374. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  375. int nr = attr->index;
  376. unsigned long min;
  377. int reg;
  378. unsigned long val;
  379. int err;
  380. err = kstrtoul(buf, 10, &val);
  381. if (err)
  382. return err;
  383. mutex_lock(&data->update_lock);
  384. min = FAN_FROM_REG(data->fan_min[nr],
  385. DIV_FROM_REG(data->fan_div[nr]));
  386. reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
  387. switch (val) {
  388. case 1:
  389. data->fan_div[nr] = 0;
  390. break;
  391. case 2:
  392. data->fan_div[nr] = 1;
  393. break;
  394. case 4:
  395. data->fan_div[nr] = 2;
  396. break;
  397. case 8:
  398. data->fan_div[nr] = 3;
  399. break;
  400. default:
  401. dev_err(dev, "fan_div value %ld not "
  402. "supported. Choose one of 1, 2, 4 or 8!\n", val);
  403. mutex_unlock(&data->update_lock);
  404. return -EINVAL;
  405. }
  406. switch (nr) {
  407. case 0:
  408. reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
  409. break;
  410. case 1:
  411. reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
  412. break;
  413. }
  414. sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
  415. data->fan_min[nr] =
  416. FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  417. sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
  418. mutex_unlock(&data->update_lock);
  419. return count;
  420. }
  421. #define show_fan_offset(offset) \
  422. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  423. show_fan, NULL, offset - 1); \
  424. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  425. show_fan_min, set_fan_min, offset - 1); \
  426. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  427. show_fan_div, set_fan_div, offset - 1);
  428. show_fan_offset(1);
  429. show_fan_offset(2);
  430. /* Alarms */
  431. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  432. char *buf)
  433. {
  434. struct sis5595_data *data = sis5595_update_device(dev);
  435. return sprintf(buf, "%d\n", data->alarms);
  436. }
  437. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  438. static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
  439. char *buf)
  440. {
  441. struct sis5595_data *data = sis5595_update_device(dev);
  442. int nr = to_sensor_dev_attr(da)->index;
  443. return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
  444. }
  445. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  446. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  447. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  448. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  449. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 15);
  450. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  451. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  452. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 15);
  453. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  454. char *buf)
  455. {
  456. struct sis5595_data *data = dev_get_drvdata(dev);
  457. return sprintf(buf, "%s\n", data->name);
  458. }
  459. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  460. static struct attribute *sis5595_attributes[] = {
  461. &sensor_dev_attr_in0_input.dev_attr.attr,
  462. &sensor_dev_attr_in0_min.dev_attr.attr,
  463. &sensor_dev_attr_in0_max.dev_attr.attr,
  464. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  465. &sensor_dev_attr_in1_input.dev_attr.attr,
  466. &sensor_dev_attr_in1_min.dev_attr.attr,
  467. &sensor_dev_attr_in1_max.dev_attr.attr,
  468. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  469. &sensor_dev_attr_in2_input.dev_attr.attr,
  470. &sensor_dev_attr_in2_min.dev_attr.attr,
  471. &sensor_dev_attr_in2_max.dev_attr.attr,
  472. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  473. &sensor_dev_attr_in3_input.dev_attr.attr,
  474. &sensor_dev_attr_in3_min.dev_attr.attr,
  475. &sensor_dev_attr_in3_max.dev_attr.attr,
  476. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  477. &sensor_dev_attr_fan1_input.dev_attr.attr,
  478. &sensor_dev_attr_fan1_min.dev_attr.attr,
  479. &sensor_dev_attr_fan1_div.dev_attr.attr,
  480. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  481. &sensor_dev_attr_fan2_input.dev_attr.attr,
  482. &sensor_dev_attr_fan2_min.dev_attr.attr,
  483. &sensor_dev_attr_fan2_div.dev_attr.attr,
  484. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  485. &dev_attr_alarms.attr,
  486. &dev_attr_name.attr,
  487. NULL
  488. };
  489. static const struct attribute_group sis5595_group = {
  490. .attrs = sis5595_attributes,
  491. };
  492. static struct attribute *sis5595_attributes_in4[] = {
  493. &sensor_dev_attr_in4_input.dev_attr.attr,
  494. &sensor_dev_attr_in4_min.dev_attr.attr,
  495. &sensor_dev_attr_in4_max.dev_attr.attr,
  496. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  497. NULL
  498. };
  499. static const struct attribute_group sis5595_group_in4 = {
  500. .attrs = sis5595_attributes_in4,
  501. };
  502. static struct attribute *sis5595_attributes_temp1[] = {
  503. &dev_attr_temp1_input.attr,
  504. &dev_attr_temp1_max.attr,
  505. &dev_attr_temp1_max_hyst.attr,
  506. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  507. NULL
  508. };
  509. static const struct attribute_group sis5595_group_temp1 = {
  510. .attrs = sis5595_attributes_temp1,
  511. };
  512. /* This is called when the module is loaded */
  513. static int sis5595_probe(struct platform_device *pdev)
  514. {
  515. int err = 0;
  516. int i;
  517. struct sis5595_data *data;
  518. struct resource *res;
  519. char val;
  520. /* Reserve the ISA region */
  521. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  522. if (!devm_request_region(&pdev->dev, res->start, SIS5595_EXTENT,
  523. sis5595_driver.driver.name))
  524. return -EBUSY;
  525. data = devm_kzalloc(&pdev->dev, sizeof(struct sis5595_data),
  526. GFP_KERNEL);
  527. if (!data)
  528. return -ENOMEM;
  529. mutex_init(&data->lock);
  530. mutex_init(&data->update_lock);
  531. data->addr = res->start;
  532. data->name = "sis5595";
  533. platform_set_drvdata(pdev, data);
  534. /*
  535. * Check revision and pin registers to determine whether 4 or 5 voltages
  536. */
  537. data->revision = s_bridge->revision;
  538. /* 4 voltages, 1 temp */
  539. data->maxins = 3;
  540. if (data->revision >= REV2MIN) {
  541. pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
  542. if (!(val & 0x80))
  543. /* 5 voltages, no temps */
  544. data->maxins = 4;
  545. }
  546. /* Initialize the SIS5595 chip */
  547. sis5595_init_device(data);
  548. /* A few vars need to be filled upon startup */
  549. for (i = 0; i < 2; i++) {
  550. data->fan_min[i] = sis5595_read_value(data,
  551. SIS5595_REG_FAN_MIN(i));
  552. }
  553. /* Register sysfs hooks */
  554. err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group);
  555. if (err)
  556. return err;
  557. if (data->maxins == 4) {
  558. err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_in4);
  559. if (err)
  560. goto exit_remove_files;
  561. } else {
  562. err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_temp1);
  563. if (err)
  564. goto exit_remove_files;
  565. }
  566. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  567. if (IS_ERR(data->hwmon_dev)) {
  568. err = PTR_ERR(data->hwmon_dev);
  569. goto exit_remove_files;
  570. }
  571. return 0;
  572. exit_remove_files:
  573. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
  574. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
  575. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
  576. return err;
  577. }
  578. static int sis5595_remove(struct platform_device *pdev)
  579. {
  580. struct sis5595_data *data = platform_get_drvdata(pdev);
  581. hwmon_device_unregister(data->hwmon_dev);
  582. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
  583. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
  584. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
  585. return 0;
  586. }
  587. /* ISA access must be locked explicitly. */
  588. static int sis5595_read_value(struct sis5595_data *data, u8 reg)
  589. {
  590. int res;
  591. mutex_lock(&data->lock);
  592. outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  593. res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
  594. mutex_unlock(&data->lock);
  595. return res;
  596. }
  597. static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
  598. {
  599. mutex_lock(&data->lock);
  600. outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  601. outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
  602. mutex_unlock(&data->lock);
  603. }
  604. /* Called when we have found a new SIS5595. */
  605. static void sis5595_init_device(struct sis5595_data *data)
  606. {
  607. u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
  608. if (!(config & 0x01))
  609. sis5595_write_value(data, SIS5595_REG_CONFIG,
  610. (config & 0xf7) | 0x01);
  611. }
  612. static struct sis5595_data *sis5595_update_device(struct device *dev)
  613. {
  614. struct sis5595_data *data = dev_get_drvdata(dev);
  615. int i;
  616. mutex_lock(&data->update_lock);
  617. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  618. || !data->valid) {
  619. for (i = 0; i <= data->maxins; i++) {
  620. data->in[i] =
  621. sis5595_read_value(data, SIS5595_REG_IN(i));
  622. data->in_min[i] =
  623. sis5595_read_value(data,
  624. SIS5595_REG_IN_MIN(i));
  625. data->in_max[i] =
  626. sis5595_read_value(data,
  627. SIS5595_REG_IN_MAX(i));
  628. }
  629. for (i = 0; i < 2; i++) {
  630. data->fan[i] =
  631. sis5595_read_value(data, SIS5595_REG_FAN(i));
  632. data->fan_min[i] =
  633. sis5595_read_value(data,
  634. SIS5595_REG_FAN_MIN(i));
  635. }
  636. if (data->maxins == 3) {
  637. data->temp =
  638. sis5595_read_value(data, SIS5595_REG_TEMP);
  639. data->temp_over =
  640. sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
  641. data->temp_hyst =
  642. sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
  643. }
  644. i = sis5595_read_value(data, SIS5595_REG_FANDIV);
  645. data->fan_div[0] = (i >> 4) & 0x03;
  646. data->fan_div[1] = i >> 6;
  647. data->alarms =
  648. sis5595_read_value(data, SIS5595_REG_ALARM1) |
  649. (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
  650. data->last_updated = jiffies;
  651. data->valid = 1;
  652. }
  653. mutex_unlock(&data->update_lock);
  654. return data;
  655. }
  656. static DEFINE_PCI_DEVICE_TABLE(sis5595_pci_ids) = {
  657. { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
  658. { 0, }
  659. };
  660. MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
  661. static int blacklist[] = {
  662. PCI_DEVICE_ID_SI_540,
  663. PCI_DEVICE_ID_SI_550,
  664. PCI_DEVICE_ID_SI_630,
  665. PCI_DEVICE_ID_SI_645,
  666. PCI_DEVICE_ID_SI_730,
  667. PCI_DEVICE_ID_SI_735,
  668. PCI_DEVICE_ID_SI_5511, /*
  669. * 5513 chip has the 0008 device but
  670. * that ID shows up in other chips so we
  671. * use the 5511 ID for recognition
  672. */
  673. PCI_DEVICE_ID_SI_5597,
  674. PCI_DEVICE_ID_SI_5598,
  675. 0 };
  676. static int sis5595_device_add(unsigned short address)
  677. {
  678. struct resource res = {
  679. .start = address,
  680. .end = address + SIS5595_EXTENT - 1,
  681. .name = "sis5595",
  682. .flags = IORESOURCE_IO,
  683. };
  684. int err;
  685. err = acpi_check_resource_conflict(&res);
  686. if (err)
  687. goto exit;
  688. pdev = platform_device_alloc("sis5595", address);
  689. if (!pdev) {
  690. err = -ENOMEM;
  691. pr_err("Device allocation failed\n");
  692. goto exit;
  693. }
  694. err = platform_device_add_resources(pdev, &res, 1);
  695. if (err) {
  696. pr_err("Device resource addition failed (%d)\n", err);
  697. goto exit_device_put;
  698. }
  699. err = platform_device_add(pdev);
  700. if (err) {
  701. pr_err("Device addition failed (%d)\n", err);
  702. goto exit_device_put;
  703. }
  704. return 0;
  705. exit_device_put:
  706. platform_device_put(pdev);
  707. exit:
  708. return err;
  709. }
  710. static int sis5595_pci_probe(struct pci_dev *dev,
  711. const struct pci_device_id *id)
  712. {
  713. u16 address;
  714. u8 enable;
  715. int *i;
  716. for (i = blacklist; *i != 0; i++) {
  717. struct pci_dev *d;
  718. d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
  719. if (d) {
  720. dev_err(&d->dev,
  721. "Looked for SIS5595 but found unsupported device %.4x\n",
  722. *i);
  723. pci_dev_put(d);
  724. return -ENODEV;
  725. }
  726. }
  727. force_addr &= ~(SIS5595_EXTENT - 1);
  728. if (force_addr) {
  729. dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", force_addr);
  730. pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
  731. }
  732. if (PCIBIOS_SUCCESSFUL !=
  733. pci_read_config_word(dev, SIS5595_BASE_REG, &address)) {
  734. dev_err(&dev->dev, "Failed to read ISA address\n");
  735. return -ENODEV;
  736. }
  737. address &= ~(SIS5595_EXTENT - 1);
  738. if (!address) {
  739. dev_err(&dev->dev,
  740. "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
  741. return -ENODEV;
  742. }
  743. if (force_addr && address != force_addr) {
  744. /* doesn't work for some chips? */
  745. dev_err(&dev->dev, "Failed to force ISA address\n");
  746. return -ENODEV;
  747. }
  748. if (PCIBIOS_SUCCESSFUL !=
  749. pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable)) {
  750. dev_err(&dev->dev, "Failed to read enable register\n");
  751. return -ENODEV;
  752. }
  753. if (!(enable & 0x80)) {
  754. if ((PCIBIOS_SUCCESSFUL !=
  755. pci_write_config_byte(dev, SIS5595_ENABLE_REG,
  756. enable | 0x80))
  757. || (PCIBIOS_SUCCESSFUL !=
  758. pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable))
  759. || (!(enable & 0x80))) {
  760. /* doesn't work for some chips! */
  761. dev_err(&dev->dev, "Failed to enable HWM device\n");
  762. return -ENODEV;
  763. }
  764. }
  765. if (platform_driver_register(&sis5595_driver)) {
  766. dev_dbg(&dev->dev, "Failed to register sis5595 driver\n");
  767. goto exit;
  768. }
  769. s_bridge = pci_dev_get(dev);
  770. /* Sets global pdev as a side effect */
  771. if (sis5595_device_add(address))
  772. goto exit_unregister;
  773. /*
  774. * Always return failure here. This is to allow other drivers to bind
  775. * to this pci device. We don't really want to have control over the
  776. * pci device, we only wanted to read as few register values from it.
  777. */
  778. return -ENODEV;
  779. exit_unregister:
  780. pci_dev_put(dev);
  781. platform_driver_unregister(&sis5595_driver);
  782. exit:
  783. return -ENODEV;
  784. }
  785. static struct pci_driver sis5595_pci_driver = {
  786. .name = "sis5595",
  787. .id_table = sis5595_pci_ids,
  788. .probe = sis5595_pci_probe,
  789. };
  790. static int __init sm_sis5595_init(void)
  791. {
  792. return pci_register_driver(&sis5595_pci_driver);
  793. }
  794. static void __exit sm_sis5595_exit(void)
  795. {
  796. pci_unregister_driver(&sis5595_pci_driver);
  797. if (s_bridge != NULL) {
  798. platform_device_unregister(pdev);
  799. platform_driver_unregister(&sis5595_driver);
  800. pci_dev_put(s_bridge);
  801. s_bridge = NULL;
  802. }
  803. }
  804. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  805. MODULE_DESCRIPTION("SiS 5595 Sensor device");
  806. MODULE_LICENSE("GPL");
  807. module_init(sm_sis5595_init);
  808. module_exit(sm_sis5595_exit);