xref: /OK3568_Linux_fs/kernel/drivers/thermal/max77620_thermal.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Junction temperature thermal driver for Maxim Max77620.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2016, NVIDIA CORPORATION.  All rights reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Laxman Dewangan <ldewangan@nvidia.com>
8*4882a593Smuzhiyun  *	   Mallikarjun Kasoju <mkasoju@nvidia.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/irq.h>
12*4882a593Smuzhiyun #include <linux/interrupt.h>
13*4882a593Smuzhiyun #include <linux/mfd/max77620.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/of.h>
16*4882a593Smuzhiyun #include <linux/platform_device.h>
17*4882a593Smuzhiyun #include <linux/regmap.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/thermal.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define MAX77620_NORMAL_OPERATING_TEMP	100000
22*4882a593Smuzhiyun #define MAX77620_TJALARM1_TEMP		120000
23*4882a593Smuzhiyun #define MAX77620_TJALARM2_TEMP		140000
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun struct max77620_therm_info {
26*4882a593Smuzhiyun 	struct device			*dev;
27*4882a593Smuzhiyun 	struct regmap			*rmap;
28*4882a593Smuzhiyun 	struct thermal_zone_device	*tz_device;
29*4882a593Smuzhiyun 	int				irq_tjalarm1;
30*4882a593Smuzhiyun 	int				irq_tjalarm2;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun  * max77620_thermal_read_temp: Read PMIC die temperatue.
35*4882a593Smuzhiyun  * @data:	Device specific data.
36*4882a593Smuzhiyun  * @temp:	Temperature in millidegrees Celsius
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * The actual temperature of PMIC die is not available from PMIC.
39*4882a593Smuzhiyun  * PMIC only tells the status if it has crossed or not the threshold level
40*4882a593Smuzhiyun  * of 120degC or 140degC.
41*4882a593Smuzhiyun  * If threshold has not been crossed then assume die temperature as 100degC
42*4882a593Smuzhiyun  * else 120degC or 140deG based on the PMIC die temp threshold status.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * Return 0 on success otherwise error number to show reason of failure.
45*4882a593Smuzhiyun  */
46*4882a593Smuzhiyun 
max77620_thermal_read_temp(void * data,int * temp)47*4882a593Smuzhiyun static int max77620_thermal_read_temp(void *data, int *temp)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	struct max77620_therm_info *mtherm = data;
50*4882a593Smuzhiyun 	unsigned int val;
51*4882a593Smuzhiyun 	int ret;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	ret = regmap_read(mtherm->rmap, MAX77620_REG_STATLBT, &val);
54*4882a593Smuzhiyun 	if (ret < 0) {
55*4882a593Smuzhiyun 		dev_err(mtherm->dev, "Failed to read STATLBT: %d\n", ret);
56*4882a593Smuzhiyun 		return ret;
57*4882a593Smuzhiyun 	}
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	if (val & MAX77620_IRQ_TJALRM2_MASK)
60*4882a593Smuzhiyun 		*temp = MAX77620_TJALARM2_TEMP;
61*4882a593Smuzhiyun 	else if (val & MAX77620_IRQ_TJALRM1_MASK)
62*4882a593Smuzhiyun 		*temp = MAX77620_TJALARM1_TEMP;
63*4882a593Smuzhiyun 	else
64*4882a593Smuzhiyun 		*temp = MAX77620_NORMAL_OPERATING_TEMP;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	return 0;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun static const struct thermal_zone_of_device_ops max77620_thermal_ops = {
70*4882a593Smuzhiyun 	.get_temp = max77620_thermal_read_temp,
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
max77620_thermal_irq(int irq,void * data)73*4882a593Smuzhiyun static irqreturn_t max77620_thermal_irq(int irq, void *data)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	struct max77620_therm_info *mtherm = data;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (irq == mtherm->irq_tjalarm1)
78*4882a593Smuzhiyun 		dev_warn(mtherm->dev, "Junction Temp Alarm1(120C) occurred\n");
79*4882a593Smuzhiyun 	else if (irq == mtherm->irq_tjalarm2)
80*4882a593Smuzhiyun 		dev_crit(mtherm->dev, "Junction Temp Alarm2(140C) occurred\n");
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	thermal_zone_device_update(mtherm->tz_device,
83*4882a593Smuzhiyun 				   THERMAL_EVENT_UNSPECIFIED);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	return IRQ_HANDLED;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
max77620_thermal_probe(struct platform_device * pdev)88*4882a593Smuzhiyun static int max77620_thermal_probe(struct platform_device *pdev)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	struct max77620_therm_info *mtherm;
91*4882a593Smuzhiyun 	int ret;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	mtherm = devm_kzalloc(&pdev->dev, sizeof(*mtherm), GFP_KERNEL);
94*4882a593Smuzhiyun 	if (!mtherm)
95*4882a593Smuzhiyun 		return -ENOMEM;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	mtherm->irq_tjalarm1 = platform_get_irq(pdev, 0);
98*4882a593Smuzhiyun 	mtherm->irq_tjalarm2 = platform_get_irq(pdev, 1);
99*4882a593Smuzhiyun 	if ((mtherm->irq_tjalarm1 < 0) || (mtherm->irq_tjalarm2 < 0)) {
100*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Alarm irq number not available\n");
101*4882a593Smuzhiyun 		return -EINVAL;
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	mtherm->dev = &pdev->dev;
105*4882a593Smuzhiyun 	mtherm->rmap = dev_get_regmap(pdev->dev.parent, NULL);
106*4882a593Smuzhiyun 	if (!mtherm->rmap) {
107*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to get parent regmap\n");
108*4882a593Smuzhiyun 		return -ENODEV;
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	/*
112*4882a593Smuzhiyun 	 * The reference taken to the parent's node which will be balanced on
113*4882a593Smuzhiyun 	 * reprobe or on platform-device release.
114*4882a593Smuzhiyun 	 */
115*4882a593Smuzhiyun 	device_set_of_node_from_dev(&pdev->dev, pdev->dev.parent);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	mtherm->tz_device = devm_thermal_zone_of_sensor_register(&pdev->dev, 0,
118*4882a593Smuzhiyun 				mtherm, &max77620_thermal_ops);
119*4882a593Smuzhiyun 	if (IS_ERR(mtherm->tz_device)) {
120*4882a593Smuzhiyun 		ret = PTR_ERR(mtherm->tz_device);
121*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to register thermal zone: %d\n",
122*4882a593Smuzhiyun 			ret);
123*4882a593Smuzhiyun 		return ret;
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	ret = devm_request_threaded_irq(&pdev->dev, mtherm->irq_tjalarm1, NULL,
127*4882a593Smuzhiyun 					max77620_thermal_irq,
128*4882a593Smuzhiyun 					IRQF_ONESHOT | IRQF_SHARED,
129*4882a593Smuzhiyun 					dev_name(&pdev->dev), mtherm);
130*4882a593Smuzhiyun 	if (ret < 0) {
131*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to request irq1: %d\n", ret);
132*4882a593Smuzhiyun 		return ret;
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	ret = devm_request_threaded_irq(&pdev->dev, mtherm->irq_tjalarm2, NULL,
136*4882a593Smuzhiyun 					max77620_thermal_irq,
137*4882a593Smuzhiyun 					IRQF_ONESHOT | IRQF_SHARED,
138*4882a593Smuzhiyun 					dev_name(&pdev->dev), mtherm);
139*4882a593Smuzhiyun 	if (ret < 0) {
140*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to request irq2: %d\n", ret);
141*4882a593Smuzhiyun 		return ret;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	platform_set_drvdata(pdev, mtherm);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	return 0;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun static struct platform_device_id max77620_thermal_devtype[] = {
150*4882a593Smuzhiyun 	{ .name = "max77620-thermal", },
151*4882a593Smuzhiyun 	{},
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun MODULE_DEVICE_TABLE(platform, max77620_thermal_devtype);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun static struct platform_driver max77620_thermal_driver = {
156*4882a593Smuzhiyun 	.driver = {
157*4882a593Smuzhiyun 		.name = "max77620-thermal",
158*4882a593Smuzhiyun 	},
159*4882a593Smuzhiyun 	.probe = max77620_thermal_probe,
160*4882a593Smuzhiyun 	.id_table = max77620_thermal_devtype,
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun module_platform_driver(max77620_thermal_driver);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun MODULE_DESCRIPTION("Max77620 Junction temperature Thermal driver");
166*4882a593Smuzhiyun MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
167*4882a593Smuzhiyun MODULE_AUTHOR("Mallikarjun Kasoju <mkasoju@nvidia.com>");
168*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
169