1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Thermal device driver for DA9062 and DA9061
4*4882a593Smuzhiyun * Copyright (C) 2017 Dialog Semiconductor
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun /* When over-temperature is reached, an interrupt from the device will be
8*4882a593Smuzhiyun * triggered. Following this event the interrupt will be disabled and
9*4882a593Smuzhiyun * periodic transmission of uevents (HOT trip point) should define the
10*4882a593Smuzhiyun * first level of temperature supervision. It is expected that any final
11*4882a593Smuzhiyun * implementation of the thermal driver will include a .notify() function
12*4882a593Smuzhiyun * to implement these uevents to userspace.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * These uevents are intended to indicate non-invasive temperature control
15*4882a593Smuzhiyun * of the system, where the necessary measures for cooling are the
16*4882a593Smuzhiyun * responsibility of the host software. Once the temperature falls again,
17*4882a593Smuzhiyun * the IRQ is re-enabled so the start of a new over-temperature event can
18*4882a593Smuzhiyun * be detected without constant software monitoring.
19*4882a593Smuzhiyun */
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include <linux/errno.h>
22*4882a593Smuzhiyun #include <linux/interrupt.h>
23*4882a593Smuzhiyun #include <linux/module.h>
24*4882a593Smuzhiyun #include <linux/of.h>
25*4882a593Smuzhiyun #include <linux/platform_device.h>
26*4882a593Smuzhiyun #include <linux/regmap.h>
27*4882a593Smuzhiyun #include <linux/thermal.h>
28*4882a593Smuzhiyun #include <linux/workqueue.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include <linux/mfd/da9062/core.h>
31*4882a593Smuzhiyun #include <linux/mfd/da9062/registers.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /* Minimum, maximum and default polling millisecond periods are provided
34*4882a593Smuzhiyun * here as an example. It is expected that any final implementation to also
35*4882a593Smuzhiyun * include a modification of these settings to match the required
36*4882a593Smuzhiyun * application.
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun #define DA9062_DEFAULT_POLLING_MS_PERIOD 3000
39*4882a593Smuzhiyun #define DA9062_MAX_POLLING_MS_PERIOD 10000
40*4882a593Smuzhiyun #define DA9062_MIN_POLLING_MS_PERIOD 1000
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define DA9062_MILLI_CELSIUS(t) ((t) * 1000)
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun struct da9062_thermal_config {
45*4882a593Smuzhiyun const char *name;
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun struct da9062_thermal {
49*4882a593Smuzhiyun struct da9062 *hw;
50*4882a593Smuzhiyun struct delayed_work work;
51*4882a593Smuzhiyun struct thermal_zone_device *zone;
52*4882a593Smuzhiyun struct mutex lock; /* protection for da9062_thermal temperature */
53*4882a593Smuzhiyun int temperature;
54*4882a593Smuzhiyun int irq;
55*4882a593Smuzhiyun const struct da9062_thermal_config *config;
56*4882a593Smuzhiyun struct device *dev;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun
da9062_thermal_poll_on(struct work_struct * work)59*4882a593Smuzhiyun static void da9062_thermal_poll_on(struct work_struct *work)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun struct da9062_thermal *thermal = container_of(work,
62*4882a593Smuzhiyun struct da9062_thermal,
63*4882a593Smuzhiyun work.work);
64*4882a593Smuzhiyun unsigned long delay;
65*4882a593Smuzhiyun unsigned int val;
66*4882a593Smuzhiyun int ret;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* clear E_TEMP */
69*4882a593Smuzhiyun ret = regmap_write(thermal->hw->regmap,
70*4882a593Smuzhiyun DA9062AA_EVENT_B,
71*4882a593Smuzhiyun DA9062AA_E_TEMP_MASK);
72*4882a593Smuzhiyun if (ret < 0) {
73*4882a593Smuzhiyun dev_err(thermal->dev,
74*4882a593Smuzhiyun "Cannot clear the TJUNC temperature status\n");
75*4882a593Smuzhiyun goto err_enable_irq;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /* Now read E_TEMP again: it is acting like a status bit.
79*4882a593Smuzhiyun * If over-temperature, then this status will be true.
80*4882a593Smuzhiyun * If not over-temperature, this status will be false.
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun ret = regmap_read(thermal->hw->regmap,
83*4882a593Smuzhiyun DA9062AA_EVENT_B,
84*4882a593Smuzhiyun &val);
85*4882a593Smuzhiyun if (ret < 0) {
86*4882a593Smuzhiyun dev_err(thermal->dev,
87*4882a593Smuzhiyun "Cannot check the TJUNC temperature status\n");
88*4882a593Smuzhiyun goto err_enable_irq;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun if (val & DA9062AA_E_TEMP_MASK) {
92*4882a593Smuzhiyun mutex_lock(&thermal->lock);
93*4882a593Smuzhiyun thermal->temperature = DA9062_MILLI_CELSIUS(125);
94*4882a593Smuzhiyun mutex_unlock(&thermal->lock);
95*4882a593Smuzhiyun thermal_zone_device_update(thermal->zone,
96*4882a593Smuzhiyun THERMAL_EVENT_UNSPECIFIED);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun delay = msecs_to_jiffies(thermal->zone->passive_delay);
99*4882a593Smuzhiyun queue_delayed_work(system_freezable_wq, &thermal->work, delay);
100*4882a593Smuzhiyun return;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun mutex_lock(&thermal->lock);
104*4882a593Smuzhiyun thermal->temperature = DA9062_MILLI_CELSIUS(0);
105*4882a593Smuzhiyun mutex_unlock(&thermal->lock);
106*4882a593Smuzhiyun thermal_zone_device_update(thermal->zone,
107*4882a593Smuzhiyun THERMAL_EVENT_UNSPECIFIED);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun err_enable_irq:
110*4882a593Smuzhiyun enable_irq(thermal->irq);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
da9062_thermal_irq_handler(int irq,void * data)113*4882a593Smuzhiyun static irqreturn_t da9062_thermal_irq_handler(int irq, void *data)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun struct da9062_thermal *thermal = data;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun disable_irq_nosync(thermal->irq);
118*4882a593Smuzhiyun queue_delayed_work(system_freezable_wq, &thermal->work, 0);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun return IRQ_HANDLED;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
da9062_thermal_get_trip_type(struct thermal_zone_device * z,int trip,enum thermal_trip_type * type)123*4882a593Smuzhiyun static int da9062_thermal_get_trip_type(struct thermal_zone_device *z,
124*4882a593Smuzhiyun int trip,
125*4882a593Smuzhiyun enum thermal_trip_type *type)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun struct da9062_thermal *thermal = z->devdata;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun switch (trip) {
130*4882a593Smuzhiyun case 0:
131*4882a593Smuzhiyun *type = THERMAL_TRIP_HOT;
132*4882a593Smuzhiyun break;
133*4882a593Smuzhiyun default:
134*4882a593Smuzhiyun dev_err(thermal->dev,
135*4882a593Smuzhiyun "Driver does not support more than 1 trip-wire\n");
136*4882a593Smuzhiyun return -EINVAL;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun return 0;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
da9062_thermal_get_trip_temp(struct thermal_zone_device * z,int trip,int * temp)142*4882a593Smuzhiyun static int da9062_thermal_get_trip_temp(struct thermal_zone_device *z,
143*4882a593Smuzhiyun int trip,
144*4882a593Smuzhiyun int *temp)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun struct da9062_thermal *thermal = z->devdata;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun switch (trip) {
149*4882a593Smuzhiyun case 0:
150*4882a593Smuzhiyun *temp = DA9062_MILLI_CELSIUS(125);
151*4882a593Smuzhiyun break;
152*4882a593Smuzhiyun default:
153*4882a593Smuzhiyun dev_err(thermal->dev,
154*4882a593Smuzhiyun "Driver does not support more than 1 trip-wire\n");
155*4882a593Smuzhiyun return -EINVAL;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun return 0;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
da9062_thermal_get_temp(struct thermal_zone_device * z,int * temp)161*4882a593Smuzhiyun static int da9062_thermal_get_temp(struct thermal_zone_device *z,
162*4882a593Smuzhiyun int *temp)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun struct da9062_thermal *thermal = z->devdata;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun mutex_lock(&thermal->lock);
167*4882a593Smuzhiyun *temp = thermal->temperature;
168*4882a593Smuzhiyun mutex_unlock(&thermal->lock);
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun return 0;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun static struct thermal_zone_device_ops da9062_thermal_ops = {
174*4882a593Smuzhiyun .get_temp = da9062_thermal_get_temp,
175*4882a593Smuzhiyun .get_trip_type = da9062_thermal_get_trip_type,
176*4882a593Smuzhiyun .get_trip_temp = da9062_thermal_get_trip_temp,
177*4882a593Smuzhiyun };
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun static const struct da9062_thermal_config da9062_config = {
180*4882a593Smuzhiyun .name = "da9062-thermal",
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun static const struct of_device_id da9062_compatible_reg_id_table[] = {
184*4882a593Smuzhiyun { .compatible = "dlg,da9062-thermal", .data = &da9062_config },
185*4882a593Smuzhiyun { },
186*4882a593Smuzhiyun };
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, da9062_compatible_reg_id_table);
189*4882a593Smuzhiyun
da9062_thermal_probe(struct platform_device * pdev)190*4882a593Smuzhiyun static int da9062_thermal_probe(struct platform_device *pdev)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
193*4882a593Smuzhiyun struct da9062_thermal *thermal;
194*4882a593Smuzhiyun unsigned int pp_tmp = DA9062_DEFAULT_POLLING_MS_PERIOD;
195*4882a593Smuzhiyun const struct of_device_id *match;
196*4882a593Smuzhiyun int ret = 0;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun match = of_match_node(da9062_compatible_reg_id_table,
199*4882a593Smuzhiyun pdev->dev.of_node);
200*4882a593Smuzhiyun if (!match)
201*4882a593Smuzhiyun return -ENXIO;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (pdev->dev.of_node) {
204*4882a593Smuzhiyun if (!of_property_read_u32(pdev->dev.of_node,
205*4882a593Smuzhiyun "polling-delay-passive",
206*4882a593Smuzhiyun &pp_tmp)) {
207*4882a593Smuzhiyun if (pp_tmp < DA9062_MIN_POLLING_MS_PERIOD ||
208*4882a593Smuzhiyun pp_tmp > DA9062_MAX_POLLING_MS_PERIOD) {
209*4882a593Smuzhiyun dev_warn(&pdev->dev,
210*4882a593Smuzhiyun "Out-of-range polling period %d ms\n",
211*4882a593Smuzhiyun pp_tmp);
212*4882a593Smuzhiyun pp_tmp = DA9062_DEFAULT_POLLING_MS_PERIOD;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun thermal = devm_kzalloc(&pdev->dev, sizeof(struct da9062_thermal),
218*4882a593Smuzhiyun GFP_KERNEL);
219*4882a593Smuzhiyun if (!thermal) {
220*4882a593Smuzhiyun ret = -ENOMEM;
221*4882a593Smuzhiyun goto err;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun thermal->config = match->data;
225*4882a593Smuzhiyun thermal->hw = chip;
226*4882a593Smuzhiyun thermal->dev = &pdev->dev;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun INIT_DELAYED_WORK(&thermal->work, da9062_thermal_poll_on);
229*4882a593Smuzhiyun mutex_init(&thermal->lock);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun thermal->zone = thermal_zone_device_register(thermal->config->name,
232*4882a593Smuzhiyun 1, 0, thermal,
233*4882a593Smuzhiyun &da9062_thermal_ops, NULL, pp_tmp,
234*4882a593Smuzhiyun 0);
235*4882a593Smuzhiyun if (IS_ERR(thermal->zone)) {
236*4882a593Smuzhiyun dev_err(&pdev->dev, "Cannot register thermal zone device\n");
237*4882a593Smuzhiyun ret = PTR_ERR(thermal->zone);
238*4882a593Smuzhiyun goto err;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun ret = thermal_zone_device_enable(thermal->zone);
241*4882a593Smuzhiyun if (ret) {
242*4882a593Smuzhiyun dev_err(&pdev->dev, "Cannot enable thermal zone device\n");
243*4882a593Smuzhiyun goto err_zone;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun dev_dbg(&pdev->dev,
247*4882a593Smuzhiyun "TJUNC temperature polling period set at %d ms\n",
248*4882a593Smuzhiyun thermal->zone->passive_delay);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun ret = platform_get_irq_byname(pdev, "THERMAL");
251*4882a593Smuzhiyun if (ret < 0) {
252*4882a593Smuzhiyun dev_err(&pdev->dev, "Failed to get platform IRQ.\n");
253*4882a593Smuzhiyun goto err_zone;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun thermal->irq = ret;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun ret = request_threaded_irq(thermal->irq, NULL,
258*4882a593Smuzhiyun da9062_thermal_irq_handler,
259*4882a593Smuzhiyun IRQF_TRIGGER_LOW | IRQF_ONESHOT,
260*4882a593Smuzhiyun "THERMAL", thermal);
261*4882a593Smuzhiyun if (ret) {
262*4882a593Smuzhiyun dev_err(&pdev->dev,
263*4882a593Smuzhiyun "Failed to request thermal device IRQ.\n");
264*4882a593Smuzhiyun goto err_zone;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun platform_set_drvdata(pdev, thermal);
268*4882a593Smuzhiyun return 0;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun err_zone:
271*4882a593Smuzhiyun thermal_zone_device_unregister(thermal->zone);
272*4882a593Smuzhiyun err:
273*4882a593Smuzhiyun return ret;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
da9062_thermal_remove(struct platform_device * pdev)276*4882a593Smuzhiyun static int da9062_thermal_remove(struct platform_device *pdev)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun struct da9062_thermal *thermal = platform_get_drvdata(pdev);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun free_irq(thermal->irq, thermal);
281*4882a593Smuzhiyun cancel_delayed_work_sync(&thermal->work);
282*4882a593Smuzhiyun thermal_zone_device_unregister(thermal->zone);
283*4882a593Smuzhiyun return 0;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun static struct platform_driver da9062_thermal_driver = {
287*4882a593Smuzhiyun .probe = da9062_thermal_probe,
288*4882a593Smuzhiyun .remove = da9062_thermal_remove,
289*4882a593Smuzhiyun .driver = {
290*4882a593Smuzhiyun .name = "da9062-thermal",
291*4882a593Smuzhiyun .of_match_table = da9062_compatible_reg_id_table,
292*4882a593Smuzhiyun },
293*4882a593Smuzhiyun };
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun module_platform_driver(da9062_thermal_driver);
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun MODULE_AUTHOR("Steve Twiss");
298*4882a593Smuzhiyun MODULE_DESCRIPTION("Thermal TJUNC device driver for Dialog DA9062 and DA9061");
299*4882a593Smuzhiyun MODULE_LICENSE("GPL");
300*4882a593Smuzhiyun MODULE_ALIAS("platform:da9062-thermal");
301