pc87360.c 55 KB

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