thermal/core: Add a generic thermal_zone_set_trip() function

The thermal zone ops defines a set_trip callback where we can invoke
the backend driver to set an interrupt for the next trip point
temperature being crossed the way up or down, or setting the low level
with the hysteresis.

The ops is only called from the thermal sysfs code where the userspace
has the ability to modify a trip point characteristic.

With the effort of encapsulating the thermal framework core code,
let's create a thermal_zone_set_trip() which is the writable side of
the thermal_zone_get_trip() and put there all the ops encapsulation.

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Link: https://lore.kernel.org/r/20221003092602.1323944-4-daniel.lezcano@linaro.org
This commit is contained in:
Daniel Lezcano 2022-10-03 11:25:36 +02:00 committed by Daniel Lezcano
parent 0614755dbf
commit 2e38a2a981
3 changed files with 57 additions and 41 deletions

View file

@ -1228,6 +1228,45 @@ int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
} }
EXPORT_SYMBOL_GPL(thermal_zone_get_trip); EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
const struct thermal_trip *trip)
{
struct thermal_trip t;
int ret;
if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips)
return -EINVAL;
ret = __thermal_zone_get_trip(tz, trip_id, &t);
if (ret)
return ret;
if (t.type != trip->type)
return -EINVAL;
if (t.temperature != trip->temperature && tz->ops->set_trip_temp) {
ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature);
if (ret)
return ret;
}
if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) {
ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis);
if (ret)
return ret;
}
if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis))
tz->trips[trip_id] = *trip;
thermal_notify_tz_trip_change(tz->id, trip_id, trip->type,
trip->temperature, trip->hysteresis);
__thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
return 0;
}
/** /**
* thermal_zone_device_register_with_trips() - register a new thermal zone device * thermal_zone_device_register_with_trips() - register a new thermal zone device
* @type: the thermal zone device type * @type: the thermal zone device type

View file

@ -123,15 +123,9 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
struct thermal_trip trip; struct thermal_trip trip;
int trip_id, ret; int trip_id, ret;
if (!tz->ops->set_trip_temp && !tz->trips)
return -EPERM;
if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1) if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
return -EINVAL; return -EINVAL;
if (kstrtoint(buf, 10, &trip.temperature))
return -EINVAL;
mutex_lock(&tz->lock); mutex_lock(&tz->lock);
if (!device_is_registered(dev)) { if (!device_is_registered(dev)) {
@ -139,31 +133,19 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
goto unlock; goto unlock;
} }
if (tz->ops->set_trip_temp) {
ret = tz->ops->set_trip_temp(tz, trip_id, trip.temperature);
if (ret)
goto unlock;
}
if (tz->trips)
tz->trips[trip_id].temperature = trip.temperature;
ret = __thermal_zone_get_trip(tz, trip_id, &trip); ret = __thermal_zone_get_trip(tz, trip_id, &trip);
if (ret) if (ret)
goto unlock; goto unlock;
thermal_notify_tz_trip_change(tz->id, trip_id, trip.type, ret = kstrtoint(buf, 10, &trip.temperature);
trip.temperature, trip.hysteresis); if (ret)
goto unlock;
__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
ret = thermal_zone_set_trip(tz, trip_id, &trip);
unlock: unlock:
mutex_unlock(&tz->lock); mutex_unlock(&tz->lock);
if (ret) return ret ? ret : count;
return ret;
return count;
} }
static ssize_t static ssize_t
@ -197,16 +179,13 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct thermal_zone_device *tz = to_thermal_zone(dev); struct thermal_zone_device *tz = to_thermal_zone(dev);
int trip, ret; struct thermal_trip trip;
int temperature; int trip_id, ret;
if (!tz->ops->set_trip_hyst) if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
return -EPERM;
if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
return -EINVAL; return -EINVAL;
if (kstrtoint(buf, 10, &temperature)) if (kstrtoint(buf, 10, &trip.hysteresis))
return -EINVAL; return -EINVAL;
mutex_lock(&tz->lock); mutex_lock(&tz->lock);
@ -216,16 +195,11 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
goto unlock; goto unlock;
} }
/* ret = __thermal_zone_get_trip(tz, trip_id, &trip);
* We are not doing any check on the 'temperature' value if (ret)
* here. The driver implementing 'set_trip_hyst' has to goto unlock;
* take care of this.
*/ ret = thermal_zone_set_trip(tz, trip_id, &trip);
ret = tz->ops->set_trip_hyst(tz, trip, temperature);
if (!ret)
__thermal_zone_set_trips(tz);
unlock: unlock:
mutex_unlock(&tz->lock); mutex_unlock(&tz->lock);

View file

@ -337,6 +337,9 @@ static inline void devm_thermal_of_zone_unregister(struct device *dev,
int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
struct thermal_trip *trip); struct thermal_trip *trip);
int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
const struct thermal_trip *trip);
int thermal_zone_get_num_trips(struct thermal_zone_device *tz); int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp); int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);