Skip to content

Commit 0015503

Browse files
fancergroeck
authored andcommitted
hwmon: (bt1-pvt) Cache current update timeout
Instead of converting the update timeout data to the milliseconds each time on the read procedure let's preserve the currently set timeout in the dedicated driver private data cache. The cached value will be then used in the timeout read method and in the alarm-less data conversion to prevent the caller task hanging up in case if the PVT sensor is suddenly powered down. Fixes: 87976ce ("hwmon: Add Baikal-T1 PVT sensor driver") Signed-off-by: Serge Semin <Sergey.Semin@baikalelectronics.ru> Link: https://lore.kernel.org/r/20200920110924.19741-3-Sergey.Semin@baikalelectronics.ru Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent a6db156 commit 0015503

2 files changed

Lines changed: 49 additions & 39 deletions

File tree

‎drivers/hwmon/bt1-pvt.c‎

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -655,52 +655,24 @@ static int pvt_write_trim(struct pvt_hwmon *pvt, long val)
655655

656656
static int pvt_read_timeout(struct pvt_hwmon *pvt, long *val)
657657
{
658-
unsigned long rate;
659-
ktime_t kt;
660-
u32 data;
661-
662-
rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
663-
if (!rate)
664-
return -ENODEV;
665-
666-
/*
667-
* Don't bother with mutex here, since we just read data from MMIO.
668-
* We also have to scale the ticks timeout up to compensate the
669-
* ms-ns-data translations.
670-
*/
671-
data = readl(pvt->regs + PVT_TTIMEOUT) + 1;
658+
int ret;
672659

673-
/*
674-
* Calculate ref-clock based delay (Ttotal) between two consecutive
675-
* data samples of the same sensor. So we first must calculate the
676-
* delay introduced by the internal ref-clock timer (Tref * Fclk).
677-
* Then add the constant timeout cuased by each conversion latency
678-
* (Tmin). The basic formulae for each conversion is following:
679-
* Ttotal = Tref * Fclk + Tmin
680-
* Note if alarms are enabled the sensors are polled one after
681-
* another, so in order to have the delay being applicable for each
682-
* sensor the requested value must be equally redistirbuted.
683-
*/
684-
#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
685-
kt = ktime_set(PVT_SENSORS_NUM * (u64)data, 0);
686-
kt = ktime_divns(kt, rate);
687-
kt = ktime_add_ns(kt, PVT_SENSORS_NUM * PVT_TOUT_MIN);
688-
#else
689-
kt = ktime_set(data, 0);
690-
kt = ktime_divns(kt, rate);
691-
kt = ktime_add_ns(kt, PVT_TOUT_MIN);
692-
#endif
660+
ret = mutex_lock_interruptible(&pvt->iface_mtx);
661+
if (ret)
662+
return ret;
693663

694664
/* Return the result in msec as hwmon sysfs interface requires. */
695-
*val = ktime_to_ms(kt);
665+
*val = ktime_to_ms(pvt->timeout);
666+
667+
mutex_unlock(&pvt->iface_mtx);
696668

697669
return 0;
698670
}
699671

700672
static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
701673
{
702674
unsigned long rate;
703-
ktime_t kt;
675+
ktime_t kt, cache;
704676
u32 data;
705677
int ret;
706678

@@ -713,7 +685,7 @@ static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
713685
* between all available sensors to have the requested delay
714686
* applicable to each individual sensor.
715687
*/
716-
kt = ms_to_ktime(val);
688+
cache = kt = ms_to_ktime(val);
717689
#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
718690
kt = ktime_divns(kt, PVT_SENSORS_NUM);
719691
#endif
@@ -742,6 +714,7 @@ static int pvt_write_timeout(struct pvt_hwmon *pvt, long val)
742714
return ret;
743715

744716
pvt_set_tout(pvt, data);
717+
pvt->timeout = cache;
745718

746719
mutex_unlock(&pvt->iface_mtx);
747720

@@ -1018,10 +991,17 @@ static int pvt_check_pwr(struct pvt_hwmon *pvt)
1018991
return ret;
1019992
}
1020993

1021-
static void pvt_init_iface(struct pvt_hwmon *pvt)
994+
static int pvt_init_iface(struct pvt_hwmon *pvt)
1022995
{
996+
unsigned long rate;
1023997
u32 trim, temp;
1024998

999+
rate = clk_get_rate(pvt->clks[PVT_CLOCK_REF].clk);
1000+
if (!rate) {
1001+
dev_err(pvt->dev, "Invalid reference clock rate\n");
1002+
return -ENODEV;
1003+
}
1004+
10251005
/*
10261006
* Make sure all interrupts and controller are disabled so not to
10271007
* accidentally have ISR executed before the driver data is fully
@@ -1036,12 +1016,37 @@ static void pvt_init_iface(struct pvt_hwmon *pvt)
10361016
pvt_set_mode(pvt, pvt_info[pvt->sensor].mode);
10371017
pvt_set_tout(pvt, PVT_TOUT_DEF);
10381018

1019+
/*
1020+
* Preserve the current ref-clock based delay (Ttotal) between the
1021+
* sensors data samples in the driver data so not to recalculate it
1022+
* each time on the data requests and timeout reads. It consists of the
1023+
* delay introduced by the internal ref-clock timer (N / Fclk) and the
1024+
* constant timeout caused by each conversion latency (Tmin):
1025+
* Ttotal = N / Fclk + Tmin
1026+
* If alarms are enabled the sensors are polled one after another and
1027+
* in order to get the next measurement of a particular sensor the
1028+
* caller will have to wait for at most until all the others are
1029+
* polled. In that case the formulae will look a bit different:
1030+
* Ttotal = 5 * (N / Fclk + Tmin)
1031+
*/
1032+
#if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
1033+
pvt->timeout = ktime_set(PVT_SENSORS_NUM * PVT_TOUT_DEF, 0);
1034+
pvt->timeout = ktime_divns(pvt->timeout, rate);
1035+
pvt->timeout = ktime_add_ns(pvt->timeout, PVT_SENSORS_NUM * PVT_TOUT_MIN);
1036+
#else
1037+
pvt->timeout = ktime_set(PVT_TOUT_DEF, 0);
1038+
pvt->timeout = ktime_divns(pvt->timeout, rate);
1039+
pvt->timeout = ktime_add_ns(pvt->timeout, PVT_TOUT_MIN);
1040+
#endif
1041+
10391042
trim = PVT_TRIM_DEF;
10401043
if (!of_property_read_u32(pvt->dev->of_node,
10411044
"baikal,pvt-temp-offset-millicelsius", &temp))
10421045
trim = pvt_calc_trim(temp);
10431046

10441047
pvt_set_trim(pvt, trim);
1048+
1049+
return 0;
10451050
}
10461051

10471052
static int pvt_request_irq(struct pvt_hwmon *pvt)
@@ -1149,7 +1154,9 @@ static int pvt_probe(struct platform_device *pdev)
11491154
if (ret)
11501155
return ret;
11511156

1152-
pvt_init_iface(pvt);
1157+
ret = pvt_init_iface(pvt);
1158+
if (ret)
1159+
return ret;
11531160

11541161
ret = pvt_request_irq(pvt);
11551162
if (ret)

‎drivers/hwmon/bt1-pvt.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/completion.h>
1111
#include <linux/hwmon.h>
1212
#include <linux/kernel.h>
13+
#include <linux/ktime.h>
1314
#include <linux/mutex.h>
1415
#include <linux/seqlock.h>
1516

@@ -201,6 +202,7 @@ struct pvt_cache {
201202
* if alarms are disabled).
202203
* @sensor: current PVT sensor the data conversion is being performed for.
203204
* @cache: data cache descriptor.
205+
* @timeout: conversion timeout cache.
204206
*/
205207
struct pvt_hwmon {
206208
struct device *dev;
@@ -214,6 +216,7 @@ struct pvt_hwmon {
214216
struct mutex iface_mtx;
215217
enum pvt_sensor_type sensor;
216218
struct pvt_cache cache[PVT_SENSORS_NUM];
219+
ktime_t timeout;
217220
};
218221

219222
/*

0 commit comments

Comments
 (0)