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. /* Following macros takes channel parameter starting from 0 to 2 */
  28. #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
  29. #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
  30. #define ADM1031_REG_PWM (0x22)
  31. #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
  32. #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4*(nr))
  33. #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4*(nr))
  34. #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4*(nr))
  35. #define ADM1031_REG_TEMP(nr) (0xa + (nr))
  36. #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
  37. #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
  38. #define ADM1031_REG_CONF1 0x0
  39. #define ADM1031_REG_CONF2 0x1
  40. #define ADM1031_REG_EXT_TEMP 0x6
  41. #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
  42. #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
  43. #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
  44. #define ADM1031_CONF2_PWM1_ENABLE 0x01
  45. #define ADM1031_CONF2_PWM2_ENABLE 0x02
  46. #define ADM1031_CONF2_TACH1_ENABLE 0x04
  47. #define ADM1031_CONF2_TACH2_ENABLE 0x08
  48. #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
  49. /* Addresses to scan */
  50. static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  51. /* Insmod parameters */
  52. I2C_CLIENT_INSMOD_2(adm1030, adm1031);
  53. typedef u8 auto_chan_table_t[8][2];
  54. /* Each client has this additional data */
  55. struct adm1031_data {
  56. struct i2c_client client;
  57. struct class_device *class_dev;
  58. struct semaphore update_lock;
  59. int chip_type;
  60. char valid; /* !=0 if following fields are valid */
  61. unsigned long last_updated; /* In jiffies */
  62. /* The chan_select_table contains the possible configurations for
  63. * auto fan control.
  64. */
  65. auto_chan_table_t *chan_select_table;
  66. u16 alarm;
  67. u8 conf1;
  68. u8 conf2;
  69. u8 fan[2];
  70. u8 fan_div[2];
  71. u8 fan_min[2];
  72. u8 pwm[2];
  73. u8 old_pwm[2];
  74. s8 temp[3];
  75. u8 ext_temp[3];
  76. u8 auto_temp[3];
  77. u8 auto_temp_min[3];
  78. u8 auto_temp_off[3];
  79. u8 auto_temp_max[3];
  80. s8 temp_min[3];
  81. s8 temp_max[3];
  82. s8 temp_crit[3];
  83. };
  84. static int adm1031_attach_adapter(struct i2c_adapter *adapter);
  85. static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind);
  86. static void adm1031_init_client(struct i2c_client *client);
  87. static int adm1031_detach_client(struct i2c_client *client);
  88. static struct adm1031_data *adm1031_update_device(struct device *dev);
  89. /* This is the driver that will be inserted */
  90. static struct i2c_driver adm1031_driver = {
  91. .owner = THIS_MODULE,
  92. .name = "adm1031",
  93. .flags = I2C_DF_NOTIFY,
  94. .attach_adapter = adm1031_attach_adapter,
  95. .detach_client = adm1031_detach_client,
  96. };
  97. static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
  98. {
  99. return i2c_smbus_read_byte_data(client, reg);
  100. }
  101. static inline int
  102. adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
  103. {
  104. return i2c_smbus_write_byte_data(client, reg, value);
  105. }
  106. #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
  107. ((val + 500) / 1000)))
  108. #define TEMP_FROM_REG(val) ((val) * 1000)
  109. #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
  110. #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
  111. static int FAN_TO_REG(int reg, int div)
  112. {
  113. int tmp;
  114. tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
  115. return tmp > 255 ? 255 : tmp;
  116. }
  117. #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
  118. #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
  119. #define PWM_FROM_REG(val) ((val) << 4)
  120. #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
  121. #define FAN_CHAN_TO_REG(val, reg) \
  122. (((reg) & 0x1F) | (((val) << 5) & 0xe0))
  123. #define AUTO_TEMP_MIN_TO_REG(val, reg) \
  124. ((((val)/500) & 0xf8)|((reg) & 0x7))
  125. #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
  126. #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
  127. #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
  128. #define AUTO_TEMP_OFF_FROM_REG(reg) \
  129. (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
  130. #define AUTO_TEMP_MAX_FROM_REG(reg) \
  131. (AUTO_TEMP_RANGE_FROM_REG(reg) + \
  132. AUTO_TEMP_MIN_FROM_REG(reg))
  133. static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
  134. {
  135. int ret;
  136. int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
  137. range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
  138. ret = ((reg & 0xf8) |
  139. (range < 10000 ? 0 :
  140. range < 20000 ? 1 :
  141. range < 40000 ? 2 : range < 80000 ? 3 : 4));
  142. return ret;
  143. }
  144. /* FAN auto control */
  145. #define GET_FAN_AUTO_BITFIELD(data, idx) \
  146. (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
  147. /* The tables below contains the possible values for the auto fan
  148. * control bitfields. the index in the table is the register value.
  149. * MSb is the auto fan control enable bit, so the four first entries
  150. * in the table disables auto fan control when both bitfields are zero.
  151. */
  152. static auto_chan_table_t auto_channel_select_table_adm1031 = {
  153. {0, 0}, {0, 0}, {0, 0}, {0, 0},
  154. {2 /*0b010 */ , 4 /*0b100 */ },
  155. {2 /*0b010 */ , 2 /*0b010 */ },
  156. {4 /*0b100 */ , 4 /*0b100 */ },
  157. {7 /*0b111 */ , 7 /*0b111 */ },
  158. };
  159. static auto_chan_table_t auto_channel_select_table_adm1030 = {
  160. {0, 0}, {0, 0}, {0, 0}, {0, 0},
  161. {2 /*0b10 */ , 0},
  162. {0xff /*invalid */ , 0},
  163. {0xff /*invalid */ , 0},
  164. {3 /*0b11 */ , 0},
  165. };
  166. /* That function checks if a bitfield is valid and returns the other bitfield
  167. * nearest match if no exact match where found.
  168. */
  169. static int
  170. get_fan_auto_nearest(struct adm1031_data *data,
  171. int chan, u8 val, u8 reg, u8 * new_reg)
  172. {
  173. int i;
  174. int first_match = -1, exact_match = -1;
  175. u8 other_reg_val =
  176. (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
  177. if (val == 0) {
  178. *new_reg = 0;
  179. return 0;
  180. }
  181. for (i = 0; i < 8; i++) {
  182. if ((val == (*data->chan_select_table)[i][chan]) &&
  183. ((*data->chan_select_table)[i][chan ? 0 : 1] ==
  184. other_reg_val)) {
  185. /* We found an exact match */
  186. exact_match = i;
  187. break;
  188. } else if (val == (*data->chan_select_table)[i][chan] &&
  189. first_match == -1) {
  190. /* Save the first match in case of an exact match has not been
  191. * found
  192. */
  193. first_match = i;
  194. }
  195. }
  196. if (exact_match >= 0) {
  197. *new_reg = exact_match;
  198. } else if (first_match >= 0) {
  199. *new_reg = first_match;
  200. } else {
  201. return -EINVAL;
  202. }
  203. return 0;
  204. }
  205. static ssize_t show_fan_auto_channel(struct device *dev, char *buf, int nr)
  206. {
  207. struct adm1031_data *data = adm1031_update_device(dev);
  208. return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
  209. }
  210. static ssize_t
  211. set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
  212. {
  213. struct i2c_client *client = to_i2c_client(dev);
  214. struct adm1031_data *data = i2c_get_clientdata(client);
  215. int val = simple_strtol(buf, NULL, 10);
  216. u8 reg;
  217. int ret;
  218. u8 old_fan_mode;
  219. old_fan_mode = data->conf1;
  220. down(&data->update_lock);
  221. if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
  222. up(&data->update_lock);
  223. return ret;
  224. }
  225. if (((data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1)) & ADM1031_CONF1_AUTO_MODE) ^
  226. (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
  227. if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
  228. /* Switch to Auto Fan Mode
  229. * Save PWM registers
  230. * Set PWM registers to 33% Both */
  231. data->old_pwm[0] = data->pwm[0];
  232. data->old_pwm[1] = data->pwm[1];
  233. adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
  234. } else {
  235. /* Switch to Manual Mode */
  236. data->pwm[0] = data->old_pwm[0];
  237. data->pwm[1] = data->old_pwm[1];
  238. /* Restore PWM registers */
  239. adm1031_write_value(client, ADM1031_REG_PWM,
  240. data->pwm[0] | (data->pwm[1] << 4));
  241. }
  242. }
  243. data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
  244. adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
  245. up(&data->update_lock);
  246. return count;
  247. }
  248. #define fan_auto_channel_offset(offset) \
  249. static ssize_t show_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  250. { \
  251. return show_fan_auto_channel(dev, buf, offset - 1); \
  252. } \
  253. static ssize_t set_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, \
  254. const char *buf, size_t count) \
  255. { \
  256. return set_fan_auto_channel(dev, buf, count, offset - 1); \
  257. } \
  258. static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR, \
  259. show_fan_auto_channel_##offset, \
  260. set_fan_auto_channel_##offset)
  261. fan_auto_channel_offset(1);
  262. fan_auto_channel_offset(2);
  263. /* Auto Temps */
  264. static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr)
  265. {
  266. struct adm1031_data *data = adm1031_update_device(dev);
  267. return sprintf(buf, "%d\n",
  268. AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
  269. }
  270. static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr)
  271. {
  272. struct adm1031_data *data = adm1031_update_device(dev);
  273. return sprintf(buf, "%d\n",
  274. AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
  275. }
  276. static ssize_t
  277. set_auto_temp_min(struct device *dev, const char *buf, size_t count, int nr)
  278. {
  279. struct i2c_client *client = to_i2c_client(dev);
  280. struct adm1031_data *data = i2c_get_clientdata(client);
  281. int val = simple_strtol(buf, NULL, 10);
  282. down(&data->update_lock);
  283. data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
  284. adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
  285. data->auto_temp[nr]);
  286. up(&data->update_lock);
  287. return count;
  288. }
  289. static ssize_t show_auto_temp_max(struct device *dev, char *buf, int nr)
  290. {
  291. struct adm1031_data *data = adm1031_update_device(dev);
  292. return sprintf(buf, "%d\n",
  293. AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
  294. }
  295. static ssize_t
  296. set_auto_temp_max(struct device *dev, const char *buf, size_t count, int nr)
  297. {
  298. struct i2c_client *client = to_i2c_client(dev);
  299. struct adm1031_data *data = i2c_get_clientdata(client);
  300. int val = simple_strtol(buf, NULL, 10);
  301. down(&data->update_lock);
  302. data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
  303. adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
  304. data->temp_max[nr]);
  305. up(&data->update_lock);
  306. return count;
  307. }
  308. #define auto_temp_reg(offset) \
  309. static ssize_t show_auto_temp_##offset##_off (struct device *dev, struct device_attribute *attr, char *buf) \
  310. { \
  311. return show_auto_temp_off(dev, buf, offset - 1); \
  312. } \
  313. static ssize_t show_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
  314. { \
  315. return show_auto_temp_min(dev, buf, offset - 1); \
  316. } \
  317. static ssize_t show_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
  318. { \
  319. return show_auto_temp_max(dev, buf, offset - 1); \
  320. } \
  321. static ssize_t set_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
  322. const char *buf, size_t count) \
  323. { \
  324. return set_auto_temp_min(dev, buf, count, offset - 1); \
  325. } \
  326. static ssize_t set_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
  327. const char *buf, size_t count) \
  328. { \
  329. return set_auto_temp_max(dev, buf, count, offset - 1); \
  330. } \
  331. static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
  332. show_auto_temp_##offset##_off, NULL); \
  333. static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
  334. show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\
  335. static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
  336. show_auto_temp_##offset##_max, set_auto_temp_##offset##_max)
  337. auto_temp_reg(1);
  338. auto_temp_reg(2);
  339. auto_temp_reg(3);
  340. /* pwm */
  341. static ssize_t show_pwm(struct device *dev, char *buf, int nr)
  342. {
  343. struct adm1031_data *data = adm1031_update_device(dev);
  344. return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
  345. }
  346. static ssize_t
  347. set_pwm(struct device *dev, const char *buf, size_t count, int nr)
  348. {
  349. struct i2c_client *client = to_i2c_client(dev);
  350. struct adm1031_data *data = i2c_get_clientdata(client);
  351. int val = simple_strtol(buf, NULL, 10);
  352. int reg;
  353. down(&data->update_lock);
  354. if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
  355. (((val>>4) & 0xf) != 5)) {
  356. /* In automatic mode, the only PWM accepted is 33% */
  357. up(&data->update_lock);
  358. return -EINVAL;
  359. }
  360. data->pwm[nr] = PWM_TO_REG(val);
  361. reg = adm1031_read_value(client, ADM1031_REG_PWM);
  362. adm1031_write_value(client, ADM1031_REG_PWM,
  363. nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
  364. : (data->pwm[nr] & 0xf) | (reg & 0xf0));
  365. up(&data->update_lock);
  366. return count;
  367. }
  368. #define pwm_reg(offset) \
  369. static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  370. { \
  371. return show_pwm(dev, buf, offset - 1); \
  372. } \
  373. static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \
  374. const char *buf, size_t count) \
  375. { \
  376. return set_pwm(dev, buf, count, offset - 1); \
  377. } \
  378. static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
  379. show_pwm_##offset, set_pwm_##offset)
  380. pwm_reg(1);
  381. pwm_reg(2);
  382. /* Fans */
  383. /*
  384. * That function checks the cases where the fan reading is not
  385. * relevant. It is used to provide 0 as fan reading when the fan is
  386. * not supposed to run
  387. */
  388. static int trust_fan_readings(struct adm1031_data *data, int chan)
  389. {
  390. int res = 0;
  391. if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
  392. switch (data->conf1 & 0x60) {
  393. case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
  394. res = data->temp[chan+1] >=
  395. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
  396. break;
  397. case 0x20: /* remote temp1 controls both fans */
  398. res =
  399. data->temp[1] >=
  400. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
  401. break;
  402. case 0x40: /* remote temp2 controls both fans */
  403. res =
  404. data->temp[2] >=
  405. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
  406. break;
  407. case 0x60: /* max controls both fans */
  408. res =
  409. data->temp[0] >=
  410. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
  411. || data->temp[1] >=
  412. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
  413. || (data->chip_type == adm1031
  414. && data->temp[2] >=
  415. AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
  416. break;
  417. }
  418. } else {
  419. res = data->pwm[chan] > 0;
  420. }
  421. return res;
  422. }
  423. static ssize_t show_fan(struct device *dev, char *buf, int nr)
  424. {
  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, char *buf, int nr)
  432. {
  433. struct adm1031_data *data = adm1031_update_device(dev);
  434. return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
  435. }
  436. static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
  437. {
  438. struct adm1031_data *data = adm1031_update_device(dev);
  439. return sprintf(buf, "%d\n",
  440. FAN_FROM_REG(data->fan_min[nr],
  441. FAN_DIV_FROM_REG(data->fan_div[nr])));
  442. }
  443. static ssize_t
  444. set_fan_min(struct device *dev, const char *buf, size_t count, int nr)
  445. {
  446. struct i2c_client *client = to_i2c_client(dev);
  447. struct adm1031_data *data = i2c_get_clientdata(client);
  448. int val = simple_strtol(buf, NULL, 10);
  449. down(&data->update_lock);
  450. if (val) {
  451. data->fan_min[nr] =
  452. FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
  453. } else {
  454. data->fan_min[nr] = 0xff;
  455. }
  456. adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
  457. up(&data->update_lock);
  458. return count;
  459. }
  460. static ssize_t
  461. set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
  462. {
  463. struct i2c_client *client = to_i2c_client(dev);
  464. struct adm1031_data *data = i2c_get_clientdata(client);
  465. int val = simple_strtol(buf, NULL, 10);
  466. u8 tmp;
  467. int old_div;
  468. int new_min;
  469. tmp = val == 8 ? 0xc0 :
  470. val == 4 ? 0x80 :
  471. val == 2 ? 0x40 :
  472. val == 1 ? 0x00 :
  473. 0xff;
  474. if (tmp == 0xff)
  475. return -EINVAL;
  476. down(&data->update_lock);
  477. old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
  478. data->fan_div[nr] = (tmp & 0xC0) | (0x3f & data->fan_div[nr]);
  479. new_min = data->fan_min[nr] * old_div /
  480. FAN_DIV_FROM_REG(data->fan_div[nr]);
  481. data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
  482. data->fan[nr] = data->fan[nr] * old_div /
  483. FAN_DIV_FROM_REG(data->fan_div[nr]);
  484. adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
  485. data->fan_div[nr]);
  486. adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
  487. data->fan_min[nr]);
  488. up(&data->update_lock);
  489. return count;
  490. }
  491. #define fan_offset(offset) \
  492. static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  493. { \
  494. return show_fan(dev, buf, offset - 1); \
  495. } \
  496. static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
  497. { \
  498. return show_fan_min(dev, buf, offset - 1); \
  499. } \
  500. static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
  501. { \
  502. return show_fan_div(dev, buf, offset - 1); \
  503. } \
  504. static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
  505. const char *buf, size_t count) \
  506. { \
  507. return set_fan_min(dev, buf, count, offset - 1); \
  508. } \
  509. static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, \
  510. const char *buf, size_t count) \
  511. { \
  512. return set_fan_div(dev, buf, count, offset - 1); \
  513. } \
  514. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
  515. NULL); \
  516. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  517. show_fan_##offset##_min, set_fan_##offset##_min); \
  518. static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  519. show_fan_##offset##_div, set_fan_##offset##_div); \
  520. static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR, \
  521. show_pwm_##offset, set_pwm_##offset)
  522. fan_offset(1);
  523. fan_offset(2);
  524. /* Temps */
  525. static ssize_t show_temp(struct device *dev, char *buf, int nr)
  526. {
  527. struct adm1031_data *data = adm1031_update_device(dev);
  528. int ext;
  529. ext = nr == 0 ?
  530. ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
  531. (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
  532. return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
  533. }
  534. static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
  535. {
  536. struct adm1031_data *data = adm1031_update_device(dev);
  537. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
  538. }
  539. static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
  540. {
  541. struct adm1031_data *data = adm1031_update_device(dev);
  542. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
  543. }
  544. static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
  545. {
  546. struct adm1031_data *data = adm1031_update_device(dev);
  547. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
  548. }
  549. static ssize_t
  550. set_temp_min(struct device *dev, const char *buf, size_t count, int nr)
  551. {
  552. struct i2c_client *client = to_i2c_client(dev);
  553. struct adm1031_data *data = i2c_get_clientdata(client);
  554. int val;
  555. val = simple_strtol(buf, NULL, 10);
  556. val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
  557. down(&data->update_lock);
  558. data->temp_min[nr] = TEMP_TO_REG(val);
  559. adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
  560. data->temp_min[nr]);
  561. up(&data->update_lock);
  562. return count;
  563. }
  564. static ssize_t
  565. set_temp_max(struct device *dev, const char *buf, size_t count, int nr)
  566. {
  567. struct i2c_client *client = to_i2c_client(dev);
  568. struct adm1031_data *data = i2c_get_clientdata(client);
  569. int val;
  570. val = simple_strtol(buf, NULL, 10);
  571. val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
  572. down(&data->update_lock);
  573. data->temp_max[nr] = TEMP_TO_REG(val);
  574. adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
  575. data->temp_max[nr]);
  576. up(&data->update_lock);
  577. return count;
  578. }
  579. static ssize_t
  580. set_temp_crit(struct device *dev, const char *buf, size_t count, int nr)
  581. {
  582. struct i2c_client *client = to_i2c_client(dev);
  583. struct adm1031_data *data = i2c_get_clientdata(client);
  584. int val;
  585. val = simple_strtol(buf, NULL, 10);
  586. val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
  587. down(&data->update_lock);
  588. data->temp_crit[nr] = TEMP_TO_REG(val);
  589. adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
  590. data->temp_crit[nr]);
  591. up(&data->update_lock);
  592. return count;
  593. }
  594. #define temp_reg(offset) \
  595. static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  596. { \
  597. return show_temp(dev, buf, offset - 1); \
  598. } \
  599. static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
  600. { \
  601. return show_temp_min(dev, buf, offset - 1); \
  602. } \
  603. static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
  604. { \
  605. return show_temp_max(dev, buf, offset - 1); \
  606. } \
  607. static ssize_t show_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, char *buf) \
  608. { \
  609. return show_temp_crit(dev, buf, offset - 1); \
  610. } \
  611. static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
  612. const char *buf, size_t count) \
  613. { \
  614. return set_temp_min(dev, buf, count, offset - 1); \
  615. } \
  616. static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
  617. const char *buf, size_t count) \
  618. { \
  619. return set_temp_max(dev, buf, count, offset - 1); \
  620. } \
  621. static ssize_t set_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, \
  622. const char *buf, size_t count) \
  623. { \
  624. return set_temp_crit(dev, buf, count, offset - 1); \
  625. } \
  626. static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
  627. NULL); \
  628. static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  629. show_temp_##offset##_min, set_temp_##offset##_min); \
  630. static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  631. show_temp_##offset##_max, set_temp_##offset##_max); \
  632. static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
  633. show_temp_##offset##_crit, set_temp_##offset##_crit)
  634. temp_reg(1);
  635. temp_reg(2);
  636. temp_reg(3);
  637. /* Alarms */
  638. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  639. {
  640. struct adm1031_data *data = adm1031_update_device(dev);
  641. return sprintf(buf, "%d\n", data->alarm);
  642. }
  643. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  644. static int adm1031_attach_adapter(struct i2c_adapter *adapter)
  645. {
  646. if (!(adapter->class & I2C_CLASS_HWMON))
  647. return 0;
  648. return i2c_probe(adapter, &addr_data, adm1031_detect);
  649. }
  650. /* This function is called by i2c_probe */
  651. static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
  652. {
  653. struct i2c_client *new_client;
  654. struct adm1031_data *data;
  655. int err = 0;
  656. const char *name = "";
  657. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  658. goto exit;
  659. if (!(data = kmalloc(sizeof(struct adm1031_data), GFP_KERNEL))) {
  660. err = -ENOMEM;
  661. goto exit;
  662. }
  663. memset(data, 0, sizeof(struct adm1031_data));
  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. init_MUTEX(&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. down(&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. up(&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);