w83l786ng.c 20 KB

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