adm9240.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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-sysfs.h>
  48. #include <linux/hwmon.h>
  49. #include <linux/hwmon-vid.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. /* Insmod parameters */
  55. I2C_CLIENT_INSMOD_3(adm9240, ds1780, lm81);
  56. /* ADM9240 registers */
  57. #define ADM9240_REG_MAN_ID 0x3e
  58. #define ADM9240_REG_DIE_REV 0x3f
  59. #define ADM9240_REG_CONFIG 0x40
  60. #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
  61. #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
  62. #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
  63. #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
  64. #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
  65. #define ADM9240_REG_INT(nr) (0x41 + (nr))
  66. #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
  67. #define ADM9240_REG_TEMP 0x27
  68. #define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
  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. .driver = {
  124. .name = "adm9240",
  125. },
  126. .id = I2C_DRIVERID_ADM9240,
  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_max[2]; /* rw 0 -> temp_max, 1 -> temp_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. /*** sysfs accessors ***/
  153. /* temperature */
  154. static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
  155. char *buf)
  156. {
  157. struct adm9240_data *data = adm9240_update_device(dev);
  158. return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
  159. }
  160. static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
  161. char *buf)
  162. {
  163. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  164. struct adm9240_data *data = adm9240_update_device(dev);
  165. return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
  166. }
  167. static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
  168. const char *buf, size_t count)
  169. {
  170. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  171. struct i2c_client *client = to_i2c_client(dev);
  172. struct adm9240_data *data = i2c_get_clientdata(client);
  173. long val = simple_strtol(buf, NULL, 10);
  174. down(&data->update_lock);
  175. data->temp_max[attr->index] = TEMP_TO_REG(val);
  176. i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
  177. data->temp_max[attr->index]);
  178. up(&data->update_lock);
  179. return count;
  180. }
  181. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  182. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  183. show_max, set_max, 0);
  184. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  185. show_max, set_max, 1);
  186. /* voltage */
  187. static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
  188. char *buf)
  189. {
  190. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  191. struct adm9240_data *data = adm9240_update_device(dev);
  192. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
  193. attr->index));
  194. }
  195. static ssize_t show_in_min(struct device *dev,
  196. struct device_attribute *devattr, char *buf)
  197. {
  198. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  199. struct adm9240_data *data = adm9240_update_device(dev);
  200. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
  201. attr->index));
  202. }
  203. static ssize_t show_in_max(struct device *dev,
  204. struct device_attribute *devattr, char *buf)
  205. {
  206. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  207. struct adm9240_data *data = adm9240_update_device(dev);
  208. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
  209. attr->index));
  210. }
  211. static ssize_t set_in_min(struct device *dev,
  212. struct device_attribute *devattr,
  213. const char *buf, size_t count)
  214. {
  215. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  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[attr->index] = IN_TO_REG(val, attr->index);
  221. i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
  222. data->in_min[attr->index]);
  223. up(&data->update_lock);
  224. return count;
  225. }
  226. static ssize_t set_in_max(struct device *dev,
  227. struct device_attribute *devattr,
  228. const char *buf, size_t count)
  229. {
  230. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  231. struct i2c_client *client = to_i2c_client(dev);
  232. struct adm9240_data *data = i2c_get_clientdata(client);
  233. unsigned long val = simple_strtoul(buf, NULL, 10);
  234. down(&data->update_lock);
  235. data->in_max[attr->index] = IN_TO_REG(val, attr->index);
  236. i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
  237. data->in_max[attr->index]);
  238. up(&data->update_lock);
  239. return count;
  240. }
  241. #define vin(nr) \
  242. static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \
  243. show_in, NULL, nr); \
  244. static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \
  245. show_in_min, set_in_min, nr); \
  246. static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \
  247. show_in_max, set_in_max, nr);
  248. vin(0);
  249. vin(1);
  250. vin(2);
  251. vin(3);
  252. vin(4);
  253. vin(5);
  254. /* fans */
  255. static ssize_t show_fan(struct device *dev,
  256. struct device_attribute *devattr, char *buf)
  257. {
  258. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  259. struct adm9240_data *data = adm9240_update_device(dev);
  260. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
  261. 1 << data->fan_div[attr->index]));
  262. }
  263. static ssize_t show_fan_min(struct device *dev,
  264. struct device_attribute *devattr, char *buf)
  265. {
  266. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  267. struct adm9240_data *data = adm9240_update_device(dev);
  268. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
  269. 1 << data->fan_div[attr->index]));
  270. }
  271. static ssize_t show_fan_div(struct device *dev,
  272. struct device_attribute *devattr, char *buf)
  273. {
  274. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  275. struct adm9240_data *data = adm9240_update_device(dev);
  276. return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
  277. }
  278. /* write new fan div, callers must hold data->update_lock */
  279. static void adm9240_write_fan_div(struct i2c_client *client, int nr,
  280. u8 fan_div)
  281. {
  282. u8 reg, old, shift = (nr + 2) * 2;
  283. reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
  284. old = (reg >> shift) & 3;
  285. reg &= ~(3 << shift);
  286. reg |= (fan_div << shift);
  287. i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
  288. dev_dbg(&client->dev, "fan%d clock divider changed from %u "
  289. "to %u\n", nr + 1, 1 << old, 1 << fan_div);
  290. }
  291. /*
  292. * set fan speed low limit:
  293. *
  294. * - value is zero: disable fan speed low limit alarm
  295. *
  296. * - value is below fan speed measurement range: enable fan speed low
  297. * limit alarm to be asserted while fan speed too slow to measure
  298. *
  299. * - otherwise: select fan clock divider to suit fan speed low limit,
  300. * measurement code may adjust registers to ensure fan speed reading
  301. */
  302. static ssize_t set_fan_min(struct device *dev,
  303. struct device_attribute *devattr,
  304. const char *buf, size_t count)
  305. {
  306. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  307. struct i2c_client *client = to_i2c_client(dev);
  308. struct adm9240_data *data = i2c_get_clientdata(client);
  309. unsigned long val = simple_strtoul(buf, NULL, 10);
  310. int nr = attr->index;
  311. u8 new_div;
  312. down(&data->update_lock);
  313. if (!val) {
  314. data->fan_min[nr] = 255;
  315. new_div = data->fan_div[nr];
  316. dev_dbg(&client->dev, "fan%u low limit set disabled\n",
  317. nr + 1);
  318. } else if (val < 1350000 / (8 * 254)) {
  319. new_div = 3;
  320. data->fan_min[nr] = 254;
  321. dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
  322. nr + 1, FAN_FROM_REG(254, 1 << new_div));
  323. } else {
  324. unsigned int new_min = 1350000 / val;
  325. new_div = 0;
  326. while (new_min > 192 && new_div < 3) {
  327. new_div++;
  328. new_min /= 2;
  329. }
  330. if (!new_min) /* keep > 0 */
  331. new_min++;
  332. data->fan_min[nr] = new_min;
  333. dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
  334. nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
  335. }
  336. if (new_div != data->fan_div[nr]) {
  337. data->fan_div[nr] = new_div;
  338. adm9240_write_fan_div(client, nr, new_div);
  339. }
  340. i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
  341. data->fan_min[nr]);
  342. up(&data->update_lock);
  343. return count;
  344. }
  345. #define fan(nr) \
  346. static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \
  347. show_fan, NULL, nr - 1); \
  348. static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \
  349. show_fan_div, NULL, nr - 1); \
  350. static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \
  351. show_fan_min, set_fan_min, nr - 1);
  352. fan(1);
  353. fan(2);
  354. /* alarms */
  355. static ssize_t show_alarms(struct device *dev,
  356. struct device_attribute *attr, char *buf)
  357. {
  358. struct adm9240_data *data = adm9240_update_device(dev);
  359. return sprintf(buf, "%u\n", data->alarms);
  360. }
  361. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  362. /* vid */
  363. static ssize_t show_vid(struct device *dev,
  364. struct device_attribute *attr, char *buf)
  365. {
  366. struct adm9240_data *data = adm9240_update_device(dev);
  367. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  368. }
  369. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  370. /* analog output */
  371. static ssize_t show_aout(struct device *dev,
  372. struct device_attribute *attr, char *buf)
  373. {
  374. struct adm9240_data *data = adm9240_update_device(dev);
  375. return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
  376. }
  377. static ssize_t set_aout(struct device *dev,
  378. struct device_attribute *attr,
  379. const char *buf, size_t count)
  380. {
  381. struct i2c_client *client = to_i2c_client(dev);
  382. struct adm9240_data *data = i2c_get_clientdata(client);
  383. unsigned long val = simple_strtol(buf, NULL, 10);
  384. down(&data->update_lock);
  385. data->aout = AOUT_TO_REG(val);
  386. i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
  387. up(&data->update_lock);
  388. return count;
  389. }
  390. static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
  391. /* chassis_clear */
  392. static ssize_t chassis_clear(struct device *dev,
  393. struct device_attribute *attr,
  394. const char *buf, size_t count)
  395. {
  396. struct i2c_client *client = to_i2c_client(dev);
  397. unsigned long val = simple_strtol(buf, NULL, 10);
  398. if (val == 1) {
  399. i2c_smbus_write_byte_data(client,
  400. ADM9240_REG_CHASSIS_CLEAR, 0x80);
  401. dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
  402. }
  403. return count;
  404. }
  405. static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
  406. /*** sensor chip detect and driver install ***/
  407. static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
  408. {
  409. struct i2c_client *new_client;
  410. struct adm9240_data *data;
  411. int err = 0;
  412. const char *name = "";
  413. u8 man_id, die_rev;
  414. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  415. goto exit;
  416. if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) {
  417. err = -ENOMEM;
  418. goto exit;
  419. }
  420. new_client = &data->client;
  421. i2c_set_clientdata(new_client, data);
  422. new_client->addr = address;
  423. new_client->adapter = adapter;
  424. new_client->driver = &adm9240_driver;
  425. new_client->flags = 0;
  426. if (kind == 0) {
  427. kind = adm9240;
  428. }
  429. if (kind < 0) {
  430. /* verify chip: reg address should match i2c address */
  431. if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
  432. != address) {
  433. dev_err(&adapter->dev, "detect fail: address match, "
  434. "0x%02x\n", address);
  435. goto exit_free;
  436. }
  437. /* check known chip manufacturer */
  438. man_id = i2c_smbus_read_byte_data(new_client,
  439. ADM9240_REG_MAN_ID);
  440. if (man_id == 0x23) {
  441. kind = adm9240;
  442. } else if (man_id == 0xda) {
  443. kind = ds1780;
  444. } else if (man_id == 0x01) {
  445. kind = lm81;
  446. } else {
  447. dev_err(&adapter->dev, "detect fail: unknown manuf, "
  448. "0x%02x\n", man_id);
  449. goto exit_free;
  450. }
  451. /* successful detect, print chip info */
  452. die_rev = i2c_smbus_read_byte_data(new_client,
  453. ADM9240_REG_DIE_REV);
  454. dev_info(&adapter->dev, "found %s revision %u\n",
  455. man_id == 0x23 ? "ADM9240" :
  456. man_id == 0xda ? "DS1780" : "LM81", die_rev);
  457. }
  458. /* either forced or detected chip kind */
  459. if (kind == adm9240) {
  460. name = "adm9240";
  461. } else if (kind == ds1780) {
  462. name = "ds1780";
  463. } else if (kind == lm81) {
  464. name = "lm81";
  465. }
  466. /* fill in the remaining client fields and attach */
  467. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  468. data->type = kind;
  469. init_MUTEX(&data->update_lock);
  470. if ((err = i2c_attach_client(new_client)))
  471. goto exit_free;
  472. adm9240_init_client(new_client);
  473. /* populate sysfs filesystem */
  474. data->class_dev = hwmon_device_register(&new_client->dev);
  475. if (IS_ERR(data->class_dev)) {
  476. err = PTR_ERR(data->class_dev);
  477. goto exit_detach;
  478. }
  479. device_create_file(&new_client->dev,
  480. &sensor_dev_attr_in0_input.dev_attr);
  481. device_create_file(&new_client->dev,
  482. &sensor_dev_attr_in0_min.dev_attr);
  483. device_create_file(&new_client->dev,
  484. &sensor_dev_attr_in0_max.dev_attr);
  485. device_create_file(&new_client->dev,
  486. &sensor_dev_attr_in1_input.dev_attr);
  487. device_create_file(&new_client->dev,
  488. &sensor_dev_attr_in1_min.dev_attr);
  489. device_create_file(&new_client->dev,
  490. &sensor_dev_attr_in1_max.dev_attr);
  491. device_create_file(&new_client->dev,
  492. &sensor_dev_attr_in2_input.dev_attr);
  493. device_create_file(&new_client->dev,
  494. &sensor_dev_attr_in2_min.dev_attr);
  495. device_create_file(&new_client->dev,
  496. &sensor_dev_attr_in2_max.dev_attr);
  497. device_create_file(&new_client->dev,
  498. &sensor_dev_attr_in3_input.dev_attr);
  499. device_create_file(&new_client->dev,
  500. &sensor_dev_attr_in3_min.dev_attr);
  501. device_create_file(&new_client->dev,
  502. &sensor_dev_attr_in3_max.dev_attr);
  503. device_create_file(&new_client->dev,
  504. &sensor_dev_attr_in4_input.dev_attr);
  505. device_create_file(&new_client->dev,
  506. &sensor_dev_attr_in4_min.dev_attr);
  507. device_create_file(&new_client->dev,
  508. &sensor_dev_attr_in4_max.dev_attr);
  509. device_create_file(&new_client->dev,
  510. &sensor_dev_attr_in5_input.dev_attr);
  511. device_create_file(&new_client->dev,
  512. &sensor_dev_attr_in5_min.dev_attr);
  513. device_create_file(&new_client->dev,
  514. &sensor_dev_attr_in5_max.dev_attr);
  515. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  516. device_create_file(&new_client->dev,
  517. &sensor_dev_attr_temp1_max.dev_attr);
  518. device_create_file(&new_client->dev,
  519. &sensor_dev_attr_temp1_max_hyst.dev_attr);
  520. device_create_file(&new_client->dev,
  521. &sensor_dev_attr_fan1_input.dev_attr);
  522. device_create_file(&new_client->dev,
  523. &sensor_dev_attr_fan1_div.dev_attr);
  524. device_create_file(&new_client->dev,
  525. &sensor_dev_attr_fan1_min.dev_attr);
  526. device_create_file(&new_client->dev,
  527. &sensor_dev_attr_fan2_input.dev_attr);
  528. device_create_file(&new_client->dev,
  529. &sensor_dev_attr_fan2_div.dev_attr);
  530. device_create_file(&new_client->dev,
  531. &sensor_dev_attr_fan2_min.dev_attr);
  532. device_create_file(&new_client->dev, &dev_attr_alarms);
  533. device_create_file(&new_client->dev, &dev_attr_aout_output);
  534. device_create_file(&new_client->dev, &dev_attr_chassis_clear);
  535. device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
  536. return 0;
  537. exit_detach:
  538. i2c_detach_client(new_client);
  539. exit_free:
  540. kfree(data);
  541. exit:
  542. return err;
  543. }
  544. static int adm9240_attach_adapter(struct i2c_adapter *adapter)
  545. {
  546. if (!(adapter->class & I2C_CLASS_HWMON))
  547. return 0;
  548. return i2c_probe(adapter, &addr_data, adm9240_detect);
  549. }
  550. static int adm9240_detach_client(struct i2c_client *client)
  551. {
  552. struct adm9240_data *data = i2c_get_clientdata(client);
  553. int err;
  554. hwmon_device_unregister(data->class_dev);
  555. if ((err = i2c_detach_client(client)))
  556. return err;
  557. kfree(data);
  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 = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
  564. u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
  565. data->vrm = vid_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. i2c_smbus_write_byte_data(client,
  576. ADM9240_REG_IN_MIN(i), 0);
  577. i2c_smbus_write_byte_data(client,
  578. ADM9240_REG_IN_MAX(i), 255);
  579. }
  580. i2c_smbus_write_byte_data(client,
  581. ADM9240_REG_FAN_MIN(0), 255);
  582. i2c_smbus_write_byte_data(client,
  583. ADM9240_REG_FAN_MIN(1), 255);
  584. i2c_smbus_write_byte_data(client,
  585. ADM9240_REG_TEMP_MAX(0), 127);
  586. i2c_smbus_write_byte_data(client,
  587. ADM9240_REG_TEMP_MAX(1), 127);
  588. /* start measurement cycle */
  589. i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
  590. dev_info(&client->dev, "cold start: config was 0x%02x "
  591. "mode %u\n", conf, mode);
  592. }
  593. }
  594. static struct adm9240_data *adm9240_update_device(struct device *dev)
  595. {
  596. struct i2c_client *client = to_i2c_client(dev);
  597. struct adm9240_data *data = i2c_get_clientdata(client);
  598. int i;
  599. down(&data->update_lock);
  600. /* minimum measurement cycle: 1.75 seconds */
  601. if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
  602. || !data->valid) {
  603. for (i = 0; i < 6; i++) /* read voltages */
  604. {
  605. data->in[i] = i2c_smbus_read_byte_data(client,
  606. ADM9240_REG_IN(i));
  607. }
  608. data->alarms = i2c_smbus_read_byte_data(client,
  609. ADM9240_REG_INT(0)) |
  610. i2c_smbus_read_byte_data(client,
  611. ADM9240_REG_INT(1)) << 8;
  612. /* read temperature: assume temperature changes less than
  613. * 0.5'C per two measurement cycles thus ignore possible
  614. * but unlikely aliasing error on lsb reading. --Grant */
  615. data->temp = ((i2c_smbus_read_byte_data(client,
  616. ADM9240_REG_TEMP) << 8) |
  617. i2c_smbus_read_byte_data(client,
  618. ADM9240_REG_TEMP_CONF)) / 128;
  619. for (i = 0; i < 2; i++) /* read fans */
  620. {
  621. data->fan[i] = i2c_smbus_read_byte_data(client,
  622. ADM9240_REG_FAN(i));
  623. /* adjust fan clock divider on overflow */
  624. if (data->valid && data->fan[i] == 255 &&
  625. data->fan_div[i] < 3) {
  626. adm9240_write_fan_div(client, i,
  627. ++data->fan_div[i]);
  628. /* adjust fan_min if active, but not to 0 */
  629. if (data->fan_min[i] < 255 &&
  630. data->fan_min[i] >= 2)
  631. data->fan_min[i] /= 2;
  632. }
  633. }
  634. data->last_updated_measure = jiffies;
  635. }
  636. /* minimum config reading cycle: 300 seconds */
  637. if (time_after(jiffies, data->last_updated_config + (HZ * 300))
  638. || !data->valid) {
  639. for (i = 0; i < 6; i++)
  640. {
  641. data->in_min[i] = i2c_smbus_read_byte_data(client,
  642. ADM9240_REG_IN_MIN(i));
  643. data->in_max[i] = i2c_smbus_read_byte_data(client,
  644. ADM9240_REG_IN_MAX(i));
  645. }
  646. for (i = 0; i < 2; i++)
  647. {
  648. data->fan_min[i] = i2c_smbus_read_byte_data(client,
  649. ADM9240_REG_FAN_MIN(i));
  650. }
  651. data->temp_max[0] = i2c_smbus_read_byte_data(client,
  652. ADM9240_REG_TEMP_MAX(0));
  653. data->temp_max[1] = i2c_smbus_read_byte_data(client,
  654. ADM9240_REG_TEMP_MAX(1));
  655. /* read fan divs and 5-bit VID */
  656. i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
  657. data->fan_div[0] = (i >> 4) & 3;
  658. data->fan_div[1] = (i >> 6) & 3;
  659. data->vid = i & 0x0f;
  660. data->vid |= (i2c_smbus_read_byte_data(client,
  661. ADM9240_REG_VID4) & 1) << 4;
  662. /* read analog out */
  663. data->aout = i2c_smbus_read_byte_data(client,
  664. ADM9240_REG_ANALOG_OUT);
  665. data->last_updated_config = jiffies;
  666. data->valid = 1;
  667. }
  668. up(&data->update_lock);
  669. return data;
  670. }
  671. static int __init sensors_adm9240_init(void)
  672. {
  673. return i2c_add_driver(&adm9240_driver);
  674. }
  675. static void __exit sensors_adm9240_exit(void)
  676. {
  677. i2c_del_driver(&adm9240_driver);
  678. }
  679. MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
  680. "Grant Coady <gcoady@gmail.com> and others");
  681. MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
  682. MODULE_LICENSE("GPL");
  683. module_init(sensors_adm9240_init);
  684. module_exit(sensors_adm9240_exit);