pc87360.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. /*
  2. * pc87360.c - Part of lm_sensors, Linux kernel modules
  3. * for hardware monitoring
  4. * Copyright (C) 2004, 2007 Jean Delvare <khali@linux-fr.org>
  5. *
  6. * Copied from smsc47m1.c:
  7. * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Supports the following chips:
  24. *
  25. * Chip #vin #fan #pwm #temp devid
  26. * PC87360 - 2 2 - 0xE1
  27. * PC87363 - 2 2 - 0xE8
  28. * PC87364 - 3 3 - 0xE4
  29. * PC87365 11 3 3 2 0xE5
  30. * PC87366 11 3 3 3-4 0xE9
  31. *
  32. * This driver assumes that no more than one chip is present, and one of
  33. * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
  34. */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/jiffies.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/hwmon.h>
  41. #include <linux/hwmon-sysfs.h>
  42. #include <linux/hwmon-vid.h>
  43. #include <linux/err.h>
  44. #include <linux/mutex.h>
  45. #include <asm/io.h>
  46. static u8 devid;
  47. static struct platform_device *pdev;
  48. static unsigned short extra_isa[3];
  49. static u8 confreg[4];
  50. static int init = 1;
  51. module_param(init, int, 0);
  52. MODULE_PARM_DESC(init,
  53. "Chip initialization level:\n"
  54. " 0: None\n"
  55. "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
  56. " 2: Forcibly enable all voltage and temperature channels, except in9\n"
  57. " 3: Forcibly enable all voltage and temperature channels, including in9");
  58. static unsigned short force_id;
  59. module_param(force_id, ushort, 0);
  60. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  61. /*
  62. * Super-I/O registers and operations
  63. */
  64. #define DEV 0x07 /* Register: Logical device select */
  65. #define DEVID 0x20 /* Register: Device ID */
  66. #define ACT 0x30 /* Register: Device activation */
  67. #define BASE 0x60 /* Register: Base address */
  68. #define FSCM 0x09 /* Logical device: fans */
  69. #define VLM 0x0d /* Logical device: voltages */
  70. #define TMS 0x0e /* Logical device: temperatures */
  71. #define LDNI_MAX 3
  72. static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
  73. #define LD_FAN 0
  74. #define LD_IN 1
  75. #define LD_TEMP 2
  76. static inline void superio_outb(int sioaddr, int reg, int val)
  77. {
  78. outb(reg, sioaddr);
  79. outb(val, sioaddr+1);
  80. }
  81. static inline int superio_inb(int sioaddr, int reg)
  82. {
  83. outb(reg, sioaddr);
  84. return inb(sioaddr+1);
  85. }
  86. static inline void superio_exit(int sioaddr)
  87. {
  88. outb(0x02, sioaddr);
  89. outb(0x02, sioaddr+1);
  90. }
  91. /*
  92. * Logical devices
  93. */
  94. #define PC87360_EXTENT 0x10
  95. #define PC87365_REG_BANK 0x09
  96. #define NO_BANK 0xff
  97. /*
  98. * Fan registers and conversions
  99. */
  100. /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
  101. #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
  102. #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
  103. #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
  104. #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
  105. #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
  106. #define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
  107. 480000 / ((val)*(div)))
  108. #define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
  109. 480000 / ((val)*(div)))
  110. #define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
  111. #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
  112. #define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
  113. #define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
  114. #define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
  115. #define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
  116. static inline u8 PWM_TO_REG(int val, int inv)
  117. {
  118. if (inv)
  119. val = 255 - val;
  120. if (val < 0)
  121. return 0;
  122. if (val > 255)
  123. return 255;
  124. return val;
  125. }
  126. /*
  127. * Voltage registers and conversions
  128. */
  129. #define PC87365_REG_IN_CONVRATE 0x07
  130. #define PC87365_REG_IN_CONFIG 0x08
  131. #define PC87365_REG_IN 0x0B
  132. #define PC87365_REG_IN_MIN 0x0D
  133. #define PC87365_REG_IN_MAX 0x0C
  134. #define PC87365_REG_IN_STATUS 0x0A
  135. #define PC87365_REG_IN_ALARMS1 0x00
  136. #define PC87365_REG_IN_ALARMS2 0x01
  137. #define PC87365_REG_VID 0x06
  138. #define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
  139. #define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
  140. (val)*256 >= (ref)*255 ? 255: \
  141. ((val) * 256 + (ref)/2) / (ref))
  142. /*
  143. * Temperature registers and conversions
  144. */
  145. #define PC87365_REG_TEMP_CONFIG 0x08
  146. #define PC87365_REG_TEMP 0x0B
  147. #define PC87365_REG_TEMP_MIN 0x0D
  148. #define PC87365_REG_TEMP_MAX 0x0C
  149. #define PC87365_REG_TEMP_CRIT 0x0E
  150. #define PC87365_REG_TEMP_STATUS 0x0A
  151. #define PC87365_REG_TEMP_ALARMS 0x00
  152. #define TEMP_FROM_REG(val) ((val) * 1000)
  153. #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
  154. (val) > 127000 ? 127 : \
  155. (val) < 0 ? ((val) - 500) / 1000 : \
  156. ((val) + 500) / 1000)
  157. /*
  158. * Device data
  159. */
  160. struct pc87360_data {
  161. const char *name;
  162. struct device *hwmon_dev;
  163. struct mutex lock;
  164. struct mutex update_lock;
  165. char valid; /* !=0 if following fields are valid */
  166. unsigned long last_updated; /* In jiffies */
  167. int address[3];
  168. u8 fannr, innr, tempnr;
  169. u8 fan[3]; /* Register value */
  170. u8 fan_min[3]; /* Register value */
  171. u8 fan_status[3]; /* Register value */
  172. u8 pwm[3]; /* Register value */
  173. u16 fan_conf; /* Configuration register values, combined */
  174. u16 in_vref; /* 1 mV/bit */
  175. u8 in[14]; /* Register value */
  176. u8 in_min[14]; /* Register value */
  177. u8 in_max[14]; /* Register value */
  178. u8 in_crit[3]; /* Register value */
  179. u8 in_status[14]; /* Register value */
  180. u16 in_alarms; /* Register values, combined, masked */
  181. u8 vid_conf; /* Configuration register value */
  182. u8 vrm;
  183. u8 vid; /* Register value */
  184. s8 temp[3]; /* Register value */
  185. s8 temp_min[3]; /* Register value */
  186. s8 temp_max[3]; /* Register value */
  187. s8 temp_crit[3]; /* Register value */
  188. u8 temp_status[3]; /* Register value */
  189. u8 temp_alarms; /* Register value, masked */
  190. };
  191. /*
  192. * Functions declaration
  193. */
  194. static int pc87360_probe(struct platform_device *pdev);
  195. static int __devexit pc87360_remove(struct platform_device *pdev);
  196. static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
  197. u8 reg);
  198. static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
  199. u8 reg, u8 value);
  200. static void pc87360_init_device(struct platform_device *pdev,
  201. int use_thermistors);
  202. static struct pc87360_data *pc87360_update_device(struct device *dev);
  203. /*
  204. * Driver data
  205. */
  206. static struct platform_driver pc87360_driver = {
  207. .driver = {
  208. .owner = THIS_MODULE,
  209. .name = "pc87360",
  210. },
  211. .probe = pc87360_probe,
  212. .remove = __devexit_p(pc87360_remove),
  213. };
  214. /*
  215. * Sysfs stuff
  216. */
  217. static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
  218. {
  219. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  220. struct pc87360_data *data = pc87360_update_device(dev);
  221. return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
  222. FAN_DIV_FROM_REG(data->fan_status[attr->index])));
  223. }
  224. static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
  225. {
  226. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  227. struct pc87360_data *data = pc87360_update_device(dev);
  228. return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
  229. FAN_DIV_FROM_REG(data->fan_status[attr->index])));
  230. }
  231. static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
  232. {
  233. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  234. struct pc87360_data *data = pc87360_update_device(dev);
  235. return sprintf(buf, "%u\n",
  236. FAN_DIV_FROM_REG(data->fan_status[attr->index]));
  237. }
  238. static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
  239. {
  240. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  241. struct pc87360_data *data = pc87360_update_device(dev);
  242. return sprintf(buf, "%u\n",
  243. FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
  244. }
  245. static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
  246. size_t count)
  247. {
  248. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  249. struct pc87360_data *data = dev_get_drvdata(dev);
  250. long fan_min = simple_strtol(buf, NULL, 10);
  251. mutex_lock(&data->update_lock);
  252. fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
  253. /* If it wouldn't fit, change clock divisor */
  254. while (fan_min > 255
  255. && (data->fan_status[attr->index] & 0x60) != 0x60) {
  256. fan_min >>= 1;
  257. data->fan[attr->index] >>= 1;
  258. data->fan_status[attr->index] += 0x20;
  259. }
  260. data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
  261. pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
  262. data->fan_min[attr->index]);
  263. /* Write new divider, preserve alarm bits */
  264. pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
  265. data->fan_status[attr->index] & 0xF9);
  266. mutex_unlock(&data->update_lock);
  267. return count;
  268. }
  269. static struct sensor_device_attribute fan_input[] = {
  270. SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
  271. SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
  272. SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
  273. };
  274. static struct sensor_device_attribute fan_status[] = {
  275. SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
  276. SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
  277. SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
  278. };
  279. static struct sensor_device_attribute fan_div[] = {
  280. SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
  281. SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
  282. SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
  283. };
  284. static struct sensor_device_attribute fan_min[] = {
  285. SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
  286. SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
  287. SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
  288. };
  289. #define FAN_UNIT_ATTRS(X) \
  290. &fan_input[X].dev_attr.attr, \
  291. &fan_status[X].dev_attr.attr, \
  292. &fan_div[X].dev_attr.attr, \
  293. &fan_min[X].dev_attr.attr
  294. static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
  295. {
  296. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  297. struct pc87360_data *data = pc87360_update_device(dev);
  298. return sprintf(buf, "%u\n",
  299. PWM_FROM_REG(data->pwm[attr->index],
  300. FAN_CONFIG_INVERT(data->fan_conf,
  301. attr->index)));
  302. }
  303. static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
  304. size_t count)
  305. {
  306. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  307. struct pc87360_data *data = dev_get_drvdata(dev);
  308. long val = simple_strtol(buf, NULL, 10);
  309. mutex_lock(&data->update_lock);
  310. data->pwm[attr->index] = PWM_TO_REG(val,
  311. FAN_CONFIG_INVERT(data->fan_conf, attr->index));
  312. pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
  313. data->pwm[attr->index]);
  314. mutex_unlock(&data->update_lock);
  315. return count;
  316. }
  317. static struct sensor_device_attribute pwm[] = {
  318. SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
  319. SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
  320. SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
  321. };
  322. static struct attribute * pc8736x_fan_attr_array[] = {
  323. FAN_UNIT_ATTRS(0),
  324. FAN_UNIT_ATTRS(1),
  325. FAN_UNIT_ATTRS(2),
  326. &pwm[0].dev_attr.attr,
  327. &pwm[1].dev_attr.attr,
  328. &pwm[2].dev_attr.attr,
  329. NULL
  330. };
  331. static const struct attribute_group pc8736x_fan_group = {
  332. .attrs = pc8736x_fan_attr_array,
  333. };
  334. static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
  335. {
  336. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  337. struct pc87360_data *data = pc87360_update_device(dev);
  338. return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
  339. data->in_vref));
  340. }
  341. static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
  342. {
  343. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  344. struct pc87360_data *data = pc87360_update_device(dev);
  345. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
  346. data->in_vref));
  347. }
  348. static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
  349. {
  350. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  351. struct pc87360_data *data = pc87360_update_device(dev);
  352. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
  353. data->in_vref));
  354. }
  355. static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
  356. {
  357. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  358. struct pc87360_data *data = pc87360_update_device(dev);
  359. return sprintf(buf, "%u\n", data->in_status[attr->index]);
  360. }
  361. static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
  362. size_t count)
  363. {
  364. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  365. struct pc87360_data *data = dev_get_drvdata(dev);
  366. long val = simple_strtol(buf, NULL, 10);
  367. mutex_lock(&data->update_lock);
  368. data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
  369. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
  370. data->in_min[attr->index]);
  371. mutex_unlock(&data->update_lock);
  372. return count;
  373. }
  374. static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
  375. size_t count)
  376. {
  377. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  378. struct pc87360_data *data = dev_get_drvdata(dev);
  379. long val = simple_strtol(buf, NULL, 10);
  380. mutex_lock(&data->update_lock);
  381. data->in_max[attr->index] = IN_TO_REG(val,
  382. data->in_vref);
  383. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
  384. data->in_max[attr->index]);
  385. mutex_unlock(&data->update_lock);
  386. return count;
  387. }
  388. static struct sensor_device_attribute in_input[] = {
  389. SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
  390. SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
  391. SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
  392. SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
  393. SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
  394. SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
  395. SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
  396. SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
  397. SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
  398. SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
  399. SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
  400. };
  401. static struct sensor_device_attribute in_status[] = {
  402. SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
  403. SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
  404. SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
  405. SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
  406. SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
  407. SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
  408. SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
  409. SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
  410. SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
  411. SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
  412. SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
  413. };
  414. static struct sensor_device_attribute in_min[] = {
  415. SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
  416. SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
  417. SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
  418. SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
  419. SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
  420. SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
  421. SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
  422. SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
  423. SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
  424. SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
  425. SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
  426. };
  427. static struct sensor_device_attribute in_max[] = {
  428. SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
  429. SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
  430. SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
  431. SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
  432. SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
  433. SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
  434. SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
  435. SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
  436. SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
  437. SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
  438. SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
  439. };
  440. /* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
  441. #define CHAN_ALM_MIN 0x02 /* min limit crossed */
  442. #define CHAN_ALM_MAX 0x04 /* max limit exceeded */
  443. #define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
  444. /* show_in_min/max_alarm() reads data from the per-channel status
  445. register (sec 11.5.12), not the vin event status registers (sec
  446. 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms) */
  447. static ssize_t show_in_min_alarm(struct device *dev,
  448. struct device_attribute *devattr, char *buf)
  449. {
  450. struct pc87360_data *data = pc87360_update_device(dev);
  451. unsigned nr = to_sensor_dev_attr(devattr)->index;
  452. return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
  453. }
  454. static ssize_t show_in_max_alarm(struct device *dev,
  455. struct device_attribute *devattr, char *buf)
  456. {
  457. struct pc87360_data *data = pc87360_update_device(dev);
  458. unsigned nr = to_sensor_dev_attr(devattr)->index;
  459. return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
  460. }
  461. static struct sensor_device_attribute in_min_alarm[] = {
  462. SENSOR_ATTR(in0_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 0),
  463. SENSOR_ATTR(in1_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 1),
  464. SENSOR_ATTR(in2_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 2),
  465. SENSOR_ATTR(in3_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 3),
  466. SENSOR_ATTR(in4_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 4),
  467. SENSOR_ATTR(in5_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 5),
  468. SENSOR_ATTR(in6_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 6),
  469. SENSOR_ATTR(in7_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 7),
  470. SENSOR_ATTR(in8_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 8),
  471. SENSOR_ATTR(in9_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 9),
  472. SENSOR_ATTR(in10_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 10),
  473. };
  474. static struct sensor_device_attribute in_max_alarm[] = {
  475. SENSOR_ATTR(in0_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 0),
  476. SENSOR_ATTR(in1_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 1),
  477. SENSOR_ATTR(in2_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 2),
  478. SENSOR_ATTR(in3_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 3),
  479. SENSOR_ATTR(in4_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 4),
  480. SENSOR_ATTR(in5_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 5),
  481. SENSOR_ATTR(in6_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 6),
  482. SENSOR_ATTR(in7_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 7),
  483. SENSOR_ATTR(in8_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 8),
  484. SENSOR_ATTR(in9_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 9),
  485. SENSOR_ATTR(in10_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 10),
  486. };
  487. #define VIN_UNIT_ATTRS(X) \
  488. &in_input[X].dev_attr.attr, \
  489. &in_status[X].dev_attr.attr, \
  490. &in_min[X].dev_attr.attr, \
  491. &in_max[X].dev_attr.attr, \
  492. &in_min_alarm[X].dev_attr.attr, \
  493. &in_max_alarm[X].dev_attr.attr
  494. static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  495. {
  496. struct pc87360_data *data = pc87360_update_device(dev);
  497. return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
  498. }
  499. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  500. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
  501. {
  502. struct pc87360_data *data = dev_get_drvdata(dev);
  503. return sprintf(buf, "%u\n", data->vrm);
  504. }
  505. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  506. {
  507. struct pc87360_data *data = dev_get_drvdata(dev);
  508. data->vrm = simple_strtoul(buf, NULL, 10);
  509. return count;
  510. }
  511. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  512. static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  513. {
  514. struct pc87360_data *data = pc87360_update_device(dev);
  515. return sprintf(buf, "%u\n", data->in_alarms);
  516. }
  517. static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
  518. static struct attribute *pc8736x_vin_attr_array[] = {
  519. VIN_UNIT_ATTRS(0),
  520. VIN_UNIT_ATTRS(1),
  521. VIN_UNIT_ATTRS(2),
  522. VIN_UNIT_ATTRS(3),
  523. VIN_UNIT_ATTRS(4),
  524. VIN_UNIT_ATTRS(5),
  525. VIN_UNIT_ATTRS(6),
  526. VIN_UNIT_ATTRS(7),
  527. VIN_UNIT_ATTRS(8),
  528. VIN_UNIT_ATTRS(9),
  529. VIN_UNIT_ATTRS(10),
  530. &dev_attr_cpu0_vid.attr,
  531. &dev_attr_vrm.attr,
  532. &dev_attr_alarms_in.attr,
  533. NULL
  534. };
  535. static const struct attribute_group pc8736x_vin_group = {
  536. .attrs = pc8736x_vin_attr_array,
  537. };
  538. static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
  539. {
  540. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  541. struct pc87360_data *data = pc87360_update_device(dev);
  542. return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
  543. data->in_vref));
  544. }
  545. static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
  546. {
  547. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  548. struct pc87360_data *data = pc87360_update_device(dev);
  549. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
  550. data->in_vref));
  551. }
  552. static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
  553. {
  554. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  555. struct pc87360_data *data = pc87360_update_device(dev);
  556. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
  557. data->in_vref));
  558. }
  559. static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
  560. {
  561. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  562. struct pc87360_data *data = pc87360_update_device(dev);
  563. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
  564. data->in_vref));
  565. }
  566. static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
  567. {
  568. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  569. struct pc87360_data *data = pc87360_update_device(dev);
  570. return sprintf(buf, "%u\n", data->in_status[attr->index]);
  571. }
  572. static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
  573. size_t count)
  574. {
  575. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  576. struct pc87360_data *data = dev_get_drvdata(dev);
  577. long val = simple_strtol(buf, NULL, 10);
  578. mutex_lock(&data->update_lock);
  579. data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
  580. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
  581. data->in_min[attr->index]);
  582. mutex_unlock(&data->update_lock);
  583. return count;
  584. }
  585. static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
  586. size_t count)
  587. {
  588. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  589. struct pc87360_data *data = dev_get_drvdata(dev);
  590. long val = simple_strtol(buf, NULL, 10);
  591. mutex_lock(&data->update_lock);
  592. data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
  593. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
  594. data->in_max[attr->index]);
  595. mutex_unlock(&data->update_lock);
  596. return count;
  597. }
  598. static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
  599. size_t count)
  600. {
  601. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  602. struct pc87360_data *data = dev_get_drvdata(dev);
  603. long val = simple_strtol(buf, NULL, 10);
  604. mutex_lock(&data->update_lock);
  605. data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
  606. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
  607. data->in_crit[attr->index-11]);
  608. mutex_unlock(&data->update_lock);
  609. return count;
  610. }
  611. /* the +11 term below reflects the fact that VLM units 11,12,13 are
  612. used in the chip to measure voltage across the thermistors
  613. */
  614. static struct sensor_device_attribute therm_input[] = {
  615. SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
  616. SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
  617. SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
  618. };
  619. static struct sensor_device_attribute therm_status[] = {
  620. SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
  621. SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
  622. SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
  623. };
  624. static struct sensor_device_attribute therm_min[] = {
  625. SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
  626. show_therm_min, set_therm_min, 0+11),
  627. SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
  628. show_therm_min, set_therm_min, 1+11),
  629. SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
  630. show_therm_min, set_therm_min, 2+11),
  631. };
  632. static struct sensor_device_attribute therm_max[] = {
  633. SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
  634. show_therm_max, set_therm_max, 0+11),
  635. SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
  636. show_therm_max, set_therm_max, 1+11),
  637. SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
  638. show_therm_max, set_therm_max, 2+11),
  639. };
  640. static struct sensor_device_attribute therm_crit[] = {
  641. SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
  642. show_therm_crit, set_therm_crit, 0+11),
  643. SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
  644. show_therm_crit, set_therm_crit, 1+11),
  645. SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
  646. show_therm_crit, set_therm_crit, 2+11),
  647. };
  648. /* show_therm_min/max_alarm() reads data from the per-channel voltage
  649. status register (sec 11.5.12) */
  650. static ssize_t show_therm_min_alarm(struct device *dev,
  651. struct device_attribute *devattr, char *buf)
  652. {
  653. struct pc87360_data *data = pc87360_update_device(dev);
  654. unsigned nr = to_sensor_dev_attr(devattr)->index;
  655. return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
  656. }
  657. static ssize_t show_therm_max_alarm(struct device *dev,
  658. struct device_attribute *devattr, char *buf)
  659. {
  660. struct pc87360_data *data = pc87360_update_device(dev);
  661. unsigned nr = to_sensor_dev_attr(devattr)->index;
  662. return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
  663. }
  664. static ssize_t show_therm_crit_alarm(struct device *dev,
  665. struct device_attribute *devattr, char *buf)
  666. {
  667. struct pc87360_data *data = pc87360_update_device(dev);
  668. unsigned nr = to_sensor_dev_attr(devattr)->index;
  669. return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
  670. }
  671. static struct sensor_device_attribute therm_min_alarm[] = {
  672. SENSOR_ATTR(temp4_min_alarm, S_IRUGO,
  673. show_therm_min_alarm, NULL, 0+11),
  674. SENSOR_ATTR(temp5_min_alarm, S_IRUGO,
  675. show_therm_min_alarm, NULL, 1+11),
  676. SENSOR_ATTR(temp6_min_alarm, S_IRUGO,
  677. show_therm_min_alarm, NULL, 2+11),
  678. };
  679. static struct sensor_device_attribute therm_max_alarm[] = {
  680. SENSOR_ATTR(temp4_max_alarm, S_IRUGO,
  681. show_therm_max_alarm, NULL, 0+11),
  682. SENSOR_ATTR(temp5_max_alarm, S_IRUGO,
  683. show_therm_max_alarm, NULL, 1+11),
  684. SENSOR_ATTR(temp6_max_alarm, S_IRUGO,
  685. show_therm_max_alarm, NULL, 2+11),
  686. };
  687. static struct sensor_device_attribute therm_crit_alarm[] = {
  688. SENSOR_ATTR(temp4_crit_alarm, S_IRUGO,
  689. show_therm_crit_alarm, NULL, 0+11),
  690. SENSOR_ATTR(temp5_crit_alarm, S_IRUGO,
  691. show_therm_crit_alarm, NULL, 1+11),
  692. SENSOR_ATTR(temp6_crit_alarm, S_IRUGO,
  693. show_therm_crit_alarm, NULL, 2+11),
  694. };
  695. #define THERM_UNIT_ATTRS(X) \
  696. &therm_input[X].dev_attr.attr, \
  697. &therm_status[X].dev_attr.attr, \
  698. &therm_min[X].dev_attr.attr, \
  699. &therm_max[X].dev_attr.attr, \
  700. &therm_crit[X].dev_attr.attr, \
  701. &therm_min_alarm[X].dev_attr.attr, \
  702. &therm_max_alarm[X].dev_attr.attr, \
  703. &therm_crit_alarm[X].dev_attr.attr
  704. static struct attribute * pc8736x_therm_attr_array[] = {
  705. THERM_UNIT_ATTRS(0),
  706. THERM_UNIT_ATTRS(1),
  707. THERM_UNIT_ATTRS(2),
  708. NULL
  709. };
  710. static const struct attribute_group pc8736x_therm_group = {
  711. .attrs = pc8736x_therm_attr_array,
  712. };
  713. static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
  714. {
  715. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  716. struct pc87360_data *data = pc87360_update_device(dev);
  717. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  718. }
  719. static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
  720. {
  721. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  722. struct pc87360_data *data = pc87360_update_device(dev);
  723. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
  724. }
  725. static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
  726. {
  727. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  728. struct pc87360_data *data = pc87360_update_device(dev);
  729. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
  730. }
  731. static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
  732. {
  733. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  734. struct pc87360_data *data = pc87360_update_device(dev);
  735. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
  736. }
  737. static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
  738. {
  739. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  740. struct pc87360_data *data = pc87360_update_device(dev);
  741. return sprintf(buf, "%d\n", data->temp_status[attr->index]);
  742. }
  743. static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
  744. size_t count)
  745. {
  746. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  747. struct pc87360_data *data = dev_get_drvdata(dev);
  748. long val = simple_strtol(buf, NULL, 10);
  749. mutex_lock(&data->update_lock);
  750. data->temp_min[attr->index] = TEMP_TO_REG(val);
  751. pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
  752. data->temp_min[attr->index]);
  753. mutex_unlock(&data->update_lock);
  754. return count;
  755. }
  756. static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
  757. size_t count)
  758. {
  759. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  760. struct pc87360_data *data = dev_get_drvdata(dev);
  761. long val = simple_strtol(buf, NULL, 10);
  762. mutex_lock(&data->update_lock);
  763. data->temp_max[attr->index] = TEMP_TO_REG(val);
  764. pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
  765. data->temp_max[attr->index]);
  766. mutex_unlock(&data->update_lock);
  767. return count;
  768. }
  769. static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
  770. size_t count)
  771. {
  772. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  773. struct pc87360_data *data = dev_get_drvdata(dev);
  774. long val = simple_strtol(buf, NULL, 10);
  775. mutex_lock(&data->update_lock);
  776. data->temp_crit[attr->index] = TEMP_TO_REG(val);
  777. pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
  778. data->temp_crit[attr->index]);
  779. mutex_unlock(&data->update_lock);
  780. return count;
  781. }
  782. static struct sensor_device_attribute temp_input[] = {
  783. SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
  784. SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
  785. SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
  786. };
  787. static struct sensor_device_attribute temp_status[] = {
  788. SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
  789. SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
  790. SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
  791. };
  792. static struct sensor_device_attribute temp_min[] = {
  793. SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
  794. show_temp_min, set_temp_min, 0),
  795. SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
  796. show_temp_min, set_temp_min, 1),
  797. SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
  798. show_temp_min, set_temp_min, 2),
  799. };
  800. static struct sensor_device_attribute temp_max[] = {
  801. SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  802. show_temp_max, set_temp_max, 0),
  803. SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
  804. show_temp_max, set_temp_max, 1),
  805. SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
  806. show_temp_max, set_temp_max, 2),
  807. };
  808. static struct sensor_device_attribute temp_crit[] = {
  809. SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  810. show_temp_crit, set_temp_crit, 0),
  811. SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
  812. show_temp_crit, set_temp_crit, 1),
  813. SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
  814. show_temp_crit, set_temp_crit, 2),
  815. };
  816. static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  817. {
  818. struct pc87360_data *data = pc87360_update_device(dev);
  819. return sprintf(buf, "%u\n", data->temp_alarms);
  820. }
  821. static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
  822. /* show_temp_min/max_alarm() reads data from the per-channel status
  823. register (sec 12.3.7), not the temp event status registers (sec
  824. 12.3.2) that show_temp_alarm() reads (via data->temp_alarms) */
  825. static ssize_t show_temp_min_alarm(struct device *dev,
  826. struct device_attribute *devattr, char *buf)
  827. {
  828. struct pc87360_data *data = pc87360_update_device(dev);
  829. unsigned nr = to_sensor_dev_attr(devattr)->index;
  830. return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
  831. }
  832. static ssize_t show_temp_max_alarm(struct device *dev,
  833. struct device_attribute *devattr, char *buf)
  834. {
  835. struct pc87360_data *data = pc87360_update_device(dev);
  836. unsigned nr = to_sensor_dev_attr(devattr)->index;
  837. return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
  838. }
  839. static ssize_t show_temp_crit_alarm(struct device *dev,
  840. struct device_attribute *devattr, char *buf)
  841. {
  842. struct pc87360_data *data = pc87360_update_device(dev);
  843. unsigned nr = to_sensor_dev_attr(devattr)->index;
  844. return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
  845. }
  846. static struct sensor_device_attribute temp_min_alarm[] = {
  847. SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 0),
  848. SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 1),
  849. SENSOR_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 2),
  850. };
  851. static struct sensor_device_attribute temp_max_alarm[] = {
  852. SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 0),
  853. SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 1),
  854. SENSOR_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 2),
  855. };
  856. static struct sensor_device_attribute temp_crit_alarm[] = {
  857. SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 0),
  858. SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 1),
  859. SENSOR_ATTR(temp3_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 2),
  860. };
  861. #define TEMP_FAULT 0x40 /* open diode */
  862. static ssize_t show_temp_fault(struct device *dev,
  863. struct device_attribute *devattr, char *buf)
  864. {
  865. struct pc87360_data *data = pc87360_update_device(dev);
  866. unsigned nr = to_sensor_dev_attr(devattr)->index;
  867. return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
  868. }
  869. static struct sensor_device_attribute temp_fault[] = {
  870. SENSOR_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0),
  871. SENSOR_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1),
  872. SENSOR_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2),
  873. };
  874. #define TEMP_UNIT_ATTRS(X) \
  875. &temp_input[X].dev_attr.attr, \
  876. &temp_status[X].dev_attr.attr, \
  877. &temp_min[X].dev_attr.attr, \
  878. &temp_max[X].dev_attr.attr, \
  879. &temp_crit[X].dev_attr.attr, \
  880. &temp_min_alarm[X].dev_attr.attr, \
  881. &temp_max_alarm[X].dev_attr.attr, \
  882. &temp_crit_alarm[X].dev_attr.attr, \
  883. &temp_fault[X].dev_attr.attr
  884. static struct attribute * pc8736x_temp_attr_array[] = {
  885. TEMP_UNIT_ATTRS(0),
  886. TEMP_UNIT_ATTRS(1),
  887. TEMP_UNIT_ATTRS(2),
  888. /* include the few miscellaneous atts here */
  889. &dev_attr_alarms_temp.attr,
  890. NULL
  891. };
  892. static const struct attribute_group pc8736x_temp_group = {
  893. .attrs = pc8736x_temp_attr_array,
  894. };
  895. static ssize_t show_name(struct device *dev,
  896. struct device_attribute *devattr, char *buf)
  897. {
  898. struct pc87360_data *data = dev_get_drvdata(dev);
  899. return sprintf(buf, "%s\n", data->name);
  900. }
  901. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  902. /*
  903. * Device detection, registration and update
  904. */
  905. static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
  906. {
  907. u16 val;
  908. int i;
  909. int nrdev; /* logical device count */
  910. /* No superio_enter */
  911. /* Identify device */
  912. val = force_id ? force_id : superio_inb(sioaddr, DEVID);
  913. switch (val) {
  914. case 0xE1: /* PC87360 */
  915. case 0xE8: /* PC87363 */
  916. case 0xE4: /* PC87364 */
  917. nrdev = 1;
  918. break;
  919. case 0xE5: /* PC87365 */
  920. case 0xE9: /* PC87366 */
  921. nrdev = 3;
  922. break;
  923. default:
  924. superio_exit(sioaddr);
  925. return -ENODEV;
  926. }
  927. /* Remember the device id */
  928. *devid = val;
  929. for (i = 0; i < nrdev; i++) {
  930. /* select logical device */
  931. superio_outb(sioaddr, DEV, logdev[i]);
  932. val = superio_inb(sioaddr, ACT);
  933. if (!(val & 0x01)) {
  934. printk(KERN_INFO "pc87360: Device 0x%02x not "
  935. "activated\n", logdev[i]);
  936. continue;
  937. }
  938. val = (superio_inb(sioaddr, BASE) << 8)
  939. | superio_inb(sioaddr, BASE + 1);
  940. if (!val) {
  941. printk(KERN_INFO "pc87360: Base address not set for "
  942. "device 0x%02x\n", logdev[i]);
  943. continue;
  944. }
  945. addresses[i] = val;
  946. if (i==0) { /* Fans */
  947. confreg[0] = superio_inb(sioaddr, 0xF0);
  948. confreg[1] = superio_inb(sioaddr, 0xF1);
  949. #ifdef DEBUG
  950. printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
  951. "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
  952. (confreg[0]>>3)&1, (confreg[0]>>4)&1);
  953. printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
  954. "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
  955. (confreg[0]>>6)&1, (confreg[0]>>7)&1);
  956. printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
  957. "ctrl=%d inv=%d\n", confreg[1]&1,
  958. (confreg[1]>>1)&1, (confreg[1]>>2)&1);
  959. #endif
  960. } else if (i==1) { /* Voltages */
  961. /* Are we using thermistors? */
  962. if (*devid == 0xE9) { /* PC87366 */
  963. /* These registers are not logical-device
  964. specific, just that we won't need them if
  965. we don't use the VLM device */
  966. confreg[2] = superio_inb(sioaddr, 0x2B);
  967. confreg[3] = superio_inb(sioaddr, 0x25);
  968. if (confreg[2] & 0x40) {
  969. printk(KERN_INFO "pc87360: Using "
  970. "thermistors for temperature "
  971. "monitoring\n");
  972. }
  973. if (confreg[3] & 0xE0) {
  974. printk(KERN_INFO "pc87360: VID "
  975. "inputs routed (mode %u)\n",
  976. confreg[3] >> 5);
  977. }
  978. }
  979. }
  980. }
  981. superio_exit(sioaddr);
  982. return 0;
  983. }
  984. static int __devinit pc87360_probe(struct platform_device *pdev)
  985. {
  986. int i;
  987. struct pc87360_data *data;
  988. int err = 0;
  989. const char *name = "pc87360";
  990. int use_thermistors = 0;
  991. struct device *dev = &pdev->dev;
  992. if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
  993. return -ENOMEM;
  994. data->fannr = 2;
  995. data->innr = 0;
  996. data->tempnr = 0;
  997. switch (devid) {
  998. case 0xe8:
  999. name = "pc87363";
  1000. break;
  1001. case 0xe4:
  1002. name = "pc87364";
  1003. data->fannr = 3;
  1004. break;
  1005. case 0xe5:
  1006. name = "pc87365";
  1007. data->fannr = extra_isa[0] ? 3 : 0;
  1008. data->innr = extra_isa[1] ? 11 : 0;
  1009. data->tempnr = extra_isa[2] ? 2 : 0;
  1010. break;
  1011. case 0xe9:
  1012. name = "pc87366";
  1013. data->fannr = extra_isa[0] ? 3 : 0;
  1014. data->innr = extra_isa[1] ? 14 : 0;
  1015. data->tempnr = extra_isa[2] ? 3 : 0;
  1016. break;
  1017. }
  1018. data->name = name;
  1019. data->valid = 0;
  1020. mutex_init(&data->lock);
  1021. mutex_init(&data->update_lock);
  1022. platform_set_drvdata(pdev, data);
  1023. for (i = 0; i < LDNI_MAX; i++) {
  1024. if (((data->address[i] = extra_isa[i]))
  1025. && !request_region(extra_isa[i], PC87360_EXTENT,
  1026. pc87360_driver.driver.name)) {
  1027. dev_err(dev, "Region 0x%x-0x%x already "
  1028. "in use!\n", extra_isa[i],
  1029. extra_isa[i]+PC87360_EXTENT-1);
  1030. for (i--; i >= 0; i--)
  1031. release_region(extra_isa[i], PC87360_EXTENT);
  1032. err = -EBUSY;
  1033. goto ERROR1;
  1034. }
  1035. }
  1036. /* Retrieve the fans configuration from Super-I/O space */
  1037. if (data->fannr)
  1038. data->fan_conf = confreg[0] | (confreg[1] << 8);
  1039. /* Use the correct reference voltage
  1040. Unless both the VLM and the TMS logical devices agree to
  1041. use an external Vref, the internal one is used. */
  1042. if (data->innr) {
  1043. i = pc87360_read_value(data, LD_IN, NO_BANK,
  1044. PC87365_REG_IN_CONFIG);
  1045. if (data->tempnr) {
  1046. i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
  1047. PC87365_REG_TEMP_CONFIG);
  1048. }
  1049. data->in_vref = (i&0x02) ? 3025 : 2966;
  1050. dev_dbg(dev, "Using %s reference voltage\n",
  1051. (i&0x02) ? "external" : "internal");
  1052. data->vid_conf = confreg[3];
  1053. data->vrm = vid_which_vrm();
  1054. }
  1055. /* Fan clock dividers may be needed before any data is read */
  1056. for (i = 0; i < data->fannr; i++) {
  1057. if (FAN_CONFIG_MONITOR(data->fan_conf, i))
  1058. data->fan_status[i] = pc87360_read_value(data,
  1059. LD_FAN, NO_BANK,
  1060. PC87360_REG_FAN_STATUS(i));
  1061. }
  1062. if (init > 0) {
  1063. if (devid == 0xe9 && data->address[1]) /* PC87366 */
  1064. use_thermistors = confreg[2] & 0x40;
  1065. pc87360_init_device(pdev, use_thermistors);
  1066. }
  1067. /* Register all-or-nothing sysfs groups */
  1068. if (data->innr &&
  1069. (err = sysfs_create_group(&dev->kobj,
  1070. &pc8736x_vin_group)))
  1071. goto ERROR3;
  1072. if (data->innr == 14 &&
  1073. (err = sysfs_create_group(&dev->kobj,
  1074. &pc8736x_therm_group)))
  1075. goto ERROR3;
  1076. /* create device attr-files for varying sysfs groups */
  1077. if (data->tempnr) {
  1078. for (i = 0; i < data->tempnr; i++) {
  1079. if ((err = device_create_file(dev,
  1080. &temp_input[i].dev_attr))
  1081. || (err = device_create_file(dev,
  1082. &temp_min[i].dev_attr))
  1083. || (err = device_create_file(dev,
  1084. &temp_max[i].dev_attr))
  1085. || (err = device_create_file(dev,
  1086. &temp_crit[i].dev_attr))
  1087. || (err = device_create_file(dev,
  1088. &temp_status[i].dev_attr))
  1089. || (err = device_create_file(dev,
  1090. &temp_min_alarm[i].dev_attr))
  1091. || (err = device_create_file(dev,
  1092. &temp_max_alarm[i].dev_attr))
  1093. || (err = device_create_file(dev,
  1094. &temp_crit_alarm[i].dev_attr))
  1095. || (err = device_create_file(dev,
  1096. &temp_fault[i].dev_attr)))
  1097. goto ERROR3;
  1098. }
  1099. if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
  1100. goto ERROR3;
  1101. }
  1102. for (i = 0; i < data->fannr; i++) {
  1103. if (FAN_CONFIG_MONITOR(data->fan_conf, i)
  1104. && ((err = device_create_file(dev,
  1105. &fan_input[i].dev_attr))
  1106. || (err = device_create_file(dev,
  1107. &fan_min[i].dev_attr))
  1108. || (err = device_create_file(dev,
  1109. &fan_div[i].dev_attr))
  1110. || (err = device_create_file(dev,
  1111. &fan_status[i].dev_attr))))
  1112. goto ERROR3;
  1113. if (FAN_CONFIG_CONTROL(data->fan_conf, i)
  1114. && (err = device_create_file(dev, &pwm[i].dev_attr)))
  1115. goto ERROR3;
  1116. }
  1117. if ((err = device_create_file(dev, &dev_attr_name)))
  1118. goto ERROR3;
  1119. data->hwmon_dev = hwmon_device_register(dev);
  1120. if (IS_ERR(data->hwmon_dev)) {
  1121. err = PTR_ERR(data->hwmon_dev);
  1122. goto ERROR3;
  1123. }
  1124. return 0;
  1125. ERROR3:
  1126. device_remove_file(dev, &dev_attr_name);
  1127. /* can still remove groups whose members were added individually */
  1128. sysfs_remove_group(&dev->kobj, &pc8736x_temp_group);
  1129. sysfs_remove_group(&dev->kobj, &pc8736x_fan_group);
  1130. sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
  1131. sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
  1132. for (i = 0; i < 3; i++) {
  1133. if (data->address[i]) {
  1134. release_region(data->address[i], PC87360_EXTENT);
  1135. }
  1136. }
  1137. ERROR1:
  1138. kfree(data);
  1139. return err;
  1140. }
  1141. static int __devexit pc87360_remove(struct platform_device *pdev)
  1142. {
  1143. struct pc87360_data *data = platform_get_drvdata(pdev);
  1144. int i;
  1145. hwmon_device_unregister(data->hwmon_dev);
  1146. device_remove_file(&pdev->dev, &dev_attr_name);
  1147. sysfs_remove_group(&pdev->dev.kobj, &pc8736x_temp_group);
  1148. sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_group);
  1149. sysfs_remove_group(&pdev->dev.kobj, &pc8736x_therm_group);
  1150. sysfs_remove_group(&pdev->dev.kobj, &pc8736x_vin_group);
  1151. for (i = 0; i < 3; i++) {
  1152. if (data->address[i]) {
  1153. release_region(data->address[i], PC87360_EXTENT);
  1154. }
  1155. }
  1156. kfree(data);
  1157. return 0;
  1158. }
  1159. /* ldi is the logical device index
  1160. bank is for voltages and temperatures only */
  1161. static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
  1162. u8 reg)
  1163. {
  1164. int res;
  1165. mutex_lock(&(data->lock));
  1166. if (bank != NO_BANK)
  1167. outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
  1168. res = inb_p(data->address[ldi] + reg);
  1169. mutex_unlock(&(data->lock));
  1170. return res;
  1171. }
  1172. static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
  1173. u8 reg, u8 value)
  1174. {
  1175. mutex_lock(&(data->lock));
  1176. if (bank != NO_BANK)
  1177. outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
  1178. outb_p(value, data->address[ldi] + reg);
  1179. mutex_unlock(&(data->lock));
  1180. }
  1181. /* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
  1182. #define CHAN_CNVRTD 0x80 /* new data ready */
  1183. #define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
  1184. #define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
  1185. #define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
  1186. #define TEMP_OTS_OE 0x20 /* OTS Output Enable */
  1187. #define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */
  1188. #define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
  1189. static void pc87360_init_device(struct platform_device *pdev,
  1190. int use_thermistors)
  1191. {
  1192. struct pc87360_data *data = platform_get_drvdata(pdev);
  1193. int i, nr;
  1194. const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
  1195. const u8 init_temp[3] = { 2, 2, 1 };
  1196. u8 reg;
  1197. if (init >= 2 && data->innr) {
  1198. reg = pc87360_read_value(data, LD_IN, NO_BANK,
  1199. PC87365_REG_IN_CONVRATE);
  1200. dev_info(&pdev->dev, "VLM conversion set to "
  1201. "1s period, 160us delay\n");
  1202. pc87360_write_value(data, LD_IN, NO_BANK,
  1203. PC87365_REG_IN_CONVRATE,
  1204. (reg & 0xC0) | 0x11);
  1205. }
  1206. nr = data->innr < 11 ? data->innr : 11;
  1207. for (i = 0; i < nr; i++) {
  1208. reg = pc87360_read_value(data, LD_IN, i,
  1209. PC87365_REG_IN_STATUS);
  1210. dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
  1211. if (init >= init_in[i]) {
  1212. /* Forcibly enable voltage channel */
  1213. if (!(reg & CHAN_ENA)) {
  1214. dev_dbg(&pdev->dev, "Forcibly "
  1215. "enabling in%d\n", i);
  1216. pc87360_write_value(data, LD_IN, i,
  1217. PC87365_REG_IN_STATUS,
  1218. (reg & 0x68) | 0x87);
  1219. }
  1220. }
  1221. }
  1222. /* We can't blindly trust the Super-I/O space configuration bit,
  1223. most BIOS won't set it properly */
  1224. dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
  1225. for (i = 11; i < data->innr; i++) {
  1226. reg = pc87360_read_value(data, LD_IN, i,
  1227. PC87365_REG_TEMP_STATUS);
  1228. use_thermistors = use_thermistors || (reg & CHAN_ENA);
  1229. /* thermistors are temp[4-6], measured on vin[11-14] */
  1230. dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
  1231. }
  1232. dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
  1233. i = use_thermistors ? 2 : 0;
  1234. for (; i < data->tempnr; i++) {
  1235. reg = pc87360_read_value(data, LD_TEMP, i,
  1236. PC87365_REG_TEMP_STATUS);
  1237. dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i+1, reg);
  1238. if (init >= init_temp[i]) {
  1239. /* Forcibly enable temperature channel */
  1240. if (!(reg & CHAN_ENA)) {
  1241. dev_dbg(&pdev->dev, "Forcibly "
  1242. "enabling temp%d\n", i+1);
  1243. pc87360_write_value(data, LD_TEMP, i,
  1244. PC87365_REG_TEMP_STATUS,
  1245. 0xCF);
  1246. }
  1247. }
  1248. }
  1249. if (use_thermistors) {
  1250. for (i = 11; i < data->innr; i++) {
  1251. if (init >= init_in[i]) {
  1252. /* The pin may already be used by thermal
  1253. diodes */
  1254. reg = pc87360_read_value(data, LD_TEMP,
  1255. (i-11)/2, PC87365_REG_TEMP_STATUS);
  1256. if (reg & CHAN_ENA) {
  1257. dev_dbg(&pdev->dev, "Skipping "
  1258. "temp%d, pin already in use "
  1259. "by temp%d\n", i-7, (i-11)/2);
  1260. continue;
  1261. }
  1262. /* Forcibly enable thermistor channel */
  1263. reg = pc87360_read_value(data, LD_IN, i,
  1264. PC87365_REG_IN_STATUS);
  1265. if (!(reg & CHAN_ENA)) {
  1266. dev_dbg(&pdev->dev, "Forcibly "
  1267. "enabling temp%d\n", i-7);
  1268. pc87360_write_value(data, LD_IN, i,
  1269. PC87365_REG_TEMP_STATUS,
  1270. (reg & 0x60) | 0x8F);
  1271. }
  1272. }
  1273. }
  1274. }
  1275. if (data->innr) {
  1276. reg = pc87360_read_value(data, LD_IN, NO_BANK,
  1277. PC87365_REG_IN_CONFIG);
  1278. dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
  1279. if (reg & CHAN_ENA) {
  1280. dev_dbg(&pdev->dev, "Forcibly "
  1281. "enabling monitoring (VLM)\n");
  1282. pc87360_write_value(data, LD_IN, NO_BANK,
  1283. PC87365_REG_IN_CONFIG,
  1284. reg & 0xFE);
  1285. }
  1286. }
  1287. if (data->tempnr) {
  1288. reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
  1289. PC87365_REG_TEMP_CONFIG);
  1290. dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
  1291. if (reg & CHAN_ENA) {
  1292. dev_dbg(&pdev->dev, "Forcibly enabling "
  1293. "monitoring (TMS)\n");
  1294. pc87360_write_value(data, LD_TEMP, NO_BANK,
  1295. PC87365_REG_TEMP_CONFIG,
  1296. reg & 0xFE);
  1297. }
  1298. if (init >= 2) {
  1299. /* Chip config as documented by National Semi. */
  1300. pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
  1301. /* We voluntarily omit the bank here, in case the
  1302. sequence itself matters. It shouldn't be a problem,
  1303. since nobody else is supposed to access the
  1304. device at that point. */
  1305. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
  1306. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
  1307. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
  1308. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
  1309. }
  1310. }
  1311. }
  1312. static void pc87360_autodiv(struct device *dev, int nr)
  1313. {
  1314. struct pc87360_data *data = dev_get_drvdata(dev);
  1315. u8 old_min = data->fan_min[nr];
  1316. /* Increase clock divider if needed and possible */
  1317. if ((data->fan_status[nr] & 0x04) /* overflow flag */
  1318. || (data->fan[nr] >= 224)) { /* next to overflow */
  1319. if ((data->fan_status[nr] & 0x60) != 0x60) {
  1320. data->fan_status[nr] += 0x20;
  1321. data->fan_min[nr] >>= 1;
  1322. data->fan[nr] >>= 1;
  1323. dev_dbg(dev, "Increasing "
  1324. "clock divider to %d for fan %d\n",
  1325. FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
  1326. }
  1327. } else {
  1328. /* Decrease clock divider if possible */
  1329. while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
  1330. && data->fan[nr] < 85 /* bad accuracy */
  1331. && (data->fan_status[nr] & 0x60) != 0x00) {
  1332. data->fan_status[nr] -= 0x20;
  1333. data->fan_min[nr] <<= 1;
  1334. data->fan[nr] <<= 1;
  1335. dev_dbg(dev, "Decreasing "
  1336. "clock divider to %d for fan %d\n",
  1337. FAN_DIV_FROM_REG(data->fan_status[nr]),
  1338. nr+1);
  1339. }
  1340. }
  1341. /* Write new fan min if it changed */
  1342. if (old_min != data->fan_min[nr]) {
  1343. pc87360_write_value(data, LD_FAN, NO_BANK,
  1344. PC87360_REG_FAN_MIN(nr),
  1345. data->fan_min[nr]);
  1346. }
  1347. }
  1348. static struct pc87360_data *pc87360_update_device(struct device *dev)
  1349. {
  1350. struct pc87360_data *data = dev_get_drvdata(dev);
  1351. u8 i;
  1352. mutex_lock(&data->update_lock);
  1353. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  1354. dev_dbg(dev, "Data update\n");
  1355. /* Fans */
  1356. for (i = 0; i < data->fannr; i++) {
  1357. if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
  1358. data->fan_status[i] =
  1359. pc87360_read_value(data, LD_FAN,
  1360. NO_BANK, PC87360_REG_FAN_STATUS(i));
  1361. data->fan[i] = pc87360_read_value(data, LD_FAN,
  1362. NO_BANK, PC87360_REG_FAN(i));
  1363. data->fan_min[i] = pc87360_read_value(data,
  1364. LD_FAN, NO_BANK,
  1365. PC87360_REG_FAN_MIN(i));
  1366. /* Change clock divider if needed */
  1367. pc87360_autodiv(dev, i);
  1368. /* Clear bits and write new divider */
  1369. pc87360_write_value(data, LD_FAN, NO_BANK,
  1370. PC87360_REG_FAN_STATUS(i),
  1371. data->fan_status[i]);
  1372. }
  1373. if (FAN_CONFIG_CONTROL(data->fan_conf, i))
  1374. data->pwm[i] = pc87360_read_value(data, LD_FAN,
  1375. NO_BANK, PC87360_REG_PWM(i));
  1376. }
  1377. /* Voltages */
  1378. for (i = 0; i < data->innr; i++) {
  1379. data->in_status[i] = pc87360_read_value(data, LD_IN, i,
  1380. PC87365_REG_IN_STATUS);
  1381. /* Clear bits */
  1382. pc87360_write_value(data, LD_IN, i,
  1383. PC87365_REG_IN_STATUS,
  1384. data->in_status[i]);
  1385. if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
  1386. data->in[i] = pc87360_read_value(data, LD_IN,
  1387. i, PC87365_REG_IN);
  1388. }
  1389. if (data->in_status[i] & CHAN_ENA) {
  1390. data->in_min[i] = pc87360_read_value(data,
  1391. LD_IN, i,
  1392. PC87365_REG_IN_MIN);
  1393. data->in_max[i] = pc87360_read_value(data,
  1394. LD_IN, i,
  1395. PC87365_REG_IN_MAX);
  1396. if (i >= 11)
  1397. data->in_crit[i-11] =
  1398. pc87360_read_value(data, LD_IN,
  1399. i, PC87365_REG_TEMP_CRIT);
  1400. }
  1401. }
  1402. if (data->innr) {
  1403. data->in_alarms = pc87360_read_value(data, LD_IN,
  1404. NO_BANK, PC87365_REG_IN_ALARMS1)
  1405. | ((pc87360_read_value(data, LD_IN,
  1406. NO_BANK, PC87365_REG_IN_ALARMS2)
  1407. & 0x07) << 8);
  1408. data->vid = (data->vid_conf & 0xE0) ?
  1409. pc87360_read_value(data, LD_IN,
  1410. NO_BANK, PC87365_REG_VID) : 0x1F;
  1411. }
  1412. /* Temperatures */
  1413. for (i = 0; i < data->tempnr; i++) {
  1414. data->temp_status[i] = pc87360_read_value(data,
  1415. LD_TEMP, i,
  1416. PC87365_REG_TEMP_STATUS);
  1417. /* Clear bits */
  1418. pc87360_write_value(data, LD_TEMP, i,
  1419. PC87365_REG_TEMP_STATUS,
  1420. data->temp_status[i]);
  1421. if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
  1422. data->temp[i] = pc87360_read_value(data,
  1423. LD_TEMP, i,
  1424. PC87365_REG_TEMP);
  1425. }
  1426. if (data->temp_status[i] & CHAN_ENA) {
  1427. data->temp_min[i] = pc87360_read_value(data,
  1428. LD_TEMP, i,
  1429. PC87365_REG_TEMP_MIN);
  1430. data->temp_max[i] = pc87360_read_value(data,
  1431. LD_TEMP, i,
  1432. PC87365_REG_TEMP_MAX);
  1433. data->temp_crit[i] = pc87360_read_value(data,
  1434. LD_TEMP, i,
  1435. PC87365_REG_TEMP_CRIT);
  1436. }
  1437. }
  1438. if (data->tempnr) {
  1439. data->temp_alarms = pc87360_read_value(data, LD_TEMP,
  1440. NO_BANK, PC87365_REG_TEMP_ALARMS)
  1441. & 0x3F;
  1442. }
  1443. data->last_updated = jiffies;
  1444. data->valid = 1;
  1445. }
  1446. mutex_unlock(&data->update_lock);
  1447. return data;
  1448. }
  1449. static int __init pc87360_device_add(unsigned short address)
  1450. {
  1451. struct resource res = {
  1452. .name = "pc87360",
  1453. .flags = IORESOURCE_IO,
  1454. };
  1455. int err, i;
  1456. pdev = platform_device_alloc("pc87360", address);
  1457. if (!pdev) {
  1458. err = -ENOMEM;
  1459. printk(KERN_ERR "pc87360: Device allocation failed\n");
  1460. goto exit;
  1461. }
  1462. for (i = 0; i < 3; i++) {
  1463. if (!extra_isa[i])
  1464. continue;
  1465. res.start = extra_isa[i];
  1466. res.end = extra_isa[i] + PC87360_EXTENT - 1;
  1467. err = platform_device_add_resources(pdev, &res, 1);
  1468. if (err) {
  1469. printk(KERN_ERR "pc87360: Device resource[%d] "
  1470. "addition failed (%d)\n", i, err);
  1471. goto exit_device_put;
  1472. }
  1473. }
  1474. err = platform_device_add(pdev);
  1475. if (err) {
  1476. printk(KERN_ERR "pc87360: Device addition failed (%d)\n",
  1477. err);
  1478. goto exit_device_put;
  1479. }
  1480. return 0;
  1481. exit_device_put:
  1482. platform_device_put(pdev);
  1483. exit:
  1484. return err;
  1485. }
  1486. static int __init pc87360_init(void)
  1487. {
  1488. int err, i;
  1489. unsigned short address = 0;
  1490. if (pc87360_find(0x2e, &devid, extra_isa)
  1491. && pc87360_find(0x4e, &devid, extra_isa)) {
  1492. printk(KERN_WARNING "pc87360: PC8736x not detected, "
  1493. "module not inserted.\n");
  1494. return -ENODEV;
  1495. }
  1496. /* Arbitrarily pick one of the addresses */
  1497. for (i = 0; i < 3; i++) {
  1498. if (extra_isa[i] != 0x0000) {
  1499. address = extra_isa[i];
  1500. break;
  1501. }
  1502. }
  1503. if (address == 0x0000) {
  1504. printk(KERN_WARNING "pc87360: No active logical device, "
  1505. "module not inserted.\n");
  1506. return -ENODEV;
  1507. }
  1508. err = platform_driver_register(&pc87360_driver);
  1509. if (err)
  1510. goto exit;
  1511. /* Sets global pdev as a side effect */
  1512. err = pc87360_device_add(address);
  1513. if (err)
  1514. goto exit_driver;
  1515. return 0;
  1516. exit_driver:
  1517. platform_driver_unregister(&pc87360_driver);
  1518. exit:
  1519. return err;
  1520. }
  1521. static void __exit pc87360_exit(void)
  1522. {
  1523. platform_device_unregister(pdev);
  1524. platform_driver_unregister(&pc87360_driver);
  1525. }
  1526. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  1527. MODULE_DESCRIPTION("PC8736x hardware monitor");
  1528. MODULE_LICENSE("GPL");
  1529. module_init(pc87360_init);
  1530. module_exit(pc87360_exit);