smsc47m192.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. smsc47m192.c - Support for hardware monitoring block of
  3. SMSC LPC47M192 and LPC47M997 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. /* Addresses to scan */
  28. static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  29. /* Insmod parameters */
  30. I2C_CLIENT_INSMOD_1(smsc47m192);
  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 i2c_client client;
  81. struct class_device *class_dev;
  82. struct semaphore update_lock;
  83. char valid; /* !=0 if following fields are valid */
  84. unsigned long last_updated; /* In jiffies */
  85. u8 in[8]; /* Register value */
  86. u8 in_max[8]; /* Register value */
  87. u8 in_min[8]; /* Register value */
  88. s8 temp[3]; /* Register value */
  89. s8 temp_max[3]; /* Register value */
  90. s8 temp_min[3]; /* Register value */
  91. s8 temp_offset[3]; /* Register value */
  92. u16 alarms; /* Register encoding, combined */
  93. u8 vid; /* Register encoding, combined */
  94. u8 vrm;
  95. };
  96. static int smsc47m192_attach_adapter(struct i2c_adapter *adapter);
  97. static int smsc47m192_detect(struct i2c_adapter *adapter, int address,
  98. int kind);
  99. static int smsc47m192_detach_client(struct i2c_client *client);
  100. static struct smsc47m192_data *smsc47m192_update_device(struct device *dev);
  101. static struct i2c_driver smsc47m192_driver = {
  102. .driver = {
  103. .name = "smsc47m192",
  104. },
  105. .attach_adapter = smsc47m192_attach_adapter,
  106. .detach_client = smsc47m192_detach_client,
  107. };
  108. /* Voltages */
  109. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  110. char *buf)
  111. {
  112. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  113. int nr = sensor_attr->index;
  114. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  115. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
  116. }
  117. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  118. char *buf)
  119. {
  120. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  121. int nr = sensor_attr->index;
  122. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  123. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
  124. }
  125. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  126. char *buf)
  127. {
  128. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  129. int nr = sensor_attr->index;
  130. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  131. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
  132. }
  133. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  134. const char *buf, size_t count)
  135. {
  136. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  137. int nr = sensor_attr->index;
  138. struct i2c_client *client = to_i2c_client(dev);
  139. struct smsc47m192_data *data = i2c_get_clientdata(client);
  140. unsigned long val = simple_strtoul(buf, NULL, 10);
  141. down(&data->update_lock);
  142. data->in_min[nr] = IN_TO_REG(val, nr);
  143. i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MIN(nr),
  144. data->in_min[nr]);
  145. up(&data->update_lock);
  146. return count;
  147. }
  148. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  149. const char *buf, size_t count)
  150. {
  151. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  152. int nr = sensor_attr->index;
  153. struct i2c_client *client = to_i2c_client(dev);
  154. struct smsc47m192_data *data = i2c_get_clientdata(client);
  155. unsigned long val = simple_strtoul(buf, NULL, 10);
  156. down(&data->update_lock);
  157. data->in_max[nr] = IN_TO_REG(val, nr);
  158. i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MAX(nr),
  159. data->in_max[nr]);
  160. up(&data->update_lock);
  161. return count;
  162. }
  163. #define show_in_offset(offset) \
  164. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  165. show_in, NULL, offset); \
  166. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  167. show_in_min, set_in_min, offset); \
  168. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  169. show_in_max, set_in_max, offset);
  170. show_in_offset(0)
  171. show_in_offset(1)
  172. show_in_offset(2)
  173. show_in_offset(3)
  174. show_in_offset(4)
  175. show_in_offset(5)
  176. show_in_offset(6)
  177. show_in_offset(7)
  178. /* Temperatures */
  179. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  180. char *buf)
  181. {
  182. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  183. int nr = sensor_attr->index;
  184. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  185. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  186. }
  187. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  188. char *buf)
  189. {
  190. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  191. int nr = sensor_attr->index;
  192. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  193. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
  194. }
  195. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  196. char *buf)
  197. {
  198. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  199. int nr = sensor_attr->index;
  200. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  201. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
  202. }
  203. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  204. const char *buf, size_t count)
  205. {
  206. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  207. int nr = sensor_attr->index;
  208. struct i2c_client *client = to_i2c_client(dev);
  209. struct smsc47m192_data *data = i2c_get_clientdata(client);
  210. long val = simple_strtol(buf, NULL, 10);
  211. down(&data->update_lock);
  212. data->temp_min[nr] = TEMP_TO_REG(val);
  213. i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MIN[nr],
  214. data->temp_min[nr]);
  215. up(&data->update_lock);
  216. return count;
  217. }
  218. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  219. const char *buf, size_t count)
  220. {
  221. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  222. int nr = sensor_attr->index;
  223. struct i2c_client *client = to_i2c_client(dev);
  224. struct smsc47m192_data *data = i2c_get_clientdata(client);
  225. long val = simple_strtol(buf, NULL, 10);
  226. down(&data->update_lock);
  227. data->temp_max[nr] = TEMP_TO_REG(val);
  228. i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MAX[nr],
  229. data->temp_max[nr]);
  230. up(&data->update_lock);
  231. return count;
  232. }
  233. static ssize_t show_temp_offset(struct device *dev, struct device_attribute
  234. *attr, char *buf)
  235. {
  236. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  237. int nr = sensor_attr->index;
  238. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  239. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
  240. }
  241. static ssize_t set_temp_offset(struct device *dev, struct device_attribute
  242. *attr, const char *buf, size_t count)
  243. {
  244. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  245. int nr = sensor_attr->index;
  246. struct i2c_client *client = to_i2c_client(dev);
  247. struct smsc47m192_data *data = i2c_get_clientdata(client);
  248. u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
  249. long val = simple_strtol(buf, NULL, 10);
  250. down(&data->update_lock);
  251. data->temp_offset[nr] = TEMP_TO_REG(val);
  252. if (nr>1)
  253. i2c_smbus_write_byte_data(client,
  254. SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
  255. else if (data->temp_offset[nr] != 0) {
  256. /* offset[0] and offset[1] share the same register,
  257. SFR bit 4 activates offset[0] */
  258. i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
  259. (sfr & 0xef) | (nr==0 ? 0x10 : 0));
  260. data->temp_offset[1-nr] = 0;
  261. i2c_smbus_write_byte_data(client,
  262. SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]);
  263. } else if ((sfr & 0x10) == (nr==0 ? 0x10 : 0))
  264. i2c_smbus_write_byte_data(client,
  265. SMSC47M192_REG_TEMP_OFFSET(nr), 0);
  266. up(&data->update_lock);
  267. return count;
  268. }
  269. #define show_temp_index(index) \
  270. static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, \
  271. show_temp, NULL, index-1); \
  272. static SENSOR_DEVICE_ATTR(temp##index##_min, S_IRUGO | S_IWUSR, \
  273. show_temp_min, set_temp_min, index-1); \
  274. static SENSOR_DEVICE_ATTR(temp##index##_max, S_IRUGO | S_IWUSR, \
  275. show_temp_max, set_temp_max, index-1); \
  276. static SENSOR_DEVICE_ATTR(temp##index##_offset, S_IRUGO | S_IWUSR, \
  277. show_temp_offset, set_temp_offset, index-1);
  278. show_temp_index(1)
  279. show_temp_index(2)
  280. show_temp_index(3)
  281. /* VID */
  282. static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
  283. char *buf)
  284. {
  285. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  286. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  287. }
  288. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  289. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
  290. char *buf)
  291. {
  292. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  293. return sprintf(buf, "%d\n", data->vrm);
  294. }
  295. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
  296. const char *buf, size_t count)
  297. {
  298. struct i2c_client *client = to_i2c_client(dev);
  299. struct smsc47m192_data *data = i2c_get_clientdata(client);
  300. data->vrm = simple_strtoul(buf, NULL, 10);
  301. return count;
  302. }
  303. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  304. /* Alarms */
  305. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  306. char *buf)
  307. {
  308. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  309. int nr = sensor_attr->index;
  310. struct smsc47m192_data *data = smsc47m192_update_device(dev);
  311. return sprintf(buf, "%u\n", (data->alarms & nr) ? 1 : 0);
  312. }
  313. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0x0010);
  314. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0x0020);
  315. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0x0040);
  316. static SENSOR_DEVICE_ATTR(temp2_input_fault, S_IRUGO, show_alarm, NULL, 0x4000);
  317. static SENSOR_DEVICE_ATTR(temp3_input_fault, S_IRUGO, show_alarm, NULL, 0x8000);
  318. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0x0001);
  319. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0x0002);
  320. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 0x0004);
  321. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 0x0008);
  322. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 0x0100);
  323. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 0x0200);
  324. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 0x0400);
  325. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 0x0800);
  326. /* This function is called when:
  327. * smsc47m192_driver is inserted (when this module is loaded), for each
  328. available adapter
  329. * when a new adapter is inserted (and smsc47m192_driver is still present) */
  330. static int smsc47m192_attach_adapter(struct i2c_adapter *adapter)
  331. {
  332. if (!(adapter->class & I2C_CLASS_HWMON))
  333. return 0;
  334. return i2c_probe(adapter, &addr_data, smsc47m192_detect);
  335. }
  336. static void smsc47m192_init_client(struct i2c_client *client)
  337. {
  338. int i;
  339. u8 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
  340. u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
  341. /* select cycle mode (pause 1 sec between updates) */
  342. i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR,
  343. (sfr & 0xfd) | 0x02);
  344. if (!(config & 0x01)) {
  345. /* initialize alarm limits */
  346. for (i=0; i<8; i++) {
  347. i2c_smbus_write_byte_data(client,
  348. SMSC47M192_REG_IN_MIN(i), 0);
  349. i2c_smbus_write_byte_data(client,
  350. SMSC47M192_REG_IN_MAX(i), 0xff);
  351. }
  352. for (i=0; i<3; i++) {
  353. i2c_smbus_write_byte_data(client,
  354. SMSC47M192_REG_TEMP_MIN[i], 0x80);
  355. i2c_smbus_write_byte_data(client,
  356. SMSC47M192_REG_TEMP_MAX[i], 0x7f);
  357. }
  358. /* start monitoring */
  359. i2c_smbus_write_byte_data(client, SMSC47M192_REG_CONFIG,
  360. (config & 0xf7) | 0x01);
  361. }
  362. }
  363. /* This function is called by i2c_probe */
  364. static int smsc47m192_detect(struct i2c_adapter *adapter, int address,
  365. int kind)
  366. {
  367. struct i2c_client *client;
  368. struct smsc47m192_data *data;
  369. int err = 0;
  370. int version, config;
  371. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  372. goto exit;
  373. if (!(data = kzalloc(sizeof(struct smsc47m192_data), GFP_KERNEL))) {
  374. err = -ENOMEM;
  375. goto exit;
  376. }
  377. client = &data->client;
  378. i2c_set_clientdata(client, data);
  379. client->addr = address;
  380. client->adapter = adapter;
  381. client->driver = &smsc47m192_driver;
  382. if (kind == 0)
  383. kind = smsc47m192;
  384. /* Detection criteria from sensors_detect script */
  385. if (kind < 0) {
  386. if (i2c_smbus_read_byte_data(client,
  387. SMSC47M192_REG_COMPANY_ID) == 0x55
  388. && ((version = i2c_smbus_read_byte_data(client,
  389. SMSC47M192_REG_VERSION)) & 0xf0) == 0x20
  390. && (i2c_smbus_read_byte_data(client,
  391. SMSC47M192_REG_VID) & 0x70) == 0x00
  392. && (i2c_smbus_read_byte_data(client,
  393. SMSC47M192_REG_VID4) & 0xfe) == 0x80) {
  394. dev_info(&adapter->dev,
  395. "found SMSC47M192 or SMSC47M997, "
  396. "version 2, stepping A%d\n", version & 0x0f);
  397. } else {
  398. dev_dbg(&adapter->dev,
  399. "SMSC47M192 detection failed at 0x%02x\n",
  400. address);
  401. goto exit_free;
  402. }
  403. }
  404. /* Fill in the remaining client fields and put into the global list */
  405. strlcpy(client->name, "smsc47m192", I2C_NAME_SIZE);
  406. data->vrm = vid_which_vrm();
  407. init_MUTEX(&data->update_lock);
  408. /* Tell the I2C layer a new client has arrived */
  409. if ((err = i2c_attach_client(client)))
  410. goto exit_free;
  411. /* Initialize the SMSC47M192 chip */
  412. smsc47m192_init_client(client);
  413. /* Register sysfs hooks */
  414. data->class_dev = hwmon_device_register(&client->dev);
  415. if (IS_ERR(data->class_dev)) {
  416. err = PTR_ERR(data->class_dev);
  417. goto exit_detach;
  418. }
  419. device_create_file(&client->dev, &sensor_dev_attr_in0_input.dev_attr);
  420. device_create_file(&client->dev, &sensor_dev_attr_in0_min.dev_attr);
  421. device_create_file(&client->dev, &sensor_dev_attr_in0_max.dev_attr);
  422. device_create_file(&client->dev, &sensor_dev_attr_in0_alarm.dev_attr);
  423. device_create_file(&client->dev, &sensor_dev_attr_in1_input.dev_attr);
  424. device_create_file(&client->dev, &sensor_dev_attr_in1_min.dev_attr);
  425. device_create_file(&client->dev, &sensor_dev_attr_in1_max.dev_attr);
  426. device_create_file(&client->dev, &sensor_dev_attr_in1_alarm.dev_attr);
  427. device_create_file(&client->dev, &sensor_dev_attr_in2_input.dev_attr);
  428. device_create_file(&client->dev, &sensor_dev_attr_in2_min.dev_attr);
  429. device_create_file(&client->dev, &sensor_dev_attr_in2_max.dev_attr);
  430. device_create_file(&client->dev, &sensor_dev_attr_in2_alarm.dev_attr);
  431. device_create_file(&client->dev, &sensor_dev_attr_in3_input.dev_attr);
  432. device_create_file(&client->dev, &sensor_dev_attr_in3_min.dev_attr);
  433. device_create_file(&client->dev, &sensor_dev_attr_in3_max.dev_attr);
  434. device_create_file(&client->dev, &sensor_dev_attr_in3_alarm.dev_attr);
  435. /* Pin 110 is either in4 (+12V) or VID4 */
  436. config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG);
  437. if (!(config & 0x20)) {
  438. device_create_file(&client->dev,
  439. &sensor_dev_attr_in4_input.dev_attr);
  440. device_create_file(&client->dev,
  441. &sensor_dev_attr_in4_min.dev_attr);
  442. device_create_file(&client->dev,
  443. &sensor_dev_attr_in4_max.dev_attr);
  444. device_create_file(&client->dev,
  445. &sensor_dev_attr_in4_alarm.dev_attr);
  446. }
  447. device_create_file(&client->dev, &sensor_dev_attr_in5_input.dev_attr);
  448. device_create_file(&client->dev, &sensor_dev_attr_in5_min.dev_attr);
  449. device_create_file(&client->dev, &sensor_dev_attr_in5_max.dev_attr);
  450. device_create_file(&client->dev, &sensor_dev_attr_in5_alarm.dev_attr);
  451. device_create_file(&client->dev, &sensor_dev_attr_in6_input.dev_attr);
  452. device_create_file(&client->dev, &sensor_dev_attr_in6_min.dev_attr);
  453. device_create_file(&client->dev, &sensor_dev_attr_in6_max.dev_attr);
  454. device_create_file(&client->dev, &sensor_dev_attr_in6_alarm.dev_attr);
  455. device_create_file(&client->dev, &sensor_dev_attr_in7_input.dev_attr);
  456. device_create_file(&client->dev, &sensor_dev_attr_in7_min.dev_attr);
  457. device_create_file(&client->dev, &sensor_dev_attr_in7_max.dev_attr);
  458. device_create_file(&client->dev, &sensor_dev_attr_in7_alarm.dev_attr);
  459. device_create_file(&client->dev, &sensor_dev_attr_temp1_input.dev_attr);
  460. device_create_file(&client->dev, &sensor_dev_attr_temp1_max.dev_attr);
  461. device_create_file(&client->dev, &sensor_dev_attr_temp1_min.dev_attr);
  462. device_create_file(&client->dev,
  463. &sensor_dev_attr_temp1_offset.dev_attr);
  464. device_create_file(&client->dev, &sensor_dev_attr_temp1_alarm.dev_attr);
  465. device_create_file(&client->dev, &sensor_dev_attr_temp2_input.dev_attr);
  466. device_create_file(&client->dev, &sensor_dev_attr_temp2_max.dev_attr);
  467. device_create_file(&client->dev, &sensor_dev_attr_temp2_min.dev_attr);
  468. device_create_file(&client->dev,
  469. &sensor_dev_attr_temp2_offset.dev_attr);
  470. device_create_file(&client->dev, &sensor_dev_attr_temp2_alarm.dev_attr);
  471. device_create_file(&client->dev,
  472. &sensor_dev_attr_temp2_input_fault.dev_attr);
  473. device_create_file(&client->dev, &sensor_dev_attr_temp3_input.dev_attr);
  474. device_create_file(&client->dev, &sensor_dev_attr_temp3_max.dev_attr);
  475. device_create_file(&client->dev, &sensor_dev_attr_temp3_min.dev_attr);
  476. device_create_file(&client->dev,
  477. &sensor_dev_attr_temp3_offset.dev_attr);
  478. device_create_file(&client->dev, &sensor_dev_attr_temp3_alarm.dev_attr);
  479. device_create_file(&client->dev,
  480. &sensor_dev_attr_temp3_input_fault.dev_attr);
  481. device_create_file(&client->dev, &dev_attr_cpu0_vid);
  482. device_create_file(&client->dev, &dev_attr_vrm);
  483. return 0;
  484. exit_detach:
  485. i2c_detach_client(client);
  486. exit_free:
  487. kfree(data);
  488. exit:
  489. return err;
  490. }
  491. static int smsc47m192_detach_client(struct i2c_client *client)
  492. {
  493. struct smsc47m192_data *data = i2c_get_clientdata(client);
  494. int err;
  495. hwmon_device_unregister(data->class_dev);
  496. if ((err = i2c_detach_client(client)))
  497. return err;
  498. kfree(data);
  499. return 0;
  500. }
  501. static struct smsc47m192_data *smsc47m192_update_device(struct device *dev)
  502. {
  503. struct i2c_client *client = to_i2c_client(dev);
  504. struct smsc47m192_data *data = i2c_get_clientdata(client);
  505. int i, config;
  506. down(&data->update_lock);
  507. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  508. || !data->valid) {
  509. u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR);
  510. dev_dbg(&client->dev, "Starting smsc47m192 update\n");
  511. for (i = 0; i <= 7; i++) {
  512. data->in[i] = i2c_smbus_read_byte_data(client,
  513. SMSC47M192_REG_IN(i));
  514. data->in_min[i] = i2c_smbus_read_byte_data(client,
  515. SMSC47M192_REG_IN_MIN(i));
  516. data->in_max[i] = i2c_smbus_read_byte_data(client,
  517. SMSC47M192_REG_IN_MAX(i));
  518. }
  519. for (i = 0; i < 3; i++) {
  520. data->temp[i] = i2c_smbus_read_byte_data(client,
  521. SMSC47M192_REG_TEMP[i]);
  522. data->temp_max[i] = i2c_smbus_read_byte_data(client,
  523. SMSC47M192_REG_TEMP_MAX[i]);
  524. data->temp_min[i] = i2c_smbus_read_byte_data(client,
  525. SMSC47M192_REG_TEMP_MIN[i]);
  526. }
  527. for (i = 1; i < 3; i++)
  528. data->temp_offset[i] = i2c_smbus_read_byte_data(client,
  529. SMSC47M192_REG_TEMP_OFFSET(i));
  530. /* first offset is temp_offset[0] if SFR bit 4 is set,
  531. temp_offset[1] otherwise */
  532. if (sfr & 0x10) {
  533. data->temp_offset[0] = data->temp_offset[1];
  534. data->temp_offset[1] = 0;
  535. } else
  536. data->temp_offset[0] = 0;
  537. data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID)
  538. & 0x0f;
  539. config = i2c_smbus_read_byte_data(client,
  540. SMSC47M192_REG_CONFIG);
  541. if (config & 0x20)
  542. data->vid |= (i2c_smbus_read_byte_data(client,
  543. SMSC47M192_REG_VID4) & 0x01) << 4;
  544. data->alarms = i2c_smbus_read_byte_data(client,
  545. SMSC47M192_REG_ALARM1) |
  546. (i2c_smbus_read_byte_data(client,
  547. SMSC47M192_REG_ALARM2) << 8);
  548. data->last_updated = jiffies;
  549. data->valid = 1;
  550. }
  551. up(&data->update_lock);
  552. return data;
  553. }
  554. static int __init smsc47m192_init(void)
  555. {
  556. return i2c_add_driver(&smsc47m192_driver);
  557. }
  558. static void __exit smsc47m192_exit(void)
  559. {
  560. i2c_del_driver(&smsc47m192_driver);
  561. }
  562. MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>");
  563. MODULE_DESCRIPTION("SMSC47M192 driver");
  564. MODULE_LICENSE("GPL");
  565. module_init(smsc47m192_init);
  566. module_exit(smsc47m192_exit);