sch5627.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 "sch56xx-common.h"
  30. #define DRVNAME "sch5627"
  31. #define DEVNAME DRVNAME /* We only support one model */
  32. #define SCH5627_HWMON_ID 0xa5
  33. #define SCH5627_COMPANY_ID 0x5c
  34. #define SCH5627_PRIMARY_ID 0xa0
  35. #define SCH5627_REG_BUILD_CODE 0x39
  36. #define SCH5627_REG_BUILD_ID 0x3a
  37. #define SCH5627_REG_HWMON_ID 0x3c
  38. #define SCH5627_REG_HWMON_REV 0x3d
  39. #define SCH5627_REG_COMPANY_ID 0x3e
  40. #define SCH5627_REG_PRIMARY_ID 0x3f
  41. #define SCH5627_REG_CTRL 0x40
  42. #define SCH5627_NO_TEMPS 8
  43. #define SCH5627_NO_FANS 4
  44. #define SCH5627_NO_IN 5
  45. static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = {
  46. 0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 };
  47. static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = {
  48. 0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 };
  49. static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = {
  50. 0, 0, 1, 1, 0, 0, 0, 1 };
  51. static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = {
  52. 0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 };
  53. static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = {
  54. 0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 };
  55. static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = {
  56. 0x2C, 0x2E, 0x30, 0x32 };
  57. static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = {
  58. 0x62, 0x64, 0x66, 0x68 };
  59. static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = {
  60. 0x22, 0x23, 0x24, 0x25, 0x189 };
  61. static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = {
  62. 0xE4, 0xE4, 0xE3, 0xE3, 0x18A };
  63. static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = {
  64. 1, 0, 1, 0, 1 };
  65. static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = {
  66. 10745, 3660, 9765, 10745, 3660 };
  67. static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = {
  68. "VCC", "VTT", "VBAT", "VTR", "V_IN" };
  69. struct sch5627_data {
  70. unsigned short addr;
  71. struct device *hwmon_dev;
  72. u8 control;
  73. u8 temp_max[SCH5627_NO_TEMPS];
  74. u8 temp_crit[SCH5627_NO_TEMPS];
  75. u16 fan_min[SCH5627_NO_FANS];
  76. struct mutex update_lock;
  77. unsigned long last_battery; /* In jiffies */
  78. char valid; /* !=0 if following fields are valid */
  79. unsigned long last_updated; /* In jiffies */
  80. u16 temp[SCH5627_NO_TEMPS];
  81. u16 fan[SCH5627_NO_FANS];
  82. u16 in[SCH5627_NO_IN];
  83. };
  84. static struct sch5627_data *sch5627_update_device(struct device *dev)
  85. {
  86. struct sch5627_data *data = dev_get_drvdata(dev);
  87. struct sch5627_data *ret = data;
  88. int i, val;
  89. mutex_lock(&data->update_lock);
  90. /* Trigger a Vbat voltage measurement every 5 minutes */
  91. if (time_after(jiffies, data->last_battery + 300 * HZ)) {
  92. sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL,
  93. data->control | 0x10);
  94. data->last_battery = jiffies;
  95. }
  96. /* Cache the values for 1 second */
  97. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  98. for (i = 0; i < SCH5627_NO_TEMPS; i++) {
  99. val = sch56xx_read_virtual_reg12(data->addr,
  100. SCH5627_REG_TEMP_MSB[i],
  101. SCH5627_REG_TEMP_LSN[i],
  102. SCH5627_REG_TEMP_HIGH_NIBBLE[i]);
  103. if (unlikely(val < 0)) {
  104. ret = ERR_PTR(val);
  105. goto abort;
  106. }
  107. data->temp[i] = val;
  108. }
  109. for (i = 0; i < SCH5627_NO_FANS; i++) {
  110. val = sch56xx_read_virtual_reg16(data->addr,
  111. SCH5627_REG_FAN[i]);
  112. if (unlikely(val < 0)) {
  113. ret = ERR_PTR(val);
  114. goto abort;
  115. }
  116. data->fan[i] = val;
  117. }
  118. for (i = 0; i < SCH5627_NO_IN; i++) {
  119. val = sch56xx_read_virtual_reg12(data->addr,
  120. SCH5627_REG_IN_MSB[i],
  121. SCH5627_REG_IN_LSN[i],
  122. SCH5627_REG_IN_HIGH_NIBBLE[i]);
  123. if (unlikely(val < 0)) {
  124. ret = ERR_PTR(val);
  125. goto abort;
  126. }
  127. data->in[i] = val;
  128. }
  129. data->last_updated = jiffies;
  130. data->valid = 1;
  131. }
  132. abort:
  133. mutex_unlock(&data->update_lock);
  134. return ret;
  135. }
  136. static int __devinit sch5627_read_limits(struct sch5627_data *data)
  137. {
  138. int i, val;
  139. for (i = 0; i < SCH5627_NO_TEMPS; i++) {
  140. /*
  141. * Note what SMSC calls ABS, is what lm_sensors calls max
  142. * (aka high), and HIGH is what lm_sensors calls crit.
  143. */
  144. val = sch56xx_read_virtual_reg(data->addr,
  145. SCH5627_REG_TEMP_ABS[i]);
  146. if (val < 0)
  147. return val;
  148. data->temp_max[i] = val;
  149. val = sch56xx_read_virtual_reg(data->addr,
  150. SCH5627_REG_TEMP_HIGH[i]);
  151. if (val < 0)
  152. return val;
  153. data->temp_crit[i] = val;
  154. }
  155. for (i = 0; i < SCH5627_NO_FANS; i++) {
  156. val = sch56xx_read_virtual_reg16(data->addr,
  157. SCH5627_REG_FAN_MIN[i]);
  158. if (val < 0)
  159. return val;
  160. data->fan_min[i] = val;
  161. }
  162. return 0;
  163. }
  164. static int reg_to_temp(u16 reg)
  165. {
  166. return (reg * 625) / 10 - 64000;
  167. }
  168. static int reg_to_temp_limit(u8 reg)
  169. {
  170. return (reg - 64) * 1000;
  171. }
  172. static int reg_to_rpm(u16 reg)
  173. {
  174. if (reg == 0)
  175. return -EIO;
  176. if (reg == 0xffff)
  177. return 0;
  178. return 5400540 / reg;
  179. }
  180. static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
  181. char *buf)
  182. {
  183. return snprintf(buf, PAGE_SIZE, "%s\n", DEVNAME);
  184. }
  185. static ssize_t show_temp(struct device *dev, struct device_attribute
  186. *devattr, char *buf)
  187. {
  188. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  189. struct sch5627_data *data = sch5627_update_device(dev);
  190. int val;
  191. if (IS_ERR(data))
  192. return PTR_ERR(data);
  193. val = reg_to_temp(data->temp[attr->index]);
  194. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  195. }
  196. static ssize_t show_temp_fault(struct device *dev, struct device_attribute
  197. *devattr, char *buf)
  198. {
  199. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  200. struct sch5627_data *data = sch5627_update_device(dev);
  201. if (IS_ERR(data))
  202. return PTR_ERR(data);
  203. return snprintf(buf, PAGE_SIZE, "%d\n", data->temp[attr->index] == 0);
  204. }
  205. static ssize_t show_temp_max(struct device *dev, struct device_attribute
  206. *devattr, char *buf)
  207. {
  208. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  209. struct sch5627_data *data = dev_get_drvdata(dev);
  210. int val;
  211. val = reg_to_temp_limit(data->temp_max[attr->index]);
  212. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  213. }
  214. static ssize_t show_temp_crit(struct device *dev, struct device_attribute
  215. *devattr, char *buf)
  216. {
  217. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  218. struct sch5627_data *data = dev_get_drvdata(dev);
  219. int val;
  220. val = reg_to_temp_limit(data->temp_crit[attr->index]);
  221. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  222. }
  223. static ssize_t show_fan(struct device *dev, struct device_attribute
  224. *devattr, char *buf)
  225. {
  226. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  227. struct sch5627_data *data = sch5627_update_device(dev);
  228. int val;
  229. if (IS_ERR(data))
  230. return PTR_ERR(data);
  231. val = reg_to_rpm(data->fan[attr->index]);
  232. if (val < 0)
  233. return val;
  234. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  235. }
  236. static ssize_t show_fan_fault(struct device *dev, struct device_attribute
  237. *devattr, char *buf)
  238. {
  239. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  240. struct sch5627_data *data = sch5627_update_device(dev);
  241. if (IS_ERR(data))
  242. return PTR_ERR(data);
  243. return snprintf(buf, PAGE_SIZE, "%d\n",
  244. data->fan[attr->index] == 0xffff);
  245. }
  246. static ssize_t show_fan_min(struct device *dev, struct device_attribute
  247. *devattr, char *buf)
  248. {
  249. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  250. struct sch5627_data *data = dev_get_drvdata(dev);
  251. int val = reg_to_rpm(data->fan_min[attr->index]);
  252. if (val < 0)
  253. return val;
  254. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  255. }
  256. static ssize_t show_in(struct device *dev, struct device_attribute
  257. *devattr, char *buf)
  258. {
  259. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  260. struct sch5627_data *data = sch5627_update_device(dev);
  261. int val;
  262. if (IS_ERR(data))
  263. return PTR_ERR(data);
  264. val = DIV_ROUND_CLOSEST(
  265. data->in[attr->index] * SCH5627_REG_IN_FACTOR[attr->index],
  266. 10000);
  267. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  268. }
  269. static ssize_t show_in_label(struct device *dev, struct device_attribute
  270. *devattr, char *buf)
  271. {
  272. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  273. return snprintf(buf, PAGE_SIZE, "%s\n",
  274. SCH5627_IN_LABELS[attr->index]);
  275. }
  276. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  277. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  278. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  279. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  280. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  281. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
  282. static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
  283. static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
  284. static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
  285. static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
  286. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
  287. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
  288. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
  289. static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4);
  290. static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5);
  291. static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_temp_fault, NULL, 6);
  292. static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_temp_fault, NULL, 7);
  293. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL, 0);
  294. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, NULL, 1);
  295. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, NULL, 2);
  296. static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, NULL, 3);
  297. static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, NULL, 4);
  298. static SENSOR_DEVICE_ATTR(temp6_max, S_IRUGO, show_temp_max, NULL, 5);
  299. static SENSOR_DEVICE_ATTR(temp7_max, S_IRUGO, show_temp_max, NULL, 6);
  300. static SENSOR_DEVICE_ATTR(temp8_max, S_IRUGO, show_temp_max, NULL, 7);
  301. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0);
  302. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL, 1);
  303. static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit, NULL, 2);
  304. static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL, 3);
  305. static SENSOR_DEVICE_ATTR(temp5_crit, S_IRUGO, show_temp_crit, NULL, 4);
  306. static SENSOR_DEVICE_ATTR(temp6_crit, S_IRUGO, show_temp_crit, NULL, 5);
  307. static SENSOR_DEVICE_ATTR(temp7_crit, S_IRUGO, show_temp_crit, NULL, 6);
  308. static SENSOR_DEVICE_ATTR(temp8_crit, S_IRUGO, show_temp_crit, NULL, 7);
  309. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  310. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  311. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  312. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
  313. static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL, 0);
  314. static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault, NULL, 1);
  315. static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, show_fan_fault, NULL, 2);
  316. static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, show_fan_fault, NULL, 3);
  317. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan_min, NULL, 0);
  318. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan_min, NULL, 1);
  319. static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO, show_fan_min, NULL, 2);
  320. static SENSOR_DEVICE_ATTR(fan4_min, S_IRUGO, show_fan_min, NULL, 3);
  321. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
  322. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
  323. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
  324. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
  325. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
  326. static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_in_label, NULL, 0);
  327. static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_in_label, NULL, 1);
  328. static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_in_label, NULL, 2);
  329. static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_in_label, NULL, 3);
  330. static struct attribute *sch5627_attributes[] = {
  331. &dev_attr_name.attr,
  332. &sensor_dev_attr_temp1_input.dev_attr.attr,
  333. &sensor_dev_attr_temp2_input.dev_attr.attr,
  334. &sensor_dev_attr_temp3_input.dev_attr.attr,
  335. &sensor_dev_attr_temp4_input.dev_attr.attr,
  336. &sensor_dev_attr_temp5_input.dev_attr.attr,
  337. &sensor_dev_attr_temp6_input.dev_attr.attr,
  338. &sensor_dev_attr_temp7_input.dev_attr.attr,
  339. &sensor_dev_attr_temp8_input.dev_attr.attr,
  340. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  341. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  342. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  343. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  344. &sensor_dev_attr_temp5_fault.dev_attr.attr,
  345. &sensor_dev_attr_temp6_fault.dev_attr.attr,
  346. &sensor_dev_attr_temp7_fault.dev_attr.attr,
  347. &sensor_dev_attr_temp8_fault.dev_attr.attr,
  348. &sensor_dev_attr_temp1_max.dev_attr.attr,
  349. &sensor_dev_attr_temp2_max.dev_attr.attr,
  350. &sensor_dev_attr_temp3_max.dev_attr.attr,
  351. &sensor_dev_attr_temp4_max.dev_attr.attr,
  352. &sensor_dev_attr_temp5_max.dev_attr.attr,
  353. &sensor_dev_attr_temp6_max.dev_attr.attr,
  354. &sensor_dev_attr_temp7_max.dev_attr.attr,
  355. &sensor_dev_attr_temp8_max.dev_attr.attr,
  356. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  357. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  358. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  359. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  360. &sensor_dev_attr_temp5_crit.dev_attr.attr,
  361. &sensor_dev_attr_temp6_crit.dev_attr.attr,
  362. &sensor_dev_attr_temp7_crit.dev_attr.attr,
  363. &sensor_dev_attr_temp8_crit.dev_attr.attr,
  364. &sensor_dev_attr_fan1_input.dev_attr.attr,
  365. &sensor_dev_attr_fan2_input.dev_attr.attr,
  366. &sensor_dev_attr_fan3_input.dev_attr.attr,
  367. &sensor_dev_attr_fan4_input.dev_attr.attr,
  368. &sensor_dev_attr_fan1_fault.dev_attr.attr,
  369. &sensor_dev_attr_fan2_fault.dev_attr.attr,
  370. &sensor_dev_attr_fan3_fault.dev_attr.attr,
  371. &sensor_dev_attr_fan4_fault.dev_attr.attr,
  372. &sensor_dev_attr_fan1_min.dev_attr.attr,
  373. &sensor_dev_attr_fan2_min.dev_attr.attr,
  374. &sensor_dev_attr_fan3_min.dev_attr.attr,
  375. &sensor_dev_attr_fan4_min.dev_attr.attr,
  376. &sensor_dev_attr_in0_input.dev_attr.attr,
  377. &sensor_dev_attr_in1_input.dev_attr.attr,
  378. &sensor_dev_attr_in2_input.dev_attr.attr,
  379. &sensor_dev_attr_in3_input.dev_attr.attr,
  380. &sensor_dev_attr_in4_input.dev_attr.attr,
  381. &sensor_dev_attr_in0_label.dev_attr.attr,
  382. &sensor_dev_attr_in1_label.dev_attr.attr,
  383. &sensor_dev_attr_in2_label.dev_attr.attr,
  384. &sensor_dev_attr_in3_label.dev_attr.attr,
  385. /* No in4_label as in4 is a generic input pin */
  386. NULL
  387. };
  388. static const struct attribute_group sch5627_group = {
  389. .attrs = sch5627_attributes,
  390. };
  391. static int sch5627_remove(struct platform_device *pdev)
  392. {
  393. struct sch5627_data *data = platform_get_drvdata(pdev);
  394. if (data->hwmon_dev)
  395. hwmon_device_unregister(data->hwmon_dev);
  396. sysfs_remove_group(&pdev->dev.kobj, &sch5627_group);
  397. platform_set_drvdata(pdev, NULL);
  398. kfree(data);
  399. return 0;
  400. }
  401. static int __devinit sch5627_probe(struct platform_device *pdev)
  402. {
  403. struct sch5627_data *data;
  404. int err, build_code, build_id, hwmon_rev, val;
  405. data = kzalloc(sizeof(struct sch5627_data), GFP_KERNEL);
  406. if (!data)
  407. return -ENOMEM;
  408. data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
  409. mutex_init(&data->update_lock);
  410. platform_set_drvdata(pdev, data);
  411. val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_HWMON_ID);
  412. if (val < 0) {
  413. err = val;
  414. goto error;
  415. }
  416. if (val != SCH5627_HWMON_ID) {
  417. pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon",
  418. val, SCH5627_HWMON_ID);
  419. err = -ENODEV;
  420. goto error;
  421. }
  422. val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_COMPANY_ID);
  423. if (val < 0) {
  424. err = val;
  425. goto error;
  426. }
  427. if (val != SCH5627_COMPANY_ID) {
  428. pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company",
  429. val, SCH5627_COMPANY_ID);
  430. err = -ENODEV;
  431. goto error;
  432. }
  433. val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_PRIMARY_ID);
  434. if (val < 0) {
  435. err = val;
  436. goto error;
  437. }
  438. if (val != SCH5627_PRIMARY_ID) {
  439. pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary",
  440. val, SCH5627_PRIMARY_ID);
  441. err = -ENODEV;
  442. goto error;
  443. }
  444. build_code = sch56xx_read_virtual_reg(data->addr,
  445. SCH5627_REG_BUILD_CODE);
  446. if (build_code < 0) {
  447. err = build_code;
  448. goto error;
  449. }
  450. build_id = sch56xx_read_virtual_reg16(data->addr,
  451. SCH5627_REG_BUILD_ID);
  452. if (build_id < 0) {
  453. err = build_id;
  454. goto error;
  455. }
  456. hwmon_rev = sch56xx_read_virtual_reg(data->addr,
  457. SCH5627_REG_HWMON_REV);
  458. if (hwmon_rev < 0) {
  459. err = hwmon_rev;
  460. goto error;
  461. }
  462. val = sch56xx_read_virtual_reg(data->addr, SCH5627_REG_CTRL);
  463. if (val < 0) {
  464. err = val;
  465. goto error;
  466. }
  467. data->control = val;
  468. if (!(data->control & 0x01)) {
  469. pr_err("hardware monitoring not enabled\n");
  470. err = -ENODEV;
  471. goto error;
  472. }
  473. /* Trigger a Vbat voltage measurement, so that we get a valid reading
  474. the first time we read Vbat */
  475. sch56xx_write_virtual_reg(data->addr, SCH5627_REG_CTRL,
  476. data->control | 0x10);
  477. data->last_battery = jiffies;
  478. /*
  479. * Read limits, we do this only once as reading a register on
  480. * the sch5627 is quite expensive (and they don't change).
  481. */
  482. err = sch5627_read_limits(data);
  483. if (err)
  484. goto error;
  485. pr_info("found %s chip at %#hx\n", DEVNAME, data->addr);
  486. pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n",
  487. build_code, build_id, hwmon_rev);
  488. /* Register sysfs interface files */
  489. err = sysfs_create_group(&pdev->dev.kobj, &sch5627_group);
  490. if (err)
  491. goto error;
  492. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  493. if (IS_ERR(data->hwmon_dev)) {
  494. err = PTR_ERR(data->hwmon_dev);
  495. data->hwmon_dev = NULL;
  496. goto error;
  497. }
  498. return 0;
  499. error:
  500. sch5627_remove(pdev);
  501. return err;
  502. }
  503. static struct platform_driver sch5627_driver = {
  504. .driver = {
  505. .owner = THIS_MODULE,
  506. .name = DRVNAME,
  507. },
  508. .probe = sch5627_probe,
  509. .remove = sch5627_remove,
  510. };
  511. static int __init sch5627_init(void)
  512. {
  513. return platform_driver_register(&sch5627_driver);
  514. }
  515. static void __exit sch5627_exit(void)
  516. {
  517. platform_driver_unregister(&sch5627_driver);
  518. }
  519. MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver");
  520. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  521. MODULE_LICENSE("GPL");
  522. module_init(sch5627_init);
  523. module_exit(sch5627_exit);