adm1031.c 34 KB

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