Support setting the reference voltage in 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.
Extend the device documentation and add new documentation for the
devicetree bindings.
Also change S_IRUGO to the better readable 0444, which fixes a
checkpatch warning.
Signed-off-by: Clemens Gruber
---
.../devicetree/bindings/hwmon/mcp3021.txt | 21 +++
Documentation/hwmon/mcp3021| 5 +
drivers/hwmon/mcp3021.c| 184 ++---
3 files changed, 149 insertions(+), 61 deletions(-)
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 000..e1d1e62
--- /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
+ Reference voltage in millivolt (mV)
+
+Example:
+
+mcp3021@4d {
+ compatible = "microchip,mcp3021";
+ reg = <0x4d>;
+
+ reference-voltage = <4500>; /* 4.5 V */
+};
diff --git a/Documentation/hwmon/mcp3021 b/Documentation/hwmon/mcp3021
index 74a6b72..55792c3 100644
--- a/Documentation/hwmon/mcp3021
+++ b/Documentation/hwmon/mcp3021
@@ -12,6 +12,7 @@ Supported chips:
Authors:
Mingkai Hu
Sven Schuchmann
+ Clemens Gruber
Description
---
@@ -27,3 +28,7 @@ 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 via platform data or
+device tree. Please refer to Documentation/devicetree/bindings/i2c/mcp3021.txt
+for information about the bindings if the device tree is used.
diff --git a/drivers/hwmon/mcp3021.c b/drivers/hwmon/mcp3021.c
index 972444a..ebbd3d2 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
* Reworked by Sven Schuchmann
+ * Copyright (C) 2016 Clemens Gruber
*
* This driver export the value of analog input voltage to sysfs, the
* voltage unit is mV. Through the sysfs interface, lm-sensors tool
@@ -22,35 +23,74 @@
#include
#include
#include
+#include
+#include
-/* Vdd info */
+/* Vdd / reference voltage */
#define MCP3021_VDD_MAX5500
#define MCP3021_VDD_MIN2700
-#define MCP3021_VDD_REF3300
+#define MCP3021_VDD_DEFAULT3300
-/* output format */
-#define MCP3021_SAR_SHIFT 2
-#define MCP3021_SAR_MASK 0x3ff
-#define MCP3021_OUTPUT_RES 10 /* 10-bit resolution */
+struct mcp3021_chip_info {
+ u16 sar_shift;
+ u16 sar_mask;
+ u8 output_res;
+};
+
+struct mcp3021_data {
+ struct device *hwmon_dev;
+ const struct mcp3021_chip_info *chip_info;
+ u32 vdd; /* Supply and reference voltage for AD conversion */
+};
-#define MCP3221_SAR_SHIFT 0
-#define MCP3221_SAR_MASK 0xfff
-#define MCP3221_OUTPUT_RES 12 /* 12-bit resolution */
+static int mcp3021_probe(struct i2c_client *client,
+ const struct i2c_device_id *id);
+static int mcp3021_remove(struct i2c_client *client);
-enum chips {
+enum {
mcp3021,
mcp3221
};
-/*
- * 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;
+static const struct i2c_device_id mcp3021_id[] = {
+ { "mcp3021", mcp3021 },
+ { "mcp3221", mcp3221 },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(i2c, mcp3021_id);
+
+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 },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, of_mcp3021_match);
+#endif
+
+static struct i2c_driver mcp3021_driver = {
+ .driver = {
+ .name = "mcp302