pc87360.c 55 KB

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