/kern_oII/drivers/hwmon/pc87360.c

http://omnia2droid.googlecode.com/ · C · 1714 lines · 1410 code · 180 blank · 124 comment · 128 complexity · 10bc504f228aff6737a141eeb8d2ed44 MD5 · raw file

Large files are truncated click here to view the full file

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