PageRenderTime 146ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/hwmon/w83l786ng.c

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