···1515- clock-latency: Specify the possible maximum transition latency for clock,1616 in unit of nanoseconds.1717- voltage-tolerance: Specify the CPU voltage tolerance in percentage.1818+- #cooling-cells:1919+- cooling-min-level:2020+- cooling-max-level:2121+ Please refer to Documentation/devicetree/bindings/thermal/thermal.txt.18221923Examples:2024···3733 198000 8500003834 >;3935 clock-latency = <61036>; /* two CLK32 periods */3636+ #cooling-cells = <2>;3737+ cooling-min-level = <0>;3838+ cooling-max-level = <2>;4039 };41404241 cpu@1 {
···11+* Thermal Framework Device Tree descriptor22+33+This file describes a generic binding to provide a way of44+defining hardware thermal structure using device tree.55+A thermal structure includes thermal zones and their components,66+such as trip points, polling intervals, sensors and cooling devices77+binding descriptors.88+99+The target of device tree thermal descriptors is to describe only1010+the hardware thermal aspects. The thermal device tree bindings are1111+not about how the system must control or which algorithm or policy1212+must be taken in place.1313+1414+There are five types of nodes involved to describe thermal bindings:1515+- thermal sensors: devices which may be used to take temperature1616+ measurements.1717+- cooling devices: devices which may be used to dissipate heat.1818+- trip points: describe key temperatures at which cooling is recommended. The1919+ set of points should be chosen based on hardware limits.2020+- cooling maps: used to describe links between trip points and cooling devices;2121+- thermal zones: used to describe thermal data within the hardware;2222+2323+The following is a description of each of these node types.2424+2525+* Thermal sensor devices2626+2727+Thermal sensor devices are nodes providing temperature sensing capabilities on2828+thermal zones. Typical devices are I2C ADC converters and bandgaps. These are2929+nodes providing temperature data to thermal zones. Thermal sensor devices may3030+control one or more internal sensors.3131+3232+Required property:3333+- #thermal-sensor-cells: Used to provide sensor device specific information3434+ Type: unsigned while referring to it. Typically 0 on thermal sensor3535+ Size: one cell nodes with only one sensor, and at least 1 on nodes3636+ with several internal sensors, in order3737+ to identify uniquely the sensor instances within3838+ the IC. See thermal zone binding for more details3939+ on how consumers refer to sensor devices.4040+4141+* Cooling device nodes4242+4343+Cooling devices are nodes providing control on power dissipation. There4444+are essentially two ways to provide control on power dissipation. First4545+is by means of regulating device performance, which is known as passive4646+cooling. A typical passive cooling is a CPU that has dynamic voltage and4747+frequency scaling (DVFS), and uses lower frequencies as cooling states.4848+Second is by means of activating devices in order to remove4949+the dissipated heat, which is known as active cooling, e.g. regulating5050+fan speeds. In both cases, cooling devices shall have a way to determine5151+the state of cooling in which the device is.5252+5353+Any cooling device has a range of cooling states (i.e. different levels5454+of heat dissipation). For example a fan's cooling states correspond to5555+the different fan speeds possible. Cooling states are referred to by5656+single unsigned integers, where larger numbers mean greater heat5757+dissipation. The precise set of cooling states associated with a device5858+(as referred to be the cooling-min-state and cooling-max-state5959+properties) should be defined in a particular device's binding.6060+For more examples of cooling devices, refer to the example sections below.6161+6262+Required properties:6363+- cooling-min-state: An integer indicating the smallest6464+ Type: unsigned cooling state accepted. Typically 0.6565+ Size: one cell6666+6767+- cooling-max-state: An integer indicating the largest6868+ Type: unsigned cooling state accepted.6969+ Size: one cell7070+7171+- #cooling-cells: Used to provide cooling device specific information7272+ Type: unsigned while referring to it. Must be at least 2, in order7373+ Size: one cell to specify minimum and maximum cooling state used7474+ in the reference. The first cell is the minimum7575+ cooling state requested and the second cell is7676+ the maximum cooling state requested in the reference.7777+ See Cooling device maps section below for more details7878+ on how consumers refer to cooling devices.7979+8080+* Trip points8181+8282+The trip node is a node to describe a point in the temperature domain8383+in which the system takes an action. This node describes just the point,8484+not the action.8585+8686+Required properties:8787+- temperature: An integer indicating the trip temperature level,8888+ Type: signed in millicelsius.8989+ Size: one cell9090+9191+- hysteresis: A low hysteresis value on temperature property (above).9292+ Type: unsigned This is a relative value, in millicelsius.9393+ Size: one cell9494+9595+- type: a string containing the trip type. Expected values are:9696+ "active": A trip point to enable active cooling9797+ "passive": A trip point to enable passive cooling9898+ "hot": A trip point to notify emergency9999+ "critical": Hardware not reliable.100100+ Type: string101101+102102+* Cooling device maps103103+104104+The cooling device maps node is a node to describe how cooling devices105105+get assigned to trip points of the zone. The cooling devices are expected106106+to be loaded in the target system.107107+108108+Required properties:109109+- cooling-device: A phandle of a cooling device with its specifier,110110+ Type: phandle + referring to which cooling device is used in this111111+ cooling specifier binding. In the cooling specifier, the first cell112112+ is the minimum cooling state and the second cell113113+ is the maximum cooling state used in this map.114114+- trip: A phandle of a trip point node within the same thermal115115+ Type: phandle of zone.116116+ trip point node117117+118118+Optional property:119119+- contribution: The cooling contribution to the thermal zone of the120120+ Type: unsigned referred cooling device at the referred trip point.121121+ Size: one cell The contribution is a ratio of the sum122122+ of all cooling contributions within a thermal zone.123123+124124+Note: Using the THERMAL_NO_LIMIT (-1UL) constant in the cooling-device phandle125125+limit specifier means:126126+(i) - minimum state allowed for minimum cooling state used in the reference.127127+(ii) - maximum state allowed for maximum cooling state used in the reference.128128+Refer to include/dt-bindings/thermal/thermal.h for definition of this constant.129129+130130+* Thermal zone nodes131131+132132+The thermal zone node is the node containing all the required info133133+for describing a thermal zone, including its cooling device bindings. The134134+thermal zone node must contain, apart from its own properties, one sub-node135135+containing trip nodes and one sub-node containing all the zone cooling maps.136136+137137+Required properties:138138+- polling-delay: The maximum number of milliseconds to wait between polls139139+ Type: unsigned when checking this thermal zone.140140+ Size: one cell141141+142142+- polling-delay-passive: The maximum number of milliseconds to wait143143+ Type: unsigned between polls when performing passive cooling.144144+ Size: one cell145145+146146+- thermal-sensors: A list of thermal sensor phandles and sensor specifier147147+ Type: list of used while monitoring the thermal zone.148148+ phandles + sensor149149+ specifier150150+151151+- trips: A sub-node which is a container of only trip point nodes152152+ Type: sub-node required to describe the thermal zone.153153+154154+- cooling-maps: A sub-node which is a container of only cooling device155155+ Type: sub-node map nodes, used to describe the relation between trips156156+ and cooling devices.157157+158158+Optional property:159159+- coefficients: An array of integers (one signed cell) containing160160+ Type: array coefficients to compose a linear relation between161161+ Elem size: one cell the sensors listed in the thermal-sensors property.162162+ Elem type: signed Coefficients defaults to 1, in case this property163163+ is not specified. A simple linear polynomial is used:164164+ Z = c0 * x0 + c1 + x1 + ... + c(n-1) * x(n-1) + cn.165165+166166+ The coefficients are ordered and they match with sensors167167+ by means of sensor ID. Additional coefficients are168168+ interpreted as constant offset.169169+170170+Note: The delay properties are bound to the maximum dT/dt (temperature171171+derivative over time) in two situations for a thermal zone:172172+(i) - when passive cooling is activated (polling-delay-passive); and173173+(ii) - when the zone just needs to be monitored (polling-delay) or174174+when active cooling is activated.175175+176176+The maximum dT/dt is highly bound to hardware power consumption and dissipation177177+capability. The delays should be chosen to account for said max dT/dt,178178+such that a device does not cross several trip boundaries unexpectedly179179+between polls. Choosing the right polling delays shall avoid having the180180+device in temperature ranges that may damage the silicon structures and181181+reduce silicon lifetime.182182+183183+* The thermal-zones node184184+185185+The "thermal-zones" node is a container for all thermal zone nodes. It shall186186+contain only sub-nodes describing thermal zones as in the section187187+"Thermal zone nodes". The "thermal-zones" node appears under "/".188188+189189+* Examples190190+191191+Below are several examples on how to use thermal data descriptors192192+using device tree bindings:193193+194194+(a) - CPU thermal zone195195+196196+The CPU thermal zone example below describes how to setup one thermal zone197197+using one single sensor as temperature source and many cooling devices and198198+power dissipation control sources.199199+200200+#include <dt-bindings/thermal/thermal.h>201201+202202+cpus {203203+ /*204204+ * Here is an example of describing a cooling device for a DVFS205205+ * capable CPU. The CPU node describes its four OPPs.206206+ * The cooling states possible are 0..3, and they are207207+ * used as OPP indexes. The minimum cooling state is 0, which means208208+ * all four OPPs can be available to the system. The maximum209209+ * cooling state is 3, which means only the lowest OPPs (198MHz@0.85V)210210+ * can be available in the system.211211+ */212212+ cpu0: cpu@0 {213213+ ...214214+ operating-points = <215215+ /* kHz uV */216216+ 970000 1200000217217+ 792000 1100000218218+ 396000 950000219219+ 198000 850000220220+ >;221221+ cooling-min-state = <0>;222222+ cooling-max-state = <3>;223223+ #cooling-cells = <2>; /* min followed by max */224224+ };225225+ ...226226+};227227+228228+&i2c1 {229229+ ...230230+ /*231231+ * A simple fan controller which supports 10 speeds of operation232232+ * (represented as 0-9).233233+ */234234+ fan0: fan@0x48 {235235+ ...236236+ cooling-min-state = <0>;237237+ cooling-max-state = <9>;238238+ #cooling-cells = <2>; /* min followed by max */239239+ };240240+};241241+242242+ocp {243243+ ...244244+ /*245245+ * A simple IC with a single bandgap temperature sensor.246246+ */247247+ bandgap0: bandgap@0x0000ED00 {248248+ ...249249+ #thermal-sensor-cells = <0>;250250+ };251251+};252252+253253+thermal-zones {254254+ cpu-thermal: cpu-thermal {255255+ polling-delay-passive = <250>; /* milliseconds */256256+ polling-delay = <1000>; /* milliseconds */257257+258258+ thermal-sensors = <&bandgap0>;259259+260260+ trips {261261+ cpu-alert0: cpu-alert {262262+ temperature = <90000>; /* millicelsius */263263+ hysteresis = <2000>; /* millicelsius */264264+ type = "active";265265+ };266266+ cpu-alert1: cpu-alert {267267+ temperature = <100000>; /* millicelsius */268268+ hysteresis = <2000>; /* millicelsius */269269+ type = "passive";270270+ };271271+ cpu-crit: cpu-crit {272272+ temperature = <125000>; /* millicelsius */273273+ hysteresis = <2000>; /* millicelsius */274274+ type = "critical";275275+ };276276+ };277277+278278+ cooling-maps {279279+ map0 {280280+ trip = <&cpu-alert0>;281281+ cooling-device = <&fan0 THERMAL_NO_LIMITS 4>;282282+ };283283+ map1 {284284+ trip = <&cpu-alert1>;285285+ cooling-device = <&fan0 5 THERMAL_NO_LIMITS>;286286+ };287287+ map2 {288288+ trip = <&cpu-alert1>;289289+ cooling-device =290290+ <&cpu0 THERMAL_NO_LIMITS THERMAL_NO_LIMITS>;291291+ };292292+ };293293+ };294294+};295295+296296+In the example above, the ADC sensor (bandgap0) at address 0x0000ED00 is297297+used to monitor the zone 'cpu-thermal' using its sole sensor. A fan298298+device (fan0) is controlled via I2C bus 1, at address 0x48, and has ten299299+different cooling states 0-9. It is used to remove the heat out of300300+the thermal zone 'cpu-thermal' using its cooling states301301+from its minimum to 4, when it reaches trip point 'cpu-alert0'302302+at 90C, as an example of active cooling. The same cooling device is used at303303+'cpu-alert1', but from 5 to its maximum state. The cpu@0 device is also304304+linked to the same thermal zone, 'cpu-thermal', as a passive cooling device,305305+using all its cooling states at trip point 'cpu-alert1',306306+which is a trip point at 100C. On the thermal zone 'cpu-thermal', at the307307+temperature of 125C, represented by the trip point 'cpu-crit', the silicon308308+is not reliable anymore.309309+310310+(b) - IC with several internal sensors311311+312312+The example below describes how to deploy several thermal zones based off a313313+single sensor IC, assuming it has several internal sensors. This is a common314314+case on SoC designs with several internal IPs that may need different thermal315315+requirements, and thus may have their own sensor to monitor or detect internal316316+hotspots in their silicon.317317+318318+#include <dt-bindings/thermal/thermal.h>319319+320320+ocp {321321+ ...322322+ /*323323+ * A simple IC with several bandgap temperature sensors.324324+ */325325+ bandgap0: bandgap@0x0000ED00 {326326+ ...327327+ #thermal-sensor-cells = <1>;328328+ };329329+};330330+331331+thermal-zones {332332+ cpu-thermal: cpu-thermal {333333+ polling-delay-passive = <250>; /* milliseconds */334334+ polling-delay = <1000>; /* milliseconds */335335+336336+ /* sensor ID */337337+ thermal-sensors = <&bandgap0 0>;338338+339339+ trips {340340+ /* each zone within the SoC may have its own trips */341341+ cpu-alert: cpu-alert {342342+ temperature = <100000>; /* millicelsius */343343+ hysteresis = <2000>; /* millicelsius */344344+ type = "passive";345345+ };346346+ cpu-crit: cpu-crit {347347+ temperature = <125000>; /* millicelsius */348348+ hysteresis = <2000>; /* millicelsius */349349+ type = "critical";350350+ };351351+ };352352+353353+ cooling-maps {354354+ /* each zone within the SoC may have its own cooling */355355+ ...356356+ };357357+ };358358+359359+ gpu-thermal: gpu-thermal {360360+ polling-delay-passive = <120>; /* milliseconds */361361+ polling-delay = <1000>; /* milliseconds */362362+363363+ /* sensor ID */364364+ thermal-sensors = <&bandgap0 1>;365365+366366+ trips {367367+ /* each zone within the SoC may have its own trips */368368+ gpu-alert: gpu-alert {369369+ temperature = <90000>; /* millicelsius */370370+ hysteresis = <2000>; /* millicelsius */371371+ type = "passive";372372+ };373373+ gpu-crit: gpu-crit {374374+ temperature = <105000>; /* millicelsius */375375+ hysteresis = <2000>; /* millicelsius */376376+ type = "critical";377377+ };378378+ };379379+380380+ cooling-maps {381381+ /* each zone within the SoC may have its own cooling */382382+ ...383383+ };384384+ };385385+386386+ dsp-thermal: dsp-thermal {387387+ polling-delay-passive = <50>; /* milliseconds */388388+ polling-delay = <1000>; /* milliseconds */389389+390390+ /* sensor ID */391391+ thermal-sensors = <&bandgap0 2>;392392+393393+ trips {394394+ /* each zone within the SoC may have its own trips */395395+ dsp-alert: gpu-alert {396396+ temperature = <90000>; /* millicelsius */397397+ hysteresis = <2000>; /* millicelsius */398398+ type = "passive";399399+ };400400+ dsp-crit: gpu-crit {401401+ temperature = <135000>; /* millicelsius */402402+ hysteresis = <2000>; /* millicelsius */403403+ type = "critical";404404+ };405405+ };406406+407407+ cooling-maps {408408+ /* each zone within the SoC may have its own cooling */409409+ ...410410+ };411411+ };412412+};413413+414414+In the example above, there is one bandgap IC which has the capability to415415+monitor three sensors. The hardware has been designed so that sensors are416416+placed on different places in the DIE to monitor different temperature417417+hotspots: one for CPU thermal zone, one for GPU thermal zone and the418418+other to monitor a DSP thermal zone.419419+420420+Thus, there is a need to assign each sensor provided by the bandgap IC421421+to different thermal zones. This is achieved by means of using the422422+#thermal-sensor-cells property and using the first cell of the sensor423423+specifier as sensor ID. In the example, then, <bandgap 0> is used to424424+monitor CPU thermal zone, <bandgap 1> is used to monitor GPU thermal425425+zone and <bandgap 2> is used to monitor DSP thermal zone. Each zone426426+may be uncorrelated, having its own dT/dt requirements, trips427427+and cooling maps.428428+429429+430430+(c) - Several sensors within one single thermal zone431431+432432+The example below illustrates how to use more than one sensor within433433+one thermal zone.434434+435435+#include <dt-bindings/thermal/thermal.h>436436+437437+&i2c1 {438438+ ...439439+ /*440440+ * A simple IC with a single temperature sensor.441441+ */442442+ adc: sensor@0x49 {443443+ ...444444+ #thermal-sensor-cells = <0>;445445+ };446446+};447447+448448+ocp {449449+ ...450450+ /*451451+ * A simple IC with a single bandgap temperature sensor.452452+ */453453+ bandgap0: bandgap@0x0000ED00 {454454+ ...455455+ #thermal-sensor-cells = <0>;456456+ };457457+};458458+459459+thermal-zones {460460+ cpu-thermal: cpu-thermal {461461+ polling-delay-passive = <250>; /* milliseconds */462462+ polling-delay = <1000>; /* milliseconds */463463+464464+ thermal-sensors = <&bandgap0>, /* cpu */465465+ <&adc>; /* pcb north */466466+467467+ /* hotspot = 100 * bandgap - 120 * adc + 484 */468468+ coefficients = <100 -120 484>;469469+470470+ trips {471471+ ...472472+ };473473+474474+ cooling-maps {475475+ ...476476+ };477477+ };478478+};479479+480480+In some cases, there is a need to use more than one sensor to extrapolate481481+a thermal hotspot in the silicon. The above example illustrates this situation.482482+For instance, it may be the case that a sensor external to CPU IP may be placed483483+close to CPU hotspot and together with internal CPU sensor, it is used484484+to determine the hotspot. Assuming this is the case for the above example,485485+the hypothetical extrapolation rule would be:486486+ hotspot = 100 * bandgap - 120 * adc + 484487487+488488+In other context, the same idea can be used to add fixed offset. For instance,489489+consider the hotspot extrapolation rule below:490490+ hotspot = 1 * adc + 6000491491+492492+In the above equation, the hotspot is always 6C higher than what is read493493+from the ADC sensor. The binding would be then:494494+ thermal-sensors = <&adc>;495495+496496+ /* hotspot = 1 * adc + 6000 */497497+ coefficients = <1 6000>;498498+499499+(d) - Board thermal500500+501501+The board thermal example below illustrates how to setup one thermal zone502502+with many sensors and many cooling devices.503503+504504+#include <dt-bindings/thermal/thermal.h>505505+506506+&i2c1 {507507+ ...508508+ /*509509+ * An IC with several temperature sensor.510510+ */511511+ adc-dummy: sensor@0x50 {512512+ ...513513+ #thermal-sensor-cells = <1>; /* sensor internal ID */514514+ };515515+};516516+517517+thermal-zones {518518+ batt-thermal {519519+ polling-delay-passive = <500>; /* milliseconds */520520+ polling-delay = <2500>; /* milliseconds */521521+522522+ /* sensor ID */523523+ thermal-sensors = <&adc-dummy 4>;524524+525525+ trips {526526+ ...527527+ };528528+529529+ cooling-maps {530530+ ...531531+ };532532+ };533533+534534+ board-thermal: board-thermal {535535+ polling-delay-passive = <1000>; /* milliseconds */536536+ polling-delay = <2500>; /* milliseconds */537537+538538+ /* sensor ID */539539+ thermal-sensors = <&adc-dummy 0>, /* pcb top edge */540540+ <&adc-dummy 1>, /* lcd */541541+ <&adc-dymmy 2>; /* back cover */542542+ /*543543+ * An array of coefficients describing the sensor544544+ * linear relation. E.g.:545545+ * z = c1*x1 + c2*x2 + c3*x3546546+ */547547+ coefficients = <1200 -345 890>;548548+549549+ trips {550550+ /* Trips are based on resulting linear equation */551551+ cpu-trip: cpu-trip {552552+ temperature = <60000>; /* millicelsius */553553+ hysteresis = <2000>; /* millicelsius */554554+ type = "passive";555555+ };556556+ gpu-trip: gpu-trip {557557+ temperature = <55000>; /* millicelsius */558558+ hysteresis = <2000>; /* millicelsius */559559+ type = "passive";560560+ }561561+ lcd-trip: lcp-trip {562562+ temperature = <53000>; /* millicelsius */563563+ hysteresis = <2000>; /* millicelsius */564564+ type = "passive";565565+ };566566+ crit-trip: crit-trip {567567+ temperature = <68000>; /* millicelsius */568568+ hysteresis = <2000>; /* millicelsius */569569+ type = "critical";570570+ };571571+ };572572+573573+ cooling-maps {574574+ map0 {575575+ trip = <&cpu-trip>;576576+ cooling-device = <&cpu0 0 2>;577577+ contribution = <55>;578578+ };579579+ map1 {580580+ trip = <&gpu-trip>;581581+ cooling-device = <&gpu0 0 2>;582582+ contribution = <20>;583583+ };584584+ map2 {585585+ trip = <&lcd-trip>;586586+ cooling-device = <&lcd0 5 10>;587587+ contribution = <15>;588588+ };589589+ };590590+ };591591+};592592+593593+The above example is a mix of previous examples, a sensor IP with several internal594594+sensors used to monitor different zones, one of them is composed by several sensors and595595+with different cooling devices.
+1
MAINTAINERS
···85018501F: drivers/thermal/85028502F: include/linux/thermal.h85038503F: include/linux/cpu_cooling.h85048504+F: Documentation/devicetree/bindings/thermal/8504850585058506THINGM BLINK(1) USB RGB LED DRIVER85068507M: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
+41
arch/arm/boot/dts/omap4-cpu-thermal.dtsi
···11+/*22+ * Device Tree Source for OMAP4/5 SoC CPU thermal33+ *44+ * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/55+ * Contact: Eduardo Valentin <eduardo.valentin@ti.com>66+ *77+ * This file is licensed under the terms of the GNU General Public License88+ * version 2. This program is licensed "as is" without any warranty of any99+ * kind, whether express or implied.1010+ */1111+1212+#include <dt-bindings/thermal/thermal.h>1313+1414+cpu_thermal: cpu_thermal {1515+ polling-delay-passive = <250>; /* milliseconds */1616+ polling-delay = <1000>; /* milliseconds */1717+1818+ /* sensor ID */1919+ thermal-sensors = <&bandgap 0>;2020+2121+ trips {2222+ cpu_alert0: cpu_alert {2323+ temperature = <100000>; /* millicelsius */2424+ hysteresis = <2000>; /* millicelsius */2525+ type = "passive";2626+ };2727+ cpu_crit: cpu_crit {2828+ temperature = <125000>; /* millicelsius */2929+ hysteresis = <2000>; /* millicelsius */3030+ type = "critical";3131+ };3232+ };3333+3434+ cooling-maps {3535+ map0 {3636+ trip = <&cpu_alert0>;3737+ cooling-device =3838+ <&cpu0 THERMAL_NO_LIMIT THERMAL_NO_LIMIT>;3939+ };4040+ };4141+};
···11+/*22+ * Device Tree Source for OMAP543x SoC CORE thermal33+ *44+ * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/55+ * Contact: Eduardo Valentin <eduardo.valentin@ti.com>66+ *77+ * This file is licensed under the terms of the GNU General Public License88+ * version 2. This program is licensed "as is" without any warranty of any99+ * kind, whether express or implied.1010+ */1111+1212+#include <dt-bindings/thermal/thermal.h>1313+1414+core_thermal: core_thermal {1515+ polling-delay-passive = <250>; /* milliseconds */1616+ polling-delay = <1000>; /* milliseconds */1717+1818+ /* sensor ID */1919+ thermal-sensors = <&bandgap 2>;2020+2121+ trips {2222+ core_crit: core_crit {2323+ temperature = <125000>; /* milliCelsius */2424+ hysteresis = <2000>; /* milliCelsius */2525+ type = "critical";2626+ };2727+ };2828+};
+28
arch/arm/boot/dts/omap5-gpu-thermal.dtsi
···11+/*22+ * Device Tree Source for OMAP543x SoC GPU thermal33+ *44+ * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/55+ * Contact: Eduardo Valentin <eduardo.valentin@ti.com>66+ *77+ * This file is licensed under the terms of the GNU General Public License88+ * version 2. This program is licensed "as is" without any warranty of any99+ * kind, whether express or implied.1010+ */1111+1212+#include <dt-bindings/thermal/thermal.h>1313+1414+gpu_thermal: gpu_thermal {1515+ polling-delay-passive = <250>; /* milliseconds */1616+ polling-delay = <1000>; /* milliseconds */1717+1818+ /* sensor ID */1919+ thermal-sensors = <&bandgap 1>;2020+2121+ trips {2222+ gpu_crit: gpu_crit {2323+ temperature = <125000>; /* milliCelsius */2424+ hysteresis = <2000>; /* milliCelsius */2525+ type = "critical";2626+ };2727+ };2828+};
···181181182182config GENERIC_CPUFREQ_CPU0183183 tristate "Generic CPU0 cpufreq driver"184184- depends on HAVE_CLK && REGULATOR && PM_OPP && OF184184+ depends on HAVE_CLK && REGULATOR && PM_OPP && OF && THERMAL && CPU_THERMAL185185 help186186 This adds a generic cpufreq driver for CPU0 frequency management.187187 It supports both uniprocessor (UP) and symmetric multiprocessor (SMP)
+16
drivers/cpufreq/cpufreq-cpu0.c
···13131414#include <linux/clk.h>1515#include <linux/cpu.h>1616+#include <linux/cpu_cooling.h>1617#include <linux/cpufreq.h>1818+#include <linux/cpumask.h>1719#include <linux/err.h>1820#include <linux/module.h>1921#include <linux/of.h>···2321#include <linux/platform_device.h>2422#include <linux/regulator/consumer.h>2523#include <linux/slab.h>2424+#include <linux/thermal.h>26252726static unsigned int transition_latency;2827static unsigned int voltage_tolerance; /* in percentage */···3229static struct clk *cpu_clk;3330static struct regulator *cpu_reg;3431static struct cpufreq_frequency_table *freq_table;3232+static struct thermal_cooling_device *cdev;35333634static unsigned int cpu0_get_speed(unsigned int cpu)3735{···205201 goto out_free_table;206202 }207203204204+ /*205205+ * For now, just loading the cooling device;206206+ * thermal DT code takes care of matching them.207207+ */208208+ if (of_find_property(np, "#cooling-cells", NULL)) {209209+ cdev = of_cpufreq_cooling_register(np, cpu_present_mask);210210+ if (IS_ERR(cdev))211211+ pr_err("running cpufreq without cooling device: %ld\n",212212+ PTR_ERR(cdev));213213+ }214214+208215 of_node_put(np);209216 return 0;210217···228213229214static int cpu0_cpufreq_remove(struct platform_device *pdev)230215{216216+ cpufreq_cooling_unregister(cdev);231217 cpufreq_unregister_driver(&cpu0_cpufreq_driver);232218 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);233219
···2929 Say 'Y' here if you want all thermal sensors to3030 have hwmon sysfs interface too.31313232+config THERMAL_OF3333+ bool3434+ prompt "APIs to parse thermal data out of device tree"3535+ depends on OF3636+ default y3737+ help3838+ This options provides helpers to add the support to3939+ read and parse thermal data definitions out of the4040+ device tree blob.4141+4242+ Say 'Y' here if you need to build thermal infrastructure4343+ based on device tree.4444+3245choice3346 prompt "Default Thermal governor"3447 default THERMAL_DEFAULT_GOV_STEP_WISE···9279config CPU_THERMAL9380 bool "generic cpu cooling support"9481 depends on CPU_FREQ8282+ depends on THERMAL_OF9583 help9684 This implements the generic cpu cooling mechanism through frequency9785 reduction. An ACPI version of this already exists···135121136122config RCAR_THERMAL137123 tristate "Renesas R-Car thermal driver"138138- depends on ARCH_SHMOBILE124124+ depends on ARCH_SHMOBILE || COMPILE_TEST139125 help140126 Enable this to plug the R-Car thermal sensor driver into the Linux141127 thermal framework.···205191 thermal zone. Each package will have its own thermal zone. There are206192 two trip points which can be set by user to get notifications via thermal207193 notification methods.194194+195195+config ACPI_INT3403_THERMAL196196+ tristate "ACPI INT3403 thermal driver"197197+ depends on X86 && ACPI198198+ help199199+ This driver uses ACPI INT3403 device objects. If present, it will200200+ register each INT3403 thermal sensor as a thermal zone.208201209202menu "Texas Instruments thermal drivers"210203source "drivers/thermal/ti-soc-thermal/Kconfig"
···424424};425425426426/**427427- * cpufreq_cooling_register - function to create cpufreq cooling device.427427+ * __cpufreq_cooling_register - helper function to create cpufreq cooling device428428+ * @np: a valid struct device_node to the cooling device device tree node428429 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.429430 *430431 * This interface function registers the cpufreq cooling device with the name431432 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq432432- * cooling devices.433433+ * cooling devices. It also gives the opportunity to link the cooling device434434+ * with a device tree node, in order to bind it via the thermal DT code.433435 *434436 * Return: a valid struct thermal_cooling_device pointer on success,435437 * on failure, it returns a corresponding ERR_PTR().436438 */437437-struct thermal_cooling_device *438438-cpufreq_cooling_register(const struct cpumask *clip_cpus)439439+static struct thermal_cooling_device *440440+__cpufreq_cooling_register(struct device_node *np,441441+ const struct cpumask *clip_cpus)439442{440443 struct thermal_cooling_device *cool_dev;441444 struct cpufreq_cooling_device *cpufreq_dev = NULL;···477474 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",478475 cpufreq_dev->id);479476480480- cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,481481- &cpufreq_cooling_ops);477477+ cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,478478+ &cpufreq_cooling_ops);482479 if (IS_ERR(cool_dev)) {483480 release_idr(&cpufreq_idr, cpufreq_dev->id);484481 kfree(cpufreq_dev);···498495499496 return cool_dev;500497}498498+499499+/**500500+ * cpufreq_cooling_register - function to create cpufreq cooling device.501501+ * @clip_cpus: cpumask of cpus where the frequency constraints will happen.502502+ *503503+ * This interface function registers the cpufreq cooling device with the name504504+ * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq505505+ * cooling devices.506506+ *507507+ * Return: a valid struct thermal_cooling_device pointer on success,508508+ * on failure, it returns a corresponding ERR_PTR().509509+ */510510+struct thermal_cooling_device *511511+cpufreq_cooling_register(const struct cpumask *clip_cpus)512512+{513513+ return __cpufreq_cooling_register(NULL, clip_cpus);514514+}501515EXPORT_SYMBOL_GPL(cpufreq_cooling_register);516516+517517+/**518518+ * of_cpufreq_cooling_register - function to create cpufreq cooling device.519519+ * @np: a valid struct device_node to the cooling device device tree node520520+ * @clip_cpus: cpumask of cpus where the frequency constraints will happen.521521+ *522522+ * This interface function registers the cpufreq cooling device with the name523523+ * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq524524+ * cooling devices. Using this API, the cpufreq cooling device will be525525+ * linked to the device tree node provided.526526+ *527527+ * Return: a valid struct thermal_cooling_device pointer on success,528528+ * on failure, it returns a corresponding ERR_PTR().529529+ */530530+struct thermal_cooling_device *531531+of_cpufreq_cooling_register(struct device_node *np,532532+ const struct cpumask *clip_cpus)533533+{534534+ if (!np)535535+ return ERR_PTR(-EINVAL);536536+537537+ return __cpufreq_cooling_register(np, clip_cpus);538538+}539539+EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);502540503541/**504542 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
+18-14
drivers/thermal/imx_thermal.c
···490490{491491 struct imx_thermal_data *data = dev_get_drvdata(dev);492492 struct regmap *map = data->tempmon;493493- u32 val;494493495495- regmap_read(map, TEMPSENSE0, &val);496496- if ((val & TEMPSENSE0_POWER_DOWN) == 0) {497497- /*498498- * If a measurement is taking place, wait for a long enough499499- * time for it to finish, and then check again. If it still500500- * does not finish, something must go wrong.501501- */502502- udelay(50);503503- regmap_read(map, TEMPSENSE0, &val);504504- if ((val & TEMPSENSE0_POWER_DOWN) == 0)505505- return -ETIMEDOUT;506506- }494494+ /*495495+ * Need to disable thermal sensor, otherwise, when thermal core496496+ * try to get temperature before thermal sensor resume, a wrong497497+ * temperature will be read as the thermal sensor is powered498498+ * down.499499+ */500500+ regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);501501+ regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);502502+ data->mode = THERMAL_DEVICE_DISABLED;507503508504 return 0;509505}510506511507static int imx_thermal_resume(struct device *dev)512508{513513- /* Nothing to do for now */509509+ struct imx_thermal_data *data = dev_get_drvdata(dev);510510+ struct regmap *map = data->tempmon;511511+512512+ /* Enabled thermal sensor after resume */513513+ regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);514514+ regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);515515+ data->mode = THERMAL_DEVICE_ENABLED;516516+514517 return 0;515518}516519#endif···525522 { .compatible = "fsl,imx6q-tempmon", },526523 { /* end */ }527524};525525+MODULE_DEVICE_TABLE(of, of_imx_thermal_match);528526529527static struct platform_driver imx_thermal = {530528 .driver = {
+237
drivers/thermal/int3403_thermal.c
···11+/*22+ * ACPI INT3403 thermal driver33+ * Copyright (c) 2013, Intel Corporation.44+ *55+ * This program is free software; you can redistribute it and/or modify it66+ * under the terms and conditions of the GNU General Public License,77+ * version 2, as published by the Free Software Foundation.88+ *99+ * This program is distributed in the hope it will be useful, but WITHOUT1010+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or1111+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for1212+ * more details.1313+ */1414+1515+#include <linux/kernel.h>1616+#include <linux/module.h>1717+#include <linux/init.h>1818+#include <linux/types.h>1919+#include <linux/acpi.h>2020+#include <linux/thermal.h>2121+2222+#define INT3403_TYPE_SENSOR 0x032323+#define INT3403_PERF_CHANGED_EVENT 0x802424+#define INT3403_THERMAL_EVENT 0x902525+2626+#define DECI_KELVIN_TO_MILLI_CELSIUS(t, off) (((t) - (off)) * 100)2727+#define KELVIN_OFFSET 27322828+#define MILLI_CELSIUS_TO_DECI_KELVIN(t, off) (((t) / 100) + (off))2929+3030+#define ACPI_INT3403_CLASS "int3403"3131+#define ACPI_INT3403_FILE_STATE "state"3232+3333+struct int3403_sensor {3434+ struct thermal_zone_device *tzone;3535+ unsigned long *thresholds;3636+};3737+3838+static int sys_get_curr_temp(struct thermal_zone_device *tzone,3939+ unsigned long *temp)4040+{4141+ struct acpi_device *device = tzone->devdata;4242+ unsigned long long tmp;4343+ acpi_status status;4444+4545+ status = acpi_evaluate_integer(device->handle, "_TMP", NULL, &tmp);4646+ if (ACPI_FAILURE(status))4747+ return -EIO;4848+4949+ *temp = DECI_KELVIN_TO_MILLI_CELSIUS(tmp, KELVIN_OFFSET);5050+5151+ return 0;5252+}5353+5454+static int sys_get_trip_hyst(struct thermal_zone_device *tzone,5555+ int trip, unsigned long *temp)5656+{5757+ struct acpi_device *device = tzone->devdata;5858+ unsigned long long hyst;5959+ acpi_status status;6060+6161+ status = acpi_evaluate_integer(device->handle, "GTSH", NULL, &hyst);6262+ if (ACPI_FAILURE(status))6363+ return -EIO;6464+6565+ *temp = DECI_KELVIN_TO_MILLI_CELSIUS(hyst, KELVIN_OFFSET);6666+6767+ return 0;6868+}6969+7070+static int sys_get_trip_temp(struct thermal_zone_device *tzone,7171+ int trip, unsigned long *temp)7272+{7373+ struct acpi_device *device = tzone->devdata;7474+ struct int3403_sensor *obj = acpi_driver_data(device);7575+7676+ /*7777+ * get_trip_temp is a mandatory callback but7878+ * PATx method doesn't return any value, so return7979+ * cached value, which was last set from user space.8080+ */8181+ *temp = obj->thresholds[trip];8282+8383+ return 0;8484+}8585+8686+static int sys_get_trip_type(struct thermal_zone_device *thermal,8787+ int trip, enum thermal_trip_type *type)8888+{8989+ /* Mandatory callback, may not mean much here */9090+ *type = THERMAL_TRIP_PASSIVE;9191+9292+ return 0;9393+}9494+9595+int sys_set_trip_temp(struct thermal_zone_device *tzone, int trip,9696+ unsigned long temp)9797+{9898+ struct acpi_device *device = tzone->devdata;9999+ acpi_status status;100100+ char name[10];101101+ int ret = 0;102102+ struct int3403_sensor *obj = acpi_driver_data(device);103103+104104+ snprintf(name, sizeof(name), "PAT%d", trip);105105+ if (acpi_has_method(device->handle, name)) {106106+ status = acpi_execute_simple_method(device->handle, name,107107+ MILLI_CELSIUS_TO_DECI_KELVIN(temp,108108+ KELVIN_OFFSET));109109+ if (ACPI_FAILURE(status))110110+ ret = -EIO;111111+ else112112+ obj->thresholds[trip] = temp;113113+ } else {114114+ ret = -EIO;115115+ dev_err(&device->dev, "sys_set_trip_temp: method not found\n");116116+ }117117+118118+ return ret;119119+}120120+121121+static struct thermal_zone_device_ops tzone_ops = {122122+ .get_temp = sys_get_curr_temp,123123+ .get_trip_temp = sys_get_trip_temp,124124+ .get_trip_type = sys_get_trip_type,125125+ .set_trip_temp = sys_set_trip_temp,126126+ .get_trip_hyst = sys_get_trip_hyst,127127+};128128+129129+static void acpi_thermal_notify(struct acpi_device *device, u32 event)130130+{131131+ struct int3403_sensor *obj;132132+133133+ if (!device)134134+ return;135135+136136+ obj = acpi_driver_data(device);137137+ if (!obj)138138+ return;139139+140140+ switch (event) {141141+ case INT3403_PERF_CHANGED_EVENT:142142+ break;143143+ case INT3403_THERMAL_EVENT:144144+ thermal_zone_device_update(obj->tzone);145145+ break;146146+ default:147147+ dev_err(&device->dev, "Unsupported event [0x%x]\n", event);148148+ break;149149+ }150150+}151151+152152+static int acpi_int3403_add(struct acpi_device *device)153153+{154154+ int result = 0;155155+ unsigned long long ptyp;156156+ acpi_status status;157157+ struct int3403_sensor *obj;158158+ unsigned long long trip_cnt;159159+ int trip_mask = 0;160160+161161+ if (!device)162162+ return -EINVAL;163163+164164+ status = acpi_evaluate_integer(device->handle, "PTYP", NULL, &ptyp);165165+ if (ACPI_FAILURE(status))166166+ return -EINVAL;167167+168168+ if (ptyp != INT3403_TYPE_SENSOR)169169+ return -EINVAL;170170+171171+ obj = devm_kzalloc(&device->dev, sizeof(*obj), GFP_KERNEL);172172+ if (!obj)173173+ return -ENOMEM;174174+175175+ device->driver_data = obj;176176+177177+ status = acpi_evaluate_integer(device->handle, "PATC", NULL,178178+ &trip_cnt);179179+ if (ACPI_FAILURE(status))180180+ trip_cnt = 0;181181+182182+ if (trip_cnt) {183183+ /* We have to cache, thresholds can't be readback */184184+ obj->thresholds = devm_kzalloc(&device->dev,185185+ sizeof(*obj->thresholds) * trip_cnt,186186+ GFP_KERNEL);187187+ if (!obj->thresholds)188188+ return -ENOMEM;189189+ trip_mask = BIT(trip_cnt) - 1;190190+ }191191+ obj->tzone = thermal_zone_device_register(acpi_device_bid(device),192192+ trip_cnt, trip_mask, device, &tzone_ops,193193+ NULL, 0, 0);194194+ if (IS_ERR(obj->tzone)) {195195+ result = PTR_ERR(obj->tzone);196196+ return result;197197+ }198198+199199+ strcpy(acpi_device_name(device), "INT3403");200200+ strcpy(acpi_device_class(device), ACPI_INT3403_CLASS);201201+202202+ return 0;203203+}204204+205205+static int acpi_int3403_remove(struct acpi_device *device)206206+{207207+ struct int3403_sensor *obj;208208+209209+ obj = acpi_driver_data(device);210210+ thermal_zone_device_unregister(obj->tzone);211211+212212+ return 0;213213+}214214+215215+ACPI_MODULE_NAME("int3403");216216+static const struct acpi_device_id int3403_device_ids[] = {217217+ {"INT3403", 0},218218+ {"", 0},219219+};220220+MODULE_DEVICE_TABLE(acpi, int3403_device_ids);221221+222222+static struct acpi_driver acpi_int3403_driver = {223223+ .name = "INT3403",224224+ .class = ACPI_INT3403_CLASS,225225+ .ids = int3403_device_ids,226226+ .ops = {227227+ .add = acpi_int3403_add,228228+ .remove = acpi_int3403_remove,229229+ .notify = acpi_thermal_notify,230230+ },231231+};232232+233233+module_acpi_driver(acpi_int3403_driver);234234+235235+MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");236236+MODULE_LICENSE("GPL v2");237237+MODULE_DESCRIPTION("ACPI INT3403 thermal driver");
+849
drivers/thermal/of-thermal.c
···11+/*22+ * of-thermal.c - Generic Thermal Management device tree support.33+ *44+ * Copyright (C) 2013 Texas Instruments55+ * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>66+ *77+ *88+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~99+ *1010+ * This program is free software; you can redistribute it and/or modify1111+ * it under the terms of the GNU General Public License as published by1212+ * the Free Software Foundation; version 2 of the License.1313+ *1414+ * This program is distributed in the hope that it will be useful, but1515+ * WITHOUT ANY WARRANTY; without even the implied warranty of1616+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU1717+ * General Public License for more details.1818+ *1919+ * You should have received a copy of the GNU General Public License along2020+ * with this program; if not, write to the Free Software Foundation, Inc.,2121+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.2222+ *2323+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2424+ */2525+#include <linux/thermal.h>2626+#include <linux/slab.h>2727+#include <linux/types.h>2828+#include <linux/of_device.h>2929+#include <linux/of_platform.h>3030+#include <linux/err.h>3131+#include <linux/export.h>3232+#include <linux/string.h>3333+3434+#include "thermal_core.h"3535+3636+/*** Private data structures to represent thermal device tree data ***/3737+3838+/**3939+ * struct __thermal_trip - representation of a point in temperature domain4040+ * @np: pointer to struct device_node that this trip point was created from4141+ * @temperature: temperature value in miliCelsius4242+ * @hysteresis: relative hysteresis in miliCelsius4343+ * @type: trip point type4444+ */4545+4646+struct __thermal_trip {4747+ struct device_node *np;4848+ unsigned long int temperature;4949+ unsigned long int hysteresis;5050+ enum thermal_trip_type type;5151+};5252+5353+/**5454+ * struct __thermal_bind_param - a match between trip and cooling device5555+ * @cooling_device: a pointer to identify the referred cooling device5656+ * @trip_id: the trip point index5757+ * @usage: the percentage (from 0 to 100) of cooling contribution5858+ * @min: minimum cooling state used at this trip point5959+ * @max: maximum cooling state used at this trip point6060+ */6161+6262+struct __thermal_bind_params {6363+ struct device_node *cooling_device;6464+ unsigned int trip_id;6565+ unsigned int usage;6666+ unsigned long min;6767+ unsigned long max;6868+};6969+7070+/**7171+ * struct __thermal_zone - internal representation of a thermal zone7272+ * @mode: current thermal zone device mode (enabled/disabled)7373+ * @passive_delay: polling interval while passive cooling is activated7474+ * @polling_delay: zone polling interval7575+ * @ntrips: number of trip points7676+ * @trips: an array of trip points (0..ntrips - 1)7777+ * @num_tbps: number of thermal bind params7878+ * @tbps: an array of thermal bind params (0..num_tbps - 1)7979+ * @sensor_data: sensor private data used while reading temperature and trend8080+ * @get_temp: sensor callback to read temperature8181+ * @get_trend: sensor callback to read temperature trend8282+ */8383+8484+struct __thermal_zone {8585+ enum thermal_device_mode mode;8686+ int passive_delay;8787+ int polling_delay;8888+8989+ /* trip data */9090+ int ntrips;9191+ struct __thermal_trip *trips;9292+9393+ /* cooling binding data */9494+ int num_tbps;9595+ struct __thermal_bind_params *tbps;9696+9797+ /* sensor interface */9898+ void *sensor_data;9999+ int (*get_temp)(void *, long *);100100+ int (*get_trend)(void *, long *);101101+};102102+103103+/*** DT thermal zone device callbacks ***/104104+105105+static int of_thermal_get_temp(struct thermal_zone_device *tz,106106+ unsigned long *temp)107107+{108108+ struct __thermal_zone *data = tz->devdata;109109+110110+ if (!data->get_temp)111111+ return -EINVAL;112112+113113+ return data->get_temp(data->sensor_data, temp);114114+}115115+116116+static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,117117+ enum thermal_trend *trend)118118+{119119+ struct __thermal_zone *data = tz->devdata;120120+ long dev_trend;121121+ int r;122122+123123+ if (!data->get_trend)124124+ return -EINVAL;125125+126126+ r = data->get_trend(data->sensor_data, &dev_trend);127127+ if (r)128128+ return r;129129+130130+ /* TODO: These intervals might have some thresholds, but in core code */131131+ if (dev_trend > 0)132132+ *trend = THERMAL_TREND_RAISING;133133+ else if (dev_trend < 0)134134+ *trend = THERMAL_TREND_DROPPING;135135+ else136136+ *trend = THERMAL_TREND_STABLE;137137+138138+ return 0;139139+}140140+141141+static int of_thermal_bind(struct thermal_zone_device *thermal,142142+ struct thermal_cooling_device *cdev)143143+{144144+ struct __thermal_zone *data = thermal->devdata;145145+ int i;146146+147147+ if (!data || IS_ERR(data))148148+ return -ENODEV;149149+150150+ /* find where to bind */151151+ for (i = 0; i < data->num_tbps; i++) {152152+ struct __thermal_bind_params *tbp = data->tbps + i;153153+154154+ if (tbp->cooling_device == cdev->np) {155155+ int ret;156156+157157+ ret = thermal_zone_bind_cooling_device(thermal,158158+ tbp->trip_id, cdev,159159+ tbp->min,160160+ tbp->max);161161+ if (ret)162162+ return ret;163163+ }164164+ }165165+166166+ return 0;167167+}168168+169169+static int of_thermal_unbind(struct thermal_zone_device *thermal,170170+ struct thermal_cooling_device *cdev)171171+{172172+ struct __thermal_zone *data = thermal->devdata;173173+ int i;174174+175175+ if (!data || IS_ERR(data))176176+ return -ENODEV;177177+178178+ /* find where to unbind */179179+ for (i = 0; i < data->num_tbps; i++) {180180+ struct __thermal_bind_params *tbp = data->tbps + i;181181+182182+ if (tbp->cooling_device == cdev->np) {183183+ int ret;184184+185185+ ret = thermal_zone_unbind_cooling_device(thermal,186186+ tbp->trip_id, cdev);187187+ if (ret)188188+ return ret;189189+ }190190+ }191191+192192+ return 0;193193+}194194+195195+static int of_thermal_get_mode(struct thermal_zone_device *tz,196196+ enum thermal_device_mode *mode)197197+{198198+ struct __thermal_zone *data = tz->devdata;199199+200200+ *mode = data->mode;201201+202202+ return 0;203203+}204204+205205+static int of_thermal_set_mode(struct thermal_zone_device *tz,206206+ enum thermal_device_mode mode)207207+{208208+ struct __thermal_zone *data = tz->devdata;209209+210210+ mutex_lock(&tz->lock);211211+212212+ if (mode == THERMAL_DEVICE_ENABLED)213213+ tz->polling_delay = data->polling_delay;214214+ else215215+ tz->polling_delay = 0;216216+217217+ mutex_unlock(&tz->lock);218218+219219+ data->mode = mode;220220+ thermal_zone_device_update(tz);221221+222222+ return 0;223223+}224224+225225+static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,226226+ enum thermal_trip_type *type)227227+{228228+ struct __thermal_zone *data = tz->devdata;229229+230230+ if (trip >= data->ntrips || trip < 0)231231+ return -EDOM;232232+233233+ *type = data->trips[trip].type;234234+235235+ return 0;236236+}237237+238238+static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,239239+ unsigned long *temp)240240+{241241+ struct __thermal_zone *data = tz->devdata;242242+243243+ if (trip >= data->ntrips || trip < 0)244244+ return -EDOM;245245+246246+ *temp = data->trips[trip].temperature;247247+248248+ return 0;249249+}250250+251251+static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,252252+ unsigned long temp)253253+{254254+ struct __thermal_zone *data = tz->devdata;255255+256256+ if (trip >= data->ntrips || trip < 0)257257+ return -EDOM;258258+259259+ /* thermal framework should take care of data->mask & (1 << trip) */260260+ data->trips[trip].temperature = temp;261261+262262+ return 0;263263+}264264+265265+static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,266266+ unsigned long *hyst)267267+{268268+ struct __thermal_zone *data = tz->devdata;269269+270270+ if (trip >= data->ntrips || trip < 0)271271+ return -EDOM;272272+273273+ *hyst = data->trips[trip].hysteresis;274274+275275+ return 0;276276+}277277+278278+static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,279279+ unsigned long hyst)280280+{281281+ struct __thermal_zone *data = tz->devdata;282282+283283+ if (trip >= data->ntrips || trip < 0)284284+ return -EDOM;285285+286286+ /* thermal framework should take care of data->mask & (1 << trip) */287287+ data->trips[trip].hysteresis = hyst;288288+289289+ return 0;290290+}291291+292292+static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,293293+ unsigned long *temp)294294+{295295+ struct __thermal_zone *data = tz->devdata;296296+ int i;297297+298298+ for (i = 0; i < data->ntrips; i++)299299+ if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {300300+ *temp = data->trips[i].temperature;301301+ return 0;302302+ }303303+304304+ return -EINVAL;305305+}306306+307307+static struct thermal_zone_device_ops of_thermal_ops = {308308+ .get_mode = of_thermal_get_mode,309309+ .set_mode = of_thermal_set_mode,310310+311311+ .get_trip_type = of_thermal_get_trip_type,312312+ .get_trip_temp = of_thermal_get_trip_temp,313313+ .set_trip_temp = of_thermal_set_trip_temp,314314+ .get_trip_hyst = of_thermal_get_trip_hyst,315315+ .set_trip_hyst = of_thermal_set_trip_hyst,316316+ .get_crit_temp = of_thermal_get_crit_temp,317317+318318+ .bind = of_thermal_bind,319319+ .unbind = of_thermal_unbind,320320+};321321+322322+/*** sensor API ***/323323+324324+static struct thermal_zone_device *325325+thermal_zone_of_add_sensor(struct device_node *zone,326326+ struct device_node *sensor, void *data,327327+ int (*get_temp)(void *, long *),328328+ int (*get_trend)(void *, long *))329329+{330330+ struct thermal_zone_device *tzd;331331+ struct __thermal_zone *tz;332332+333333+ tzd = thermal_zone_get_zone_by_name(zone->name);334334+ if (IS_ERR(tzd))335335+ return ERR_PTR(-EPROBE_DEFER);336336+337337+ tz = tzd->devdata;338338+339339+ mutex_lock(&tzd->lock);340340+ tz->get_temp = get_temp;341341+ tz->get_trend = get_trend;342342+ tz->sensor_data = data;343343+344344+ tzd->ops->get_temp = of_thermal_get_temp;345345+ tzd->ops->get_trend = of_thermal_get_trend;346346+ mutex_unlock(&tzd->lock);347347+348348+ return tzd;349349+}350350+351351+/**352352+ * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone353353+ * @dev: a valid struct device pointer of a sensor device. Must contain354354+ * a valid .of_node, for the sensor node.355355+ * @sensor_id: a sensor identifier, in case the sensor IP has more356356+ * than one sensors357357+ * @data: a private pointer (owned by the caller) that will be passed358358+ * back, when a temperature reading is needed.359359+ * @get_temp: a pointer to a function that reads the sensor temperature.360360+ * @get_trend: a pointer to a function that reads the sensor temperature trend.361361+ *362362+ * This function will search the list of thermal zones described in device363363+ * tree and look for the zone that refer to the sensor device pointed by364364+ * @dev->of_node as temperature providers. For the zone pointing to the365365+ * sensor node, the sensor will be added to the DT thermal zone device.366366+ *367367+ * The thermal zone temperature is provided by the @get_temp function368368+ * pointer. When called, it will have the private pointer @data back.369369+ *370370+ * The thermal zone temperature trend is provided by the @get_trend function371371+ * pointer. When called, it will have the private pointer @data back.372372+ *373373+ * TODO:374374+ * 01 - This function must enqueue the new sensor instead of using375375+ * it as the only source of temperature values.376376+ *377377+ * 02 - There must be a way to match the sensor with all thermal zones378378+ * that refer to it.379379+ *380380+ * Return: On success returns a valid struct thermal_zone_device,381381+ * otherwise, it returns a corresponding ERR_PTR(). Caller must382382+ * check the return value with help of IS_ERR() helper.383383+ */384384+struct thermal_zone_device *385385+thermal_zone_of_sensor_register(struct device *dev, int sensor_id,386386+ void *data, int (*get_temp)(void *, long *),387387+ int (*get_trend)(void *, long *))388388+{389389+ struct device_node *np, *child, *sensor_np;390390+391391+ np = of_find_node_by_name(NULL, "thermal-zones");392392+ if (!np)393393+ return ERR_PTR(-ENODEV);394394+395395+ if (!dev || !dev->of_node)396396+ return ERR_PTR(-EINVAL);397397+398398+ sensor_np = dev->of_node;399399+400400+ for_each_child_of_node(np, child) {401401+ struct of_phandle_args sensor_specs;402402+ int ret, id;403403+404404+ /* For now, thermal framework supports only 1 sensor per zone */405405+ ret = of_parse_phandle_with_args(child, "thermal-sensors",406406+ "#thermal-sensor-cells",407407+ 0, &sensor_specs);408408+ if (ret)409409+ continue;410410+411411+ if (sensor_specs.args_count >= 1) {412412+ id = sensor_specs.args[0];413413+ WARN(sensor_specs.args_count > 1,414414+ "%s: too many cells in sensor specifier %d\n",415415+ sensor_specs.np->name, sensor_specs.args_count);416416+ } else {417417+ id = 0;418418+ }419419+420420+ if (sensor_specs.np == sensor_np && id == sensor_id) {421421+ of_node_put(np);422422+ return thermal_zone_of_add_sensor(child, sensor_np,423423+ data,424424+ get_temp,425425+ get_trend);426426+ }427427+ }428428+ of_node_put(np);429429+430430+ return ERR_PTR(-ENODEV);431431+}432432+EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);433433+434434+/**435435+ * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone436436+ * @dev: a valid struct device pointer of a sensor device. Must contain437437+ * a valid .of_node, for the sensor node.438438+ * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.439439+ *440440+ * This function removes the sensor callbacks and private data from the441441+ * thermal zone device registered with thermal_zone_of_sensor_register()442442+ * API. It will also silent the zone by remove the .get_temp() and .get_trend()443443+ * thermal zone device callbacks.444444+ *445445+ * TODO: When the support to several sensors per zone is added, this446446+ * function must search the sensor list based on @dev parameter.447447+ *448448+ */449449+void thermal_zone_of_sensor_unregister(struct device *dev,450450+ struct thermal_zone_device *tzd)451451+{452452+ struct __thermal_zone *tz;453453+454454+ if (!dev || !tzd || !tzd->devdata)455455+ return;456456+457457+ tz = tzd->devdata;458458+459459+ /* no __thermal_zone, nothing to be done */460460+ if (!tz)461461+ return;462462+463463+ mutex_lock(&tzd->lock);464464+ tzd->ops->get_temp = NULL;465465+ tzd->ops->get_trend = NULL;466466+467467+ tz->get_temp = NULL;468468+ tz->get_trend = NULL;469469+ tz->sensor_data = NULL;470470+ mutex_unlock(&tzd->lock);471471+}472472+EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);473473+474474+/*** functions parsing device tree nodes ***/475475+476476+/**477477+ * thermal_of_populate_bind_params - parse and fill cooling map data478478+ * @np: DT node containing a cooling-map node479479+ * @__tbp: data structure to be filled with cooling map info480480+ * @trips: array of thermal zone trip points481481+ * @ntrips: number of trip points inside trips.482482+ *483483+ * This function parses a cooling-map type of node represented by484484+ * @np parameter and fills the read data into @__tbp data structure.485485+ * It needs the already parsed array of trip points of the thermal zone486486+ * in consideration.487487+ *488488+ * Return: 0 on success, proper error code otherwise489489+ */490490+static int thermal_of_populate_bind_params(struct device_node *np,491491+ struct __thermal_bind_params *__tbp,492492+ struct __thermal_trip *trips,493493+ int ntrips)494494+{495495+ struct of_phandle_args cooling_spec;496496+ struct device_node *trip;497497+ int ret, i;498498+ u32 prop;499499+500500+ /* Default weight. Usage is optional */501501+ __tbp->usage = 0;502502+ ret = of_property_read_u32(np, "contribution", &prop);503503+ if (ret == 0)504504+ __tbp->usage = prop;505505+506506+ trip = of_parse_phandle(np, "trip", 0);507507+ if (!trip) {508508+ pr_err("missing trip property\n");509509+ return -ENODEV;510510+ }511511+512512+ /* match using device_node */513513+ for (i = 0; i < ntrips; i++)514514+ if (trip == trips[i].np) {515515+ __tbp->trip_id = i;516516+ break;517517+ }518518+519519+ if (i == ntrips) {520520+ ret = -ENODEV;521521+ goto end;522522+ }523523+524524+ ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",525525+ 0, &cooling_spec);526526+ if (ret < 0) {527527+ pr_err("missing cooling_device property\n");528528+ goto end;529529+ }530530+ __tbp->cooling_device = cooling_spec.np;531531+ if (cooling_spec.args_count >= 2) { /* at least min and max */532532+ __tbp->min = cooling_spec.args[0];533533+ __tbp->max = cooling_spec.args[1];534534+ } else {535535+ pr_err("wrong reference to cooling device, missing limits\n");536536+ }537537+538538+end:539539+ of_node_put(trip);540540+541541+ return ret;542542+}543543+544544+/**545545+ * It maps 'enum thermal_trip_type' found in include/linux/thermal.h546546+ * into the device tree binding of 'trip', property type.547547+ */548548+static const char * const trip_types[] = {549549+ [THERMAL_TRIP_ACTIVE] = "active",550550+ [THERMAL_TRIP_PASSIVE] = "passive",551551+ [THERMAL_TRIP_HOT] = "hot",552552+ [THERMAL_TRIP_CRITICAL] = "critical",553553+};554554+555555+/**556556+ * thermal_of_get_trip_type - Get phy mode for given device_node557557+ * @np: Pointer to the given device_node558558+ * @type: Pointer to resulting trip type559559+ *560560+ * The function gets trip type string from property 'type',561561+ * and store its index in trip_types table in @type,562562+ *563563+ * Return: 0 on success, or errno in error case.564564+ */565565+static int thermal_of_get_trip_type(struct device_node *np,566566+ enum thermal_trip_type *type)567567+{568568+ const char *t;569569+ int err, i;570570+571571+ err = of_property_read_string(np, "type", &t);572572+ if (err < 0)573573+ return err;574574+575575+ for (i = 0; i < ARRAY_SIZE(trip_types); i++)576576+ if (!strcasecmp(t, trip_types[i])) {577577+ *type = i;578578+ return 0;579579+ }580580+581581+ return -ENODEV;582582+}583583+584584+/**585585+ * thermal_of_populate_trip - parse and fill one trip point data586586+ * @np: DT node containing a trip point node587587+ * @trip: trip point data structure to be filled up588588+ *589589+ * This function parses a trip point type of node represented by590590+ * @np parameter and fills the read data into @trip data structure.591591+ *592592+ * Return: 0 on success, proper error code otherwise593593+ */594594+static int thermal_of_populate_trip(struct device_node *np,595595+ struct __thermal_trip *trip)596596+{597597+ int prop;598598+ int ret;599599+600600+ ret = of_property_read_u32(np, "temperature", &prop);601601+ if (ret < 0) {602602+ pr_err("missing temperature property\n");603603+ return ret;604604+ }605605+ trip->temperature = prop;606606+607607+ ret = of_property_read_u32(np, "hysteresis", &prop);608608+ if (ret < 0) {609609+ pr_err("missing hysteresis property\n");610610+ return ret;611611+ }612612+ trip->hysteresis = prop;613613+614614+ ret = thermal_of_get_trip_type(np, &trip->type);615615+ if (ret < 0) {616616+ pr_err("wrong trip type property\n");617617+ return ret;618618+ }619619+620620+ /* Required for cooling map matching */621621+ trip->np = np;622622+623623+ return 0;624624+}625625+626626+/**627627+ * thermal_of_build_thermal_zone - parse and fill one thermal zone data628628+ * @np: DT node containing a thermal zone node629629+ *630630+ * This function parses a thermal zone type of node represented by631631+ * @np parameter and fills the read data into a __thermal_zone data structure632632+ * and return this pointer.633633+ *634634+ * TODO: Missing properties to parse: thermal-sensor-names and coefficients635635+ *636636+ * Return: On success returns a valid struct __thermal_zone,637637+ * otherwise, it returns a corresponding ERR_PTR(). Caller must638638+ * check the return value with help of IS_ERR() helper.639639+ */640640+static struct __thermal_zone *641641+thermal_of_build_thermal_zone(struct device_node *np)642642+{643643+ struct device_node *child = NULL, *gchild;644644+ struct __thermal_zone *tz;645645+ int ret, i;646646+ u32 prop;647647+648648+ if (!np) {649649+ pr_err("no thermal zone np\n");650650+ return ERR_PTR(-EINVAL);651651+ }652652+653653+ tz = kzalloc(sizeof(*tz), GFP_KERNEL);654654+ if (!tz)655655+ return ERR_PTR(-ENOMEM);656656+657657+ ret = of_property_read_u32(np, "polling-delay-passive", &prop);658658+ if (ret < 0) {659659+ pr_err("missing polling-delay-passive property\n");660660+ goto free_tz;661661+ }662662+ tz->passive_delay = prop;663663+664664+ ret = of_property_read_u32(np, "polling-delay", &prop);665665+ if (ret < 0) {666666+ pr_err("missing polling-delay property\n");667667+ goto free_tz;668668+ }669669+ tz->polling_delay = prop;670670+671671+ /* trips */672672+ child = of_get_child_by_name(np, "trips");673673+674674+ /* No trips provided */675675+ if (!child)676676+ goto finish;677677+678678+ tz->ntrips = of_get_child_count(child);679679+ if (tz->ntrips == 0) /* must have at least one child */680680+ goto finish;681681+682682+ tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);683683+ if (!tz->trips) {684684+ ret = -ENOMEM;685685+ goto free_tz;686686+ }687687+688688+ i = 0;689689+ for_each_child_of_node(child, gchild) {690690+ ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);691691+ if (ret)692692+ goto free_trips;693693+ }694694+695695+ of_node_put(child);696696+697697+ /* cooling-maps */698698+ child = of_get_child_by_name(np, "cooling-maps");699699+700700+ /* cooling-maps not provided */701701+ if (!child)702702+ goto finish;703703+704704+ tz->num_tbps = of_get_child_count(child);705705+ if (tz->num_tbps == 0)706706+ goto finish;707707+708708+ tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);709709+ if (!tz->tbps) {710710+ ret = -ENOMEM;711711+ goto free_trips;712712+ }713713+714714+ i = 0;715715+ for_each_child_of_node(child, gchild)716716+ ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],717717+ tz->trips, tz->ntrips);718718+ if (ret)719719+ goto free_tbps;720720+721721+finish:722722+ of_node_put(child);723723+ tz->mode = THERMAL_DEVICE_DISABLED;724724+725725+ return tz;726726+727727+free_tbps:728728+ kfree(tz->tbps);729729+free_trips:730730+ kfree(tz->trips);731731+free_tz:732732+ kfree(tz);733733+ of_node_put(child);734734+735735+ return ERR_PTR(ret);736736+}737737+738738+static inline void of_thermal_free_zone(struct __thermal_zone *tz)739739+{740740+ kfree(tz->tbps);741741+ kfree(tz->trips);742742+ kfree(tz);743743+}744744+745745+/**746746+ * of_parse_thermal_zones - parse device tree thermal data747747+ *748748+ * Initialization function that can be called by machine initialization749749+ * code to parse thermal data and populate the thermal framework750750+ * with hardware thermal zones info. This function only parses thermal zones.751751+ * Cooling devices and sensor devices nodes are supposed to be parsed752752+ * by their respective drivers.753753+ *754754+ * Return: 0 on success, proper error code otherwise755755+ *756756+ */757757+int __init of_parse_thermal_zones(void)758758+{759759+ struct device_node *np, *child;760760+ struct __thermal_zone *tz;761761+ struct thermal_zone_device_ops *ops;762762+763763+ np = of_find_node_by_name(NULL, "thermal-zones");764764+ if (!np) {765765+ pr_debug("unable to find thermal zones\n");766766+ return 0; /* Run successfully on systems without thermal DT */767767+ }768768+769769+ for_each_child_of_node(np, child) {770770+ struct thermal_zone_device *zone;771771+ struct thermal_zone_params *tzp;772772+773773+ tz = thermal_of_build_thermal_zone(child);774774+ if (IS_ERR(tz)) {775775+ pr_err("failed to build thermal zone %s: %ld\n",776776+ child->name,777777+ PTR_ERR(tz));778778+ continue;779779+ }780780+781781+ ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);782782+ if (!ops)783783+ goto exit_free;784784+785785+ tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);786786+ if (!tzp) {787787+ kfree(ops);788788+ goto exit_free;789789+ }790790+791791+ /* No hwmon because there might be hwmon drivers registering */792792+ tzp->no_hwmon = true;793793+794794+ zone = thermal_zone_device_register(child->name, tz->ntrips,795795+ 0, tz,796796+ ops, tzp,797797+ tz->passive_delay,798798+ tz->polling_delay);799799+ if (IS_ERR(zone)) {800800+ pr_err("Failed to build %s zone %ld\n", child->name,801801+ PTR_ERR(zone));802802+ kfree(tzp);803803+ kfree(ops);804804+ of_thermal_free_zone(tz);805805+ /* attempting to build remaining zones still */806806+ }807807+ }808808+809809+ return 0;810810+811811+exit_free:812812+ of_thermal_free_zone(tz);813813+814814+ /* no memory available, so free what we have built */815815+ of_thermal_destroy_zones();816816+817817+ return -ENOMEM;818818+}819819+820820+/**821821+ * of_thermal_destroy_zones - remove all zones parsed and allocated resources822822+ *823823+ * Finds all zones parsed and added to the thermal framework and remove them824824+ * from the system, together with their resources.825825+ *826826+ */827827+void of_thermal_destroy_zones(void)828828+{829829+ struct device_node *np, *child;830830+831831+ np = of_find_node_by_name(NULL, "thermal-zones");832832+ if (!np) {833833+ pr_err("unable to find thermal zones\n");834834+ return;835835+ }836836+837837+ for_each_child_of_node(np, child) {838838+ struct thermal_zone_device *zone;839839+840840+ zone = thermal_zone_get_zone_by_name(child->name);841841+ if (IS_ERR(zone))842842+ continue;843843+844844+ thermal_zone_device_unregister(zone);845845+ kfree(zone->tzp);846846+ kfree(zone->ops);847847+ of_thermal_free_zone(zone->devdata);848848+ }849849+}
···205205skip_calib_data:206206 if (pdata->max_trigger_level > MAX_THRESHOLD_LEVS) {207207 dev_err(&pdev->dev, "Invalid max trigger level\n");208208+ ret = -EINVAL;208209 goto out;209210 }210211
+70-7
drivers/thermal/thermal_core.c
···3434#include <linux/thermal.h>3535#include <linux/reboot.h>3636#include <linux/string.h>3737+#include <linux/of.h>3738#include <net/netlink.h>3839#include <net/genetlink.h>3940···404403 enum thermal_trip_type type;405404#endif406405407407- if (!tz || IS_ERR(tz))406406+ if (!tz || IS_ERR(tz) || !tz->ops->get_temp)408407 goto exit;409408410409 mutex_lock(&tz->lock);···459458void thermal_zone_device_update(struct thermal_zone_device *tz)460459{461460 int count;461461+462462+ if (!tz->ops->get_temp)463463+ return;462464463465 update_temperature(tz);464466···10591055};1060105610611057/**10621062- * thermal_cooling_device_register() - register a new thermal cooling device10581058+ * __thermal_cooling_device_register() - register a new thermal cooling device10591059+ * @np: a pointer to a device tree node.10631060 * @type: the thermal cooling device type.10641061 * @devdata: device private data.10651062 * @ops: standard thermal cooling devices callbacks.···10681063 * This interface function adds a new thermal cooling device (fan/processor/...)10691064 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself10701065 * to all the thermal zone devices registered at the same time.10661066+ * It also gives the opportunity to link the cooling device to a device tree10671067+ * node, so that it can be bound to a thermal zone created out of device tree.10711068 *10721069 * Return: a pointer to the created struct thermal_cooling_device or an10731070 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.10741071 */10751075-struct thermal_cooling_device *10761076-thermal_cooling_device_register(char *type, void *devdata,10771077- const struct thermal_cooling_device_ops *ops)10721072+static struct thermal_cooling_device *10731073+__thermal_cooling_device_register(struct device_node *np,10741074+ char *type, void *devdata,10751075+ const struct thermal_cooling_device_ops *ops)10781076{10791077 struct thermal_cooling_device *cdev;10801078 int result;···11021094 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));11031095 mutex_init(&cdev->lock);11041096 INIT_LIST_HEAD(&cdev->thermal_instances);10971097+ cdev->np = np;11051098 cdev->ops = ops;11061099 cdev->updated = true;11071100 cdev->device.class = &thermal_class;···11451136 device_unregister(&cdev->device);11461137 return ERR_PTR(result);11471138}11391139+11401140+/**11411141+ * thermal_cooling_device_register() - register a new thermal cooling device11421142+ * @type: the thermal cooling device type.11431143+ * @devdata: device private data.11441144+ * @ops: standard thermal cooling devices callbacks.11451145+ *11461146+ * This interface function adds a new thermal cooling device (fan/processor/...)11471147+ * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself11481148+ * to all the thermal zone devices registered at the same time.11491149+ *11501150+ * Return: a pointer to the created struct thermal_cooling_device or an11511151+ * ERR_PTR. Caller must check return value with IS_ERR*() helpers.11521152+ */11531153+struct thermal_cooling_device *11541154+thermal_cooling_device_register(char *type, void *devdata,11551155+ const struct thermal_cooling_device_ops *ops)11561156+{11571157+ return __thermal_cooling_device_register(NULL, type, devdata, ops);11581158+}11481159EXPORT_SYMBOL_GPL(thermal_cooling_device_register);11601160+11611161+/**11621162+ * thermal_of_cooling_device_register() - register an OF thermal cooling device11631163+ * @np: a pointer to a device tree node.11641164+ * @type: the thermal cooling device type.11651165+ * @devdata: device private data.11661166+ * @ops: standard thermal cooling devices callbacks.11671167+ *11681168+ * This function will register a cooling device with device tree node reference.11691169+ * This interface function adds a new thermal cooling device (fan/processor/...)11701170+ * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself11711171+ * to all the thermal zone devices registered at the same time.11721172+ *11731173+ * Return: a pointer to the created struct thermal_cooling_device or an11741174+ * ERR_PTR. Caller must check return value with IS_ERR*() helpers.11751175+ */11761176+struct thermal_cooling_device *11771177+thermal_of_cooling_device_register(struct device_node *np,11781178+ char *type, void *devdata,11791179+ const struct thermal_cooling_device_ops *ops)11801180+{11811181+ return __thermal_cooling_device_register(np, type, devdata, ops);11821182+}11831183+EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);1149118411501185/**11511186 * thermal_cooling_device_unregister - removes the registered thermal cooling device···14291376 */14301377struct thermal_zone_device *thermal_zone_device_register(const char *type,14311378 int trips, int mask, void *devdata,14321432- const struct thermal_zone_device_ops *ops,13791379+ struct thermal_zone_device_ops *ops,14331380 const struct thermal_zone_params *tzp,14341381 int passive_delay, int polling_delay)14351382{···14451392 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)14461393 return ERR_PTR(-EINVAL);1447139414481448- if (!ops || !ops->get_temp)13951395+ if (!ops)14491396 return ERR_PTR(-EINVAL);1450139714511398 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))···15481495 bind_tz(tz);1549149615501497 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);14981498+14991499+ if (!tz->ops->get_temp)15001500+ thermal_zone_device_set_polling(tz, 0);1551150115521502 thermal_zone_device_update(tz);15531503···18021746 if (result)18031747 goto unregister_class;1804174817491749+ result = of_parse_thermal_zones();17501750+ if (result)17511751+ goto exit_netlink;17521752+18051753 return 0;1806175417551755+exit_netlink:17561756+ genetlink_exit();18071757unregister_governors:18081758 thermal_unregister_governors();18091759unregister_class:···1825176318261764static void __exit thermal_exit(void)18271765{17661766+ of_thermal_destroy_zones();18281767 genetlink_exit();18291768 class_unregister(&thermal_class);18301769 thermal_unregister_governors();
+9
drivers/thermal/thermal_core.h
···7777static inline void thermal_gov_user_space_unregister(void) {}7878#endif /* CONFIG_THERMAL_GOV_USER_SPACE */79798080+/* device tree support */8181+#ifdef CONFIG_THERMAL_OF8282+int of_parse_thermal_zones(void);8383+void of_thermal_destroy_zones(void);8484+#else8585+static inline int of_parse_thermal_zones(void) { return 0; }8686+static inline void of_thermal_destroy_zones(void) { }8787+#endif8888+8089#endif /* __THERMAL_CORE_H__ */
···3131#include <linux/cpufreq.h>3232#include <linux/cpumask.h>3333#include <linux/cpu_cooling.h>3434+#include <linux/of.h>34353536#include "ti-thermal.h"3637#include "ti-bandgap.h"···4544 enum thermal_device_mode mode;4645 struct work_struct thermal_wq;4746 int sensor_id;4747+ bool our_zone;4848};49495050static void ti_thermal_work(struct work_struct *work)···77757876/* thermal zone ops */7977/* Get temperature callback function for thermal zone*/8080-static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,8181- unsigned long *temp)7878+static inline int __ti_thermal_get_temp(void *devdata, long *temp)8279{8380 struct thermal_zone_device *pcb_tz = NULL;8484- struct ti_thermal_data *data = thermal->devdata;8181+ struct ti_thermal_data *data = devdata;8582 struct ti_bandgap *bgp;8683 const struct ti_temp_sensor *s;8784 int ret, tmp, slope, constant;···117116 *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);118117119118 return ret;119119+}120120+121121+static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal,122122+ unsigned long *temp)123123+{124124+ struct ti_thermal_data *data = thermal->devdata;125125+126126+ return __ti_thermal_get_temp(data, temp);120127}121128122129/* Bind callback functions for thermal zone */···239230 return 0;240231}241232242242-/* Get the temperature trend callback functions for thermal zone */243243-static int ti_thermal_get_trend(struct thermal_zone_device *thermal,244244- int trip, enum thermal_trend *trend)233233+static int __ti_thermal_get_trend(void *p, long *trend)245234{246246- struct ti_thermal_data *data = thermal->devdata;235235+ struct ti_thermal_data *data = p;247236 struct ti_bandgap *bgp;248237 int id, tr, ret = 0;249238···249242 id = data->sensor_id;250243251244 ret = ti_bandgap_get_trend(bgp, id, &tr);245245+ if (ret)246246+ return ret;247247+248248+ *trend = tr;249249+250250+ return 0;251251+}252252+253253+/* Get the temperature trend callback functions for thermal zone */254254+static int ti_thermal_get_trend(struct thermal_zone_device *thermal,255255+ int trip, enum thermal_trend *trend)256256+{257257+ int ret;258258+ long tr;259259+260260+ ret = __ti_thermal_get_trend(thermal->devdata, &tr);252261 if (ret)253262 return ret;254263···331308 if (!data)332309 return -EINVAL;333310334334- /* Create thermal zone */335335- data->ti_thermal = thermal_zone_device_register(domain,311311+ /* in case this is specified by DT */312312+ data->ti_thermal = thermal_zone_of_sensor_register(bgp->dev, id,313313+ data, __ti_thermal_get_temp,314314+ __ti_thermal_get_trend);315315+ if (IS_ERR(data->ti_thermal)) {316316+ /* Create thermal zone */317317+ data->ti_thermal = thermal_zone_device_register(domain,336318 OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops,337319 NULL, FAST_TEMP_MONITORING_RATE,338320 FAST_TEMP_MONITORING_RATE);339339- if (IS_ERR(data->ti_thermal)) {340340- dev_err(bgp->dev, "thermal zone device is NULL\n");341341- return PTR_ERR(data->ti_thermal);321321+ if (IS_ERR(data->ti_thermal)) {322322+ dev_err(bgp->dev, "thermal zone device is NULL\n");323323+ return PTR_ERR(data->ti_thermal);324324+ }325325+ data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;326326+ data->our_zone = true;342327 }343343- data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE;344328 ti_bandgap_set_sensor_data(bgp, id, data);345329 ti_bandgap_write_update_interval(bgp, data->sensor_id,346330 data->ti_thermal->polling_delay);···361331362332 data = ti_bandgap_get_sensor_data(bgp, id);363333364364- thermal_zone_device_unregister(data->ti_thermal);334334+ if (data && data->ti_thermal) {335335+ if (data->our_zone)336336+ thermal_zone_device_unregister(data->ti_thermal);337337+ else338338+ thermal_zone_of_sensor_unregister(bgp->dev,339339+ data->ti_thermal);340340+ }365341366342 return 0;367343}···386350int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)387351{388352 struct ti_thermal_data *data;353353+ struct device_node *np = bgp->dev->of_node;354354+355355+ /*356356+ * We are assuming here that if one deploys the zone357357+ * using DT, then it must be aware that the cooling device358358+ * loading has to happen via cpufreq driver.359359+ */360360+ if (of_find_property(np, "#thermal-sensor-cells", NULL))361361+ return 0;389362390363 data = ti_bandgap_get_sensor_data(bgp, id);391364 if (!data || IS_ERR(data))···425380 struct ti_thermal_data *data;426381427382 data = ti_bandgap_get_sensor_data(bgp, id);428428- cpufreq_cooling_unregister(data->cool_dev);383383+384384+ if (data && data->cool_dev)385385+ cpufreq_cooling_unregister(data->cool_dev);429386430387 return 0;431388}
+1-1
drivers/thermal/x86_pkg_temp_thermal.c
···215215 return 0;216216}217217218218-int sys_set_trip_temp(struct thermal_zone_device *tzd, int trip,218218+static int sys_set_trip_temp(struct thermal_zone_device *tzd, int trip,219219 unsigned long temp)220220{221221 u32 l, h;
+17
include/dt-bindings/thermal/thermal.h
···11+/*22+ * This header provides constants for most thermal bindings.33+ *44+ * Copyright (C) 2013 Texas Instruments55+ * Eduardo Valentin <eduardo.valentin@ti.com>66+ *77+ * GPLv2 only88+ */99+1010+#ifndef _DT_BINDINGS_THERMAL_THERMAL_H1111+#define _DT_BINDINGS_THERMAL_THERMAL_H1212+1313+/* On cooling devices upper and lower limits */1414+#define THERMAL_NO_LIMIT (-1UL)1515+1616+#endif1717+
+25
include/linux/cpu_cooling.h
···2424#ifndef __CPU_COOLING_H__2525#define __CPU_COOLING_H__26262727+#include <linux/of.h>2728#include <linux/thermal.h>2829#include <linux/cpumask.h>2930···3736cpufreq_cooling_register(const struct cpumask *clip_cpus);38373938/**3939+ * of_cpufreq_cooling_register - create cpufreq cooling device based on DT.4040+ * @np: a valid struct device_node to the cooling device device tree node.4141+ * @clip_cpus: cpumask of cpus where the frequency constraints will happen4242+ */4343+#ifdef CONFIG_THERMAL_OF4444+struct thermal_cooling_device *4545+of_cpufreq_cooling_register(struct device_node *np,4646+ const struct cpumask *clip_cpus);4747+#else4848+static inline struct thermal_cooling_device *4949+of_cpufreq_cooling_register(struct device_node *np,5050+ const struct cpumask *clip_cpus)5151+{5252+ return NULL;5353+}5454+#endif5555+5656+/**4057 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.4158 * @cdev: thermal cooling device pointer.4259 */···6445#else /* !CONFIG_CPU_THERMAL */6546static inline struct thermal_cooling_device *6647cpufreq_cooling_register(const struct cpumask *clip_cpus)4848+{4949+ return NULL;5050+}5151+static inline struct thermal_cooling_device *5252+of_cpufreq_cooling_register(struct device_node *np,5353+ const struct cpumask *clip_cpus)6754{6855 return NULL;6956}
+30-2
include/linux/thermal.h
···2525#ifndef __THERMAL_H__2626#define __THERMAL_H__27272828+#include <linux/of.h>2829#include <linux/idr.h>2930#include <linux/device.h>3031#include <linux/workqueue.h>···144143 int id;145144 char type[THERMAL_NAME_LENGTH];146145 struct device device;146146+ struct device_node *np;147147 void *devdata;148148 const struct thermal_cooling_device_ops *ops;149149 bool updated; /* true if the cooling device does not need update */···174172 int emul_temperature;175173 int passive;176174 unsigned int forced_passive;177177- const struct thermal_zone_device_ops *ops;175175+ struct thermal_zone_device_ops *ops;178176 const struct thermal_zone_params *tzp;179177 struct thermal_governor *governor;180178 struct list_head thermal_instances;···244242};245243246244/* Function declarations */245245+#ifdef CONFIG_THERMAL_OF246246+struct thermal_zone_device *247247+thermal_zone_of_sensor_register(struct device *dev, int id,248248+ void *data, int (*get_temp)(void *, long *),249249+ int (*get_trend)(void *, long *));250250+void thermal_zone_of_sensor_unregister(struct device *dev,251251+ struct thermal_zone_device *tz);252252+#else253253+static inline struct thermal_zone_device *254254+thermal_zone_of_sensor_register(struct device *dev, int id,255255+ void *data, int (*get_temp)(void *, long *),256256+ int (*get_trend)(void *, long *))257257+{258258+ return NULL;259259+}260260+261261+static inline262262+void thermal_zone_of_sensor_unregister(struct device *dev,263263+ struct thermal_zone_device *tz)264264+{265265+}266266+267267+#endif247268struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,248248- void *, const struct thermal_zone_device_ops *,269269+ void *, struct thermal_zone_device_ops *,249270 const struct thermal_zone_params *, int, int);250271void thermal_zone_device_unregister(struct thermal_zone_device *);251272···281256282257struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,283258 const struct thermal_cooling_device_ops *);259259+struct thermal_cooling_device *260260+thermal_of_cooling_device_register(struct device_node *np, char *, void *,261261+ const struct thermal_cooling_device_ops *);284262void thermal_cooling_device_unregister(struct thermal_cooling_device *);285263struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);286264int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp);