adm1031.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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/err.h>
  27. #include <linux/mutex.h>
  28. /* Following macros takes channel parameter starting from 0 to 2 */
  29. #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
  30. #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
  31. #define ADM1031_REG_PWM (0x22)
  32. #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
  33. #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4*(nr))
  34. #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4*(nr))
  35. #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4*(nr))
  36. #define ADM1031_REG_TEMP(nr) (0xa + (nr))
  37. #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
  38. #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
  39. #define ADM1031_REG_CONF1 0x0
  40. #define ADM1031_REG_CONF2 0x1
  41. #define ADM1031_REG_EXT_TEMP 0x6
  42. #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
  43. #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
  44. #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
  45. #define ADM1031_CONF2_PWM1_ENABLE 0x01
  46. #define ADM1031_CONF2_PWM2_ENABLE 0x02
  47. #define ADM1031_CONF2_TACH1_ENABLE 0x04
  48. #define ADM1031_CONF2_TACH2_ENABLE 0x08
  49. #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
  50. /* Addresses to scan */
  51. static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  52. /* Insmod parameters */
  53. I2C_CLIENT_INSMOD_2(adm1030, adm1031);
  54. typedef u8 auto_chan_table_t[8][2];
  55. /* Each client has this additional data */
  56. struct adm1031_data {
  57. struct i2c_client client;
  58. struct class_device *class_dev;
  59. struct mutex update_lock;
  60. int chip_type;
  61. char valid; /* !=0 if following fields are valid */
  62. unsigned long last_updated; /* In jiffies */
  63. /* The chan_select_table contains the possible configurations for
  64. * auto fan control.
  65. */
  66. auto_chan_table_t *chan_select_table;
  67. u16 alarm;
  68. u8 conf1;
  69. u8 conf2;
  70. u8 fan[2];
  71. u8 fan_div[2];
  72. u8 fan_min[2];
  73. u8 pwm[2];
  74. u8 old_pwm[2];
  75. s8 temp[3];
  76. u8 ext_temp[3];
  77. u8 auto_temp[3];
  78. u8 auto_temp_min[3];
  79. u8 auto_temp_off[3];
  80. u8 auto_temp_max[3];
  81. s8 temp_min[3];
  82. s8 temp_max[3];
  83. s8 temp_crit[3];
  84. };
  85. static int adm1031_attach_adapter(struct i2c_adapter *adapter);
  86. static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind);
  87. static void adm1031_init_client(struct i2c_client *client);
  88. static int adm1031_detach_client(struct i2c_client *client);
  89. static struct adm1031_data *adm1031_update_device(struct device *dev);
  90. /* This is the driver that will be inserted */
  91. static struct i2c_driver adm1031_driver = {
  92. .driver = {
  93. .name = "adm1031",
  94. },
  95. .attach_adapter = adm1031_attach_adapter,
  96. .detach_client = adm1031_detach_client,
  97. };
  98. static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
  99. {
  100. return i2c_smbus_read_byte_data(client, reg);
  101. }
  102. static inline int
  103. adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
  104. {
  105. return i2c_smbus_write_byte_data(client, reg, value);
  106. }
  107. #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
  108. ((val + 500) / 1000)))
  109. #define TEMP_FROM_REG(val) ((val) * 1000)
  110. #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
  111. #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
  112. static int FAN_TO_REG(int reg, int div)
  113. {
  114. int tmp;
  115. tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
  116. return tmp > 255 ? 255 : tmp;
  117. }
  118. #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
  119. #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
  120. #define PWM_FROM_REG(val) ((val) << 4)
  121. #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
  122. #define FAN_CHAN_TO_REG(val, reg) \
  123. (((reg) & 0x1F) | (((val) << 5) & 0xe0))
  124. #define AUTO_TEMP_MIN_TO_REG(val, reg) \
  125. ((((val)/500) & 0xf8)|((reg) & 0x7))
  126. #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
  127. #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
  128. #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
  129. #define AUTO_TEMP_OFF_FROM_REG(reg) \
  130. (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
  131. #define AUTO_TEMP_MAX_FROM_REG(reg) \
  132. (AUTO_TEMP_RANGE_FROM_REG(reg) + \
  133. AUTO_TEMP_MIN_FROM_REG(reg))
  134. static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
  135. {
  136. int ret;
  137. int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
  138. range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
  139. ret = ((reg & 0xf8) |
  140. (range < 10000 ? 0 :
  141. range < 20000 ? 1 :
  142. range < 40000 ? 2 : range < 80000 ? 3 : 4));
  143. return ret;
  144. }
  145. /* FAN auto control */
  146. #define GET_FAN_AUTO_BITFIELD(data, idx) \
  147. (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
  148. /* The tables below contains the possible values for the auto fan
  149. * control bitfields. the index in the table is the register value.
  150. * MSb is the auto fan control enable bit, so the four first entries
  151. * in the table disables auto fan control when both bitfields are zero.
  152. */
  153. static auto_chan_table_t auto_channel_select_table_adm1031 = {
  154. {0, 0}, {0, 0}, {0, 0}, {0, 0},
  155. {2 /*0b010 */ , 4 /*0b100 */ },
  156. {2 /*0b010 */ , 2 /*0b010 */ },
  157. {4 /*0b100 */ , 4 /*0b100 */ },
  158. {7 /*0b111 */ , 7 /*0b111 */ },
  159. };
  160. static auto_chan_table_t auto_channel_select_table_adm1030 = {
  161. {0, 0}, {0, 0}, {0, 0}, {0, 0},
  162. {2 /*0b10 */ , 0},
  163. {0xff /*invalid */ , 0},
  164. {0xff /*invalid */ , 0},
  165. {3 /*0b11 */ , 0},
  166. };
  167. /* That function checks if a bitfield is valid and returns the other bitfield
  168. * nearest match if no exact match where found.
  169. */
  170. static int
  171. get_fan_auto_nearest(struct adm1031_data *data,
  172. int chan, u8 val, u8 reg, u8 * new_reg)
  173. {
  174. int i;
  175. int first_match = -1, exact_match = -1;
  176. u8 other_reg_val =
  177. (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
  178. if (val == 0) {
  179. *new_reg = 0;
  180. return 0;
  181. }
  182. for (i = 0; i < 8; i++) {
  183. if ((val == (*data->chan_select_table)[i][chan]) &&
  184. ((*data->chan_select_table)[i][chan ? 0 : 1] ==
  185. other_reg_val)) {
  186. /* We found an exact match */
  187. exact_match = i;
  188. break;
  189. } else if (val == (*data->chan_select_table)[i][chan] &&
  190. first_match == -1) {
  191. /* Save the first match in case of an exact match has not been
  192. * found
  193. */
  194. first_match = i;
  195. }
  196. }
  197. if (exact_match >= 0) {
  198. *new_reg = exact_match;
  199. } else if (first_match >= 0) {
  200. *new_reg = first_match;
  201. } else {
  202. return -EINVAL;
  203. }
  204. return 0;
  205. }
  206. static ssize_t show_fan_auto_channel(struct device *dev, char *buf, int nr)
  207. {
  208. struct adm1031_data *data = adm1031_update_device(dev);
  209. return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
  210. }
  211. static ssize_t
  212. set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
  213. {
  214. struct i2c_client *client = to_i2c_client(dev);
  215. struct adm1031_data *data = i2c_get_clientdata(client);
  216. int val = simple_strtol(buf, NULL, 10);
  217. u8 reg;
  218. int ret;
  219. u8 old_fan_mode;
  220. old_fan_mode = data->conf1;
  221. mutex_lock(&data->update_lock);
  222. if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
  223. mutex_unlock(&data->update_lock);
  224. return ret;
  225. }
  226. if (((data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1)) & ADM1031_CONF1_AUTO_MODE) ^
  227. (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
  228. if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
  229. /* Switch to Auto Fan Mode
  230. * Save PWM registers
  231. * Set PWM registers to 33% Both */
  232. data->old_pwm[0] = data->pwm[0];
  233. data->old_pwm[1] = data->pwm[1];
  234. adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
  235. } else {
  236. /* Switch to Manual Mode */
  237. data->pwm[0] = data->old_pwm[0];
  238. data->pwm[1] = data->old_pwm[1];
  239. /* Restore PWM registers */
  240. adm1031_write_value(client, ADM1031_REG_PWM,
  241. data->pwm[0] | (data->pwm[1] << 4));
  242. }
  243. }
  244. data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
  245. adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
  246. mutex_unlock(&data->update_lock);
  247. return count;
  248. }
  249. #define fan_auto_channel_offset(offset) \
  250. static ssize_t show_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  251. { \
  252. return show_fan_auto_channel(dev, buf, offset - 1); \
  253. } \
  254. static ssize_t set_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, \
  255. const char *buf, size_t count) \
  256. { \
  257. return set_fan_auto_channel(dev, buf, count, offset - 1); \
  258. } \
  259. static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR, \
  260. show_fan_auto_channel_##offset, \
  261. set_fan_auto_channel_##offset)
  262. fan_auto_channel_offset(1);
  263. fan_auto_channel_offset(2);
  264. /* Auto Temps */
  265. static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr)
  266. {
  267. struct adm1031_data *data = adm1031_update_device(dev);
  268. return sprintf(buf, "%d\n",
  269. AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
  270. }
  271. static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr)
  272. {
  273. struct adm1031_data *data = adm1031_update_device(dev);
  274. return sprintf(buf, "%d\n",
  275. AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
  276. }
  277. static ssize_t
  278. set_auto_temp_min(struct device *dev, const char *buf, size_t count, int nr)
  279. {
  280. struct i2c_client *client = to_i2c_client(dev);
  281. struct adm1031_data *data = i2c_get_clientdata(client);
  282. int val = simple_strtol(buf, NULL, 10);
  283. mutex_lock(&data->update_lock);
  284. data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
  285. adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
  286. data->auto_temp[nr]);
  287. mutex_unlock(&data->update_lock);
  288. return count;
  289. }
  290. static ssize_t show_auto_temp_max(struct device *dev, char *buf, int nr)
  291. {
  292. struct adm1031_data *data = adm1031_update_device(dev);
  293. return sprintf(buf, "%d\n",
  294. AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
  295. }
  296. static ssize_t
  297. set_auto_temp_max(struct device *dev, const char *buf, size_t count, int nr)
  298. {
  299. struct i2c_client *client = to_i2c_client(dev);
  300. struct adm1031_data *data = i2c_get_clientdata(client);
  301. int val = simple_strtol(buf, NULL, 10);
  302. mutex_lock(&data->update_lock);
  303. data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
  304. adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
  305. data->temp_max[nr]);
  306. mutex_unlock(&data->update_lock);
  307. return count;
  308. }
  309. #define auto_temp_reg(offset) \
  310. static ssize_t show_auto_temp_##offset##_off (struct device *dev, struct device_attribute *attr, char *buf) \
  311. { \
  312. return show_auto_temp_off(dev, buf, offset - 1); \
  313. } \
  314. static ssize_t show_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
  315. { \
  316. return show_auto_temp_min(dev, buf, offset - 1); \
  317. } \
  318. static ssize_t show_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
  319. { \
  320. return show_auto_temp_max(dev, buf, offset - 1); \
  321. } \
  322. static ssize_t set_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
  323. const char *buf, size_t count) \
  324. { \
  325. return set_auto_temp_min(dev, buf, count, offset - 1); \
  326. } \
  327. static ssize_t set_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
  328. const char *buf, size_t count) \
  329. { \
  330. return set_auto_temp_max(dev, buf, count, offset - 1); \
  331. } \
  332. static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
  333. show_auto_temp_##offset##_off, NULL); \
  334. static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
  335. show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\
  336. static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
  337. show_auto_temp_##offset##_max, set_auto_temp_##offset##_max)
  338. auto_temp_reg(1);
  339. auto_temp_reg(2);
  340. auto_temp_reg(3);
  341. /* pwm */
  342. static ssize_t show_pwm(struct device *dev, char *buf, int nr)
  343. {
  344. struct adm1031_data *data = adm1031_update_device(dev);
  345. return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
  346. }
  347. static ssize_t
  348. set_pwm(struct device *dev, const char *buf, size_t count, int nr)
  349. {
  350. struct i2c_client *client = to_i2c_client(dev);
  351. struct adm1031_data *data = i2c_get_clientdata(client);
  352. int val = simple_strtol(buf, NULL, 10);
  353. int reg;
  354. mutex_lock(&data->update_lock);
  355. if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
  356. (((val>>4) & 0xf) != 5)) {
  357. /* In automatic mode, the only PWM accepted is 33% */
  358. mutex_unlock(&data->update_lock);
  359. return -EINVAL;
  360. }
  361. data->pwm[nr] = PWM_TO_REG(val);
  362. reg = adm1031_read_value(client, ADM1031_REG_PWM);
  363. adm1031_write_value(client, ADM1031_REG_PWM,
  364. nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
  365. : (data->pwm[nr] & 0xf) | (reg & 0xf0));
  366. mutex_unlock(&data->update_lock);
  367. return count;
  368. }
  369. #define pwm_reg(offset) \
  370. static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  371. { \
  372. return show_pwm(dev, buf, offset - 1); \
  373. } \
  374. static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \
  375. const char *buf, size_t count) \
  376. { \
  377. return set_pwm(dev, buf, count, offset - 1); \
  378. } \
  379. static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
  380. show_pwm_##offset, set_pwm_##offset)
  381. pwm_reg(1);
  382. pwm_reg(2);
  383. /* Fans */
  384. /*
  385. * That function checks the cases where the fan reading is not
  386. * relevant. It is used to provide 0 as fan reading when the fan is
  387. * not supposed to run
  388. */
  389. static int trust_fan_readings(struct adm1031_data *data, int chan)
  390. {
  391. int res = 0;
  392. if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
  393. switch (data->conf1 & 0x60) {
  394. case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
  395. res = data->temp[chan+1] >=
  396. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
  397. break;
  398. case 0x20: /* remote temp1 controls both fans */
  399. res =
  400. data->temp[1] >=
  401. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
  402. break;
  403. case 0x40: /* remote temp2 controls both fans */
  404. res =
  405. data->temp[2] >=
  406. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
  407. break;
  408. case 0x60: /* max controls both fans */
  409. res =
  410. data->temp[0] >=
  411. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
  412. || data->temp[1] >=
  413. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
  414. || (data->chip_type == adm1031
  415. && data->temp[2] >=
  416. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
  417. break;
  418. }
  419. } else {
  420. res = data->pwm[chan] > 0;
  421. }
  422. return res;
  423. }
  424. static ssize_t show_fan(struct device *dev, char *buf, int nr)
  425. {
  426. struct adm1031_data *data = adm1031_update_device(dev);
  427. int value;
  428. value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
  429. FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
  430. return sprintf(buf, "%d\n", value);
  431. }
  432. static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
  433. {
  434. struct adm1031_data *data = adm1031_update_device(dev);
  435. return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
  436. }
  437. static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
  438. {
  439. struct adm1031_data *data = adm1031_update_device(dev);
  440. return sprintf(buf, "%d\n",
  441. FAN_FROM_REG(data->fan_min[nr],
  442. FAN_DIV_FROM_REG(data->fan_div[nr])));
  443. }
  444. static ssize_t
  445. set_fan_min(struct device *dev, const char *buf, size_t count, int nr)
  446. {
  447. struct i2c_client *client = to_i2c_client(dev);
  448. struct adm1031_data *data = i2c_get_clientdata(client);
  449. int val = simple_strtol(buf, NULL, 10);
  450. mutex_lock(&data->update_lock);
  451. if (val) {
  452. data->fan_min[nr] =
  453. FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
  454. } else {
  455. data->fan_min[nr] = 0xff;
  456. }
  457. adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
  458. mutex_unlock(&data->update_lock);
  459. return count;
  460. }
  461. static ssize_t
  462. set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
  463. {
  464. struct i2c_client *client = to_i2c_client(dev);
  465. struct adm1031_data *data = i2c_get_clientdata(client);
  466. int val = simple_strtol(buf, NULL, 10);
  467. u8 tmp;
  468. int old_div;
  469. int new_min;
  470. tmp = val == 8 ? 0xc0 :
  471. val == 4 ? 0x80 :
  472. val == 2 ? 0x40 :
  473. val == 1 ? 0x00 :
  474. 0xff;
  475. if (tmp == 0xff)
  476. return -EINVAL;
  477. mutex_lock(&data->update_lock);
  478. old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
  479. data->fan_div[nr] = (tmp & 0xC0) | (0x3f & data->fan_div[nr]);
  480. new_min = data->fan_min[nr] * old_div /
  481. FAN_DIV_FROM_REG(data->fan_div[nr]);
  482. data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
  483. data->fan[nr] = data->fan[nr] * old_div /
  484. FAN_DIV_FROM_REG(data->fan_div[nr]);
  485. adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
  486. data->fan_div[nr]);
  487. adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
  488. data->fan_min[nr]);
  489. mutex_unlock(&data->update_lock);
  490. return count;
  491. }
  492. #define fan_offset(offset) \
  493. static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  494. { \
  495. return show_fan(dev, buf, offset - 1); \
  496. } \
  497. static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
  498. { \
  499. return show_fan_min(dev, buf, offset - 1); \
  500. } \
  501. static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
  502. { \
  503. return show_fan_div(dev, buf, offset - 1); \
  504. } \
  505. static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
  506. const char *buf, size_t count) \
  507. { \
  508. return set_fan_min(dev, buf, count, offset - 1); \
  509. } \
  510. static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, \
  511. const char *buf, size_t count) \
  512. { \
  513. return set_fan_div(dev, buf, count, offset - 1); \
  514. } \
  515. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
  516. NULL); \
  517. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  518. show_fan_##offset##_min, set_fan_##offset##_min); \
  519. static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  520. show_fan_##offset##_div, set_fan_##offset##_div); \
  521. static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR, \
  522. show_pwm_##offset, set_pwm_##offset)
  523. fan_offset(1);
  524. fan_offset(2);
  525. /* Temps */
  526. static ssize_t show_temp(struct device *dev, char *buf, int nr)
  527. {
  528. struct adm1031_data *data = adm1031_update_device(dev);
  529. int ext;
  530. ext = nr == 0 ?
  531. ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
  532. (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
  533. return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
  534. }
  535. static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
  536. {
  537. struct adm1031_data *data = adm1031_update_device(dev);
  538. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
  539. }
  540. static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
  541. {
  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, char *buf, int nr)
  546. {
  547. struct adm1031_data *data = adm1031_update_device(dev);
  548. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
  549. }
  550. static ssize_t
  551. set_temp_min(struct device *dev, const char *buf, size_t count, int nr)
  552. {
  553. struct i2c_client *client = to_i2c_client(dev);
  554. struct adm1031_data *data = i2c_get_clientdata(client);
  555. int val;
  556. val = simple_strtol(buf, NULL, 10);
  557. val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
  558. mutex_lock(&data->update_lock);
  559. data->temp_min[nr] = TEMP_TO_REG(val);
  560. adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
  561. data->temp_min[nr]);
  562. mutex_unlock(&data->update_lock);
  563. return count;
  564. }
  565. static ssize_t
  566. set_temp_max(struct device *dev, const char *buf, size_t count, int nr)
  567. {
  568. struct i2c_client *client = to_i2c_client(dev);
  569. struct adm1031_data *data = i2c_get_clientdata(client);
  570. int val;
  571. val = simple_strtol(buf, NULL, 10);
  572. val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
  573. mutex_lock(&data->update_lock);
  574. data->temp_max[nr] = TEMP_TO_REG(val);
  575. adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
  576. data->temp_max[nr]);
  577. mutex_unlock(&data->update_lock);
  578. return count;
  579. }
  580. static ssize_t
  581. set_temp_crit(struct device *dev, const char *buf, size_t count, int nr)
  582. {
  583. struct i2c_client *client = to_i2c_client(dev);
  584. struct adm1031_data *data = i2c_get_clientdata(client);
  585. int val;
  586. val = simple_strtol(buf, NULL, 10);
  587. val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
  588. mutex_lock(&data->update_lock);
  589. data->temp_crit[nr] = TEMP_TO_REG(val);
  590. adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
  591. data->temp_crit[nr]);
  592. mutex_unlock(&data->update_lock);
  593. return count;
  594. }
  595. #define temp_reg(offset) \
  596. static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  597. { \
  598. return show_temp(dev, buf, offset - 1); \
  599. } \
  600. static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
  601. { \
  602. return show_temp_min(dev, buf, offset - 1); \
  603. } \
  604. static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
  605. { \
  606. return show_temp_max(dev, buf, offset - 1); \
  607. } \
  608. static ssize_t show_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, char *buf) \
  609. { \
  610. return show_temp_crit(dev, buf, offset - 1); \
  611. } \
  612. static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
  613. const char *buf, size_t count) \
  614. { \
  615. return set_temp_min(dev, buf, count, offset - 1); \
  616. } \
  617. static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
  618. const char *buf, size_t count) \
  619. { \
  620. return set_temp_max(dev, buf, count, offset - 1); \
  621. } \
  622. static ssize_t set_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, \
  623. const char *buf, size_t count) \
  624. { \
  625. return set_temp_crit(dev, buf, count, offset - 1); \
  626. } \
  627. static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
  628. NULL); \
  629. static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  630. show_temp_##offset##_min, set_temp_##offset##_min); \
  631. static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  632. show_temp_##offset##_max, set_temp_##offset##_max); \
  633. static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
  634. show_temp_##offset##_crit, set_temp_##offset##_crit)
  635. temp_reg(1);
  636. temp_reg(2);
  637. temp_reg(3);
  638. /* Alarms */
  639. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  640. {
  641. struct adm1031_data *data = adm1031_update_device(dev);
  642. return sprintf(buf, "%d\n", data->alarm);
  643. }
  644. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  645. static int adm1031_attach_adapter(struct i2c_adapter *adapter)
  646. {
  647. if (!(adapter->class & I2C_CLASS_HWMON))
  648. return 0;
  649. return i2c_probe(adapter, &addr_data, adm1031_detect);
  650. }
  651. /* This function is called by i2c_probe */
  652. static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
  653. {
  654. struct i2c_client *new_client;
  655. struct adm1031_data *data;
  656. int err = 0;
  657. const char *name = "";
  658. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  659. goto exit;
  660. if (!(data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL))) {
  661. err = -ENOMEM;
  662. goto exit;
  663. }
  664. new_client = &data->client;
  665. i2c_set_clientdata(new_client, data);
  666. new_client->addr = address;
  667. new_client->adapter = adapter;
  668. new_client->driver = &adm1031_driver;
  669. new_client->flags = 0;
  670. if (kind < 0) {
  671. int id, co;
  672. id = i2c_smbus_read_byte_data(new_client, 0x3d);
  673. co = i2c_smbus_read_byte_data(new_client, 0x3e);
  674. if (!((id == 0x31 || id == 0x30) && co == 0x41))
  675. goto exit_free;
  676. kind = (id == 0x30) ? adm1030 : adm1031;
  677. }
  678. if (kind <= 0)
  679. kind = adm1031;
  680. /* Given the detected chip type, set the chip name and the
  681. * auto fan control helper table. */
  682. if (kind == adm1030) {
  683. name = "adm1030";
  684. data->chan_select_table = &auto_channel_select_table_adm1030;
  685. } else if (kind == adm1031) {
  686. name = "adm1031";
  687. data->chan_select_table = &auto_channel_select_table_adm1031;
  688. }
  689. data->chip_type = kind;
  690. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  691. data->valid = 0;
  692. mutex_init(&data->update_lock);
  693. /* Tell the I2C layer a new client has arrived */
  694. if ((err = i2c_attach_client(new_client)))
  695. goto exit_free;
  696. /* Initialize the ADM1031 chip */
  697. adm1031_init_client(new_client);
  698. /* Register sysfs hooks */
  699. data->class_dev = hwmon_device_register(&new_client->dev);
  700. if (IS_ERR(data->class_dev)) {
  701. err = PTR_ERR(data->class_dev);
  702. goto exit_detach;
  703. }
  704. device_create_file(&new_client->dev, &dev_attr_fan1_input);
  705. device_create_file(&new_client->dev, &dev_attr_fan1_div);
  706. device_create_file(&new_client->dev, &dev_attr_fan1_min);
  707. device_create_file(&new_client->dev, &dev_attr_pwm1);
  708. device_create_file(&new_client->dev, &dev_attr_auto_fan1_channel);
  709. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  710. device_create_file(&new_client->dev, &dev_attr_temp1_min);
  711. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  712. device_create_file(&new_client->dev, &dev_attr_temp1_crit);
  713. device_create_file(&new_client->dev, &dev_attr_temp2_input);
  714. device_create_file(&new_client->dev, &dev_attr_temp2_min);
  715. device_create_file(&new_client->dev, &dev_attr_temp2_max);
  716. device_create_file(&new_client->dev, &dev_attr_temp2_crit);
  717. device_create_file(&new_client->dev, &dev_attr_auto_temp1_off);
  718. device_create_file(&new_client->dev, &dev_attr_auto_temp1_min);
  719. device_create_file(&new_client->dev, &dev_attr_auto_temp1_max);
  720. device_create_file(&new_client->dev, &dev_attr_auto_temp2_off);
  721. device_create_file(&new_client->dev, &dev_attr_auto_temp2_min);
  722. device_create_file(&new_client->dev, &dev_attr_auto_temp2_max);
  723. device_create_file(&new_client->dev, &dev_attr_auto_fan1_min_pwm);
  724. device_create_file(&new_client->dev, &dev_attr_alarms);
  725. if (kind == adm1031) {
  726. device_create_file(&new_client->dev, &dev_attr_fan2_input);
  727. device_create_file(&new_client->dev, &dev_attr_fan2_div);
  728. device_create_file(&new_client->dev, &dev_attr_fan2_min);
  729. device_create_file(&new_client->dev, &dev_attr_pwm2);
  730. device_create_file(&new_client->dev,
  731. &dev_attr_auto_fan2_channel);
  732. device_create_file(&new_client->dev, &dev_attr_temp3_input);
  733. device_create_file(&new_client->dev, &dev_attr_temp3_min);
  734. device_create_file(&new_client->dev, &dev_attr_temp3_max);
  735. device_create_file(&new_client->dev, &dev_attr_temp3_crit);
  736. device_create_file(&new_client->dev, &dev_attr_auto_temp3_off);
  737. device_create_file(&new_client->dev, &dev_attr_auto_temp3_min);
  738. device_create_file(&new_client->dev, &dev_attr_auto_temp3_max);
  739. device_create_file(&new_client->dev, &dev_attr_auto_fan2_min_pwm);
  740. }
  741. return 0;
  742. exit_detach:
  743. i2c_detach_client(new_client);
  744. exit_free:
  745. kfree(data);
  746. exit:
  747. return err;
  748. }
  749. static int adm1031_detach_client(struct i2c_client *client)
  750. {
  751. struct adm1031_data *data = i2c_get_clientdata(client);
  752. int ret;
  753. hwmon_device_unregister(data->class_dev);
  754. if ((ret = i2c_detach_client(client)) != 0) {
  755. return ret;
  756. }
  757. kfree(data);
  758. return 0;
  759. }
  760. static void adm1031_init_client(struct i2c_client *client)
  761. {
  762. unsigned int read_val;
  763. unsigned int mask;
  764. struct adm1031_data *data = i2c_get_clientdata(client);
  765. mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
  766. if (data->chip_type == adm1031) {
  767. mask |= (ADM1031_CONF2_PWM2_ENABLE |
  768. ADM1031_CONF2_TACH2_ENABLE);
  769. }
  770. /* Initialize the ADM1031 chip (enables fan speed reading ) */
  771. read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
  772. if ((read_val | mask) != read_val) {
  773. adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
  774. }
  775. read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
  776. if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
  777. adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
  778. ADM1031_CONF1_MONITOR_ENABLE);
  779. }
  780. }
  781. static struct adm1031_data *adm1031_update_device(struct device *dev)
  782. {
  783. struct i2c_client *client = to_i2c_client(dev);
  784. struct adm1031_data *data = i2c_get_clientdata(client);
  785. int chan;
  786. mutex_lock(&data->update_lock);
  787. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  788. || !data->valid) {
  789. dev_dbg(&client->dev, "Starting adm1031 update\n");
  790. for (chan = 0;
  791. chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
  792. u8 oldh, newh;
  793. oldh =
  794. adm1031_read_value(client, ADM1031_REG_TEMP(chan));
  795. data->ext_temp[chan] =
  796. adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
  797. newh =
  798. adm1031_read_value(client, ADM1031_REG_TEMP(chan));
  799. if (newh != oldh) {
  800. data->ext_temp[chan] =
  801. adm1031_read_value(client,
  802. ADM1031_REG_EXT_TEMP);
  803. #ifdef DEBUG
  804. oldh =
  805. adm1031_read_value(client,
  806. ADM1031_REG_TEMP(chan));
  807. /* oldh is actually newer */
  808. if (newh != oldh)
  809. dev_warn(&client->dev,
  810. "Remote temperature may be "
  811. "wrong.\n");
  812. #endif
  813. }
  814. data->temp[chan] = newh;
  815. data->temp_min[chan] =
  816. adm1031_read_value(client,
  817. ADM1031_REG_TEMP_MIN(chan));
  818. data->temp_max[chan] =
  819. adm1031_read_value(client,
  820. ADM1031_REG_TEMP_MAX(chan));
  821. data->temp_crit[chan] =
  822. adm1031_read_value(client,
  823. ADM1031_REG_TEMP_CRIT(chan));
  824. data->auto_temp[chan] =
  825. adm1031_read_value(client,
  826. ADM1031_REG_AUTO_TEMP(chan));
  827. }
  828. data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
  829. data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
  830. data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
  831. | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
  832. << 8);
  833. if (data->chip_type == adm1030) {
  834. data->alarm &= 0xc0ff;
  835. }
  836. for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
  837. data->fan_div[chan] =
  838. adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
  839. data->fan_min[chan] =
  840. adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
  841. data->fan[chan] =
  842. adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
  843. data->pwm[chan] =
  844. 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
  845. (4*chan));
  846. }
  847. data->last_updated = jiffies;
  848. data->valid = 1;
  849. }
  850. mutex_unlock(&data->update_lock);
  851. return data;
  852. }
  853. static int __init sensors_adm1031_init(void)
  854. {
  855. return i2c_add_driver(&adm1031_driver);
  856. }
  857. static void __exit sensors_adm1031_exit(void)
  858. {
  859. i2c_del_driver(&adm1031_driver);
  860. }
  861. MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
  862. MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
  863. MODULE_LICENSE("GPL");
  864. module_init(sensors_adm1031_init);
  865. module_exit(sensors_adm1031_exit);