mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Inochi Amaoto <inochiama@outlook.com>
Cc: Chen Wang <unicorn_wang@outlook.com>,
	Jean Delvare <jdelvare@suse.com>, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Paul Walmsley <paul.walmsley@sifive.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>, Guo Ren <guoren@kernel.org>,
	Chao Wei <chao.wei@sophgo.com>,
	Hal Feng <hal.feng@starfivetech.com>,
	Jinyu Tang <tangjinyu@tinylab.org>,
	Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>,
	chunzhi.lin@sophgo.com, haijiao.liu@sophgo.com,
	linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-riscv@lists.infradead.org
Subject: Re: [PATCH v8 2/4] drivers: hwmon: sophgo: Add SG2042 external hardware monitor support
Date: Wed, 31 Jul 2024 08:02:36 -0700	[thread overview]
Message-ID: <75f6f910-43ff-4d98-b39f-b4b0629a56a1@roeck-us.net> (raw)
In-Reply-To: <IA1PR20MB49534944E268A0A71AA3D5D1BBB12@IA1PR20MB4953.namprd20.prod.outlook.com>

On Wed, Jul 31, 2024 at 03:17:57PM +0800, Inochi Amaoto wrote:
> On Wed, Jul 31, 2024 at 02:13:20PM GMT, Chen Wang wrote:
> > 
> > On 2024/7/30 15:50, Inochi Amaoto wrote:
> > [......]
> > > +#define REG_CRITICAL_ACTIONS			0x65
> > The name "REG_CRITICAL_ACTIONS" is ambiguous. I have confirmed with sophgo
> > engineers that the complete process is: when the measured temperature
> > exceeds the temperature set by REG_CRITICAL_TEMP, the processor is powered
> > off and shut down, and then after the temperature returns to the temperature
> > set by REG_REPOWER_TEMP, it is decided whether to power on again or remain
> > in the shutdown state based on the action set by REG_CRITICAL_ACTIONS,
> > whether it is reboot or poweroff.
> > 
> > So based on the above description, I think it would be better to
> > call "REG_CRITICAL_ACTIONS" as "REG_REPOWER_ACTIONS". "REG_CRITICAL_ACTIONS"
> > gives people the first impression that it is used to set actions related to
> > REG_CRITICAL_TEMP.
> > 
> > It is also recommended to add the above description of temperature control
> > and action settings in the code. Currently, sophgo does not have a clear
> > document description for this part, and adding it will help us understand
> > its functions.
> > 
> > Adding sophgo engineers Chunzhi and Haijiao, FYI.
> > 
> > > +#define REG_CRITICAL_TEMP			0x66
> > > +#define REG_REPOWER_TEMP			0x67
> > > +
> > > +#define CRITICAL_ACTION_REBOOT			1
> > > +#define CRITICAL_ACTION_POWEROFF		2
> > 
> > As I said upon, actions are not related to critical, but is for restoring
> > from critical, suggest to give a better name.
> > 
> > [......]
> > 
> > > +static ssize_t critical_action_show(struct device *dev,
> > [......]
> > > +static ssize_t critical_action_store(struct device *dev,
> > 
> > [......]
> > 
> > The same reason as upon, "critical_action_xxx" is misleading.
> > 
> > [......]
> > 
> 
> Thanks for explanation, I just get the name from the driver of SG2042.
> This is out of my knowledge.
> 
> > > +static int sg2042_mcu_read_temp(struct device *dev,
> > > +				u32 attr, int channel,
> > > +				long *val)
> > > +{
> > > +	struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
> > > +	int tmp;
> > > +	u8 reg;
> > > +
> > > +	switch (attr) {
> > > +	case hwmon_temp_input:
> > > +		reg = channel ? REG_BOARD_TEMP : REG_SOC_TEMP;
> > > +		break;
> > > +	case hwmon_temp_crit:
> > > +		reg = REG_CRITICAL_TEMP;
> > > +		break;
> > > +	case hwmon_temp_crit_hyst:
> > > +		reg = REG_REPOWER_TEMP;
> > > +		break;
> > > +	default:
> > > +		return -EOPNOTSUPP;
> > > +	}
> > > +
> > > +	tmp = i2c_smbus_read_byte_data(mcu->client, reg);
> > > +	if (tmp < 0)
> > > +		return tmp;
> > > +	*val = tmp * 1000;
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +static int sg2042_mcu_read(struct device *dev,
> > > +			   enum hwmon_sensor_types type,
> > > +			   u32 attr, int channel, long *val)
> > > +{
> > > +	return sg2042_mcu_read_temp(dev, attr, channel, val);
> > > +}
> > Can we merge sg2042_mcu_read and sg2042_mcu_read_temp?
> 
> Yes, it can be merged. but I think using this nested function 
> is more clear. And gcc can auto inline this function so we
> got no performance penalty.
> 

FWIW, I think that is pointless. Te only difference is unused
parameters.

> > > +
> > > +static int sg2042_mcu_write(struct device *dev,
> > > +			    enum hwmon_sensor_types type,
> > > +			    u32 attr, int channel, long val)
> > > +{
> > > +	struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
> > > +	int temp = val / 1000;
> > > +	int hyst_temp, crit_temp;
> > > +	int ret;
> > > +	u8 reg;
> > > +
> > > +	if (temp > MCU_POWER_MAX)
> > > +		temp = MCU_POWER_MAX;

No lower limit ? -1000000 is ok ?

> > > +
> > > +	mutex_lock(&mcu->mutex);
> > > +
> > > +	switch (attr) {
> > > +	case hwmon_temp_crit:
> > > +		hyst_temp = i2c_smbus_read_byte_data(mcu->client,
> > > +						     REG_REPOWER_TEMP);
> > > +		if (hyst_temp < 0) {
> > > +			ret = -ENODEV;
> > > +			goto failed;

Do not overwrite error codes.

> > > +		}
> > > +
> > > +		crit_temp = temp;
> > > +		reg = REG_CRITICAL_TEMP;
> > > +		break;
> > > +	case hwmon_temp_crit_hyst:
> > > +		crit_temp = i2c_smbus_read_byte_data(mcu->client,
> > > +						     REG_CRITICAL_TEMP);
> > > +		if (crit_temp < 0) {
> > > +			ret = -ENODEV;
> > > +			goto failed;

Do not overwrite error codes.

> > > +		}
> > > +
> > > +		hyst_temp = temp;
> > > +		reg = REG_REPOWER_TEMP;
> > > +		break;
> > > +	default:
> > > +		mutex_unlock(&mcu->mutex);
> > > +		return -EOPNOTSUPP;

This is inconsistent.

> > > +	}
> > > +
> > It is recommended to add some comments to explain why we need to ensure that
> > crit_temp is greater than or equal to hyst_temp. This is entirely because
> > the current MCU does not limit the input, which may cause user to set
> > incorrect crit_temp and hyst_temp.
> 
> Yeah, this is good idea.
> 
> > > +	if (crit_temp < hyst_temp) {
> > > +		ret = -EINVAL;
> > > +		goto failed;
> > > +	}
> > > +
> > > +	ret = i2c_smbus_write_byte_data(mcu->client, reg, temp);
> > > +
> > > +failed:
> > > +	mutex_unlock(&mcu->mutex);
> > > +	return ret;
> > > +}
> > > +
> > [......]

  reply	other threads:[~2024-07-31 15:02 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-30  7:49 [PATCH v8 0/4] riscv: " Inochi Amaoto
2024-07-30  7:50 ` [PATCH v8 1/4] dt-bindings: hwmon: Add Sophgo " Inochi Amaoto
2024-07-30  7:50 ` [PATCH v8 2/4] drivers: hwmon: sophgo: Add " Inochi Amaoto
2024-07-31  6:13   ` Chen Wang
2024-07-31  7:17     ` Inochi Amaoto
2024-07-31 15:02       ` Guenter Roeck [this message]
2024-07-31 22:48         ` Inochi Amaoto
2024-07-30  7:50 ` [PATCH v8 3/4] riscv: dts: sophgo: Add mcu device for Milk-V Pioneer Inochi Amaoto
2024-07-30  9:36   ` kernel test robot
2024-07-30  7:50 ` [PATCH v8 4/4] riscv: defconfig: Enable MCU support for SG2042 Inochi Amaoto
2024-07-31  6:17 ` [PATCH v8 0/4] riscv: sophgo: Add SG2042 external hardware monitor support Chen Wang

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=75f6f910-43ff-4d98-b39f-b4b0629a56a1@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=aou@eecs.berkeley.edu \
    --cc=chao.wei@sophgo.com \
    --cc=chunzhi.lin@sophgo.com \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=devicetree@vger.kernel.org \
    --cc=guoren@kernel.org \
    --cc=haijiao.liu@sophgo.com \
    --cc=hal.feng@starfivetech.com \
    --cc=inochiama@outlook.com \
    --cc=jdelvare@suse.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=prabhakar.mahadev-lad.rj@bp.renesas.com \
    --cc=robh@kernel.org \
    --cc=tangjinyu@tinylab.org \
    --cc=unicorn_wang@outlook.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®