sch5627.c 19 KB

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