adm9240.c 23 KB

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