adm1031.c 31 KB

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