Linux Audio

Check our new training course

Loading...
v5.9
 
  1/*
  2 * Hisilicon thermal sensor driver
  3 *
  4 * Copyright (c) 2014-2015 Hisilicon Limited.
  5 * Copyright (c) 2014-2015 Linaro Limited.
  6 *
  7 * Xinwei Kong <kong.kongxinwei@hisilicon.com>
  8 * Leo Yan <leo.yan@linaro.org>
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License version 2 as
 12 * published by the Free Software Foundation.
 13 *
 14 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 15 * kind, whether express or implied; without even the implied warranty
 16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17 * GNU General Public License for more details.
 18 */
 19
 20#include <linux/cpufreq.h>
 21#include <linux/delay.h>
 22#include <linux/interrupt.h>
 23#include <linux/module.h>
 24#include <linux/platform_device.h>
 25#include <linux/io.h>
 26#include <linux/of_device.h>
 27
 28#include "thermal_core.h"
 29
 30#define HI6220_TEMP0_LAG			(0x0)
 31#define HI6220_TEMP0_TH				(0x4)
 32#define HI6220_TEMP0_RST_TH			(0x8)
 33#define HI6220_TEMP0_CFG			(0xC)
 34#define HI6220_TEMP0_CFG_SS_MSK			(0xF000)
 35#define HI6220_TEMP0_CFG_HDAK_MSK		(0x30)
 36#define HI6220_TEMP0_EN				(0x10)
 37#define HI6220_TEMP0_INT_EN			(0x14)
 38#define HI6220_TEMP0_INT_CLR			(0x18)
 39#define HI6220_TEMP0_RST_MSK			(0x1C)
 40#define HI6220_TEMP0_VALUE			(0x28)
 41
 42#define HI3660_OFFSET(chan)		((chan) * 0x40)
 43#define HI3660_TEMP(chan)		(HI3660_OFFSET(chan) + 0x1C)
 44#define HI3660_TH(chan)			(HI3660_OFFSET(chan) + 0x20)
 45#define HI3660_LAG(chan)		(HI3660_OFFSET(chan) + 0x28)
 46#define HI3660_INT_EN(chan)		(HI3660_OFFSET(chan) + 0x2C)
 47#define HI3660_INT_CLR(chan)		(HI3660_OFFSET(chan) + 0x30)
 48
 49#define HI6220_TEMP_BASE			(-60000)
 50#define HI6220_TEMP_RESET			(100000)
 51#define HI6220_TEMP_STEP			(785)
 52#define HI6220_TEMP_LAG				(3500)
 53
 54#define HI3660_TEMP_BASE		(-63780)
 55#define HI3660_TEMP_STEP		(205)
 56#define HI3660_TEMP_LAG			(4000)
 57
 58#define HI6220_CLUSTER0_SENSOR		2
 59#define HI6220_CLUSTER1_SENSOR		1
 60
 61#define HI3660_LITTLE_SENSOR		0
 62#define HI3660_BIG_SENSOR		1
 63#define HI3660_G3D_SENSOR		2
 64#define HI3660_MODEM_SENSOR		3
 65
 66struct hisi_thermal_data;
 67
 68struct hisi_thermal_sensor {
 69	struct hisi_thermal_data *data;
 70	struct thermal_zone_device *tzd;
 71	const char *irq_name;
 72	uint32_t id;
 73	uint32_t thres_temp;
 74};
 75
 76struct hisi_thermal_ops {
 77	int (*get_temp)(struct hisi_thermal_sensor *sensor);
 78	int (*enable_sensor)(struct hisi_thermal_sensor *sensor);
 79	int (*disable_sensor)(struct hisi_thermal_sensor *sensor);
 80	int (*irq_handler)(struct hisi_thermal_sensor *sensor);
 81	int (*probe)(struct hisi_thermal_data *data);
 82};
 83
 84struct hisi_thermal_data {
 85	const struct hisi_thermal_ops *ops;
 86	struct hisi_thermal_sensor *sensor;
 87	struct platform_device *pdev;
 88	struct clk *clk;
 89	void __iomem *regs;
 90	int nr_sensors;
 91};
 92
 93/*
 94 * The temperature computation on the tsensor is as follow:
 95 *	Unit: millidegree Celsius
 96 *	Step: 200/255 (0.7843)
 97 *	Temperature base: -60°C
 98 *
 99 * The register is programmed in temperature steps, every step is 785
100 * millidegree and begins at -60 000 m°C
101 *
102 * The temperature from the steps:
103 *
104 *	Temp = TempBase + (steps x 785)
105 *
106 * and the steps from the temperature:
107 *
108 *	steps = (Temp - TempBase) / 785
109 *
110 */
111static inline int hi6220_thermal_step_to_temp(int step)
112{
113	return HI6220_TEMP_BASE + (step * HI6220_TEMP_STEP);
114}
115
116static inline int hi6220_thermal_temp_to_step(int temp)
117{
118	return DIV_ROUND_UP(temp - HI6220_TEMP_BASE, HI6220_TEMP_STEP);
119}
120
121/*
122 * for Hi3660,
123 *	Step: 189/922 (0.205)
124 *	Temperature base: -63.780°C
125 *
126 * The register is programmed in temperature steps, every step is 205
127 * millidegree and begins at -63 780 m°C
128 */
129static inline int hi3660_thermal_step_to_temp(int step)
130{
131	return HI3660_TEMP_BASE + step * HI3660_TEMP_STEP;
132}
133
134static inline int hi3660_thermal_temp_to_step(int temp)
135{
136	return DIV_ROUND_UP(temp - HI3660_TEMP_BASE, HI3660_TEMP_STEP);
137}
138
139/*
140 * The lag register contains 5 bits encoding the temperature in steps.
141 *
142 * Each time the temperature crosses the threshold boundary, an
143 * interrupt is raised. It could be when the temperature is going
144 * above the threshold or below. However, if the temperature is
145 * fluctuating around this value due to the load, we can receive
146 * several interrupts which may not desired.
147 *
148 * We can setup a temperature representing the delta between the
149 * threshold and the current temperature when the temperature is
150 * decreasing.
151 *
152 * For instance: the lag register is 5°C, the threshold is 65°C, when
153 * the temperature reaches 65°C an interrupt is raised and when the
154 * temperature decrease to 65°C - 5°C another interrupt is raised.
155 *
156 * A very short lag can lead to an interrupt storm, a long lag
157 * increase the latency to react to the temperature changes.  In our
158 * case, that is not really a problem as we are polling the
159 * temperature.
160 *
161 * [0:4] : lag register
162 *
163 * The temperature is coded in steps, cf. HI6220_TEMP_STEP.
164 *
165 * Min : 0x00 :  0.0 °C
166 * Max : 0x1F : 24.3 °C
167 *
168 * The 'value' parameter is in milliCelsius.
169 */
170static inline void hi6220_thermal_set_lag(void __iomem *addr, int value)
171{
172	writel(DIV_ROUND_UP(value, HI6220_TEMP_STEP) & 0x1F,
173			addr + HI6220_TEMP0_LAG);
174}
175
176static inline void hi6220_thermal_alarm_clear(void __iomem *addr, int value)
177{
178	writel(value, addr + HI6220_TEMP0_INT_CLR);
179}
180
181static inline void hi6220_thermal_alarm_enable(void __iomem *addr, int value)
182{
183	writel(value, addr + HI6220_TEMP0_INT_EN);
184}
185
186static inline void hi6220_thermal_alarm_set(void __iomem *addr, int temp)
187{
188	writel(hi6220_thermal_temp_to_step(temp) | 0x0FFFFFF00,
189	       addr + HI6220_TEMP0_TH);
190}
191
192static inline void hi6220_thermal_reset_set(void __iomem *addr, int temp)
193{
194	writel(hi6220_thermal_temp_to_step(temp), addr + HI6220_TEMP0_RST_TH);
195}
196
197static inline void hi6220_thermal_reset_enable(void __iomem *addr, int value)
198{
199	writel(value, addr + HI6220_TEMP0_RST_MSK);
200}
201
202static inline void hi6220_thermal_enable(void __iomem *addr, int value)
203{
204	writel(value, addr + HI6220_TEMP0_EN);
205}
206
207static inline int hi6220_thermal_get_temperature(void __iomem *addr)
208{
209	return hi6220_thermal_step_to_temp(readl(addr + HI6220_TEMP0_VALUE));
210}
211
212/*
213 * [0:6] lag register
214 *
215 * The temperature is coded in steps, cf. HI3660_TEMP_STEP.
216 *
217 * Min : 0x00 :  0.0 °C
218 * Max : 0x7F : 26.0 °C
219 *
220 */
221static inline void hi3660_thermal_set_lag(void __iomem *addr,
222					  int id, int value)
223{
224	writel(DIV_ROUND_UP(value, HI3660_TEMP_STEP) & 0x7F,
225			addr + HI3660_LAG(id));
226}
227
228static inline void hi3660_thermal_alarm_clear(void __iomem *addr,
229					      int id, int value)
230{
231	writel(value, addr + HI3660_INT_CLR(id));
232}
233
234static inline void hi3660_thermal_alarm_enable(void __iomem *addr,
235					       int id, int value)
236{
237	writel(value, addr + HI3660_INT_EN(id));
238}
239
240static inline void hi3660_thermal_alarm_set(void __iomem *addr,
241					    int id, int value)
242{
243	writel(value, addr + HI3660_TH(id));
244}
245
246static inline int hi3660_thermal_get_temperature(void __iomem *addr, int id)
247{
248	return hi3660_thermal_step_to_temp(readl(addr + HI3660_TEMP(id)));
249}
250
251/*
252 * Temperature configuration register - Sensor selection
253 *
254 * Bits [19:12]
255 *
256 * 0x0: local sensor (default)
257 * 0x1: remote sensor 1 (ACPU cluster 1)
258 * 0x2: remote sensor 2 (ACPU cluster 0)
259 * 0x3: remote sensor 3 (G3D)
260 */
261static inline void hi6220_thermal_sensor_select(void __iomem *addr, int sensor)
262{
263	writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_SS_MSK) |
264	       (sensor << 12), addr + HI6220_TEMP0_CFG);
265}
266
267/*
268 * Temperature configuration register - Hdak conversion polling interval
269 *
270 * Bits [5:4]
271 *
272 * 0x0 :   0.768 ms
273 * 0x1 :   6.144 ms
274 * 0x2 :  49.152 ms
275 * 0x3 : 393.216 ms
276 */
277static inline void hi6220_thermal_hdak_set(void __iomem *addr, int value)
278{
279	writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_HDAK_MSK) |
280	       (value << 4), addr + HI6220_TEMP0_CFG);
281}
282
283static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
284{
285	struct hisi_thermal_data *data = sensor->data;
286
287	hi6220_thermal_alarm_clear(data->regs, 1);
288	return 0;
289}
290
291static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
292{
293	struct hisi_thermal_data *data = sensor->data;
294
295	hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
296	return 0;
297}
298
299static int hi6220_thermal_get_temp(struct hisi_thermal_sensor *sensor)
300{
301	struct hisi_thermal_data *data = sensor->data;
302
303	return hi6220_thermal_get_temperature(data->regs);
304}
305
306static int hi3660_thermal_get_temp(struct hisi_thermal_sensor *sensor)
307{
308	struct hisi_thermal_data *data = sensor->data;
309
310	return hi3660_thermal_get_temperature(data->regs, sensor->id);
311}
312
313static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
314{
315	struct hisi_thermal_data *data = sensor->data;
316
317	/* disable sensor module */
318	hi6220_thermal_enable(data->regs, 0);
319	hi6220_thermal_alarm_enable(data->regs, 0);
320	hi6220_thermal_reset_enable(data->regs, 0);
321
322	clk_disable_unprepare(data->clk);
323
324	return 0;
325}
326
327static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
328{
329	struct hisi_thermal_data *data = sensor->data;
330
331	/* disable sensor module */
332	hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
333	return 0;
334}
335
336static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
337{
338	struct hisi_thermal_data *data = sensor->data;
339	int ret;
340
341	/* enable clock for tsensor */
342	ret = clk_prepare_enable(data->clk);
343	if (ret)
344		return ret;
345
346	/* disable module firstly */
347	hi6220_thermal_reset_enable(data->regs, 0);
348	hi6220_thermal_enable(data->regs, 0);
349
350	/* select sensor id */
351	hi6220_thermal_sensor_select(data->regs, sensor->id);
352
353	/* setting the hdak time */
354	hi6220_thermal_hdak_set(data->regs, 0);
355
356	/* setting lag value between current temp and the threshold */
357	hi6220_thermal_set_lag(data->regs, HI6220_TEMP_LAG);
358
359	/* enable for interrupt */
360	hi6220_thermal_alarm_set(data->regs, sensor->thres_temp);
361
362	hi6220_thermal_reset_set(data->regs, HI6220_TEMP_RESET);
363
364	/* enable module */
365	hi6220_thermal_reset_enable(data->regs, 1);
366	hi6220_thermal_enable(data->regs, 1);
367
368	hi6220_thermal_alarm_clear(data->regs, 0);
369	hi6220_thermal_alarm_enable(data->regs, 1);
370
371	return 0;
372}
373
374static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
375{
376	unsigned int value;
377	struct hisi_thermal_data *data = sensor->data;
378
379	/* disable interrupt */
380	hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
381
382	/* setting lag value between current temp and the threshold */
383	hi3660_thermal_set_lag(data->regs, sensor->id, HI3660_TEMP_LAG);
384
385	/* set interrupt threshold */
386	value = hi3660_thermal_temp_to_step(sensor->thres_temp);
387	hi3660_thermal_alarm_set(data->regs, sensor->id, value);
388
389	/* enable interrupt */
390	hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
391	hi3660_thermal_alarm_enable(data->regs, sensor->id, 1);
392
393	return 0;
394}
395
396static int hi6220_thermal_probe(struct hisi_thermal_data *data)
397{
398	struct platform_device *pdev = data->pdev;
399	struct device *dev = &pdev->dev;
400	int ret;
401
402	data->clk = devm_clk_get(dev, "thermal_clk");
403	if (IS_ERR(data->clk)) {
404		ret = PTR_ERR(data->clk);
405		if (ret != -EPROBE_DEFER)
406			dev_err(dev, "failed to get thermal clk: %d\n", ret);
407		return ret;
408	}
409
410	data->sensor = devm_kzalloc(dev, sizeof(*data->sensor), GFP_KERNEL);
411	if (!data->sensor)
412		return -ENOMEM;
413
414	data->sensor[0].id = HI6220_CLUSTER0_SENSOR;
415	data->sensor[0].irq_name = "tsensor_intr";
416	data->sensor[0].data = data;
417	data->nr_sensors = 1;
418
419	return 0;
420}
421
422static int hi3660_thermal_probe(struct hisi_thermal_data *data)
423{
424	struct platform_device *pdev = data->pdev;
425	struct device *dev = &pdev->dev;
426
427	data->nr_sensors = 1;
428
429	data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) *
430				    data->nr_sensors, GFP_KERNEL);
431	if (!data->sensor)
432		return -ENOMEM;
433
434	data->sensor[0].id = HI3660_BIG_SENSOR;
435	data->sensor[0].irq_name = "tsensor_a73";
436	data->sensor[0].data = data;
437
438	data->sensor[1].id = HI3660_LITTLE_SENSOR;
439	data->sensor[1].irq_name = "tsensor_a53";
440	data->sensor[1].data = data;
441
442	return 0;
443}
444
445static int hisi_thermal_get_temp(void *__data, int *temp)
446{
447	struct hisi_thermal_sensor *sensor = __data;
448	struct hisi_thermal_data *data = sensor->data;
449
450	*temp = data->ops->get_temp(sensor);
451
452	dev_dbg(&data->pdev->dev, "tzd=%p, id=%d, temp=%d, thres=%d\n",
453		sensor->tzd, sensor->id, *temp, sensor->thres_temp);
454
455	return 0;
456}
457
458static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
459	.get_temp = hisi_thermal_get_temp,
460};
461
462static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
463{
464	struct hisi_thermal_sensor *sensor = dev;
465	struct hisi_thermal_data *data = sensor->data;
466	int temp = 0;
467
468	data->ops->irq_handler(sensor);
469
470	hisi_thermal_get_temp(sensor, &temp);
471
472	if (temp >= sensor->thres_temp) {
473		dev_crit(&data->pdev->dev,
474			 "sensor <%d> THERMAL ALARM: %d > %d\n",
475			 sensor->id, temp, sensor->thres_temp);
476
477		thermal_zone_device_update(sensor->tzd,
478					   THERMAL_EVENT_UNSPECIFIED);
479
480	} else {
481		dev_crit(&data->pdev->dev,
482			 "sensor <%d> THERMAL ALARM stopped: %d < %d\n",
483			 sensor->id, temp, sensor->thres_temp);
484	}
485
486	return IRQ_HANDLED;
487}
488
489static int hisi_thermal_register_sensor(struct platform_device *pdev,
490					struct hisi_thermal_sensor *sensor)
491{
492	int ret, i;
493	const struct thermal_trip *trip;
494
495	sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
496							   sensor->id, sensor,
497							   &hisi_of_thermal_ops);
498	if (IS_ERR(sensor->tzd)) {
499		ret = PTR_ERR(sensor->tzd);
500		sensor->tzd = NULL;
501		dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
502			sensor->id, ret);
503		return ret;
504	}
505
506	trip = of_thermal_get_trip_points(sensor->tzd);
507
508	for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
509		if (trip[i].type == THERMAL_TRIP_PASSIVE) {
510			sensor->thres_temp = trip[i].temperature;
511			break;
512		}
513	}
514
515	return 0;
516}
517
518static const struct hisi_thermal_ops hi6220_ops = {
519	.get_temp	= hi6220_thermal_get_temp,
520	.enable_sensor	= hi6220_thermal_enable_sensor,
521	.disable_sensor	= hi6220_thermal_disable_sensor,
522	.irq_handler	= hi6220_thermal_irq_handler,
523	.probe		= hi6220_thermal_probe,
524};
525
526static const struct hisi_thermal_ops hi3660_ops = {
527	.get_temp	= hi3660_thermal_get_temp,
528	.enable_sensor	= hi3660_thermal_enable_sensor,
529	.disable_sensor	= hi3660_thermal_disable_sensor,
530	.irq_handler	= hi3660_thermal_irq_handler,
531	.probe		= hi3660_thermal_probe,
532};
533
534static const struct of_device_id of_hisi_thermal_match[] = {
535	{
536		.compatible = "hisilicon,tsensor",
537		.data = &hi6220_ops,
538	},
539	{
540		.compatible = "hisilicon,hi3660-tsensor",
541		.data = &hi3660_ops,
542	},
543	{ /* end */ }
544};
545MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
546
547static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
548				       bool on)
549{
550	struct thermal_zone_device *tzd = sensor->tzd;
551
552	if (on)
553		thermal_zone_device_enable(tzd);
554	else
555		thermal_zone_device_disable(tzd);
556}
557
558static int hisi_thermal_probe(struct platform_device *pdev)
559{
560	struct hisi_thermal_data *data;
561	struct device *dev = &pdev->dev;
562	struct resource *res;
563	int i, ret;
564
565	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
566	if (!data)
567		return -ENOMEM;
568
569	data->pdev = pdev;
570	platform_set_drvdata(pdev, data);
571	data->ops = of_device_get_match_data(dev);
572
573	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
574	data->regs = devm_ioremap_resource(dev, res);
575	if (IS_ERR(data->regs)) {
576		dev_err(dev, "failed to get io address\n");
577		return PTR_ERR(data->regs);
578	}
579
580	ret = data->ops->probe(data);
581	if (ret)
582		return ret;
583
584	for (i = 0; i < data->nr_sensors; i++) {
585		struct hisi_thermal_sensor *sensor = &data->sensor[i];
586
587		ret = hisi_thermal_register_sensor(pdev, sensor);
588		if (ret) {
589			dev_err(dev, "failed to register thermal sensor: %d\n",
590				ret);
591			return ret;
592		}
593
594		ret = platform_get_irq(pdev, 0);
595		if (ret < 0)
596			return ret;
597
598		ret = devm_request_threaded_irq(dev, ret, NULL,
599						hisi_thermal_alarm_irq_thread,
600						IRQF_ONESHOT, sensor->irq_name,
601						sensor);
602		if (ret < 0) {
603			dev_err(dev, "Failed to request alarm irq: %d\n", ret);
604			return ret;
605		}
606
607		ret = data->ops->enable_sensor(sensor);
608		if (ret) {
609			dev_err(dev, "Failed to setup the sensor: %d\n", ret);
610			return ret;
611		}
612
613		hisi_thermal_toggle_sensor(sensor, true);
614	}
615
616	return 0;
617}
618
619static int hisi_thermal_remove(struct platform_device *pdev)
620{
621	struct hisi_thermal_data *data = platform_get_drvdata(pdev);
622	int i;
623
624	for (i = 0; i < data->nr_sensors; i++) {
625		struct hisi_thermal_sensor *sensor = &data->sensor[i];
626
627		hisi_thermal_toggle_sensor(sensor, false);
628		data->ops->disable_sensor(sensor);
629	}
630
631	return 0;
632}
633
634#ifdef CONFIG_PM_SLEEP
635static int hisi_thermal_suspend(struct device *dev)
636{
637	struct hisi_thermal_data *data = dev_get_drvdata(dev);
638	int i;
639
640	for (i = 0; i < data->nr_sensors; i++)
641		data->ops->disable_sensor(&data->sensor[i]);
642
643	return 0;
644}
645
646static int hisi_thermal_resume(struct device *dev)
647{
648	struct hisi_thermal_data *data = dev_get_drvdata(dev);
649	int i, ret = 0;
650
651	for (i = 0; i < data->nr_sensors; i++)
652		ret |= data->ops->enable_sensor(&data->sensor[i]);
653
654	return ret;
655}
656#endif
657
658static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
659			 hisi_thermal_suspend, hisi_thermal_resume);
660
661static struct platform_driver hisi_thermal_driver = {
662	.driver = {
663		.name		= "hisi_thermal",
664		.pm		= &hisi_thermal_pm_ops,
665		.of_match_table = of_hisi_thermal_match,
666	},
667	.probe	= hisi_thermal_probe,
668	.remove	= hisi_thermal_remove,
669};
670
671module_platform_driver(hisi_thermal_driver);
672
673MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
674MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
675MODULE_DESCRIPTION("Hisilicon thermal driver");
676MODULE_LICENSE("GPL v2");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * HiSilicon thermal sensor driver
  4 *
  5 * Copyright (c) 2014-2015 HiSilicon Limited.
  6 * Copyright (c) 2014-2015 Linaro Limited.
  7 *
  8 * Xinwei Kong <kong.kongxinwei@hisilicon.com>
  9 * Leo Yan <leo.yan@linaro.org>
 
 
 
 
 
 
 
 
 
 10 */
 11
 12#include <linux/cpufreq.h>
 13#include <linux/delay.h>
 14#include <linux/interrupt.h>
 15#include <linux/module.h>
 16#include <linux/platform_device.h>
 17#include <linux/io.h>
 18#include <linux/of_device.h>
 19
 20#include "thermal_core.h"
 21
 22#define HI6220_TEMP0_LAG			(0x0)
 23#define HI6220_TEMP0_TH				(0x4)
 24#define HI6220_TEMP0_RST_TH			(0x8)
 25#define HI6220_TEMP0_CFG			(0xC)
 26#define HI6220_TEMP0_CFG_SS_MSK			(0xF000)
 27#define HI6220_TEMP0_CFG_HDAK_MSK		(0x30)
 28#define HI6220_TEMP0_EN				(0x10)
 29#define HI6220_TEMP0_INT_EN			(0x14)
 30#define HI6220_TEMP0_INT_CLR			(0x18)
 31#define HI6220_TEMP0_RST_MSK			(0x1C)
 32#define HI6220_TEMP0_VALUE			(0x28)
 33
 34#define HI3660_OFFSET(chan)		((chan) * 0x40)
 35#define HI3660_TEMP(chan)		(HI3660_OFFSET(chan) + 0x1C)
 36#define HI3660_TH(chan)			(HI3660_OFFSET(chan) + 0x20)
 37#define HI3660_LAG(chan)		(HI3660_OFFSET(chan) + 0x28)
 38#define HI3660_INT_EN(chan)		(HI3660_OFFSET(chan) + 0x2C)
 39#define HI3660_INT_CLR(chan)		(HI3660_OFFSET(chan) + 0x30)
 40
 41#define HI6220_TEMP_BASE			(-60000)
 42#define HI6220_TEMP_RESET			(100000)
 43#define HI6220_TEMP_STEP			(785)
 44#define HI6220_TEMP_LAG				(3500)
 45
 46#define HI3660_TEMP_BASE		(-63780)
 47#define HI3660_TEMP_STEP		(205)
 48#define HI3660_TEMP_LAG			(4000)
 49
 50#define HI6220_CLUSTER0_SENSOR		2
 51#define HI6220_CLUSTER1_SENSOR		1
 52
 53#define HI3660_LITTLE_SENSOR		0
 54#define HI3660_BIG_SENSOR		1
 55#define HI3660_G3D_SENSOR		2
 56#define HI3660_MODEM_SENSOR		3
 57
 58struct hisi_thermal_data;
 59
 60struct hisi_thermal_sensor {
 61	struct hisi_thermal_data *data;
 62	struct thermal_zone_device *tzd;
 63	const char *irq_name;
 64	uint32_t id;
 65	uint32_t thres_temp;
 66};
 67
 68struct hisi_thermal_ops {
 69	int (*get_temp)(struct hisi_thermal_sensor *sensor);
 70	int (*enable_sensor)(struct hisi_thermal_sensor *sensor);
 71	int (*disable_sensor)(struct hisi_thermal_sensor *sensor);
 72	int (*irq_handler)(struct hisi_thermal_sensor *sensor);
 73	int (*probe)(struct hisi_thermal_data *data);
 74};
 75
 76struct hisi_thermal_data {
 77	const struct hisi_thermal_ops *ops;
 78	struct hisi_thermal_sensor *sensor;
 79	struct platform_device *pdev;
 80	struct clk *clk;
 81	void __iomem *regs;
 82	int nr_sensors;
 83};
 84
 85/*
 86 * The temperature computation on the tsensor is as follow:
 87 *	Unit: millidegree Celsius
 88 *	Step: 200/255 (0.7843)
 89 *	Temperature base: -60°C
 90 *
 91 * The register is programmed in temperature steps, every step is 785
 92 * millidegree and begins at -60 000 m°C
 93 *
 94 * The temperature from the steps:
 95 *
 96 *	Temp = TempBase + (steps x 785)
 97 *
 98 * and the steps from the temperature:
 99 *
100 *	steps = (Temp - TempBase) / 785
101 *
102 */
103static inline int hi6220_thermal_step_to_temp(int step)
104{
105	return HI6220_TEMP_BASE + (step * HI6220_TEMP_STEP);
106}
107
108static inline int hi6220_thermal_temp_to_step(int temp)
109{
110	return DIV_ROUND_UP(temp - HI6220_TEMP_BASE, HI6220_TEMP_STEP);
111}
112
113/*
114 * for Hi3660,
115 *	Step: 189/922 (0.205)
116 *	Temperature base: -63.780°C
117 *
118 * The register is programmed in temperature steps, every step is 205
119 * millidegree and begins at -63 780 m°C
120 */
121static inline int hi3660_thermal_step_to_temp(int step)
122{
123	return HI3660_TEMP_BASE + step * HI3660_TEMP_STEP;
124}
125
126static inline int hi3660_thermal_temp_to_step(int temp)
127{
128	return DIV_ROUND_UP(temp - HI3660_TEMP_BASE, HI3660_TEMP_STEP);
129}
130
131/*
132 * The lag register contains 5 bits encoding the temperature in steps.
133 *
134 * Each time the temperature crosses the threshold boundary, an
135 * interrupt is raised. It could be when the temperature is going
136 * above the threshold or below. However, if the temperature is
137 * fluctuating around this value due to the load, we can receive
138 * several interrupts which may not desired.
139 *
140 * We can setup a temperature representing the delta between the
141 * threshold and the current temperature when the temperature is
142 * decreasing.
143 *
144 * For instance: the lag register is 5°C, the threshold is 65°C, when
145 * the temperature reaches 65°C an interrupt is raised and when the
146 * temperature decrease to 65°C - 5°C another interrupt is raised.
147 *
148 * A very short lag can lead to an interrupt storm, a long lag
149 * increase the latency to react to the temperature changes.  In our
150 * case, that is not really a problem as we are polling the
151 * temperature.
152 *
153 * [0:4] : lag register
154 *
155 * The temperature is coded in steps, cf. HI6220_TEMP_STEP.
156 *
157 * Min : 0x00 :  0.0 °C
158 * Max : 0x1F : 24.3 °C
159 *
160 * The 'value' parameter is in milliCelsius.
161 */
162static inline void hi6220_thermal_set_lag(void __iomem *addr, int value)
163{
164	writel(DIV_ROUND_UP(value, HI6220_TEMP_STEP) & 0x1F,
165			addr + HI6220_TEMP0_LAG);
166}
167
168static inline void hi6220_thermal_alarm_clear(void __iomem *addr, int value)
169{
170	writel(value, addr + HI6220_TEMP0_INT_CLR);
171}
172
173static inline void hi6220_thermal_alarm_enable(void __iomem *addr, int value)
174{
175	writel(value, addr + HI6220_TEMP0_INT_EN);
176}
177
178static inline void hi6220_thermal_alarm_set(void __iomem *addr, int temp)
179{
180	writel(hi6220_thermal_temp_to_step(temp) | 0x0FFFFFF00,
181	       addr + HI6220_TEMP0_TH);
182}
183
184static inline void hi6220_thermal_reset_set(void __iomem *addr, int temp)
185{
186	writel(hi6220_thermal_temp_to_step(temp), addr + HI6220_TEMP0_RST_TH);
187}
188
189static inline void hi6220_thermal_reset_enable(void __iomem *addr, int value)
190{
191	writel(value, addr + HI6220_TEMP0_RST_MSK);
192}
193
194static inline void hi6220_thermal_enable(void __iomem *addr, int value)
195{
196	writel(value, addr + HI6220_TEMP0_EN);
197}
198
199static inline int hi6220_thermal_get_temperature(void __iomem *addr)
200{
201	return hi6220_thermal_step_to_temp(readl(addr + HI6220_TEMP0_VALUE));
202}
203
204/*
205 * [0:6] lag register
206 *
207 * The temperature is coded in steps, cf. HI3660_TEMP_STEP.
208 *
209 * Min : 0x00 :  0.0 °C
210 * Max : 0x7F : 26.0 °C
211 *
212 */
213static inline void hi3660_thermal_set_lag(void __iomem *addr,
214					  int id, int value)
215{
216	writel(DIV_ROUND_UP(value, HI3660_TEMP_STEP) & 0x7F,
217			addr + HI3660_LAG(id));
218}
219
220static inline void hi3660_thermal_alarm_clear(void __iomem *addr,
221					      int id, int value)
222{
223	writel(value, addr + HI3660_INT_CLR(id));
224}
225
226static inline void hi3660_thermal_alarm_enable(void __iomem *addr,
227					       int id, int value)
228{
229	writel(value, addr + HI3660_INT_EN(id));
230}
231
232static inline void hi3660_thermal_alarm_set(void __iomem *addr,
233					    int id, int value)
234{
235	writel(value, addr + HI3660_TH(id));
236}
237
238static inline int hi3660_thermal_get_temperature(void __iomem *addr, int id)
239{
240	return hi3660_thermal_step_to_temp(readl(addr + HI3660_TEMP(id)));
241}
242
243/*
244 * Temperature configuration register - Sensor selection
245 *
246 * Bits [19:12]
247 *
248 * 0x0: local sensor (default)
249 * 0x1: remote sensor 1 (ACPU cluster 1)
250 * 0x2: remote sensor 2 (ACPU cluster 0)
251 * 0x3: remote sensor 3 (G3D)
252 */
253static inline void hi6220_thermal_sensor_select(void __iomem *addr, int sensor)
254{
255	writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_SS_MSK) |
256	       (sensor << 12), addr + HI6220_TEMP0_CFG);
257}
258
259/*
260 * Temperature configuration register - Hdak conversion polling interval
261 *
262 * Bits [5:4]
263 *
264 * 0x0 :   0.768 ms
265 * 0x1 :   6.144 ms
266 * 0x2 :  49.152 ms
267 * 0x3 : 393.216 ms
268 */
269static inline void hi6220_thermal_hdak_set(void __iomem *addr, int value)
270{
271	writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_HDAK_MSK) |
272	       (value << 4), addr + HI6220_TEMP0_CFG);
273}
274
275static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
276{
277	struct hisi_thermal_data *data = sensor->data;
278
279	hi6220_thermal_alarm_clear(data->regs, 1);
280	return 0;
281}
282
283static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
284{
285	struct hisi_thermal_data *data = sensor->data;
286
287	hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
288	return 0;
289}
290
291static int hi6220_thermal_get_temp(struct hisi_thermal_sensor *sensor)
292{
293	struct hisi_thermal_data *data = sensor->data;
294
295	return hi6220_thermal_get_temperature(data->regs);
296}
297
298static int hi3660_thermal_get_temp(struct hisi_thermal_sensor *sensor)
299{
300	struct hisi_thermal_data *data = sensor->data;
301
302	return hi3660_thermal_get_temperature(data->regs, sensor->id);
303}
304
305static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
306{
307	struct hisi_thermal_data *data = sensor->data;
308
309	/* disable sensor module */
310	hi6220_thermal_enable(data->regs, 0);
311	hi6220_thermal_alarm_enable(data->regs, 0);
312	hi6220_thermal_reset_enable(data->regs, 0);
313
314	clk_disable_unprepare(data->clk);
315
316	return 0;
317}
318
319static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
320{
321	struct hisi_thermal_data *data = sensor->data;
322
323	/* disable sensor module */
324	hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
325	return 0;
326}
327
328static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
329{
330	struct hisi_thermal_data *data = sensor->data;
331	int ret;
332
333	/* enable clock for tsensor */
334	ret = clk_prepare_enable(data->clk);
335	if (ret)
336		return ret;
337
338	/* disable module firstly */
339	hi6220_thermal_reset_enable(data->regs, 0);
340	hi6220_thermal_enable(data->regs, 0);
341
342	/* select sensor id */
343	hi6220_thermal_sensor_select(data->regs, sensor->id);
344
345	/* setting the hdak time */
346	hi6220_thermal_hdak_set(data->regs, 0);
347
348	/* setting lag value between current temp and the threshold */
349	hi6220_thermal_set_lag(data->regs, HI6220_TEMP_LAG);
350
351	/* enable for interrupt */
352	hi6220_thermal_alarm_set(data->regs, sensor->thres_temp);
353
354	hi6220_thermal_reset_set(data->regs, HI6220_TEMP_RESET);
355
356	/* enable module */
357	hi6220_thermal_reset_enable(data->regs, 1);
358	hi6220_thermal_enable(data->regs, 1);
359
360	hi6220_thermal_alarm_clear(data->regs, 0);
361	hi6220_thermal_alarm_enable(data->regs, 1);
362
363	return 0;
364}
365
366static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
367{
368	unsigned int value;
369	struct hisi_thermal_data *data = sensor->data;
370
371	/* disable interrupt */
372	hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
373
374	/* setting lag value between current temp and the threshold */
375	hi3660_thermal_set_lag(data->regs, sensor->id, HI3660_TEMP_LAG);
376
377	/* set interrupt threshold */
378	value = hi3660_thermal_temp_to_step(sensor->thres_temp);
379	hi3660_thermal_alarm_set(data->regs, sensor->id, value);
380
381	/* enable interrupt */
382	hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
383	hi3660_thermal_alarm_enable(data->regs, sensor->id, 1);
384
385	return 0;
386}
387
388static int hi6220_thermal_probe(struct hisi_thermal_data *data)
389{
390	struct platform_device *pdev = data->pdev;
391	struct device *dev = &pdev->dev;
392	int ret;
393
394	data->clk = devm_clk_get(dev, "thermal_clk");
395	if (IS_ERR(data->clk)) {
396		ret = PTR_ERR(data->clk);
397		if (ret != -EPROBE_DEFER)
398			dev_err(dev, "failed to get thermal clk: %d\n", ret);
399		return ret;
400	}
401
402	data->sensor = devm_kzalloc(dev, sizeof(*data->sensor), GFP_KERNEL);
403	if (!data->sensor)
404		return -ENOMEM;
405
406	data->sensor[0].id = HI6220_CLUSTER0_SENSOR;
407	data->sensor[0].irq_name = "tsensor_intr";
408	data->sensor[0].data = data;
409	data->nr_sensors = 1;
410
411	return 0;
412}
413
414static int hi3660_thermal_probe(struct hisi_thermal_data *data)
415{
416	struct platform_device *pdev = data->pdev;
417	struct device *dev = &pdev->dev;
418
419	data->nr_sensors = 1;
420
421	data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) *
422				    data->nr_sensors, GFP_KERNEL);
423	if (!data->sensor)
424		return -ENOMEM;
425
426	data->sensor[0].id = HI3660_BIG_SENSOR;
427	data->sensor[0].irq_name = "tsensor_a73";
428	data->sensor[0].data = data;
429
430	data->sensor[1].id = HI3660_LITTLE_SENSOR;
431	data->sensor[1].irq_name = "tsensor_a53";
432	data->sensor[1].data = data;
433
434	return 0;
435}
436
437static int hisi_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
438{
439	struct hisi_thermal_sensor *sensor = tz->devdata;
440	struct hisi_thermal_data *data = sensor->data;
441
442	*temp = data->ops->get_temp(sensor);
443
444	dev_dbg(&data->pdev->dev, "tzd=%p, id=%d, temp=%d, thres=%d\n",
445		sensor->tzd, sensor->id, *temp, sensor->thres_temp);
446
447	return 0;
448}
449
450static const struct thermal_zone_device_ops hisi_of_thermal_ops = {
451	.get_temp = hisi_thermal_get_temp,
452};
453
454static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
455{
456	struct hisi_thermal_sensor *sensor = dev;
457	struct hisi_thermal_data *data = sensor->data;
458	int temp = 0;
459
460	data->ops->irq_handler(sensor);
461
462	temp = data->ops->get_temp(sensor);
463
464	if (temp >= sensor->thres_temp) {
465		dev_crit(&data->pdev->dev,
466			 "sensor <%d> THERMAL ALARM: %d > %d\n",
467			 sensor->id, temp, sensor->thres_temp);
468
469		thermal_zone_device_update(sensor->tzd,
470					   THERMAL_EVENT_UNSPECIFIED);
471
472	} else {
473		dev_crit(&data->pdev->dev,
474			 "sensor <%d> THERMAL ALARM stopped: %d < %d\n",
475			 sensor->id, temp, sensor->thres_temp);
476	}
477
478	return IRQ_HANDLED;
479}
480
481static int hisi_thermal_register_sensor(struct platform_device *pdev,
482					struct hisi_thermal_sensor *sensor)
483{
484	int ret, i;
485	const struct thermal_trip *trip;
486
487	sensor->tzd = devm_thermal_of_zone_register(&pdev->dev,
488						    sensor->id, sensor,
489						    &hisi_of_thermal_ops);
490	if (IS_ERR(sensor->tzd)) {
491		ret = PTR_ERR(sensor->tzd);
492		sensor->tzd = NULL;
493		dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
494			sensor->id, ret);
495		return ret;
496	}
497
498	trip = of_thermal_get_trip_points(sensor->tzd);
499
500	for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
501		if (trip[i].type == THERMAL_TRIP_PASSIVE) {
502			sensor->thres_temp = trip[i].temperature;
503			break;
504		}
505	}
506
507	return 0;
508}
509
510static const struct hisi_thermal_ops hi6220_ops = {
511	.get_temp	= hi6220_thermal_get_temp,
512	.enable_sensor	= hi6220_thermal_enable_sensor,
513	.disable_sensor	= hi6220_thermal_disable_sensor,
514	.irq_handler	= hi6220_thermal_irq_handler,
515	.probe		= hi6220_thermal_probe,
516};
517
518static const struct hisi_thermal_ops hi3660_ops = {
519	.get_temp	= hi3660_thermal_get_temp,
520	.enable_sensor	= hi3660_thermal_enable_sensor,
521	.disable_sensor	= hi3660_thermal_disable_sensor,
522	.irq_handler	= hi3660_thermal_irq_handler,
523	.probe		= hi3660_thermal_probe,
524};
525
526static const struct of_device_id of_hisi_thermal_match[] = {
527	{
528		.compatible = "hisilicon,tsensor",
529		.data = &hi6220_ops,
530	},
531	{
532		.compatible = "hisilicon,hi3660-tsensor",
533		.data = &hi3660_ops,
534	},
535	{ /* end */ }
536};
537MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
538
539static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
540				       bool on)
541{
542	struct thermal_zone_device *tzd = sensor->tzd;
543
544	if (on)
545		thermal_zone_device_enable(tzd);
546	else
547		thermal_zone_device_disable(tzd);
548}
549
550static int hisi_thermal_probe(struct platform_device *pdev)
551{
552	struct hisi_thermal_data *data;
553	struct device *dev = &pdev->dev;
554	struct resource *res;
555	int i, ret;
556
557	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
558	if (!data)
559		return -ENOMEM;
560
561	data->pdev = pdev;
562	platform_set_drvdata(pdev, data);
563	data->ops = of_device_get_match_data(dev);
564
565	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
566	data->regs = devm_ioremap_resource(dev, res);
567	if (IS_ERR(data->regs))
 
568		return PTR_ERR(data->regs);
 
569
570	ret = data->ops->probe(data);
571	if (ret)
572		return ret;
573
574	for (i = 0; i < data->nr_sensors; i++) {
575		struct hisi_thermal_sensor *sensor = &data->sensor[i];
576
577		ret = hisi_thermal_register_sensor(pdev, sensor);
578		if (ret) {
579			dev_err(dev, "failed to register thermal sensor: %d\n",
580				ret);
581			return ret;
582		}
583
584		ret = platform_get_irq(pdev, 0);
585		if (ret < 0)
586			return ret;
587
588		ret = devm_request_threaded_irq(dev, ret, NULL,
589						hisi_thermal_alarm_irq_thread,
590						IRQF_ONESHOT, sensor->irq_name,
591						sensor);
592		if (ret < 0) {
593			dev_err(dev, "Failed to request alarm irq: %d\n", ret);
594			return ret;
595		}
596
597		ret = data->ops->enable_sensor(sensor);
598		if (ret) {
599			dev_err(dev, "Failed to setup the sensor: %d\n", ret);
600			return ret;
601		}
602
603		hisi_thermal_toggle_sensor(sensor, true);
604	}
605
606	return 0;
607}
608
609static int hisi_thermal_remove(struct platform_device *pdev)
610{
611	struct hisi_thermal_data *data = platform_get_drvdata(pdev);
612	int i;
613
614	for (i = 0; i < data->nr_sensors; i++) {
615		struct hisi_thermal_sensor *sensor = &data->sensor[i];
616
617		hisi_thermal_toggle_sensor(sensor, false);
618		data->ops->disable_sensor(sensor);
619	}
620
621	return 0;
622}
623
 
624static int hisi_thermal_suspend(struct device *dev)
625{
626	struct hisi_thermal_data *data = dev_get_drvdata(dev);
627	int i;
628
629	for (i = 0; i < data->nr_sensors; i++)
630		data->ops->disable_sensor(&data->sensor[i]);
631
632	return 0;
633}
634
635static int hisi_thermal_resume(struct device *dev)
636{
637	struct hisi_thermal_data *data = dev_get_drvdata(dev);
638	int i, ret = 0;
639
640	for (i = 0; i < data->nr_sensors; i++)
641		ret |= data->ops->enable_sensor(&data->sensor[i]);
642
643	return ret;
644}
 
645
646static DEFINE_SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
647			 hisi_thermal_suspend, hisi_thermal_resume);
648
649static struct platform_driver hisi_thermal_driver = {
650	.driver = {
651		.name		= "hisi_thermal",
652		.pm		= pm_sleep_ptr(&hisi_thermal_pm_ops),
653		.of_match_table = of_hisi_thermal_match,
654	},
655	.probe	= hisi_thermal_probe,
656	.remove	= hisi_thermal_remove,
657};
658
659module_platform_driver(hisi_thermal_driver);
660
661MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
662MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
663MODULE_DESCRIPTION("HiSilicon thermal driver");
664MODULE_LICENSE("GPL v2");