Linux-Hwmon Archive mirror
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Naresh Solanki <naresh.solanki@9elements.com>
Cc: Jean Delvare <jdelvare@suse.com>,
	mazziesaccount@gmail.com,
	Patrick Rudolph <patrick.rudolph@9elements.com>,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [RESEND PATCH v2] hwmon: (pmbus/mp2975) Fix IRQ masking
Date: Thu, 7 Mar 2024 09:59:38 -0800	[thread overview]
Message-ID: <e680cc14-1068-4ae7-8400-5adcc9650c2f@roeck-us.net> (raw)
In-Reply-To: <20240305101608.2807612-1-naresh.solanki@9elements.com>

On Tue, Mar 05, 2024 at 03:46:07PM +0530, Naresh Solanki wrote:
> From: Patrick Rudolph <patrick.rudolph@9elements.com>
> 
> The MP2971/MP2973 use a custom 16bit register format for
> SMBALERT_MASK which doesn't follow the PMBUS specification.
> 
> Map the PMBUS defined bits used by the common code onto the custom
> format used by MPS and since the SMBALERT_MASK is currently never read
> by common code only implement the mapping for write transactions.
> 
> Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
> Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
> ---
> 
> Changes in V2:
> 1. Add/Update comment
> 2. Update SWAP define to include both variable.
> 3. Add defines for each bits of SMBALERT mask.
> ---
>  drivers/hwmon/pmbus/mp2975.c | 77 ++++++++++++++++++++++++++++++++++++
>  1 file changed, 77 insertions(+)
> 
> 
> base-commit: 90d35da658da8cff0d4ecbb5113f5fac9d00eb72
> 
> diff --git a/drivers/hwmon/pmbus/mp2975.c b/drivers/hwmon/pmbus/mp2975.c
> index e5fa10b3b8bc..766026204d88 100644
> --- a/drivers/hwmon/pmbus/mp2975.c
> +++ b/drivers/hwmon/pmbus/mp2975.c
> @@ -392,6 +392,82 @@ static int mp2973_read_word_data(struct i2c_client *client, int page,
>  	return ret;
>  }
>  
> +static int mp2973_write_word_data(struct i2c_client *client, int page,
> +				  int reg, u16 word)
> +{
> +	u8 target, mask;
> +	int ret;
> +
> +	if (reg != PMBUS_SMBALERT_MASK)
> +		return -ENODATA;
> +
> +	/*
> +	 * Vendor-specific SMBALERT_MASK register with 16 maskable bits.
> +	 */
> +	ret = pmbus_read_word_data(client, 0, 0, PMBUS_SMBALERT_MASK);
> +	if (ret < 0)
> +		return ret;
> +
> +	target = word & 0xff;
> +	mask = word >> 8;
> +
> +/*
> + * Set/Clear 'bit' in 'ret' based on condition followed by define for each bit in SMBALERT_MASK.
> + * Also bit 2 & 15 are reserved.
> + */
> +#define SWAP(cond, bit) (ret = (mask & cond) ? (ret & ~BIT(bit)) : (ret | BIT(bit)))
> +

Checkpatch:

CHECK: Macro argument 'cond' may be better as '(cond)' to avoid precedence issues

In addition to that, I really dislike side effect programming
as it is risky and difficult to understand. Please make that
something like

#define SWAP(val, mask, cond, bit) (((mask) & (cond)) ? ((val) & ~BIT(bit)) : ((val) | BIT(bit)))

and
	ret = SWAP(ret, mask, PB_CML_FAULT_INVALID_DATA, MP2973_INVALID_DATA);

Yes, I know, checkpatch will still complain about the re-use of bit,
but such is life.

Thanks,
Guenter

> +#define MP2973_TEMP_OT		0
> +#define MP2973_VIN_UVLO		1
> +#define MP2973_VIN_OVP		3
> +#define MP2973_MTP_FAULT	4
> +#define MP2973_OTHER_COMM	5
> +#define MP2973_MTP_BLK_TRIG	6
> +#define MP2973_PACKET_ERROR	7
> +#define MP2973_INVALID_DATA	8
> +#define MP2973_INVALID_COMMAND	9
> +#define MP2973_IOUT_OC_LV	10
> +#define MP2973_IOUT_OC		11
> +#define MP2973_VOUT_MAX_MIN_WARNING 12
> +#define MP2973_VOLTAGE_UV	13
> +#define MP2973_VOLTAGE_OV	14
> +
> +	switch (target) {
> +	case PMBUS_STATUS_CML:
> +		SWAP(PB_CML_FAULT_INVALID_DATA, MP2973_INVALID_DATA);
> +		SWAP(PB_CML_FAULT_INVALID_COMMAND,  MP2973_INVALID_COMMAND);
> +		SWAP(PB_CML_FAULT_OTHER_COMM, MP2973_OTHER_COMM);
> +		SWAP(PB_CML_FAULT_PACKET_ERROR, MP2973_PACKET_ERROR);
> +		break;
> +	case PMBUS_STATUS_VOUT:
> +		SWAP(PB_VOLTAGE_UV_FAULT, MP2973_VOLTAGE_UV);
> +		SWAP(PB_VOLTAGE_OV_FAULT, MP2973_VOLTAGE_OV);
> +		break;
> +	case PMBUS_STATUS_IOUT:
> +		SWAP(PB_IOUT_OC_FAULT, MP2973_IOUT_OC);
> +		SWAP(PB_IOUT_OC_LV_FAULT, MP2973_IOUT_OC_LV);
> +		break;
> +	case PMBUS_STATUS_TEMPERATURE:
> +		SWAP(PB_TEMP_OT_FAULT, MP2973_TEMP_OT);
> +		break;
> +	/*
> +	 * Map remaining bits to MFR specific to let the PMBUS core mask
> +	 * those bits by default.
> +	 */
> +	case PMBUS_STATUS_MFR_SPECIFIC:
> +		SWAP(BIT(1), MP2973_VIN_UVLO);
> +		SWAP(BIT(3), MP2973_VIN_OVP);
> +		SWAP(BIT(4), MP2973_MTP_FAULT);
> +		SWAP(BIT(6), MP2973_MTP_BLK_TRIG);
> +		break;
> +	default:
> +		return 0;
> +	}
> +#undef SWAP
> +
> +	return pmbus_write_word_data(client, 0, PMBUS_SMBALERT_MASK, ret);
> +}
> +
>  static int mp2975_read_word_data(struct i2c_client *client, int page,
>  				 int phase, int reg)
>  {
> @@ -907,6 +983,7 @@ static struct pmbus_driver_info mp2973_info = {
>  		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT |
>  		PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
>  	.read_word_data = mp2973_read_word_data,
> +	.write_word_data = mp2973_write_word_data,
>  #if IS_ENABLED(CONFIG_SENSORS_MP2975_REGULATOR)
>  	.num_regulators = 1,
>  	.reg_desc = mp2975_reg_desc,

      reply	other threads:[~2024-03-07 17:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-05 10:16 [RESEND PATCH v2] hwmon: (pmbus/mp2975) Fix IRQ masking Naresh Solanki
2024-03-07 17:59 ` Guenter Roeck [this message]

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=e680cc14-1068-4ae7-8400-5adcc9650c2f@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mazziesaccount@gmail.com \
    --cc=naresh.solanki@9elements.com \
    --cc=patrick.rudolph@9elements.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).