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