linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [PATCH 1/2] hwmon: (k8temp) update to use new hwmon registration API
       [not found] <BVOE7U9MRMZY.38N6DGWH9KX7H@HP>
@ 2019-07-20 22:46 ` Guenter Roeck
  2019-07-21 12:00   ` [PATCH v3 1/1] " Robert Karszniewicz
  0 siblings, 1 reply; 8+ messages in thread
From: Guenter Roeck @ 2019-07-20 22:46 UTC (permalink / raw)
  To: Robert Karszniewicz, Rudolf Marek, Jean Delvare; +Cc: linux-hwmon, linux-kernel

On 7/20/19 2:13 PM, Robert Karszniewicz wrote:

[ ... ]

>>> +	if (data->swap_core_select)
>>> +		core = core ? 0 : 1;
>>
>> 		core = 1 - core;
>>
>> would accomplish the same without conditional.
> 
> How do you like
> 	core ^= 1;
> ?
> 

I didn't notice that before.

Your call. Everything is fine as long as it doesn't involve a conditional.
Hmm ... having said that, how about the following ?

	core ^= data->swap_core_select;

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3 1/1] hwmon: (k8temp) update to use new hwmon registration API
  2019-07-20 22:46 ` [PATCH 1/2] hwmon: (k8temp) update to use new hwmon registration API Guenter Roeck
@ 2019-07-21 12:00   ` Robert Karszniewicz
  2019-07-21 15:14     ` Guenter Roeck
  0 siblings, 1 reply; 8+ messages in thread
From: Robert Karszniewicz @ 2019-07-21 12:00 UTC (permalink / raw)
  To: Rudolf Marek, Jean Delvare, Guenter Roeck
  Cc: Robert Karszniewicz, linux-hwmon, linux-kernel, Robert Karszniewicz

Removes:
- hwmon_dev from k8temp_data struct, as that is now passed
  to callbacks, anyway.
- other k8temp_data struct fields, too.
- k8temp_update_device()

Also reduces binary size:
   text    data     bss     dec     hex filename
   4139    1448       0    5587    15d3 drivers/hwmon/k8temp.ko.bak
   3103    1220       0    4323    10e3 drivers/hwmon/k8temp.ko

Signed-off-by: Robert Karszniewicz <avoidr@firemail.cc>
Signed-off-by: Robert Karszniewicz <avoidr@riseup.net>
---
Changes from v2:
- if (data->swap_core_select)
-     core ^= 1;
+ core ^= data->swap_core_select;

However, that produces slightly more .text than v2, and is a tad too
"tricky", I personally find.

- Changed email service due to problems with the previous one.

Changes from v1:
- correct is_visible() and remove unnecessary checks
- k8temp_read(): remove unnecessary checks and replace the switch
- use HWMON_CHANNEL_INFO()
- remove k8temp->name and pass the string directly
- use PTR_ERR_OR_ZERO()
- remove k8temp_update_device() and more k8temp_data fields

 drivers/hwmon/k8temp.c | 233 ++++++++++++-----------------------------
 1 file changed, 69 insertions(+), 164 deletions(-)

diff --git a/drivers/hwmon/k8temp.c b/drivers/hwmon/k8temp.c
index 4994c90c8929..f73bd4eceb28 100644
--- a/drivers/hwmon/k8temp.c
+++ b/drivers/hwmon/k8temp.c
@@ -10,10 +10,8 @@
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/slab.h>
-#include <linux/jiffies.h>
 #include <linux/pci.h>
 #include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
 #include <linux/err.h>
 #include <linux/mutex.h>
 #include <asm/processor.h>
@@ -24,108 +22,18 @@
 #define SEL_CORE	0x04
 
 struct k8temp_data {
-	struct device *hwmon_dev;
 	struct mutex update_lock;
-	const char *name;
-	char valid;		/* zero until following fields are valid */
-	unsigned long last_updated;	/* in jiffies */
 
 	/* registers values */
 	u8 sensorsp;		/* sensor presence bits - SEL_CORE, SEL_PLACE */
-	u32 temp[2][2];		/* core, place */
 	u8 swap_core_select;    /* meaning of SEL_CORE is inverted */
 	u32 temp_offset;
 };
 
-static struct k8temp_data *k8temp_update_device(struct device *dev)
-{
-	struct k8temp_data *data = dev_get_drvdata(dev);
-	struct pci_dev *pdev = to_pci_dev(dev);
-	u8 tmp;
-
-	mutex_lock(&data->update_lock);
-
-	if (!data->valid
-	    || time_after(jiffies, data->last_updated + HZ)) {
-		pci_read_config_byte(pdev, REG_TEMP, &tmp);
-		tmp &= ~(SEL_PLACE | SEL_CORE);	/* Select sensor 0, core0 */
-		pci_write_config_byte(pdev, REG_TEMP, tmp);
-		pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
-
-		if (data->sensorsp & SEL_PLACE) {
-			tmp |= SEL_PLACE;	/* Select sensor 1, core0 */
-			pci_write_config_byte(pdev, REG_TEMP, tmp);
-			pci_read_config_dword(pdev, REG_TEMP,
-					      &data->temp[0][1]);
-		}
-
-		if (data->sensorsp & SEL_CORE) {
-			tmp &= ~SEL_PLACE;	/* Select sensor 0, core1 */
-			tmp |= SEL_CORE;
-			pci_write_config_byte(pdev, REG_TEMP, tmp);
-			pci_read_config_dword(pdev, REG_TEMP,
-					      &data->temp[1][0]);
-
-			if (data->sensorsp & SEL_PLACE) {
-				tmp |= SEL_PLACE; /* Select sensor 1, core1 */
-				pci_write_config_byte(pdev, REG_TEMP, tmp);
-				pci_read_config_dword(pdev, REG_TEMP,
-						      &data->temp[1][1]);
-			}
-		}
-
-		data->last_updated = jiffies;
-		data->valid = 1;
-	}
-
-	mutex_unlock(&data->update_lock);
-	return data;
-}
-
-/*
- * Sysfs stuff
- */
-
-static ssize_t name_show(struct device *dev, struct device_attribute
-			 *devattr, char *buf)
-{
-	struct k8temp_data *data = dev_get_drvdata(dev);
-
-	return sprintf(buf, "%s\n", data->name);
-}
-
-
-static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
-			 char *buf)
-{
-	struct sensor_device_attribute_2 *attr =
-	    to_sensor_dev_attr_2(devattr);
-	int core = attr->nr;
-	int place = attr->index;
-	int temp;
-	struct k8temp_data *data = k8temp_update_device(dev);
-
-	if (data->swap_core_select && (data->sensorsp & SEL_CORE))
-		core = core ? 0 : 1;
-
-	temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
-
-	return sprintf(buf, "%d\n", temp);
-}
-
-/* core, place */
-
-static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0);
-static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1);
-static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 1, 0);
-static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 1, 1);
-static DEVICE_ATTR_RO(name);
-
 static const struct pci_device_id k8temp_ids[] = {
 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
 	{ 0 },
 };
-
 MODULE_DEVICE_TABLE(pci, k8temp_ids);
 
 static int is_rev_g_desktop(u8 model)
@@ -159,14 +67,76 @@ static int is_rev_g_desktop(u8 model)
 	return 1;
 }
 
+static umode_t
+k8temp_is_visible(const void *drvdata, enum hwmon_sensor_types type,
+		  u32 attr, int channel)
+{
+	const struct k8temp_data *data = drvdata;
+
+	if ((channel & 1) && !(data->sensorsp & SEL_PLACE))
+		return 0;
+
+	if ((channel & 2) && !(data->sensorsp & SEL_CORE))
+		return 0;
+
+	return 0444;
+}
+
+static int
+k8temp_read(struct device *dev, enum hwmon_sensor_types type,
+	    u32 attr, int channel, long *val)
+{
+	struct k8temp_data *data = dev_get_drvdata(dev);
+	struct pci_dev *pdev = to_pci_dev(dev->parent);
+	int core, place;
+	u32 temp;
+	u8 tmp;
+
+	core = (channel >> 1) & 1;
+	place = channel & 1;
+
+	core ^= data->swap_core_select;
+
+	mutex_lock(&data->update_lock);
+	pci_read_config_byte(pdev, REG_TEMP, &tmp);
+	tmp &= ~(SEL_PLACE | SEL_CORE);
+	if (core)
+		tmp |= SEL_CORE;
+	if (place)
+		tmp |= SEL_PLACE;
+	pci_write_config_byte(pdev, REG_TEMP, tmp);
+	pci_read_config_dword(pdev, REG_TEMP, &temp);
+	mutex_unlock(&data->update_lock);
+
+	*val = TEMP_FROM_REG(temp) + data->temp_offset;
+
+	return 0;
+}
+
+static const struct hwmon_ops k8temp_ops = {
+	.is_visible = k8temp_is_visible,
+	.read = k8temp_read,
+};
+
+static const struct hwmon_channel_info *k8temp_info[] = {
+	HWMON_CHANNEL_INFO(temp,
+		HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT),
+	NULL
+};
+
+static const struct hwmon_chip_info k8temp_chip_info = {
+	.ops = &k8temp_ops,
+	.info = k8temp_info,
+};
+
 static int k8temp_probe(struct pci_dev *pdev,
 				  const struct pci_device_id *id)
 {
-	int err;
 	u8 scfg;
 	u32 temp;
 	u8 model, stepping;
 	struct k8temp_data *data;
+	struct device *hwmon_dev;
 
 	data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
 	if (!data)
@@ -231,86 +201,21 @@ static int k8temp_probe(struct pci_dev *pdev,
 			data->sensorsp &= ~SEL_CORE;
 	}
 
-	data->name = "k8temp";
 	mutex_init(&data->update_lock);
-	pci_set_drvdata(pdev, data);
-
-	/* Register sysfs hooks */
-	err = device_create_file(&pdev->dev,
-			   &sensor_dev_attr_temp1_input.dev_attr);
-	if (err)
-		goto exit_remove;
-
-	/* sensor can be changed and reports something */
-	if (data->sensorsp & SEL_PLACE) {
-		err = device_create_file(&pdev->dev,
-				   &sensor_dev_attr_temp2_input.dev_attr);
-		if (err)
-			goto exit_remove;
-	}
-
-	/* core can be changed and reports something */
-	if (data->sensorsp & SEL_CORE) {
-		err = device_create_file(&pdev->dev,
-				   &sensor_dev_attr_temp3_input.dev_attr);
-		if (err)
-			goto exit_remove;
-		if (data->sensorsp & SEL_PLACE) {
-			err = device_create_file(&pdev->dev,
-					   &sensor_dev_attr_temp4_input.
-					   dev_attr);
-			if (err)
-				goto exit_remove;
-		}
-	}
-
-	err = device_create_file(&pdev->dev, &dev_attr_name);
-	if (err)
-		goto exit_remove;
 
-	data->hwmon_dev = hwmon_device_register(&pdev->dev);
+	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
+							 "k8temp",
+							 data,
+							 &k8temp_chip_info,
+							 NULL);
 
-	if (IS_ERR(data->hwmon_dev)) {
-		err = PTR_ERR(data->hwmon_dev);
-		goto exit_remove;
-	}
-
-	return 0;
-
-exit_remove:
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp1_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp2_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp3_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp4_input.dev_attr);
-	device_remove_file(&pdev->dev, &dev_attr_name);
-	return err;
-}
-
-static void k8temp_remove(struct pci_dev *pdev)
-{
-	struct k8temp_data *data = pci_get_drvdata(pdev);
-
-	hwmon_device_unregister(data->hwmon_dev);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp1_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp2_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp3_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp4_input.dev_attr);
-	device_remove_file(&pdev->dev, &dev_attr_name);
+	return PTR_ERR_OR_ZERO(hwmon_dev);
 }
 
 static struct pci_driver k8temp_driver = {
 	.name = "k8temp",
 	.id_table = k8temp_ids,
 	.probe = k8temp_probe,
-	.remove = k8temp_remove,
 };
 
 module_pci_driver(k8temp_driver);
-- 
2.22.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 1/1] hwmon: (k8temp) update to use new hwmon registration API
  2019-07-21 12:00   ` [PATCH v3 1/1] " Robert Karszniewicz
@ 2019-07-21 15:14     ` Guenter Roeck
  2019-07-21 20:37       ` avoidr
  0 siblings, 1 reply; 8+ messages in thread
From: Guenter Roeck @ 2019-07-21 15:14 UTC (permalink / raw)
  To: Robert Karszniewicz
  Cc: Rudolf Marek, Jean Delvare, linux-hwmon, linux-kernel,
	Robert Karszniewicz

On Sun, Jul 21, 2019 at 02:00:51PM +0200, Robert Karszniewicz wrote:
> Removes:
> - hwmon_dev from k8temp_data struct, as that is now passed
>   to callbacks, anyway.
> - other k8temp_data struct fields, too.
> - k8temp_update_device()
> 
> Also reduces binary size:
>    text    data     bss     dec     hex filename
>    4139    1448       0    5587    15d3 drivers/hwmon/k8temp.ko.bak
>    3103    1220       0    4323    10e3 drivers/hwmon/k8temp.ko
> 
> Signed-off-by: Robert Karszniewicz <avoidr@firemail.cc>
> Signed-off-by: Robert Karszniewicz <avoidr@riseup.net>

Applied.

> ---
> Changes from v2:
> - if (data->swap_core_select)
> -     core ^= 1;
> + core ^= data->swap_core_select;
> 
> However, that produces slightly more .text than v2, and is a tad too
> "tricky", I personally find.
> 
Interesting - for me it produces ~30 bytes less code (with gcc 7.4.0).

> - Changed email service due to problems with the previous one.
> 
> Changes from v1:
> - correct is_visible() and remove unnecessary checks
> - k8temp_read(): remove unnecessary checks and replace the switch
> - use HWMON_CHANNEL_INFO()
> - remove k8temp->name and pass the string directly
> - use PTR_ERR_OR_ZERO()
> - remove k8temp_update_device() and more k8temp_data fields
> 
>  drivers/hwmon/k8temp.c | 233 ++++++++++++-----------------------------
>  1 file changed, 69 insertions(+), 164 deletions(-)
> 
> diff --git a/drivers/hwmon/k8temp.c b/drivers/hwmon/k8temp.c
> index 4994c90c8929..f73bd4eceb28 100644
> --- a/drivers/hwmon/k8temp.c
> +++ b/drivers/hwmon/k8temp.c
> @@ -10,10 +10,8 @@
>  #include <linux/module.h>
>  #include <linux/init.h>
>  #include <linux/slab.h>
> -#include <linux/jiffies.h>
>  #include <linux/pci.h>
>  #include <linux/hwmon.h>
> -#include <linux/hwmon-sysfs.h>
>  #include <linux/err.h>
>  #include <linux/mutex.h>
>  #include <asm/processor.h>
> @@ -24,108 +22,18 @@
>  #define SEL_CORE	0x04
>  
>  struct k8temp_data {
> -	struct device *hwmon_dev;
>  	struct mutex update_lock;
> -	const char *name;
> -	char valid;		/* zero until following fields are valid */
> -	unsigned long last_updated;	/* in jiffies */
>  
>  	/* registers values */
>  	u8 sensorsp;		/* sensor presence bits - SEL_CORE, SEL_PLACE */
> -	u32 temp[2][2];		/* core, place */
>  	u8 swap_core_select;    /* meaning of SEL_CORE is inverted */
>  	u32 temp_offset;
>  };
>  
> -static struct k8temp_data *k8temp_update_device(struct device *dev)
> -{
> -	struct k8temp_data *data = dev_get_drvdata(dev);
> -	struct pci_dev *pdev = to_pci_dev(dev);
> -	u8 tmp;
> -
> -	mutex_lock(&data->update_lock);
> -
> -	if (!data->valid
> -	    || time_after(jiffies, data->last_updated + HZ)) {
> -		pci_read_config_byte(pdev, REG_TEMP, &tmp);
> -		tmp &= ~(SEL_PLACE | SEL_CORE);	/* Select sensor 0, core0 */
> -		pci_write_config_byte(pdev, REG_TEMP, tmp);
> -		pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
> -
> -		if (data->sensorsp & SEL_PLACE) {
> -			tmp |= SEL_PLACE;	/* Select sensor 1, core0 */
> -			pci_write_config_byte(pdev, REG_TEMP, tmp);
> -			pci_read_config_dword(pdev, REG_TEMP,
> -					      &data->temp[0][1]);
> -		}
> -
> -		if (data->sensorsp & SEL_CORE) {
> -			tmp &= ~SEL_PLACE;	/* Select sensor 0, core1 */
> -			tmp |= SEL_CORE;
> -			pci_write_config_byte(pdev, REG_TEMP, tmp);
> -			pci_read_config_dword(pdev, REG_TEMP,
> -					      &data->temp[1][0]);
> -
> -			if (data->sensorsp & SEL_PLACE) {
> -				tmp |= SEL_PLACE; /* Select sensor 1, core1 */
> -				pci_write_config_byte(pdev, REG_TEMP, tmp);
> -				pci_read_config_dword(pdev, REG_TEMP,
> -						      &data->temp[1][1]);
> -			}
> -		}
> -
> -		data->last_updated = jiffies;
> -		data->valid = 1;
> -	}
> -
> -	mutex_unlock(&data->update_lock);
> -	return data;
> -}
> -
> -/*
> - * Sysfs stuff
> - */
> -
> -static ssize_t name_show(struct device *dev, struct device_attribute
> -			 *devattr, char *buf)
> -{
> -	struct k8temp_data *data = dev_get_drvdata(dev);
> -
> -	return sprintf(buf, "%s\n", data->name);
> -}
> -
> -
> -static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
> -			 char *buf)
> -{
> -	struct sensor_device_attribute_2 *attr =
> -	    to_sensor_dev_attr_2(devattr);
> -	int core = attr->nr;
> -	int place = attr->index;
> -	int temp;
> -	struct k8temp_data *data = k8temp_update_device(dev);
> -
> -	if (data->swap_core_select && (data->sensorsp & SEL_CORE))
> -		core = core ? 0 : 1;
> -
> -	temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
> -
> -	return sprintf(buf, "%d\n", temp);
> -}
> -
> -/* core, place */
> -
> -static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0);
> -static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1);
> -static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 1, 0);
> -static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 1, 1);
> -static DEVICE_ATTR_RO(name);
> -
>  static const struct pci_device_id k8temp_ids[] = {
>  	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
>  	{ 0 },
>  };
> -
>  MODULE_DEVICE_TABLE(pci, k8temp_ids);
>  
>  static int is_rev_g_desktop(u8 model)
> @@ -159,14 +67,76 @@ static int is_rev_g_desktop(u8 model)
>  	return 1;
>  }
>  
> +static umode_t
> +k8temp_is_visible(const void *drvdata, enum hwmon_sensor_types type,
> +		  u32 attr, int channel)
> +{
> +	const struct k8temp_data *data = drvdata;
> +
> +	if ((channel & 1) && !(data->sensorsp & SEL_PLACE))
> +		return 0;
> +
> +	if ((channel & 2) && !(data->sensorsp & SEL_CORE))
> +		return 0;
> +
> +	return 0444;
> +}
> +
> +static int
> +k8temp_read(struct device *dev, enum hwmon_sensor_types type,
> +	    u32 attr, int channel, long *val)
> +{
> +	struct k8temp_data *data = dev_get_drvdata(dev);
> +	struct pci_dev *pdev = to_pci_dev(dev->parent);
> +	int core, place;
> +	u32 temp;
> +	u8 tmp;
> +
> +	core = (channel >> 1) & 1;
> +	place = channel & 1;
> +
> +	core ^= data->swap_core_select;
> +
> +	mutex_lock(&data->update_lock);
> +	pci_read_config_byte(pdev, REG_TEMP, &tmp);
> +	tmp &= ~(SEL_PLACE | SEL_CORE);
> +	if (core)
> +		tmp |= SEL_CORE;
> +	if (place)
> +		tmp |= SEL_PLACE;
> +	pci_write_config_byte(pdev, REG_TEMP, tmp);
> +	pci_read_config_dword(pdev, REG_TEMP, &temp);
> +	mutex_unlock(&data->update_lock);
> +
> +	*val = TEMP_FROM_REG(temp) + data->temp_offset;
> +
> +	return 0;
> +}
> +
> +static const struct hwmon_ops k8temp_ops = {
> +	.is_visible = k8temp_is_visible,
> +	.read = k8temp_read,
> +};
> +
> +static const struct hwmon_channel_info *k8temp_info[] = {
> +	HWMON_CHANNEL_INFO(temp,
> +		HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT),
> +	NULL
> +};
> +
> +static const struct hwmon_chip_info k8temp_chip_info = {
> +	.ops = &k8temp_ops,
> +	.info = k8temp_info,
> +};
> +
>  static int k8temp_probe(struct pci_dev *pdev,
>  				  const struct pci_device_id *id)
>  {
> -	int err;
>  	u8 scfg;
>  	u32 temp;
>  	u8 model, stepping;
>  	struct k8temp_data *data;
> +	struct device *hwmon_dev;
>  
>  	data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
>  	if (!data)
> @@ -231,86 +201,21 @@ static int k8temp_probe(struct pci_dev *pdev,
>  			data->sensorsp &= ~SEL_CORE;
>  	}
>  
> -	data->name = "k8temp";
>  	mutex_init(&data->update_lock);
> -	pci_set_drvdata(pdev, data);
> -
> -	/* Register sysfs hooks */
> -	err = device_create_file(&pdev->dev,
> -			   &sensor_dev_attr_temp1_input.dev_attr);
> -	if (err)
> -		goto exit_remove;
> -
> -	/* sensor can be changed and reports something */
> -	if (data->sensorsp & SEL_PLACE) {
> -		err = device_create_file(&pdev->dev,
> -				   &sensor_dev_attr_temp2_input.dev_attr);
> -		if (err)
> -			goto exit_remove;
> -	}
> -
> -	/* core can be changed and reports something */
> -	if (data->sensorsp & SEL_CORE) {
> -		err = device_create_file(&pdev->dev,
> -				   &sensor_dev_attr_temp3_input.dev_attr);
> -		if (err)
> -			goto exit_remove;
> -		if (data->sensorsp & SEL_PLACE) {
> -			err = device_create_file(&pdev->dev,
> -					   &sensor_dev_attr_temp4_input.
> -					   dev_attr);
> -			if (err)
> -				goto exit_remove;
> -		}
> -	}
> -
> -	err = device_create_file(&pdev->dev, &dev_attr_name);
> -	if (err)
> -		goto exit_remove;
>  
> -	data->hwmon_dev = hwmon_device_register(&pdev->dev);
> +	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
> +							 "k8temp",
> +							 data,
> +							 &k8temp_chip_info,
> +							 NULL);
>  
> -	if (IS_ERR(data->hwmon_dev)) {
> -		err = PTR_ERR(data->hwmon_dev);
> -		goto exit_remove;
> -	}
> -
> -	return 0;
> -
> -exit_remove:
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp1_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp2_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp3_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp4_input.dev_attr);
> -	device_remove_file(&pdev->dev, &dev_attr_name);
> -	return err;
> -}
> -
> -static void k8temp_remove(struct pci_dev *pdev)
> -{
> -	struct k8temp_data *data = pci_get_drvdata(pdev);
> -
> -	hwmon_device_unregister(data->hwmon_dev);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp1_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp2_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp3_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp4_input.dev_attr);
> -	device_remove_file(&pdev->dev, &dev_attr_name);
> +	return PTR_ERR_OR_ZERO(hwmon_dev);
>  }
>  
>  static struct pci_driver k8temp_driver = {
>  	.name = "k8temp",
>  	.id_table = k8temp_ids,
>  	.probe = k8temp_probe,
> -	.remove = k8temp_remove,
>  };
>  
>  module_pci_driver(k8temp_driver);

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 1/1] hwmon: (k8temp) update to use new hwmon registration API
  2019-07-21 15:14     ` Guenter Roeck
@ 2019-07-21 20:37       ` avoidr
  2019-07-22  0:03         ` Guenter Roeck
  0 siblings, 1 reply; 8+ messages in thread
From: avoidr @ 2019-07-21 20:37 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Rudolf Marek, Jean Delvare, linux-hwmon, linux-kernel,
	Robert Karszniewicz, linux-hwmon-owner

On 2019-07-21 17:14, Guenter Roeck wrote:
> On Sun, Jul 21, 2019 at 02:00:51PM +0200, Robert Karszniewicz wrote:
>> Removes:
>> - hwmon_dev from k8temp_data struct, as that is now passed
>>   to callbacks, anyway.
>> - other k8temp_data struct fields, too.
>> - k8temp_update_device()
>>
>> Also reduces binary size:
>>    text    data     bss     dec     hex filename
>>    4139    1448       0    5587    15d3 drivers/hwmon/k8temp.ko.bak
>>    3103    1220       0    4323    10e3 drivers/hwmon/k8temp.ko
>>
>> Signed-off-by: Robert Karszniewicz <avoidr@firemail.cc>
>> Signed-off-by: Robert Karszniewicz <avoidr@riseup.net>
> 
> Applied.

Thank you! It's been a joy!

>> ---
>> Changes from v2:
>> - if (data->swap_core_select)
>> -     core ^= 1;
>> + core ^= data->swap_core_select;
>>
>> However, that produces slightly more .text than v2, and is a tad too
>> "tricky", I personally find.
>>
> Interesting - for me it produces ~30 bytes less code (with gcc 7.4.0).

Strange. I just verified to make sure and I do get ~30 bytes /more/ code
(with gcc 9.1.0).

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 1/1] hwmon: (k8temp) update to use new hwmon registration API
  2019-07-21 20:37       ` avoidr
@ 2019-07-22  0:03         ` Guenter Roeck
  0 siblings, 0 replies; 8+ messages in thread
From: Guenter Roeck @ 2019-07-22  0:03 UTC (permalink / raw)
  To: avoidr
  Cc: Rudolf Marek, Jean Delvare, linux-hwmon, linux-kernel,
	Robert Karszniewicz, linux-hwmon-owner

On 7/21/19 1:37 PM, avoidr@riseup.net wrote:
> On 2019-07-21 17:14, Guenter Roeck wrote:
>> On Sun, Jul 21, 2019 at 02:00:51PM +0200, Robert Karszniewicz wrote:
>>> Removes:
>>> - hwmon_dev from k8temp_data struct, as that is now passed
>>>    to callbacks, anyway.
>>> - other k8temp_data struct fields, too.
>>> - k8temp_update_device()
>>>
>>> Also reduces binary size:
>>>     text    data     bss     dec     hex filename
>>>     4139    1448       0    5587    15d3 drivers/hwmon/k8temp.ko.bak
>>>     3103    1220       0    4323    10e3 drivers/hwmon/k8temp.ko
>>>
>>> Signed-off-by: Robert Karszniewicz <avoidr@firemail.cc>
>>> Signed-off-by: Robert Karszniewicz <avoidr@riseup.net>
>>
>> Applied.
> 
> Thank you! It's been a joy!
> 
>>> ---
>>> Changes from v2:
>>> - if (data->swap_core_select)
>>> -     core ^= 1;
>>> + core ^= data->swap_core_select;
>>>
>>> However, that produces slightly more .text than v2, and is a tad too
>>> "tricky", I personally find.
>>>
>> Interesting - for me it produces ~30 bytes less code (with gcc 7.4.0).
> 
> Strange. I just verified to make sure and I do get ~30 bytes /more/ code
> (with gcc 9.1.0).
> 

Mine is gcc 7.4.0. Oh well...

Guenter

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] hwmon: (k8temp) update to use new hwmon registration API
       [not found] <BVOE1YNHJDU6.38N6DGWH9KX7H@HP>
@ 2019-07-20 21:12 ` Guenter Roeck
  0 siblings, 0 replies; 8+ messages in thread
From: Guenter Roeck @ 2019-07-20 21:12 UTC (permalink / raw)
  To: Robert Karszniewicz, Rudolf Marek, Jean Delvare; +Cc: linux-hwmon, linux-kernel

On 7/20/19 2:05 PM, Robert Karszniewicz wrote:
> Hello Guenter.
> 
> Thank you for your feedback! I am working on a version 2.
> 
> On Sat Jul 20, 2019 at 7:08 AM Guenter Roeck wrote:
>>> +static umode_t
>>> +k8temp_is_visible(const void *drvdata, enum hwmon_sensor_types type,
>>> +		  u32 attr, int channel)
>>> +{
>>> +	if (type != hwmon_temp)
>>> +		return 0;
>>
>> Not really needed since only temperature sensors are registered in the first place.
>>
>>> +
>>> +	if (attr != hwmon_temp_input)
>>> +		return 0;
>>> +
>>
>> The idea here is to only create sensors if they actually exist, so I would expect
>> something like the following here.
> 
> I realised that in the probe() function I deleted all the code which
> conditionally creates the sysfs files. Is that how it's supposed to be
> done and that's exactly what is_visible() is for, or is the proper way
> to still create the sysfs files conditionally, too?
> 

Yes, that is what the function is for. sysfs files are created in the core,
based on the result from the call to the is_visible function. If the function
returns 0, the respective sysfs file won't be created.

Thanks,
Guenter

>> 	struct k8temp_data *data = drvdata;
>>
>> 	if (attr != hwmon_temp_input)
>> 		return 0;
>>
>> 	if ((channel & 1) && !(data->sensorsp & SEL_PLACE))
>> 		return 0;
>>
>> 	if ((channel & 2) && !(data->sensorsp & SEL_CORE))
>> 		return 0;
>>
>>> +	return 0444;
>>> +}
> 
>>> +	if (data->swap_core_select)
>>> +		core = core ? 0 : 1;
>>
>> 		core = 1 - core;
>>
>> would accomplish the same without conditional.
> 
> How do you like
> 	core ^= 1;
> ?
> 
>>>    static int k8temp_probe(struct pci_dev *pdev,
>>>    				  const struct pci_device_id *id)
>>>    {
>>> -	int err;
>>>    	u8 scfg;
>>>    	u32 temp;
>>>    	u8 model, stepping;
>>>    	struct k8temp_data *data;
>>> +	struct device *hwmon_dev;
>>>    
>>>    	data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
>>>    	if (!data)
>>> @@ -233,84 +274,23 @@ static int k8temp_probe(struct pci_dev *pdev,
>>>    
>>>    	data->name = "k8temp";
>>>    	mutex_init(&data->update_lock);
>>> -	pci_set_drvdata(pdev, data);
>>> -
>>> -	/* Register sysfs hooks */
>>> -	err = device_create_file(&pdev->dev,
>>> -			   &sensor_dev_attr_temp1_input.dev_attr);
>>> -	if (err)
>>> -		goto exit_remove;
>>> -
>>> -	/* sensor can be changed and reports something */
>>> -	if (data->sensorsp & SEL_PLACE) {
>>> -		err = device_create_file(&pdev->dev,
>>> -				   &sensor_dev_attr_temp2_input.dev_attr);
>>> -		if (err)
>>> -			goto exit_remove;
>>> -	}
>>> -
>>> -	/* core can be changed and reports something */
>>> -	if (data->sensorsp & SEL_CORE) {
>>> -		err = device_create_file(&pdev->dev,
>>> -				   &sensor_dev_attr_temp3_input.dev_attr);
>>> -		if (err)
>>> -			goto exit_remove;
>>> -		if (data->sensorsp & SEL_PLACE) {
>>> -			err = device_create_file(&pdev->dev,
>>> -					   &sensor_dev_attr_temp4_input.
>>> -					   dev_attr);
>>> -			if (err)
>>> -				goto exit_remove;
>>> -		}
>>> -	}
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] hwmon: (k8temp) update to use new hwmon registration API
  2019-07-20 13:16 ` [PATCH 1/2] " Robert Karszniewicz
@ 2019-07-20 14:08   ` Guenter Roeck
  0 siblings, 0 replies; 8+ messages in thread
From: Guenter Roeck @ 2019-07-20 14:08 UTC (permalink / raw)
  To: Robert Karszniewicz, Rudolf Marek, Jean Delvare; +Cc: linux-hwmon, linux-kernel

Hi Robert,

On 7/20/19 6:16 AM, Robert Karszniewicz wrote:
> Also removes hwmon_dev from k8temp_data struct, as that is now passed
> to callbacks, anyway.
> 
> Also reduces binary size:
>     text    data     bss     dec     hex filename
>     4185    1384       0    5569    15c1 drivers/hwmon/k8temp.ko.bak
>     3877    1120       0    4997    1385 drivers/hwmon/k8temp.ko
> 

Excellent. Couple of comments below.

> Signed-off-by: Robert Karszniewicz <avoidr@firemail.cc>
> ---
>   drivers/hwmon/k8temp.c | 204 +++++++++++++++++++----------------------
>   1 file changed, 92 insertions(+), 112 deletions(-)
> 
> diff --git a/drivers/hwmon/k8temp.c b/drivers/hwmon/k8temp.c
> index 4994c90c8929..2e8fa1e07398 100644
> --- a/drivers/hwmon/k8temp.c
> +++ b/drivers/hwmon/k8temp.c
> @@ -13,7 +13,6 @@
>   #include <linux/jiffies.h>
>   #include <linux/pci.h>
>   #include <linux/hwmon.h>
> -#include <linux/hwmon-sysfs.h>
>   #include <linux/err.h>
>   #include <linux/mutex.h>
>   #include <asm/processor.h>
> @@ -24,7 +23,6 @@
>   #define SEL_CORE	0x04
>   
>   struct k8temp_data {
> -	struct device *hwmon_dev;
>   	struct mutex update_lock;
>   	const char *name;

No longer needed (see below)

>   	char valid;		/* zero until following fields are valid */
> @@ -40,7 +38,7 @@ struct k8temp_data {
>   static struct k8temp_data *k8temp_update_device(struct device *dev)
>   {
>   	struct k8temp_data *data = dev_get_drvdata(dev);
> -	struct pci_dev *pdev = to_pci_dev(dev);
> +	struct pci_dev *pdev = to_pci_dev(dev->parent);
>   	u8 tmp;
>   
>   	mutex_lock(&data->update_lock);
> @@ -82,50 +80,10 @@ static struct k8temp_data *k8temp_update_device(struct device *dev)
>   	return data;
>   }
>   
> -/*
> - * Sysfs stuff
> - */
> -
> -static ssize_t name_show(struct device *dev, struct device_attribute
> -			 *devattr, char *buf)
> -{
> -	struct k8temp_data *data = dev_get_drvdata(dev);
> -
> -	return sprintf(buf, "%s\n", data->name);
> -}
> -
> -
> -static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
> -			 char *buf)
> -{
> -	struct sensor_device_attribute_2 *attr =
> -	    to_sensor_dev_attr_2(devattr);
> -	int core = attr->nr;
> -	int place = attr->index;
> -	int temp;
> -	struct k8temp_data *data = k8temp_update_device(dev);
> -
> -	if (data->swap_core_select && (data->sensorsp & SEL_CORE))
> -		core = core ? 0 : 1;
> -
> -	temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
> -
> -	return sprintf(buf, "%d\n", temp);
> -}
> -
> -/* core, place */
> -
> -static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0);
> -static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1);
> -static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 1, 0);
> -static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 1, 1);
> -static DEVICE_ATTR_RO(name);
> -
>   static const struct pci_device_id k8temp_ids[] = {
>   	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
>   	{ 0 },
>   };
> -
>   MODULE_DEVICE_TABLE(pci, k8temp_ids);
>   
>   static int is_rev_g_desktop(u8 model)
> @@ -159,14 +117,97 @@ static int is_rev_g_desktop(u8 model)
>   	return 1;
>   }
>   
> +static umode_t
> +k8temp_is_visible(const void *drvdata, enum hwmon_sensor_types type,
> +		  u32 attr, int channel)
> +{
> +	if (type != hwmon_temp)
> +		return 0;

Not really needed since only temperature sensors are registered in the first place.

> +
> +	if (attr != hwmon_temp_input)
> +		return 0;
> +

The idea here is to only create sensors if they actually exist, so I would expect
something like the following here.

	struct k8temp_data *data = drvdata;

	if (attr != hwmon_temp_input)
		return 0;

	if ((channel & 1) && !(data->sensorsp & SEL_PLACE))
		return 0;

	if ((channel & 2) && !(data->sensorsp & SEL_CORE))
		return 0;

> +	return 0444;
> +}
> +
> +static int
> +k8temp_read(struct device *dev, enum hwmon_sensor_types type,
> +	    u32 attr, int channel, long *val)
> +{
> +	struct k8temp_data *data = k8temp_update_device(dev);

It might make sense to remove all the caching. Reading the sensors
isn't that expensive, after all. More on that see below.

> +	int core, place;
> +
> +	if (type != hwmon_temp)
> +		return -EINVAL;
> +
> +	if (attr != hwmon_temp_input)
> +		return -EINVAL;
> +

The above checks are not needed here, since other sensors and types
have not been instantiated.

> +	switch (channel) {
> +	case 0:
> +		core = 0;
> +		place = 0;
> +		break;
> +	case 1:
> +		core = 0;
> +		place = 1;
> +		break;
> +	case 2:
> +		core = 1;
> +		place = 0;
> +		break;
> +	case 3:
> +		core = 1;
> +		place = 1;
> +		break;
> +	default:
> +		return -EINVAL;
> +	}

How about
	core = (channel >> 1) & 1;
	place = channel & 1;
instead ?

> +
> +	if (data->swap_core_select)
> +		core = core ? 0 : 1;

		core = 1 - core;

would accomplish the same without conditional.

As for caching, let's see. Something like the following should do.

	struct pci_dev *pdev = to_pci_dev(dev);
	u32 temp;
	u8 tmp;
	...

	mutex_lock(&data->update_lock);
	pci_read_config_byte(pdev, REG_TEMP, &tmp);
	tmp &= ~(SEL_PLACE | SEL_CORE);
	if (channel)
		tmp |= SEL_PLACE;
	if (core)
		tmp |= SEL_CORE;
	pci_write_config_byte(pdev, REG_TEMP, tmp);
	pci_read_config_dword(pdev, REG_TEMP, &temp);
	mutex_unlock(&data->update_lock);

	*val = TEMP_FROM_REG(temp) + data->temp_offset;

... and then you could drop a lot of code.

> +
> +	*val = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
> +
> +	return 0;
> +}
> +
> +static const struct hwmon_ops k8temp_ops = {
> +	.is_visible = k8temp_is_visible,
> +	.read = k8temp_read,
> +};
> +
> +static const u32 k8temp_config[] = {
> +	HWMON_T_INPUT,
> +	HWMON_T_INPUT,
> +	HWMON_T_INPUT,
> +	HWMON_T_INPUT,
> +	0
> +};
> +	
> +static const struct hwmon_channel_info k8temp_temp_info = {
> +	.type = hwmon_temp,
> +	.config = k8temp_config,
> +};
> +
> +static const struct hwmon_channel_info *k8temp_info[] = {
> +	&k8temp_temp_info,
> +	NULL
> +};

static const struct hwmon_channel_info *k8temp_info[] = {
	HWMON_CHANNEL_INFO(temp,
		HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT),
	NULL
};

> +
> +static const struct hwmon_chip_info k8temp_chip_info = {
> +	.ops = &k8temp_ops,
> +	.info = k8temp_info,
> +};
> +
>   static int k8temp_probe(struct pci_dev *pdev,
>   				  const struct pci_device_id *id)
>   {
> -	int err;
>   	u8 scfg;
>   	u32 temp;
>   	u8 model, stepping;
>   	struct k8temp_data *data;
> +	struct device *hwmon_dev;
>   
>   	data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
>   	if (!data)
> @@ -233,84 +274,23 @@ static int k8temp_probe(struct pci_dev *pdev,
>   
>   	data->name = "k8temp";
>   	mutex_init(&data->update_lock);
> -	pci_set_drvdata(pdev, data);
> -
> -	/* Register sysfs hooks */
> -	err = device_create_file(&pdev->dev,
> -			   &sensor_dev_attr_temp1_input.dev_attr);
> -	if (err)
> -		goto exit_remove;
> -
> -	/* sensor can be changed and reports something */
> -	if (data->sensorsp & SEL_PLACE) {
> -		err = device_create_file(&pdev->dev,
> -				   &sensor_dev_attr_temp2_input.dev_attr);
> -		if (err)
> -			goto exit_remove;
> -	}
> -
> -	/* core can be changed and reports something */
> -	if (data->sensorsp & SEL_CORE) {
> -		err = device_create_file(&pdev->dev,
> -				   &sensor_dev_attr_temp3_input.dev_attr);
> -		if (err)
> -			goto exit_remove;
> -		if (data->sensorsp & SEL_PLACE) {
> -			err = device_create_file(&pdev->dev,
> -					   &sensor_dev_attr_temp4_input.
> -					   dev_attr);
> -			if (err)
> -				goto exit_remove;
> -		}
> -	}
>   
> -	err = device_create_file(&pdev->dev, &dev_attr_name);
> -	if (err)
> -		goto exit_remove;
> +	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
> +							 data->name,

data->name isn't needed. Just pass "k8temp" directly.

> +							 data,
> +							 &k8temp_chip_info,
> +							 NULL);
>   
> -	data->hwmon_dev = hwmon_device_register(&pdev->dev);
> -
> -	if (IS_ERR(data->hwmon_dev)) {
> -		err = PTR_ERR(data->hwmon_dev);
> -		goto exit_remove;
> -	}
> +	if (IS_ERR(hwmon_dev))
> +		return PTR_ERR(hwmon_dev);
>   
>   	return 0;

	return PTR_ERR_OR_ZERO(hwmon_dev);

> -
> -exit_remove:
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp1_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp2_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp3_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp4_input.dev_attr);
> -	device_remove_file(&pdev->dev, &dev_attr_name);
> -	return err;
> -}
> -
> -static void k8temp_remove(struct pci_dev *pdev)
> -{
> -	struct k8temp_data *data = pci_get_drvdata(pdev);
> -
> -	hwmon_device_unregister(data->hwmon_dev);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp1_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp2_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp3_input.dev_attr);
> -	device_remove_file(&pdev->dev,
> -			   &sensor_dev_attr_temp4_input.dev_attr);
> -	device_remove_file(&pdev->dev, &dev_attr_name);
>   }
>   
>   static struct pci_driver k8temp_driver = {
>   	.name = "k8temp",
>   	.id_table = k8temp_ids,
>   	.probe = k8temp_probe,
> -	.remove = k8temp_remove,
>   };
>   
>   module_pci_driver(k8temp_driver);
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/2] hwmon: (k8temp) update to use new hwmon registration API
  2019-07-20 13:15 [PATCH 0/2] " Robert Karszniewicz
@ 2019-07-20 13:16 ` Robert Karszniewicz
  2019-07-20 14:08   ` Guenter Roeck
  0 siblings, 1 reply; 8+ messages in thread
From: Robert Karszniewicz @ 2019-07-20 13:16 UTC (permalink / raw)
  To: Rudolf Marek, Jean Delvare, Guenter Roeck
  Cc: Robert Karszniewicz, linux-hwmon, linux-kernel

Also removes hwmon_dev from k8temp_data struct, as that is now passed
to callbacks, anyway.

Also reduces binary size:
   text    data     bss     dec     hex filename
   4185    1384       0    5569    15c1 drivers/hwmon/k8temp.ko.bak
   3877    1120       0    4997    1385 drivers/hwmon/k8temp.ko

Signed-off-by: Robert Karszniewicz <avoidr@firemail.cc>
---
 drivers/hwmon/k8temp.c | 204 +++++++++++++++++++----------------------
 1 file changed, 92 insertions(+), 112 deletions(-)

diff --git a/drivers/hwmon/k8temp.c b/drivers/hwmon/k8temp.c
index 4994c90c8929..2e8fa1e07398 100644
--- a/drivers/hwmon/k8temp.c
+++ b/drivers/hwmon/k8temp.c
@@ -13,7 +13,6 @@
 #include <linux/jiffies.h>
 #include <linux/pci.h>
 #include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
 #include <linux/err.h>
 #include <linux/mutex.h>
 #include <asm/processor.h>
@@ -24,7 +23,6 @@
 #define SEL_CORE	0x04
 
 struct k8temp_data {
-	struct device *hwmon_dev;
 	struct mutex update_lock;
 	const char *name;
 	char valid;		/* zero until following fields are valid */
@@ -40,7 +38,7 @@ struct k8temp_data {
 static struct k8temp_data *k8temp_update_device(struct device *dev)
 {
 	struct k8temp_data *data = dev_get_drvdata(dev);
-	struct pci_dev *pdev = to_pci_dev(dev);
+	struct pci_dev *pdev = to_pci_dev(dev->parent);
 	u8 tmp;
 
 	mutex_lock(&data->update_lock);
@@ -82,50 +80,10 @@ static struct k8temp_data *k8temp_update_device(struct device *dev)
 	return data;
 }
 
-/*
- * Sysfs stuff
- */
-
-static ssize_t name_show(struct device *dev, struct device_attribute
-			 *devattr, char *buf)
-{
-	struct k8temp_data *data = dev_get_drvdata(dev);
-
-	return sprintf(buf, "%s\n", data->name);
-}
-
-
-static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
-			 char *buf)
-{
-	struct sensor_device_attribute_2 *attr =
-	    to_sensor_dev_attr_2(devattr);
-	int core = attr->nr;
-	int place = attr->index;
-	int temp;
-	struct k8temp_data *data = k8temp_update_device(dev);
-
-	if (data->swap_core_select && (data->sensorsp & SEL_CORE))
-		core = core ? 0 : 1;
-
-	temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
-
-	return sprintf(buf, "%d\n", temp);
-}
-
-/* core, place */
-
-static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0);
-static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1);
-static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 1, 0);
-static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 1, 1);
-static DEVICE_ATTR_RO(name);
-
 static const struct pci_device_id k8temp_ids[] = {
 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
 	{ 0 },
 };
-
 MODULE_DEVICE_TABLE(pci, k8temp_ids);
 
 static int is_rev_g_desktop(u8 model)
@@ -159,14 +117,97 @@ static int is_rev_g_desktop(u8 model)
 	return 1;
 }
 
+static umode_t
+k8temp_is_visible(const void *drvdata, enum hwmon_sensor_types type,
+		  u32 attr, int channel)
+{
+	if (type != hwmon_temp)
+		return 0;
+
+	if (attr != hwmon_temp_input)
+		return 0;
+
+	return 0444;
+}
+
+static int
+k8temp_read(struct device *dev, enum hwmon_sensor_types type,
+	    u32 attr, int channel, long *val)
+{
+	struct k8temp_data *data = k8temp_update_device(dev);
+	int core, place;
+
+	if (type != hwmon_temp)
+		return -EINVAL;
+
+	if (attr != hwmon_temp_input)
+		return -EINVAL;
+
+	switch (channel) {
+	case 0:
+		core = 0;
+		place = 0;
+		break;
+	case 1:
+		core = 0;
+		place = 1;
+		break;
+	case 2:
+		core = 1;
+		place = 0;
+		break;
+	case 3:
+		core = 1;
+		place = 1;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (data->swap_core_select)
+		core = core ? 0 : 1;
+
+	*val = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
+
+	return 0;
+}
+
+static const struct hwmon_ops k8temp_ops = {
+	.is_visible = k8temp_is_visible,
+	.read = k8temp_read,
+};
+
+static const u32 k8temp_config[] = {
+	HWMON_T_INPUT,
+	HWMON_T_INPUT,
+	HWMON_T_INPUT,
+	HWMON_T_INPUT,
+	0
+};
+
+static const struct hwmon_channel_info k8temp_temp_info = {
+	.type = hwmon_temp,
+	.config = k8temp_config,
+};
+
+static const struct hwmon_channel_info *k8temp_info[] = {
+	&k8temp_temp_info,
+	NULL
+};
+
+static const struct hwmon_chip_info k8temp_chip_info = {
+	.ops = &k8temp_ops,
+	.info = k8temp_info,
+};
+
 static int k8temp_probe(struct pci_dev *pdev,
 				  const struct pci_device_id *id)
 {
-	int err;
 	u8 scfg;
 	u32 temp;
 	u8 model, stepping;
 	struct k8temp_data *data;
+	struct device *hwmon_dev;
 
 	data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
 	if (!data)
@@ -233,84 +274,23 @@ static int k8temp_probe(struct pci_dev *pdev,
 
 	data->name = "k8temp";
 	mutex_init(&data->update_lock);
-	pci_set_drvdata(pdev, data);
-
-	/* Register sysfs hooks */
-	err = device_create_file(&pdev->dev,
-			   &sensor_dev_attr_temp1_input.dev_attr);
-	if (err)
-		goto exit_remove;
-
-	/* sensor can be changed and reports something */
-	if (data->sensorsp & SEL_PLACE) {
-		err = device_create_file(&pdev->dev,
-				   &sensor_dev_attr_temp2_input.dev_attr);
-		if (err)
-			goto exit_remove;
-	}
-
-	/* core can be changed and reports something */
-	if (data->sensorsp & SEL_CORE) {
-		err = device_create_file(&pdev->dev,
-				   &sensor_dev_attr_temp3_input.dev_attr);
-		if (err)
-			goto exit_remove;
-		if (data->sensorsp & SEL_PLACE) {
-			err = device_create_file(&pdev->dev,
-					   &sensor_dev_attr_temp4_input.
-					   dev_attr);
-			if (err)
-				goto exit_remove;
-		}
-	}
 
-	err = device_create_file(&pdev->dev, &dev_attr_name);
-	if (err)
-		goto exit_remove;
+	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
+							 data->name,
+							 data,
+							 &k8temp_chip_info,
+							 NULL);
 
-	data->hwmon_dev = hwmon_device_register(&pdev->dev);
-
-	if (IS_ERR(data->hwmon_dev)) {
-		err = PTR_ERR(data->hwmon_dev);
-		goto exit_remove;
-	}
+	if (IS_ERR(hwmon_dev))
+		return PTR_ERR(hwmon_dev);
 
 	return 0;
-
-exit_remove:
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp1_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp2_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp3_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp4_input.dev_attr);
-	device_remove_file(&pdev->dev, &dev_attr_name);
-	return err;
-}
-
-static void k8temp_remove(struct pci_dev *pdev)
-{
-	struct k8temp_data *data = pci_get_drvdata(pdev);
-
-	hwmon_device_unregister(data->hwmon_dev);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp1_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp2_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp3_input.dev_attr);
-	device_remove_file(&pdev->dev,
-			   &sensor_dev_attr_temp4_input.dev_attr);
-	device_remove_file(&pdev->dev, &dev_attr_name);
 }
 
 static struct pci_driver k8temp_driver = {
 	.name = "k8temp",
 	.id_table = k8temp_ids,
 	.probe = k8temp_probe,
-	.remove = k8temp_remove,
 };
 
 module_pci_driver(k8temp_driver);
-- 
2.22.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-07-22  0:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <BVOE7U9MRMZY.38N6DGWH9KX7H@HP>
2019-07-20 22:46 ` [PATCH 1/2] hwmon: (k8temp) update to use new hwmon registration API Guenter Roeck
2019-07-21 12:00   ` [PATCH v3 1/1] " Robert Karszniewicz
2019-07-21 15:14     ` Guenter Roeck
2019-07-21 20:37       ` avoidr
2019-07-22  0:03         ` Guenter Roeck
     [not found] <BVOE1YNHJDU6.38N6DGWH9KX7H@HP>
2019-07-20 21:12 ` [PATCH 1/2] " Guenter Roeck
2019-07-20 13:15 [PATCH 0/2] " Robert Karszniewicz
2019-07-20 13:16 ` [PATCH 1/2] " Robert Karszniewicz
2019-07-20 14:08   ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).