adm1031.c 30 KB

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