w83l786ng.c 23 KB

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