adm9240.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. *
  5. * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl>
  6. * Philip Edelbrock <phil@netroedge.com>
  7. * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl>
  8. * Copyright (C) 2005 Grant Coady <gcoady@gmail.com> with valuable
  9. * guidance from Jean Delvare
  10. *
  11. * Driver supports Analog Devices ADM9240
  12. * Dallas Semiconductor DS1780
  13. * National Semiconductor LM81
  14. *
  15. * ADM9240 is the reference, DS1780 and LM81 are register compatibles
  16. *
  17. * Voltage Six inputs are scaled by chip, VID also reported
  18. * Temperature Chip temperature to 0.5'C, maximum and max_hysteris
  19. * Fans 2 fans, low speed alarm, automatic fan clock divider
  20. * Alarms 16-bit map of active alarms
  21. * Analog Out 0..1250 mV output
  22. *
  23. * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear'
  24. *
  25. * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
  26. *
  27. * LM81 extended temp reading not implemented
  28. *
  29. * This program is free software; you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation; either version 2 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program; if not, write to the Free Software
  41. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42. */
  43. #include <linux/init.h>
  44. #include <linux/module.h>
  45. #include <linux/slab.h>
  46. #include <linux/i2c.h>
  47. #include <linux/i2c-sensor.h>
  48. #include <linux/i2c-vid.h>
  49. #include <linux/hwmon.h>
  50. #include <linux/err.h>
  51. /* Addresses to scan */
  52. static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
  53. I2C_CLIENT_END };
  54. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  55. /* Insmod parameters */
  56. SENSORS_INSMOD_3(adm9240, ds1780, lm81);
  57. /* ADM9240 registers */
  58. #define ADM9240_REG_MAN_ID 0x3e
  59. #define ADM9240_REG_DIE_REV 0x3f
  60. #define ADM9240_REG_CONFIG 0x40
  61. #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
  62. #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
  63. #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
  64. #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
  65. #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
  66. #define ADM9240_REG_INT(nr) (0x41 + (nr))
  67. #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
  68. #define ADM9240_REG_TEMP 0x27
  69. #define ADM9240_REG_TEMP_HIGH 0x39
  70. #define ADM9240_REG_TEMP_HYST 0x3a
  71. #define ADM9240_REG_ANALOG_OUT 0x19
  72. #define ADM9240_REG_CHASSIS_CLEAR 0x46
  73. #define ADM9240_REG_VID_FAN_DIV 0x47
  74. #define ADM9240_REG_I2C_ADDR 0x48
  75. #define ADM9240_REG_VID4 0x49
  76. #define ADM9240_REG_TEMP_CONF 0x4b
  77. /* generalised scaling with integer rounding */
  78. static inline int SCALE(long val, int mul, int div)
  79. {
  80. if (val < 0)
  81. return (val * mul - div / 2) / div;
  82. else
  83. return (val * mul + div / 2) / div;
  84. }
  85. /* adm9240 internally scales voltage measurements */
  86. static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
  87. static inline unsigned int IN_FROM_REG(u8 reg, int n)
  88. {
  89. return SCALE(reg, nom_mv[n], 192);
  90. }
  91. static inline u8 IN_TO_REG(unsigned long val, int n)
  92. {
  93. return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
  94. }
  95. /* temperature range: -40..125, 127 disables temperature alarm */
  96. static inline s8 TEMP_TO_REG(long val)
  97. {
  98. return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
  99. }
  100. /* two fans, each with low fan speed limit */
  101. static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
  102. {
  103. if (!reg) /* error */
  104. return -1;
  105. if (reg == 255)
  106. return 0;
  107. return SCALE(1350000, 1, reg * div);
  108. }
  109. /* analog out 0..1250mV */
  110. static inline u8 AOUT_TO_REG(unsigned long val)
  111. {
  112. return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
  113. }
  114. static inline unsigned int AOUT_FROM_REG(u8 reg)
  115. {
  116. return SCALE(reg, 1250, 255);
  117. }
  118. static int adm9240_attach_adapter(struct i2c_adapter *adapter);
  119. static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
  120. static void adm9240_init_client(struct i2c_client *client);
  121. static int adm9240_detach_client(struct i2c_client *client);
  122. static struct adm9240_data *adm9240_update_device(struct device *dev);
  123. /* driver data */
  124. static struct i2c_driver adm9240_driver = {
  125. .owner = THIS_MODULE,
  126. .name = "adm9240",
  127. .id = I2C_DRIVERID_ADM9240,
  128. .flags = I2C_DF_NOTIFY,
  129. .attach_adapter = adm9240_attach_adapter,
  130. .detach_client = adm9240_detach_client,
  131. };
  132. /* per client data */
  133. struct adm9240_data {
  134. enum chips type;
  135. struct i2c_client client;
  136. struct class_device *class_dev;
  137. struct semaphore update_lock;
  138. char valid;
  139. unsigned long last_updated_measure;
  140. unsigned long last_updated_config;
  141. u8 in[6]; /* ro in0_input */
  142. u8 in_max[6]; /* rw in0_max */
  143. u8 in_min[6]; /* rw in0_min */
  144. u8 fan[2]; /* ro fan1_input */
  145. u8 fan_min[2]; /* rw fan1_min */
  146. u8 fan_div[2]; /* rw fan1_div, read-only accessor */
  147. s16 temp; /* ro temp1_input, 9-bit sign-extended */
  148. s8 temp_high; /* rw temp1_max */
  149. s8 temp_hyst; /* rw temp1_max_hyst */
  150. u16 alarms; /* ro alarms */
  151. u8 aout; /* rw aout_output */
  152. u8 vid; /* ro vid */
  153. u8 vrm; /* -- vrm set on startup, no accessor */
  154. };
  155. /* i2c byte read/write interface */
  156. static int adm9240_read_value(struct i2c_client *client, u8 reg)
  157. {
  158. return i2c_smbus_read_byte_data(client, reg);
  159. }
  160. static int adm9240_write_value(struct i2c_client *client, u8 reg, u8 value)
  161. {
  162. return i2c_smbus_write_byte_data(client, reg, value);
  163. }
  164. /*** sysfs accessors ***/
  165. /* temperature */
  166. #define show_temp(value, scale) \
  167. static ssize_t show_##value(struct device *dev, \
  168. struct device_attribute *attr, \
  169. char *buf) \
  170. { \
  171. struct adm9240_data *data = adm9240_update_device(dev); \
  172. return sprintf(buf, "%d\n", data->value * scale); \
  173. }
  174. show_temp(temp_high, 1000);
  175. show_temp(temp_hyst, 1000);
  176. show_temp(temp, 500); /* 0.5'C per bit */
  177. #define set_temp(value, reg) \
  178. static ssize_t set_##value(struct device *dev, \
  179. struct device_attribute *attr, \
  180. const char *buf, size_t count) \
  181. { \
  182. struct i2c_client *client = to_i2c_client(dev); \
  183. struct adm9240_data *data = adm9240_update_device(dev); \
  184. long temp = simple_strtoul(buf, NULL, 10); \
  185. \
  186. down(&data->update_lock); \
  187. data->value = TEMP_TO_REG(temp); \
  188. adm9240_write_value(client, reg, data->value); \
  189. up(&data->update_lock); \
  190. return count; \
  191. }
  192. set_temp(temp_high, ADM9240_REG_TEMP_HIGH);
  193. set_temp(temp_hyst, ADM9240_REG_TEMP_HYST);
  194. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  195. show_temp_high, set_temp_high);
  196. static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  197. show_temp_hyst, set_temp_hyst);
  198. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  199. /* voltage */
  200. static ssize_t show_in(struct device *dev, char *buf, int nr)
  201. {
  202. struct adm9240_data *data = adm9240_update_device(dev);
  203. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
  204. }
  205. static ssize_t show_in_min(struct device *dev, char *buf, int nr)
  206. {
  207. struct adm9240_data *data = adm9240_update_device(dev);
  208. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
  209. }
  210. static ssize_t show_in_max(struct device *dev, char *buf, int nr)
  211. {
  212. struct adm9240_data *data = adm9240_update_device(dev);
  213. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
  214. }
  215. static ssize_t set_in_min(struct device *dev, const char *buf,
  216. size_t count, int nr)
  217. {
  218. struct i2c_client *client = to_i2c_client(dev);
  219. struct adm9240_data *data = i2c_get_clientdata(client);
  220. unsigned long val = simple_strtoul(buf, NULL, 10);
  221. down(&data->update_lock);
  222. data->in_min[nr] = IN_TO_REG(val, nr);
  223. adm9240_write_value(client, ADM9240_REG_IN_MIN(nr), data->in_min[nr]);
  224. up(&data->update_lock);
  225. return count;
  226. }
  227. static ssize_t set_in_max(struct device *dev, const char *buf,
  228. size_t count, int nr)
  229. {
  230. struct i2c_client *client = to_i2c_client(dev);
  231. struct adm9240_data *data = i2c_get_clientdata(client);
  232. unsigned long val = simple_strtoul(buf, NULL, 10);
  233. down(&data->update_lock);
  234. data->in_max[nr] = IN_TO_REG(val, nr);
  235. adm9240_write_value(client, ADM9240_REG_IN_MAX(nr), data->in_max[nr]);
  236. up(&data->update_lock);
  237. return count;
  238. }
  239. #define show_in_offset(offset) \
  240. static ssize_t show_in##offset(struct device *dev, \
  241. struct device_attribute *attr, \
  242. char *buf) \
  243. { \
  244. return show_in(dev, buf, offset); \
  245. } \
  246. static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL); \
  247. static ssize_t show_in##offset##_min(struct device *dev, \
  248. struct device_attribute *attr, \
  249. char *buf) \
  250. { \
  251. return show_in_min(dev, buf, offset); \
  252. } \
  253. static ssize_t show_in##offset##_max(struct device *dev, \
  254. struct device_attribute *attr, \
  255. char *buf) \
  256. { \
  257. return show_in_max(dev, buf, offset); \
  258. } \
  259. static ssize_t \
  260. set_in##offset##_min(struct device *dev, \
  261. struct device_attribute *attr, const char *buf, \
  262. size_t count) \
  263. { \
  264. return set_in_min(dev, buf, count, offset); \
  265. } \
  266. static ssize_t \
  267. set_in##offset##_max(struct device *dev, \
  268. struct device_attribute *attr, const char *buf, \
  269. size_t count) \
  270. { \
  271. return set_in_max(dev, buf, count, offset); \
  272. } \
  273. static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  274. show_in##offset##_min, set_in##offset##_min); \
  275. static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  276. show_in##offset##_max, set_in##offset##_max);
  277. show_in_offset(0);
  278. show_in_offset(1);
  279. show_in_offset(2);
  280. show_in_offset(3);
  281. show_in_offset(4);
  282. show_in_offset(5);
  283. /* fans */
  284. static ssize_t show_fan(struct device *dev, char *buf, int nr)
  285. {
  286. struct adm9240_data *data = adm9240_update_device(dev);
  287. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  288. 1 << data->fan_div[nr]));
  289. }
  290. static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
  291. {
  292. struct adm9240_data *data = adm9240_update_device(dev);
  293. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  294. 1 << data->fan_div[nr]));
  295. }
  296. static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
  297. {
  298. struct adm9240_data *data = adm9240_update_device(dev);
  299. return sprintf(buf, "%d\n", 1 << data->fan_div[nr]);
  300. }
  301. /* write new fan div, callers must hold data->update_lock */
  302. static void adm9240_write_fan_div(struct i2c_client *client, int nr,
  303. u8 fan_div)
  304. {
  305. u8 reg, old, shift = (nr + 2) * 2;
  306. reg = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV);
  307. old = (reg >> shift) & 3;
  308. reg &= ~(3 << shift);
  309. reg |= (fan_div << shift);
  310. adm9240_write_value(client, ADM9240_REG_VID_FAN_DIV, reg);
  311. dev_dbg(&client->dev, "fan%d clock divider changed from %u "
  312. "to %u\n", nr + 1, 1 << old, 1 << fan_div);
  313. }
  314. /*
  315. * set fan speed low limit:
  316. *
  317. * - value is zero: disable fan speed low limit alarm
  318. *
  319. * - value is below fan speed measurement range: enable fan speed low
  320. * limit alarm to be asserted while fan speed too slow to measure
  321. *
  322. * - otherwise: select fan clock divider to suit fan speed low limit,
  323. * measurement code may adjust registers to ensure fan speed reading
  324. */
  325. static ssize_t set_fan_min(struct device *dev, const char *buf,
  326. size_t count, int nr)
  327. {
  328. struct i2c_client *client = to_i2c_client(dev);
  329. struct adm9240_data *data = i2c_get_clientdata(client);
  330. unsigned long val = simple_strtoul(buf, NULL, 10);
  331. u8 new_div;
  332. down(&data->update_lock);
  333. if (!val) {
  334. data->fan_min[nr] = 255;
  335. new_div = data->fan_div[nr];
  336. dev_dbg(&client->dev, "fan%u low limit set disabled\n",
  337. nr + 1);
  338. } else if (val < 1350000 / (8 * 254)) {
  339. new_div = 3;
  340. data->fan_min[nr] = 254;
  341. dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
  342. nr + 1, FAN_FROM_REG(254, 1 << new_div));
  343. } else {
  344. unsigned int new_min = 1350000 / val;
  345. new_div = 0;
  346. while (new_min > 192 && new_div < 3) {
  347. new_div++;
  348. new_min /= 2;
  349. }
  350. if (!new_min) /* keep > 0 */
  351. new_min++;
  352. data->fan_min[nr] = new_min;
  353. dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
  354. nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
  355. }
  356. if (new_div != data->fan_div[nr]) {
  357. data->fan_div[nr] = new_div;
  358. adm9240_write_fan_div(client, nr, new_div);
  359. }
  360. adm9240_write_value(client, ADM9240_REG_FAN_MIN(nr),
  361. data->fan_min[nr]);
  362. up(&data->update_lock);
  363. return count;
  364. }
  365. #define show_fan_offset(offset) \
  366. static ssize_t show_fan_##offset (struct device *dev, \
  367. struct device_attribute *attr, \
  368. char *buf) \
  369. { \
  370. return show_fan(dev, buf, offset - 1); \
  371. } \
  372. static ssize_t show_fan_##offset##_div (struct device *dev, \
  373. struct device_attribute *attr, \
  374. char *buf) \
  375. { \
  376. return show_fan_div(dev, buf, offset - 1); \
  377. } \
  378. static ssize_t show_fan_##offset##_min (struct device *dev, \
  379. struct device_attribute *attr, \
  380. char *buf) \
  381. { \
  382. return show_fan_min(dev, buf, offset - 1); \
  383. } \
  384. static ssize_t set_fan_##offset##_min (struct device *dev, \
  385. struct device_attribute *attr, \
  386. const char *buf, size_t count) \
  387. { \
  388. return set_fan_min(dev, buf, count, offset - 1); \
  389. } \
  390. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  391. show_fan_##offset, NULL); \
  392. static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
  393. show_fan_##offset##_div, NULL); \
  394. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  395. show_fan_##offset##_min, set_fan_##offset##_min);
  396. show_fan_offset(1);
  397. show_fan_offset(2);
  398. /* alarms */
  399. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  400. {
  401. struct adm9240_data *data = adm9240_update_device(dev);
  402. return sprintf(buf, "%u\n", data->alarms);
  403. }
  404. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  405. /* vid */
  406. static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  407. {
  408. struct adm9240_data *data = adm9240_update_device(dev);
  409. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  410. }
  411. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  412. /* analog output */
  413. static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
  414. {
  415. struct adm9240_data *data = adm9240_update_device(dev);
  416. return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
  417. }
  418. static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  419. {
  420. struct i2c_client *client = to_i2c_client(dev);
  421. struct adm9240_data *data = i2c_get_clientdata(client);
  422. unsigned long val = simple_strtol(buf, NULL, 10);
  423. down(&data->update_lock);
  424. data->aout = AOUT_TO_REG(val);
  425. adm9240_write_value(client, ADM9240_REG_ANALOG_OUT, data->aout);
  426. up(&data->update_lock);
  427. return count;
  428. }
  429. static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
  430. /* chassis_clear */
  431. static ssize_t chassis_clear(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  432. {
  433. struct i2c_client *client = to_i2c_client(dev);
  434. unsigned long val = simple_strtol(buf, NULL, 10);
  435. if (val == 1) {
  436. adm9240_write_value(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
  437. dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
  438. }
  439. return count;
  440. }
  441. static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
  442. /*** sensor chip detect and driver install ***/
  443. static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
  444. {
  445. struct i2c_client *new_client;
  446. struct adm9240_data *data;
  447. int err = 0;
  448. const char *name = "";
  449. u8 man_id, die_rev;
  450. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  451. goto exit;
  452. if (!(data = kmalloc(sizeof(struct adm9240_data), GFP_KERNEL))) {
  453. err = -ENOMEM;
  454. goto exit;
  455. }
  456. memset(data, 0, sizeof(struct adm9240_data));
  457. new_client = &data->client;
  458. i2c_set_clientdata(new_client, data);
  459. new_client->addr = address;
  460. new_client->adapter = adapter;
  461. new_client->driver = &adm9240_driver;
  462. new_client->flags = 0;
  463. if (kind == 0) {
  464. kind = adm9240;
  465. }
  466. if (kind < 0) {
  467. /* verify chip: reg address should match i2c address */
  468. if (adm9240_read_value(new_client, ADM9240_REG_I2C_ADDR)
  469. != address) {
  470. dev_err(&adapter->dev, "detect fail: address match, "
  471. "0x%02x\n", address);
  472. goto exit_free;
  473. }
  474. /* check known chip manufacturer */
  475. man_id = adm9240_read_value(new_client, ADM9240_REG_MAN_ID);
  476. if (man_id == 0x23) {
  477. kind = adm9240;
  478. } else if (man_id == 0xda) {
  479. kind = ds1780;
  480. } else if (man_id == 0x01) {
  481. kind = lm81;
  482. } else {
  483. dev_err(&adapter->dev, "detect fail: unknown manuf, "
  484. "0x%02x\n", man_id);
  485. goto exit_free;
  486. }
  487. /* successful detect, print chip info */
  488. die_rev = adm9240_read_value(new_client, ADM9240_REG_DIE_REV);
  489. dev_info(&adapter->dev, "found %s revision %u\n",
  490. man_id == 0x23 ? "ADM9240" :
  491. man_id == 0xda ? "DS1780" : "LM81", die_rev);
  492. }
  493. /* either forced or detected chip kind */
  494. if (kind == adm9240) {
  495. name = "adm9240";
  496. } else if (kind == ds1780) {
  497. name = "ds1780";
  498. } else if (kind == lm81) {
  499. name = "lm81";
  500. }
  501. /* fill in the remaining client fields and attach */
  502. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  503. data->type = kind;
  504. init_MUTEX(&data->update_lock);
  505. if ((err = i2c_attach_client(new_client)))
  506. goto exit_free;
  507. adm9240_init_client(new_client);
  508. /* populate sysfs filesystem */
  509. data->class_dev = hwmon_device_register(&new_client->dev);
  510. if (IS_ERR(data->class_dev)) {
  511. err = PTR_ERR(data->class_dev);
  512. goto exit_detach;
  513. }
  514. device_create_file(&new_client->dev, &dev_attr_in0_input);
  515. device_create_file(&new_client->dev, &dev_attr_in0_min);
  516. device_create_file(&new_client->dev, &dev_attr_in0_max);
  517. device_create_file(&new_client->dev, &dev_attr_in1_input);
  518. device_create_file(&new_client->dev, &dev_attr_in1_min);
  519. device_create_file(&new_client->dev, &dev_attr_in1_max);
  520. device_create_file(&new_client->dev, &dev_attr_in2_input);
  521. device_create_file(&new_client->dev, &dev_attr_in2_min);
  522. device_create_file(&new_client->dev, &dev_attr_in2_max);
  523. device_create_file(&new_client->dev, &dev_attr_in3_input);
  524. device_create_file(&new_client->dev, &dev_attr_in3_min);
  525. device_create_file(&new_client->dev, &dev_attr_in3_max);
  526. device_create_file(&new_client->dev, &dev_attr_in4_input);
  527. device_create_file(&new_client->dev, &dev_attr_in4_min);
  528. device_create_file(&new_client->dev, &dev_attr_in4_max);
  529. device_create_file(&new_client->dev, &dev_attr_in5_input);
  530. device_create_file(&new_client->dev, &dev_attr_in5_min);
  531. device_create_file(&new_client->dev, &dev_attr_in5_max);
  532. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  533. device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
  534. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  535. device_create_file(&new_client->dev, &dev_attr_fan1_input);
  536. device_create_file(&new_client->dev, &dev_attr_fan1_div);
  537. device_create_file(&new_client->dev, &dev_attr_fan1_min);
  538. device_create_file(&new_client->dev, &dev_attr_fan2_input);
  539. device_create_file(&new_client->dev, &dev_attr_fan2_div);
  540. device_create_file(&new_client->dev, &dev_attr_fan2_min);
  541. device_create_file(&new_client->dev, &dev_attr_alarms);
  542. device_create_file(&new_client->dev, &dev_attr_aout_output);
  543. device_create_file(&new_client->dev, &dev_attr_chassis_clear);
  544. device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
  545. return 0;
  546. exit_detach:
  547. i2c_detach_client(new_client);
  548. exit_free:
  549. kfree(data);
  550. exit:
  551. return err;
  552. }
  553. static int adm9240_attach_adapter(struct i2c_adapter *adapter)
  554. {
  555. if (!(adapter->class & I2C_CLASS_HWMON))
  556. return 0;
  557. return i2c_detect(adapter, &addr_data, adm9240_detect);
  558. }
  559. static int adm9240_detach_client(struct i2c_client *client)
  560. {
  561. struct adm9240_data *data = i2c_get_clientdata(client);
  562. int err;
  563. hwmon_device_unregister(data->class_dev);
  564. if ((err = i2c_detach_client(client))) {
  565. dev_err(&client->dev, "Client deregistration failed, "
  566. "client not detached.\n");
  567. return err;
  568. }
  569. kfree(data);
  570. return 0;
  571. }
  572. static void adm9240_init_client(struct i2c_client *client)
  573. {
  574. struct adm9240_data *data = i2c_get_clientdata(client);
  575. u8 conf = adm9240_read_value(client, ADM9240_REG_CONFIG);
  576. u8 mode = adm9240_read_value(client, ADM9240_REG_TEMP_CONF) & 3;
  577. data->vrm = i2c_which_vrm(); /* need this to report vid as mV */
  578. dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
  579. data->vrm % 10);
  580. if (conf & 1) { /* measurement cycle running: report state */
  581. dev_info(&client->dev, "status: config 0x%02x mode %u\n",
  582. conf, mode);
  583. } else { /* cold start: open limits before starting chip */
  584. int i;
  585. for (i = 0; i < 6; i++)
  586. {
  587. adm9240_write_value(client,
  588. ADM9240_REG_IN_MIN(i), 0);
  589. adm9240_write_value(client,
  590. ADM9240_REG_IN_MAX(i), 255);
  591. }
  592. adm9240_write_value(client, ADM9240_REG_FAN_MIN(0), 255);
  593. adm9240_write_value(client, ADM9240_REG_FAN_MIN(1), 255);
  594. adm9240_write_value(client, ADM9240_REG_TEMP_HIGH, 127);
  595. adm9240_write_value(client, ADM9240_REG_TEMP_HYST, 127);
  596. /* start measurement cycle */
  597. adm9240_write_value(client, ADM9240_REG_CONFIG, 1);
  598. dev_info(&client->dev, "cold start: config was 0x%02x "
  599. "mode %u\n", conf, mode);
  600. }
  601. }
  602. static struct adm9240_data *adm9240_update_device(struct device *dev)
  603. {
  604. struct i2c_client *client = to_i2c_client(dev);
  605. struct adm9240_data *data = i2c_get_clientdata(client);
  606. int i;
  607. down(&data->update_lock);
  608. /* minimum measurement cycle: 1.75 seconds */
  609. if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
  610. || !data->valid) {
  611. for (i = 0; i < 6; i++) /* read voltages */
  612. {
  613. data->in[i] = adm9240_read_value(client,
  614. ADM9240_REG_IN(i));
  615. }
  616. data->alarms = adm9240_read_value(client,
  617. ADM9240_REG_INT(0)) |
  618. adm9240_read_value(client,
  619. ADM9240_REG_INT(1)) << 8;
  620. /* read temperature: assume temperature changes less than
  621. * 0.5'C per two measurement cycles thus ignore possible
  622. * but unlikely aliasing error on lsb reading. --Grant */
  623. data->temp = ((adm9240_read_value(client,
  624. ADM9240_REG_TEMP) << 8) |
  625. adm9240_read_value(client,
  626. ADM9240_REG_TEMP_CONF)) / 128;
  627. for (i = 0; i < 2; i++) /* read fans */
  628. {
  629. data->fan[i] = adm9240_read_value(client,
  630. ADM9240_REG_FAN(i));
  631. /* adjust fan clock divider on overflow */
  632. if (data->valid && data->fan[i] == 255 &&
  633. data->fan_div[i] < 3) {
  634. adm9240_write_fan_div(client, i,
  635. ++data->fan_div[i]);
  636. /* adjust fan_min if active, but not to 0 */
  637. if (data->fan_min[i] < 255 &&
  638. data->fan_min[i] >= 2)
  639. data->fan_min[i] /= 2;
  640. }
  641. }
  642. data->last_updated_measure = jiffies;
  643. }
  644. /* minimum config reading cycle: 300 seconds */
  645. if (time_after(jiffies, data->last_updated_config + (HZ * 300))
  646. || !data->valid) {
  647. for (i = 0; i < 6; i++)
  648. {
  649. data->in_min[i] = adm9240_read_value(client,
  650. ADM9240_REG_IN_MIN(i));
  651. data->in_max[i] = adm9240_read_value(client,
  652. ADM9240_REG_IN_MAX(i));
  653. }
  654. for (i = 0; i < 2; i++)
  655. {
  656. data->fan_min[i] = adm9240_read_value(client,
  657. ADM9240_REG_FAN_MIN(i));
  658. }
  659. data->temp_high = adm9240_read_value(client,
  660. ADM9240_REG_TEMP_HIGH);
  661. data->temp_hyst = adm9240_read_value(client,
  662. ADM9240_REG_TEMP_HYST);
  663. /* read fan divs and 5-bit VID */
  664. i = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV);
  665. data->fan_div[0] = (i >> 4) & 3;
  666. data->fan_div[1] = (i >> 6) & 3;
  667. data->vid = i & 0x0f;
  668. data->vid |= (adm9240_read_value(client,
  669. ADM9240_REG_VID4) & 1) << 4;
  670. /* read analog out */
  671. data->aout = adm9240_read_value(client,
  672. ADM9240_REG_ANALOG_OUT);
  673. data->last_updated_config = jiffies;
  674. data->valid = 1;
  675. }
  676. up(&data->update_lock);
  677. return data;
  678. }
  679. static int __init sensors_adm9240_init(void)
  680. {
  681. return i2c_add_driver(&adm9240_driver);
  682. }
  683. static void __exit sensors_adm9240_exit(void)
  684. {
  685. i2c_del_driver(&adm9240_driver);
  686. }
  687. MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
  688. "Grant Coady <gcoady@gmail.com> and others");
  689. MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
  690. MODULE_LICENSE("GPL");
  691. module_init(sensors_adm9240_init);
  692. module_exit(sensors_adm9240_exit);