All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/3] hwmon: (mcp3021) rework for DT support
@ 2016-10-27 22:33 Clemens Gruber
  2016-10-27 22:33   ` Clemens Gruber
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Clemens Gruber @ 2016-10-27 22:33 UTC (permalink / raw
  To: linux-hwmon
  Cc: Guenter Roeck, Rob Herring, Jean Delvare, devicetree, linux-doc,
	linux-kernel, Clemens Gruber

Support setting the reference voltage from the device tree.
Rework of driver structure, put chip specific data in a separate
structure and assign it depending on device id from platform data or
DT match.

Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
---
 Documentation/hwmon/mcp3021 |   6 ++
 drivers/hwmon/mcp3021.c     | 145 ++++++++++++++++++++++++++++++++------------
 2 files changed, 111 insertions(+), 40 deletions(-)

diff --git a/Documentation/hwmon/mcp3021 b/Documentation/hwmon/mcp3021
index 74a6b72..be252b7 100644
--- a/Documentation/hwmon/mcp3021
+++ b/Documentation/hwmon/mcp3021
@@ -12,6 +12,7 @@ Supported chips:
 Authors:
    Mingkai Hu
    Sven Schuchmann <schuchmann@schleissheimer.de>
+   Clemens Gruber <clemens.gruber@pqgruber.com>
 
 Description
 -----------
@@ -27,3 +28,8 @@ Communication to the MCP3021/MCP3221  is performed using a 2-wire I2C
 compatible interface. Standard (100 kHz) and Fast (400 kHz) I2C modes are
 available. The default I2C device address is 0x4d (contact the Microchip
 factory for additional address options).
+
+The reference voltage used in the conversion can be set through platform data
+in millivolt (for backwards compatibility) or via device tree in microvolt.
+Please refer to Documentation/devicetree/bindings/i2c/mcp3021.txt for details
+about the device tree bindings.
diff --git a/drivers/hwmon/mcp3021.c b/drivers/hwmon/mcp3021.c
index 972444a..a8cf97f 100644
--- a/drivers/hwmon/mcp3021.c
+++ b/drivers/hwmon/mcp3021.c
@@ -4,6 +4,7 @@
  * Copyright (C) 2008-2009, 2012 Freescale Semiconductor, Inc.
  * Author: Mingkai Hu <Mingkai.hu@freescale.com>
  * Reworked by Sven Schuchmann <schuchmann@schleissheimer.de>
+ * Copyright (C) 2016 Clemens Gruber <clemens.gruber@pqgruber.com>
  *
  * This driver export the value of analog input voltage to sysfs, the
  * voltage unit is mV. Through the sysfs interface, lm-sensors tool
@@ -22,37 +23,56 @@
 #include <linux/i2c.h>
 #include <linux/err.h>
 #include <linux/device.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
 
-/* Vdd info */
+/* Vdd / reference voltage in millivolt */
 #define MCP3021_VDD_MAX		5500
 #define MCP3021_VDD_MIN		2700
-#define MCP3021_VDD_REF		3300
-
-/* output format */
-#define MCP3021_SAR_SHIFT	2
-#define MCP3021_SAR_MASK	0x3ff
-#define MCP3021_OUTPUT_RES	10	/* 10-bit resolution */
-
-#define MCP3221_SAR_SHIFT	0
-#define MCP3221_SAR_MASK	0xfff
-#define MCP3221_OUTPUT_RES	12	/* 12-bit resolution */
+#define MCP3021_VDD_DEFAULT	3300
 
 enum chips {
 	mcp3021,
 	mcp3221
 };
 
+struct mcp3021_chip_info {
+	u16 sar_shift;
+	u16 sar_mask;
+	u8 output_res;
+};
+
 /*
  * Client data (each client gets its own)
  */
 struct mcp3021_data {
 	struct device *hwmon_dev;
-	u32 vdd;	/* device power supply */
-	u16 sar_shift;
-	u16 sar_mask;
-	u8 output_res;
+	const struct mcp3021_chip_info *chip_info;
+	u32 vdd; /* device power supply and reference voltage in millivolt */
 };
 
+static const struct mcp3021_chip_info mcp3021_chip_info_tbl[] = {
+	[mcp3021] = {
+		.sar_shift = 2,
+		.sar_mask = 0x3ff,
+		.output_res = 10,	/* 10-bit resolution */
+	},
+	[mcp3221] = {
+		.sar_shift = 0,
+		.sar_mask = 0xfff,
+		.output_res = 12,	/* 12-bit resolution */
+	},
+};
+
+#ifdef CONFIG_OF
+static const struct of_device_id of_mcp3021_match[] = {
+	{ .compatible = "microchip,mcp3021", .data = (void *)mcp3021 },
+	{ .compatible = "microchip,mcp3221", .data = (void *)mcp3221 },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, of_mcp3021_match);
+#endif
+
 static int mcp3021_read16(struct i2c_client *client)
 {
 	struct mcp3021_data *data = i2c_get_clientdata(client);
@@ -73,14 +93,15 @@ static int mcp3021_read16(struct i2c_client *client)
 	 * The ten-bit output code is composed of the lower 4-bit of the
 	 * first byte and the upper 6-bit of the second byte.
 	 */
-	reg = (reg >> data->sar_shift) & data->sar_mask;
+	reg = (reg >> data->chip_info->sar_shift) & data->chip_info->sar_mask;
 
 	return reg;
 }
 
 static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
 {
-	return DIV_ROUND_CLOSEST(data->vdd * val, 1 << data->output_res);
+	return DIV_ROUND_CLOSEST(data->vdd * val,
+				 1 << data->chip_info->output_res);
 }
 
 static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
@@ -101,44 +122,85 @@ static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
 
 static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL);
 
-static int mcp3021_probe(struct i2c_client *client,
+#ifdef CONFIG_OF
+static int mcp3021_probe_dt(struct i2c_client *client,
 				const struct i2c_device_id *id)
 {
-	int err;
-	struct mcp3021_data *data = NULL;
+	struct mcp3021_data *data = i2c_get_clientdata(client);
+	struct device_node *np = client->dev.of_node;
+	const struct of_device_id *match;
+	int devid, ret;
 
-	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+	match = of_match_device(of_mcp3021_match, &client->dev);
+	if (!match)
 		return -ENODEV;
 
-	data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
-			    GFP_KERNEL);
-	if (!data)
-		return -ENOMEM;
-
-	i2c_set_clientdata(client, data);
+	devid = (int)(uintptr_t)match->data;
+	data->chip_info = &mcp3021_chip_info_tbl[devid];
 
-	switch (id->driver_data) {
-	case mcp3021:
-		data->sar_shift = MCP3021_SAR_SHIFT;
-		data->sar_mask = MCP3021_SAR_MASK;
-		data->output_res = MCP3021_OUTPUT_RES;
-		break;
-
-	case mcp3221:
-		data->sar_shift = MCP3221_SAR_SHIFT;
-		data->sar_mask = MCP3221_SAR_MASK;
-		data->output_res = MCP3221_OUTPUT_RES;
-		break;
+	ret = of_property_read_u32(np, "reference-voltage-microvolt",
+				   &data->vdd);
+	if (ret) {
+		/* fallback */
+		data->vdd = MCP3021_VDD_DEFAULT;
+		return 0;
 	}
 
+	/* Convert microvolt from DT to millivolt used in the formula */
+	data->vdd /= 1000;
+
+	if (data->vdd > MCP3021_VDD_MAX || data->vdd < MCP3021_VDD_MIN)
+		return -EINVAL;
+
+	return 0;
+}
+#else
+static int mcp3021_probe_dt(struct i2c_client *client,
+				const struct i2c_device_id *id)
+{
+	return 1;
+}
+#endif
+
+static int mcp3021_probe_pdata(struct i2c_client *client,
+				const struct i2c_device_id *id)
+{
+	struct mcp3021_data *data = i2c_get_clientdata(client);
+
+	data->chip_info = &mcp3021_chip_info_tbl[id->driver_data];
+
 	if (dev_get_platdata(&client->dev)) {
 		data->vdd = *(u32 *)dev_get_platdata(&client->dev);
 		if (data->vdd > MCP3021_VDD_MAX || data->vdd < MCP3021_VDD_MIN)
 			return -EINVAL;
 	} else {
-		data->vdd = MCP3021_VDD_REF;
+		data->vdd = MCP3021_VDD_DEFAULT;
 	}
 
+	return 0;
+}
+
+static int mcp3021_probe(struct i2c_client *client,
+				const struct i2c_device_id *id)
+{
+	struct mcp3021_data *data = NULL;
+	int err;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -ENODEV;
+
+	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	i2c_set_clientdata(client, data);
+
+	err = mcp3021_probe_dt(client, id);
+	if (err > 0)
+		mcp3021_probe_pdata(client, id);
+	else if (err < 0)
+		return err;
+
 	err = sysfs_create_file(&client->dev.kobj, &dev_attr_in0_input.attr);
 	if (err)
 		return err;
@@ -176,6 +238,9 @@ MODULE_DEVICE_TABLE(i2c, mcp3021_id);
 static struct i2c_driver mcp3021_driver = {
 	.driver = {
 		.name = "mcp3021",
+#ifdef CONFIG_OF
+		.of_match_table = of_match_ptr(of_mcp3021_match),
+#endif
 	},
 	.probe = mcp3021_probe,
 	.remove = mcp3021_remove,
-- 
2.10.1

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

* [PATCH v2 2/3] hwmon: (mcp3021) add devicetree bindings documentation
@ 2016-10-27 22:33   ` Clemens Gruber
  0 siblings, 0 replies; 9+ messages in thread
From: Clemens Gruber @ 2016-10-27 22:33 UTC (permalink / raw
  To: linux-hwmon
  Cc: Guenter Roeck, Rob Herring, Jean Delvare, devicetree, linux-doc,
	linux-kernel, Clemens Gruber

Document the devicetree bindings for the Microchip MCP3021/3221.

Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
---
 Documentation/devicetree/bindings/hwmon/mcp3021.txt | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/mcp3021.txt

diff --git a/Documentation/devicetree/bindings/hwmon/mcp3021.txt b/Documentation/devicetree/bindings/hwmon/mcp3021.txt
new file mode 100644
index 0000000..294318b
--- /dev/null
+++ b/Documentation/devicetree/bindings/hwmon/mcp3021.txt
@@ -0,0 +1,21 @@
+mcp3021 properties
+
+Required properties:
+- compatible: Must be one of the following:
+	- "microchip,mcp3021" for mcp3021
+	- "microchip,mcp3221" for mcp3221
+- reg: I2C address
+
+Optional properties:
+
+- reference-voltage-microvolt
+	Reference voltage in microvolt (uV)
+
+Example:
+
+mcp3021@4d {
+	compatible = "microchip,mcp3021";
+	reg = <0x4d>;
+
+	reference-voltage-microvolt = <4500000>; /* 4.5 V */
+};
-- 
2.10.1

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

* [PATCH v2 2/3] hwmon: (mcp3021) add devicetree bindings documentation
@ 2016-10-27 22:33   ` Clemens Gruber
  0 siblings, 0 replies; 9+ messages in thread
From: Clemens Gruber @ 2016-10-27 22:33 UTC (permalink / raw
  To: linux-hwmon-u79uwXL29TY76Z2rM5mHXA
  Cc: Guenter Roeck, Rob Herring, Jean Delvare,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-doc-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA, Clemens Gruber

Document the devicetree bindings for the Microchip MCP3021/3221.

Signed-off-by: Clemens Gruber <clemens.gruber-lZxf/j91sTNWk0Htik3J/w@public.gmane.org>
---
 Documentation/devicetree/bindings/hwmon/mcp3021.txt | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/mcp3021.txt

diff --git a/Documentation/devicetree/bindings/hwmon/mcp3021.txt b/Documentation/devicetree/bindings/hwmon/mcp3021.txt
new file mode 100644
index 0000000..294318b
--- /dev/null
+++ b/Documentation/devicetree/bindings/hwmon/mcp3021.txt
@@ -0,0 +1,21 @@
+mcp3021 properties
+
+Required properties:
+- compatible: Must be one of the following:
+	- "microchip,mcp3021" for mcp3021
+	- "microchip,mcp3221" for mcp3221
+- reg: I2C address
+
+Optional properties:
+
+- reference-voltage-microvolt
+	Reference voltage in microvolt (uV)
+
+Example:
+
+mcp3021@4d {
+	compatible = "microchip,mcp3021";
+	reg = <0x4d>;
+
+	reference-voltage-microvolt = <4500000>; /* 4.5 V */
+};
-- 
2.10.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH v2 3/3] hwmon: (mcp3021) replace S_IRUGO with 0444
  2016-10-27 22:33 [PATCH v2 1/3] hwmon: (mcp3021) rework for DT support Clemens Gruber
  2016-10-27 22:33   ` Clemens Gruber
@ 2016-10-27 22:33 ` Clemens Gruber
  2016-11-09 11:53   ` Guenter Roeck
  2 siblings, 0 replies; 9+ messages in thread
From: Clemens Gruber @ 2016-10-27 22:33 UTC (permalink / raw
  To: linux-hwmon
  Cc: Guenter Roeck, Rob Herring, Jean Delvare, devicetree, linux-doc,
	linux-kernel, Clemens Gruber

Replace S_IRUGO with the better readable 0444.
This fixes a checkpatch warning.

Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
---
 drivers/hwmon/mcp3021.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hwmon/mcp3021.c b/drivers/hwmon/mcp3021.c
index a8cf97f..97c832d 100644
--- a/drivers/hwmon/mcp3021.c
+++ b/drivers/hwmon/mcp3021.c
@@ -120,7 +120,7 @@ static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
 	return sprintf(buf, "%d\n", in_input);
 }
 
-static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL);
+static DEVICE_ATTR(in0_input, 0444, show_in_input, NULL);
 
 #ifdef CONFIG_OF
 static int mcp3021_probe_dt(struct i2c_client *client,
-- 
2.10.1

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

* Re: [PATCH v2 2/3] hwmon: (mcp3021) add devicetree bindings documentation
  2016-10-27 22:33   ` Clemens Gruber
  (?)
@ 2016-10-31  6:04   ` Rob Herring
  -1 siblings, 0 replies; 9+ messages in thread
From: Rob Herring @ 2016-10-31  6:04 UTC (permalink / raw
  To: Clemens Gruber
  Cc: linux-hwmon, Guenter Roeck, Jean Delvare, devicetree, linux-doc,
	linux-kernel

On Fri, Oct 28, 2016 at 12:33:44AM +0200, Clemens Gruber wrote:
> Document the devicetree bindings for the Microchip MCP3021/3221.
> 
> Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
> ---
>  Documentation/devicetree/bindings/hwmon/mcp3021.txt | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/hwmon/mcp3021.txt

Acked-by: Rob Herring <robh@kernel.org>

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

* Re: [PATCH v2 1/3] hwmon: (mcp3021) rework for DT support
@ 2016-11-09 11:53   ` Guenter Roeck
  0 siblings, 0 replies; 9+ messages in thread
From: Guenter Roeck @ 2016-11-09 11:53 UTC (permalink / raw
  To: Clemens Gruber, linux-hwmon
  Cc: Rob Herring, Jean Delvare, devicetree, linux-doc, linux-kernel

On 10/27/2016 03:33 PM, Clemens Gruber wrote:
> Support setting the reference voltage from the device tree.
> Rework of driver structure, put chip specific data in a separate
> structure and assign it depending on device id from platform data or
> DT match.
>
> Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
> ---
>  Documentation/hwmon/mcp3021 |   6 ++
>  drivers/hwmon/mcp3021.c     | 145 ++++++++++++++++++++++++++++++++------------
>  2 files changed, 111 insertions(+), 40 deletions(-)
>
> diff --git a/Documentation/hwmon/mcp3021 b/Documentation/hwmon/mcp3021
> index 74a6b72..be252b7 100644
> --- a/Documentation/hwmon/mcp3021
> +++ b/Documentation/hwmon/mcp3021
> @@ -12,6 +12,7 @@ Supported chips:
>  Authors:
>     Mingkai Hu
>     Sven Schuchmann <schuchmann@schleissheimer.de>
> +   Clemens Gruber <clemens.gruber@pqgruber.com>
>
>  Description
>  -----------
> @@ -27,3 +28,8 @@ Communication to the MCP3021/MCP3221  is performed using a 2-wire I2C
>  compatible interface. Standard (100 kHz) and Fast (400 kHz) I2C modes are
>  available. The default I2C device address is 0x4d (contact the Microchip
>  factory for additional address options).
> +
> +The reference voltage used in the conversion can be set through platform data
> +in millivolt (for backwards compatibility) or via device tree in microvolt.
> +Please refer to Documentation/devicetree/bindings/i2c/mcp3021.txt for details
> +about the device tree bindings.
> diff --git a/drivers/hwmon/mcp3021.c b/drivers/hwmon/mcp3021.c
> index 972444a..a8cf97f 100644
> --- a/drivers/hwmon/mcp3021.c
> +++ b/drivers/hwmon/mcp3021.c
> @@ -4,6 +4,7 @@
>   * Copyright (C) 2008-2009, 2012 Freescale Semiconductor, Inc.
>   * Author: Mingkai Hu <Mingkai.hu@freescale.com>
>   * Reworked by Sven Schuchmann <schuchmann@schleissheimer.de>
> + * Copyright (C) 2016 Clemens Gruber <clemens.gruber@pqgruber.com>
>   *
>   * This driver export the value of analog input voltage to sysfs, the
>   * voltage unit is mV. Through the sysfs interface, lm-sensors tool
> @@ -22,37 +23,56 @@
>  #include <linux/i2c.h>
>  #include <linux/err.h>
>  #include <linux/device.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
>
> -/* Vdd info */
> +/* Vdd / reference voltage in millivolt */
>  #define MCP3021_VDD_MAX		5500
>  #define MCP3021_VDD_MIN		2700
> -#define MCP3021_VDD_REF		3300
> -
> -/* output format */
> -#define MCP3021_SAR_SHIFT	2
> -#define MCP3021_SAR_MASK	0x3ff
> -#define MCP3021_OUTPUT_RES	10	/* 10-bit resolution */
> -
> -#define MCP3221_SAR_SHIFT	0
> -#define MCP3221_SAR_MASK	0xfff
> -#define MCP3221_OUTPUT_RES	12	/* 12-bit resolution */
> +#define MCP3021_VDD_DEFAULT	3300
>

There is no technical reason to drop / change those defines and use literals
in the code instead. Please don't.

>  enum chips {
>  	mcp3021,
>  	mcp3221
>  };
>
> +struct mcp3021_chip_info {
> +	u16 sar_shift;
> +	u16 sar_mask;
> +	u8 output_res;
> +};
> +
>  /*
>   * Client data (each client gets its own)
>   */
>  struct mcp3021_data {
>  	struct device *hwmon_dev;
> -	u32 vdd;	/* device power supply */
> -	u16 sar_shift;
> -	u16 sar_mask;
> -	u8 output_res;
> +	const struct mcp3021_chip_info *chip_info;
> +	u32 vdd; /* device power supply and reference voltage in millivolt */
>  };
>
> +static const struct mcp3021_chip_info mcp3021_chip_info_tbl[] = {
> +	[mcp3021] = {
> +		.sar_shift = 2,
> +		.sar_mask = 0x3ff,
> +		.output_res = 10,	/* 10-bit resolution */
> +	},
> +	[mcp3221] = {
> +		.sar_shift = 0,
> +		.sar_mask = 0xfff,
> +		.output_res = 12,	/* 12-bit resolution */
> +	},
> +};
> +
> +#ifdef CONFIG_OF
> +static const struct of_device_id of_mcp3021_match[] = {
> +	{ .compatible = "microchip,mcp3021", .data = (void *)mcp3021 },
> +	{ .compatible = "microchip,mcp3221", .data = (void *)mcp3221 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, of_mcp3021_match);
> +#endif
> +
>  static int mcp3021_read16(struct i2c_client *client)
>  {
>  	struct mcp3021_data *data = i2c_get_clientdata(client);
> @@ -73,14 +93,15 @@ static int mcp3021_read16(struct i2c_client *client)
>  	 * The ten-bit output code is composed of the lower 4-bit of the
>  	 * first byte and the upper 6-bit of the second byte.
>  	 */
> -	reg = (reg >> data->sar_shift) & data->sar_mask;
> +	reg = (reg >> data->chip_info->sar_shift) & data->chip_info->sar_mask;
>
>  	return reg;
>  }
>
>  static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
>  {
> -	return DIV_ROUND_CLOSEST(data->vdd * val, 1 << data->output_res);
> +	return DIV_ROUND_CLOSEST(data->vdd * val,
> +				 1 << data->chip_info->output_res);
>  }
>
>  static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
> @@ -101,44 +122,85 @@ static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
>
>  static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL);
>
> -static int mcp3021_probe(struct i2c_client *client,
> +#ifdef CONFIG_OF
> +static int mcp3021_probe_dt(struct i2c_client *client,
>  				const struct i2c_device_id *id)
>  {
> -	int err;
> -	struct mcp3021_data *data = NULL;
> +	struct mcp3021_data *data = i2c_get_clientdata(client);
> +	struct device_node *np = client->dev.of_node;
> +	const struct of_device_id *match;
> +	int devid, ret;
>
> -	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
> +	match = of_match_device(of_mcp3021_match, &client->dev);
> +	if (!match)
>  		return -ENODEV;
>
> -	data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
> -			    GFP_KERNEL);
> -	if (!data)
> -		return -ENOMEM;
> -
> -	i2c_set_clientdata(client, data);
> +	devid = (int)(uintptr_t)match->data;
> +	data->chip_info = &mcp3021_chip_info_tbl[devid];
>
> -	switch (id->driver_data) {
> -	case mcp3021:
> -		data->sar_shift = MCP3021_SAR_SHIFT;
> -		data->sar_mask = MCP3021_SAR_MASK;
> -		data->output_res = MCP3021_OUTPUT_RES;
> -		break;
> -
> -	case mcp3221:
> -		data->sar_shift = MCP3221_SAR_SHIFT;
> -		data->sar_mask = MCP3221_SAR_MASK;
> -		data->output_res = MCP3221_OUTPUT_RES;
> -		break;
> +	ret = of_property_read_u32(np, "reference-voltage-microvolt",
> +				   &data->vdd);
> +	if (ret) {
> +		/* fallback */
> +		data->vdd = MCP3021_VDD_DEFAULT;
> +		return 0;
>  	}
>
> +	/* Convert microvolt from DT to millivolt used in the formula */
> +	data->vdd /= 1000;
> +
> +	if (data->vdd > MCP3021_VDD_MAX || data->vdd < MCP3021_VDD_MIN)
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +#else
> +static int mcp3021_probe_dt(struct i2c_client *client,
> +				const struct i2c_device_id *id)
> +{
> +	return 1;
> +}
> +#endif
> +
> +static int mcp3021_probe_pdata(struct i2c_client *client,
> +				const struct i2c_device_id *id)
> +{
> +	struct mcp3021_data *data = i2c_get_clientdata(client);
> +
> +	data->chip_info = &mcp3021_chip_info_tbl[id->driver_data];
> +
>  	if (dev_get_platdata(&client->dev)) {
>  		data->vdd = *(u32 *)dev_get_platdata(&client->dev);
>  		if (data->vdd > MCP3021_VDD_MAX || data->vdd < MCP3021_VDD_MIN)
>  			return -EINVAL;
>  	} else {
> -		data->vdd = MCP3021_VDD_REF;
> +		data->vdd = MCP3021_VDD_DEFAULT;
>  	}
>
> +	return 0;
> +}
> +
> +static int mcp3021_probe(struct i2c_client *client,
> +				const struct i2c_device_id *id)
> +{
> +	struct mcp3021_data *data = NULL;
> +	int err;
> +
> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
> +		return -ENODEV;
> +
> +	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
> +	if (!data)
> +		return -ENOMEM;
> +
> +	i2c_set_clientdata(client, data);
> +
> +	err = mcp3021_probe_dt(client, id);
> +	if (err > 0)
> +		mcp3021_probe_pdata(client, id);
> +	else if (err < 0)
> +		return err;
> +
Most other drivers generate the platform data from devicetree data.

	if (of_match_device())
		pdata = probe_dt();
	else
		pdata = dev_get_platdata();

In this case, this is even simpler, since the only devicetree property is the reference
voltage.

	if (np) {
		if (of_property_read_u32(np, "reference-voltage-microvolt", &vdd))
			vdd = MCP3021_VDD_REF * 1000;
		vdd /= 1000;
		devid = (int)(uintptr_t)match->data;
	} else {
		u32 *pdata = dev_get_platdata(&client->dev);

		if (pdata)
			vdd = *pdata;
		else
			vdd = MCP3021_VDD_REF;
		devid = id->driver_data;
	}

You should actually be able to use id->driver_data directly in both cases since it is
always passed as parameter from the i2c core.

This would be much simpler than your suggested changes.

>  	err = sysfs_create_file(&client->dev.kobj, &dev_attr_in0_input.attr);
>  	if (err)
>  		return err;
> @@ -176,6 +238,9 @@ MODULE_DEVICE_TABLE(i2c, mcp3021_id);
>  static struct i2c_driver mcp3021_driver = {
>  	.driver = {
>  		.name = "mcp3021",
> +#ifdef CONFIG_OF
> +		.of_match_table = of_match_ptr(of_mcp3021_match),
> +#endif

of_match_ptr() is already protected with #ifdef CONFIG_OF and otherwise
returns NULL, so this ifdef is unnecessary.

>  	},
>  	.probe = mcp3021_probe,
>  	.remove = mcp3021_remove,
>

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

* Re: [PATCH v2 1/3] hwmon: (mcp3021) rework for DT support
@ 2016-11-09 11:53   ` Guenter Roeck
  0 siblings, 0 replies; 9+ messages in thread
From: Guenter Roeck @ 2016-11-09 11:53 UTC (permalink / raw
  To: Clemens Gruber, linux-hwmon-u79uwXL29TY76Z2rM5mHXA
  Cc: Rob Herring, Jean Delvare, devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-doc-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA

On 10/27/2016 03:33 PM, Clemens Gruber wrote:
> Support setting the reference voltage from the device tree.
> Rework of driver structure, put chip specific data in a separate
> structure and assign it depending on device id from platform data or
> DT match.
>
> Signed-off-by: Clemens Gruber <clemens.gruber-lZxf/j91sTNWk0Htik3J/w@public.gmane.org>
> ---
>  Documentation/hwmon/mcp3021 |   6 ++
>  drivers/hwmon/mcp3021.c     | 145 ++++++++++++++++++++++++++++++++------------
>  2 files changed, 111 insertions(+), 40 deletions(-)
>
> diff --git a/Documentation/hwmon/mcp3021 b/Documentation/hwmon/mcp3021
> index 74a6b72..be252b7 100644
> --- a/Documentation/hwmon/mcp3021
> +++ b/Documentation/hwmon/mcp3021
> @@ -12,6 +12,7 @@ Supported chips:
>  Authors:
>     Mingkai Hu
>     Sven Schuchmann <schuchmann-ogs+x9k7xBHcX9AoAvVr1bNAH6kLmebB@public.gmane.org>
> +   Clemens Gruber <clemens.gruber-lZxf/j91sTNWk0Htik3J/w@public.gmane.org>
>
>  Description
>  -----------
> @@ -27,3 +28,8 @@ Communication to the MCP3021/MCP3221  is performed using a 2-wire I2C
>  compatible interface. Standard (100 kHz) and Fast (400 kHz) I2C modes are
>  available. The default I2C device address is 0x4d (contact the Microchip
>  factory for additional address options).
> +
> +The reference voltage used in the conversion can be set through platform data
> +in millivolt (for backwards compatibility) or via device tree in microvolt.
> +Please refer to Documentation/devicetree/bindings/i2c/mcp3021.txt for details
> +about the device tree bindings.
> diff --git a/drivers/hwmon/mcp3021.c b/drivers/hwmon/mcp3021.c
> index 972444a..a8cf97f 100644
> --- a/drivers/hwmon/mcp3021.c
> +++ b/drivers/hwmon/mcp3021.c
> @@ -4,6 +4,7 @@
>   * Copyright (C) 2008-2009, 2012 Freescale Semiconductor, Inc.
>   * Author: Mingkai Hu <Mingkai.hu-KZfg59tc24xl57MIdRCFDg@public.gmane.org>
>   * Reworked by Sven Schuchmann <schuchmann-ogs+x9k7xBHcX9AoAvVr1bNAH6kLmebB@public.gmane.org>
> + * Copyright (C) 2016 Clemens Gruber <clemens.gruber-lZxf/j91sTNWk0Htik3J/w@public.gmane.org>
>   *
>   * This driver export the value of analog input voltage to sysfs, the
>   * voltage unit is mV. Through the sysfs interface, lm-sensors tool
> @@ -22,37 +23,56 @@
>  #include <linux/i2c.h>
>  #include <linux/err.h>
>  #include <linux/device.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
>
> -/* Vdd info */
> +/* Vdd / reference voltage in millivolt */
>  #define MCP3021_VDD_MAX		5500
>  #define MCP3021_VDD_MIN		2700
> -#define MCP3021_VDD_REF		3300
> -
> -/* output format */
> -#define MCP3021_SAR_SHIFT	2
> -#define MCP3021_SAR_MASK	0x3ff
> -#define MCP3021_OUTPUT_RES	10	/* 10-bit resolution */
> -
> -#define MCP3221_SAR_SHIFT	0
> -#define MCP3221_SAR_MASK	0xfff
> -#define MCP3221_OUTPUT_RES	12	/* 12-bit resolution */
> +#define MCP3021_VDD_DEFAULT	3300
>

There is no technical reason to drop / change those defines and use literals
in the code instead. Please don't.

>  enum chips {
>  	mcp3021,
>  	mcp3221
>  };
>
> +struct mcp3021_chip_info {
> +	u16 sar_shift;
> +	u16 sar_mask;
> +	u8 output_res;
> +};
> +
>  /*
>   * Client data (each client gets its own)
>   */
>  struct mcp3021_data {
>  	struct device *hwmon_dev;
> -	u32 vdd;	/* device power supply */
> -	u16 sar_shift;
> -	u16 sar_mask;
> -	u8 output_res;
> +	const struct mcp3021_chip_info *chip_info;
> +	u32 vdd; /* device power supply and reference voltage in millivolt */
>  };
>
> +static const struct mcp3021_chip_info mcp3021_chip_info_tbl[] = {
> +	[mcp3021] = {
> +		.sar_shift = 2,
> +		.sar_mask = 0x3ff,
> +		.output_res = 10,	/* 10-bit resolution */
> +	},
> +	[mcp3221] = {
> +		.sar_shift = 0,
> +		.sar_mask = 0xfff,
> +		.output_res = 12,	/* 12-bit resolution */
> +	},
> +};
> +
> +#ifdef CONFIG_OF
> +static const struct of_device_id of_mcp3021_match[] = {
> +	{ .compatible = "microchip,mcp3021", .data = (void *)mcp3021 },
> +	{ .compatible = "microchip,mcp3221", .data = (void *)mcp3221 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(of, of_mcp3021_match);
> +#endif
> +
>  static int mcp3021_read16(struct i2c_client *client)
>  {
>  	struct mcp3021_data *data = i2c_get_clientdata(client);
> @@ -73,14 +93,15 @@ static int mcp3021_read16(struct i2c_client *client)
>  	 * The ten-bit output code is composed of the lower 4-bit of the
>  	 * first byte and the upper 6-bit of the second byte.
>  	 */
> -	reg = (reg >> data->sar_shift) & data->sar_mask;
> +	reg = (reg >> data->chip_info->sar_shift) & data->chip_info->sar_mask;
>
>  	return reg;
>  }
>
>  static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
>  {
> -	return DIV_ROUND_CLOSEST(data->vdd * val, 1 << data->output_res);
> +	return DIV_ROUND_CLOSEST(data->vdd * val,
> +				 1 << data->chip_info->output_res);
>  }
>
>  static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
> @@ -101,44 +122,85 @@ static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
>
>  static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL);
>
> -static int mcp3021_probe(struct i2c_client *client,
> +#ifdef CONFIG_OF
> +static int mcp3021_probe_dt(struct i2c_client *client,
>  				const struct i2c_device_id *id)
>  {
> -	int err;
> -	struct mcp3021_data *data = NULL;
> +	struct mcp3021_data *data = i2c_get_clientdata(client);
> +	struct device_node *np = client->dev.of_node;
> +	const struct of_device_id *match;
> +	int devid, ret;
>
> -	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
> +	match = of_match_device(of_mcp3021_match, &client->dev);
> +	if (!match)
>  		return -ENODEV;
>
> -	data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
> -			    GFP_KERNEL);
> -	if (!data)
> -		return -ENOMEM;
> -
> -	i2c_set_clientdata(client, data);
> +	devid = (int)(uintptr_t)match->data;
> +	data->chip_info = &mcp3021_chip_info_tbl[devid];
>
> -	switch (id->driver_data) {
> -	case mcp3021:
> -		data->sar_shift = MCP3021_SAR_SHIFT;
> -		data->sar_mask = MCP3021_SAR_MASK;
> -		data->output_res = MCP3021_OUTPUT_RES;
> -		break;
> -
> -	case mcp3221:
> -		data->sar_shift = MCP3221_SAR_SHIFT;
> -		data->sar_mask = MCP3221_SAR_MASK;
> -		data->output_res = MCP3221_OUTPUT_RES;
> -		break;
> +	ret = of_property_read_u32(np, "reference-voltage-microvolt",
> +				   &data->vdd);
> +	if (ret) {
> +		/* fallback */
> +		data->vdd = MCP3021_VDD_DEFAULT;
> +		return 0;
>  	}
>
> +	/* Convert microvolt from DT to millivolt used in the formula */
> +	data->vdd /= 1000;
> +
> +	if (data->vdd > MCP3021_VDD_MAX || data->vdd < MCP3021_VDD_MIN)
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +#else
> +static int mcp3021_probe_dt(struct i2c_client *client,
> +				const struct i2c_device_id *id)
> +{
> +	return 1;
> +}
> +#endif
> +
> +static int mcp3021_probe_pdata(struct i2c_client *client,
> +				const struct i2c_device_id *id)
> +{
> +	struct mcp3021_data *data = i2c_get_clientdata(client);
> +
> +	data->chip_info = &mcp3021_chip_info_tbl[id->driver_data];
> +
>  	if (dev_get_platdata(&client->dev)) {
>  		data->vdd = *(u32 *)dev_get_platdata(&client->dev);
>  		if (data->vdd > MCP3021_VDD_MAX || data->vdd < MCP3021_VDD_MIN)
>  			return -EINVAL;
>  	} else {
> -		data->vdd = MCP3021_VDD_REF;
> +		data->vdd = MCP3021_VDD_DEFAULT;
>  	}
>
> +	return 0;
> +}
> +
> +static int mcp3021_probe(struct i2c_client *client,
> +				const struct i2c_device_id *id)
> +{
> +	struct mcp3021_data *data = NULL;
> +	int err;
> +
> +	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
> +		return -ENODEV;
> +
> +	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
> +	if (!data)
> +		return -ENOMEM;
> +
> +	i2c_set_clientdata(client, data);
> +
> +	err = mcp3021_probe_dt(client, id);
> +	if (err > 0)
> +		mcp3021_probe_pdata(client, id);
> +	else if (err < 0)
> +		return err;
> +
Most other drivers generate the platform data from devicetree data.

	if (of_match_device())
		pdata = probe_dt();
	else
		pdata = dev_get_platdata();

In this case, this is even simpler, since the only devicetree property is the reference
voltage.

	if (np) {
		if (of_property_read_u32(np, "reference-voltage-microvolt", &vdd))
			vdd = MCP3021_VDD_REF * 1000;
		vdd /= 1000;
		devid = (int)(uintptr_t)match->data;
	} else {
		u32 *pdata = dev_get_platdata(&client->dev);

		if (pdata)
			vdd = *pdata;
		else
			vdd = MCP3021_VDD_REF;
		devid = id->driver_data;
	}

You should actually be able to use id->driver_data directly in both cases since it is
always passed as parameter from the i2c core.

This would be much simpler than your suggested changes.

>  	err = sysfs_create_file(&client->dev.kobj, &dev_attr_in0_input.attr);
>  	if (err)
>  		return err;
> @@ -176,6 +238,9 @@ MODULE_DEVICE_TABLE(i2c, mcp3021_id);
>  static struct i2c_driver mcp3021_driver = {
>  	.driver = {
>  		.name = "mcp3021",
> +#ifdef CONFIG_OF
> +		.of_match_table = of_match_ptr(of_mcp3021_match),
> +#endif

of_match_ptr() is already protected with #ifdef CONFIG_OF and otherwise
returns NULL, so this ifdef is unnecessary.

>  	},
>  	.probe = mcp3021_probe,
>  	.remove = mcp3021_remove,
>

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [v2,2/3] hwmon: (mcp3021) add devicetree bindings documentation
@ 2016-11-19 16:28     ` Guenter Roeck
  0 siblings, 0 replies; 9+ messages in thread
From: Guenter Roeck @ 2016-11-19 16:28 UTC (permalink / raw
  To: Clemens Gruber
  Cc: linux-hwmon, Rob Herring, Jean Delvare, devicetree, linux-doc,
	linux-kernel

On Fri, Oct 28, 2016 at 12:33:44AM +0200, Clemens Gruber wrote:
> Document the devicetree bindings for the Microchip MCP3021/3221.
> 
> Signed-off-by: Clemens Gruber <clemens.gruber@pqgruber.com>
> Acked-by: Rob Herring <robh@kernel.org>

Applied to -next.

Guenter

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

* Re: [v2,2/3] hwmon: (mcp3021) add devicetree bindings documentation
@ 2016-11-19 16:28     ` Guenter Roeck
  0 siblings, 0 replies; 9+ messages in thread
From: Guenter Roeck @ 2016-11-19 16:28 UTC (permalink / raw
  To: Clemens Gruber
  Cc: linux-hwmon-u79uwXL29TY76Z2rM5mHXA, Rob Herring, Jean Delvare,
	devicetree-u79uwXL29TY76Z2rM5mHXA,
	linux-doc-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA

On Fri, Oct 28, 2016 at 12:33:44AM +0200, Clemens Gruber wrote:
> Document the devicetree bindings for the Microchip MCP3021/3221.
> 
> Signed-off-by: Clemens Gruber <clemens.gruber-lZxf/j91sTNWk0Htik3J/w@public.gmane.org>
> Acked-by: Rob Herring <robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>

Applied to -next.

Guenter
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

end of thread, other threads:[~2016-11-19 16:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-10-27 22:33 [PATCH v2 1/3] hwmon: (mcp3021) rework for DT support Clemens Gruber
2016-10-27 22:33 ` [PATCH v2 2/3] hwmon: (mcp3021) add devicetree bindings documentation Clemens Gruber
2016-10-27 22:33   ` Clemens Gruber
2016-10-31  6:04   ` Rob Herring
2016-11-19 16:28   ` [v2,2/3] " Guenter Roeck
2016-11-19 16:28     ` Guenter Roeck
2016-10-27 22:33 ` [PATCH v2 3/3] hwmon: (mcp3021) replace S_IRUGO with 0444 Clemens Gruber
2016-11-09 11:53 ` [PATCH v2 1/3] hwmon: (mcp3021) rework for DT support Guenter Roeck
2016-11-09 11:53   ` Guenter Roeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.