adm1031.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /*
  2. * adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Based on lm75.c and lm85.c
  5. * Supports adm1030 / adm1031
  6. * Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
  7. * Reworked by Jean Delvare <jdelvare@suse.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/i2c.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. /* Following macros takes channel parameter starting from 0 to 2 */
  33. #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
  34. #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
  35. #define ADM1031_REG_PWM (0x22)
  36. #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
  37. #define ADM1031_REG_FAN_FILTER (0x23)
  38. #define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
  39. #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
  40. #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
  41. #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
  42. #define ADM1031_REG_TEMP(nr) (0x0a + (nr))
  43. #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
  44. #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
  45. #define ADM1031_REG_CONF1 0x00
  46. #define ADM1031_REG_CONF2 0x01
  47. #define ADM1031_REG_EXT_TEMP 0x06
  48. #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
  49. #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
  50. #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
  51. #define ADM1031_CONF2_PWM1_ENABLE 0x01
  52. #define ADM1031_CONF2_PWM2_ENABLE 0x02
  53. #define ADM1031_CONF2_TACH1_ENABLE 0x04
  54. #define ADM1031_CONF2_TACH2_ENABLE 0x08
  55. #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
  56. #define ADM1031_UPDATE_RATE_MASK 0x1c
  57. #define ADM1031_UPDATE_RATE_SHIFT 2
  58. /* Addresses to scan */
  59. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  60. enum chips { adm1030, adm1031 };
  61. typedef u8 auto_chan_table_t[8][2];
  62. /* Each client has this additional data */
  63. struct adm1031_data {
  64. struct i2c_client *client;
  65. const struct attribute_group *groups[3];
  66. struct mutex update_lock;
  67. int chip_type;
  68. char valid; /* !=0 if following fields are valid */
  69. unsigned long last_updated; /* In jiffies */
  70. unsigned int update_interval; /* In milliseconds */
  71. /*
  72. * The chan_select_table contains the possible configurations for
  73. * auto fan control.
  74. */
  75. const auto_chan_table_t *chan_select_table;
  76. u16 alarm;
  77. u8 conf1;
  78. u8 conf2;
  79. u8 fan[2];
  80. u8 fan_div[2];
  81. u8 fan_min[2];
  82. u8 pwm[2];
  83. u8 old_pwm[2];
  84. s8 temp[3];
  85. u8 ext_temp[3];
  86. u8 auto_temp[3];
  87. u8 auto_temp_min[3];
  88. u8 auto_temp_off[3];
  89. u8 auto_temp_max[3];
  90. s8 temp_offset[3];
  91. s8 temp_min[3];
  92. s8 temp_max[3];
  93. s8 temp_crit[3];
  94. };
  95. static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
  96. {
  97. return i2c_smbus_read_byte_data(client, reg);
  98. }
  99. static inline int
  100. adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
  101. {
  102. return i2c_smbus_write_byte_data(client, reg, value);
  103. }
  104. static struct adm1031_data *adm1031_update_device(struct device *dev)
  105. {
  106. struct adm1031_data *data = dev_get_drvdata(dev);
  107. struct i2c_client *client = data->client;
  108. unsigned long next_update;
  109. int chan;
  110. mutex_lock(&data->update_lock);
  111. next_update = data->last_updated
  112. + msecs_to_jiffies(data->update_interval);
  113. if (time_after(jiffies, next_update) || !data->valid) {
  114. dev_dbg(&client->dev, "Starting adm1031 update\n");
  115. for (chan = 0;
  116. chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
  117. u8 oldh, newh;
  118. oldh =
  119. adm1031_read_value(client, ADM1031_REG_TEMP(chan));
  120. data->ext_temp[chan] =
  121. adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
  122. newh =
  123. adm1031_read_value(client, ADM1031_REG_TEMP(chan));
  124. if (newh != oldh) {
  125. data->ext_temp[chan] =
  126. adm1031_read_value(client,
  127. ADM1031_REG_EXT_TEMP);
  128. #ifdef DEBUG
  129. oldh =
  130. adm1031_read_value(client,
  131. ADM1031_REG_TEMP(chan));
  132. /* oldh is actually newer */
  133. if (newh != oldh)
  134. dev_warn(&client->dev,
  135. "Remote temperature may be wrong.\n");
  136. #endif
  137. }
  138. data->temp[chan] = newh;
  139. data->temp_offset[chan] =
  140. adm1031_read_value(client,
  141. ADM1031_REG_TEMP_OFFSET(chan));
  142. data->temp_min[chan] =
  143. adm1031_read_value(client,
  144. ADM1031_REG_TEMP_MIN(chan));
  145. data->temp_max[chan] =
  146. adm1031_read_value(client,
  147. ADM1031_REG_TEMP_MAX(chan));
  148. data->temp_crit[chan] =
  149. adm1031_read_value(client,
  150. ADM1031_REG_TEMP_CRIT(chan));
  151. data->auto_temp[chan] =
  152. adm1031_read_value(client,
  153. ADM1031_REG_AUTO_TEMP(chan));
  154. }
  155. data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
  156. data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
  157. data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
  158. | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) << 8);
  159. if (data->chip_type == adm1030)
  160. data->alarm &= 0xc0ff;
  161. for (chan = 0; chan < (data->chip_type == adm1030 ? 1 : 2);
  162. chan++) {
  163. data->fan_div[chan] =
  164. adm1031_read_value(client,
  165. ADM1031_REG_FAN_DIV(chan));
  166. data->fan_min[chan] =
  167. adm1031_read_value(client,
  168. ADM1031_REG_FAN_MIN(chan));
  169. data->fan[chan] =
  170. adm1031_read_value(client,
  171. ADM1031_REG_FAN_SPEED(chan));
  172. data->pwm[chan] =
  173. (adm1031_read_value(client,
  174. ADM1031_REG_PWM) >> (4 * chan)) & 0x0f;
  175. }
  176. data->last_updated = jiffies;
  177. data->valid = 1;
  178. }
  179. mutex_unlock(&data->update_lock);
  180. return data;
  181. }
  182. #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
  183. ((val + 500) / 1000)))
  184. #define TEMP_FROM_REG(val) ((val) * 1000)
  185. #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
  186. #define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
  187. #define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
  188. (val) | 0x70 : (val))
  189. #define FAN_FROM_REG(reg, div) ((reg) ? \
  190. (11250 * 60) / ((reg) * (div)) : 0)
  191. static int FAN_TO_REG(int reg, int div)
  192. {
  193. int tmp;
  194. tmp = FAN_FROM_REG(clamp_val(reg, 0, 65535), div);
  195. return tmp > 255 ? 255 : tmp;
  196. }
  197. #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
  198. #define PWM_TO_REG(val) (clamp_val((val), 0, 255) >> 4)
  199. #define PWM_FROM_REG(val) ((val) << 4)
  200. #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
  201. #define FAN_CHAN_TO_REG(val, reg) \
  202. (((reg) & 0x1F) | (((val) << 5) & 0xe0))
  203. #define AUTO_TEMP_MIN_TO_REG(val, reg) \
  204. ((((val) / 500) & 0xf8) | ((reg) & 0x7))
  205. #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7)))
  206. #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
  207. #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
  208. #define AUTO_TEMP_OFF_FROM_REG(reg) \
  209. (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
  210. #define AUTO_TEMP_MAX_FROM_REG(reg) \
  211. (AUTO_TEMP_RANGE_FROM_REG(reg) + \
  212. AUTO_TEMP_MIN_FROM_REG(reg))
  213. static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
  214. {
  215. int ret;
  216. int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
  217. range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
  218. ret = ((reg & 0xf8) |
  219. (range < 10000 ? 0 :
  220. range < 20000 ? 1 :
  221. range < 40000 ? 2 : range < 80000 ? 3 : 4));
  222. return ret;
  223. }
  224. /* FAN auto control */
  225. #define GET_FAN_AUTO_BITFIELD(data, idx) \
  226. (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
  227. /*
  228. * The tables below contains the possible values for the auto fan
  229. * control bitfields. the index in the table is the register value.
  230. * MSb is the auto fan control enable bit, so the four first entries
  231. * in the table disables auto fan control when both bitfields are zero.
  232. */
  233. static const auto_chan_table_t auto_channel_select_table_adm1031 = {
  234. { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
  235. { 2 /* 0b010 */ , 4 /* 0b100 */ },
  236. { 2 /* 0b010 */ , 2 /* 0b010 */ },
  237. { 4 /* 0b100 */ , 4 /* 0b100 */ },
  238. { 7 /* 0b111 */ , 7 /* 0b111 */ },
  239. };
  240. static const auto_chan_table_t auto_channel_select_table_adm1030 = {
  241. { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
  242. { 2 /* 0b10 */ , 0 },
  243. { 0xff /* invalid */ , 0 },
  244. { 0xff /* invalid */ , 0 },
  245. { 3 /* 0b11 */ , 0 },
  246. };
  247. /*
  248. * That function checks if a bitfield is valid and returns the other bitfield
  249. * nearest match if no exact match where found.
  250. */
  251. static int
  252. get_fan_auto_nearest(struct adm1031_data *data, int chan, u8 val, u8 reg)
  253. {
  254. int i;
  255. int first_match = -1, exact_match = -1;
  256. u8 other_reg_val =
  257. (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
  258. if (val == 0)
  259. return 0;
  260. for (i = 0; i < 8; i++) {
  261. if ((val == (*data->chan_select_table)[i][chan]) &&
  262. ((*data->chan_select_table)[i][chan ? 0 : 1] ==
  263. other_reg_val)) {
  264. /* We found an exact match */
  265. exact_match = i;
  266. break;
  267. } else if (val == (*data->chan_select_table)[i][chan] &&
  268. first_match == -1) {
  269. /*
  270. * Save the first match in case of an exact match has
  271. * not been found
  272. */
  273. first_match = i;
  274. }
  275. }
  276. if (exact_match >= 0)
  277. return exact_match;
  278. else if (first_match >= 0)
  279. return first_match;
  280. return -EINVAL;
  281. }
  282. static ssize_t show_fan_auto_channel(struct device *dev,
  283. struct device_attribute *attr, char *buf)
  284. {
  285. int nr = to_sensor_dev_attr(attr)->index;
  286. struct adm1031_data *data = adm1031_update_device(dev);
  287. return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
  288. }
  289. static ssize_t
  290. set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
  291. const char *buf, size_t count)
  292. {
  293. struct adm1031_data *data = dev_get_drvdata(dev);
  294. struct i2c_client *client = data->client;
  295. int nr = to_sensor_dev_attr(attr)->index;
  296. long val;
  297. u8 reg;
  298. int ret;
  299. u8 old_fan_mode;
  300. ret = kstrtol(buf, 10, &val);
  301. if (ret)
  302. return ret;
  303. old_fan_mode = data->conf1;
  304. mutex_lock(&data->update_lock);
  305. ret = get_fan_auto_nearest(data, nr, val, data->conf1);
  306. if (ret < 0) {
  307. mutex_unlock(&data->update_lock);
  308. return ret;
  309. }
  310. reg = ret;
  311. data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
  312. if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
  313. (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
  314. if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
  315. /*
  316. * Switch to Auto Fan Mode
  317. * Save PWM registers
  318. * Set PWM registers to 33% Both
  319. */
  320. data->old_pwm[0] = data->pwm[0];
  321. data->old_pwm[1] = data->pwm[1];
  322. adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
  323. } else {
  324. /* Switch to Manual Mode */
  325. data->pwm[0] = data->old_pwm[0];
  326. data->pwm[1] = data->old_pwm[1];
  327. /* Restore PWM registers */
  328. adm1031_write_value(client, ADM1031_REG_PWM,
  329. data->pwm[0] | (data->pwm[1] << 4));
  330. }
  331. }
  332. data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
  333. adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
  334. mutex_unlock(&data->update_lock);
  335. return count;
  336. }
  337. static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
  338. show_fan_auto_channel, set_fan_auto_channel, 0);
  339. static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
  340. show_fan_auto_channel, set_fan_auto_channel, 1);
  341. /* Auto Temps */
  342. static ssize_t show_auto_temp_off(struct device *dev,
  343. struct device_attribute *attr, char *buf)
  344. {
  345. int nr = to_sensor_dev_attr(attr)->index;
  346. struct adm1031_data *data = adm1031_update_device(dev);
  347. return sprintf(buf, "%d\n",
  348. AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
  349. }
  350. static ssize_t show_auto_temp_min(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. int nr = to_sensor_dev_attr(attr)->index;
  354. struct adm1031_data *data = adm1031_update_device(dev);
  355. return sprintf(buf, "%d\n",
  356. AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
  357. }
  358. static ssize_t
  359. set_auto_temp_min(struct device *dev, struct device_attribute *attr,
  360. const char *buf, size_t count)
  361. {
  362. struct adm1031_data *data = dev_get_drvdata(dev);
  363. struct i2c_client *client = data->client;
  364. int nr = to_sensor_dev_attr(attr)->index;
  365. long val;
  366. int ret;
  367. ret = kstrtol(buf, 10, &val);
  368. if (ret)
  369. return ret;
  370. val = clamp_val(val, 0, 127000);
  371. mutex_lock(&data->update_lock);
  372. data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
  373. adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
  374. data->auto_temp[nr]);
  375. mutex_unlock(&data->update_lock);
  376. return count;
  377. }
  378. static ssize_t show_auto_temp_max(struct device *dev,
  379. struct device_attribute *attr, char *buf)
  380. {
  381. int nr = to_sensor_dev_attr(attr)->index;
  382. struct adm1031_data *data = adm1031_update_device(dev);
  383. return sprintf(buf, "%d\n",
  384. AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
  385. }
  386. static ssize_t
  387. set_auto_temp_max(struct device *dev, struct device_attribute *attr,
  388. const char *buf, size_t count)
  389. {
  390. struct adm1031_data *data = dev_get_drvdata(dev);
  391. struct i2c_client *client = data->client;
  392. int nr = to_sensor_dev_attr(attr)->index;
  393. long val;
  394. int ret;
  395. ret = kstrtol(buf, 10, &val);
  396. if (ret)
  397. return ret;
  398. val = clamp_val(val, 0, 127000);
  399. mutex_lock(&data->update_lock);
  400. data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
  401. data->pwm[nr]);
  402. adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
  403. data->temp_max[nr]);
  404. mutex_unlock(&data->update_lock);
  405. return count;
  406. }
  407. #define auto_temp_reg(offset) \
  408. static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
  409. show_auto_temp_off, NULL, offset - 1); \
  410. static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
  411. show_auto_temp_min, set_auto_temp_min, offset - 1); \
  412. static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
  413. show_auto_temp_max, set_auto_temp_max, offset - 1)
  414. auto_temp_reg(1);
  415. auto_temp_reg(2);
  416. auto_temp_reg(3);
  417. /* pwm */
  418. static ssize_t show_pwm(struct device *dev,
  419. struct device_attribute *attr, char *buf)
  420. {
  421. int nr = to_sensor_dev_attr(attr)->index;
  422. struct adm1031_data *data = adm1031_update_device(dev);
  423. return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
  424. }
  425. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  426. const char *buf, size_t count)
  427. {
  428. struct adm1031_data *data = dev_get_drvdata(dev);
  429. struct i2c_client *client = data->client;
  430. int nr = to_sensor_dev_attr(attr)->index;
  431. long val;
  432. int ret, reg;
  433. ret = kstrtol(buf, 10, &val);
  434. if (ret)
  435. return ret;
  436. mutex_lock(&data->update_lock);
  437. if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
  438. (((val>>4) & 0xf) != 5)) {
  439. /* In automatic mode, the only PWM accepted is 33% */
  440. mutex_unlock(&data->update_lock);
  441. return -EINVAL;
  442. }
  443. data->pwm[nr] = PWM_TO_REG(val);
  444. reg = adm1031_read_value(client, ADM1031_REG_PWM);
  445. adm1031_write_value(client, ADM1031_REG_PWM,
  446. nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
  447. : (data->pwm[nr] & 0xf) | (reg & 0xf0));
  448. mutex_unlock(&data->update_lock);
  449. return count;
  450. }
  451. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
  452. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
  453. static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
  454. show_pwm, set_pwm, 0);
  455. static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
  456. show_pwm, set_pwm, 1);
  457. /* Fans */
  458. /*
  459. * That function checks the cases where the fan reading is not
  460. * relevant. It is used to provide 0 as fan reading when the fan is
  461. * not supposed to run
  462. */
  463. static int trust_fan_readings(struct adm1031_data *data, int chan)
  464. {
  465. int res = 0;
  466. if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
  467. switch (data->conf1 & 0x60) {
  468. case 0x00:
  469. /*
  470. * remote temp1 controls fan1,
  471. * remote temp2 controls fan2
  472. */
  473. res = data->temp[chan+1] >=
  474. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
  475. break;
  476. case 0x20: /* remote temp1 controls both fans */
  477. res =
  478. data->temp[1] >=
  479. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
  480. break;
  481. case 0x40: /* remote temp2 controls both fans */
  482. res =
  483. data->temp[2] >=
  484. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
  485. break;
  486. case 0x60: /* max controls both fans */
  487. res =
  488. data->temp[0] >=
  489. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
  490. || data->temp[1] >=
  491. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
  492. || (data->chip_type == adm1031
  493. && data->temp[2] >=
  494. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
  495. break;
  496. }
  497. } else {
  498. res = data->pwm[chan] > 0;
  499. }
  500. return res;
  501. }
  502. static ssize_t show_fan(struct device *dev,
  503. struct device_attribute *attr, char *buf)
  504. {
  505. int nr = to_sensor_dev_attr(attr)->index;
  506. struct adm1031_data *data = adm1031_update_device(dev);
  507. int value;
  508. value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
  509. FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
  510. return sprintf(buf, "%d\n", value);
  511. }
  512. static ssize_t show_fan_div(struct device *dev,
  513. struct device_attribute *attr, char *buf)
  514. {
  515. int nr = to_sensor_dev_attr(attr)->index;
  516. struct adm1031_data *data = adm1031_update_device(dev);
  517. return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
  518. }
  519. static ssize_t show_fan_min(struct device *dev,
  520. struct device_attribute *attr, char *buf)
  521. {
  522. int nr = to_sensor_dev_attr(attr)->index;
  523. struct adm1031_data *data = adm1031_update_device(dev);
  524. return sprintf(buf, "%d\n",
  525. FAN_FROM_REG(data->fan_min[nr],
  526. FAN_DIV_FROM_REG(data->fan_div[nr])));
  527. }
  528. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  529. const char *buf, size_t count)
  530. {
  531. struct adm1031_data *data = dev_get_drvdata(dev);
  532. struct i2c_client *client = data->client;
  533. int nr = to_sensor_dev_attr(attr)->index;
  534. long val;
  535. int ret;
  536. ret = kstrtol(buf, 10, &val);
  537. if (ret)
  538. return ret;
  539. mutex_lock(&data->update_lock);
  540. if (val) {
  541. data->fan_min[nr] =
  542. FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
  543. } else {
  544. data->fan_min[nr] = 0xff;
  545. }
  546. adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
  547. mutex_unlock(&data->update_lock);
  548. return count;
  549. }
  550. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  551. const char *buf, size_t count)
  552. {
  553. struct adm1031_data *data = dev_get_drvdata(dev);
  554. struct i2c_client *client = data->client;
  555. int nr = to_sensor_dev_attr(attr)->index;
  556. long val;
  557. u8 tmp;
  558. int old_div;
  559. int new_min;
  560. int ret;
  561. ret = kstrtol(buf, 10, &val);
  562. if (ret)
  563. return ret;
  564. tmp = val == 8 ? 0xc0 :
  565. val == 4 ? 0x80 :
  566. val == 2 ? 0x40 :
  567. val == 1 ? 0x00 :
  568. 0xff;
  569. if (tmp == 0xff)
  570. return -EINVAL;
  571. mutex_lock(&data->update_lock);
  572. /* Get fresh readings */
  573. data->fan_div[nr] = adm1031_read_value(client,
  574. ADM1031_REG_FAN_DIV(nr));
  575. data->fan_min[nr] = adm1031_read_value(client,
  576. ADM1031_REG_FAN_MIN(nr));
  577. /* Write the new clock divider and fan min */
  578. old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
  579. data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
  580. new_min = data->fan_min[nr] * old_div / val;
  581. data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
  582. adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
  583. data->fan_div[nr]);
  584. adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
  585. data->fan_min[nr]);
  586. /* Invalidate the cache: fan speed is no longer valid */
  587. data->valid = 0;
  588. mutex_unlock(&data->update_lock);
  589. return count;
  590. }
  591. #define fan_offset(offset) \
  592. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  593. show_fan, NULL, offset - 1); \
  594. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  595. show_fan_min, set_fan_min, offset - 1); \
  596. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  597. show_fan_div, set_fan_div, offset - 1)
  598. fan_offset(1);
  599. fan_offset(2);
  600. /* Temps */
  601. static ssize_t show_temp(struct device *dev,
  602. struct device_attribute *attr, char *buf)
  603. {
  604. int nr = to_sensor_dev_attr(attr)->index;
  605. struct adm1031_data *data = adm1031_update_device(dev);
  606. int ext;
  607. ext = nr == 0 ?
  608. ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
  609. (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
  610. return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
  611. }
  612. static ssize_t show_temp_offset(struct device *dev,
  613. struct device_attribute *attr, char *buf)
  614. {
  615. int nr = to_sensor_dev_attr(attr)->index;
  616. struct adm1031_data *data = adm1031_update_device(dev);
  617. return sprintf(buf, "%d\n",
  618. TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
  619. }
  620. static ssize_t show_temp_min(struct device *dev,
  621. struct device_attribute *attr, char *buf)
  622. {
  623. int nr = to_sensor_dev_attr(attr)->index;
  624. struct adm1031_data *data = adm1031_update_device(dev);
  625. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
  626. }
  627. static ssize_t show_temp_max(struct device *dev,
  628. struct device_attribute *attr, char *buf)
  629. {
  630. int nr = to_sensor_dev_attr(attr)->index;
  631. struct adm1031_data *data = adm1031_update_device(dev);
  632. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
  633. }
  634. static ssize_t show_temp_crit(struct device *dev,
  635. struct device_attribute *attr, char *buf)
  636. {
  637. int nr = to_sensor_dev_attr(attr)->index;
  638. struct adm1031_data *data = adm1031_update_device(dev);
  639. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
  640. }
  641. static ssize_t set_temp_offset(struct device *dev,
  642. struct device_attribute *attr, const char *buf,
  643. size_t count)
  644. {
  645. struct adm1031_data *data = dev_get_drvdata(dev);
  646. struct i2c_client *client = data->client;
  647. int nr = to_sensor_dev_attr(attr)->index;
  648. long val;
  649. int ret;
  650. ret = kstrtol(buf, 10, &val);
  651. if (ret)
  652. return ret;
  653. val = clamp_val(val, -15000, 15000);
  654. mutex_lock(&data->update_lock);
  655. data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
  656. adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
  657. data->temp_offset[nr]);
  658. mutex_unlock(&data->update_lock);
  659. return count;
  660. }
  661. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  662. const char *buf, size_t count)
  663. {
  664. struct adm1031_data *data = dev_get_drvdata(dev);
  665. struct i2c_client *client = data->client;
  666. int nr = to_sensor_dev_attr(attr)->index;
  667. long val;
  668. int ret;
  669. ret = kstrtol(buf, 10, &val);
  670. if (ret)
  671. return ret;
  672. val = clamp_val(val, -55000, 127000);
  673. mutex_lock(&data->update_lock);
  674. data->temp_min[nr] = TEMP_TO_REG(val);
  675. adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
  676. data->temp_min[nr]);
  677. mutex_unlock(&data->update_lock);
  678. return count;
  679. }
  680. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  681. const char *buf, size_t count)
  682. {
  683. struct adm1031_data *data = dev_get_drvdata(dev);
  684. struct i2c_client *client = data->client;
  685. int nr = to_sensor_dev_attr(attr)->index;
  686. long val;
  687. int ret;
  688. ret = kstrtol(buf, 10, &val);
  689. if (ret)
  690. return ret;
  691. val = clamp_val(val, -55000, 127000);
  692. mutex_lock(&data->update_lock);
  693. data->temp_max[nr] = TEMP_TO_REG(val);
  694. adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
  695. data->temp_max[nr]);
  696. mutex_unlock(&data->update_lock);
  697. return count;
  698. }
  699. static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
  700. const char *buf, size_t count)
  701. {
  702. struct adm1031_data *data = dev_get_drvdata(dev);
  703. struct i2c_client *client = data->client;
  704. int nr = to_sensor_dev_attr(attr)->index;
  705. long val;
  706. int ret;
  707. ret = kstrtol(buf, 10, &val);
  708. if (ret)
  709. return ret;
  710. val = clamp_val(val, -55000, 127000);
  711. mutex_lock(&data->update_lock);
  712. data->temp_crit[nr] = TEMP_TO_REG(val);
  713. adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
  714. data->temp_crit[nr]);
  715. mutex_unlock(&data->update_lock);
  716. return count;
  717. }
  718. #define temp_reg(offset) \
  719. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  720. show_temp, NULL, offset - 1); \
  721. static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
  722. show_temp_offset, set_temp_offset, offset - 1); \
  723. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  724. show_temp_min, set_temp_min, offset - 1); \
  725. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  726. show_temp_max, set_temp_max, offset - 1); \
  727. static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
  728. show_temp_crit, set_temp_crit, offset - 1)
  729. temp_reg(1);
  730. temp_reg(2);
  731. temp_reg(3);
  732. /* Alarms */
  733. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  734. char *buf)
  735. {
  736. struct adm1031_data *data = adm1031_update_device(dev);
  737. return sprintf(buf, "%d\n", data->alarm);
  738. }
  739. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  740. static ssize_t show_alarm(struct device *dev,
  741. struct device_attribute *attr, char *buf)
  742. {
  743. int bitnr = to_sensor_dev_attr(attr)->index;
  744. struct adm1031_data *data = adm1031_update_device(dev);
  745. return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
  746. }
  747. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
  748. static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
  749. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
  750. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  751. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
  752. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
  753. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  754. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
  755. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
  756. static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
  757. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
  758. static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
  759. static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
  760. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
  761. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
  762. /* Update Interval */
  763. static const unsigned int update_intervals[] = {
  764. 16000, 8000, 4000, 2000, 1000, 500, 250, 125,
  765. };
  766. static ssize_t show_update_interval(struct device *dev,
  767. struct device_attribute *attr, char *buf)
  768. {
  769. struct adm1031_data *data = dev_get_drvdata(dev);
  770. return sprintf(buf, "%u\n", data->update_interval);
  771. }
  772. static ssize_t set_update_interval(struct device *dev,
  773. struct device_attribute *attr,
  774. const char *buf, size_t count)
  775. {
  776. struct adm1031_data *data = dev_get_drvdata(dev);
  777. struct i2c_client *client = data->client;
  778. unsigned long val;
  779. int i, err;
  780. u8 reg;
  781. err = kstrtoul(buf, 10, &val);
  782. if (err)
  783. return err;
  784. /*
  785. * Find the nearest update interval from the table.
  786. * Use it to determine the matching update rate.
  787. */
  788. for (i = 0; i < ARRAY_SIZE(update_intervals) - 1; i++) {
  789. if (val >= update_intervals[i])
  790. break;
  791. }
  792. /* if not found, we point to the last entry (lowest update interval) */
  793. /* set the new update rate while preserving other settings */
  794. reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
  795. reg &= ~ADM1031_UPDATE_RATE_MASK;
  796. reg |= i << ADM1031_UPDATE_RATE_SHIFT;
  797. adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg);
  798. mutex_lock(&data->update_lock);
  799. data->update_interval = update_intervals[i];
  800. mutex_unlock(&data->update_lock);
  801. return count;
  802. }
  803. static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
  804. set_update_interval);
  805. static struct attribute *adm1031_attributes[] = {
  806. &sensor_dev_attr_fan1_input.dev_attr.attr,
  807. &sensor_dev_attr_fan1_div.dev_attr.attr,
  808. &sensor_dev_attr_fan1_min.dev_attr.attr,
  809. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  810. &sensor_dev_attr_fan1_fault.dev_attr.attr,
  811. &sensor_dev_attr_pwm1.dev_attr.attr,
  812. &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
  813. &sensor_dev_attr_temp1_input.dev_attr.attr,
  814. &sensor_dev_attr_temp1_offset.dev_attr.attr,
  815. &sensor_dev_attr_temp1_min.dev_attr.attr,
  816. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  817. &sensor_dev_attr_temp1_max.dev_attr.attr,
  818. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  819. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  820. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  821. &sensor_dev_attr_temp2_input.dev_attr.attr,
  822. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  823. &sensor_dev_attr_temp2_min.dev_attr.attr,
  824. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  825. &sensor_dev_attr_temp2_max.dev_attr.attr,
  826. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  827. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  828. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  829. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  830. &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
  831. &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
  832. &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
  833. &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
  834. &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
  835. &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
  836. &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
  837. &dev_attr_update_interval.attr,
  838. &dev_attr_alarms.attr,
  839. NULL
  840. };
  841. static const struct attribute_group adm1031_group = {
  842. .attrs = adm1031_attributes,
  843. };
  844. static struct attribute *adm1031_attributes_opt[] = {
  845. &sensor_dev_attr_fan2_input.dev_attr.attr,
  846. &sensor_dev_attr_fan2_div.dev_attr.attr,
  847. &sensor_dev_attr_fan2_min.dev_attr.attr,
  848. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  849. &sensor_dev_attr_fan2_fault.dev_attr.attr,
  850. &sensor_dev_attr_pwm2.dev_attr.attr,
  851. &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
  852. &sensor_dev_attr_temp3_input.dev_attr.attr,
  853. &sensor_dev_attr_temp3_offset.dev_attr.attr,
  854. &sensor_dev_attr_temp3_min.dev_attr.attr,
  855. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  856. &sensor_dev_attr_temp3_max.dev_attr.attr,
  857. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  858. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  859. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  860. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  861. &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
  862. &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
  863. &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
  864. &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
  865. NULL
  866. };
  867. static const struct attribute_group adm1031_group_opt = {
  868. .attrs = adm1031_attributes_opt,
  869. };
  870. /* Return 0 if detection is successful, -ENODEV otherwise */
  871. static int adm1031_detect(struct i2c_client *client,
  872. struct i2c_board_info *info)
  873. {
  874. struct i2c_adapter *adapter = client->adapter;
  875. const char *name;
  876. int id, co;
  877. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  878. return -ENODEV;
  879. id = i2c_smbus_read_byte_data(client, 0x3d);
  880. co = i2c_smbus_read_byte_data(client, 0x3e);
  881. if (!((id == 0x31 || id == 0x30) && co == 0x41))
  882. return -ENODEV;
  883. name = (id == 0x30) ? "adm1030" : "adm1031";
  884. strlcpy(info->type, name, I2C_NAME_SIZE);
  885. return 0;
  886. }
  887. static void adm1031_init_client(struct i2c_client *client)
  888. {
  889. unsigned int read_val;
  890. unsigned int mask;
  891. int i;
  892. struct adm1031_data *data = i2c_get_clientdata(client);
  893. mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
  894. if (data->chip_type == adm1031) {
  895. mask |= (ADM1031_CONF2_PWM2_ENABLE |
  896. ADM1031_CONF2_TACH2_ENABLE);
  897. }
  898. /* Initialize the ADM1031 chip (enables fan speed reading ) */
  899. read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
  900. if ((read_val | mask) != read_val)
  901. adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
  902. read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
  903. if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
  904. adm1031_write_value(client, ADM1031_REG_CONF1,
  905. read_val | ADM1031_CONF1_MONITOR_ENABLE);
  906. }
  907. /* Read the chip's update rate */
  908. mask = ADM1031_UPDATE_RATE_MASK;
  909. read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
  910. i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT;
  911. /* Save it as update interval */
  912. data->update_interval = update_intervals[i];
  913. }
  914. static int adm1031_probe(struct i2c_client *client,
  915. const struct i2c_device_id *id)
  916. {
  917. struct device *dev = &client->dev;
  918. struct device *hwmon_dev;
  919. struct adm1031_data *data;
  920. data = devm_kzalloc(dev, sizeof(struct adm1031_data), GFP_KERNEL);
  921. if (!data)
  922. return -ENOMEM;
  923. i2c_set_clientdata(client, data);
  924. data->client = client;
  925. data->chip_type = id->driver_data;
  926. mutex_init(&data->update_lock);
  927. if (data->chip_type == adm1030)
  928. data->chan_select_table = &auto_channel_select_table_adm1030;
  929. else
  930. data->chan_select_table = &auto_channel_select_table_adm1031;
  931. /* Initialize the ADM1031 chip */
  932. adm1031_init_client(client);
  933. /* sysfs hooks */
  934. data->groups[0] = &adm1031_group;
  935. if (data->chip_type == adm1031)
  936. data->groups[1] = &adm1031_group_opt;
  937. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  938. data, data->groups);
  939. return PTR_ERR_OR_ZERO(hwmon_dev);
  940. }
  941. static const struct i2c_device_id adm1031_id[] = {
  942. { "adm1030", adm1030 },
  943. { "adm1031", adm1031 },
  944. { }
  945. };
  946. MODULE_DEVICE_TABLE(i2c, adm1031_id);
  947. static struct i2c_driver adm1031_driver = {
  948. .class = I2C_CLASS_HWMON,
  949. .driver = {
  950. .name = "adm1031",
  951. },
  952. .probe = adm1031_probe,
  953. .id_table = adm1031_id,
  954. .detect = adm1031_detect,
  955. .address_list = normal_i2c,
  956. };
  957. module_i2c_driver(adm1031_driver);
  958. MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
  959. MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
  960. MODULE_LICENSE("GPL");