sch5627.c 26 KB

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