w83l786ng.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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 = clamp_val(rpm, 1, 1000000);
  75. return clamp_val((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) (clamp_val(((val) < 0 ? (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) (clamp_val((((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 = clamp_val(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 i2c_client *client;
  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 u8
  128. w83l786ng_read_value(struct i2c_client *client, u8 reg)
  129. {
  130. return i2c_smbus_read_byte_data(client, reg);
  131. }
  132. static int
  133. w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value)
  134. {
  135. return i2c_smbus_write_byte_data(client, reg, value);
  136. }
  137. static struct w83l786ng_data *w83l786ng_update_device(struct device *dev)
  138. {
  139. struct w83l786ng_data *data = dev_get_drvdata(dev);
  140. struct i2c_client *client = data->client;
  141. int i, j;
  142. u8 reg_tmp, pwmcfg;
  143. mutex_lock(&data->update_lock);
  144. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  145. || !data->valid) {
  146. dev_dbg(&client->dev, "Updating w83l786ng data.\n");
  147. /* Update the voltages measured value and limits */
  148. for (i = 0; i < 3; i++) {
  149. data->in[i] = w83l786ng_read_value(client,
  150. W83L786NG_REG_IN(i));
  151. data->in_min[i] = w83l786ng_read_value(client,
  152. W83L786NG_REG_IN_MIN(i));
  153. data->in_max[i] = w83l786ng_read_value(client,
  154. W83L786NG_REG_IN_MAX(i));
  155. }
  156. /* Update the fan counts and limits */
  157. for (i = 0; i < 2; i++) {
  158. data->fan[i] = w83l786ng_read_value(client,
  159. W83L786NG_REG_FAN(i));
  160. data->fan_min[i] = w83l786ng_read_value(client,
  161. W83L786NG_REG_FAN_MIN(i));
  162. }
  163. /* Update the fan divisor */
  164. reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
  165. data->fan_div[0] = reg_tmp & 0x07;
  166. data->fan_div[1] = (reg_tmp >> 4) & 0x07;
  167. pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
  168. for (i = 0; i < 2; i++) {
  169. data->pwm_mode[i] =
  170. ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
  171. ? 0 : 1;
  172. data->pwm_enable[i] =
  173. ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1;
  174. data->pwm[i] =
  175. (w83l786ng_read_value(client, W83L786NG_REG_PWM[i])
  176. & 0x0f) * 0x11;
  177. }
  178. /* Update the temperature sensors */
  179. for (i = 0; i < 2; i++) {
  180. for (j = 0; j < 3; j++) {
  181. data->temp[i][j] = w83l786ng_read_value(client,
  182. W83L786NG_REG_TEMP[i][j]);
  183. }
  184. }
  185. /* Update Smart Fan I/II tolerance */
  186. reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE);
  187. data->tolerance[0] = reg_tmp & 0x0f;
  188. data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
  189. data->last_updated = jiffies;
  190. data->valid = 1;
  191. }
  192. mutex_unlock(&data->update_lock);
  193. return data;
  194. }
  195. /* following are the sysfs callback functions */
  196. #define show_in_reg(reg) \
  197. static ssize_t \
  198. show_##reg(struct device *dev, struct device_attribute *attr, \
  199. char *buf) \
  200. { \
  201. int nr = to_sensor_dev_attr(attr)->index; \
  202. struct w83l786ng_data *data = w83l786ng_update_device(dev); \
  203. return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
  204. }
  205. show_in_reg(in)
  206. show_in_reg(in_min)
  207. show_in_reg(in_max)
  208. #define store_in_reg(REG, reg) \
  209. static ssize_t \
  210. store_in_##reg(struct device *dev, struct device_attribute *attr, \
  211. const char *buf, size_t count) \
  212. { \
  213. int nr = to_sensor_dev_attr(attr)->index; \
  214. struct w83l786ng_data *data = dev_get_drvdata(dev); \
  215. struct i2c_client *client = data->client; \
  216. unsigned long val; \
  217. int err = kstrtoul(buf, 10, &val); \
  218. if (err) \
  219. return err; \
  220. mutex_lock(&data->update_lock); \
  221. data->in_##reg[nr] = IN_TO_REG(val); \
  222. w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \
  223. data->in_##reg[nr]); \
  224. mutex_unlock(&data->update_lock); \
  225. return count; \
  226. }
  227. store_in_reg(MIN, min)
  228. store_in_reg(MAX, max)
  229. static struct sensor_device_attribute sda_in_input[] = {
  230. SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
  231. SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
  232. SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
  233. };
  234. static struct sensor_device_attribute sda_in_min[] = {
  235. SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
  236. SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
  237. SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
  238. };
  239. static struct sensor_device_attribute sda_in_max[] = {
  240. SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
  241. SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
  242. SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
  243. };
  244. #define show_fan_reg(reg) \
  245. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  246. char *buf) \
  247. { \
  248. int nr = to_sensor_dev_attr(attr)->index; \
  249. struct w83l786ng_data *data = w83l786ng_update_device(dev); \
  250. return sprintf(buf, "%d\n", \
  251. FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
  252. }
  253. show_fan_reg(fan);
  254. show_fan_reg(fan_min);
  255. static ssize_t
  256. store_fan_min(struct device *dev, struct device_attribute *attr,
  257. const char *buf, size_t count)
  258. {
  259. int nr = to_sensor_dev_attr(attr)->index;
  260. struct w83l786ng_data *data = dev_get_drvdata(dev);
  261. struct i2c_client *client = data->client;
  262. unsigned long val;
  263. int err;
  264. err = kstrtoul(buf, 10, &val);
  265. if (err)
  266. return err;
  267. mutex_lock(&data->update_lock);
  268. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  269. w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
  270. data->fan_min[nr]);
  271. mutex_unlock(&data->update_lock);
  272. return count;
  273. }
  274. static ssize_t
  275. show_fan_div(struct device *dev, struct device_attribute *attr,
  276. char *buf)
  277. {
  278. int nr = to_sensor_dev_attr(attr)->index;
  279. struct w83l786ng_data *data = w83l786ng_update_device(dev);
  280. return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr]));
  281. }
  282. /*
  283. * Note: we save and restore the fan minimum here, because its value is
  284. * determined in part by the fan divisor. This follows the principle of
  285. * least surprise; the user doesn't expect the fan minimum to change just
  286. * because the divisor changed.
  287. */
  288. static ssize_t
  289. store_fan_div(struct device *dev, struct device_attribute *attr,
  290. const char *buf, size_t count)
  291. {
  292. int nr = to_sensor_dev_attr(attr)->index;
  293. struct w83l786ng_data *data = dev_get_drvdata(dev);
  294. struct i2c_client *client = data->client;
  295. unsigned long min;
  296. u8 tmp_fan_div;
  297. u8 fan_div_reg;
  298. u8 keep_mask = 0;
  299. u8 new_shift = 0;
  300. unsigned long val;
  301. int err;
  302. err = kstrtoul(buf, 10, &val);
  303. if (err)
  304. return err;
  305. /* Save fan_min */
  306. mutex_lock(&data->update_lock);
  307. min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
  308. data->fan_div[nr] = DIV_TO_REG(val);
  309. switch (nr) {
  310. case 0:
  311. keep_mask = 0xf8;
  312. new_shift = 0;
  313. break;
  314. case 1:
  315. keep_mask = 0x8f;
  316. new_shift = 4;
  317. break;
  318. }
  319. fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV)
  320. & keep_mask;
  321. tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask;
  322. w83l786ng_write_value(client, W83L786NG_REG_FAN_DIV,
  323. fan_div_reg | tmp_fan_div);
  324. /* Restore fan_min */
  325. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  326. w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
  327. data->fan_min[nr]);
  328. mutex_unlock(&data->update_lock);
  329. return count;
  330. }
  331. static struct sensor_device_attribute sda_fan_input[] = {
  332. SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
  333. SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
  334. };
  335. static struct sensor_device_attribute sda_fan_min[] = {
  336. SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
  337. store_fan_min, 0),
  338. SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
  339. store_fan_min, 1),
  340. };
  341. static struct sensor_device_attribute sda_fan_div[] = {
  342. SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div,
  343. store_fan_div, 0),
  344. SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div,
  345. store_fan_div, 1),
  346. };
  347. /* read/write the temperature, includes measured value and limits */
  348. static ssize_t
  349. show_temp(struct device *dev, struct device_attribute *attr, char *buf)
  350. {
  351. struct sensor_device_attribute_2 *sensor_attr =
  352. to_sensor_dev_attr_2(attr);
  353. int nr = sensor_attr->nr;
  354. int index = sensor_attr->index;
  355. struct w83l786ng_data *data = w83l786ng_update_device(dev);
  356. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
  357. }
  358. static ssize_t
  359. store_temp(struct device *dev, struct device_attribute *attr,
  360. const char *buf, size_t count)
  361. {
  362. struct sensor_device_attribute_2 *sensor_attr =
  363. to_sensor_dev_attr_2(attr);
  364. int nr = sensor_attr->nr;
  365. int index = sensor_attr->index;
  366. struct w83l786ng_data *data = dev_get_drvdata(dev);
  367. struct i2c_client *client = data->client;
  368. long val;
  369. int err;
  370. err = kstrtol(buf, 10, &val);
  371. if (err)
  372. return err;
  373. mutex_lock(&data->update_lock);
  374. data->temp[nr][index] = TEMP_TO_REG(val);
  375. w83l786ng_write_value(client, W83L786NG_REG_TEMP[nr][index],
  376. data->temp[nr][index]);
  377. mutex_unlock(&data->update_lock);
  378. return count;
  379. }
  380. static struct sensor_device_attribute_2 sda_temp_input[] = {
  381. SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
  382. SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
  383. };
  384. static struct sensor_device_attribute_2 sda_temp_max[] = {
  385. SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
  386. show_temp, store_temp, 0, 1),
  387. SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR,
  388. show_temp, store_temp, 1, 1),
  389. };
  390. static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
  391. SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
  392. show_temp, store_temp, 0, 2),
  393. SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
  394. show_temp, store_temp, 1, 2),
  395. };
  396. #define show_pwm_reg(reg) \
  397. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  398. char *buf) \
  399. { \
  400. struct w83l786ng_data *data = w83l786ng_update_device(dev); \
  401. int nr = to_sensor_dev_attr(attr)->index; \
  402. return sprintf(buf, "%d\n", data->reg[nr]); \
  403. }
  404. show_pwm_reg(pwm_mode)
  405. show_pwm_reg(pwm_enable)
  406. show_pwm_reg(pwm)
  407. static ssize_t
  408. store_pwm_mode(struct device *dev, struct device_attribute *attr,
  409. const char *buf, size_t count)
  410. {
  411. int nr = to_sensor_dev_attr(attr)->index;
  412. struct w83l786ng_data *data = dev_get_drvdata(dev);
  413. struct i2c_client *client = data->client;
  414. u8 reg;
  415. unsigned long val;
  416. int err;
  417. err = kstrtoul(buf, 10, &val);
  418. if (err)
  419. return err;
  420. if (val > 1)
  421. return -EINVAL;
  422. mutex_lock(&data->update_lock);
  423. data->pwm_mode[nr] = val;
  424. reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
  425. reg &= ~(1 << W83L786NG_PWM_MODE_SHIFT[nr]);
  426. if (!val)
  427. reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr];
  428. w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
  429. mutex_unlock(&data->update_lock);
  430. return count;
  431. }
  432. static ssize_t
  433. store_pwm(struct device *dev, struct device_attribute *attr,
  434. const char *buf, size_t count)
  435. {
  436. int nr = to_sensor_dev_attr(attr)->index;
  437. struct w83l786ng_data *data = dev_get_drvdata(dev);
  438. struct i2c_client *client = data->client;
  439. unsigned long val;
  440. int err;
  441. err = kstrtoul(buf, 10, &val);
  442. if (err)
  443. return err;
  444. val = clamp_val(val, 0, 255);
  445. val = DIV_ROUND_CLOSEST(val, 0x11);
  446. mutex_lock(&data->update_lock);
  447. data->pwm[nr] = val * 0x11;
  448. val |= w83l786ng_read_value(client, W83L786NG_REG_PWM[nr]) & 0xf0;
  449. w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val);
  450. mutex_unlock(&data->update_lock);
  451. return count;
  452. }
  453. static ssize_t
  454. store_pwm_enable(struct device *dev, struct device_attribute *attr,
  455. const char *buf, size_t count)
  456. {
  457. int nr = to_sensor_dev_attr(attr)->index;
  458. struct w83l786ng_data *data = dev_get_drvdata(dev);
  459. struct i2c_client *client = data->client;
  460. u8 reg;
  461. unsigned long val;
  462. int err;
  463. err = kstrtoul(buf, 10, &val);
  464. if (err)
  465. return err;
  466. if (!val || val > 2) /* only modes 1 and 2 are supported */
  467. return -EINVAL;
  468. mutex_lock(&data->update_lock);
  469. reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
  470. data->pwm_enable[nr] = val;
  471. reg &= ~(0x03 << W83L786NG_PWM_ENABLE_SHIFT[nr]);
  472. reg |= (val - 1) << W83L786NG_PWM_ENABLE_SHIFT[nr];
  473. w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
  474. mutex_unlock(&data->update_lock);
  475. return count;
  476. }
  477. static struct sensor_device_attribute sda_pwm[] = {
  478. SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
  479. SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
  480. };
  481. static struct sensor_device_attribute sda_pwm_mode[] = {
  482. SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
  483. store_pwm_mode, 0),
  484. SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
  485. store_pwm_mode, 1),
  486. };
  487. static struct sensor_device_attribute sda_pwm_enable[] = {
  488. SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
  489. store_pwm_enable, 0),
  490. SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
  491. store_pwm_enable, 1),
  492. };
  493. /* For Smart Fan I/Thermal Cruise and Smart Fan II */
  494. static ssize_t
  495. show_tolerance(struct device *dev, struct device_attribute *attr, char *buf)
  496. {
  497. int nr = to_sensor_dev_attr(attr)->index;
  498. struct w83l786ng_data *data = w83l786ng_update_device(dev);
  499. return sprintf(buf, "%ld\n", (long)data->tolerance[nr]);
  500. }
  501. static ssize_t
  502. store_tolerance(struct device *dev, struct device_attribute *attr,
  503. const char *buf, size_t count)
  504. {
  505. int nr = to_sensor_dev_attr(attr)->index;
  506. struct w83l786ng_data *data = dev_get_drvdata(dev);
  507. struct i2c_client *client = data->client;
  508. u8 tol_tmp, tol_mask;
  509. unsigned long val;
  510. int err;
  511. err = kstrtoul(buf, 10, &val);
  512. if (err)
  513. return err;
  514. mutex_lock(&data->update_lock);
  515. tol_mask = w83l786ng_read_value(client,
  516. W83L786NG_REG_TOLERANCE) & ((nr == 1) ? 0x0f : 0xf0);
  517. tol_tmp = clamp_val(val, 0, 15);
  518. tol_tmp &= 0x0f;
  519. data->tolerance[nr] = tol_tmp;
  520. if (nr == 1)
  521. tol_tmp <<= 4;
  522. w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE,
  523. tol_mask | tol_tmp);
  524. mutex_unlock(&data->update_lock);
  525. return count;
  526. }
  527. static struct sensor_device_attribute sda_tolerance[] = {
  528. SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO,
  529. show_tolerance, store_tolerance, 0),
  530. SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO,
  531. show_tolerance, store_tolerance, 1),
  532. };
  533. #define IN_UNIT_ATTRS(X) \
  534. &sda_in_input[X].dev_attr.attr, \
  535. &sda_in_min[X].dev_attr.attr, \
  536. &sda_in_max[X].dev_attr.attr
  537. #define FAN_UNIT_ATTRS(X) \
  538. &sda_fan_input[X].dev_attr.attr, \
  539. &sda_fan_min[X].dev_attr.attr, \
  540. &sda_fan_div[X].dev_attr.attr
  541. #define TEMP_UNIT_ATTRS(X) \
  542. &sda_temp_input[X].dev_attr.attr, \
  543. &sda_temp_max[X].dev_attr.attr, \
  544. &sda_temp_max_hyst[X].dev_attr.attr
  545. #define PWM_UNIT_ATTRS(X) \
  546. &sda_pwm[X].dev_attr.attr, \
  547. &sda_pwm_mode[X].dev_attr.attr, \
  548. &sda_pwm_enable[X].dev_attr.attr
  549. #define TOLERANCE_UNIT_ATTRS(X) \
  550. &sda_tolerance[X].dev_attr.attr
  551. static struct attribute *w83l786ng_attrs[] = {
  552. IN_UNIT_ATTRS(0),
  553. IN_UNIT_ATTRS(1),
  554. IN_UNIT_ATTRS(2),
  555. FAN_UNIT_ATTRS(0),
  556. FAN_UNIT_ATTRS(1),
  557. TEMP_UNIT_ATTRS(0),
  558. TEMP_UNIT_ATTRS(1),
  559. PWM_UNIT_ATTRS(0),
  560. PWM_UNIT_ATTRS(1),
  561. TOLERANCE_UNIT_ATTRS(0),
  562. TOLERANCE_UNIT_ATTRS(1),
  563. NULL
  564. };
  565. ATTRIBUTE_GROUPS(w83l786ng);
  566. static int
  567. w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info)
  568. {
  569. struct i2c_adapter *adapter = client->adapter;
  570. u16 man_id;
  571. u8 chip_id;
  572. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  573. return -ENODEV;
  574. /* Detection */
  575. if ((w83l786ng_read_value(client, W83L786NG_REG_CONFIG) & 0x80)) {
  576. dev_dbg(&adapter->dev, "W83L786NG detection failed at 0x%02x\n",
  577. client->addr);
  578. return -ENODEV;
  579. }
  580. /* Identification */
  581. man_id = (w83l786ng_read_value(client, W83L786NG_REG_MAN_ID1) << 8) +
  582. w83l786ng_read_value(client, W83L786NG_REG_MAN_ID2);
  583. chip_id = w83l786ng_read_value(client, W83L786NG_REG_CHIP_ID);
  584. if (man_id != 0x5CA3 || /* Winbond */
  585. chip_id != 0x80) { /* W83L786NG */
  586. dev_dbg(&adapter->dev,
  587. "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
  588. man_id, chip_id);
  589. return -ENODEV;
  590. }
  591. strlcpy(info->type, "w83l786ng", I2C_NAME_SIZE);
  592. return 0;
  593. }
  594. static void w83l786ng_init_client(struct i2c_client *client)
  595. {
  596. u8 tmp;
  597. if (reset)
  598. w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
  599. /* Start monitoring */
  600. tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
  601. if (!(tmp & 0x01))
  602. w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01);
  603. }
  604. static int
  605. w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id)
  606. {
  607. struct device *dev = &client->dev;
  608. struct w83l786ng_data *data;
  609. struct device *hwmon_dev;
  610. int i;
  611. u8 reg_tmp;
  612. data = devm_kzalloc(dev, sizeof(struct w83l786ng_data), GFP_KERNEL);
  613. if (!data)
  614. return -ENOMEM;
  615. data->client = client;
  616. mutex_init(&data->update_lock);
  617. /* Initialize the chip */
  618. w83l786ng_init_client(client);
  619. /* A few vars need to be filled upon startup */
  620. for (i = 0; i < 2; i++) {
  621. data->fan_min[i] = w83l786ng_read_value(client,
  622. W83L786NG_REG_FAN_MIN(i));
  623. }
  624. /* Update the fan divisor */
  625. reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
  626. data->fan_div[0] = reg_tmp & 0x07;
  627. data->fan_div[1] = (reg_tmp >> 4) & 0x07;
  628. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  629. data,
  630. w83l786ng_groups);
  631. return PTR_ERR_OR_ZERO(hwmon_dev);
  632. }
  633. static const struct i2c_device_id w83l786ng_id[] = {
  634. { "w83l786ng", 0 },
  635. { }
  636. };
  637. MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
  638. static struct i2c_driver w83l786ng_driver = {
  639. .class = I2C_CLASS_HWMON,
  640. .driver = {
  641. .name = "w83l786ng",
  642. },
  643. .probe = w83l786ng_probe,
  644. .id_table = w83l786ng_id,
  645. .detect = w83l786ng_detect,
  646. .address_list = normal_i2c,
  647. };
  648. module_i2c_driver(w83l786ng_driver);
  649. MODULE_AUTHOR("Kevin Lo");
  650. MODULE_DESCRIPTION("w83l786ng driver");
  651. MODULE_LICENSE("GPL");