adm1031.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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");