Re: [PATCH 4/4] rtc: pcf2127: support battery low voltage detection function

Stefan Kerkmann <[email protected]>
Newsgroups org.kernel.vger.linux-rtc,org.kernel.vger.linux-kernel
Message-ID <[email protected]>

On 3/11/26 21:02, Hugo Villeneuve wrote:
> From: Hugo Villeneuve <[email protected]>
> 
> Add support for parameter RTC_PARAM_BATTERY_LOW_DETECT in RTC_PARAM_SET
> ioctl to enable/disable/query battery low voltage detection. This is
> especially relevant on the pcf2131 where this function is disabled by
> default, contrary to the pcf2127.
> 
> Signed-off-by: Hugo Villeneuve <[email protected]>
> ---
>   drivers/rtc/rtc-pcf2127.c | 60 ++++++++++++++++++++++++++++++++-------
>   1 file changed, 50 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
> index 0605295026564..05b08867ffcb3 100644
> --- a/drivers/rtc/rtc-pcf2127.c
> +++ b/drivers/rtc/rtc-pcf2127.c
> @@ -364,12 +364,12 @@ static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
>   	u8 value;
>   	int ret;
>   
> +	ret = pcf2127_pwrmng_get(dev, &value);
> +	if (ret < 0)
> +		return ret;
> +
>   	switch (param->param) {
>   	case RTC_PARAM_BACKUP_SWITCH_MODE:
> -		ret = pcf2127_pwrmng_get(dev, &value);
> -		if (ret < 0)
> -			return ret;
> -
>   		if (value < 0x3)
>   			param->uvalue = RTC_BSM_LEVEL;
>   		else if (value < 0x6)
> @@ -379,6 +379,14 @@ static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
>   
>   		break;
>   
> +	case RTC_PARAM_BATTERY_LOW_DETECT:
> +		if (value == 0x0 || value == 0x3)
> +			param->uvalue = RTC_BATTERY_LOW_DETECT_ENABLED;
> +		else
> +			param->uvalue = RTC_BATTERY_LOW_DETECT_DISABLED;
> +
> +		break;
> +
>   	default:
>   		return -EINVAL;
>   	}
> @@ -392,12 +400,12 @@ static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
>   	u8 value;
>   	int ret;
>   
> +	ret = pcf2127_pwrmng_get(dev, &value);
> +	if (ret < 0)
> +		return ret;
> +
>   	switch (param->param) {
>   	case RTC_PARAM_BACKUP_SWITCH_MODE:
> -		ret = pcf2127_pwrmng_get(dev, &value);
> -		if (ret < 0)
> -			return ret;
> -
>   		if (value > 5)
>   			value -= 5;
>   		else if (value > 2)
> @@ -418,13 +426,45 @@ static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
>   			return -EINVAL;
>   		}
>   
> -		return pcf2127_pwrmng_set(dev, mode + value);
> +		break;
> +
> +	case RTC_PARAM_BATTERY_LOW_DETECT:
> +		if (value > 5) {
> +			value -= 5;
> +			mode = 5;
> +		} else if (value > 2) {
> +			value -= 3;
> +			mode = 3;
> +		}
> +
> +		switch (param->uvalue) {
> +		case RTC_BATTERY_LOW_DETECT_DISABLED:
> +			if (mode != 5)
> +				if (value == 0)
> +					value = 1;
> +
> +			break;
> +		case RTC_BATTERY_LOW_DETECT_ENABLED:
> +			if (mode != 5)
> +				value = 0; /* Enable battery low detection. */
> +			else
> +				return -EINVAL; /*
> +						 * battery low detection can't be enabled if
> +						 * battery switch over is disabled.
> +						 */
> +			break;
> +
> +		default:
> +			return -EINVAL;
> +		}
> +
> +		break;
>   
>   	default:
>   		return -EINVAL;
>   	}
>   
> -	return 0;
> +	return pcf2127_pwrmng_set(dev, mode + value);
>   }
>   
>   static int pcf2127_rtc_ioctl(struct device *dev,

Works as expected, thank you!

Reviewed-by: Stefan Kerkmann <[email protected]>
Tested-by: Stefan Kerkmann <[email protected]>

-- 
Pengutronix e.K.                       | Stefan Kerkmann             |
Steuerwalder Str. 21                   | https://www.pengutronix.de/ |
31137 Hildesheim, Germany              | Phone: +49-5121-206917-128  |
Amtsgericht Hildesheim, HRA 2686       | Fax:   +49-5121-206917-9    |
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.