pc87360.c 55 KB

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