adm9240.c 23 KB

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