smsc47m192.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * smsc47m192.c - Support for hardware monitoring block of
  3. * SMSC LPC47M192 and compatible Super I/O chips
  4. *
  5. * Copyright (C) 2006 Hartmut Rick <linux@rick.claranet.de>
  6. *
  7. * Derived from lm78.c and other chip drivers.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/i2c.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/hwmon-vid.h>
  31. #include <linux/err.h>
  32. #include <linux/sysfs.h>
  33. #include <linux/mutex.h>
  34. /* Addresses to scan */
  35. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  36. /* SMSC47M192 registers */
  37. #define SMSC47M192_REG_IN(nr) ((nr) < 6 ? (0x20 + (nr)) : \
  38. (0x50 + (nr) - 6))
  39. #define SMSC47M192_REG_IN_MAX(nr) ((nr) < 6 ? (0x2b + (nr) * 2) : \
  40. (0x54 + (((nr) - 6) * 2)))
  41. #define SMSC47M192_REG_IN_MIN(nr) ((nr) < 6 ? (0x2c + (nr) * 2) : \
  42. (0x55 + (((nr) - 6) * 2)))
  43. static u8 SMSC47M192_REG_TEMP[3] = { 0x27, 0x26, 0x52 };
  44. static u8 SMSC47M192_REG_TEMP_MAX[3] = { 0x39, 0x37, 0x58 };
  45. static u8 SMSC47M192_REG_TEMP_MIN[3] = { 0x3A, 0x38, 0x59 };
  46. #define SMSC47M192_REG_TEMP_OFFSET(nr) ((nr) == 2 ? 0x1e : 0x1f)
  47. #define SMSC47M192_REG_ALARM1 0x41
  48. #define SMSC47M192_REG_ALARM2 0x42
  49. #define SMSC47M192_REG_VID 0x47
  50. #define SMSC47M192_REG_VID4 0x49
  51. #define SMSC47M192_REG_CONFIG 0x40
  52. #define SMSC47M192_REG_SFR 0x4f
  53. #define SMSC47M192_REG_COMPANY_ID 0x3e
  54. #define SMSC47M192_REG_VERSION 0x3f
  55. /* generalised scaling with integer rounding */
  56. static inline int SCALE(long val, int mul, int div)
  57. {
  58. if (val < 0)
  59. return (val * mul - div / 2) / div;
  60. else
  61. return (val * mul + div / 2) / div;
  62. }
  63. /* Conversions */
  64. /* smsc47m192 internally scales voltage measurements */
  65. static const u16 nom_mv[] = { 2500, 2250, 3300, 5000, 12000, 3300, 1500, 1800 };
  66. static inline unsigned int IN_FROM_REG(u8 reg, int n)
  67. {
  68. return SCALE(reg, nom_mv[n], 192);
  69. }
  70. static inline u8 IN_TO_REG(unsigned long val, int n)
  71. {
  72. return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
  73. }
  74. /*
  75. * TEMP: 0.001 degC units (-128C to +127C)
  76. * REG: 1C/bit, two's complement
  77. */
  78. static inline s8 TEMP_TO_REG(int val)
  79. {
  80. return SCALE(clamp_val(val, -128000, 127000), 1, 1000);
  81. }
  82. static inline int TEMP_FROM_REG(s8 val)
  83. {
  84. return val * 1000;
  85. }
  86. struct smsc47m192_data {
  87. struct i2c_client *client;
  88. const struct attribute_group *groups[3];
  89. struct mutex update_lock;
  90. char valid; /* !=0 if following fields are valid */
  91. unsigned long last_updated; /* In jiffies */
  92. u8 in[8]; /* Register value */
  93. u8 in_max[8]; /* Register value */
  94. u8 in_min[8]; /* Register value */
  95. s8 temp[3]; /* Register value */
  96. s8 temp_max[3]; /* Register value */
  97. s8 temp_min[3]; /* Register value */
  98. s8 temp_offset[3]; /* Register value */
  99. u16 alarms; /* Register encoding, combined */
  100. u8 vid; /* Register encoding, combined */
  101. u8 vrm;
  102. };
  103. static struct smsc47m192_data *smsc47m192_update_device(struct device *dev)
  104. {
  105. struct smsc47m192_data *data = dev_get_drvdata(dev);
  106. struct i2c_client *client = data->client;
  107. int i, config;
  108. mutex_lock(&data->update_lock);
  109. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  110. || !data->valid) {
  111. u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
  112. dev_dbg(&client->dev, "Starting smsc47m192 update\n");
  113. for (i = 0; i <= 7; i++) {
  114. data->in[i] = i2c_smbus_read_byte_data(client,
  115. SMSC47M192_REG_IN(i));
  116. data->in_min[i] = i2c_smbus_read_byte_data(client,
  117. SMSC47M192_REG_IN_MIN(i));
  118. data->in_max[i] = i2c_smbus_read_byte_data(client,
  119. SMSC47M192_REG_IN_MAX(i));
  120. }
  121. for (i = 0; i < 3; i++) {
  122. data->temp[i] = i2c_smbus_read_byte_data(client,
  123. SMSC47M192_REG_TEMP[i]);
  124. data->temp_max[i] = i2c_smbus_read_byte_data(client,
  125. SMSC47M192_REG_TEMP_MAX[i]);
  126. data->temp_min[i] = i2c_smbus_read_byte_data(client,
  127. SMSC47M192_REG_TEMP_MIN[i]);
  128. }
  129. for (i = 1; i < 3; i++)
  130. data->temp_offset[i] = i2c_smbus_read_byte_data(client,
  131. SMSC47M192_REG_TEMP_OFFSET(i));
  132. /*
  133. * first offset is temp_offset[0] if SFR bit 4 is set,
  134. * temp_offset[1] otherwise
  135. */
  136. if (sfr & 0x10) {
  137. data->temp_offset[0] = data->temp_offset[1];
  138. data->temp_offset[1] = 0;
  139. } else
  140. data->temp_offset[0] = 0;
  141. data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID)
  142. & 0x0f;
  143. config = i2c_smbus_read_byte_data(client,
  144. SMSC47M192_REG_CONFIG);
  145. if (config & 0x20)
  146. data->vid |= (i2c_smbus_read_byte_data(client,
  147. SMSC47M192_REG_VID4) & 0x01) << 4;
  148. data->alarms = i2c_smbus_read_byte_data(client,
  149. SMSC47M192_REG_ALARM1) |
  150. (i2c_smbus_read_byte_data(client,
  151. SMSC47M192_REG_ALARM2) << 8);
  152. data->last_updated = jiffies;
  153. data->valid = 1;
  154. }
  155. mutex_unlock(&data->update_lock);
  156. return data;
  157. }
  158. /* Voltages */
  159. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  160. char *buf)
  161. {
  162. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  163. int nr = sensor_attr->index;
  164. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  165. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
  166. }
  167. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  168. char *buf)
  169. {
  170. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  171. int nr = sensor_attr->index;
  172. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  173. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
  174. }
  175. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  176. char *buf)
  177. {
  178. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  179. int nr = sensor_attr->index;
  180. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  181. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
  182. }
  183. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  184. const char *buf, size_t count)
  185. {
  186. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  187. int nr = sensor_attr->index;
  188. struct smsc47m192_data *data = dev_get_drvdata(dev);
  189. struct i2c_client *client = data->client;
  190. unsigned long val;
  191. int err;
  192. err = kstrtoul(buf, 10, &val);
  193. if (err)
  194. return err;
  195. mutex_lock(&data->update_lock);
  196. data->in_min[nr] = IN_TO_REG(val, nr);
  197. i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MIN(nr),
  198. data->in_min[nr]);
  199. mutex_unlock(&data->update_lock);
  200. return count;
  201. }
  202. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  203. const char *buf, size_t count)
  204. {
  205. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  206. int nr = sensor_attr->index;
  207. struct smsc47m192_data *data = dev_get_drvdata(dev);
  208. struct i2c_client *client = data->client;
  209. unsigned long val;
  210. int err;
  211. err = kstrtoul(buf, 10, &val);
  212. if (err)
  213. return err;
  214. mutex_lock(&data->update_lock);
  215. data->in_max[nr] = IN_TO_REG(val, nr);
  216. i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MAX(nr),
  217. data->in_max[nr]);
  218. mutex_unlock(&data->update_lock);
  219. return count;
  220. }
  221. #define show_in_offset(offset) \
  222. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  223. show_in, NULL, offset); \
  224. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  225. show_in_min, set_in_min, offset); \
  226. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  227. show_in_max, set_in_max, offset);
  228. show_in_offset(0)
  229. show_in_offset(1)
  230. show_in_offset(2)
  231. show_in_offset(3)
  232. show_in_offset(4)
  233. show_in_offset(5)
  234. show_in_offset(6)
  235. show_in_offset(7)
  236. /* Temperatures */
  237. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  238. char *buf)
  239. {
  240. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  241. int nr = sensor_attr->index;
  242. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  243. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  244. }
  245. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  246. char *buf)
  247. {
  248. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  249. int nr = sensor_attr->index;
  250. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  251. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
  252. }
  253. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  254. char *buf)
  255. {
  256. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  257. int nr = sensor_attr->index;
  258. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  259. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
  260. }
  261. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  262. const char *buf, size_t count)
  263. {
  264. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  265. int nr = sensor_attr->index;
  266. struct smsc47m192_data *data = dev_get_drvdata(dev);
  267. struct i2c_client *client = data->client;
  268. long val;
  269. int err;
  270. err = kstrtol(buf, 10, &val);
  271. if (err)
  272. return err;
  273. mutex_lock(&data->update_lock);
  274. data->temp_min[nr] = TEMP_TO_REG(val);
  275. i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MIN[nr],
  276. data->temp_min[nr]);
  277. mutex_unlock(&data->update_lock);
  278. return count;
  279. }
  280. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  281. const char *buf, size_t count)
  282. {
  283. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  284. int nr = sensor_attr->index;
  285. struct smsc47m192_data *data = dev_get_drvdata(dev);
  286. struct i2c_client *client = data->client;
  287. long val;
  288. int err;
  289. err = kstrtol(buf, 10, &val);
  290. if (err)
  291. return err;
  292. mutex_lock(&data->update_lock);
  293. data->temp_max[nr] = TEMP_TO_REG(val);
  294. i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MAX[nr],
  295. data->temp_max[nr]);
  296. mutex_unlock(&data->update_lock);
  297. return count;
  298. }
  299. static ssize_t show_temp_offset(struct device *dev, struct device_attribute
  300. *attr, char *buf)
  301. {
  302. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  303. int nr = sensor_attr->index;
  304. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  305. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
  306. }
  307. static ssize_t set_temp_offset(struct device *dev, struct device_attribute
  308. *attr, const char *buf, size_t count)
  309. {
  310. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  311. int nr = sensor_attr->index;
  312. struct smsc47m192_data *data = dev_get_drvdata(dev);
  313. struct i2c_client *client = data->client;
  314. u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
  315. long val;
  316. int err;
  317. err = kstrtol(buf, 10, &val);
  318. if (err)
  319. return err;
  320. mutex_lock(&data->update_lock);
  321. data->temp_offset[nr] = TEMP_TO_REG(val);
  322. if (nr > 1)
  323. i2c_smbus_write_byte_data(client,
  324. SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
  325. else if (data->temp_offset[nr] != 0) {
  326. /*
  327. * offset[0] and offset[1] share the same register,
  328. * SFR bit 4 activates offset[0]
  329. */
  330. i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
  331. (sfr & 0xef) | (nr == 0 ? 0x10 : 0));
  332. data->temp_offset[1-nr] = 0;
  333. i2c_smbus_write_byte_data(client,
  334. SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
  335. } else if ((sfr & 0x10) == (nr == 0 ? 0x10 : 0))
  336. i2c_smbus_write_byte_data(client,
  337. SMSC47M192_REG_TEMP_OFFSET(nr), 0);
  338. mutex_unlock(&data->update_lock);
  339. return count;
  340. }
  341. #define show_temp_index(index) \
  342. static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, \
  343. show_temp, NULL, index-1); \
  344. static SENSOR_DEVICE_ATTR(temp##index##_min, S_IRUGO | S_IWUSR, \
  345. show_temp_min, set_temp_min, index-1); \
  346. static SENSOR_DEVICE_ATTR(temp##index##_max, S_IRUGO | S_IWUSR, \
  347. show_temp_max, set_temp_max, index-1); \
  348. static SENSOR_DEVICE_ATTR(temp##index##_offset, S_IRUGO | S_IWUSR, \
  349. show_temp_offset, set_temp_offset, index-1);
  350. show_temp_index(1)
  351. show_temp_index(2)
  352. show_temp_index(3)
  353. /* VID */
  354. static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
  355. char *buf)
  356. {
  357. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  358. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  359. }
  360. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  361. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
  362. char *buf)
  363. {
  364. struct smsc47m192_data *data = dev_get_drvdata(dev);
  365. return sprintf(buf, "%d\n", data->vrm);
  366. }
  367. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
  368. const char *buf, size_t count)
  369. {
  370. struct smsc47m192_data *data = dev_get_drvdata(dev);
  371. unsigned long val;
  372. int err;
  373. err = kstrtoul(buf, 10, &val);
  374. if (err)
  375. return err;
  376. if (val > 255)
  377. return -EINVAL;
  378. data->vrm = val;
  379. return count;
  380. }
  381. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  382. /* Alarms */
  383. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  384. char *buf)
  385. {
  386. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  387. int nr = sensor_attr->index;
  388. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  389. return sprintf(buf, "%u\n", (data->alarms & nr) ? 1 : 0);
  390. }
  391. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0x0010);
  392. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0x0020);
  393. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0x0040);
  394. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 0x4000);
  395. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 0x8000);
  396. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0x0001);
  397. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0x0002);
  398. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 0x0004);
  399. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 0x0008);
  400. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 0x0100);
  401. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 0x0200);
  402. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 0x0400);
  403. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 0x0800);
  404. static struct attribute *smsc47m192_attributes[] = {
  405. &sensor_dev_attr_in0_input.dev_attr.attr,
  406. &sensor_dev_attr_in0_min.dev_attr.attr,
  407. &sensor_dev_attr_in0_max.dev_attr.attr,
  408. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  409. &sensor_dev_attr_in1_input.dev_attr.attr,
  410. &sensor_dev_attr_in1_min.dev_attr.attr,
  411. &sensor_dev_attr_in1_max.dev_attr.attr,
  412. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  413. &sensor_dev_attr_in2_input.dev_attr.attr,
  414. &sensor_dev_attr_in2_min.dev_attr.attr,
  415. &sensor_dev_attr_in2_max.dev_attr.attr,
  416. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  417. &sensor_dev_attr_in3_input.dev_attr.attr,
  418. &sensor_dev_attr_in3_min.dev_attr.attr,
  419. &sensor_dev_attr_in3_max.dev_attr.attr,
  420. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  421. &sensor_dev_attr_in5_input.dev_attr.attr,
  422. &sensor_dev_attr_in5_min.dev_attr.attr,
  423. &sensor_dev_attr_in5_max.dev_attr.attr,
  424. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  425. &sensor_dev_attr_in6_input.dev_attr.attr,
  426. &sensor_dev_attr_in6_min.dev_attr.attr,
  427. &sensor_dev_attr_in6_max.dev_attr.attr,
  428. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  429. &sensor_dev_attr_in7_input.dev_attr.attr,
  430. &sensor_dev_attr_in7_min.dev_attr.attr,
  431. &sensor_dev_attr_in7_max.dev_attr.attr,
  432. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  433. &sensor_dev_attr_temp1_input.dev_attr.attr,
  434. &sensor_dev_attr_temp1_max.dev_attr.attr,
  435. &sensor_dev_attr_temp1_min.dev_attr.attr,
  436. &sensor_dev_attr_temp1_offset.dev_attr.attr,
  437. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  438. &sensor_dev_attr_temp2_input.dev_attr.attr,
  439. &sensor_dev_attr_temp2_max.dev_attr.attr,
  440. &sensor_dev_attr_temp2_min.dev_attr.attr,
  441. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  442. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  443. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  444. &sensor_dev_attr_temp3_input.dev_attr.attr,
  445. &sensor_dev_attr_temp3_max.dev_attr.attr,
  446. &sensor_dev_attr_temp3_min.dev_attr.attr,
  447. &sensor_dev_attr_temp3_offset.dev_attr.attr,
  448. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  449. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  450. &dev_attr_cpu0_vid.attr,
  451. &dev_attr_vrm.attr,
  452. NULL
  453. };
  454. static const struct attribute_group smsc47m192_group = {
  455. .attrs = smsc47m192_attributes,
  456. };
  457. static struct attribute *smsc47m192_attributes_in4[] = {
  458. &sensor_dev_attr_in4_input.dev_attr.attr,
  459. &sensor_dev_attr_in4_min.dev_attr.attr,
  460. &sensor_dev_attr_in4_max.dev_attr.attr,
  461. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  462. NULL
  463. };
  464. static const struct attribute_group smsc47m192_group_in4 = {
  465. .attrs = smsc47m192_attributes_in4,
  466. };
  467. static void smsc47m192_init_client(struct i2c_client *client)
  468. {
  469. int i;
  470. u8 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
  471. u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
  472. /* select cycle mode (pause 1 sec between updates) */
  473. i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
  474. (sfr & 0xfd) | 0x02);
  475. if (!(config & 0x01)) {
  476. /* initialize alarm limits */
  477. for (i = 0; i < 8; i++) {
  478. i2c_smbus_write_byte_data(client,
  479. SMSC47M192_REG_IN_MIN(i), 0);
  480. i2c_smbus_write_byte_data(client,
  481. SMSC47M192_REG_IN_MAX(i), 0xff);
  482. }
  483. for (i = 0; i < 3; i++) {
  484. i2c_smbus_write_byte_data(client,
  485. SMSC47M192_REG_TEMP_MIN[i], 0x80);
  486. i2c_smbus_write_byte_data(client,
  487. SMSC47M192_REG_TEMP_MAX[i], 0x7f);
  488. }
  489. /* start monitoring */
  490. i2c_smbus_write_byte_data(client, SMSC47M192_REG_CONFIG,
  491. (config & 0xf7) | 0x01);
  492. }
  493. }
  494. /* Return 0 if detection is successful, -ENODEV otherwise */
  495. static int smsc47m192_detect(struct i2c_client *client,
  496. struct i2c_board_info *info)
  497. {
  498. struct i2c_adapter *adapter = client->adapter;
  499. int version;
  500. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  501. return -ENODEV;
  502. /* Detection criteria from sensors_detect script */
  503. version = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VERSION);
  504. if (i2c_smbus_read_byte_data(client,
  505. SMSC47M192_REG_COMPANY_ID) == 0x55
  506. && (version & 0xf0) == 0x20
  507. && (i2c_smbus_read_byte_data(client,
  508. SMSC47M192_REG_VID) & 0x70) == 0x00
  509. && (i2c_smbus_read_byte_data(client,
  510. SMSC47M192_REG_VID4) & 0xfe) == 0x80) {
  511. dev_info(&adapter->dev,
  512. "found SMSC47M192 or compatible, "
  513. "version 2, stepping A%d\n", version & 0x0f);
  514. } else {
  515. dev_dbg(&adapter->dev,
  516. "SMSC47M192 detection failed at 0x%02x\n",
  517. client->addr);
  518. return -ENODEV;
  519. }
  520. strlcpy(info->type, "smsc47m192", I2C_NAME_SIZE);
  521. return 0;
  522. }
  523. static int smsc47m192_probe(struct i2c_client *client,
  524. const struct i2c_device_id *id)
  525. {
  526. struct device *dev = &client->dev;
  527. struct device *hwmon_dev;
  528. struct smsc47m192_data *data;
  529. int config;
  530. data = devm_kzalloc(dev, sizeof(struct smsc47m192_data), GFP_KERNEL);
  531. if (!data)
  532. return -ENOMEM;
  533. data->client = client;
  534. data->vrm = vid_which_vrm();
  535. mutex_init(&data->update_lock);
  536. /* Initialize the SMSC47M192 chip */
  537. smsc47m192_init_client(client);
  538. /* sysfs hooks */
  539. data->groups[0] = &smsc47m192_group;
  540. /* Pin 110 is either in4 (+12V) or VID4 */
  541. config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
  542. if (!(config & 0x20))
  543. data->groups[1] = &smsc47m192_group_in4;
  544. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  545. data, data->groups);
  546. return PTR_ERR_OR_ZERO(hwmon_dev);
  547. }
  548. static const struct i2c_device_id smsc47m192_id[] = {
  549. { "smsc47m192", 0 },
  550. { }
  551. };
  552. MODULE_DEVICE_TABLE(i2c, smsc47m192_id);
  553. static struct i2c_driver smsc47m192_driver = {
  554. .class = I2C_CLASS_HWMON,
  555. .driver = {
  556. .name = "smsc47m192",
  557. },
  558. .probe = smsc47m192_probe,
  559. .id_table = smsc47m192_id,
  560. .detect = smsc47m192_detect,
  561. .address_list = normal_i2c,
  562. };
  563. module_i2c_driver(smsc47m192_driver);
  564. MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
  565. MODULE_DESCRIPTION("SMSC47M192 driver");
  566. MODULE_LICENSE("GPL");