smsc47m192.c 21 KB

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