#define HTU21_RH_MEASUREMENT_HM 0xE5
struct htu21 {
- struct device *hwmon_dev;
+ struct i2c_client *client;
struct mutex lock;
bool valid;
unsigned long last_update;
return ((15625 * ticks) >> 13) - 6000;
}
-static int htu21_update_measurements(struct i2c_client *client)
+static int htu21_update_measurements(struct device *dev)
{
+ struct htu21 *htu21 = dev_get_drvdata(dev);
+ struct i2c_client *client = htu21->client;
int ret = 0;
- struct htu21 *htu21 = i2c_get_clientdata(client);
mutex_lock(&htu21->lock);
static ssize_t htu21_show_temperature(struct device *dev,
struct device_attribute *attr, char *buf)
{
- struct i2c_client *client = to_i2c_client(dev);
- struct htu21 *htu21 = i2c_get_clientdata(client);
- int ret = htu21_update_measurements(client);
+ struct htu21 *htu21 = dev_get_drvdata(dev);
+ int ret;
+
+ ret = htu21_update_measurements(dev);
if (ret < 0)
return ret;
return sprintf(buf, "%d\n", htu21->temperature);
static ssize_t htu21_show_humidity(struct device *dev,
struct device_attribute *attr, char *buf)
{
- struct i2c_client *client = to_i2c_client(dev);
- struct htu21 *htu21 = i2c_get_clientdata(client);
- int ret = htu21_update_measurements(client);
+ struct htu21 *htu21 = dev_get_drvdata(dev);
+ int ret;
+
+ ret = htu21_update_measurements(dev);
if (ret < 0)
return ret;
return sprintf(buf, "%d\n", htu21->humidity);
static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
htu21_show_humidity, NULL, 0);
-static struct attribute *htu21_attributes[] = {
+static struct attribute *htu21_attrs[] = {
&sensor_dev_attr_temp1_input.dev_attr.attr,
&sensor_dev_attr_humidity1_input.dev_attr.attr,
NULL
};
-static const struct attribute_group htu21_group = {
- .attrs = htu21_attributes,
-};
+ATTRIBUTE_GROUPS(htu21);
static int htu21_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
+ struct device *dev = &client->dev;
struct htu21 *htu21;
- int err;
+ struct device *hwmon_dev;
if (!i2c_check_functionality(client->adapter,
I2C_FUNC_SMBUS_READ_WORD_DATA)) {
return -ENODEV;
}
- htu21 = devm_kzalloc(&client->dev, sizeof(*htu21), GFP_KERNEL);
+ htu21 = devm_kzalloc(dev, sizeof(*htu21), GFP_KERNEL);
if (!htu21)
return -ENOMEM;
- i2c_set_clientdata(client, htu21);
-
+ htu21->client = client;
mutex_init(&htu21->lock);
- err = sysfs_create_group(&client->dev.kobj, &htu21_group);
- if (err) {
- dev_dbg(&client->dev, "could not create sysfs files\n");
- return err;
- }
- htu21->hwmon_dev = hwmon_device_register(&client->dev);
- if (IS_ERR(htu21->hwmon_dev)) {
- dev_dbg(&client->dev, "unable to register hwmon device\n");
- err = PTR_ERR(htu21->hwmon_dev);
- goto error;
- }
-
- dev_info(&client->dev, "initialized\n");
-
- return 0;
-
-error:
- sysfs_remove_group(&client->dev.kobj, &htu21_group);
- return err;
-}
-
-static int htu21_remove(struct i2c_client *client)
-{
- struct htu21 *htu21 = i2c_get_clientdata(client);
-
- hwmon_device_unregister(htu21->hwmon_dev);
- sysfs_remove_group(&client->dev.kobj, &htu21_group);
-
- return 0;
+ hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
+ htu21,
+ htu21_groups);
+ return PTR_ERR_OR_ZERO(hwmon_dev);
}
static const struct i2c_device_id htu21_id[] = {
.name = "htu21",
},
.probe = htu21_probe,
- .remove = htu21_remove,
.id_table = htu21_id,
};