sch5627.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /***************************************************************************
  2. * Copyright (C) 2010-2011 Hans de Goede <hdegoede@redhat.com> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/err.h>
  28. #include <linux/mutex.h>
  29. #include <linux/io.h>
  30. #include <linux/acpi.h>
  31. #include <linux/delay.h>
  32. #define DRVNAME "sch5627"
  33. #define DEVNAME DRVNAME /* We only support one model */
  34. #define SIO_SCH5627_EM_LD 0x0C /* Embedded Microcontroller LD */
  35. #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */
  36. #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
  37. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  38. #define SIO_REG_DEVID 0x20 /* Device ID */
  39. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  40. #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */
  41. #define SIO_SCH5627_ID 0xC6 /* Chipset ID */
  42. #define REGION_LENGTH 9
  43. #define SCH5627_HWMON_ID 0xa5
  44. #define SCH5627_COMPANY_ID 0x5c
  45. #define SCH5627_PRIMARY_ID 0xa0
  46. #define SCH5627_REG_BUILD_CODE 0x39
  47. #define SCH5627_REG_BUILD_ID 0x3a
  48. #define SCH5627_REG_HWMON_ID 0x3c
  49. #define SCH5627_REG_HWMON_REV 0x3d
  50. #define SCH5627_REG_COMPANY_ID 0x3e
  51. #define SCH5627_REG_PRIMARY_ID 0x3f
  52. #define SCH5627_REG_CTRL 0x40
  53. #define SCH5627_NO_TEMPS 8
  54. #define SCH5627_NO_FANS 4
  55. #define SCH5627_NO_IN 5
  56. static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = {
  57. 0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 };
  58. static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = {
  59. 0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 };
  60. static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = {
  61. 0, 0, 1, 1, 0, 0, 0, 1 };
  62. static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = {
  63. 0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 };
  64. static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = {
  65. 0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 };
  66. static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = {
  67. 0x2C, 0x2E, 0x30, 0x32 };
  68. static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = {
  69. 0x62, 0x64, 0x66, 0x68 };
  70. static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = {
  71. 0x22, 0x23, 0x24, 0x25, 0x189 };
  72. static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = {
  73. 0xE4, 0xE4, 0xE3, 0xE3, 0x18A };
  74. static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = {
  75. 1, 0, 1, 0, 1 };
  76. static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = {
  77. 10745, 3660, 9765, 10745, 3660 };
  78. static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = {
  79. "VCC", "VTT", "VBAT", "VTR", "V_IN" };
  80. struct sch5627_data {
  81. unsigned short addr;
  82. struct device *hwmon_dev;
  83. u8 temp_max[SCH5627_NO_TEMPS];
  84. u8 temp_crit[SCH5627_NO_TEMPS];
  85. u16 fan_min[SCH5627_NO_FANS];
  86. struct mutex update_lock;
  87. char valid; /* !=0 if following fields are valid */
  88. unsigned long last_updated; /* In jiffies */
  89. u16 temp[SCH5627_NO_TEMPS];
  90. u16 fan[SCH5627_NO_FANS];
  91. u16 in[SCH5627_NO_IN];
  92. };
  93. static struct platform_device *sch5627_pdev;
  94. /* Super I/O functions */
  95. static inline int superio_inb(int base, int reg)
  96. {
  97. outb(reg, base);
  98. return inb(base + 1);
  99. }
  100. static inline int superio_enter(int base)
  101. {
  102. /* Don't step on other drivers' I/O space by accident */
  103. if (!request_muxed_region(base, 2, DRVNAME)) {
  104. pr_err("I/O address 0x%04x already in use\n", base);
  105. return -EBUSY;
  106. }
  107. outb(SIO_UNLOCK_KEY, base);
  108. return 0;
  109. }
  110. static inline void superio_select(int base, int ld)
  111. {
  112. outb(SIO_REG_LDSEL, base);
  113. outb(ld, base + 1);
  114. }
  115. static inline void superio_exit(int base)
  116. {
  117. outb(SIO_LOCK_KEY, base);
  118. release_region(base, 2);
  119. }
  120. static int sch5627_read_virtual_reg(struct sch5627_data *data, u16 reg)
  121. {
  122. u8 val;
  123. int i;
  124. /*
  125. * According to SMSC for the commands we use the maximum time for
  126. * the EM to respond is 15 ms, but testing shows in practice it
  127. * responds within 15-32 reads, so we first busy poll, and if
  128. * that fails sleep a bit and try again until we are way past
  129. * the 15 ms maximum response time.
  130. */
  131. const int max_busy_polls = 64;
  132. const int max_lazy_polls = 32;
  133. /* (Optional) Write-Clear the EC to Host Mailbox Register */
  134. val = inb(data->addr + 1);
  135. outb(val, data->addr + 1);
  136. /* Set Mailbox Address Pointer to first location in Region 1 */
  137. outb(0x00, data->addr + 2);
  138. outb(0x80, data->addr + 3);
  139. /* Write Request Packet Header */
  140. outb(0x02, data->addr + 4); /* Access Type: VREG read */
  141. outb(0x01, data->addr + 5); /* # of Entries: 1 Byte (8-bit) */
  142. outb(0x04, data->addr + 2); /* Mailbox AP to first data entry loc. */
  143. /* Write Address field */
  144. outb(reg & 0xff, data->addr + 6);
  145. outb(reg >> 8, data->addr + 7);
  146. /* Execute the Random Access Command */
  147. outb(0x01, data->addr); /* Write 01h to the Host-to-EC register */
  148. /* EM Interface Polling "Algorithm" */
  149. for (i = 0; i < max_busy_polls + max_lazy_polls; i++) {
  150. if (i >= max_busy_polls)
  151. msleep(1);
  152. /* Read Interrupt source Register */
  153. val = inb(data->addr + 8);
  154. /* Write Clear the interrupt source bits */
  155. if (val)
  156. outb(val, data->addr + 8);
  157. /* Command Completed ? */
  158. if (val & 0x01)
  159. break;
  160. }
  161. if (i == max_busy_polls + max_lazy_polls) {
  162. pr_err("Max retries exceeded reading virtual "
  163. "register 0x%04hx (%d)\n", reg, 1);
  164. return -EIO;
  165. }
  166. /*
  167. * According to SMSC we may need to retry this, but sofar I've always
  168. * seen this succeed in 1 try.
  169. */
  170. for (i = 0; i < max_busy_polls; i++) {
  171. /* Read EC-to-Host Register */
  172. val = inb(data->addr + 1);
  173. /* Command Completed ? */
  174. if (val == 0x01)
  175. break;
  176. if (i == 0)
  177. pr_warn("EC reports: 0x%02x reading virtual register "
  178. "0x%04hx\n", (unsigned int)val, reg);
  179. }
  180. if (i == max_busy_polls) {
  181. pr_err("Max retries exceeded reading virtual "
  182. "register 0x%04hx (%d)\n", reg, 2);
  183. return -EIO;
  184. }
  185. /*
  186. * According to the SMSC app note we should now do:
  187. *
  188. * Set Mailbox Address Pointer to first location in Region 1 *
  189. * outb(0x00, data->addr + 2);
  190. * outb(0x80, data->addr + 3);
  191. *
  192. * But if we do that things don't work, so let's not.
  193. */
  194. /* Read Data from Mailbox */
  195. return inb(data->addr + 4);
  196. }
  197. static int sch5627_read_virtual_reg16(struct sch5627_data *data, u16 reg)
  198. {
  199. int lsb, msb;
  200. /* Read LSB first, this will cause the matching MSB to be latched */
  201. lsb = sch5627_read_virtual_reg(data, reg);
  202. if (lsb < 0)
  203. return lsb;
  204. msb = sch5627_read_virtual_reg(data, reg + 1);
  205. if (msb < 0)
  206. return msb;
  207. return lsb | (msb << 8);
  208. }
  209. static int sch5627_read_virtual_reg12(struct sch5627_data *data, u16 msb_reg,
  210. u16 lsn_reg, int high_nibble)
  211. {
  212. int msb, lsn;
  213. /* Read MSB first, this will cause the matching LSN to be latched */
  214. msb = sch5627_read_virtual_reg(data, msb_reg);
  215. if (msb < 0)
  216. return msb;
  217. lsn = sch5627_read_virtual_reg(data, lsn_reg);
  218. if (lsn < 0)
  219. return lsn;
  220. if (high_nibble)
  221. return (msb << 4) | (lsn >> 4);
  222. else
  223. return (msb << 4) | (lsn & 0x0f);
  224. }
  225. static struct sch5627_data *sch5627_update_device(struct device *dev)
  226. {
  227. struct sch5627_data *data = dev_get_drvdata(dev);
  228. struct sch5627_data *ret = data;
  229. int i, val;
  230. mutex_lock(&data->update_lock);
  231. /* Cache the values for 1 second */
  232. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  233. for (i = 0; i < SCH5627_NO_TEMPS; i++) {
  234. val = sch5627_read_virtual_reg12(data,
  235. SCH5627_REG_TEMP_MSB[i],
  236. SCH5627_REG_TEMP_LSN[i],
  237. SCH5627_REG_TEMP_HIGH_NIBBLE[i]);
  238. if (unlikely(val < 0)) {
  239. ret = ERR_PTR(val);
  240. goto abort;
  241. }
  242. data->temp[i] = val;
  243. }
  244. for (i = 0; i < SCH5627_NO_FANS; i++) {
  245. val = sch5627_read_virtual_reg16(data,
  246. SCH5627_REG_FAN[i]);
  247. if (unlikely(val < 0)) {
  248. ret = ERR_PTR(val);
  249. goto abort;
  250. }
  251. data->fan[i] = val;
  252. }
  253. for (i = 0; i < SCH5627_NO_IN; i++) {
  254. val = sch5627_read_virtual_reg12(data,
  255. SCH5627_REG_IN_MSB[i],
  256. SCH5627_REG_IN_LSN[i],
  257. SCH5627_REG_IN_HIGH_NIBBLE[i]);
  258. if (unlikely(val < 0)) {
  259. ret = ERR_PTR(val);
  260. goto abort;
  261. }
  262. data->in[i] = val;
  263. }
  264. data->last_updated = jiffies;
  265. data->valid = 1;
  266. }
  267. abort:
  268. mutex_unlock(&data->update_lock);
  269. return ret;
  270. }
  271. static int __devinit sch5627_read_limits(struct sch5627_data *data)
  272. {
  273. int i, val;
  274. for (i = 0; i < SCH5627_NO_TEMPS; i++) {
  275. /*
  276. * Note what SMSC calls ABS, is what lm_sensors calls max
  277. * (aka high), and HIGH is what lm_sensors calls crit.
  278. */
  279. val = sch5627_read_virtual_reg(data, SCH5627_REG_TEMP_ABS[i]);
  280. if (val < 0)
  281. return val;
  282. data->temp_max[i] = val;
  283. val = sch5627_read_virtual_reg(data, SCH5627_REG_TEMP_HIGH[i]);
  284. if (val < 0)
  285. return val;
  286. data->temp_crit[i] = val;
  287. }
  288. for (i = 0; i < SCH5627_NO_FANS; i++) {
  289. val = sch5627_read_virtual_reg16(data, SCH5627_REG_FAN_MIN[i]);
  290. if (val < 0)
  291. return val;
  292. data->fan_min[i] = val;
  293. }
  294. return 0;
  295. }
  296. static int reg_to_temp(u16 reg)
  297. {
  298. return (reg * 625) / 10 - 64000;
  299. }
  300. static int reg_to_temp_limit(u8 reg)
  301. {
  302. return (reg - 64) * 1000;
  303. }
  304. static int reg_to_rpm(u16 reg)
  305. {
  306. if (reg == 0)
  307. return -EIO;
  308. if (reg == 0xffff)
  309. return 0;
  310. return 5400540 / reg;
  311. }
  312. static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
  313. char *buf)
  314. {
  315. return snprintf(buf, PAGE_SIZE, "%s\n", DEVNAME);
  316. }
  317. static ssize_t show_temp(struct device *dev, struct device_attribute
  318. *devattr, char *buf)
  319. {
  320. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  321. struct sch5627_data *data = sch5627_update_device(dev);
  322. int val;
  323. if (IS_ERR(data))
  324. return PTR_ERR(data);
  325. val = reg_to_temp(data->temp[attr->index]);
  326. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  327. }
  328. static ssize_t show_temp_fault(struct device *dev, struct device_attribute
  329. *devattr, char *buf)
  330. {
  331. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  332. struct sch5627_data *data = sch5627_update_device(dev);
  333. if (IS_ERR(data))
  334. return PTR_ERR(data);
  335. return snprintf(buf, PAGE_SIZE, "%d\n", data->temp[attr->index] == 0);
  336. }
  337. static ssize_t show_temp_max(struct device *dev, struct device_attribute
  338. *devattr, char *buf)
  339. {
  340. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  341. struct sch5627_data *data = dev_get_drvdata(dev);
  342. int val;
  343. val = reg_to_temp_limit(data->temp_max[attr->index]);
  344. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  345. }
  346. static ssize_t show_temp_crit(struct device *dev, struct device_attribute
  347. *devattr, char *buf)
  348. {
  349. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  350. struct sch5627_data *data = dev_get_drvdata(dev);
  351. int val;
  352. val = reg_to_temp_limit(data->temp_crit[attr->index]);
  353. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  354. }
  355. static ssize_t show_fan(struct device *dev, struct device_attribute
  356. *devattr, char *buf)
  357. {
  358. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  359. struct sch5627_data *data = sch5627_update_device(dev);
  360. int val;
  361. if (IS_ERR(data))
  362. return PTR_ERR(data);
  363. val = reg_to_rpm(data->fan[attr->index]);
  364. if (val < 0)
  365. return val;
  366. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  367. }
  368. static ssize_t show_fan_fault(struct device *dev, struct device_attribute
  369. *devattr, char *buf)
  370. {
  371. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  372. struct sch5627_data *data = sch5627_update_device(dev);
  373. if (IS_ERR(data))
  374. return PTR_ERR(data);
  375. return snprintf(buf, PAGE_SIZE, "%d\n",
  376. data->fan[attr->index] == 0xffff);
  377. }
  378. static ssize_t show_fan_min(struct device *dev, struct device_attribute
  379. *devattr, char *buf)
  380. {
  381. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  382. struct sch5627_data *data = dev_get_drvdata(dev);
  383. int val = reg_to_rpm(data->fan_min[attr->index]);
  384. if (val < 0)
  385. return val;
  386. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  387. }
  388. static ssize_t show_in(struct device *dev, struct device_attribute
  389. *devattr, char *buf)
  390. {
  391. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  392. struct sch5627_data *data = sch5627_update_device(dev);
  393. int val;
  394. if (IS_ERR(data))
  395. return PTR_ERR(data);
  396. val = DIV_ROUND_CLOSEST(
  397. data->in[attr->index] * SCH5627_REG_IN_FACTOR[attr->index],
  398. 10000);
  399. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  400. }
  401. static ssize_t show_in_label(struct device *dev, struct device_attribute
  402. *devattr, char *buf)
  403. {
  404. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  405. return snprintf(buf, PAGE_SIZE, "%s\n",
  406. SCH5627_IN_LABELS[attr->index]);
  407. }
  408. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  409. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  410. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  411. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  412. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  413. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
  414. static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
  415. static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
  416. static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
  417. static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
  418. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
  419. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
  420. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
  421. static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4);
  422. static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5);
  423. static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_temp_fault, NULL, 6);
  424. static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_temp_fault, NULL, 7);
  425. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL, 0);
  426. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, NULL, 1);
  427. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, NULL, 2);
  428. static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, NULL, 3);
  429. static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, NULL, 4);
  430. static SENSOR_DEVICE_ATTR(temp6_max, S_IRUGO, show_temp_max, NULL, 5);
  431. static SENSOR_DEVICE_ATTR(temp7_max, S_IRUGO, show_temp_max, NULL, 6);
  432. static SENSOR_DEVICE_ATTR(temp8_max, S_IRUGO, show_temp_max, NULL, 7);
  433. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0);
  434. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL, 1);
  435. static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit, NULL, 2);
  436. static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL, 3);
  437. static SENSOR_DEVICE_ATTR(temp5_crit, S_IRUGO, show_temp_crit, NULL, 4);
  438. static SENSOR_DEVICE_ATTR(temp6_crit, S_IRUGO, show_temp_crit, NULL, 5);
  439. static SENSOR_DEVICE_ATTR(temp7_crit, S_IRUGO, show_temp_crit, NULL, 6);
  440. static SENSOR_DEVICE_ATTR(temp8_crit, S_IRUGO, show_temp_crit, NULL, 7);
  441. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  442. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  443. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  444. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
  445. static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL, 0);
  446. static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault, NULL, 1);
  447. static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, show_fan_fault, NULL, 2);
  448. static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, show_fan_fault, NULL, 3);
  449. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan_min, NULL, 0);
  450. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan_min, NULL, 1);
  451. static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO, show_fan_min, NULL, 2);
  452. static SENSOR_DEVICE_ATTR(fan4_min, S_IRUGO, show_fan_min, NULL, 3);
  453. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
  454. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
  455. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
  456. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
  457. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
  458. static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_in_label, NULL, 0);
  459. static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_in_label, NULL, 1);
  460. static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_in_label, NULL, 2);
  461. static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_in_label, NULL, 3);
  462. static struct attribute *sch5627_attributes[] = {
  463. &dev_attr_name.attr,
  464. &sensor_dev_attr_temp1_input.dev_attr.attr,
  465. &sensor_dev_attr_temp2_input.dev_attr.attr,
  466. &sensor_dev_attr_temp3_input.dev_attr.attr,
  467. &sensor_dev_attr_temp4_input.dev_attr.attr,
  468. &sensor_dev_attr_temp5_input.dev_attr.attr,
  469. &sensor_dev_attr_temp6_input.dev_attr.attr,
  470. &sensor_dev_attr_temp7_input.dev_attr.attr,
  471. &sensor_dev_attr_temp8_input.dev_attr.attr,
  472. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  473. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  474. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  475. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  476. &sensor_dev_attr_temp5_fault.dev_attr.attr,
  477. &sensor_dev_attr_temp6_fault.dev_attr.attr,
  478. &sensor_dev_attr_temp7_fault.dev_attr.attr,
  479. &sensor_dev_attr_temp8_fault.dev_attr.attr,
  480. &sensor_dev_attr_temp1_max.dev_attr.attr,
  481. &sensor_dev_attr_temp2_max.dev_attr.attr,
  482. &sensor_dev_attr_temp3_max.dev_attr.attr,
  483. &sensor_dev_attr_temp4_max.dev_attr.attr,
  484. &sensor_dev_attr_temp5_max.dev_attr.attr,
  485. &sensor_dev_attr_temp6_max.dev_attr.attr,
  486. &sensor_dev_attr_temp7_max.dev_attr.attr,
  487. &sensor_dev_attr_temp8_max.dev_attr.attr,
  488. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  489. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  490. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  491. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  492. &sensor_dev_attr_temp5_crit.dev_attr.attr,
  493. &sensor_dev_attr_temp6_crit.dev_attr.attr,
  494. &sensor_dev_attr_temp7_crit.dev_attr.attr,
  495. &sensor_dev_attr_temp8_crit.dev_attr.attr,
  496. &sensor_dev_attr_fan1_input.dev_attr.attr,
  497. &sensor_dev_attr_fan2_input.dev_attr.attr,
  498. &sensor_dev_attr_fan3_input.dev_attr.attr,
  499. &sensor_dev_attr_fan4_input.dev_attr.attr,
  500. &sensor_dev_attr_fan1_fault.dev_attr.attr,
  501. &sensor_dev_attr_fan2_fault.dev_attr.attr,
  502. &sensor_dev_attr_fan3_fault.dev_attr.attr,
  503. &sensor_dev_attr_fan4_fault.dev_attr.attr,
  504. &sensor_dev_attr_fan1_min.dev_attr.attr,
  505. &sensor_dev_attr_fan2_min.dev_attr.attr,
  506. &sensor_dev_attr_fan3_min.dev_attr.attr,
  507. &sensor_dev_attr_fan4_min.dev_attr.attr,
  508. &sensor_dev_attr_in0_input.dev_attr.attr,
  509. &sensor_dev_attr_in1_input.dev_attr.attr,
  510. &sensor_dev_attr_in2_input.dev_attr.attr,
  511. &sensor_dev_attr_in3_input.dev_attr.attr,
  512. &sensor_dev_attr_in4_input.dev_attr.attr,
  513. &sensor_dev_attr_in0_label.dev_attr.attr,
  514. &sensor_dev_attr_in1_label.dev_attr.attr,
  515. &sensor_dev_attr_in2_label.dev_attr.attr,
  516. &sensor_dev_attr_in3_label.dev_attr.attr,
  517. /* No in4_label as in4 is a generic input pin */
  518. NULL
  519. };
  520. static const struct attribute_group sch5627_group = {
  521. .attrs = sch5627_attributes,
  522. };
  523. static int sch5627_remove(struct platform_device *pdev)
  524. {
  525. struct sch5627_data *data = platform_get_drvdata(pdev);
  526. if (data->hwmon_dev)
  527. hwmon_device_unregister(data->hwmon_dev);
  528. sysfs_remove_group(&pdev->dev.kobj, &sch5627_group);
  529. platform_set_drvdata(pdev, NULL);
  530. kfree(data);
  531. return 0;
  532. }
  533. static int __devinit sch5627_probe(struct platform_device *pdev)
  534. {
  535. struct sch5627_data *data;
  536. int err, build_code, build_id, hwmon_rev, val;
  537. data = kzalloc(sizeof(struct sch5627_data), GFP_KERNEL);
  538. if (!data)
  539. return -ENOMEM;
  540. data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
  541. mutex_init(&data->update_lock);
  542. platform_set_drvdata(pdev, data);
  543. val = sch5627_read_virtual_reg(data, SCH5627_REG_HWMON_ID);
  544. if (val < 0) {
  545. err = val;
  546. goto error;
  547. }
  548. if (val != SCH5627_HWMON_ID) {
  549. pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon",
  550. val, SCH5627_HWMON_ID);
  551. err = -ENODEV;
  552. goto error;
  553. }
  554. val = sch5627_read_virtual_reg(data, SCH5627_REG_COMPANY_ID);
  555. if (val < 0) {
  556. err = val;
  557. goto error;
  558. }
  559. if (val != SCH5627_COMPANY_ID) {
  560. pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company",
  561. val, SCH5627_COMPANY_ID);
  562. err = -ENODEV;
  563. goto error;
  564. }
  565. val = sch5627_read_virtual_reg(data, SCH5627_REG_PRIMARY_ID);
  566. if (val < 0) {
  567. err = val;
  568. goto error;
  569. }
  570. if (val != SCH5627_PRIMARY_ID) {
  571. pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary",
  572. val, SCH5627_PRIMARY_ID);
  573. err = -ENODEV;
  574. goto error;
  575. }
  576. build_code = sch5627_read_virtual_reg(data, SCH5627_REG_BUILD_CODE);
  577. if (build_code < 0) {
  578. err = build_code;
  579. goto error;
  580. }
  581. build_id = sch5627_read_virtual_reg16(data, SCH5627_REG_BUILD_ID);
  582. if (build_id < 0) {
  583. err = build_id;
  584. goto error;
  585. }
  586. hwmon_rev = sch5627_read_virtual_reg(data, SCH5627_REG_HWMON_REV);
  587. if (hwmon_rev < 0) {
  588. err = hwmon_rev;
  589. goto error;
  590. }
  591. val = sch5627_read_virtual_reg(data, SCH5627_REG_CTRL);
  592. if (val < 0) {
  593. err = val;
  594. goto error;
  595. }
  596. if (!(val & 0x01)) {
  597. pr_err("hardware monitoring not enabled\n");
  598. err = -ENODEV;
  599. goto error;
  600. }
  601. /*
  602. * Read limits, we do this only once as reading a register on
  603. * the sch5627 is quite expensive (and they don't change).
  604. */
  605. err = sch5627_read_limits(data);
  606. if (err)
  607. goto error;
  608. pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n",
  609. build_code, build_id, hwmon_rev);
  610. /* Register sysfs interface files */
  611. err = sysfs_create_group(&pdev->dev.kobj, &sch5627_group);
  612. if (err)
  613. goto error;
  614. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  615. if (IS_ERR(data->hwmon_dev)) {
  616. err = PTR_ERR(data->hwmon_dev);
  617. data->hwmon_dev = NULL;
  618. goto error;
  619. }
  620. return 0;
  621. error:
  622. sch5627_remove(pdev);
  623. return err;
  624. }
  625. static int __init sch5627_find(int sioaddr, unsigned short *address)
  626. {
  627. u8 devid;
  628. int err = superio_enter(sioaddr);
  629. if (err)
  630. return err;
  631. devid = superio_inb(sioaddr, SIO_REG_DEVID);
  632. if (devid != SIO_SCH5627_ID) {
  633. pr_debug("Unsupported device id: 0x%02x\n",
  634. (unsigned int)devid);
  635. err = -ENODEV;
  636. goto exit;
  637. }
  638. superio_select(sioaddr, SIO_SCH5627_EM_LD);
  639. if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
  640. pr_warn("Device not activated\n");
  641. err = -ENODEV;
  642. goto exit;
  643. }
  644. /*
  645. * Warning the order of the low / high byte is the other way around
  646. * as on most other superio devices!!
  647. */
  648. *address = superio_inb(sioaddr, SIO_REG_ADDR) |
  649. superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
  650. if (*address == 0) {
  651. pr_warn("Base address not set\n");
  652. err = -ENODEV;
  653. goto exit;
  654. }
  655. pr_info("Found %s chip at %#hx\n", DEVNAME, *address);
  656. exit:
  657. superio_exit(sioaddr);
  658. return err;
  659. }
  660. static int __init sch5627_device_add(unsigned short address)
  661. {
  662. struct resource res = {
  663. .start = address,
  664. .end = address + REGION_LENGTH - 1,
  665. .flags = IORESOURCE_IO,
  666. };
  667. int err;
  668. sch5627_pdev = platform_device_alloc(DRVNAME, address);
  669. if (!sch5627_pdev)
  670. return -ENOMEM;
  671. res.name = sch5627_pdev->name;
  672. err = acpi_check_resource_conflict(&res);
  673. if (err)
  674. goto exit_device_put;
  675. err = platform_device_add_resources(sch5627_pdev, &res, 1);
  676. if (err) {
  677. pr_err("Device resource addition failed\n");
  678. goto exit_device_put;
  679. }
  680. err = platform_device_add(sch5627_pdev);
  681. if (err) {
  682. pr_err("Device addition failed\n");
  683. goto exit_device_put;
  684. }
  685. return 0;
  686. exit_device_put:
  687. platform_device_put(sch5627_pdev);
  688. return err;
  689. }
  690. static struct platform_driver sch5627_driver = {
  691. .driver = {
  692. .owner = THIS_MODULE,
  693. .name = DRVNAME,
  694. },
  695. .probe = sch5627_probe,
  696. .remove = sch5627_remove,
  697. };
  698. static int __init sch5627_init(void)
  699. {
  700. int err = -ENODEV;
  701. unsigned short address;
  702. if (sch5627_find(0x4e, &address) && sch5627_find(0x2e, &address))
  703. goto exit;
  704. err = platform_driver_register(&sch5627_driver);
  705. if (err)
  706. goto exit;
  707. err = sch5627_device_add(address);
  708. if (err)
  709. goto exit_driver;
  710. return 0;
  711. exit_driver:
  712. platform_driver_unregister(&sch5627_driver);
  713. exit:
  714. return err;
  715. }
  716. static void __exit sch5627_exit(void)
  717. {
  718. platform_device_unregister(sch5627_pdev);
  719. platform_driver_unregister(&sch5627_driver);
  720. }
  721. MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver");
  722. MODULE_AUTHOR("Hans de Goede (hdegoede@redhat.com)");
  723. MODULE_LICENSE("GPL");
  724. module_init(sch5627_init);
  725. module_exit(sch5627_exit);