Source
133
133
134
134
/*
135
135
* Chip dependent ADC conversion time, in uS
136
136
*/
137
137
#define SMM665_ADC_WAIT_SMM665 70
138
138
#define SMM665_ADC_WAIT_SMM766 185
139
139
140
140
struct smm665_data {
141
141
enum chips type;
142
142
int conversion_time; /* ADC conversion time */
143
-
struct device *hwmon_dev;
143
+
struct i2c_client *client;
144
144
struct mutex update_lock;
145
145
bool valid;
146
146
unsigned long last_updated; /* in jiffies */
147
147
u16 adc[SMM665_NUM_ADC]; /* adc values (raw) */
148
148
u16 faults; /* fault status */
149
149
/* The following values are in mV */
150
150
int critical_min_limit[SMM665_NUM_ADC];
151
151
int alarm_min_limit[SMM665_NUM_ADC];
152
152
int critical_max_limit[SMM665_NUM_ADC];
153
153
int alarm_max_limit[SMM665_NUM_ADC];
232
232
dev_dbg(&client->dev, "Unexpected RADC: Expected %d got %d",
233
233
adc, radc);
234
234
return -EIO;
235
235
}
236
236
237
237
return rv & SMM665_ADC_MASK;
238
238
}
239
239
240
240
static struct smm665_data *smm665_update_device(struct device *dev)
241
241
{
242
-
struct i2c_client *client = to_i2c_client(dev);
243
-
struct smm665_data *data = i2c_get_clientdata(client);
242
+
struct smm665_data *data = dev_get_drvdata(dev);
243
+
struct i2c_client *client = data->client;
244
244
struct smm665_data *ret = data;
245
245
246
246
mutex_lock(&data->update_lock);
247
247
248
248
if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
249
249
int i, val;
250
250
251
251
/*
252
252
* read status registers
253
253
*/
308
308
/* If we get here, the developer messed up */
309
309
WARN_ON_ONCE(1);
310
310
break;
311
311
}
312
312
313
313
return val;
314
314
}
315
315
316
316
static int smm665_get_min(struct device *dev, int index)
317
317
{
318
-
struct i2c_client *client = to_i2c_client(dev);
319
-
struct smm665_data *data = i2c_get_clientdata(client);
318
+
struct smm665_data *data = dev_get_drvdata(dev);
320
319
321
320
return data->alarm_min_limit[index];
322
321
}
323
322
324
323
static int smm665_get_max(struct device *dev, int index)
325
324
{
326
-
struct i2c_client *client = to_i2c_client(dev);
327
-
struct smm665_data *data = i2c_get_clientdata(client);
325
+
struct smm665_data *data = dev_get_drvdata(dev);
328
326
329
327
return data->alarm_max_limit[index];
330
328
}
331
329
332
330
static int smm665_get_lcrit(struct device *dev, int index)
333
331
{
334
-
struct i2c_client *client = to_i2c_client(dev);
335
-
struct smm665_data *data = i2c_get_clientdata(client);
332
+
struct smm665_data *data = dev_get_drvdata(dev);
336
333
337
334
return data->critical_min_limit[index];
338
335
}
339
336
340
337
static int smm665_get_crit(struct device *dev, int index)
341
338
{
342
-
struct i2c_client *client = to_i2c_client(dev);
343
-
struct smm665_data *data = i2c_get_clientdata(client);
339
+
struct smm665_data *data = dev_get_drvdata(dev);
344
340
345
341
return data->critical_max_limit[index];
346
342
}
347
343
348
344
static ssize_t smm665_show_crit_alarm(struct device *dev,
349
345
struct device_attribute *da, char *buf)
350
346
{
351
347
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
352
348
struct smm665_data *data = smm665_update_device(dev);
353
349
int val = 0;
479
475
SMM665_ATTR(temp1, min, SMM665_MISC16_ADC_DATA_INT_TEMP);
480
476
SMM665_ATTR(temp1, max, SMM665_MISC16_ADC_DATA_INT_TEMP);
481
477
SMM665_ATTR(temp1, lcrit, SMM665_MISC16_ADC_DATA_INT_TEMP);
482
478
SMM665_ATTR(temp1, crit, SMM665_MISC16_ADC_DATA_INT_TEMP);
483
479
SMM665_ATTR(temp1, crit_alarm, SMM665_FAULT_TEMP);
484
480
485
481
/*
486
482
* Finally, construct an array of pointers to members of the above objects,
487
483
* as required for sysfs_create_group()
488
484
*/
489
-
static struct attribute *smm665_attributes[] = {
485
+
static struct attribute *smm665_attrs[] = {
490
486
&sensor_dev_attr_in1_input.dev_attr.attr,
491
487
&sensor_dev_attr_in1_min.dev_attr.attr,
492
488
&sensor_dev_attr_in1_max.dev_attr.attr,
493
489
&sensor_dev_attr_in1_lcrit.dev_attr.attr,
494
490
&sensor_dev_attr_in1_crit.dev_attr.attr,
495
491
&sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
496
492
497
493
&sensor_dev_attr_in2_input.dev_attr.attr,
498
494
&sensor_dev_attr_in2_min.dev_attr.attr,
499
495
&sensor_dev_attr_in2_max.dev_attr.attr,
560
556
&sensor_dev_attr_temp1_input.dev_attr.attr,
561
557
&sensor_dev_attr_temp1_min.dev_attr.attr,
562
558
&sensor_dev_attr_temp1_max.dev_attr.attr,
563
559
&sensor_dev_attr_temp1_lcrit.dev_attr.attr,
564
560
&sensor_dev_attr_temp1_crit.dev_attr.attr,
565
561
&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
566
562
567
563
NULL,
568
564
};
569
565
570
-
static const struct attribute_group smm665_group = {
571
-
.attrs = smm665_attributes,
572
-
};
566
+
ATTRIBUTE_GROUPS(smm665);
573
567
574
568
static int smm665_probe(struct i2c_client *client,
575
569
const struct i2c_device_id *id)
576
570
{
577
571
struct i2c_adapter *adapter = client->adapter;
578
572
struct smm665_data *data;
573
+
struct device *hwmon_dev;
579
574
int i, ret;
580
575
581
576
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
582
577
| I2C_FUNC_SMBUS_WORD_DATA))
583
578
return -ENODEV;
584
579
585
580
if (i2c_smbus_read_byte_data(client, SMM665_ADOC_ENABLE) < 0)
586
581
return -ENODEV;
587
582
588
583
data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
589
584
if (!data)
590
585
return -ENOMEM;
591
586
592
587
i2c_set_clientdata(client, data);
593
588
mutex_init(&data->update_lock);
594
589
590
+
data->client = client;
595
591
data->type = id->driver_data;
596
592
data->cmdreg = i2c_new_dummy(adapter, (client->addr & ~SMM665_REGMASK)
597
593
| SMM665_CMDREG_BASE);
598
594
if (!data->cmdreg)
599
595
return -ENOMEM;
600
596
601
597
switch (data->type) {
602
598
case smm465:
603
599
case smm665:
604
600
data->conversion_time = SMM665_ADC_WAIT_SMM665;
655
651
= smm665_convert(val, i);
656
652
val = smm665_read16(client, SMM665_LIMIT_BASE + i * 8 + 6);
657
653
if (unlikely(val < 0))
658
654
goto out_unregister;
659
655
if (smm665_is_critical(val))
660
656
data->critical_max_limit[i] = smm665_convert(val, i);
661
657
else
662
658
data->alarm_max_limit[i] = smm665_convert(val, i);
663
659
}
664
660
665
-
/* Register sysfs hooks */
666
-
ret = sysfs_create_group(&client->dev.kobj, &smm665_group);
667
-
if (ret)
661
+
hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
662
+
client->name, data,
663
+
smm665_groups);
664
+
if (IS_ERR(hwmon_dev)) {
665
+
ret = PTR_ERR(hwmon_dev);
668
666
goto out_unregister;
669
-
670
-
data->hwmon_dev = hwmon_device_register(&client->dev);
671
-
if (IS_ERR(data->hwmon_dev)) {
672
-
ret = PTR_ERR(data->hwmon_dev);
673
-
goto out_remove_group;
674
667
}
675
668
676
669
return 0;
677
670
678
-
out_remove_group:
679
-
sysfs_remove_group(&client->dev.kobj, &smm665_group);
680
671
out_unregister:
681
672
i2c_unregister_device(data->cmdreg);
682
673
return ret;
683
674
}
684
675
685
676
static int smm665_remove(struct i2c_client *client)
686
677
{
687
678
struct smm665_data *data = i2c_get_clientdata(client);
688
679
689
680
i2c_unregister_device(data->cmdreg);
690
-
hwmon_device_unregister(data->hwmon_dev);
691
-
sysfs_remove_group(&client->dev.kobj, &smm665_group);
692
-
693
681
return 0;
694
682
}
695
683
696
684
static const struct i2c_device_id smm665_id[] = {
697
685
{"smm465", smm465},
698
686
{"smm665", smm665},
699
687
{"smm665c", smm665c},
700
688
{"smm764", smm764},
701
689
{"smm766", smm766},
702
690
{}