sis5595.c 25 KB

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