sis5595.c 24 KB

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