hwmon: (lm75) Handle cleanup with devm_add_action
Guenter Roeck
maintainerPATCH v3 3/4 hwmon: (lm75) Add update_interval attribute
In reply to Guenter RoeckSince we know the chip's update interval, let's make it available to the user.
---
v2, v3: No change
drivers/hwmon/lm75.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/hwmon/lm75.c +13 -0
@@ -159,16 +159,29 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da,return count; } +static ssize_t show_update_interval (struct device *dev, +struct device_attribute *da,char *buf) +{ +struct lm75_data *data =lm75_update_device (dev); + +if (IS_ERR (data)) +return PTR_ERR (data); + +return sprintf (buf,"%u\n" ,jiffies_to_msecs (data->sample_time)); +} + static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 1); static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp, set_temp, 2); static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); +static DEVICE_ATTR(update_interval, S_IRUGO, show_update_interval, NULL);static struct attribute *lm75_attrs[] = { &sensor_dev_attr_temp1_input.dev_attr.attr, &sensor_dev_attr_temp1_max.dev_attr.attr, &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, + &dev_attr_update_interval.attr,NULL };
Signature · 1 line
2.5.0
Guenter Roeck
maintainerPATCH v3 1/4 hwmon: (lm75) Handle cleanup with devm_add_action
Use devm_add_action() to register the function to restore the original chip configuration. Use devm_hwmon_device_register_with_groups() to register the hwmon device, and drop the remove function as no longer needed.
---
v2, v3: No change
drivers/hwmon/lm75.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
drivers/hwmon/lm75.c +19 -19
@@ -76,7 +76,6 @@ static const u8 LM75_REG_TEMP[3] = {/* Each client has this additional data */ struct lm75_data {struct i2c_client *client; -struct device *hwmon_dev;struct mutex update_lock;u8 orig_conf;u8 resolution;/* In bits, between 9 and 12 */
@@ -185,10 +184,19 @@ static const struct thermal_zone_of_device_ops lm75_of_thermal_ops = {/* device probe and removal */ +static void lm75_remove (void *data) +{ +struct lm75_data *lm75 = data; +struct i2c_client *client = lm75->client; + +i2c_smbus_write_byte_data (client,LM75_REG_CONF , lm75->orig_conf); +} +static int lm75_probe (struct i2c_client *client,const struct i2c_device_id *id) {struct device *dev = &client->dev; +struct device *hwmon_dev;struct lm75_data *data;int status;u8 set_mask, clr_mask;
@@ -298,29 +306,22 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) new |= set_mask;if (status != new)lm75_write_value (client,LM75_REG_CONF , new); -dev_dbg (dev,"Config %02x\n" , new); - data->hwmon_dev =hwmon_device_register_with_groups (dev, client->name, - data, lm75_groups); -if (IS_ERR (data->hwmon_dev)) -return PTR_ERR (data->hwmon_dev); +devm_add_action (dev, lm75_remove, data); -devm_thermal_zone_of_sensor_register (data->hwmon_dev,0 , - data->hwmon_dev, - &lm75_of_thermal_ops); +dev_dbg (dev,"Config %02x\n" , new); -dev_info (dev,"%s: sensor '%s'\n" , -dev_name (data->hwmon_dev), client->name); + hwmon_dev =devm_hwmon_device_register_with_groups (dev, client->name, + data, lm75_groups); +if (IS_ERR (hwmon_dev)) +return PTR_ERR (hwmon_dev); -return 0 ; -} +devm_thermal_zone_of_sensor_register (hwmon_dev,0 , + hwmon_dev, + &lm75_of_thermal_ops); -static int lm75_remove (struct i2c_client *client) -{ -struct lm75_data *data =i2c_get_clientdata (client); +dev_info (dev,"%s: sensor '%s'\n" ,dev_name (hwmon_dev), client->name); -hwmon_device_unregister (data->hwmon_dev); -lm75_write_value (client,LM75_REG_CONF , data->orig_conf);return 0 ; }
@@ -489,7 +490,6 @@ static struct i2c_driver lm75_driver = { .pm = LM75_DEV_PM_OPS, }, .probe = lm75_probe, - .remove = lm75_remove, .id_table = lm75_ids, .detect = lm75_detect, .address_list = normal_i2c,
Signature · 1 line
2.5.0
Guenter Roeck
maintainerPATCH v3 2/4 hwmon: (lm75) Drop lm75_read_value and lm75_write_value
lm75_read_value and lm75_write_value don't really add any value. Replace with direct smbus access functions.
---
v2, v3: No change
drivers/hwmon/lm75.c | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-)
drivers/hwmon/lm75.c +9 -33
@@ -89,8 +89,6 @@ struct lm75_data {2 = hyst */ }; -static int lm75_read_value (struct i2c_client *client,u8 reg); -static int lm75_write_value (struct i2c_client *client,u8 reg,u16 value);static struct lm75_data *lm75_update_device (struct device *dev);
@@ -156,7 +154,7 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da, temp =clamp_val (temp,LM75_TEMP_MIN ,LM75_TEMP_MAX ); data->temp[nr] =DIV_ROUND_CLOSEST (temp << (resolution -8 ),1000 ) << (16 - resolution); -lm75_write_value (client,LM75_REG_TEMP [nr], data->temp[nr]); +i2c_smbus_write_word_swapped (client,LM75_REG_TEMP [nr], data->temp[nr]);mutex_unlock (&data->update_lock);return count; }
@@ -296,7 +294,7 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) }/* configure as specified */ - status =lm75_read_value (client,LM75_REG_CONF ); + status =i2c_smbus_read_byte_data (client,LM75_REG_CONF );if (status <0 ) {dev_dbg (dev,"Can't read config? %d\n" , status);return status;
@@ -305,7 +303,7 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) new = status & ~clr_mask; new |= set_mask;if (status != new) -lm75_write_value (client,LM75_REG_CONF , new); +i2c_smbus_write_byte_data (client,LM75_REG_CONF , new);devm_add_action (dev, lm75_remove, data);
@@ -450,13 +448,13 @@ static int lm75_suspend(struct device *dev) {int status;struct i2c_client *client =to_i2c_client (dev); - status =lm75_read_value (client,LM75_REG_CONF ); + status =i2c_smbus_read_byte_data (client,LM75_REG_CONF );if (status <0 ) {dev_dbg (&client->dev,"Can't read config? %d\n" , status);return status; } status = status |LM75_SHUTDOWN ; -lm75_write_value (client,LM75_REG_CONF , status); +i2c_smbus_write_byte_data (client,LM75_REG_CONF , status);return 0 ; }
@@ -464,13 +462,13 @@ static int lm75_resume(struct device *dev) {int status;struct i2c_client *client =to_i2c_client (dev); - status =lm75_read_value (client,LM75_REG_CONF ); + status =i2c_smbus_read_byte_data (client,LM75_REG_CONF );if (status <0 ) {dev_dbg (&client->dev,"Can't read config? %d\n" , status);return status; } status = status & ~LM75_SHUTDOWN ; -lm75_write_value (client,LM75_REG_CONF , status); +i2c_smbus_write_byte_data (client,LM75_REG_CONF , status);return 0 ; }
@@ -497,29 +495,6 @@ static struct i2c_driver lm75_driver = {/*-----------------------------------------------------------------------*/ -/* register access */ - -/* -* All registers are word-sized, except for the configuration register. -* LM75 uses a high-byte first convention, which is exactly opposite to -* the SMBus standard. -*/ -static int lm75_read_value (struct i2c_client *client,u8 reg) -{ -if (reg ==LM75_REG_CONF ) -return i2c_smbus_read_byte_data (client, reg); -else -return i2c_smbus_read_word_swapped (client, reg); -} - -static int lm75_write_value (struct i2c_client *client,u8 reg,u16 value) -{ -if (reg ==LM75_REG_CONF ) -return i2c_smbus_write_byte_data (client, reg, value); -else -return i2c_smbus_write_word_swapped (client, reg, value); -} -static struct lm75_data *lm75_update_device (struct device *dev) {struct lm75_data *data =dev_get_drvdata (dev);
@@ -536,7 +511,8 @@ static struct lm75_data *lm75_update_device(struct device *dev)for (i =0 ; i <ARRAY_SIZE (data->temp); i++) {int status; - status =lm75_read_value (client,LM75_REG_TEMP [i]); + status =i2c_smbus_read_word_swapped (client, +LM75_REG_TEMP [i]);if (unlikely (status <0 )) { dev_dbg(dev, "LM75: Failed to read value: reg %d, error %d\n",
Signature · 1 line
2.5.0
Guenter Roeck
maintainerPATCH v3 4/4 hwmon: (lm75) Convert to use regmap
In reply to Guenter RoeckConvert to use regmap. Leave caching to regmap and drop the register update function. While this can result in additional read operations if the temperature register is read continuously, it avoids re-reading the limit registers and thus overall reduces complexity.
---
v3: Add missing "select REGMAP_I2C"
v2: Set use_single_rw to indicate that the chip can not perform
multi-register read operations.
Use REGCACHE_RBTREE instead REGCACHE_FLAT. REGCACHE_FLAT does not initialize the cache from the chip unless num_reg_defaults_raw is provided. If it is provided without actual cache values, it complains with 'No cache defaults, reading back from HW'. REGCACHE_RBTREE automatically initializes itself from the chip, and does not complain about it.
drivers/hwmon/Kconfig | 1 + drivers/hwmon/lm75.c | 162 +++++++++++++++++++++----------------------------- 2 files changed, 69 insertions(+), 94 deletions(-)
drivers/hwmon/Kconfig +1 -0
@@ -958,6 +958,7 @@ config SENSORS_LM75 tristate "National Semiconductor LM75 and compatibles" depends on I2C depends on THERMAL || !THERMAL_OF + select REGMAP_I2C help If you say yes here you get support for one common type of temperature sensor chip, with models including:
drivers/hwmon/lm75.c +68 -94
299 lines not on this page · raw message
Signature · 1 line
2.5.0