aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/w83l786ng.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hwmon/w83l786ng.c')
-rw-r--r--drivers/hwmon/w83l786ng.c289
1 files changed, 126 insertions, 163 deletions
diff --git a/drivers/hwmon/w83l786ng.c b/drivers/hwmon/w83l786ng.c
index 32487c19cbfc..330299613d38 100644
--- a/drivers/hwmon/w83l786ng.c
+++ b/drivers/hwmon/w83l786ng.c
@@ -124,7 +124,7 @@ DIV_TO_REG(long val)
124} 124}
125 125
126struct w83l786ng_data { 126struct w83l786ng_data {
127 struct device *hwmon_dev; 127 struct i2c_client *client;
128 struct mutex update_lock; 128 struct mutex update_lock;
129 char valid; /* !=0 if following fields are valid */ 129 char valid; /* !=0 if following fields are valid */
130 unsigned long last_updated; /* In jiffies */ 130 unsigned long last_updated; /* In jiffies */
@@ -148,32 +148,6 @@ struct w83l786ng_data {
148 u8 tolerance[2]; 148 u8 tolerance[2];
149}; 149};
150 150
151static int w83l786ng_probe(struct i2c_client *client,
152 const struct i2c_device_id *id);
153static int w83l786ng_detect(struct i2c_client *client,
154 struct i2c_board_info *info);
155static int w83l786ng_remove(struct i2c_client *client);
156static void w83l786ng_init_client(struct i2c_client *client);
157static struct w83l786ng_data *w83l786ng_update_device(struct device *dev);
158
159static const struct i2c_device_id w83l786ng_id[] = {
160 { "w83l786ng", 0 },
161 { }
162};
163MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
164
165static struct i2c_driver w83l786ng_driver = {
166 .class = I2C_CLASS_HWMON,
167 .driver = {
168 .name = "w83l786ng",
169 },
170 .probe = w83l786ng_probe,
171 .remove = w83l786ng_remove,
172 .id_table = w83l786ng_id,
173 .detect = w83l786ng_detect,
174 .address_list = normal_i2c,
175};
176
177static u8 151static u8
178w83l786ng_read_value(struct i2c_client *client, u8 reg) 152w83l786ng_read_value(struct i2c_client *client, u8 reg)
179{ 153{
@@ -186,6 +160,77 @@ w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value)
186 return i2c_smbus_write_byte_data(client, reg, value); 160 return i2c_smbus_write_byte_data(client, reg, value);
187} 161}
188 162
163static struct w83l786ng_data *w83l786ng_update_device(struct device *dev)
164{
165 struct w83l786ng_data *data = dev_get_drvdata(dev);
166 struct i2c_client *client = data->client;
167 int i, j;
168 u8 reg_tmp, pwmcfg;
169
170 mutex_lock(&data->update_lock);
171 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
172 || !data->valid) {
173 dev_dbg(&client->dev, "Updating w83l786ng data.\n");
174
175 /* Update the voltages measured value and limits */
176 for (i = 0; i < 3; i++) {
177 data->in[i] = w83l786ng_read_value(client,
178 W83L786NG_REG_IN(i));
179 data->in_min[i] = w83l786ng_read_value(client,
180 W83L786NG_REG_IN_MIN(i));
181 data->in_max[i] = w83l786ng_read_value(client,
182 W83L786NG_REG_IN_MAX(i));
183 }
184
185 /* Update the fan counts and limits */
186 for (i = 0; i < 2; i++) {
187 data->fan[i] = w83l786ng_read_value(client,
188 W83L786NG_REG_FAN(i));
189 data->fan_min[i] = w83l786ng_read_value(client,
190 W83L786NG_REG_FAN_MIN(i));
191 }
192
193 /* Update the fan divisor */
194 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
195 data->fan_div[0] = reg_tmp & 0x07;
196 data->fan_div[1] = (reg_tmp >> 4) & 0x07;
197
198 pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
199 for (i = 0; i < 2; i++) {
200 data->pwm_mode[i] =
201 ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
202 ? 0 : 1;
203 data->pwm_enable[i] =
204 ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1;
205 data->pwm[i] =
206 (w83l786ng_read_value(client, W83L786NG_REG_PWM[i])
207 & 0x0f) * 0x11;
208 }
209
210
211 /* Update the temperature sensors */
212 for (i = 0; i < 2; i++) {
213 for (j = 0; j < 3; j++) {
214 data->temp[i][j] = w83l786ng_read_value(client,
215 W83L786NG_REG_TEMP[i][j]);
216 }
217 }
218
219 /* Update Smart Fan I/II tolerance */
220 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE);
221 data->tolerance[0] = reg_tmp & 0x0f;
222 data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
223
224 data->last_updated = jiffies;
225 data->valid = 1;
226
227 }
228
229 mutex_unlock(&data->update_lock);
230
231 return data;
232}
233
189/* following are the sysfs callback functions */ 234/* following are the sysfs callback functions */
190#define show_in_reg(reg) \ 235#define show_in_reg(reg) \
191static ssize_t \ 236static ssize_t \
@@ -207,8 +252,8 @@ store_in_##reg(struct device *dev, struct device_attribute *attr, \
207 const char *buf, size_t count) \ 252 const char *buf, size_t count) \
208{ \ 253{ \
209 int nr = to_sensor_dev_attr(attr)->index; \ 254 int nr = to_sensor_dev_attr(attr)->index; \
210 struct i2c_client *client = to_i2c_client(dev); \ 255 struct w83l786ng_data *data = dev_get_drvdata(dev); \
211 struct w83l786ng_data *data = i2c_get_clientdata(client); \ 256 struct i2c_client *client = data->client; \
212 unsigned long val; \ 257 unsigned long val; \
213 int err = kstrtoul(buf, 10, &val); \ 258 int err = kstrtoul(buf, 10, &val); \
214 if (err) \ 259 if (err) \
@@ -260,8 +305,8 @@ store_fan_min(struct device *dev, struct device_attribute *attr,
260 const char *buf, size_t count) 305 const char *buf, size_t count)
261{ 306{
262 int nr = to_sensor_dev_attr(attr)->index; 307 int nr = to_sensor_dev_attr(attr)->index;
263 struct i2c_client *client = to_i2c_client(dev); 308 struct w83l786ng_data *data = dev_get_drvdata(dev);
264 struct w83l786ng_data *data = i2c_get_clientdata(client); 309 struct i2c_client *client = data->client;
265 unsigned long val; 310 unsigned long val;
266 int err; 311 int err;
267 312
@@ -298,8 +343,8 @@ store_fan_div(struct device *dev, struct device_attribute *attr,
298 const char *buf, size_t count) 343 const char *buf, size_t count)
299{ 344{
300 int nr = to_sensor_dev_attr(attr)->index; 345 int nr = to_sensor_dev_attr(attr)->index;
301 struct i2c_client *client = to_i2c_client(dev); 346 struct w83l786ng_data *data = dev_get_drvdata(dev);
302 struct w83l786ng_data *data = i2c_get_clientdata(client); 347 struct i2c_client *client = data->client;
303 348
304 unsigned long min; 349 unsigned long min;
305 u8 tmp_fan_div; 350 u8 tmp_fan_div;
@@ -389,8 +434,8 @@ store_temp(struct device *dev, struct device_attribute *attr,
389 to_sensor_dev_attr_2(attr); 434 to_sensor_dev_attr_2(attr);
390 int nr = sensor_attr->nr; 435 int nr = sensor_attr->nr;
391 int index = sensor_attr->index; 436 int index = sensor_attr->index;
392 struct i2c_client *client = to_i2c_client(dev); 437 struct w83l786ng_data *data = dev_get_drvdata(dev);
393 struct w83l786ng_data *data = i2c_get_clientdata(client); 438 struct i2c_client *client = data->client;
394 long val; 439 long val;
395 int err; 440 int err;
396 441
@@ -444,8 +489,8 @@ store_pwm_mode(struct device *dev, struct device_attribute *attr,
444 const char *buf, size_t count) 489 const char *buf, size_t count)
445{ 490{
446 int nr = to_sensor_dev_attr(attr)->index; 491 int nr = to_sensor_dev_attr(attr)->index;
447 struct i2c_client *client = to_i2c_client(dev); 492 struct w83l786ng_data *data = dev_get_drvdata(dev);
448 struct w83l786ng_data *data = i2c_get_clientdata(client); 493 struct i2c_client *client = data->client;
449 u8 reg; 494 u8 reg;
450 unsigned long val; 495 unsigned long val;
451 int err; 496 int err;
@@ -472,8 +517,8 @@ store_pwm(struct device *dev, struct device_attribute *attr,
472 const char *buf, size_t count) 517 const char *buf, size_t count)
473{ 518{
474 int nr = to_sensor_dev_attr(attr)->index; 519 int nr = to_sensor_dev_attr(attr)->index;
475 struct i2c_client *client = to_i2c_client(dev); 520 struct w83l786ng_data *data = dev_get_drvdata(dev);
476 struct w83l786ng_data *data = i2c_get_clientdata(client); 521 struct i2c_client *client = data->client;
477 unsigned long val; 522 unsigned long val;
478 int err; 523 int err;
479 524
@@ -496,8 +541,8 @@ store_pwm_enable(struct device *dev, struct device_attribute *attr,
496 const char *buf, size_t count) 541 const char *buf, size_t count)
497{ 542{
498 int nr = to_sensor_dev_attr(attr)->index; 543 int nr = to_sensor_dev_attr(attr)->index;
499 struct i2c_client *client = to_i2c_client(dev); 544 struct w83l786ng_data *data = dev_get_drvdata(dev);
500 struct w83l786ng_data *data = i2c_get_clientdata(client); 545 struct i2c_client *client = data->client;
501 u8 reg; 546 u8 reg;
502 unsigned long val; 547 unsigned long val;
503 int err; 548 int err;
@@ -552,8 +597,8 @@ store_tolerance(struct device *dev, struct device_attribute *attr,
552 const char *buf, size_t count) 597 const char *buf, size_t count)
553{ 598{
554 int nr = to_sensor_dev_attr(attr)->index; 599 int nr = to_sensor_dev_attr(attr)->index;
555 struct i2c_client *client = to_i2c_client(dev); 600 struct w83l786ng_data *data = dev_get_drvdata(dev);
556 struct w83l786ng_data *data = i2c_get_clientdata(client); 601 struct i2c_client *client = data->client;
557 u8 tol_tmp, tol_mask; 602 u8 tol_tmp, tol_mask;
558 unsigned long val; 603 unsigned long val;
559 int err; 604 int err;
@@ -608,7 +653,7 @@ static struct sensor_device_attribute sda_tolerance[] = {
608#define TOLERANCE_UNIT_ATTRS(X) \ 653#define TOLERANCE_UNIT_ATTRS(X) \
609 &sda_tolerance[X].dev_attr.attr 654 &sda_tolerance[X].dev_attr.attr
610 655
611static struct attribute *w83l786ng_attributes[] = { 656static struct attribute *w83l786ng_attrs[] = {
612 IN_UNIT_ATTRS(0), 657 IN_UNIT_ATTRS(0),
613 IN_UNIT_ATTRS(1), 658 IN_UNIT_ATTRS(1),
614 IN_UNIT_ATTRS(2), 659 IN_UNIT_ATTRS(2),
@@ -623,9 +668,7 @@ static struct attribute *w83l786ng_attributes[] = {
623 NULL 668 NULL
624}; 669};
625 670
626static const struct attribute_group w83l786ng_group = { 671ATTRIBUTE_GROUPS(w83l786ng);
627 .attrs = w83l786ng_attributes,
628};
629 672
630static int 673static int
631w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info) 674w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info)
@@ -662,20 +705,33 @@ w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info)
662 return 0; 705 return 0;
663} 706}
664 707
708static void w83l786ng_init_client(struct i2c_client *client)
709{
710 u8 tmp;
711
712 if (reset)
713 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
714
715 /* Start monitoring */
716 tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
717 if (!(tmp & 0x01))
718 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01);
719}
720
665static int 721static int
666w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id) 722w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id)
667{ 723{
668 struct device *dev = &client->dev; 724 struct device *dev = &client->dev;
669 struct w83l786ng_data *data; 725 struct w83l786ng_data *data;
670 int i, err = 0; 726 struct device *hwmon_dev;
727 int i;
671 u8 reg_tmp; 728 u8 reg_tmp;
672 729
673 data = devm_kzalloc(&client->dev, sizeof(struct w83l786ng_data), 730 data = devm_kzalloc(dev, sizeof(struct w83l786ng_data), GFP_KERNEL);
674 GFP_KERNEL);
675 if (!data) 731 if (!data)
676 return -ENOMEM; 732 return -ENOMEM;
677 733
678 i2c_set_clientdata(client, data); 734 data->client = client;
679 mutex_init(&data->update_lock); 735 mutex_init(&data->update_lock);
680 736
681 /* Initialize the chip */ 737 /* Initialize the chip */
@@ -692,121 +748,28 @@ w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id)
692 data->fan_div[0] = reg_tmp & 0x07; 748 data->fan_div[0] = reg_tmp & 0x07;
693 data->fan_div[1] = (reg_tmp >> 4) & 0x07; 749 data->fan_div[1] = (reg_tmp >> 4) & 0x07;
694 750
695 /* Register sysfs hooks */ 751 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
696 err = sysfs_create_group(&client->dev.kobj, &w83l786ng_group); 752 data,
697 if (err) 753 w83l786ng_groups);
698 goto exit_remove; 754 return PTR_ERR_OR_ZERO(hwmon_dev);
699
700 data->hwmon_dev = hwmon_device_register(dev);
701 if (IS_ERR(data->hwmon_dev)) {
702 err = PTR_ERR(data->hwmon_dev);
703 goto exit_remove;
704 }
705
706 return 0;
707
708 /* Unregister sysfs hooks */
709
710exit_remove:
711 sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
712 return err;
713}
714
715static int
716w83l786ng_remove(struct i2c_client *client)
717{
718 struct w83l786ng_data *data = i2c_get_clientdata(client);
719
720 hwmon_device_unregister(data->hwmon_dev);
721 sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
722
723 return 0;
724} 755}
725 756
726static void 757static const struct i2c_device_id w83l786ng_id[] = {
727w83l786ng_init_client(struct i2c_client *client) 758 { "w83l786ng", 0 },
728{ 759 { }
729 u8 tmp; 760};
730 761MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
731 if (reset)
732 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
733
734 /* Start monitoring */
735 tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
736 if (!(tmp & 0x01))
737 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01);
738}
739
740static struct w83l786ng_data *w83l786ng_update_device(struct device *dev)
741{
742 struct i2c_client *client = to_i2c_client(dev);
743 struct w83l786ng_data *data = i2c_get_clientdata(client);
744 int i, j;
745 u8 reg_tmp, pwmcfg;
746
747 mutex_lock(&data->update_lock);
748 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
749 || !data->valid) {
750 dev_dbg(&client->dev, "Updating w83l786ng data.\n");
751
752 /* Update the voltages measured value and limits */
753 for (i = 0; i < 3; i++) {
754 data->in[i] = w83l786ng_read_value(client,
755 W83L786NG_REG_IN(i));
756 data->in_min[i] = w83l786ng_read_value(client,
757 W83L786NG_REG_IN_MIN(i));
758 data->in_max[i] = w83l786ng_read_value(client,
759 W83L786NG_REG_IN_MAX(i));
760 }
761
762 /* Update the fan counts and limits */
763 for (i = 0; i < 2; i++) {
764 data->fan[i] = w83l786ng_read_value(client,
765 W83L786NG_REG_FAN(i));
766 data->fan_min[i] = w83l786ng_read_value(client,
767 W83L786NG_REG_FAN_MIN(i));
768 }
769
770 /* Update the fan divisor */
771 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
772 data->fan_div[0] = reg_tmp & 0x07;
773 data->fan_div[1] = (reg_tmp >> 4) & 0x07;
774
775 pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
776 for (i = 0; i < 2; i++) {
777 data->pwm_mode[i] =
778 ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
779 ? 0 : 1;
780 data->pwm_enable[i] =
781 ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1;
782 data->pwm[i] =
783 (w83l786ng_read_value(client, W83L786NG_REG_PWM[i])
784 & 0x0f) * 0x11;
785 }
786
787
788 /* Update the temperature sensors */
789 for (i = 0; i < 2; i++) {
790 for (j = 0; j < 3; j++) {
791 data->temp[i][j] = w83l786ng_read_value(client,
792 W83L786NG_REG_TEMP[i][j]);
793 }
794 }
795
796 /* Update Smart Fan I/II tolerance */
797 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE);
798 data->tolerance[0] = reg_tmp & 0x0f;
799 data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
800
801 data->last_updated = jiffies;
802 data->valid = 1;
803
804 }
805
806 mutex_unlock(&data->update_lock);
807 762
808 return data; 763static struct i2c_driver w83l786ng_driver = {
809} 764 .class = I2C_CLASS_HWMON,
765 .driver = {
766 .name = "w83l786ng",
767 },
768 .probe = w83l786ng_probe,
769 .id_table = w83l786ng_id,
770 .detect = w83l786ng_detect,
771 .address_list = normal_i2c,
772};
810 773
811module_i2c_driver(w83l786ng_driver); 774module_i2c_driver(w83l786ng_driver);
812 775