linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/4] hwmon: (lm75) Support configuring the sample time for various chips
@ 2019-09-03 14:03 Guenter Roeck
  2019-09-03 14:03 ` [PATCH 2/4] hwmon: (lm75) Move updating the sample interval to its own function Guenter Roeck
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Guenter Roeck @ 2019-09-03 14:03 UTC (permalink / raw)
  To: Hardware Monitoring; +Cc: Iker Perez del Palomar, Guenter Roeck

The conversion (sample) time is configurable for several chips supported
by the lm75 driver. With the necessary infrastructure in place, enable
this support for all chips using the configuration register for this
purpose.

DS1775:
	Conversion time:	187.5, 375, 750, 1500 ms
	Sensor resolution: 	9, 10, 11, 12 bit
DS75, STDS75:
	Conversion time:	150, 300, 600, 1200 ms
	Sensor resolution: 	9, 10, 11, 12 bit
DS7505:
	Conversion time:	25, 50, 100, 200 ms
	Sensor resolution: 	9, 10, 11, 12 bit
MCP980[0123]:
	Conversion time:	75, 150, 300, 600 ms
	Sensor resolution: 	9, 10, 11, 12 bit
TMP100, TMP101:
	Conversion time:	75, 150, 300, 600 ms
	Sensor resolution: 	9, 10, 11, 12 bit
TMP75, TMP105, TMP175, TMP275:
	Conversion time:	38, 75, 150, 300 ms
	Sensor resolution: 	9, 10, 11, 12 bit

While doing this, it became obvious that the masks and values to set
the converion (sample) time is similar for all those chips, and that
other chips with configurable sample times will need separate code anyway.
For that reason, replace the sample_set_masks and sample_clr_mask
configuration parameters with a single array and with a constant.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/lm75.c | 83 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 56 insertions(+), 27 deletions(-)

diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c
index f68ef9d451ab..6474b8f21981 100644
--- a/drivers/hwmon/lm75.c
+++ b/drivers/hwmon/lm75.c
@@ -73,10 +73,6 @@ enum lm75_type {		/* keep sorted in alphabetical order */
  * @sample_times:	All the possible sample times to be set. Mandatory if
  *			num_sample_times is larger than 1. If set, number of
  *			entries must match num_sample_times.
- * @sample_set_masks:	All the set_masks for the possible sample times.
- *			Mandatory if num_sample_times is larger than 1.
- *			If set, number of entries must match num_sample_times.
- * @sample_clr_mask:	Clear mask used to set the sample time.
  */
 
 struct lm75_params {
@@ -88,8 +84,6 @@ struct lm75_params {
 	unsigned int		default_sample_time;
 	u8			num_sample_times;
 	const unsigned int	*sample_times;
-	const u8		*sample_set_masks;
-	u8			sample_clr_mask;
 };
 
 /* Addresses scanned */
@@ -115,6 +109,11 @@ struct lm75_data {
 };
 
 /*-----------------------------------------------------------------------*/
+
+static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
+
+#define LM75_SAMPLE_CLEAR_MASK	(3 << 5)
+
 /* The structure below stores the configuration values of the supported devices.
  * In case of being supported multiple configurations, the default one must
  * always be the first element of the array
@@ -129,19 +128,28 @@ static const struct lm75_params device_params[] = {
 		.clr_mask = 3 << 5,
 		.set_mask = 2 << 5,	/* 11-bit mode */
 		.default_resolution = 11,
-		.default_sample_time = MSEC_PER_SEC,
+		.default_sample_time = 750,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 188, 375, 750, 1500 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
 	[ds75] = {
 		.clr_mask = 3 << 5,
 		.set_mask = 2 << 5,	/* 11-bit mode */
 		.default_resolution = 11,
-		.default_sample_time = MSEC_PER_SEC,
+		.default_sample_time = 600,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
 	[stds75] = {
 		.clr_mask = 3 << 5,
 		.set_mask = 2 << 5,	/* 11-bit mode */
 		.default_resolution = 11,
-		.default_sample_time = MSEC_PER_SEC,
+		.default_sample_time = 600,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
 	[stlm75] = {
 		.default_resolution = 9,
@@ -150,7 +158,10 @@ static const struct lm75_params device_params[] = {
 	[ds7505] = {
 		.set_mask = 3 << 5,	/* 12-bit mode*/
 		.default_resolution = 12,
-		.default_sample_time = MSEC_PER_SEC / 4,
+		.default_sample_time = 200,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 25, 50, 100, 200 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
 	[g751] = {
 		.default_resolution = 9,
@@ -194,56 +205,74 @@ static const struct lm75_params device_params[] = {
 		.clr_mask = 1 << 7,	/* not one-shot mode */
 		.default_resolution = 12,
 		.resolution_limits = 9,
-		.default_sample_time = MSEC_PER_SEC,
+		.default_sample_time = 240,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 75, 150, 300, 600 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
 	[tmp100] = {
 		.set_mask = 3 << 5,	/* 12-bit mode */
 		.clr_mask = 1 << 7,	/* not one-shot mode */
 		.default_resolution = 12,
-		.default_sample_time = MSEC_PER_SEC,
+		.default_sample_time = 320,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 75, 150, 300, 600 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
 	[tmp101] = {
 		.set_mask = 3 << 5,	/* 12-bit mode */
 		.clr_mask = 1 << 7,	/* not one-shot mode */
 		.default_resolution = 12,
-		.default_sample_time = MSEC_PER_SEC,
+		.default_sample_time = 320,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 75, 150, 300, 600 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
-	[tmp112] = {
+	[tmp105] = {
 		.set_mask = 3 << 5,	/* 12-bit mode */
-		.clr_mask = 1 << 7,	/* no one-shot mode*/
+		.clr_mask = 1 << 7,	/* not one-shot mode*/
 		.default_resolution = 12,
-		.default_sample_time = MSEC_PER_SEC / 4,
+		.default_sample_time = 220,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 38, 75, 150, 300 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
-	[tmp105] = {
+	[tmp112] = {
 		.set_mask = 3 << 5,	/* 12-bit mode */
-		.clr_mask = 1 << 7,	/* not one-shot mode*/
+		.clr_mask = 1 << 7,	/* no one-shot mode*/
 		.default_resolution = 12,
-		.default_sample_time = MSEC_PER_SEC / 2,
+		.default_sample_time = MSEC_PER_SEC / 4,
 	},
 	[tmp175] = {
 		.set_mask = 3 << 5,	/* 12-bit mode */
 		.clr_mask = 1 << 7,	/* not one-shot mode*/
 		.default_resolution = 12,
-		.default_sample_time = MSEC_PER_SEC / 2,
+		.default_sample_time = 220,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 38, 75, 150, 300 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
 	[tmp275] = {
 		.set_mask = 3 << 5,	/* 12-bit mode */
 		.clr_mask = 1 << 7,	/* not one-shot mode*/
 		.default_resolution = 12,
-		.default_sample_time = MSEC_PER_SEC / 2,
+		.default_sample_time = 220,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 38, 75, 150, 300 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
 	[tmp75] = {
 		.set_mask = 3 << 5,	/* 12-bit mode */
 		.clr_mask = 1 << 7,	/* not one-shot mode*/
 		.default_resolution = 12,
-		.default_sample_time = MSEC_PER_SEC / 2,
+		.default_sample_time = 220,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 38, 75, 150, 300 },
+		.resolutions = (u8 []) {9, 10, 11, 12 },
 	},
 	[tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */
 		.clr_mask = 1 << 7 | 3 << 5,
 		.default_resolution = 12,
-		.sample_set_masks = (u8 []){ 0 << 5, 1 << 5, 2 << 5,
-			3 << 5 },
-		.sample_clr_mask = 3 << 5,
 		.default_sample_time = MSEC_PER_SEC / 37,
 		.sample_times = (unsigned int []){ MSEC_PER_SEC / 37,
 			MSEC_PER_SEC / 18,
@@ -371,8 +400,8 @@ static int lm75_write_chip(struct device *dev, u32 attr, long val)
 				     (int)data->params->num_sample_times);
 
 		err = lm75_write_config(data,
-					data->params->sample_set_masks[index],
-					data->params->sample_clr_mask);
+					lm75_sample_set_masks[index],
+					LM75_SAMPLE_CLEAR_MASK);
 		if (err)
 			return err;
 		data->sample_time = data->params->sample_times[index];
-- 
2.7.4


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

* [PATCH 2/4] hwmon: (lm75) Move updating the sample interval to its own function
  2019-09-03 14:03 [PATCH 1/4] hwmon: (lm75) Support configuring the sample time for various chips Guenter Roeck
@ 2019-09-03 14:03 ` Guenter Roeck
  2019-09-03 14:03 ` [PATCH 3/4] hwmon: (lm75) Add support for writing conversion time for TMP112 Guenter Roeck
  2019-09-03 14:03 ` [PATCH 4/4] hwmon: (lm75) Add support for writing sampling period on PCT2075 Guenter Roeck
  2 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2019-09-03 14:03 UTC (permalink / raw)
  To: Hardware Monitoring; +Cc: Iker Perez del Palomar, Guenter Roeck

We'll need per-chip handling for updating the sample interval.
To prepare for it, separate the code implementing it into its own
function.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/lm75.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c
index 6474b8f21981..644da2620a26 100644
--- a/drivers/hwmon/lm75.c
+++ b/drivers/hwmon/lm75.c
@@ -388,27 +388,32 @@ static int lm75_write_temp(struct device *dev, u32 attr, long temp)
 	return regmap_write(data->regmap, reg, (u16)temp);
 }
 
-static int lm75_write_chip(struct device *dev, u32 attr, long val)
+static int lm75_update_interval(struct device *dev, long val)
 {
 	struct lm75_data *data = dev_get_drvdata(dev);
 	u8 index;
 	s32 err;
 
-	switch (attr) {
-	case hwmon_chip_update_interval:
-		index = find_closest(val, data->params->sample_times,
-				     (int)data->params->num_sample_times);
+	index = find_closest(val, data->params->sample_times,
+			     (int)data->params->num_sample_times);
 
-		err = lm75_write_config(data,
-					lm75_sample_set_masks[index],
-					LM75_SAMPLE_CLEAR_MASK);
-		if (err)
-			return err;
-		data->sample_time = data->params->sample_times[index];
+	err = lm75_write_config(data, lm75_sample_set_masks[index],
+				LM75_SAMPLE_CLEAR_MASK);
+	if (err)
+		return err;
 
-		if (data->params->resolutions)
-			data->resolution = data->params->resolutions[index];
-		break;
+	data->sample_time = data->params->sample_times[index];
+	if (data->params->resolutions)
+		data->resolution = data->params->resolutions[index];
+
+	return 0;
+}
+
+static int lm75_write_chip(struct device *dev, u32 attr, long val)
+{
+	switch (attr) {
+	case hwmon_chip_update_interval:
+		return lm75_update_interval(dev, val);
 	default:
 		return -EINVAL;
 	}
-- 
2.7.4


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

* [PATCH 3/4] hwmon: (lm75) Add support for writing conversion time for TMP112
  2019-09-03 14:03 [PATCH 1/4] hwmon: (lm75) Support configuring the sample time for various chips Guenter Roeck
  2019-09-03 14:03 ` [PATCH 2/4] hwmon: (lm75) Move updating the sample interval to its own function Guenter Roeck
@ 2019-09-03 14:03 ` Guenter Roeck
  2019-09-03 14:03 ` [PATCH 4/4] hwmon: (lm75) Add support for writing sampling period on PCT2075 Guenter Roeck
  2 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2019-09-03 14:03 UTC (permalink / raw)
  To: Hardware Monitoring; +Cc: Iker Perez del Palomar, Guenter Roeck

TMP112 uses an uncommon method to write the conversion time: its
configuration register is 16 bit wide, and the conversion time is
configured in its second byte.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/lm75.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c
index 644da2620a26..ce0f6c671b8f 100644
--- a/drivers/hwmon/lm75.c
+++ b/drivers/hwmon/lm75.c
@@ -242,6 +242,8 @@ static const struct lm75_params device_params[] = {
 		.clr_mask = 1 << 7,	/* no one-shot mode*/
 		.default_resolution = 12,
 		.default_sample_time = MSEC_PER_SEC / 4,
+		.num_sample_times = 4,
+		.sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
 	},
 	[tmp175] = {
 		.set_mask = 3 << 5,	/* 12-bit mode */
@@ -391,21 +393,36 @@ static int lm75_write_temp(struct device *dev, u32 attr, long temp)
 static int lm75_update_interval(struct device *dev, long val)
 {
 	struct lm75_data *data = dev_get_drvdata(dev);
+	unsigned int reg;
 	u8 index;
 	s32 err;
 
 	index = find_closest(val, data->params->sample_times,
 			     (int)data->params->num_sample_times);
 
-	err = lm75_write_config(data, lm75_sample_set_masks[index],
-				LM75_SAMPLE_CLEAR_MASK);
-	if (err)
-		return err;
-
-	data->sample_time = data->params->sample_times[index];
-	if (data->params->resolutions)
-		data->resolution = data->params->resolutions[index];
+	switch (data->kind) {
+	default:
+		err = lm75_write_config(data, lm75_sample_set_masks[index],
+					LM75_SAMPLE_CLEAR_MASK);
+		if (err)
+			return err;
 
+		data->sample_time = data->params->sample_times[index];
+		if (data->params->resolutions)
+			data->resolution = data->params->resolutions[index];
+		break;
+	case tmp112:
+		err = regmap_read(data->regmap, LM75_REG_CONF, &reg);
+		if (err < 0)
+			return err;
+		reg &= ~0x00c0;
+		reg |= (3 - index) << 6;
+		err = regmap_write(data->regmap, LM75_REG_CONF, reg);
+		if (err < 0)
+			return err;
+		data->sample_time = data->params->sample_times[index];
+		break;
+	}
 	return 0;
 }
 
@@ -489,7 +506,7 @@ static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
 
 static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
 {
-	return reg == LM75_REG_TEMP;
+	return reg == LM75_REG_TEMP || reg == LM75_REG_CONF;
 }
 
 static const struct regmap_config lm75_regmap_config = {
-- 
2.7.4


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

* [PATCH 4/4] hwmon: (lm75) Add support for writing sampling period on PCT2075
  2019-09-03 14:03 [PATCH 1/4] hwmon: (lm75) Support configuring the sample time for various chips Guenter Roeck
  2019-09-03 14:03 ` [PATCH 2/4] hwmon: (lm75) Move updating the sample interval to its own function Guenter Roeck
  2019-09-03 14:03 ` [PATCH 3/4] hwmon: (lm75) Add support for writing conversion time for TMP112 Guenter Roeck
@ 2019-09-03 14:03 ` Guenter Roeck
  2 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2019-09-03 14:03 UTC (permalink / raw)
  To: Hardware Monitoring; +Cc: Iker Perez del Palomar, Guenter Roeck

For PCT7027, the sampling period is configured using a dedicated
register.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/lm75.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c
index ce0f6c671b8f..d45e5cc4adf5 100644
--- a/drivers/hwmon/lm75.c
+++ b/drivers/hwmon/lm75.c
@@ -95,6 +95,7 @@ static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
 #define LM75_REG_CONF		0x01
 #define LM75_REG_HYST		0x02
 #define LM75_REG_MAX		0x03
+#define PCT2075_REG_IDLE	0x04
 
 /* Each client has this additional data */
 struct lm75_data {
@@ -199,6 +200,11 @@ static const struct lm75_params device_params[] = {
 	[pct2075] = {
 		.default_resolution = 11,
 		.default_sample_time = MSEC_PER_SEC / 10,
+		.num_sample_times = 31,
+		.sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600,
+		700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
+		1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700,
+		2800, 2900, 3000, 3100 },
 	},
 	[mcp980x] = {
 		.set_mask = 3 << 5,	/* 12-bit mode */
@@ -422,6 +428,13 @@ static int lm75_update_interval(struct device *dev, long val)
 			return err;
 		data->sample_time = data->params->sample_times[index];
 		break;
+	case pct2075:
+		err = i2c_smbus_write_byte_data(data->client, PCT2075_REG_IDLE,
+						index + 1);
+		if (err)
+			return err;
+		data->sample_time = data->params->sample_times[index];
+		break;
 	}
 	return 0;
 }
@@ -512,7 +525,7 @@ static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
 static const struct regmap_config lm75_regmap_config = {
 	.reg_bits = 8,
 	.val_bits = 16,
-	.max_register = LM75_REG_MAX,
+	.max_register = PCT2075_REG_IDLE,
 	.writeable_reg = lm75_is_writeable_reg,
 	.volatile_reg = lm75_is_volatile_reg,
 	.val_format_endian = REGMAP_ENDIAN_BIG,
-- 
2.7.4


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

end of thread, other threads:[~2019-09-03 14:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-03 14:03 [PATCH 1/4] hwmon: (lm75) Support configuring the sample time for various chips Guenter Roeck
2019-09-03 14:03 ` [PATCH 2/4] hwmon: (lm75) Move updating the sample interval to its own function Guenter Roeck
2019-09-03 14:03 ` [PATCH 3/4] hwmon: (lm75) Add support for writing conversion time for TMP112 Guenter Roeck
2019-09-03 14:03 ` [PATCH 4/4] hwmon: (lm75) Add support for writing sampling period on PCT2075 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).