pc87360.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. /*
  2. * pc87360.c - Part of lm_sensors, Linux kernel modules
  3. * for hardware monitoring
  4. * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
  5. *
  6. * Copied from smsc47m1.c:
  7. * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Supports the following chips:
  24. *
  25. * Chip #vin #fan #pwm #temp devid
  26. * PC87360 - 2 2 - 0xE1
  27. * PC87363 - 2 2 - 0xE8
  28. * PC87364 - 3 3 - 0xE4
  29. * PC87365 11 3 3 2 0xE5
  30. * PC87366 11 3 3 3-4 0xE9
  31. *
  32. * This driver assumes that no more than one chip is present, and one of
  33. * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
  34. */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/jiffies.h>
  39. #include <linux/i2c.h>
  40. #include <linux/i2c-isa.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 <asm/io.h>
  47. static u8 devid;
  48. static unsigned short address;
  49. static unsigned short extra_isa[3];
  50. static u8 confreg[4];
  51. enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
  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. /*
  61. * Super-I/O registers and operations
  62. */
  63. #define DEV 0x07 /* Register: Logical device select */
  64. #define DEVID 0x20 /* Register: Device ID */
  65. #define ACT 0x30 /* Register: Device activation */
  66. #define BASE 0x60 /* Register: Base address */
  67. #define FSCM 0x09 /* Logical device: fans */
  68. #define VLM 0x0d /* Logical device: voltages */
  69. #define TMS 0x0e /* Logical device: temperatures */
  70. static const u8 logdev[3] = { FSCM, VLM, TMS };
  71. #define LD_FAN 0
  72. #define LD_IN 1
  73. #define LD_TEMP 2
  74. static inline void superio_outb(int sioaddr, int reg, int val)
  75. {
  76. outb(reg, sioaddr);
  77. outb(val, sioaddr+1);
  78. }
  79. static inline int superio_inb(int sioaddr, int reg)
  80. {
  81. outb(reg, sioaddr);
  82. return inb(sioaddr+1);
  83. }
  84. static inline void superio_exit(int sioaddr)
  85. {
  86. outb(0x02, sioaddr);
  87. outb(0x02, sioaddr+1);
  88. }
  89. /*
  90. * Logical devices
  91. */
  92. #define PC87360_EXTENT 0x10
  93. #define PC87365_REG_BANK 0x09
  94. #define NO_BANK 0xff
  95. /*
  96. * Fan registers and conversions
  97. */
  98. /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
  99. #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
  100. #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
  101. #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
  102. #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
  103. #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
  104. #define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
  105. 480000 / ((val)*(div)))
  106. #define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
  107. 480000 / ((val)*(div)))
  108. #define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
  109. #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
  110. #define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
  111. #define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
  112. #define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
  113. #define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
  114. static inline u8 PWM_TO_REG(int val, int inv)
  115. {
  116. if (inv)
  117. val = 255 - val;
  118. if (val < 0)
  119. return 0;
  120. if (val > 255)
  121. return 255;
  122. return val;
  123. }
  124. /*
  125. * Voltage registers and conversions
  126. */
  127. #define PC87365_REG_IN_CONVRATE 0x07
  128. #define PC87365_REG_IN_CONFIG 0x08
  129. #define PC87365_REG_IN 0x0B
  130. #define PC87365_REG_IN_MIN 0x0D
  131. #define PC87365_REG_IN_MAX 0x0C
  132. #define PC87365_REG_IN_STATUS 0x0A
  133. #define PC87365_REG_IN_ALARMS1 0x00
  134. #define PC87365_REG_IN_ALARMS2 0x01
  135. #define PC87365_REG_VID 0x06
  136. #define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
  137. #define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
  138. (val)*256 >= (ref)*255 ? 255: \
  139. ((val) * 256 + (ref)/2) / (ref))
  140. /*
  141. * Temperature registers and conversions
  142. */
  143. #define PC87365_REG_TEMP_CONFIG 0x08
  144. #define PC87365_REG_TEMP 0x0B
  145. #define PC87365_REG_TEMP_MIN 0x0D
  146. #define PC87365_REG_TEMP_MAX 0x0C
  147. #define PC87365_REG_TEMP_CRIT 0x0E
  148. #define PC87365_REG_TEMP_STATUS 0x0A
  149. #define PC87365_REG_TEMP_ALARMS 0x00
  150. #define TEMP_FROM_REG(val) ((val) * 1000)
  151. #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
  152. (val) > 127000 ? 127 : \
  153. (val) < 0 ? ((val) - 500) / 1000 : \
  154. ((val) + 500) / 1000)
  155. /*
  156. * Client data (each client gets its own)
  157. */
  158. struct pc87360_data {
  159. struct i2c_client client;
  160. struct class_device *class_dev;
  161. struct mutex lock;
  162. struct mutex update_lock;
  163. char valid; /* !=0 if following fields are valid */
  164. unsigned long last_updated; /* In jiffies */
  165. int address[3];
  166. u8 fannr, innr, tempnr;
  167. u8 fan[3]; /* Register value */
  168. u8 fan_min[3]; /* Register value */
  169. u8 fan_status[3]; /* Register value */
  170. u8 pwm[3]; /* Register value */
  171. u16 fan_conf; /* Configuration register values, combined */
  172. u16 in_vref; /* 1 mV/bit */
  173. u8 in[14]; /* Register value */
  174. u8 in_min[14]; /* Register value */
  175. u8 in_max[14]; /* Register value */
  176. u8 in_crit[3]; /* Register value */
  177. u8 in_status[14]; /* Register value */
  178. u16 in_alarms; /* Register values, combined, masked */
  179. u8 vid_conf; /* Configuration register value */
  180. u8 vrm;
  181. u8 vid; /* Register value */
  182. s8 temp[3]; /* Register value */
  183. s8 temp_min[3]; /* Register value */
  184. s8 temp_max[3]; /* Register value */
  185. s8 temp_crit[3]; /* Register value */
  186. u8 temp_status[3]; /* Register value */
  187. u8 temp_alarms; /* Register value, masked */
  188. };
  189. /*
  190. * Functions declaration
  191. */
  192. static int pc87360_detect(struct i2c_adapter *adapter);
  193. static int pc87360_detach_client(struct i2c_client *client);
  194. static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
  195. u8 reg);
  196. static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
  197. u8 reg, u8 value);
  198. static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
  199. static struct pc87360_data *pc87360_update_device(struct device *dev);
  200. /*
  201. * Driver data (common to all clients)
  202. */
  203. static struct i2c_driver pc87360_driver = {
  204. .driver = {
  205. .owner = THIS_MODULE,
  206. .name = "pc87360",
  207. },
  208. .attach_adapter = pc87360_detect,
  209. .detach_client = pc87360_detach_client,
  210. };
  211. /*
  212. * Sysfs stuff
  213. */
  214. static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
  215. {
  216. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  217. struct pc87360_data *data = pc87360_update_device(dev);
  218. return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
  219. FAN_DIV_FROM_REG(data->fan_status[attr->index])));
  220. }
  221. static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
  222. {
  223. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  224. struct pc87360_data *data = pc87360_update_device(dev);
  225. return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
  226. FAN_DIV_FROM_REG(data->fan_status[attr->index])));
  227. }
  228. static ssize_t show_fan_div(struct device *dev, 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",
  233. FAN_DIV_FROM_REG(data->fan_status[attr->index]));
  234. }
  235. static ssize_t show_fan_status(struct device *dev, 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_STATUS_FROM_REG(data->fan_status[attr->index]));
  241. }
  242. static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
  243. size_t count)
  244. {
  245. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  246. struct i2c_client *client = to_i2c_client(dev);
  247. struct pc87360_data *data = i2c_get_clientdata(client);
  248. long fan_min = simple_strtol(buf, NULL, 10);
  249. mutex_lock(&data->update_lock);
  250. fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
  251. /* If it wouldn't fit, change clock divisor */
  252. while (fan_min > 255
  253. && (data->fan_status[attr->index] & 0x60) != 0x60) {
  254. fan_min >>= 1;
  255. data->fan[attr->index] >>= 1;
  256. data->fan_status[attr->index] += 0x20;
  257. }
  258. data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
  259. pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
  260. data->fan_min[attr->index]);
  261. /* Write new divider, preserve alarm bits */
  262. pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
  263. data->fan_status[attr->index] & 0xF9);
  264. mutex_unlock(&data->update_lock);
  265. return count;
  266. }
  267. static struct sensor_device_attribute fan_input[] = {
  268. SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
  269. SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
  270. SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
  271. };
  272. static struct sensor_device_attribute fan_status[] = {
  273. SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
  274. SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
  275. SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
  276. };
  277. static struct sensor_device_attribute fan_div[] = {
  278. SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
  279. SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
  280. SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
  281. };
  282. static struct sensor_device_attribute fan_min[] = {
  283. SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
  284. SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
  285. SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
  286. };
  287. static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
  288. {
  289. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  290. struct pc87360_data *data = pc87360_update_device(dev);
  291. return sprintf(buf, "%u\n",
  292. PWM_FROM_REG(data->pwm[attr->index],
  293. FAN_CONFIG_INVERT(data->fan_conf,
  294. attr->index)));
  295. }
  296. static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
  297. size_t count)
  298. {
  299. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  300. struct i2c_client *client = to_i2c_client(dev);
  301. struct pc87360_data *data = i2c_get_clientdata(client);
  302. long val = simple_strtol(buf, NULL, 10);
  303. mutex_lock(&data->update_lock);
  304. data->pwm[attr->index] = PWM_TO_REG(val,
  305. FAN_CONFIG_INVERT(data->fan_conf, attr->index));
  306. pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
  307. data->pwm[attr->index]);
  308. mutex_unlock(&data->update_lock);
  309. return count;
  310. }
  311. static struct sensor_device_attribute pwm[] = {
  312. SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
  313. SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
  314. SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
  315. };
  316. static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
  317. {
  318. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  319. struct pc87360_data *data = pc87360_update_device(dev);
  320. return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
  321. data->in_vref));
  322. }
  323. static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
  324. {
  325. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  326. struct pc87360_data *data = pc87360_update_device(dev);
  327. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
  328. data->in_vref));
  329. }
  330. static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
  331. {
  332. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  333. struct pc87360_data *data = pc87360_update_device(dev);
  334. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
  335. data->in_vref));
  336. }
  337. static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
  338. {
  339. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  340. struct pc87360_data *data = pc87360_update_device(dev);
  341. return sprintf(buf, "%u\n", data->in_status[attr->index]);
  342. }
  343. static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
  344. size_t count)
  345. {
  346. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  347. struct i2c_client *client = to_i2c_client(dev);
  348. struct pc87360_data *data = i2c_get_clientdata(client);
  349. long val = simple_strtol(buf, NULL, 10);
  350. mutex_lock(&data->update_lock);
  351. data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
  352. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
  353. data->in_min[attr->index]);
  354. mutex_unlock(&data->update_lock);
  355. return count;
  356. }
  357. static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
  358. size_t count)
  359. {
  360. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  361. struct i2c_client *client = to_i2c_client(dev);
  362. struct pc87360_data *data = i2c_get_clientdata(client);
  363. long val = simple_strtol(buf, NULL, 10);
  364. mutex_lock(&data->update_lock);
  365. data->in_max[attr->index] = IN_TO_REG(val,
  366. data->in_vref);
  367. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
  368. data->in_max[attr->index]);
  369. mutex_unlock(&data->update_lock);
  370. return count;
  371. }
  372. static struct sensor_device_attribute in_input[] = {
  373. SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
  374. SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
  375. SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
  376. SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
  377. SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
  378. SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
  379. SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
  380. SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
  381. SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
  382. SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
  383. SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
  384. };
  385. static struct sensor_device_attribute in_status[] = {
  386. SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
  387. SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
  388. SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
  389. SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
  390. SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
  391. SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
  392. SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
  393. SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
  394. SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
  395. SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
  396. SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
  397. };
  398. static struct sensor_device_attribute in_min[] = {
  399. SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
  400. SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
  401. SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
  402. SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
  403. SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
  404. SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
  405. SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
  406. SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
  407. SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
  408. SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
  409. SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
  410. };
  411. static struct sensor_device_attribute in_max[] = {
  412. SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
  413. SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
  414. SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
  415. SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
  416. SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
  417. SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
  418. SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
  419. SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
  420. SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
  421. SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
  422. SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
  423. };
  424. static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
  425. {
  426. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  427. struct pc87360_data *data = pc87360_update_device(dev);
  428. return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
  429. data->in_vref));
  430. }
  431. static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
  432. {
  433. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  434. struct pc87360_data *data = pc87360_update_device(dev);
  435. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
  436. data->in_vref));
  437. }
  438. static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
  439. {
  440. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  441. struct pc87360_data *data = pc87360_update_device(dev);
  442. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
  443. data->in_vref));
  444. }
  445. static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
  446. {
  447. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  448. struct pc87360_data *data = pc87360_update_device(dev);
  449. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
  450. data->in_vref));
  451. }
  452. static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
  453. {
  454. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  455. struct pc87360_data *data = pc87360_update_device(dev);
  456. return sprintf(buf, "%u\n", data->in_status[attr->index]);
  457. }
  458. static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
  459. size_t count)
  460. {
  461. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  462. struct i2c_client *client = to_i2c_client(dev);
  463. struct pc87360_data *data = i2c_get_clientdata(client);
  464. long val = simple_strtol(buf, NULL, 10);
  465. mutex_lock(&data->update_lock);
  466. data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
  467. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
  468. data->in_min[attr->index]);
  469. mutex_unlock(&data->update_lock);
  470. return count;
  471. }
  472. static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
  473. size_t count)
  474. {
  475. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  476. struct i2c_client *client = to_i2c_client(dev);
  477. struct pc87360_data *data = i2c_get_clientdata(client);
  478. long val = simple_strtol(buf, NULL, 10);
  479. mutex_lock(&data->update_lock);
  480. data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
  481. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
  482. data->in_max[attr->index]);
  483. mutex_unlock(&data->update_lock);
  484. return count;
  485. }
  486. static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
  487. size_t count)
  488. {
  489. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  490. struct i2c_client *client = to_i2c_client(dev);
  491. struct pc87360_data *data = i2c_get_clientdata(client);
  492. long val = simple_strtol(buf, NULL, 10);
  493. mutex_lock(&data->update_lock);
  494. data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
  495. pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
  496. data->in_crit[attr->index-11]);
  497. mutex_unlock(&data->update_lock);
  498. return count;
  499. }
  500. /* the +11 term below reflects the fact that VLM units 11,12,13 are
  501. used in the chip to measure voltage across the thermistors
  502. */
  503. static struct sensor_device_attribute therm_input[] = {
  504. SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
  505. SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
  506. SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
  507. };
  508. static struct sensor_device_attribute therm_status[] = {
  509. SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
  510. SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
  511. SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
  512. };
  513. static struct sensor_device_attribute therm_min[] = {
  514. SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
  515. show_therm_min, set_therm_min, 0+11),
  516. SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
  517. show_therm_min, set_therm_min, 1+11),
  518. SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
  519. show_therm_min, set_therm_min, 2+11),
  520. };
  521. static struct sensor_device_attribute therm_max[] = {
  522. SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
  523. show_therm_max, set_therm_max, 0+11),
  524. SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
  525. show_therm_max, set_therm_max, 1+11),
  526. SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
  527. show_therm_max, set_therm_max, 2+11),
  528. };
  529. static struct sensor_device_attribute therm_crit[] = {
  530. SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
  531. show_therm_crit, set_therm_crit, 0+11),
  532. SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
  533. show_therm_crit, set_therm_crit, 1+11),
  534. SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
  535. show_therm_crit, set_therm_crit, 2+11),
  536. };
  537. static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  538. {
  539. struct pc87360_data *data = pc87360_update_device(dev);
  540. return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
  541. }
  542. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  543. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
  544. {
  545. struct pc87360_data *data = pc87360_update_device(dev);
  546. return sprintf(buf, "%u\n", data->vrm);
  547. }
  548. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  549. {
  550. struct i2c_client *client = to_i2c_client(dev);
  551. struct pc87360_data *data = i2c_get_clientdata(client);
  552. data->vrm = simple_strtoul(buf, NULL, 10);
  553. return count;
  554. }
  555. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  556. static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  557. {
  558. struct pc87360_data *data = pc87360_update_device(dev);
  559. return sprintf(buf, "%u\n", data->in_alarms);
  560. }
  561. static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
  562. static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
  563. {
  564. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  565. struct pc87360_data *data = pc87360_update_device(dev);
  566. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  567. }
  568. static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
  569. {
  570. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  571. struct pc87360_data *data = pc87360_update_device(dev);
  572. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
  573. }
  574. static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
  575. {
  576. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  577. struct pc87360_data *data = pc87360_update_device(dev);
  578. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
  579. }
  580. static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
  581. {
  582. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  583. struct pc87360_data *data = pc87360_update_device(dev);
  584. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
  585. }
  586. static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
  587. {
  588. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  589. struct pc87360_data *data = pc87360_update_device(dev);
  590. return sprintf(buf, "%d\n", data->temp_status[attr->index]);
  591. }
  592. static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
  593. size_t count)
  594. {
  595. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  596. struct i2c_client *client = to_i2c_client(dev);
  597. struct pc87360_data *data = i2c_get_clientdata(client);
  598. long val = simple_strtol(buf, NULL, 10);
  599. mutex_lock(&data->update_lock);
  600. data->temp_min[attr->index] = TEMP_TO_REG(val);
  601. pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
  602. data->temp_min[attr->index]);
  603. mutex_unlock(&data->update_lock);
  604. return count;
  605. }
  606. static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
  607. size_t count)
  608. {
  609. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  610. struct i2c_client *client = to_i2c_client(dev);
  611. struct pc87360_data *data = i2c_get_clientdata(client);
  612. long val = simple_strtol(buf, NULL, 10);
  613. mutex_lock(&data->update_lock);
  614. data->temp_max[attr->index] = TEMP_TO_REG(val);
  615. pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
  616. data->temp_max[attr->index]);
  617. mutex_unlock(&data->update_lock);
  618. return count;
  619. }
  620. static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
  621. size_t count)
  622. {
  623. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  624. struct i2c_client *client = to_i2c_client(dev);
  625. struct pc87360_data *data = i2c_get_clientdata(client);
  626. long val = simple_strtol(buf, NULL, 10);
  627. mutex_lock(&data->update_lock);
  628. data->temp_crit[attr->index] = TEMP_TO_REG(val);
  629. pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
  630. data->temp_crit[attr->index]);
  631. mutex_unlock(&data->update_lock);
  632. return count;
  633. }
  634. static struct sensor_device_attribute temp_input[] = {
  635. SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
  636. SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
  637. SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
  638. };
  639. static struct sensor_device_attribute temp_status[] = {
  640. SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
  641. SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
  642. SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
  643. };
  644. static struct sensor_device_attribute temp_min[] = {
  645. SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
  646. show_temp_min, set_temp_min, 0),
  647. SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
  648. show_temp_min, set_temp_min, 1),
  649. SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
  650. show_temp_min, set_temp_min, 2),
  651. };
  652. static struct sensor_device_attribute temp_max[] = {
  653. SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  654. show_temp_max, set_temp_max, 0),
  655. SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
  656. show_temp_max, set_temp_max, 1),
  657. SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
  658. show_temp_max, set_temp_max, 2),
  659. };
  660. static struct sensor_device_attribute temp_crit[] = {
  661. SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  662. show_temp_crit, set_temp_crit, 0),
  663. SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
  664. show_temp_crit, set_temp_crit, 1),
  665. SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
  666. show_temp_crit, set_temp_crit, 2),
  667. };
  668. static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  669. {
  670. struct pc87360_data *data = pc87360_update_device(dev);
  671. return sprintf(buf, "%u\n", data->temp_alarms);
  672. }
  673. static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
  674. /*
  675. * Device detection, registration and update
  676. */
  677. static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
  678. {
  679. u16 val;
  680. int i;
  681. int nrdev; /* logical device count */
  682. /* No superio_enter */
  683. /* Identify device */
  684. val = superio_inb(sioaddr, DEVID);
  685. switch (val) {
  686. case 0xE1: /* PC87360 */
  687. case 0xE8: /* PC87363 */
  688. case 0xE4: /* PC87364 */
  689. nrdev = 1;
  690. break;
  691. case 0xE5: /* PC87365 */
  692. case 0xE9: /* PC87366 */
  693. nrdev = 3;
  694. break;
  695. default:
  696. superio_exit(sioaddr);
  697. return -ENODEV;
  698. }
  699. /* Remember the device id */
  700. *devid = val;
  701. for (i = 0; i < nrdev; i++) {
  702. /* select logical device */
  703. superio_outb(sioaddr, DEV, logdev[i]);
  704. val = superio_inb(sioaddr, ACT);
  705. if (!(val & 0x01)) {
  706. printk(KERN_INFO "pc87360: Device 0x%02x not "
  707. "activated\n", logdev[i]);
  708. continue;
  709. }
  710. val = (superio_inb(sioaddr, BASE) << 8)
  711. | superio_inb(sioaddr, BASE + 1);
  712. if (!val) {
  713. printk(KERN_INFO "pc87360: Base address not set for "
  714. "device 0x%02x\n", logdev[i]);
  715. continue;
  716. }
  717. addresses[i] = val;
  718. if (i==0) { /* Fans */
  719. confreg[0] = superio_inb(sioaddr, 0xF0);
  720. confreg[1] = superio_inb(sioaddr, 0xF1);
  721. #ifdef DEBUG
  722. printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
  723. "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
  724. (confreg[0]>>3)&1, (confreg[0]>>4)&1);
  725. printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
  726. "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
  727. (confreg[0]>>6)&1, (confreg[0]>>7)&1);
  728. printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
  729. "ctrl=%d inv=%d\n", confreg[1]&1,
  730. (confreg[1]>>1)&1, (confreg[1]>>2)&1);
  731. #endif
  732. } else if (i==1) { /* Voltages */
  733. /* Are we using thermistors? */
  734. if (*devid == 0xE9) { /* PC87366 */
  735. /* These registers are not logical-device
  736. specific, just that we won't need them if
  737. we don't use the VLM device */
  738. confreg[2] = superio_inb(sioaddr, 0x2B);
  739. confreg[3] = superio_inb(sioaddr, 0x25);
  740. if (confreg[2] & 0x40) {
  741. printk(KERN_INFO "pc87360: Using "
  742. "thermistors for temperature "
  743. "monitoring\n");
  744. }
  745. if (confreg[3] & 0xE0) {
  746. printk(KERN_INFO "pc87360: VID "
  747. "inputs routed (mode %u)\n",
  748. confreg[3] >> 5);
  749. }
  750. }
  751. }
  752. }
  753. superio_exit(sioaddr);
  754. return 0;
  755. }
  756. static int pc87360_detect(struct i2c_adapter *adapter)
  757. {
  758. int i;
  759. struct i2c_client *client;
  760. struct pc87360_data *data;
  761. int err = 0;
  762. const char *name = "pc87360";
  763. int use_thermistors = 0;
  764. struct device *dev;
  765. if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
  766. return -ENOMEM;
  767. client = &data->client;
  768. dev = &client->dev;
  769. i2c_set_clientdata(client, data);
  770. client->addr = address;
  771. mutex_init(&data->lock);
  772. client->adapter = adapter;
  773. client->driver = &pc87360_driver;
  774. client->flags = 0;
  775. data->fannr = 2;
  776. data->innr = 0;
  777. data->tempnr = 0;
  778. switch (devid) {
  779. case 0xe8:
  780. name = "pc87363";
  781. break;
  782. case 0xe4:
  783. name = "pc87364";
  784. data->fannr = 3;
  785. break;
  786. case 0xe5:
  787. name = "pc87365";
  788. data->fannr = extra_isa[0] ? 3 : 0;
  789. data->innr = extra_isa[1] ? 11 : 0;
  790. data->tempnr = extra_isa[2] ? 2 : 0;
  791. break;
  792. case 0xe9:
  793. name = "pc87366";
  794. data->fannr = extra_isa[0] ? 3 : 0;
  795. data->innr = extra_isa[1] ? 14 : 0;
  796. data->tempnr = extra_isa[2] ? 3 : 0;
  797. break;
  798. }
  799. strlcpy(client->name, name, sizeof(client->name));
  800. data->valid = 0;
  801. mutex_init(&data->update_lock);
  802. for (i = 0; i < 3; i++) {
  803. if (((data->address[i] = extra_isa[i]))
  804. && !request_region(extra_isa[i], PC87360_EXTENT,
  805. pc87360_driver.driver.name)) {
  806. dev_err(&client->dev, "Region 0x%x-0x%x already "
  807. "in use!\n", extra_isa[i],
  808. extra_isa[i]+PC87360_EXTENT-1);
  809. for (i--; i >= 0; i--)
  810. release_region(extra_isa[i], PC87360_EXTENT);
  811. err = -EBUSY;
  812. goto ERROR1;
  813. }
  814. }
  815. /* Retrieve the fans configuration from Super-I/O space */
  816. if (data->fannr)
  817. data->fan_conf = confreg[0] | (confreg[1] << 8);
  818. if ((err = i2c_attach_client(client)))
  819. goto ERROR2;
  820. /* Use the correct reference voltage
  821. Unless both the VLM and the TMS logical devices agree to
  822. use an external Vref, the internal one is used. */
  823. if (data->innr) {
  824. i = pc87360_read_value(data, LD_IN, NO_BANK,
  825. PC87365_REG_IN_CONFIG);
  826. if (data->tempnr) {
  827. i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
  828. PC87365_REG_TEMP_CONFIG);
  829. }
  830. data->in_vref = (i&0x02) ? 3025 : 2966;
  831. dev_dbg(&client->dev, "Using %s reference voltage\n",
  832. (i&0x02) ? "external" : "internal");
  833. data->vid_conf = confreg[3];
  834. data->vrm = 90;
  835. }
  836. /* Fan clock dividers may be needed before any data is read */
  837. for (i = 0; i < data->fannr; i++) {
  838. if (FAN_CONFIG_MONITOR(data->fan_conf, i))
  839. data->fan_status[i] = pc87360_read_value(data,
  840. LD_FAN, NO_BANK,
  841. PC87360_REG_FAN_STATUS(i));
  842. }
  843. if (init > 0) {
  844. if (devid == 0xe9 && data->address[1]) /* PC87366 */
  845. use_thermistors = confreg[2] & 0x40;
  846. pc87360_init_client(client, use_thermistors);
  847. }
  848. /* Register sysfs hooks */
  849. data->class_dev = hwmon_device_register(&client->dev);
  850. if (IS_ERR(data->class_dev)) {
  851. err = PTR_ERR(data->class_dev);
  852. goto ERROR3;
  853. }
  854. if (data->innr) {
  855. for (i = 0; i < 11; i++) {
  856. device_create_file(dev, &in_input[i].dev_attr);
  857. device_create_file(dev, &in_min[i].dev_attr);
  858. device_create_file(dev, &in_max[i].dev_attr);
  859. device_create_file(dev, &in_status[i].dev_attr);
  860. }
  861. device_create_file(dev, &dev_attr_cpu0_vid);
  862. device_create_file(dev, &dev_attr_vrm);
  863. device_create_file(dev, &dev_attr_alarms_in);
  864. }
  865. if (data->tempnr) {
  866. for (i = 0; i < data->tempnr; i++) {
  867. device_create_file(dev, &temp_input[i].dev_attr);
  868. device_create_file(dev, &temp_min[i].dev_attr);
  869. device_create_file(dev, &temp_max[i].dev_attr);
  870. device_create_file(dev, &temp_crit[i].dev_attr);
  871. device_create_file(dev, &temp_status[i].dev_attr);
  872. }
  873. device_create_file(dev, &dev_attr_alarms_temp);
  874. }
  875. if (data->innr == 14) {
  876. for (i = 0; i < 3; i++) {
  877. device_create_file(dev, &therm_input[i].dev_attr);
  878. device_create_file(dev, &therm_min[i].dev_attr);
  879. device_create_file(dev, &therm_max[i].dev_attr);
  880. device_create_file(dev, &therm_crit[i].dev_attr);
  881. device_create_file(dev, &therm_status[i].dev_attr);
  882. }
  883. }
  884. for (i = 0; i < data->fannr; i++) {
  885. if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
  886. device_create_file(dev, &fan_input[i].dev_attr);
  887. device_create_file(dev, &fan_min[i].dev_attr);
  888. device_create_file(dev, &fan_div[i].dev_attr);
  889. device_create_file(dev, &fan_status[i].dev_attr);
  890. }
  891. if (FAN_CONFIG_CONTROL(data->fan_conf, i))
  892. device_create_file(dev, &pwm[i].dev_attr);
  893. }
  894. return 0;
  895. ERROR3:
  896. i2c_detach_client(client);
  897. ERROR2:
  898. for (i = 0; i < 3; i++) {
  899. if (data->address[i]) {
  900. release_region(data->address[i], PC87360_EXTENT);
  901. }
  902. }
  903. ERROR1:
  904. kfree(data);
  905. return err;
  906. }
  907. static int pc87360_detach_client(struct i2c_client *client)
  908. {
  909. struct pc87360_data *data = i2c_get_clientdata(client);
  910. int i;
  911. hwmon_device_unregister(data->class_dev);
  912. if ((i = i2c_detach_client(client)))
  913. return i;
  914. for (i = 0; i < 3; i++) {
  915. if (data->address[i]) {
  916. release_region(data->address[i], PC87360_EXTENT);
  917. }
  918. }
  919. kfree(data);
  920. return 0;
  921. }
  922. /* ldi is the logical device index
  923. bank is for voltages and temperatures only */
  924. static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
  925. u8 reg)
  926. {
  927. int res;
  928. mutex_lock(&(data->lock));
  929. if (bank != NO_BANK)
  930. outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
  931. res = inb_p(data->address[ldi] + reg);
  932. mutex_unlock(&(data->lock));
  933. return res;
  934. }
  935. static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
  936. u8 reg, u8 value)
  937. {
  938. mutex_lock(&(data->lock));
  939. if (bank != NO_BANK)
  940. outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
  941. outb_p(value, data->address[ldi] + reg);
  942. mutex_unlock(&(data->lock));
  943. }
  944. static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
  945. {
  946. struct pc87360_data *data = i2c_get_clientdata(client);
  947. int i, nr;
  948. const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
  949. const u8 init_temp[3] = { 2, 2, 1 };
  950. u8 reg;
  951. if (init >= 2 && data->innr) {
  952. reg = pc87360_read_value(data, LD_IN, NO_BANK,
  953. PC87365_REG_IN_CONVRATE);
  954. dev_info(&client->dev, "VLM conversion set to "
  955. "1s period, 160us delay\n");
  956. pc87360_write_value(data, LD_IN, NO_BANK,
  957. PC87365_REG_IN_CONVRATE,
  958. (reg & 0xC0) | 0x11);
  959. }
  960. nr = data->innr < 11 ? data->innr : 11;
  961. for (i = 0; i < nr; i++) {
  962. if (init >= init_in[i]) {
  963. /* Forcibly enable voltage channel */
  964. reg = pc87360_read_value(data, LD_IN, i,
  965. PC87365_REG_IN_STATUS);
  966. if (!(reg & 0x01)) {
  967. dev_dbg(&client->dev, "Forcibly "
  968. "enabling in%d\n", i);
  969. pc87360_write_value(data, LD_IN, i,
  970. PC87365_REG_IN_STATUS,
  971. (reg & 0x68) | 0x87);
  972. }
  973. }
  974. }
  975. /* We can't blindly trust the Super-I/O space configuration bit,
  976. most BIOS won't set it properly */
  977. for (i = 11; i < data->innr; i++) {
  978. reg = pc87360_read_value(data, LD_IN, i,
  979. PC87365_REG_TEMP_STATUS);
  980. use_thermistors = use_thermistors || (reg & 0x01);
  981. }
  982. i = use_thermistors ? 2 : 0;
  983. for (; i < data->tempnr; i++) {
  984. if (init >= init_temp[i]) {
  985. /* Forcibly enable temperature channel */
  986. reg = pc87360_read_value(data, LD_TEMP, i,
  987. PC87365_REG_TEMP_STATUS);
  988. if (!(reg & 0x01)) {
  989. dev_dbg(&client->dev, "Forcibly "
  990. "enabling temp%d\n", i+1);
  991. pc87360_write_value(data, LD_TEMP, i,
  992. PC87365_REG_TEMP_STATUS,
  993. 0xCF);
  994. }
  995. }
  996. }
  997. if (use_thermistors) {
  998. for (i = 11; i < data->innr; i++) {
  999. if (init >= init_in[i]) {
  1000. /* The pin may already be used by thermal
  1001. diodes */
  1002. reg = pc87360_read_value(data, LD_TEMP,
  1003. (i-11)/2, PC87365_REG_TEMP_STATUS);
  1004. if (reg & 0x01) {
  1005. dev_dbg(&client->dev, "Skipping "
  1006. "temp%d, pin already in use "
  1007. "by temp%d\n", i-7, (i-11)/2);
  1008. continue;
  1009. }
  1010. /* Forcibly enable thermistor channel */
  1011. reg = pc87360_read_value(data, LD_IN, i,
  1012. PC87365_REG_IN_STATUS);
  1013. if (!(reg & 0x01)) {
  1014. dev_dbg(&client->dev, "Forcibly "
  1015. "enabling temp%d\n", i-7);
  1016. pc87360_write_value(data, LD_IN, i,
  1017. PC87365_REG_TEMP_STATUS,
  1018. (reg & 0x60) | 0x8F);
  1019. }
  1020. }
  1021. }
  1022. }
  1023. if (data->innr) {
  1024. reg = pc87360_read_value(data, LD_IN, NO_BANK,
  1025. PC87365_REG_IN_CONFIG);
  1026. if (reg & 0x01) {
  1027. dev_dbg(&client->dev, "Forcibly "
  1028. "enabling monitoring (VLM)\n");
  1029. pc87360_write_value(data, LD_IN, NO_BANK,
  1030. PC87365_REG_IN_CONFIG,
  1031. reg & 0xFE);
  1032. }
  1033. }
  1034. if (data->tempnr) {
  1035. reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
  1036. PC87365_REG_TEMP_CONFIG);
  1037. if (reg & 0x01) {
  1038. dev_dbg(&client->dev, "Forcibly enabling "
  1039. "monitoring (TMS)\n");
  1040. pc87360_write_value(data, LD_TEMP, NO_BANK,
  1041. PC87365_REG_TEMP_CONFIG,
  1042. reg & 0xFE);
  1043. }
  1044. if (init >= 2) {
  1045. /* Chip config as documented by National Semi. */
  1046. pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
  1047. /* We voluntarily omit the bank here, in case the
  1048. sequence itself matters. It shouldn't be a problem,
  1049. since nobody else is supposed to access the
  1050. device at that point. */
  1051. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
  1052. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
  1053. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
  1054. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
  1055. }
  1056. }
  1057. }
  1058. static void pc87360_autodiv(struct i2c_client *client, int nr)
  1059. {
  1060. struct pc87360_data *data = i2c_get_clientdata(client);
  1061. u8 old_min = data->fan_min[nr];
  1062. /* Increase clock divider if needed and possible */
  1063. if ((data->fan_status[nr] & 0x04) /* overflow flag */
  1064. || (data->fan[nr] >= 224)) { /* next to overflow */
  1065. if ((data->fan_status[nr] & 0x60) != 0x60) {
  1066. data->fan_status[nr] += 0x20;
  1067. data->fan_min[nr] >>= 1;
  1068. data->fan[nr] >>= 1;
  1069. dev_dbg(&client->dev, "Increasing "
  1070. "clock divider to %d for fan %d\n",
  1071. FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
  1072. }
  1073. } else {
  1074. /* Decrease clock divider if possible */
  1075. while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
  1076. && data->fan[nr] < 85 /* bad accuracy */
  1077. && (data->fan_status[nr] & 0x60) != 0x00) {
  1078. data->fan_status[nr] -= 0x20;
  1079. data->fan_min[nr] <<= 1;
  1080. data->fan[nr] <<= 1;
  1081. dev_dbg(&client->dev, "Decreasing "
  1082. "clock divider to %d for fan %d\n",
  1083. FAN_DIV_FROM_REG(data->fan_status[nr]),
  1084. nr+1);
  1085. }
  1086. }
  1087. /* Write new fan min if it changed */
  1088. if (old_min != data->fan_min[nr]) {
  1089. pc87360_write_value(data, LD_FAN, NO_BANK,
  1090. PC87360_REG_FAN_MIN(nr),
  1091. data->fan_min[nr]);
  1092. }
  1093. }
  1094. static struct pc87360_data *pc87360_update_device(struct device *dev)
  1095. {
  1096. struct i2c_client *client = to_i2c_client(dev);
  1097. struct pc87360_data *data = i2c_get_clientdata(client);
  1098. u8 i;
  1099. mutex_lock(&data->update_lock);
  1100. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  1101. dev_dbg(&client->dev, "Data update\n");
  1102. /* Fans */
  1103. for (i = 0; i < data->fannr; i++) {
  1104. if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
  1105. data->fan_status[i] =
  1106. pc87360_read_value(data, LD_FAN,
  1107. NO_BANK, PC87360_REG_FAN_STATUS(i));
  1108. data->fan[i] = pc87360_read_value(data, LD_FAN,
  1109. NO_BANK, PC87360_REG_FAN(i));
  1110. data->fan_min[i] = pc87360_read_value(data,
  1111. LD_FAN, NO_BANK,
  1112. PC87360_REG_FAN_MIN(i));
  1113. /* Change clock divider if needed */
  1114. pc87360_autodiv(client, i);
  1115. /* Clear bits and write new divider */
  1116. pc87360_write_value(data, LD_FAN, NO_BANK,
  1117. PC87360_REG_FAN_STATUS(i),
  1118. data->fan_status[i]);
  1119. }
  1120. if (FAN_CONFIG_CONTROL(data->fan_conf, i))
  1121. data->pwm[i] = pc87360_read_value(data, LD_FAN,
  1122. NO_BANK, PC87360_REG_PWM(i));
  1123. }
  1124. /* Voltages */
  1125. for (i = 0; i < data->innr; i++) {
  1126. data->in_status[i] = pc87360_read_value(data, LD_IN, i,
  1127. PC87365_REG_IN_STATUS);
  1128. /* Clear bits */
  1129. pc87360_write_value(data, LD_IN, i,
  1130. PC87365_REG_IN_STATUS,
  1131. data->in_status[i]);
  1132. if ((data->in_status[i] & 0x81) == 0x81) {
  1133. data->in[i] = pc87360_read_value(data, LD_IN,
  1134. i, PC87365_REG_IN);
  1135. }
  1136. if (data->in_status[i] & 0x01) {
  1137. data->in_min[i] = pc87360_read_value(data,
  1138. LD_IN, i,
  1139. PC87365_REG_IN_MIN);
  1140. data->in_max[i] = pc87360_read_value(data,
  1141. LD_IN, i,
  1142. PC87365_REG_IN_MAX);
  1143. if (i >= 11)
  1144. data->in_crit[i-11] =
  1145. pc87360_read_value(data, LD_IN,
  1146. i, PC87365_REG_TEMP_CRIT);
  1147. }
  1148. }
  1149. if (data->innr) {
  1150. data->in_alarms = pc87360_read_value(data, LD_IN,
  1151. NO_BANK, PC87365_REG_IN_ALARMS1)
  1152. | ((pc87360_read_value(data, LD_IN,
  1153. NO_BANK, PC87365_REG_IN_ALARMS2)
  1154. & 0x07) << 8);
  1155. data->vid = (data->vid_conf & 0xE0) ?
  1156. pc87360_read_value(data, LD_IN,
  1157. NO_BANK, PC87365_REG_VID) : 0x1F;
  1158. }
  1159. /* Temperatures */
  1160. for (i = 0; i < data->tempnr; i++) {
  1161. data->temp_status[i] = pc87360_read_value(data,
  1162. LD_TEMP, i,
  1163. PC87365_REG_TEMP_STATUS);
  1164. /* Clear bits */
  1165. pc87360_write_value(data, LD_TEMP, i,
  1166. PC87365_REG_TEMP_STATUS,
  1167. data->temp_status[i]);
  1168. if ((data->temp_status[i] & 0x81) == 0x81) {
  1169. data->temp[i] = pc87360_read_value(data,
  1170. LD_TEMP, i,
  1171. PC87365_REG_TEMP);
  1172. }
  1173. if (data->temp_status[i] & 0x01) {
  1174. data->temp_min[i] = pc87360_read_value(data,
  1175. LD_TEMP, i,
  1176. PC87365_REG_TEMP_MIN);
  1177. data->temp_max[i] = pc87360_read_value(data,
  1178. LD_TEMP, i,
  1179. PC87365_REG_TEMP_MAX);
  1180. data->temp_crit[i] = pc87360_read_value(data,
  1181. LD_TEMP, i,
  1182. PC87365_REG_TEMP_CRIT);
  1183. }
  1184. }
  1185. if (data->tempnr) {
  1186. data->temp_alarms = pc87360_read_value(data, LD_TEMP,
  1187. NO_BANK, PC87365_REG_TEMP_ALARMS)
  1188. & 0x3F;
  1189. }
  1190. data->last_updated = jiffies;
  1191. data->valid = 1;
  1192. }
  1193. mutex_unlock(&data->update_lock);
  1194. return data;
  1195. }
  1196. static int __init pc87360_init(void)
  1197. {
  1198. int i;
  1199. if (pc87360_find(0x2e, &devid, extra_isa)
  1200. && pc87360_find(0x4e, &devid, extra_isa)) {
  1201. printk(KERN_WARNING "pc87360: PC8736x not detected, "
  1202. "module not inserted.\n");
  1203. return -ENODEV;
  1204. }
  1205. /* Arbitrarily pick one of the addresses */
  1206. for (i = 0; i < 3; i++) {
  1207. if (extra_isa[i] != 0x0000) {
  1208. address = extra_isa[i];
  1209. break;
  1210. }
  1211. }
  1212. if (address == 0x0000) {
  1213. printk(KERN_WARNING "pc87360: No active logical device, "
  1214. "module not inserted.\n");
  1215. return -ENODEV;
  1216. }
  1217. return i2c_isa_add_driver(&pc87360_driver);
  1218. }
  1219. static void __exit pc87360_exit(void)
  1220. {
  1221. i2c_isa_del_driver(&pc87360_driver);
  1222. }
  1223. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  1224. MODULE_DESCRIPTION("PC8736x hardware monitor");
  1225. MODULE_LICENSE("GPL");
  1226. module_init(pc87360_init);
  1227. module_exit(pc87360_exit);