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