w83l786ng.c 22 KB

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