Commit a5afc18c authored by Axel Lin's avatar Axel Lin Committed by Guenter Roeck

hwmon: (hih6130) Convert to devm_hwmon_device_register_with_groups

Use ATTRIBUTE_GROUPS macro and devm_hwmon_device_register_with_groups() to
simplify the code a bit.
Signed-off-by: default avatarAxel Lin <axel.lin@ingics.com>
Tested-by: default avatarIain Paton <ipaton0@gmail.com>
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent 233adcef
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
* @write_length: length for I2C measurement request * @write_length: length for I2C measurement request
*/ */
struct hih6130 { struct hih6130 {
struct device *hwmon_dev; struct i2c_client *client;
struct mutex lock; struct mutex lock;
bool valid; bool valid;
unsigned long last_update; unsigned long last_update;
...@@ -62,7 +62,6 @@ struct hih6130 { ...@@ -62,7 +62,6 @@ struct hih6130 {
*/ */
static inline int hih6130_temp_ticks_to_millicelsius(int ticks) static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
{ {
ticks = ticks >> 2; ticks = ticks >> 2;
/* /*
* from data sheet section 5.0 * from data sheet section 5.0
...@@ -78,7 +77,6 @@ static inline int hih6130_temp_ticks_to_millicelsius(int ticks) ...@@ -78,7 +77,6 @@ static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
*/ */
static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks) static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
{ {
ticks &= ~0xC000; /* clear status bits */ ticks &= ~0xC000; /* clear status bits */
/* /*
* from data sheet section 4.0 * from data sheet section 4.0
...@@ -89,15 +87,16 @@ static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks) ...@@ -89,15 +87,16 @@ static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
/** /**
* hih6130_update_measurements() - get updated measurements from device * hih6130_update_measurements() - get updated measurements from device
* @client: I2C client device * @dev: device
* *
* Returns 0 on success, else negative errno. * Returns 0 on success, else negative errno.
*/ */
static int hih6130_update_measurements(struct i2c_client *client) static int hih6130_update_measurements(struct device *dev)
{ {
struct hih6130 *hih6130 = dev_get_drvdata(dev);
struct i2c_client *client = hih6130->client;
int ret = 0; int ret = 0;
int t; int t;
struct hih6130 *hih6130 = i2c_get_clientdata(client);
unsigned char tmp[4]; unsigned char tmp[4];
struct i2c_msg msgs[1] = { struct i2c_msg msgs[1] = {
{ {
...@@ -176,9 +175,10 @@ static ssize_t hih6130_show_temperature(struct device *dev, ...@@ -176,9 +175,10 @@ static ssize_t hih6130_show_temperature(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
char *buf) char *buf)
{ {
struct i2c_client *client = to_i2c_client(dev); struct hih6130 *hih6130 = dev_get_drvdata(dev);
struct hih6130 *hih6130 = i2c_get_clientdata(client); int ret;
int ret = hih6130_update_measurements(client);
ret = hih6130_update_measurements(dev);
if (ret < 0) if (ret < 0)
return ret; return ret;
return sprintf(buf, "%d\n", hih6130->temperature); return sprintf(buf, "%d\n", hih6130->temperature);
...@@ -196,9 +196,10 @@ static ssize_t hih6130_show_temperature(struct device *dev, ...@@ -196,9 +196,10 @@ static ssize_t hih6130_show_temperature(struct device *dev,
static ssize_t hih6130_show_humidity(struct device *dev, static ssize_t hih6130_show_humidity(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct i2c_client *client = to_i2c_client(dev); struct hih6130 *hih6130 = dev_get_drvdata(dev);
struct hih6130 *hih6130 = i2c_get_clientdata(client); int ret;
int ret = hih6130_update_measurements(client);
ret = hih6130_update_measurements(dev);
if (ret < 0) if (ret < 0)
return ret; return ret;
return sprintf(buf, "%d\n", hih6130->humidity); return sprintf(buf, "%d\n", hih6130->humidity);
...@@ -210,79 +211,37 @@ static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih6130_show_temperature, ...@@ -210,79 +211,37 @@ static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih6130_show_temperature,
static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity, static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity,
NULL, 0); NULL, 0);
static struct attribute *hih6130_attributes[] = { static struct attribute *hih6130_attrs[] = {
&sensor_dev_attr_temp1_input.dev_attr.attr, &sensor_dev_attr_temp1_input.dev_attr.attr,
&sensor_dev_attr_humidity1_input.dev_attr.attr, &sensor_dev_attr_humidity1_input.dev_attr.attr,
NULL NULL
}; };
static const struct attribute_group hih6130_attr_group = { ATTRIBUTE_GROUPS(hih6130);
.attrs = hih6130_attributes,
};
/**
* hih6130_probe() - probe device
* @client: I2C client device
* @id: device ID
*
* Called by the I2C core when an entry in the ID table matches a
* device's name.
* Returns 0 on success.
*/
static int hih6130_probe(struct i2c_client *client, static int hih6130_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
struct device *dev = &client->dev;
struct hih6130 *hih6130; struct hih6130 *hih6130;
int err; struct device *hwmon_dev;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
dev_err(&client->dev, "adapter does not support true I2C\n"); dev_err(&client->dev, "adapter does not support true I2C\n");
return -ENODEV; return -ENODEV;
} }
hih6130 = devm_kzalloc(&client->dev, sizeof(*hih6130), GFP_KERNEL); hih6130 = devm_kzalloc(dev, sizeof(*hih6130), GFP_KERNEL);
if (!hih6130) if (!hih6130)
return -ENOMEM; return -ENOMEM;
i2c_set_clientdata(client, hih6130); hih6130->client = client;
mutex_init(&hih6130->lock); mutex_init(&hih6130->lock);
err = sysfs_create_group(&client->dev.kobj, &hih6130_attr_group); hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
if (err) { hih6130,
dev_dbg(&client->dev, "could not create sysfs files\n"); hih6130_groups);
return err; return PTR_ERR_OR_ZERO(hwmon_dev);
}
hih6130->hwmon_dev = hwmon_device_register(&client->dev);
if (IS_ERR(hih6130->hwmon_dev)) {
dev_dbg(&client->dev, "unable to register hwmon device\n");
err = PTR_ERR(hih6130->hwmon_dev);
goto fail_remove_sysfs;
}
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
hih6130->write_length = 1;
return 0;
fail_remove_sysfs:
sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
return err;
}
/**
* hih6130_remove() - remove device
* @client: I2C client device
*/
static int hih6130_remove(struct i2c_client *client)
{
struct hih6130 *hih6130 = i2c_get_clientdata(client);
hwmon_device_unregister(hih6130->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
return 0;
} }
/* Device ID table */ /* Device ID table */
...@@ -295,7 +254,6 @@ MODULE_DEVICE_TABLE(i2c, hih6130_id); ...@@ -295,7 +254,6 @@ MODULE_DEVICE_TABLE(i2c, hih6130_id);
static struct i2c_driver hih6130_driver = { static struct i2c_driver hih6130_driver = {
.driver.name = "hih6130", .driver.name = "hih6130",
.probe = hih6130_probe, .probe = hih6130_probe,
.remove = hih6130_remove,
.id_table = hih6130_id, .id_table = hih6130_id,
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment