pc87427.c 40 KB

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