w83l786ng.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * w83l786ng.c - Linux kernel driver for hardware monitoring
  3. * Copyright (c) 2007 Kevin Lo <kevlo@kevlo.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation - version 2.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA.
  18. */
  19. /*
  20. * Supports following chips:
  21. *
  22. * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  23. * w83l786ng 3 2 2 2 0x7b 0x5ca3 yes no
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/i2c.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-vid.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/err.h>
  33. #include <linux/mutex.h>
  34. /* Addresses to scan */
  35. static const unsigned short normal_i2c[] = { 0x2e, 0x2f, I2C_CLIENT_END };
  36. /* Insmod parameters */
  37. static bool reset;
  38. module_param(reset, bool, 0);
  39. MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
  40. #define W83L786NG_REG_IN_MIN(nr) (0x2C + (nr) * 2)
  41. #define W83L786NG_REG_IN_MAX(nr) (0x2B + (nr) * 2)
  42. #define W83L786NG_REG_IN(nr) ((nr) + 0x20)
  43. #define W83L786NG_REG_FAN(nr) ((nr) + 0x28)
  44. #define W83L786NG_REG_FAN_MIN(nr) ((nr) + 0x3B)
  45. #define W83L786NG_REG_CONFIG 0x40
  46. #define W83L786NG_REG_ALARM1 0x41
  47. #define W83L786NG_REG_ALARM2 0x42
  48. #define W83L786NG_REG_GPIO_EN 0x47
  49. #define W83L786NG_REG_MAN_ID2 0x4C
  50. #define W83L786NG_REG_MAN_ID1 0x4D
  51. #define W83L786NG_REG_CHIP_ID 0x4E
  52. #define W83L786NG_REG_DIODE 0x53
  53. #define W83L786NG_REG_FAN_DIV 0x54
  54. #define W83L786NG_REG_FAN_CFG 0x80
  55. #define W83L786NG_REG_TOLERANCE 0x8D
  56. static const u8 W83L786NG_REG_TEMP[2][3] = {
  57. { 0x25, /* TEMP 0 in DataSheet */
  58. 0x35, /* TEMP 0 Over in DataSheet */
  59. 0x36 }, /* TEMP 0 Hyst in DataSheet */
  60. { 0x26, /* TEMP 1 in DataSheet */
  61. 0x37, /* TEMP 1 Over in DataSheet */
  62. 0x38 } /* TEMP 1 Hyst in DataSheet */
  63. };
  64. static const u8 W83L786NG_PWM_MODE_SHIFT[] = {6, 7};
  65. static const u8 W83L786NG_PWM_ENABLE_SHIFT[] = {2, 4};
  66. /* FAN Duty Cycle, be used to control */
  67. static const u8 W83L786NG_REG_PWM[] = {0x81, 0x87};
  68. static inline u8
  69. FAN_TO_REG(long rpm, int div)
  70. {
  71. if (rpm == 0)
  72. return 255;
  73. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  74. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  75. }
  76. #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
  77. ((val) == 255 ? 0 : \
  78. 1350000 / ((val) * (div))))
  79. /* for temp */
  80. #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? \
  81. (val) + 0x100 * 1000 \
  82. : (val)) / 1000, 0, 0xff))
  83. #define TEMP_FROM_REG(val) (((val) & 0x80 ? \
  84. (val) - 0x100 : (val)) * 1000)
  85. /*
  86. * The analog voltage inputs have 8mV LSB. Since the sysfs output is
  87. * in mV as would be measured on the chip input pin, need to just
  88. * multiply/divide by 8 to translate from/to register values.
  89. */
  90. #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 4) / 8), 0, 255))
  91. #define IN_FROM_REG(val) ((val) * 8)
  92. #define DIV_FROM_REG(val) (1 << (val))
  93. static inline u8
  94. DIV_TO_REG(long val)
  95. {
  96. int i;
  97. val = SENSORS_LIMIT(val, 1, 128) >> 1;
  98. for (i = 0; i < 7; i++) {
  99. if (val == 0)
  100. break;
  101. val >>= 1;
  102. }
  103. return (u8)i;
  104. }
  105. struct w83l786ng_data {
  106. struct device *hwmon_dev;
  107. struct mutex update_lock;
  108. char valid; /* !=0 if following fields are valid */
  109. unsigned long last_updated; /* In jiffies */
  110. unsigned long last_nonvolatile; /* In jiffies, last time we update the
  111. * nonvolatile registers */
  112. u8 in[3];
  113. u8 in_max[3];
  114. u8 in_min[3];
  115. u8 fan[2];
  116. u8 fan_div[2];
  117. u8 fan_min[2];
  118. u8 temp_type[2];
  119. u8 temp[2][3];
  120. u8 pwm[2];
  121. u8 pwm_mode[2]; /* 0->DC variable voltage
  122. * 1->PWM variable duty cycle */
  123. u8 pwm_enable[2]; /* 1->manual
  124. * 2->thermal cruise (also called SmartFan I) */
  125. u8 tolerance[2];
  126. };
  127. static int w83l786ng_probe(struct i2c_client *client,
  128. const struct i2c_device_id *id);
  129. static int w83l786ng_detect(struct i2c_client *client,
  130. struct i2c_board_info *info);
  131. static int w83l786ng_remove(struct i2c_client *client);
  132. static void w83l786ng_init_client(struct i2c_client *client);
  133. static struct w83l786ng_data *w83l786ng_update_device(struct device *dev);
  134. static const struct i2c_device_id w83l786ng_id[] = {
  135. { "w83l786ng", 0 },
  136. { }
  137. };
  138. MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
  139. static struct i2c_driver w83l786ng_driver = {
  140. .class = I2C_CLASS_HWMON,
  141. .driver = {
  142. .name = "w83l786ng",
  143. },
  144. .probe = w83l786ng_probe,
  145. .remove = w83l786ng_remove,
  146. .id_table = w83l786ng_id,
  147. .detect = w83l786ng_detect,
  148. .address_list = normal_i2c,
  149. };
  150. static u8
  151. w83l786ng_read_value(struct i2c_client *client, u8 reg)
  152. {
  153. return i2c_smbus_read_byte_data(client, reg);
  154. }
  155. static int
  156. w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value)
  157. {
  158. return i2c_smbus_write_byte_data(client, reg, value);
  159. }
  160. /* following are the sysfs callback functions */
  161. #define show_in_reg(reg) \
  162. static ssize_t \
  163. show_##reg(struct device *dev, struct device_attribute *attr, \
  164. char *buf) \
  165. { \
  166. int nr = to_sensor_dev_attr(attr)->index; \
  167. struct w83l786ng_data *data = w83l786ng_update_device(dev); \
  168. return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
  169. }
  170. show_in_reg(in)
  171. show_in_reg(in_min)
  172. show_in_reg(in_max)
  173. #define store_in_reg(REG, reg) \
  174. static ssize_t \
  175. store_in_##reg(struct device *dev, struct device_attribute *attr, \
  176. const char *buf, size_t count) \
  177. { \
  178. int nr = to_sensor_dev_attr(attr)->index; \
  179. struct i2c_client *client = to_i2c_client(dev); \
  180. struct w83l786ng_data *data = i2c_get_clientdata(client); \
  181. unsigned long val; \
  182. int err = kstrtoul(buf, 10, &val); \
  183. if (err) \
  184. return err; \
  185. mutex_lock(&data->update_lock); \
  186. data->in_##reg[nr] = IN_TO_REG(val); \
  187. w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \
  188. data->in_##reg[nr]); \
  189. mutex_unlock(&data->update_lock); \
  190. return count; \
  191. }
  192. store_in_reg(MIN, min)
  193. store_in_reg(MAX, max)
  194. static struct sensor_device_attribute sda_in_input[] = {
  195. SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
  196. SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
  197. SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
  198. };
  199. static struct sensor_device_attribute sda_in_min[] = {
  200. SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
  201. SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
  202. SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
  203. };
  204. static struct sensor_device_attribute sda_in_max[] = {
  205. SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
  206. SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
  207. SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
  208. };
  209. #define show_fan_reg(reg) \
  210. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  211. char *buf) \
  212. { \
  213. int nr = to_sensor_dev_attr(attr)->index; \
  214. struct w83l786ng_data *data = w83l786ng_update_device(dev); \
  215. return sprintf(buf, "%d\n", \
  216. FAN_FROM_REG(data->fan[nr], DIV_FROM_REG(data->fan_div[nr]))); \
  217. }
  218. show_fan_reg(fan);
  219. show_fan_reg(fan_min);
  220. static ssize_t
  221. store_fan_min(struct device *dev, struct device_attribute *attr,
  222. const char *buf, size_t count)
  223. {
  224. int nr = to_sensor_dev_attr(attr)->index;
  225. struct i2c_client *client = to_i2c_client(dev);
  226. struct w83l786ng_data *data = i2c_get_clientdata(client);
  227. unsigned long val;
  228. int err;
  229. err = kstrtoul(buf, 10, &val);
  230. if (err)
  231. return err;
  232. mutex_lock(&data->update_lock);
  233. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  234. w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
  235. data->fan_min[nr]);
  236. mutex_unlock(&data->update_lock);
  237. return count;
  238. }
  239. static ssize_t
  240. show_fan_div(struct device *dev, struct device_attribute *attr,
  241. char *buf)
  242. {
  243. int nr = to_sensor_dev_attr(attr)->index;
  244. struct w83l786ng_data *data = w83l786ng_update_device(dev);
  245. return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr]));
  246. }
  247. /*
  248. * Note: we save and restore the fan minimum here, because its value is
  249. * determined in part by the fan divisor. This follows the principle of
  250. * least surprise; the user doesn't expect the fan minimum to change just
  251. * because the divisor changed.
  252. */
  253. static ssize_t
  254. store_fan_div(struct device *dev, struct device_attribute *attr,
  255. const char *buf, size_t count)
  256. {
  257. int nr = to_sensor_dev_attr(attr)->index;
  258. struct i2c_client *client = to_i2c_client(dev);
  259. struct w83l786ng_data *data = i2c_get_clientdata(client);
  260. unsigned long min;
  261. u8 tmp_fan_div;
  262. u8 fan_div_reg;
  263. u8 keep_mask = 0;
  264. u8 new_shift = 0;
  265. unsigned long val;
  266. int err;
  267. err = kstrtoul(buf, 10, &val);
  268. if (err)
  269. return err;
  270. /* Save fan_min */
  271. mutex_lock(&data->update_lock);
  272. min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
  273. data->fan_div[nr] = DIV_TO_REG(val);
  274. switch (nr) {
  275. case 0:
  276. keep_mask = 0xf8;
  277. new_shift = 0;
  278. break;
  279. case 1:
  280. keep_mask = 0x8f;
  281. new_shift = 4;
  282. break;
  283. }
  284. fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV)
  285. & keep_mask;
  286. tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask;
  287. w83l786ng_write_value(client, W83L786NG_REG_FAN_DIV,
  288. fan_div_reg | tmp_fan_div);
  289. /* Restore fan_min */
  290. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  291. w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
  292. data->fan_min[nr]);
  293. mutex_unlock(&data->update_lock);
  294. return count;
  295. }
  296. static struct sensor_device_attribute sda_fan_input[] = {
  297. SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
  298. SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
  299. };
  300. static struct sensor_device_attribute sda_fan_min[] = {
  301. SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
  302. store_fan_min, 0),
  303. SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
  304. store_fan_min, 1),
  305. };
  306. static struct sensor_device_attribute sda_fan_div[] = {
  307. SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div,
  308. store_fan_div, 0),
  309. SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div,
  310. store_fan_div, 1),
  311. };
  312. /* read/write the temperature, includes measured value and limits */
  313. static ssize_t
  314. show_temp(struct device *dev, struct device_attribute *attr, char *buf)
  315. {
  316. struct sensor_device_attribute_2 *sensor_attr =
  317. to_sensor_dev_attr_2(attr);
  318. int nr = sensor_attr->nr;
  319. int index = sensor_attr->index;
  320. struct w83l786ng_data *data = w83l786ng_update_device(dev);
  321. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
  322. }
  323. static ssize_t
  324. store_temp(struct device *dev, struct device_attribute *attr,
  325. const char *buf, size_t count)
  326. {
  327. struct sensor_device_attribute_2 *sensor_attr =
  328. to_sensor_dev_attr_2(attr);
  329. int nr = sensor_attr->nr;
  330. int index = sensor_attr->index;
  331. struct i2c_client *client = to_i2c_client(dev);
  332. struct w83l786ng_data *data = i2c_get_clientdata(client);
  333. long val;
  334. int err;
  335. err = kstrtol(buf, 10, &val);
  336. if (err)
  337. return err;
  338. mutex_lock(&data->update_lock);
  339. data->temp[nr][index] = TEMP_TO_REG(val);
  340. w83l786ng_write_value(client, W83L786NG_REG_TEMP[nr][index],
  341. data->temp[nr][index]);
  342. mutex_unlock(&data->update_lock);
  343. return count;
  344. }
  345. static struct sensor_device_attribute_2 sda_temp_input[] = {
  346. SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
  347. SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
  348. };
  349. static struct sensor_device_attribute_2 sda_temp_max[] = {
  350. SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
  351. show_temp, store_temp, 0, 1),
  352. SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR,
  353. show_temp, store_temp, 1, 1),
  354. };
  355. static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
  356. SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
  357. show_temp, store_temp, 0, 2),
  358. SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
  359. show_temp, store_temp, 1, 2),
  360. };
  361. #define show_pwm_reg(reg) \
  362. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  363. char *buf) \
  364. { \
  365. struct w83l786ng_data *data = w83l786ng_update_device(dev); \
  366. int nr = to_sensor_dev_attr(attr)->index; \
  367. return sprintf(buf, "%d\n", data->reg[nr]); \
  368. }
  369. show_pwm_reg(pwm_mode)
  370. show_pwm_reg(pwm_enable)
  371. show_pwm_reg(pwm)
  372. static ssize_t
  373. store_pwm_mode(struct device *dev, struct device_attribute *attr,
  374. const char *buf, size_t count)
  375. {
  376. int nr = to_sensor_dev_attr(attr)->index;
  377. struct i2c_client *client = to_i2c_client(dev);
  378. struct w83l786ng_data *data = i2c_get_clientdata(client);
  379. u8 reg;
  380. unsigned long val;
  381. int err;
  382. err = kstrtoul(buf, 10, &val);
  383. if (err)
  384. return err;
  385. if (val > 1)
  386. return -EINVAL;
  387. mutex_lock(&data->update_lock);
  388. data->pwm_mode[nr] = val;
  389. reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
  390. reg &= ~(1 << W83L786NG_PWM_MODE_SHIFT[nr]);
  391. if (!val)
  392. reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr];
  393. w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
  394. mutex_unlock(&data->update_lock);
  395. return count;
  396. }
  397. static ssize_t
  398. store_pwm(struct device *dev, struct device_attribute *attr,
  399. const char *buf, size_t count)
  400. {
  401. int nr = to_sensor_dev_attr(attr)->index;
  402. struct i2c_client *client = to_i2c_client(dev);
  403. struct w83l786ng_data *data = i2c_get_clientdata(client);
  404. unsigned long val;
  405. int err;
  406. err = kstrtoul(buf, 10, &val);
  407. if (err)
  408. return err;
  409. val = SENSORS_LIMIT(val, 0, 255);
  410. val = DIV_ROUND_CLOSEST(val, 0x11);
  411. mutex_lock(&data->update_lock);
  412. data->pwm[nr] = val * 0x11;
  413. val |= w83l786ng_read_value(client, W83L786NG_REG_PWM[nr]) & 0xf0;
  414. w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val);
  415. mutex_unlock(&data->update_lock);
  416. return count;
  417. }
  418. static ssize_t
  419. store_pwm_enable(struct device *dev, struct device_attribute *attr,
  420. const char *buf, size_t count)
  421. {
  422. int nr = to_sensor_dev_attr(attr)->index;
  423. struct i2c_client *client = to_i2c_client(dev);
  424. struct w83l786ng_data *data = i2c_get_clientdata(client);
  425. u8 reg;
  426. unsigned long val;
  427. int err;
  428. err = kstrtoul(buf, 10, &val);
  429. if (err)
  430. return err;
  431. if (!val || val > 2) /* only modes 1 and 2 are supported */
  432. return -EINVAL;
  433. mutex_lock(&data->update_lock);
  434. reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
  435. data->pwm_enable[nr] = val;
  436. reg &= ~(0x03 << W83L786NG_PWM_ENABLE_SHIFT[nr]);
  437. reg |= (val - 1) << W83L786NG_PWM_ENABLE_SHIFT[nr];
  438. w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
  439. mutex_unlock(&data->update_lock);
  440. return count;
  441. }
  442. static struct sensor_device_attribute sda_pwm[] = {
  443. SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
  444. SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
  445. };
  446. static struct sensor_device_attribute sda_pwm_mode[] = {
  447. SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
  448. store_pwm_mode, 0),
  449. SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
  450. store_pwm_mode, 1),
  451. };
  452. static struct sensor_device_attribute sda_pwm_enable[] = {
  453. SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
  454. store_pwm_enable, 0),
  455. SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
  456. store_pwm_enable, 1),
  457. };
  458. /* For Smart Fan I/Thermal Cruise and Smart Fan II */
  459. static ssize_t
  460. show_tolerance(struct device *dev, struct device_attribute *attr, char *buf)
  461. {
  462. int nr = to_sensor_dev_attr(attr)->index;
  463. struct w83l786ng_data *data = w83l786ng_update_device(dev);
  464. return sprintf(buf, "%ld\n", (long)data->tolerance[nr]);
  465. }
  466. static ssize_t
  467. store_tolerance(struct device *dev, struct device_attribute *attr,
  468. const char *buf, size_t count)
  469. {
  470. int nr = to_sensor_dev_attr(attr)->index;
  471. struct i2c_client *client = to_i2c_client(dev);
  472. struct w83l786ng_data *data = i2c_get_clientdata(client);
  473. u8 tol_tmp, tol_mask;
  474. unsigned long val;
  475. int err;
  476. err = kstrtoul(buf, 10, &val);
  477. if (err)
  478. return err;
  479. mutex_lock(&data->update_lock);
  480. tol_mask = w83l786ng_read_value(client,
  481. W83L786NG_REG_TOLERANCE) & ((nr == 1) ? 0x0f : 0xf0);
  482. tol_tmp = SENSORS_LIMIT(val, 0, 15);
  483. tol_tmp &= 0x0f;
  484. data->tolerance[nr] = tol_tmp;
  485. if (nr == 1)
  486. tol_tmp <<= 4;
  487. w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE,
  488. tol_mask | tol_tmp);
  489. mutex_unlock(&data->update_lock);
  490. return count;
  491. }
  492. static struct sensor_device_attribute sda_tolerance[] = {
  493. SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO,
  494. show_tolerance, store_tolerance, 0),
  495. SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO,
  496. show_tolerance, store_tolerance, 1),
  497. };
  498. #define IN_UNIT_ATTRS(X) \
  499. &sda_in_input[X].dev_attr.attr, \
  500. &sda_in_min[X].dev_attr.attr, \
  501. &sda_in_max[X].dev_attr.attr
  502. #define FAN_UNIT_ATTRS(X) \
  503. &sda_fan_input[X].dev_attr.attr, \
  504. &sda_fan_min[X].dev_attr.attr, \
  505. &sda_fan_div[X].dev_attr.attr
  506. #define TEMP_UNIT_ATTRS(X) \
  507. &sda_temp_input[X].dev_attr.attr, \
  508. &sda_temp_max[X].dev_attr.attr, \
  509. &sda_temp_max_hyst[X].dev_attr.attr
  510. #define PWM_UNIT_ATTRS(X) \
  511. &sda_pwm[X].dev_attr.attr, \
  512. &sda_pwm_mode[X].dev_attr.attr, \
  513. &sda_pwm_enable[X].dev_attr.attr
  514. #define TOLERANCE_UNIT_ATTRS(X) \
  515. &sda_tolerance[X].dev_attr.attr
  516. static struct attribute *w83l786ng_attributes[] = {
  517. IN_UNIT_ATTRS(0),
  518. IN_UNIT_ATTRS(1),
  519. IN_UNIT_ATTRS(2),
  520. FAN_UNIT_ATTRS(0),
  521. FAN_UNIT_ATTRS(1),
  522. TEMP_UNIT_ATTRS(0),
  523. TEMP_UNIT_ATTRS(1),
  524. PWM_UNIT_ATTRS(0),
  525. PWM_UNIT_ATTRS(1),
  526. TOLERANCE_UNIT_ATTRS(0),
  527. TOLERANCE_UNIT_ATTRS(1),
  528. NULL
  529. };
  530. static const struct attribute_group w83l786ng_group = {
  531. .attrs = w83l786ng_attributes,
  532. };
  533. static int
  534. w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info)
  535. {
  536. struct i2c_adapter *adapter = client->adapter;
  537. u16 man_id;
  538. u8 chip_id;
  539. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  540. return -ENODEV;
  541. /* Detection */
  542. if ((w83l786ng_read_value(client, W83L786NG_REG_CONFIG) & 0x80)) {
  543. dev_dbg(&adapter->dev, "W83L786NG detection failed at 0x%02x\n",
  544. client->addr);
  545. return -ENODEV;
  546. }
  547. /* Identification */
  548. man_id = (w83l786ng_read_value(client, W83L786NG_REG_MAN_ID1) << 8) +
  549. w83l786ng_read_value(client, W83L786NG_REG_MAN_ID2);
  550. chip_id = w83l786ng_read_value(client, W83L786NG_REG_CHIP_ID);
  551. if (man_id != 0x5CA3 || /* Winbond */
  552. chip_id != 0x80) { /* W83L786NG */
  553. dev_dbg(&adapter->dev,
  554. "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
  555. man_id, chip_id);
  556. return -ENODEV;
  557. }
  558. strlcpy(info->type, "w83l786ng", I2C_NAME_SIZE);
  559. return 0;
  560. }
  561. static int
  562. w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id)
  563. {
  564. struct device *dev = &client->dev;
  565. struct w83l786ng_data *data;
  566. int i, err = 0;
  567. u8 reg_tmp;
  568. data = kzalloc(sizeof(struct w83l786ng_data), GFP_KERNEL);
  569. if (!data) {
  570. err = -ENOMEM;
  571. goto exit;
  572. }
  573. i2c_set_clientdata(client, data);
  574. mutex_init(&data->update_lock);
  575. /* Initialize the chip */
  576. w83l786ng_init_client(client);
  577. /* A few vars need to be filled upon startup */
  578. for (i = 0; i < 2; i++) {
  579. data->fan_min[i] = w83l786ng_read_value(client,
  580. W83L786NG_REG_FAN_MIN(i));
  581. }
  582. /* Update the fan divisor */
  583. reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
  584. data->fan_div[0] = reg_tmp & 0x07;
  585. data->fan_div[1] = (reg_tmp >> 4) & 0x07;
  586. /* Register sysfs hooks */
  587. err = sysfs_create_group(&client->dev.kobj, &w83l786ng_group);
  588. if (err)
  589. goto exit_remove;
  590. data->hwmon_dev = hwmon_device_register(dev);
  591. if (IS_ERR(data->hwmon_dev)) {
  592. err = PTR_ERR(data->hwmon_dev);
  593. goto exit_remove;
  594. }
  595. return 0;
  596. /* Unregister sysfs hooks */
  597. exit_remove:
  598. sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
  599. kfree(data);
  600. exit:
  601. return err;
  602. }
  603. static int
  604. w83l786ng_remove(struct i2c_client *client)
  605. {
  606. struct w83l786ng_data *data = i2c_get_clientdata(client);
  607. hwmon_device_unregister(data->hwmon_dev);
  608. sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
  609. kfree(data);
  610. return 0;
  611. }
  612. static void
  613. w83l786ng_init_client(struct i2c_client *client)
  614. {
  615. u8 tmp;
  616. if (reset)
  617. w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
  618. /* Start monitoring */
  619. tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
  620. if (!(tmp & 0x01))
  621. w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01);
  622. }
  623. static struct w83l786ng_data *w83l786ng_update_device(struct device *dev)
  624. {
  625. struct i2c_client *client = to_i2c_client(dev);
  626. struct w83l786ng_data *data = i2c_get_clientdata(client);
  627. int i, j;
  628. u8 reg_tmp, pwmcfg;
  629. mutex_lock(&data->update_lock);
  630. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  631. || !data->valid) {
  632. dev_dbg(&client->dev, "Updating w83l786ng data.\n");
  633. /* Update the voltages measured value and limits */
  634. for (i = 0; i < 3; i++) {
  635. data->in[i] = w83l786ng_read_value(client,
  636. W83L786NG_REG_IN(i));
  637. data->in_min[i] = w83l786ng_read_value(client,
  638. W83L786NG_REG_IN_MIN(i));
  639. data->in_max[i] = w83l786ng_read_value(client,
  640. W83L786NG_REG_IN_MAX(i));
  641. }
  642. /* Update the fan counts and limits */
  643. for (i = 0; i < 2; i++) {
  644. data->fan[i] = w83l786ng_read_value(client,
  645. W83L786NG_REG_FAN(i));
  646. data->fan_min[i] = w83l786ng_read_value(client,
  647. W83L786NG_REG_FAN_MIN(i));
  648. }
  649. /* Update the fan divisor */
  650. reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
  651. data->fan_div[0] = reg_tmp & 0x07;
  652. data->fan_div[1] = (reg_tmp >> 4) & 0x07;
  653. pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
  654. for (i = 0; i < 2; i++) {
  655. data->pwm_mode[i] =
  656. ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
  657. ? 0 : 1;
  658. data->pwm_enable[i] =
  659. ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1;
  660. data->pwm[i] =
  661. (w83l786ng_read_value(client, W83L786NG_REG_PWM[i])
  662. & 0x0f) * 0x11;
  663. }
  664. /* Update the temperature sensors */
  665. for (i = 0; i < 2; i++) {
  666. for (j = 0; j < 3; j++) {
  667. data->temp[i][j] = w83l786ng_read_value(client,
  668. W83L786NG_REG_TEMP[i][j]);
  669. }
  670. }
  671. /* Update Smart Fan I/II tolerance */
  672. reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE);
  673. data->tolerance[0] = reg_tmp & 0x0f;
  674. data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
  675. data->last_updated = jiffies;
  676. data->valid = 1;
  677. }
  678. mutex_unlock(&data->update_lock);
  679. return data;
  680. }
  681. module_i2c_driver(w83l786ng_driver);
  682. MODULE_AUTHOR("Kevin Lo");
  683. MODULE_DESCRIPTION("w83l786ng driver");
  684. MODULE_LICENSE("GPL");