pc87427.c 40 KB

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