All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cosmin Tanislav <demonsingur@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: cosmin.tanislav@analog.com, demonsingur@gmail.com,
	Jean Delvare <jdelvare@suse.com>,
	Guenter Roeck <linux@roeck-us.net>,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v1 06/10] hwmon: adt7x10: extract read/write methods from show/store
Date: Tue, 21 Dec 2021 14:39:40 +0200	[thread overview]
Message-ID: <20211221123944.2683245-6-demonsingur@gmail.com> (raw)
In-Reply-To: <20211221123944.2683245-1-demonsingur@gmail.com>

From: Cosmin Tanislav <cosmin.tanislav@analog.com>

These will later be used by hwmon read and write callbacks.

Signed-off-by: Cosmin Tanislav <cosmin.tanislav@analog.com>
---
 drivers/hwmon/adt7x10.c | 128 +++++++++++++++++++++++++++++-----------
 1 file changed, 95 insertions(+), 33 deletions(-)

diff --git a/drivers/hwmon/adt7x10.c b/drivers/hwmon/adt7x10.c
index ec7e526b3441..32ea9438ad64 100644
--- a/drivers/hwmon/adt7x10.c
+++ b/drivers/hwmon/adt7x10.c
@@ -219,14 +219,11 @@ static int ADT7X10_REG_TO_TEMP(struct adt7x10_data *data, s16 reg)
 
 /* sysfs attributes for hwmon */
 
-static ssize_t adt7x10_temp_show(struct device *dev,
-				 struct device_attribute *da, char *buf)
+static int adt7x10_temp_read(struct adt7x10_data *data, int index, long *val)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
-	struct adt7x10_data *data = dev_get_drvdata(dev);
-
+	struct device *dev = data->bus_dev;
 
-	if (attr->index == 0) {
+	if (index == 0) {
 		int ret;
 
 		ret = adt7x10_update_temp(dev);
@@ -234,39 +231,62 @@ static ssize_t adt7x10_temp_show(struct device *dev,
 			return ret;
 	}
 
-	return sprintf(buf, "%d\n", ADT7X10_REG_TO_TEMP(data,
-		       data->temp[attr->index]));
+	*val = ADT7X10_REG_TO_TEMP(data, data->temp[index]);
+
+	return 0;
 }
 
-static ssize_t adt7x10_temp_store(struct device *dev,
-				  struct device_attribute *da,
-				  const char *buf, size_t count)
+static ssize_t adt7x10_temp_show(struct device *dev,
+				 struct device_attribute *da, char *buf)
 {
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
 	struct adt7x10_data *data = dev_get_drvdata(dev);
-	int nr = attr->index;
-	long temp;
+	long val;
 	int ret;
 
-	ret = kstrtol(buf, 10, &temp);
+	ret = adt7x10_temp_read(data, attr->index, &val);
 	if (ret)
 		return ret;
 
+	return sprintf(buf, "%ld\n", val);
+}
+
+static int adt7x10_temp_write(struct adt7x10_data *data, unsigned int nr,
+			      long temp)
+{
+	struct device *dev = data->bus_dev;
+	int ret;
+
 	mutex_lock(&data->update_lock);
 	data->temp[nr] = ADT7X10_TEMP_TO_REG(temp);
 	ret = adt7x10_write_word(dev, ADT7X10_REG_TEMP[nr], data->temp[nr]);
-	if (ret)
-		count = ret;
 	mutex_unlock(&data->update_lock);
-	return count;
+
+	return ret;
 }
 
-static ssize_t adt7x10_t_hyst_show(struct device *dev,
-				   struct device_attribute *da, char *buf)
+static ssize_t adt7x10_temp_store(struct device *dev,
+				  struct device_attribute *da,
+				  const char *buf, size_t count)
 {
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
 	struct adt7x10_data *data = dev_get_drvdata(dev);
 	int nr = attr->index;
+	long temp;
+	int ret;
+
+	ret = kstrtol(buf, 10, &temp);
+	if (ret)
+		return ret;
+
+	ret = adt7x10_temp_write(data, nr, temp);
+
+	return ret ?: count;
+}
+
+static int adt7x10_hyst_read(struct adt7x10_data *data, unsigned int nr,
+			     long *val)
+{
 	int hyst;
 
 	hyst = (data->hyst & ADT7X10_T_HYST_MASK) * 1000;
@@ -277,44 +297,86 @@ static ssize_t adt7x10_t_hyst_show(struct device *dev,
 	 */
 	if (nr == 2)	/* min has positive offset, others have negative */
 		hyst = -hyst;
-	return sprintf(buf, "%d\n",
-		       ADT7X10_REG_TO_TEMP(data, data->temp[nr]) - hyst);
+
+	*val = ADT7X10_REG_TO_TEMP(data, data->temp[nr]) - hyst;
+
+	return 0;
 }
 
-static ssize_t adt7x10_t_hyst_store(struct device *dev,
-				    struct device_attribute *da,
-				    const char *buf, size_t count)
+static ssize_t adt7x10_t_hyst_show(struct device *dev,
+				   struct device_attribute *da, char *buf)
 {
+	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
 	struct adt7x10_data *data = dev_get_drvdata(dev);
-	int limit, ret;
-	long hyst;
+	int nr = attr->index;
+	long val;
+	int ret;
 
-	ret = kstrtol(buf, 10, &hyst);
+	ret = adt7x10_hyst_read(data, nr, &val);
 	if (ret)
 		return ret;
+
+	return sprintf(buf, "%ld\n", val);
+}
+
+static int adt7x10_hyst_write(struct adt7x10_data *data, long hyst)
+{
+	struct device *dev = data->bus_dev;
+	int limit;
+
 	/* convert absolute hysteresis value to a 4 bit delta value */
 	limit = ADT7X10_REG_TO_TEMP(data, data->temp[1]);
 	hyst = clamp_val(hyst, ADT7X10_TEMP_MIN, ADT7X10_TEMP_MAX);
 	data->hyst = clamp_val(DIV_ROUND_CLOSEST(limit - hyst, 1000),
-				   0, ADT7X10_T_HYST_MASK);
-	ret = adt7x10_write_byte(dev, ADT7X10_T_HYST, data->hyst);
+			       0, ADT7X10_T_HYST_MASK);
+	return adt7x10_write_byte(dev, ADT7X10_T_HYST, data->hyst);
+}
+
+static ssize_t adt7x10_t_hyst_store(struct device *dev,
+				    struct device_attribute *da,
+				    const char *buf, size_t count)
+{
+	struct adt7x10_data *data = dev_get_drvdata(dev);
+	int ret;
+	long hyst;
+
+	ret = kstrtol(buf, 10, &hyst);
 	if (ret)
 		return ret;
 
-	return count;
+	ret = adt7x10_hyst_write(data, hyst);
+
+	return ret ?: count;
+}
+
+static int adt7x10_alarm_read(struct adt7x10_data *data, unsigned int index,
+			      long *val)
+{
+	struct device *dev = data->bus_dev;
+	int ret;
+
+	ret = adt7x10_read_byte(dev, ADT7X10_STATUS);
+	if (ret < 0)
+		return ret;
+
+	*val = !!(ret & index);
+
+	return 0;
 }
 
 static ssize_t adt7x10_alarm_show(struct device *dev,
 				  struct device_attribute *da, char *buf)
 {
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
+	struct adt7x10_data *data = dev_get_drvdata(dev);
+	long val;
 	int ret;
 
-	ret = adt7x10_read_byte(dev, ADT7X10_STATUS);
-	if (ret < 0)
+	ret = adt7x10_alarm_read(data, attr->index, &val);
+	if (ret)
 		return ret;
 
-	return sprintf(buf, "%d\n", !!(ret & attr->index));
+	return sprintf(buf, "%ld\n", val);
 }
 
 static SENSOR_DEVICE_ATTR_RO(temp1_input, adt7x10_temp, 0);
-- 
2.34.1


  parent reply	other threads:[~2021-12-21 12:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-21 12:39 [PATCH v1 01/10] hwmon: adt7x10: store bus_dev in private data Cosmin Tanislav
2021-12-21 12:39 ` [PATCH v1 02/10] hwmon: adt7x10: do not create name attr Cosmin Tanislav
2021-12-21 15:17   ` Guenter Roeck
2021-12-21 12:39 ` [PATCH v1 03/10] hwmon: adt7x10: use devm_request_threaded_irq Cosmin Tanislav
2021-12-21 16:08   ` Guenter Roeck
2021-12-21 12:39 ` [PATCH v1 04/10] hwmon: adt7x10: avoid storing hwinfo dev into private data Cosmin Tanislav
2021-12-21 15:16   ` Guenter Roeck
2021-12-21 12:39 ` [PATCH v1 05/10] hwmon: adt7x10: pass hwinfo dev to irq handler Cosmin Tanislav
2021-12-21 12:39 ` Cosmin Tanislav [this message]
2021-12-21 12:39 ` [PATCH v1 07/10] hwmon: adt7x10: remove attrs Cosmin Tanislav
2021-12-21 15:23   ` Guenter Roeck
2021-12-21 12:39 ` [PATCH v1 08/10] hwmon: adt7x10: use hwmon_device_register_with_info Cosmin Tanislav
2021-12-21 12:39 ` [PATCH v1 09/10] hwmon: adt7x10: use devm_hwmon_device_register_with_info Cosmin Tanislav
2021-12-21 15:34   ` Guenter Roeck
2021-12-21 12:39 ` [PATCH v1 10/10] hwmon: adt7x10: use hwmon_notify_event Cosmin Tanislav

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211221123944.2683245-6-demonsingur@gmail.com \
    --to=demonsingur@gmail.com \
    --cc=cosmin.tanislav@analog.com \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.