adm9240.c 22 KB

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