Skip to content

Commit

Permalink
hwmon: (g762) Convert to hwmon_device_register_with_groups
Browse files Browse the repository at this point in the history
Use ATTRIBUTE_GROUPS macro and hwmon_device_register_with_groups() to simplify
the code a bit.

Use hwmon_device_register_with_groups rather than the device managed version to
ensure g762_of_clock_disable() is called after hwmon_device_unregister().

Signed-off-by: Axel Lin <[email protected]>
Signed-off-by: Guenter Roeck <[email protected]>
  • Loading branch information
AxelLin authored and groeck committed Aug 4, 2014
1 parent 9dd304f commit 398e16d
Showing 1 changed file with 27 additions and 44 deletions.
71 changes: 27 additions & 44 deletions drivers/hwmon/g762.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ enum g762_regs {
G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))

struct g762_data {
struct i2c_client *client;
struct device *hwmon_dev;
struct i2c_client *client;
struct clk *clk;

/* update mutex */
Expand Down Expand Up @@ -206,8 +206,8 @@ static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p,
/* helper to grab and cache data, at most one time per second */
static struct g762_data *g762_update_client(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = i2c_get_clientdata(client);
struct g762_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
int ret = 0;

mutex_lock(&data->update_lock);
Expand Down Expand Up @@ -266,8 +266,7 @@ static struct g762_data *g762_update_client(struct device *dev)
*/
static int do_set_clk_freq(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = i2c_get_clientdata(client);
struct g762_data *data = dev_get_drvdata(dev);

if (val > 0xffffff)
return -EINVAL;
Expand All @@ -282,7 +281,6 @@ static int do_set_clk_freq(struct device *dev, unsigned long val)
/* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
static int do_set_pwm_mode(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev);
int ret;

Expand All @@ -301,7 +299,7 @@ static int do_set_pwm_mode(struct device *dev, unsigned long val)
ret = -EINVAL;
goto out;
}
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1);
data->valid = false;
out:
Expand All @@ -313,7 +311,6 @@ static int do_set_pwm_mode(struct device *dev, unsigned long val)
/* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
static int do_set_fan_div(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev);
int ret;

Expand Down Expand Up @@ -342,7 +339,7 @@ static int do_set_fan_div(struct device *dev, unsigned long val)
ret = -EINVAL;
goto out;
}
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1);
data->valid = false;
out:
Expand All @@ -354,7 +351,6 @@ static int do_set_fan_div(struct device *dev, unsigned long val)
/* Set fan gear mode. Accepts either 0, 1 or 2. */
static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev);
int ret;

Expand All @@ -379,7 +375,7 @@ static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
ret = -EINVAL;
goto out;
}
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2,
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
data->fan_cmd2);
data->valid = false;
out:
Expand All @@ -391,7 +387,6 @@ static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
/* Set number of fan pulses per revolution. Accepts either 2 or 4. */
static int do_set_fan_pulses(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev);
int ret;

Expand All @@ -410,7 +405,7 @@ static int do_set_fan_pulses(struct device *dev, unsigned long val)
ret = -EINVAL;
goto out;
}
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1);
data->valid = false;
out:
Expand All @@ -422,7 +417,6 @@ static int do_set_fan_pulses(struct device *dev, unsigned long val)
/* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
static int do_set_pwm_enable(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev);
int ret;

Expand All @@ -444,15 +438,15 @@ static int do_set_pwm_enable(struct device *dev, unsigned long val)
* value of 254 if it is 255 when switching to open-loop.
*/
if (data->set_cnt == 0xff)
i2c_smbus_write_byte_data(client, G762_REG_SET_CNT,
254);
i2c_smbus_write_byte_data(data->client,
G762_REG_SET_CNT, 254);
break;
default:
ret = -EINVAL;
goto out;
}

ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1);
data->valid = false;
out:
Expand All @@ -464,7 +458,6 @@ static int do_set_pwm_enable(struct device *dev, unsigned long val)
/* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
static int do_set_pwm_polarity(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev);
int ret;

Expand All @@ -483,7 +476,7 @@ static int do_set_pwm_polarity(struct device *dev, unsigned long val)
ret = -EINVAL;
goto out;
}
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1);
data->valid = false;
out:
Expand All @@ -498,8 +491,8 @@ static int do_set_pwm_polarity(struct device *dev, unsigned long val)
*/
static int do_set_pwm(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = i2c_get_clientdata(client);
struct g762_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
int ret;

if (val > 255)
Expand All @@ -519,7 +512,6 @@ static int do_set_pwm(struct device *dev, unsigned long val)
*/
static int do_set_fan_target(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev);
int ret;

Expand All @@ -531,7 +523,7 @@ static int do_set_fan_target(struct device *dev, unsigned long val)
G762_PULSE_FROM_REG(data->fan_cmd1),
G762_CLKDIV_FROM_REG(data->fan_cmd1),
G762_GEARMULT_FROM_REG(data->fan_cmd2));
ret = i2c_smbus_write_byte_data(client, G762_REG_SET_CNT,
ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
data->set_cnt);
data->valid = false;
mutex_unlock(&data->update_lock);
Expand All @@ -542,7 +534,6 @@ static int do_set_fan_target(struct device *dev, unsigned long val)
/* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
static int do_set_fan_startv(struct device *dev, unsigned long val)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev);
int ret;

Expand Down Expand Up @@ -571,7 +562,7 @@ static int do_set_fan_startv(struct device *dev, unsigned long val)
ret = -EINVAL;
goto out;
}
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2,
ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
data->fan_cmd2);
data->valid = false;
out:
Expand Down Expand Up @@ -1026,7 +1017,7 @@ static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO,
get_fan_pulses, set_fan_pulses);

/* Driver data */
static struct attribute *g762_attributes[] = {
static struct attribute *g762_attrs[] = {
&dev_attr_fan1_input.attr,
&dev_attr_fan1_alarm.attr,
&dev_attr_fan1_fault.attr,
Expand All @@ -1039,9 +1030,7 @@ static struct attribute *g762_attributes[] = {
NULL
};

static const struct attribute_group g762_group = {
.attrs = g762_attributes,
};
ATTRIBUTE_GROUPS(g762);

/*
* Enable both fan failure detection and fan out of control protection. The
Expand All @@ -1050,7 +1039,6 @@ static const struct attribute_group g762_group = {
*/
static inline int g762_fan_init(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev);

if (IS_ERR(data))
Expand All @@ -1060,20 +1048,21 @@ static inline int g762_fan_init(struct device *dev)
data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
data->valid = false;

return i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1,
return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1);
}

static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct device *dev = &client->dev;
struct g762_data *data;
int ret;

if (!i2c_check_functionality(client->adapter,
I2C_FUNC_SMBUS_BYTE_DATA))
return -ENODEV;

data = devm_kzalloc(&client->dev, sizeof(struct g762_data), GFP_KERNEL);
data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
if (!data)
return -ENOMEM;

Expand All @@ -1082,7 +1071,7 @@ static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
mutex_init(&data->update_lock);

/* Enable fan failure detection and fan out of control protection */
ret = g762_fan_init(&client->dev);
ret = g762_fan_init(dev);
if (ret)
return ret;

Expand All @@ -1098,22 +1087,17 @@ static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
if (ret)
goto clock_dis;

/* Register sysfs hooks */
ret = sysfs_create_group(&client->dev.kobj, &g762_group);
if (ret)
goto clock_dis;

data->hwmon_dev = hwmon_device_register(&client->dev);
data->hwmon_dev = devm_hwmon_device_register_with_groups(dev,
client->name,
data,
g762_groups);
if (IS_ERR(data->hwmon_dev)) {
ret = PTR_ERR(data->hwmon_dev);
goto sysfs_rem;
goto clock_dis;
}

return 0;

sysfs_rem:
sysfs_remove_group(&client->dev.kobj, &g762_group);

clock_dis:
g762_of_clock_disable(client);

Expand All @@ -1125,7 +1109,6 @@ static int g762_remove(struct i2c_client *client)
struct g762_data *data = i2c_get_clientdata(client);

hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &g762_group);
g762_of_clock_disable(client);

return 0;
Expand Down

0 comments on commit 398e16d

Please sign in to comment.