pc87427.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /*
  2. * pc87427.c - hardware monitoring driver for the
  3. * National Semiconductor PC87427 Super-I/O chip
  4. * Copyright (C) 2006, 2008, 2010 Jean Delvare <khali@linux-fr.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Supports the following chips:
  16. *
  17. * Chip #vin #fan #pwm #temp devid
  18. * PC87427 - 8 4 6 0xF2
  19. *
  20. * This driver assumes that no more than one chip is present.
  21. * Only fans are fully supported so far. Temperatures are in read-only
  22. * mode, and voltages aren't supported at all.
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/err.h>
  33. #include <linux/mutex.h>
  34. #include <linux/sysfs.h>
  35. #include <linux/ioport.h>
  36. #include <linux/acpi.h>
  37. #include <linux/io.h>
  38. static unsigned short force_id;
  39. module_param(force_id, ushort, 0);
  40. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  41. static struct platform_device *pdev;
  42. #define DRVNAME "pc87427"
  43. /*
  44. * The lock mutex protects both the I/O accesses (needed because the
  45. * device is using banked registers) and the register cache (needed to keep
  46. * the data in the registers and the cache in sync at any time).
  47. */
  48. struct pc87427_data {
  49. struct device *hwmon_dev;
  50. struct mutex lock;
  51. int address[2];
  52. const char *name;
  53. unsigned long last_updated; /* in jiffies */
  54. u8 fan_enabled; /* bit vector */
  55. u16 fan[8]; /* register values */
  56. u16 fan_min[8]; /* register values */
  57. u8 fan_status[8]; /* register values */
  58. u8 pwm_enabled; /* bit vector */
  59. u8 pwm_auto_ok; /* bit vector */
  60. u8 pwm_enable[4]; /* register values */
  61. u8 pwm[4]; /* register values */
  62. u8 temp_enabled; /* bit vector */
  63. s16 temp[6]; /* register values */
  64. s8 temp_min[6]; /* register values */
  65. s8 temp_max[6]; /* register values */
  66. s8 temp_crit[6]; /* register values */
  67. u8 temp_status[6]; /* register values */
  68. u8 temp_type[6]; /* register values */
  69. };
  70. struct pc87427_sio_data {
  71. unsigned short address[2];
  72. u8 has_fanin;
  73. u8 has_fanout;
  74. };
  75. /*
  76. * Super-I/O registers and operations
  77. */
  78. #define SIOREG_LDSEL 0x07 /* Logical device select */
  79. #define SIOREG_DEVID 0x20 /* Device ID */
  80. #define SIOREG_CF2 0x22 /* Configuration 2 */
  81. #define SIOREG_CF3 0x23 /* Configuration 3 */
  82. #define SIOREG_CF4 0x24 /* Configuration 4 */
  83. #define SIOREG_CF5 0x25 /* Configuration 5 */
  84. #define SIOREG_CFB 0x2B /* Configuration B */
  85. #define SIOREG_CFC 0x2C /* Configuration C */
  86. #define SIOREG_CFD 0x2D /* Configuration D */
  87. #define SIOREG_ACT 0x30 /* Device activation */
  88. #define SIOREG_MAP 0x50 /* I/O or memory mapping */
  89. #define SIOREG_IOBASE 0x60 /* I/O base address */
  90. static const u8 logdev[2] = { 0x09, 0x14 };
  91. static const char *logdev_str[2] = { DRVNAME " FMC", DRVNAME " HMC" };
  92. #define LD_FAN 0
  93. #define LD_IN 1
  94. #define LD_TEMP 1
  95. static inline void superio_outb(int sioaddr, int reg, int val)
  96. {
  97. outb(reg, sioaddr);
  98. outb(val, sioaddr + 1);
  99. }
  100. static inline int superio_inb(int sioaddr, int reg)
  101. {
  102. outb(reg, sioaddr);
  103. return inb(sioaddr + 1);
  104. }
  105. static inline void superio_exit(int sioaddr)
  106. {
  107. outb(0x02, sioaddr);
  108. outb(0x02, sioaddr + 1);
  109. }
  110. /*
  111. * Logical devices
  112. */
  113. #define REGION_LENGTH 32
  114. #define PC87427_REG_BANK 0x0f
  115. #define BANK_FM(nr) (nr)
  116. #define BANK_FT(nr) (0x08 + (nr))
  117. #define BANK_FC(nr) (0x10 + (nr) * 2)
  118. #define BANK_TM(nr) (nr)
  119. #define BANK_VM(nr) (0x08 + (nr))
  120. /*
  121. * I/O access functions
  122. */
  123. /* ldi is the logical device index */
  124. static inline int pc87427_read8(struct pc87427_data *data, u8 ldi, u8 reg)
  125. {
  126. return inb(data->address[ldi] + reg);
  127. }
  128. /* Must be called with data->lock held, except during init */
  129. static inline int pc87427_read8_bank(struct pc87427_data *data, u8 ldi,
  130. u8 bank, u8 reg)
  131. {
  132. outb(bank, data->address[ldi] + PC87427_REG_BANK);
  133. return inb(data->address[ldi] + reg);
  134. }
  135. /* Must be called with data->lock held, except during init */
  136. static inline void pc87427_write8_bank(struct pc87427_data *data, u8 ldi,
  137. u8 bank, u8 reg, u8 value)
  138. {
  139. outb(bank, data->address[ldi] + PC87427_REG_BANK);
  140. outb(value, data->address[ldi] + reg);
  141. }
  142. /*
  143. * Fan registers and conversions
  144. */
  145. /* fan data registers are 16-bit wide */
  146. #define PC87427_REG_FAN 0x12
  147. #define PC87427_REG_FAN_MIN 0x14
  148. #define PC87427_REG_FAN_STATUS 0x10
  149. #define FAN_STATUS_STALL (1 << 3)
  150. #define FAN_STATUS_LOSPD (1 << 1)
  151. #define FAN_STATUS_MONEN (1 << 0)
  152. /*
  153. * Dedicated function to read all registers related to a given fan input.
  154. * This saves us quite a few locks and bank selections.
  155. * Must be called with data->lock held.
  156. * nr is from 0 to 7
  157. */
  158. static void pc87427_readall_fan(struct pc87427_data *data, u8 nr)
  159. {
  160. int iobase = data->address[LD_FAN];
  161. outb(BANK_FM(nr), iobase + PC87427_REG_BANK);
  162. data->fan[nr] = inw(iobase + PC87427_REG_FAN);
  163. data->fan_min[nr] = inw(iobase + PC87427_REG_FAN_MIN);
  164. data->fan_status[nr] = inb(iobase + PC87427_REG_FAN_STATUS);
  165. /* Clear fan alarm bits */
  166. outb(data->fan_status[nr], iobase + PC87427_REG_FAN_STATUS);
  167. }
  168. /*
  169. * The 2 LSB of fan speed registers are used for something different.
  170. * The actual 2 LSB of the measurements are not available.
  171. */
  172. static inline unsigned long fan_from_reg(u16 reg)
  173. {
  174. reg &= 0xfffc;
  175. if (reg == 0x0000 || reg == 0xfffc)
  176. return 0;
  177. return 5400000UL / reg;
  178. }
  179. /* The 2 LSB of the fan speed limit registers are not significant. */
  180. static inline u16 fan_to_reg(unsigned long val)
  181. {
  182. if (val < 83UL)
  183. return 0xffff;
  184. if (val >= 1350000UL)
  185. return 0x0004;
  186. return ((1350000UL + val / 2) / val) << 2;
  187. }
  188. /*
  189. * PWM registers and conversions
  190. */
  191. #define PC87427_REG_PWM_ENABLE 0x10
  192. #define PC87427_REG_PWM_DUTY 0x12
  193. #define PWM_ENABLE_MODE_MASK (7 << 4)
  194. #define PWM_ENABLE_CTLEN (1 << 0)
  195. #define PWM_MODE_MANUAL (0 << 4)
  196. #define PWM_MODE_AUTO (1 << 4)
  197. #define PWM_MODE_OFF (2 << 4)
  198. #define PWM_MODE_ON (7 << 4)
  199. /*
  200. * Dedicated function to read all registers related to a given PWM output.
  201. * This saves us quite a few locks and bank selections.
  202. * Must be called with data->lock held.
  203. * nr is from 0 to 3
  204. */
  205. static void pc87427_readall_pwm(struct pc87427_data *data, u8 nr)
  206. {
  207. int iobase = data->address[LD_FAN];
  208. outb(BANK_FC(nr), iobase + PC87427_REG_BANK);
  209. data->pwm_enable[nr] = inb(iobase + PC87427_REG_PWM_ENABLE);
  210. data->pwm[nr] = inb(iobase + PC87427_REG_PWM_DUTY);
  211. }
  212. static inline int pwm_enable_from_reg(u8 reg)
  213. {
  214. switch (reg & PWM_ENABLE_MODE_MASK) {
  215. case PWM_MODE_ON:
  216. return 0;
  217. case PWM_MODE_MANUAL:
  218. case PWM_MODE_OFF:
  219. return 1;
  220. case PWM_MODE_AUTO:
  221. return 2;
  222. default:
  223. return -EPROTO;
  224. }
  225. }
  226. static inline u8 pwm_enable_to_reg(unsigned long val, u8 pwmval)
  227. {
  228. switch (val) {
  229. default:
  230. return PWM_MODE_ON;
  231. case 1:
  232. return pwmval ? PWM_MODE_MANUAL : PWM_MODE_OFF;
  233. case 2:
  234. return PWM_MODE_AUTO;
  235. }
  236. }
  237. /*
  238. * Temperature registers and conversions
  239. */
  240. #define PC87427_REG_TEMP_STATUS 0x10
  241. #define PC87427_REG_TEMP 0x14
  242. #define PC87427_REG_TEMP_MAX 0x18
  243. #define PC87427_REG_TEMP_MIN 0x19
  244. #define PC87427_REG_TEMP_CRIT 0x1a
  245. #define PC87427_REG_TEMP_TYPE 0x1d
  246. #define TEMP_STATUS_CHANEN (1 << 0)
  247. #define TEMP_STATUS_LOWFLG (1 << 1)
  248. #define TEMP_STATUS_HIGHFLG (1 << 2)
  249. #define TEMP_STATUS_CRITFLG (1 << 3)
  250. #define TEMP_STATUS_SENSERR (1 << 5)
  251. #define TEMP_TYPE_MASK (3 << 5)
  252. #define TEMP_TYPE_THERMISTOR (1 << 5)
  253. #define TEMP_TYPE_REMOTE_DIODE (2 << 5)
  254. #define TEMP_TYPE_LOCAL_DIODE (3 << 5)
  255. /*
  256. * Dedicated function to read all registers related to a given temperature
  257. * input. This saves us quite a few locks and bank selections.
  258. * Must be called with data->lock held.
  259. * nr is from 0 to 5
  260. */
  261. static void pc87427_readall_temp(struct pc87427_data *data, u8 nr)
  262. {
  263. int iobase = data->address[LD_TEMP];
  264. outb(BANK_TM(nr), iobase + PC87427_REG_BANK);
  265. data->temp[nr] = le16_to_cpu(inw(iobase + PC87427_REG_TEMP));
  266. data->temp_max[nr] = inb(iobase + PC87427_REG_TEMP_MAX);
  267. data->temp_min[nr] = inb(iobase + PC87427_REG_TEMP_MIN);
  268. data->temp_crit[nr] = inb(iobase + PC87427_REG_TEMP_CRIT);
  269. data->temp_type[nr] = inb(iobase + PC87427_REG_TEMP_TYPE);
  270. data->temp_status[nr] = inb(iobase + PC87427_REG_TEMP_STATUS);
  271. /* Clear fan alarm bits */
  272. outb(data->temp_status[nr], iobase + PC87427_REG_TEMP_STATUS);
  273. }
  274. static inline unsigned int temp_type_from_reg(u8 reg)
  275. {
  276. switch (reg & TEMP_TYPE_MASK) {
  277. case TEMP_TYPE_THERMISTOR:
  278. return 4;
  279. case TEMP_TYPE_REMOTE_DIODE:
  280. case TEMP_TYPE_LOCAL_DIODE:
  281. return 3;
  282. default:
  283. return 0;
  284. }
  285. }
  286. /*
  287. * We assume 8-bit thermal sensors; 9-bit thermal sensors are possible
  288. * too, but I have no idea how to figure out when they are used.
  289. */
  290. static inline long temp_from_reg(s16 reg)
  291. {
  292. return reg * 1000 / 256;
  293. }
  294. static inline long temp_from_reg8(s8 reg)
  295. {
  296. return reg * 1000;
  297. }
  298. /*
  299. * Data interface
  300. */
  301. static struct pc87427_data *pc87427_update_device(struct device *dev)
  302. {
  303. struct pc87427_data *data = dev_get_drvdata(dev);
  304. int i;
  305. mutex_lock(&data->lock);
  306. if (!time_after(jiffies, data->last_updated + HZ)
  307. && data->last_updated)
  308. goto done;
  309. /* Fans */
  310. for (i = 0; i < 8; i++) {
  311. if (!(data->fan_enabled & (1 << i)))
  312. continue;
  313. pc87427_readall_fan(data, i);
  314. }
  315. /* PWM outputs */
  316. for (i = 0; i < 4; i++) {
  317. if (!(data->pwm_enabled & (1 << i)))
  318. continue;
  319. pc87427_readall_pwm(data, i);
  320. }
  321. /* Temperature channels */
  322. for (i = 0; i < 6; i++) {
  323. if (!(data->temp_enabled & (1 << i)))
  324. continue;
  325. pc87427_readall_temp(data, i);
  326. }
  327. data->last_updated = jiffies;
  328. done:
  329. mutex_unlock(&data->lock);
  330. return data;
  331. }
  332. static ssize_t show_fan_input(struct device *dev, struct device_attribute
  333. *devattr, char *buf)
  334. {
  335. struct pc87427_data *data = pc87427_update_device(dev);
  336. int nr = to_sensor_dev_attr(devattr)->index;
  337. return sprintf(buf, "%lu\n", fan_from_reg(data->fan[nr]));
  338. }
  339. static ssize_t show_fan_min(struct device *dev, struct device_attribute
  340. *devattr, char *buf)
  341. {
  342. struct pc87427_data *data = pc87427_update_device(dev);
  343. int nr = to_sensor_dev_attr(devattr)->index;
  344. return sprintf(buf, "%lu\n", fan_from_reg(data->fan_min[nr]));
  345. }
  346. static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
  347. *devattr, char *buf)
  348. {
  349. struct pc87427_data *data = pc87427_update_device(dev);
  350. int nr = to_sensor_dev_attr(devattr)->index;
  351. return sprintf(buf, "%d\n", !!(data->fan_status[nr]
  352. & FAN_STATUS_LOSPD));
  353. }
  354. static ssize_t show_fan_fault(struct device *dev, struct device_attribute
  355. *devattr, char *buf)
  356. {
  357. struct pc87427_data *data = pc87427_update_device(dev);
  358. int nr = to_sensor_dev_attr(devattr)->index;
  359. return sprintf(buf, "%d\n", !!(data->fan_status[nr]
  360. & FAN_STATUS_STALL));
  361. }
  362. static ssize_t set_fan_min(struct device *dev, struct device_attribute
  363. *devattr, const char *buf, size_t count)
  364. {
  365. struct pc87427_data *data = dev_get_drvdata(dev);
  366. int nr = to_sensor_dev_attr(devattr)->index;
  367. unsigned long val;
  368. int iobase = data->address[LD_FAN];
  369. if (kstrtoul(buf, 10, &val) < 0)
  370. return -EINVAL;
  371. mutex_lock(&data->lock);
  372. outb(BANK_FM(nr), iobase + PC87427_REG_BANK);
  373. /*
  374. * The low speed limit registers are read-only while monitoring
  375. * is enabled, so we have to disable monitoring, then change the
  376. * limit, and finally enable monitoring again.
  377. */
  378. outb(0, iobase + PC87427_REG_FAN_STATUS);
  379. data->fan_min[nr] = fan_to_reg(val);
  380. outw(data->fan_min[nr], iobase + PC87427_REG_FAN_MIN);
  381. outb(FAN_STATUS_MONEN, iobase + PC87427_REG_FAN_STATUS);
  382. mutex_unlock(&data->lock);
  383. return count;
  384. }
  385. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
  386. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
  387. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2);
  388. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_input, NULL, 3);
  389. static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan_input, NULL, 4);
  390. static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan_input, NULL, 5);
  391. static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan_input, NULL, 6);
  392. static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan_input, NULL, 7);
  393. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
  394. show_fan_min, set_fan_min, 0);
  395. static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
  396. show_fan_min, set_fan_min, 1);
  397. static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO,
  398. show_fan_min, set_fan_min, 2);
  399. static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO,
  400. show_fan_min, set_fan_min, 3);
  401. static SENSOR_DEVICE_ATTR(fan5_min, S_IWUSR | S_IRUGO,
  402. show_fan_min, set_fan_min, 4);
  403. static SENSOR_DEVICE_ATTR(fan6_min, S_IWUSR | S_IRUGO,
  404. show_fan_min, set_fan_min, 5);
  405. static SENSOR_DEVICE_ATTR(fan7_min, S_IWUSR | S_IRUGO,
  406. show_fan_min, set_fan_min, 6);
  407. static SENSOR_DEVICE_ATTR(fan8_min, S_IWUSR | S_IRUGO,
  408. show_fan_min, set_fan_min, 7);
  409. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0);
  410. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 1);
  411. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 2);
  412. static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 3);
  413. static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_fan_alarm, NULL, 4);
  414. static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_fan_alarm, NULL, 5);
  415. static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_fan_alarm, NULL, 6);
  416. static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_fan_alarm, NULL, 7);
  417. static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL, 0);
  418. static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault, NULL, 1);
  419. static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, show_fan_fault, NULL, 2);
  420. static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, show_fan_fault, NULL, 3);
  421. static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, show_fan_fault, NULL, 4);
  422. static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, show_fan_fault, NULL, 5);
  423. static SENSOR_DEVICE_ATTR(fan7_fault, S_IRUGO, show_fan_fault, NULL, 6);
  424. static SENSOR_DEVICE_ATTR(fan8_fault, S_IRUGO, show_fan_fault, NULL, 7);
  425. static struct attribute *pc87427_attributes_fan[8][5] = {
  426. {
  427. &sensor_dev_attr_fan1_input.dev_attr.attr,
  428. &sensor_dev_attr_fan1_min.dev_attr.attr,
  429. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  430. &sensor_dev_attr_fan1_fault.dev_attr.attr,
  431. NULL
  432. }, {
  433. &sensor_dev_attr_fan2_input.dev_attr.attr,
  434. &sensor_dev_attr_fan2_min.dev_attr.attr,
  435. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  436. &sensor_dev_attr_fan2_fault.dev_attr.attr,
  437. NULL
  438. }, {
  439. &sensor_dev_attr_fan3_input.dev_attr.attr,
  440. &sensor_dev_attr_fan3_min.dev_attr.attr,
  441. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  442. &sensor_dev_attr_fan3_fault.dev_attr.attr,
  443. NULL
  444. }, {
  445. &sensor_dev_attr_fan4_input.dev_attr.attr,
  446. &sensor_dev_attr_fan4_min.dev_attr.attr,
  447. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  448. &sensor_dev_attr_fan4_fault.dev_attr.attr,
  449. NULL
  450. }, {
  451. &sensor_dev_attr_fan5_input.dev_attr.attr,
  452. &sensor_dev_attr_fan5_min.dev_attr.attr,
  453. &sensor_dev_attr_fan5_alarm.dev_attr.attr,
  454. &sensor_dev_attr_fan5_fault.dev_attr.attr,
  455. NULL
  456. }, {
  457. &sensor_dev_attr_fan6_input.dev_attr.attr,
  458. &sensor_dev_attr_fan6_min.dev_attr.attr,
  459. &sensor_dev_attr_fan6_alarm.dev_attr.attr,
  460. &sensor_dev_attr_fan6_fault.dev_attr.attr,
  461. NULL
  462. }, {
  463. &sensor_dev_attr_fan7_input.dev_attr.attr,
  464. &sensor_dev_attr_fan7_min.dev_attr.attr,
  465. &sensor_dev_attr_fan7_alarm.dev_attr.attr,
  466. &sensor_dev_attr_fan7_fault.dev_attr.attr,
  467. NULL
  468. }, {
  469. &sensor_dev_attr_fan8_input.dev_attr.attr,
  470. &sensor_dev_attr_fan8_min.dev_attr.attr,
  471. &sensor_dev_attr_fan8_alarm.dev_attr.attr,
  472. &sensor_dev_attr_fan8_fault.dev_attr.attr,
  473. NULL
  474. }
  475. };
  476. static const struct attribute_group pc87427_group_fan[8] = {
  477. { .attrs = pc87427_attributes_fan[0] },
  478. { .attrs = pc87427_attributes_fan[1] },
  479. { .attrs = pc87427_attributes_fan[2] },
  480. { .attrs = pc87427_attributes_fan[3] },
  481. { .attrs = pc87427_attributes_fan[4] },
  482. { .attrs = pc87427_attributes_fan[5] },
  483. { .attrs = pc87427_attributes_fan[6] },
  484. { .attrs = pc87427_attributes_fan[7] },
  485. };
  486. /*
  487. * Must be called with data->lock held and pc87427_readall_pwm() freshly
  488. * called
  489. */
  490. static void update_pwm_enable(struct pc87427_data *data, int nr, u8 mode)
  491. {
  492. int iobase = data->address[LD_FAN];
  493. data->pwm_enable[nr] &= ~PWM_ENABLE_MODE_MASK;
  494. data->pwm_enable[nr] |= mode;
  495. outb(data->pwm_enable[nr], iobase + PC87427_REG_PWM_ENABLE);
  496. }
  497. static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
  498. *devattr, char *buf)
  499. {
  500. struct pc87427_data *data = pc87427_update_device(dev);
  501. int nr = to_sensor_dev_attr(devattr)->index;
  502. int pwm_enable;
  503. pwm_enable = pwm_enable_from_reg(data->pwm_enable[nr]);
  504. if (pwm_enable < 0)
  505. return pwm_enable;
  506. return sprintf(buf, "%d\n", pwm_enable);
  507. }
  508. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
  509. *devattr, const char *buf, size_t count)
  510. {
  511. struct pc87427_data *data = dev_get_drvdata(dev);
  512. int nr = to_sensor_dev_attr(devattr)->index;
  513. unsigned long val;
  514. if (kstrtoul(buf, 10, &val) < 0 || val > 2)
  515. return -EINVAL;
  516. /* Can't go to automatic mode if it isn't configured */
  517. if (val == 2 && !(data->pwm_auto_ok & (1 << nr)))
  518. return -EINVAL;
  519. mutex_lock(&data->lock);
  520. pc87427_readall_pwm(data, nr);
  521. update_pwm_enable(data, nr, pwm_enable_to_reg(val, data->pwm[nr]));
  522. mutex_unlock(&data->lock);
  523. return count;
  524. }
  525. static ssize_t show_pwm(struct device *dev, struct device_attribute
  526. *devattr, char *buf)
  527. {
  528. struct pc87427_data *data = pc87427_update_device(dev);
  529. int nr = to_sensor_dev_attr(devattr)->index;
  530. return sprintf(buf, "%d\n", (int)data->pwm[nr]);
  531. }
  532. static ssize_t set_pwm(struct device *dev, struct device_attribute
  533. *devattr, const char *buf, size_t count)
  534. {
  535. struct pc87427_data *data = dev_get_drvdata(dev);
  536. int nr = to_sensor_dev_attr(devattr)->index;
  537. unsigned long val;
  538. int iobase = data->address[LD_FAN];
  539. u8 mode;
  540. if (kstrtoul(buf, 10, &val) < 0 || val > 0xff)
  541. return -EINVAL;
  542. mutex_lock(&data->lock);
  543. pc87427_readall_pwm(data, nr);
  544. mode = data->pwm_enable[nr] & PWM_ENABLE_MODE_MASK;
  545. if (mode != PWM_MODE_MANUAL && mode != PWM_MODE_OFF) {
  546. dev_notice(dev, "Can't set PWM%d duty cycle while not in "
  547. "manual mode\n", nr + 1);
  548. mutex_unlock(&data->lock);
  549. return -EPERM;
  550. }
  551. /* We may have to change the mode */
  552. if (mode == PWM_MODE_MANUAL && val == 0) {
  553. /* Transition from Manual to Off */
  554. update_pwm_enable(data, nr, PWM_MODE_OFF);
  555. mode = PWM_MODE_OFF;
  556. dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1,
  557. "manual", "off");
  558. } else if (mode == PWM_MODE_OFF && val != 0) {
  559. /* Transition from Off to Manual */
  560. update_pwm_enable(data, nr, PWM_MODE_MANUAL);
  561. mode = PWM_MODE_MANUAL;
  562. dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1,
  563. "off", "manual");
  564. }
  565. data->pwm[nr] = val;
  566. if (mode == PWM_MODE_MANUAL)
  567. outb(val, iobase + PC87427_REG_PWM_DUTY);
  568. mutex_unlock(&data->lock);
  569. return count;
  570. }
  571. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  572. show_pwm_enable, set_pwm_enable, 0);
  573. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
  574. show_pwm_enable, set_pwm_enable, 1);
  575. static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
  576. show_pwm_enable, set_pwm_enable, 2);
  577. static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO,
  578. show_pwm_enable, set_pwm_enable, 3);
  579. static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
  580. static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
  581. static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
  582. static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
  583. static struct attribute *pc87427_attributes_pwm[4][3] = {
  584. {
  585. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  586. &sensor_dev_attr_pwm1.dev_attr.attr,
  587. NULL
  588. }, {
  589. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  590. &sensor_dev_attr_pwm2.dev_attr.attr,
  591. NULL
  592. }, {
  593. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  594. &sensor_dev_attr_pwm3.dev_attr.attr,
  595. NULL
  596. }, {
  597. &sensor_dev_attr_pwm4_enable.dev_attr.attr,
  598. &sensor_dev_attr_pwm4.dev_attr.attr,
  599. NULL
  600. }
  601. };
  602. static const struct attribute_group pc87427_group_pwm[4] = {
  603. { .attrs = pc87427_attributes_pwm[0] },
  604. { .attrs = pc87427_attributes_pwm[1] },
  605. { .attrs = pc87427_attributes_pwm[2] },
  606. { .attrs = pc87427_attributes_pwm[3] },
  607. };
  608. static ssize_t show_temp_input(struct device *dev, struct device_attribute
  609. *devattr, char *buf)
  610. {
  611. struct pc87427_data *data = pc87427_update_device(dev);
  612. int nr = to_sensor_dev_attr(devattr)->index;
  613. return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
  614. }
  615. static ssize_t show_temp_min(struct device *dev, struct device_attribute
  616. *devattr, char *buf)
  617. {
  618. struct pc87427_data *data = pc87427_update_device(dev);
  619. int nr = to_sensor_dev_attr(devattr)->index;
  620. return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_min[nr]));
  621. }
  622. static ssize_t show_temp_max(struct device *dev, struct device_attribute
  623. *devattr, char *buf)
  624. {
  625. struct pc87427_data *data = pc87427_update_device(dev);
  626. int nr = to_sensor_dev_attr(devattr)->index;
  627. return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_max[nr]));
  628. }
  629. static ssize_t show_temp_crit(struct device *dev, struct device_attribute
  630. *devattr, char *buf)
  631. {
  632. struct pc87427_data *data = pc87427_update_device(dev);
  633. int nr = to_sensor_dev_attr(devattr)->index;
  634. return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_crit[nr]));
  635. }
  636. static ssize_t show_temp_type(struct device *dev, struct device_attribute
  637. *devattr, char *buf)
  638. {
  639. struct pc87427_data *data = pc87427_update_device(dev);
  640. int nr = to_sensor_dev_attr(devattr)->index;
  641. return sprintf(buf, "%u\n", temp_type_from_reg(data->temp_type[nr]));
  642. }
  643. static ssize_t show_temp_min_alarm(struct device *dev, struct device_attribute
  644. *devattr, char *buf)
  645. {
  646. struct pc87427_data *data = pc87427_update_device(dev);
  647. int nr = to_sensor_dev_attr(devattr)->index;
  648. return sprintf(buf, "%d\n", !!(data->temp_status[nr]
  649. & TEMP_STATUS_LOWFLG));
  650. }
  651. static ssize_t show_temp_max_alarm(struct device *dev, struct device_attribute
  652. *devattr, char *buf)
  653. {
  654. struct pc87427_data *data = pc87427_update_device(dev);
  655. int nr = to_sensor_dev_attr(devattr)->index;
  656. return sprintf(buf, "%d\n", !!(data->temp_status[nr]
  657. & TEMP_STATUS_HIGHFLG));
  658. }
  659. static ssize_t show_temp_crit_alarm(struct device *dev, struct device_attribute
  660. *devattr, char *buf)
  661. {
  662. struct pc87427_data *data = pc87427_update_device(dev);
  663. int nr = to_sensor_dev_attr(devattr)->index;
  664. return sprintf(buf, "%d\n", !!(data->temp_status[nr]
  665. & TEMP_STATUS_CRITFLG));
  666. }
  667. static ssize_t show_temp_fault(struct device *dev, struct device_attribute
  668. *devattr, char *buf)
  669. {
  670. struct pc87427_data *data = pc87427_update_device(dev);
  671. int nr = to_sensor_dev_attr(devattr)->index;
  672. return sprintf(buf, "%d\n", !!(data->temp_status[nr]
  673. & TEMP_STATUS_SENSERR));
  674. }
  675. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
  676. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1);
  677. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2);
  678. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input, NULL, 3);
  679. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_input, NULL, 4);
  680. static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_input, NULL, 5);
  681. static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min, NULL, 0);
  682. static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min, NULL, 1);
  683. static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min, NULL, 2);
  684. static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min, NULL, 3);
  685. static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min, NULL, 4);
  686. static SENSOR_DEVICE_ATTR(temp6_min, S_IRUGO, show_temp_min, NULL, 5);
  687. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL, 0);
  688. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, NULL, 1);
  689. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, NULL, 2);
  690. static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, NULL, 3);
  691. static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, NULL, 4);
  692. static SENSOR_DEVICE_ATTR(temp6_max, S_IRUGO, show_temp_max, NULL, 5);
  693. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0);
  694. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL, 1);
  695. static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit, NULL, 2);
  696. static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL, 3);
  697. static SENSOR_DEVICE_ATTR(temp5_crit, S_IRUGO, show_temp_crit, NULL, 4);
  698. static SENSOR_DEVICE_ATTR(temp6_crit, S_IRUGO, show_temp_crit, NULL, 5);
  699. static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
  700. static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
  701. static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
  702. static SENSOR_DEVICE_ATTR(temp4_type, S_IRUGO, show_temp_type, NULL, 3);
  703. static SENSOR_DEVICE_ATTR(temp5_type, S_IRUGO, show_temp_type, NULL, 4);
  704. static SENSOR_DEVICE_ATTR(temp6_type, S_IRUGO, show_temp_type, NULL, 5);
  705. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
  706. show_temp_min_alarm, NULL, 0);
  707. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO,
  708. show_temp_min_alarm, NULL, 1);
  709. static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO,
  710. show_temp_min_alarm, NULL, 2);
  711. static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO,
  712. show_temp_min_alarm, NULL, 3);
  713. static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO,
  714. show_temp_min_alarm, NULL, 4);
  715. static SENSOR_DEVICE_ATTR(temp6_min_alarm, S_IRUGO,
  716. show_temp_min_alarm, NULL, 5);
  717. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
  718. show_temp_max_alarm, NULL, 0);
  719. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO,
  720. show_temp_max_alarm, NULL, 1);
  721. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO,
  722. show_temp_max_alarm, NULL, 2);
  723. static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO,
  724. show_temp_max_alarm, NULL, 3);
  725. static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO,
  726. show_temp_max_alarm, NULL, 4);
  727. static SENSOR_DEVICE_ATTR(temp6_max_alarm, S_IRUGO,
  728. show_temp_max_alarm, NULL, 5);
  729. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
  730. show_temp_crit_alarm, NULL, 0);
  731. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO,
  732. show_temp_crit_alarm, NULL, 1);
  733. static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO,
  734. show_temp_crit_alarm, NULL, 2);
  735. static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO,
  736. show_temp_crit_alarm, NULL, 3);
  737. static SENSOR_DEVICE_ATTR(temp5_crit_alarm, S_IRUGO,
  738. show_temp_crit_alarm, NULL, 4);
  739. static SENSOR_DEVICE_ATTR(temp6_crit_alarm, S_IRUGO,
  740. show_temp_crit_alarm, NULL, 5);
  741. static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
  742. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
  743. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
  744. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
  745. static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4);
  746. static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5);
  747. static struct attribute *pc87427_attributes_temp[6][10] = {
  748. {
  749. &sensor_dev_attr_temp1_input.dev_attr.attr,
  750. &sensor_dev_attr_temp1_min.dev_attr.attr,
  751. &sensor_dev_attr_temp1_max.dev_attr.attr,
  752. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  753. &sensor_dev_attr_temp1_type.dev_attr.attr,
  754. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  755. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  756. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  757. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  758. NULL
  759. }, {
  760. &sensor_dev_attr_temp2_input.dev_attr.attr,
  761. &sensor_dev_attr_temp2_min.dev_attr.attr,
  762. &sensor_dev_attr_temp2_max.dev_attr.attr,
  763. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  764. &sensor_dev_attr_temp2_type.dev_attr.attr,
  765. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  766. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  767. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  768. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  769. NULL
  770. }, {
  771. &sensor_dev_attr_temp3_input.dev_attr.attr,
  772. &sensor_dev_attr_temp3_min.dev_attr.attr,
  773. &sensor_dev_attr_temp3_max.dev_attr.attr,
  774. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  775. &sensor_dev_attr_temp3_type.dev_attr.attr,
  776. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  777. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  778. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  779. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  780. NULL
  781. }, {
  782. &sensor_dev_attr_temp4_input.dev_attr.attr,
  783. &sensor_dev_attr_temp4_min.dev_attr.attr,
  784. &sensor_dev_attr_temp4_max.dev_attr.attr,
  785. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  786. &sensor_dev_attr_temp4_type.dev_attr.attr,
  787. &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
  788. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  789. &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
  790. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  791. NULL
  792. }, {
  793. &sensor_dev_attr_temp5_input.dev_attr.attr,
  794. &sensor_dev_attr_temp5_min.dev_attr.attr,
  795. &sensor_dev_attr_temp5_max.dev_attr.attr,
  796. &sensor_dev_attr_temp5_crit.dev_attr.attr,
  797. &sensor_dev_attr_temp5_type.dev_attr.attr,
  798. &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
  799. &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
  800. &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
  801. &sensor_dev_attr_temp5_fault.dev_attr.attr,
  802. NULL
  803. }, {
  804. &sensor_dev_attr_temp6_input.dev_attr.attr,
  805. &sensor_dev_attr_temp6_min.dev_attr.attr,
  806. &sensor_dev_attr_temp6_max.dev_attr.attr,
  807. &sensor_dev_attr_temp6_crit.dev_attr.attr,
  808. &sensor_dev_attr_temp6_type.dev_attr.attr,
  809. &sensor_dev_attr_temp6_min_alarm.dev_attr.attr,
  810. &sensor_dev_attr_temp6_max_alarm.dev_attr.attr,
  811. &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr,
  812. &sensor_dev_attr_temp6_fault.dev_attr.attr,
  813. NULL
  814. }
  815. };
  816. static const struct attribute_group pc87427_group_temp[6] = {
  817. { .attrs = pc87427_attributes_temp[0] },
  818. { .attrs = pc87427_attributes_temp[1] },
  819. { .attrs = pc87427_attributes_temp[2] },
  820. { .attrs = pc87427_attributes_temp[3] },
  821. { .attrs = pc87427_attributes_temp[4] },
  822. { .attrs = pc87427_attributes_temp[5] },
  823. };
  824. static ssize_t show_name(struct device *dev, struct device_attribute
  825. *devattr, char *buf)
  826. {
  827. struct pc87427_data *data = dev_get_drvdata(dev);
  828. return sprintf(buf, "%s\n", data->name);
  829. }
  830. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  831. /*
  832. * Device detection, attach and detach
  833. */
  834. static void pc87427_release_regions(struct platform_device *pdev, int count)
  835. {
  836. struct resource *res;
  837. int i;
  838. for (i = 0; i < count; i++) {
  839. res = platform_get_resource(pdev, IORESOURCE_IO, i);
  840. release_region(res->start, resource_size(res));
  841. }
  842. }
  843. static int __devinit pc87427_request_regions(struct platform_device *pdev,
  844. int count)
  845. {
  846. struct resource *res;
  847. int i, err = 0;
  848. for (i = 0; i < count; i++) {
  849. res = platform_get_resource(pdev, IORESOURCE_IO, i);
  850. if (!res) {
  851. err = -ENOENT;
  852. dev_err(&pdev->dev, "Missing resource #%d\n", i);
  853. break;
  854. }
  855. if (!request_region(res->start, resource_size(res), DRVNAME)) {
  856. err = -EBUSY;
  857. dev_err(&pdev->dev,
  858. "Failed to request region 0x%lx-0x%lx\n",
  859. (unsigned long)res->start,
  860. (unsigned long)res->end);
  861. break;
  862. }
  863. }
  864. if (err && i)
  865. pc87427_release_regions(pdev, i);
  866. return err;
  867. }
  868. static void __devinit pc87427_init_device(struct device *dev)
  869. {
  870. struct pc87427_sio_data *sio_data = dev->platform_data;
  871. struct pc87427_data *data = dev_get_drvdata(dev);
  872. int i;
  873. u8 reg;
  874. /* The FMC module should be ready */
  875. reg = pc87427_read8(data, LD_FAN, PC87427_REG_BANK);
  876. if (!(reg & 0x80))
  877. dev_warn(dev, "%s module not ready!\n", "FMC");
  878. /* Check which fans are enabled */
  879. for (i = 0; i < 8; i++) {
  880. if (!(sio_data->has_fanin & (1 << i))) /* Not wired */
  881. continue;
  882. reg = pc87427_read8_bank(data, LD_FAN, BANK_FM(i),
  883. PC87427_REG_FAN_STATUS);
  884. if (reg & FAN_STATUS_MONEN)
  885. data->fan_enabled |= (1 << i);
  886. }
  887. if (!data->fan_enabled) {
  888. dev_dbg(dev, "Enabling monitoring of all fans\n");
  889. for (i = 0; i < 8; i++) {
  890. if (!(sio_data->has_fanin & (1 << i))) /* Not wired */
  891. continue;
  892. pc87427_write8_bank(data, LD_FAN, BANK_FM(i),
  893. PC87427_REG_FAN_STATUS,
  894. FAN_STATUS_MONEN);
  895. }
  896. data->fan_enabled = sio_data->has_fanin;
  897. }
  898. /* Check which PWM outputs are enabled */
  899. for (i = 0; i < 4; i++) {
  900. if (!(sio_data->has_fanout & (1 << i))) /* Not wired */
  901. continue;
  902. reg = pc87427_read8_bank(data, LD_FAN, BANK_FC(i),
  903. PC87427_REG_PWM_ENABLE);
  904. if (reg & PWM_ENABLE_CTLEN)
  905. data->pwm_enabled |= (1 << i);
  906. /*
  907. * We don't expose an interface to reconfigure the automatic
  908. * fan control mode, so only allow to return to this mode if
  909. * it was originally set.
  910. */
  911. if ((reg & PWM_ENABLE_MODE_MASK) == PWM_MODE_AUTO) {
  912. dev_dbg(dev, "PWM%d is in automatic control mode\n",
  913. i + 1);
  914. data->pwm_auto_ok |= (1 << i);
  915. }
  916. }
  917. /* The HMC module should be ready */
  918. reg = pc87427_read8(data, LD_TEMP, PC87427_REG_BANK);
  919. if (!(reg & 0x80))
  920. dev_warn(dev, "%s module not ready!\n", "HMC");
  921. /* Check which temperature channels are enabled */
  922. for (i = 0; i < 6; i++) {
  923. reg = pc87427_read8_bank(data, LD_TEMP, BANK_TM(i),
  924. PC87427_REG_TEMP_STATUS);
  925. if (reg & TEMP_STATUS_CHANEN)
  926. data->temp_enabled |= (1 << i);
  927. }
  928. }
  929. static void pc87427_remove_files(struct device *dev)
  930. {
  931. struct pc87427_data *data = dev_get_drvdata(dev);
  932. int i;
  933. device_remove_file(dev, &dev_attr_name);
  934. for (i = 0; i < 8; i++) {
  935. if (!(data->fan_enabled & (1 << i)))
  936. continue;
  937. sysfs_remove_group(&dev->kobj, &pc87427_group_fan[i]);
  938. }
  939. for (i = 0; i < 4; i++) {
  940. if (!(data->pwm_enabled & (1 << i)))
  941. continue;
  942. sysfs_remove_group(&dev->kobj, &pc87427_group_pwm[i]);
  943. }
  944. for (i = 0; i < 6; i++) {
  945. if (!(data->temp_enabled & (1 << i)))
  946. continue;
  947. sysfs_remove_group(&dev->kobj, &pc87427_group_temp[i]);
  948. }
  949. }
  950. static int __devinit pc87427_probe(struct platform_device *pdev)
  951. {
  952. struct pc87427_sio_data *sio_data = pdev->dev.platform_data;
  953. struct pc87427_data *data;
  954. int i, err, res_count;
  955. data = kzalloc(sizeof(struct pc87427_data), GFP_KERNEL);
  956. if (!data) {
  957. err = -ENOMEM;
  958. pr_err("Out of memory\n");
  959. goto exit;
  960. }
  961. data->address[0] = sio_data->address[0];
  962. data->address[1] = sio_data->address[1];
  963. res_count = (data->address[0] != 0) + (data->address[1] != 0);
  964. err = pc87427_request_regions(pdev, res_count);
  965. if (err)
  966. goto exit_kfree;
  967. mutex_init(&data->lock);
  968. data->name = "pc87427";
  969. platform_set_drvdata(pdev, data);
  970. pc87427_init_device(&pdev->dev);
  971. /* Register sysfs hooks */
  972. err = device_create_file(&pdev->dev, &dev_attr_name);
  973. if (err)
  974. goto exit_release_region;
  975. for (i = 0; i < 8; i++) {
  976. if (!(data->fan_enabled & (1 << i)))
  977. continue;
  978. err = sysfs_create_group(&pdev->dev.kobj,
  979. &pc87427_group_fan[i]);
  980. if (err)
  981. goto exit_remove_files;
  982. }
  983. for (i = 0; i < 4; i++) {
  984. if (!(data->pwm_enabled & (1 << i)))
  985. continue;
  986. err = sysfs_create_group(&pdev->dev.kobj,
  987. &pc87427_group_pwm[i]);
  988. if (err)
  989. goto exit_remove_files;
  990. }
  991. for (i = 0; i < 6; i++) {
  992. if (!(data->temp_enabled & (1 << i)))
  993. continue;
  994. err = sysfs_create_group(&pdev->dev.kobj,
  995. &pc87427_group_temp[i]);
  996. if (err)
  997. goto exit_remove_files;
  998. }
  999. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  1000. if (IS_ERR(data->hwmon_dev)) {
  1001. err = PTR_ERR(data->hwmon_dev);
  1002. dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
  1003. goto exit_remove_files;
  1004. }
  1005. return 0;
  1006. exit_remove_files:
  1007. pc87427_remove_files(&pdev->dev);
  1008. exit_release_region:
  1009. pc87427_release_regions(pdev, res_count);
  1010. exit_kfree:
  1011. platform_set_drvdata(pdev, NULL);
  1012. kfree(data);
  1013. exit:
  1014. return err;
  1015. }
  1016. static int __devexit pc87427_remove(struct platform_device *pdev)
  1017. {
  1018. struct pc87427_data *data = platform_get_drvdata(pdev);
  1019. int res_count;
  1020. res_count = (data->address[0] != 0) + (data->address[1] != 0);
  1021. hwmon_device_unregister(data->hwmon_dev);
  1022. pc87427_remove_files(&pdev->dev);
  1023. platform_set_drvdata(pdev, NULL);
  1024. kfree(data);
  1025. pc87427_release_regions(pdev, res_count);
  1026. return 0;
  1027. }
  1028. static struct platform_driver pc87427_driver = {
  1029. .driver = {
  1030. .owner = THIS_MODULE,
  1031. .name = DRVNAME,
  1032. },
  1033. .probe = pc87427_probe,
  1034. .remove = __devexit_p(pc87427_remove),
  1035. };
  1036. static int __init pc87427_device_add(const struct pc87427_sio_data *sio_data)
  1037. {
  1038. struct resource res[2] = {
  1039. { .flags = IORESOURCE_IO },
  1040. { .flags = IORESOURCE_IO },
  1041. };
  1042. int err, i, res_count;
  1043. res_count = 0;
  1044. for (i = 0; i < 2; i++) {
  1045. if (!sio_data->address[i])
  1046. continue;
  1047. res[res_count].start = sio_data->address[i];
  1048. res[res_count].end = sio_data->address[i] + REGION_LENGTH - 1;
  1049. res[res_count].name = logdev_str[i];
  1050. err = acpi_check_resource_conflict(&res[res_count]);
  1051. if (err)
  1052. goto exit;
  1053. res_count++;
  1054. }
  1055. pdev = platform_device_alloc(DRVNAME, res[0].start);
  1056. if (!pdev) {
  1057. err = -ENOMEM;
  1058. pr_err("Device allocation failed\n");
  1059. goto exit;
  1060. }
  1061. err = platform_device_add_resources(pdev, res, res_count);
  1062. if (err) {
  1063. pr_err("Device resource addition failed (%d)\n", err);
  1064. goto exit_device_put;
  1065. }
  1066. err = platform_device_add_data(pdev, sio_data,
  1067. sizeof(struct pc87427_sio_data));
  1068. if (err) {
  1069. pr_err("Platform data allocation failed\n");
  1070. goto exit_device_put;
  1071. }
  1072. err = platform_device_add(pdev);
  1073. if (err) {
  1074. pr_err("Device addition failed (%d)\n", err);
  1075. goto exit_device_put;
  1076. }
  1077. return 0;
  1078. exit_device_put:
  1079. platform_device_put(pdev);
  1080. exit:
  1081. return err;
  1082. }
  1083. static int __init pc87427_find(int sioaddr, struct pc87427_sio_data *sio_data)
  1084. {
  1085. u16 val;
  1086. u8 cfg, cfg_b;
  1087. int i, err = 0;
  1088. /* Identify device */
  1089. val = force_id ? force_id : superio_inb(sioaddr, SIOREG_DEVID);
  1090. if (val != 0xf2) { /* PC87427 */
  1091. err = -ENODEV;
  1092. goto exit;
  1093. }
  1094. for (i = 0; i < 2; i++) {
  1095. sio_data->address[i] = 0;
  1096. /* Select logical device */
  1097. superio_outb(sioaddr, SIOREG_LDSEL, logdev[i]);
  1098. val = superio_inb(sioaddr, SIOREG_ACT);
  1099. if (!(val & 0x01)) {
  1100. pr_info("Logical device 0x%02x not activated\n",
  1101. logdev[i]);
  1102. continue;
  1103. }
  1104. val = superio_inb(sioaddr, SIOREG_MAP);
  1105. if (val & 0x01) {
  1106. pr_warn("Logical device 0x%02x is memory-mapped, "
  1107. "can't use\n", logdev[i]);
  1108. continue;
  1109. }
  1110. val = (superio_inb(sioaddr, SIOREG_IOBASE) << 8)
  1111. | superio_inb(sioaddr, SIOREG_IOBASE + 1);
  1112. if (!val) {
  1113. pr_info("I/O base address not set for logical device "
  1114. "0x%02x\n", logdev[i]);
  1115. continue;
  1116. }
  1117. sio_data->address[i] = val;
  1118. }
  1119. /* No point in loading the driver if everything is disabled */
  1120. if (!sio_data->address[0] && !sio_data->address[1]) {
  1121. err = -ENODEV;
  1122. goto exit;
  1123. }
  1124. /* Check which fan inputs are wired */
  1125. sio_data->has_fanin = (1 << 2) | (1 << 3); /* FANIN2, FANIN3 */
  1126. cfg = superio_inb(sioaddr, SIOREG_CF2);
  1127. if (!(cfg & (1 << 3)))
  1128. sio_data->has_fanin |= (1 << 0); /* FANIN0 */
  1129. if (!(cfg & (1 << 2)))
  1130. sio_data->has_fanin |= (1 << 4); /* FANIN4 */
  1131. cfg = superio_inb(sioaddr, SIOREG_CFD);
  1132. if (!(cfg & (1 << 0)))
  1133. sio_data->has_fanin |= (1 << 1); /* FANIN1 */
  1134. cfg = superio_inb(sioaddr, SIOREG_CF4);
  1135. if (!(cfg & (1 << 0)))
  1136. sio_data->has_fanin |= (1 << 7); /* FANIN7 */
  1137. cfg_b = superio_inb(sioaddr, SIOREG_CFB);
  1138. if (!(cfg & (1 << 1)) && (cfg_b & (1 << 3)))
  1139. sio_data->has_fanin |= (1 << 5); /* FANIN5 */
  1140. cfg = superio_inb(sioaddr, SIOREG_CF3);
  1141. if ((cfg & (1 << 3)) && !(cfg_b & (1 << 5)))
  1142. sio_data->has_fanin |= (1 << 6); /* FANIN6 */
  1143. /* Check which fan outputs are wired */
  1144. sio_data->has_fanout = (1 << 0); /* FANOUT0 */
  1145. if (cfg_b & (1 << 0))
  1146. sio_data->has_fanout |= (1 << 3); /* FANOUT3 */
  1147. cfg = superio_inb(sioaddr, SIOREG_CFC);
  1148. if (!(cfg & (1 << 4))) {
  1149. if (cfg_b & (1 << 1))
  1150. sio_data->has_fanout |= (1 << 1); /* FANOUT1 */
  1151. if (cfg_b & (1 << 2))
  1152. sio_data->has_fanout |= (1 << 2); /* FANOUT2 */
  1153. }
  1154. /* FANOUT1 and FANOUT2 can each be routed to 2 different pins */
  1155. cfg = superio_inb(sioaddr, SIOREG_CF5);
  1156. if (cfg & (1 << 6))
  1157. sio_data->has_fanout |= (1 << 1); /* FANOUT1 */
  1158. if (cfg & (1 << 5))
  1159. sio_data->has_fanout |= (1 << 2); /* FANOUT2 */
  1160. exit:
  1161. superio_exit(sioaddr);
  1162. return err;
  1163. }
  1164. static int __init pc87427_init(void)
  1165. {
  1166. int err;
  1167. struct pc87427_sio_data sio_data;
  1168. if (pc87427_find(0x2e, &sio_data)
  1169. && pc87427_find(0x4e, &sio_data))
  1170. return -ENODEV;
  1171. err = platform_driver_register(&pc87427_driver);
  1172. if (err)
  1173. goto exit;
  1174. /* Sets global pdev as a side effect */
  1175. err = pc87427_device_add(&sio_data);
  1176. if (err)
  1177. goto exit_driver;
  1178. return 0;
  1179. exit_driver:
  1180. platform_driver_unregister(&pc87427_driver);
  1181. exit:
  1182. return err;
  1183. }
  1184. static void __exit pc87427_exit(void)
  1185. {
  1186. platform_device_unregister(pdev);
  1187. platform_driver_unregister(&pc87427_driver);
  1188. }
  1189. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  1190. MODULE_DESCRIPTION("PC87427 hardware monitoring driver");
  1191. MODULE_LICENSE("GPL");
  1192. module_init(pc87427_init);
  1193. module_exit(pc87427_exit);